xref: /titanic_51/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision 91822a6784e98553f726916a707eb87a0e0db110)
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  */
216e062f4aSMarcel Telka 
227c478bd9Sstevel@tonic-gate /*
233a36268eSMarcel Telka  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
24bffeae97SMarcel Telka  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25e36d7b11SSebastien Roy  * Copyright (c) 2012 by Delphix. All rights reserved.
2615721462SDaniil Lunev  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
307c478bd9Sstevel@tonic-gate  *	All Rights Reserved
317c478bd9Sstevel@tonic-gate  */
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>
39aa59c4cbSrsb #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>
595679c89fSjv227347 #include <sys/zone.h>
607c478bd9Sstevel@tonic-gate 
612f172c55SRobert Thurlow #include <fs/fs_reparse.h>
622f172c55SRobert Thurlow 
637c478bd9Sstevel@tonic-gate #include <rpc/types.h>
647c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
657c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
667c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
697c478bd9Sstevel@tonic-gate #include <nfs/export.h>
70b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <nfs/nfs_cmd.h>
717c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
727c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
757c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <inet/common.h>
787c478bd9Sstevel@tonic-gate #include <inet/ip.h>
797c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
807c478bd9Sstevel@tonic-gate 
8145916cd2Sjpk #include <sys/tsol/label.h>
8245916cd2Sjpk #include <sys/tsol/tndb.h>
8345916cd2Sjpk 
847c478bd9Sstevel@tonic-gate #define	RFS4_MAXLOCK_TRIES 4	/* Try to get the lock this many times */
857c478bd9Sstevel@tonic-gate static int rfs4_maxlock_tries = RFS4_MAXLOCK_TRIES;
867c478bd9Sstevel@tonic-gate #define	RFS4_LOCK_DELAY 10	/* Milliseconds */
877c478bd9Sstevel@tonic-gate static clock_t  rfs4_lock_delay = RFS4_LOCK_DELAY;
880a701b1eSRobert Gordon extern struct svc_ops rdma_svc_ops;
89c242f9a0Schunli zhang - Sun Microsystems - Irvine United States extern int nfs_loaned_buffers;
90c242f9a0Schunli zhang - Sun Microsystems - Irvine United States /* End of Tunables */
917c478bd9Sstevel@tonic-gate 
920a701b1eSRobert Gordon static int rdma_setup_read_data4(READ4args *, READ4res *);
930a701b1eSRobert Gordon 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Used to bump the stateid4.seqid value and show changes in the stateid
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #define	next_stateid(sp) (++(sp)->bits.chgseq)
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
1017c478bd9Sstevel@tonic-gate  *	This is used to return NFS4ERR_TOOSMALL when clients specify
1027c478bd9Sstevel@tonic-gate  *	maxcount that isn't large enough to hold the smallest possible
1037c478bd9Sstevel@tonic-gate  *	XDR encoded dirent.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  *	    sizeof cookie (8 bytes) +
1067c478bd9Sstevel@tonic-gate  *	    sizeof name_len (4 bytes) +
1077c478bd9Sstevel@tonic-gate  *	    sizeof smallest (padded) name (4 bytes) +
1087c478bd9Sstevel@tonic-gate  *	    sizeof bitmap4_len (12 bytes) +   NOTE: we always encode len=2 bm4
1097c478bd9Sstevel@tonic-gate  *	    sizeof attrlist4_len (4 bytes) +
1107c478bd9Sstevel@tonic-gate  *	    sizeof next boolean (4 bytes)
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
1137c478bd9Sstevel@tonic-gate  * the smallest possible entry4 (assumes no attrs requested).
1147c478bd9Sstevel@tonic-gate  *	sizeof nfsstat4 (4 bytes) +
1157c478bd9Sstevel@tonic-gate  *	sizeof verifier4 (8 bytes) +
1167c478bd9Sstevel@tonic-gate  *	sizeof entry4list bool (4 bytes) +
1177c478bd9Sstevel@tonic-gate  *	sizeof entry4 	(36 bytes) +
1187c478bd9Sstevel@tonic-gate  *	sizeof eof bool  (4 bytes)
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
1217c478bd9Sstevel@tonic-gate  *	VOP_READDIR.  Its value is the size of the maximum possible dirent
1227c478bd9Sstevel@tonic-gate  *	for solaris.  The DIRENT64_RECLEN macro returns	the size of dirent
1237c478bd9Sstevel@tonic-gate  *	required for a given name length.  MAXNAMELEN is the maximum
1247c478bd9Sstevel@tonic-gate  *	filename length allowed in Solaris.  The first two DIRENT64_RECLEN()
1257c478bd9Sstevel@tonic-gate  *	macros are to allow for . and .. entries -- just a minor tweak to try
1267c478bd9Sstevel@tonic-gate  *	and guarantee that buffer we give to VOP_READDIR will be large enough
1277c478bd9Sstevel@tonic-gate  *	to hold ., .., and the largest possible solaris dirent64.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_ENTRY4 36
1307c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
1317c478bd9Sstevel@tonic-gate #define	RFS4_MINLEN_RDDIR_BUF \
1327c478bd9Sstevel@tonic-gate 	(DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * It would be better to pad to 4 bytes since that's what XDR would do,
1367c478bd9Sstevel@tonic-gate  * but the dirents UFS gives us are already padded to 8, so just take
1377c478bd9Sstevel@tonic-gate  * what we're given.  Dircount is only a hint anyway.  Currently the
1387c478bd9Sstevel@tonic-gate  * solaris kernel is ASCII only, so there's no point in calling the
1397c478bd9Sstevel@tonic-gate  * UTF8 functions.
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  * dirent64: named padded to provide 8 byte struct alignment
1427c478bd9Sstevel@tonic-gate  *	d_ino(8) + d_off(8) + d_reclen(2) + d_name(namelen + null(1) + pad)
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * cookie: uint64_t   +  utf8namelen: uint_t  +   utf8name padded to 8 bytes
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate #define	DIRENT64_TO_DIRCOUNT(dp) \
1487c478bd9Sstevel@tonic-gate 	(3 * BYTES_PER_XDR_UNIT + DIRENT64_NAMELEN((dp)->d_reclen))
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate time_t rfs4_start_time;			/* Initialized in rfs4_srvrinit */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static sysid_t lockt_sysid;		/* dummy sysid for all LOCKT calls */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate u_longlong_t	nfs4_srv_caller_id;
1551b300de9Sjwahlig uint_t		nfs4_srv_vkey = 0;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate verifier4	Write4verf;
1587c478bd9Sstevel@tonic-gate verifier4	Readdir4verf;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate void	rfs4_init_compound_state(struct compound_state *);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static void	nullfree(caddr_t);
1637c478bd9Sstevel@tonic-gate static void	rfs4_op_inval(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1647c478bd9Sstevel@tonic-gate 			struct compound_state *);
1657c478bd9Sstevel@tonic-gate static void	rfs4_op_access(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1667c478bd9Sstevel@tonic-gate 			struct compound_state *);
1677c478bd9Sstevel@tonic-gate static void	rfs4_op_close(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1687c478bd9Sstevel@tonic-gate 			struct compound_state *);
1697c478bd9Sstevel@tonic-gate static void	rfs4_op_commit(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1707c478bd9Sstevel@tonic-gate 			struct compound_state *);
1717c478bd9Sstevel@tonic-gate static void	rfs4_op_create(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1727c478bd9Sstevel@tonic-gate 			struct compound_state *);
1737c478bd9Sstevel@tonic-gate static void	rfs4_op_create_free(nfs_resop4 *resop);
1747c478bd9Sstevel@tonic-gate static void	rfs4_op_delegreturn(nfs_argop4 *, nfs_resop4 *,
1757c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
176f3b585ceSsamf static void	rfs4_op_delegpurge(nfs_argop4 *, nfs_resop4 *,
177f3b585ceSsamf 			struct svc_req *, struct compound_state *);
1787c478bd9Sstevel@tonic-gate static void	rfs4_op_getattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1797c478bd9Sstevel@tonic-gate 			struct compound_state *);
1807c478bd9Sstevel@tonic-gate static void	rfs4_op_getattr_free(nfs_resop4 *);
1817c478bd9Sstevel@tonic-gate static void	rfs4_op_getfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1827c478bd9Sstevel@tonic-gate 			struct compound_state *);
1837c478bd9Sstevel@tonic-gate static void	rfs4_op_getfh_free(nfs_resop4 *);
1847c478bd9Sstevel@tonic-gate static void	rfs4_op_illegal(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1857c478bd9Sstevel@tonic-gate 			struct compound_state *);
1867c478bd9Sstevel@tonic-gate static void	rfs4_op_link(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1877c478bd9Sstevel@tonic-gate 			struct compound_state *);
1887c478bd9Sstevel@tonic-gate static void	rfs4_op_lock(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1897c478bd9Sstevel@tonic-gate 			struct compound_state *);
1907c478bd9Sstevel@tonic-gate static void	lock_denied_free(nfs_resop4 *);
1917c478bd9Sstevel@tonic-gate static void	rfs4_op_locku(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1927c478bd9Sstevel@tonic-gate 			struct compound_state *);
1937c478bd9Sstevel@tonic-gate static void	rfs4_op_lockt(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1947c478bd9Sstevel@tonic-gate 			struct compound_state *);
1957c478bd9Sstevel@tonic-gate static void	rfs4_op_lookup(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1967c478bd9Sstevel@tonic-gate 			struct compound_state *);
1977c478bd9Sstevel@tonic-gate static void	rfs4_op_lookupp(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
1987c478bd9Sstevel@tonic-gate 			struct compound_state *);
1997c478bd9Sstevel@tonic-gate static void	rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop,
2007c478bd9Sstevel@tonic-gate 				struct svc_req *req, struct compound_state *cs);
2017c478bd9Sstevel@tonic-gate static void	rfs4_op_nverify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2027c478bd9Sstevel@tonic-gate 			struct compound_state *);
2037c478bd9Sstevel@tonic-gate static void	rfs4_op_open(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2047c478bd9Sstevel@tonic-gate 			struct compound_state *);
2057c478bd9Sstevel@tonic-gate static void	rfs4_op_open_confirm(nfs_argop4 *, nfs_resop4 *,
2067c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2077c478bd9Sstevel@tonic-gate static void	rfs4_op_open_downgrade(nfs_argop4 *, nfs_resop4 *,
2087c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2097c478bd9Sstevel@tonic-gate static void	rfs4_op_putfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2107c478bd9Sstevel@tonic-gate 			struct compound_state *);
2117c478bd9Sstevel@tonic-gate static void	rfs4_op_putpubfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2127c478bd9Sstevel@tonic-gate 			struct compound_state *);
2137c478bd9Sstevel@tonic-gate static void	rfs4_op_putrootfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2147c478bd9Sstevel@tonic-gate 			struct compound_state *);
2157c478bd9Sstevel@tonic-gate static void	rfs4_op_read(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2167c478bd9Sstevel@tonic-gate 			struct compound_state *);
2177c478bd9Sstevel@tonic-gate static void	rfs4_op_read_free(nfs_resop4 *);
2187c478bd9Sstevel@tonic-gate static void	rfs4_op_readdir_free(nfs_resop4 *resop);
2197c478bd9Sstevel@tonic-gate static void	rfs4_op_readlink(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2207c478bd9Sstevel@tonic-gate 			struct compound_state *);
2217c478bd9Sstevel@tonic-gate static void	rfs4_op_readlink_free(nfs_resop4 *);
2227c478bd9Sstevel@tonic-gate static void	rfs4_op_release_lockowner(nfs_argop4 *, nfs_resop4 *,
2237c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2247c478bd9Sstevel@tonic-gate static void	rfs4_op_remove(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2257c478bd9Sstevel@tonic-gate 			struct compound_state *);
2267c478bd9Sstevel@tonic-gate static void	rfs4_op_rename(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2277c478bd9Sstevel@tonic-gate 			struct compound_state *);
2287c478bd9Sstevel@tonic-gate static void	rfs4_op_renew(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2297c478bd9Sstevel@tonic-gate 			struct compound_state *);
2307c478bd9Sstevel@tonic-gate static void	rfs4_op_restorefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2317c478bd9Sstevel@tonic-gate 			struct compound_state *);
2327c478bd9Sstevel@tonic-gate static void	rfs4_op_savefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2337c478bd9Sstevel@tonic-gate 			struct compound_state *);
2347c478bd9Sstevel@tonic-gate static void	rfs4_op_setattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2357c478bd9Sstevel@tonic-gate 			struct compound_state *);
2367c478bd9Sstevel@tonic-gate static void	rfs4_op_verify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2377c478bd9Sstevel@tonic-gate 			struct compound_state *);
2387c478bd9Sstevel@tonic-gate static void	rfs4_op_write(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2397c478bd9Sstevel@tonic-gate 			struct compound_state *);
2407c478bd9Sstevel@tonic-gate static void	rfs4_op_setclientid(nfs_argop4 *, nfs_resop4 *,
2417c478bd9Sstevel@tonic-gate 			struct svc_req *, struct compound_state *);
2427c478bd9Sstevel@tonic-gate static void	rfs4_op_setclientid_confirm(nfs_argop4 *, nfs_resop4 *,
2437c478bd9Sstevel@tonic-gate 			struct svc_req *req, struct compound_state *);
2447c478bd9Sstevel@tonic-gate static void	rfs4_op_secinfo(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
2457c478bd9Sstevel@tonic-gate 			struct compound_state *);
2467c478bd9Sstevel@tonic-gate static void	rfs4_op_secinfo_free(nfs_resop4 *);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static nfsstat4 check_open_access(uint32_t,
2497c478bd9Sstevel@tonic-gate 				struct compound_state *, struct svc_req *);
2507c478bd9Sstevel@tonic-gate nfsstat4 rfs4_client_sysid(rfs4_client_t *, sysid_t *);
2512f172c55SRobert Thurlow void rfs4_ss_clid(rfs4_client_t *);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * translation table for attrs
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate struct nfs4_ntov_table {
2577c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
2587c478bd9Sstevel@tonic-gate 	uint8_t amap[NFS4_MAXNUM_ATTRS];
2597c478bd9Sstevel@tonic-gate 	int attrcnt;
2607c478bd9Sstevel@tonic-gate 	bool_t vfsstat;
2617c478bd9Sstevel@tonic-gate };
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static void	nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp);
2647c478bd9Sstevel@tonic-gate static void	nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
2657c478bd9Sstevel@tonic-gate 				    struct nfs4_svgetit_arg *sargp);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static nfsstat4	do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp,
2687c478bd9Sstevel@tonic-gate 		    struct compound_state *cs, struct nfs4_svgetit_arg *sargp,
2697c478bd9Sstevel@tonic-gate 		    struct nfs4_ntov_table *ntovp, nfs4_attr_cmd_t cmd);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate fem_t		*deleg_rdops;
2727c478bd9Sstevel@tonic-gate fem_t		*deleg_wrops;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate rfs4_servinst_t *rfs4_cur_servinst = NULL;	/* current server instance */
2757c478bd9Sstevel@tonic-gate kmutex_t	rfs4_servinst_lock;	/* protects linked list */
2767c478bd9Sstevel@tonic-gate int		rfs4_seen_first_compound;	/* set first time we see one */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * NFS4 op dispatch table
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate struct rfsv4disp {
2837c478bd9Sstevel@tonic-gate 	void	(*dis_proc)();		/* proc to call */
2847c478bd9Sstevel@tonic-gate 	void	(*dis_resfree)();	/* frees space allocated by proc */
2857c478bd9Sstevel@tonic-gate 	int	dis_flags;		/* RPC_IDEMPOTENT, etc... */
2867c478bd9Sstevel@tonic-gate };
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static struct rfsv4disp rfsv4disptab[] = {
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * NFS VERSION 4
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/* RFS_NULL = 0 */
294c93d332cSDan McDonald 	{rfs4_op_illegal, nullfree, 0},
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* UNUSED = 1 */
297c93d332cSDan McDonald 	{rfs4_op_illegal, nullfree, 0},
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* UNUSED = 2 */
300c93d332cSDan McDonald 	{rfs4_op_illegal, nullfree, 0},
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* OP_ACCESS = 3 */
303c93d332cSDan McDonald 	{rfs4_op_access, nullfree, RPC_IDEMPOTENT},
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/* OP_CLOSE = 4 */
306c93d332cSDan McDonald 	{rfs4_op_close, nullfree, 0},
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/* OP_COMMIT = 5 */
309c93d332cSDan McDonald 	{rfs4_op_commit, nullfree, RPC_IDEMPOTENT},
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* OP_CREATE = 6 */
312c93d332cSDan McDonald 	{rfs4_op_create, nullfree, 0},
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* OP_DELEGPURGE = 7 */
315c93d332cSDan McDonald 	{rfs4_op_delegpurge, nullfree, 0},
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* OP_DELEGRETURN = 8 */
318c93d332cSDan McDonald 	{rfs4_op_delegreturn, nullfree, 0},
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* OP_GETATTR = 9 */
321c93d332cSDan McDonald 	{rfs4_op_getattr, rfs4_op_getattr_free, RPC_IDEMPOTENT},
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/* OP_GETFH = 10 */
324c93d332cSDan McDonald 	{rfs4_op_getfh, rfs4_op_getfh_free, RPC_ALL},
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* OP_LINK = 11 */
327c93d332cSDan McDonald 	{rfs4_op_link, nullfree, 0},
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* OP_LOCK = 12 */
330c93d332cSDan McDonald 	{rfs4_op_lock, lock_denied_free, 0},
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/* OP_LOCKT = 13 */
333c93d332cSDan McDonald 	{rfs4_op_lockt, lock_denied_free, 0},
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* OP_LOCKU = 14 */
336c93d332cSDan McDonald 	{rfs4_op_locku, nullfree, 0},
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* OP_LOOKUP = 15 */
339c93d332cSDan McDonald 	{rfs4_op_lookup, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)},
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* OP_LOOKUPP = 16 */
342c93d332cSDan McDonald 	{rfs4_op_lookupp, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)},
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* OP_NVERIFY = 17 */
345c93d332cSDan McDonald 	{rfs4_op_nverify, nullfree, RPC_IDEMPOTENT},
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/* OP_OPEN = 18 */
348c93d332cSDan McDonald 	{rfs4_op_open, rfs4_free_reply, 0},
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/* OP_OPENATTR = 19 */
351c93d332cSDan McDonald 	{rfs4_op_openattr, nullfree, 0},
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* OP_OPEN_CONFIRM = 20 */
354c93d332cSDan McDonald 	{rfs4_op_open_confirm, nullfree, 0},
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* OP_OPEN_DOWNGRADE = 21 */
357c93d332cSDan McDonald 	{rfs4_op_open_downgrade, nullfree, 0},
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/* OP_OPEN_PUTFH = 22 */
360c93d332cSDan McDonald 	{rfs4_op_putfh, nullfree, RPC_ALL},
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* OP_PUTPUBFH = 23 */
363c93d332cSDan McDonald 	{rfs4_op_putpubfh, nullfree, RPC_ALL},
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* OP_PUTROOTFH = 24 */
366c93d332cSDan McDonald 	{rfs4_op_putrootfh, nullfree, RPC_ALL},
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* OP_READ = 25 */
369c93d332cSDan McDonald 	{rfs4_op_read, rfs4_op_read_free, RPC_IDEMPOTENT},
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/* OP_READDIR = 26 */
372c93d332cSDan McDonald 	{rfs4_op_readdir, rfs4_op_readdir_free, RPC_IDEMPOTENT},
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/* OP_READLINK = 27 */
375c93d332cSDan McDonald 	{rfs4_op_readlink, rfs4_op_readlink_free, RPC_IDEMPOTENT},
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* OP_REMOVE = 28 */
378c93d332cSDan McDonald 	{rfs4_op_remove, nullfree, 0},
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* OP_RENAME = 29 */
381c93d332cSDan McDonald 	{rfs4_op_rename, nullfree, 0},
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/* OP_RENEW = 30 */
384c93d332cSDan McDonald 	{rfs4_op_renew, nullfree, 0},
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* OP_RESTOREFH = 31 */
387c93d332cSDan McDonald 	{rfs4_op_restorefh, nullfree, RPC_ALL},
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/* OP_SAVEFH = 32 */
390c93d332cSDan McDonald 	{rfs4_op_savefh, nullfree, RPC_ALL},
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* OP_SECINFO = 33 */
393c93d332cSDan McDonald 	{rfs4_op_secinfo, rfs4_op_secinfo_free, 0},
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/* OP_SETATTR = 34 */
396c93d332cSDan McDonald 	{rfs4_op_setattr, nullfree, 0},
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* OP_SETCLIENTID = 35 */
399c93d332cSDan McDonald 	{rfs4_op_setclientid, nullfree, 0},
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* OP_SETCLIENTID_CONFIRM = 36 */
402c93d332cSDan McDonald 	{rfs4_op_setclientid_confirm, nullfree, 0},
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/* OP_VERIFY = 37 */
405c93d332cSDan McDonald 	{rfs4_op_verify, nullfree, RPC_IDEMPOTENT},
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* OP_WRITE = 38 */
408c93d332cSDan McDonald 	{rfs4_op_write, nullfree, 0},
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* OP_RELEASE_LOCKOWNER = 39 */
411c93d332cSDan McDonald 	{rfs4_op_release_lockowner, nullfree, 0},
4127c478bd9Sstevel@tonic-gate };
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static uint_t rfsv4disp_cnt = sizeof (rfsv4disptab) / sizeof (rfsv4disptab[0]);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #define	OP_ILLEGAL_IDX (rfsv4disp_cnt)
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate #ifdef DEBUG
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate int		rfs4_fillone_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 
4732f172c55SRobert Thurlow extern void	rfs4_free_fs_locations4(fs_locations4 *);
4742f172c55SRobert Thurlow 
4757c478bd9Sstevel@tonic-gate #ifdef	nextdp
4767c478bd9Sstevel@tonic-gate #undef nextdp
4777c478bd9Sstevel@tonic-gate #endif
4787c478bd9Sstevel@tonic-gate #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static const fs_operation_def_t nfs4_rd_deleg_tmpl[] = {
481cfae96c2Sjwahlig 	VOPNAME_OPEN,		{ .femop_open = deleg_rd_open },
482cfae96c2Sjwahlig 	VOPNAME_WRITE,		{ .femop_write = deleg_rd_write },
483cfae96c2Sjwahlig 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_rd_setattr },
484aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_rd_rwlock },
485cfae96c2Sjwahlig 	VOPNAME_SPACE,		{ .femop_space = deleg_rd_space },
486cfae96c2Sjwahlig 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_rd_setsecattr },
487cfae96c2Sjwahlig 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_rd_vnevent },
4887c478bd9Sstevel@tonic-gate 	NULL,			NULL
4897c478bd9Sstevel@tonic-gate };
4907c478bd9Sstevel@tonic-gate static const fs_operation_def_t nfs4_wr_deleg_tmpl[] = {
491cfae96c2Sjwahlig 	VOPNAME_OPEN,		{ .femop_open = deleg_wr_open },
492cfae96c2Sjwahlig 	VOPNAME_READ,		{ .femop_read = deleg_wr_read },
493cfae96c2Sjwahlig 	VOPNAME_WRITE,		{ .femop_write = deleg_wr_write },
494cfae96c2Sjwahlig 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_wr_setattr },
495aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_wr_rwlock },
496cfae96c2Sjwahlig 	VOPNAME_SPACE,		{ .femop_space = deleg_wr_space },
497cfae96c2Sjwahlig 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_wr_setsecattr },
498cfae96c2Sjwahlig 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_wr_vnevent },
4997c478bd9Sstevel@tonic-gate 	NULL,			NULL
5007c478bd9Sstevel@tonic-gate };
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate int
5037c478bd9Sstevel@tonic-gate rfs4_srvrinit(void)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	timespec32_t verf;
5067c478bd9Sstevel@tonic-gate 	int error;
5077c478bd9Sstevel@tonic-gate 	extern void rfs4_attr_init();
5087c478bd9Sstevel@tonic-gate 	extern krwlock_t rfs4_deleg_policy_lock;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * The following algorithm attempts to find a unique verifier
5127c478bd9Sstevel@tonic-gate 	 * to be used as the write verifier returned from the server
5137c478bd9Sstevel@tonic-gate 	 * to the client.  It is important that this verifier change
5147c478bd9Sstevel@tonic-gate 	 * whenever the server reboots.  Of secondary importance, it
5157c478bd9Sstevel@tonic-gate 	 * is important for the verifier to be unique between two
5167c478bd9Sstevel@tonic-gate 	 * different servers.
5177c478bd9Sstevel@tonic-gate 	 *
5187c478bd9Sstevel@tonic-gate 	 * Thus, an attempt is made to use the system hostid and the
5197c478bd9Sstevel@tonic-gate 	 * current time in seconds when the nfssrv kernel module is
5207c478bd9Sstevel@tonic-gate 	 * loaded.  It is assumed that an NFS server will not be able
5217c478bd9Sstevel@tonic-gate 	 * to boot and then to reboot in less than a second.  If the
5227c478bd9Sstevel@tonic-gate 	 * hostid has not been set, then the current high resolution
5237c478bd9Sstevel@tonic-gate 	 * time is used.  This will ensure different verifiers each
5247c478bd9Sstevel@tonic-gate 	 * time the server reboots and minimize the chances that two
5257c478bd9Sstevel@tonic-gate 	 * different servers will have the same verifier.
5267c478bd9Sstevel@tonic-gate 	 * XXX - this is broken on LP64 kernels.
5277c478bd9Sstevel@tonic-gate 	 */
5285679c89fSjv227347 	verf.tv_sec = (time_t)zone_get_hostid(NULL);
5297c478bd9Sstevel@tonic-gate 	if (verf.tv_sec != 0) {
5307c478bd9Sstevel@tonic-gate 		verf.tv_nsec = gethrestime_sec();
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		timespec_t tverf;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		gethrestime(&tverf);
5357c478bd9Sstevel@tonic-gate 		verf.tv_sec = (time_t)tverf.tv_sec;
5367c478bd9Sstevel@tonic-gate 		verf.tv_nsec = tverf.tv_nsec;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	Write4verf = *(uint64_t *)&verf;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	rfs4_attr_init();
5427c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_deleg_lock, NULL, MUTEX_DEFAULT, NULL);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/* Used to manage create/destroy of server state */
5457c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_state_lock, NULL, MUTEX_DEFAULT, NULL);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/* Used to manage access to server instance linked list */
5487c478bd9Sstevel@tonic-gate 	mutex_init(&rfs4_servinst_lock, NULL, MUTEX_DEFAULT, NULL);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/* Used to manage access to rfs4_deleg_policy */
5517c478bd9Sstevel@tonic-gate 	rw_init(&rfs4_deleg_policy_lock, NULL, RW_DEFAULT, NULL);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	error = fem_create("deleg_rdops", nfs4_rd_deleg_tmpl, &deleg_rdops);
5547c478bd9Sstevel@tonic-gate 	if (error != 0) {
5557c478bd9Sstevel@tonic-gate 		rfs4_disable_delegation();
5567c478bd9Sstevel@tonic-gate 	} else {
5577c478bd9Sstevel@tonic-gate 		error = fem_create("deleg_wrops", nfs4_wr_deleg_tmpl,
5587c478bd9Sstevel@tonic-gate 		    &deleg_wrops);
5597c478bd9Sstevel@tonic-gate 		if (error != 0) {
5607c478bd9Sstevel@tonic-gate 			rfs4_disable_delegation();
5617c478bd9Sstevel@tonic-gate 			fem_free(deleg_rdops);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	nfs4_srv_caller_id = fs_new_caller_id();
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	lockt_sysid = lm_alloc_sysidt();
5687c478bd9Sstevel@tonic-gate 
5691b300de9Sjwahlig 	vsd_create(&nfs4_srv_vkey, NULL);
5701b300de9Sjwahlig 
5717c478bd9Sstevel@tonic-gate 	return (0);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate void
5757c478bd9Sstevel@tonic-gate rfs4_srvrfini(void)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	extern krwlock_t rfs4_deleg_policy_lock;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (lockt_sysid != LM_NOSYSID) {
5807c478bd9Sstevel@tonic-gate 		lm_free_sysidt(lockt_sysid);
5817c478bd9Sstevel@tonic-gate 		lockt_sysid = LM_NOSYSID;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	mutex_destroy(&rfs4_deleg_lock);
5857c478bd9Sstevel@tonic-gate 	mutex_destroy(&rfs4_state_lock);
5867c478bd9Sstevel@tonic-gate 	rw_destroy(&rfs4_deleg_policy_lock);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	fem_free(deleg_rdops);
5897c478bd9Sstevel@tonic-gate 	fem_free(deleg_wrops);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate void
5937c478bd9Sstevel@tonic-gate rfs4_init_compound_state(struct compound_state *cs)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	bzero(cs, sizeof (*cs));
5967c478bd9Sstevel@tonic-gate 	cs->cont = TRUE;
5977c478bd9Sstevel@tonic-gate 	cs->access = CS_ACCESS_DENIED;
5987c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
5997c478bd9Sstevel@tonic-gate 	cs->mandlock = FALSE;
6007c478bd9Sstevel@tonic-gate 	cs->fh.nfs_fh4_val = cs->fhbuf;
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate void
6047c478bd9Sstevel@tonic-gate rfs4_grace_start(rfs4_servinst_t *sip)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_WRITER);
607d3d50737SRafael Vanoni 	sip->start_time = (time_t)TICK_TO_SEC(ddi_get_lbolt());
6087c478bd9Sstevel@tonic-gate 	sip->grace_period = rfs4_grace_period;
6097c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * returns true if the instance's grace period has never been started
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate int
6167c478bd9Sstevel@tonic-gate rfs4_servinst_grace_new(rfs4_servinst_t *sip)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	time_t start_time;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_READER);
6217c478bd9Sstevel@tonic-gate 	start_time = sip->start_time;
6227c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	return (start_time == 0);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * Indicates if server instance is within the
6297c478bd9Sstevel@tonic-gate  * grace period.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate int
6327c478bd9Sstevel@tonic-gate rfs4_servinst_in_grace(rfs4_servinst_t *sip)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	time_t grace_expiry;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	rw_enter(&sip->rwlock, RW_READER);
6377c478bd9Sstevel@tonic-gate 	grace_expiry = sip->start_time + sip->grace_period;
6387c478bd9Sstevel@tonic-gate 	rw_exit(&sip->rwlock);
6397c478bd9Sstevel@tonic-gate 
640d3d50737SRafael Vanoni 	return (((time_t)TICK_TO_SEC(ddi_get_lbolt())) < grace_expiry);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate int
6447c478bd9Sstevel@tonic-gate rfs4_clnt_in_grace(rfs4_client_t *cp)
6457c478bd9Sstevel@tonic-gate {
646d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
6477c478bd9Sstevel@tonic-gate 
648d216dff5SRobert Mastors 	return (rfs4_servinst_in_grace(cp->rc_server_instance));
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * reset all currently active grace periods
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate void
6557c478bd9Sstevel@tonic-gate rfs4_grace_reset_all(void)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
660cee86682Scalum 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
661cee86682Scalum 		if (rfs4_servinst_in_grace(sip))
6627c478bd9Sstevel@tonic-gate 			rfs4_grace_start(sip);
6637c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * start any new instances' grace periods
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate void
6707c478bd9Sstevel@tonic-gate rfs4_grace_start_new(void)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
675cee86682Scalum 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
6767c478bd9Sstevel@tonic-gate 		if (rfs4_servinst_grace_new(sip))
6777c478bd9Sstevel@tonic-gate 			rfs4_grace_start(sip);
6787c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
679cee86682Scalum }
6807c478bd9Sstevel@tonic-gate 
681cee86682Scalum static rfs4_dss_path_t *
682cee86682Scalum rfs4_dss_newpath(rfs4_servinst_t *sip, char *path, unsigned index)
683cee86682Scalum {
684cee86682Scalum 	size_t len;
685cee86682Scalum 	rfs4_dss_path_t *dss_path;
686cee86682Scalum 
687cee86682Scalum 	dss_path = kmem_alloc(sizeof (rfs4_dss_path_t), KM_SLEEP);
688cee86682Scalum 
689cee86682Scalum 	/*
690cee86682Scalum 	 * Take a copy of the string, since the original may be overwritten.
691cee86682Scalum 	 * Sadly, no strdup() in the kernel.
692cee86682Scalum 	 */
693cee86682Scalum 	/* allow for NUL */
694cee86682Scalum 	len = strlen(path) + 1;
695cee86682Scalum 	dss_path->path = kmem_alloc(len, KM_SLEEP);
696cee86682Scalum 	(void) strlcpy(dss_path->path, path, len);
697cee86682Scalum 
698cee86682Scalum 	/* associate with servinst */
699cee86682Scalum 	dss_path->sip = sip;
700cee86682Scalum 	dss_path->index = index;
701cee86682Scalum 
702cee86682Scalum 	/*
703cee86682Scalum 	 * Add to list of served paths.
704cee86682Scalum 	 * No locking required, as we're only ever called at startup.
705cee86682Scalum 	 */
706cee86682Scalum 	if (rfs4_dss_pathlist == NULL) {
707cee86682Scalum 		/* this is the first dss_path_t */
708cee86682Scalum 
709cee86682Scalum 		/* needed for insque/remque */
710cee86682Scalum 		dss_path->next = dss_path->prev = dss_path;
711cee86682Scalum 
712cee86682Scalum 		rfs4_dss_pathlist = dss_path;
713cee86682Scalum 	} else {
714cee86682Scalum 		insque(dss_path, rfs4_dss_pathlist);
715cee86682Scalum 	}
716cee86682Scalum 
717cee86682Scalum 	return (dss_path);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate  * Create a new server instance, and make it the currently active instance.
7227c478bd9Sstevel@tonic-gate  * Note that starting the grace period too early will reduce the clients'
7237c478bd9Sstevel@tonic-gate  * recovery window.
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate void
726cee86682Scalum rfs4_servinst_create(int start_grace, int dss_npaths, char **dss_paths)
7277c478bd9Sstevel@tonic-gate {
728cee86682Scalum 	unsigned i;
7297c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip;
730cee86682Scalum 	rfs4_oldstate_t *oldstate;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	sip = kmem_alloc(sizeof (rfs4_servinst_t), KM_SLEEP);
7337c478bd9Sstevel@tonic-gate 	rw_init(&sip->rwlock, NULL, RW_DEFAULT, NULL);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	sip->start_time = (time_t)0;
7367c478bd9Sstevel@tonic-gate 	sip->grace_period = (time_t)0;
7377c478bd9Sstevel@tonic-gate 	sip->next = NULL;
7387c478bd9Sstevel@tonic-gate 	sip->prev = NULL;
7397c478bd9Sstevel@tonic-gate 
740cee86682Scalum 	rw_init(&sip->oldstate_lock, NULL, RW_DEFAULT, NULL);
741cee86682Scalum 	/*
742cee86682Scalum 	 * This initial dummy entry is required to setup for insque/remque.
743cee86682Scalum 	 * It must be skipped over whenever the list is traversed.
744cee86682Scalum 	 */
745cee86682Scalum 	oldstate = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
746cee86682Scalum 	/* insque/remque require initial list entry to be self-terminated */
747cee86682Scalum 	oldstate->next = oldstate;
748cee86682Scalum 	oldstate->prev = oldstate;
749cee86682Scalum 	sip->oldstate = oldstate;
750cee86682Scalum 
751cee86682Scalum 
752cee86682Scalum 	sip->dss_npaths = dss_npaths;
753cee86682Scalum 	sip->dss_paths = kmem_alloc(dss_npaths *
754cee86682Scalum 	    sizeof (rfs4_dss_path_t *), KM_SLEEP);
755cee86682Scalum 
756cee86682Scalum 	for (i = 0; i < dss_npaths; i++) {
757cee86682Scalum 		sip->dss_paths[i] = rfs4_dss_newpath(sip, dss_paths[i], i);
758cee86682Scalum 	}
759cee86682Scalum 
7607c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
761cee86682Scalum 	if (rfs4_cur_servinst != NULL) {
7627c478bd9Sstevel@tonic-gate 		/* add to linked list */
7637c478bd9Sstevel@tonic-gate 		sip->prev = rfs4_cur_servinst;
7647c478bd9Sstevel@tonic-gate 		rfs4_cur_servinst->next = sip;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 	if (start_grace)
7677c478bd9Sstevel@tonic-gate 		rfs4_grace_start(sip);
7687c478bd9Sstevel@tonic-gate 	/* make the new instance "current" */
7697c478bd9Sstevel@tonic-gate 	rfs4_cur_servinst = sip;
7707c478bd9Sstevel@tonic-gate 
771cee86682Scalum 	mutex_exit(&rfs4_servinst_lock);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * In future, we might add a rfs4_servinst_destroy(sip) but, for now, destroy
7767c478bd9Sstevel@tonic-gate  * all instances directly.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate void
7797c478bd9Sstevel@tonic-gate rfs4_servinst_destroy_all(void)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	rfs4_servinst_t *sip, *prev, *current;
7827c478bd9Sstevel@tonic-gate #ifdef DEBUG
7837c478bd9Sstevel@tonic-gate 	int n = 0;
7847c478bd9Sstevel@tonic-gate #endif
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
7877c478bd9Sstevel@tonic-gate 	ASSERT(rfs4_cur_servinst != NULL);
7887c478bd9Sstevel@tonic-gate 	current = rfs4_cur_servinst;
7897c478bd9Sstevel@tonic-gate 	rfs4_cur_servinst = NULL;
7907c478bd9Sstevel@tonic-gate 	for (sip = current; sip != NULL; sip = prev) {
7917c478bd9Sstevel@tonic-gate 		prev = sip->prev;
7927c478bd9Sstevel@tonic-gate 		rw_destroy(&sip->rwlock);
793cee86682Scalum 		if (sip->oldstate)
794cee86682Scalum 			kmem_free(sip->oldstate, sizeof (rfs4_oldstate_t));
795cee86682Scalum 		if (sip->dss_paths)
796cee86682Scalum 			kmem_free(sip->dss_paths,
797cee86682Scalum 			    sip->dss_npaths * sizeof (rfs4_dss_path_t *));
7987c478bd9Sstevel@tonic-gate 		kmem_free(sip, sizeof (rfs4_servinst_t));
7997c478bd9Sstevel@tonic-gate #ifdef DEBUG
8007c478bd9Sstevel@tonic-gate 		n++;
8017c478bd9Sstevel@tonic-gate #endif
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * Assign the current server instance to a client_t.
808d216dff5SRobert Mastors  * Should be called with cp->rc_dbe held.
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate void
8117c478bd9Sstevel@tonic-gate rfs4_servinst_assign(rfs4_client_t *cp, rfs4_servinst_t *sip)
8127c478bd9Sstevel@tonic-gate {
813d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/*
8167c478bd9Sstevel@tonic-gate 	 * The lock ensures that if the current instance is in the process
8177c478bd9Sstevel@tonic-gate 	 * of changing, we will see the new one.
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	mutex_enter(&rfs4_servinst_lock);
820d216dff5SRobert Mastors 	cp->rc_server_instance = sip;
8217c478bd9Sstevel@tonic-gate 	mutex_exit(&rfs4_servinst_lock);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate rfs4_servinst_t *
8257c478bd9Sstevel@tonic-gate rfs4_servinst(rfs4_client_t *cp)
8267c478bd9Sstevel@tonic-gate {
827d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
8287c478bd9Sstevel@tonic-gate 
829d216dff5SRobert Mastors 	return (cp->rc_server_instance);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /* ARGSUSED */
8337c478bd9Sstevel@tonic-gate static void
8347c478bd9Sstevel@tonic-gate nullfree(caddr_t resop)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * This is a fall-through for invalid or not implemented (yet) ops
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate /* ARGSUSED */
8427c478bd9Sstevel@tonic-gate static void
8437c478bd9Sstevel@tonic-gate rfs4_op_inval(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
8447c478bd9Sstevel@tonic-gate     struct compound_state *cs)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	*cs->statusp = *((nfsstat4 *)&(resop)->nfs_resop4_u) = NFS4ERR_INVAL;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * Check if the security flavor, nfsnum, is in the flavor_list.
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate bool_t
8537c478bd9Sstevel@tonic-gate in_flavor_list(int nfsnum, int *flavor_list, int count)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate 	int i;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
8587c478bd9Sstevel@tonic-gate 		if (nfsnum == flavor_list[i])
8597c478bd9Sstevel@tonic-gate 			return (TRUE);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 	return (FALSE);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate  * Used by rfs4_op_secinfo to get the security information from the
8667c478bd9Sstevel@tonic-gate  * export structure associated with the component.
8677c478bd9Sstevel@tonic-gate  */
8687c478bd9Sstevel@tonic-gate /* ARGSUSED */
8697c478bd9Sstevel@tonic-gate static nfsstat4
8707c478bd9Sstevel@tonic-gate do_rfs4_op_secinfo(struct compound_state *cs, char *nm, SECINFO4res *resp)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	int error, different_export = 0;
8736e062f4aSMarcel Telka 	vnode_t *dvp, *vp;
8747c478bd9Sstevel@tonic-gate 	struct exportinfo *exi = NULL;
875fbd2e8e1SArne Jansen 	struct exportinfo *oexi = NULL;
8767c478bd9Sstevel@tonic-gate 	fid_t fid;
8777c478bd9Sstevel@tonic-gate 	uint_t count, i;
8787c478bd9Sstevel@tonic-gate 	secinfo4 *resok_val;
8797c478bd9Sstevel@tonic-gate 	struct secinfo *secp;
8801b300de9Sjwahlig 	seconfig_t *si;
881bffeae97SMarcel Telka 	bool_t did_traverse = FALSE;
8827c478bd9Sstevel@tonic-gate 	int dotdot, walk;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
8857c478bd9Sstevel@tonic-gate 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * If dotdotting, then need to check whether it's above the
8897c478bd9Sstevel@tonic-gate 	 * root of a filesystem, or above an export point.
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	if (dotdot) {
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		/*
8947c478bd9Sstevel@tonic-gate 		 * If dotdotting at the root of a filesystem, then
8957c478bd9Sstevel@tonic-gate 		 * need to traverse back to the mounted-on filesystem
8967c478bd9Sstevel@tonic-gate 		 * and do the dotdot lookup there.
8977c478bd9Sstevel@tonic-gate 		 */
8987c478bd9Sstevel@tonic-gate 		if (cs->vp->v_flag & VROOT) {
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			/*
9017c478bd9Sstevel@tonic-gate 			 * If at the system root, then can
9027c478bd9Sstevel@tonic-gate 			 * go up no further.
9037c478bd9Sstevel@tonic-gate 			 */
9047c478bd9Sstevel@tonic-gate 			if (VN_CMP(dvp, rootdir))
9057c478bd9Sstevel@tonic-gate 				return (puterrno4(ENOENT));
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 			/*
9087c478bd9Sstevel@tonic-gate 			 * Traverse back to the mounted-on filesystem
9097c478bd9Sstevel@tonic-gate 			 */
9107c478bd9Sstevel@tonic-gate 			dvp = untraverse(cs->vp);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			/*
9137c478bd9Sstevel@tonic-gate 			 * Set the different_export flag so we remember
9147c478bd9Sstevel@tonic-gate 			 * to pick up a new exportinfo entry for
9157c478bd9Sstevel@tonic-gate 			 * this new filesystem.
9167c478bd9Sstevel@tonic-gate 			 */
9177c478bd9Sstevel@tonic-gate 			different_export = 1;
9187c478bd9Sstevel@tonic-gate 		} else {
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 			/*
9217c478bd9Sstevel@tonic-gate 			 * If dotdotting above an export point then set
9227c478bd9Sstevel@tonic-gate 			 * the different_export to get new export info.
9237c478bd9Sstevel@tonic-gate 			 */
9247c478bd9Sstevel@tonic-gate 			different_export = nfs_exported(cs->exi, cs->vp);
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Get the vnode for the component "nm".
9307c478bd9Sstevel@tonic-gate 	 */
931da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cs->cr,
932da6c28aaSamw 	    NULL, NULL, NULL);
9337c478bd9Sstevel@tonic-gate 	if (error)
9347c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/*
9377c478bd9Sstevel@tonic-gate 	 * If the vnode is in a pseudo filesystem, or if the security flavor
9387c478bd9Sstevel@tonic-gate 	 * used in the request is valid but not an explicitly shared flavor,
9397c478bd9Sstevel@tonic-gate 	 * or the access bit indicates that this is a limited access,
9407c478bd9Sstevel@tonic-gate 	 * check whether this vnode is visible.
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	if (!different_export &&
9437c478bd9Sstevel@tonic-gate 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
9447c478bd9Sstevel@tonic-gate 	    cs->access & CS_ACCESS_LIMITED)) {
9457c478bd9Sstevel@tonic-gate 		if (! nfs_visible(cs->exi, vp, &different_export)) {
9467c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9477c478bd9Sstevel@tonic-gate 			return (puterrno4(ENOENT));
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * If it's a mountpoint, then traverse it.
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(vp)) {
9556e062f4aSMarcel Telka 		if ((error = traverse(&vp)) != 0) {
9567c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9577c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 		/* remember that we had to traverse mountpoint */
9607c478bd9Sstevel@tonic-gate 		did_traverse = TRUE;
9617c478bd9Sstevel@tonic-gate 		different_export = 1;
9627c478bd9Sstevel@tonic-gate 	} else if (vp->v_vfsp != dvp->v_vfsp) {
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * If vp isn't a mountpoint and the vfs ptrs aren't the same,
9657c478bd9Sstevel@tonic-gate 		 * then vp is probably an LOFS object.  We don't need the
9667c478bd9Sstevel@tonic-gate 		 * realvp, we just need to know that we might have crossed
967fbd2e8e1SArne Jansen 		 * a server fs boundary and need to call checkexport.
9687c478bd9Sstevel@tonic-gate 		 * (LOFS lookup hides server fs mountpoints, and actually calls
9697c478bd9Sstevel@tonic-gate 		 * traverse)
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		different_export = 1;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * Get the export information for it.
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	if (different_export) {
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 		bzero(&fid, sizeof (fid));
9807c478bd9Sstevel@tonic-gate 		fid.fid_len = MAXFIDSZ;
9817c478bd9Sstevel@tonic-gate 		error = vop_fid_pseudo(vp, &fid);
9827c478bd9Sstevel@tonic-gate 		if (error) {
9837c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
9847c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		if (dotdot)
988fbd2e8e1SArne Jansen 			oexi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
9897c478bd9Sstevel@tonic-gate 		else
990fbd2e8e1SArne Jansen 			oexi = checkexport(&vp->v_vfsp->vfs_fsid, &fid, vp);
9917c478bd9Sstevel@tonic-gate 
992fbd2e8e1SArne Jansen 		if (oexi == NULL) {
9937c478bd9Sstevel@tonic-gate 			if (did_traverse == TRUE) {
9947c478bd9Sstevel@tonic-gate 				/*
9957c478bd9Sstevel@tonic-gate 				 * If this vnode is a mounted-on vnode,
9967c478bd9Sstevel@tonic-gate 				 * but the mounted-on file system is not
9977c478bd9Sstevel@tonic-gate 				 * exported, send back the secinfo for
9987c478bd9Sstevel@tonic-gate 				 * the exported node that the mounted-on
9997c478bd9Sstevel@tonic-gate 				 * vnode lives in.
10007c478bd9Sstevel@tonic-gate 				 */
10017c478bd9Sstevel@tonic-gate 				exi = cs->exi;
10027c478bd9Sstevel@tonic-gate 			} else {
10037c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
10047c478bd9Sstevel@tonic-gate 				return (puterrno4(EACCES));
10057c478bd9Sstevel@tonic-gate 			}
1006fbd2e8e1SArne Jansen 		} else {
1007fbd2e8e1SArne Jansen 			exi = oexi;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 	} else {
10107c478bd9Sstevel@tonic-gate 		exi = cs->exi;
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	ASSERT(exi != NULL);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * Create the secinfo result based on the security information
10177c478bd9Sstevel@tonic-gate 	 * from the exportinfo structure (exi).
10187c478bd9Sstevel@tonic-gate 	 *
10197c478bd9Sstevel@tonic-gate 	 * Return all flavors for a pseudo node.
10207c478bd9Sstevel@tonic-gate 	 * For a real export node, return the flavor that the client
10217c478bd9Sstevel@tonic-gate 	 * has access with.
10227c478bd9Sstevel@tonic-gate 	 */
1023fbd2e8e1SArne Jansen 	rw_enter(&exported_lock, RW_READER);
10247c478bd9Sstevel@tonic-gate 	if (PSEUDO(exi)) {
10257c478bd9Sstevel@tonic-gate 		count = exi->exi_export.ex_seccnt; /* total sec count */
10267c478bd9Sstevel@tonic-gate 		resok_val = kmem_alloc(count * sizeof (secinfo4), KM_SLEEP);
10277c478bd9Sstevel@tonic-gate 		secp = exi->exi_export.ex_secinfo;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
10301b300de9Sjwahlig 			si = &secp[i].s_secinfo;
10311b300de9Sjwahlig 			resok_val[i].flavor = si->sc_rpcnum;
10327c478bd9Sstevel@tonic-gate 			if (resok_val[i].flavor == RPCSEC_GSS) {
10337c478bd9Sstevel@tonic-gate 				rpcsec_gss_info *info;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 				info = &resok_val[i].flavor_info;
10361b300de9Sjwahlig 				info->qop = si->sc_qop;
10371b300de9Sjwahlig 				info->service = (rpc_gss_svc_t)si->sc_service;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 				/* get oid opaque data */
10407c478bd9Sstevel@tonic-gate 				info->oid.sec_oid4_len =
10411b300de9Sjwahlig 				    si->sc_gss_mech_type->length;
10421b300de9Sjwahlig 				info->oid.sec_oid4_val = kmem_alloc(
10431b300de9Sjwahlig 				    si->sc_gss_mech_type->length, KM_SLEEP);
10441b300de9Sjwahlig 				bcopy(
10451b300de9Sjwahlig 				    si->sc_gss_mech_type->elements,
10461b300de9Sjwahlig 				    info->oid.sec_oid4_val,
10471b300de9Sjwahlig 				    info->oid.sec_oid4_len);
10487c478bd9Sstevel@tonic-gate 			}
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_len = count;
10517c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_val = resok_val;
10527c478bd9Sstevel@tonic-gate 	} else {
10537c478bd9Sstevel@tonic-gate 		int ret_cnt = 0, k = 0;
10547c478bd9Sstevel@tonic-gate 		int *flavor_list;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		count = exi->exi_export.ex_seccnt; /* total sec count */
10577c478bd9Sstevel@tonic-gate 		secp = exi->exi_export.ex_secinfo;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 		flavor_list = kmem_alloc(count * sizeof (int), KM_SLEEP);
10607c478bd9Sstevel@tonic-gate 		/* find out which flavors to return */
10617c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i ++) {
10627c478bd9Sstevel@tonic-gate 			int access, flavor, perm;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 			flavor = secp[i].s_secinfo.sc_nfsnum;
10657c478bd9Sstevel@tonic-gate 			perm = secp[i].s_flags;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 			access = nfsauth4_secinfo_access(exi, cs->req,
10685cb0d679SMarcel Telka 			    flavor, perm, cs->basecr);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 			if (! (access & NFSAUTH_DENIED) &&
10717c478bd9Sstevel@tonic-gate 			    ! (access & NFSAUTH_WRONGSEC)) {
10727c478bd9Sstevel@tonic-gate 				flavor_list[ret_cnt] = flavor;
10737c478bd9Sstevel@tonic-gate 				ret_cnt++;
10747c478bd9Sstevel@tonic-gate 			}
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		/* Create the returning SECINFO value */
10787c478bd9Sstevel@tonic-gate 		resok_val = kmem_alloc(ret_cnt * sizeof (secinfo4), KM_SLEEP);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
1081f3b585ceSsamf 			/*
1082f3b585ceSsamf 			 * If the flavor is in the flavor list,
1083f3b585ceSsamf 			 * fill in resok_val.
1084f3b585ceSsamf 			 */
10851b300de9Sjwahlig 			si = &secp[i].s_secinfo;
10861b300de9Sjwahlig 			if (in_flavor_list(si->sc_nfsnum,
10877c478bd9Sstevel@tonic-gate 			    flavor_list, ret_cnt)) {
10881b300de9Sjwahlig 				resok_val[k].flavor = si->sc_rpcnum;
10897c478bd9Sstevel@tonic-gate 				if (resok_val[k].flavor == RPCSEC_GSS) {
10907c478bd9Sstevel@tonic-gate 					rpcsec_gss_info *info;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 					info = &resok_val[k].flavor_info;
10931b300de9Sjwahlig 					info->qop = si->sc_qop;
10941b300de9Sjwahlig 					info->service = (rpc_gss_svc_t)
10951b300de9Sjwahlig 					    si->sc_service;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 					/* get oid opaque data */
10987c478bd9Sstevel@tonic-gate 					info->oid.sec_oid4_len =
10991b300de9Sjwahlig 					    si->sc_gss_mech_type->length;
11001b300de9Sjwahlig 					info->oid.sec_oid4_val = kmem_alloc(
11011b300de9Sjwahlig 					    si->sc_gss_mech_type->length,
11027c478bd9Sstevel@tonic-gate 					    KM_SLEEP);
11031b300de9Sjwahlig 					bcopy(si->sc_gss_mech_type->elements,
11041b300de9Sjwahlig 					    info->oid.sec_oid4_val,
11051b300de9Sjwahlig 					    info->oid.sec_oid4_len);
11067c478bd9Sstevel@tonic-gate 				}
11077c478bd9Sstevel@tonic-gate 				k++;
11087c478bd9Sstevel@tonic-gate 			}
11097c478bd9Sstevel@tonic-gate 			if (k >= ret_cnt)
11107c478bd9Sstevel@tonic-gate 				break;
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_len = ret_cnt;
11137c478bd9Sstevel@tonic-gate 		resp->SECINFO4resok_val = resok_val;
11147c478bd9Sstevel@tonic-gate 		kmem_free(flavor_list, count * sizeof (int));
11157c478bd9Sstevel@tonic-gate 	}
1116fbd2e8e1SArne Jansen 	rw_exit(&exported_lock);
1117aafcd32bSMarcel Telka 	if (oexi != NULL)
1118fbd2e8e1SArne Jansen 		exi_rele(oexi);
1119aafcd32bSMarcel Telka 
11207c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
11217c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate /*
11257c478bd9Sstevel@tonic-gate  * SECINFO (Operation 33): Obtain required security information on
11267c478bd9Sstevel@tonic-gate  * the component name in the format of (security-mechanism-oid, qop, service)
11277c478bd9Sstevel@tonic-gate  * triplets.
11287c478bd9Sstevel@tonic-gate  */
11297c478bd9Sstevel@tonic-gate /* ARGSUSED */
11307c478bd9Sstevel@tonic-gate static void
11317c478bd9Sstevel@tonic-gate rfs4_op_secinfo(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
11327c478bd9Sstevel@tonic-gate     struct compound_state *cs)
11337c478bd9Sstevel@tonic-gate {
1134f3b585ceSsamf 	SECINFO4args *args = &argop->nfs_argop4_u.opsecinfo;
11357c478bd9Sstevel@tonic-gate 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
1136f3b585ceSsamf 	utf8string *utfnm = &args->name;
11377c478bd9Sstevel@tonic-gate 	uint_t len;
11387c478bd9Sstevel@tonic-gate 	char *nm;
1139b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
1140b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
114115721462SDaniil Lunev 	nfsstat4 status = NFS4_OK;
11427c478bd9Sstevel@tonic-gate 
1143f3b585ceSsamf 	DTRACE_NFSV4_2(op__secinfo__start, struct compound_state *, cs,
1144f3b585ceSsamf 	    SECINFO4args *, args);
1145f3b585ceSsamf 
11467c478bd9Sstevel@tonic-gate 	/*
11477c478bd9Sstevel@tonic-gate 	 * Current file handle (cfh) should have been set before getting
11487c478bd9Sstevel@tonic-gate 	 * into this function. If not, return error.
11497c478bd9Sstevel@tonic-gate 	 */
11507c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
11517c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1152f3b585ceSsamf 		goto out;
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
11567c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
1157f3b585ceSsamf 		goto out;
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * Verify the component name. If failed, error out, but
11627c478bd9Sstevel@tonic-gate 	 * do not error out if the component name is a "..".
11637c478bd9Sstevel@tonic-gate 	 * SECINFO will return its parents secinfo data for SECINFO "..".
11647c478bd9Sstevel@tonic-gate 	 */
116515721462SDaniil Lunev 	status = utf8_dir_verify(utfnm);
116615721462SDaniil Lunev 	if (status != NFS4_OK) {
11677c478bd9Sstevel@tonic-gate 		if (utfnm->utf8string_len != 2 ||
11687c478bd9Sstevel@tonic-gate 		    utfnm->utf8string_val[0] != '.' ||
11697c478bd9Sstevel@tonic-gate 		    utfnm->utf8string_val[1] != '.') {
117015721462SDaniil Lunev 			*cs->statusp = resp->status = status;
1171f3b585ceSsamf 			goto out;
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	nm = utf8_to_str(utfnm, &len, NULL);
11767c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
11777c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1178f3b585ceSsamf 		goto out;
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
11827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
11837c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
1184f3b585ceSsamf 		goto out;
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 
1187b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1188b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
1189b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
11907c478bd9Sstevel@tonic-gate 
1191b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
1192b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1193b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, len);
1194b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
1195b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
1196b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1197b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1198b89a8333Snatalie li - Sun Microsystems - Irvine United States 	*cs->statusp = resp->status = do_rfs4_op_secinfo(cs, name, resp);
1199b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1200b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name != nm)
1201b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
12027c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
1203f3b585ceSsamf 
1204f3b585ceSsamf out:
1205f3b585ceSsamf 	DTRACE_NFSV4_2(op__secinfo__done, struct compound_state *, cs,
1206f3b585ceSsamf 	    SECINFO4res *, resp);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * Free SECINFO result.
12117c478bd9Sstevel@tonic-gate  */
12127c478bd9Sstevel@tonic-gate /* ARGSUSED */
12137c478bd9Sstevel@tonic-gate static void
12147c478bd9Sstevel@tonic-gate rfs4_op_secinfo_free(nfs_resop4 *resop)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
12177c478bd9Sstevel@tonic-gate 	int count, i;
12187c478bd9Sstevel@tonic-gate 	secinfo4 *resok_val;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	/* If this is not an Ok result, nothing to free. */
12217c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
12227c478bd9Sstevel@tonic-gate 		return;
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	count = resp->SECINFO4resok_len;
12267c478bd9Sstevel@tonic-gate 	resok_val = resp->SECINFO4resok_val;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
12297c478bd9Sstevel@tonic-gate 		if (resok_val[i].flavor == RPCSEC_GSS) {
12307c478bd9Sstevel@tonic-gate 			rpcsec_gss_info *info;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			info = &resok_val[i].flavor_info;
12331b300de9Sjwahlig 			kmem_free(info->oid.sec_oid4_val,
12341b300de9Sjwahlig 			    info->oid.sec_oid4_len);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 	kmem_free(resok_val, count * sizeof (secinfo4));
12387c478bd9Sstevel@tonic-gate 	resp->SECINFO4resok_len = 0;
12397c478bd9Sstevel@tonic-gate 	resp->SECINFO4resok_val = NULL;
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /* ARGSUSED */
12437c478bd9Sstevel@tonic-gate static void
12447c478bd9Sstevel@tonic-gate rfs4_op_access(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
12457c478bd9Sstevel@tonic-gate     struct compound_state *cs)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	ACCESS4args *args = &argop->nfs_argop4_u.opaccess;
12487c478bd9Sstevel@tonic-gate 	ACCESS4res *resp = &resop->nfs_resop4_u.opaccess;
12497c478bd9Sstevel@tonic-gate 	int error;
12507c478bd9Sstevel@tonic-gate 	vnode_t *vp;
12517c478bd9Sstevel@tonic-gate 	struct vattr va;
12527c478bd9Sstevel@tonic-gate 	int checkwriteperm;
12537c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
125445916cd2Sjpk 	bslabel_t *clabel, *slabel;
125545916cd2Sjpk 	ts_label_t *tslabel;
125645916cd2Sjpk 	boolean_t admin_low_client;
12577c478bd9Sstevel@tonic-gate 
1258f3b585ceSsamf 	DTRACE_NFSV4_2(op__access__start, struct compound_state *, cs,
1259f3b585ceSsamf 	    ACCESS4args *, args);
1260f3b585ceSsamf 
12617c478bd9Sstevel@tonic-gate #if 0	/* XXX allow access even if !cs->access. Eventually only pseudo fs */
12627c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
12637c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1264f3b585ceSsamf 		goto out;
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate #endif
12677c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
12687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1269f3b585ceSsamf 		goto out;
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	vp = cs->vp;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	/*
12777c478bd9Sstevel@tonic-gate 	 * If the file system is exported read only, it is not appropriate
12787c478bd9Sstevel@tonic-gate 	 * to check write permissions for regular files and directories.
12797c478bd9Sstevel@tonic-gate 	 * Special files are interpreted by the client, so the underlying
12807c478bd9Sstevel@tonic-gate 	 * permissions are sent back to the client for interpretation.
12817c478bd9Sstevel@tonic-gate 	 */
12825cb0d679SMarcel Telka 	if (rdonly4(req, cs) &&
12837c478bd9Sstevel@tonic-gate 	    (vp->v_type == VREG || vp->v_type == VDIR))
12847c478bd9Sstevel@tonic-gate 		checkwriteperm = 0;
12857c478bd9Sstevel@tonic-gate 	else
12867c478bd9Sstevel@tonic-gate 		checkwriteperm = 1;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * XXX
12907c478bd9Sstevel@tonic-gate 	 * We need the mode so that we can correctly determine access
12917c478bd9Sstevel@tonic-gate 	 * permissions relative to a mandatory lock file.  Access to
12927c478bd9Sstevel@tonic-gate 	 * mandatory lock files is denied on the server, so it might
12937c478bd9Sstevel@tonic-gate 	 * as well be reflected to the server during the open.
12947c478bd9Sstevel@tonic-gate 	 */
12957c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE;
1296da6c28aaSamw 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
12977c478bd9Sstevel@tonic-gate 	if (error) {
12987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
1299f3b585ceSsamf 		goto out;
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	resp->access = 0;
13027c478bd9Sstevel@tonic-gate 	resp->supported = 0;
13037c478bd9Sstevel@tonic-gate 
130445916cd2Sjpk 	if (is_system_labeled()) {
130545916cd2Sjpk 		ASSERT(req->rq_label != NULL);
130645916cd2Sjpk 		clabel = req->rq_label;
130745916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opaccess__clabel, char *,
130845916cd2Sjpk 		    "got client label from request(1)",
130945916cd2Sjpk 		    struct svc_req *, req);
131045916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1311bd6f1640SJarrett Lu 			if ((tslabel = nfs_getflabel(vp, cs->exi)) == NULL) {
131245916cd2Sjpk 				*cs->statusp = resp->status = puterrno4(EACCES);
1313f3b585ceSsamf 				goto out;
131445916cd2Sjpk 			}
131545916cd2Sjpk 			slabel = label2bslabel(tslabel);
131645916cd2Sjpk 			DTRACE_PROBE3(tx__rfs4__log__info__opaccess__slabel,
131745916cd2Sjpk 			    char *, "got server label(1) for vp(2)",
131845916cd2Sjpk 			    bslabel_t *, slabel, vnode_t *, vp);
131945916cd2Sjpk 
132045916cd2Sjpk 			admin_low_client = B_FALSE;
132145916cd2Sjpk 		} else
132245916cd2Sjpk 			admin_low_client = B_TRUE;
132345916cd2Sjpk 	}
132445916cd2Sjpk 
13257c478bd9Sstevel@tonic-gate 	if (args->access & ACCESS4_READ) {
1326da6c28aaSamw 		error = VOP_ACCESS(vp, VREAD, 0, cr, NULL);
132745916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
132845916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
132945916cd2Sjpk 		    bldominates(clabel, slabel)))
13307c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_READ;
13317c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_READ;
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 	if ((args->access & ACCESS4_LOOKUP) && vp->v_type == VDIR) {
1334da6c28aaSamw 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
133545916cd2Sjpk 		if (!error && (!is_system_labeled() || admin_low_client ||
133645916cd2Sjpk 		    bldominates(clabel, slabel)))
13377c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_LOOKUP;
13387c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_LOOKUP;
13397c478bd9Sstevel@tonic-gate 	}
13407c478bd9Sstevel@tonic-gate 	if (checkwriteperm &&
13417c478bd9Sstevel@tonic-gate 	    (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND))) {
1342da6c28aaSamw 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
134345916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
134445916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
134545916cd2Sjpk 		    blequal(clabel, slabel)))
13467c478bd9Sstevel@tonic-gate 			resp->access |=
13477c478bd9Sstevel@tonic-gate 			    (args->access & (ACCESS4_MODIFY | ACCESS4_EXTEND));
1348e4359d72SDaniil Lunev 		resp->supported |=
1349e4359d72SDaniil Lunev 		    resp->access & (ACCESS4_MODIFY | ACCESS4_EXTEND);
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	if (checkwriteperm &&
13537c478bd9Sstevel@tonic-gate 	    (args->access & ACCESS4_DELETE) && vp->v_type == VDIR) {
1354da6c28aaSamw 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
135545916cd2Sjpk 		if (!error && (!is_system_labeled() || admin_low_client ||
135645916cd2Sjpk 		    blequal(clabel, slabel)))
13577c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_DELETE;
13587c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_DELETE;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 	if (args->access & ACCESS4_EXECUTE && vp->v_type != VDIR) {
1361da6c28aaSamw 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
136245916cd2Sjpk 		if (!error && !MANDLOCK(vp, va.va_mode) &&
136345916cd2Sjpk 		    (!is_system_labeled() || admin_low_client ||
136445916cd2Sjpk 		    bldominates(clabel, slabel)))
13657c478bd9Sstevel@tonic-gate 			resp->access |= ACCESS4_EXECUTE;
13667c478bd9Sstevel@tonic-gate 		resp->supported |= ACCESS4_EXECUTE;
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
136945916cd2Sjpk 	if (is_system_labeled() && !admin_low_client)
137045916cd2Sjpk 		label_rele(tslabel);
137145916cd2Sjpk 
13727c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
1373f3b585ceSsamf out:
1374f3b585ceSsamf 	DTRACE_NFSV4_2(op__access__done, struct compound_state *, cs,
1375f3b585ceSsamf 	    ACCESS4res *, resp);
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate /* ARGSUSED */
13797c478bd9Sstevel@tonic-gate static void
13807c478bd9Sstevel@tonic-gate rfs4_op_commit(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
13817c478bd9Sstevel@tonic-gate     struct compound_state *cs)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	COMMIT4args *args = &argop->nfs_argop4_u.opcommit;
13847c478bd9Sstevel@tonic-gate 	COMMIT4res *resp = &resop->nfs_resop4_u.opcommit;
13857c478bd9Sstevel@tonic-gate 	int error;
13867c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
13877c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
13887c478bd9Sstevel@tonic-gate 	vattr_t va;
13897c478bd9Sstevel@tonic-gate 
1390f3b585ceSsamf 	DTRACE_NFSV4_2(op__commit__start, struct compound_state *, cs,
1391f3b585ceSsamf 	    COMMIT4args *, args);
1392f3b585ceSsamf 
13937c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
13947c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1395f3b585ceSsamf 		goto out;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
13987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1399f3b585ceSsamf 		goto out;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if (args->offset + args->count < args->offset) {
14037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1404f3b585ceSsamf 		goto out;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	va.va_mask = AT_UID;
1408da6c28aaSamw 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
14127c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
14137c478bd9Sstevel@tonic-gate 	 */
14147c478bd9Sstevel@tonic-gate 	if (error) {
14157c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
1416f3b585ceSsamf 		goto out;
14177c478bd9Sstevel@tonic-gate 	}
14185cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
14197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
1420f3b585ceSsamf 		goto out;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
14247c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR)
14257c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_ISDIR;
14267c478bd9Sstevel@tonic-gate 		else
14277c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_INVAL;
14287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
1429f3b585ceSsamf 		goto out;
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != va.va_uid &&
1433da6c28aaSamw 	    (error = VOP_ACCESS(vp, VWRITE, 0, cs->cr, NULL))) {
14347c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
1435f3b585ceSsamf 		goto out;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
1438f63200e6SJeff A. Smith 	error = VOP_FSYNC(vp, FSYNC, cr, NULL);
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (error) {
14417c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
1442f3b585ceSsamf 		goto out;
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
14467c478bd9Sstevel@tonic-gate 	resp->writeverf = Write4verf;
1447f3b585ceSsamf out:
1448f3b585ceSsamf 	DTRACE_NFSV4_2(op__commit__done, struct compound_state *, cs,
1449f3b585ceSsamf 	    COMMIT4res *, resp);
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate /*
14537c478bd9Sstevel@tonic-gate  * rfs4_op_mknod is called from rfs4_op_create after all initial verification
14547c478bd9Sstevel@tonic-gate  * was completed. It does the nfsv4 create for special files.
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate /* ARGSUSED */
14577c478bd9Sstevel@tonic-gate static vnode_t *
14587c478bd9Sstevel@tonic-gate do_rfs4_op_mknod(CREATE4args *args, CREATE4res *resp, struct svc_req *req,
14597c478bd9Sstevel@tonic-gate     struct compound_state *cs, vattr_t *vap, char *nm)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	int error;
14627c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
14637c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
14647c478bd9Sstevel@tonic-gate 	vnode_t *vp = NULL;
14657c478bd9Sstevel@tonic-gate 	int mode;
14667c478bd9Sstevel@tonic-gate 	enum vcexcl excl;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	switch (args->type) {
14697c478bd9Sstevel@tonic-gate 	case NF4CHR:
14707c478bd9Sstevel@tonic-gate 	case NF4BLK:
14717c478bd9Sstevel@tonic-gate 		if (secpolicy_sys_devices(cr) != 0) {
14727c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_PERM;
14737c478bd9Sstevel@tonic-gate 			return (NULL);
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 		if (args->type == NF4CHR)
14767c478bd9Sstevel@tonic-gate 			vap->va_type = VCHR;
14777c478bd9Sstevel@tonic-gate 		else
14787c478bd9Sstevel@tonic-gate 			vap->va_type = VBLK;
14797c478bd9Sstevel@tonic-gate 		vap->va_rdev = makedevice(args->ftype4_u.devdata.specdata1,
14807c478bd9Sstevel@tonic-gate 		    args->ftype4_u.devdata.specdata2);
14817c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_RDEV;
14827c478bd9Sstevel@tonic-gate 		break;
14837c478bd9Sstevel@tonic-gate 	case NF4SOCK:
14847c478bd9Sstevel@tonic-gate 		vap->va_type = VSOCK;
14857c478bd9Sstevel@tonic-gate 		break;
14867c478bd9Sstevel@tonic-gate 	case NF4FIFO:
14877c478bd9Sstevel@tonic-gate 		vap->va_type = VFIFO;
14887c478bd9Sstevel@tonic-gate 		break;
14897c478bd9Sstevel@tonic-gate 	default:
14907c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
14917c478bd9Sstevel@tonic-gate 		return (NULL);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	/*
14957c478bd9Sstevel@tonic-gate 	 * Must specify the mode.
14967c478bd9Sstevel@tonic-gate 	 */
14977c478bd9Sstevel@tonic-gate 	if (!(vap->va_mask & AT_MODE)) {
14987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
14997c478bd9Sstevel@tonic-gate 		return (NULL);
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	excl = EXCL;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	mode = 0;
15057c478bd9Sstevel@tonic-gate 
1506da6c28aaSamw 	error = VOP_CREATE(dvp, nm, vap, excl, mode, &vp, cr, 0, NULL, NULL);
15077c478bd9Sstevel@tonic-gate 	if (error) {
15087c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
15097c478bd9Sstevel@tonic-gate 		return (NULL);
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	return (vp);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /*
15157c478bd9Sstevel@tonic-gate  * nfsv4 create is used to create non-regular files. For regular files,
15167c478bd9Sstevel@tonic-gate  * use nfsv4 open.
15177c478bd9Sstevel@tonic-gate  */
15187c478bd9Sstevel@tonic-gate /* ARGSUSED */
15197c478bd9Sstevel@tonic-gate static void
15207c478bd9Sstevel@tonic-gate rfs4_op_create(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
15217c478bd9Sstevel@tonic-gate     struct compound_state *cs)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate 	CREATE4args *args = &argop->nfs_argop4_u.opcreate;
15247c478bd9Sstevel@tonic-gate 	CREATE4res *resp = &resop->nfs_resop4_u.opcreate;
15257c478bd9Sstevel@tonic-gate 	int error;
15267c478bd9Sstevel@tonic-gate 	struct vattr bva, iva, iva2, ava, *vap;
15277c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
15287c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
15297c478bd9Sstevel@tonic-gate 	vnode_t *vp = NULL;
1530fd7da618Sgt29601 	vnode_t *realvp;
15317c478bd9Sstevel@tonic-gate 	char *nm, *lnm;
15327c478bd9Sstevel@tonic-gate 	uint_t len, llen;
15337c478bd9Sstevel@tonic-gate 	int syncval = 0;
15347c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
15357c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
15367c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
15377c478bd9Sstevel@tonic-gate 	nfsstat4 status;
1538b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
1539b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
1540b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *lname = NULL;
15417c478bd9Sstevel@tonic-gate 
1542f3b585ceSsamf 	DTRACE_NFSV4_2(op__create__start, struct compound_state *, cs,
1543f3b585ceSsamf 	    CREATE4args *, args);
1544f3b585ceSsamf 
15457c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
15487c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1549f3b585ceSsamf 		goto out;
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	/*
15537c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
15547c478bd9Sstevel@tonic-gate 	 * do not allow to create an object in this directory.
15557c478bd9Sstevel@tonic-gate 	 */
15567c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
15577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1558f3b585ceSsamf 		goto out;
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	/* Verify that type is correct */
15627c478bd9Sstevel@tonic-gate 	switch (args->type) {
15637c478bd9Sstevel@tonic-gate 	case NF4LNK:
15647c478bd9Sstevel@tonic-gate 	case NF4BLK:
15657c478bd9Sstevel@tonic-gate 	case NF4CHR:
15667c478bd9Sstevel@tonic-gate 	case NF4SOCK:
15677c478bd9Sstevel@tonic-gate 	case NF4FIFO:
15687c478bd9Sstevel@tonic-gate 	case NF4DIR:
15697c478bd9Sstevel@tonic-gate 		break;
15707c478bd9Sstevel@tonic-gate 	default:
15717c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
1572f3b585ceSsamf 		goto out;
15737c478bd9Sstevel@tonic-gate 	};
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
15767c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1577f3b585ceSsamf 		goto out;
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
15807c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
1581f3b585ceSsamf 		goto out;
15827c478bd9Sstevel@tonic-gate 	}
158315721462SDaniil Lunev 	status = utf8_dir_verify(&args->objname);
158415721462SDaniil Lunev 	if (status != NFS4_OK) {
158515721462SDaniil Lunev 		*cs->statusp = resp->status = status;
1586f3b585ceSsamf 		goto out;
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15895cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
15907c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
1591f3b585ceSsamf 		goto out;
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * Name of newly created object
15967c478bd9Sstevel@tonic-gate 	 */
15977c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->objname, &len, NULL);
15987c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
15997c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1600f3b585ceSsamf 		goto out;
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
16047c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
16057c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
1606f3b585ceSsamf 		goto out;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
1609b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1610b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
1611b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
1612b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1613b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
1614b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1615b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, len);
1616b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
1617b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
1618b89a8333Snatalie li - Sun Microsystems - Irvine United States 
16197c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
16222f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
16237c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	status = do_rfs4_set_attrs(&resp->attrset,
16261b300de9Sjwahlig 	    &args->createattrs, cs, &sarg, &ntov, NFS4ATTR_SETIT);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (sarg.vap->va_mask == 0 && status == NFS4_OK)
16297c478bd9Sstevel@tonic-gate 		status = NFS4ERR_INVAL;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
16327c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
163359fb210bSJan Kryl 		if (name != nm)
163459fb210bSJan Kryl 			kmem_free(name, MAXPATHLEN + 1);
16357c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
16367c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
16377c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
1638f3b585ceSsamf 		goto out;
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
1642e3c57d6aSGerald Thornbrugh 	bva.va_mask = AT_CTIME|AT_SEQ|AT_MODE;
1643da6c28aaSamw 	error = VOP_GETATTR(dvp, &bva, 0, cr, NULL);
16447c478bd9Sstevel@tonic-gate 	if (error) {
16457c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
164659fb210bSJan Kryl 		if (name != nm)
164759fb210bSJan Kryl 			kmem_free(name, MAXPATHLEN + 1);
16487c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
16497c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
16507c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
1651f3b585ceSsamf 		goto out;
16527c478bd9Sstevel@tonic-gate 	}
16537c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bva.va_ctime)
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	vap = sarg.vap;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	/*
1658e3c57d6aSGerald Thornbrugh 	 * Set the default initial values for attributes when the parent
1659e3c57d6aSGerald Thornbrugh 	 * directory does not have the VSUID/VSGID bit set and they have
1660e3c57d6aSGerald Thornbrugh 	 * not been specified in createattrs.
16617c478bd9Sstevel@tonic-gate 	 */
1662e3c57d6aSGerald Thornbrugh 	if (!(bva.va_mode & VSUID) && (vap->va_mask & AT_UID) == 0) {
16637c478bd9Sstevel@tonic-gate 		vap->va_uid = crgetuid(cr);
16647c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_UID;
16657c478bd9Sstevel@tonic-gate 	}
1666e3c57d6aSGerald Thornbrugh 	if (!(bva.va_mode & VSGID) && (vap->va_mask & AT_GID) == 0) {
16677c478bd9Sstevel@tonic-gate 		vap->va_gid = crgetgid(cr);
16687c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_GID;
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	vap->va_mask |= AT_TYPE;
16727c478bd9Sstevel@tonic-gate 	switch (args->type) {
16737c478bd9Sstevel@tonic-gate 	case NF4DIR:
16747c478bd9Sstevel@tonic-gate 		vap->va_type = VDIR;
16757c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
16767c478bd9Sstevel@tonic-gate 			vap->va_mode = 0700;	/* default: owner rwx only */
16777c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
16787c478bd9Sstevel@tonic-gate 		}
167959fb210bSJan Kryl 		error = VOP_MKDIR(dvp, name, vap, &vp, cr, NULL, 0, NULL);
16807c478bd9Sstevel@tonic-gate 		if (error)
16817c478bd9Sstevel@tonic-gate 			break;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		/*
16847c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
16857c478bd9Sstevel@tonic-gate 		 * set to zero
16867c478bd9Sstevel@tonic-gate 		 */
16877c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
1688da6c28aaSamw 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
16897c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
16907c478bd9Sstevel@tonic-gate 		break;
16917c478bd9Sstevel@tonic-gate 	case NF4LNK:
16927c478bd9Sstevel@tonic-gate 		vap->va_type = VLNK;
16937c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
16947c478bd9Sstevel@tonic-gate 			vap->va_mode = 0700;	/* default: owner rwx only */
16957c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		/*
16997c478bd9Sstevel@tonic-gate 		 * symlink names must be treated as data
17007c478bd9Sstevel@tonic-gate 		 */
1701bbe876c0SMarcel Telka 		lnm = utf8_to_str((utf8string *)&args->ftype4_u.linkdata,
1702bbe876c0SMarcel Telka 		    &llen, NULL);
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		if (lnm == NULL) {
17057c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_INVAL;
1706b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (name != nm)
1707b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
17087c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
17097c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
17107c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
1711f3b585ceSsamf 			goto out;
17127c478bd9Sstevel@tonic-gate 		}
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 		if (llen > MAXPATHLEN) {
17157c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1716b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (name != nm)
1717b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
1718b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(nm, len);
1719b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(lnm, llen);
1720b89a8333Snatalie li - Sun Microsystems - Irvine United States 			nfs4_ntov_table_free(&ntov, &sarg);
1721b89a8333Snatalie li - Sun Microsystems - Irvine United States 			resp->attrset = 0;
1722b89a8333Snatalie li - Sun Microsystems - Irvine United States 			goto out;
1723b89a8333Snatalie li - Sun Microsystems - Irvine United States 		}
1724b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1725b89a8333Snatalie li - Sun Microsystems - Irvine United States 		lname = nfscmd_convname(ca, cs->exi, lnm,
1726b89a8333Snatalie li - Sun Microsystems - Irvine United States 		    NFSCMD_CONV_INBOUND, MAXPATHLEN  + 1);
1727b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1728b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (lname == NULL) {
1729b89a8333Snatalie li - Sun Microsystems - Irvine United States 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
1730b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (name != nm)
1731b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
17327c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
17337c478bd9Sstevel@tonic-gate 			kmem_free(lnm, llen);
17347c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
17357c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
1736f3b585ceSsamf 			goto out;
17377c478bd9Sstevel@tonic-gate 		}
17387c478bd9Sstevel@tonic-gate 
173959fb210bSJan Kryl 		error = VOP_SYMLINK(dvp, name, vap, lname, cr, NULL, 0);
1740b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (lname != lnm)
1741b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(lname, MAXPATHLEN + 1);
17427c478bd9Sstevel@tonic-gate 		kmem_free(lnm, llen);
17437c478bd9Sstevel@tonic-gate 		if (error)
17447c478bd9Sstevel@tonic-gate 			break;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		/*
17477c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
17487c478bd9Sstevel@tonic-gate 		 * set to zero
17497c478bd9Sstevel@tonic-gate 		 */
17507c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
1751da6c28aaSamw 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
17527c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
17537c478bd9Sstevel@tonic-gate 
175459fb210bSJan Kryl 		error = VOP_LOOKUP(dvp, name, &vp, NULL, 0, NULL, cr,
1755da6c28aaSamw 		    NULL, NULL, NULL);
17567c478bd9Sstevel@tonic-gate 		if (error)
17577c478bd9Sstevel@tonic-gate 			break;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 		/*
17607c478bd9Sstevel@tonic-gate 		 * va_seq is not safe over VOP calls, check it again
17617c478bd9Sstevel@tonic-gate 		 * if it has changed zero out iva to force atomic = FALSE.
17627c478bd9Sstevel@tonic-gate 		 */
17637c478bd9Sstevel@tonic-gate 		iva2.va_mask = AT_SEQ;
1764da6c28aaSamw 		if (VOP_GETATTR(dvp, &iva2, 0, cs->cr, NULL) ||
17657c478bd9Sstevel@tonic-gate 		    iva2.va_seq != iva.va_seq)
17667c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
17677c478bd9Sstevel@tonic-gate 		break;
17687c478bd9Sstevel@tonic-gate 	default:
17697c478bd9Sstevel@tonic-gate 		/*
17707c478bd9Sstevel@tonic-gate 		 * probably a special file.
17717c478bd9Sstevel@tonic-gate 		 */
17727c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
17737c478bd9Sstevel@tonic-gate 			vap->va_mode = 0600;	/* default: owner rw only */
17747c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
17757c478bd9Sstevel@tonic-gate 		}
17767c478bd9Sstevel@tonic-gate 		syncval = FNODSYNC;
17777c478bd9Sstevel@tonic-gate 		/*
17787c478bd9Sstevel@tonic-gate 		 * We know this will only generate one VOP call
17797c478bd9Sstevel@tonic-gate 		 */
178059fb210bSJan Kryl 		vp = do_rfs4_op_mknod(args, resp, req, cs, vap, name);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		if (vp == NULL) {
1783b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (name != nm)
1784b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
17857c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
17867c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
17877c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
1788f3b585ceSsamf 			goto out;
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		/*
17927c478bd9Sstevel@tonic-gate 		 * Get the initial "after" sequence number, if it fails,
17937c478bd9Sstevel@tonic-gate 		 * set to zero
17947c478bd9Sstevel@tonic-gate 		 */
17957c478bd9Sstevel@tonic-gate 		iva.va_mask = AT_SEQ;
1796da6c28aaSamw 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
17977c478bd9Sstevel@tonic-gate 			iva.va_seq = 0;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 		break;
18007c478bd9Sstevel@tonic-gate 	}
1801b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name != nm)
1802b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
18037c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	if (error) {
18067c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/*
18107c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
18117c478bd9Sstevel@tonic-gate 	 */
1812da6c28aaSamw 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
18157c478bd9Sstevel@tonic-gate 		if (vp != NULL)
18167c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
18177c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
18187c478bd9Sstevel@tonic-gate 		resp->attrset = 0;
1819f3b585ceSsamf 		goto out;
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * Finish setup of cinfo response, "before" value already set.
18247c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
18257c478bd9Sstevel@tonic-gate 	 * before value.
18267c478bd9Sstevel@tonic-gate 	 */
18277c478bd9Sstevel@tonic-gate 	ava.va_mask = AT_CTIME|AT_SEQ;
1828da6c28aaSamw 	if (VOP_GETATTR(dvp, &ava, 0, cr, NULL)) {
18297c478bd9Sstevel@tonic-gate 		ava.va_ctime = bva.va_ctime;
18307c478bd9Sstevel@tonic-gate 		ava.va_seq = 0;
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, ava.va_ctime);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/*
18357c478bd9Sstevel@tonic-gate 	 * True verification that object was created with correct
18367c478bd9Sstevel@tonic-gate 	 * attrs is impossible.  The attrs could have been changed
18377c478bd9Sstevel@tonic-gate 	 * immediately after object creation.  If attributes did
18387c478bd9Sstevel@tonic-gate 	 * not verify, the only recourse for the server is to
18397c478bd9Sstevel@tonic-gate 	 * destroy the object.  Maybe if some attrs (like gid)
18407c478bd9Sstevel@tonic-gate 	 * are set incorrectly, the object should be destroyed;
18417c478bd9Sstevel@tonic-gate 	 * however, seems bad as a default policy.  Do we really
18427c478bd9Sstevel@tonic-gate 	 * want to destroy an object over one of the times not
18437c478bd9Sstevel@tonic-gate 	 * verifying correctly?  For these reasons, the server
18447c478bd9Sstevel@tonic-gate 	 * currently sets bits in attrset for createattrs
18457c478bd9Sstevel@tonic-gate 	 * that were set; however, no verification is done.
18467c478bd9Sstevel@tonic-gate 	 *
18477c478bd9Sstevel@tonic-gate 	 * vmask_to_nmask accounts for vattr bits set on create
18487c478bd9Sstevel@tonic-gate 	 *	[do_rfs4_set_attrs() only sets resp bits for
18497c478bd9Sstevel@tonic-gate 	 *	 non-vattr/vfs bits.]
18507c478bd9Sstevel@tonic-gate 	 * Mask off any bits set by default so as not to return
18517c478bd9Sstevel@tonic-gate 	 * more attrset bits than were requested in createattrs
18527c478bd9Sstevel@tonic-gate 	 */
18537c478bd9Sstevel@tonic-gate 	nfs4_vmask_to_nmask(sarg.vap->va_mask, &resp->attrset);
18547c478bd9Sstevel@tonic-gate 	resp->attrset &= args->createattrs.attrmask;
18557c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
18587c478bd9Sstevel@tonic-gate 	if (error) {
18597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we got no errors, we have
18647c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
18657c478bd9Sstevel@tonic-gate 	 * during the creation and it didn't change during the VOP_LOOKUP
18667c478bd9Sstevel@tonic-gate 	 * or VOP_FSYNC.
18677c478bd9Sstevel@tonic-gate 	 */
18687c478bd9Sstevel@tonic-gate 	if (!error && bva.va_seq && iva.va_seq && ava.va_seq &&
18691b300de9Sjwahlig 	    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
18707c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
18717c478bd9Sstevel@tonic-gate 	else
18727c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
18737c478bd9Sstevel@tonic-gate 
1874fd7da618Sgt29601 	/*
1875fd7da618Sgt29601 	 * Force modified metadata out to stable storage.
1876fd7da618Sgt29601 	 *
1877fd7da618Sgt29601 	 * if a underlying vp exists, pass it to VOP_FSYNC
1878fd7da618Sgt29601 	 */
1879fd7da618Sgt29601 	if (VOP_REALVP(vp, &realvp, NULL) == 0)
1880fd7da618Sgt29601 		(void) VOP_FSYNC(realvp, syncval, cr, NULL);
1881fd7da618Sgt29601 	else
1882da6c28aaSamw 		(void) VOP_FSYNC(vp, syncval, cr, NULL);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
18857c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1886f3b585ceSsamf 		goto out;
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 	if (cs->vp)
18897c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	cs->vp = vp;
18927c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
1893f3b585ceSsamf out:
1894f3b585ceSsamf 	DTRACE_NFSV4_2(op__create__done, struct compound_state *, cs,
1895f3b585ceSsamf 	    CREATE4res *, resp);
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate 
1898f3b585ceSsamf /*ARGSUSED*/
1899f3b585ceSsamf static void
1900f3b585ceSsamf rfs4_op_delegpurge(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1901f3b585ceSsamf     struct compound_state *cs)
1902f3b585ceSsamf {
1903f3b585ceSsamf 	DTRACE_NFSV4_2(op__delegpurge__start, struct compound_state *, cs,
1904f3b585ceSsamf 	    DELEGPURGE4args *, &argop->nfs_argop4_u.opdelegpurge);
1905f3b585ceSsamf 
1906f3b585ceSsamf 	rfs4_op_inval(argop, resop, req, cs);
1907f3b585ceSsamf 
1908f3b585ceSsamf 	DTRACE_NFSV4_2(op__delegpurge__done, struct compound_state *, cs,
1909f3b585ceSsamf 	    DELEGPURGE4res *, &resop->nfs_resop4_u.opdelegpurge);
1910f3b585ceSsamf }
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19137c478bd9Sstevel@tonic-gate static void
19147c478bd9Sstevel@tonic-gate rfs4_op_delegreturn(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
19157c478bd9Sstevel@tonic-gate     struct compound_state *cs)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate 	DELEGRETURN4args *args = &argop->nfs_argop4_u.opdelegreturn;
19187c478bd9Sstevel@tonic-gate 	DELEGRETURN4res *resp = &resop->nfs_resop4_u.opdelegreturn;
19197c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
19207c478bd9Sstevel@tonic-gate 	nfsstat4 status;
19217c478bd9Sstevel@tonic-gate 
1922f3b585ceSsamf 	DTRACE_NFSV4_2(op__delegreturn__start, struct compound_state *, cs,
1923f3b585ceSsamf 	    DELEGRETURN4args *, args);
1924f3b585ceSsamf 
19257c478bd9Sstevel@tonic-gate 	status = rfs4_get_deleg_state(&args->deleg_stateid, &dsp);
19267c478bd9Sstevel@tonic-gate 	resp->status = *cs->statusp = status;
19277c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK)
1928f3b585ceSsamf 		goto out;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
1931d216dff5SRobert Mastors 	if (cs->vp != dsp->rds_finfo->rf_vp) {
19327c478bd9Sstevel@tonic-gate 		resp->status = *cs->statusp = NFS4ERR_BAD_STATEID;
19337c478bd9Sstevel@tonic-gate 	} else
19347c478bd9Sstevel@tonic-gate 		rfs4_return_deleg(dsp, FALSE);
19357c478bd9Sstevel@tonic-gate 
1936d216dff5SRobert Mastors 	rfs4_update_lease(dsp->rds_client);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
1939f3b585ceSsamf out:
1940f3b585ceSsamf 	DTRACE_NFSV4_2(op__delegreturn__done, struct compound_state *, cs,
1941f3b585ceSsamf 	    DELEGRETURN4res *, resp);
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate /*
19457c478bd9Sstevel@tonic-gate  * Check to see if a given "flavor" is an explicitly shared flavor.
19467c478bd9Sstevel@tonic-gate  * The assumption of this routine is the "flavor" is already a valid
19477c478bd9Sstevel@tonic-gate  * flavor in the secinfo list of "exi".
19487c478bd9Sstevel@tonic-gate  *
19497c478bd9Sstevel@tonic-gate  *	e.g.
19507c478bd9Sstevel@tonic-gate  *		# share -o sec=flavor1 /export
19517c478bd9Sstevel@tonic-gate  *		# share -o sec=flavor2 /export/home
19527c478bd9Sstevel@tonic-gate  *
19537c478bd9Sstevel@tonic-gate  *		flavor2 is not an explicitly shared flavor for /export,
19547c478bd9Sstevel@tonic-gate  *		however it is in the secinfo list for /export thru the
19557c478bd9Sstevel@tonic-gate  *		server namespace setup.
19567c478bd9Sstevel@tonic-gate  */
19577c478bd9Sstevel@tonic-gate int
19587c478bd9Sstevel@tonic-gate is_exported_sec(int flavor, struct exportinfo *exi)
19597c478bd9Sstevel@tonic-gate {
19607c478bd9Sstevel@tonic-gate 	int	i;
19617c478bd9Sstevel@tonic-gate 	struct secinfo *sp;
19627c478bd9Sstevel@tonic-gate 
1963aafcd32bSMarcel Telka 	rw_enter(&exported_lock, RW_READER);
19647c478bd9Sstevel@tonic-gate 	sp = exi->exi_export.ex_secinfo;
19657c478bd9Sstevel@tonic-gate 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
19667c478bd9Sstevel@tonic-gate 		if (flavor == sp[i].s_secinfo.sc_nfsnum ||
19677c478bd9Sstevel@tonic-gate 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1968aafcd32bSMarcel Telka 			int ret = SEC_REF_EXPORTED(&sp[i]);
1969aafcd32bSMarcel Telka 			rw_exit(&exported_lock);
1970aafcd32bSMarcel Telka 			return (ret);
19717c478bd9Sstevel@tonic-gate 		}
19727c478bd9Sstevel@tonic-gate 	}
1973aafcd32bSMarcel Telka 	rw_exit(&exported_lock);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	/* Should not reach this point based on the assumption */
19767c478bd9Sstevel@tonic-gate 	return (0);
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate /*
19807c478bd9Sstevel@tonic-gate  * Check if the security flavor used in the request matches what is
19817c478bd9Sstevel@tonic-gate  * required at the export point or at the root pseudo node (exi_root).
19827c478bd9Sstevel@tonic-gate  *
19837c478bd9Sstevel@tonic-gate  * returns 1 if there's a match or if exported with AUTH_NONE; 0 otherwise.
19847c478bd9Sstevel@tonic-gate  *
19857c478bd9Sstevel@tonic-gate  */
19867c478bd9Sstevel@tonic-gate static int
19877c478bd9Sstevel@tonic-gate secinfo_match_or_authnone(struct compound_state *cs)
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate 	int	i;
19907c478bd9Sstevel@tonic-gate 	struct secinfo *sp;
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	/*
19937c478bd9Sstevel@tonic-gate 	 * Check cs->nfsflavor (from the request) against
19947c478bd9Sstevel@tonic-gate 	 * the current export data in cs->exi.
19957c478bd9Sstevel@tonic-gate 	 */
1996aafcd32bSMarcel Telka 	rw_enter(&exported_lock, RW_READER);
19977c478bd9Sstevel@tonic-gate 	sp = cs->exi->exi_export.ex_secinfo;
19987c478bd9Sstevel@tonic-gate 	for (i = 0; i < cs->exi->exi_export.ex_seccnt; i++) {
19997c478bd9Sstevel@tonic-gate 		if (cs->nfsflavor == sp[i].s_secinfo.sc_nfsnum ||
2000aafcd32bSMarcel Telka 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
2001aafcd32bSMarcel Telka 			rw_exit(&exported_lock);
20027c478bd9Sstevel@tonic-gate 			return (1);
20037c478bd9Sstevel@tonic-gate 		}
2004aafcd32bSMarcel Telka 	}
2005aafcd32bSMarcel Telka 	rw_exit(&exported_lock);
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	return (0);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate /*
20117c478bd9Sstevel@tonic-gate  * Check the access authority for the client and return the correct error.
20127c478bd9Sstevel@tonic-gate  */
20137c478bd9Sstevel@tonic-gate nfsstat4
20147c478bd9Sstevel@tonic-gate call_checkauth4(struct compound_state *cs, struct svc_req *req)
20157c478bd9Sstevel@tonic-gate {
20167c478bd9Sstevel@tonic-gate 	int	authres;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	/*
20197c478bd9Sstevel@tonic-gate 	 * First, check if the security flavor used in the request
20207c478bd9Sstevel@tonic-gate 	 * are among the flavors set in the server namespace.
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	if (!secinfo_match_or_authnone(cs)) {
20237c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_WRONGSEC;
20247c478bd9Sstevel@tonic-gate 		return (*cs->statusp);
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	authres = checkauth4(cs, req);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	if (authres > 0) {
20307c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4_OK;
20317c478bd9Sstevel@tonic-gate 		if (! (cs->access & CS_ACCESS_LIMITED))
20327c478bd9Sstevel@tonic-gate 			cs->access = CS_ACCESS_OK;
20337c478bd9Sstevel@tonic-gate 	} else if (authres == 0) {
20347c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_ACCESS;
20357c478bd9Sstevel@tonic-gate 	} else if (authres == -2) {
20367c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_WRONGSEC;
20377c478bd9Sstevel@tonic-gate 	} else {
20387c478bd9Sstevel@tonic-gate 		*cs->statusp = NFS4ERR_DELAY;
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 	return (*cs->statusp);
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * bitmap4_to_attrmask is called by getattr and readdir.
20457c478bd9Sstevel@tonic-gate  * It sets up the vattr mask and determines whether vfsstat call is needed
20467c478bd9Sstevel@tonic-gate  * based on the input bitmap.
20477c478bd9Sstevel@tonic-gate  * Returns nfsv4 status.
20487c478bd9Sstevel@tonic-gate  */
20497c478bd9Sstevel@tonic-gate static nfsstat4
20507c478bd9Sstevel@tonic-gate bitmap4_to_attrmask(bitmap4 breq, struct nfs4_svgetit_arg *sargp)
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate 	int i;
20537c478bd9Sstevel@tonic-gate 	uint_t	va_mask;
20547c478bd9Sstevel@tonic-gate 	struct statvfs64 *sbp = sargp->sbp;
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	sargp->sbp = NULL;
20577c478bd9Sstevel@tonic-gate 	sargp->flag = 0;
20587c478bd9Sstevel@tonic-gate 	sargp->rdattr_error = NFS4_OK;
20597c478bd9Sstevel@tonic-gate 	sargp->mntdfid_set = FALSE;
20607c478bd9Sstevel@tonic-gate 	if (sargp->cs->vp)
20617c478bd9Sstevel@tonic-gate 		sargp->xattr = get_fh4_flag(&sargp->cs->fh,
20627c478bd9Sstevel@tonic-gate 		    FH4_ATTRDIR | FH4_NAMEDATTR);
20637c478bd9Sstevel@tonic-gate 	else
20647c478bd9Sstevel@tonic-gate 		sargp->xattr = 0;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/*
20677c478bd9Sstevel@tonic-gate 	 * Set rdattr_error_req to true if return error per
20687c478bd9Sstevel@tonic-gate 	 * failed entry rather than fail the readdir.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	if (breq & FATTR4_RDATTR_ERROR_MASK)
20717c478bd9Sstevel@tonic-gate 		sargp->rdattr_error_req = 1;
20727c478bd9Sstevel@tonic-gate 	else
20737c478bd9Sstevel@tonic-gate 		sargp->rdattr_error_req = 0;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * generate the va_mask
20777c478bd9Sstevel@tonic-gate 	 * Handle the easy cases first
20787c478bd9Sstevel@tonic-gate 	 */
20797c478bd9Sstevel@tonic-gate 	switch (breq) {
20807c478bd9Sstevel@tonic-gate 	case NFS4_NTOV_ATTR_MASK:
20817c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_NTOV_ATTR_AT_MASK;
20827c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	case NFS4_FS_ATTR_MASK:
20857c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_FS_ATTR_AT_MASK;
20867c478bd9Sstevel@tonic-gate 		sargp->sbp = sbp;
20877c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	case NFS4_NTOV_ATTR_CACHE_MASK:
20907c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = NFS4_NTOV_ATTR_CACHE_AT_MASK;
20917c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	case FATTR4_LEASE_TIME_MASK:
20947c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = 0;
20957c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	default:
20987c478bd9Sstevel@tonic-gate 		va_mask = 0;
20997c478bd9Sstevel@tonic-gate 		for (i = 0; i < nfs4_ntov_map_size; i++) {
21007c478bd9Sstevel@tonic-gate 			if ((breq & nfs4_ntov_map[i].fbit) &&
21017c478bd9Sstevel@tonic-gate 			    nfs4_ntov_map[i].vbit)
21027c478bd9Sstevel@tonic-gate 				va_mask |= nfs4_ntov_map[i].vbit;
21037c478bd9Sstevel@tonic-gate 		}
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 		/*
21067c478bd9Sstevel@tonic-gate 		 * Check is vfsstat is needed
21077c478bd9Sstevel@tonic-gate 		 */
21087c478bd9Sstevel@tonic-gate 		if (breq & NFS4_FS_ATTR_MASK)
21097c478bd9Sstevel@tonic-gate 			sargp->sbp = sbp;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask = va_mask;
21127c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
21157c478bd9Sstevel@tonic-gate }
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate  * bitmap4_get_sysattrs is called by getattr and readdir.
21197c478bd9Sstevel@tonic-gate  * It calls both VOP_GETATTR and VFS_STATVFS calls to get the attrs.
21207c478bd9Sstevel@tonic-gate  * Returns nfsv4 status.
21217c478bd9Sstevel@tonic-gate  */
21227c478bd9Sstevel@tonic-gate static nfsstat4
21237c478bd9Sstevel@tonic-gate bitmap4_get_sysattrs(struct nfs4_svgetit_arg *sargp)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	int error;
21267c478bd9Sstevel@tonic-gate 	struct compound_state *cs = sargp->cs;
21277c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if (sargp->sbp != NULL) {
21307c478bd9Sstevel@tonic-gate 		if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) {
21317c478bd9Sstevel@tonic-gate 			sargp->sbp = NULL;	/* to identify error */
21327c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
21337c478bd9Sstevel@tonic-gate 		}
21347c478bd9Sstevel@tonic-gate 	}
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr));
21377c478bd9Sstevel@tonic-gate }
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate static void
21407c478bd9Sstevel@tonic-gate nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp)
21417c478bd9Sstevel@tonic-gate {
21427c478bd9Sstevel@tonic-gate 	ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size,
21437c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
21447c478bd9Sstevel@tonic-gate 	ntovp->attrcnt = 0;
21457c478bd9Sstevel@tonic-gate 	ntovp->vfsstat = FALSE;
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate static void
21497c478bd9Sstevel@tonic-gate nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
21507c478bd9Sstevel@tonic-gate     struct nfs4_svgetit_arg *sargp)
21517c478bd9Sstevel@tonic-gate {
21527c478bd9Sstevel@tonic-gate 	int i;
21537c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
21547c478bd9Sstevel@tonic-gate 	uint8_t *amap;
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * XXX Should do the same checks for whether the bit is set
21587c478bd9Sstevel@tonic-gate 	 */
21597c478bd9Sstevel@tonic-gate 	for (i = 0, na = ntovp->na, amap = ntovp->amap;
21607c478bd9Sstevel@tonic-gate 	    i < ntovp->attrcnt; i++, na++, amap++) {
21617c478bd9Sstevel@tonic-gate 		(void) (*nfs4_ntov_map[*amap].sv_getit)(
21627c478bd9Sstevel@tonic-gate 		    NFS4ATTR_FREEIT, sargp, na);
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 	if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) {
21657c478bd9Sstevel@tonic-gate 		/*
21667c478bd9Sstevel@tonic-gate 		 * xdr_free for getattr will be done later
21677c478bd9Sstevel@tonic-gate 		 */
21687c478bd9Sstevel@tonic-gate 		for (i = 0, na = ntovp->na, amap = ntovp->amap;
21697c478bd9Sstevel@tonic-gate 		    i < ntovp->attrcnt; i++, na++, amap++) {
21707c478bd9Sstevel@tonic-gate 			xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na);
21717c478bd9Sstevel@tonic-gate 		}
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 	kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size);
21747c478bd9Sstevel@tonic-gate }
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate /*
21777c478bd9Sstevel@tonic-gate  * do_rfs4_op_getattr gets the system attrs and converts into fattr4.
21787c478bd9Sstevel@tonic-gate  */
21797c478bd9Sstevel@tonic-gate static nfsstat4
21807c478bd9Sstevel@tonic-gate do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp,
21817c478bd9Sstevel@tonic-gate     struct nfs4_svgetit_arg *sargp)
21827c478bd9Sstevel@tonic-gate {
21837c478bd9Sstevel@tonic-gate 	int error = 0;
21847c478bd9Sstevel@tonic-gate 	int i, k;
21857c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
21867c478bd9Sstevel@tonic-gate 	XDR xdr;
21877c478bd9Sstevel@tonic-gate 	ulong_t xdr_size;
21887c478bd9Sstevel@tonic-gate 	char *xdr_attrs;
21897c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
21907c478bd9Sstevel@tonic-gate 	nfsstat4 prev_rdattr_error = sargp->rdattr_error;
21917c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
21927c478bd9Sstevel@tonic-gate 	uint8_t *amap;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	sargp->op = NFS4ATTR_GETIT;
21957c478bd9Sstevel@tonic-gate 	sargp->flag = 0;
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	fattrp->attrmask = 0;
21987c478bd9Sstevel@tonic-gate 	/* if no bits requested, then return empty fattr4 */
21997c478bd9Sstevel@tonic-gate 	if (breq == 0) {
22007c478bd9Sstevel@tonic-gate 		fattrp->attrlist4_len = 0;
22017c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = NULL;
22027c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	/*
22067c478bd9Sstevel@tonic-gate 	 * return NFS4ERR_INVAL when client requests write-only attrs
22077c478bd9Sstevel@tonic-gate 	 */
22087c478bd9Sstevel@tonic-gate 	if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK))
22097c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
22127c478bd9Sstevel@tonic-gate 	na = ntov.na;
22137c478bd9Sstevel@tonic-gate 	amap = ntov.amap;
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * Now loop to get or verify the attrs
22177c478bd9Sstevel@tonic-gate 	 */
22187c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_ntov_map_size; i++) {
22197c478bd9Sstevel@tonic-gate 		if (breq & nfs4_ntov_map[i].fbit) {
22207c478bd9Sstevel@tonic-gate 			if ((*nfs4_ntov_map[i].sv_getit)(
22217c478bd9Sstevel@tonic-gate 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0) {
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 				error = (*nfs4_ntov_map[i].sv_getit)(
22247c478bd9Sstevel@tonic-gate 				    NFS4ATTR_GETIT, sargp, na);
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 				/*
22277c478bd9Sstevel@tonic-gate 				 * Possible error values:
22287c478bd9Sstevel@tonic-gate 				 * >0 if sv_getit failed to
22297c478bd9Sstevel@tonic-gate 				 * get the attr; 0 if succeeded;
22307c478bd9Sstevel@tonic-gate 				 * <0 if rdattr_error and the
22317c478bd9Sstevel@tonic-gate 				 * attribute cannot be returned.
22327c478bd9Sstevel@tonic-gate 				 */
22337c478bd9Sstevel@tonic-gate 				if (error && !(sargp->rdattr_error_req))
22347c478bd9Sstevel@tonic-gate 					goto done;
22357c478bd9Sstevel@tonic-gate 				/*
22367c478bd9Sstevel@tonic-gate 				 * If error then just for entry
22377c478bd9Sstevel@tonic-gate 				 */
22387c478bd9Sstevel@tonic-gate 				if (error == 0) {
22397c478bd9Sstevel@tonic-gate 					fattrp->attrmask |=
22407c478bd9Sstevel@tonic-gate 					    nfs4_ntov_map[i].fbit;
22417c478bd9Sstevel@tonic-gate 					*amap++ =
22427c478bd9Sstevel@tonic-gate 					    (uint8_t)nfs4_ntov_map[i].nval;
22437c478bd9Sstevel@tonic-gate 					na++;
22447c478bd9Sstevel@tonic-gate 					(ntov.attrcnt)++;
22457c478bd9Sstevel@tonic-gate 				} else if ((error > 0) &&
22467c478bd9Sstevel@tonic-gate 				    (sargp->rdattr_error == NFS4_OK)) {
22477c478bd9Sstevel@tonic-gate 					sargp->rdattr_error = puterrno4(error);
22487c478bd9Sstevel@tonic-gate 				}
22497c478bd9Sstevel@tonic-gate 				error = 0;
22507c478bd9Sstevel@tonic-gate 			}
22517c478bd9Sstevel@tonic-gate 		}
22527c478bd9Sstevel@tonic-gate 	}
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	/*
22557c478bd9Sstevel@tonic-gate 	 * If rdattr_error was set after the return value for it was assigned,
22567c478bd9Sstevel@tonic-gate 	 * update it.
22577c478bd9Sstevel@tonic-gate 	 */
22587c478bd9Sstevel@tonic-gate 	if (prev_rdattr_error != sargp->rdattr_error) {
22597c478bd9Sstevel@tonic-gate 		na = ntov.na;
22607c478bd9Sstevel@tonic-gate 		amap = ntov.amap;
22617c478bd9Sstevel@tonic-gate 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
22627c478bd9Sstevel@tonic-gate 			k = *amap;
22637c478bd9Sstevel@tonic-gate 			if (k < FATTR4_RDATTR_ERROR) {
22647c478bd9Sstevel@tonic-gate 				continue;
22657c478bd9Sstevel@tonic-gate 			}
22667c478bd9Sstevel@tonic-gate 			if ((k == FATTR4_RDATTR_ERROR) &&
22677c478bd9Sstevel@tonic-gate 			    ((*nfs4_ntov_map[k].sv_getit)(
22687c478bd9Sstevel@tonic-gate 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) {
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 				(void) (*nfs4_ntov_map[k].sv_getit)(
22717c478bd9Sstevel@tonic-gate 				    NFS4ATTR_GETIT, sargp, na);
22727c478bd9Sstevel@tonic-gate 			}
22737c478bd9Sstevel@tonic-gate 			break;
22747c478bd9Sstevel@tonic-gate 		}
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	xdr_size = 0;
22787c478bd9Sstevel@tonic-gate 	na = ntov.na;
22797c478bd9Sstevel@tonic-gate 	amap = ntov.amap;
22807c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
22817c478bd9Sstevel@tonic-gate 		xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na);
22827c478bd9Sstevel@tonic-gate 	}
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	fattrp->attrlist4_len = xdr_size;
22857c478bd9Sstevel@tonic-gate 	if (xdr_size) {
22867c478bd9Sstevel@tonic-gate 		/* freed by rfs4_op_getattr_free() */
22877c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE);
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 		na = ntov.na;
22927c478bd9Sstevel@tonic-gate 		amap = ntov.amap;
22937c478bd9Sstevel@tonic-gate 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
22947c478bd9Sstevel@tonic-gate 			if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) {
22950a701b1eSRobert Gordon 				DTRACE_PROBE1(nfss__e__getattr4_encfail,
22960a701b1eSRobert Gordon 				    int, *amap);
22977c478bd9Sstevel@tonic-gate 				status = NFS4ERR_SERVERFAULT;
22987c478bd9Sstevel@tonic-gate 				break;
22997c478bd9Sstevel@tonic-gate 			}
23007c478bd9Sstevel@tonic-gate 		}
23017c478bd9Sstevel@tonic-gate 		/* xdrmem_destroy(&xdrs); */	/* NO-OP */
23027c478bd9Sstevel@tonic-gate 	} else {
23037c478bd9Sstevel@tonic-gate 		fattrp->attrlist4 = NULL;
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate done:
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, sargp);
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	if (error != 0)
23107c478bd9Sstevel@tonic-gate 		status = puterrno4(error);
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	return (status);
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate /* ARGSUSED */
23167c478bd9Sstevel@tonic-gate static void
23177c478bd9Sstevel@tonic-gate rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
23187c478bd9Sstevel@tonic-gate     struct compound_state *cs)
23197c478bd9Sstevel@tonic-gate {
23207c478bd9Sstevel@tonic-gate 	GETATTR4args *args = &argop->nfs_argop4_u.opgetattr;
23217c478bd9Sstevel@tonic-gate 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
23227c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
23237c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
23247c478bd9Sstevel@tonic-gate 	nfsstat4 status;
23257c478bd9Sstevel@tonic-gate 
2326f3b585ceSsamf 	DTRACE_NFSV4_2(op__getattr__start, struct compound_state *, cs,
2327f3b585ceSsamf 	    GETATTR4args *, args);
2328f3b585ceSsamf 
23297c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
23307c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2331f3b585ceSsamf 		goto out;
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
23357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2336f3b585ceSsamf 		goto out;
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
23407c478bd9Sstevel@tonic-gate 	sarg.cs = cs;
23412f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	status = bitmap4_to_attrmask(args->attr_request, &sarg);
23447c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK) {
23452f172c55SRobert Thurlow 
23467c478bd9Sstevel@tonic-gate 		status = bitmap4_get_sysattrs(&sarg);
23472f172c55SRobert Thurlow 		if (status == NFS4_OK) {
23482f172c55SRobert Thurlow 
23492f172c55SRobert Thurlow 			/* Is this a referral? */
23502f172c55SRobert Thurlow 			if (vn_is_nfs_reparse(cs->vp, cs->cr)) {
23512f172c55SRobert Thurlow 				/* Older V4 Solaris client sees a link */
23522f172c55SRobert Thurlow 				if (client_is_downrev(req))
23532f172c55SRobert Thurlow 					sarg.vap->va_type = VLNK;
23542f172c55SRobert Thurlow 				else
23552f172c55SRobert Thurlow 					sarg.is_referral = B_TRUE;
23562f172c55SRobert Thurlow 			}
23572f172c55SRobert Thurlow 
23587c478bd9Sstevel@tonic-gate 			status = do_rfs4_op_getattr(args->attr_request,
23597c478bd9Sstevel@tonic-gate 			    &resp->obj_attributes, &sarg);
23607c478bd9Sstevel@tonic-gate 		}
23612f172c55SRobert Thurlow 	}
23627c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
2363f3b585ceSsamf out:
2364f3b585ceSsamf 	DTRACE_NFSV4_2(op__getattr__done, struct compound_state *, cs,
2365f3b585ceSsamf 	    GETATTR4res *, resp);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate static void
23697c478bd9Sstevel@tonic-gate rfs4_op_getattr_free(nfs_resop4 *resop)
23707c478bd9Sstevel@tonic-gate {
23717c478bd9Sstevel@tonic-gate 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	nfs4_fattr4_free(&resp->obj_attributes);
23747c478bd9Sstevel@tonic-gate }
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate /* ARGSUSED */
23777c478bd9Sstevel@tonic-gate static void
23787c478bd9Sstevel@tonic-gate rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
23797c478bd9Sstevel@tonic-gate     struct compound_state *cs)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
23827c478bd9Sstevel@tonic-gate 
2383f3b585ceSsamf 	DTRACE_NFSV4_1(op__getfh__start, struct compound_state *, cs);
2384f3b585ceSsamf 
23857c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
23867c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2387f3b585ceSsamf 		goto out;
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
23907c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2391f3b585ceSsamf 		goto out;
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 
23942f172c55SRobert Thurlow 	/* check for reparse point at the share point */
23952f172c55SRobert Thurlow 	if (cs->exi->exi_moved || vn_is_nfs_reparse(cs->exi->exi_vp, cs->cr)) {
23962f172c55SRobert Thurlow 		/* it's all bad */
23972f172c55SRobert Thurlow 		cs->exi->exi_moved = 1;
23982f172c55SRobert Thurlow 		*cs->statusp = resp->status = NFS4ERR_MOVED;
23992f172c55SRobert Thurlow 		DTRACE_PROBE2(nfs4serv__func__referral__shared__moved,
24002f172c55SRobert Thurlow 		    vnode_t *, cs->vp, char *, "rfs4_op_getfh");
24012f172c55SRobert Thurlow 		return;
24022f172c55SRobert Thurlow 	}
24032f172c55SRobert Thurlow 
24042f172c55SRobert Thurlow 	/* check for reparse point at vp */
24052f172c55SRobert Thurlow 	if (vn_is_nfs_reparse(cs->vp, cs->cr) && !client_is_downrev(req)) {
24062f172c55SRobert Thurlow 		/* it's not all bad */
24072f172c55SRobert Thurlow 		*cs->statusp = resp->status = NFS4ERR_MOVED;
24082f172c55SRobert Thurlow 		DTRACE_PROBE2(nfs4serv__func__referral__moved,
24092f172c55SRobert Thurlow 		    vnode_t *, cs->vp, char *, "rfs4_op_getfh");
24102f172c55SRobert Thurlow 		return;
24112f172c55SRobert Thurlow 	}
24122f172c55SRobert Thurlow 
24137c478bd9Sstevel@tonic-gate 	resp->object.nfs_fh4_val =
24147c478bd9Sstevel@tonic-gate 	    kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP);
24157c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->fh, &resp->object);
24167c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
2417f3b585ceSsamf out:
2418f3b585ceSsamf 	DTRACE_NFSV4_2(op__getfh__done, struct compound_state *, cs,
2419f3b585ceSsamf 	    GETFH4res *, resp);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate static void
24237c478bd9Sstevel@tonic-gate rfs4_op_getfh_free(nfs_resop4 *resop)
24247c478bd9Sstevel@tonic-gate {
24257c478bd9Sstevel@tonic-gate 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK &&
24287c478bd9Sstevel@tonic-gate 	    resp->object.nfs_fh4_val != NULL) {
24297c478bd9Sstevel@tonic-gate 		kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len);
24307c478bd9Sstevel@tonic-gate 		resp->object.nfs_fh4_val = NULL;
24317c478bd9Sstevel@tonic-gate 		resp->object.nfs_fh4_len = 0;
24327c478bd9Sstevel@tonic-gate 	}
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate /*
24367c478bd9Sstevel@tonic-gate  * illegal: args: void
24377c478bd9Sstevel@tonic-gate  *	    res : status (NFS4ERR_OP_ILLEGAL)
24387c478bd9Sstevel@tonic-gate  */
24397c478bd9Sstevel@tonic-gate /* ARGSUSED */
24407c478bd9Sstevel@tonic-gate static void
24417c478bd9Sstevel@tonic-gate rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop,
24427c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
24437c478bd9Sstevel@tonic-gate {
24447c478bd9Sstevel@tonic-gate 	ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	resop->resop = OP_ILLEGAL;
24477c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * link: args: SAVED_FH: file, CURRENT_FH: target directory
24527c478bd9Sstevel@tonic-gate  *	 res: status. If success - CURRENT_FH unchanged, return change_info
24537c478bd9Sstevel@tonic-gate  */
24547c478bd9Sstevel@tonic-gate /* ARGSUSED */
24557c478bd9Sstevel@tonic-gate static void
24567c478bd9Sstevel@tonic-gate rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
24577c478bd9Sstevel@tonic-gate     struct compound_state *cs)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	LINK4args *args = &argop->nfs_argop4_u.oplink;
24607c478bd9Sstevel@tonic-gate 	LINK4res *resp = &resop->nfs_resop4_u.oplink;
24617c478bd9Sstevel@tonic-gate 	int error;
24627c478bd9Sstevel@tonic-gate 	vnode_t *vp;
24637c478bd9Sstevel@tonic-gate 	vnode_t *dvp;
24647c478bd9Sstevel@tonic-gate 	struct vattr bdva, idva, adva;
24657c478bd9Sstevel@tonic-gate 	char *nm;
24667c478bd9Sstevel@tonic-gate 	uint_t  len;
2467b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
2468b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
246915721462SDaniil Lunev 	nfsstat4 status;
24707c478bd9Sstevel@tonic-gate 
2471f3b585ceSsamf 	DTRACE_NFSV4_2(op__link__start, struct compound_state *, cs,
2472f3b585ceSsamf 	    LINK4args *, args);
2473f3b585ceSsamf 
24747c478bd9Sstevel@tonic-gate 	/* SAVED_FH: source object */
24757c478bd9Sstevel@tonic-gate 	vp = cs->saved_vp;
24767c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
24777c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2478f3b585ceSsamf 		goto out;
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: target directory */
24827c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
24837c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
24847c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2485f3b585ceSsamf 		goto out;
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	/*
24897c478bd9Sstevel@tonic-gate 	 * If there is a non-shared filesystem mounted on this vnode,
24907c478bd9Sstevel@tonic-gate 	 * do not allow to link any file in this directory.
24917c478bd9Sstevel@tonic-gate 	 */
24927c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
24937c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2494f3b585ceSsamf 		goto out;
24957c478bd9Sstevel@tonic-gate 	}
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
24987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2499f3b585ceSsamf 		goto out;
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	/* Check source object's type validity */
25037c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
25047c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
2505f3b585ceSsamf 		goto out;
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	/* Check target directory's type */
25097c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
25107c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2511f3b585ceSsamf 		goto out;
25127c478bd9Sstevel@tonic-gate 	}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	if (cs->saved_exi != cs->exi) {
25157c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_XDEV;
2516f3b585ceSsamf 		goto out;
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 
251915721462SDaniil Lunev 	status = utf8_dir_verify(&args->newname);
252015721462SDaniil Lunev 	if (status != NFS4_OK) {
252115721462SDaniil Lunev 		*cs->statusp = resp->status = status;
2522f3b585ceSsamf 		goto out;
25237c478bd9Sstevel@tonic-gate 	}
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->newname, &len, NULL);
25267c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
25277c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2528f3b585ceSsamf 		goto out;
25297c478bd9Sstevel@tonic-gate 	}
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
25327c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
25337c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
2534f3b585ceSsamf 		goto out;
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 
25375cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
25387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
25397c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
2540f3b585ceSsamf 		goto out;
25417c478bd9Sstevel@tonic-gate 	}
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
25447c478bd9Sstevel@tonic-gate 	bdva.va_mask = AT_CTIME|AT_SEQ;
2545da6c28aaSamw 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
25467c478bd9Sstevel@tonic-gate 	if (error) {
25477c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
25487c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
2549f3b585ceSsamf 		goto out;
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
2552b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2553b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
2554b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
2555b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2556b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
2557b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2558b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, len);
2559b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
2560b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
2561b89a8333Snatalie li - Sun Microsystems - Irvine United States 
25627c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
25637c478bd9Sstevel@tonic-gate 
2564b89a8333Snatalie li - Sun Microsystems - Irvine United States 	error = VOP_LINK(dvp, vp, name, cs->cr, NULL, 0);
25657c478bd9Sstevel@tonic-gate 
2566b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nm != name)
2567b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
25687c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	/*
25717c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
25727c478bd9Sstevel@tonic-gate 	 */
25737c478bd9Sstevel@tonic-gate 	idva.va_mask = AT_SEQ;
2574da6c28aaSamw 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
25757c478bd9Sstevel@tonic-gate 		idva.va_seq = 0;
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	/*
25787c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
25797c478bd9Sstevel@tonic-gate 	 */
2580da6c28aaSamw 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
2581da6c28aaSamw 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	if (error) {
25847c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
2585f3b585ceSsamf 		goto out;
25867c478bd9Sstevel@tonic-gate 	}
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 	/*
25897c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
25907c478bd9Sstevel@tonic-gate 	 * before value.
25917c478bd9Sstevel@tonic-gate 	 */
25927c478bd9Sstevel@tonic-gate 	adva.va_mask = AT_CTIME|AT_SEQ;
2593da6c28aaSamw 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
25947c478bd9Sstevel@tonic-gate 		adva.va_ctime = bdva.va_ctime;
25957c478bd9Sstevel@tonic-gate 		adva.va_seq = 0;
25967c478bd9Sstevel@tonic-gate 	}
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	/*
26017c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
26027c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
26037c478bd9Sstevel@tonic-gate 	 * during the VOP_LINK and it didn't change during the VOP_FSYNC.
26047c478bd9Sstevel@tonic-gate 	 */
26057c478bd9Sstevel@tonic-gate 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
26061b300de9Sjwahlig 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
26077c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
26087c478bd9Sstevel@tonic-gate 	else
26097c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
2612f3b585ceSsamf out:
2613f3b585ceSsamf 	DTRACE_NFSV4_2(op__link__done, struct compound_state *, cs,
2614f3b585ceSsamf 	    LINK4res *, resp);
26157c478bd9Sstevel@tonic-gate }
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate /*
26187c478bd9Sstevel@tonic-gate  * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work.
26197c478bd9Sstevel@tonic-gate  */
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate /* ARGSUSED */
26227c478bd9Sstevel@tonic-gate static nfsstat4
2623593cc11bSJan Kryl do_rfs4_op_lookup(char *nm, struct svc_req *req, struct compound_state *cs)
26247c478bd9Sstevel@tonic-gate {
26257c478bd9Sstevel@tonic-gate 	int error;
26267c478bd9Sstevel@tonic-gate 	int different_export = 0;
26276e062f4aSMarcel Telka 	vnode_t *vp, *pre_tvp = NULL, *oldvp = NULL;
2628aafcd32bSMarcel Telka 	struct exportinfo *exi = NULL, *pre_exi = NULL;
26297c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
26307c478bd9Sstevel@tonic-gate 	fid_t fid;
26317c478bd9Sstevel@tonic-gate 	int attrdir, dotdot, walk;
26327c478bd9Sstevel@tonic-gate 	bool_t is_newvp = FALSE;
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	if (cs->vp->v_flag & V_XATTRDIR) {
26357c478bd9Sstevel@tonic-gate 		attrdir = 1;
26367c478bd9Sstevel@tonic-gate 		ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR));
26377c478bd9Sstevel@tonic-gate 	} else {
26387c478bd9Sstevel@tonic-gate 		attrdir = 0;
26397c478bd9Sstevel@tonic-gate 		ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR));
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	/*
26457c478bd9Sstevel@tonic-gate 	 * If dotdotting, then need to check whether it's
26467c478bd9Sstevel@tonic-gate 	 * above the root of a filesystem, or above an
26477c478bd9Sstevel@tonic-gate 	 * export point.
26487c478bd9Sstevel@tonic-gate 	 */
26497c478bd9Sstevel@tonic-gate 	if (dotdot) {
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 		/*
26527c478bd9Sstevel@tonic-gate 		 * If dotdotting at the root of a filesystem, then
26537c478bd9Sstevel@tonic-gate 		 * need to traverse back to the mounted-on filesystem
26547c478bd9Sstevel@tonic-gate 		 * and do the dotdot lookup there.
26557c478bd9Sstevel@tonic-gate 		 */
26567c478bd9Sstevel@tonic-gate 		if (cs->vp->v_flag & VROOT) {
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 			/*
26597c478bd9Sstevel@tonic-gate 			 * If at the system root, then can
26607c478bd9Sstevel@tonic-gate 			 * go up no further.
26617c478bd9Sstevel@tonic-gate 			 */
26627c478bd9Sstevel@tonic-gate 			if (VN_CMP(cs->vp, rootdir))
26637c478bd9Sstevel@tonic-gate 				return (puterrno4(ENOENT));
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 			/*
26667c478bd9Sstevel@tonic-gate 			 * Traverse back to the mounted-on filesystem
26677c478bd9Sstevel@tonic-gate 			 */
26687c478bd9Sstevel@tonic-gate 			cs->vp = untraverse(cs->vp);
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 			/*
26717c478bd9Sstevel@tonic-gate 			 * Set the different_export flag so we remember
26727c478bd9Sstevel@tonic-gate 			 * to pick up a new exportinfo entry for
26737c478bd9Sstevel@tonic-gate 			 * this new filesystem.
26747c478bd9Sstevel@tonic-gate 			 */
26757c478bd9Sstevel@tonic-gate 			different_export = 1;
26767c478bd9Sstevel@tonic-gate 		} else {
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 			/*
26797c478bd9Sstevel@tonic-gate 			 * If dotdotting above an export point then set
26807c478bd9Sstevel@tonic-gate 			 * the different_export to get new export info.
26817c478bd9Sstevel@tonic-gate 			 */
26827c478bd9Sstevel@tonic-gate 			different_export = nfs_exported(cs->exi, cs->vp);
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 	}
26857c478bd9Sstevel@tonic-gate 
2686da6c28aaSamw 	error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr,
2687da6c28aaSamw 	    NULL, NULL, NULL);
26887c478bd9Sstevel@tonic-gate 	if (error)
26897c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	/*
26927c478bd9Sstevel@tonic-gate 	 * If the vnode is in a pseudo filesystem, check whether it is visible.
26937c478bd9Sstevel@tonic-gate 	 *
26947c478bd9Sstevel@tonic-gate 	 * XXX if the vnode is a symlink and it is not visible in
26957c478bd9Sstevel@tonic-gate 	 * a pseudo filesystem, return ENOENT (not following symlink).
26967c478bd9Sstevel@tonic-gate 	 * V4 client can not mount such symlink. This is a regression
26977c478bd9Sstevel@tonic-gate 	 * from V2/V3.
26987c478bd9Sstevel@tonic-gate 	 *
26997c478bd9Sstevel@tonic-gate 	 * In the same exported filesystem, if the security flavor used
27007c478bd9Sstevel@tonic-gate 	 * is not an explicitly shared flavor, limit the view to the visible
27017c478bd9Sstevel@tonic-gate 	 * list entries only. This is not a WRONGSEC case because it's already
27027c478bd9Sstevel@tonic-gate 	 * checked via PUTROOTFH/PUTPUBFH or PUTFH.
27037c478bd9Sstevel@tonic-gate 	 */
27047c478bd9Sstevel@tonic-gate 	if (!different_export &&
27057c478bd9Sstevel@tonic-gate 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
27067c478bd9Sstevel@tonic-gate 	    cs->access & CS_ACCESS_LIMITED)) {
27077c478bd9Sstevel@tonic-gate 		if (! nfs_visible(cs->exi, vp, &different_export)) {
27087c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27097c478bd9Sstevel@tonic-gate 			return (puterrno4(ENOENT));
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 	}
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	/*
27147c478bd9Sstevel@tonic-gate 	 * If it's a mountpoint, then traverse it.
27157c478bd9Sstevel@tonic-gate 	 */
27167c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(vp)) {
27177c478bd9Sstevel@tonic-gate 		pre_exi = cs->exi;	/* save pre-traversed exportinfo */
27187c478bd9Sstevel@tonic-gate 		pre_tvp = vp;		/* save pre-traversed vnode	*/
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 		/*
27217c478bd9Sstevel@tonic-gate 		 * hold pre_tvp to counteract rele by traverse.  We will
2722fbd2e8e1SArne Jansen 		 * need pre_tvp below if checkexport fails
27237c478bd9Sstevel@tonic-gate 		 */
27247c478bd9Sstevel@tonic-gate 		VN_HOLD(pre_tvp);
27256e062f4aSMarcel Telka 		if ((error = traverse(&vp)) != 0) {
27267c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27277c478bd9Sstevel@tonic-gate 			VN_RELE(pre_tvp);
27287c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
27297c478bd9Sstevel@tonic-gate 		}
27307c478bd9Sstevel@tonic-gate 		different_export = 1;
27317c478bd9Sstevel@tonic-gate 	} else if (vp->v_vfsp != cs->vp->v_vfsp) {
27327c478bd9Sstevel@tonic-gate 		/*
27337c478bd9Sstevel@tonic-gate 		 * The vfsp comparison is to handle the case where
27347c478bd9Sstevel@tonic-gate 		 * a LOFS mount is shared.  lo_lookup traverses mount points,
27357c478bd9Sstevel@tonic-gate 		 * and NFS is unaware of local fs transistions because
27367c478bd9Sstevel@tonic-gate 		 * v_vfsmountedhere isn't set.  For this special LOFS case,
27377c478bd9Sstevel@tonic-gate 		 * the dir and the obj returned by lookup will have different
27387c478bd9Sstevel@tonic-gate 		 * vfs ptrs.
27397c478bd9Sstevel@tonic-gate 		 */
27407c478bd9Sstevel@tonic-gate 		different_export = 1;
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	if (different_export) {
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 		bzero(&fid, sizeof (fid));
27467c478bd9Sstevel@tonic-gate 		fid.fid_len = MAXFIDSZ;
27477c478bd9Sstevel@tonic-gate 		error = vop_fid_pseudo(vp, &fid);
27487c478bd9Sstevel@tonic-gate 		if (error) {
27497c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27507c478bd9Sstevel@tonic-gate 			if (pre_tvp)
27517c478bd9Sstevel@tonic-gate 				VN_RELE(pre_tvp);
27527c478bd9Sstevel@tonic-gate 			return (puterrno4(error));
27537c478bd9Sstevel@tonic-gate 		}
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 		if (dotdot)
27567c478bd9Sstevel@tonic-gate 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
27577c478bd9Sstevel@tonic-gate 		else
2758fbd2e8e1SArne Jansen 			exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid, vp);
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 		if (exi == NULL) {
27617c478bd9Sstevel@tonic-gate 			if (pre_tvp) {
27627c478bd9Sstevel@tonic-gate 				/*
27637c478bd9Sstevel@tonic-gate 				 * If this vnode is a mounted-on vnode,
27647c478bd9Sstevel@tonic-gate 				 * but the mounted-on file system is not
27657c478bd9Sstevel@tonic-gate 				 * exported, send back the filehandle for
27667c478bd9Sstevel@tonic-gate 				 * the mounted-on vnode, not the root of
27677c478bd9Sstevel@tonic-gate 				 * the mounted-on file system.
27687c478bd9Sstevel@tonic-gate 				 */
27697c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
27707c478bd9Sstevel@tonic-gate 				vp = pre_tvp;
27717c478bd9Sstevel@tonic-gate 				exi = pre_exi;
2772aafcd32bSMarcel Telka 				if (exi != NULL)
2773fbd2e8e1SArne Jansen 					exi_hold(exi);
27747c478bd9Sstevel@tonic-gate 			} else {
27757c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
27767c478bd9Sstevel@tonic-gate 				return (puterrno4(EACCES));
27777c478bd9Sstevel@tonic-gate 			}
27787c478bd9Sstevel@tonic-gate 		} else if (pre_tvp) {
27797c478bd9Sstevel@tonic-gate 			/* we're done with pre_tvp now. release extra hold */
27807c478bd9Sstevel@tonic-gate 			VN_RELE(pre_tvp);
27817c478bd9Sstevel@tonic-gate 		}
27827c478bd9Sstevel@tonic-gate 
2783aafcd32bSMarcel Telka 		if (cs->exi != NULL)
2784fbd2e8e1SArne Jansen 			exi_rele(cs->exi);
27857c478bd9Sstevel@tonic-gate 		cs->exi = exi;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 		/*
27887c478bd9Sstevel@tonic-gate 		 * Now we do a checkauth4. The reason is that
27897c478bd9Sstevel@tonic-gate 		 * this client/user may not have access to the new
27907c478bd9Sstevel@tonic-gate 		 * exported file system, and if he does,
27917c478bd9Sstevel@tonic-gate 		 * the client/user may be mapped to a different uid.
27927c478bd9Sstevel@tonic-gate 		 *
27937c478bd9Sstevel@tonic-gate 		 * We start with a new cr, because the checkauth4 done
27947c478bd9Sstevel@tonic-gate 		 * in the PUT*FH operation over wrote the cred's uid,
27957c478bd9Sstevel@tonic-gate 		 * gid, etc, and we want the real thing before calling
27967c478bd9Sstevel@tonic-gate 		 * checkauth4()
27977c478bd9Sstevel@tonic-gate 		 */
27987c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
27997c478bd9Sstevel@tonic-gate 		cs->cr = crdup(cs->basecr);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 		oldvp = cs->vp;
28027c478bd9Sstevel@tonic-gate 		cs->vp = vp;
28037c478bd9Sstevel@tonic-gate 		is_newvp = TRUE;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 		stat = call_checkauth4(cs, req);
28067c478bd9Sstevel@tonic-gate 		if (stat != NFS4_OK) {
28077c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
28087c478bd9Sstevel@tonic-gate 			cs->vp = oldvp;
28097c478bd9Sstevel@tonic-gate 			return (stat);
28107c478bd9Sstevel@tonic-gate 		}
28117c478bd9Sstevel@tonic-gate 	}
28127c478bd9Sstevel@tonic-gate 
281345916cd2Sjpk 	/*
281445916cd2Sjpk 	 * After various NFS checks, do a label check on the path
281545916cd2Sjpk 	 * component. The label on this path should either be the
281645916cd2Sjpk 	 * global zone's label or a zone's label. We are only
281745916cd2Sjpk 	 * interested in the zone's label because exported files
281845916cd2Sjpk 	 * in global zone is accessible (though read-only) to
281945916cd2Sjpk 	 * clients. The exportability/visibility check is already
282045916cd2Sjpk 	 * done before reaching this code.
282145916cd2Sjpk 	 */
282245916cd2Sjpk 	if (is_system_labeled()) {
282345916cd2Sjpk 		bslabel_t *clabel;
282445916cd2Sjpk 
282545916cd2Sjpk 		ASSERT(req->rq_label != NULL);
282645916cd2Sjpk 		clabel = req->rq_label;
282745916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *,
282845916cd2Sjpk 		    "got client label from request(1)", struct svc_req *, req);
282945916cd2Sjpk 
283045916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2831bd6f1640SJarrett Lu 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
2832bd6f1640SJarrett Lu 			    cs->exi)) {
283345916cd2Sjpk 				error = EACCES;
283445916cd2Sjpk 				goto err_out;
283545916cd2Sjpk 			}
283645916cd2Sjpk 		} else {
283745916cd2Sjpk 			/*
283845916cd2Sjpk 			 * We grant access to admin_low label clients
283945916cd2Sjpk 			 * only if the client is trusted, i.e. also
284045916cd2Sjpk 			 * running Solaris Trusted Extension.
284145916cd2Sjpk 			 */
284245916cd2Sjpk 			struct sockaddr	*ca;
284345916cd2Sjpk 			int		addr_type;
284445916cd2Sjpk 			void		*ipaddr;
284545916cd2Sjpk 			tsol_tpc_t	*tp;
284645916cd2Sjpk 
284745916cd2Sjpk 			ca = (struct sockaddr *)svc_getrpccaller(
284845916cd2Sjpk 			    req->rq_xprt)->buf;
284945916cd2Sjpk 			if (ca->sa_family == AF_INET) {
285045916cd2Sjpk 				addr_type = IPV4_VERSION;
285145916cd2Sjpk 				ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
285245916cd2Sjpk 			} else if (ca->sa_family == AF_INET6) {
285345916cd2Sjpk 				addr_type = IPV6_VERSION;
285445916cd2Sjpk 				ipaddr = &((struct sockaddr_in6 *)
285545916cd2Sjpk 				    ca)->sin6_addr;
285645916cd2Sjpk 			}
285745916cd2Sjpk 			tp = find_tpc(ipaddr, addr_type, B_FALSE);
285845916cd2Sjpk 			if (tp == NULL || tp->tpc_tp.tp_doi !=
285945916cd2Sjpk 			    l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
286045916cd2Sjpk 			    SUN_CIPSO) {
286103986916Sjarrett 				if (tp != NULL)
286203986916Sjarrett 					TPC_RELE(tp);
286345916cd2Sjpk 				error = EACCES;
286445916cd2Sjpk 				goto err_out;
286545916cd2Sjpk 			}
286603986916Sjarrett 			TPC_RELE(tp);
286745916cd2Sjpk 		}
286845916cd2Sjpk 	}
286945916cd2Sjpk 
28707c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
28717c478bd9Sstevel@tonic-gate 
287245916cd2Sjpk err_out:
28737c478bd9Sstevel@tonic-gate 	if (error) {
28747c478bd9Sstevel@tonic-gate 		if (is_newvp) {
28757c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
28767c478bd9Sstevel@tonic-gate 			cs->vp = oldvp;
28777c478bd9Sstevel@tonic-gate 		} else
28787c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
28797c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
28807c478bd9Sstevel@tonic-gate 	}
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	if (!is_newvp) {
28837c478bd9Sstevel@tonic-gate 		if (cs->vp)
28847c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
28857c478bd9Sstevel@tonic-gate 		cs->vp = vp;
28867c478bd9Sstevel@tonic-gate 	} else if (oldvp)
28877c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	/*
28907c478bd9Sstevel@tonic-gate 	 * if did lookup on attrdir and didn't lookup .., set named
28917c478bd9Sstevel@tonic-gate 	 * attr fh flag
28927c478bd9Sstevel@tonic-gate 	 */
28937c478bd9Sstevel@tonic-gate 	if (attrdir && ! dotdot)
28947c478bd9Sstevel@tonic-gate 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	/* Assume false for now, open proc will set this */
28977c478bd9Sstevel@tonic-gate 	cs->mandlock = FALSE;
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
29007c478bd9Sstevel@tonic-gate }
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate /* ARGSUSED */
29037c478bd9Sstevel@tonic-gate static void
29047c478bd9Sstevel@tonic-gate rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
29057c478bd9Sstevel@tonic-gate     struct compound_state *cs)
29067c478bd9Sstevel@tonic-gate {
29077c478bd9Sstevel@tonic-gate 	LOOKUP4args *args = &argop->nfs_argop4_u.oplookup;
29087c478bd9Sstevel@tonic-gate 	LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup;
29097c478bd9Sstevel@tonic-gate 	char *nm;
29107c478bd9Sstevel@tonic-gate 	uint_t len;
2911b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
2912b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
291315721462SDaniil Lunev 	nfsstat4 status;
29147c478bd9Sstevel@tonic-gate 
2915f3b585ceSsamf 	DTRACE_NFSV4_2(op__lookup__start, struct compound_state *, cs,
2916f3b585ceSsamf 	    LOOKUP4args *, args);
2917f3b585ceSsamf 
29187c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
29197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2920f3b585ceSsamf 		goto out;
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type == VLNK) {
29247c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SYMLINK;
2925f3b585ceSsamf 		goto out;
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
29297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2930f3b585ceSsamf 		goto out;
29317c478bd9Sstevel@tonic-gate 	}
29327c478bd9Sstevel@tonic-gate 
293315721462SDaniil Lunev 	status = utf8_dir_verify(&args->objname);
293415721462SDaniil Lunev 	if (status != NFS4_OK) {
293515721462SDaniil Lunev 		*cs->statusp = resp->status = status;
2936f3b585ceSsamf 		goto out;
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	nm = utf8_to_str(&args->objname, &len, NULL);
29407c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
29417c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2942f3b585ceSsamf 		goto out;
29437c478bd9Sstevel@tonic-gate 	}
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
29467c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
29477c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
2948f3b585ceSsamf 		goto out;
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate 
2951b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2952b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
2953b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
2954b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2955b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
2956b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2957b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, len);
2958b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
2959b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
2960b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2961593cc11bSJan Kryl 	*cs->statusp = resp->status = do_rfs4_op_lookup(name, req, cs);
2962b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2963b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name != nm)
2964b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
29657c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
2966f3b585ceSsamf 
2967f3b585ceSsamf out:
2968f3b585ceSsamf 	DTRACE_NFSV4_2(op__lookup__done, struct compound_state *, cs,
2969f3b585ceSsamf 	    LOOKUP4res *, resp);
29707c478bd9Sstevel@tonic-gate }
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate /* ARGSUSED */
29737c478bd9Sstevel@tonic-gate static void
29747c478bd9Sstevel@tonic-gate rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
29757c478bd9Sstevel@tonic-gate     struct compound_state *cs)
29767c478bd9Sstevel@tonic-gate {
29777c478bd9Sstevel@tonic-gate 	LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp;
29787c478bd9Sstevel@tonic-gate 
2979f3b585ceSsamf 	DTRACE_NFSV4_1(op__lookupp__start, struct compound_state *, cs);
2980f3b585ceSsamf 
29817c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
29827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2983f3b585ceSsamf 		goto out;
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
29877c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2988f3b585ceSsamf 		goto out;
29897c478bd9Sstevel@tonic-gate 	}
29907c478bd9Sstevel@tonic-gate 
2991593cc11bSJan Kryl 	*cs->statusp = resp->status = do_rfs4_op_lookup("..", req, cs);
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/*
29947c478bd9Sstevel@tonic-gate 	 * From NFSV4 Specification, LOOKUPP should not check for
29957c478bd9Sstevel@tonic-gate 	 * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead.
29967c478bd9Sstevel@tonic-gate 	 */
29977c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4ERR_WRONGSEC) {
29987c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
29997c478bd9Sstevel@tonic-gate 	}
3000f3b585ceSsamf 
3001f3b585ceSsamf out:
3002f3b585ceSsamf 	DTRACE_NFSV4_2(op__lookupp__done, struct compound_state *, cs,
3003f3b585ceSsamf 	    LOOKUPP4res *, resp);
30047c478bd9Sstevel@tonic-gate }
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
30087c478bd9Sstevel@tonic-gate static void
30097c478bd9Sstevel@tonic-gate rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
30107c478bd9Sstevel@tonic-gate     struct compound_state *cs)
30117c478bd9Sstevel@tonic-gate {
30127c478bd9Sstevel@tonic-gate 	OPENATTR4args	*args = &argop->nfs_argop4_u.opopenattr;
30137c478bd9Sstevel@tonic-gate 	OPENATTR4res	*resp = &resop->nfs_resop4_u.opopenattr;
30147c478bd9Sstevel@tonic-gate 	vnode_t		*avp = NULL;
30157c478bd9Sstevel@tonic-gate 	int		lookup_flags = LOOKUP_XATTR, error;
30167c478bd9Sstevel@tonic-gate 	int		exp_ro = 0;
30177c478bd9Sstevel@tonic-gate 
3018f3b585ceSsamf 	DTRACE_NFSV4_2(op__openattr__start, struct compound_state *, cs,
3019f3b585ceSsamf 	    OPENATTR4args *, args);
3020f3b585ceSsamf 
30217c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
30227c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3023f3b585ceSsamf 		goto out;
30247c478bd9Sstevel@tonic-gate 	}
30257c478bd9Sstevel@tonic-gate 
3026da6c28aaSamw 	if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 &&
30279660e5cbSJanice Chang 	    !vfs_has_feature(cs->vp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
3028f3b585ceSsamf 		*cs->statusp = resp->status = puterrno4(ENOTSUP);
3029f3b585ceSsamf 		goto out;
30307c478bd9Sstevel@tonic-gate 	}
30317c478bd9Sstevel@tonic-gate 
303293aeed83Smarks 	/*
303393aeed83Smarks 	 * If file system supports passing ACE mask to VOP_ACCESS then
303493aeed83Smarks 	 * check for ACE_READ_NAMED_ATTRS, otherwise do legacy checks
303593aeed83Smarks 	 */
303693aeed83Smarks 
303793aeed83Smarks 	if (vfs_has_feature(cs->vp->v_vfsp, VFSFT_ACEMASKONACCESS))
303893aeed83Smarks 		error = VOP_ACCESS(cs->vp, ACE_READ_NAMED_ATTRS,
303993aeed83Smarks 		    V_ACE_MASK, cs->cr, NULL);
304093aeed83Smarks 	else
304193aeed83Smarks 		error = ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) &&
3042da6c28aaSamw 		    (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) &&
304393aeed83Smarks 		    (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0));
304493aeed83Smarks 
304593aeed83Smarks 	if (error) {
3046f3b585ceSsamf 		*cs->statusp = resp->status = puterrno4(EACCES);
3047f3b585ceSsamf 		goto out;
30487c478bd9Sstevel@tonic-gate 	}
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	/*
30517c478bd9Sstevel@tonic-gate 	 * The CREATE_XATTR_DIR VOP flag cannot be specified if
30527c478bd9Sstevel@tonic-gate 	 * the file system is exported read-only -- regardless of
30537c478bd9Sstevel@tonic-gate 	 * createdir flag.  Otherwise the attrdir would be created
30547c478bd9Sstevel@tonic-gate 	 * (assuming server fs isn't mounted readonly locally).  If
30557c478bd9Sstevel@tonic-gate 	 * VOP_LOOKUP returns ENOENT in this case, the error will
30567c478bd9Sstevel@tonic-gate 	 * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
30577c478bd9Sstevel@tonic-gate 	 * because specfs has no VOP_LOOKUP op, so the macro would
30587c478bd9Sstevel@tonic-gate 	 * return ENOSYS.  EINVAL is returned by all (current)
30597c478bd9Sstevel@tonic-gate 	 * Solaris file system implementations when any of their
30607c478bd9Sstevel@tonic-gate 	 * restrictions are violated (xattr(dir) can't have xattrdir).
30617c478bd9Sstevel@tonic-gate 	 * Returning NOTSUPP is more appropriate in this case
30627c478bd9Sstevel@tonic-gate 	 * because the object will never be able to have an attrdir.
30637c478bd9Sstevel@tonic-gate 	 */
30645cb0d679SMarcel Telka 	if (args->createdir && ! (exp_ro = rdonly4(req, cs)))
30657c478bd9Sstevel@tonic-gate 		lookup_flags |= CREATE_XATTR_DIR;
30667c478bd9Sstevel@tonic-gate 
3067da6c28aaSamw 	error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr,
3068da6c28aaSamw 	    NULL, NULL, NULL);
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	if (error) {
30717c478bd9Sstevel@tonic-gate 		if (error == ENOENT && args->createdir && exp_ro)
3072f3b585ceSsamf 			*cs->statusp = resp->status = puterrno4(EROFS);
30737c478bd9Sstevel@tonic-gate 		else if (error == EINVAL || error == ENOSYS)
3074f3b585ceSsamf 			*cs->statusp = resp->status = puterrno4(ENOTSUP);
3075f3b585ceSsamf 		else
3076f3b585ceSsamf 			*cs->statusp = resp->status = puterrno4(error);
3077f3b585ceSsamf 		goto out;
30787c478bd9Sstevel@tonic-gate 	}
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 	ASSERT(avp->v_flag & V_XATTRDIR);
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, avp, cs->exi);
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	if (error) {
30857c478bd9Sstevel@tonic-gate 		VN_RELE(avp);
3086f3b585ceSsamf 		*cs->statusp = resp->status = puterrno4(error);
3087f3b585ceSsamf 		goto out;
30887c478bd9Sstevel@tonic-gate 	}
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	VN_RELE(cs->vp);
30917c478bd9Sstevel@tonic-gate 	cs->vp = avp;
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	/*
30947c478bd9Sstevel@tonic-gate 	 * There is no requirement for an attrdir fh flag
30957c478bd9Sstevel@tonic-gate 	 * because the attrdir has a vnode flag to distinguish
30967c478bd9Sstevel@tonic-gate 	 * it from regular (non-xattr) directories.  The
30977c478bd9Sstevel@tonic-gate 	 * FH4_ATTRDIR flag is set for future sanity checks.
30987c478bd9Sstevel@tonic-gate 	 */
30997c478bd9Sstevel@tonic-gate 	set_fh4_flag(&cs->fh, FH4_ATTRDIR);
31007c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
31017c478bd9Sstevel@tonic-gate 
3102f3b585ceSsamf out:
3103f3b585ceSsamf 	DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs,
3104f3b585ceSsamf 	    OPENATTR4res *, resp);
31057c478bd9Sstevel@tonic-gate }
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate static int
3108da6c28aaSamw do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred,
3109da6c28aaSamw     caller_context_t *ct)
31107c478bd9Sstevel@tonic-gate {
31117c478bd9Sstevel@tonic-gate 	int error;
31127c478bd9Sstevel@tonic-gate 	int i;
31137c478bd9Sstevel@tonic-gate 	clock_t delaytime;
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	/*
31187c478bd9Sstevel@tonic-gate 	 * Don't block on mandatory locks. If this routine returns
31197c478bd9Sstevel@tonic-gate 	 * EAGAIN, the caller should return NFS4ERR_LOCKED.
31207c478bd9Sstevel@tonic-gate 	 */
31217c478bd9Sstevel@tonic-gate 	uio->uio_fmode = FNONBLOCK;
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	for (i = 0; i < rfs4_maxlock_tries; i++) {
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 		if (direction == FREAD) {
3127da6c28aaSamw 			(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct);
3128da6c28aaSamw 			error = VOP_READ(vp, uio, ioflag, cred, ct);
3129da6c28aaSamw 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct);
31307c478bd9Sstevel@tonic-gate 		} else {
3131da6c28aaSamw 			(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct);
3132da6c28aaSamw 			error = VOP_WRITE(vp, uio, ioflag, cred, ct);
3133da6c28aaSamw 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct);
31347c478bd9Sstevel@tonic-gate 		}
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 		if (error != EAGAIN)
31377c478bd9Sstevel@tonic-gate 			break;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 		if (i < rfs4_maxlock_tries - 1) {
31407c478bd9Sstevel@tonic-gate 			delay(delaytime);
31417c478bd9Sstevel@tonic-gate 			delaytime *= 2;
31427c478bd9Sstevel@tonic-gate 		}
31437c478bd9Sstevel@tonic-gate 	}
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	return (error);
31467c478bd9Sstevel@tonic-gate }
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate /* ARGSUSED */
31497c478bd9Sstevel@tonic-gate static void
31507c478bd9Sstevel@tonic-gate rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
31517c478bd9Sstevel@tonic-gate     struct compound_state *cs)
31527c478bd9Sstevel@tonic-gate {
31537c478bd9Sstevel@tonic-gate 	READ4args *args = &argop->nfs_argop4_u.opread;
31547c478bd9Sstevel@tonic-gate 	READ4res *resp = &resop->nfs_resop4_u.opread;
31557c478bd9Sstevel@tonic-gate 	int error;
31567c478bd9Sstevel@tonic-gate 	int verror;
31577c478bd9Sstevel@tonic-gate 	vnode_t *vp;
31587c478bd9Sstevel@tonic-gate 	struct vattr va;
3159e36d7b11SSebastien Roy 	struct iovec iov, *iovp = NULL;
3160e36d7b11SSebastien Roy 	int iovcnt;
31617c478bd9Sstevel@tonic-gate 	struct uio uio;
31627c478bd9Sstevel@tonic-gate 	u_offset_t offset;
31637c478bd9Sstevel@tonic-gate 	bool_t *deleg = &cs->deleg;
31647c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
31657c478bd9Sstevel@tonic-gate 	int in_crit = 0;
3166c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	mblk_t *mp = NULL;
31677c478bd9Sstevel@tonic-gate 	int alloc_err = 0;
3168c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int rdma_used = 0;
3169c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	int loaned_buffers;
3170da6c28aaSamw 	caller_context_t ct;
3171c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	struct uio *uiop;
31727c478bd9Sstevel@tonic-gate 
3173f3b585ceSsamf 	DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs,
3174f3b585ceSsamf 	    READ4args, args);
3175f3b585ceSsamf 
31767c478bd9Sstevel@tonic-gate 	vp = cs->vp;
31777c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
31787c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3179f3b585ceSsamf 		goto out;
31807c478bd9Sstevel@tonic-gate 	}
31817c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
31827c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3183f3b585ceSsamf 		goto out;
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate 
3186da6c28aaSamw 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3187da6c28aaSamw 	    deleg, TRUE, &ct)) != NFS4_OK) {
3188da6c28aaSamw 		*cs->statusp = resp->status = stat;
3189da6c28aaSamw 		goto out;
3190da6c28aaSamw 	}
3191da6c28aaSamw 
31927c478bd9Sstevel@tonic-gate 	/*
31937c478bd9Sstevel@tonic-gate 	 * Enter the critical region before calling VOP_RWLOCK
31947c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with write requests.
31957c478bd9Sstevel@tonic-gate 	 */
31967c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
31977c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
31987c478bd9Sstevel@tonic-gate 		in_crit = 1;
3199da6c28aaSamw 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
3200da6c28aaSamw 		    &ct)) {
32017c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
32027c478bd9Sstevel@tonic-gate 			goto out;
32037c478bd9Sstevel@tonic-gate 		}
32047c478bd9Sstevel@tonic-gate 	}
32057c478bd9Sstevel@tonic-gate 
320635bbd688SKaren Rochford 	if (args->wlist) {
320735bbd688SKaren Rochford 		if (args->count > clist_len(args->wlist)) {
320835bbd688SKaren Rochford 			*cs->statusp = resp->status = NFS4ERR_INVAL;
320935bbd688SKaren Rochford 			goto out;
321035bbd688SKaren Rochford 		}
3211c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		rdma_used = 1;
321235bbd688SKaren Rochford 	}
3213c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3214c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* use loaned buffers for TCP */
3215c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	loaned_buffers = (nfs_loaned_buffers && !rdma_used) ? 1 : 0;
3216c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
32177c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE|AT_SIZE|AT_UID;
3218da6c28aaSamw 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	/*
32217c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
32227c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
32237c478bd9Sstevel@tonic-gate 	 */
32247c478bd9Sstevel@tonic-gate 	if (verror) {
32257c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(verror);
32267c478bd9Sstevel@tonic-gate 		goto out;
32277c478bd9Sstevel@tonic-gate 	}
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
32307c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
32317c478bd9Sstevel@tonic-gate 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
32327c478bd9Sstevel@tonic-gate 		goto out;
32337c478bd9Sstevel@tonic-gate 	}
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	if (crgetuid(cs->cr) != va.va_uid &&
3236da6c28aaSamw 	    (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) &&
3237da6c28aaSamw 	    (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) {
32387c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
32397c478bd9Sstevel@tonic-gate 		goto out;
32407c478bd9Sstevel@tonic-gate 	}
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
32437c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
32447c478bd9Sstevel@tonic-gate 		goto out;
32457c478bd9Sstevel@tonic-gate 	}
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	offset = args->offset;
32487c478bd9Sstevel@tonic-gate 	if (offset >= va.va_size) {
32497c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
32507c478bd9Sstevel@tonic-gate 		resp->eof = TRUE;
32517c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
32527c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
32537c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
32540a701b1eSRobert Gordon 		/* RDMA */
32550a701b1eSRobert Gordon 		resp->wlist = args->wlist;
32560a701b1eSRobert Gordon 		resp->wlist_len = resp->data_len;
32577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
3258f837ee4aSSiddheshwar Mahesh 		if (resp->wlist)
3259f837ee4aSSiddheshwar Mahesh 			clist_zero_len(resp->wlist);
32607c478bd9Sstevel@tonic-gate 		goto out;
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if (args->count == 0) {
32647c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
32657c478bd9Sstevel@tonic-gate 		resp->eof = FALSE;
32667c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
32677c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
32687c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
32690a701b1eSRobert Gordon 		/* RDMA */
32700a701b1eSRobert Gordon 		resp->wlist = args->wlist;
32710a701b1eSRobert Gordon 		resp->wlist_len = resp->data_len;
3272f837ee4aSSiddheshwar Mahesh 		if (resp->wlist)
3273f837ee4aSSiddheshwar Mahesh 			clist_zero_len(resp->wlist);
32747c478bd9Sstevel@tonic-gate 		goto out;
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	/*
32787c478bd9Sstevel@tonic-gate 	 * Do not allocate memory more than maximum allowed
32797c478bd9Sstevel@tonic-gate 	 * transfer size
32807c478bd9Sstevel@tonic-gate 	 */
32817c478bd9Sstevel@tonic-gate 	if (args->count > rfs4_tsize(req))
32827c478bd9Sstevel@tonic-gate 		args->count = rfs4_tsize(req);
32837c478bd9Sstevel@tonic-gate 
3284c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (loaned_buffers) {
3285c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		uiop = (uio_t *)rfs_setup_xuio(vp);
3286c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		ASSERT(uiop != NULL);
3287c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		uiop->uio_segflg = UIO_SYSSPACE;
3288c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		uiop->uio_loffset = args->offset;
3289c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		uiop->uio_resid = args->count;
3290c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3291c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		/* Jump to do the read if successful */
3292c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (!VOP_REQZCBUF(vp, UIO_READ, (xuio_t *)uiop, cs->cr, &ct)) {
3293c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			/*
3294c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			 * Need to hold the vnode until after VOP_RETZCBUF()
3295c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			 * is called.
3296c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			 */
3297c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			VN_HOLD(vp);
3298c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			goto doio_read;
3299c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		}
3300c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3301c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		DTRACE_PROBE2(nfss__i__reqzcbuf_failed, int,
3302c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		    uiop->uio_loffset, int, uiop->uio_resid);
3303c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3304c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		uiop->uio_extflg = 0;
3305c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3306c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		/* failure to setup for zero copy */
3307c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		rfs_free_xuio((void *)uiop);
3308c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		loaned_buffers = 0;
3309c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
3310c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
33117c478bd9Sstevel@tonic-gate 	/*
33120a701b1eSRobert Gordon 	 * If returning data via RDMA Write, then grab the chunk list. If we
33130a701b1eSRobert Gordon 	 * aren't returning READ data w/RDMA_WRITE, then grab a mblk.
33140a701b1eSRobert Gordon 	 */
3315c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (rdma_used) {
33160a701b1eSRobert Gordon 		mp = NULL;
33170a701b1eSRobert Gordon 		(void) rdma_get_wchunk(req, &iov, args->wlist);
3318e36d7b11SSebastien Roy 		uio.uio_iov = &iov;
3319e36d7b11SSebastien Roy 		uio.uio_iovcnt = 1;
33200a701b1eSRobert Gordon 	} else {
33210a701b1eSRobert Gordon 		/*
33227c478bd9Sstevel@tonic-gate 		 * mp will contain the data to be sent out in the read reply.
3323e36d7b11SSebastien Roy 		 * It will be freed after the reply has been sent.
33247c478bd9Sstevel@tonic-gate 		 */
3325e36d7b11SSebastien Roy 		mp = rfs_read_alloc(args->count, &iovp, &iovcnt);
33267c478bd9Sstevel@tonic-gate 		ASSERT(mp != NULL);
33277c478bd9Sstevel@tonic-gate 		ASSERT(alloc_err == 0);
3328e36d7b11SSebastien Roy 		uio.uio_iov = iovp;
3329e36d7b11SSebastien Roy 		uio.uio_iovcnt = iovcnt;
33300a701b1eSRobert Gordon 	}
33310a701b1eSRobert Gordon 
33327c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
33337c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
33347c478bd9Sstevel@tonic-gate 	uio.uio_loffset = args->offset;
33357c478bd9Sstevel@tonic-gate 	uio.uio_resid = args->count;
3336c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	uiop = &uio;
33377c478bd9Sstevel@tonic-gate 
3338c242f9a0Schunli zhang - Sun Microsystems - Irvine United States doio_read:
3339c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	error = do_io(FREAD, vp, uiop, 0, cs->cr, &ct);
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
3342da6c28aaSamw 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 	if (error) {
3345c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		if (mp)
3346c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 			freemsg(mp);
33477c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
33487c478bd9Sstevel@tonic-gate 		goto out;
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 
3351c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* make mblk using zc buffers */
3352c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (loaned_buffers) {
3353c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		mp = uio_to_mblk(uiop);
3354c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		ASSERT(mp != NULL);
3355c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	}
3356c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
33577c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
33587c478bd9Sstevel@tonic-gate 
3359c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	ASSERT(uiop->uio_resid >= 0);
3360c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	resp->data_len = args->count - uiop->uio_resid;
33610a701b1eSRobert Gordon 	if (mp) {
33627c478bd9Sstevel@tonic-gate 		resp->data_val = (char *)mp->b_datap->db_base;
3363c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		rfs_rndup_mblks(mp, resp->data_len, loaned_buffers);
33640a701b1eSRobert Gordon 	} else {
33650a701b1eSRobert Gordon 		resp->data_val = (caddr_t)iov.iov_base;
33660a701b1eSRobert Gordon 	}
3367c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
33687c478bd9Sstevel@tonic-gate 	resp->mblk = mp;
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	if (!verror && offset + resp->data_len == va.va_size)
33717c478bd9Sstevel@tonic-gate 		resp->eof = TRUE;
33727c478bd9Sstevel@tonic-gate 	else
33737c478bd9Sstevel@tonic-gate 		resp->eof = FALSE;
33747c478bd9Sstevel@tonic-gate 
3375c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if (rdma_used) {
33760a701b1eSRobert Gordon 		if (!rdma_setup_read_data4(args, resp)) {
33770a701b1eSRobert Gordon 			*cs->statusp = resp->status = NFS4ERR_INVAL;
33780a701b1eSRobert Gordon 		}
33790a701b1eSRobert Gordon 	} else {
33800a701b1eSRobert Gordon 		resp->wlist = NULL;
33810a701b1eSRobert Gordon 	}
33820a701b1eSRobert Gordon 
33837c478bd9Sstevel@tonic-gate out:
33847c478bd9Sstevel@tonic-gate 	if (in_crit)
33857c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
3386f3b585ceSsamf 
3387e36d7b11SSebastien Roy 	if (iovp != NULL)
3388e36d7b11SSebastien Roy 		kmem_free(iovp, iovcnt * sizeof (struct iovec));
3389e36d7b11SSebastien Roy 
3390f3b585ceSsamf 	DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs,
3391f3b585ceSsamf 	    READ4res *, resp);
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate static void
33957c478bd9Sstevel@tonic-gate rfs4_op_read_free(nfs_resop4 *resop)
33967c478bd9Sstevel@tonic-gate {
33977c478bd9Sstevel@tonic-gate 	READ4res	*resp = &resop->nfs_resop4_u.opread;
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3400c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		freemsg(resp->mblk);
34017c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
34027c478bd9Sstevel@tonic-gate 		resp->data_val = NULL;
34037c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
34047c478bd9Sstevel@tonic-gate 	}
34057c478bd9Sstevel@tonic-gate }
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate static void
34087c478bd9Sstevel@tonic-gate rfs4_op_readdir_free(nfs_resop4 * resop)
34097c478bd9Sstevel@tonic-gate {
34107c478bd9Sstevel@tonic-gate 	READDIR4res    *resp = &resop->nfs_resop4_u.opreaddir;
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
34137c478bd9Sstevel@tonic-gate 		freeb(resp->mblk);
34147c478bd9Sstevel@tonic-gate 		resp->mblk = NULL;
34157c478bd9Sstevel@tonic-gate 		resp->data_len = 0;
34167c478bd9Sstevel@tonic-gate 	}
34177c478bd9Sstevel@tonic-gate }
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate /* ARGSUSED */
34217c478bd9Sstevel@tonic-gate static void
34227c478bd9Sstevel@tonic-gate rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
34237c478bd9Sstevel@tonic-gate     struct compound_state *cs)
34247c478bd9Sstevel@tonic-gate {
34257c478bd9Sstevel@tonic-gate 	PUTPUBFH4res	*resp = &resop->nfs_resop4_u.opputpubfh;
34267c478bd9Sstevel@tonic-gate 	int		error;
34277c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
34287c478bd9Sstevel@tonic-gate 	struct exportinfo *exi, *sav_exi;
34297c478bd9Sstevel@tonic-gate 	nfs_fh4_fmt_t	*fh_fmtp;
34307c478bd9Sstevel@tonic-gate 
3431f3b585ceSsamf 	DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs);
3432f3b585ceSsamf 
34337c478bd9Sstevel@tonic-gate 	if (cs->vp) {
34347c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
34357c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	if (cs->cr)
34397c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
34427c478bd9Sstevel@tonic-gate 
3443aafcd32bSMarcel Telka 	rw_enter(&exported_lock, RW_READER);
3444aafcd32bSMarcel Telka 	exi = exi_public;
3445aafcd32bSMarcel Telka 	exi_hold(exi);
3446aafcd32bSMarcel Telka 	rw_exit(&exported_lock);
3447aafcd32bSMarcel Telka 
3448aafcd32bSMarcel Telka 	vp = exi->exi_vp;
34497c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
3450aafcd32bSMarcel Telka 		exi_rele(exi);
34517c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3452f3b585ceSsamf 		goto out;
34537c478bd9Sstevel@tonic-gate 	}
34547c478bd9Sstevel@tonic-gate 
3455aafcd32bSMarcel Telka 	error = makefh4(&cs->fh, vp, exi);
34567c478bd9Sstevel@tonic-gate 	if (error != 0) {
3457aafcd32bSMarcel Telka 		exi_rele(exi);
34587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
3459f3b585ceSsamf 		goto out;
34607c478bd9Sstevel@tonic-gate 	}
3461aafcd32bSMarcel Telka 
34627c478bd9Sstevel@tonic-gate 	sav_exi = cs->exi;
3463aafcd32bSMarcel Telka 	cs->exi = exi;
3464aafcd32bSMarcel Telka 
3465aafcd32bSMarcel Telka 	if (cs->exi == exi_root) {
34667c478bd9Sstevel@tonic-gate 		/*
34677c478bd9Sstevel@tonic-gate 		 * No filesystem is actually shared public, so we default
34687c478bd9Sstevel@tonic-gate 		 * to exi_root. In this case, we must check whether root
34697c478bd9Sstevel@tonic-gate 		 * is exported.
34707c478bd9Sstevel@tonic-gate 		 */
34717c478bd9Sstevel@tonic-gate 		fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 		/*
34747c478bd9Sstevel@tonic-gate 		 * if root filesystem is exported, the exportinfo struct that we
3475aafcd32bSMarcel Telka 		 * should use is what checkexport returns, because exi_root is
34767c478bd9Sstevel@tonic-gate 		 * actually a mostly empty struct.
34777c478bd9Sstevel@tonic-gate 		 */
3478fbd2e8e1SArne Jansen 		exi = checkexport(&fh_fmtp->fh4_fsid,
34797c478bd9Sstevel@tonic-gate 		    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
3480aafcd32bSMarcel Telka 		if (exi != NULL) {
3481aafcd32bSMarcel Telka 			exi_rele(cs->exi);
3482fbd2e8e1SArne Jansen 			cs->exi = exi;
3483fbd2e8e1SArne Jansen 		}
34847c478bd9Sstevel@tonic-gate 	}
34857c478bd9Sstevel@tonic-gate 
3486bd6f1640SJarrett Lu 	if (is_system_labeled()) {
3487bd6f1640SJarrett Lu 		bslabel_t *clabel;
3488bd6f1640SJarrett Lu 
3489bd6f1640SJarrett Lu 		ASSERT(req->rq_label != NULL);
3490bd6f1640SJarrett Lu 		clabel = req->rq_label;
3491bd6f1640SJarrett Lu 		DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *,
3492bd6f1640SJarrett Lu 		    "got client label from request(1)",
3493bd6f1640SJarrett Lu 		    struct svc_req *, req);
3494bd6f1640SJarrett Lu 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
3495bd6f1640SJarrett Lu 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
3496bd6f1640SJarrett Lu 			    cs->exi)) {
3497bd6f1640SJarrett Lu 				*cs->statusp = resp->status =
3498bd6f1640SJarrett Lu 				    NFS4ERR_SERVERFAULT;
3499aafcd32bSMarcel Telka 				if (sav_exi != NULL)
3500fbd2e8e1SArne Jansen 					exi_rele(sav_exi);
3501bd6f1640SJarrett Lu 				goto out;
3502bd6f1640SJarrett Lu 			}
3503bd6f1640SJarrett Lu 		}
3504bd6f1640SJarrett Lu 	}
3505bd6f1640SJarrett Lu 
35067c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
35077c478bd9Sstevel@tonic-gate 	cs->vp = vp;
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
35107c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
35117c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
3512fbd2e8e1SArne Jansen 		exi_rele(cs->exi);
35137c478bd9Sstevel@tonic-gate 		cs->exi = sav_exi;
3514f3b585ceSsamf 		goto out;
35157c478bd9Sstevel@tonic-gate 	}
3516aafcd32bSMarcel Telka 	if (sav_exi != NULL)
3517fbd2e8e1SArne Jansen 		exi_rele(sav_exi);
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
3520f3b585ceSsamf out:
3521f3b585ceSsamf 	DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs,
3522f3b585ceSsamf 	    PUTPUBFH4res *, resp);
35237c478bd9Sstevel@tonic-gate }
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate /*
35267c478bd9Sstevel@tonic-gate  * XXX - issue with put*fh operations. Suppose /export/home is exported.
35277c478bd9Sstevel@tonic-gate  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
35287c478bd9Sstevel@tonic-gate  * or joe have restrictive search permissions, then we shouldn't let
35297c478bd9Sstevel@tonic-gate  * the client get a file handle. This is easy to enforce. However, we
35307c478bd9Sstevel@tonic-gate  * don't know what security flavor should be used until we resolve the
35317c478bd9Sstevel@tonic-gate  * path name. Another complication is uid mapping. If root is
35327c478bd9Sstevel@tonic-gate  * the user, then it will be mapped to the anonymous user by default,
35337c478bd9Sstevel@tonic-gate  * but we won't know that till we've resolved the path name. And we won't
35347c478bd9Sstevel@tonic-gate  * know what the anonymous user is.
35357c478bd9Sstevel@tonic-gate  * Luckily, SECINFO is specified to take a full filename.
35367c478bd9Sstevel@tonic-gate  * So what we will have to in rfs4_op_lookup is check that flavor of
35377c478bd9Sstevel@tonic-gate  * the target object matches that of the request, and if root was the
35387c478bd9Sstevel@tonic-gate  * caller, check for the root= and anon= options, and if necessary,
35397c478bd9Sstevel@tonic-gate  * repeat the lookup using the right cred_t. But that's not done yet.
35407c478bd9Sstevel@tonic-gate  */
35417c478bd9Sstevel@tonic-gate /* ARGSUSED */
35427c478bd9Sstevel@tonic-gate static void
35437c478bd9Sstevel@tonic-gate rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
35447c478bd9Sstevel@tonic-gate     struct compound_state *cs)
35457c478bd9Sstevel@tonic-gate {
35467c478bd9Sstevel@tonic-gate 	PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
35477c478bd9Sstevel@tonic-gate 	PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
35487c478bd9Sstevel@tonic-gate 	nfs_fh4_fmt_t *fh_fmtp;
35497c478bd9Sstevel@tonic-gate 
3550f3b585ceSsamf 	DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs,
3551f3b585ceSsamf 	    PUTFH4args *, args);
3552f3b585ceSsamf 
35537c478bd9Sstevel@tonic-gate 	if (cs->vp) {
35547c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
35557c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	if (cs->cr) {
35597c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
35607c478bd9Sstevel@tonic-gate 		cs->cr = NULL;
35617c478bd9Sstevel@tonic-gate 	}
35627c478bd9Sstevel@tonic-gate 
3563c93d332cSDan McDonald 
35647c478bd9Sstevel@tonic-gate 	if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
35657c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
3566f3b585ceSsamf 		goto out;
35677c478bd9Sstevel@tonic-gate 	}
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 	fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
3570aafcd32bSMarcel Telka 	if (cs->exi != NULL)
3571fbd2e8e1SArne Jansen 		exi_rele(cs->exi);
3572fbd2e8e1SArne Jansen 	cs->exi = checkexport(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
35737c478bd9Sstevel@tonic-gate 	    NULL);
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 	if (cs->exi == NULL) {
35767c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_STALE;
3577f3b585ceSsamf 		goto out;
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	ASSERT(cs->cr != NULL);
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
35857c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
3586f3b585ceSsamf 		goto out;
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
35907c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
35917c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
3592f3b585ceSsamf 		goto out;
35937c478bd9Sstevel@tonic-gate 	}
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&args->object, &cs->fh);
35967c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
35977c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
3598f3b585ceSsamf 
3599f3b585ceSsamf out:
3600f3b585ceSsamf 	DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs,
3601f3b585ceSsamf 	    PUTFH4res *, resp);
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate /* ARGSUSED */
36057c478bd9Sstevel@tonic-gate static void
36067c478bd9Sstevel@tonic-gate rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
36077c478bd9Sstevel@tonic-gate     struct compound_state *cs)
36087c478bd9Sstevel@tonic-gate {
36097c478bd9Sstevel@tonic-gate 	PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
36107c478bd9Sstevel@tonic-gate 	int error;
36117c478bd9Sstevel@tonic-gate 	fid_t fid;
36127c478bd9Sstevel@tonic-gate 	struct exportinfo *exi, *sav_exi;
36137c478bd9Sstevel@tonic-gate 
3614f3b585ceSsamf 	DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs);
3615f3b585ceSsamf 
36167c478bd9Sstevel@tonic-gate 	if (cs->vp) {
36177c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
36187c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	if (cs->cr)
36227c478bd9Sstevel@tonic-gate 		crfree(cs->cr);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	cs->cr = crdup(cs->basecr);
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 	/*
36277c478bd9Sstevel@tonic-gate 	 * Using rootdir, the system root vnode,
36287c478bd9Sstevel@tonic-gate 	 * get its fid.
36297c478bd9Sstevel@tonic-gate 	 */
36307c478bd9Sstevel@tonic-gate 	bzero(&fid, sizeof (fid));
36317c478bd9Sstevel@tonic-gate 	fid.fid_len = MAXFIDSZ;
36327c478bd9Sstevel@tonic-gate 	error = vop_fid_pseudo(rootdir, &fid);
36337c478bd9Sstevel@tonic-gate 	if (error != 0) {
36347c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
3635f3b585ceSsamf 		goto out;
36367c478bd9Sstevel@tonic-gate 	}
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	/*
36397c478bd9Sstevel@tonic-gate 	 * Then use the root fsid & fid it to find out if it's exported
36407c478bd9Sstevel@tonic-gate 	 *
36417c478bd9Sstevel@tonic-gate 	 * If the server root isn't exported directly, then
36427c478bd9Sstevel@tonic-gate 	 * it should at least be a pseudo export based on
36437c478bd9Sstevel@tonic-gate 	 * one or more exports further down in the server's
36447c478bd9Sstevel@tonic-gate 	 * file tree.
36457c478bd9Sstevel@tonic-gate 	 */
3646fbd2e8e1SArne Jansen 	exi = checkexport(&rootdir->v_vfsp->vfs_fsid, &fid, NULL);
36477c478bd9Sstevel@tonic-gate 	if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
36487c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
36497c478bd9Sstevel@tonic-gate 		    (CE_WARN, "rfs4_op_putrootfh: export check failure"));
36507c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3651aafcd32bSMarcel Telka 		if (exi != NULL)
3652fbd2e8e1SArne Jansen 			exi_rele(exi);
3653f3b585ceSsamf 		goto out;
36547c478bd9Sstevel@tonic-gate 	}
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	/*
36577c478bd9Sstevel@tonic-gate 	 * Now make a filehandle based on the root
36587c478bd9Sstevel@tonic-gate 	 * export and root vnode.
36597c478bd9Sstevel@tonic-gate 	 */
36607c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, rootdir, exi);
36617c478bd9Sstevel@tonic-gate 	if (error != 0) {
36627c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
3663fbd2e8e1SArne Jansen 		exi_rele(exi);
3664f3b585ceSsamf 		goto out;
36657c478bd9Sstevel@tonic-gate 	}
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate 	sav_exi = cs->exi;
36687c478bd9Sstevel@tonic-gate 	cs->exi = exi;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 	VN_HOLD(rootdir);
36717c478bd9Sstevel@tonic-gate 	cs->vp = rootdir;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
36747c478bd9Sstevel@tonic-gate 		VN_RELE(rootdir);
36757c478bd9Sstevel@tonic-gate 		cs->vp = NULL;
3676fbd2e8e1SArne Jansen 		exi_rele(exi);
36777c478bd9Sstevel@tonic-gate 		cs->exi = sav_exi;
3678f3b585ceSsamf 		goto out;
36797c478bd9Sstevel@tonic-gate 	}
3680aafcd32bSMarcel Telka 	if (sav_exi != NULL)
3681fbd2e8e1SArne Jansen 		exi_rele(sav_exi);
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
36847c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
3685f3b585ceSsamf out:
3686f3b585ceSsamf 	DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs,
3687f3b585ceSsamf 	    PUTROOTFH4res *, resp);
36887c478bd9Sstevel@tonic-gate }
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate /*
36917c478bd9Sstevel@tonic-gate  * set_rdattr_params sets up the variables used to manage what information
36927c478bd9Sstevel@tonic-gate  * to get for each directory entry.
36937c478bd9Sstevel@tonic-gate  */
36947c478bd9Sstevel@tonic-gate static nfsstat4
36957c478bd9Sstevel@tonic-gate set_rdattr_params(struct nfs4_svgetit_arg *sargp,
36967c478bd9Sstevel@tonic-gate     bitmap4 attrs, bool_t *need_to_lookup)
36977c478bd9Sstevel@tonic-gate {
36987c478bd9Sstevel@tonic-gate 	uint_t	va_mask;
36997c478bd9Sstevel@tonic-gate 	nfsstat4 status;
37007c478bd9Sstevel@tonic-gate 	bitmap4 objbits;
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	status = bitmap4_to_attrmask(attrs, sargp);
37037c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
37047c478bd9Sstevel@tonic-gate 		/*
37057c478bd9Sstevel@tonic-gate 		 * could not even figure attr mask
37067c478bd9Sstevel@tonic-gate 		 */
37077c478bd9Sstevel@tonic-gate 		return (status);
37087c478bd9Sstevel@tonic-gate 	}
37097c478bd9Sstevel@tonic-gate 	va_mask = sargp->vap->va_mask;
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	/*
37127c478bd9Sstevel@tonic-gate 	 * dirent's d_ino is always correct value for mounted_on_fileid.
37137c478bd9Sstevel@tonic-gate 	 * mntdfid_set is set once here, but mounted_on_fileid is
37147c478bd9Sstevel@tonic-gate 	 * set in main dirent processing loop for each dirent.
37157c478bd9Sstevel@tonic-gate 	 * The mntdfid_set is a simple optimization that lets the
37167c478bd9Sstevel@tonic-gate 	 * server attr code avoid work when caller is readdir.
37177c478bd9Sstevel@tonic-gate 	 */
37187c478bd9Sstevel@tonic-gate 	sargp->mntdfid_set = TRUE;
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate 	/*
37217c478bd9Sstevel@tonic-gate 	 * Lookup entry only if client asked for any of the following:
37227c478bd9Sstevel@tonic-gate 	 * a) vattr attrs
37237c478bd9Sstevel@tonic-gate 	 * b) vfs attrs
37247c478bd9Sstevel@tonic-gate 	 * c) attrs w/per-object scope requested (change, filehandle, etc)
37257c478bd9Sstevel@tonic-gate 	 *    other than mounted_on_fileid (which we can take from dirent)
37267c478bd9Sstevel@tonic-gate 	 */
37277c478bd9Sstevel@tonic-gate 	objbits = attrs ? attrs & NFS4_VP_ATTR_MASK : 0;
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 	if (va_mask || sargp->sbp || (objbits & ~FATTR4_MOUNTED_ON_FILEID_MASK))
37307c478bd9Sstevel@tonic-gate 		*need_to_lookup = TRUE;
37317c478bd9Sstevel@tonic-gate 	else
37327c478bd9Sstevel@tonic-gate 		*need_to_lookup = FALSE;
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	if (sargp->sbp == NULL)
37357c478bd9Sstevel@tonic-gate 		return (NFS4_OK);
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate 	/*
37387c478bd9Sstevel@tonic-gate 	 * If filesystem attrs are requested, get them now from the
37397c478bd9Sstevel@tonic-gate 	 * directory vp, as most entries will have same filesystem. The only
37407c478bd9Sstevel@tonic-gate 	 * exception are mounted over entries but we handle
37417c478bd9Sstevel@tonic-gate 	 * those as we go (XXX mounted over detection not yet implemented).
37427c478bd9Sstevel@tonic-gate 	 */
37437c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = 0;	/* to avoid VOP_GETATTR */
37447c478bd9Sstevel@tonic-gate 	status = bitmap4_get_sysattrs(sargp);
37457c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = va_mask;
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 	if ((status != NFS4_OK) && sargp->rdattr_error_req) {
37487c478bd9Sstevel@tonic-gate 		/*
37497c478bd9Sstevel@tonic-gate 		 * Failed to get filesystem attributes.
37507c478bd9Sstevel@tonic-gate 		 * Return a rdattr_error for each entry, but don't fail.
37517c478bd9Sstevel@tonic-gate 		 * However, don't get any obj-dependent attrs.
37527c478bd9Sstevel@tonic-gate 		 */
37537c478bd9Sstevel@tonic-gate 		sargp->rdattr_error = status;	/* for rdattr_error */
37547c478bd9Sstevel@tonic-gate 		*need_to_lookup = FALSE;
37557c478bd9Sstevel@tonic-gate 		/*
37567c478bd9Sstevel@tonic-gate 		 * At least get fileid for regular readdir output
37577c478bd9Sstevel@tonic-gate 		 */
37587c478bd9Sstevel@tonic-gate 		sargp->vap->va_mask &= AT_NODEID;
37597c478bd9Sstevel@tonic-gate 		status = NFS4_OK;
37607c478bd9Sstevel@tonic-gate 	}
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 	return (status);
37637c478bd9Sstevel@tonic-gate }
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate /*
37667c478bd9Sstevel@tonic-gate  * readlink: args: CURRENT_FH.
37677c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return linktext.
37687c478bd9Sstevel@tonic-gate  */
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate /* ARGSUSED */
37717c478bd9Sstevel@tonic-gate static void
37727c478bd9Sstevel@tonic-gate rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
37737c478bd9Sstevel@tonic-gate     struct compound_state *cs)
37747c478bd9Sstevel@tonic-gate {
37757c478bd9Sstevel@tonic-gate 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
37767c478bd9Sstevel@tonic-gate 	int error;
37777c478bd9Sstevel@tonic-gate 	vnode_t *vp;
37787c478bd9Sstevel@tonic-gate 	struct iovec iov;
37797c478bd9Sstevel@tonic-gate 	struct vattr va;
37807c478bd9Sstevel@tonic-gate 	struct uio uio;
37817c478bd9Sstevel@tonic-gate 	char *data;
3782b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
3783b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
37842f172c55SRobert Thurlow 	int is_referral;
37857c478bd9Sstevel@tonic-gate 
3786f3b585ceSsamf 	DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs);
3787f3b585ceSsamf 
37887c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: directory */
37897c478bd9Sstevel@tonic-gate 	vp = cs->vp;
37907c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
37917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3792f3b585ceSsamf 		goto out;
37937c478bd9Sstevel@tonic-gate 	}
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
37967c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3797f3b585ceSsamf 		goto out;
37987c478bd9Sstevel@tonic-gate 	}
37997c478bd9Sstevel@tonic-gate 
38002f172c55SRobert Thurlow 	/* Is it a referral? */
38012f172c55SRobert Thurlow 	if (vn_is_nfs_reparse(vp, cs->cr) && client_is_downrev(req)) {
38022f172c55SRobert Thurlow 
38032f172c55SRobert Thurlow 		is_referral = 1;
38042f172c55SRobert Thurlow 
38052f172c55SRobert Thurlow 	} else {
38062f172c55SRobert Thurlow 
38072f172c55SRobert Thurlow 		is_referral = 0;
38082f172c55SRobert Thurlow 
38097c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
38107c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
3811f3b585ceSsamf 			goto out;
38127c478bd9Sstevel@tonic-gate 		}
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 		if (vp->v_type != VLNK) {
38157c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_INVAL;
3816f3b585ceSsamf 			goto out;
38177c478bd9Sstevel@tonic-gate 		}
38187c478bd9Sstevel@tonic-gate 
38192f172c55SRobert Thurlow 	}
38202f172c55SRobert Thurlow 
38217c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE;
3822da6c28aaSamw 	error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
38237c478bd9Sstevel@tonic-gate 	if (error) {
38247c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
3825f3b585ceSsamf 		goto out;
38267c478bd9Sstevel@tonic-gate 	}
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, va.va_mode)) {
38297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3830f3b585ceSsamf 		goto out;
38317c478bd9Sstevel@tonic-gate 	}
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
38347c478bd9Sstevel@tonic-gate 
38352f172c55SRobert Thurlow 	if (is_referral) {
38362f172c55SRobert Thurlow 		char *s;
38372f172c55SRobert Thurlow 		size_t strsz;
38382f172c55SRobert Thurlow 
38392f172c55SRobert Thurlow 		/* Get an artificial symlink based on a referral */
38402f172c55SRobert Thurlow 		s = build_symlink(vp, cs->cr, &strsz);
38412f172c55SRobert Thurlow 		global_svstat_ptr[4][NFS_REFERLINKS].value.ui64++;
38422f172c55SRobert Thurlow 		DTRACE_PROBE2(nfs4serv__func__referral__reflink,
38432f172c55SRobert Thurlow 		    vnode_t *, vp, char *, s);
38442f172c55SRobert Thurlow 		if (s == NULL)
38452f172c55SRobert Thurlow 			error = EINVAL;
38462f172c55SRobert Thurlow 		else {
38472f172c55SRobert Thurlow 			error = 0;
38482f172c55SRobert Thurlow 			(void) strlcpy(data, s, MAXPATHLEN + 1);
38492f172c55SRobert Thurlow 			kmem_free(s, strsz);
38502f172c55SRobert Thurlow 		}
38512f172c55SRobert Thurlow 
38522f172c55SRobert Thurlow 	} else {
38532f172c55SRobert Thurlow 
38547c478bd9Sstevel@tonic-gate 		iov.iov_base = data;
38557c478bd9Sstevel@tonic-gate 		iov.iov_len = MAXPATHLEN;
38567c478bd9Sstevel@tonic-gate 		uio.uio_iov = &iov;
38577c478bd9Sstevel@tonic-gate 		uio.uio_iovcnt = 1;
38587c478bd9Sstevel@tonic-gate 		uio.uio_segflg = UIO_SYSSPACE;
38597c478bd9Sstevel@tonic-gate 		uio.uio_extflg = UIO_COPY_CACHED;
38607c478bd9Sstevel@tonic-gate 		uio.uio_loffset = 0;
38617c478bd9Sstevel@tonic-gate 		uio.uio_resid = MAXPATHLEN;
38627c478bd9Sstevel@tonic-gate 
3863da6c28aaSamw 		error = VOP_READLINK(vp, &uio, cs->cr, NULL);
38647c478bd9Sstevel@tonic-gate 
38652f172c55SRobert Thurlow 		if (!error)
38662f172c55SRobert Thurlow 			*(data + MAXPATHLEN - uio.uio_resid) = '\0';
38672f172c55SRobert Thurlow 	}
38682f172c55SRobert Thurlow 
38697c478bd9Sstevel@tonic-gate 	if (error) {
38707c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
38717c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
3872f3b585ceSsamf 		goto out;
38737c478bd9Sstevel@tonic-gate 	}
38747c478bd9Sstevel@tonic-gate 
3875b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
3876b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, data, NFSCMD_CONV_OUTBOUND,
3877b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
3878b89a8333Snatalie li - Sun Microsystems - Irvine United States 
3879b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
3880b89a8333Snatalie li - Sun Microsystems - Irvine United States 		/*
3881b89a8333Snatalie li - Sun Microsystems - Irvine United States 		 * Even though the conversion failed, we return
3882b89a8333Snatalie li - Sun Microsystems - Irvine United States 		 * something. We just don't translate it.
3883b89a8333Snatalie li - Sun Microsystems - Irvine United States 		 */
3884b89a8333Snatalie li - Sun Microsystems - Irvine United States 		name = data;
3885b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
3886b89a8333Snatalie li - Sun Microsystems - Irvine United States 
38877c478bd9Sstevel@tonic-gate 	/*
38887c478bd9Sstevel@tonic-gate 	 * treat link name as data
38897c478bd9Sstevel@tonic-gate 	 */
3890bbe876c0SMarcel Telka 	(void) str_to_utf8(name, (utf8string *)&resp->link);
38917c478bd9Sstevel@tonic-gate 
3892b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name != data)
3893b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
38947c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
38957c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
3896f3b585ceSsamf 
3897f3b585ceSsamf out:
3898f3b585ceSsamf 	DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs,
3899f3b585ceSsamf 	    READLINK4res *, resp);
39007c478bd9Sstevel@tonic-gate }
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate static void
39037c478bd9Sstevel@tonic-gate rfs4_op_readlink_free(nfs_resop4 *resop)
39047c478bd9Sstevel@tonic-gate {
39057c478bd9Sstevel@tonic-gate 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3906bbe876c0SMarcel Telka 	utf8string *symlink = (utf8string *)&resp->link;
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 	if (symlink->utf8string_val) {
39097c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(*symlink)
39107c478bd9Sstevel@tonic-gate 	}
39117c478bd9Sstevel@tonic-gate }
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate /*
39147c478bd9Sstevel@tonic-gate  * release_lockowner:
39157c478bd9Sstevel@tonic-gate  *	Release any state associated with the supplied
39167c478bd9Sstevel@tonic-gate  *	lockowner. Note if any lo_state is holding locks we will not
39177c478bd9Sstevel@tonic-gate  *	rele that lo_state and thus the lockowner will not be destroyed.
39187c478bd9Sstevel@tonic-gate  *	A client using lock after the lock owner stateid has been released
39197c478bd9Sstevel@tonic-gate  *	will suffer the consequence of NFS4ERR_BAD_STATEID and would have
39207c478bd9Sstevel@tonic-gate  *	to reissue the lock with new_lock_owner set to TRUE.
39217c478bd9Sstevel@tonic-gate  *	args: lock_owner
39227c478bd9Sstevel@tonic-gate  *	res:  status
39237c478bd9Sstevel@tonic-gate  */
39247c478bd9Sstevel@tonic-gate /* ARGSUSED */
39257c478bd9Sstevel@tonic-gate static void
39267c478bd9Sstevel@tonic-gate rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
39277c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
39287c478bd9Sstevel@tonic-gate {
39297c478bd9Sstevel@tonic-gate 	RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
39307c478bd9Sstevel@tonic-gate 	RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
39317c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
3932d216dff5SRobert Mastors 	rfs4_openowner_t *oo;
39337c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
39347c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
39357c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
39367c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
39377c478bd9Sstevel@tonic-gate 	locklist_t *llist;
39387c478bd9Sstevel@tonic-gate 	sysid_t sysid;
39397c478bd9Sstevel@tonic-gate 
3940f3b585ceSsamf 	DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *,
3941f3b585ceSsamf 	    cs, RELEASE_LOCKOWNER4args *, ap);
3942f3b585ceSsamf 
39437c478bd9Sstevel@tonic-gate 	/* Make sure there is a clientid around for this request */
39447c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
39477c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
39487c478bd9Sstevel@tonic-gate 		    rfs4_check_clientid(&ap->lock_owner.clientid, 0);
3949f3b585ceSsamf 		goto out;
39507c478bd9Sstevel@tonic-gate 	}
39517c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner(&ap->lock_owner, &create);
39547c478bd9Sstevel@tonic-gate 	if (lo == NULL) {
39557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
3956f3b585ceSsamf 		goto out;
39577c478bd9Sstevel@tonic-gate 	}
3958d216dff5SRobert Mastors 	ASSERT(lo->rl_client != NULL);
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 	/*
39617c478bd9Sstevel@tonic-gate 	 * Check for EXPIRED client. If so will reap state with in a lease
39627c478bd9Sstevel@tonic-gate 	 * period or on next set_clientid_confirm step
39637c478bd9Sstevel@tonic-gate 	 */
3964d216dff5SRobert Mastors 	if (rfs4_lease_expired(lo->rl_client)) {
39657c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
39667c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
3967f3b585ceSsamf 		goto out;
39687c478bd9Sstevel@tonic-gate 	}
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	/*
39717c478bd9Sstevel@tonic-gate 	 * If no sysid has been assigned, then no locks exist; just return.
39727c478bd9Sstevel@tonic-gate 	 */
3973d216dff5SRobert Mastors 	rfs4_dbe_lock(lo->rl_client->rc_dbe);
3974d216dff5SRobert Mastors 	if (lo->rl_client->rc_sysidt == LM_NOSYSID) {
39757c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
3976d216dff5SRobert Mastors 		rfs4_dbe_unlock(lo->rl_client->rc_dbe);
3977f3b585ceSsamf 		goto out;
39787c478bd9Sstevel@tonic-gate 	}
39797c478bd9Sstevel@tonic-gate 
3980d216dff5SRobert Mastors 	sysid = lo->rl_client->rc_sysidt;
3981d216dff5SRobert Mastors 	rfs4_dbe_unlock(lo->rl_client->rc_dbe);
39827c478bd9Sstevel@tonic-gate 
39837c478bd9Sstevel@tonic-gate 	/*
39847c478bd9Sstevel@tonic-gate 	 * Mark the lockowner invalid.
39857c478bd9Sstevel@tonic-gate 	 */
3986d216dff5SRobert Mastors 	rfs4_dbe_hide(lo->rl_dbe);
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	/*
39897c478bd9Sstevel@tonic-gate 	 * sysid-pid pair should now not be used since the lockowner is
39907c478bd9Sstevel@tonic-gate 	 * invalid. If the client were to instantiate the lockowner again
39917c478bd9Sstevel@tonic-gate 	 * it would be assigned a new pid. Thus we can get the list of
39927c478bd9Sstevel@tonic-gate 	 * current locks.
39937c478bd9Sstevel@tonic-gate 	 */
39947c478bd9Sstevel@tonic-gate 
3995d216dff5SRobert Mastors 	llist = flk_get_active_locks(sysid, lo->rl_pid);
39967c478bd9Sstevel@tonic-gate 	/* If we are still holding locks fail */
39977c478bd9Sstevel@tonic-gate 	if (llist != NULL) {
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 		flk_free_locklist(llist);
40027c478bd9Sstevel@tonic-gate 		/*
40037c478bd9Sstevel@tonic-gate 		 * We need to unhide the lockowner so the client can
40047c478bd9Sstevel@tonic-gate 		 * try it again. The bad thing here is if the client
40057c478bd9Sstevel@tonic-gate 		 * has a logic error that took it here in the first place
40067c478bd9Sstevel@tonic-gate 		 * he probably has lost accounting of the locks that it
40077c478bd9Sstevel@tonic-gate 		 * is holding. So we may have dangling state until the
40087c478bd9Sstevel@tonic-gate 		 * open owner state is reaped via close. One scenario
40097c478bd9Sstevel@tonic-gate 		 * that could possibly occur is that the client has
40107c478bd9Sstevel@tonic-gate 		 * sent the unlock request(s) in separate threads
40117c478bd9Sstevel@tonic-gate 		 * and has not waited for the replies before sending the
40127c478bd9Sstevel@tonic-gate 		 * RELEASE_LOCKOWNER request. Presumably, it would expect
40137c478bd9Sstevel@tonic-gate 		 * and deal appropriately with NFS4ERR_LOCKS_HELD, by
40147c478bd9Sstevel@tonic-gate 		 * reissuing the request.
40157c478bd9Sstevel@tonic-gate 		 */
4016d216dff5SRobert Mastors 		rfs4_dbe_unhide(lo->rl_dbe);
40177c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
4018f3b585ceSsamf 		goto out;
40197c478bd9Sstevel@tonic-gate 	}
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 	/*
40227c478bd9Sstevel@tonic-gate 	 * For the corresponding client we need to check each open
40237c478bd9Sstevel@tonic-gate 	 * owner for any opens that have lockowner state associated
40247c478bd9Sstevel@tonic-gate 	 * with this lockowner.
40257c478bd9Sstevel@tonic-gate 	 */
40267c478bd9Sstevel@tonic-gate 
4027d216dff5SRobert Mastors 	rfs4_dbe_lock(lo->rl_client->rc_dbe);
4028d216dff5SRobert Mastors 	for (oo = list_head(&lo->rl_client->rc_openownerlist); oo != NULL;
4029d216dff5SRobert Mastors 	    oo = list_next(&lo->rl_client->rc_openownerlist, oo)) {
40307c478bd9Sstevel@tonic-gate 
4031d216dff5SRobert Mastors 		rfs4_dbe_lock(oo->ro_dbe);
4032d216dff5SRobert Mastors 		for (sp = list_head(&oo->ro_statelist); sp != NULL;
4033d216dff5SRobert Mastors 		    sp = list_next(&oo->ro_statelist, sp)) {
40347c478bd9Sstevel@tonic-gate 
4035d216dff5SRobert Mastors 			rfs4_dbe_lock(sp->rs_dbe);
4036d216dff5SRobert Mastors 			for (lsp = list_head(&sp->rs_lostatelist);
4037d216dff5SRobert Mastors 			    lsp != NULL;
4038d216dff5SRobert Mastors 			    lsp = list_next(&sp->rs_lostatelist, lsp)) {
4039d216dff5SRobert Mastors 				if (lsp->rls_locker == lo) {
4040d216dff5SRobert Mastors 					rfs4_dbe_lock(lsp->rls_dbe);
4041d216dff5SRobert Mastors 					rfs4_dbe_invalidate(lsp->rls_dbe);
4042d216dff5SRobert Mastors 					rfs4_dbe_unlock(lsp->rls_dbe);
40437c478bd9Sstevel@tonic-gate 				}
40447c478bd9Sstevel@tonic-gate 			}
4045d216dff5SRobert Mastors 			rfs4_dbe_unlock(sp->rs_dbe);
40467c478bd9Sstevel@tonic-gate 		}
4047d216dff5SRobert Mastors 		rfs4_dbe_unlock(oo->ro_dbe);
40487c478bd9Sstevel@tonic-gate 	}
4049d216dff5SRobert Mastors 	rfs4_dbe_unlock(lo->rl_client->rc_dbe);
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 	rfs4_lockowner_rele(lo);
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
4054f3b585ceSsamf 
4055f3b585ceSsamf out:
4056f3b585ceSsamf 	DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *,
4057f3b585ceSsamf 	    cs, RELEASE_LOCKOWNER4res *, resp);
40587c478bd9Sstevel@tonic-gate }
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate /*
40617c478bd9Sstevel@tonic-gate  * short utility function to lookup a file and recall the delegation
40627c478bd9Sstevel@tonic-gate  */
40637c478bd9Sstevel@tonic-gate static rfs4_file_t *
40647c478bd9Sstevel@tonic-gate rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
40657c478bd9Sstevel@tonic-gate     int *lkup_error, cred_t *cr)
40667c478bd9Sstevel@tonic-gate {
40677c478bd9Sstevel@tonic-gate 	vnode_t *vp;
40687c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp = NULL;
40697c478bd9Sstevel@tonic-gate 	bool_t fcreate = FALSE;
40707c478bd9Sstevel@tonic-gate 	int error;
40717c478bd9Sstevel@tonic-gate 
40727c478bd9Sstevel@tonic-gate 	if (vpp)
40737c478bd9Sstevel@tonic-gate 		*vpp = NULL;
40747c478bd9Sstevel@tonic-gate 
4075da6c28aaSamw 	if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL,
4076da6c28aaSamw 	    NULL)) == 0) {
40777c478bd9Sstevel@tonic-gate 		if (vp->v_type == VREG)
40787c478bd9Sstevel@tonic-gate 			fp = rfs4_findfile(vp, NULL, &fcreate);
40797c478bd9Sstevel@tonic-gate 		if (vpp)
40807c478bd9Sstevel@tonic-gate 			*vpp = vp;
40817c478bd9Sstevel@tonic-gate 		else
40827c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
40837c478bd9Sstevel@tonic-gate 	}
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	if (lkup_error)
40867c478bd9Sstevel@tonic-gate 		*lkup_error = error;
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	return (fp);
40897c478bd9Sstevel@tonic-gate }
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate /*
40927c478bd9Sstevel@tonic-gate  * remove: args: CURRENT_FH: directory; name.
40937c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return change_info
40947c478bd9Sstevel@tonic-gate  *		for directory.
40957c478bd9Sstevel@tonic-gate  */
40967c478bd9Sstevel@tonic-gate /* ARGSUSED */
40977c478bd9Sstevel@tonic-gate static void
40987c478bd9Sstevel@tonic-gate rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
40997c478bd9Sstevel@tonic-gate     struct compound_state *cs)
41007c478bd9Sstevel@tonic-gate {
41017c478bd9Sstevel@tonic-gate 	REMOVE4args *args = &argop->nfs_argop4_u.opremove;
41027c478bd9Sstevel@tonic-gate 	REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
41037c478bd9Sstevel@tonic-gate 	int error;
41047c478bd9Sstevel@tonic-gate 	vnode_t *dvp, *vp;
41057c478bd9Sstevel@tonic-gate 	struct vattr bdva, idva, adva;
41067c478bd9Sstevel@tonic-gate 	char *nm;
41077c478bd9Sstevel@tonic-gate 	uint_t len;
41087c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
41097c478bd9Sstevel@tonic-gate 	int in_crit = 0;
411045916cd2Sjpk 	bslabel_t *clabel;
4111b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
4112b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
411315721462SDaniil Lunev 	nfsstat4 status;
41147c478bd9Sstevel@tonic-gate 
4115f3b585ceSsamf 	DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs,
4116f3b585ceSsamf 	    REMOVE4args *, args);
4117f3b585ceSsamf 
41187c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: directory */
41197c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
41207c478bd9Sstevel@tonic-gate 	if (dvp == NULL) {
41217c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4122f3b585ceSsamf 		goto out;
41237c478bd9Sstevel@tonic-gate 	}
41247c478bd9Sstevel@tonic-gate 
41257c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
41267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4127f3b585ceSsamf 		goto out;
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	/*
41317c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
41327c478bd9Sstevel@tonic-gate 	 * Do not allow to remove anything in this directory.
41337c478bd9Sstevel@tonic-gate 	 */
41347c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(dvp)) {
41357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4136f3b585ceSsamf 		goto out;
41377c478bd9Sstevel@tonic-gate 	}
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
41407c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4141f3b585ceSsamf 		goto out;
41427c478bd9Sstevel@tonic-gate 	}
41437c478bd9Sstevel@tonic-gate 
414415721462SDaniil Lunev 	status = utf8_dir_verify(&args->target);
414515721462SDaniil Lunev 	if (status != NFS4_OK) {
414615721462SDaniil Lunev 		*cs->statusp = resp->status = status;
4147f3b585ceSsamf 		goto out;
41487c478bd9Sstevel@tonic-gate 	}
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	/*
41517c478bd9Sstevel@tonic-gate 	 * Lookup the file so that we can check if it's a directory
41527c478bd9Sstevel@tonic-gate 	 */
41537c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(&args->target, &len, NULL);
41547c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
41557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4156f3b585ceSsamf 		goto out;
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
41607c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
41617c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
4162f3b585ceSsamf 		goto out;
41637c478bd9Sstevel@tonic-gate 	}
41647c478bd9Sstevel@tonic-gate 
41655cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
41667c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
41677c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
4168f3b585ceSsamf 		goto out;
41697c478bd9Sstevel@tonic-gate 	}
41707c478bd9Sstevel@tonic-gate 
4171b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4172b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
4173b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
4174b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4175b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
4176b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4177b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, len);
4178b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
4179b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
4180b89a8333Snatalie li - Sun Microsystems - Irvine United States 
41817c478bd9Sstevel@tonic-gate 	/*
41827c478bd9Sstevel@tonic-gate 	 * Lookup the file to determine type and while we are see if
41837c478bd9Sstevel@tonic-gate 	 * there is a file struct around and check for delegation.
41847c478bd9Sstevel@tonic-gate 	 * We don't need to acquire va_seq before this lookup, if
41857c478bd9Sstevel@tonic-gate 	 * it causes an update, cinfo.before will not match, which will
41867c478bd9Sstevel@tonic-gate 	 * trigger a cache flush even if atomic is TRUE.
41877c478bd9Sstevel@tonic-gate 	 */
4188b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (fp = rfs4_lookup_and_findfile(dvp, name, &vp, &error, cs->cr)) {
41897c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
41907c478bd9Sstevel@tonic-gate 		    NULL)) {
41917c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
41927c478bd9Sstevel@tonic-gate 			rfs4_file_rele(fp);
41937c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4194b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (nm != name)
4195b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
41967c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
4197f3b585ceSsamf 			goto out;
41987c478bd9Sstevel@tonic-gate 		}
41997c478bd9Sstevel@tonic-gate 	}
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	/* Didn't find anything to remove */
42027c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
42037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = error;
4204b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (nm != name)
4205b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(name, MAXPATHLEN + 1);
42067c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
4207f3b585ceSsamf 		goto out;
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
42117c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
42127c478bd9Sstevel@tonic-gate 		in_crit = 1;
4213da6c28aaSamw 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
42147c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4215b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (nm != name)
4216b89a8333Snatalie li - Sun Microsystems - Irvine United States 				kmem_free(name, MAXPATHLEN + 1);
42177c478bd9Sstevel@tonic-gate 			kmem_free(nm, len);
42187c478bd9Sstevel@tonic-gate 			nbl_end_crit(vp);
42197c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
42207c478bd9Sstevel@tonic-gate 			if (fp) {
42217c478bd9Sstevel@tonic-gate 				rfs4_clear_dont_grant(fp);
42227c478bd9Sstevel@tonic-gate 				rfs4_file_rele(fp);
42237c478bd9Sstevel@tonic-gate 			}
4224f3b585ceSsamf 			goto out;
42257c478bd9Sstevel@tonic-gate 		}
42267c478bd9Sstevel@tonic-gate 	}
42277c478bd9Sstevel@tonic-gate 
422845916cd2Sjpk 	/* check label before allowing removal */
422945916cd2Sjpk 	if (is_system_labeled()) {
423045916cd2Sjpk 		ASSERT(req->rq_label != NULL);
423145916cd2Sjpk 		clabel = req->rq_label;
423245916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
423345916cd2Sjpk 		    "got client label from request(1)",
423445916cd2Sjpk 		    struct svc_req *, req);
423545916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4236bd6f1640SJarrett Lu 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
4237bd6f1640SJarrett Lu 			    cs->exi)) {
423845916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4239b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (name != nm)
4240b89a8333Snatalie li - Sun Microsystems - Irvine United States 					kmem_free(name, MAXPATHLEN + 1);
424145916cd2Sjpk 				kmem_free(nm, len);
424245916cd2Sjpk 				if (in_crit)
424345916cd2Sjpk 					nbl_end_crit(vp);
424445916cd2Sjpk 				VN_RELE(vp);
424545916cd2Sjpk 				if (fp) {
424645916cd2Sjpk 					rfs4_clear_dont_grant(fp);
424745916cd2Sjpk 					rfs4_file_rele(fp);
424845916cd2Sjpk 				}
4249f3b585ceSsamf 				goto out;
425045916cd2Sjpk 			}
425145916cd2Sjpk 		}
425245916cd2Sjpk 	}
425345916cd2Sjpk 
42547c478bd9Sstevel@tonic-gate 	/* Get dir "before" change value */
42557c478bd9Sstevel@tonic-gate 	bdva.va_mask = AT_CTIME|AT_SEQ;
4256da6c28aaSamw 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
42577c478bd9Sstevel@tonic-gate 	if (error) {
42587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
4259b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (nm != name)
4260b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(name, MAXPATHLEN + 1);
42617c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
4262a08f57bcSJames Wahlig 		if (in_crit)
4263a08f57bcSJames Wahlig 			nbl_end_crit(vp);
4264a08f57bcSJames Wahlig 		VN_RELE(vp);
4265a08f57bcSJames Wahlig 		if (fp) {
4266a08f57bcSJames Wahlig 			rfs4_clear_dont_grant(fp);
4267a08f57bcSJames Wahlig 			rfs4_file_rele(fp);
4268a08f57bcSJames Wahlig 		}
4269f3b585ceSsamf 		goto out;
42707c478bd9Sstevel@tonic-gate 	}
42717c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	/* Actually do the REMOVE operation */
42747c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
42757c478bd9Sstevel@tonic-gate 		/*
42767c478bd9Sstevel@tonic-gate 		 * Can't remove a directory that has a mounted-on filesystem.
42777c478bd9Sstevel@tonic-gate 		 */
42787c478bd9Sstevel@tonic-gate 		if (vn_ismntpt(vp)) {
42797c478bd9Sstevel@tonic-gate 			error = EACCES;
42807c478bd9Sstevel@tonic-gate 		} else {
42817c478bd9Sstevel@tonic-gate 			/*
42827c478bd9Sstevel@tonic-gate 			 * System V defines rmdir to return EEXIST,
428359fb210bSJan Kryl 			 * not ENOTEMPTY, if the directory is not
42847c478bd9Sstevel@tonic-gate 			 * empty.  A System V NFS server needs to map
42857c478bd9Sstevel@tonic-gate 			 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
42867c478bd9Sstevel@tonic-gate 			 * transmit over the wire.
42877c478bd9Sstevel@tonic-gate 			 */
428859fb210bSJan Kryl 			if ((error = VOP_RMDIR(dvp, name, rootdir, cs->cr,
4289da6c28aaSamw 			    NULL, 0)) == EEXIST)
42907c478bd9Sstevel@tonic-gate 				error = ENOTEMPTY;
42917c478bd9Sstevel@tonic-gate 		}
4292193db0f2SSimon Klinkert 
4293193db0f2SSimon Klinkert 		if (in_crit)
4294193db0f2SSimon Klinkert 			nbl_end_crit(vp);
4295193db0f2SSimon Klinkert 		VN_RELE(vp);
42967c478bd9Sstevel@tonic-gate 	} else {
4297193db0f2SSimon Klinkert 		if (!in_crit)
4298193db0f2SSimon Klinkert 			VN_RELE(vp);
4299b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if ((error = VOP_REMOVE(dvp, name, cs->cr, NULL, 0)) == 0 &&
43007c478bd9Sstevel@tonic-gate 		    fp != NULL) {
43017c478bd9Sstevel@tonic-gate 			struct vattr va;
4302418d27f3Sshepler 			vnode_t *tvp;
43037c478bd9Sstevel@tonic-gate 
4304d216dff5SRobert Mastors 			rfs4_dbe_lock(fp->rf_dbe);
4305d216dff5SRobert Mastors 			tvp = fp->rf_vp;
4306418d27f3Sshepler 			if (tvp)
4307418d27f3Sshepler 				VN_HOLD(tvp);
4308d216dff5SRobert Mastors 			rfs4_dbe_unlock(fp->rf_dbe);
4309418d27f3Sshepler 
4310418d27f3Sshepler 			if (tvp) {
43117c478bd9Sstevel@tonic-gate 				/*
43127c478bd9Sstevel@tonic-gate 				 * This is va_seq safe because we are not
43137c478bd9Sstevel@tonic-gate 				 * manipulating dvp.
43147c478bd9Sstevel@tonic-gate 				 */
43157c478bd9Sstevel@tonic-gate 				va.va_mask = AT_NLINK;
4316da6c28aaSamw 				if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
43177c478bd9Sstevel@tonic-gate 				    va.va_nlink == 0) {
4318418d27f3Sshepler 					/* Remove state on file remove */
43197c478bd9Sstevel@tonic-gate 					if (in_crit) {
43207c478bd9Sstevel@tonic-gate 						nbl_end_crit(vp);
4321193db0f2SSimon Klinkert 						VN_RELE(vp);
43227c478bd9Sstevel@tonic-gate 						in_crit = 0;
43237c478bd9Sstevel@tonic-gate 					}
43247c478bd9Sstevel@tonic-gate 					rfs4_close_all_state(fp);
43257c478bd9Sstevel@tonic-gate 				}
4326418d27f3Sshepler 				VN_RELE(tvp);
4327418d27f3Sshepler 			}
43287c478bd9Sstevel@tonic-gate 		}
43297c478bd9Sstevel@tonic-gate 
4330193db0f2SSimon Klinkert 		if (in_crit) {
43317c478bd9Sstevel@tonic-gate 			nbl_end_crit(vp);
43327c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
4333193db0f2SSimon Klinkert 		}
4334193db0f2SSimon Klinkert 	}
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	if (fp) {
43377c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(fp);
43387c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
43397c478bd9Sstevel@tonic-gate 	}
4340b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nm != name)
4341b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
43427c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	if (error) {
43457c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
4346f3b585ceSsamf 		goto out;
43477c478bd9Sstevel@tonic-gate 	}
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	/*
43507c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
43517c478bd9Sstevel@tonic-gate 	 */
43527c478bd9Sstevel@tonic-gate 	idva.va_mask = AT_SEQ;
4353da6c28aaSamw 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
43547c478bd9Sstevel@tonic-gate 		idva.va_seq = 0;
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	/*
43577c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
43587c478bd9Sstevel@tonic-gate 	 */
4359da6c28aaSamw 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 	/*
43627c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
43637c478bd9Sstevel@tonic-gate 	 * before value.
43647c478bd9Sstevel@tonic-gate 	 */
43657c478bd9Sstevel@tonic-gate 	adva.va_mask = AT_CTIME|AT_SEQ;
4366da6c28aaSamw 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
43677c478bd9Sstevel@tonic-gate 		adva.va_ctime = bdva.va_ctime;
43687c478bd9Sstevel@tonic-gate 		adva.va_seq = 0;
43697c478bd9Sstevel@tonic-gate 	}
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate 	/*
43747c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
43757c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
43767c478bd9Sstevel@tonic-gate 	 * during the VOP_REMOVE/RMDIR and it didn't change during
43777c478bd9Sstevel@tonic-gate 	 * the VOP_FSYNC.
43787c478bd9Sstevel@tonic-gate 	 */
43797c478bd9Sstevel@tonic-gate 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
43801b300de9Sjwahlig 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
43817c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = TRUE;
43827c478bd9Sstevel@tonic-gate 	else
43837c478bd9Sstevel@tonic-gate 		resp->cinfo.atomic = FALSE;
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
4386f3b585ceSsamf 
4387f3b585ceSsamf out:
4388f3b585ceSsamf 	DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs,
4389f3b585ceSsamf 	    REMOVE4res *, resp);
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate /*
43937c478bd9Sstevel@tonic-gate  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
43947c478bd9Sstevel@tonic-gate  *		oldname and newname.
43957c478bd9Sstevel@tonic-gate  *	res: status. If success - CURRENT_FH unchanged, return change_info
43967c478bd9Sstevel@tonic-gate  *		for both from and target directories.
43977c478bd9Sstevel@tonic-gate  */
43987c478bd9Sstevel@tonic-gate /* ARGSUSED */
43997c478bd9Sstevel@tonic-gate static void
44007c478bd9Sstevel@tonic-gate rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
44017c478bd9Sstevel@tonic-gate     struct compound_state *cs)
44027c478bd9Sstevel@tonic-gate {
44037c478bd9Sstevel@tonic-gate 	RENAME4args *args = &argop->nfs_argop4_u.oprename;
44047c478bd9Sstevel@tonic-gate 	RENAME4res *resp = &resop->nfs_resop4_u.oprename;
44057c478bd9Sstevel@tonic-gate 	int error;
44067c478bd9Sstevel@tonic-gate 	vnode_t *odvp;
44077c478bd9Sstevel@tonic-gate 	vnode_t *ndvp;
44087c478bd9Sstevel@tonic-gate 	vnode_t *srcvp, *targvp;
44097c478bd9Sstevel@tonic-gate 	struct vattr obdva, oidva, oadva;
44107c478bd9Sstevel@tonic-gate 	struct vattr nbdva, nidva, nadva;
44117c478bd9Sstevel@tonic-gate 	char *onm, *nnm;
44127c478bd9Sstevel@tonic-gate 	uint_t olen, nlen;
44137c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp, *sfp;
44147c478bd9Sstevel@tonic-gate 	int in_crit_src, in_crit_targ;
44157c478bd9Sstevel@tonic-gate 	int fp_rele_grant_hold, sfp_rele_grant_hold;
441645916cd2Sjpk 	bslabel_t *clabel;
4417b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
4418b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *converted_onm = NULL;
4419b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *converted_nnm = NULL;
442015721462SDaniil Lunev 	nfsstat4 status;
44217c478bd9Sstevel@tonic-gate 
4422f3b585ceSsamf 	DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs,
4423f3b585ceSsamf 	    RENAME4args *, args);
4424f3b585ceSsamf 
44257c478bd9Sstevel@tonic-gate 	fp = sfp = NULL;
44267c478bd9Sstevel@tonic-gate 	srcvp = targvp = NULL;
44277c478bd9Sstevel@tonic-gate 	in_crit_src = in_crit_targ = 0;
44287c478bd9Sstevel@tonic-gate 	fp_rele_grant_hold = sfp_rele_grant_hold = 0;
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 	/* CURRENT_FH: target directory */
44317c478bd9Sstevel@tonic-gate 	ndvp = cs->vp;
44327c478bd9Sstevel@tonic-gate 	if (ndvp == NULL) {
44337c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4434f3b585ceSsamf 		goto out;
44357c478bd9Sstevel@tonic-gate 	}
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	/* SAVED_FH: from directory */
44387c478bd9Sstevel@tonic-gate 	odvp = cs->saved_vp;
44397c478bd9Sstevel@tonic-gate 	if (odvp == NULL) {
44407c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4441f3b585ceSsamf 		goto out;
44427c478bd9Sstevel@tonic-gate 	}
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
44457c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4446f3b585ceSsamf 		goto out;
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	/*
44507c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
44517c478bd9Sstevel@tonic-gate 	 * do not allow to rename objects in this directory.
44527c478bd9Sstevel@tonic-gate 	 */
44537c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(odvp)) {
44547c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4455f3b585ceSsamf 		goto out;
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	/*
44597c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
44607c478bd9Sstevel@tonic-gate 	 * do not allow to rename to this directory.
44617c478bd9Sstevel@tonic-gate 	 */
44627c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(ndvp)) {
44637c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4464f3b585ceSsamf 		goto out;
44657c478bd9Sstevel@tonic-gate 	}
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
44687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4469f3b585ceSsamf 		goto out;
44707c478bd9Sstevel@tonic-gate 	}
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate 	if (cs->saved_exi != cs->exi) {
44737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_XDEV;
4474f3b585ceSsamf 		goto out;
44757c478bd9Sstevel@tonic-gate 	}
44767c478bd9Sstevel@tonic-gate 
447715721462SDaniil Lunev 	status = utf8_dir_verify(&args->oldname);
447815721462SDaniil Lunev 	if (status != NFS4_OK) {
447915721462SDaniil Lunev 		*cs->statusp = resp->status = status;
4480f3b585ceSsamf 		goto out;
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 
448315721462SDaniil Lunev 	status = utf8_dir_verify(&args->newname);
448415721462SDaniil Lunev 	if (status != NFS4_OK) {
448515721462SDaniil Lunev 		*cs->statusp = resp->status = status;
4486f3b585ceSsamf 		goto out;
44877c478bd9Sstevel@tonic-gate 	}
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	onm = utf8_to_fn(&args->oldname, &olen, NULL);
44907c478bd9Sstevel@tonic-gate 	if (onm == NULL) {
44917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4492f3b585ceSsamf 		goto out;
44937c478bd9Sstevel@tonic-gate 	}
4494b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4495b89a8333Snatalie li - Sun Microsystems - Irvine United States 	nlen = MAXPATHLEN + 1;
4496b89a8333Snatalie li - Sun Microsystems - Irvine United States 	converted_onm = nfscmd_convname(ca, cs->exi, onm, NFSCMD_CONV_INBOUND,
4497b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    nlen);
44987c478bd9Sstevel@tonic-gate 
4499b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (converted_onm == NULL) {
45007c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
45017c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
4502f3b585ceSsamf 		goto out;
45037c478bd9Sstevel@tonic-gate 	}
45047c478bd9Sstevel@tonic-gate 
4505b89a8333Snatalie li - Sun Microsystems - Irvine United States 	nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4506b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nnm == NULL) {
4507b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4508b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (onm != converted_onm)
4509b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(converted_onm, MAXPATHLEN + 1);
4510b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(onm, olen);
4511b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
4512b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
4513b89a8333Snatalie li - Sun Microsystems - Irvine United States 	converted_nnm = nfscmd_convname(ca, cs->exi, nnm, NFSCMD_CONV_INBOUND,
4514b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
4515b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4516b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (converted_nnm == NULL) {
4517b89a8333Snatalie li - Sun Microsystems - Irvine United States 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4518b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nnm, nlen);
4519b89a8333Snatalie li - Sun Microsystems - Irvine United States 		nnm = NULL;
4520b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (onm != converted_onm)
4521b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(converted_onm, MAXPATHLEN + 1);
4522b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(onm, olen);
4523b89a8333Snatalie li - Sun Microsystems - Irvine United States 		goto out;
4524b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
4525b89a8333Snatalie li - Sun Microsystems - Irvine United States 
4526b89a8333Snatalie li - Sun Microsystems - Irvine United States 
45277c478bd9Sstevel@tonic-gate 	if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
45287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
45297c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
45307c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
4531f3b585ceSsamf 		goto out;
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 
45355cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
45367c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
4537b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (onm != converted_onm)
4538b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(converted_onm, MAXPATHLEN + 1);
45397c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
4540b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (nnm != converted_nnm)
4541b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(converted_nnm, MAXPATHLEN + 1);
45427c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
4543f3b585ceSsamf 		goto out;
45447c478bd9Sstevel@tonic-gate 	}
45457c478bd9Sstevel@tonic-gate 
454645916cd2Sjpk 	/* check label of the target dir */
454745916cd2Sjpk 	if (is_system_labeled()) {
454845916cd2Sjpk 		ASSERT(req->rq_label != NULL);
454945916cd2Sjpk 		clabel = req->rq_label;
455045916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
455145916cd2Sjpk 		    "got client label from request(1)",
455245916cd2Sjpk 		    struct svc_req *, req);
455345916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
455403986916Sjarrett 			if (!do_rfs_label_check(clabel, ndvp,
4555bd6f1640SJarrett Lu 			    EQUALITY_CHECK, cs->exi)) {
455645916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4557b89a8333Snatalie li - Sun Microsystems - Irvine United States 				goto err_out;
455845916cd2Sjpk 			}
455945916cd2Sjpk 		}
456045916cd2Sjpk 	}
456145916cd2Sjpk 
45627c478bd9Sstevel@tonic-gate 	/*
45637c478bd9Sstevel@tonic-gate 	 * Is the source a file and have a delegation?
45647c478bd9Sstevel@tonic-gate 	 * We don't need to acquire va_seq before these lookups, if
45657c478bd9Sstevel@tonic-gate 	 * it causes an update, cinfo.before will not match, which will
45667c478bd9Sstevel@tonic-gate 	 * trigger a cache flush even if atomic is TRUE.
45677c478bd9Sstevel@tonic-gate 	 */
4568b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (sfp = rfs4_lookup_and_findfile(odvp, converted_onm, &srcvp,
4569b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    &error, cs->cr)) {
45707c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
45717c478bd9Sstevel@tonic-gate 		    NULL)) {
45727c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
45737c478bd9Sstevel@tonic-gate 			goto err_out;
45747c478bd9Sstevel@tonic-gate 		}
45757c478bd9Sstevel@tonic-gate 	}
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	if (srcvp == NULL) {
45787c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
4579b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (onm != converted_onm)
4580b89a8333Snatalie li - Sun Microsystems - Irvine United States 			kmem_free(converted_onm, MAXPATHLEN + 1);
45817c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
4582b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (nnm != converted_nnm)
4583bffeae97SMarcel Telka 			kmem_free(converted_nnm, MAXPATHLEN + 1);
45847c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
4585f3b585ceSsamf 		goto out;
45867c478bd9Sstevel@tonic-gate 	}
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 	sfp_rele_grant_hold = 1;
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	/* Does the destination exist and a file and have a delegation? */
4591b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (fp = rfs4_lookup_and_findfile(ndvp, converted_nnm, &targvp,
4592b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    NULL, cs->cr)) {
45937c478bd9Sstevel@tonic-gate 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
45947c478bd9Sstevel@tonic-gate 		    NULL)) {
45957c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_DELAY;
45967c478bd9Sstevel@tonic-gate 			goto err_out;
45977c478bd9Sstevel@tonic-gate 		}
45987c478bd9Sstevel@tonic-gate 	}
45997c478bd9Sstevel@tonic-gate 	fp_rele_grant_hold = 1;
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	/* Check for NBMAND lock on both source and target */
46037c478bd9Sstevel@tonic-gate 	if (nbl_need_check(srcvp)) {
46047c478bd9Sstevel@tonic-gate 		nbl_start_crit(srcvp, RW_READER);
46057c478bd9Sstevel@tonic-gate 		in_crit_src = 1;
4606da6c28aaSamw 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) {
46077c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
46087c478bd9Sstevel@tonic-gate 			goto err_out;
46097c478bd9Sstevel@tonic-gate 		}
46107c478bd9Sstevel@tonic-gate 	}
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate 	if (targvp && nbl_need_check(targvp)) {
46137c478bd9Sstevel@tonic-gate 		nbl_start_crit(targvp, RW_READER);
46147c478bd9Sstevel@tonic-gate 		in_crit_targ = 1;
4615da6c28aaSamw 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
46167c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
46177c478bd9Sstevel@tonic-gate 			goto err_out;
46187c478bd9Sstevel@tonic-gate 		}
46197c478bd9Sstevel@tonic-gate 	}
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate 	/* Get source "before" change value */
46227c478bd9Sstevel@tonic-gate 	obdva.va_mask = AT_CTIME|AT_SEQ;
4623da6c28aaSamw 	error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL);
46247c478bd9Sstevel@tonic-gate 	if (!error) {
46257c478bd9Sstevel@tonic-gate 		nbdva.va_mask = AT_CTIME|AT_SEQ;
4626da6c28aaSamw 		error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL);
46277c478bd9Sstevel@tonic-gate 	}
46287c478bd9Sstevel@tonic-gate 	if (error) {
46297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
46307c478bd9Sstevel@tonic-gate 		goto err_out;
46317c478bd9Sstevel@tonic-gate 	}
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
46347c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
46357c478bd9Sstevel@tonic-gate 
4636b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if ((error = VOP_RENAME(odvp, converted_onm, ndvp, converted_nnm,
4637b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    cs->cr, NULL, 0)) == 0 && fp != NULL) {
46387c478bd9Sstevel@tonic-gate 		struct vattr va;
4639418d27f3Sshepler 		vnode_t *tvp;
46407c478bd9Sstevel@tonic-gate 
4641d216dff5SRobert Mastors 		rfs4_dbe_lock(fp->rf_dbe);
4642d216dff5SRobert Mastors 		tvp = fp->rf_vp;
4643418d27f3Sshepler 		if (tvp)
4644418d27f3Sshepler 			VN_HOLD(tvp);
4645d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
4646418d27f3Sshepler 
4647418d27f3Sshepler 		if (tvp) {
46487c478bd9Sstevel@tonic-gate 			va.va_mask = AT_NLINK;
4649da6c28aaSamw 			if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
46507c478bd9Sstevel@tonic-gate 			    va.va_nlink == 0) {
46517c478bd9Sstevel@tonic-gate 				/* The file is gone and so should the state */
46527c478bd9Sstevel@tonic-gate 				if (in_crit_targ) {
46537c478bd9Sstevel@tonic-gate 					nbl_end_crit(targvp);
46547c478bd9Sstevel@tonic-gate 					in_crit_targ = 0;
46557c478bd9Sstevel@tonic-gate 				}
46567c478bd9Sstevel@tonic-gate 				rfs4_close_all_state(fp);
46577c478bd9Sstevel@tonic-gate 			}
4658418d27f3Sshepler 			VN_RELE(tvp);
4659418d27f3Sshepler 		}
46607c478bd9Sstevel@tonic-gate 	}
466151ece835Seschrock 	if (error == 0)
466251ece835Seschrock 		vn_renamepath(ndvp, srcvp, nnm, nlen - 1);
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate 	if (in_crit_src)
46657c478bd9Sstevel@tonic-gate 		nbl_end_crit(srcvp);
46667c478bd9Sstevel@tonic-gate 	if (srcvp)
46677c478bd9Sstevel@tonic-gate 		VN_RELE(srcvp);
46687c478bd9Sstevel@tonic-gate 	if (in_crit_targ)
46697c478bd9Sstevel@tonic-gate 		nbl_end_crit(targvp);
46707c478bd9Sstevel@tonic-gate 	if (targvp)
46717c478bd9Sstevel@tonic-gate 		VN_RELE(targvp);
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 	if (sfp) {
46747c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(sfp);
46757c478bd9Sstevel@tonic-gate 		rfs4_file_rele(sfp);
46767c478bd9Sstevel@tonic-gate 	}
46777c478bd9Sstevel@tonic-gate 	if (fp) {
46787c478bd9Sstevel@tonic-gate 		rfs4_clear_dont_grant(fp);
46797c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
46807c478bd9Sstevel@tonic-gate 	}
46817c478bd9Sstevel@tonic-gate 
4682b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (converted_onm != onm)
4683b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(converted_onm, MAXPATHLEN + 1);
46847c478bd9Sstevel@tonic-gate 	kmem_free(onm, olen);
4685b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (converted_nnm != nnm)
4686b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(converted_nnm, MAXPATHLEN + 1);
46877c478bd9Sstevel@tonic-gate 	kmem_free(nnm, nlen);
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate 	/*
46907c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails, set to zero
46917c478bd9Sstevel@tonic-gate 	 */
46927c478bd9Sstevel@tonic-gate 	oidva.va_mask = AT_SEQ;
4693da6c28aaSamw 	if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL))
46947c478bd9Sstevel@tonic-gate 		oidva.va_seq = 0;
46957c478bd9Sstevel@tonic-gate 
46967c478bd9Sstevel@tonic-gate 	nidva.va_mask = AT_SEQ;
4697da6c28aaSamw 	if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL))
46987c478bd9Sstevel@tonic-gate 		nidva.va_seq = 0;
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 	/*
47017c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
47027c478bd9Sstevel@tonic-gate 	 */
4703da6c28aaSamw 	(void) VOP_FSYNC(odvp, 0, cs->cr, NULL);
4704da6c28aaSamw 	(void) VOP_FSYNC(ndvp, 0, cs->cr, NULL);
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate 	if (error) {
47077c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
4708f3b585ceSsamf 		goto out;
47097c478bd9Sstevel@tonic-gate 	}
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 	/*
47127c478bd9Sstevel@tonic-gate 	 * Get "after" change values, if it fails, simply return the
47137c478bd9Sstevel@tonic-gate 	 * before value.
47147c478bd9Sstevel@tonic-gate 	 */
47157c478bd9Sstevel@tonic-gate 	oadva.va_mask = AT_CTIME|AT_SEQ;
4716da6c28aaSamw 	if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) {
47177c478bd9Sstevel@tonic-gate 		oadva.va_ctime = obdva.va_ctime;
47187c478bd9Sstevel@tonic-gate 		oadva.va_seq = 0;
47197c478bd9Sstevel@tonic-gate 	}
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	nadva.va_mask = AT_CTIME|AT_SEQ;
4722da6c28aaSamw 	if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) {
47237c478bd9Sstevel@tonic-gate 		nadva.va_ctime = nbdva.va_ctime;
47247c478bd9Sstevel@tonic-gate 		nadva.va_seq = 0;
47257c478bd9Sstevel@tonic-gate 	}
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
47287c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	/*
47317c478bd9Sstevel@tonic-gate 	 * The cinfo.atomic = TRUE only if we have
47327c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and it has incremented by exactly one
47337c478bd9Sstevel@tonic-gate 	 * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
47347c478bd9Sstevel@tonic-gate 	 */
47357c478bd9Sstevel@tonic-gate 	if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
47361b300de9Sjwahlig 	    oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq)
47377c478bd9Sstevel@tonic-gate 		resp->source_cinfo.atomic = TRUE;
47387c478bd9Sstevel@tonic-gate 	else
47397c478bd9Sstevel@tonic-gate 		resp->source_cinfo.atomic = FALSE;
47407c478bd9Sstevel@tonic-gate 
47417c478bd9Sstevel@tonic-gate 	if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
47421b300de9Sjwahlig 	    nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq)
47437c478bd9Sstevel@tonic-gate 		resp->target_cinfo.atomic = TRUE;
47447c478bd9Sstevel@tonic-gate 	else
47457c478bd9Sstevel@tonic-gate 		resp->target_cinfo.atomic = FALSE;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate #ifdef	VOLATILE_FH_TEST
47487c478bd9Sstevel@tonic-gate 	{
47497c478bd9Sstevel@tonic-gate 	extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	/*
47527c478bd9Sstevel@tonic-gate 	 * Add the renamed file handle to the volatile rename list
47537c478bd9Sstevel@tonic-gate 	 */
47547c478bd9Sstevel@tonic-gate 	if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
47557c478bd9Sstevel@tonic-gate 		/* file handles may expire on rename */
47567c478bd9Sstevel@tonic-gate 		vnode_t *vp;
47577c478bd9Sstevel@tonic-gate 
47587c478bd9Sstevel@tonic-gate 		nnm = utf8_to_fn(&args->newname, &nlen, NULL);
47597c478bd9Sstevel@tonic-gate 		/*
47607c478bd9Sstevel@tonic-gate 		 * Already know that nnm will be a valid string
47617c478bd9Sstevel@tonic-gate 		 */
4762da6c28aaSamw 		error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr,
4763da6c28aaSamw 		    NULL, NULL, NULL);
47647c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
47657c478bd9Sstevel@tonic-gate 		if (!error) {
47667c478bd9Sstevel@tonic-gate 			add_volrnm_fh(cs->exi, vp);
47677c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
47687c478bd9Sstevel@tonic-gate 		}
47697c478bd9Sstevel@tonic-gate 	}
47707c478bd9Sstevel@tonic-gate 	}
47717c478bd9Sstevel@tonic-gate #endif	/* VOLATILE_FH_TEST */
47727c478bd9Sstevel@tonic-gate 
47737c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
4774f3b585ceSsamf out:
4775f3b585ceSsamf 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4776f3b585ceSsamf 	    RENAME4res *, resp);
47777c478bd9Sstevel@tonic-gate 	return;
47787c478bd9Sstevel@tonic-gate 
47797c478bd9Sstevel@tonic-gate err_out:
4780b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (onm != converted_onm)
4781b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(converted_onm, MAXPATHLEN + 1);
4782b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (onm != NULL)
47837c478bd9Sstevel@tonic-gate 		kmem_free(onm, olen);
4784b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nnm != converted_nnm)
4785b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(converted_nnm, MAXPATHLEN + 1);
4786b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nnm != NULL)
47877c478bd9Sstevel@tonic-gate 		kmem_free(nnm, nlen);
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 	if (in_crit_src) nbl_end_crit(srcvp);
47907c478bd9Sstevel@tonic-gate 	if (in_crit_targ) nbl_end_crit(targvp);
47917c478bd9Sstevel@tonic-gate 	if (targvp) VN_RELE(targvp);
47927c478bd9Sstevel@tonic-gate 	if (srcvp) VN_RELE(srcvp);
47937c478bd9Sstevel@tonic-gate 	if (sfp) {
47947c478bd9Sstevel@tonic-gate 		if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
47957c478bd9Sstevel@tonic-gate 		rfs4_file_rele(sfp);
47967c478bd9Sstevel@tonic-gate 	}
47977c478bd9Sstevel@tonic-gate 	if (fp) {
47987c478bd9Sstevel@tonic-gate 		if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
47997c478bd9Sstevel@tonic-gate 		rfs4_file_rele(fp);
48007c478bd9Sstevel@tonic-gate 	}
4801f3b585ceSsamf 
4802f3b585ceSsamf 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4803f3b585ceSsamf 	    RENAME4res *, resp);
48047c478bd9Sstevel@tonic-gate }
48057c478bd9Sstevel@tonic-gate 
48067c478bd9Sstevel@tonic-gate /* ARGSUSED */
48077c478bd9Sstevel@tonic-gate static void
48087c478bd9Sstevel@tonic-gate rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
48097c478bd9Sstevel@tonic-gate     struct compound_state *cs)
48107c478bd9Sstevel@tonic-gate {
48117c478bd9Sstevel@tonic-gate 	RENEW4args *args = &argop->nfs_argop4_u.oprenew;
48127c478bd9Sstevel@tonic-gate 	RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
48137c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
48147c478bd9Sstevel@tonic-gate 
4815f3b585ceSsamf 	DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs,
4816f3b585ceSsamf 	    RENEW4args *, args);
4817f3b585ceSsamf 
48187c478bd9Sstevel@tonic-gate 	if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
48197c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
48207c478bd9Sstevel@tonic-gate 		    rfs4_check_clientid(&args->clientid, 0);
4821f3b585ceSsamf 		goto out;
48227c478bd9Sstevel@tonic-gate 	}
48237c478bd9Sstevel@tonic-gate 
48247c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
48257c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
48267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
4827f3b585ceSsamf 		goto out;
48287c478bd9Sstevel@tonic-gate 	}
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	rfs4_update_lease(cp);
48317c478bd9Sstevel@tonic-gate 
4832d216dff5SRobert Mastors 	mutex_enter(cp->rc_cbinfo.cb_lock);
4833d216dff5SRobert Mastors 	if (cp->rc_cbinfo.cb_notified_of_cb_path_down == FALSE) {
4834d216dff5SRobert Mastors 		cp->rc_cbinfo.cb_notified_of_cb_path_down = TRUE;
48357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
48367c478bd9Sstevel@tonic-gate 	} else {
48377c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
48387c478bd9Sstevel@tonic-gate 	}
4839d216dff5SRobert Mastors 	mutex_exit(cp->rc_cbinfo.cb_lock);
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
48427c478bd9Sstevel@tonic-gate 
4843f3b585ceSsamf out:
4844f3b585ceSsamf 	DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs,
4845f3b585ceSsamf 	    RENEW4res *, resp);
48467c478bd9Sstevel@tonic-gate }
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate /* ARGSUSED */
48497c478bd9Sstevel@tonic-gate static void
48507c478bd9Sstevel@tonic-gate rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
48517c478bd9Sstevel@tonic-gate     struct compound_state *cs)
48527c478bd9Sstevel@tonic-gate {
48537c478bd9Sstevel@tonic-gate 	RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
48547c478bd9Sstevel@tonic-gate 
4855f3b585ceSsamf 	DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs);
4856f3b585ceSsamf 
48577c478bd9Sstevel@tonic-gate 	/* No need to check cs->access - we are not accessing any object */
48587c478bd9Sstevel@tonic-gate 	if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
48597c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_RESTOREFH;
4860f3b585ceSsamf 		goto out;
48617c478bd9Sstevel@tonic-gate 	}
48627c478bd9Sstevel@tonic-gate 	if (cs->vp != NULL) {
48637c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
48647c478bd9Sstevel@tonic-gate 	}
48657c478bd9Sstevel@tonic-gate 	cs->vp = cs->saved_vp;
48667c478bd9Sstevel@tonic-gate 	cs->saved_vp = NULL;
4867aafcd32bSMarcel Telka 	if (cs->exi != NULL)
4868fbd2e8e1SArne Jansen 		exi_rele(cs->exi);
48697c478bd9Sstevel@tonic-gate 	cs->exi = cs->saved_exi;
4870aafcd32bSMarcel Telka 	if (cs->exi != NULL)
4871fbd2e8e1SArne Jansen 		exi_hold(cs->exi);
48727c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->saved_fh, &cs->fh);
48737c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
48747c478bd9Sstevel@tonic-gate 	cs->deleg = FALSE;
4875f3b585ceSsamf 
4876f3b585ceSsamf out:
4877f3b585ceSsamf 	DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs,
4878f3b585ceSsamf 	    RESTOREFH4res *, resp);
48797c478bd9Sstevel@tonic-gate }
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate /* ARGSUSED */
48827c478bd9Sstevel@tonic-gate static void
48837c478bd9Sstevel@tonic-gate rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
48847c478bd9Sstevel@tonic-gate     struct compound_state *cs)
48857c478bd9Sstevel@tonic-gate {
48867c478bd9Sstevel@tonic-gate 	SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
48877c478bd9Sstevel@tonic-gate 
4888f3b585ceSsamf 	DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs);
4889f3b585ceSsamf 
48907c478bd9Sstevel@tonic-gate 	/* No need to check cs->access - we are not accessing any object */
48917c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
48927c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4893f3b585ceSsamf 		goto out;
48947c478bd9Sstevel@tonic-gate 	}
48957c478bd9Sstevel@tonic-gate 	if (cs->saved_vp != NULL) {
48967c478bd9Sstevel@tonic-gate 		VN_RELE(cs->saved_vp);
48977c478bd9Sstevel@tonic-gate 	}
48987c478bd9Sstevel@tonic-gate 	cs->saved_vp = cs->vp;
48997c478bd9Sstevel@tonic-gate 	VN_HOLD(cs->saved_vp);
4900aafcd32bSMarcel Telka 	if (cs->saved_exi != NULL)
4901fbd2e8e1SArne Jansen 		exi_rele(cs->saved_exi);
49027c478bd9Sstevel@tonic-gate 	cs->saved_exi = cs->exi;
4903aafcd32bSMarcel Telka 	if (cs->saved_exi != NULL)
4904fbd2e8e1SArne Jansen 		exi_hold(cs->saved_exi);
49057c478bd9Sstevel@tonic-gate 	/*
49067c478bd9Sstevel@tonic-gate 	 * since SAVEFH is fairly rare, don't alloc space for its fh
49077c478bd9Sstevel@tonic-gate 	 * unless necessary.
49087c478bd9Sstevel@tonic-gate 	 */
49097c478bd9Sstevel@tonic-gate 	if (cs->saved_fh.nfs_fh4_val == NULL) {
49107c478bd9Sstevel@tonic-gate 		cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
49117c478bd9Sstevel@tonic-gate 	}
49127c478bd9Sstevel@tonic-gate 	nfs_fh4_copy(&cs->fh, &cs->saved_fh);
49137c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
4914f3b585ceSsamf 
4915f3b585ceSsamf out:
4916f3b585ceSsamf 	DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs,
4917f3b585ceSsamf 	    SAVEFH4res *, resp);
49187c478bd9Sstevel@tonic-gate }
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate /*
49217c478bd9Sstevel@tonic-gate  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
49227c478bd9Sstevel@tonic-gate  * return the bitmap of attrs that were set successfully. It is also
49237c478bd9Sstevel@tonic-gate  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
49247c478bd9Sstevel@tonic-gate  * always be called only after rfs4_do_set_attrs().
49257c478bd9Sstevel@tonic-gate  *
49267c478bd9Sstevel@tonic-gate  * Verify that the attributes are same as the expected ones. sargp->vap
49277c478bd9Sstevel@tonic-gate  * and sargp->sbp contain the input attributes as translated from fattr4.
49287c478bd9Sstevel@tonic-gate  *
49297c478bd9Sstevel@tonic-gate  * This function verifies only the attrs that correspond to a vattr or
49307c478bd9Sstevel@tonic-gate  * vfsstat struct. That is because of the extra step needed to get the
49317c478bd9Sstevel@tonic-gate  * corresponding system structs. Other attributes have already been set or
49327c478bd9Sstevel@tonic-gate  * verified by do_rfs4_set_attrs.
49337c478bd9Sstevel@tonic-gate  *
49347c478bd9Sstevel@tonic-gate  * Return 0 if all attrs match, -1 if some don't, error if error processing.
49357c478bd9Sstevel@tonic-gate  */
49367c478bd9Sstevel@tonic-gate static int
49377c478bd9Sstevel@tonic-gate rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
49387c478bd9Sstevel@tonic-gate     bitmap4 *resp, struct nfs4_ntov_table *ntovp)
49397c478bd9Sstevel@tonic-gate {
49407c478bd9Sstevel@tonic-gate 	int error, ret_error = 0;
49417c478bd9Sstevel@tonic-gate 	int i, k;
49427c478bd9Sstevel@tonic-gate 	uint_t sva_mask = sargp->vap->va_mask;
49437c478bd9Sstevel@tonic-gate 	uint_t vbit;
49447c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
49457c478bd9Sstevel@tonic-gate 	uint8_t *amap;
49467c478bd9Sstevel@tonic-gate 	bool_t getsb = ntovp->vfsstat;
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate 	if (sva_mask != 0) {
49497c478bd9Sstevel@tonic-gate 		/*
49507c478bd9Sstevel@tonic-gate 		 * Okay to overwrite sargp->vap because we verify based
49517c478bd9Sstevel@tonic-gate 		 * on the incoming values.
49527c478bd9Sstevel@tonic-gate 		 */
49537c478bd9Sstevel@tonic-gate 		ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
4954da6c28aaSamw 		    sargp->cs->cr, NULL);
49557c478bd9Sstevel@tonic-gate 		if (ret_error) {
49567c478bd9Sstevel@tonic-gate 			if (resp == NULL)
49577c478bd9Sstevel@tonic-gate 				return (ret_error);
49587c478bd9Sstevel@tonic-gate 			/*
49597c478bd9Sstevel@tonic-gate 			 * Must return bitmap of successful attrs
49607c478bd9Sstevel@tonic-gate 			 */
49617c478bd9Sstevel@tonic-gate 			sva_mask = 0;	/* to prevent checking vap later */
49627c478bd9Sstevel@tonic-gate 		} else {
49637c478bd9Sstevel@tonic-gate 			/*
49647c478bd9Sstevel@tonic-gate 			 * Some file systems clobber va_mask. it is probably
49657c478bd9Sstevel@tonic-gate 			 * wrong of them to do so, nonethless we practice
49667c478bd9Sstevel@tonic-gate 			 * defensive coding.
49677c478bd9Sstevel@tonic-gate 			 * See bug id 4276830.
49687c478bd9Sstevel@tonic-gate 			 */
49697c478bd9Sstevel@tonic-gate 			sargp->vap->va_mask = sva_mask;
49707c478bd9Sstevel@tonic-gate 		}
49717c478bd9Sstevel@tonic-gate 	}
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 	if (getsb) {
49747c478bd9Sstevel@tonic-gate 		/*
49757c478bd9Sstevel@tonic-gate 		 * Now get the superblock and loop on the bitmap, as there is
49767c478bd9Sstevel@tonic-gate 		 * no simple way of translating from superblock to bitmap4.
49777c478bd9Sstevel@tonic-gate 		 */
49787c478bd9Sstevel@tonic-gate 		ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
49797c478bd9Sstevel@tonic-gate 		if (ret_error) {
49807c478bd9Sstevel@tonic-gate 			if (resp == NULL)
49817c478bd9Sstevel@tonic-gate 				goto errout;
49827c478bd9Sstevel@tonic-gate 			getsb = FALSE;
49837c478bd9Sstevel@tonic-gate 		}
49847c478bd9Sstevel@tonic-gate 	}
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 	/*
49877c478bd9Sstevel@tonic-gate 	 * Now loop and verify each attribute which getattr returned
49887c478bd9Sstevel@tonic-gate 	 * whether it's the same as the input.
49897c478bd9Sstevel@tonic-gate 	 */
49907c478bd9Sstevel@tonic-gate 	if (resp == NULL && !getsb && (sva_mask == 0))
49917c478bd9Sstevel@tonic-gate 		goto errout;
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate 	na = ntovp->na;
49947c478bd9Sstevel@tonic-gate 	amap = ntovp->amap;
49957c478bd9Sstevel@tonic-gate 	k = 0;
49967c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
49977c478bd9Sstevel@tonic-gate 		k = *amap;
49987c478bd9Sstevel@tonic-gate 		ASSERT(nfs4_ntov_map[k].nval == k);
49997c478bd9Sstevel@tonic-gate 		vbit = nfs4_ntov_map[k].vbit;
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 		/*
50027c478bd9Sstevel@tonic-gate 		 * If vattr attribute but VOP_GETATTR failed, or it's
50037c478bd9Sstevel@tonic-gate 		 * superblock attribute but VFS_STATVFS failed, skip
50047c478bd9Sstevel@tonic-gate 		 */
50057c478bd9Sstevel@tonic-gate 		if (vbit) {
50067c478bd9Sstevel@tonic-gate 			if ((vbit & sva_mask) == 0)
50077c478bd9Sstevel@tonic-gate 				continue;
50087c478bd9Sstevel@tonic-gate 		} else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
50097c478bd9Sstevel@tonic-gate 			continue;
50107c478bd9Sstevel@tonic-gate 		}
50111b300de9Sjwahlig 		error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na);
50127c478bd9Sstevel@tonic-gate 		if (resp != NULL) {
50137c478bd9Sstevel@tonic-gate 			if (error)
50147c478bd9Sstevel@tonic-gate 				ret_error = -1;	/* not all match */
50157c478bd9Sstevel@tonic-gate 			else	/* update response bitmap */
50167c478bd9Sstevel@tonic-gate 				*resp |= nfs4_ntov_map[k].fbit;
50177c478bd9Sstevel@tonic-gate 			continue;
50187c478bd9Sstevel@tonic-gate 		}
50197c478bd9Sstevel@tonic-gate 		if (error) {
50207c478bd9Sstevel@tonic-gate 			ret_error = -1;	/* not all match */
50217c478bd9Sstevel@tonic-gate 			break;
50227c478bd9Sstevel@tonic-gate 		}
50237c478bd9Sstevel@tonic-gate 	}
50247c478bd9Sstevel@tonic-gate errout:
50257c478bd9Sstevel@tonic-gate 	return (ret_error);
50267c478bd9Sstevel@tonic-gate }
50277c478bd9Sstevel@tonic-gate 
50287c478bd9Sstevel@tonic-gate /*
50297c478bd9Sstevel@tonic-gate  * Decode the attribute to be set/verified. If the attr requires a sys op
50307c478bd9Sstevel@tonic-gate  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
50317c478bd9Sstevel@tonic-gate  * call the sv_getit function for it, because the sys op hasn't yet been done.
50327c478bd9Sstevel@tonic-gate  * Return 0 for success, error code if failed.
50337c478bd9Sstevel@tonic-gate  *
50347c478bd9Sstevel@tonic-gate  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
50357c478bd9Sstevel@tonic-gate  */
50367c478bd9Sstevel@tonic-gate static int
50377c478bd9Sstevel@tonic-gate decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
50387c478bd9Sstevel@tonic-gate     int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
50397c478bd9Sstevel@tonic-gate {
50407c478bd9Sstevel@tonic-gate 	int error = 0;
50417c478bd9Sstevel@tonic-gate 	bool_t set_later;
50427c478bd9Sstevel@tonic-gate 
50437c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 	if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
50467c478bd9Sstevel@tonic-gate 		set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
50477c478bd9Sstevel@tonic-gate 		/*
50487c478bd9Sstevel@tonic-gate 		 * don't verify yet if a vattr or sb dependent attr,
50497c478bd9Sstevel@tonic-gate 		 * because we don't have their sys values yet.
50507c478bd9Sstevel@tonic-gate 		 * Will be done later.
50517c478bd9Sstevel@tonic-gate 		 */
50527c478bd9Sstevel@tonic-gate 		if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
50537c478bd9Sstevel@tonic-gate 			/*
50547c478bd9Sstevel@tonic-gate 			 * ACLs are a special case, since setting the MODE
50557c478bd9Sstevel@tonic-gate 			 * conflicts with setting the ACL.  We delay setting
50567c478bd9Sstevel@tonic-gate 			 * the ACL until all other attributes have been set.
50577c478bd9Sstevel@tonic-gate 			 * The ACL gets set in do_rfs4_op_setattr().
50587c478bd9Sstevel@tonic-gate 			 */
50597c478bd9Sstevel@tonic-gate 			if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
50607c478bd9Sstevel@tonic-gate 				error = (*nfs4_ntov_map[k].sv_getit)(cmd,
50617c478bd9Sstevel@tonic-gate 				    sargp, nap);
50627c478bd9Sstevel@tonic-gate 				if (error) {
50637c478bd9Sstevel@tonic-gate 					xdr_free(nfs4_ntov_map[k].xfunc,
50647c478bd9Sstevel@tonic-gate 					    (caddr_t)nap);
50657c478bd9Sstevel@tonic-gate 				}
50667c478bd9Sstevel@tonic-gate 			}
50677c478bd9Sstevel@tonic-gate 		}
50687c478bd9Sstevel@tonic-gate 	} else {
50697c478bd9Sstevel@tonic-gate #ifdef  DEBUG
50707c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "decode_fattr4_attr: error "
50717c478bd9Sstevel@tonic-gate 		    "decoding attribute %d\n", k);
50727c478bd9Sstevel@tonic-gate #endif
50737c478bd9Sstevel@tonic-gate 		error = EINVAL;
50747c478bd9Sstevel@tonic-gate 	}
50757c478bd9Sstevel@tonic-gate 	if (!error && resp_bval && !set_later) {
50767c478bd9Sstevel@tonic-gate 		*resp_bval |= nfs4_ntov_map[k].fbit;
50777c478bd9Sstevel@tonic-gate 	}
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	return (error);
50807c478bd9Sstevel@tonic-gate }
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate /*
50837c478bd9Sstevel@tonic-gate  * Set vattr based on incoming fattr4 attrs - used by setattr.
50847c478bd9Sstevel@tonic-gate  * Set response mask. Ignore any values that are not writable vattr attrs.
50857c478bd9Sstevel@tonic-gate  */
50867c478bd9Sstevel@tonic-gate static nfsstat4
50877c478bd9Sstevel@tonic-gate do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
50887c478bd9Sstevel@tonic-gate     struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
50897c478bd9Sstevel@tonic-gate     nfs4_attr_cmd_t cmd)
50907c478bd9Sstevel@tonic-gate {
50917c478bd9Sstevel@tonic-gate 	int error = 0;
50927c478bd9Sstevel@tonic-gate 	int i;
50937c478bd9Sstevel@tonic-gate 	char *attrs = fattrp->attrlist4;
50947c478bd9Sstevel@tonic-gate 	uint32_t attrslen = fattrp->attrlist4_len;
50957c478bd9Sstevel@tonic-gate 	XDR xdr;
50967c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
50977c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
50987c478bd9Sstevel@tonic-gate 	union nfs4_attr_u *na;
50997c478bd9Sstevel@tonic-gate 	uint8_t *amap;
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate #ifndef lint
51027c478bd9Sstevel@tonic-gate 	/*
51037c478bd9Sstevel@tonic-gate 	 * Make sure that maximum attribute number can be expressed as an
51047c478bd9Sstevel@tonic-gate 	 * 8 bit quantity.
51057c478bd9Sstevel@tonic-gate 	 */
51067c478bd9Sstevel@tonic-gate 	ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
51077c478bd9Sstevel@tonic-gate #endif
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
51107c478bd9Sstevel@tonic-gate 		if (resp)
51117c478bd9Sstevel@tonic-gate 			*resp = 0;
51127c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOFILEHANDLE);
51137c478bd9Sstevel@tonic-gate 	}
51147c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
51157c478bd9Sstevel@tonic-gate 		if (resp)
51167c478bd9Sstevel@tonic-gate 			*resp = 0;
51177c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ACCESS);
51187c478bd9Sstevel@tonic-gate 	}
51197c478bd9Sstevel@tonic-gate 
51207c478bd9Sstevel@tonic-gate 	sargp->op = cmd;
51217c478bd9Sstevel@tonic-gate 	sargp->cs = cs;
51227c478bd9Sstevel@tonic-gate 	sargp->flag = 0;	/* may be set later */
51237c478bd9Sstevel@tonic-gate 	sargp->vap->va_mask = 0;
51247c478bd9Sstevel@tonic-gate 	sargp->rdattr_error = NFS4_OK;
51257c478bd9Sstevel@tonic-gate 	sargp->rdattr_error_req = FALSE;
51267c478bd9Sstevel@tonic-gate 	/* sargp->sbp is set by the caller */
51277c478bd9Sstevel@tonic-gate 
51287c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 	na = ntovp->na;
51317c478bd9Sstevel@tonic-gate 	amap = ntovp->amap;
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	/*
51347c478bd9Sstevel@tonic-gate 	 * The following loop iterates on the nfs4_ntov_map checking
51357c478bd9Sstevel@tonic-gate 	 * if the fbit is set in the requested bitmap.
51367c478bd9Sstevel@tonic-gate 	 * If set then we process the arguments using the
51377c478bd9Sstevel@tonic-gate 	 * rfs4_fattr4 conversion functions to populate the setattr
51387c478bd9Sstevel@tonic-gate 	 * vattr and va_mask. Any settable attrs that are not using vattr
51397c478bd9Sstevel@tonic-gate 	 * will be set in this loop.
51407c478bd9Sstevel@tonic-gate 	 */
51417c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfs4_ntov_map_size; i++) {
51427c478bd9Sstevel@tonic-gate 		if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
51437c478bd9Sstevel@tonic-gate 			continue;
51447c478bd9Sstevel@tonic-gate 		}
51457c478bd9Sstevel@tonic-gate 		/*
51467c478bd9Sstevel@tonic-gate 		 * If setattr, must be a writable attr.
51477c478bd9Sstevel@tonic-gate 		 * If verify/nverify, must be a readable attr.
51487c478bd9Sstevel@tonic-gate 		 */
51497c478bd9Sstevel@tonic-gate 		if ((error = (*nfs4_ntov_map[i].sv_getit)(
51507c478bd9Sstevel@tonic-gate 		    NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
51517c478bd9Sstevel@tonic-gate 			/*
51527c478bd9Sstevel@tonic-gate 			 * Client tries to set/verify an
51537c478bd9Sstevel@tonic-gate 			 * unsupported attribute, tries to set
51547c478bd9Sstevel@tonic-gate 			 * a read only attr or verify a write
51557c478bd9Sstevel@tonic-gate 			 * only one - error!
51567c478bd9Sstevel@tonic-gate 			 */
51577c478bd9Sstevel@tonic-gate 			break;
51587c478bd9Sstevel@tonic-gate 		}
51597c478bd9Sstevel@tonic-gate 		/*
51607c478bd9Sstevel@tonic-gate 		 * Decode the attribute to set/verify
51617c478bd9Sstevel@tonic-gate 		 */
51627c478bd9Sstevel@tonic-gate 		error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
51637c478bd9Sstevel@tonic-gate 		    &xdr, resp ? resp : NULL, na);
51647c478bd9Sstevel@tonic-gate 		if (error)
51657c478bd9Sstevel@tonic-gate 			break;
51667c478bd9Sstevel@tonic-gate 		*amap++ = (uint8_t)nfs4_ntov_map[i].nval;
51677c478bd9Sstevel@tonic-gate 		na++;
51687c478bd9Sstevel@tonic-gate 		(ntovp->attrcnt)++;
51697c478bd9Sstevel@tonic-gate 		if (nfs4_ntov_map[i].vfsstat)
51707c478bd9Sstevel@tonic-gate 			ntovp->vfsstat = TRUE;
51717c478bd9Sstevel@tonic-gate 	}
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 	if (error != 0)
51747c478bd9Sstevel@tonic-gate 		status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP :
51757c478bd9Sstevel@tonic-gate 		    puterrno4(error));
51767c478bd9Sstevel@tonic-gate 	/* xdrmem_destroy(&xdrs); */	/* NO-OP */
51777c478bd9Sstevel@tonic-gate 	return (status);
51787c478bd9Sstevel@tonic-gate }
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate static nfsstat4
51817c478bd9Sstevel@tonic-gate do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
51827c478bd9Sstevel@tonic-gate     stateid4 *stateid)
51837c478bd9Sstevel@tonic-gate {
51847c478bd9Sstevel@tonic-gate 	int error = 0;
51857c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
51867c478bd9Sstevel@tonic-gate 	bool_t trunc;
51877c478bd9Sstevel@tonic-gate 
51887c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
51897c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
51907c478bd9Sstevel@tonic-gate 	vnode_t *vp = cs->vp;
51917c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
51927c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
51937c478bd9Sstevel@tonic-gate 	struct vattr bva;
51947c478bd9Sstevel@tonic-gate 	struct flock64 bf;
51957c478bd9Sstevel@tonic-gate 	int in_crit = 0;
51967c478bd9Sstevel@tonic-gate 	uint_t saved_mask = 0;
51977c478bd9Sstevel@tonic-gate 	caller_context_t ct;
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	*resp = 0;
52007c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
52012f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
52027c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
52037c478bd9Sstevel@tonic-gate 	status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
52047c478bd9Sstevel@tonic-gate 	    NFS4ATTR_SETIT);
52057c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
52067c478bd9Sstevel@tonic-gate 		/*
52077c478bd9Sstevel@tonic-gate 		 * failed set attrs
52087c478bd9Sstevel@tonic-gate 		 */
52097c478bd9Sstevel@tonic-gate 		goto done;
52107c478bd9Sstevel@tonic-gate 	}
52117c478bd9Sstevel@tonic-gate 	if ((sarg.vap->va_mask == 0) &&
52127c478bd9Sstevel@tonic-gate 	    (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
52137c478bd9Sstevel@tonic-gate 		/*
52147c478bd9Sstevel@tonic-gate 		 * no further work to be done
52157c478bd9Sstevel@tonic-gate 		 */
52167c478bd9Sstevel@tonic-gate 		goto done;
52177c478bd9Sstevel@tonic-gate 	}
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 	/*
52207c478bd9Sstevel@tonic-gate 	 * If we got a request to set the ACL and the MODE, only
52217c478bd9Sstevel@tonic-gate 	 * allow changing VSUID, VSGID, and VSVTX.  Attempting
52227c478bd9Sstevel@tonic-gate 	 * to change any other bits, along with setting an ACL,
52237c478bd9Sstevel@tonic-gate 	 * gives NFS4ERR_INVAL.
52247c478bd9Sstevel@tonic-gate 	 */
52257c478bd9Sstevel@tonic-gate 	if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
52267c478bd9Sstevel@tonic-gate 	    (fattrp->attrmask & FATTR4_MODE_MASK)) {
52277c478bd9Sstevel@tonic-gate 		vattr_t va;
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate 		va.va_mask = AT_MODE;
5230da6c28aaSamw 		error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
52317c478bd9Sstevel@tonic-gate 		if (error) {
52327c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
52337c478bd9Sstevel@tonic-gate 			goto done;
52347c478bd9Sstevel@tonic-gate 		}
52357c478bd9Sstevel@tonic-gate 		if ((sarg.vap->va_mode ^ va.va_mode) &
52367c478bd9Sstevel@tonic-gate 		    ~(VSUID | VSGID | VSVTX)) {
52377c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
52387c478bd9Sstevel@tonic-gate 			goto done;
52397c478bd9Sstevel@tonic-gate 		}
52407c478bd9Sstevel@tonic-gate 	}
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 	/* Check stateid only if size has been set */
52437c478bd9Sstevel@tonic-gate 	if (sarg.vap->va_mask & AT_SIZE) {
52447c478bd9Sstevel@tonic-gate 		trunc = (sarg.vap->va_size == 0);
52457c478bd9Sstevel@tonic-gate 		status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
5246da6c28aaSamw 		    trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct);
52477c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK)
52487c478bd9Sstevel@tonic-gate 			goto done;
5249da6c28aaSamw 	} else {
52507c478bd9Sstevel@tonic-gate 		ct.cc_sysid = 0;
52517c478bd9Sstevel@tonic-gate 		ct.cc_pid = 0;
52527c478bd9Sstevel@tonic-gate 		ct.cc_caller_id = nfs4_srv_caller_id;
525362b9fcbeSjwahlig 		ct.cc_flags = CC_DONTBLOCK;
5254da6c28aaSamw 	}
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 	/* XXX start of possible race with delegations */
52577c478bd9Sstevel@tonic-gate 
52587c478bd9Sstevel@tonic-gate 	/*
52597c478bd9Sstevel@tonic-gate 	 * We need to specially handle size changes because it is
52607c478bd9Sstevel@tonic-gate 	 * possible for the client to create a file with read-only
52617c478bd9Sstevel@tonic-gate 	 * modes, but with the file opened for writing. If the client
52627c478bd9Sstevel@tonic-gate 	 * then tries to set the file size, e.g. ftruncate(3C),
52637c478bd9Sstevel@tonic-gate 	 * fcntl(F_FREESP), the normal access checking done in
52647c478bd9Sstevel@tonic-gate 	 * VOP_SETATTR would prevent the client from doing it even though
52657c478bd9Sstevel@tonic-gate 	 * it should be allowed to do so.  To get around this, we do the
52667c478bd9Sstevel@tonic-gate 	 * access checking for ourselves and use VOP_SPACE which doesn't
52677c478bd9Sstevel@tonic-gate 	 * do the access checking.
52687c478bd9Sstevel@tonic-gate 	 * Also the client should not be allowed to change the file
52697c478bd9Sstevel@tonic-gate 	 * size if there is a conflicting non-blocking mandatory lock in
52707c478bd9Sstevel@tonic-gate 	 * the region of the change.
52717c478bd9Sstevel@tonic-gate 	 */
52727c478bd9Sstevel@tonic-gate 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
52737c478bd9Sstevel@tonic-gate 		u_offset_t offset;
52747c478bd9Sstevel@tonic-gate 		ssize_t length;
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 		/*
52779720e166Sjasmith 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
52789720e166Sjasmith 		 * before returning, sarg.vap->va_mask is used to
52799720e166Sjasmith 		 * generate the setattr reply bitmap.  We also clear
52809720e166Sjasmith 		 * AT_SIZE below before calling VOP_SPACE.  For both
52819720e166Sjasmith 		 * of these cases, the va_mask needs to be saved here
52829720e166Sjasmith 		 * and restored after calling VOP_SETATTR.
52839720e166Sjasmith 		 */
52849720e166Sjasmith 		saved_mask = sarg.vap->va_mask;
52859720e166Sjasmith 
52869720e166Sjasmith 		/*
52877c478bd9Sstevel@tonic-gate 		 * Check any possible conflict due to NBMAND locks.
52887c478bd9Sstevel@tonic-gate 		 * Get into critical region before VOP_GETATTR, so the
52897c478bd9Sstevel@tonic-gate 		 * size attribute is valid when checking conflicts.
52907c478bd9Sstevel@tonic-gate 		 */
52917c478bd9Sstevel@tonic-gate 		if (nbl_need_check(vp)) {
52927c478bd9Sstevel@tonic-gate 			nbl_start_crit(vp, RW_READER);
52937c478bd9Sstevel@tonic-gate 			in_crit = 1;
52947c478bd9Sstevel@tonic-gate 		}
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate 		bva.va_mask = AT_UID|AT_SIZE;
5297da6c28aaSamw 		if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
52987c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
52997c478bd9Sstevel@tonic-gate 			goto done;
53007c478bd9Sstevel@tonic-gate 		}
53017c478bd9Sstevel@tonic-gate 
53027c478bd9Sstevel@tonic-gate 		if (in_crit) {
53037c478bd9Sstevel@tonic-gate 			if (sarg.vap->va_size < bva.va_size) {
53047c478bd9Sstevel@tonic-gate 				offset = sarg.vap->va_size;
53057c478bd9Sstevel@tonic-gate 				length = bva.va_size - sarg.vap->va_size;
53067c478bd9Sstevel@tonic-gate 			} else {
53077c478bd9Sstevel@tonic-gate 				offset = bva.va_size;
53087c478bd9Sstevel@tonic-gate 				length = sarg.vap->va_size - bva.va_size;
53097c478bd9Sstevel@tonic-gate 			}
5310da6c28aaSamw 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
5311da6c28aaSamw 			    &ct)) {
53127c478bd9Sstevel@tonic-gate 				status = NFS4ERR_LOCKED;
53137c478bd9Sstevel@tonic-gate 				goto done;
53147c478bd9Sstevel@tonic-gate 			}
53157c478bd9Sstevel@tonic-gate 		}
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate 		if (crgetuid(cr) == bva.va_uid) {
53187c478bd9Sstevel@tonic-gate 			sarg.vap->va_mask &= ~AT_SIZE;
53197c478bd9Sstevel@tonic-gate 			bf.l_type = F_WRLCK;
53207c478bd9Sstevel@tonic-gate 			bf.l_whence = 0;
53217c478bd9Sstevel@tonic-gate 			bf.l_start = (off64_t)sarg.vap->va_size;
53227c478bd9Sstevel@tonic-gate 			bf.l_len = 0;
53237c478bd9Sstevel@tonic-gate 			bf.l_sysid = 0;
53247c478bd9Sstevel@tonic-gate 			bf.l_pid = 0;
53257c478bd9Sstevel@tonic-gate 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
53267c478bd9Sstevel@tonic-gate 			    (offset_t)sarg.vap->va_size, cr, &ct);
53277c478bd9Sstevel@tonic-gate 		}
53287c478bd9Sstevel@tonic-gate 	}
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	if (!error && sarg.vap->va_mask != 0)
53317c478bd9Sstevel@tonic-gate 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
53327c478bd9Sstevel@tonic-gate 
53339720e166Sjasmith 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
53347c478bd9Sstevel@tonic-gate 	if (saved_mask & AT_SIZE)
53357c478bd9Sstevel@tonic-gate 		sarg.vap->va_mask |= AT_SIZE;
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 	/*
53387c478bd9Sstevel@tonic-gate 	 * If an ACL was being set, it has been delayed until now,
53397c478bd9Sstevel@tonic-gate 	 * in order to set the mode (via the VOP_SETATTR() above) first.
53407c478bd9Sstevel@tonic-gate 	 */
53417c478bd9Sstevel@tonic-gate 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
53427c478bd9Sstevel@tonic-gate 		int i;
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
53457c478bd9Sstevel@tonic-gate 			if (ntov.amap[i] == FATTR4_ACL)
53467c478bd9Sstevel@tonic-gate 				break;
53477c478bd9Sstevel@tonic-gate 		if (i < NFS4_MAXNUM_ATTRS) {
53487c478bd9Sstevel@tonic-gate 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
53497c478bd9Sstevel@tonic-gate 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
53507c478bd9Sstevel@tonic-gate 			if (error == 0) {
53517c478bd9Sstevel@tonic-gate 				*resp |= FATTR4_ACL_MASK;
53527c478bd9Sstevel@tonic-gate 			} else if (error == ENOTSUP) {
53537c478bd9Sstevel@tonic-gate 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
53547c478bd9Sstevel@tonic-gate 				status = NFS4ERR_ATTRNOTSUPP;
53557c478bd9Sstevel@tonic-gate 				goto done;
53567c478bd9Sstevel@tonic-gate 			}
53577c478bd9Sstevel@tonic-gate 		} else {
53587c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
53597c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "do_rfs4_op_setattr: "
53607c478bd9Sstevel@tonic-gate 			    "unable to find ACL in fattr4"));
53617c478bd9Sstevel@tonic-gate 			error = EINVAL;
53627c478bd9Sstevel@tonic-gate 		}
53637c478bd9Sstevel@tonic-gate 	}
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 	if (error) {
536662b9fcbeSjwahlig 		/* check if a monitor detected a delegation conflict */
536762b9fcbeSjwahlig 		if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
536862b9fcbeSjwahlig 			status = NFS4ERR_DELAY;
536962b9fcbeSjwahlig 		else
53707c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
53717c478bd9Sstevel@tonic-gate 
53727c478bd9Sstevel@tonic-gate 		/*
53737c478bd9Sstevel@tonic-gate 		 * Set the response bitmap when setattr failed.
53747c478bd9Sstevel@tonic-gate 		 * If VOP_SETATTR partially succeeded, test by doing a
53757c478bd9Sstevel@tonic-gate 		 * VOP_GETATTR on the object and comparing the data
53767c478bd9Sstevel@tonic-gate 		 * to the setattr arguments.
53777c478bd9Sstevel@tonic-gate 		 */
53787c478bd9Sstevel@tonic-gate 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
53797c478bd9Sstevel@tonic-gate 	} else {
53807c478bd9Sstevel@tonic-gate 		/*
53817c478bd9Sstevel@tonic-gate 		 * Force modified metadata out to stable storage.
53827c478bd9Sstevel@tonic-gate 		 */
5383da6c28aaSamw 		(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
53847c478bd9Sstevel@tonic-gate 		/*
53857c478bd9Sstevel@tonic-gate 		 * Set response bitmap
53867c478bd9Sstevel@tonic-gate 		 */
53879720e166Sjasmith 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
53887c478bd9Sstevel@tonic-gate 	}
53897c478bd9Sstevel@tonic-gate 
53907c478bd9Sstevel@tonic-gate /* Return early and already have a NFSv4 error */
53917c478bd9Sstevel@tonic-gate done:
53929720e166Sjasmith 	/*
53939720e166Sjasmith 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
53949720e166Sjasmith 	 * conversion sets both readable and writeable NFS4 attrs
53959720e166Sjasmith 	 * for AT_MTIME and AT_ATIME.  The line below masks out
53969720e166Sjasmith 	 * unrequested attrs from the setattr result bitmap.  This
53979720e166Sjasmith 	 * is placed after the done: label to catch the ATTRNOTSUP
53989720e166Sjasmith 	 * case.
53999720e166Sjasmith 	 */
54009720e166Sjasmith 	*resp &= fattrp->attrmask;
54019720e166Sjasmith 
54027c478bd9Sstevel@tonic-gate 	if (in_crit)
54037c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
54067c478bd9Sstevel@tonic-gate 
54077c478bd9Sstevel@tonic-gate 	return (status);
54087c478bd9Sstevel@tonic-gate }
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate /* ARGSUSED */
54117c478bd9Sstevel@tonic-gate static void
54127c478bd9Sstevel@tonic-gate rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
54137c478bd9Sstevel@tonic-gate     struct compound_state *cs)
54147c478bd9Sstevel@tonic-gate {
54157c478bd9Sstevel@tonic-gate 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
54167c478bd9Sstevel@tonic-gate 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
541745916cd2Sjpk 	bslabel_t *clabel;
54187c478bd9Sstevel@tonic-gate 
5419f3b585ceSsamf 	DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5420f3b585ceSsamf 	    SETATTR4args *, args);
5421f3b585ceSsamf 
54227c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
54237c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5424f3b585ceSsamf 		goto out;
54257c478bd9Sstevel@tonic-gate 	}
54267c478bd9Sstevel@tonic-gate 
54277c478bd9Sstevel@tonic-gate 	/*
54287c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
54297c478bd9Sstevel@tonic-gate 	 * do not allow to setattr on this vnode.
54307c478bd9Sstevel@tonic-gate 	 */
54317c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(cs->vp)) {
54327c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5433f3b585ceSsamf 		goto out;
54347c478bd9Sstevel@tonic-gate 	}
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	resp->attrsset = 0;
54377c478bd9Sstevel@tonic-gate 
54385cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
54397c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5440f3b585ceSsamf 		goto out;
54417c478bd9Sstevel@tonic-gate 	}
54427c478bd9Sstevel@tonic-gate 
544345916cd2Sjpk 	/* check label before setting attributes */
544445916cd2Sjpk 	if (is_system_labeled()) {
544545916cd2Sjpk 		ASSERT(req->rq_label != NULL);
544645916cd2Sjpk 		clabel = req->rq_label;
544745916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
544845916cd2Sjpk 		    "got client label from request(1)",
544945916cd2Sjpk 		    struct svc_req *, req);
545045916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
545103986916Sjarrett 			if (!do_rfs_label_check(clabel, cs->vp,
5452bd6f1640SJarrett Lu 			    EQUALITY_CHECK, cs->exi)) {
545345916cd2Sjpk 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
5454f3b585ceSsamf 				goto out;
545545916cd2Sjpk 			}
545645916cd2Sjpk 		}
545745916cd2Sjpk 	}
545845916cd2Sjpk 
54597c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status =
54607c478bd9Sstevel@tonic-gate 	    do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
54617c478bd9Sstevel@tonic-gate 	    &args->stateid);
5462f3b585ceSsamf 
5463f3b585ceSsamf out:
5464f3b585ceSsamf 	DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5465f3b585ceSsamf 	    SETATTR4res *, resp);
54667c478bd9Sstevel@tonic-gate }
54677c478bd9Sstevel@tonic-gate 
54687c478bd9Sstevel@tonic-gate /* ARGSUSED */
54697c478bd9Sstevel@tonic-gate static void
54707c478bd9Sstevel@tonic-gate rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
54717c478bd9Sstevel@tonic-gate     struct compound_state *cs)
54727c478bd9Sstevel@tonic-gate {
54737c478bd9Sstevel@tonic-gate 	/*
54747c478bd9Sstevel@tonic-gate 	 * verify and nverify are exactly the same, except that nverify
54757c478bd9Sstevel@tonic-gate 	 * succeeds when some argument changed, and verify succeeds when
54767c478bd9Sstevel@tonic-gate 	 * when none changed.
54777c478bd9Sstevel@tonic-gate 	 */
54787c478bd9Sstevel@tonic-gate 
54797c478bd9Sstevel@tonic-gate 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
54807c478bd9Sstevel@tonic-gate 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate 	int error;
54837c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
54847c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
54857c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
54867c478bd9Sstevel@tonic-gate 
5487f3b585ceSsamf 	DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5488f3b585ceSsamf 	    VERIFY4args *, args);
5489f3b585ceSsamf 
54907c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
54917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5492f3b585ceSsamf 		goto out;
54937c478bd9Sstevel@tonic-gate 	}
54947c478bd9Sstevel@tonic-gate 
54957c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
54962f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
54977c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
54987c478bd9Sstevel@tonic-gate 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
54997c478bd9Sstevel@tonic-gate 	    &sarg, &ntov, NFS4ATTR_VERIT);
55007c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
55017c478bd9Sstevel@tonic-gate 		/*
55027c478bd9Sstevel@tonic-gate 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
55037c478bd9Sstevel@tonic-gate 		 * so could return -1 for "no match".
55047c478bd9Sstevel@tonic-gate 		 */
55057c478bd9Sstevel@tonic-gate 		if (resp->status == -1)
55067c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_NOT_SAME;
55077c478bd9Sstevel@tonic-gate 		goto done;
55087c478bd9Sstevel@tonic-gate 	}
55097c478bd9Sstevel@tonic-gate 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
55107c478bd9Sstevel@tonic-gate 	switch (error) {
55117c478bd9Sstevel@tonic-gate 	case 0:
55127c478bd9Sstevel@tonic-gate 		resp->status = NFS4_OK;
55137c478bd9Sstevel@tonic-gate 		break;
55147c478bd9Sstevel@tonic-gate 	case -1:
55157c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_NOT_SAME;
55167c478bd9Sstevel@tonic-gate 		break;
55177c478bd9Sstevel@tonic-gate 	default:
55187c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
55197c478bd9Sstevel@tonic-gate 		break;
55207c478bd9Sstevel@tonic-gate 	}
55217c478bd9Sstevel@tonic-gate done:
55227c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
55237c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
5524f3b585ceSsamf out:
5525f3b585ceSsamf 	DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5526f3b585ceSsamf 	    VERIFY4res *, resp);
55277c478bd9Sstevel@tonic-gate }
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate /* ARGSUSED */
55307c478bd9Sstevel@tonic-gate static void
55317c478bd9Sstevel@tonic-gate rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
55327c478bd9Sstevel@tonic-gate     struct compound_state *cs)
55337c478bd9Sstevel@tonic-gate {
55347c478bd9Sstevel@tonic-gate 	/*
55357c478bd9Sstevel@tonic-gate 	 * verify and nverify are exactly the same, except that nverify
55367c478bd9Sstevel@tonic-gate 	 * succeeds when some argument changed, and verify succeeds when
55377c478bd9Sstevel@tonic-gate 	 * when none changed.
55387c478bd9Sstevel@tonic-gate 	 */
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
55417c478bd9Sstevel@tonic-gate 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	int error;
55447c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
55457c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
55467c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
55477c478bd9Sstevel@tonic-gate 
5548f3b585ceSsamf 	DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5549f3b585ceSsamf 	    NVERIFY4args *, args);
5550f3b585ceSsamf 
55517c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
55527c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5553f3b585ceSsamf 		DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5554f3b585ceSsamf 		    NVERIFY4res *, resp);
55557c478bd9Sstevel@tonic-gate 		return;
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
55582f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
55597c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_init(&ntov);
55607c478bd9Sstevel@tonic-gate 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
55617c478bd9Sstevel@tonic-gate 	    &sarg, &ntov, NFS4ATTR_VERIT);
55627c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
55637c478bd9Sstevel@tonic-gate 		/*
55647c478bd9Sstevel@tonic-gate 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
55657c478bd9Sstevel@tonic-gate 		 * so could return -1 for "no match".
55667c478bd9Sstevel@tonic-gate 		 */
55677c478bd9Sstevel@tonic-gate 		if (resp->status == -1)
55687c478bd9Sstevel@tonic-gate 			resp->status = NFS4_OK;
55697c478bd9Sstevel@tonic-gate 		goto done;
55707c478bd9Sstevel@tonic-gate 	}
55717c478bd9Sstevel@tonic-gate 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
55727c478bd9Sstevel@tonic-gate 	switch (error) {
55737c478bd9Sstevel@tonic-gate 	case 0:
55747c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SAME;
55757c478bd9Sstevel@tonic-gate 		break;
55767c478bd9Sstevel@tonic-gate 	case -1:
55777c478bd9Sstevel@tonic-gate 		resp->status = NFS4_OK;
55787c478bd9Sstevel@tonic-gate 		break;
55797c478bd9Sstevel@tonic-gate 	default:
55807c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
55817c478bd9Sstevel@tonic-gate 		break;
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate done:
55847c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
55857c478bd9Sstevel@tonic-gate 	nfs4_ntov_table_free(&ntov, &sarg);
5586f3b585ceSsamf 
5587f3b585ceSsamf 	DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5588f3b585ceSsamf 	    NVERIFY4res *, resp);
55897c478bd9Sstevel@tonic-gate }
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate /*
55927c478bd9Sstevel@tonic-gate  * XXX - This should live in an NFS header file.
55937c478bd9Sstevel@tonic-gate  */
55947c478bd9Sstevel@tonic-gate #define	MAX_IOVECS	12
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate /* ARGSUSED */
55977c478bd9Sstevel@tonic-gate static void
55987c478bd9Sstevel@tonic-gate rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
55997c478bd9Sstevel@tonic-gate     struct compound_state *cs)
56007c478bd9Sstevel@tonic-gate {
56017c478bd9Sstevel@tonic-gate 	WRITE4args *args = &argop->nfs_argop4_u.opwrite;
56027c478bd9Sstevel@tonic-gate 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
56037c478bd9Sstevel@tonic-gate 	int error;
56047c478bd9Sstevel@tonic-gate 	vnode_t *vp;
56057c478bd9Sstevel@tonic-gate 	struct vattr bva;
56067c478bd9Sstevel@tonic-gate 	u_offset_t rlimit;
56077c478bd9Sstevel@tonic-gate 	struct uio uio;
56087c478bd9Sstevel@tonic-gate 	struct iovec iov[MAX_IOVECS];
56097c478bd9Sstevel@tonic-gate 	struct iovec *iovp;
56107c478bd9Sstevel@tonic-gate 	int iovcnt;
56117c478bd9Sstevel@tonic-gate 	int ioflag;
56127c478bd9Sstevel@tonic-gate 	cred_t *savecred, *cr;
56137c478bd9Sstevel@tonic-gate 	bool_t *deleg = &cs->deleg;
56147c478bd9Sstevel@tonic-gate 	nfsstat4 stat;
56157c478bd9Sstevel@tonic-gate 	int in_crit = 0;
5616da6c28aaSamw 	caller_context_t ct;
56177c478bd9Sstevel@tonic-gate 
5618f3b585ceSsamf 	DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5619f3b585ceSsamf 	    WRITE4args *, args);
5620f3b585ceSsamf 
56217c478bd9Sstevel@tonic-gate 	vp = cs->vp;
56227c478bd9Sstevel@tonic-gate 	if (vp == NULL) {
56237c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5624f3b585ceSsamf 		goto out;
56257c478bd9Sstevel@tonic-gate 	}
56267c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED) {
56277c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5628f3b585ceSsamf 		goto out;
56297c478bd9Sstevel@tonic-gate 	}
56307c478bd9Sstevel@tonic-gate 
56317c478bd9Sstevel@tonic-gate 	cr = cs->cr;
56327c478bd9Sstevel@tonic-gate 
5633da6c28aaSamw 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5634da6c28aaSamw 	    deleg, TRUE, &ct)) != NFS4_OK) {
5635da6c28aaSamw 		*cs->statusp = resp->status = stat;
5636da6c28aaSamw 		goto out;
5637da6c28aaSamw 	}
5638da6c28aaSamw 
56397c478bd9Sstevel@tonic-gate 	/*
56407c478bd9Sstevel@tonic-gate 	 * We have to enter the critical region before calling VOP_RWLOCK
56417c478bd9Sstevel@tonic-gate 	 * to avoid a deadlock with ufs.
56427c478bd9Sstevel@tonic-gate 	 */
56437c478bd9Sstevel@tonic-gate 	if (nbl_need_check(vp)) {
56447c478bd9Sstevel@tonic-gate 		nbl_start_crit(vp, RW_READER);
56457c478bd9Sstevel@tonic-gate 		in_crit = 1;
56467c478bd9Sstevel@tonic-gate 		if (nbl_conflict(vp, NBL_WRITE,
5647da6c28aaSamw 		    args->offset, args->data_len, 0, &ct)) {
56487c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
56497c478bd9Sstevel@tonic-gate 			goto out;
56507c478bd9Sstevel@tonic-gate 		}
56517c478bd9Sstevel@tonic-gate 	}
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_MODE | AT_UID;
5654da6c28aaSamw 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 	/*
56577c478bd9Sstevel@tonic-gate 	 * If we can't get the attributes, then we can't do the
56587c478bd9Sstevel@tonic-gate 	 * right access checking.  So, we'll fail the request.
56597c478bd9Sstevel@tonic-gate 	 */
56607c478bd9Sstevel@tonic-gate 	if (error) {
56617c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
56627c478bd9Sstevel@tonic-gate 		goto out;
56637c478bd9Sstevel@tonic-gate 	}
56647c478bd9Sstevel@tonic-gate 
56655cb0d679SMarcel Telka 	if (rdonly4(req, cs)) {
56667c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ROFS;
56677c478bd9Sstevel@tonic-gate 		goto out;
56687c478bd9Sstevel@tonic-gate 	}
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
56717c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
56727c478bd9Sstevel@tonic-gate 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
56737c478bd9Sstevel@tonic-gate 		goto out;
56747c478bd9Sstevel@tonic-gate 	}
56757c478bd9Sstevel@tonic-gate 
56767c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != bva.va_uid &&
5677da6c28aaSamw 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
56787c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
56797c478bd9Sstevel@tonic-gate 		goto out;
56807c478bd9Sstevel@tonic-gate 	}
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, bva.va_mode)) {
56837c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
56847c478bd9Sstevel@tonic-gate 		goto out;
56857c478bd9Sstevel@tonic-gate 	}
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate 	if (args->data_len == 0) {
56887c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
56897c478bd9Sstevel@tonic-gate 		resp->count = 0;
56907c478bd9Sstevel@tonic-gate 		resp->committed = args->stable;
56917c478bd9Sstevel@tonic-gate 		resp->writeverf = Write4verf;
56927c478bd9Sstevel@tonic-gate 		goto out;
56937c478bd9Sstevel@tonic-gate 	}
56947c478bd9Sstevel@tonic-gate 
56957c478bd9Sstevel@tonic-gate 	if (args->mblk != NULL) {
56967c478bd9Sstevel@tonic-gate 		mblk_t *m;
56977c478bd9Sstevel@tonic-gate 		uint_t bytes, round_len;
56987c478bd9Sstevel@tonic-gate 
56997c478bd9Sstevel@tonic-gate 		iovcnt = 0;
57007c478bd9Sstevel@tonic-gate 		bytes = 0;
57017c478bd9Sstevel@tonic-gate 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
57027c478bd9Sstevel@tonic-gate 		for (m = args->mblk;
57037c478bd9Sstevel@tonic-gate 		    m != NULL && bytes < round_len;
57047c478bd9Sstevel@tonic-gate 		    m = m->b_cont) {
57057c478bd9Sstevel@tonic-gate 			iovcnt++;
57067c478bd9Sstevel@tonic-gate 			bytes += MBLKL(m);
57077c478bd9Sstevel@tonic-gate 		}
57087c478bd9Sstevel@tonic-gate #ifdef DEBUG
57097c478bd9Sstevel@tonic-gate 		/* should have ended on an mblk boundary */
57107c478bd9Sstevel@tonic-gate 		if (bytes != round_len) {
57117c478bd9Sstevel@tonic-gate 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
57127c478bd9Sstevel@tonic-gate 			    bytes, round_len, args->data_len);
57137c478bd9Sstevel@tonic-gate 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
57147c478bd9Sstevel@tonic-gate 			    (void *)args->mblk, (void *)m);
57157c478bd9Sstevel@tonic-gate 			ASSERT(bytes == round_len);
57167c478bd9Sstevel@tonic-gate 		}
57177c478bd9Sstevel@tonic-gate #endif
57187c478bd9Sstevel@tonic-gate 		if (iovcnt <= MAX_IOVECS) {
57197c478bd9Sstevel@tonic-gate 			iovp = iov;
57207c478bd9Sstevel@tonic-gate 		} else {
57217c478bd9Sstevel@tonic-gate 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
57227c478bd9Sstevel@tonic-gate 		}
57237c478bd9Sstevel@tonic-gate 		mblk_to_iov(args->mblk, iovcnt, iovp);
57240a701b1eSRobert Gordon 	} else if (args->rlist != NULL) {
57250a701b1eSRobert Gordon 		iovcnt = 1;
57260a701b1eSRobert Gordon 		iovp = iov;
57270a701b1eSRobert Gordon 		iovp->iov_base = (char *)((args->rlist)->u.c_daddr3);
57280a701b1eSRobert Gordon 		iovp->iov_len = args->data_len;
57297c478bd9Sstevel@tonic-gate 	} else {
57307c478bd9Sstevel@tonic-gate 		iovcnt = 1;
57317c478bd9Sstevel@tonic-gate 		iovp = iov;
57327c478bd9Sstevel@tonic-gate 		iovp->iov_base = args->data_val;
57337c478bd9Sstevel@tonic-gate 		iovp->iov_len = args->data_len;
57347c478bd9Sstevel@tonic-gate 	}
57357c478bd9Sstevel@tonic-gate 
57367c478bd9Sstevel@tonic-gate 	uio.uio_iov = iovp;
57377c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = iovcnt;
57387c478bd9Sstevel@tonic-gate 
57397c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
57407c478bd9Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_DEFAULT;
57417c478bd9Sstevel@tonic-gate 	uio.uio_loffset = args->offset;
57427c478bd9Sstevel@tonic-gate 	uio.uio_resid = args->data_len;
57437c478bd9Sstevel@tonic-gate 	uio.uio_llimit = curproc->p_fsz_ctl;
57447c478bd9Sstevel@tonic-gate 	rlimit = uio.uio_llimit - args->offset;
57457c478bd9Sstevel@tonic-gate 	if (rlimit < (u_offset_t)uio.uio_resid)
57467c478bd9Sstevel@tonic-gate 		uio.uio_resid = (int)rlimit;
57477c478bd9Sstevel@tonic-gate 
57487c478bd9Sstevel@tonic-gate 	if (args->stable == UNSTABLE4)
57497c478bd9Sstevel@tonic-gate 		ioflag = 0;
57507c478bd9Sstevel@tonic-gate 	else if (args->stable == FILE_SYNC4)
57517c478bd9Sstevel@tonic-gate 		ioflag = FSYNC;
57527c478bd9Sstevel@tonic-gate 	else if (args->stable == DATA_SYNC4)
57537c478bd9Sstevel@tonic-gate 		ioflag = FDSYNC;
57547c478bd9Sstevel@tonic-gate 	else {
57557c478bd9Sstevel@tonic-gate 		if (iovp != iov)
57567c478bd9Sstevel@tonic-gate 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
57577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
57587c478bd9Sstevel@tonic-gate 		goto out;
57597c478bd9Sstevel@tonic-gate 	}
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate 	/*
57627c478bd9Sstevel@tonic-gate 	 * We're changing creds because VM may fault and we need
57637c478bd9Sstevel@tonic-gate 	 * the cred of the current thread to be used if quota
57647c478bd9Sstevel@tonic-gate 	 * checking is enabled.
57657c478bd9Sstevel@tonic-gate 	 */
57667c478bd9Sstevel@tonic-gate 	savecred = curthread->t_cred;
57677c478bd9Sstevel@tonic-gate 	curthread->t_cred = cr;
5768da6c28aaSamw 	error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
57697c478bd9Sstevel@tonic-gate 	curthread->t_cred = savecred;
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	if (iovp != iov)
57727c478bd9Sstevel@tonic-gate 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
57737c478bd9Sstevel@tonic-gate 
57747c478bd9Sstevel@tonic-gate 	if (error) {
57757c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
57767c478bd9Sstevel@tonic-gate 		goto out;
57777c478bd9Sstevel@tonic-gate 	}
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
57807c478bd9Sstevel@tonic-gate 	resp->count = args->data_len - uio.uio_resid;
57817c478bd9Sstevel@tonic-gate 
57827c478bd9Sstevel@tonic-gate 	if (ioflag == 0)
57837c478bd9Sstevel@tonic-gate 		resp->committed = UNSTABLE4;
57847c478bd9Sstevel@tonic-gate 	else
57857c478bd9Sstevel@tonic-gate 		resp->committed = FILE_SYNC4;
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 	resp->writeverf = Write4verf;
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate out:
57907c478bd9Sstevel@tonic-gate 	if (in_crit)
57917c478bd9Sstevel@tonic-gate 		nbl_end_crit(vp);
5792f3b585ceSsamf 
5793f3b585ceSsamf 	DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5794f3b585ceSsamf 	    WRITE4res *, resp);
57957c478bd9Sstevel@tonic-gate }
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 
57987c478bd9Sstevel@tonic-gate /* XXX put in a header file */
57997c478bd9Sstevel@tonic-gate extern int	sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
58007c478bd9Sstevel@tonic-gate 
58017c478bd9Sstevel@tonic-gate void
58027c478bd9Sstevel@tonic-gate rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
58032e9d26a4Srmesta     struct svc_req *req, cred_t *cr, int *rv)
58047c478bd9Sstevel@tonic-gate {
58057c478bd9Sstevel@tonic-gate 	uint_t i;
58067c478bd9Sstevel@tonic-gate 	struct compound_state cs;
58077c478bd9Sstevel@tonic-gate 
58082e9d26a4Srmesta 	if (rv != NULL)
58092e9d26a4Srmesta 		*rv = 0;
58107c478bd9Sstevel@tonic-gate 	rfs4_init_compound_state(&cs);
58117c478bd9Sstevel@tonic-gate 	/*
58127c478bd9Sstevel@tonic-gate 	 * Form a reply tag by copying over the reqeuest tag.
58137c478bd9Sstevel@tonic-gate 	 */
58147c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_val =
58157c478bd9Sstevel@tonic-gate 	    kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
58167c478bd9Sstevel@tonic-gate 	resp->tag.utf8string_len = args->tag.utf8string_len;
58177c478bd9Sstevel@tonic-gate 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
58187c478bd9Sstevel@tonic-gate 	    resp->tag.utf8string_len);
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	cs.statusp = &resp->status;
5821f3b585ceSsamf 	cs.req = req;
5822945b8d40SDaniil Lunev 	resp->array = NULL;
5823945b8d40SDaniil Lunev 	resp->array_len = 0;
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate 	/*
58267c478bd9Sstevel@tonic-gate 	 * XXX for now, minorversion should be zero
58277c478bd9Sstevel@tonic-gate 	 */
58287c478bd9Sstevel@tonic-gate 	if (args->minorversion != NFS4_MINORVERSION) {
5829f3b585ceSsamf 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5830f3b585ceSsamf 		    &cs, COMPOUND4args *, args);
58317c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5832f3b585ceSsamf 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5833f3b585ceSsamf 		    &cs, COMPOUND4res *, resp);
58347c478bd9Sstevel@tonic-gate 		return;
58357c478bd9Sstevel@tonic-gate 	}
58367c478bd9Sstevel@tonic-gate 
5837945b8d40SDaniil Lunev 	if (args->array_len == 0) {
5838945b8d40SDaniil Lunev 		resp->status = NFS4_OK;
5839945b8d40SDaniil Lunev 		return;
5840945b8d40SDaniil Lunev 	}
5841945b8d40SDaniil Lunev 
58427c478bd9Sstevel@tonic-gate 	ASSERT(exi == NULL);
58437c478bd9Sstevel@tonic-gate 	ASSERT(cr == NULL);
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	cr = crget();
58467c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5849f3b585ceSsamf 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5850f3b585ceSsamf 		    &cs, COMPOUND4args *, args);
58517c478bd9Sstevel@tonic-gate 		crfree(cr);
5852f3b585ceSsamf 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5853f3b585ceSsamf 		    &cs, COMPOUND4res *, resp);
58542e9d26a4Srmesta 		svcerr_badcred(req->rq_xprt);
58552e9d26a4Srmesta 		if (rv != NULL)
58562e9d26a4Srmesta 			*rv = 1;
58577c478bd9Sstevel@tonic-gate 		return;
58587c478bd9Sstevel@tonic-gate 	}
58592e9d26a4Srmesta 	resp->array_len = args->array_len;
58602e9d26a4Srmesta 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
58612e9d26a4Srmesta 	    KM_SLEEP);
58627c478bd9Sstevel@tonic-gate 
58637c478bd9Sstevel@tonic-gate 	cs.basecr = cr;
58647c478bd9Sstevel@tonic-gate 
5865f3b585ceSsamf 	DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5866f3b585ceSsamf 	    COMPOUND4args *, args);
58677c478bd9Sstevel@tonic-gate 
58687c478bd9Sstevel@tonic-gate 	/*
58697c478bd9Sstevel@tonic-gate 	 * If this is the first compound we've seen, we need to start all
58707c478bd9Sstevel@tonic-gate 	 * new instances' grace periods.
58717c478bd9Sstevel@tonic-gate 	 */
58727c478bd9Sstevel@tonic-gate 	if (rfs4_seen_first_compound == 0) {
58737c478bd9Sstevel@tonic-gate 		rfs4_grace_start_new();
58747c478bd9Sstevel@tonic-gate 		/*
58757c478bd9Sstevel@tonic-gate 		 * This must be set after rfs4_grace_start_new(), otherwise
58767c478bd9Sstevel@tonic-gate 		 * another thread could proceed past here before the former
58777c478bd9Sstevel@tonic-gate 		 * is finished.
58787c478bd9Sstevel@tonic-gate 		 */
58797c478bd9Sstevel@tonic-gate 		rfs4_seen_first_compound = 1;
58807c478bd9Sstevel@tonic-gate 	}
58817c478bd9Sstevel@tonic-gate 
58827c478bd9Sstevel@tonic-gate 	for (i = 0; i < args->array_len && cs.cont; i++) {
58837c478bd9Sstevel@tonic-gate 		nfs_argop4 *argop;
58847c478bd9Sstevel@tonic-gate 		nfs_resop4 *resop;
58857c478bd9Sstevel@tonic-gate 		uint_t op;
58867c478bd9Sstevel@tonic-gate 
58877c478bd9Sstevel@tonic-gate 		argop = &args->array[i];
58887c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
58897c478bd9Sstevel@tonic-gate 		resop->resop = argop->argop;
58907c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
58917c478bd9Sstevel@tonic-gate 
58927c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt) {
58937c478bd9Sstevel@tonic-gate 			/*
58947c478bd9Sstevel@tonic-gate 			 * Count the individual ops here; NULL and COMPOUND
58957c478bd9Sstevel@tonic-gate 			 * are counted in common_dispatch()
58967c478bd9Sstevel@tonic-gate 			 */
58977c478bd9Sstevel@tonic-gate 			rfsproccnt_v4_ptr[op].value.ui64++;
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug > 1,
59007c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "Executing %s", rfs4_op_string[op]));
59017c478bd9Sstevel@tonic-gate 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
59021b300de9Sjwahlig 			NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
59037c478bd9Sstevel@tonic-gate 			    rfs4_op_string[op], *cs.statusp));
59047c478bd9Sstevel@tonic-gate 			if (*cs.statusp != NFS4_OK)
59057c478bd9Sstevel@tonic-gate 				cs.cont = FALSE;
59067c478bd9Sstevel@tonic-gate 		} else {
59077c478bd9Sstevel@tonic-gate 			/*
59087c478bd9Sstevel@tonic-gate 			 * This is effectively dead code since XDR code
59097c478bd9Sstevel@tonic-gate 			 * will have already returned BADXDR if op doesn't
59107c478bd9Sstevel@tonic-gate 			 * decode to legal value.  This only done for a
59117c478bd9Sstevel@tonic-gate 			 * day when XDR code doesn't verify v4 opcodes.
59127c478bd9Sstevel@tonic-gate 			 */
59137c478bd9Sstevel@tonic-gate 			op = OP_ILLEGAL;
59147c478bd9Sstevel@tonic-gate 			rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
59157c478bd9Sstevel@tonic-gate 
59167c478bd9Sstevel@tonic-gate 			rfs4_op_illegal(argop, resop, req, &cs);
59177c478bd9Sstevel@tonic-gate 			cs.cont = FALSE;
59187c478bd9Sstevel@tonic-gate 		}
59197c478bd9Sstevel@tonic-gate 
59207c478bd9Sstevel@tonic-gate 		/*
59217c478bd9Sstevel@tonic-gate 		 * If not at last op, and if we are to stop, then
59227c478bd9Sstevel@tonic-gate 		 * compact the results array.
59237c478bd9Sstevel@tonic-gate 		 */
59247c478bd9Sstevel@tonic-gate 		if ((i + 1) < args->array_len && !cs.cont) {
59257c478bd9Sstevel@tonic-gate 			nfs_resop4 *new_res = kmem_alloc(
59267c478bd9Sstevel@tonic-gate 			    (i+1) * sizeof (nfs_resop4), KM_SLEEP);
59277c478bd9Sstevel@tonic-gate 			bcopy(resp->array,
59287c478bd9Sstevel@tonic-gate 			    new_res, (i+1) * sizeof (nfs_resop4));
59297c478bd9Sstevel@tonic-gate 			kmem_free(resp->array,
59307c478bd9Sstevel@tonic-gate 			    args->array_len * sizeof (nfs_resop4));
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate 			resp->array_len =  i + 1;
59337c478bd9Sstevel@tonic-gate 			resp->array = new_res;
59347c478bd9Sstevel@tonic-gate 		}
59357c478bd9Sstevel@tonic-gate 	}
59367c478bd9Sstevel@tonic-gate 
59377c478bd9Sstevel@tonic-gate 
5938f3b585ceSsamf 	DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5939f3b585ceSsamf 	    COMPOUND4res *, resp);
5940f3b585ceSsamf 
5941aafcd32bSMarcel Telka 	if (cs.exi != NULL)
5942fbd2e8e1SArne Jansen 		exi_rele(cs.exi);
5943aafcd32bSMarcel Telka 	if (cs.saved_exi != NULL)
5944fbd2e8e1SArne Jansen 		exi_rele(cs.saved_exi);
5945aafcd32bSMarcel Telka 	if (cs.vp != NULL)
59467c478bd9Sstevel@tonic-gate 		VN_RELE(cs.vp);
5947aafcd32bSMarcel Telka 	if (cs.saved_vp != NULL)
59487c478bd9Sstevel@tonic-gate 		VN_RELE(cs.saved_vp);
5949aafcd32bSMarcel Telka 	if (cs.saved_fh.nfs_fh4_val != NULL)
59507c478bd9Sstevel@tonic-gate 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
59517c478bd9Sstevel@tonic-gate 
59527c478bd9Sstevel@tonic-gate 	if (cs.basecr)
59537c478bd9Sstevel@tonic-gate 		crfree(cs.basecr);
59547c478bd9Sstevel@tonic-gate 	if (cs.cr)
59557c478bd9Sstevel@tonic-gate 		crfree(cs.cr);
595645916cd2Sjpk 	/*
595745916cd2Sjpk 	 * done with this compound request, free the label
595845916cd2Sjpk 	 */
595945916cd2Sjpk 
596045916cd2Sjpk 	if (req->rq_label != NULL) {
596145916cd2Sjpk 		kmem_free(req->rq_label, sizeof (bslabel_t));
596245916cd2Sjpk 		req->rq_label = NULL;
596345916cd2Sjpk 	}
59647c478bd9Sstevel@tonic-gate }
59657c478bd9Sstevel@tonic-gate 
59667c478bd9Sstevel@tonic-gate /*
59677c478bd9Sstevel@tonic-gate  * XXX because of what appears to be duplicate calls to rfs4_compound_free
59687c478bd9Sstevel@tonic-gate  * XXX zero out the tag and array values. Need to investigate why the
59697c478bd9Sstevel@tonic-gate  * XXX calls occur, but at least prevent the panic for now.
59707c478bd9Sstevel@tonic-gate  */
59717c478bd9Sstevel@tonic-gate void
59727c478bd9Sstevel@tonic-gate rfs4_compound_free(COMPOUND4res *resp)
59737c478bd9Sstevel@tonic-gate {
59747c478bd9Sstevel@tonic-gate 	uint_t i;
59757c478bd9Sstevel@tonic-gate 
59767c478bd9Sstevel@tonic-gate 	if (resp->tag.utf8string_val) {
59777c478bd9Sstevel@tonic-gate 		UTF8STRING_FREE(resp->tag)
59787c478bd9Sstevel@tonic-gate 	}
59797c478bd9Sstevel@tonic-gate 
59807c478bd9Sstevel@tonic-gate 	for (i = 0; i < resp->array_len; i++) {
59817c478bd9Sstevel@tonic-gate 		nfs_resop4 *resop;
59827c478bd9Sstevel@tonic-gate 		uint_t op;
59837c478bd9Sstevel@tonic-gate 
59847c478bd9Sstevel@tonic-gate 		resop = &resp->array[i];
59857c478bd9Sstevel@tonic-gate 		op = (uint_t)resop->resop;
59867c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt) {
59877c478bd9Sstevel@tonic-gate 			(*rfsv4disptab[op].dis_resfree)(resop);
59887c478bd9Sstevel@tonic-gate 		}
59897c478bd9Sstevel@tonic-gate 	}
59907c478bd9Sstevel@tonic-gate 	if (resp->array != NULL) {
59917c478bd9Sstevel@tonic-gate 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
59927c478bd9Sstevel@tonic-gate 	}
59937c478bd9Sstevel@tonic-gate }
59947c478bd9Sstevel@tonic-gate 
59957c478bd9Sstevel@tonic-gate /*
59967c478bd9Sstevel@tonic-gate  * Process the value of the compound request rpc flags, as a bit-AND
59977c478bd9Sstevel@tonic-gate  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
59987c478bd9Sstevel@tonic-gate  */
59997c478bd9Sstevel@tonic-gate void
60007c478bd9Sstevel@tonic-gate rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
60017c478bd9Sstevel@tonic-gate {
60027c478bd9Sstevel@tonic-gate 	int i;
60037c478bd9Sstevel@tonic-gate 	int flag = RPC_ALL;
60047c478bd9Sstevel@tonic-gate 
60057c478bd9Sstevel@tonic-gate 	for (i = 0; flag && i < args->array_len; i++) {
60067c478bd9Sstevel@tonic-gate 		uint_t op;
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate 		op = (uint_t)args->array[i].argop;
60097c478bd9Sstevel@tonic-gate 
60107c478bd9Sstevel@tonic-gate 		if (op < rfsv4disp_cnt)
60117c478bd9Sstevel@tonic-gate 			flag &= rfsv4disptab[op].dis_flags;
60127c478bd9Sstevel@tonic-gate 		else
60137c478bd9Sstevel@tonic-gate 			flag = 0;
60147c478bd9Sstevel@tonic-gate 	}
60157c478bd9Sstevel@tonic-gate 	*flagp = flag;
60167c478bd9Sstevel@tonic-gate }
60177c478bd9Sstevel@tonic-gate 
60187c478bd9Sstevel@tonic-gate nfsstat4
60197c478bd9Sstevel@tonic-gate rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
60207c478bd9Sstevel@tonic-gate {
60217c478bd9Sstevel@tonic-gate 	nfsstat4 e;
60227c478bd9Sstevel@tonic-gate 
6023d216dff5SRobert Mastors 	rfs4_dbe_lock(cp->rc_dbe);
60247c478bd9Sstevel@tonic-gate 
6025d216dff5SRobert Mastors 	if (cp->rc_sysidt != LM_NOSYSID) {
6026d216dff5SRobert Mastors 		*sp = cp->rc_sysidt;
60277c478bd9Sstevel@tonic-gate 		e = NFS4_OK;
60287c478bd9Sstevel@tonic-gate 
6029d216dff5SRobert Mastors 	} else if ((cp->rc_sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
6030d216dff5SRobert Mastors 		*sp = cp->rc_sysidt;
60317c478bd9Sstevel@tonic-gate 		e = NFS4_OK;
60327c478bd9Sstevel@tonic-gate 
60337c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
60347c478bd9Sstevel@tonic-gate 		    "rfs4_client_sysid: allocated 0x%x\n", *sp));
60357c478bd9Sstevel@tonic-gate 	} else
60367c478bd9Sstevel@tonic-gate 		e = NFS4ERR_DELAY;
60377c478bd9Sstevel@tonic-gate 
6038d216dff5SRobert Mastors 	rfs4_dbe_unlock(cp->rc_dbe);
60397c478bd9Sstevel@tonic-gate 	return (e);
60407c478bd9Sstevel@tonic-gate }
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate #if defined(DEBUG) && ! defined(lint)
60437c478bd9Sstevel@tonic-gate static void lock_print(char *str, int operation, struct flock64 *flk)
60447c478bd9Sstevel@tonic-gate {
60457c478bd9Sstevel@tonic-gate 	char *op, *type;
60467c478bd9Sstevel@tonic-gate 
60477c478bd9Sstevel@tonic-gate 	switch (operation) {
60487c478bd9Sstevel@tonic-gate 	case F_GETLK: op = "F_GETLK";
60497c478bd9Sstevel@tonic-gate 		break;
60507c478bd9Sstevel@tonic-gate 	case F_SETLK: op = "F_SETLK";
60517c478bd9Sstevel@tonic-gate 		break;
6052da6c28aaSamw 	case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
6053da6c28aaSamw 		break;
60547c478bd9Sstevel@tonic-gate 	default: op = "F_UNKNOWN";
60557c478bd9Sstevel@tonic-gate 		break;
60567c478bd9Sstevel@tonic-gate 	}
60577c478bd9Sstevel@tonic-gate 	switch (flk->l_type) {
60587c478bd9Sstevel@tonic-gate 	case F_UNLCK: type = "F_UNLCK";
60597c478bd9Sstevel@tonic-gate 		break;
60607c478bd9Sstevel@tonic-gate 	case F_RDLCK: type = "F_RDLCK";
60617c478bd9Sstevel@tonic-gate 		break;
60627c478bd9Sstevel@tonic-gate 	case F_WRLCK: type = "F_WRLCK";
60637c478bd9Sstevel@tonic-gate 		break;
60647c478bd9Sstevel@tonic-gate 	default: type = "F_UNKNOWN";
60657c478bd9Sstevel@tonic-gate 		break;
60667c478bd9Sstevel@tonic-gate 	}
60677c478bd9Sstevel@tonic-gate 
60687c478bd9Sstevel@tonic-gate 	ASSERT(flk->l_whence == 0);
60697c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
60701b300de9Sjwahlig 	    str, op, type, (longlong_t)flk->l_start,
60711b300de9Sjwahlig 	    flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
60727c478bd9Sstevel@tonic-gate }
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
60757c478bd9Sstevel@tonic-gate #else
60767c478bd9Sstevel@tonic-gate #define	LOCK_PRINT(d, s, t, f)
60777c478bd9Sstevel@tonic-gate #endif
60787c478bd9Sstevel@tonic-gate 
60797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
60807c478bd9Sstevel@tonic-gate static bool_t
60817c478bd9Sstevel@tonic-gate creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
60827c478bd9Sstevel@tonic-gate {
60837c478bd9Sstevel@tonic-gate 	return (TRUE);
60847c478bd9Sstevel@tonic-gate }
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate /*
60877c478bd9Sstevel@tonic-gate  * Look up the pathname using the vp in cs as the directory vnode.
60887c478bd9Sstevel@tonic-gate  * cs->vp will be the vnode for the file on success
60897c478bd9Sstevel@tonic-gate  */
60907c478bd9Sstevel@tonic-gate 
60917c478bd9Sstevel@tonic-gate static nfsstat4
60927c478bd9Sstevel@tonic-gate rfs4_lookup(component4 *component, struct svc_req *req,
60937c478bd9Sstevel@tonic-gate     struct compound_state *cs)
60947c478bd9Sstevel@tonic-gate {
60957c478bd9Sstevel@tonic-gate 	char *nm;
60967c478bd9Sstevel@tonic-gate 	uint32_t len;
60977c478bd9Sstevel@tonic-gate 	nfsstat4 status;
6098593cc11bSJan Kryl 	struct sockaddr *ca;
6099593cc11bSJan Kryl 	char *name;
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
61027c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOFILEHANDLE);
61037c478bd9Sstevel@tonic-gate 	}
61047c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VDIR) {
61057c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOTDIR);
61067c478bd9Sstevel@tonic-gate 	}
61077c478bd9Sstevel@tonic-gate 
610815721462SDaniil Lunev 	status = utf8_dir_verify(component);
610915721462SDaniil Lunev 	if (status != NFS4_OK)
611015721462SDaniil Lunev 		return (status);
61117c478bd9Sstevel@tonic-gate 
61127c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(component, &len, NULL);
61137c478bd9Sstevel@tonic-gate 	if (nm == NULL) {
61147c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
61157c478bd9Sstevel@tonic-gate 	}
61167c478bd9Sstevel@tonic-gate 
61177c478bd9Sstevel@tonic-gate 	if (len > MAXNAMELEN) {
61187c478bd9Sstevel@tonic-gate 		kmem_free(nm, len);
61197c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NAMETOOLONG);
61207c478bd9Sstevel@tonic-gate 	}
61217c478bd9Sstevel@tonic-gate 
6122593cc11bSJan Kryl 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6123593cc11bSJan Kryl 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6124593cc11bSJan Kryl 	    MAXPATHLEN + 1);
6125593cc11bSJan Kryl 
6126593cc11bSJan Kryl 	if (name == NULL) {
6127593cc11bSJan Kryl 		kmem_free(nm, len);
6128593cc11bSJan Kryl 		return (NFS4ERR_INVAL);
6129593cc11bSJan Kryl 	}
6130593cc11bSJan Kryl 
6131593cc11bSJan Kryl 	status = do_rfs4_op_lookup(name, req, cs);
6132593cc11bSJan Kryl 
6133593cc11bSJan Kryl 	if (name != nm)
6134593cc11bSJan Kryl 		kmem_free(name, MAXPATHLEN + 1);
61357c478bd9Sstevel@tonic-gate 
61367c478bd9Sstevel@tonic-gate 	kmem_free(nm, len);
61377c478bd9Sstevel@tonic-gate 
61387c478bd9Sstevel@tonic-gate 	return (status);
61397c478bd9Sstevel@tonic-gate }
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate static nfsstat4
61427c478bd9Sstevel@tonic-gate rfs4_lookupfile(component4 *component, struct svc_req *req,
6143d216dff5SRobert Mastors     struct compound_state *cs, uint32_t access, change_info4 *cinfo)
61447c478bd9Sstevel@tonic-gate {
61457c478bd9Sstevel@tonic-gate 	nfsstat4 status;
61467c478bd9Sstevel@tonic-gate 	vnode_t *dvp = cs->vp;
61477c478bd9Sstevel@tonic-gate 	vattr_t bva, ava, fva;
61487c478bd9Sstevel@tonic-gate 	int error;
61497c478bd9Sstevel@tonic-gate 
61507c478bd9Sstevel@tonic-gate 	/* Get "before" change value */
61517c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_CTIME|AT_SEQ;
6152da6c28aaSamw 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
61537c478bd9Sstevel@tonic-gate 	if (error)
61547c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
61557c478bd9Sstevel@tonic-gate 
61567c478bd9Sstevel@tonic-gate 	/* rfs4_lookup may VN_RELE directory */
61577c478bd9Sstevel@tonic-gate 	VN_HOLD(dvp);
61587c478bd9Sstevel@tonic-gate 
61597c478bd9Sstevel@tonic-gate 	status = rfs4_lookup(component, req, cs);
61607c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
61617c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
61627c478bd9Sstevel@tonic-gate 		return (status);
61637c478bd9Sstevel@tonic-gate 	}
61647c478bd9Sstevel@tonic-gate 
61657c478bd9Sstevel@tonic-gate 	/*
61667c478bd9Sstevel@tonic-gate 	 * Get "after" change value, if it fails, simply return the
61677c478bd9Sstevel@tonic-gate 	 * before value.
61687c478bd9Sstevel@tonic-gate 	 */
61697c478bd9Sstevel@tonic-gate 	ava.va_mask = AT_CTIME|AT_SEQ;
6170da6c28aaSamw 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
61717c478bd9Sstevel@tonic-gate 		ava.va_ctime = bva.va_ctime;
61727c478bd9Sstevel@tonic-gate 		ava.va_seq = 0;
61737c478bd9Sstevel@tonic-gate 	}
61747c478bd9Sstevel@tonic-gate 	VN_RELE(dvp);
61757c478bd9Sstevel@tonic-gate 
61767c478bd9Sstevel@tonic-gate 	/*
61777c478bd9Sstevel@tonic-gate 	 * Validate the file is a file
61787c478bd9Sstevel@tonic-gate 	 */
61797c478bd9Sstevel@tonic-gate 	fva.va_mask = AT_TYPE|AT_MODE;
6180da6c28aaSamw 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
61817c478bd9Sstevel@tonic-gate 	if (error)
61827c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
61837c478bd9Sstevel@tonic-gate 
61847c478bd9Sstevel@tonic-gate 	if (fva.va_type != VREG) {
61857c478bd9Sstevel@tonic-gate 		if (fva.va_type == VDIR)
61867c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ISDIR);
61877c478bd9Sstevel@tonic-gate 		if (fva.va_type == VLNK)
61887c478bd9Sstevel@tonic-gate 			return (NFS4ERR_SYMLINK);
61897c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
61907c478bd9Sstevel@tonic-gate 	}
61917c478bd9Sstevel@tonic-gate 
61927c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
61937c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
61947c478bd9Sstevel@tonic-gate 
61957c478bd9Sstevel@tonic-gate 	/*
61967c478bd9Sstevel@tonic-gate 	 * It is undefined if VOP_LOOKUP will change va_seq, so
61977c478bd9Sstevel@tonic-gate 	 * cinfo.atomic = TRUE only if we have
61987c478bd9Sstevel@tonic-gate 	 * non-zero va_seq's, and they have not changed.
61997c478bd9Sstevel@tonic-gate 	 */
62007c478bd9Sstevel@tonic-gate 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
62017c478bd9Sstevel@tonic-gate 		cinfo->atomic = TRUE;
62027c478bd9Sstevel@tonic-gate 	else
62037c478bd9Sstevel@tonic-gate 		cinfo->atomic = FALSE;
62047c478bd9Sstevel@tonic-gate 
62057c478bd9Sstevel@tonic-gate 	/* Check for mandatory locking */
62067c478bd9Sstevel@tonic-gate 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
62077c478bd9Sstevel@tonic-gate 	return (check_open_access(access, cs, req));
62087c478bd9Sstevel@tonic-gate }
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate static nfsstat4
62117c478bd9Sstevel@tonic-gate create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
62127c478bd9Sstevel@tonic-gate     timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created)
62137c478bd9Sstevel@tonic-gate {
62147c478bd9Sstevel@tonic-gate 	int error;
62157c478bd9Sstevel@tonic-gate 	nfsstat4 status = NFS4_OK;
62167c478bd9Sstevel@tonic-gate 	vattr_t va;
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate tryagain:
62197c478bd9Sstevel@tonic-gate 
62207c478bd9Sstevel@tonic-gate 	/*
62217c478bd9Sstevel@tonic-gate 	 * The file open mode used is VWRITE.  If the client needs
62227c478bd9Sstevel@tonic-gate 	 * some other semantic, then it should do the access checking
62237c478bd9Sstevel@tonic-gate 	 * itself.  It would have been nice to have the file open mode
62247c478bd9Sstevel@tonic-gate 	 * passed as part of the arguments.
62257c478bd9Sstevel@tonic-gate 	 */
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 	*created = TRUE;
6228da6c28aaSamw 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
62297c478bd9Sstevel@tonic-gate 
62307c478bd9Sstevel@tonic-gate 	if (error) {
62317c478bd9Sstevel@tonic-gate 		*created = FALSE;
62327c478bd9Sstevel@tonic-gate 
62337c478bd9Sstevel@tonic-gate 		/*
62347c478bd9Sstevel@tonic-gate 		 * If we got something other than file already exists
62357c478bd9Sstevel@tonic-gate 		 * then just return this error.  Otherwise, we got
62367c478bd9Sstevel@tonic-gate 		 * EEXIST.  If we were doing a GUARDED create, then
62377c478bd9Sstevel@tonic-gate 		 * just return this error.  Otherwise, we need to
62387c478bd9Sstevel@tonic-gate 		 * make sure that this wasn't a duplicate of an
62397c478bd9Sstevel@tonic-gate 		 * exclusive create request.
62407c478bd9Sstevel@tonic-gate 		 *
62417c478bd9Sstevel@tonic-gate 		 * The assumption is made that a non-exclusive create
62427c478bd9Sstevel@tonic-gate 		 * request will never return EEXIST.
62437c478bd9Sstevel@tonic-gate 		 */
62447c478bd9Sstevel@tonic-gate 
62457c478bd9Sstevel@tonic-gate 		if (error != EEXIST || mode == GUARDED4) {
62467c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
62477c478bd9Sstevel@tonic-gate 			return (status);
62487c478bd9Sstevel@tonic-gate 		}
6249da6c28aaSamw 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
6250da6c28aaSamw 		    NULL, NULL, NULL);
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 		if (error) {
62537c478bd9Sstevel@tonic-gate 			/*
62547c478bd9Sstevel@tonic-gate 			 * We couldn't find the file that we thought that
62557c478bd9Sstevel@tonic-gate 			 * we just created.  So, we'll just try creating
62567c478bd9Sstevel@tonic-gate 			 * it again.
62577c478bd9Sstevel@tonic-gate 			 */
62587c478bd9Sstevel@tonic-gate 			if (error == ENOENT)
62597c478bd9Sstevel@tonic-gate 				goto tryagain;
62607c478bd9Sstevel@tonic-gate 
62617c478bd9Sstevel@tonic-gate 			status = puterrno4(error);
62627c478bd9Sstevel@tonic-gate 			return (status);
62637c478bd9Sstevel@tonic-gate 		}
62647c478bd9Sstevel@tonic-gate 
62657c478bd9Sstevel@tonic-gate 		if (mode == UNCHECKED4) {
62667c478bd9Sstevel@tonic-gate 			/* existing object must be regular file */
62677c478bd9Sstevel@tonic-gate 			if ((*vpp)->v_type != VREG) {
62687c478bd9Sstevel@tonic-gate 				if ((*vpp)->v_type == VDIR)
62697c478bd9Sstevel@tonic-gate 					status = NFS4ERR_ISDIR;
62707c478bd9Sstevel@tonic-gate 				else if ((*vpp)->v_type == VLNK)
62717c478bd9Sstevel@tonic-gate 					status = NFS4ERR_SYMLINK;
62727c478bd9Sstevel@tonic-gate 				else
62737c478bd9Sstevel@tonic-gate 					status = NFS4ERR_INVAL;
62747c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
62757c478bd9Sstevel@tonic-gate 				return (status);
62767c478bd9Sstevel@tonic-gate 			}
62777c478bd9Sstevel@tonic-gate 
62787c478bd9Sstevel@tonic-gate 			return (NFS4_OK);
62797c478bd9Sstevel@tonic-gate 		}
62807c478bd9Sstevel@tonic-gate 
62817c478bd9Sstevel@tonic-gate 		/* Check for duplicate request */
62827c478bd9Sstevel@tonic-gate 		ASSERT(mtime != 0);
62837c478bd9Sstevel@tonic-gate 		va.va_mask = AT_MTIME;
6284da6c28aaSamw 		error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
62857c478bd9Sstevel@tonic-gate 		if (!error) {
62867c478bd9Sstevel@tonic-gate 			/* We found the file */
62877c478bd9Sstevel@tonic-gate 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
62887c478bd9Sstevel@tonic-gate 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
62897c478bd9Sstevel@tonic-gate 				/* but its not our creation */
62907c478bd9Sstevel@tonic-gate 				VN_RELE(*vpp);
62917c478bd9Sstevel@tonic-gate 				return (NFS4ERR_EXIST);
62927c478bd9Sstevel@tonic-gate 			}
62937c478bd9Sstevel@tonic-gate 			*created = TRUE; /* retrans of create == created */
62947c478bd9Sstevel@tonic-gate 			return (NFS4_OK);
62957c478bd9Sstevel@tonic-gate 		}
62967c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
62977c478bd9Sstevel@tonic-gate 		return (NFS4ERR_EXIST);
62987c478bd9Sstevel@tonic-gate 	}
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
63017c478bd9Sstevel@tonic-gate }
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate static nfsstat4
6304d216dff5SRobert Mastors check_open_access(uint32_t access, struct compound_state *cs,
6305d216dff5SRobert Mastors     struct svc_req *req)
63067c478bd9Sstevel@tonic-gate {
63077c478bd9Sstevel@tonic-gate 	int error;
63087c478bd9Sstevel@tonic-gate 	vnode_t *vp;
63097c478bd9Sstevel@tonic-gate 	bool_t readonly;
63107c478bd9Sstevel@tonic-gate 	cred_t *cr = cs->cr;
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 	/* For now we don't allow mandatory locking as per V2/V3 */
63137c478bd9Sstevel@tonic-gate 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
63147c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ACCESS);
63157c478bd9Sstevel@tonic-gate 	}
63167c478bd9Sstevel@tonic-gate 
63177c478bd9Sstevel@tonic-gate 	vp = cs->vp;
63187c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL && vp->v_type == VREG);
63197c478bd9Sstevel@tonic-gate 
63207c478bd9Sstevel@tonic-gate 	/*
63217c478bd9Sstevel@tonic-gate 	 * If the file system is exported read only and we are trying
63227c478bd9Sstevel@tonic-gate 	 * to open for write, then return NFS4ERR_ROFS
63237c478bd9Sstevel@tonic-gate 	 */
63247c478bd9Sstevel@tonic-gate 
63255cb0d679SMarcel Telka 	readonly = rdonly4(req, cs);
63267c478bd9Sstevel@tonic-gate 
63277c478bd9Sstevel@tonic-gate 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
63287c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ROFS);
63297c478bd9Sstevel@tonic-gate 
63307c478bd9Sstevel@tonic-gate 	if (access & OPEN4_SHARE_ACCESS_READ) {
6331da6c28aaSamw 		if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
6332da6c28aaSamw 		    (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
63337c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ACCESS);
63347c478bd9Sstevel@tonic-gate 		}
63357c478bd9Sstevel@tonic-gate 	}
63367c478bd9Sstevel@tonic-gate 
63377c478bd9Sstevel@tonic-gate 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
6338da6c28aaSamw 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
63397c478bd9Sstevel@tonic-gate 		if (error)
63407c478bd9Sstevel@tonic-gate 			return (NFS4ERR_ACCESS);
63417c478bd9Sstevel@tonic-gate 	}
63427c478bd9Sstevel@tonic-gate 
63437c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
63447c478bd9Sstevel@tonic-gate }
63457c478bd9Sstevel@tonic-gate 
63467c478bd9Sstevel@tonic-gate static nfsstat4
63477c478bd9Sstevel@tonic-gate rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
63487c478bd9Sstevel@tonic-gate     change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
63497c478bd9Sstevel@tonic-gate {
63507c478bd9Sstevel@tonic-gate 	struct nfs4_svgetit_arg sarg;
63517c478bd9Sstevel@tonic-gate 	struct nfs4_ntov_table ntov;
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate 	bool_t ntov_table_init = FALSE;
63547c478bd9Sstevel@tonic-gate 	struct statvfs64 sb;
63557c478bd9Sstevel@tonic-gate 	nfsstat4 status;
63567c478bd9Sstevel@tonic-gate 	vnode_t *vp;
63577c478bd9Sstevel@tonic-gate 	vattr_t bva, ava, iva, cva, *vap;
63587c478bd9Sstevel@tonic-gate 	vnode_t *dvp;
63597c478bd9Sstevel@tonic-gate 	timespec32_t *mtime;
63607c478bd9Sstevel@tonic-gate 	char *nm = NULL;
63617c478bd9Sstevel@tonic-gate 	uint_t buflen;
63627c478bd9Sstevel@tonic-gate 	bool_t created;
63637c478bd9Sstevel@tonic-gate 	bool_t setsize = FALSE;
63647c478bd9Sstevel@tonic-gate 	len_t reqsize;
63657c478bd9Sstevel@tonic-gate 	int error;
63667c478bd9Sstevel@tonic-gate 	bool_t trunc;
63677c478bd9Sstevel@tonic-gate 	caller_context_t ct;
63687c478bd9Sstevel@tonic-gate 	component4 *component;
636945916cd2Sjpk 	bslabel_t *clabel;
6370b89a8333Snatalie li - Sun Microsystems - Irvine United States 	struct sockaddr *ca;
6371b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *name = NULL;
63727c478bd9Sstevel@tonic-gate 
63737c478bd9Sstevel@tonic-gate 	sarg.sbp = &sb;
63742f172c55SRobert Thurlow 	sarg.is_referral = B_FALSE;
63757c478bd9Sstevel@tonic-gate 
63767c478bd9Sstevel@tonic-gate 	dvp = cs->vp;
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate 	/* Check if the file system is read only */
63795cb0d679SMarcel Telka 	if (rdonly4(req, cs))
63807c478bd9Sstevel@tonic-gate 		return (NFS4ERR_ROFS);
63817c478bd9Sstevel@tonic-gate 
638245916cd2Sjpk 	/* check the label of including directory */
638345916cd2Sjpk 	if (is_system_labeled()) {
638445916cd2Sjpk 		ASSERT(req->rq_label != NULL);
638545916cd2Sjpk 		clabel = req->rq_label;
638645916cd2Sjpk 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
638745916cd2Sjpk 		    "got client label from request(1)",
638845916cd2Sjpk 		    struct svc_req *, req);
638945916cd2Sjpk 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
6390bd6f1640SJarrett Lu 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
6391bd6f1640SJarrett Lu 			    cs->exi)) {
639245916cd2Sjpk 				return (NFS4ERR_ACCESS);
639345916cd2Sjpk 			}
639445916cd2Sjpk 		}
639545916cd2Sjpk 	}
639645916cd2Sjpk 
63977c478bd9Sstevel@tonic-gate 	/*
63987c478bd9Sstevel@tonic-gate 	 * Get the last component of path name in nm. cs will reference
63997c478bd9Sstevel@tonic-gate 	 * the including directory on success.
64007c478bd9Sstevel@tonic-gate 	 */
64017c478bd9Sstevel@tonic-gate 	component = &args->open_claim4_u.file;
640215721462SDaniil Lunev 	status = utf8_dir_verify(component);
640315721462SDaniil Lunev 	if (status != NFS4_OK)
640415721462SDaniil Lunev 		return (status);
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate 	nm = utf8_to_fn(component, &buflen, NULL);
64077c478bd9Sstevel@tonic-gate 
64087c478bd9Sstevel@tonic-gate 	if (nm == NULL)
64097c478bd9Sstevel@tonic-gate 		return (NFS4ERR_RESOURCE);
64107c478bd9Sstevel@tonic-gate 
64117c478bd9Sstevel@tonic-gate 	if (buflen > MAXNAMELEN) {
64127c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
64137c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NAMETOOLONG);
64147c478bd9Sstevel@tonic-gate 	}
64157c478bd9Sstevel@tonic-gate 
64167c478bd9Sstevel@tonic-gate 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6417da6c28aaSamw 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
64187c478bd9Sstevel@tonic-gate 	if (error) {
64197c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
64207c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
64217c478bd9Sstevel@tonic-gate 	}
64227c478bd9Sstevel@tonic-gate 
64237c478bd9Sstevel@tonic-gate 	if (bva.va_type != VDIR) {
64247c478bd9Sstevel@tonic-gate 		kmem_free(nm, buflen);
64257c478bd9Sstevel@tonic-gate 		return (NFS4ERR_NOTDIR);
64267c478bd9Sstevel@tonic-gate 	}
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
64297c478bd9Sstevel@tonic-gate 
64307c478bd9Sstevel@tonic-gate 	switch (args->mode) {
64317c478bd9Sstevel@tonic-gate 	case GUARDED4:
64327c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
64337c478bd9Sstevel@tonic-gate 	case UNCHECKED4:
64347c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_init(&ntov);
64357c478bd9Sstevel@tonic-gate 		ntov_table_init = TRUE;
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 		*attrset = 0;
64387c478bd9Sstevel@tonic-gate 		status = do_rfs4_set_attrs(attrset,
64397c478bd9Sstevel@tonic-gate 		    &args->createhow4_u.createattrs,
64407c478bd9Sstevel@tonic-gate 		    cs, &sarg, &ntov, NFS4ATTR_SETIT);
64417c478bd9Sstevel@tonic-gate 
64427c478bd9Sstevel@tonic-gate 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
64437c478bd9Sstevel@tonic-gate 		    sarg.vap->va_type != VREG) {
64447c478bd9Sstevel@tonic-gate 			if (sarg.vap->va_type == VDIR)
64457c478bd9Sstevel@tonic-gate 				status = NFS4ERR_ISDIR;
64467c478bd9Sstevel@tonic-gate 			else if (sarg.vap->va_type == VLNK)
64477c478bd9Sstevel@tonic-gate 				status = NFS4ERR_SYMLINK;
64487c478bd9Sstevel@tonic-gate 			else
64497c478bd9Sstevel@tonic-gate 				status = NFS4ERR_INVAL;
64507c478bd9Sstevel@tonic-gate 		}
64517c478bd9Sstevel@tonic-gate 
64527c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK) {
64537c478bd9Sstevel@tonic-gate 			kmem_free(nm, buflen);
64547c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
64557c478bd9Sstevel@tonic-gate 			*attrset = 0;
64567c478bd9Sstevel@tonic-gate 			return (status);
64577c478bd9Sstevel@tonic-gate 		}
64587c478bd9Sstevel@tonic-gate 
64597c478bd9Sstevel@tonic-gate 		vap = sarg.vap;
64607c478bd9Sstevel@tonic-gate 		vap->va_type = VREG;
64617c478bd9Sstevel@tonic-gate 		vap->va_mask |= AT_TYPE;
64627c478bd9Sstevel@tonic-gate 
64637c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & AT_MODE) == 0) {
64647c478bd9Sstevel@tonic-gate 			vap->va_mask |= AT_MODE;
64657c478bd9Sstevel@tonic-gate 			vap->va_mode = (mode_t)0600;
64667c478bd9Sstevel@tonic-gate 		}
64677c478bd9Sstevel@tonic-gate 
64687c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_SIZE) {
64697c478bd9Sstevel@tonic-gate 
64707c478bd9Sstevel@tonic-gate 			/* Disallow create with a non-zero size */
64717c478bd9Sstevel@tonic-gate 
64727c478bd9Sstevel@tonic-gate 			if ((reqsize = sarg.vap->va_size) != 0) {
64737c478bd9Sstevel@tonic-gate 				kmem_free(nm, buflen);
64747c478bd9Sstevel@tonic-gate 				nfs4_ntov_table_free(&ntov, &sarg);
64757c478bd9Sstevel@tonic-gate 				*attrset = 0;
64767c478bd9Sstevel@tonic-gate 				return (NFS4ERR_INVAL);
64777c478bd9Sstevel@tonic-gate 			}
64787c478bd9Sstevel@tonic-gate 			setsize = TRUE;
64797c478bd9Sstevel@tonic-gate 		}
64807c478bd9Sstevel@tonic-gate 		break;
64817c478bd9Sstevel@tonic-gate 
64827c478bd9Sstevel@tonic-gate 	case EXCLUSIVE4:
64837c478bd9Sstevel@tonic-gate 		/* prohibit EXCL create of named attributes */
64847c478bd9Sstevel@tonic-gate 		if (dvp->v_flag & V_XATTRDIR) {
64857c478bd9Sstevel@tonic-gate 			kmem_free(nm, buflen);
64867c478bd9Sstevel@tonic-gate 			*attrset = 0;
64877c478bd9Sstevel@tonic-gate 			return (NFS4ERR_INVAL);
64887c478bd9Sstevel@tonic-gate 		}
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
64917c478bd9Sstevel@tonic-gate 		cva.va_type = VREG;
64927c478bd9Sstevel@tonic-gate 		/*
64937c478bd9Sstevel@tonic-gate 		 * Ensure no time overflows. Assumes underlying
64947c478bd9Sstevel@tonic-gate 		 * filesystem supports at least 32 bits.
64957c478bd9Sstevel@tonic-gate 		 * Truncate nsec to usec resolution to allow valid
64967c478bd9Sstevel@tonic-gate 		 * compares even if the underlying filesystem truncates.
64977c478bd9Sstevel@tonic-gate 		 */
64987c478bd9Sstevel@tonic-gate 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
64997c478bd9Sstevel@tonic-gate 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
65007c478bd9Sstevel@tonic-gate 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
65017c478bd9Sstevel@tonic-gate 		cva.va_mode = (mode_t)0;
65027c478bd9Sstevel@tonic-gate 		vap = &cva;
6503476ea8c9SJeff A. Smith 
6504476ea8c9SJeff A. Smith 		/*
6505476ea8c9SJeff A. Smith 		 * For EXCL create, attrset is set to the server attr
6506476ea8c9SJeff A. Smith 		 * used to cache the client's verifier.
6507476ea8c9SJeff A. Smith 		 */
6508476ea8c9SJeff A. Smith 		*attrset = FATTR4_TIME_MODIFY_MASK;
65097c478bd9Sstevel@tonic-gate 		break;
65107c478bd9Sstevel@tonic-gate 	}
65117c478bd9Sstevel@tonic-gate 
6512b89a8333Snatalie li - Sun Microsystems - Irvine United States 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6513b89a8333Snatalie li - Sun Microsystems - Irvine United States 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6514b89a8333Snatalie li - Sun Microsystems - Irvine United States 	    MAXPATHLEN  + 1);
6515b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6516b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (name == NULL) {
6517b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(nm, buflen);
6518b89a8333Snatalie li - Sun Microsystems - Irvine United States 		return (NFS4ERR_SERVERFAULT);
6519b89a8333Snatalie li - Sun Microsystems - Irvine United States 	}
6520b89a8333Snatalie li - Sun Microsystems - Irvine United States 
6521b89a8333Snatalie li - Sun Microsystems - Irvine United States 	status = create_vnode(dvp, name, vap, args->mode, mtime,
65227c478bd9Sstevel@tonic-gate 	    cs->cr, &vp, &created);
6523b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (nm != name)
6524b89a8333Snatalie li - Sun Microsystems - Irvine United States 		kmem_free(name, MAXPATHLEN + 1);
65257c478bd9Sstevel@tonic-gate 	kmem_free(nm, buflen);
65267c478bd9Sstevel@tonic-gate 
65277c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
65287c478bd9Sstevel@tonic-gate 		if (ntov_table_init)
65297c478bd9Sstevel@tonic-gate 			nfs4_ntov_table_free(&ntov, &sarg);
65307c478bd9Sstevel@tonic-gate 		*attrset = 0;
65317c478bd9Sstevel@tonic-gate 		return (status);
65327c478bd9Sstevel@tonic-gate 	}
65337c478bd9Sstevel@tonic-gate 
65347c478bd9Sstevel@tonic-gate 	trunc = (setsize && !created);
65357c478bd9Sstevel@tonic-gate 
65367c478bd9Sstevel@tonic-gate 	if (args->mode != EXCLUSIVE4) {
65377c478bd9Sstevel@tonic-gate 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
65387c478bd9Sstevel@tonic-gate 
65397c478bd9Sstevel@tonic-gate 		/*
65407c478bd9Sstevel@tonic-gate 		 * True verification that object was created with correct
65417c478bd9Sstevel@tonic-gate 		 * attrs is impossible.  The attrs could have been changed
65427c478bd9Sstevel@tonic-gate 		 * immediately after object creation.  If attributes did
65437c478bd9Sstevel@tonic-gate 		 * not verify, the only recourse for the server is to
65447c478bd9Sstevel@tonic-gate 		 * destroy the object.  Maybe if some attrs (like gid)
65457c478bd9Sstevel@tonic-gate 		 * are set incorrectly, the object should be destroyed;
65467c478bd9Sstevel@tonic-gate 		 * however, seems bad as a default policy.  Do we really
65477c478bd9Sstevel@tonic-gate 		 * want to destroy an object over one of the times not
65487c478bd9Sstevel@tonic-gate 		 * verifying correctly?  For these reasons, the server
65497c478bd9Sstevel@tonic-gate 		 * currently sets bits in attrset for createattrs
65507c478bd9Sstevel@tonic-gate 		 * that were set; however, no verification is done.
65517c478bd9Sstevel@tonic-gate 		 *
65527c478bd9Sstevel@tonic-gate 		 * vmask_to_nmask accounts for vattr bits set on create
65537c478bd9Sstevel@tonic-gate 		 *	[do_rfs4_set_attrs() only sets resp bits for
65547c478bd9Sstevel@tonic-gate 		 *	 non-vattr/vfs bits.]
65557c478bd9Sstevel@tonic-gate 		 * Mask off any bits we set by default so as not to return
65567c478bd9Sstevel@tonic-gate 		 * more attrset bits than were requested in createattrs
65577c478bd9Sstevel@tonic-gate 		 */
65587c478bd9Sstevel@tonic-gate 		if (created) {
65597c478bd9Sstevel@tonic-gate 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
65607c478bd9Sstevel@tonic-gate 			*attrset &= createmask;
65617c478bd9Sstevel@tonic-gate 		} else {
65627c478bd9Sstevel@tonic-gate 			/*
65637c478bd9Sstevel@tonic-gate 			 * We did not create the vnode (we tried but it
65647c478bd9Sstevel@tonic-gate 			 * already existed).  In this case, the only createattr
65657c478bd9Sstevel@tonic-gate 			 * that the spec allows the server to set is size,
65667c478bd9Sstevel@tonic-gate 			 * and even then, it can only be set if it is 0.
65677c478bd9Sstevel@tonic-gate 			 */
65687c478bd9Sstevel@tonic-gate 			*attrset = 0;
65697c478bd9Sstevel@tonic-gate 			if (trunc)
65707c478bd9Sstevel@tonic-gate 				*attrset = FATTR4_SIZE_MASK;
65717c478bd9Sstevel@tonic-gate 		}
65727c478bd9Sstevel@tonic-gate 	}
65737c478bd9Sstevel@tonic-gate 	if (ntov_table_init)
65747c478bd9Sstevel@tonic-gate 		nfs4_ntov_table_free(&ntov, &sarg);
65757c478bd9Sstevel@tonic-gate 
65767c478bd9Sstevel@tonic-gate 	/*
65777c478bd9Sstevel@tonic-gate 	 * Get the initial "after" sequence number, if it fails,
65787c478bd9Sstevel@tonic-gate 	 * set to zero, time to before.
65797c478bd9Sstevel@tonic-gate 	 */
65807c478bd9Sstevel@tonic-gate 	iva.va_mask = AT_CTIME|AT_SEQ;
6581da6c28aaSamw 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
65827c478bd9Sstevel@tonic-gate 		iva.va_seq = 0;
65837c478bd9Sstevel@tonic-gate 		iva.va_ctime = bva.va_ctime;
65847c478bd9Sstevel@tonic-gate 	}
65857c478bd9Sstevel@tonic-gate 
65867c478bd9Sstevel@tonic-gate 	/*
65877c478bd9Sstevel@tonic-gate 	 * create_vnode attempts to create the file exclusive,
65887c478bd9Sstevel@tonic-gate 	 * if it already exists the VOP_CREATE will fail and
65897c478bd9Sstevel@tonic-gate 	 * may not increase va_seq. It is atomic if
65907c478bd9Sstevel@tonic-gate 	 * we haven't changed the directory, but if it has changed
65917c478bd9Sstevel@tonic-gate 	 * we don't know what changed it.
65927c478bd9Sstevel@tonic-gate 	 */
65937c478bd9Sstevel@tonic-gate 	if (!created) {
65947c478bd9Sstevel@tonic-gate 		if (bva.va_seq && iva.va_seq &&
65957c478bd9Sstevel@tonic-gate 		    bva.va_seq == iva.va_seq)
65967c478bd9Sstevel@tonic-gate 			cinfo->atomic = TRUE;
65977c478bd9Sstevel@tonic-gate 		else
65987c478bd9Sstevel@tonic-gate 			cinfo->atomic = FALSE;
65997c478bd9Sstevel@tonic-gate 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
66007c478bd9Sstevel@tonic-gate 	} else {
66017c478bd9Sstevel@tonic-gate 		/*
66027c478bd9Sstevel@tonic-gate 		 * The entry was created, we need to sync the
66037c478bd9Sstevel@tonic-gate 		 * directory metadata.
66047c478bd9Sstevel@tonic-gate 		 */
6605da6c28aaSamw 		(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
66067c478bd9Sstevel@tonic-gate 
66077c478bd9Sstevel@tonic-gate 		/*
66087c478bd9Sstevel@tonic-gate 		 * Get "after" change value, if it fails, simply return the
66097c478bd9Sstevel@tonic-gate 		 * before value.
66107c478bd9Sstevel@tonic-gate 		 */
66117c478bd9Sstevel@tonic-gate 		ava.va_mask = AT_CTIME|AT_SEQ;
6612da6c28aaSamw 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
66137c478bd9Sstevel@tonic-gate 			ava.va_ctime = bva.va_ctime;
66147c478bd9Sstevel@tonic-gate 			ava.va_seq = 0;
66157c478bd9Sstevel@tonic-gate 		}
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
66187c478bd9Sstevel@tonic-gate 
66197c478bd9Sstevel@tonic-gate 		/*
66207c478bd9Sstevel@tonic-gate 		 * The cinfo->atomic = TRUE only if we have
66217c478bd9Sstevel@tonic-gate 		 * non-zero va_seq's, and it has incremented by exactly one
66227c478bd9Sstevel@tonic-gate 		 * during the create_vnode and it didn't
66237c478bd9Sstevel@tonic-gate 		 * change during the VOP_FSYNC.
66247c478bd9Sstevel@tonic-gate 		 */
66257c478bd9Sstevel@tonic-gate 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
66261b300de9Sjwahlig 		    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
66277c478bd9Sstevel@tonic-gate 			cinfo->atomic = TRUE;
66287c478bd9Sstevel@tonic-gate 		else
66297c478bd9Sstevel@tonic-gate 			cinfo->atomic = FALSE;
66307c478bd9Sstevel@tonic-gate 	}
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate 	/* Check for mandatory locking and that the size gets set. */
66337c478bd9Sstevel@tonic-gate 	cva.va_mask = AT_MODE;
66347c478bd9Sstevel@tonic-gate 	if (setsize)
66357c478bd9Sstevel@tonic-gate 		cva.va_mask |= AT_SIZE;
66367c478bd9Sstevel@tonic-gate 
66377c478bd9Sstevel@tonic-gate 	/* Assume the worst */
66387c478bd9Sstevel@tonic-gate 	cs->mandlock = TRUE;
66397c478bd9Sstevel@tonic-gate 
6640da6c28aaSamw 	if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
66417c478bd9Sstevel@tonic-gate 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
66427c478bd9Sstevel@tonic-gate 
66437c478bd9Sstevel@tonic-gate 		/*
66447c478bd9Sstevel@tonic-gate 		 * Truncate the file if necessary; this would be
66457c478bd9Sstevel@tonic-gate 		 * the case for create over an existing file.
66467c478bd9Sstevel@tonic-gate 		 */
66477c478bd9Sstevel@tonic-gate 
66487c478bd9Sstevel@tonic-gate 		if (trunc) {
66497c478bd9Sstevel@tonic-gate 			int in_crit = 0;
66507c478bd9Sstevel@tonic-gate 			rfs4_file_t *fp;
66517c478bd9Sstevel@tonic-gate 			bool_t create = FALSE;
66527c478bd9Sstevel@tonic-gate 
66537c478bd9Sstevel@tonic-gate 			/*
66547c478bd9Sstevel@tonic-gate 			 * We are writing over an existing file.
66557c478bd9Sstevel@tonic-gate 			 * Check to see if we need to recall a delegation.
66567c478bd9Sstevel@tonic-gate 			 */
66577c478bd9Sstevel@tonic-gate 			rfs4_hold_deleg_policy();
66587c478bd9Sstevel@tonic-gate 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
66597c478bd9Sstevel@tonic-gate 				if (rfs4_check_delegated_byfp(FWRITE, fp,
66601b300de9Sjwahlig 				    (reqsize == 0), FALSE, FALSE, &clientid)) {
66617c478bd9Sstevel@tonic-gate 					rfs4_file_rele(fp);
66627c478bd9Sstevel@tonic-gate 					rfs4_rele_deleg_policy();
66637c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
66647c478bd9Sstevel@tonic-gate 					*attrset = 0;
66657c478bd9Sstevel@tonic-gate 					return (NFS4ERR_DELAY);
66667c478bd9Sstevel@tonic-gate 				}
66677c478bd9Sstevel@tonic-gate 				rfs4_file_rele(fp);
66687c478bd9Sstevel@tonic-gate 			}
66697c478bd9Sstevel@tonic-gate 			rfs4_rele_deleg_policy();
66707c478bd9Sstevel@tonic-gate 
66717c478bd9Sstevel@tonic-gate 			if (nbl_need_check(vp)) {
66727c478bd9Sstevel@tonic-gate 				in_crit = 1;
66737c478bd9Sstevel@tonic-gate 
66747c478bd9Sstevel@tonic-gate 				ASSERT(reqsize == 0);
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate 				nbl_start_crit(vp, RW_READER);
66777c478bd9Sstevel@tonic-gate 				if (nbl_conflict(vp, NBL_WRITE, 0,
6678da6c28aaSamw 				    cva.va_size, 0, NULL)) {
66797c478bd9Sstevel@tonic-gate 					in_crit = 0;
66807c478bd9Sstevel@tonic-gate 					nbl_end_crit(vp);
66817c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
66827c478bd9Sstevel@tonic-gate 					*attrset = 0;
66837c478bd9Sstevel@tonic-gate 					return (NFS4ERR_ACCESS);
66847c478bd9Sstevel@tonic-gate 				}
66857c478bd9Sstevel@tonic-gate 			}
66867c478bd9Sstevel@tonic-gate 			ct.cc_sysid = 0;
66877c478bd9Sstevel@tonic-gate 			ct.cc_pid = 0;
66887c478bd9Sstevel@tonic-gate 			ct.cc_caller_id = nfs4_srv_caller_id;
668962b9fcbeSjwahlig 			ct.cc_flags = CC_DONTBLOCK;
66907c478bd9Sstevel@tonic-gate 
66917c478bd9Sstevel@tonic-gate 			cva.va_mask = AT_SIZE;
66927c478bd9Sstevel@tonic-gate 			cva.va_size = reqsize;
66937c478bd9Sstevel@tonic-gate 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
66947c478bd9Sstevel@tonic-gate 			if (in_crit)
66957c478bd9Sstevel@tonic-gate 				nbl_end_crit(vp);
66967c478bd9Sstevel@tonic-gate 		}
66977c478bd9Sstevel@tonic-gate 	}
66987c478bd9Sstevel@tonic-gate 
66997c478bd9Sstevel@tonic-gate 	error = makefh4(&cs->fh, vp, cs->exi);
67007c478bd9Sstevel@tonic-gate 
67017c478bd9Sstevel@tonic-gate 	/*
67027c478bd9Sstevel@tonic-gate 	 * Force modified data and metadata out to stable storage.
67037c478bd9Sstevel@tonic-gate 	 */
6704da6c28aaSamw 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate 	if (error) {
67077c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
67087c478bd9Sstevel@tonic-gate 		*attrset = 0;
67097c478bd9Sstevel@tonic-gate 		return (puterrno4(error));
67107c478bd9Sstevel@tonic-gate 	}
67117c478bd9Sstevel@tonic-gate 
67127c478bd9Sstevel@tonic-gate 	/* if parent dir is attrdir, set namedattr fh flag */
67137c478bd9Sstevel@tonic-gate 	if (dvp->v_flag & V_XATTRDIR)
67147c478bd9Sstevel@tonic-gate 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
67157c478bd9Sstevel@tonic-gate 
67167c478bd9Sstevel@tonic-gate 	if (cs->vp)
67177c478bd9Sstevel@tonic-gate 		VN_RELE(cs->vp);
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 	cs->vp = vp;
67207c478bd9Sstevel@tonic-gate 
67217c478bd9Sstevel@tonic-gate 	/*
67227c478bd9Sstevel@tonic-gate 	 * if we did not create the file, we will need to check
67237c478bd9Sstevel@tonic-gate 	 * the access bits on the file
67247c478bd9Sstevel@tonic-gate 	 */
67257c478bd9Sstevel@tonic-gate 
67267c478bd9Sstevel@tonic-gate 	if (!created) {
67277c478bd9Sstevel@tonic-gate 		if (setsize)
67287c478bd9Sstevel@tonic-gate 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
67297c478bd9Sstevel@tonic-gate 		status = check_open_access(args->share_access, cs, req);
67307c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK)
67317c478bd9Sstevel@tonic-gate 			*attrset = 0;
67327c478bd9Sstevel@tonic-gate 	}
67337c478bd9Sstevel@tonic-gate 	return (status);
67347c478bd9Sstevel@tonic-gate }
67357c478bd9Sstevel@tonic-gate 
67367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
67377c478bd9Sstevel@tonic-gate static void
67387c478bd9Sstevel@tonic-gate rfs4_do_open(struct compound_state *cs, struct svc_req *req,
67397c478bd9Sstevel@tonic-gate     rfs4_openowner_t *oo, delegreq_t deleg,
67407c478bd9Sstevel@tonic-gate     uint32_t access, uint32_t deny,
6741da6c28aaSamw     OPEN4res *resp, int deleg_cur)
67427c478bd9Sstevel@tonic-gate {
67437c478bd9Sstevel@tonic-gate 	/* XXX Currently not using req  */
6744d216dff5SRobert Mastors 	rfs4_state_t *sp;
6745d216dff5SRobert Mastors 	rfs4_file_t *fp;
67467c478bd9Sstevel@tonic-gate 	bool_t screate = TRUE;
67477c478bd9Sstevel@tonic-gate 	bool_t fcreate = TRUE;
674850956b22SJames Wahlig 	uint32_t open_a, share_a;
674950956b22SJames Wahlig 	uint32_t open_d, share_d;
67507c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
67517c478bd9Sstevel@tonic-gate 	sysid_t sysid;
67527c478bd9Sstevel@tonic-gate 	nfsstat4 status;
6753da6c28aaSamw 	caller_context_t ct;
67547c478bd9Sstevel@tonic-gate 	int fflags = 0;
67557c478bd9Sstevel@tonic-gate 	int recall = 0;
67567c478bd9Sstevel@tonic-gate 	int err;
6757d216dff5SRobert Mastors 	int first_open;
6758*91822a67SArne Jansen 	struct vattr va;
67597c478bd9Sstevel@tonic-gate 
67607c478bd9Sstevel@tonic-gate 	/* get the file struct and hold a lock on it during initial open */
6761d216dff5SRobert Mastors 	fp = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6762d216dff5SRobert Mastors 	if (fp == NULL) {
6763f6cf9e50SRick Mesta 		resp->status = NFS4ERR_RESOURCE;
6764f6cf9e50SRick Mesta 		DTRACE_PROBE1(nfss__e__do__open1, nfsstat4, resp->status);
67657c478bd9Sstevel@tonic-gate 		return;
67667c478bd9Sstevel@tonic-gate 	}
67677c478bd9Sstevel@tonic-gate 
6768*91822a67SArne Jansen 	/*
6769*91822a67SArne Jansen 	 * See if the file has been unlinked in the meantime
6770*91822a67SArne Jansen 	 */
6771*91822a67SArne Jansen 	va.va_mask = AT_NLINK;
6772*91822a67SArne Jansen 	if (!VOP_GETATTR(fp->rf_vp, &va, 0, cs->cr, NULL) && va.va_nlink == 0) {
6773*91822a67SArne Jansen 		resp->status = NFS4ERR_RESOURCE;
6774*91822a67SArne Jansen 		DTRACE_PROBE1(nfss__e__do__open2, nfsstat4, resp->status);
6775*91822a67SArne Jansen 		rw_exit(&fp->rf_file_rwlock);
6776*91822a67SArne Jansen 		rfs4_file_rele(fp);
6777*91822a67SArne Jansen 		return;
6778*91822a67SArne Jansen 	}
6779*91822a67SArne Jansen 
6780d216dff5SRobert Mastors 	sp = rfs4_findstate_by_owner_file(oo, fp, &screate);
6781d216dff5SRobert Mastors 	if (sp == NULL) {
67827c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_RESOURCE;
6783*91822a67SArne Jansen 		DTRACE_PROBE1(nfss__e__do__open3, nfsstat4, resp->status);
67847c478bd9Sstevel@tonic-gate 		/* No need to keep any reference */
6785d216dff5SRobert Mastors 		rw_exit(&fp->rf_file_rwlock);
6786d216dff5SRobert Mastors 		rfs4_file_rele(fp);
67877c478bd9Sstevel@tonic-gate 		return;
67887c478bd9Sstevel@tonic-gate 	}
67897c478bd9Sstevel@tonic-gate 
6790fd93bfc3Sjwahlig 	/* try to get the sysid before continuing */
6791d216dff5SRobert Mastors 	if ((status = rfs4_client_sysid(oo->ro_client, &sysid)) != NFS4_OK) {
67927c478bd9Sstevel@tonic-gate 		resp->status = status;
6793d216dff5SRobert Mastors 		rfs4_file_rele(fp);
67947c478bd9Sstevel@tonic-gate 		/* Not a fully formed open; "close" it */
67957c478bd9Sstevel@tonic-gate 		if (screate == TRUE)
6796d216dff5SRobert Mastors 			rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6797d216dff5SRobert Mastors 		rfs4_state_rele(sp);
67987c478bd9Sstevel@tonic-gate 		return;
67997c478bd9Sstevel@tonic-gate 	}
6800fd93bfc3Sjwahlig 
6801da6c28aaSamw 	/* Calculate the fflags for this OPEN. */
6802da6c28aaSamw 	if (access & OPEN4_SHARE_ACCESS_READ)
6803da6c28aaSamw 		fflags |= FREAD;
6804da6c28aaSamw 	if (access & OPEN4_SHARE_ACCESS_WRITE)
6805da6c28aaSamw 		fflags |= FWRITE;
6806da6c28aaSamw 
6807d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
6808d216dff5SRobert Mastors 
6809fd93bfc3Sjwahlig 	/*
6810fd93bfc3Sjwahlig 	 * Calculate the new deny and access mode that this open is adding to
6811fd93bfc3Sjwahlig 	 * the file for this open owner;
6812fd93bfc3Sjwahlig 	 */
681350956b22SJames Wahlig 	open_d = (deny & ~sp->rs_open_deny);
681450956b22SJames Wahlig 	open_a = (access & ~sp->rs_open_access);
6815d216dff5SRobert Mastors 
681650956b22SJames Wahlig 	/*
681750956b22SJames Wahlig 	 * Calculate the new share access and share deny modes that this open
681850956b22SJames Wahlig 	 * is adding to the file for this open owner;
681950956b22SJames Wahlig 	 */
682050956b22SJames Wahlig 	share_a = (access & ~sp->rs_share_access);
682150956b22SJames Wahlig 	share_d = (deny & ~sp->rs_share_deny);
682250956b22SJames Wahlig 
682350956b22SJames Wahlig 	first_open = (sp->rs_open_access & OPEN4_SHARE_ACCESS_BOTH) == 0;
6824fd93bfc3Sjwahlig 
6825fd93bfc3Sjwahlig 	/*
6826fd93bfc3Sjwahlig 	 * Check to see the client has already sent an open for this
6827fd93bfc3Sjwahlig 	 * open owner on this file with the same share/deny modes.
6828fd93bfc3Sjwahlig 	 * If so, we don't need to check for a conflict and we don't
6829fd93bfc3Sjwahlig 	 * need to add another shrlock.  If not, then we need to
6830fd93bfc3Sjwahlig 	 * check for conflicts in deny and access before checking for
6831fd93bfc3Sjwahlig 	 * conflicts in delegation.  We don't want to recall a
6832fd93bfc3Sjwahlig 	 * delegation based on an open that will eventually fail based
6833fd93bfc3Sjwahlig 	 * on shares modes.
6834fd93bfc3Sjwahlig 	 */
6835fd93bfc3Sjwahlig 
683650956b22SJames Wahlig 	if (share_a || share_d) {
6837d216dff5SRobert Mastors 		if ((err = rfs4_share(sp, access, deny)) != 0) {
6838d216dff5SRobert Mastors 			rfs4_dbe_unlock(sp->rs_dbe);
6839d216dff5SRobert Mastors 			resp->status = err;
68407c478bd9Sstevel@tonic-gate 
6841d216dff5SRobert Mastors 			rfs4_file_rele(fp);
68427c478bd9Sstevel@tonic-gate 			/* Not a fully formed open; "close" it */
68437c478bd9Sstevel@tonic-gate 			if (screate == TRUE)
6844d216dff5SRobert Mastors 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6845d216dff5SRobert Mastors 			rfs4_state_rele(sp);
68467c478bd9Sstevel@tonic-gate 			return;
68477c478bd9Sstevel@tonic-gate 		}
6848fd93bfc3Sjwahlig 	}
68497c478bd9Sstevel@tonic-gate 
6850d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
68517c478bd9Sstevel@tonic-gate 
68527c478bd9Sstevel@tonic-gate 	/*
68537c478bd9Sstevel@tonic-gate 	 * Check to see if this file is delegated and if so, if a
68547c478bd9Sstevel@tonic-gate 	 * recall needs to be done.
68557c478bd9Sstevel@tonic-gate 	 */
6856d216dff5SRobert Mastors 	if (rfs4_check_recall(sp, access)) {
6857d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
6858d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
6859d216dff5SRobert Mastors 		rfs4_recall_deleg(fp, FALSE, sp->rs_owner->ro_client);
68607c478bd9Sstevel@tonic-gate 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6861d216dff5SRobert Mastors 		rfs4_dbe_lock(sp->rs_dbe);
686214f41b92SRobert Mastors 
686314f41b92SRobert Mastors 		/* if state closed while lock was dropped */
686414f41b92SRobert Mastors 		if (sp->rs_closed) {
686550956b22SJames Wahlig 			if (share_a || share_d)
686614f41b92SRobert Mastors 				(void) rfs4_unshare(sp);
686714f41b92SRobert Mastors 			rfs4_dbe_unlock(sp->rs_dbe);
686814f41b92SRobert Mastors 			rfs4_file_rele(fp);
686914f41b92SRobert Mastors 			/* Not a fully formed open; "close" it */
687014f41b92SRobert Mastors 			if (screate == TRUE)
687114f41b92SRobert Mastors 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
687214f41b92SRobert Mastors 			rfs4_state_rele(sp);
687314f41b92SRobert Mastors 			resp->status = NFS4ERR_OLD_STATEID;
687414f41b92SRobert Mastors 			return;
687514f41b92SRobert Mastors 		}
687614f41b92SRobert Mastors 
6877d216dff5SRobert Mastors 		rfs4_dbe_lock(fp->rf_dbe);
68787c478bd9Sstevel@tonic-gate 		/* Let's see if the delegation was returned */
6879d216dff5SRobert Mastors 		if (rfs4_check_recall(sp, access)) {
6880d216dff5SRobert Mastors 			rfs4_dbe_unlock(fp->rf_dbe);
688150956b22SJames Wahlig 			if (share_a || share_d)
6882d216dff5SRobert Mastors 				(void) rfs4_unshare(sp);
6883d216dff5SRobert Mastors 			rfs4_dbe_unlock(sp->rs_dbe);
6884d216dff5SRobert Mastors 			rfs4_file_rele(fp);
6885d216dff5SRobert Mastors 			rfs4_update_lease(sp->rs_owner->ro_client);
6886d216dff5SRobert Mastors 
68877c478bd9Sstevel@tonic-gate 			/* Not a fully formed open; "close" it */
68887c478bd9Sstevel@tonic-gate 			if (screate == TRUE)
6889d216dff5SRobert Mastors 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6890d216dff5SRobert Mastors 			rfs4_state_rele(sp);
68917c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_DELAY;
68927c478bd9Sstevel@tonic-gate 			return;
68937c478bd9Sstevel@tonic-gate 		}
68947c478bd9Sstevel@tonic-gate 	}
6895da6c28aaSamw 	/*
6896da6c28aaSamw 	 * the share check passed and any delegation conflict has been
6897da6c28aaSamw 	 * taken care of, now call vop_open.
6898da6c28aaSamw 	 * if this is the first open then call vop_open with fflags.
6899da6c28aaSamw 	 * if not, call vn_open_upgrade with just the upgrade flags.
6900da6c28aaSamw 	 *
6901da6c28aaSamw 	 * if the file has been opened already, it will have the current
6902da6c28aaSamw 	 * access mode in the state struct.  if it has no share access, then
6903da6c28aaSamw 	 * this is a new open.
6904da6c28aaSamw 	 *
6905da6c28aaSamw 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6906da6c28aaSamw 	 * call VOP_OPEN(), just do the open upgrade.
6907da6c28aaSamw 	 */
6908d216dff5SRobert Mastors 	if (first_open && !deleg_cur) {
6909da6c28aaSamw 		ct.cc_sysid = sysid;
6910d216dff5SRobert Mastors 		ct.cc_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
6911da6c28aaSamw 		ct.cc_caller_id = nfs4_srv_caller_id;
691262b9fcbeSjwahlig 		ct.cc_flags = CC_DONTBLOCK;
6913da6c28aaSamw 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6914da6c28aaSamw 		if (err) {
6915d216dff5SRobert Mastors 			rfs4_dbe_unlock(fp->rf_dbe);
691650956b22SJames Wahlig 			if (share_a || share_d)
6917d216dff5SRobert Mastors 				(void) rfs4_unshare(sp);
6918d216dff5SRobert Mastors 			rfs4_dbe_unlock(sp->rs_dbe);
6919d216dff5SRobert Mastors 			rfs4_file_rele(fp);
6920d216dff5SRobert Mastors 
6921da6c28aaSamw 			/* Not a fully formed open; "close" it */
6922da6c28aaSamw 			if (screate == TRUE)
6923d216dff5SRobert Mastors 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6924d216dff5SRobert Mastors 			rfs4_state_rele(sp);
6925335fb9e6Sjwahlig 			/* check if a monitor detected a delegation conflict */
6926335fb9e6Sjwahlig 			if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6927335fb9e6Sjwahlig 				resp->status = NFS4ERR_DELAY;
6928335fb9e6Sjwahlig 			else
6929da6c28aaSamw 				resp->status = NFS4ERR_SERVERFAULT;
6930da6c28aaSamw 			return;
6931da6c28aaSamw 		}
6932da6c28aaSamw 	} else { /* open upgrade */
6933da6c28aaSamw 		/*
6934da6c28aaSamw 		 * calculate the fflags for the new mode that is being added
6935da6c28aaSamw 		 * by this upgrade.
6936da6c28aaSamw 		 */
6937da6c28aaSamw 		fflags = 0;
693850956b22SJames Wahlig 		if (open_a & OPEN4_SHARE_ACCESS_READ)
6939da6c28aaSamw 			fflags |= FREAD;
694050956b22SJames Wahlig 		if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6941da6c28aaSamw 			fflags |= FWRITE;
6942da6c28aaSamw 		vn_open_upgrade(cs->vp, fflags);
6943da6c28aaSamw 	}
694450956b22SJames Wahlig 	sp->rs_open_access |= access;
694550956b22SJames Wahlig 	sp->rs_open_deny |= deny;
69467c478bd9Sstevel@tonic-gate 
694750956b22SJames Wahlig 	if (open_d & OPEN4_SHARE_DENY_READ)
6948d216dff5SRobert Mastors 		fp->rf_deny_read++;
694950956b22SJames Wahlig 	if (open_d & OPEN4_SHARE_DENY_WRITE)
6950d216dff5SRobert Mastors 		fp->rf_deny_write++;
6951d216dff5SRobert Mastors 	fp->rf_share_deny |= deny;
69527c478bd9Sstevel@tonic-gate 
695350956b22SJames Wahlig 	if (open_a & OPEN4_SHARE_ACCESS_READ)
6954d216dff5SRobert Mastors 		fp->rf_access_read++;
695550956b22SJames Wahlig 	if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6956d216dff5SRobert Mastors 		fp->rf_access_write++;
6957d216dff5SRobert Mastors 	fp->rf_share_access |= access;
69587c478bd9Sstevel@tonic-gate 
69597c478bd9Sstevel@tonic-gate 	/*
69607c478bd9Sstevel@tonic-gate 	 * Check for delegation here. if the deleg argument is not
69617c478bd9Sstevel@tonic-gate 	 * DELEG_ANY, then this is a reclaim from a client and
69627c478bd9Sstevel@tonic-gate 	 * we must honor the delegation requested. If necessary we can
69637c478bd9Sstevel@tonic-gate 	 * set the recall flag.
69647c478bd9Sstevel@tonic-gate 	 */
69657c478bd9Sstevel@tonic-gate 
6966d216dff5SRobert Mastors 	dsp = rfs4_grant_delegation(deleg, sp, &recall);
69677c478bd9Sstevel@tonic-gate 
6968d216dff5SRobert Mastors 	cs->deleg = (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE);
69697c478bd9Sstevel@tonic-gate 
6970d216dff5SRobert Mastors 	next_stateid(&sp->rs_stateid);
69717c478bd9Sstevel@tonic-gate 
6972d216dff5SRobert Mastors 	resp->stateid = sp->rs_stateid.stateid;
69737c478bd9Sstevel@tonic-gate 
6974d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
6975d216dff5SRobert Mastors 	rfs4_dbe_unlock(sp->rs_dbe);
69767c478bd9Sstevel@tonic-gate 
69777c478bd9Sstevel@tonic-gate 	if (dsp) {
69787c478bd9Sstevel@tonic-gate 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
69797c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
69807c478bd9Sstevel@tonic-gate 	}
69817c478bd9Sstevel@tonic-gate 
6982d216dff5SRobert Mastors 	rfs4_file_rele(fp);
6983d216dff5SRobert Mastors 	rfs4_state_rele(sp);
69847c478bd9Sstevel@tonic-gate 
69857c478bd9Sstevel@tonic-gate 	resp->status = NFS4_OK;
69867c478bd9Sstevel@tonic-gate }
69877c478bd9Sstevel@tonic-gate 
69887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
69897c478bd9Sstevel@tonic-gate static void
69907c478bd9Sstevel@tonic-gate rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
69917c478bd9Sstevel@tonic-gate     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
69927c478bd9Sstevel@tonic-gate {
69937c478bd9Sstevel@tonic-gate 	change_info4 *cinfo = &resp->cinfo;
69947c478bd9Sstevel@tonic-gate 	bitmap4 *attrset = &resp->attrset;
69957c478bd9Sstevel@tonic-gate 
69967c478bd9Sstevel@tonic-gate 	if (args->opentype == OPEN4_NOCREATE)
69977c478bd9Sstevel@tonic-gate 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
69987c478bd9Sstevel@tonic-gate 		    req, cs, args->share_access, cinfo);
69997c478bd9Sstevel@tonic-gate 	else {
70007c478bd9Sstevel@tonic-gate 		/* inhibit delegation grants during exclusive create */
70017c478bd9Sstevel@tonic-gate 
70027c478bd9Sstevel@tonic-gate 		if (args->mode == EXCLUSIVE4)
70037c478bd9Sstevel@tonic-gate 			rfs4_disable_delegation();
70047c478bd9Sstevel@tonic-gate 
70057c478bd9Sstevel@tonic-gate 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
7006d216dff5SRobert Mastors 		    oo->ro_client->rc_clientid);
70077c478bd9Sstevel@tonic-gate 	}
70087c478bd9Sstevel@tonic-gate 
70097c478bd9Sstevel@tonic-gate 	if (resp->status == NFS4_OK) {
70107c478bd9Sstevel@tonic-gate 
70117c478bd9Sstevel@tonic-gate 		/* cs->vp cs->fh now reference the desired file */
70127c478bd9Sstevel@tonic-gate 
7013e228dd39SJames Wahlig 		rfs4_do_open(cs, req, oo,
7014e228dd39SJames Wahlig 		    oo->ro_need_confirm ? DELEG_NONE : DELEG_ANY,
7015e228dd39SJames Wahlig 		    args->share_access, args->share_deny, resp, 0);
70167c478bd9Sstevel@tonic-gate 
70177c478bd9Sstevel@tonic-gate 		/*
70187c478bd9Sstevel@tonic-gate 		 * If rfs4_createfile set attrset, we must
70197c478bd9Sstevel@tonic-gate 		 * clear this attrset before the response is copied.
70207c478bd9Sstevel@tonic-gate 		 */
70217c478bd9Sstevel@tonic-gate 		if (resp->status != NFS4_OK && resp->attrset) {
70227c478bd9Sstevel@tonic-gate 			resp->attrset = 0;
70237c478bd9Sstevel@tonic-gate 		}
70247c478bd9Sstevel@tonic-gate 	}
70257c478bd9Sstevel@tonic-gate 	else
70267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status;
70277c478bd9Sstevel@tonic-gate 
70287c478bd9Sstevel@tonic-gate 	if (args->mode == EXCLUSIVE4)
70297c478bd9Sstevel@tonic-gate 		rfs4_enable_delegation();
70307c478bd9Sstevel@tonic-gate }
70317c478bd9Sstevel@tonic-gate 
70327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
70337c478bd9Sstevel@tonic-gate static void
70347c478bd9Sstevel@tonic-gate rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
70357c478bd9Sstevel@tonic-gate     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
70367c478bd9Sstevel@tonic-gate {
70377c478bd9Sstevel@tonic-gate 	change_info4 *cinfo = &resp->cinfo;
70387c478bd9Sstevel@tonic-gate 	vattr_t va;
70397c478bd9Sstevel@tonic-gate 	vtype_t v_type = cs->vp->v_type;
70407c478bd9Sstevel@tonic-gate 	int error = 0;
70417c478bd9Sstevel@tonic-gate 
70427c478bd9Sstevel@tonic-gate 	/* Verify that we have a regular file */
70437c478bd9Sstevel@tonic-gate 	if (v_type != VREG) {
70447c478bd9Sstevel@tonic-gate 		if (v_type == VDIR)
70457c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_ISDIR;
70467c478bd9Sstevel@tonic-gate 		else if (v_type == VLNK)
70477c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_SYMLINK;
70487c478bd9Sstevel@tonic-gate 		else
70497c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_INVAL;
70507c478bd9Sstevel@tonic-gate 		return;
70517c478bd9Sstevel@tonic-gate 	}
70527c478bd9Sstevel@tonic-gate 
70537c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE|AT_UID;
7054da6c28aaSamw 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
70557c478bd9Sstevel@tonic-gate 	if (error) {
70567c478bd9Sstevel@tonic-gate 		resp->status = puterrno4(error);
70577c478bd9Sstevel@tonic-gate 		return;
70587c478bd9Sstevel@tonic-gate 	}
70597c478bd9Sstevel@tonic-gate 
70607c478bd9Sstevel@tonic-gate 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 	/*
70637c478bd9Sstevel@tonic-gate 	 * Check if we have access to the file, Note the the file
70647c478bd9Sstevel@tonic-gate 	 * could have originally been open UNCHECKED or GUARDED
70657c478bd9Sstevel@tonic-gate 	 * with mode bits that will now fail, but there is nothing
70667c478bd9Sstevel@tonic-gate 	 * we can really do about that except in the case that the
70677c478bd9Sstevel@tonic-gate 	 * owner of the file is the one requesting the open.
70687c478bd9Sstevel@tonic-gate 	 */
70697c478bd9Sstevel@tonic-gate 	if (crgetuid(cs->cr) != va.va_uid) {
70707c478bd9Sstevel@tonic-gate 		resp->status = check_open_access(args->share_access, cs, req);
70717c478bd9Sstevel@tonic-gate 		if (resp->status != NFS4_OK) {
70727c478bd9Sstevel@tonic-gate 			return;
70737c478bd9Sstevel@tonic-gate 		}
70747c478bd9Sstevel@tonic-gate 	}
70757c478bd9Sstevel@tonic-gate 
70767c478bd9Sstevel@tonic-gate 	/*
70777c478bd9Sstevel@tonic-gate 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
70787c478bd9Sstevel@tonic-gate 	 */
70797c478bd9Sstevel@tonic-gate 	cinfo->before = 0;
70807c478bd9Sstevel@tonic-gate 	cinfo->after = 0;
70817c478bd9Sstevel@tonic-gate 	cinfo->atomic = FALSE;
70827c478bd9Sstevel@tonic-gate 
70837c478bd9Sstevel@tonic-gate 	rfs4_do_open(cs, req, oo,
70847c478bd9Sstevel@tonic-gate 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
7085da6c28aaSamw 	    args->share_access, args->share_deny, resp, 0);
70867c478bd9Sstevel@tonic-gate }
70877c478bd9Sstevel@tonic-gate 
70887c478bd9Sstevel@tonic-gate static void
70897c478bd9Sstevel@tonic-gate rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
70907c478bd9Sstevel@tonic-gate     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
70917c478bd9Sstevel@tonic-gate {
70927c478bd9Sstevel@tonic-gate 	int error;
70937c478bd9Sstevel@tonic-gate 	nfsstat4 status;
70947c478bd9Sstevel@tonic-gate 	stateid4 stateid =
70957c478bd9Sstevel@tonic-gate 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
70967c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 	/*
70997c478bd9Sstevel@tonic-gate 	 * Find the state info from the stateid and confirm that the
71007c478bd9Sstevel@tonic-gate 	 * file is delegated.  If the state openowner is the same as
71017c478bd9Sstevel@tonic-gate 	 * the supplied openowner we're done. If not, get the file
71027c478bd9Sstevel@tonic-gate 	 * info from the found state info. Use that file info to
71037c478bd9Sstevel@tonic-gate 	 * create the state for this lock owner. Note solaris doen't
71047c478bd9Sstevel@tonic-gate 	 * really need the pathname to find the file. We may want to
71057c478bd9Sstevel@tonic-gate 	 * lookup the pathname and make sure that the vp exist and
71067c478bd9Sstevel@tonic-gate 	 * matches the vp in the file structure. However it is
71077c478bd9Sstevel@tonic-gate 	 * possible that the pathname nolonger exists (local process
71087c478bd9Sstevel@tonic-gate 	 * unlinks the file), so this may not be that useful.
71097c478bd9Sstevel@tonic-gate 	 */
71107c478bd9Sstevel@tonic-gate 
71117c478bd9Sstevel@tonic-gate 	status = rfs4_get_deleg_state(&stateid, &dsp);
71127c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
71137c478bd9Sstevel@tonic-gate 		resp->status = status;
71147c478bd9Sstevel@tonic-gate 		return;
71157c478bd9Sstevel@tonic-gate 	}
71167c478bd9Sstevel@tonic-gate 
7117d216dff5SRobert Mastors 	ASSERT(dsp->rds_finfo->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE);
71187c478bd9Sstevel@tonic-gate 
71197c478bd9Sstevel@tonic-gate 	/*
71207c478bd9Sstevel@tonic-gate 	 * New lock owner, create state. Since this was probably called
71217c478bd9Sstevel@tonic-gate 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
71227c478bd9Sstevel@tonic-gate 	 */
71237c478bd9Sstevel@tonic-gate 
71247c478bd9Sstevel@tonic-gate 	ASSERT(cs->vp != NULL);
71257c478bd9Sstevel@tonic-gate 	VN_RELE(cs->vp);
7126d216dff5SRobert Mastors 	VN_HOLD(dsp->rds_finfo->rf_vp);
7127d216dff5SRobert Mastors 	cs->vp = dsp->rds_finfo->rf_vp;
71287c478bd9Sstevel@tonic-gate 
71297c478bd9Sstevel@tonic-gate 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
71307c478bd9Sstevel@tonic-gate 		rfs4_deleg_state_rele(dsp);
71317c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = puterrno4(error);
71327c478bd9Sstevel@tonic-gate 		return;
71337c478bd9Sstevel@tonic-gate 	}
71347c478bd9Sstevel@tonic-gate 
71357c478bd9Sstevel@tonic-gate 	/* Mark progress for delegation returns */
7136d216dff5SRobert Mastors 	dsp->rds_finfo->rf_dinfo.rd_time_lastwrite = gethrestime_sec();
71377c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
71387c478bd9Sstevel@tonic-gate 	rfs4_do_open(cs, req, oo, DELEG_NONE,
7139da6c28aaSamw 	    args->share_access, args->share_deny, resp, 1);
71407c478bd9Sstevel@tonic-gate }
71417c478bd9Sstevel@tonic-gate 
71427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
71437c478bd9Sstevel@tonic-gate static void
71447c478bd9Sstevel@tonic-gate rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
71457c478bd9Sstevel@tonic-gate     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
71467c478bd9Sstevel@tonic-gate {
71477c478bd9Sstevel@tonic-gate 	/*
71487c478bd9Sstevel@tonic-gate 	 * Lookup the pathname, it must already exist since this file
71497c478bd9Sstevel@tonic-gate 	 * was delegated.
71507c478bd9Sstevel@tonic-gate 	 *
71517c478bd9Sstevel@tonic-gate 	 * Find the file and state info for this vp and open owner pair.
71527c478bd9Sstevel@tonic-gate 	 *	check that they are in fact delegated.
71537c478bd9Sstevel@tonic-gate 	 *	check that the state access and deny modes are the same.
71547c478bd9Sstevel@tonic-gate 	 *
71557c478bd9Sstevel@tonic-gate 	 * Return the delgation possibly seting the recall flag.
71567c478bd9Sstevel@tonic-gate 	 */
7157d216dff5SRobert Mastors 	rfs4_file_t *fp;
7158d216dff5SRobert Mastors 	rfs4_state_t *sp;
71597c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
71607c478bd9Sstevel@tonic-gate 	bool_t dcreate = FALSE;
71617c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_t *dsp;
71627c478bd9Sstevel@tonic-gate 	nfsace4 *ace;
71637c478bd9Sstevel@tonic-gate 
71647c478bd9Sstevel@tonic-gate 	/* Note we ignore oflags */
71657c478bd9Sstevel@tonic-gate 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
71667c478bd9Sstevel@tonic-gate 	    req, cs, args->share_access, &resp->cinfo);
71677c478bd9Sstevel@tonic-gate 
71687c478bd9Sstevel@tonic-gate 	if (resp->status != NFS4_OK) {
71697c478bd9Sstevel@tonic-gate 		return;
71707c478bd9Sstevel@tonic-gate 	}
71717c478bd9Sstevel@tonic-gate 
71727c478bd9Sstevel@tonic-gate 	/* get the file struct and hold a lock on it during initial open */
7173d216dff5SRobert Mastors 	fp = rfs4_findfile_withlock(cs->vp, NULL, &create);
7174d216dff5SRobert Mastors 	if (fp == NULL) {
7175f6cf9e50SRick Mesta 		resp->status = NFS4ERR_RESOURCE;
7176f6cf9e50SRick Mesta 		DTRACE_PROBE1(nfss__e__do_opendelprev1, nfsstat4, resp->status);
71777c478bd9Sstevel@tonic-gate 		return;
71787c478bd9Sstevel@tonic-gate 	}
71797c478bd9Sstevel@tonic-gate 
7180d216dff5SRobert Mastors 	sp = rfs4_findstate_by_owner_file(oo, fp, &create);
7181d216dff5SRobert Mastors 	if (sp == NULL) {
71827c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
7183f6cf9e50SRick Mesta 		DTRACE_PROBE1(nfss__e__do_opendelprev2, nfsstat4, resp->status);
7184d216dff5SRobert Mastors 		rw_exit(&fp->rf_file_rwlock);
7185d216dff5SRobert Mastors 		rfs4_file_rele(fp);
71867c478bd9Sstevel@tonic-gate 		return;
71877c478bd9Sstevel@tonic-gate 	}
71887c478bd9Sstevel@tonic-gate 
7189d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
7190d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
7191d216dff5SRobert Mastors 	if (args->share_access != sp->rs_share_access ||
7192d216dff5SRobert Mastors 	    args->share_deny != sp->rs_share_deny ||
7193d216dff5SRobert Mastors 	    sp->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
71947c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
71957c478bd9Sstevel@tonic-gate 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
7196d216dff5SRobert Mastors 		rfs4_dbe_unlock(fp->rf_dbe);
7197d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
7198d216dff5SRobert Mastors 		rfs4_file_rele(fp);
7199d216dff5SRobert Mastors 		rfs4_state_rele(sp);
72007c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
72017c478bd9Sstevel@tonic-gate 		return;
72027c478bd9Sstevel@tonic-gate 	}
7203d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
7204d216dff5SRobert Mastors 	rfs4_dbe_unlock(sp->rs_dbe);
72057c478bd9Sstevel@tonic-gate 
7206d216dff5SRobert Mastors 	dsp = rfs4_finddeleg(sp, &dcreate);
72077c478bd9Sstevel@tonic-gate 	if (dsp == NULL) {
7208d216dff5SRobert Mastors 		rfs4_state_rele(sp);
7209d216dff5SRobert Mastors 		rfs4_file_rele(fp);
72107c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
72117c478bd9Sstevel@tonic-gate 		return;
72127c478bd9Sstevel@tonic-gate 	}
72137c478bd9Sstevel@tonic-gate 
7214d216dff5SRobert Mastors 	next_stateid(&sp->rs_stateid);
72157c478bd9Sstevel@tonic-gate 
7216d216dff5SRobert Mastors 	resp->stateid = sp->rs_stateid.stateid;
72177c478bd9Sstevel@tonic-gate 
7218d216dff5SRobert Mastors 	resp->delegation.delegation_type = dsp->rds_dtype;
72197c478bd9Sstevel@tonic-gate 
7220d216dff5SRobert Mastors 	if (dsp->rds_dtype == OPEN_DELEGATE_READ) {
72217c478bd9Sstevel@tonic-gate 		open_read_delegation4 *rv =
72227c478bd9Sstevel@tonic-gate 		    &resp->delegation.open_delegation4_u.read;
72237c478bd9Sstevel@tonic-gate 
7224d216dff5SRobert Mastors 		rv->stateid = dsp->rds_delegid.stateid;
72257c478bd9Sstevel@tonic-gate 		rv->recall = FALSE; /* no policy in place to set to TRUE */
72267c478bd9Sstevel@tonic-gate 		ace = &rv->permissions;
72277c478bd9Sstevel@tonic-gate 	} else {
72287c478bd9Sstevel@tonic-gate 		open_write_delegation4 *rv =
72297c478bd9Sstevel@tonic-gate 		    &resp->delegation.open_delegation4_u.write;
72307c478bd9Sstevel@tonic-gate 
7231d216dff5SRobert Mastors 		rv->stateid = dsp->rds_delegid.stateid;
72327c478bd9Sstevel@tonic-gate 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
72337c478bd9Sstevel@tonic-gate 		ace = &rv->permissions;
72347c478bd9Sstevel@tonic-gate 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
72357c478bd9Sstevel@tonic-gate 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
72367c478bd9Sstevel@tonic-gate 	}
72377c478bd9Sstevel@tonic-gate 
72387c478bd9Sstevel@tonic-gate 	/* XXX For now */
72397c478bd9Sstevel@tonic-gate 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
72407c478bd9Sstevel@tonic-gate 	ace->flag = 0;
72417c478bd9Sstevel@tonic-gate 	ace->access_mask = 0;
72427c478bd9Sstevel@tonic-gate 	ace->who.utf8string_len = 0;
72437c478bd9Sstevel@tonic-gate 	ace->who.utf8string_val = 0;
72447c478bd9Sstevel@tonic-gate 
72457c478bd9Sstevel@tonic-gate 	rfs4_deleg_state_rele(dsp);
7246d216dff5SRobert Mastors 	rfs4_state_rele(sp);
7247d216dff5SRobert Mastors 	rfs4_file_rele(fp);
72487c478bd9Sstevel@tonic-gate }
72497c478bd9Sstevel@tonic-gate 
72507c478bd9Sstevel@tonic-gate typedef enum {
72517c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_OKAY = 0,
72527c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_REPLAY = 1,
72537c478bd9Sstevel@tonic-gate 	NFS4_CHKSEQ_BAD = 2
72547c478bd9Sstevel@tonic-gate } rfs4_chkseq_t;
72557c478bd9Sstevel@tonic-gate 
72567c478bd9Sstevel@tonic-gate /*
72577c478bd9Sstevel@tonic-gate  * Generic function for sequence number checks.
72587c478bd9Sstevel@tonic-gate  */
72597c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
72607c478bd9Sstevel@tonic-gate rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
72617c478bd9Sstevel@tonic-gate     seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
72627c478bd9Sstevel@tonic-gate {
72637c478bd9Sstevel@tonic-gate 	/* Same sequence ids and matching operations? */
72647c478bd9Sstevel@tonic-gate 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
72657c478bd9Sstevel@tonic-gate 		if (copyres == TRUE) {
72667c478bd9Sstevel@tonic-gate 			rfs4_free_reply(resop);
72677c478bd9Sstevel@tonic-gate 			rfs4_copy_reply(resop, lastop);
72687c478bd9Sstevel@tonic-gate 		}
72697c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
72707c478bd9Sstevel@tonic-gate 		    "Replayed SEQID %d\n", seqid));
72717c478bd9Sstevel@tonic-gate 		return (NFS4_CHKSEQ_REPLAY);
72727c478bd9Sstevel@tonic-gate 	}
72737c478bd9Sstevel@tonic-gate 
72747c478bd9Sstevel@tonic-gate 	/* If the incoming sequence is not the next expected then it is bad */
72757c478bd9Sstevel@tonic-gate 	if (rqst_seq != seqid + 1) {
72767c478bd9Sstevel@tonic-gate 		if (rqst_seq == seqid) {
72777c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
72787c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
72797c478bd9Sstevel@tonic-gate 			    "but last op was %d current op is %d\n",
72807c478bd9Sstevel@tonic-gate 			    lastop->resop, resop->resop));
72817c478bd9Sstevel@tonic-gate 			return (NFS4_CHKSEQ_BAD);
72827c478bd9Sstevel@tonic-gate 		}
72837c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
72847c478bd9Sstevel@tonic-gate 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
72857c478bd9Sstevel@tonic-gate 		    rqst_seq, seqid));
72867c478bd9Sstevel@tonic-gate 		return (NFS4_CHKSEQ_BAD);
72877c478bd9Sstevel@tonic-gate 	}
72887c478bd9Sstevel@tonic-gate 
72897c478bd9Sstevel@tonic-gate 	/* Everything okay -- next expected */
72907c478bd9Sstevel@tonic-gate 	return (NFS4_CHKSEQ_OKAY);
72917c478bd9Sstevel@tonic-gate }
72927c478bd9Sstevel@tonic-gate 
72937c478bd9Sstevel@tonic-gate 
72947c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
72957c478bd9Sstevel@tonic-gate rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
72967c478bd9Sstevel@tonic-gate {
72977c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc;
72987c478bd9Sstevel@tonic-gate 
7299d216dff5SRobert Mastors 	rfs4_dbe_lock(op->ro_dbe);
7300d216dff5SRobert Mastors 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply, seqid, resop,
7301d216dff5SRobert Mastors 	    TRUE);
7302d216dff5SRobert Mastors 	rfs4_dbe_unlock(op->ro_dbe);
73037c478bd9Sstevel@tonic-gate 
73047c478bd9Sstevel@tonic-gate 	if (rc == NFS4_CHKSEQ_OKAY)
7305d216dff5SRobert Mastors 		rfs4_update_lease(op->ro_client);
73067c478bd9Sstevel@tonic-gate 
73077c478bd9Sstevel@tonic-gate 	return (rc);
73087c478bd9Sstevel@tonic-gate }
73097c478bd9Sstevel@tonic-gate 
73107c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
7311d216dff5SRobert Mastors rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
73127c478bd9Sstevel@tonic-gate {
73137c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc;
73147c478bd9Sstevel@tonic-gate 
7315d216dff5SRobert Mastors 	rfs4_dbe_lock(op->ro_dbe);
7316d216dff5SRobert Mastors 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply,
73177c478bd9Sstevel@tonic-gate 	    olo_seqid, resop, FALSE);
7318d216dff5SRobert Mastors 	rfs4_dbe_unlock(op->ro_dbe);
73197c478bd9Sstevel@tonic-gate 
73207c478bd9Sstevel@tonic-gate 	return (rc);
73217c478bd9Sstevel@tonic-gate }
73227c478bd9Sstevel@tonic-gate 
73237c478bd9Sstevel@tonic-gate static rfs4_chkseq_t
7324d216dff5SRobert Mastors rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lsp, nfs_resop4 *resop)
73257c478bd9Sstevel@tonic-gate {
73267c478bd9Sstevel@tonic-gate 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
73277c478bd9Sstevel@tonic-gate 
7328d216dff5SRobert Mastors 	rfs4_dbe_lock(lsp->rls_dbe);
7329d216dff5SRobert Mastors 	if (!lsp->rls_skip_seqid_check)
7330d216dff5SRobert Mastors 		rc = rfs4_check_seqid(lsp->rls_seqid, &lsp->rls_reply, seqid,
7331d216dff5SRobert Mastors 		    resop, TRUE);
7332d216dff5SRobert Mastors 	rfs4_dbe_unlock(lsp->rls_dbe);
73337c478bd9Sstevel@tonic-gate 
73347c478bd9Sstevel@tonic-gate 	return (rc);
73357c478bd9Sstevel@tonic-gate }
73367c478bd9Sstevel@tonic-gate 
73377c478bd9Sstevel@tonic-gate static void
73387c478bd9Sstevel@tonic-gate rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
73397c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
73407c478bd9Sstevel@tonic-gate {
73417c478bd9Sstevel@tonic-gate 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
73427c478bd9Sstevel@tonic-gate 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
73437c478bd9Sstevel@tonic-gate 	open_owner4 *owner = &args->owner;
73447c478bd9Sstevel@tonic-gate 	open_claim_type4 claim = args->claim;
73457c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
73467c478bd9Sstevel@tonic-gate 	rfs4_openowner_t *oo;
73477c478bd9Sstevel@tonic-gate 	bool_t create;
73487c478bd9Sstevel@tonic-gate 	bool_t replay = FALSE;
73497c478bd9Sstevel@tonic-gate 	int can_reclaim;
73507c478bd9Sstevel@tonic-gate 
7351f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
7352f3b585ceSsamf 	    OPEN4args *, args);
73537c478bd9Sstevel@tonic-gate 
73547c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
73557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7356f3b585ceSsamf 		goto end;
73577c478bd9Sstevel@tonic-gate 	}
73587c478bd9Sstevel@tonic-gate 
73597c478bd9Sstevel@tonic-gate 	/*
73607c478bd9Sstevel@tonic-gate 	 * Need to check clientid and lease expiration first based on
73617c478bd9Sstevel@tonic-gate 	 * error ordering and incrementing sequence id.
73627c478bd9Sstevel@tonic-gate 	 */
73637c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
73647c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
73657c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
73667c478bd9Sstevel@tonic-gate 		    rfs4_check_clientid(&owner->clientid, 0);
7367f3b585ceSsamf 		goto end;
73687c478bd9Sstevel@tonic-gate 	}
73697c478bd9Sstevel@tonic-gate 
73707c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
73717c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
73727c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7373f3b585ceSsamf 		goto end;
73747c478bd9Sstevel@tonic-gate 	}
7375d216dff5SRobert Mastors 	can_reclaim = cp->rc_can_reclaim;
73767c478bd9Sstevel@tonic-gate 
73777c478bd9Sstevel@tonic-gate 	/*
73787c478bd9Sstevel@tonic-gate 	 * Find the open_owner for use from this point forward.  Take
73797c478bd9Sstevel@tonic-gate 	 * care in updating the sequence id based on the type of error
73807c478bd9Sstevel@tonic-gate 	 * being returned.
73817c478bd9Sstevel@tonic-gate 	 */
73827c478bd9Sstevel@tonic-gate retry:
73837c478bd9Sstevel@tonic-gate 	create = TRUE;
73847c478bd9Sstevel@tonic-gate 	oo = rfs4_findopenowner(owner, &create, args->seqid);
73857c478bd9Sstevel@tonic-gate 	if (oo == NULL) {
738611bb729eSMarcel Telka 		*cs->statusp = resp->status = NFS4ERR_RESOURCE;
73877c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
7388f3b585ceSsamf 		goto end;
73897c478bd9Sstevel@tonic-gate 	}
73907c478bd9Sstevel@tonic-gate 
73917c478bd9Sstevel@tonic-gate 	/* Hold off access to the sequence space while the open is done */
7392d216dff5SRobert Mastors 	rfs4_sw_enter(&oo->ro_sw);
73937c478bd9Sstevel@tonic-gate 
73947c478bd9Sstevel@tonic-gate 	/*
73957c478bd9Sstevel@tonic-gate 	 * If the open_owner existed before at the server, then check
73967c478bd9Sstevel@tonic-gate 	 * the sequence id.
73977c478bd9Sstevel@tonic-gate 	 */
7398d216dff5SRobert Mastors 	if (!create && !oo->ro_postpone_confirm) {
73997c478bd9Sstevel@tonic-gate 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
74007c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
7401d216dff5SRobert Mastors 			if ((args->seqid > oo->ro_open_seqid) &&
7402d216dff5SRobert Mastors 			    oo->ro_need_confirm) {
74037c478bd9Sstevel@tonic-gate 				rfs4_free_opens(oo, TRUE, FALSE);
7404d216dff5SRobert Mastors 				rfs4_sw_exit(&oo->ro_sw);
74057c478bd9Sstevel@tonic-gate 				rfs4_openowner_rele(oo);
74067c478bd9Sstevel@tonic-gate 				goto retry;
74077c478bd9Sstevel@tonic-gate 			}
74087c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_BAD_SEQID;
74097c478bd9Sstevel@tonic-gate 			goto out;
74107c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
74117c478bd9Sstevel@tonic-gate 			replay = TRUE;
74127c478bd9Sstevel@tonic-gate 			goto out;
74137c478bd9Sstevel@tonic-gate 		default:
74147c478bd9Sstevel@tonic-gate 			break;
74157c478bd9Sstevel@tonic-gate 		}
74167c478bd9Sstevel@tonic-gate 
74177c478bd9Sstevel@tonic-gate 		/*
74187c478bd9Sstevel@tonic-gate 		 * Sequence was ok and open owner exists
74197c478bd9Sstevel@tonic-gate 		 * check to see if we have yet to see an
74207c478bd9Sstevel@tonic-gate 		 * open_confirm.
74217c478bd9Sstevel@tonic-gate 		 */
7422d216dff5SRobert Mastors 		if (oo->ro_need_confirm) {
74237c478bd9Sstevel@tonic-gate 			rfs4_free_opens(oo, TRUE, FALSE);
7424d216dff5SRobert Mastors 			rfs4_sw_exit(&oo->ro_sw);
74257c478bd9Sstevel@tonic-gate 			rfs4_openowner_rele(oo);
74267c478bd9Sstevel@tonic-gate 			goto retry;
74277c478bd9Sstevel@tonic-gate 		}
74287c478bd9Sstevel@tonic-gate 	}
74297c478bd9Sstevel@tonic-gate 	/* Grace only applies to regular-type OPENs */
74307c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) &&
74317c478bd9Sstevel@tonic-gate 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
74327c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_GRACE;
74337c478bd9Sstevel@tonic-gate 		goto out;
74347c478bd9Sstevel@tonic-gate 	}
74357c478bd9Sstevel@tonic-gate 
74367c478bd9Sstevel@tonic-gate 	/*
74377c478bd9Sstevel@tonic-gate 	 * If previous state at the server existed then can_reclaim
74387c478bd9Sstevel@tonic-gate 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
74397c478bd9Sstevel@tonic-gate 	 * client.
74407c478bd9Sstevel@tonic-gate 	 */
74417c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
74427c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
74437c478bd9Sstevel@tonic-gate 		goto out;
74447c478bd9Sstevel@tonic-gate 	}
74457c478bd9Sstevel@tonic-gate 
74467c478bd9Sstevel@tonic-gate 
74477c478bd9Sstevel@tonic-gate 	/*
74487c478bd9Sstevel@tonic-gate 	 * Reject the open if the client has missed the grace period
74497c478bd9Sstevel@tonic-gate 	 */
74507c478bd9Sstevel@tonic-gate 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
74517c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
74527c478bd9Sstevel@tonic-gate 		goto out;
74537c478bd9Sstevel@tonic-gate 	}
74547c478bd9Sstevel@tonic-gate 
74557c478bd9Sstevel@tonic-gate 	/* Couple of up-front bookkeeping items */
7456d216dff5SRobert Mastors 	if (oo->ro_need_confirm) {
74577c478bd9Sstevel@tonic-gate 		/*
74587c478bd9Sstevel@tonic-gate 		 * If this is a reclaim OPEN then we should not ask
74597c478bd9Sstevel@tonic-gate 		 * for a confirmation of the open_owner per the
74607c478bd9Sstevel@tonic-gate 		 * protocol specification.
74617c478bd9Sstevel@tonic-gate 		 */
74627c478bd9Sstevel@tonic-gate 		if (claim == CLAIM_PREVIOUS)
7463d216dff5SRobert Mastors 			oo->ro_need_confirm = FALSE;
74647c478bd9Sstevel@tonic-gate 		else
74657c478bd9Sstevel@tonic-gate 			resp->rflags |= OPEN4_RESULT_CONFIRM;
74667c478bd9Sstevel@tonic-gate 	}
74677c478bd9Sstevel@tonic-gate 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
74687c478bd9Sstevel@tonic-gate 
74697c478bd9Sstevel@tonic-gate 	/*
74707c478bd9Sstevel@tonic-gate 	 * If there is an unshared filesystem mounted on this vnode,
74717c478bd9Sstevel@tonic-gate 	 * do not allow to open/create in this directory.
74727c478bd9Sstevel@tonic-gate 	 */
74737c478bd9Sstevel@tonic-gate 	if (vn_ismntpt(cs->vp)) {
74747c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
74757c478bd9Sstevel@tonic-gate 		goto out;
74767c478bd9Sstevel@tonic-gate 	}
74777c478bd9Sstevel@tonic-gate 
74787c478bd9Sstevel@tonic-gate 	/*
74797c478bd9Sstevel@tonic-gate 	 * access must READ, WRITE, or BOTH.  No access is invalid.
74807c478bd9Sstevel@tonic-gate 	 * deny can be READ, WRITE, BOTH, or NONE.
74817c478bd9Sstevel@tonic-gate 	 * bits not defined for access/deny are invalid.
74827c478bd9Sstevel@tonic-gate 	 */
74837c478bd9Sstevel@tonic-gate 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
74847c478bd9Sstevel@tonic-gate 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
74857c478bd9Sstevel@tonic-gate 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
74867c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
74877c478bd9Sstevel@tonic-gate 		goto out;
74887c478bd9Sstevel@tonic-gate 	}
74897c478bd9Sstevel@tonic-gate 
74907c478bd9Sstevel@tonic-gate 
74917c478bd9Sstevel@tonic-gate 	/*
74927c478bd9Sstevel@tonic-gate 	 * make sure attrset is zero before response is built.
74937c478bd9Sstevel@tonic-gate 	 */
74947c478bd9Sstevel@tonic-gate 	resp->attrset = 0;
74957c478bd9Sstevel@tonic-gate 
74967c478bd9Sstevel@tonic-gate 	switch (claim) {
74977c478bd9Sstevel@tonic-gate 	case CLAIM_NULL:
74987c478bd9Sstevel@tonic-gate 		rfs4_do_opennull(cs, req, args, oo, resp);
74997c478bd9Sstevel@tonic-gate 		break;
75007c478bd9Sstevel@tonic-gate 	case CLAIM_PREVIOUS:
75017c478bd9Sstevel@tonic-gate 		rfs4_do_openprev(cs, req, args, oo, resp);
75027c478bd9Sstevel@tonic-gate 		break;
75037c478bd9Sstevel@tonic-gate 	case CLAIM_DELEGATE_CUR:
75047c478bd9Sstevel@tonic-gate 		rfs4_do_opendelcur(cs, req, args, oo, resp);
75057c478bd9Sstevel@tonic-gate 		break;
75067c478bd9Sstevel@tonic-gate 	case CLAIM_DELEGATE_PREV:
75077c478bd9Sstevel@tonic-gate 		rfs4_do_opendelprev(cs, req, args, oo, resp);
75087c478bd9Sstevel@tonic-gate 		break;
75097c478bd9Sstevel@tonic-gate 	default:
75107c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
75117c478bd9Sstevel@tonic-gate 		break;
75127c478bd9Sstevel@tonic-gate 	}
75137c478bd9Sstevel@tonic-gate 
75147c478bd9Sstevel@tonic-gate out:
75157c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
75167c478bd9Sstevel@tonic-gate 
75177c478bd9Sstevel@tonic-gate 	/* Catch sequence id handling here to make it a little easier */
75187c478bd9Sstevel@tonic-gate 	switch (resp->status) {
75197c478bd9Sstevel@tonic-gate 	case NFS4ERR_BADXDR:
75207c478bd9Sstevel@tonic-gate 	case NFS4ERR_BAD_SEQID:
75217c478bd9Sstevel@tonic-gate 	case NFS4ERR_BAD_STATEID:
75227c478bd9Sstevel@tonic-gate 	case NFS4ERR_NOFILEHANDLE:
75237c478bd9Sstevel@tonic-gate 	case NFS4ERR_RESOURCE:
75247c478bd9Sstevel@tonic-gate 	case NFS4ERR_STALE_CLIENTID:
75257c478bd9Sstevel@tonic-gate 	case NFS4ERR_STALE_STATEID:
75267c478bd9Sstevel@tonic-gate 		/*
75277c478bd9Sstevel@tonic-gate 		 * The protocol states that if any of these errors are
75287c478bd9Sstevel@tonic-gate 		 * being returned, the sequence id should not be
75297c478bd9Sstevel@tonic-gate 		 * incremented.  Any other return requires an
75307c478bd9Sstevel@tonic-gate 		 * increment.
75317c478bd9Sstevel@tonic-gate 		 */
75327c478bd9Sstevel@tonic-gate 		break;
75337c478bd9Sstevel@tonic-gate 	default:
75347c478bd9Sstevel@tonic-gate 		/* Always update the lease in this case */
7535d216dff5SRobert Mastors 		rfs4_update_lease(oo->ro_client);
75367c478bd9Sstevel@tonic-gate 
75377c478bd9Sstevel@tonic-gate 		/* Regular response - copy the result */
75387c478bd9Sstevel@tonic-gate 		if (!replay)
75397c478bd9Sstevel@tonic-gate 			rfs4_update_open_resp(oo, resop, &cs->fh);
75407c478bd9Sstevel@tonic-gate 
75417c478bd9Sstevel@tonic-gate 		/*
75427c478bd9Sstevel@tonic-gate 		 * REPLAY case: Only if the previous response was OK
75437c478bd9Sstevel@tonic-gate 		 * do we copy the filehandle.  If not OK, no
75447c478bd9Sstevel@tonic-gate 		 * filehandle to copy.
75457c478bd9Sstevel@tonic-gate 		 */
75467c478bd9Sstevel@tonic-gate 		if (replay == TRUE &&
75477c478bd9Sstevel@tonic-gate 		    resp->status == NFS4_OK &&
7548d216dff5SRobert Mastors 		    oo->ro_reply_fh.nfs_fh4_val) {
75497c478bd9Sstevel@tonic-gate 			/*
75507c478bd9Sstevel@tonic-gate 			 * If this is a replay, we must restore the
75517c478bd9Sstevel@tonic-gate 			 * current filehandle/vp to that of what was
75527c478bd9Sstevel@tonic-gate 			 * returned originally.  Try our best to do
75537c478bd9Sstevel@tonic-gate 			 * it.
75547c478bd9Sstevel@tonic-gate 			 */
75557c478bd9Sstevel@tonic-gate 			nfs_fh4_fmt_t *fh_fmtp =
7556d216dff5SRobert Mastors 			    (nfs_fh4_fmt_t *)oo->ro_reply_fh.nfs_fh4_val;
75577c478bd9Sstevel@tonic-gate 
7558aafcd32bSMarcel Telka 			if (cs->exi != NULL)
7559fbd2e8e1SArne Jansen 				exi_rele(cs->exi);
7560fbd2e8e1SArne Jansen 			cs->exi = checkexport(&fh_fmtp->fh4_fsid,
75617c478bd9Sstevel@tonic-gate 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
75627c478bd9Sstevel@tonic-gate 
75637c478bd9Sstevel@tonic-gate 			if (cs->exi == NULL) {
75647c478bd9Sstevel@tonic-gate 				resp->status = NFS4ERR_STALE;
75657c478bd9Sstevel@tonic-gate 				goto finish;
75667c478bd9Sstevel@tonic-gate 			}
75677c478bd9Sstevel@tonic-gate 
75687c478bd9Sstevel@tonic-gate 			VN_RELE(cs->vp);
75697c478bd9Sstevel@tonic-gate 
7570d216dff5SRobert Mastors 			cs->vp = nfs4_fhtovp(&oo->ro_reply_fh, cs->exi,
75717c478bd9Sstevel@tonic-gate 			    &resp->status);
75727c478bd9Sstevel@tonic-gate 
75737c478bd9Sstevel@tonic-gate 			if (cs->vp == NULL)
75747c478bd9Sstevel@tonic-gate 				goto finish;
75757c478bd9Sstevel@tonic-gate 
7576d216dff5SRobert Mastors 			nfs_fh4_copy(&oo->ro_reply_fh, &cs->fh);
75777c478bd9Sstevel@tonic-gate 		}
75787c478bd9Sstevel@tonic-gate 
75797c478bd9Sstevel@tonic-gate 		/*
75807c478bd9Sstevel@tonic-gate 		 * If this was a replay, no need to update the
75817c478bd9Sstevel@tonic-gate 		 * sequence id. If the open_owner was not created on
75827c478bd9Sstevel@tonic-gate 		 * this pass, then update.  The first use of an
75837c478bd9Sstevel@tonic-gate 		 * open_owner will not bump the sequence id.
75847c478bd9Sstevel@tonic-gate 		 */
75857c478bd9Sstevel@tonic-gate 		if (replay == FALSE && !create)
75867c478bd9Sstevel@tonic-gate 			rfs4_update_open_sequence(oo);
75877c478bd9Sstevel@tonic-gate 		/*
75887c478bd9Sstevel@tonic-gate 		 * If the client is receiving an error and the
75897c478bd9Sstevel@tonic-gate 		 * open_owner needs to be confirmed, there is no way
75907c478bd9Sstevel@tonic-gate 		 * to notify the client of this fact ignoring the fact
75917c478bd9Sstevel@tonic-gate 		 * that the server has no method of returning a
75927c478bd9Sstevel@tonic-gate 		 * stateid to confirm.  Therefore, the server needs to
75937c478bd9Sstevel@tonic-gate 		 * mark this open_owner in a way as to avoid the
75947c478bd9Sstevel@tonic-gate 		 * sequence id checking the next time the client uses
75957c478bd9Sstevel@tonic-gate 		 * this open_owner.
75967c478bd9Sstevel@tonic-gate 		 */
7597d216dff5SRobert Mastors 		if (resp->status != NFS4_OK && oo->ro_need_confirm)
7598d216dff5SRobert Mastors 			oo->ro_postpone_confirm = TRUE;
75997c478bd9Sstevel@tonic-gate 		/*
76007c478bd9Sstevel@tonic-gate 		 * If OK response then clear the postpone flag and
76017c478bd9Sstevel@tonic-gate 		 * reset the sequence id to keep in sync with the
76027c478bd9Sstevel@tonic-gate 		 * client.
76037c478bd9Sstevel@tonic-gate 		 */
7604d216dff5SRobert Mastors 		if (resp->status == NFS4_OK && oo->ro_postpone_confirm) {
7605d216dff5SRobert Mastors 			oo->ro_postpone_confirm = FALSE;
7606d216dff5SRobert Mastors 			oo->ro_open_seqid = args->seqid;
76077c478bd9Sstevel@tonic-gate 		}
76087c478bd9Sstevel@tonic-gate 		break;
76097c478bd9Sstevel@tonic-gate 	}
76107c478bd9Sstevel@tonic-gate 
76117c478bd9Sstevel@tonic-gate finish:
76127c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
76137c478bd9Sstevel@tonic-gate 
7614d216dff5SRobert Mastors 	rfs4_sw_exit(&oo->ro_sw);
76157c478bd9Sstevel@tonic-gate 	rfs4_openowner_rele(oo);
7616f3b585ceSsamf 
7617f3b585ceSsamf end:
7618f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7619f3b585ceSsamf 	    OPEN4res *, resp);
76207c478bd9Sstevel@tonic-gate }
76217c478bd9Sstevel@tonic-gate 
76227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
76237c478bd9Sstevel@tonic-gate void
76247c478bd9Sstevel@tonic-gate rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
76257c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
76267c478bd9Sstevel@tonic-gate {
76277c478bd9Sstevel@tonic-gate 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
76287c478bd9Sstevel@tonic-gate 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
76297c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
76307c478bd9Sstevel@tonic-gate 	nfsstat4 status;
76317c478bd9Sstevel@tonic-gate 
7632f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7633f3b585ceSsamf 	    OPEN_CONFIRM4args *, args);
7634f3b585ceSsamf 
76357c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
76367c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7637f3b585ceSsamf 		goto out;
76387c478bd9Sstevel@tonic-gate 	}
76397c478bd9Sstevel@tonic-gate 
7640447cd349SDaniil Lunev 	if (cs->vp->v_type != VREG) {
7641447cd349SDaniil Lunev 		*cs->statusp = resp->status =
7642447cd349SDaniil Lunev 		    cs->vp->v_type == VDIR ? NFS4ERR_ISDIR : NFS4ERR_INVAL;
7643447cd349SDaniil Lunev 		return;
7644447cd349SDaniil Lunev 	}
7645447cd349SDaniil Lunev 
76467c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
76477c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
76487c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
7649f3b585ceSsamf 		goto out;
76507c478bd9Sstevel@tonic-gate 	}
76517c478bd9Sstevel@tonic-gate 
76527c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
7653d216dff5SRobert Mastors 	if (cs->vp != sp->rs_finfo->rf_vp) {
76547c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
76557c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7656f3b585ceSsamf 		goto out;
76577c478bd9Sstevel@tonic-gate 	}
76587c478bd9Sstevel@tonic-gate 
76597c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
7660d216dff5SRobert Mastors 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
76617c478bd9Sstevel@tonic-gate 
76627c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
76637c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
7664d216dff5SRobert Mastors 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
76657c478bd9Sstevel@tonic-gate 		    resop) != 0) {
76667c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
76677c478bd9Sstevel@tonic-gate 			break;
76687c478bd9Sstevel@tonic-gate 		}
76697c478bd9Sstevel@tonic-gate 		/*
76707c478bd9Sstevel@tonic-gate 		 * If it is the appropriate stateid and determined to
76717c478bd9Sstevel@tonic-gate 		 * be "OKAY" then this means that the stateid does not
76727c478bd9Sstevel@tonic-gate 		 * need to be confirmed and the client is in error for
76737c478bd9Sstevel@tonic-gate 		 * sending an OPEN_CONFIRM.
76747c478bd9Sstevel@tonic-gate 		 */
76757c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
76767c478bd9Sstevel@tonic-gate 		break;
76777c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
76787c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
76797c478bd9Sstevel@tonic-gate 		break;
76807c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
76817c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
76827c478bd9Sstevel@tonic-gate 		break;
76837c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
76847c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
76857c478bd9Sstevel@tonic-gate 		break;
76867c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
76877c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
76887c478bd9Sstevel@tonic-gate 		break;
76897c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
7690d216dff5SRobert Mastors 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7691d216dff5SRobert Mastors 		    resop)) {
76927c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
76937c478bd9Sstevel@tonic-gate 			/*
76947c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
76957c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
76967c478bd9Sstevel@tonic-gate 			 */
76977c478bd9Sstevel@tonic-gate 			/* fall through */
76987c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
76997c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
77007c478bd9Sstevel@tonic-gate 			break;
77017c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
77027c478bd9Sstevel@tonic-gate 			/*
77037c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
77047c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
77057c478bd9Sstevel@tonic-gate 			 */
77067c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
7707d216dff5SRobert Mastors 			rfs4_update_lease(sp->rs_owner->ro_client);
77087c478bd9Sstevel@tonic-gate 			break;
77097c478bd9Sstevel@tonic-gate 		}
77107c478bd9Sstevel@tonic-gate 		break;
77117c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7712d216dff5SRobert Mastors 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
77137c478bd9Sstevel@tonic-gate 		    resop) != NFS4_CHKSEQ_OKAY) {
77147c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
77157c478bd9Sstevel@tonic-gate 			break;
77167c478bd9Sstevel@tonic-gate 		}
77177c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4_OK;
77187c478bd9Sstevel@tonic-gate 
7719d216dff5SRobert Mastors 		next_stateid(&sp->rs_stateid);
7720d216dff5SRobert Mastors 		resp->open_stateid = sp->rs_stateid.stateid;
7721d216dff5SRobert Mastors 		sp->rs_owner->ro_need_confirm = FALSE;
7722d216dff5SRobert Mastors 		rfs4_update_lease(sp->rs_owner->ro_client);
7723d216dff5SRobert Mastors 		rfs4_update_open_sequence(sp->rs_owner);
7724d216dff5SRobert Mastors 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
77257c478bd9Sstevel@tonic-gate 		break;
77267c478bd9Sstevel@tonic-gate 	default:
77277c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
77287c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
77297c478bd9Sstevel@tonic-gate 		break;
77307c478bd9Sstevel@tonic-gate 	}
7731d216dff5SRobert Mastors 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
77327c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
7733f3b585ceSsamf 
7734f3b585ceSsamf out:
7735f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7736f3b585ceSsamf 	    OPEN_CONFIRM4res *, resp);
77377c478bd9Sstevel@tonic-gate }
77387c478bd9Sstevel@tonic-gate 
77397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
77407c478bd9Sstevel@tonic-gate void
77417c478bd9Sstevel@tonic-gate rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
77427c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
77437c478bd9Sstevel@tonic-gate {
77447c478bd9Sstevel@tonic-gate 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
77457c478bd9Sstevel@tonic-gate 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
77467c478bd9Sstevel@tonic-gate 	uint32_t access = args->share_access;
77477c478bd9Sstevel@tonic-gate 	uint32_t deny = args->share_deny;
77487c478bd9Sstevel@tonic-gate 	nfsstat4 status;
77497c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
77507c478bd9Sstevel@tonic-gate 	rfs4_file_t *fp;
7751da6c28aaSamw 	int fflags = 0;
77527c478bd9Sstevel@tonic-gate 
7753f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7754f3b585ceSsamf 	    OPEN_DOWNGRADE4args *, args);
7755f3b585ceSsamf 
77567c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
77577c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7758f3b585ceSsamf 		goto out;
77597c478bd9Sstevel@tonic-gate 	}
77607c478bd9Sstevel@tonic-gate 
776121feb70aSDaniil Lunev 	if (cs->vp->v_type != VREG) {
776221feb70aSDaniil Lunev 		*cs->statusp = resp->status = NFS4ERR_INVAL;
776321feb70aSDaniil Lunev 		return;
776421feb70aSDaniil Lunev 	}
776521feb70aSDaniil Lunev 
77667c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
77677c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
77687c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
7769f3b585ceSsamf 		goto out;
77707c478bd9Sstevel@tonic-gate 	}
77717c478bd9Sstevel@tonic-gate 
77727c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
7773d216dff5SRobert Mastors 	if (cs->vp != sp->rs_finfo->rf_vp) {
77747c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
77757c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7776f3b585ceSsamf 		goto out;
77777c478bd9Sstevel@tonic-gate 	}
77787c478bd9Sstevel@tonic-gate 
77797c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
7780d216dff5SRobert Mastors 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
77817c478bd9Sstevel@tonic-gate 
77827c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
77837c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
7784d216dff5SRobert Mastors 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
77857c478bd9Sstevel@tonic-gate 		    resop) != NFS4_CHKSEQ_OKAY) {
77867c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
77877c478bd9Sstevel@tonic-gate 			goto end;
77887c478bd9Sstevel@tonic-gate 		}
77897c478bd9Sstevel@tonic-gate 		break;
77907c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
77917c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
77927c478bd9Sstevel@tonic-gate 		goto end;
77937c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
77947c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
77957c478bd9Sstevel@tonic-gate 		goto end;
77967c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
77977c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
77987c478bd9Sstevel@tonic-gate 		goto end;
77997c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
78007c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
78017c478bd9Sstevel@tonic-gate 		goto end;
78027c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
78037c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
78047c478bd9Sstevel@tonic-gate 		goto end;
78057c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
78067c478bd9Sstevel@tonic-gate 		/* Check the sequence id for the open owner */
7807d216dff5SRobert Mastors 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7808d216dff5SRobert Mastors 		    resop)) {
78097c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
78107c478bd9Sstevel@tonic-gate 			/*
78117c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
78127c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
78137c478bd9Sstevel@tonic-gate 			 */
78147c478bd9Sstevel@tonic-gate 			/* fall through */
78157c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
78167c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
78177c478bd9Sstevel@tonic-gate 			goto end;
78187c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
78197c478bd9Sstevel@tonic-gate 			/*
78207c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
78217c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
78227c478bd9Sstevel@tonic-gate 			 */
78237c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
7824d216dff5SRobert Mastors 			rfs4_update_lease(sp->rs_owner->ro_client);
78257c478bd9Sstevel@tonic-gate 			goto end;
78267c478bd9Sstevel@tonic-gate 		}
78277c478bd9Sstevel@tonic-gate 		break;
78287c478bd9Sstevel@tonic-gate 	default:
78297c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
78307c478bd9Sstevel@tonic-gate 		break;
78317c478bd9Sstevel@tonic-gate 	}
78327c478bd9Sstevel@tonic-gate 
7833d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
78347c478bd9Sstevel@tonic-gate 	/*
78357c478bd9Sstevel@tonic-gate 	 * Check that the new access modes and deny modes are valid.
78367c478bd9Sstevel@tonic-gate 	 * Check that no invalid bits are set.
78377c478bd9Sstevel@tonic-gate 	 */
78387c478bd9Sstevel@tonic-gate 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7839da6c28aaSamw 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
78407c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7841d216dff5SRobert Mastors 		rfs4_update_open_sequence(sp->rs_owner);
7842d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
78437c478bd9Sstevel@tonic-gate 		goto end;
78447c478bd9Sstevel@tonic-gate 	}
78457c478bd9Sstevel@tonic-gate 
78467c478bd9Sstevel@tonic-gate 	/*
78477c478bd9Sstevel@tonic-gate 	 * The new modes must be a subset of the current modes and
78487c478bd9Sstevel@tonic-gate 	 * the access must specify at least one mode. To test that
78497c478bd9Sstevel@tonic-gate 	 * the new mode is a subset of the current modes we bitwise
78507c478bd9Sstevel@tonic-gate 	 * AND them together and check that the result equals the new
78517c478bd9Sstevel@tonic-gate 	 * mode. For example:
785250956b22SJames Wahlig 	 * New mode, access == R and current mode, sp->rs_open_access  == RW
785350956b22SJames Wahlig 	 * access & sp->rs_open_access == R == access, so the new access mode
785450956b22SJames Wahlig 	 * is valid. Consider access == RW, sp->rs_open_access = R
785550956b22SJames Wahlig 	 * access & sp->rs_open_access == R != access, so the new access mode
78567c478bd9Sstevel@tonic-gate 	 * is invalid.
78577c478bd9Sstevel@tonic-gate 	 */
785850956b22SJames Wahlig 	if ((access & sp->rs_open_access) != access ||
785950956b22SJames Wahlig 	    (deny & sp->rs_open_deny) != deny ||
78607c478bd9Sstevel@tonic-gate 	    (access &
78617c478bd9Sstevel@tonic-gate 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
78627c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7863d216dff5SRobert Mastors 		rfs4_update_open_sequence(sp->rs_owner);
7864d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
78657c478bd9Sstevel@tonic-gate 		goto end;
78667c478bd9Sstevel@tonic-gate 	}
78677c478bd9Sstevel@tonic-gate 
78687c478bd9Sstevel@tonic-gate 	/*
78697c478bd9Sstevel@tonic-gate 	 * Release any share locks associated with this stateID.
78707c478bd9Sstevel@tonic-gate 	 * Strictly speaking, this violates the spec because the
78717c478bd9Sstevel@tonic-gate 	 * spec effectively requires that open downgrade be atomic.
78727c478bd9Sstevel@tonic-gate 	 * At present, fs_shrlock does not have this capability.
78737c478bd9Sstevel@tonic-gate 	 */
7874d216dff5SRobert Mastors 	(void) rfs4_unshare(sp);
78757c478bd9Sstevel@tonic-gate 
787650956b22SJames Wahlig 	status = rfs4_share(sp, access, deny);
787750956b22SJames Wahlig 	if (status != NFS4_OK) {
787850956b22SJames Wahlig 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
787950956b22SJames Wahlig 		rfs4_update_open_sequence(sp->rs_owner);
788050956b22SJames Wahlig 		rfs4_dbe_unlock(sp->rs_dbe);
788150956b22SJames Wahlig 		goto end;
788250956b22SJames Wahlig 	}
788350956b22SJames Wahlig 
7884d216dff5SRobert Mastors 	fp = sp->rs_finfo;
7885d216dff5SRobert Mastors 	rfs4_dbe_lock(fp->rf_dbe);
78867c478bd9Sstevel@tonic-gate 
78877c478bd9Sstevel@tonic-gate 	/*
78887c478bd9Sstevel@tonic-gate 	 * If the current mode has deny read and the new mode
78897c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of deny read mode bits
78907c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the deny read bit
78917c478bd9Sstevel@tonic-gate 	 * on the file.
78927c478bd9Sstevel@tonic-gate 	 */
789350956b22SJames Wahlig 	if ((sp->rs_open_deny & OPEN4_SHARE_DENY_READ) &&
78947c478bd9Sstevel@tonic-gate 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7895d216dff5SRobert Mastors 		fp->rf_deny_read--;
7896d216dff5SRobert Mastors 		if (fp->rf_deny_read == 0)
7897d216dff5SRobert Mastors 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
78987c478bd9Sstevel@tonic-gate 	}
78997c478bd9Sstevel@tonic-gate 
79007c478bd9Sstevel@tonic-gate 	/*
79017c478bd9Sstevel@tonic-gate 	 * If the current mode has deny write and the new mode
79027c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of deny write mode bits
79037c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the deny write bit
79047c478bd9Sstevel@tonic-gate 	 * on the file.
79057c478bd9Sstevel@tonic-gate 	 */
790650956b22SJames Wahlig 	if ((sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) &&
79077c478bd9Sstevel@tonic-gate 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7908d216dff5SRobert Mastors 		fp->rf_deny_write--;
7909d216dff5SRobert Mastors 		if (fp->rf_deny_write == 0)
7910d216dff5SRobert Mastors 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
79117c478bd9Sstevel@tonic-gate 	}
79127c478bd9Sstevel@tonic-gate 
79137c478bd9Sstevel@tonic-gate 	/*
79147c478bd9Sstevel@tonic-gate 	 * If the current mode has access read and the new mode
79157c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of access read mode bits
79167c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the access read bit
7917da6c28aaSamw 	 * on the file.  set fflags to FREAD for the call to
7918da6c28aaSamw 	 * vn_open_downgrade().
79197c478bd9Sstevel@tonic-gate 	 */
792050956b22SJames Wahlig 	if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) &&
79217c478bd9Sstevel@tonic-gate 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7922d216dff5SRobert Mastors 		fp->rf_access_read--;
7923d216dff5SRobert Mastors 		if (fp->rf_access_read == 0)
7924d216dff5SRobert Mastors 			fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
7925da6c28aaSamw 		fflags |= FREAD;
79267c478bd9Sstevel@tonic-gate 	}
79277c478bd9Sstevel@tonic-gate 
79287c478bd9Sstevel@tonic-gate 	/*
79297c478bd9Sstevel@tonic-gate 	 * If the current mode has access write and the new mode
79307c478bd9Sstevel@tonic-gate 	 * does not, decrement the number of access write mode bits
79317c478bd9Sstevel@tonic-gate 	 * and if it goes to zero turn off the access write bit
7932da6c28aaSamw 	 * on the file.  set fflags to FWRITE for the call to
7933da6c28aaSamw 	 * vn_open_downgrade().
79347c478bd9Sstevel@tonic-gate 	 */
793550956b22SJames Wahlig 	if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) &&
79367c478bd9Sstevel@tonic-gate 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7937d216dff5SRobert Mastors 		fp->rf_access_write--;
7938d216dff5SRobert Mastors 		if (fp->rf_access_write == 0)
7939d216dff5SRobert Mastors 			fp->rf_share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7940da6c28aaSamw 		fflags |= FWRITE;
79417c478bd9Sstevel@tonic-gate 	}
79427c478bd9Sstevel@tonic-gate 
79437c478bd9Sstevel@tonic-gate 	/* Check that the file is still accessible */
7944d216dff5SRobert Mastors 	ASSERT(fp->rf_share_access);
79457c478bd9Sstevel@tonic-gate 
7946d216dff5SRobert Mastors 	rfs4_dbe_unlock(fp->rf_dbe);
79477c478bd9Sstevel@tonic-gate 
794850956b22SJames Wahlig 	/* now set the new open access and deny modes */
794950956b22SJames Wahlig 	sp->rs_open_access = access;
795050956b22SJames Wahlig 	sp->rs_open_deny = deny;
79517c478bd9Sstevel@tonic-gate 
7952da6c28aaSamw 	/*
7953da6c28aaSamw 	 * we successfully downgraded the share lock, now we need to downgrade
7954da6c28aaSamw 	 * the open. it is possible that the downgrade was only for a deny
7955da6c28aaSamw 	 * mode and we have nothing else to do.
7956da6c28aaSamw 	 */
7957da6c28aaSamw 	if ((fflags & (FREAD|FWRITE)) != 0)
7958da6c28aaSamw 		vn_open_downgrade(cs->vp, fflags);
7959da6c28aaSamw 
79607c478bd9Sstevel@tonic-gate 	/* Update the stateid */
7961d216dff5SRobert Mastors 	next_stateid(&sp->rs_stateid);
7962d216dff5SRobert Mastors 	resp->open_stateid = sp->rs_stateid.stateid;
79637c478bd9Sstevel@tonic-gate 
7964d216dff5SRobert Mastors 	rfs4_dbe_unlock(sp->rs_dbe);
79657c478bd9Sstevel@tonic-gate 
79667c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = NFS4_OK;
79677c478bd9Sstevel@tonic-gate 	/* Update the lease */
7968d216dff5SRobert Mastors 	rfs4_update_lease(sp->rs_owner->ro_client);
79697c478bd9Sstevel@tonic-gate 	/* And the sequence */
7970d216dff5SRobert Mastors 	rfs4_update_open_sequence(sp->rs_owner);
7971d216dff5SRobert Mastors 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
79727c478bd9Sstevel@tonic-gate 
79737c478bd9Sstevel@tonic-gate end:
7974d216dff5SRobert Mastors 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
79757c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
7976f3b585ceSsamf out:
7977f3b585ceSsamf 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7978f3b585ceSsamf 	    OPEN_DOWNGRADE4res *, resp);
79797c478bd9Sstevel@tonic-gate }
79807c478bd9Sstevel@tonic-gate 
79817c9adcc5SMarcel Telka static void *
79827c9adcc5SMarcel Telka memstr(const void *s1, const char *s2, size_t n)
79837c9adcc5SMarcel Telka {
79847c9adcc5SMarcel Telka 	size_t l = strlen(s2);
79857c9adcc5SMarcel Telka 	char *p = (char *)s1;
79867c9adcc5SMarcel Telka 
79877c9adcc5SMarcel Telka 	while (n >= l) {
79887c9adcc5SMarcel Telka 		if (bcmp(p, s2, l) == 0)
79897c9adcc5SMarcel Telka 			return (p);
79907c9adcc5SMarcel Telka 		p++;
79917c9adcc5SMarcel Telka 		n--;
79927c9adcc5SMarcel Telka 	}
79937c9adcc5SMarcel Telka 
79947c9adcc5SMarcel Telka 	return (NULL);
79957c9adcc5SMarcel Telka }
79967c9adcc5SMarcel Telka 
79977c478bd9Sstevel@tonic-gate /*
79987c478bd9Sstevel@tonic-gate  * The logic behind this function is detailed in the NFSv4 RFC in the
79997c478bd9Sstevel@tonic-gate  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
80007c478bd9Sstevel@tonic-gate  * that section for explicit guidance to server behavior for
80017c478bd9Sstevel@tonic-gate  * SETCLIENTID.
80027c478bd9Sstevel@tonic-gate  */
80037c478bd9Sstevel@tonic-gate void
80047c478bd9Sstevel@tonic-gate rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
80057c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
80067c478bd9Sstevel@tonic-gate {
80077c478bd9Sstevel@tonic-gate 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
80087c478bd9Sstevel@tonic-gate 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
80097c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
80102f172c55SRobert Thurlow 	rfs4_clntip_t *ci;
80112f172c55SRobert Thurlow 	bool_t create;
80127c478bd9Sstevel@tonic-gate 	char *addr, *netid;
80137c478bd9Sstevel@tonic-gate 	int len;
80147c478bd9Sstevel@tonic-gate 
8015f3b585ceSsamf 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
8016f3b585ceSsamf 	    SETCLIENTID4args *, args);
80177c478bd9Sstevel@tonic-gate retry:
80187c478bd9Sstevel@tonic-gate 	newcp = cp_confirmed = cp_unconfirmed = NULL;
80197c478bd9Sstevel@tonic-gate 
80207c478bd9Sstevel@tonic-gate 	/*
80212f172c55SRobert Thurlow 	 * Save the caller's IP address
80222f172c55SRobert Thurlow 	 */
80232f172c55SRobert Thurlow 	args->client.cl_addr =
80242f172c55SRobert Thurlow 	    (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
80252f172c55SRobert Thurlow 
80262f172c55SRobert Thurlow 	/*
80272f172c55SRobert Thurlow 	 * Record if it is a Solaris client that cannot handle referrals.
80282f172c55SRobert Thurlow 	 */
80297c9adcc5SMarcel Telka 	if (memstr(args->client.id_val, "Solaris", args->client.id_len) &&
80307c9adcc5SMarcel Telka 	    !memstr(args->client.id_val, "+referrals", args->client.id_len)) {
80312f172c55SRobert Thurlow 		/* Add a "yes, it's downrev" record */
80322f172c55SRobert Thurlow 		create = TRUE;
80332f172c55SRobert Thurlow 		ci = rfs4_find_clntip(args->client.cl_addr, &create);
80342f172c55SRobert Thurlow 		ASSERT(ci != NULL);
80352f172c55SRobert Thurlow 		rfs4_dbe_rele(ci->ri_dbe);
80362f172c55SRobert Thurlow 	} else {
80372f172c55SRobert Thurlow 		/* Remove any previous record */
80382f172c55SRobert Thurlow 		rfs4_invalidate_clntip(args->client.cl_addr);
80392f172c55SRobert Thurlow 	}
80402f172c55SRobert Thurlow 
80412f172c55SRobert Thurlow 	/*
80427c478bd9Sstevel@tonic-gate 	 * In search of an EXISTING client matching the incoming
80437c478bd9Sstevel@tonic-gate 	 * request to establish a new client identifier at the server
80447c478bd9Sstevel@tonic-gate 	 */
80457c478bd9Sstevel@tonic-gate 	create = TRUE;
80467c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient(&args->client, &create, NULL);
80477c478bd9Sstevel@tonic-gate 
80487c478bd9Sstevel@tonic-gate 	/* Should never happen */
80497c478bd9Sstevel@tonic-gate 	ASSERT(cp != NULL);
80507c478bd9Sstevel@tonic-gate 
80517c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
80527c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8053f3b585ceSsamf 		goto out;
80547c478bd9Sstevel@tonic-gate 	}
80557c478bd9Sstevel@tonic-gate 
80567c478bd9Sstevel@tonic-gate 	/*
80577c478bd9Sstevel@tonic-gate 	 * Easiest case. Client identifier is newly created and is
80587c478bd9Sstevel@tonic-gate 	 * unconfirmed.  Also note that for this case, no other
80597c478bd9Sstevel@tonic-gate 	 * entries exist for the client identifier.  Nothing else to
80607c478bd9Sstevel@tonic-gate 	 * check.  Just setup the response and respond.
80617c478bd9Sstevel@tonic-gate 	 */
80627c478bd9Sstevel@tonic-gate 	if (create) {
80637c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4_OK;
8064d216dff5SRobert Mastors 		res->SETCLIENTID4res_u.resok4.clientid = cp->rc_clientid;
80657c478bd9Sstevel@tonic-gate 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8066d216dff5SRobert Mastors 		    cp->rc_confirm_verf;
80677c478bd9Sstevel@tonic-gate 		/* Setup callback information; CB_NULL confirmation later */
80687c478bd9Sstevel@tonic-gate 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
80697c478bd9Sstevel@tonic-gate 
80707c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
8071f3b585ceSsamf 		goto out;
80727c478bd9Sstevel@tonic-gate 	}
80737c478bd9Sstevel@tonic-gate 
80747c478bd9Sstevel@tonic-gate 	/*
80757c478bd9Sstevel@tonic-gate 	 * An existing, confirmed client may exist but it may not have
80767c478bd9Sstevel@tonic-gate 	 * been active for at least one lease period.  If so, then
80777c478bd9Sstevel@tonic-gate 	 * "close" the client and create a new client identifier
80787c478bd9Sstevel@tonic-gate 	 */
80797c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
80807c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
80817c478bd9Sstevel@tonic-gate 		goto retry;
80827c478bd9Sstevel@tonic-gate 	}
80837c478bd9Sstevel@tonic-gate 
8084d216dff5SRobert Mastors 	if (cp->rc_need_confirm == TRUE)
80857c478bd9Sstevel@tonic-gate 		cp_unconfirmed = cp;
80867c478bd9Sstevel@tonic-gate 	else
80877c478bd9Sstevel@tonic-gate 		cp_confirmed = cp;
80887c478bd9Sstevel@tonic-gate 
80897c478bd9Sstevel@tonic-gate 	cp = NULL;
80907c478bd9Sstevel@tonic-gate 
80917c478bd9Sstevel@tonic-gate 	/*
80927c478bd9Sstevel@tonic-gate 	 * We have a confirmed client, now check for an
80937c478bd9Sstevel@tonic-gate 	 * unconfimred entry
80947c478bd9Sstevel@tonic-gate 	 */
80957c478bd9Sstevel@tonic-gate 	if (cp_confirmed) {
80967c478bd9Sstevel@tonic-gate 		/* If creds don't match then client identifier is inuse */
8097d216dff5SRobert Mastors 		if (!creds_ok(cp_confirmed->rc_cr_set, req, cs)) {
80987c478bd9Sstevel@tonic-gate 			rfs4_cbinfo_t *cbp;
80997c478bd9Sstevel@tonic-gate 			/*
81007c478bd9Sstevel@tonic-gate 			 * Some one else has established this client
81017c478bd9Sstevel@tonic-gate 			 * id. Try and say * who they are. We will use
81027c478bd9Sstevel@tonic-gate 			 * the call back address supplied by * the
81037c478bd9Sstevel@tonic-gate 			 * first client.
81047c478bd9Sstevel@tonic-gate 			 */
81057c478bd9Sstevel@tonic-gate 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
81067c478bd9Sstevel@tonic-gate 
81077c478bd9Sstevel@tonic-gate 			addr = netid = NULL;
81087c478bd9Sstevel@tonic-gate 
8109d216dff5SRobert Mastors 			cbp = &cp_confirmed->rc_cbinfo;
81107c478bd9Sstevel@tonic-gate 			if (cbp->cb_callback.cb_location.r_addr &&
81117c478bd9Sstevel@tonic-gate 			    cbp->cb_callback.cb_location.r_netid) {
81127c478bd9Sstevel@tonic-gate 				cb_client4 *cbcp = &cbp->cb_callback;
81137c478bd9Sstevel@tonic-gate 
81147c478bd9Sstevel@tonic-gate 				len = strlen(cbcp->cb_location.r_addr)+1;
81157c478bd9Sstevel@tonic-gate 				addr = kmem_alloc(len, KM_SLEEP);
81167c478bd9Sstevel@tonic-gate 				bcopy(cbcp->cb_location.r_addr, addr, len);
81177c478bd9Sstevel@tonic-gate 				len = strlen(cbcp->cb_location.r_netid)+1;
81187c478bd9Sstevel@tonic-gate 				netid = kmem_alloc(len, KM_SLEEP);
81197c478bd9Sstevel@tonic-gate 				bcopy(cbcp->cb_location.r_netid, netid, len);
81207c478bd9Sstevel@tonic-gate 			}
81217c478bd9Sstevel@tonic-gate 
81227c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
81237c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
81247c478bd9Sstevel@tonic-gate 
81257c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
81267c478bd9Sstevel@tonic-gate 		}
81277c478bd9Sstevel@tonic-gate 
81287c478bd9Sstevel@tonic-gate 		/*
81297c478bd9Sstevel@tonic-gate 		 * Confirmed, creds match, and verifier matches; must
81307c478bd9Sstevel@tonic-gate 		 * be an update of the callback info
81317c478bd9Sstevel@tonic-gate 		 */
8132d216dff5SRobert Mastors 		if (cp_confirmed->rc_nfs_client.verifier ==
81337c478bd9Sstevel@tonic-gate 		    args->client.verifier) {
81347c478bd9Sstevel@tonic-gate 			/* Setup callback information */
81357c478bd9Sstevel@tonic-gate 			rfs4_client_setcb(cp_confirmed, &args->callback,
81367c478bd9Sstevel@tonic-gate 			    args->callback_ident);
81377c478bd9Sstevel@tonic-gate 
81387c478bd9Sstevel@tonic-gate 			/* everything okay -- move ahead */
81397c478bd9Sstevel@tonic-gate 			*cs->statusp = res->status = NFS4_OK;
81407c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.resok4.clientid =
8141d216dff5SRobert Mastors 			    cp_confirmed->rc_clientid;
81427c478bd9Sstevel@tonic-gate 
81437c478bd9Sstevel@tonic-gate 			/* update the confirm_verifier and return it */
81447c478bd9Sstevel@tonic-gate 			rfs4_client_scv_next(cp_confirmed);
81457c478bd9Sstevel@tonic-gate 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8146d216dff5SRobert Mastors 			    cp_confirmed->rc_confirm_verf;
81477c478bd9Sstevel@tonic-gate 
81487c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
8149f3b585ceSsamf 			goto out;
81507c478bd9Sstevel@tonic-gate 		}
81517c478bd9Sstevel@tonic-gate 
81527c478bd9Sstevel@tonic-gate 		/*
81537c478bd9Sstevel@tonic-gate 		 * Creds match but the verifier doesn't.  Must search
81547c478bd9Sstevel@tonic-gate 		 * for an unconfirmed client that would be replaced by
81557c478bd9Sstevel@tonic-gate 		 * this request.
81567c478bd9Sstevel@tonic-gate 		 */
81577c478bd9Sstevel@tonic-gate 		create = FALSE;
81587c478bd9Sstevel@tonic-gate 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
81597c478bd9Sstevel@tonic-gate 		    cp_confirmed);
81607c478bd9Sstevel@tonic-gate 	}
81617c478bd9Sstevel@tonic-gate 
81627c478bd9Sstevel@tonic-gate 	/*
81637c478bd9Sstevel@tonic-gate 	 * At this point, we have taken care of the brand new client
81647c478bd9Sstevel@tonic-gate 	 * struct, INUSE case, update of an existing, and confirmed
81657c478bd9Sstevel@tonic-gate 	 * client struct.
81667c478bd9Sstevel@tonic-gate 	 */
81677c478bd9Sstevel@tonic-gate 
81687c478bd9Sstevel@tonic-gate 	/*
81697c478bd9Sstevel@tonic-gate 	 * check to see if things have changed while we originally
81707c478bd9Sstevel@tonic-gate 	 * picked up the client struct.  If they have, then return and
81717c478bd9Sstevel@tonic-gate 	 * retry the processing of this SETCLIENTID request.
81727c478bd9Sstevel@tonic-gate 	 */
81737c478bd9Sstevel@tonic-gate 	if (cp_unconfirmed) {
8174d216dff5SRobert Mastors 		rfs4_dbe_lock(cp_unconfirmed->rc_dbe);
8175d216dff5SRobert Mastors 		if (!cp_unconfirmed->rc_need_confirm) {
8176d216dff5SRobert Mastors 			rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
81777c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_unconfirmed);
81787c478bd9Sstevel@tonic-gate 			if (cp_confirmed)
81797c478bd9Sstevel@tonic-gate 				rfs4_client_rele(cp_confirmed);
81807c478bd9Sstevel@tonic-gate 			goto retry;
81817c478bd9Sstevel@tonic-gate 		}
81827c478bd9Sstevel@tonic-gate 		/* do away with the old unconfirmed one */
8183d216dff5SRobert Mastors 		rfs4_dbe_invalidate(cp_unconfirmed->rc_dbe);
8184d216dff5SRobert Mastors 		rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
81857c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp_unconfirmed);
81867c478bd9Sstevel@tonic-gate 		cp_unconfirmed = NULL;
81877c478bd9Sstevel@tonic-gate 	}
81887c478bd9Sstevel@tonic-gate 
81897c478bd9Sstevel@tonic-gate 	/*
81907c478bd9Sstevel@tonic-gate 	 * This search will temporarily hide the confirmed client
81917c478bd9Sstevel@tonic-gate 	 * struct while a new client struct is created as the
81927c478bd9Sstevel@tonic-gate 	 * unconfirmed one.
81937c478bd9Sstevel@tonic-gate 	 */
81947c478bd9Sstevel@tonic-gate 	create = TRUE;
81957c478bd9Sstevel@tonic-gate 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
81967c478bd9Sstevel@tonic-gate 
81977c478bd9Sstevel@tonic-gate 	ASSERT(newcp != NULL);
81987c478bd9Sstevel@tonic-gate 
81997c478bd9Sstevel@tonic-gate 	if (newcp == NULL) {
82007c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
82017c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp_confirmed);
8202f3b585ceSsamf 		goto out;
82037c478bd9Sstevel@tonic-gate 	}
82047c478bd9Sstevel@tonic-gate 
82057c478bd9Sstevel@tonic-gate 	/*
82067c478bd9Sstevel@tonic-gate 	 * If one was not created, then a similar request must be in
82077c478bd9Sstevel@tonic-gate 	 * process so release and start over with this one
82087c478bd9Sstevel@tonic-gate 	 */
82097c478bd9Sstevel@tonic-gate 	if (create != TRUE) {
82107c478bd9Sstevel@tonic-gate 		rfs4_client_rele(newcp);
82117c478bd9Sstevel@tonic-gate 		if (cp_confirmed)
82127c478bd9Sstevel@tonic-gate 			rfs4_client_rele(cp_confirmed);
82137c478bd9Sstevel@tonic-gate 		goto retry;
82147c478bd9Sstevel@tonic-gate 	}
82157c478bd9Sstevel@tonic-gate 
82167c478bd9Sstevel@tonic-gate 	*cs->statusp = res->status = NFS4_OK;
8217d216dff5SRobert Mastors 	res->SETCLIENTID4res_u.resok4.clientid = newcp->rc_clientid;
8218d216dff5SRobert Mastors 	res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8219d216dff5SRobert Mastors 	    newcp->rc_confirm_verf;
82207c478bd9Sstevel@tonic-gate 	/* Setup callback information; CB_NULL confirmation later */
82211b300de9Sjwahlig 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
82227c478bd9Sstevel@tonic-gate 
8223d216dff5SRobert Mastors 	newcp->rc_cp_confirmed = cp_confirmed;
82247c478bd9Sstevel@tonic-gate 
82257c478bd9Sstevel@tonic-gate 	rfs4_client_rele(newcp);
8226f3b585ceSsamf 
8227f3b585ceSsamf out:
8228f3b585ceSsamf 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
8229f3b585ceSsamf 	    SETCLIENTID4res *, res);
82307c478bd9Sstevel@tonic-gate }
82317c478bd9Sstevel@tonic-gate 
82327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
82337c478bd9Sstevel@tonic-gate void
82347c478bd9Sstevel@tonic-gate rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
82357c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
82367c478bd9Sstevel@tonic-gate {
82377c478bd9Sstevel@tonic-gate 	SETCLIENTID_CONFIRM4args *args =
82387c478bd9Sstevel@tonic-gate 	    &argop->nfs_argop4_u.opsetclientid_confirm;
82397c478bd9Sstevel@tonic-gate 	SETCLIENTID_CONFIRM4res *res =
82407c478bd9Sstevel@tonic-gate 	    &resop->nfs_resop4_u.opsetclientid_confirm;
82417c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp, *cptoclose = NULL;
82427c478bd9Sstevel@tonic-gate 
8243f3b585ceSsamf 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
8244f3b585ceSsamf 	    struct compound_state *, cs,
8245f3b585ceSsamf 	    SETCLIENTID_CONFIRM4args *, args);
8246f3b585ceSsamf 
82477c478bd9Sstevel@tonic-gate 	*cs->statusp = res->status = NFS4_OK;
82487c478bd9Sstevel@tonic-gate 
82497c478bd9Sstevel@tonic-gate 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
82507c478bd9Sstevel@tonic-gate 
82517c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
82527c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status =
82537c478bd9Sstevel@tonic-gate 		    rfs4_check_clientid(&args->clientid, 1);
8254f3b585ceSsamf 		goto out;
82557c478bd9Sstevel@tonic-gate 	}
82567c478bd9Sstevel@tonic-gate 
82577c478bd9Sstevel@tonic-gate 	if (!creds_ok(cp, req, cs)) {
82587c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
82597c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
8260f3b585ceSsamf 		goto out;
82617c478bd9Sstevel@tonic-gate 	}
82627c478bd9Sstevel@tonic-gate 
82637c478bd9Sstevel@tonic-gate 	/* If the verifier doesn't match, the record doesn't match */
8264d216dff5SRobert Mastors 	if (cp->rc_confirm_verf != args->setclientid_confirm) {
82657c478bd9Sstevel@tonic-gate 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
82667c478bd9Sstevel@tonic-gate 		rfs4_client_rele(cp);
8267f3b585ceSsamf 		goto out;
82687c478bd9Sstevel@tonic-gate 	}
82697c478bd9Sstevel@tonic-gate 
8270d216dff5SRobert Mastors 	rfs4_dbe_lock(cp->rc_dbe);
8271d216dff5SRobert Mastors 	cp->rc_need_confirm = FALSE;
8272d216dff5SRobert Mastors 	if (cp->rc_cp_confirmed) {
8273d216dff5SRobert Mastors 		cptoclose = cp->rc_cp_confirmed;
8274d216dff5SRobert Mastors 		cptoclose->rc_ss_remove = 1;
8275d216dff5SRobert Mastors 		cp->rc_cp_confirmed = NULL;
82767c478bd9Sstevel@tonic-gate 	}
82777c478bd9Sstevel@tonic-gate 
82787c478bd9Sstevel@tonic-gate 	/*
8279cee86682Scalum 	 * Update the client's associated server instance, if it's changed
8280cee86682Scalum 	 * since the client was created.
8281cee86682Scalum 	 */
8282cee86682Scalum 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
8283cee86682Scalum 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
8284cee86682Scalum 
8285cee86682Scalum 	/*
8286cee86682Scalum 	 * Record clientid in stable storage.
8287cee86682Scalum 	 * Must be done after server instance has been assigned.
82887c478bd9Sstevel@tonic-gate 	 */
82892f172c55SRobert Thurlow 	rfs4_ss_clid(cp);
82907c478bd9Sstevel@tonic-gate 
8291d216dff5SRobert Mastors 	rfs4_dbe_unlock(cp->rc_dbe);
82927c478bd9Sstevel@tonic-gate 
82937c478bd9Sstevel@tonic-gate 	if (cptoclose)
82947c478bd9Sstevel@tonic-gate 		/* don't need to rele, client_close does it */
82957c478bd9Sstevel@tonic-gate 		rfs4_client_close(cptoclose);
82967c478bd9Sstevel@tonic-gate 
82977c478bd9Sstevel@tonic-gate 	/* If needed, initiate CB_NULL call for callback path */
82987c478bd9Sstevel@tonic-gate 	rfs4_deleg_cb_check(cp);
82997c478bd9Sstevel@tonic-gate 	rfs4_update_lease(cp);
83007c478bd9Sstevel@tonic-gate 
83017c478bd9Sstevel@tonic-gate 	/*
83027c478bd9Sstevel@tonic-gate 	 * Check to see if client can perform reclaims
83037c478bd9Sstevel@tonic-gate 	 */
83047c478bd9Sstevel@tonic-gate 	rfs4_ss_chkclid(cp);
83057c478bd9Sstevel@tonic-gate 
83067c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
8307f3b585ceSsamf 
8308f3b585ceSsamf out:
8309f3b585ceSsamf 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
8310f3b585ceSsamf 	    struct compound_state *, cs,
8311f3b585ceSsamf 	    SETCLIENTID_CONFIRM4 *, res);
83127c478bd9Sstevel@tonic-gate }
83137c478bd9Sstevel@tonic-gate 
83147c478bd9Sstevel@tonic-gate 
83157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
83167c478bd9Sstevel@tonic-gate void
83177c478bd9Sstevel@tonic-gate rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
83187c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
83197c478bd9Sstevel@tonic-gate {
83207c478bd9Sstevel@tonic-gate 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
83217c478bd9Sstevel@tonic-gate 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
83227c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp;
83237c478bd9Sstevel@tonic-gate 	nfsstat4 status;
83247c478bd9Sstevel@tonic-gate 
8325f3b585ceSsamf 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
8326f3b585ceSsamf 	    CLOSE4args *, args);
8327f3b585ceSsamf 
83287c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
83297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8330f3b585ceSsamf 		goto out;
83317c478bd9Sstevel@tonic-gate 	}
83327c478bd9Sstevel@tonic-gate 
83337c478bd9Sstevel@tonic-gate 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
83347c478bd9Sstevel@tonic-gate 	if (status != NFS4_OK) {
83357c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
8336f3b585ceSsamf 		goto out;
83377c478bd9Sstevel@tonic-gate 	}
83387c478bd9Sstevel@tonic-gate 
83397c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
8340d216dff5SRobert Mastors 	if (cs->vp != sp->rs_finfo->rf_vp) {
83417c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
83427c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8343f3b585ceSsamf 		goto out;
83447c478bd9Sstevel@tonic-gate 	}
83457c478bd9Sstevel@tonic-gate 
83467c478bd9Sstevel@tonic-gate 	/* hold off other access to open_owner while we tinker */
8347d216dff5SRobert Mastors 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
83487c478bd9Sstevel@tonic-gate 
83497c478bd9Sstevel@tonic-gate 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
83507c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
8351d216dff5SRobert Mastors 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
83527c478bd9Sstevel@tonic-gate 		    resop) != NFS4_CHKSEQ_OKAY) {
83537c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
83547c478bd9Sstevel@tonic-gate 			goto end;
83557c478bd9Sstevel@tonic-gate 		}
83567c478bd9Sstevel@tonic-gate 		break;
83577c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
83587c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
83597c478bd9Sstevel@tonic-gate 		goto end;
83607c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
83617c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
83627c478bd9Sstevel@tonic-gate 		goto end;
83637c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
83647c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
83657c478bd9Sstevel@tonic-gate 		goto end;
83667c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
83677c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
83687c478bd9Sstevel@tonic-gate 		goto end;
83697c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_UNCONFIRMED:
83707c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
83717c478bd9Sstevel@tonic-gate 		goto end;
83727c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
83737c478bd9Sstevel@tonic-gate 		/* Check the sequence id for the open owner */
8374d216dff5SRobert Mastors 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8375d216dff5SRobert Mastors 		    resop)) {
83767c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
83777c478bd9Sstevel@tonic-gate 			/*
83787c478bd9Sstevel@tonic-gate 			 * This is replayed stateid; if seqid matches
83797c478bd9Sstevel@tonic-gate 			 * next expected, then client is using wrong seqid.
83807c478bd9Sstevel@tonic-gate 			 */
83817c478bd9Sstevel@tonic-gate 			/* FALL THROUGH */
83827c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
83837c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
83847c478bd9Sstevel@tonic-gate 			goto end;
83857c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
83867c478bd9Sstevel@tonic-gate 			/*
83877c478bd9Sstevel@tonic-gate 			 * Note this case is the duplicate case so
83887c478bd9Sstevel@tonic-gate 			 * resp->status is already set.
83897c478bd9Sstevel@tonic-gate 			 */
83907c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status;
8391d216dff5SRobert Mastors 			rfs4_update_lease(sp->rs_owner->ro_client);
83927c478bd9Sstevel@tonic-gate 			goto end;
83937c478bd9Sstevel@tonic-gate 		}
83947c478bd9Sstevel@tonic-gate 		break;
83957c478bd9Sstevel@tonic-gate 	default:
83967c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
83977c478bd9Sstevel@tonic-gate 		break;
83987c478bd9Sstevel@tonic-gate 	}
83997c478bd9Sstevel@tonic-gate 
8400d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
84017c478bd9Sstevel@tonic-gate 
84027c478bd9Sstevel@tonic-gate 	/* Update the stateid. */
8403d216dff5SRobert Mastors 	next_stateid(&sp->rs_stateid);
8404d216dff5SRobert Mastors 	resp->open_stateid = sp->rs_stateid.stateid;
84057c478bd9Sstevel@tonic-gate 
8406d216dff5SRobert Mastors 	rfs4_dbe_unlock(sp->rs_dbe);
84077c478bd9Sstevel@tonic-gate 
8408d216dff5SRobert Mastors 	rfs4_update_lease(sp->rs_owner->ro_client);
8409d216dff5SRobert Mastors 	rfs4_update_open_sequence(sp->rs_owner);
8410d216dff5SRobert Mastors 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
84117c478bd9Sstevel@tonic-gate 
84127c478bd9Sstevel@tonic-gate 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
84137c478bd9Sstevel@tonic-gate 
84147c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
84157c478bd9Sstevel@tonic-gate 
84167c478bd9Sstevel@tonic-gate end:
8417d216dff5SRobert Mastors 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
84187c478bd9Sstevel@tonic-gate 	rfs4_state_rele(sp);
8419f3b585ceSsamf out:
8420f3b585ceSsamf 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
8421f3b585ceSsamf 	    CLOSE4res *, resp);
84227c478bd9Sstevel@tonic-gate }
84237c478bd9Sstevel@tonic-gate 
84247c478bd9Sstevel@tonic-gate /*
84257c478bd9Sstevel@tonic-gate  * Manage the counts on the file struct and close all file locks
84267c478bd9Sstevel@tonic-gate  */
84277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
84287c478bd9Sstevel@tonic-gate void
84297c478bd9Sstevel@tonic-gate rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
84307c478bd9Sstevel@tonic-gate     bool_t close_of_client)
84317c478bd9Sstevel@tonic-gate {
8432d216dff5SRobert Mastors 	rfs4_file_t *fp = sp->rs_finfo;
84337c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
8434d216dff5SRobert Mastors 	int fflags = 0;
84357c478bd9Sstevel@tonic-gate 
84367c478bd9Sstevel@tonic-gate 	/*
84377c478bd9Sstevel@tonic-gate 	 * If this call is part of the larger closing down of client
84387c478bd9Sstevel@tonic-gate 	 * state then it is just easier to release all locks
84397c478bd9Sstevel@tonic-gate 	 * associated with this client instead of going through each
84407c478bd9Sstevel@tonic-gate 	 * individual file and cleaning locks there.
84417c478bd9Sstevel@tonic-gate 	 */
84427c478bd9Sstevel@tonic-gate 	if (close_of_client) {
8443d216dff5SRobert Mastors 		if (sp->rs_owner->ro_client->rc_unlksys_completed == FALSE &&
8444d216dff5SRobert Mastors 		    !list_is_empty(&sp->rs_lostatelist) &&
8445d216dff5SRobert Mastors 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID) {
84467c478bd9Sstevel@tonic-gate 			/* Is the PxFS kernel module loaded? */
84477c478bd9Sstevel@tonic-gate 			if (lm_remove_file_locks != NULL) {
84487c478bd9Sstevel@tonic-gate 				int new_sysid;
84497c478bd9Sstevel@tonic-gate 
84507c478bd9Sstevel@tonic-gate 				/* Encode the cluster nodeid in new sysid */
8451d216dff5SRobert Mastors 				new_sysid = sp->rs_owner->ro_client->rc_sysidt;
84527c478bd9Sstevel@tonic-gate 				lm_set_nlmid_flk(&new_sysid);
84537c478bd9Sstevel@tonic-gate 
84547c478bd9Sstevel@tonic-gate 				/*
84557c478bd9Sstevel@tonic-gate 				 * This PxFS routine removes file locks for a
84567c478bd9Sstevel@tonic-gate 				 * client over all nodes of a cluster.
84577c478bd9Sstevel@tonic-gate 				 */
84587c478bd9Sstevel@tonic-gate 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
84597c478bd9Sstevel@tonic-gate 				    "lm_remove_file_locks(sysid=0x%x)\n",
84607c478bd9Sstevel@tonic-gate 				    new_sysid));
84617c478bd9Sstevel@tonic-gate 				(*lm_remove_file_locks)(new_sysid);
84627c478bd9Sstevel@tonic-gate 			} else {
84637c478bd9Sstevel@tonic-gate 				struct flock64 flk;
84647c478bd9Sstevel@tonic-gate 
84657c478bd9Sstevel@tonic-gate 				/* Release all locks for this client */
84667c478bd9Sstevel@tonic-gate 				flk.l_type = F_UNLKSYS;
84677c478bd9Sstevel@tonic-gate 				flk.l_whence = 0;
84687c478bd9Sstevel@tonic-gate 				flk.l_start = 0;
84697c478bd9Sstevel@tonic-gate 				flk.l_len = 0;
8470d216dff5SRobert Mastors 				flk.l_sysid =
8471d216dff5SRobert Mastors 				    sp->rs_owner->ro_client->rc_sysidt;
84727c478bd9Sstevel@tonic-gate 				flk.l_pid = 0;
8473d216dff5SRobert Mastors 				(void) VOP_FRLOCK(sp->rs_finfo->rf_vp, F_SETLK,
8474d216dff5SRobert Mastors 				    &flk, F_REMOTELOCK | FREAD | FWRITE,
8475da6c28aaSamw 				    (u_offset_t)0, NULL, CRED(), NULL);
84767c478bd9Sstevel@tonic-gate 			}
84777c478bd9Sstevel@tonic-gate 
8478d216dff5SRobert Mastors 			sp->rs_owner->ro_client->rc_unlksys_completed = TRUE;
84797c478bd9Sstevel@tonic-gate 		}
84807c478bd9Sstevel@tonic-gate 	}
84817c478bd9Sstevel@tonic-gate 
84827c478bd9Sstevel@tonic-gate 	/*
84837c478bd9Sstevel@tonic-gate 	 * Release all locks on this file by this lock owner or at
84847c478bd9Sstevel@tonic-gate 	 * least mark the locks as having been released
84857c478bd9Sstevel@tonic-gate 	 */
8486d216dff5SRobert Mastors 	for (lsp = list_head(&sp->rs_lostatelist); lsp != NULL;
8487d216dff5SRobert Mastors 	    lsp = list_next(&sp->rs_lostatelist, lsp)) {
8488d216dff5SRobert Mastors 		lsp->rls_locks_cleaned = TRUE;
84897c478bd9Sstevel@tonic-gate 
84907c478bd9Sstevel@tonic-gate 		/* Was this already taken care of above? */
84917c478bd9Sstevel@tonic-gate 		if (!close_of_client &&
8492d216dff5SRobert Mastors 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8493d216dff5SRobert Mastors 			(void) cleanlocks(sp->rs_finfo->rf_vp,
8494d216dff5SRobert Mastors 			    lsp->rls_locker->rl_pid,
8495d216dff5SRobert Mastors 			    lsp->rls_locker->rl_client->rc_sysidt);
84967c478bd9Sstevel@tonic-gate 	}
84977c478bd9Sstevel@tonic-gate 
84987c478bd9Sstevel@tonic-gate 	/*
84997c478bd9Sstevel@tonic-gate 	 * Release any shrlocks associated with this open state ID.
85007c478bd9Sstevel@tonic-gate 	 * This must be done before the rfs4_state gets marked closed.
85017c478bd9Sstevel@tonic-gate 	 */
8502d216dff5SRobert Mastors 	if (sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8503d216dff5SRobert Mastors 		(void) rfs4_unshare(sp);
8504da6c28aaSamw 
850550956b22SJames Wahlig 	if (sp->rs_open_access) {
850650956b22SJames Wahlig 		rfs4_dbe_lock(fp->rf_dbe);
850750956b22SJames Wahlig 
850814f41b92SRobert Mastors 		/*
850914f41b92SRobert Mastors 		 * Decrement the count for each access and deny bit that this
851014f41b92SRobert Mastors 		 * state has contributed to the file.
851114f41b92SRobert Mastors 		 * If the file counts go to zero
851214f41b92SRobert Mastors 		 * clear the appropriate bit in the appropriate mask.
851314f41b92SRobert Mastors 		 */
851450956b22SJames Wahlig 		if (sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) {
851514f41b92SRobert Mastors 			fp->rf_access_read--;
851614f41b92SRobert Mastors 			fflags |= FREAD;
851714f41b92SRobert Mastors 			if (fp->rf_access_read == 0)
851814f41b92SRobert Mastors 				fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
851914f41b92SRobert Mastors 		}
852050956b22SJames Wahlig 		if (sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) {
852114f41b92SRobert Mastors 			fp->rf_access_write--;
852214f41b92SRobert Mastors 			fflags |= FWRITE;
852314f41b92SRobert Mastors 			if (fp->rf_access_write == 0)
852414f41b92SRobert Mastors 				fp->rf_share_access &=
852514f41b92SRobert Mastors 				    ~OPEN4_SHARE_ACCESS_WRITE;
852614f41b92SRobert Mastors 		}
852750956b22SJames Wahlig 		if (sp->rs_open_deny & OPEN4_SHARE_DENY_READ) {
852814f41b92SRobert Mastors 			fp->rf_deny_read--;
852914f41b92SRobert Mastors 			if (fp->rf_deny_read == 0)
853014f41b92SRobert Mastors 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
853114f41b92SRobert Mastors 		}
853250956b22SJames Wahlig 		if (sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) {
853314f41b92SRobert Mastors 			fp->rf_deny_write--;
853414f41b92SRobert Mastors 			if (fp->rf_deny_write == 0)
853514f41b92SRobert Mastors 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
853614f41b92SRobert Mastors 		}
853714f41b92SRobert Mastors 
8538d216dff5SRobert Mastors 		(void) VOP_CLOSE(fp->rf_vp, fflags, 1, (offset_t)0, cr, NULL);
853914f41b92SRobert Mastors 
854050956b22SJames Wahlig 		rfs4_dbe_unlock(fp->rf_dbe);
854150956b22SJames Wahlig 
854250956b22SJames Wahlig 		sp->rs_open_access = 0;
854350956b22SJames Wahlig 		sp->rs_open_deny = 0;
854414f41b92SRobert Mastors 	}
85457c478bd9Sstevel@tonic-gate }
85467c478bd9Sstevel@tonic-gate 
85477c478bd9Sstevel@tonic-gate /*
85487c478bd9Sstevel@tonic-gate  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
85497c478bd9Sstevel@tonic-gate  */
85507c478bd9Sstevel@tonic-gate static nfsstat4
85517c478bd9Sstevel@tonic-gate lock_denied(LOCK4denied *dp, struct flock64 *flk)
85527c478bd9Sstevel@tonic-gate {
85537c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
85547c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
85557c478bd9Sstevel@tonic-gate 	uint32_t len;
85567c478bd9Sstevel@tonic-gate 
85577c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
85587c478bd9Sstevel@tonic-gate 	if (lo != NULL) {
8559d216dff5SRobert Mastors 		cp = lo->rl_client;
85607c478bd9Sstevel@tonic-gate 		if (rfs4_lease_expired(cp)) {
85617c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
8562d216dff5SRobert Mastors 			rfs4_dbe_hold(cp->rc_dbe);
85637c478bd9Sstevel@tonic-gate 			rfs4_client_close(cp);
85647c478bd9Sstevel@tonic-gate 			return (NFS4ERR_EXPIRED);
85657c478bd9Sstevel@tonic-gate 		}
8566d216dff5SRobert Mastors 		dp->owner.clientid = lo->rl_owner.clientid;
8567d216dff5SRobert Mastors 		len = lo->rl_owner.owner_len;
85687c478bd9Sstevel@tonic-gate 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8569d216dff5SRobert Mastors 		bcopy(lo->rl_owner.owner_val, dp->owner.owner_val, len);
85707c478bd9Sstevel@tonic-gate 		dp->owner.owner_len = len;
85717c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
85727c478bd9Sstevel@tonic-gate 		goto finish;
85737c478bd9Sstevel@tonic-gate 	}
85747c478bd9Sstevel@tonic-gate 
85757c478bd9Sstevel@tonic-gate 	/*
85767c478bd9Sstevel@tonic-gate 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
85777c478bd9Sstevel@tonic-gate 	 * of the client id contain the boot time for a NFS4 lock. So we
85787c478bd9Sstevel@tonic-gate 	 * fabricate and identity by setting clientid to the sysid, and
85797c478bd9Sstevel@tonic-gate 	 * the lock owner to the pid.
85807c478bd9Sstevel@tonic-gate 	 */
85817c478bd9Sstevel@tonic-gate 	dp->owner.clientid = flk->l_sysid;
85827c478bd9Sstevel@tonic-gate 	len = sizeof (pid_t);
85837c478bd9Sstevel@tonic-gate 	dp->owner.owner_len = len;
85847c478bd9Sstevel@tonic-gate 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
85857c478bd9Sstevel@tonic-gate 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
85867c478bd9Sstevel@tonic-gate finish:
85877c478bd9Sstevel@tonic-gate 	dp->offset = flk->l_start;
85887c478bd9Sstevel@tonic-gate 	dp->length = flk->l_len;
85897c478bd9Sstevel@tonic-gate 
85907c478bd9Sstevel@tonic-gate 	if (flk->l_type == F_RDLCK)
85917c478bd9Sstevel@tonic-gate 		dp->locktype = READ_LT;
85927c478bd9Sstevel@tonic-gate 	else if (flk->l_type == F_WRLCK)
85937c478bd9Sstevel@tonic-gate 		dp->locktype = WRITE_LT;
85947c478bd9Sstevel@tonic-gate 	else
85957c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
85967c478bd9Sstevel@tonic-gate 
85977c478bd9Sstevel@tonic-gate 	return (NFS4_OK);
85987c478bd9Sstevel@tonic-gate }
85997c478bd9Sstevel@tonic-gate 
860055a4551dSMarcel Telka /*
860155a4551dSMarcel Telka  * The NFSv4.0 LOCK operation does not support the blocking lock (at the
860255a4551dSMarcel Telka  * NFSv4.0 protocol level) so the client needs to resend the LOCK request in a
860355a4551dSMarcel Telka  * case the lock is denied by the NFSv4.0 server.  NFSv4.0 clients are prepared
860455a4551dSMarcel Telka  * for that (obviously); they are sending the LOCK requests with some delays
860555a4551dSMarcel Telka  * between the attempts.  See nfs4frlock() and nfs4_block_and_wait() for the
860655a4551dSMarcel Telka  * locking and delay implementation at the client side.
860755a4551dSMarcel Telka  *
860855a4551dSMarcel Telka  * To make the life of the clients easier, the NFSv4.0 server tries to do some
860955a4551dSMarcel Telka  * fast retries on its own (the for loop below) in a hope the lock will be
861055a4551dSMarcel Telka  * available soon.  And if not, the client won't need to resend the LOCK
861155a4551dSMarcel Telka  * requests so fast to check the lock availability.  This basically saves some
861255a4551dSMarcel Telka  * network traffic and tries to make sure the client gets the lock ASAP.
861355a4551dSMarcel Telka  */
86147c478bd9Sstevel@tonic-gate static int
86157c478bd9Sstevel@tonic-gate setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
86167c478bd9Sstevel@tonic-gate {
86177c478bd9Sstevel@tonic-gate 	int error;
86187c478bd9Sstevel@tonic-gate 	struct flock64 flk;
86197c478bd9Sstevel@tonic-gate 	int i;
86207c478bd9Sstevel@tonic-gate 	clock_t delaytime;
8621da6c28aaSamw 	int cmd;
862255a4551dSMarcel Telka 	int spin_cnt = 0;
86237c478bd9Sstevel@tonic-gate 
8624da6c28aaSamw 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
86257c478bd9Sstevel@tonic-gate retry:
86267c478bd9Sstevel@tonic-gate 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
86277c478bd9Sstevel@tonic-gate 
86287c478bd9Sstevel@tonic-gate 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8629da6c28aaSamw 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8630da6c28aaSamw 		error = VOP_FRLOCK(vp, cmd,
8631da6c28aaSamw 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
86327c478bd9Sstevel@tonic-gate 
86337c478bd9Sstevel@tonic-gate 		if (error != EAGAIN && error != EACCES)
86347c478bd9Sstevel@tonic-gate 			break;
86357c478bd9Sstevel@tonic-gate 
86367c478bd9Sstevel@tonic-gate 		if (i < rfs4_maxlock_tries - 1) {
86377c478bd9Sstevel@tonic-gate 			delay(delaytime);
86387c478bd9Sstevel@tonic-gate 			delaytime *= 2;
86397c478bd9Sstevel@tonic-gate 		}
86407c478bd9Sstevel@tonic-gate 	}
86417c478bd9Sstevel@tonic-gate 
86427c478bd9Sstevel@tonic-gate 	if (error == EAGAIN || error == EACCES) {
86437c478bd9Sstevel@tonic-gate 		/* Get the owner of the lock */
86447c478bd9Sstevel@tonic-gate 		flk = *flock;
86457c478bd9Sstevel@tonic-gate 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
864655a4551dSMarcel Telka 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag, 0, NULL, cred,
864755a4551dSMarcel Telka 		    NULL) == 0) {
864855a4551dSMarcel Telka 			/*
864955a4551dSMarcel Telka 			 * There's a race inherent in the current VOP_FRLOCK
865055a4551dSMarcel Telka 			 * design where:
865155a4551dSMarcel Telka 			 * a: "other guy" takes a lock that conflicts with a
865255a4551dSMarcel Telka 			 * lock we want
865355a4551dSMarcel Telka 			 * b: we attempt to take our lock (non-blocking) and
865455a4551dSMarcel Telka 			 * the attempt fails.
865555a4551dSMarcel Telka 			 * c: "other guy" releases the conflicting lock
865655a4551dSMarcel Telka 			 * d: we ask what lock conflicts with the lock we want,
865755a4551dSMarcel Telka 			 * getting F_UNLCK (no lock blocks us)
865855a4551dSMarcel Telka 			 *
865955a4551dSMarcel Telka 			 * If we retry the non-blocking lock attempt in this
866055a4551dSMarcel Telka 			 * case (restart at step 'b') there's some possibility
866155a4551dSMarcel Telka 			 * that many such attempts might fail.  However a test
866255a4551dSMarcel Telka 			 * designed to actually provoke this race shows that
866355a4551dSMarcel Telka 			 * the vast majority of cases require no retry, and
866455a4551dSMarcel Telka 			 * only a few took as many as three retries.  Here's
866555a4551dSMarcel Telka 			 * the test outcome:
866655a4551dSMarcel Telka 			 *
866755a4551dSMarcel Telka 			 *	   number of retries    how many times we needed
866855a4551dSMarcel Telka 			 *				that many retries
866955a4551dSMarcel Telka 			 *	   0			79461
867055a4551dSMarcel Telka 			 *	   1			  862
867155a4551dSMarcel Telka 			 *	   2			   49
867255a4551dSMarcel Telka 			 *	   3			    5
867355a4551dSMarcel Telka 			 *
867455a4551dSMarcel Telka 			 * Given those empirical results, we arbitrarily limit
867555a4551dSMarcel Telka 			 * the retry count to ten.
867655a4551dSMarcel Telka 			 *
867755a4551dSMarcel Telka 			 * If we actually make to ten retries and give up,
867855a4551dSMarcel Telka 			 * nothing catastrophic happens, but we're unable to
867955a4551dSMarcel Telka 			 * return the information about the conflicting lock to
868055a4551dSMarcel Telka 			 * the NFS client.  That's an acceptable trade off vs.
868155a4551dSMarcel Telka 			 * letting this retry loop run forever.
868255a4551dSMarcel Telka 			 */
86837c478bd9Sstevel@tonic-gate 			if (flk.l_type == F_UNLCK) {
868455a4551dSMarcel Telka 				if (spin_cnt++ < 10) {
86857c478bd9Sstevel@tonic-gate 					/* No longer locked, retry */
86867c478bd9Sstevel@tonic-gate 					goto retry;
86877c478bd9Sstevel@tonic-gate 				}
868855a4551dSMarcel Telka 			} else {
86897c478bd9Sstevel@tonic-gate 				*flock = flk;
86907c478bd9Sstevel@tonic-gate 				LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
86917c478bd9Sstevel@tonic-gate 				    F_GETLK, &flk);
86927c478bd9Sstevel@tonic-gate 			}
86937c478bd9Sstevel@tonic-gate 		}
869455a4551dSMarcel Telka 	}
86957c478bd9Sstevel@tonic-gate 
86967c478bd9Sstevel@tonic-gate 	return (error);
86977c478bd9Sstevel@tonic-gate }
86987c478bd9Sstevel@tonic-gate 
86997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
87007c478bd9Sstevel@tonic-gate static nfsstat4
8701d216dff5SRobert Mastors rfs4_do_lock(rfs4_lo_state_t *lsp, nfs_lock_type4 locktype,
8702d216dff5SRobert Mastors     offset4 offset, length4 length, cred_t *cred, nfs_resop4 *resop)
87037c478bd9Sstevel@tonic-gate {
87047c478bd9Sstevel@tonic-gate 	nfsstat4 status;
8705d216dff5SRobert Mastors 	rfs4_lockowner_t *lo = lsp->rls_locker;
8706d216dff5SRobert Mastors 	rfs4_state_t *sp = lsp->rls_state;
87077c478bd9Sstevel@tonic-gate 	struct flock64 flock;
87087c478bd9Sstevel@tonic-gate 	int16_t ltype;
87097c478bd9Sstevel@tonic-gate 	int flag;
87107c478bd9Sstevel@tonic-gate 	int error;
87117c478bd9Sstevel@tonic-gate 	sysid_t sysid;
87127c478bd9Sstevel@tonic-gate 	LOCK4res *lres;
87133a36268eSMarcel Telka 	vnode_t *vp;
87147c478bd9Sstevel@tonic-gate 
8715d216dff5SRobert Mastors 	if (rfs4_lease_expired(lo->rl_client)) {
87167c478bd9Sstevel@tonic-gate 		return (NFS4ERR_EXPIRED);
87177c478bd9Sstevel@tonic-gate 	}
87187c478bd9Sstevel@tonic-gate 
8719d216dff5SRobert Mastors 	if ((status = rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
87207c478bd9Sstevel@tonic-gate 		return (status);
87217c478bd9Sstevel@tonic-gate 
87227c478bd9Sstevel@tonic-gate 	/* Check for zero length. To lock to end of file use all ones for V4 */
87237c478bd9Sstevel@tonic-gate 	if (length == 0)
87247c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
87257c478bd9Sstevel@tonic-gate 	else if (length == (length4)(~0))
87267c478bd9Sstevel@tonic-gate 		length = 0;		/* Posix to end of file  */
87277c478bd9Sstevel@tonic-gate 
87287c478bd9Sstevel@tonic-gate retry:
8729d216dff5SRobert Mastors 	rfs4_dbe_lock(sp->rs_dbe);
87303a36268eSMarcel Telka 	if (sp->rs_closed == TRUE) {
8731d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
8732d216dff5SRobert Mastors 		return (NFS4ERR_OLD_STATEID);
8733d216dff5SRobert Mastors 	}
87347c478bd9Sstevel@tonic-gate 
87357c478bd9Sstevel@tonic-gate 	if (resop->resop != OP_LOCKU) {
87367c478bd9Sstevel@tonic-gate 		switch (locktype) {
87377c478bd9Sstevel@tonic-gate 		case READ_LT:
87387c478bd9Sstevel@tonic-gate 		case READW_LT:
8739d216dff5SRobert Mastors 			if ((sp->rs_share_access
87407c478bd9Sstevel@tonic-gate 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8741d216dff5SRobert Mastors 				rfs4_dbe_unlock(sp->rs_dbe);
87427c478bd9Sstevel@tonic-gate 
87437c478bd9Sstevel@tonic-gate 				return (NFS4ERR_OPENMODE);
87447c478bd9Sstevel@tonic-gate 			}
87457c478bd9Sstevel@tonic-gate 			ltype = F_RDLCK;
87467c478bd9Sstevel@tonic-gate 			break;
87477c478bd9Sstevel@tonic-gate 		case WRITE_LT:
87487c478bd9Sstevel@tonic-gate 		case WRITEW_LT:
8749d216dff5SRobert Mastors 			if ((sp->rs_share_access
87507c478bd9Sstevel@tonic-gate 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8751d216dff5SRobert Mastors 				rfs4_dbe_unlock(sp->rs_dbe);
87527c478bd9Sstevel@tonic-gate 
87537c478bd9Sstevel@tonic-gate 				return (NFS4ERR_OPENMODE);
87547c478bd9Sstevel@tonic-gate 			}
87557c478bd9Sstevel@tonic-gate 			ltype = F_WRLCK;
87567c478bd9Sstevel@tonic-gate 			break;
87577c478bd9Sstevel@tonic-gate 		}
87587c478bd9Sstevel@tonic-gate 	} else
87597c478bd9Sstevel@tonic-gate 		ltype = F_UNLCK;
87607c478bd9Sstevel@tonic-gate 
87617c478bd9Sstevel@tonic-gate 	flock.l_type = ltype;
87627c478bd9Sstevel@tonic-gate 	flock.l_whence = 0;		/* SEEK_SET */
87637c478bd9Sstevel@tonic-gate 	flock.l_start = offset;
87647c478bd9Sstevel@tonic-gate 	flock.l_len = length;
87657c478bd9Sstevel@tonic-gate 	flock.l_sysid = sysid;
8766d216dff5SRobert Mastors 	flock.l_pid = lsp->rls_locker->rl_pid;
87677c478bd9Sstevel@tonic-gate 
87687c478bd9Sstevel@tonic-gate 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
87697c478bd9Sstevel@tonic-gate 	if (flock.l_len < 0 || flock.l_start < 0) {
8770d216dff5SRobert Mastors 		rfs4_dbe_unlock(sp->rs_dbe);
87717c478bd9Sstevel@tonic-gate 		return (NFS4ERR_INVAL);
87727c478bd9Sstevel@tonic-gate 	}
87737c478bd9Sstevel@tonic-gate 
87747c478bd9Sstevel@tonic-gate 	/*
87757c478bd9Sstevel@tonic-gate 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
87767c478bd9Sstevel@tonic-gate 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
87777c478bd9Sstevel@tonic-gate 	 */
8778d216dff5SRobert Mastors 	flag = (int)sp->rs_share_access | F_REMOTELOCK;
87797c478bd9Sstevel@tonic-gate 
87803a36268eSMarcel Telka 	vp = sp->rs_finfo->rf_vp;
87813a36268eSMarcel Telka 	VN_HOLD(vp);
87823a36268eSMarcel Telka 
87833a36268eSMarcel Telka 	/*
87843a36268eSMarcel Telka 	 * We need to unlock sp before we call the underlying filesystem to
87853a36268eSMarcel Telka 	 * acquire the file lock.
87863a36268eSMarcel Telka 	 */
87873a36268eSMarcel Telka 	rfs4_dbe_unlock(sp->rs_dbe);
87883a36268eSMarcel Telka 
87893a36268eSMarcel Telka 	error = setlock(vp, &flock, flag, cred);
87903a36268eSMarcel Telka 
87913a36268eSMarcel Telka 	/*
87923a36268eSMarcel Telka 	 * Make sure the file is still open.  In a case the file was closed in
87933a36268eSMarcel Telka 	 * the meantime, clean the lock we acquired using the setlock() call
87943a36268eSMarcel Telka 	 * above, and return the appropriate error.
87953a36268eSMarcel Telka 	 */
87963a36268eSMarcel Telka 	rfs4_dbe_lock(sp->rs_dbe);
87973a36268eSMarcel Telka 	if (sp->rs_closed == TRUE) {
87983a36268eSMarcel Telka 		cleanlocks(vp, lsp->rls_locker->rl_pid, sysid);
87993a36268eSMarcel Telka 		rfs4_dbe_unlock(sp->rs_dbe);
88003a36268eSMarcel Telka 
88013a36268eSMarcel Telka 		VN_RELE(vp);
88023a36268eSMarcel Telka 
88033a36268eSMarcel Telka 		return (NFS4ERR_OLD_STATEID);
88043a36268eSMarcel Telka 	}
88053a36268eSMarcel Telka 	rfs4_dbe_unlock(sp->rs_dbe);
88063a36268eSMarcel Telka 
88073a36268eSMarcel Telka 	VN_RELE(vp);
88083a36268eSMarcel Telka 
88097c478bd9Sstevel@tonic-gate 	if (error == 0) {
8810d216dff5SRobert Mastors 		rfs4_dbe_lock(lsp->rls_dbe);
8811d216dff5SRobert Mastors 		next_stateid(&lsp->rls_lockid);
8812d216dff5SRobert Mastors 		rfs4_dbe_unlock(lsp->rls_dbe);
88137c478bd9Sstevel@tonic-gate 	}
88147c478bd9Sstevel@tonic-gate 
88157c478bd9Sstevel@tonic-gate 	/*
88167c478bd9Sstevel@tonic-gate 	 * N.B. We map error values to nfsv4 errors. This is differrent
88177c478bd9Sstevel@tonic-gate 	 * than puterrno4 routine.
88187c478bd9Sstevel@tonic-gate 	 */
88197c478bd9Sstevel@tonic-gate 	switch (error) {
88207c478bd9Sstevel@tonic-gate 	case 0:
88217c478bd9Sstevel@tonic-gate 		status = NFS4_OK;
88227c478bd9Sstevel@tonic-gate 		break;
88237c478bd9Sstevel@tonic-gate 	case EAGAIN:
88247c478bd9Sstevel@tonic-gate 	case EACCES:		/* Old value */
88257c478bd9Sstevel@tonic-gate 		/* Can only get here if op is OP_LOCK */
88267c478bd9Sstevel@tonic-gate 		ASSERT(resop->resop == OP_LOCK);
88277c478bd9Sstevel@tonic-gate 		lres = &resop->nfs_resop4_u.oplock;
88287c478bd9Sstevel@tonic-gate 		status = NFS4ERR_DENIED;
88297c478bd9Sstevel@tonic-gate 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
88307c478bd9Sstevel@tonic-gate 		    == NFS4ERR_EXPIRED)
88317c478bd9Sstevel@tonic-gate 			goto retry;
88327c478bd9Sstevel@tonic-gate 		break;
88337c478bd9Sstevel@tonic-gate 	case ENOLCK:
88347c478bd9Sstevel@tonic-gate 		status = NFS4ERR_DELAY;
88357c478bd9Sstevel@tonic-gate 		break;
88367c478bd9Sstevel@tonic-gate 	case EOVERFLOW:
88377c478bd9Sstevel@tonic-gate 		status = NFS4ERR_INVAL;
88387c478bd9Sstevel@tonic-gate 		break;
88397c478bd9Sstevel@tonic-gate 	case EINVAL:
88407c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NOTSUPP;
88417c478bd9Sstevel@tonic-gate 		break;
88427c478bd9Sstevel@tonic-gate 	default:
88437c478bd9Sstevel@tonic-gate 		status = NFS4ERR_SERVERFAULT;
88447c478bd9Sstevel@tonic-gate 		break;
88457c478bd9Sstevel@tonic-gate 	}
88467c478bd9Sstevel@tonic-gate 
88477c478bd9Sstevel@tonic-gate 	return (status);
88487c478bd9Sstevel@tonic-gate }
88497c478bd9Sstevel@tonic-gate 
88507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
88517c478bd9Sstevel@tonic-gate void
88527c478bd9Sstevel@tonic-gate rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
88537c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
88547c478bd9Sstevel@tonic-gate {
88557c478bd9Sstevel@tonic-gate 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
88567c478bd9Sstevel@tonic-gate 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
88577c478bd9Sstevel@tonic-gate 	nfsstat4 status;
88587c478bd9Sstevel@tonic-gate 	stateid4 *stateid;
88597c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
88607c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
88617c478bd9Sstevel@tonic-gate 	rfs4_state_t *sp = NULL;
88627c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp = NULL;
88637c478bd9Sstevel@tonic-gate 	bool_t ls_sw_held = FALSE;
88647c478bd9Sstevel@tonic-gate 	bool_t create = TRUE;
88657c478bd9Sstevel@tonic-gate 	bool_t lcreate = TRUE;
88667c478bd9Sstevel@tonic-gate 	bool_t dup_lock = FALSE;
88677c478bd9Sstevel@tonic-gate 	int rc;
88687c478bd9Sstevel@tonic-gate 
8869f3b585ceSsamf 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8870f3b585ceSsamf 	    LOCK4args *, args);
8871f3b585ceSsamf 
88727c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
88737c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8874f3b585ceSsamf 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8875f3b585ceSsamf 		    cs, LOCK4res *, resp);
88767c478bd9Sstevel@tonic-gate 		return;
88777c478bd9Sstevel@tonic-gate 	}
88787c478bd9Sstevel@tonic-gate 
88797c478bd9Sstevel@tonic-gate 	if (args->locker.new_lock_owner) {
88807c478bd9Sstevel@tonic-gate 		/* Create a new lockowner for this instance */
88817c478bd9Sstevel@tonic-gate 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
88827c478bd9Sstevel@tonic-gate 
88837c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
88847c478bd9Sstevel@tonic-gate 
88857c478bd9Sstevel@tonic-gate 		stateid = &olo->open_stateid;
88867c478bd9Sstevel@tonic-gate 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
88877c478bd9Sstevel@tonic-gate 		if (status != NFS4_OK) {
88887c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
88891b300de9Sjwahlig 			    (CE_NOTE, "Get state failed in lock %d", status));
88907c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = status;
8891f3b585ceSsamf 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8892f3b585ceSsamf 			    cs, LOCK4res *, resp);
88937c478bd9Sstevel@tonic-gate 			return;
88947c478bd9Sstevel@tonic-gate 		}
88957c478bd9Sstevel@tonic-gate 
88967c478bd9Sstevel@tonic-gate 		/* Ensure specified filehandle matches */
8897d216dff5SRobert Mastors 		if (cs->vp != sp->rs_finfo->rf_vp) {
88987c478bd9Sstevel@tonic-gate 			rfs4_state_rele(sp);
88997c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8900f3b585ceSsamf 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8901f3b585ceSsamf 			    cs, LOCK4res *, resp);
89027c478bd9Sstevel@tonic-gate 			return;
89037c478bd9Sstevel@tonic-gate 		}
89047c478bd9Sstevel@tonic-gate 
89057c478bd9Sstevel@tonic-gate 		/* hold off other access to open_owner while we tinker */
8906d216dff5SRobert Mastors 		rfs4_sw_enter(&sp->rs_owner->ro_sw);
89077c478bd9Sstevel@tonic-gate 
89087c478bd9Sstevel@tonic-gate 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
89097c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OLD:
89107c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
89117c478bd9Sstevel@tonic-gate 			goto end;
89127c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_BAD:
89137c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
89147c478bd9Sstevel@tonic-gate 			goto end;
89157c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_EXPIRED:
89167c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
89177c478bd9Sstevel@tonic-gate 			goto end;
89187c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_UNCONFIRMED:
89197c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
89207c478bd9Sstevel@tonic-gate 			goto end;
89217c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_CLOSED:
89227c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
89237c478bd9Sstevel@tonic-gate 			goto end;
89247c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OKAY:
89257c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_REPLAY:
89267c478bd9Sstevel@tonic-gate 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8927d216dff5SRobert Mastors 			    sp->rs_owner, resop)) {
89287c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
89297c478bd9Sstevel@tonic-gate 				if (rc == NFS4_CHECK_STATEID_OKAY)
89307c478bd9Sstevel@tonic-gate 					break;
89317c478bd9Sstevel@tonic-gate 				/*
89327c478bd9Sstevel@tonic-gate 				 * This is replayed stateid; if seqid
89337c478bd9Sstevel@tonic-gate 				 * matches next expected, then client
89347c478bd9Sstevel@tonic-gate 				 * is using wrong seqid.
89357c478bd9Sstevel@tonic-gate 				 */
89367c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
89377c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
89381b300de9Sjwahlig 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
89397c478bd9Sstevel@tonic-gate 				goto end;
89407c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
89417c478bd9Sstevel@tonic-gate 				/* This is a duplicate LOCK request */
89427c478bd9Sstevel@tonic-gate 				dup_lock = TRUE;
89437c478bd9Sstevel@tonic-gate 
89447c478bd9Sstevel@tonic-gate 				/*
89457c478bd9Sstevel@tonic-gate 				 * For a duplicate we do not want to
89467c478bd9Sstevel@tonic-gate 				 * create a new lockowner as it should
89477c478bd9Sstevel@tonic-gate 				 * already exist.
89487c478bd9Sstevel@tonic-gate 				 * Turn off the lockowner create flag.
89497c478bd9Sstevel@tonic-gate 				 */
89507c478bd9Sstevel@tonic-gate 				lcreate = FALSE;
89517c478bd9Sstevel@tonic-gate 			}
89527c478bd9Sstevel@tonic-gate 			break;
89537c478bd9Sstevel@tonic-gate 		}
89547c478bd9Sstevel@tonic-gate 
89557c478bd9Sstevel@tonic-gate 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
89567c478bd9Sstevel@tonic-gate 		if (lo == NULL) {
89577c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
89587c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
89597c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
89607c478bd9Sstevel@tonic-gate 			goto end;
89617c478bd9Sstevel@tonic-gate 		}
89627c478bd9Sstevel@tonic-gate 
89637c478bd9Sstevel@tonic-gate 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
89647c478bd9Sstevel@tonic-gate 		if (lsp == NULL) {
8965d216dff5SRobert Mastors 			rfs4_update_lease(sp->rs_owner->ro_client);
89667c478bd9Sstevel@tonic-gate 			/*
89677c478bd9Sstevel@tonic-gate 			 * Only update theh open_seqid if this is not
89687c478bd9Sstevel@tonic-gate 			 * a duplicate request
89697c478bd9Sstevel@tonic-gate 			 */
89707c478bd9Sstevel@tonic-gate 			if (dup_lock == FALSE) {
8971d216dff5SRobert Mastors 				rfs4_update_open_sequence(sp->rs_owner);
89727c478bd9Sstevel@tonic-gate 			}
89737c478bd9Sstevel@tonic-gate 
89747c478bd9Sstevel@tonic-gate 			NFS4_DEBUG(rfs4_debug,
89757c478bd9Sstevel@tonic-gate 			    (CE_NOTE, "rfs4_op_lock: no state"));
89767c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8977d216dff5SRobert Mastors 			rfs4_update_open_resp(sp->rs_owner, resop, NULL);
89787c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
89797c478bd9Sstevel@tonic-gate 			goto end;
89807c478bd9Sstevel@tonic-gate 		}
89817c478bd9Sstevel@tonic-gate 
89827c478bd9Sstevel@tonic-gate 		/*
89837c478bd9Sstevel@tonic-gate 		 * This is the new_lock_owner branch and the client is
89847c478bd9Sstevel@tonic-gate 		 * supposed to be associating a new lock_owner with
89857c478bd9Sstevel@tonic-gate 		 * the open file at this point.  If we find that a
89867c478bd9Sstevel@tonic-gate 		 * lock_owner/state association already exists and a
89877c478bd9Sstevel@tonic-gate 		 * successful LOCK request was returned to the client,
89887c478bd9Sstevel@tonic-gate 		 * an error is returned to the client since this is
89897c478bd9Sstevel@tonic-gate 		 * not appropriate.  The client should be using the
89907c478bd9Sstevel@tonic-gate 		 * existing lock_owner branch.
89917c478bd9Sstevel@tonic-gate 		 */
89927c478bd9Sstevel@tonic-gate 		if (dup_lock == FALSE && create == FALSE) {
8993d216dff5SRobert Mastors 			if (lsp->rls_lock_completed == TRUE) {
89947c478bd9Sstevel@tonic-gate 				*cs->statusp =
89957c478bd9Sstevel@tonic-gate 				    resp->status = NFS4ERR_BAD_SEQID;
89967c478bd9Sstevel@tonic-gate 				rfs4_lockowner_rele(lo);
89977c478bd9Sstevel@tonic-gate 				goto end;
89987c478bd9Sstevel@tonic-gate 			}
89997c478bd9Sstevel@tonic-gate 		}
90007c478bd9Sstevel@tonic-gate 
9001d216dff5SRobert Mastors 		rfs4_update_lease(sp->rs_owner->ro_client);
90027c478bd9Sstevel@tonic-gate 
90037c478bd9Sstevel@tonic-gate 		/*
90047c478bd9Sstevel@tonic-gate 		 * Only update theh open_seqid if this is not
90057c478bd9Sstevel@tonic-gate 		 * a duplicate request
90067c478bd9Sstevel@tonic-gate 		 */
90077c478bd9Sstevel@tonic-gate 		if (dup_lock == FALSE) {
9008d216dff5SRobert Mastors 			rfs4_update_open_sequence(sp->rs_owner);
90097c478bd9Sstevel@tonic-gate 		}
90107c478bd9Sstevel@tonic-gate 
90117c478bd9Sstevel@tonic-gate 		/*
90127c478bd9Sstevel@tonic-gate 		 * If this is a duplicate lock request, just copy the
90137c478bd9Sstevel@tonic-gate 		 * previously saved reply and return.
90147c478bd9Sstevel@tonic-gate 		 */
90157c478bd9Sstevel@tonic-gate 		if (dup_lock == TRUE) {
90167c478bd9Sstevel@tonic-gate 			/* verify that lock_seqid's match */
9017d216dff5SRobert Mastors 			if (lsp->rls_seqid != olo->lock_seqid) {
90187c478bd9Sstevel@tonic-gate 				NFS4_DEBUG(rfs4_debug,
90197c478bd9Sstevel@tonic-gate 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
90207c478bd9Sstevel@tonic-gate 				    "lsp->seqid=%d old->seqid=%d",
9021d216dff5SRobert Mastors 				    lsp->rls_seqid, olo->lock_seqid));
90227c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
90237c478bd9Sstevel@tonic-gate 			} else {
9024d216dff5SRobert Mastors 				rfs4_copy_reply(resop, &lsp->rls_reply);
90257c478bd9Sstevel@tonic-gate 				/*
90267c478bd9Sstevel@tonic-gate 				 * Make sure to copy the just
90277c478bd9Sstevel@tonic-gate 				 * retrieved reply status into the
90287c478bd9Sstevel@tonic-gate 				 * overall compound status
90297c478bd9Sstevel@tonic-gate 				 */
90307c478bd9Sstevel@tonic-gate 				*cs->statusp = resp->status;
90317c478bd9Sstevel@tonic-gate 			}
90327c478bd9Sstevel@tonic-gate 			rfs4_lockowner_rele(lo);
90337c478bd9Sstevel@tonic-gate 			goto end;
90347c478bd9Sstevel@tonic-gate 		}
90357c478bd9Sstevel@tonic-gate 
9036d216dff5SRobert Mastors 		rfs4_dbe_lock(lsp->rls_dbe);
90377c478bd9Sstevel@tonic-gate 
90387c478bd9Sstevel@tonic-gate 		/* Make sure to update the lock sequence id */
9039d216dff5SRobert Mastors 		lsp->rls_seqid = olo->lock_seqid;
90407c478bd9Sstevel@tonic-gate 
90417c478bd9Sstevel@tonic-gate 		NFS4_DEBUG(rfs4_debug,
9042d216dff5SRobert Mastors 		    (CE_NOTE, "Lock seqid established as %d", lsp->rls_seqid));
90437c478bd9Sstevel@tonic-gate 
90447c478bd9Sstevel@tonic-gate 		/*
90457c478bd9Sstevel@tonic-gate 		 * This is used to signify the newly created lockowner
90467c478bd9Sstevel@tonic-gate 		 * stateid and its sequence number.  The checks for
90477c478bd9Sstevel@tonic-gate 		 * sequence number and increment don't occur on the
90487c478bd9Sstevel@tonic-gate 		 * very first lock request for a lockowner.
90497c478bd9Sstevel@tonic-gate 		 */
9050d216dff5SRobert Mastors 		lsp->rls_skip_seqid_check = TRUE;
90517c478bd9Sstevel@tonic-gate 
90527c478bd9Sstevel@tonic-gate 		/* hold off other access to lsp while we tinker */
9053d216dff5SRobert Mastors 		rfs4_sw_enter(&lsp->rls_sw);
90547c478bd9Sstevel@tonic-gate 		ls_sw_held = TRUE;
90557c478bd9Sstevel@tonic-gate 
9056d216dff5SRobert Mastors 		rfs4_dbe_unlock(lsp->rls_dbe);
90577c478bd9Sstevel@tonic-gate 
90587c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
90597c478bd9Sstevel@tonic-gate 	} else {
90607c478bd9Sstevel@tonic-gate 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
90617c478bd9Sstevel@tonic-gate 		/* get lsp and hold the lock on the underlying file struct */
90627c478bd9Sstevel@tonic-gate 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
90637c478bd9Sstevel@tonic-gate 		    != NFS4_OK) {
90647c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = status;
9065f3b585ceSsamf 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9066f3b585ceSsamf 			    cs, LOCK4res *, resp);
90677c478bd9Sstevel@tonic-gate 			return;
90687c478bd9Sstevel@tonic-gate 		}
90697c478bd9Sstevel@tonic-gate 		create = FALSE;	/* We didn't create lsp */
90707c478bd9Sstevel@tonic-gate 
90717c478bd9Sstevel@tonic-gate 		/* Ensure specified filehandle matches */
9072d216dff5SRobert Mastors 		if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
90737c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, TRUE);
90747c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9075f3b585ceSsamf 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9076f3b585ceSsamf 			    cs, LOCK4res *, resp);
90777c478bd9Sstevel@tonic-gate 			return;
90787c478bd9Sstevel@tonic-gate 		}
90797c478bd9Sstevel@tonic-gate 
90807c478bd9Sstevel@tonic-gate 		/* hold off other access to lsp while we tinker */
9081d216dff5SRobert Mastors 		rfs4_sw_enter(&lsp->rls_sw);
90827c478bd9Sstevel@tonic-gate 		ls_sw_held = TRUE;
90837c478bd9Sstevel@tonic-gate 
90847c478bd9Sstevel@tonic-gate 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
90857c478bd9Sstevel@tonic-gate 		/*
90867c478bd9Sstevel@tonic-gate 		 * The stateid looks like it was okay (expected to be
90877c478bd9Sstevel@tonic-gate 		 * the next one)
90887c478bd9Sstevel@tonic-gate 		 */
90897c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OKAY:
90907c478bd9Sstevel@tonic-gate 			/*
90917c478bd9Sstevel@tonic-gate 			 * The sequence id is now checked.  Determine
90927c478bd9Sstevel@tonic-gate 			 * if this is a replay or if it is in the
90937c478bd9Sstevel@tonic-gate 			 * expected (next) sequence.  In the case of a
90947c478bd9Sstevel@tonic-gate 			 * replay, there are two replay conditions
90957c478bd9Sstevel@tonic-gate 			 * that may occur.  The first is the normal
90967c478bd9Sstevel@tonic-gate 			 * condition where a LOCK is done with a
90977c478bd9Sstevel@tonic-gate 			 * NFS4_OK response and the stateid is
90987c478bd9Sstevel@tonic-gate 			 * updated.  That case is handled below when
90997c478bd9Sstevel@tonic-gate 			 * the stateid is identified as a REPLAY.  The
91007c478bd9Sstevel@tonic-gate 			 * second is the case where an error is
91017c478bd9Sstevel@tonic-gate 			 * returned, like NFS4ERR_DENIED, and the
91027c478bd9Sstevel@tonic-gate 			 * sequence number is updated but the stateid
91037c478bd9Sstevel@tonic-gate 			 * is not updated.  This second case is dealt
91047c478bd9Sstevel@tonic-gate 			 * with here.  So it may seem odd that the
91057c478bd9Sstevel@tonic-gate 			 * stateid is okay but the sequence id is a
91067c478bd9Sstevel@tonic-gate 			 * replay but it is okay.
91077c478bd9Sstevel@tonic-gate 			 */
91087c478bd9Sstevel@tonic-gate 			switch (rfs4_check_lock_seqid(
91097c478bd9Sstevel@tonic-gate 			    args->locker.locker4_u.lock_owner.lock_seqid,
91107c478bd9Sstevel@tonic-gate 			    lsp, resop)) {
91117c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
91127c478bd9Sstevel@tonic-gate 				if (resp->status != NFS4_OK) {
91137c478bd9Sstevel@tonic-gate 					/*
91147c478bd9Sstevel@tonic-gate 					 * Here is our replay and need
91157c478bd9Sstevel@tonic-gate 					 * to verify that the last
91167c478bd9Sstevel@tonic-gate 					 * response was an error.
91177c478bd9Sstevel@tonic-gate 					 */
91187c478bd9Sstevel@tonic-gate 					*cs->statusp = resp->status;
91197c478bd9Sstevel@tonic-gate 					goto end;
91207c478bd9Sstevel@tonic-gate 				}
91217c478bd9Sstevel@tonic-gate 				/*
91227c478bd9Sstevel@tonic-gate 				 * This is done since the sequence id
91237c478bd9Sstevel@tonic-gate 				 * looked like a replay but it didn't
91247c478bd9Sstevel@tonic-gate 				 * pass our check so a BAD_SEQID is
91257c478bd9Sstevel@tonic-gate 				 * returned as a result.
91267c478bd9Sstevel@tonic-gate 				 */
91277c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
91287c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
91291b300de9Sjwahlig 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
91307c478bd9Sstevel@tonic-gate 				goto end;
91317c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
91327c478bd9Sstevel@tonic-gate 				/* Everything looks okay move ahead */
91337c478bd9Sstevel@tonic-gate 				break;
91347c478bd9Sstevel@tonic-gate 			}
91357c478bd9Sstevel@tonic-gate 			break;
91367c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_OLD:
91377c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
91387c478bd9Sstevel@tonic-gate 			goto end;
91397c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_BAD:
91407c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
91417c478bd9Sstevel@tonic-gate 			goto end;
91427c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_EXPIRED:
91437c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
91447c478bd9Sstevel@tonic-gate 			goto end;
91457c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_CLOSED:
91467c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
91477c478bd9Sstevel@tonic-gate 			goto end;
91487c478bd9Sstevel@tonic-gate 		case NFS4_CHECK_STATEID_REPLAY:
91497c478bd9Sstevel@tonic-gate 			switch (rfs4_check_lock_seqid(
91507c478bd9Sstevel@tonic-gate 			    args->locker.locker4_u.lock_owner.lock_seqid,
91517c478bd9Sstevel@tonic-gate 			    lsp, resop)) {
91527c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_OKAY:
91537c478bd9Sstevel@tonic-gate 				/*
91547c478bd9Sstevel@tonic-gate 				 * This is a replayed stateid; if
91557c478bd9Sstevel@tonic-gate 				 * seqid matches the next expected,
91567c478bd9Sstevel@tonic-gate 				 * then client is using wrong seqid.
91577c478bd9Sstevel@tonic-gate 				 */
91587c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_BAD:
91591b300de9Sjwahlig 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
91607c478bd9Sstevel@tonic-gate 				goto end;
91617c478bd9Sstevel@tonic-gate 			case NFS4_CHKSEQ_REPLAY:
9162d216dff5SRobert Mastors 				rfs4_update_lease(lsp->rls_locker->rl_client);
91637c478bd9Sstevel@tonic-gate 				*cs->statusp = status = resp->status;
91647c478bd9Sstevel@tonic-gate 				goto end;
91657c478bd9Sstevel@tonic-gate 			}
91667c478bd9Sstevel@tonic-gate 			break;
91677c478bd9Sstevel@tonic-gate 		default:
91687c478bd9Sstevel@tonic-gate 			ASSERT(FALSE);
91697c478bd9Sstevel@tonic-gate 			break;
91707c478bd9Sstevel@tonic-gate 		}
91717c478bd9Sstevel@tonic-gate 
91727c478bd9Sstevel@tonic-gate 		rfs4_update_lock_sequence(lsp);
9173d216dff5SRobert Mastors 		rfs4_update_lease(lsp->rls_locker->rl_client);
91747c478bd9Sstevel@tonic-gate 	}
91757c478bd9Sstevel@tonic-gate 
91767c478bd9Sstevel@tonic-gate 	/*
91777c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
91787c478bd9Sstevel@tonic-gate 	 * verify type of object.
91797c478bd9Sstevel@tonic-gate 	 */
91807c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
91817c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
91827c478bd9Sstevel@tonic-gate 			status = NFS4ERR_ISDIR;
91837c478bd9Sstevel@tonic-gate 		else
91847c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
91857c478bd9Sstevel@tonic-gate 		goto out;
91867c478bd9Sstevel@tonic-gate 	}
91877c478bd9Sstevel@tonic-gate 
9188d216dff5SRobert Mastors 	cp = lsp->rls_state->rs_owner->ro_client;
91897c478bd9Sstevel@tonic-gate 
91907c478bd9Sstevel@tonic-gate 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
91917c478bd9Sstevel@tonic-gate 		status = NFS4ERR_GRACE;
91927c478bd9Sstevel@tonic-gate 		goto out;
91937c478bd9Sstevel@tonic-gate 	}
91947c478bd9Sstevel@tonic-gate 
9195d216dff5SRobert Mastors 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->rc_can_reclaim) {
91967c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NO_GRACE;
91977c478bd9Sstevel@tonic-gate 		goto out;
91987c478bd9Sstevel@tonic-gate 	}
91997c478bd9Sstevel@tonic-gate 
92007c478bd9Sstevel@tonic-gate 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
92017c478bd9Sstevel@tonic-gate 		status = NFS4ERR_NO_GRACE;
92027c478bd9Sstevel@tonic-gate 		goto out;
92037c478bd9Sstevel@tonic-gate 	}
92047c478bd9Sstevel@tonic-gate 
9205d216dff5SRobert Mastors 	if (lsp->rls_state->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE)
92067c478bd9Sstevel@tonic-gate 		cs->deleg = TRUE;
92077c478bd9Sstevel@tonic-gate 
92087c478bd9Sstevel@tonic-gate 	status = rfs4_do_lock(lsp, args->locktype,
92091b300de9Sjwahlig 	    args->offset, args->length, cs->cr, resop);
92107c478bd9Sstevel@tonic-gate 
92117c478bd9Sstevel@tonic-gate out:
9212d216dff5SRobert Mastors 	lsp->rls_skip_seqid_check = FALSE;
92137c478bd9Sstevel@tonic-gate 
92147c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
92157c478bd9Sstevel@tonic-gate 
92167c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK) {
9217d216dff5SRobert Mastors 		resp->LOCK4res_u.lock_stateid = lsp->rls_lockid.stateid;
9218d216dff5SRobert Mastors 		lsp->rls_lock_completed = TRUE;
92197c478bd9Sstevel@tonic-gate 	}
92207c478bd9Sstevel@tonic-gate 	/*
92217c478bd9Sstevel@tonic-gate 	 * Only update the "OPEN" response here if this was a new
92227c478bd9Sstevel@tonic-gate 	 * lock_owner
92237c478bd9Sstevel@tonic-gate 	 */
92247c478bd9Sstevel@tonic-gate 	if (sp)
9225d216dff5SRobert Mastors 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
92267c478bd9Sstevel@tonic-gate 
92277c478bd9Sstevel@tonic-gate 	rfs4_update_lock_resp(lsp, resop);
92287c478bd9Sstevel@tonic-gate 
92297c478bd9Sstevel@tonic-gate end:
92307c478bd9Sstevel@tonic-gate 	if (lsp) {
92317c478bd9Sstevel@tonic-gate 		if (ls_sw_held)
9232d216dff5SRobert Mastors 			rfs4_sw_exit(&lsp->rls_sw);
92337c478bd9Sstevel@tonic-gate 		/*
92347c478bd9Sstevel@tonic-gate 		 * If an sp obtained, then the lsp does not represent
92357c478bd9Sstevel@tonic-gate 		 * a lock on the file struct.
92367c478bd9Sstevel@tonic-gate 		 */
92377c478bd9Sstevel@tonic-gate 		if (sp != NULL)
92387c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, FALSE);
92397c478bd9Sstevel@tonic-gate 		else
92407c478bd9Sstevel@tonic-gate 			rfs4_lo_state_rele(lsp, TRUE);
92417c478bd9Sstevel@tonic-gate 	}
92427c478bd9Sstevel@tonic-gate 	if (sp) {
9243d216dff5SRobert Mastors 		rfs4_sw_exit(&sp->rs_owner->ro_sw);
92447c478bd9Sstevel@tonic-gate 		rfs4_state_rele(sp);
92457c478bd9Sstevel@tonic-gate 	}
9246f3b585ceSsamf 
9247f3b585ceSsamf 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
9248f3b585ceSsamf 	    LOCK4res *, resp);
92497c478bd9Sstevel@tonic-gate }
92507c478bd9Sstevel@tonic-gate 
92517c478bd9Sstevel@tonic-gate /* free function for LOCK/LOCKT */
92527c478bd9Sstevel@tonic-gate static void
92537c478bd9Sstevel@tonic-gate lock_denied_free(nfs_resop4 *resop)
92547c478bd9Sstevel@tonic-gate {
92557c478bd9Sstevel@tonic-gate 	LOCK4denied *dp = NULL;
92567c478bd9Sstevel@tonic-gate 
92577c478bd9Sstevel@tonic-gate 	switch (resop->resop) {
92587c478bd9Sstevel@tonic-gate 	case OP_LOCK:
92597c478bd9Sstevel@tonic-gate 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
92607c478bd9Sstevel@tonic-gate 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
92617c478bd9Sstevel@tonic-gate 		break;
92627c478bd9Sstevel@tonic-gate 	case OP_LOCKT:
92637c478bd9Sstevel@tonic-gate 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
92647c478bd9Sstevel@tonic-gate 			dp = &resop->nfs_resop4_u.oplockt.denied;
92657c478bd9Sstevel@tonic-gate 		break;
92667c478bd9Sstevel@tonic-gate 	default:
92677c478bd9Sstevel@tonic-gate 		break;
92687c478bd9Sstevel@tonic-gate 	}
92697c478bd9Sstevel@tonic-gate 
92707c478bd9Sstevel@tonic-gate 	if (dp)
92717c478bd9Sstevel@tonic-gate 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
92727c478bd9Sstevel@tonic-gate }
92737c478bd9Sstevel@tonic-gate 
92747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
92757c478bd9Sstevel@tonic-gate void
92767c478bd9Sstevel@tonic-gate rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
92777c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
92787c478bd9Sstevel@tonic-gate {
92797c478bd9Sstevel@tonic-gate 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
92807c478bd9Sstevel@tonic-gate 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
92817c478bd9Sstevel@tonic-gate 	nfsstat4 status;
92827c478bd9Sstevel@tonic-gate 	stateid4 *stateid = &args->lock_stateid;
92837c478bd9Sstevel@tonic-gate 	rfs4_lo_state_t *lsp;
92847c478bd9Sstevel@tonic-gate 
9285f3b585ceSsamf 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
9286f3b585ceSsamf 	    LOCKU4args *, args);
9287f3b585ceSsamf 
92887c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
92897c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9290f3b585ceSsamf 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9291f3b585ceSsamf 		    LOCKU4res *, resp);
92927c478bd9Sstevel@tonic-gate 		return;
92937c478bd9Sstevel@tonic-gate 	}
92947c478bd9Sstevel@tonic-gate 
92957c478bd9Sstevel@tonic-gate 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
92967c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = status;
9297f3b585ceSsamf 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9298f3b585ceSsamf 		    LOCKU4res *, resp);
92997c478bd9Sstevel@tonic-gate 		return;
93007c478bd9Sstevel@tonic-gate 	}
93017c478bd9Sstevel@tonic-gate 
93027c478bd9Sstevel@tonic-gate 	/* Ensure specified filehandle matches */
9303d216dff5SRobert Mastors 	if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
93047c478bd9Sstevel@tonic-gate 		rfs4_lo_state_rele(lsp, TRUE);
93057c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9306f3b585ceSsamf 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9307f3b585ceSsamf 		    LOCKU4res *, resp);
93087c478bd9Sstevel@tonic-gate 		return;
93097c478bd9Sstevel@tonic-gate 	}
93107c478bd9Sstevel@tonic-gate 
93117c478bd9Sstevel@tonic-gate 	/* hold off other access to lsp while we tinker */
9312d216dff5SRobert Mastors 	rfs4_sw_enter(&lsp->rls_sw);
93137c478bd9Sstevel@tonic-gate 
93147c478bd9Sstevel@tonic-gate 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
93157c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OKAY:
93167c478bd9Sstevel@tonic-gate 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
93177c478bd9Sstevel@tonic-gate 		    != NFS4_CHKSEQ_OKAY) {
93187c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
93197c478bd9Sstevel@tonic-gate 			goto end;
93207c478bd9Sstevel@tonic-gate 		}
93217c478bd9Sstevel@tonic-gate 		break;
93227c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_OLD:
93237c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
93247c478bd9Sstevel@tonic-gate 		goto end;
93257c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_BAD:
93267c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
93277c478bd9Sstevel@tonic-gate 		goto end;
93287c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_EXPIRED:
93297c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
93307c478bd9Sstevel@tonic-gate 		goto end;
93317c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_CLOSED:
93327c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
93337c478bd9Sstevel@tonic-gate 		goto end;
93347c478bd9Sstevel@tonic-gate 	case NFS4_CHECK_STATEID_REPLAY:
93357c478bd9Sstevel@tonic-gate 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
93367c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_OKAY:
93377c478bd9Sstevel@tonic-gate 				/*
93387c478bd9Sstevel@tonic-gate 				 * This is a replayed stateid; if
93397c478bd9Sstevel@tonic-gate 				 * seqid matches the next expected,
93407c478bd9Sstevel@tonic-gate 				 * then client is using wrong seqid.
93417c478bd9Sstevel@tonic-gate 				 */
93427c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_BAD:
93431b300de9Sjwahlig 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
93447c478bd9Sstevel@tonic-gate 			goto end;
93457c478bd9Sstevel@tonic-gate 		case NFS4_CHKSEQ_REPLAY:
9346d216dff5SRobert Mastors 			rfs4_update_lease(lsp->rls_locker->rl_client);
93477c478bd9Sstevel@tonic-gate 			*cs->statusp = status = resp->status;
93487c478bd9Sstevel@tonic-gate 			goto end;
93497c478bd9Sstevel@tonic-gate 		}
93507c478bd9Sstevel@tonic-gate 		break;
93517c478bd9Sstevel@tonic-gate 	default:
93527c478bd9Sstevel@tonic-gate 		ASSERT(FALSE);
93537c478bd9Sstevel@tonic-gate 		break;
93547c478bd9Sstevel@tonic-gate 	}
93557c478bd9Sstevel@tonic-gate 
93567c478bd9Sstevel@tonic-gate 	rfs4_update_lock_sequence(lsp);
9357d216dff5SRobert Mastors 	rfs4_update_lease(lsp->rls_locker->rl_client);
93587c478bd9Sstevel@tonic-gate 
93597c478bd9Sstevel@tonic-gate 	/*
93607c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
93617c478bd9Sstevel@tonic-gate 	 * verify type of object.
93627c478bd9Sstevel@tonic-gate 	 */
93637c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
93647c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
93657c478bd9Sstevel@tonic-gate 			status = NFS4ERR_ISDIR;
93667c478bd9Sstevel@tonic-gate 		else
93677c478bd9Sstevel@tonic-gate 			status = NFS4ERR_INVAL;
93687c478bd9Sstevel@tonic-gate 		goto out;
93697c478bd9Sstevel@tonic-gate 	}
93707c478bd9Sstevel@tonic-gate 
9371d216dff5SRobert Mastors 	if (rfs4_clnt_in_grace(lsp->rls_state->rs_owner->ro_client)) {
93727c478bd9Sstevel@tonic-gate 		status = NFS4ERR_GRACE;
93737c478bd9Sstevel@tonic-gate 		goto out;
93747c478bd9Sstevel@tonic-gate 	}
93757c478bd9Sstevel@tonic-gate 
93767c478bd9Sstevel@tonic-gate 	status = rfs4_do_lock(lsp, args->locktype,
9377d216dff5SRobert Mastors 	    args->offset, args->length, cs->cr, resop);
93787c478bd9Sstevel@tonic-gate 
93797c478bd9Sstevel@tonic-gate out:
93807c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status = status;
93817c478bd9Sstevel@tonic-gate 
93827c478bd9Sstevel@tonic-gate 	if (status == NFS4_OK)
9383d216dff5SRobert Mastors 		resp->lock_stateid = lsp->rls_lockid.stateid;
93847c478bd9Sstevel@tonic-gate 
93857c478bd9Sstevel@tonic-gate 	rfs4_update_lock_resp(lsp, resop);
93867c478bd9Sstevel@tonic-gate 
93877c478bd9Sstevel@tonic-gate end:
9388d216dff5SRobert Mastors 	rfs4_sw_exit(&lsp->rls_sw);
93897c478bd9Sstevel@tonic-gate 	rfs4_lo_state_rele(lsp, TRUE);
9390f3b585ceSsamf 
9391f3b585ceSsamf 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9392f3b585ceSsamf 	    LOCKU4res *, resp);
93937c478bd9Sstevel@tonic-gate }
93947c478bd9Sstevel@tonic-gate 
93957c478bd9Sstevel@tonic-gate /*
93967c478bd9Sstevel@tonic-gate  * LOCKT is a best effort routine, the client can not be guaranteed that
93977c478bd9Sstevel@tonic-gate  * the status return is still in effect by the time the reply is received.
93987c478bd9Sstevel@tonic-gate  * They are numerous race conditions in this routine, but we are not required
93997c478bd9Sstevel@tonic-gate  * and can not be accurate.
94007c478bd9Sstevel@tonic-gate  */
94017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
94027c478bd9Sstevel@tonic-gate void
94037c478bd9Sstevel@tonic-gate rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
94047c478bd9Sstevel@tonic-gate     struct svc_req *req, struct compound_state *cs)
94057c478bd9Sstevel@tonic-gate {
94067c478bd9Sstevel@tonic-gate 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
94077c478bd9Sstevel@tonic-gate 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
94087c478bd9Sstevel@tonic-gate 	rfs4_lockowner_t *lo;
94097c478bd9Sstevel@tonic-gate 	rfs4_client_t *cp;
94107c478bd9Sstevel@tonic-gate 	bool_t create = FALSE;
94117c478bd9Sstevel@tonic-gate 	struct flock64 flk;
94127c478bd9Sstevel@tonic-gate 	int error;
94137c478bd9Sstevel@tonic-gate 	int flag = FREAD | FWRITE;
94147c478bd9Sstevel@tonic-gate 	int ltype;
94157c478bd9Sstevel@tonic-gate 	length4 posix_length;
94167c478bd9Sstevel@tonic-gate 	sysid_t sysid;
94177c478bd9Sstevel@tonic-gate 	pid_t pid;
94187c478bd9Sstevel@tonic-gate 
9419f3b585ceSsamf 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
9420f3b585ceSsamf 	    LOCKT4args *, args);
9421f3b585ceSsamf 
94227c478bd9Sstevel@tonic-gate 	if (cs->vp == NULL) {
94237c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9424f3b585ceSsamf 		goto out;
94257c478bd9Sstevel@tonic-gate 	}
94267c478bd9Sstevel@tonic-gate 
94277c478bd9Sstevel@tonic-gate 	/*
94287c478bd9Sstevel@tonic-gate 	 * NFS4 only allows locking on regular files, so
94297c478bd9Sstevel@tonic-gate 	 * verify type of object.
94307c478bd9Sstevel@tonic-gate 	 */
94317c478bd9Sstevel@tonic-gate 	if (cs->vp->v_type != VREG) {
94327c478bd9Sstevel@tonic-gate 		if (cs->vp->v_type == VDIR)
94337c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
94347c478bd9Sstevel@tonic-gate 		else
94357c478bd9Sstevel@tonic-gate 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
9436f3b585ceSsamf 		goto out;
94377c478bd9Sstevel@tonic-gate 	}
94387c478bd9Sstevel@tonic-gate 
94397c478bd9Sstevel@tonic-gate 	/*
94407c478bd9Sstevel@tonic-gate 	 * Check out the clientid to ensure the server knows about it
94417c478bd9Sstevel@tonic-gate 	 * so that we correctly inform the client of a server reboot.
94427c478bd9Sstevel@tonic-gate 	 */
94437c478bd9Sstevel@tonic-gate 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
94447c478bd9Sstevel@tonic-gate 	    == NULL) {
94457c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status =
94467c478bd9Sstevel@tonic-gate 		    rfs4_check_clientid(&args->owner.clientid, 0);
9447f3b585ceSsamf 		goto out;
94487c478bd9Sstevel@tonic-gate 	}
94497c478bd9Sstevel@tonic-gate 	if (rfs4_lease_expired(cp)) {
94507c478bd9Sstevel@tonic-gate 		rfs4_client_close(cp);
94517c478bd9Sstevel@tonic-gate 		/*
94527c478bd9Sstevel@tonic-gate 		 * Protocol doesn't allow returning NFS4ERR_STALE as
94537c478bd9Sstevel@tonic-gate 		 * other operations do on this check so STALE_CLIENTID
94547c478bd9Sstevel@tonic-gate 		 * is returned instead
94557c478bd9Sstevel@tonic-gate 		 */
94567c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
9457f3b585ceSsamf 		goto out;
94587c478bd9Sstevel@tonic-gate 	}
94597c478bd9Sstevel@tonic-gate 
9460d216dff5SRobert Mastors 	if (rfs4_clnt_in_grace(cp) && !(cp->rc_can_reclaim)) {
94617c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_GRACE;
9462661cedadSvv149972 		rfs4_client_rele(cp);
9463f3b585ceSsamf 		goto out;
94647c478bd9Sstevel@tonic-gate 	}
94657c478bd9Sstevel@tonic-gate 	rfs4_client_rele(cp);
94667c478bd9Sstevel@tonic-gate 
94677c478bd9Sstevel@tonic-gate 	resp->status = NFS4_OK;
94687c478bd9Sstevel@tonic-gate 
94697c478bd9Sstevel@tonic-gate 	switch (args->locktype) {
94707c478bd9Sstevel@tonic-gate 	case READ_LT:
94717c478bd9Sstevel@tonic-gate 	case READW_LT:
94727c478bd9Sstevel@tonic-gate 		ltype = F_RDLCK;
94737c478bd9Sstevel@tonic-gate 		break;
94747c478bd9Sstevel@tonic-gate 	case WRITE_LT:
94757c478bd9Sstevel@tonic-gate 	case WRITEW_LT:
94767c478bd9Sstevel@tonic-gate 		ltype = F_WRLCK;
94777c478bd9Sstevel@tonic-gate 		break;
94787c478bd9Sstevel@tonic-gate 	}
94797c478bd9Sstevel@tonic-gate 
94807c478bd9Sstevel@tonic-gate 	posix_length = args->length;
94817c478bd9Sstevel@tonic-gate 	/* Check for zero length. To lock to end of file use all ones for V4 */
94827c478bd9Sstevel@tonic-gate 	if (posix_length == 0) {
94837c478bd9Sstevel@tonic-gate 		*cs->statusp = resp->status = NFS4ERR_INVAL;
9484f3b585ceSsamf 		goto out;
94857c478bd9Sstevel@tonic-gate 	} else if (posix_length == (length4)(~0)) {
94867c478bd9Sstevel@tonic-gate 		posix_length = 0;	/* Posix to end of file  */
94877c478bd9Sstevel@tonic-gate 	}
94887c478bd9Sstevel@tonic-gate 
94897c478bd9Sstevel@tonic-gate 	/* Find or create a lockowner */
94907c478bd9Sstevel@tonic-gate 	lo = rfs4_findlockowner(&args->owner, &create);
94917c478bd9Sstevel@tonic-gate 
94927c478bd9Sstevel@tonic-gate 	if (lo) {
9493d216dff5SRobert Mastors 		pid = lo->rl_pid;
94947c478bd9Sstevel@tonic-gate 		if ((resp->status =
9495d216dff5SRobert Mastors 		    rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
9496f3b585ceSsamf 			goto err;
94977c478bd9Sstevel@tonic-gate 	} else {
94987c478bd9Sstevel@tonic-gate 		pid = 0;
94997c478bd9Sstevel@tonic-gate 		sysid = lockt_sysid;
95007c478bd9Sstevel@tonic-gate 	}
95017c478bd9Sstevel@tonic-gate retry:
95027c478bd9Sstevel@tonic-gate 	flk.l_type = ltype;
95037c478bd9Sstevel@tonic-gate 	flk.l_whence = 0;		/* SEEK_SET */
95047c478bd9Sstevel@tonic-gate 	flk.l_start = args->offset;
95057c478bd9Sstevel@tonic-gate 	flk.l_len = posix_length;
95067c478bd9Sstevel@tonic-gate 	flk.l_sysid = sysid;
95077c478bd9Sstevel@tonic-gate 	flk.l_pid = pid;
95087c478bd9Sstevel@tonic-gate 	flag |= F_REMOTELOCK;
95097c478bd9Sstevel@tonic-gate 
95107c478bd9Sstevel@tonic-gate 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
95117c478bd9Sstevel@tonic-gate 
95127c478bd9Sstevel@tonic-gate 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
95137c478bd9Sstevel@tonic-gate 	if (flk.l_len < 0 || flk.l_start < 0) {
95147c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
9515f3b585ceSsamf 		goto err;
95167c478bd9Sstevel@tonic-gate 	}
95177c478bd9Sstevel@tonic-gate 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9518da6c28aaSamw 	    NULL, cs->cr, NULL);
95197c478bd9Sstevel@tonic-gate 
95207c478bd9Sstevel@tonic-gate 	/*
95217c478bd9Sstevel@tonic-gate 	 * N.B. We map error values to nfsv4 errors. This is differrent
95227c478bd9Sstevel@tonic-gate 	 * than puterrno4 routine.
95237c478bd9Sstevel@tonic-gate 	 */
95247c478bd9Sstevel@tonic-gate 	switch (error) {
95257c478bd9Sstevel@tonic-gate 	case 0:
95267c478bd9Sstevel@tonic-gate 		if (flk.l_type == F_UNLCK)
95277c478bd9Sstevel@tonic-gate 			resp->status = NFS4_OK;
95287c478bd9Sstevel@tonic-gate 		else {
95297c478bd9Sstevel@tonic-gate 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
95307c478bd9Sstevel@tonic-gate 				goto retry;
95317c478bd9Sstevel@tonic-gate 			resp->status = NFS4ERR_DENIED;
95327c478bd9Sstevel@tonic-gate 		}
95337c478bd9Sstevel@tonic-gate 		break;
95347c478bd9Sstevel@tonic-gate 	case EOVERFLOW:
95357c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_INVAL;
95367c478bd9Sstevel@tonic-gate 		break;
95377c478bd9Sstevel@tonic-gate 	case EINVAL:
95387c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_NOTSUPP;
95397c478bd9Sstevel@tonic-gate 		break;
95407c478bd9Sstevel@tonic-gate 	default:
95417c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
95427c478bd9Sstevel@tonic-gate 		    error);
95437c478bd9Sstevel@tonic-gate 		resp->status = NFS4ERR_SERVERFAULT;
95447c478bd9Sstevel@tonic-gate 		break;
95457c478bd9Sstevel@tonic-gate 	}
95467c478bd9Sstevel@tonic-gate 
9547f3b585ceSsamf err:
95487c478bd9Sstevel@tonic-gate 	if (lo)
95497c478bd9Sstevel@tonic-gate 		rfs4_lockowner_rele(lo);
95507c478bd9Sstevel@tonic-gate 	*cs->statusp = resp->status;
9551f3b585ceSsamf out:
9552f3b585ceSsamf 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9553f3b585ceSsamf 	    LOCKT4res *, resp);
95547c478bd9Sstevel@tonic-gate }
95557c478bd9Sstevel@tonic-gate 
9556d216dff5SRobert Mastors int
9557d216dff5SRobert Mastors rfs4_share(rfs4_state_t *sp, uint32_t access, uint32_t deny)
95587c478bd9Sstevel@tonic-gate {
95597c478bd9Sstevel@tonic-gate 	int err;
9560d216dff5SRobert Mastors 	int cmd;
9561d216dff5SRobert Mastors 	vnode_t *vp;
95627c478bd9Sstevel@tonic-gate 	struct shrlock shr;
95637c478bd9Sstevel@tonic-gate 	struct shr_locowner shr_loco;
9564d216dff5SRobert Mastors 	int fflags = 0;
95657c478bd9Sstevel@tonic-gate 
9566d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9567d216dff5SRobert Mastors 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
95687c478bd9Sstevel@tonic-gate 
9569d216dff5SRobert Mastors 	if (sp->rs_closed)
9570d216dff5SRobert Mastors 		return (NFS4ERR_OLD_STATEID);
9571d216dff5SRobert Mastors 
9572d216dff5SRobert Mastors 	vp = sp->rs_finfo->rf_vp;
9573d216dff5SRobert Mastors 	ASSERT(vp);
9574d216dff5SRobert Mastors 
9575d216dff5SRobert Mastors 	shr.s_access = shr.s_deny = 0;
9576d216dff5SRobert Mastors 
9577d216dff5SRobert Mastors 	if (access & OPEN4_SHARE_ACCESS_READ) {
95787c478bd9Sstevel@tonic-gate 		fflags |= FREAD;
95797c478bd9Sstevel@tonic-gate 		shr.s_access |= F_RDACC;
95807c478bd9Sstevel@tonic-gate 	}
9581d216dff5SRobert Mastors 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
95827c478bd9Sstevel@tonic-gate 		fflags |= FWRITE;
95837c478bd9Sstevel@tonic-gate 		shr.s_access |= F_WRACC;
95847c478bd9Sstevel@tonic-gate 	}
9585d216dff5SRobert Mastors 	ASSERT(shr.s_access);
9586d216dff5SRobert Mastors 
9587d216dff5SRobert Mastors 	if (deny & OPEN4_SHARE_DENY_READ)
95887c478bd9Sstevel@tonic-gate 		shr.s_deny |= F_RDDNY;
9589d216dff5SRobert Mastors 	if (deny & OPEN4_SHARE_DENY_WRITE)
95907c478bd9Sstevel@tonic-gate 		shr.s_deny |= F_WRDNY;
95917c478bd9Sstevel@tonic-gate 
9592d216dff5SRobert Mastors 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9593d216dff5SRobert Mastors 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
95947c478bd9Sstevel@tonic-gate 	shr_loco.sl_pid = shr.s_pid;
95957c478bd9Sstevel@tonic-gate 	shr_loco.sl_id = shr.s_sysid;
95967c478bd9Sstevel@tonic-gate 	shr.s_owner = (caddr_t)&shr_loco;
95977c478bd9Sstevel@tonic-gate 	shr.s_own_len = sizeof (shr_loco);
9598d216dff5SRobert Mastors 
9599d216dff5SRobert Mastors 	cmd = nbl_need_check(vp) ? F_SHARE_NBMAND : F_SHARE;
9600d216dff5SRobert Mastors 
9601d216dff5SRobert Mastors 	err = VOP_SHRLOCK(vp, cmd, &shr, fflags, CRED(), NULL);
9602d216dff5SRobert Mastors 	if (err != 0) {
9603d216dff5SRobert Mastors 		if (err == EAGAIN)
9604d216dff5SRobert Mastors 			err = NFS4ERR_SHARE_DENIED;
9605d216dff5SRobert Mastors 		else
9606d216dff5SRobert Mastors 			err = puterrno4(err);
9607d216dff5SRobert Mastors 		return (err);
96087c478bd9Sstevel@tonic-gate 	}
96097c478bd9Sstevel@tonic-gate 
9610d216dff5SRobert Mastors 	sp->rs_share_access |= access;
9611d216dff5SRobert Mastors 	sp->rs_share_deny |= deny;
9612d216dff5SRobert Mastors 
9613d216dff5SRobert Mastors 	return (0);
96147c478bd9Sstevel@tonic-gate }
96157c478bd9Sstevel@tonic-gate 
9616d216dff5SRobert Mastors int
96177c478bd9Sstevel@tonic-gate rfs4_unshare(rfs4_state_t *sp)
96187c478bd9Sstevel@tonic-gate {
9619d216dff5SRobert Mastors 	int err;
9620d216dff5SRobert Mastors 	struct shrlock shr;
9621d216dff5SRobert Mastors 	struct shr_locowner shr_loco;
9622d216dff5SRobert Mastors 
9623d216dff5SRobert Mastors 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9624d216dff5SRobert Mastors 
9625d216dff5SRobert Mastors 	if (sp->rs_closed || sp->rs_share_access == 0)
9626d216dff5SRobert Mastors 		return (0);
9627d216dff5SRobert Mastors 
9628d216dff5SRobert Mastors 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9629d216dff5SRobert Mastors 	ASSERT(sp->rs_finfo->rf_vp);
9630d216dff5SRobert Mastors 
9631d216dff5SRobert Mastors 	shr.s_access = shr.s_deny = 0;
9632d216dff5SRobert Mastors 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9633d216dff5SRobert Mastors 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9634d216dff5SRobert Mastors 	shr_loco.sl_pid = shr.s_pid;
9635d216dff5SRobert Mastors 	shr_loco.sl_id = shr.s_sysid;
9636d216dff5SRobert Mastors 	shr.s_owner = (caddr_t)&shr_loco;
9637d216dff5SRobert Mastors 	shr.s_own_len = sizeof (shr_loco);
9638d216dff5SRobert Mastors 
9639d216dff5SRobert Mastors 	err = VOP_SHRLOCK(sp->rs_finfo->rf_vp, F_UNSHARE, &shr, 0, CRED(),
9640d216dff5SRobert Mastors 	    NULL);
9641d216dff5SRobert Mastors 	if (err != 0) {
9642d216dff5SRobert Mastors 		err = puterrno4(err);
9643d216dff5SRobert Mastors 		return (err);
9644d216dff5SRobert Mastors 	}
9645d216dff5SRobert Mastors 
9646d216dff5SRobert Mastors 	sp->rs_share_access = 0;
9647d216dff5SRobert Mastors 	sp->rs_share_deny = 0;
9648d216dff5SRobert Mastors 
9649d216dff5SRobert Mastors 	return (0);
9650d216dff5SRobert Mastors 
96517c478bd9Sstevel@tonic-gate }
96520a701b1eSRobert Gordon 
96530a701b1eSRobert Gordon static int
96540a701b1eSRobert Gordon rdma_setup_read_data4(READ4args *args, READ4res *rok)
96550a701b1eSRobert Gordon {
96560a701b1eSRobert Gordon 	struct clist	*wcl;
96570a701b1eSRobert Gordon 	count4		count = rok->data_len;
9658f837ee4aSSiddheshwar Mahesh 	int		wlist_len;
96590a701b1eSRobert Gordon 
96600a701b1eSRobert Gordon 	wcl = args->wlist;
9661f837ee4aSSiddheshwar Mahesh 	if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) {
96620a701b1eSRobert Gordon 		return (FALSE);
96630a701b1eSRobert Gordon 	}
96640a701b1eSRobert Gordon 	wcl = args->wlist;
9665f837ee4aSSiddheshwar Mahesh 	rok->wlist_len = wlist_len;
96660a701b1eSRobert Gordon 	rok->wlist = wcl;
96670a701b1eSRobert Gordon 	return (TRUE);
96680a701b1eSRobert Gordon }
96692f172c55SRobert Thurlow 
96702f172c55SRobert Thurlow /* tunable to disable server referrals */
96712f172c55SRobert Thurlow int rfs4_no_referrals = 0;
96722f172c55SRobert Thurlow 
96732f172c55SRobert Thurlow /*
96742f172c55SRobert Thurlow  * Find an NFS record in reparse point data.
96752f172c55SRobert Thurlow  * Returns 0 for success and <0 or an errno value on failure.
96762f172c55SRobert Thurlow  */
96772f172c55SRobert Thurlow int
96782f172c55SRobert Thurlow vn_find_nfs_record(vnode_t *vp, nvlist_t **nvlp, char **svcp, char **datap)
96792f172c55SRobert Thurlow {
96802f172c55SRobert Thurlow 	int err;
96812f172c55SRobert Thurlow 	char *stype, *val;
96822f172c55SRobert Thurlow 	nvlist_t *nvl;
96832f172c55SRobert Thurlow 	nvpair_t *curr;
96842f172c55SRobert Thurlow 
96852f172c55SRobert Thurlow 	if ((nvl = reparse_init()) == NULL)
96862f172c55SRobert Thurlow 		return (-1);
96872f172c55SRobert Thurlow 
96882f172c55SRobert Thurlow 	if ((err = reparse_vnode_parse(vp, nvl)) != 0) {
96892f172c55SRobert Thurlow 		reparse_free(nvl);
96902f172c55SRobert Thurlow 		return (err);
96912f172c55SRobert Thurlow 	}
96922f172c55SRobert Thurlow 
96932f172c55SRobert Thurlow 	curr = NULL;
96942f172c55SRobert Thurlow 	while ((curr = nvlist_next_nvpair(nvl, curr)) != NULL) {
96952f172c55SRobert Thurlow 		if ((stype = nvpair_name(curr)) == NULL) {
96962f172c55SRobert Thurlow 			reparse_free(nvl);
96972f172c55SRobert Thurlow 			return (-2);
96982f172c55SRobert Thurlow 		}
96992f172c55SRobert Thurlow 		if (strncasecmp(stype, "NFS", 3) == 0)
97002f172c55SRobert Thurlow 			break;
97012f172c55SRobert Thurlow 	}
97022f172c55SRobert Thurlow 
97032f172c55SRobert Thurlow 	if ((curr == NULL) ||
97042f172c55SRobert Thurlow 	    (nvpair_value_string(curr, &val))) {
97052f172c55SRobert Thurlow 		reparse_free(nvl);
97062f172c55SRobert Thurlow 		return (-3);
97072f172c55SRobert Thurlow 	}
97082f172c55SRobert Thurlow 	*nvlp = nvl;
97092f172c55SRobert Thurlow 	*svcp = stype;
97102f172c55SRobert Thurlow 	*datap = val;
97112f172c55SRobert Thurlow 	return (0);
97122f172c55SRobert Thurlow }
97132f172c55SRobert Thurlow 
97142f172c55SRobert Thurlow int
97152f172c55SRobert Thurlow vn_is_nfs_reparse(vnode_t *vp, cred_t *cr)
97162f172c55SRobert Thurlow {
97172f172c55SRobert Thurlow 	nvlist_t *nvl;
97182f172c55SRobert Thurlow 	char *s, *d;
97192f172c55SRobert Thurlow 
97202f172c55SRobert Thurlow 	if (rfs4_no_referrals != 0)
97212f172c55SRobert Thurlow 		return (B_FALSE);
97222f172c55SRobert Thurlow 
97232f172c55SRobert Thurlow 	if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
97242f172c55SRobert Thurlow 		return (B_FALSE);
97252f172c55SRobert Thurlow 
97262f172c55SRobert Thurlow 	if (vn_find_nfs_record(vp, &nvl, &s, &d) != 0)
97272f172c55SRobert Thurlow 		return (B_FALSE);
97282f172c55SRobert Thurlow 
97292f172c55SRobert Thurlow 	reparse_free(nvl);
97302f172c55SRobert Thurlow 
97312f172c55SRobert Thurlow 	return (B_TRUE);
97322f172c55SRobert Thurlow }
97332f172c55SRobert Thurlow 
97342f172c55SRobert Thurlow /*
97352f172c55SRobert Thurlow  * There is a user-level copy of this routine in ref_subr.c.
97362f172c55SRobert Thurlow  * Changes should be kept in sync.
97372f172c55SRobert Thurlow  */
97382f172c55SRobert Thurlow static int
97392f172c55SRobert Thurlow nfs4_create_components(char *path, component4 *comp4)
97402f172c55SRobert Thurlow {
97412f172c55SRobert Thurlow 	int slen, plen, ncomp;
97422f172c55SRobert Thurlow 	char *ori_path, *nxtc, buf[MAXNAMELEN];
97432f172c55SRobert Thurlow 
97442f172c55SRobert Thurlow 	if (path == NULL)
97452f172c55SRobert Thurlow 		return (0);
97462f172c55SRobert Thurlow 
97472f172c55SRobert Thurlow 	plen = strlen(path) + 1;	/* include the terminator */
97482f172c55SRobert Thurlow 	ori_path = path;
97492f172c55SRobert Thurlow 	ncomp = 0;
97502f172c55SRobert Thurlow 
97512f172c55SRobert Thurlow 	/* count number of components in the path */
97522f172c55SRobert Thurlow 	for (nxtc = path; nxtc < ori_path + plen; nxtc++) {
97532f172c55SRobert Thurlow 		if (*nxtc == '/' || *nxtc == '\0' || *nxtc == '\n') {
97542f172c55SRobert Thurlow 			if ((slen = nxtc - path) == 0) {
97552f172c55SRobert Thurlow 				path = nxtc + 1;
97562f172c55SRobert Thurlow 				continue;
97572f172c55SRobert Thurlow 			}
97582f172c55SRobert Thurlow 
97592f172c55SRobert Thurlow 			if (comp4 != NULL) {
97602f172c55SRobert Thurlow 				bcopy(path, buf, slen);
97612f172c55SRobert Thurlow 				buf[slen] = '\0';
97625301ec54SRobert Thurlow 				(void) str_to_utf8(buf, &comp4[ncomp]);
97632f172c55SRobert Thurlow 			}
97642f172c55SRobert Thurlow 
97652f172c55SRobert Thurlow 			ncomp++;	/* 1 valid component */
97662f172c55SRobert Thurlow 			path = nxtc + 1;
97672f172c55SRobert Thurlow 		}
97682f172c55SRobert Thurlow 		if (*nxtc == '\0' || *nxtc == '\n')
97692f172c55SRobert Thurlow 			break;
97702f172c55SRobert Thurlow 	}
97712f172c55SRobert Thurlow 
97722f172c55SRobert Thurlow 	return (ncomp);
97732f172c55SRobert Thurlow }
97742f172c55SRobert Thurlow 
97752f172c55SRobert Thurlow /*
97762f172c55SRobert Thurlow  * There is a user-level copy of this routine in ref_subr.c.
97772f172c55SRobert Thurlow  * Changes should be kept in sync.
97782f172c55SRobert Thurlow  */
97792f172c55SRobert Thurlow static int
97802f172c55SRobert Thurlow make_pathname4(char *path, pathname4 *pathname)
97812f172c55SRobert Thurlow {
97822f172c55SRobert Thurlow 	int ncomp;
97832f172c55SRobert Thurlow 	component4 *comp4;
97842f172c55SRobert Thurlow 
97852f172c55SRobert Thurlow 	if (pathname == NULL)
97862f172c55SRobert Thurlow 		return (0);
97872f172c55SRobert Thurlow 
97882f172c55SRobert Thurlow 	if (path == NULL) {
97892f172c55SRobert Thurlow 		pathname->pathname4_val = NULL;
97902f172c55SRobert Thurlow 		pathname->pathname4_len = 0;
97912f172c55SRobert Thurlow 		return (0);
97922f172c55SRobert Thurlow 	}
97932f172c55SRobert Thurlow 
97942f172c55SRobert Thurlow 	/* count number of components to alloc buffer */
97952f172c55SRobert Thurlow 	if ((ncomp = nfs4_create_components(path, NULL)) == 0) {
97962f172c55SRobert Thurlow 		pathname->pathname4_val = NULL;
97972f172c55SRobert Thurlow 		pathname->pathname4_len = 0;
97982f172c55SRobert Thurlow 		return (0);
97992f172c55SRobert Thurlow 	}
98002f172c55SRobert Thurlow 	comp4 = kmem_zalloc(ncomp * sizeof (component4), KM_SLEEP);
98012f172c55SRobert Thurlow 
98022f172c55SRobert Thurlow 	/* copy components into allocated buffer */
98032f172c55SRobert Thurlow 	ncomp = nfs4_create_components(path, comp4);
98042f172c55SRobert Thurlow 
98052f172c55SRobert Thurlow 	pathname->pathname4_val = comp4;
98062f172c55SRobert Thurlow 	pathname->pathname4_len = ncomp;
98072f172c55SRobert Thurlow 
98082f172c55SRobert Thurlow 	return (ncomp);
98092f172c55SRobert Thurlow }
98102f172c55SRobert Thurlow 
98112f172c55SRobert Thurlow #define	xdr_fs_locations4 xdr_fattr4_fs_locations
98122f172c55SRobert Thurlow 
98132f172c55SRobert Thurlow fs_locations4 *
98142f172c55SRobert Thurlow fetch_referral(vnode_t *vp, cred_t *cr)
98152f172c55SRobert Thurlow {
98162f172c55SRobert Thurlow 	nvlist_t *nvl;
98172f172c55SRobert Thurlow 	char *stype, *sdata;
98182f172c55SRobert Thurlow 	fs_locations4 *result;
98192f172c55SRobert Thurlow 	char buf[1024];
98202f172c55SRobert Thurlow 	size_t bufsize;
98212f172c55SRobert Thurlow 	XDR xdr;
98222f172c55SRobert Thurlow 	int err;
98232f172c55SRobert Thurlow 
98242f172c55SRobert Thurlow 	/*
98252f172c55SRobert Thurlow 	 * Check attrs to ensure it's a reparse point
98262f172c55SRobert Thurlow 	 */
98272f172c55SRobert Thurlow 	if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
98282f172c55SRobert Thurlow 		return (NULL);
98292f172c55SRobert Thurlow 
98302f172c55SRobert Thurlow 	/*
98312f172c55SRobert Thurlow 	 * Look for an NFS record and get the type and data
98322f172c55SRobert Thurlow 	 */
98332f172c55SRobert Thurlow 	if (vn_find_nfs_record(vp, &nvl, &stype, &sdata) != 0)
98342f172c55SRobert Thurlow 		return (NULL);
98352f172c55SRobert Thurlow 
98362f172c55SRobert Thurlow 	/*
98372f172c55SRobert Thurlow 	 * With the type and data, upcall to get the referral
98382f172c55SRobert Thurlow 	 */
98392f172c55SRobert Thurlow 	bufsize = sizeof (buf);
98402f172c55SRobert Thurlow 	bzero(buf, sizeof (buf));
98412f172c55SRobert Thurlow 	err = reparse_kderef((const char *)stype, (const char *)sdata,
98422f172c55SRobert Thurlow 	    buf, &bufsize);
98432f172c55SRobert Thurlow 	reparse_free(nvl);
98442f172c55SRobert Thurlow 
98452f172c55SRobert Thurlow 	DTRACE_PROBE4(nfs4serv__func__referral__upcall,
98462f172c55SRobert Thurlow 	    char *, stype, char *, sdata, char *, buf, int, err);
98472f172c55SRobert Thurlow 	if (err) {
98482f172c55SRobert Thurlow 		cmn_err(CE_NOTE,
98492f172c55SRobert Thurlow 		    "reparsed daemon not running: unable to get referral (%d)",
98502f172c55SRobert Thurlow 		    err);
98512f172c55SRobert Thurlow 		return (NULL);
98522f172c55SRobert Thurlow 	}
98532f172c55SRobert Thurlow 
98542f172c55SRobert Thurlow 	/*
98552f172c55SRobert Thurlow 	 * We get an XDR'ed record back from the kderef call
98562f172c55SRobert Thurlow 	 */
98572f172c55SRobert Thurlow 	xdrmem_create(&xdr, buf, bufsize, XDR_DECODE);
98582f172c55SRobert Thurlow 	result = kmem_alloc(sizeof (fs_locations4), KM_SLEEP);
98592f172c55SRobert Thurlow 	err = xdr_fs_locations4(&xdr, result);
98602f172c55SRobert Thurlow 	XDR_DESTROY(&xdr);
98612f172c55SRobert Thurlow 	if (err != TRUE) {
98622f172c55SRobert Thurlow 		DTRACE_PROBE1(nfs4serv__func__referral__upcall__xdrfail,
98632f172c55SRobert Thurlow 		    int, err);
98642f172c55SRobert Thurlow 		return (NULL);
98652f172c55SRobert Thurlow 	}
98662f172c55SRobert Thurlow 
98672f172c55SRobert Thurlow 	/*
98682f172c55SRobert Thurlow 	 * Look at path to recover fs_root, ignoring the leading '/'
98692f172c55SRobert Thurlow 	 */
98702f172c55SRobert Thurlow 	(void) make_pathname4(vp->v_path, &result->fs_root);
98712f172c55SRobert Thurlow 
98722f172c55SRobert Thurlow 	return (result);
98732f172c55SRobert Thurlow }
98742f172c55SRobert Thurlow 
98752f172c55SRobert Thurlow char *
98762f172c55SRobert Thurlow build_symlink(vnode_t *vp, cred_t *cr, size_t *strsz)
98772f172c55SRobert Thurlow {
98782f172c55SRobert Thurlow 	fs_locations4 *fsl;
98792f172c55SRobert Thurlow 	fs_location4 *fs;
98802f172c55SRobert Thurlow 	char *server, *path, *symbuf;
98812f172c55SRobert Thurlow 	static char *prefix = "/net/";
98822f172c55SRobert Thurlow 	int i, size, npaths;
98832f172c55SRobert Thurlow 	uint_t len;
98842f172c55SRobert Thurlow 
98852f172c55SRobert Thurlow 	/* Get the referral */
98862f172c55SRobert Thurlow 	if ((fsl = fetch_referral(vp, cr)) == NULL)
98872f172c55SRobert Thurlow 		return (NULL);
98882f172c55SRobert Thurlow 
98892f172c55SRobert Thurlow 	/* Deal with only the first location and first server */
98902f172c55SRobert Thurlow 	fs = &fsl->locations_val[0];
98912f172c55SRobert Thurlow 	server = utf8_to_str(&fs->server_val[0], &len, NULL);
98922f172c55SRobert Thurlow 	if (server == NULL) {
98932f172c55SRobert Thurlow 		rfs4_free_fs_locations4(fsl);
98942f172c55SRobert Thurlow 		kmem_free(fsl, sizeof (fs_locations4));
98952f172c55SRobert Thurlow 		return (NULL);
98962f172c55SRobert Thurlow 	}
98972f172c55SRobert Thurlow 
98982f172c55SRobert Thurlow 	/* Figure out size for "/net/" + host + /path/path/path + NULL */
98992f172c55SRobert Thurlow 	size = strlen(prefix) + len;
99002f172c55SRobert Thurlow 	for (i = 0; i < fs->rootpath.pathname4_len; i++)
99012f172c55SRobert Thurlow 		size += fs->rootpath.pathname4_val[i].utf8string_len + 1;
99022f172c55SRobert Thurlow 
99032f172c55SRobert Thurlow 	/* Allocate the symlink buffer and fill it */
99042f172c55SRobert Thurlow 	symbuf = kmem_zalloc(size, KM_SLEEP);
99052f172c55SRobert Thurlow 	(void) strcat(symbuf, prefix);
99062f172c55SRobert Thurlow 	(void) strcat(symbuf, server);
99072f172c55SRobert Thurlow 	kmem_free(server, len);
99082f172c55SRobert Thurlow 
99092f172c55SRobert Thurlow 	npaths = 0;
99102f172c55SRobert Thurlow 	for (i = 0; i < fs->rootpath.pathname4_len; i++) {
99112f172c55SRobert Thurlow 		path = utf8_to_str(&fs->rootpath.pathname4_val[i], &len, NULL);
99122f172c55SRobert Thurlow 		if (path == NULL)
99132f172c55SRobert Thurlow 			continue;
99142f172c55SRobert Thurlow 		(void) strcat(symbuf, "/");
99152f172c55SRobert Thurlow 		(void) strcat(symbuf, path);
99162f172c55SRobert Thurlow 		npaths++;
99172f172c55SRobert Thurlow 		kmem_free(path, len);
99182f172c55SRobert Thurlow 	}
99192f172c55SRobert Thurlow 
99202f172c55SRobert Thurlow 	rfs4_free_fs_locations4(fsl);
99212f172c55SRobert Thurlow 	kmem_free(fsl, sizeof (fs_locations4));
99222f172c55SRobert Thurlow 
99232f172c55SRobert Thurlow 	if (strsz != NULL)
99242f172c55SRobert Thurlow 		*strsz = size;
99252f172c55SRobert Thurlow 	return (symbuf);
99262f172c55SRobert Thurlow }
99272f172c55SRobert Thurlow 
99282f172c55SRobert Thurlow /*
99292f172c55SRobert Thurlow  * Check to see if we have a downrev Solaris client, so that we
99302f172c55SRobert Thurlow  * can send it a symlink instead of a referral.
99312f172c55SRobert Thurlow  */
99322f172c55SRobert Thurlow int
99332f172c55SRobert Thurlow client_is_downrev(struct svc_req *req)
99342f172c55SRobert Thurlow {
99352f172c55SRobert Thurlow 	struct sockaddr *ca;
99362f172c55SRobert Thurlow 	rfs4_clntip_t *ci;
99372f172c55SRobert Thurlow 	bool_t create = FALSE;
99382f172c55SRobert Thurlow 	int is_downrev;
99392f172c55SRobert Thurlow 
99402f172c55SRobert Thurlow 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
99412f172c55SRobert Thurlow 	ASSERT(ca);
99422f172c55SRobert Thurlow 	ci = rfs4_find_clntip(ca, &create);
99432f172c55SRobert Thurlow 	if (ci == NULL)
99442f172c55SRobert Thurlow 		return (0);
99452f172c55SRobert Thurlow 	is_downrev = ci->ri_no_referrals;
99462f172c55SRobert Thurlow 	rfs4_dbe_rele(ci->ri_dbe);
99472f172c55SRobert Thurlow 	return (is_downrev);
99482f172c55SRobert Thurlow }
9949