xref: /titanic_44/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision 646cf3c6c5f9a3403fe9d15809bbbb2441deb1e2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
28  *	All Rights Reserved
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/cred.h>
37 #include <sys/buf.h>
38 #include <sys/vfs.h>
39 #include <sys/vfs_opreg.h>
40 #include <sys/vnode.h>
41 #include <sys/uio.h>
42 #include <sys/errno.h>
43 #include <sys/sysmacros.h>
44 #include <sys/statvfs.h>
45 #include <sys/kmem.h>
46 #include <sys/dirent.h>
47 #include <sys/cmn_err.h>
48 #include <sys/debug.h>
49 #include <sys/systeminfo.h>
50 #include <sys/flock.h>
51 #include <sys/pathname.h>
52 #include <sys/nbmlock.h>
53 #include <sys/share.h>
54 #include <sys/atomic.h>
55 #include <sys/policy.h>
56 #include <sys/fem.h>
57 #include <sys/sdt.h>
58 #include <sys/ddi.h>
59 
60 #include <rpc/types.h>
61 #include <rpc/auth.h>
62 #include <rpc/rpcsec_gss.h>
63 #include <rpc/svc.h>
64 
65 #include <nfs/nfs.h>
66 #include <nfs/export.h>
67 #include <nfs/lm.h>
68 #include <nfs/nfs4.h>
69 
70 #include <sys/strsubr.h>
71 #include <sys/strsun.h>
72 
73 #include <inet/common.h>
74 #include <inet/ip.h>
75 #include <inet/ip6.h>
76 
77 #include <sys/tsol/label.h>
78 #include <sys/tsol/tndb.h>
79 
80 #define	RFS4_MAXLOCK_TRIES 4	/* Try to get the lock this many times */
81 static int rfs4_maxlock_tries = RFS4_MAXLOCK_TRIES;
82 #define	RFS4_LOCK_DELAY 10	/* Milliseconds */
83 static clock_t rfs4_lock_delay = RFS4_LOCK_DELAY;
84 
85 /* End of Tunables */
86 
87 /*
88  * Used to bump the stateid4.seqid value and show changes in the stateid
89  */
90 #define	next_stateid(sp) (++(sp)->bits.chgseq)
91 
92 /*
93  * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
94  *	This is used to return NFS4ERR_TOOSMALL when clients specify
95  *	maxcount that isn't large enough to hold the smallest possible
96  *	XDR encoded dirent.
97  *
98  *	    sizeof cookie (8 bytes) +
99  *	    sizeof name_len (4 bytes) +
100  *	    sizeof smallest (padded) name (4 bytes) +
101  *	    sizeof bitmap4_len (12 bytes) +   NOTE: we always encode len=2 bm4
102  *	    sizeof attrlist4_len (4 bytes) +
103  *	    sizeof next boolean (4 bytes)
104  *
105  * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
106  * the smallest possible entry4 (assumes no attrs requested).
107  *	sizeof nfsstat4 (4 bytes) +
108  *	sizeof verifier4 (8 bytes) +
109  *	sizeof entry4list bool (4 bytes) +
110  *	sizeof entry4 	(36 bytes) +
111  *	sizeof eof bool  (4 bytes)
112  *
113  * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
114  *	VOP_READDIR.  Its value is the size of the maximum possible dirent
115  *	for solaris.  The DIRENT64_RECLEN macro returns	the size of dirent
116  *	required for a given name length.  MAXNAMELEN is the maximum
117  *	filename length allowed in Solaris.  The first two DIRENT64_RECLEN()
118  *	macros are to allow for . and .. entries -- just a minor tweak to try
119  *	and guarantee that buffer we give to VOP_READDIR will be large enough
120  *	to hold ., .., and the largest possible solaris dirent64.
121  */
122 #define	RFS4_MINLEN_ENTRY4 36
123 #define	RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
124 #define	RFS4_MINLEN_RDDIR_BUF \
125 	(DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
126 
127 /*
128  * It would be better to pad to 4 bytes since that's what XDR would do,
129  * but the dirents UFS gives us are already padded to 8, so just take
130  * what we're given.  Dircount is only a hint anyway.  Currently the
131  * solaris kernel is ASCII only, so there's no point in calling the
132  * UTF8 functions.
133  *
134  * dirent64: named padded to provide 8 byte struct alignment
135  *	d_ino(8) + d_off(8) + d_reclen(2) + d_name(namelen + null(1) + pad)
136  *
137  * cookie: uint64_t   +  utf8namelen: uint_t  +   utf8name padded to 8 bytes
138  *
139  */
140 #define	DIRENT64_TO_DIRCOUNT(dp) \
141 	(3 * BYTES_PER_XDR_UNIT + DIRENT64_NAMELEN((dp)->d_reclen))
142 
143 time_t rfs4_start_time;			/* Initialized in rfs4_srvrinit */
144 
145 static sysid_t lockt_sysid;		/* dummy sysid for all LOCKT calls */
146 
147 u_longlong_t nfs4_srv_caller_id;
148 uint_t nfs4_srv_vkey = 0;
149 
150 verifier4	Write4verf;
151 verifier4	Readdir4verf;
152 
153 void		rfs4_init_compound_state(struct compound_state *);
154 
155 static void	nullfree(caddr_t);
156 static void	rfs4_op_inval(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
157 			struct compound_state *);
158 static void	rfs4_op_access(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
159 			struct compound_state *);
160 static void	rfs4_op_close(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
161 			struct compound_state *);
162 static void	rfs4_op_commit(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
163 			struct compound_state *);
164 static void	rfs4_op_create(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
165 			struct compound_state *);
166 static void	rfs4_op_create_free(nfs_resop4 *resop);
167 static void	rfs4_op_delegreturn(nfs_argop4 *, nfs_resop4 *,
168 			struct svc_req *, struct compound_state *);
169 static void	rfs4_op_delegpurge(nfs_argop4 *, nfs_resop4 *,
170 			struct svc_req *, struct compound_state *);
171 static void	rfs4_op_getattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
172 			struct compound_state *);
173 static void	rfs4_op_getattr_free(nfs_resop4 *);
174 static void	rfs4_op_getfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
175 			struct compound_state *);
176 static void	rfs4_op_getfh_free(nfs_resop4 *);
177 static void	rfs4_op_illegal(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
178 			struct compound_state *);
179 static void	rfs4_op_link(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
180 			struct compound_state *);
181 static void	rfs4_op_lock(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
182 			struct compound_state *);
183 static void	lock_denied_free(nfs_resop4 *);
184 static void	rfs4_op_locku(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
185 			struct compound_state *);
186 static void	rfs4_op_lockt(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
187 			struct compound_state *);
188 static void	rfs4_op_lookup(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
189 			struct compound_state *);
190 static void	rfs4_op_lookupp(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
191 			struct compound_state *);
192 static void	rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop,
193 				struct svc_req *req, struct compound_state *cs);
194 static void	rfs4_op_nverify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
195 			struct compound_state *);
196 static void	rfs4_op_open(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
197 			struct compound_state *);
198 static void	rfs4_op_open_confirm(nfs_argop4 *, nfs_resop4 *,
199 			struct svc_req *, struct compound_state *);
200 static void	rfs4_op_open_downgrade(nfs_argop4 *, nfs_resop4 *,
201 			struct svc_req *, struct compound_state *);
202 static void	rfs4_op_putfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
203 			struct compound_state *);
204 static void	rfs4_op_putpubfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
205 			struct compound_state *);
206 static void	rfs4_op_putrootfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
207 			struct compound_state *);
208 static void	rfs4_op_read(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
209 			struct compound_state *);
210 static void	rfs4_op_read_free(nfs_resop4 *);
211 static void	rfs4_op_readdir_free(nfs_resop4 *resop);
212 static void	rfs4_op_readlink(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
213 			struct compound_state *);
214 static void	rfs4_op_readlink_free(nfs_resop4 *);
215 static void	rfs4_op_release_lockowner(nfs_argop4 *, nfs_resop4 *,
216 			struct svc_req *, struct compound_state *);
217 static void	rfs4_op_remove(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
218 			struct compound_state *);
219 static void	rfs4_op_rename(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
220 			struct compound_state *);
221 static void	rfs4_op_renew(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
222 			struct compound_state *);
223 static void	rfs4_op_restorefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
224 			struct compound_state *);
225 static void	rfs4_op_savefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
226 			struct compound_state *);
227 static void	rfs4_op_setattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
228 			struct compound_state *);
229 static void	rfs4_op_verify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
230 			struct compound_state *);
231 static void	rfs4_op_write(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
232 			struct compound_state *);
233 static void	rfs4_op_setclientid(nfs_argop4 *, nfs_resop4 *,
234 			struct svc_req *, struct compound_state *);
235 static void	rfs4_op_setclientid_confirm(nfs_argop4 *, nfs_resop4 *,
236 			struct svc_req *req, struct compound_state *);
237 static void	rfs4_op_secinfo(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
238 			struct compound_state *);
239 static void	rfs4_op_secinfo_free(nfs_resop4 *);
240 
241 static nfsstat4 check_open_access(uint32_t,
242 				struct compound_state *, struct svc_req *);
243 nfsstat4 rfs4_client_sysid(rfs4_client_t *, sysid_t *);
244 static int	vop_shrlock(vnode_t *, int, struct shrlock *, int);
245 static int 	rfs4_shrlock(rfs4_state_t *, int);
246 static int	rfs4_share(rfs4_state_t *);
247 void rfs4_ss_clid(rfs4_client_t *, struct svc_req *);
248 
249 /*
250  * translation table for attrs
251  */
252 struct nfs4_ntov_table {
253 	union nfs4_attr_u *na;
254 	uint8_t amap[NFS4_MAXNUM_ATTRS];
255 	int attrcnt;
256 	bool_t vfsstat;
257 };
258 
259 static void	nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp);
260 static void	nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
261 				    struct nfs4_svgetit_arg *sargp);
262 
263 static nfsstat4	do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp,
264 		    struct compound_state *cs, struct nfs4_svgetit_arg *sargp,
265 		    struct nfs4_ntov_table *ntovp, nfs4_attr_cmd_t cmd);
266 
267 fem_t	*deleg_rdops;
268 fem_t	*deleg_wrops;
269 
270 rfs4_servinst_t	*rfs4_cur_servinst = NULL;	/* current server instance */
271 kmutex_t	rfs4_servinst_lock;		/* protects linked list */
272 int		rfs4_seen_first_compound;	/* set first time we see one */
273 
274 /*
275  * NFS4 op dispatch table
276  */
277 
278 struct rfsv4disp {
279 	void	(*dis_proc)();		/* proc to call */
280 	void	(*dis_resfree)();	/* frees space allocated by proc */
281 	int	dis_flags;		/* RPC_IDEMPOTENT, etc... */
282 };
283 
284 static struct rfsv4disp rfsv4disptab[] = {
285 	/*
286 	 * NFS VERSION 4
287 	 */
288 
289 	/* RFS_NULL = 0 */
290 	{rfs4_op_illegal, nullfree, 0},
291 
292 	/* UNUSED = 1 */
293 	{rfs4_op_illegal, nullfree, 0},
294 
295 	/* UNUSED = 2 */
296 	{rfs4_op_illegal, nullfree, 0},
297 
298 	/* OP_ACCESS = 3 */
299 	{rfs4_op_access, nullfree, RPC_IDEMPOTENT},
300 
301 	/* OP_CLOSE = 4 */
302 	{rfs4_op_close, nullfree, 0},
303 
304 	/* OP_COMMIT = 5 */
305 	{rfs4_op_commit, nullfree, RPC_IDEMPOTENT},
306 
307 	/* OP_CREATE = 6 */
308 	{rfs4_op_create, nullfree, 0},
309 
310 	/* OP_DELEGPURGE = 7 */
311 	{rfs4_op_delegpurge, nullfree, 0},
312 
313 	/* OP_DELEGRETURN = 8 */
314 	{rfs4_op_delegreturn, nullfree, 0},
315 
316 	/* OP_GETATTR = 9 */
317 	{rfs4_op_getattr, rfs4_op_getattr_free, RPC_IDEMPOTENT},
318 
319 	/* OP_GETFH = 10 */
320 	{rfs4_op_getfh, rfs4_op_getfh_free, RPC_ALL},
321 
322 	/* OP_LINK = 11 */
323 	{rfs4_op_link, nullfree, 0},
324 
325 	/* OP_LOCK = 12 */
326 	{rfs4_op_lock, lock_denied_free, 0},
327 
328 	/* OP_LOCKT = 13 */
329 	{rfs4_op_lockt, lock_denied_free, 0},
330 
331 	/* OP_LOCKU = 14 */
332 	{rfs4_op_locku, nullfree, 0},
333 
334 	/* OP_LOOKUP = 15 */
335 	{rfs4_op_lookup, nullfree, (RPC_IDEMPOTENT|RPC_PUBLICFH_OK)},
336 
337 	/* OP_LOOKUPP = 16 */
338 	{rfs4_op_lookupp, nullfree, (RPC_IDEMPOTENT|RPC_PUBLICFH_OK)},
339 
340 	/* OP_NVERIFY = 17 */
341 	{rfs4_op_nverify, nullfree, RPC_IDEMPOTENT},
342 
343 	/* OP_OPEN = 18 */
344 	{rfs4_op_open, rfs4_free_reply, 0},
345 
346 	/* OP_OPENATTR = 19 */
347 	{rfs4_op_openattr, nullfree, 0},
348 
349 	/* OP_OPEN_CONFIRM = 20 */
350 	{rfs4_op_open_confirm, nullfree, 0},
351 
352 	/* OP_OPEN_DOWNGRADE = 21 */
353 	{rfs4_op_open_downgrade, nullfree, 0},
354 
355 	/* OP_OPEN_PUTFH = 22 */
356 	{rfs4_op_putfh, nullfree, RPC_ALL},
357 
358 	/* OP_PUTPUBFH = 23 */
359 	{rfs4_op_putpubfh, nullfree, RPC_ALL},
360 
361 	/* OP_PUTROOTFH = 24 */
362 	{rfs4_op_putrootfh, nullfree, RPC_ALL},
363 
364 	/* OP_READ = 25 */
365 	{rfs4_op_read, rfs4_op_read_free, RPC_IDEMPOTENT},
366 
367 	/* OP_READDIR = 26 */
368 	{rfs4_op_readdir, rfs4_op_readdir_free, RPC_IDEMPOTENT},
369 
370 	/* OP_READLINK = 27 */
371 	{rfs4_op_readlink, rfs4_op_readlink_free, RPC_IDEMPOTENT},
372 
373 	/* OP_REMOVE = 28 */
374 	{rfs4_op_remove, nullfree, 0},
375 
376 	/* OP_RENAME = 29 */
377 	{rfs4_op_rename, nullfree, 0},
378 
379 	/* OP_RENEW = 30 */
380 	{rfs4_op_renew, nullfree, 0},
381 
382 	/* OP_RESTOREFH = 31 */
383 	{rfs4_op_restorefh, nullfree, RPC_ALL},
384 
385 	/* OP_SAVEFH = 32 */
386 	{rfs4_op_savefh, nullfree, RPC_ALL},
387 
388 	/* OP_SECINFO = 33 */
389 	{rfs4_op_secinfo, rfs4_op_secinfo_free, 0},
390 
391 	/* OP_SETATTR = 34 */
392 	{rfs4_op_setattr, nullfree, 0},
393 
394 	/* OP_SETCLIENTID = 35 */
395 	{rfs4_op_setclientid, nullfree, 0},
396 
397 	/* OP_SETCLIENTID_CONFIRM = 36 */
398 	{rfs4_op_setclientid_confirm, nullfree, 0},
399 
400 	/* OP_VERIFY = 37 */
401 	{rfs4_op_verify, nullfree, RPC_IDEMPOTENT},
402 
403 	/* OP_WRITE = 38 */
404 	{rfs4_op_write, nullfree, 0},
405 
406 	/* OP_RELEASE_LOCKOWNER = 39 */
407 	{rfs4_op_release_lockowner, nullfree, 0},
408 };
409 
410 static uint_t rfsv4disp_cnt = sizeof (rfsv4disptab) / sizeof (rfsv4disptab[0]);
411 
412 #define	OP_ILLEGAL_IDX (rfsv4disp_cnt)
413 
414 #ifdef DEBUG
415 
416 int rfs4_fillone_debug = 0;
417 int rfs4_shrlock_debug = 0;
418 int rfs4_no_stub_access = 1;
419 int rfs4_rddir_debug = 0;
420 
421 static char *rfs4_op_string[] = {
422 	"rfs4_op_null",
423 	"rfs4_op_1 unused",
424 	"rfs4_op_2 unused",
425 	"rfs4_op_access",
426 	"rfs4_op_close",
427 	"rfs4_op_commit",
428 	"rfs4_op_create",
429 	"rfs4_op_delegpurge",
430 	"rfs4_op_delegreturn",
431 	"rfs4_op_getattr",
432 	"rfs4_op_getfh",
433 	"rfs4_op_link",
434 	"rfs4_op_lock",
435 	"rfs4_op_lockt",
436 	"rfs4_op_locku",
437 	"rfs4_op_lookup",
438 	"rfs4_op_lookupp",
439 	"rfs4_op_nverify",
440 	"rfs4_op_open",
441 	"rfs4_op_openattr",
442 	"rfs4_op_open_confirm",
443 	"rfs4_op_open_downgrade",
444 	"rfs4_op_putfh",
445 	"rfs4_op_putpubfh",
446 	"rfs4_op_putrootfh",
447 	"rfs4_op_read",
448 	"rfs4_op_readdir",
449 	"rfs4_op_readlink",
450 	"rfs4_op_remove",
451 	"rfs4_op_rename",
452 	"rfs4_op_renew",
453 	"rfs4_op_restorefh",
454 	"rfs4_op_savefh",
455 	"rfs4_op_secinfo",
456 	"rfs4_op_setattr",
457 	"rfs4_op_setclientid",
458 	"rfs4_op_setclient_confirm",
459 	"rfs4_op_verify",
460 	"rfs4_op_write",
461 	"rfs4_op_release_lockowner",
462 	"rfs4_op_illegal"
463 };
464 #endif
465 
466 void rfs4_ss_chkclid(rfs4_client_t *);
467 
468 extern size_t strlcpy(char *dst, const char *src, size_t dstsize);
469 
470 #ifdef	nextdp
471 #undef nextdp
472 #endif
473 #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
474 
475 static const fs_operation_def_t nfs4_rd_deleg_tmpl[] = {
476 	VOPNAME_OPEN,		{ .femop_open = deleg_rd_open },
477 	VOPNAME_WRITE,		{ .femop_write = deleg_rd_write },
478 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_rd_setattr },
479 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_rd_rwlock },
480 	VOPNAME_SPACE,		{ .femop_space = deleg_rd_space },
481 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_rd_setsecattr },
482 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_rd_vnevent },
483 	NULL,			NULL
484 };
485 static const fs_operation_def_t nfs4_wr_deleg_tmpl[] = {
486 	VOPNAME_OPEN,		{ .femop_open = deleg_wr_open },
487 	VOPNAME_READ,		{ .femop_read = deleg_wr_read },
488 	VOPNAME_WRITE,		{ .femop_write = deleg_wr_write },
489 	VOPNAME_SETATTR,	{ .femop_setattr = deleg_wr_setattr },
490 	VOPNAME_RWLOCK,		{ .femop_rwlock = deleg_wr_rwlock },
491 	VOPNAME_SPACE,		{ .femop_space = deleg_wr_space },
492 	VOPNAME_SETSECATTR,	{ .femop_setsecattr = deleg_wr_setsecattr },
493 	VOPNAME_VNEVENT,	{ .femop_vnevent = deleg_wr_vnevent },
494 	NULL,			NULL
495 };
496 
497 int
498 rfs4_srvrinit(void)
499 {
500 	timespec32_t verf;
501 	int error;
502 	extern void rfs4_attr_init();
503 	extern krwlock_t rfs4_deleg_policy_lock;
504 
505 	/*
506 	 * The following algorithm attempts to find a unique verifier
507 	 * to be used as the write verifier returned from the server
508 	 * to the client.  It is important that this verifier change
509 	 * whenever the server reboots.  Of secondary importance, it
510 	 * is important for the verifier to be unique between two
511 	 * different servers.
512 	 *
513 	 * Thus, an attempt is made to use the system hostid and the
514 	 * current time in seconds when the nfssrv kernel module is
515 	 * loaded.  It is assumed that an NFS server will not be able
516 	 * to boot and then to reboot in less than a second.  If the
517 	 * hostid has not been set, then the current high resolution
518 	 * time is used.  This will ensure different verifiers each
519 	 * time the server reboots and minimize the chances that two
520 	 * different servers will have the same verifier.
521 	 * XXX - this is broken on LP64 kernels.
522 	 */
523 	verf.tv_sec = (time_t)nfs_atoi(hw_serial);
524 	if (verf.tv_sec != 0) {
525 		verf.tv_nsec = gethrestime_sec();
526 	} else {
527 		timespec_t tverf;
528 
529 		gethrestime(&tverf);
530 		verf.tv_sec = (time_t)tverf.tv_sec;
531 		verf.tv_nsec = tverf.tv_nsec;
532 	}
533 
534 	Write4verf = *(uint64_t *)&verf;
535 
536 	rfs4_attr_init();
537 	mutex_init(&rfs4_deleg_lock, NULL, MUTEX_DEFAULT, NULL);
538 
539 	/* Used to manage create/destroy of server state */
540 	mutex_init(&rfs4_state_lock, NULL, MUTEX_DEFAULT, NULL);
541 
542 	/* Used to manage access to server instance linked list */
543 	mutex_init(&rfs4_servinst_lock, NULL, MUTEX_DEFAULT, NULL);
544 
545 	/* Used to manage access to rfs4_deleg_policy */
546 	rw_init(&rfs4_deleg_policy_lock, NULL, RW_DEFAULT, NULL);
547 
548 	error = fem_create("deleg_rdops", nfs4_rd_deleg_tmpl, &deleg_rdops);
549 	if (error != 0) {
550 		rfs4_disable_delegation();
551 	} else {
552 		error = fem_create("deleg_wrops", nfs4_wr_deleg_tmpl,
553 		    &deleg_wrops);
554 		if (error != 0) {
555 			rfs4_disable_delegation();
556 			fem_free(deleg_rdops);
557 		}
558 	}
559 
560 	nfs4_srv_caller_id = fs_new_caller_id();
561 
562 	lockt_sysid = lm_alloc_sysidt();
563 
564 	vsd_create(&nfs4_srv_vkey, NULL);
565 
566 	return (0);
567 }
568 
569 void
570 rfs4_srvrfini(void)
571 {
572 	extern krwlock_t rfs4_deleg_policy_lock;
573 
574 	if (lockt_sysid != LM_NOSYSID) {
575 		lm_free_sysidt(lockt_sysid);
576 		lockt_sysid = LM_NOSYSID;
577 	}
578 
579 	mutex_destroy(&rfs4_deleg_lock);
580 	mutex_destroy(&rfs4_state_lock);
581 	rw_destroy(&rfs4_deleg_policy_lock);
582 
583 	fem_free(deleg_rdops);
584 	fem_free(deleg_wrops);
585 }
586 
587 void
588 rfs4_init_compound_state(struct compound_state *cs)
589 {
590 	bzero(cs, sizeof (*cs));
591 	cs->cont = TRUE;
592 	cs->access = CS_ACCESS_DENIED;
593 	cs->deleg = FALSE;
594 	cs->mandlock = FALSE;
595 	cs->fh.nfs_fh4_val = cs->fhbuf;
596 }
597 
598 void
599 rfs4_grace_start(rfs4_servinst_t *sip)
600 {
601 	time_t now = gethrestime_sec();
602 
603 	rw_enter(&sip->rwlock, RW_WRITER);
604 	sip->start_time = now;
605 	sip->grace_period = rfs4_grace_period;
606 	rw_exit(&sip->rwlock);
607 }
608 
609 /*
610  * returns true if the instance's grace period has never been started
611  */
612 int
613 rfs4_servinst_grace_new(rfs4_servinst_t *sip)
614 {
615 	time_t start_time;
616 
617 	rw_enter(&sip->rwlock, RW_READER);
618 	start_time = sip->start_time;
619 	rw_exit(&sip->rwlock);
620 
621 	return (start_time == 0);
622 }
623 
624 /*
625  * Indicates if server instance is within the
626  * grace period.
627  */
628 int
629 rfs4_servinst_in_grace(rfs4_servinst_t *sip)
630 {
631 	time_t grace_expiry;
632 
633 	rw_enter(&sip->rwlock, RW_READER);
634 	grace_expiry = sip->start_time + sip->grace_period;
635 	rw_exit(&sip->rwlock);
636 
637 	return (gethrestime_sec() < grace_expiry);
638 }
639 
640 int
641 rfs4_clnt_in_grace(rfs4_client_t *cp)
642 {
643 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
644 
645 	return (rfs4_servinst_in_grace(cp->server_instance));
646 }
647 
648 /*
649  * reset all currently active grace periods
650  */
651 void
652 rfs4_grace_reset_all(void)
653 {
654 	rfs4_servinst_t *sip;
655 
656 	mutex_enter(&rfs4_servinst_lock);
657 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
658 		if (rfs4_servinst_in_grace(sip))
659 			rfs4_grace_start(sip);
660 	mutex_exit(&rfs4_servinst_lock);
661 }
662 
663 /*
664  * start any new instances' grace periods
665  */
666 void
667 rfs4_grace_start_new(void)
668 {
669 	rfs4_servinst_t *sip;
670 
671 	mutex_enter(&rfs4_servinst_lock);
672 	for (sip = rfs4_cur_servinst; sip != NULL; sip = sip->prev)
673 		if (rfs4_servinst_grace_new(sip))
674 			rfs4_grace_start(sip);
675 	mutex_exit(&rfs4_servinst_lock);
676 }
677 
678 static rfs4_dss_path_t *
679 rfs4_dss_newpath(rfs4_servinst_t *sip, char *path, unsigned index)
680 {
681 	size_t len;
682 	rfs4_dss_path_t *dss_path;
683 
684 	dss_path = kmem_alloc(sizeof (rfs4_dss_path_t), KM_SLEEP);
685 
686 	/*
687 	 * Take a copy of the string, since the original may be overwritten.
688 	 * Sadly, no strdup() in the kernel.
689 	 */
690 	/* allow for NUL */
691 	len = strlen(path) + 1;
692 	dss_path->path = kmem_alloc(len, KM_SLEEP);
693 	(void) strlcpy(dss_path->path, path, len);
694 
695 	/* associate with servinst */
696 	dss_path->sip = sip;
697 	dss_path->index = index;
698 
699 	/*
700 	 * Add to list of served paths.
701 	 * No locking required, as we're only ever called at startup.
702 	 */
703 	if (rfs4_dss_pathlist == NULL) {
704 		/* this is the first dss_path_t */
705 
706 		/* needed for insque/remque */
707 		dss_path->next = dss_path->prev = dss_path;
708 
709 		rfs4_dss_pathlist = dss_path;
710 	} else {
711 		insque(dss_path, rfs4_dss_pathlist);
712 	}
713 
714 	return (dss_path);
715 }
716 
717 /*
718  * Create a new server instance, and make it the currently active instance.
719  * Note that starting the grace period too early will reduce the clients'
720  * recovery window.
721  */
722 void
723 rfs4_servinst_create(int start_grace, int dss_npaths, char **dss_paths)
724 {
725 	unsigned i;
726 	rfs4_servinst_t *sip;
727 	rfs4_oldstate_t *oldstate;
728 
729 	sip = kmem_alloc(sizeof (rfs4_servinst_t), KM_SLEEP);
730 	rw_init(&sip->rwlock, NULL, RW_DEFAULT, NULL);
731 
732 	sip->start_time = (time_t)0;
733 	sip->grace_period = (time_t)0;
734 	sip->next = NULL;
735 	sip->prev = NULL;
736 
737 	rw_init(&sip->oldstate_lock, NULL, RW_DEFAULT, NULL);
738 	/*
739 	 * This initial dummy entry is required to setup for insque/remque.
740 	 * It must be skipped over whenever the list is traversed.
741 	 */
742 	oldstate = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
743 	/* insque/remque require initial list entry to be self-terminated */
744 	oldstate->next = oldstate;
745 	oldstate->prev = oldstate;
746 	sip->oldstate = oldstate;
747 
748 
749 	sip->dss_npaths = dss_npaths;
750 	sip->dss_paths = kmem_alloc(dss_npaths *
751 	    sizeof (rfs4_dss_path_t *), KM_SLEEP);
752 
753 	for (i = 0; i < dss_npaths; i++) {
754 		sip->dss_paths[i] = rfs4_dss_newpath(sip, dss_paths[i], i);
755 	}
756 
757 	mutex_enter(&rfs4_servinst_lock);
758 	if (rfs4_cur_servinst != NULL) {
759 		/* add to linked list */
760 		sip->prev = rfs4_cur_servinst;
761 		rfs4_cur_servinst->next = sip;
762 	}
763 	if (start_grace)
764 		rfs4_grace_start(sip);
765 	/* make the new instance "current" */
766 	rfs4_cur_servinst = sip;
767 
768 	mutex_exit(&rfs4_servinst_lock);
769 }
770 
771 /*
772  * In future, we might add a rfs4_servinst_destroy(sip) but, for now, destroy
773  * all instances directly.
774  */
775 void
776 rfs4_servinst_destroy_all(void)
777 {
778 	rfs4_servinst_t *sip, *prev, *current;
779 #ifdef DEBUG
780 	int n = 0;
781 #endif
782 
783 	mutex_enter(&rfs4_servinst_lock);
784 	ASSERT(rfs4_cur_servinst != NULL);
785 	current = rfs4_cur_servinst;
786 	rfs4_cur_servinst = NULL;
787 	for (sip = current; sip != NULL; sip = prev) {
788 		prev = sip->prev;
789 		rw_destroy(&sip->rwlock);
790 		if (sip->oldstate)
791 			kmem_free(sip->oldstate, sizeof (rfs4_oldstate_t));
792 		if (sip->dss_paths)
793 			kmem_free(sip->dss_paths,
794 			    sip->dss_npaths * sizeof (rfs4_dss_path_t *));
795 		kmem_free(sip, sizeof (rfs4_servinst_t));
796 #ifdef DEBUG
797 		n++;
798 #endif
799 	}
800 	mutex_exit(&rfs4_servinst_lock);
801 }
802 
803 /*
804  * Assign the current server instance to a client_t.
805  * Should be called with cp->dbe held.
806  */
807 void
808 rfs4_servinst_assign(rfs4_client_t *cp, rfs4_servinst_t *sip)
809 {
810 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
811 
812 	/*
813 	 * The lock ensures that if the current instance is in the process
814 	 * of changing, we will see the new one.
815 	 */
816 	mutex_enter(&rfs4_servinst_lock);
817 	cp->server_instance = sip;
818 	mutex_exit(&rfs4_servinst_lock);
819 }
820 
821 rfs4_servinst_t *
822 rfs4_servinst(rfs4_client_t *cp)
823 {
824 	ASSERT(rfs4_dbe_refcnt(cp->dbe) > 0);
825 
826 	return (cp->server_instance);
827 }
828 
829 /* ARGSUSED */
830 static void
831 nullfree(caddr_t resop)
832 {
833 }
834 
835 /*
836  * This is a fall-through for invalid or not implemented (yet) ops
837  */
838 /* ARGSUSED */
839 static void
840 rfs4_op_inval(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
841 	struct compound_state *cs)
842 {
843 	*cs->statusp = *((nfsstat4 *)&(resop)->nfs_resop4_u) = NFS4ERR_INVAL;
844 }
845 
846 /*
847  * Check if the security flavor, nfsnum, is in the flavor_list.
848  */
849 bool_t
850 in_flavor_list(int nfsnum, int *flavor_list, int count)
851 {
852 	int i;
853 
854 	for (i = 0; i < count; i++) {
855 		if (nfsnum == flavor_list[i])
856 			return (TRUE);
857 	}
858 	return (FALSE);
859 }
860 
861 /*
862  * Used by rfs4_op_secinfo to get the security information from the
863  * export structure associated with the component.
864  */
865 /* ARGSUSED */
866 static nfsstat4
867 do_rfs4_op_secinfo(struct compound_state *cs, char *nm, SECINFO4res *resp)
868 {
869 	int error, different_export = 0;
870 	vnode_t *dvp, *vp, *tvp;
871 	struct exportinfo *exi = NULL;
872 	fid_t fid;
873 	uint_t count, i;
874 	secinfo4 *resok_val;
875 	struct secinfo *secp;
876 	seconfig_t *si;
877 	bool_t did_traverse;
878 	int dotdot, walk;
879 
880 	dvp = cs->vp;
881 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
882 
883 	/*
884 	 * If dotdotting, then need to check whether it's above the
885 	 * root of a filesystem, or above an export point.
886 	 */
887 	if (dotdot) {
888 
889 		/*
890 		 * If dotdotting at the root of a filesystem, then
891 		 * need to traverse back to the mounted-on filesystem
892 		 * and do the dotdot lookup there.
893 		 */
894 		if (cs->vp->v_flag & VROOT) {
895 
896 			/*
897 			 * If at the system root, then can
898 			 * go up no further.
899 			 */
900 			if (VN_CMP(dvp, rootdir))
901 				return (puterrno4(ENOENT));
902 
903 			/*
904 			 * Traverse back to the mounted-on filesystem
905 			 */
906 			dvp = untraverse(cs->vp);
907 
908 			/*
909 			 * Set the different_export flag so we remember
910 			 * to pick up a new exportinfo entry for
911 			 * this new filesystem.
912 			 */
913 			different_export = 1;
914 		} else {
915 
916 			/*
917 			 * If dotdotting above an export point then set
918 			 * the different_export to get new export info.
919 			 */
920 			different_export = nfs_exported(cs->exi, cs->vp);
921 		}
922 	}
923 
924 	/*
925 	 * Get the vnode for the component "nm".
926 	 */
927 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cs->cr,
928 	    NULL, NULL, NULL);
929 	if (error)
930 		return (puterrno4(error));
931 
932 	/*
933 	 * If the vnode is in a pseudo filesystem, or if the security flavor
934 	 * used in the request is valid but not an explicitly shared flavor,
935 	 * or the access bit indicates that this is a limited access,
936 	 * check whether this vnode is visible.
937 	 */
938 	if (!different_export &&
939 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
940 	    cs->access & CS_ACCESS_LIMITED)) {
941 		if (! nfs_visible(cs->exi, vp, &different_export)) {
942 			VN_RELE(vp);
943 			return (puterrno4(ENOENT));
944 		}
945 	}
946 
947 	/*
948 	 * If it's a mountpoint, then traverse it.
949 	 */
950 	if (vn_ismntpt(vp)) {
951 		tvp = vp;
952 		if ((error = traverse(&tvp)) != 0) {
953 			VN_RELE(vp);
954 			return (puterrno4(error));
955 		}
956 		/* remember that we had to traverse mountpoint */
957 		did_traverse = TRUE;
958 		vp = tvp;
959 		different_export = 1;
960 	} else if (vp->v_vfsp != dvp->v_vfsp) {
961 		/*
962 		 * If vp isn't a mountpoint and the vfs ptrs aren't the same,
963 		 * then vp is probably an LOFS object.  We don't need the
964 		 * realvp, we just need to know that we might have crossed
965 		 * a server fs boundary and need to call checkexport4.
966 		 * (LOFS lookup hides server fs mountpoints, and actually calls
967 		 * traverse)
968 		 */
969 		different_export = 1;
970 		did_traverse = FALSE;
971 	}
972 
973 	/*
974 	 * Get the export information for it.
975 	 */
976 	if (different_export) {
977 
978 		bzero(&fid, sizeof (fid));
979 		fid.fid_len = MAXFIDSZ;
980 		error = vop_fid_pseudo(vp, &fid);
981 		if (error) {
982 			VN_RELE(vp);
983 			return (puterrno4(error));
984 		}
985 
986 		if (dotdot)
987 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
988 		else
989 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
990 
991 		if (exi == NULL) {
992 			if (did_traverse == TRUE) {
993 				/*
994 				 * If this vnode is a mounted-on vnode,
995 				 * but the mounted-on file system is not
996 				 * exported, send back the secinfo for
997 				 * the exported node that the mounted-on
998 				 * vnode lives in.
999 				 */
1000 				exi = cs->exi;
1001 			} else {
1002 				VN_RELE(vp);
1003 				return (puterrno4(EACCES));
1004 			}
1005 		}
1006 	} else {
1007 		exi = cs->exi;
1008 	}
1009 	ASSERT(exi != NULL);
1010 
1011 
1012 	/*
1013 	 * Create the secinfo result based on the security information
1014 	 * from the exportinfo structure (exi).
1015 	 *
1016 	 * Return all flavors for a pseudo node.
1017 	 * For a real export node, return the flavor that the client
1018 	 * has access with.
1019 	 */
1020 	ASSERT(RW_LOCK_HELD(&exported_lock));
1021 	if (PSEUDO(exi)) {
1022 		count = exi->exi_export.ex_seccnt; /* total sec count */
1023 		resok_val = kmem_alloc(count * sizeof (secinfo4), KM_SLEEP);
1024 		secp = exi->exi_export.ex_secinfo;
1025 
1026 		for (i = 0; i < count; i++) {
1027 			si = &secp[i].s_secinfo;
1028 			resok_val[i].flavor = si->sc_rpcnum;
1029 			if (resok_val[i].flavor == RPCSEC_GSS) {
1030 				rpcsec_gss_info *info;
1031 
1032 				info = &resok_val[i].flavor_info;
1033 				info->qop = si->sc_qop;
1034 				info->service = (rpc_gss_svc_t)si->sc_service;
1035 
1036 				/* get oid opaque data */
1037 				info->oid.sec_oid4_len =
1038 				    si->sc_gss_mech_type->length;
1039 				info->oid.sec_oid4_val = kmem_alloc(
1040 				    si->sc_gss_mech_type->length, KM_SLEEP);
1041 				bcopy(
1042 				    si->sc_gss_mech_type->elements,
1043 				    info->oid.sec_oid4_val,
1044 				    info->oid.sec_oid4_len);
1045 			}
1046 		}
1047 		resp->SECINFO4resok_len = count;
1048 		resp->SECINFO4resok_val = resok_val;
1049 	} else {
1050 		int ret_cnt = 0, k = 0;
1051 		int *flavor_list;
1052 
1053 		count = exi->exi_export.ex_seccnt; /* total sec count */
1054 		secp = exi->exi_export.ex_secinfo;
1055 
1056 		flavor_list = kmem_alloc(count * sizeof (int), KM_SLEEP);
1057 		/* find out which flavors to return */
1058 		for (i = 0; i < count; i ++) {
1059 			int access, flavor, perm;
1060 
1061 			flavor = secp[i].s_secinfo.sc_nfsnum;
1062 			perm = secp[i].s_flags;
1063 
1064 			access = nfsauth4_secinfo_access(exi, cs->req,
1065 			    flavor, perm);
1066 
1067 			if (! (access & NFSAUTH_DENIED) &&
1068 			    ! (access & NFSAUTH_WRONGSEC)) {
1069 				flavor_list[ret_cnt] = flavor;
1070 				ret_cnt++;
1071 			}
1072 		}
1073 
1074 		/* Create the returning SECINFO value */
1075 		resok_val = kmem_alloc(ret_cnt * sizeof (secinfo4), KM_SLEEP);
1076 
1077 		for (i = 0; i < count; i++) {
1078 			/*
1079 			 * If the flavor is in the flavor list,
1080 			 * fill in resok_val.
1081 			 */
1082 			si = &secp[i].s_secinfo;
1083 			if (in_flavor_list(si->sc_nfsnum,
1084 			    flavor_list, ret_cnt)) {
1085 				resok_val[k].flavor = si->sc_rpcnum;
1086 				if (resok_val[k].flavor == RPCSEC_GSS) {
1087 					rpcsec_gss_info *info;
1088 
1089 					info = &resok_val[k].flavor_info;
1090 					info->qop = si->sc_qop;
1091 					info->service = (rpc_gss_svc_t)
1092 					    si->sc_service;
1093 
1094 					/* get oid opaque data */
1095 					info->oid.sec_oid4_len =
1096 					    si->sc_gss_mech_type->length;
1097 					info->oid.sec_oid4_val = kmem_alloc(
1098 					    si->sc_gss_mech_type->length,
1099 					    KM_SLEEP);
1100 					bcopy(si->sc_gss_mech_type->elements,
1101 					    info->oid.sec_oid4_val,
1102 					    info->oid.sec_oid4_len);
1103 				}
1104 				k++;
1105 			}
1106 			if (k >= ret_cnt)
1107 				break;
1108 		}
1109 		resp->SECINFO4resok_len = ret_cnt;
1110 		resp->SECINFO4resok_val = resok_val;
1111 		kmem_free(flavor_list, count * sizeof (int));
1112 	}
1113 
1114 	VN_RELE(vp);
1115 	return (NFS4_OK);
1116 }
1117 
1118 /*
1119  * SECINFO (Operation 33): Obtain required security information on
1120  * the component name in the format of (security-mechanism-oid, qop, service)
1121  * triplets.
1122  */
1123 /* ARGSUSED */
1124 static void
1125 rfs4_op_secinfo(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1126 	struct compound_state *cs)
1127 {
1128 	SECINFO4args *args = &argop->nfs_argop4_u.opsecinfo;
1129 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
1130 	utf8string *utfnm = &args->name;
1131 	uint_t len;
1132 	char *nm;
1133 
1134 	DTRACE_NFSV4_2(op__secinfo__start, struct compound_state *, cs,
1135 	    SECINFO4args *, args);
1136 
1137 	/*
1138 	 * Current file handle (cfh) should have been set before getting
1139 	 * into this function. If not, return error.
1140 	 */
1141 	if (cs->vp == NULL) {
1142 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1143 		goto out;
1144 	}
1145 
1146 	if (cs->vp->v_type != VDIR) {
1147 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
1148 		goto out;
1149 	}
1150 
1151 	/*
1152 	 * Verify the component name. If failed, error out, but
1153 	 * do not error out if the component name is a "..".
1154 	 * SECINFO will return its parents secinfo data for SECINFO "..".
1155 	 */
1156 	if (!utf8_dir_verify(utfnm)) {
1157 		if (utfnm->utf8string_len != 2 ||
1158 		    utfnm->utf8string_val[0] != '.' ||
1159 		    utfnm->utf8string_val[1] != '.') {
1160 			*cs->statusp = resp->status = NFS4ERR_INVAL;
1161 			goto out;
1162 		}
1163 	}
1164 
1165 	nm = utf8_to_str(utfnm, &len, NULL);
1166 	if (nm == NULL) {
1167 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1168 		goto out;
1169 	}
1170 
1171 	if (len > MAXNAMELEN) {
1172 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1173 		kmem_free(nm, len);
1174 		goto out;
1175 	}
1176 
1177 	*cs->statusp = resp->status = do_rfs4_op_secinfo(cs, nm, resp);
1178 
1179 	kmem_free(nm, len);
1180 
1181 out:
1182 	DTRACE_NFSV4_2(op__secinfo__done, struct compound_state *, cs,
1183 	    SECINFO4res *, resp);
1184 }
1185 
1186 /*
1187  * Free SECINFO result.
1188  */
1189 /* ARGSUSED */
1190 static void
1191 rfs4_op_secinfo_free(nfs_resop4 *resop)
1192 {
1193 	SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
1194 	int count, i;
1195 	secinfo4 *resok_val;
1196 
1197 	/* If this is not an Ok result, nothing to free. */
1198 	if (resp->status != NFS4_OK) {
1199 		return;
1200 	}
1201 
1202 	count = resp->SECINFO4resok_len;
1203 	resok_val = resp->SECINFO4resok_val;
1204 
1205 	for (i = 0; i < count; i++) {
1206 		if (resok_val[i].flavor == RPCSEC_GSS) {
1207 			rpcsec_gss_info *info;
1208 
1209 			info = &resok_val[i].flavor_info;
1210 			kmem_free(info->oid.sec_oid4_val,
1211 			    info->oid.sec_oid4_len);
1212 		}
1213 	}
1214 	kmem_free(resok_val, count * sizeof (secinfo4));
1215 	resp->SECINFO4resok_len = 0;
1216 	resp->SECINFO4resok_val = NULL;
1217 }
1218 
1219 /* ARGSUSED */
1220 static void
1221 rfs4_op_access(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1222 	struct compound_state *cs)
1223 {
1224 	ACCESS4args *args = &argop->nfs_argop4_u.opaccess;
1225 	ACCESS4res *resp = &resop->nfs_resop4_u.opaccess;
1226 	int error;
1227 	vnode_t *vp;
1228 	struct vattr va;
1229 	int checkwriteperm;
1230 	cred_t *cr = cs->cr;
1231 	bslabel_t *clabel, *slabel;
1232 	ts_label_t *tslabel;
1233 	boolean_t admin_low_client;
1234 
1235 	DTRACE_NFSV4_2(op__access__start, struct compound_state *, cs,
1236 	    ACCESS4args *, args);
1237 
1238 #if 0	/* XXX allow access even if !cs->access. Eventually only pseudo fs */
1239 	if (cs->access == CS_ACCESS_DENIED) {
1240 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1241 		goto out;
1242 	}
1243 #endif
1244 	if (cs->vp == NULL) {
1245 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1246 		goto out;
1247 	}
1248 
1249 	ASSERT(cr != NULL);
1250 
1251 	vp = cs->vp;
1252 
1253 	/*
1254 	 * If the file system is exported read only, it is not appropriate
1255 	 * to check write permissions for regular files and directories.
1256 	 * Special files are interpreted by the client, so the underlying
1257 	 * permissions are sent back to the client for interpretation.
1258 	 */
1259 	if (rdonly4(cs->exi, cs->vp, req) &&
1260 	    (vp->v_type == VREG || vp->v_type == VDIR))
1261 		checkwriteperm = 0;
1262 	else
1263 		checkwriteperm = 1;
1264 
1265 	/*
1266 	 * XXX
1267 	 * We need the mode so that we can correctly determine access
1268 	 * permissions relative to a mandatory lock file.  Access to
1269 	 * mandatory lock files is denied on the server, so it might
1270 	 * as well be reflected to the server during the open.
1271 	 */
1272 	va.va_mask = AT_MODE;
1273 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
1274 	if (error) {
1275 		*cs->statusp = resp->status = puterrno4(error);
1276 		goto out;
1277 	}
1278 	resp->access = 0;
1279 	resp->supported = 0;
1280 
1281 	if (is_system_labeled()) {
1282 		ASSERT(req->rq_label != NULL);
1283 		clabel = req->rq_label;
1284 		DTRACE_PROBE2(tx__rfs4__log__info__opaccess__clabel, char *,
1285 		    "got client label from request(1)",
1286 		    struct svc_req *, req);
1287 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1288 			if ((tslabel = nfs_getflabel(vp)) == NULL) {
1289 				*cs->statusp = resp->status = puterrno4(EACCES);
1290 				goto out;
1291 			}
1292 			slabel = label2bslabel(tslabel);
1293 			DTRACE_PROBE3(tx__rfs4__log__info__opaccess__slabel,
1294 			    char *, "got server label(1) for vp(2)",
1295 			    bslabel_t *, slabel, vnode_t *, vp);
1296 
1297 			admin_low_client = B_FALSE;
1298 		} else
1299 			admin_low_client = B_TRUE;
1300 	}
1301 
1302 	if (args->access & ACCESS4_READ) {
1303 		error = VOP_ACCESS(vp, VREAD, 0, cr, NULL);
1304 		if (!error && !MANDLOCK(vp, va.va_mode) &&
1305 		    (!is_system_labeled() || admin_low_client ||
1306 		    bldominates(clabel, slabel)))
1307 			resp->access |= ACCESS4_READ;
1308 		resp->supported |= ACCESS4_READ;
1309 	}
1310 	if ((args->access & ACCESS4_LOOKUP) && vp->v_type == VDIR) {
1311 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
1312 		if (!error && (!is_system_labeled() || admin_low_client ||
1313 		    bldominates(clabel, slabel)))
1314 			resp->access |= ACCESS4_LOOKUP;
1315 		resp->supported |= ACCESS4_LOOKUP;
1316 	}
1317 	if (checkwriteperm &&
1318 	    (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND))) {
1319 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
1320 		if (!error && !MANDLOCK(vp, va.va_mode) &&
1321 		    (!is_system_labeled() || admin_low_client ||
1322 		    blequal(clabel, slabel)))
1323 			resp->access |=
1324 			    (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND));
1325 		resp->supported |= (ACCESS4_MODIFY|ACCESS4_EXTEND);
1326 	}
1327 
1328 	if (checkwriteperm &&
1329 	    (args->access & ACCESS4_DELETE) && vp->v_type == VDIR) {
1330 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
1331 		if (!error && (!is_system_labeled() || admin_low_client ||
1332 		    blequal(clabel, slabel)))
1333 			resp->access |= ACCESS4_DELETE;
1334 		resp->supported |= ACCESS4_DELETE;
1335 	}
1336 	if (args->access & ACCESS4_EXECUTE && vp->v_type != VDIR) {
1337 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
1338 		if (!error && !MANDLOCK(vp, va.va_mode) &&
1339 		    (!is_system_labeled() || admin_low_client ||
1340 		    bldominates(clabel, slabel)))
1341 			resp->access |= ACCESS4_EXECUTE;
1342 		resp->supported |= ACCESS4_EXECUTE;
1343 	}
1344 
1345 	if (is_system_labeled() && !admin_low_client)
1346 		label_rele(tslabel);
1347 
1348 	*cs->statusp = resp->status = NFS4_OK;
1349 out:
1350 	DTRACE_NFSV4_2(op__access__done, struct compound_state *, cs,
1351 	    ACCESS4res *, resp);
1352 }
1353 
1354 /* ARGSUSED */
1355 static void
1356 rfs4_op_commit(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1357 	struct compound_state *cs)
1358 {
1359 	COMMIT4args *args = &argop->nfs_argop4_u.opcommit;
1360 	COMMIT4res *resp = &resop->nfs_resop4_u.opcommit;
1361 	int error;
1362 	vnode_t *vp = cs->vp;
1363 	cred_t *cr = cs->cr;
1364 	vattr_t va;
1365 
1366 	DTRACE_NFSV4_2(op__commit__start, struct compound_state *, cs,
1367 	    COMMIT4args *, args);
1368 
1369 	if (vp == NULL) {
1370 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1371 		goto out;
1372 	}
1373 	if (cs->access == CS_ACCESS_DENIED) {
1374 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1375 		goto out;
1376 	}
1377 
1378 	if (args->offset + args->count < args->offset) {
1379 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1380 		goto out;
1381 	}
1382 
1383 	va.va_mask = AT_UID;
1384 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
1385 
1386 	/*
1387 	 * If we can't get the attributes, then we can't do the
1388 	 * right access checking.  So, we'll fail the request.
1389 	 */
1390 	if (error) {
1391 		*cs->statusp = resp->status = puterrno4(error);
1392 		goto out;
1393 	}
1394 	if (rdonly4(cs->exi, cs->vp, req)) {
1395 		*cs->statusp = resp->status = NFS4ERR_ROFS;
1396 		goto out;
1397 	}
1398 
1399 	if (vp->v_type != VREG) {
1400 		if (vp->v_type == VDIR)
1401 			resp->status = NFS4ERR_ISDIR;
1402 		else
1403 			resp->status = NFS4ERR_INVAL;
1404 		*cs->statusp = resp->status;
1405 		goto out;
1406 	}
1407 
1408 	if (crgetuid(cr) != va.va_uid &&
1409 	    (error = VOP_ACCESS(vp, VWRITE, 0, cs->cr, NULL))) {
1410 		*cs->statusp = resp->status = puterrno4(error);
1411 		goto out;
1412 	}
1413 
1414 	error = VOP_PUTPAGE(vp, args->offset, args->count, 0, cr, NULL);
1415 	if (!error)
1416 		error = VOP_FSYNC(vp, FNODSYNC, cr, NULL);
1417 
1418 	if (error) {
1419 		*cs->statusp = resp->status = puterrno4(error);
1420 		goto out;
1421 	}
1422 
1423 	*cs->statusp = resp->status = NFS4_OK;
1424 	resp->writeverf = Write4verf;
1425 out:
1426 	DTRACE_NFSV4_2(op__commit__done, struct compound_state *, cs,
1427 	    COMMIT4res *, resp);
1428 }
1429 
1430 /*
1431  * rfs4_op_mknod is called from rfs4_op_create after all initial verification
1432  * was completed. It does the nfsv4 create for special files.
1433  */
1434 /* ARGSUSED */
1435 static vnode_t *
1436 do_rfs4_op_mknod(CREATE4args *args, CREATE4res *resp, struct svc_req *req,
1437 	struct compound_state *cs, vattr_t *vap, char *nm)
1438 {
1439 	int error;
1440 	cred_t *cr = cs->cr;
1441 	vnode_t *dvp = cs->vp;
1442 	vnode_t *vp = NULL;
1443 	int mode;
1444 	enum vcexcl excl;
1445 
1446 	switch (args->type) {
1447 	case NF4CHR:
1448 	case NF4BLK:
1449 		if (secpolicy_sys_devices(cr) != 0) {
1450 			*cs->statusp = resp->status = NFS4ERR_PERM;
1451 			return (NULL);
1452 		}
1453 		if (args->type == NF4CHR)
1454 			vap->va_type = VCHR;
1455 		else
1456 			vap->va_type = VBLK;
1457 		vap->va_rdev = makedevice(args->ftype4_u.devdata.specdata1,
1458 		    args->ftype4_u.devdata.specdata2);
1459 		vap->va_mask |= AT_RDEV;
1460 		break;
1461 	case NF4SOCK:
1462 		vap->va_type = VSOCK;
1463 		break;
1464 	case NF4FIFO:
1465 		vap->va_type = VFIFO;
1466 		break;
1467 	default:
1468 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
1469 		return (NULL);
1470 	}
1471 
1472 	/*
1473 	 * Must specify the mode.
1474 	 */
1475 	if (!(vap->va_mask & AT_MODE)) {
1476 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1477 		return (NULL);
1478 	}
1479 
1480 	excl = EXCL;
1481 
1482 	mode = 0;
1483 
1484 	error = VOP_CREATE(dvp, nm, vap, excl, mode, &vp, cr, 0, NULL, NULL);
1485 	if (error) {
1486 		*cs->statusp = resp->status = puterrno4(error);
1487 		return (NULL);
1488 	}
1489 	return (vp);
1490 }
1491 
1492 /*
1493  * nfsv4 create is used to create non-regular files. For regular files,
1494  * use nfsv4 open.
1495  */
1496 /* ARGSUSED */
1497 static void
1498 rfs4_op_create(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1499 	struct compound_state *cs)
1500 {
1501 	CREATE4args *args = &argop->nfs_argop4_u.opcreate;
1502 	CREATE4res *resp = &resop->nfs_resop4_u.opcreate;
1503 	int error;
1504 	struct vattr bva, iva, iva2, ava, *vap;
1505 	cred_t *cr = cs->cr;
1506 	vnode_t *dvp = cs->vp;
1507 	vnode_t *vp = NULL;
1508 	char *nm, *lnm;
1509 	uint_t len, llen;
1510 	int syncval = 0;
1511 	struct nfs4_svgetit_arg sarg;
1512 	struct nfs4_ntov_table ntov;
1513 	struct statvfs64 sb;
1514 	nfsstat4 status;
1515 
1516 	DTRACE_NFSV4_2(op__create__start, struct compound_state *, cs,
1517 	    CREATE4args *, args);
1518 
1519 	resp->attrset = 0;
1520 
1521 	if (dvp == NULL) {
1522 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1523 		goto out;
1524 	}
1525 
1526 	/*
1527 	 * If there is an unshared filesystem mounted on this vnode,
1528 	 * do not allow to create an object in this directory.
1529 	 */
1530 	if (vn_ismntpt(dvp)) {
1531 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1532 		goto out;
1533 	}
1534 
1535 	/* Verify that type is correct */
1536 	switch (args->type) {
1537 	case NF4LNK:
1538 	case NF4BLK:
1539 	case NF4CHR:
1540 	case NF4SOCK:
1541 	case NF4FIFO:
1542 	case NF4DIR:
1543 		break;
1544 	default:
1545 		*cs->statusp = resp->status = NFS4ERR_BADTYPE;
1546 		goto out;
1547 	};
1548 
1549 	if (cs->access == CS_ACCESS_DENIED) {
1550 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
1551 		goto out;
1552 	}
1553 	if (dvp->v_type != VDIR) {
1554 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
1555 		goto out;
1556 	}
1557 	if (!utf8_dir_verify(&args->objname)) {
1558 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1559 		goto out;
1560 	}
1561 
1562 	if (rdonly4(cs->exi, cs->vp, req)) {
1563 		*cs->statusp = resp->status = NFS4ERR_ROFS;
1564 		goto out;
1565 	}
1566 
1567 	/*
1568 	 * Name of newly created object
1569 	 */
1570 	nm = utf8_to_fn(&args->objname, &len, NULL);
1571 	if (nm == NULL) {
1572 		*cs->statusp = resp->status = NFS4ERR_INVAL;
1573 		goto out;
1574 	}
1575 
1576 	if (len > MAXNAMELEN) {
1577 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1578 		kmem_free(nm, len);
1579 		goto out;
1580 	}
1581 
1582 	resp->attrset = 0;
1583 
1584 	sarg.sbp = &sb;
1585 	nfs4_ntov_table_init(&ntov);
1586 
1587 	status = do_rfs4_set_attrs(&resp->attrset,
1588 	    &args->createattrs, cs, &sarg, &ntov, NFS4ATTR_SETIT);
1589 
1590 	if (sarg.vap->va_mask == 0 && status == NFS4_OK)
1591 		status = NFS4ERR_INVAL;
1592 
1593 	if (status != NFS4_OK) {
1594 		*cs->statusp = resp->status = status;
1595 		kmem_free(nm, len);
1596 		nfs4_ntov_table_free(&ntov, &sarg);
1597 		resp->attrset = 0;
1598 		goto out;
1599 	}
1600 
1601 	/* Get "before" change value */
1602 	bva.va_mask = AT_CTIME|AT_SEQ;
1603 	error = VOP_GETATTR(dvp, &bva, 0, cr, NULL);
1604 	if (error) {
1605 		*cs->statusp = resp->status = puterrno4(error);
1606 		kmem_free(nm, len);
1607 		nfs4_ntov_table_free(&ntov, &sarg);
1608 		resp->attrset = 0;
1609 		goto out;
1610 	}
1611 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bva.va_ctime)
1612 
1613 	vap = sarg.vap;
1614 
1615 	/*
1616 	 * Set default initial values for attributes when not specified
1617 	 * in createattrs.
1618 	 */
1619 	if ((vap->va_mask & AT_UID) == 0) {
1620 		vap->va_uid = crgetuid(cr);
1621 		vap->va_mask |= AT_UID;
1622 	}
1623 	if ((vap->va_mask & AT_GID) == 0) {
1624 		vap->va_gid = crgetgid(cr);
1625 		vap->va_mask |= AT_GID;
1626 	}
1627 
1628 	vap->va_mask |= AT_TYPE;
1629 	switch (args->type) {
1630 	case NF4DIR:
1631 		vap->va_type = VDIR;
1632 		if ((vap->va_mask & AT_MODE) == 0) {
1633 			vap->va_mode = 0700;	/* default: owner rwx only */
1634 			vap->va_mask |= AT_MODE;
1635 		}
1636 		error = VOP_MKDIR(dvp, nm, vap, &vp, cr, NULL, 0, NULL);
1637 		if (error)
1638 			break;
1639 
1640 		/*
1641 		 * Get the initial "after" sequence number, if it fails,
1642 		 * set to zero
1643 		 */
1644 		iva.va_mask = AT_SEQ;
1645 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1646 			iva.va_seq = 0;
1647 		break;
1648 	case NF4LNK:
1649 		vap->va_type = VLNK;
1650 		if ((vap->va_mask & AT_MODE) == 0) {
1651 			vap->va_mode = 0700;	/* default: owner rwx only */
1652 			vap->va_mask |= AT_MODE;
1653 		}
1654 
1655 		/*
1656 		 * symlink names must be treated as data
1657 		 */
1658 		lnm = utf8_to_str(&args->ftype4_u.linkdata, &llen, NULL);
1659 
1660 		if (lnm == NULL) {
1661 			*cs->statusp = resp->status = NFS4ERR_INVAL;
1662 			kmem_free(nm, len);
1663 			nfs4_ntov_table_free(&ntov, &sarg);
1664 			resp->attrset = 0;
1665 			goto out;
1666 		}
1667 
1668 		if (llen > MAXPATHLEN) {
1669 			*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1670 			kmem_free(nm, len);
1671 			kmem_free(lnm, llen);
1672 			nfs4_ntov_table_free(&ntov, &sarg);
1673 			resp->attrset = 0;
1674 			goto out;
1675 		}
1676 
1677 		error = VOP_SYMLINK(dvp, nm, vap, lnm, cr, NULL, 0);
1678 		if (lnm != NULL)
1679 			kmem_free(lnm, llen);
1680 		if (error)
1681 			break;
1682 
1683 		/*
1684 		 * Get the initial "after" sequence number, if it fails,
1685 		 * set to zero
1686 		 */
1687 		iva.va_mask = AT_SEQ;
1688 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1689 			iva.va_seq = 0;
1690 
1691 		error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr,
1692 		    NULL, NULL, NULL);
1693 		if (error)
1694 			break;
1695 
1696 		/*
1697 		 * va_seq is not safe over VOP calls, check it again
1698 		 * if it has changed zero out iva to force atomic = FALSE.
1699 		 */
1700 		iva2.va_mask = AT_SEQ;
1701 		if (VOP_GETATTR(dvp, &iva2, 0, cs->cr, NULL) ||
1702 		    iva2.va_seq != iva.va_seq)
1703 			iva.va_seq = 0;
1704 		break;
1705 	default:
1706 		/*
1707 		 * probably a special file.
1708 		 */
1709 		if ((vap->va_mask & AT_MODE) == 0) {
1710 			vap->va_mode = 0600;	/* default: owner rw only */
1711 			vap->va_mask |= AT_MODE;
1712 		}
1713 		syncval = FNODSYNC;
1714 		/*
1715 		 * We know this will only generate one VOP call
1716 		 */
1717 		vp = do_rfs4_op_mknod(args, resp, req, cs, vap, nm);
1718 
1719 		if (vp == NULL) {
1720 			kmem_free(nm, len);
1721 			nfs4_ntov_table_free(&ntov, &sarg);
1722 			resp->attrset = 0;
1723 			goto out;
1724 		}
1725 
1726 		/*
1727 		 * Get the initial "after" sequence number, if it fails,
1728 		 * set to zero
1729 		 */
1730 		iva.va_mask = AT_SEQ;
1731 		if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1732 			iva.va_seq = 0;
1733 
1734 		break;
1735 	}
1736 	kmem_free(nm, len);
1737 
1738 	if (error) {
1739 		*cs->statusp = resp->status = puterrno4(error);
1740 	}
1741 
1742 	/*
1743 	 * Force modified data and metadata out to stable storage.
1744 	 */
1745 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
1746 
1747 	if (resp->status != NFS4_OK) {
1748 		if (vp != NULL)
1749 			VN_RELE(vp);
1750 		nfs4_ntov_table_free(&ntov, &sarg);
1751 		resp->attrset = 0;
1752 		goto out;
1753 	}
1754 
1755 	/*
1756 	 * Finish setup of cinfo response, "before" value already set.
1757 	 * Get "after" change value, if it fails, simply return the
1758 	 * before value.
1759 	 */
1760 	ava.va_mask = AT_CTIME|AT_SEQ;
1761 	if (VOP_GETATTR(dvp, &ava, 0, cr, NULL)) {
1762 		ava.va_ctime = bva.va_ctime;
1763 		ava.va_seq = 0;
1764 	}
1765 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, ava.va_ctime);
1766 
1767 	/*
1768 	 * True verification that object was created with correct
1769 	 * attrs is impossible.  The attrs could have been changed
1770 	 * immediately after object creation.  If attributes did
1771 	 * not verify, the only recourse for the server is to
1772 	 * destroy the object.  Maybe if some attrs (like gid)
1773 	 * are set incorrectly, the object should be destroyed;
1774 	 * however, seems bad as a default policy.  Do we really
1775 	 * want to destroy an object over one of the times not
1776 	 * verifying correctly?  For these reasons, the server
1777 	 * currently sets bits in attrset for createattrs
1778 	 * that were set; however, no verification is done.
1779 	 *
1780 	 * vmask_to_nmask accounts for vattr bits set on create
1781 	 *	[do_rfs4_set_attrs() only sets resp bits for
1782 	 *	 non-vattr/vfs bits.]
1783 	 * Mask off any bits set by default so as not to return
1784 	 * more attrset bits than were requested in createattrs
1785 	 */
1786 	nfs4_vmask_to_nmask(sarg.vap->va_mask, &resp->attrset);
1787 	resp->attrset &= args->createattrs.attrmask;
1788 	nfs4_ntov_table_free(&ntov, &sarg);
1789 
1790 	error = makefh4(&cs->fh, vp, cs->exi);
1791 	if (error) {
1792 		*cs->statusp = resp->status = puterrno4(error);
1793 	}
1794 
1795 	/*
1796 	 * The cinfo.atomic = TRUE only if we got no errors, we have
1797 	 * non-zero va_seq's, and it has incremented by exactly one
1798 	 * during the creation and it didn't change during the VOP_LOOKUP
1799 	 * or VOP_FSYNC.
1800 	 */
1801 	if (!error && bva.va_seq && iva.va_seq && ava.va_seq &&
1802 	    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
1803 		resp->cinfo.atomic = TRUE;
1804 	else
1805 		resp->cinfo.atomic = FALSE;
1806 
1807 	(void) VOP_FSYNC(vp, syncval, cr, NULL);
1808 
1809 	if (resp->status != NFS4_OK) {
1810 		VN_RELE(vp);
1811 		goto out;
1812 	}
1813 	if (cs->vp)
1814 		VN_RELE(cs->vp);
1815 
1816 	cs->vp = vp;
1817 	*cs->statusp = resp->status = NFS4_OK;
1818 out:
1819 	DTRACE_NFSV4_2(op__create__done, struct compound_state *, cs,
1820 	    CREATE4res *, resp);
1821 }
1822 
1823 /*ARGSUSED*/
1824 static void
1825 rfs4_op_delegpurge(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1826     struct compound_state *cs)
1827 {
1828 	DTRACE_NFSV4_2(op__delegpurge__start, struct compound_state *, cs,
1829 	    DELEGPURGE4args *, &argop->nfs_argop4_u.opdelegpurge);
1830 
1831 	rfs4_op_inval(argop, resop, req, cs);
1832 
1833 	DTRACE_NFSV4_2(op__delegpurge__done, struct compound_state *, cs,
1834 	    DELEGPURGE4res *, &resop->nfs_resop4_u.opdelegpurge);
1835 }
1836 
1837 /*ARGSUSED*/
1838 static void
1839 rfs4_op_delegreturn(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1840 	struct compound_state *cs)
1841 {
1842 	DELEGRETURN4args *args = &argop->nfs_argop4_u.opdelegreturn;
1843 	DELEGRETURN4res *resp = &resop->nfs_resop4_u.opdelegreturn;
1844 	rfs4_deleg_state_t *dsp;
1845 	nfsstat4 status;
1846 
1847 	DTRACE_NFSV4_2(op__delegreturn__start, struct compound_state *, cs,
1848 	    DELEGRETURN4args *, args);
1849 
1850 	status = rfs4_get_deleg_state(&args->deleg_stateid, &dsp);
1851 	resp->status = *cs->statusp = status;
1852 	if (status != NFS4_OK)
1853 		goto out;
1854 
1855 	/* Ensure specified filehandle matches */
1856 	if (cs->vp != dsp->finfo->vp) {
1857 		resp->status = *cs->statusp = NFS4ERR_BAD_STATEID;
1858 	} else
1859 		rfs4_return_deleg(dsp, FALSE);
1860 
1861 	rfs4_update_lease(dsp->client);
1862 
1863 	rfs4_deleg_state_rele(dsp);
1864 out:
1865 	DTRACE_NFSV4_2(op__delegreturn__done, struct compound_state *, cs,
1866 	    DELEGRETURN4res *, resp);
1867 }
1868 
1869 /*
1870  * Check to see if a given "flavor" is an explicitly shared flavor.
1871  * The assumption of this routine is the "flavor" is already a valid
1872  * flavor in the secinfo list of "exi".
1873  *
1874  *	e.g.
1875  *		# share -o sec=flavor1 /export
1876  *		# share -o sec=flavor2 /export/home
1877  *
1878  *		flavor2 is not an explicitly shared flavor for /export,
1879  *		however it is in the secinfo list for /export thru the
1880  *		server namespace setup.
1881  */
1882 int
1883 is_exported_sec(int flavor, struct exportinfo *exi)
1884 {
1885 	int	i;
1886 	struct secinfo *sp;
1887 
1888 	sp = exi->exi_export.ex_secinfo;
1889 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1890 		if (flavor == sp[i].s_secinfo.sc_nfsnum ||
1891 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1892 			return (SEC_REF_EXPORTED(&sp[i]));
1893 		}
1894 	}
1895 
1896 	/* Should not reach this point based on the assumption */
1897 	return (0);
1898 }
1899 
1900 /*
1901  * Check if the security flavor used in the request matches what is
1902  * required at the export point or at the root pseudo node (exi_root).
1903  *
1904  * returns 1 if there's a match or if exported with AUTH_NONE; 0 otherwise.
1905  *
1906  */
1907 static int
1908 secinfo_match_or_authnone(struct compound_state *cs)
1909 {
1910 	int	i;
1911 	struct secinfo *sp;
1912 
1913 	/*
1914 	 * Check cs->nfsflavor (from the request) against
1915 	 * the current export data in cs->exi.
1916 	 */
1917 	sp = cs->exi->exi_export.ex_secinfo;
1918 	for (i = 0; i < cs->exi->exi_export.ex_seccnt; i++) {
1919 		if (cs->nfsflavor == sp[i].s_secinfo.sc_nfsnum ||
1920 		    sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1921 			return (1);
1922 	}
1923 
1924 	return (0);
1925 }
1926 
1927 /*
1928  * Check the access authority for the client and return the correct error.
1929  */
1930 nfsstat4
1931 call_checkauth4(struct compound_state *cs, struct svc_req *req)
1932 {
1933 	int	authres;
1934 
1935 	/*
1936 	 * First, check if the security flavor used in the request
1937 	 * are among the flavors set in the server namespace.
1938 	 */
1939 	if (!secinfo_match_or_authnone(cs)) {
1940 		*cs->statusp = NFS4ERR_WRONGSEC;
1941 		return (*cs->statusp);
1942 	}
1943 
1944 	authres = checkauth4(cs, req);
1945 
1946 	if (authres > 0) {
1947 		*cs->statusp = NFS4_OK;
1948 		if (! (cs->access & CS_ACCESS_LIMITED))
1949 			cs->access = CS_ACCESS_OK;
1950 	} else if (authres == 0) {
1951 		*cs->statusp = NFS4ERR_ACCESS;
1952 	} else if (authres == -2) {
1953 		*cs->statusp = NFS4ERR_WRONGSEC;
1954 	} else {
1955 		*cs->statusp = NFS4ERR_DELAY;
1956 	}
1957 	return (*cs->statusp);
1958 }
1959 
1960 /*
1961  * bitmap4_to_attrmask is called by getattr and readdir.
1962  * It sets up the vattr mask and determines whether vfsstat call is needed
1963  * based on the input bitmap.
1964  * Returns nfsv4 status.
1965  */
1966 static nfsstat4
1967 bitmap4_to_attrmask(bitmap4 breq, struct nfs4_svgetit_arg *sargp)
1968 {
1969 	int i;
1970 	uint_t	va_mask;
1971 	struct statvfs64 *sbp = sargp->sbp;
1972 
1973 	sargp->sbp = NULL;
1974 	sargp->flag = 0;
1975 	sargp->rdattr_error = NFS4_OK;
1976 	sargp->mntdfid_set = FALSE;
1977 	if (sargp->cs->vp)
1978 		sargp->xattr = get_fh4_flag(&sargp->cs->fh,
1979 		    FH4_ATTRDIR | FH4_NAMEDATTR);
1980 	else
1981 		sargp->xattr = 0;
1982 
1983 	/*
1984 	 * Set rdattr_error_req to true if return error per
1985 	 * failed entry rather than fail the readdir.
1986 	 */
1987 	if (breq & FATTR4_RDATTR_ERROR_MASK)
1988 		sargp->rdattr_error_req = 1;
1989 	else
1990 		sargp->rdattr_error_req = 0;
1991 
1992 	/*
1993 	 * generate the va_mask
1994 	 * Handle the easy cases first
1995 	 */
1996 	switch (breq) {
1997 	case NFS4_NTOV_ATTR_MASK:
1998 		sargp->vap->va_mask = NFS4_NTOV_ATTR_AT_MASK;
1999 		return (NFS4_OK);
2000 
2001 	case NFS4_FS_ATTR_MASK:
2002 		sargp->vap->va_mask = NFS4_FS_ATTR_AT_MASK;
2003 		sargp->sbp = sbp;
2004 		return (NFS4_OK);
2005 
2006 	case NFS4_NTOV_ATTR_CACHE_MASK:
2007 		sargp->vap->va_mask = NFS4_NTOV_ATTR_CACHE_AT_MASK;
2008 		return (NFS4_OK);
2009 
2010 	case FATTR4_LEASE_TIME_MASK:
2011 		sargp->vap->va_mask = 0;
2012 		return (NFS4_OK);
2013 
2014 	default:
2015 		va_mask = 0;
2016 		for (i = 0; i < nfs4_ntov_map_size; i++) {
2017 			if ((breq & nfs4_ntov_map[i].fbit) &&
2018 			    nfs4_ntov_map[i].vbit)
2019 				va_mask |= nfs4_ntov_map[i].vbit;
2020 		}
2021 
2022 		/*
2023 		 * Check is vfsstat is needed
2024 		 */
2025 		if (breq & NFS4_FS_ATTR_MASK)
2026 			sargp->sbp = sbp;
2027 
2028 		sargp->vap->va_mask = va_mask;
2029 		return (NFS4_OK);
2030 	}
2031 	/* NOTREACHED */
2032 }
2033 
2034 /*
2035  * bitmap4_get_sysattrs is called by getattr and readdir.
2036  * It calls both VOP_GETATTR and VFS_STATVFS calls to get the attrs.
2037  * Returns nfsv4 status.
2038  */
2039 static nfsstat4
2040 bitmap4_get_sysattrs(struct nfs4_svgetit_arg *sargp)
2041 {
2042 	int error;
2043 	struct compound_state *cs = sargp->cs;
2044 	vnode_t *vp = cs->vp;
2045 
2046 	if (sargp->sbp != NULL) {
2047 		if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) {
2048 			sargp->sbp = NULL;	/* to identify error */
2049 			return (puterrno4(error));
2050 		}
2051 	}
2052 
2053 	return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr));
2054 }
2055 
2056 static void
2057 nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp)
2058 {
2059 	ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size,
2060 	    KM_SLEEP);
2061 	ntovp->attrcnt = 0;
2062 	ntovp->vfsstat = FALSE;
2063 }
2064 
2065 static void
2066 nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
2067 	struct nfs4_svgetit_arg *sargp)
2068 {
2069 	int i;
2070 	union nfs4_attr_u *na;
2071 	uint8_t *amap;
2072 
2073 	/*
2074 	 * XXX Should do the same checks for whether the bit is set
2075 	 */
2076 	for (i = 0, na = ntovp->na, amap = ntovp->amap;
2077 	    i < ntovp->attrcnt; i++, na++, amap++) {
2078 		(void) (*nfs4_ntov_map[*amap].sv_getit)(
2079 		    NFS4ATTR_FREEIT, sargp, na);
2080 	}
2081 	if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) {
2082 		/*
2083 		 * xdr_free for getattr will be done later
2084 		 */
2085 		for (i = 0, na = ntovp->na, amap = ntovp->amap;
2086 		    i < ntovp->attrcnt; i++, na++, amap++) {
2087 			xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na);
2088 		}
2089 	}
2090 	kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size);
2091 }
2092 
2093 /*
2094  * do_rfs4_op_getattr gets the system attrs and converts into fattr4.
2095  */
2096 static nfsstat4
2097 do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp,
2098 	struct nfs4_svgetit_arg *sargp)
2099 {
2100 	int error = 0;
2101 	int i, k;
2102 	struct nfs4_ntov_table ntov;
2103 	XDR xdr;
2104 	ulong_t xdr_size;
2105 	char *xdr_attrs;
2106 	nfsstat4 status = NFS4_OK;
2107 	nfsstat4 prev_rdattr_error = sargp->rdattr_error;
2108 	union nfs4_attr_u *na;
2109 	uint8_t *amap;
2110 
2111 	sargp->op = NFS4ATTR_GETIT;
2112 	sargp->flag = 0;
2113 
2114 	fattrp->attrmask = 0;
2115 	/* if no bits requested, then return empty fattr4 */
2116 	if (breq == 0) {
2117 		fattrp->attrlist4_len = 0;
2118 		fattrp->attrlist4 = NULL;
2119 		return (NFS4_OK);
2120 	}
2121 
2122 	/*
2123 	 * return NFS4ERR_INVAL when client requests write-only attrs
2124 	 */
2125 	if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK))
2126 		return (NFS4ERR_INVAL);
2127 
2128 	nfs4_ntov_table_init(&ntov);
2129 	na = ntov.na;
2130 	amap = ntov.amap;
2131 
2132 	/*
2133 	 * Now loop to get or verify the attrs
2134 	 */
2135 	for (i = 0; i < nfs4_ntov_map_size; i++) {
2136 		if (breq & nfs4_ntov_map[i].fbit) {
2137 			if ((*nfs4_ntov_map[i].sv_getit)(
2138 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0) {
2139 
2140 				error = (*nfs4_ntov_map[i].sv_getit)(
2141 				    NFS4ATTR_GETIT, sargp, na);
2142 
2143 				/*
2144 				 * Possible error values:
2145 				 * >0 if sv_getit failed to
2146 				 * get the attr; 0 if succeeded;
2147 				 * <0 if rdattr_error and the
2148 				 * attribute cannot be returned.
2149 				 */
2150 				if (error && !(sargp->rdattr_error_req))
2151 					goto done;
2152 				/*
2153 				 * If error then just for entry
2154 				 */
2155 				if (error == 0) {
2156 					fattrp->attrmask |=
2157 					    nfs4_ntov_map[i].fbit;
2158 					*amap++ =
2159 					    (uint8_t)nfs4_ntov_map[i].nval;
2160 					na++;
2161 					(ntov.attrcnt)++;
2162 				} else if ((error > 0) &&
2163 				    (sargp->rdattr_error == NFS4_OK)) {
2164 					sargp->rdattr_error = puterrno4(error);
2165 				}
2166 				error = 0;
2167 			}
2168 		}
2169 	}
2170 
2171 	/*
2172 	 * If rdattr_error was set after the return value for it was assigned,
2173 	 * update it.
2174 	 */
2175 	if (prev_rdattr_error != sargp->rdattr_error) {
2176 		na = ntov.na;
2177 		amap = ntov.amap;
2178 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2179 			k = *amap;
2180 			if (k < FATTR4_RDATTR_ERROR) {
2181 				continue;
2182 			}
2183 			if ((k == FATTR4_RDATTR_ERROR) &&
2184 			    ((*nfs4_ntov_map[k].sv_getit)(
2185 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) {
2186 
2187 				(void) (*nfs4_ntov_map[k].sv_getit)(
2188 				    NFS4ATTR_GETIT, sargp, na);
2189 			}
2190 			break;
2191 		}
2192 	}
2193 
2194 	xdr_size = 0;
2195 	na = ntov.na;
2196 	amap = ntov.amap;
2197 	for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2198 		xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na);
2199 	}
2200 
2201 	fattrp->attrlist4_len = xdr_size;
2202 	if (xdr_size) {
2203 		/* freed by rfs4_op_getattr_free() */
2204 		fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP);
2205 
2206 		xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE);
2207 
2208 		na = ntov.na;
2209 		amap = ntov.amap;
2210 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2211 			if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) {
2212 				cmn_err(CE_WARN, "do_rfs4_op_getattr: xdr "
2213 				    "encode of attribute %d failed\n", *amap);
2214 				status = NFS4ERR_SERVERFAULT;
2215 				break;
2216 			}
2217 		}
2218 		/* xdrmem_destroy(&xdrs); */	/* NO-OP */
2219 	} else {
2220 		fattrp->attrlist4 = NULL;
2221 	}
2222 done:
2223 
2224 	nfs4_ntov_table_free(&ntov, sargp);
2225 
2226 	if (error != 0)
2227 		status = puterrno4(error);
2228 
2229 	return (status);
2230 }
2231 
2232 /* ARGSUSED */
2233 static void
2234 rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2235 	struct compound_state *cs)
2236 {
2237 	GETATTR4args *args = &argop->nfs_argop4_u.opgetattr;
2238 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2239 	struct nfs4_svgetit_arg sarg;
2240 	struct statvfs64 sb;
2241 	nfsstat4 status;
2242 
2243 	DTRACE_NFSV4_2(op__getattr__start, struct compound_state *, cs,
2244 	    GETATTR4args *, args);
2245 
2246 	if (cs->vp == NULL) {
2247 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2248 		goto out;
2249 	}
2250 
2251 	if (cs->access == CS_ACCESS_DENIED) {
2252 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2253 		goto out;
2254 	}
2255 
2256 	sarg.sbp = &sb;
2257 	sarg.cs = cs;
2258 
2259 	status = bitmap4_to_attrmask(args->attr_request, &sarg);
2260 	if (status == NFS4_OK) {
2261 		status = bitmap4_get_sysattrs(&sarg);
2262 		if (status == NFS4_OK)
2263 			status = do_rfs4_op_getattr(args->attr_request,
2264 			    &resp->obj_attributes, &sarg);
2265 	}
2266 	*cs->statusp = resp->status = status;
2267 out:
2268 	DTRACE_NFSV4_2(op__getattr__done, struct compound_state *, cs,
2269 	    GETATTR4res *, resp);
2270 }
2271 
2272 static void
2273 rfs4_op_getattr_free(nfs_resop4 *resop)
2274 {
2275 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2276 
2277 	nfs4_fattr4_free(&resp->obj_attributes);
2278 }
2279 
2280 /* ARGSUSED */
2281 static void
2282 rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2283 	struct compound_state *cs)
2284 {
2285 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2286 
2287 	DTRACE_NFSV4_1(op__getfh__start, struct compound_state *, cs);
2288 
2289 	if (cs->vp == NULL) {
2290 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2291 		goto out;
2292 	}
2293 	if (cs->access == CS_ACCESS_DENIED) {
2294 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2295 		goto out;
2296 	}
2297 
2298 	resp->object.nfs_fh4_val =
2299 	    kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP);
2300 	nfs_fh4_copy(&cs->fh, &resp->object);
2301 	*cs->statusp = resp->status = NFS4_OK;
2302 out:
2303 	DTRACE_NFSV4_2(op__getfh__done, struct compound_state *, cs,
2304 	    GETFH4res *, resp);
2305 }
2306 
2307 static void
2308 rfs4_op_getfh_free(nfs_resop4 *resop)
2309 {
2310 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2311 
2312 	if (resp->status == NFS4_OK &&
2313 	    resp->object.nfs_fh4_val != NULL) {
2314 		kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len);
2315 		resp->object.nfs_fh4_val = NULL;
2316 		resp->object.nfs_fh4_len = 0;
2317 	}
2318 }
2319 
2320 /*
2321  * illegal: args: void
2322  *	    res : status (NFS4ERR_OP_ILLEGAL)
2323  */
2324 /* ARGSUSED */
2325 static void
2326 rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop,
2327 	struct svc_req *req, struct compound_state *cs)
2328 {
2329 	ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal;
2330 
2331 	resop->resop = OP_ILLEGAL;
2332 	*cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
2333 }
2334 
2335 /*
2336  * link: args: SAVED_FH: file, CURRENT_FH: target directory
2337  *	 res: status. If success - CURRENT_FH unchanged, return change_info
2338  */
2339 /* ARGSUSED */
2340 static void
2341 rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2342 	struct compound_state *cs)
2343 {
2344 	LINK4args *args = &argop->nfs_argop4_u.oplink;
2345 	LINK4res *resp = &resop->nfs_resop4_u.oplink;
2346 	int error;
2347 	vnode_t *vp;
2348 	vnode_t *dvp;
2349 	struct vattr bdva, idva, adva;
2350 	char *nm;
2351 	uint_t  len;
2352 
2353 	DTRACE_NFSV4_2(op__link__start, struct compound_state *, cs,
2354 	    LINK4args *, args);
2355 
2356 	/* SAVED_FH: source object */
2357 	vp = cs->saved_vp;
2358 	if (vp == NULL) {
2359 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2360 		goto out;
2361 	}
2362 
2363 	/* CURRENT_FH: target directory */
2364 	dvp = cs->vp;
2365 	if (dvp == NULL) {
2366 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2367 		goto out;
2368 	}
2369 
2370 	/*
2371 	 * If there is a non-shared filesystem mounted on this vnode,
2372 	 * do not allow to link any file in this directory.
2373 	 */
2374 	if (vn_ismntpt(dvp)) {
2375 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2376 		goto out;
2377 	}
2378 
2379 	if (cs->access == CS_ACCESS_DENIED) {
2380 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2381 		goto out;
2382 	}
2383 
2384 	/* Check source object's type validity */
2385 	if (vp->v_type == VDIR) {
2386 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
2387 		goto out;
2388 	}
2389 
2390 	/* Check target directory's type */
2391 	if (dvp->v_type != VDIR) {
2392 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2393 		goto out;
2394 	}
2395 
2396 	if (cs->saved_exi != cs->exi) {
2397 		*cs->statusp = resp->status = NFS4ERR_XDEV;
2398 		goto out;
2399 	}
2400 
2401 	if (!utf8_dir_verify(&args->newname)) {
2402 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2403 		goto out;
2404 	}
2405 
2406 	nm = utf8_to_fn(&args->newname, &len, NULL);
2407 	if (nm == NULL) {
2408 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2409 		goto out;
2410 	}
2411 
2412 	if (len > MAXNAMELEN) {
2413 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
2414 		kmem_free(nm, len);
2415 		goto out;
2416 	}
2417 
2418 	if (rdonly4(cs->exi, cs->vp, req)) {
2419 		*cs->statusp = resp->status = NFS4ERR_ROFS;
2420 		kmem_free(nm, len);
2421 		goto out;
2422 	}
2423 
2424 	/* Get "before" change value */
2425 	bdva.va_mask = AT_CTIME|AT_SEQ;
2426 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
2427 	if (error) {
2428 		*cs->statusp = resp->status = puterrno4(error);
2429 		kmem_free(nm, len);
2430 		goto out;
2431 	}
2432 
2433 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
2434 
2435 	error = VOP_LINK(dvp, vp, nm, cs->cr, NULL, 0);
2436 
2437 	kmem_free(nm, len);
2438 
2439 	/*
2440 	 * Get the initial "after" sequence number, if it fails, set to zero
2441 	 */
2442 	idva.va_mask = AT_SEQ;
2443 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
2444 		idva.va_seq = 0;
2445 
2446 	/*
2447 	 * Force modified data and metadata out to stable storage.
2448 	 */
2449 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
2450 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
2451 
2452 	if (error) {
2453 		*cs->statusp = resp->status = puterrno4(error);
2454 		goto out;
2455 	}
2456 
2457 	/*
2458 	 * Get "after" change value, if it fails, simply return the
2459 	 * before value.
2460 	 */
2461 	adva.va_mask = AT_CTIME|AT_SEQ;
2462 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
2463 		adva.va_ctime = bdva.va_ctime;
2464 		adva.va_seq = 0;
2465 	}
2466 
2467 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
2468 
2469 	/*
2470 	 * The cinfo.atomic = TRUE only if we have
2471 	 * non-zero va_seq's, and it has incremented by exactly one
2472 	 * during the VOP_LINK and it didn't change during the VOP_FSYNC.
2473 	 */
2474 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
2475 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
2476 		resp->cinfo.atomic = TRUE;
2477 	else
2478 		resp->cinfo.atomic = FALSE;
2479 
2480 	*cs->statusp = resp->status = NFS4_OK;
2481 out:
2482 	DTRACE_NFSV4_2(op__link__done, struct compound_state *, cs,
2483 	    LINK4res *, resp);
2484 }
2485 
2486 /*
2487  * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work.
2488  */
2489 
2490 /* ARGSUSED */
2491 static nfsstat4
2492 do_rfs4_op_lookup(char *nm, uint_t buflen, struct svc_req *req,
2493 	struct compound_state *cs)
2494 {
2495 	int error;
2496 	int different_export = 0;
2497 	vnode_t *vp, *tvp, *pre_tvp = NULL, *oldvp = NULL;
2498 	struct exportinfo *exi = NULL, *pre_exi = NULL;
2499 	nfsstat4 stat;
2500 	fid_t fid;
2501 	int attrdir, dotdot, walk;
2502 	bool_t is_newvp = FALSE;
2503 
2504 	if (cs->vp->v_flag & V_XATTRDIR) {
2505 		attrdir = 1;
2506 		ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2507 	} else {
2508 		attrdir = 0;
2509 		ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2510 	}
2511 
2512 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
2513 
2514 	/*
2515 	 * If dotdotting, then need to check whether it's
2516 	 * above the root of a filesystem, or above an
2517 	 * export point.
2518 	 */
2519 	if (dotdot) {
2520 
2521 		/*
2522 		 * If dotdotting at the root of a filesystem, then
2523 		 * need to traverse back to the mounted-on filesystem
2524 		 * and do the dotdot lookup there.
2525 		 */
2526 		if (cs->vp->v_flag & VROOT) {
2527 
2528 			/*
2529 			 * If at the system root, then can
2530 			 * go up no further.
2531 			 */
2532 			if (VN_CMP(cs->vp, rootdir))
2533 				return (puterrno4(ENOENT));
2534 
2535 			/*
2536 			 * Traverse back to the mounted-on filesystem
2537 			 */
2538 			cs->vp = untraverse(cs->vp);
2539 
2540 			/*
2541 			 * Set the different_export flag so we remember
2542 			 * to pick up a new exportinfo entry for
2543 			 * this new filesystem.
2544 			 */
2545 			different_export = 1;
2546 		} else {
2547 
2548 			/*
2549 			 * If dotdotting above an export point then set
2550 			 * the different_export to get new export info.
2551 			 */
2552 			different_export = nfs_exported(cs->exi, cs->vp);
2553 		}
2554 	}
2555 
2556 	error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr,
2557 	    NULL, NULL, NULL);
2558 	if (error)
2559 		return (puterrno4(error));
2560 
2561 	/*
2562 	 * If the vnode is in a pseudo filesystem, check whether it is visible.
2563 	 *
2564 	 * XXX if the vnode is a symlink and it is not visible in
2565 	 * a pseudo filesystem, return ENOENT (not following symlink).
2566 	 * V4 client can not mount such symlink. This is a regression
2567 	 * from V2/V3.
2568 	 *
2569 	 * In the same exported filesystem, if the security flavor used
2570 	 * is not an explicitly shared flavor, limit the view to the visible
2571 	 * list entries only. This is not a WRONGSEC case because it's already
2572 	 * checked via PUTROOTFH/PUTPUBFH or PUTFH.
2573 	 */
2574 	if (!different_export &&
2575 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
2576 	    cs->access & CS_ACCESS_LIMITED)) {
2577 		if (! nfs_visible(cs->exi, vp, &different_export)) {
2578 			VN_RELE(vp);
2579 			return (puterrno4(ENOENT));
2580 		}
2581 	}
2582 
2583 	/*
2584 	 * If it's a mountpoint, then traverse it.
2585 	 */
2586 	if (vn_ismntpt(vp)) {
2587 		pre_exi = cs->exi;	/* save pre-traversed exportinfo */
2588 		pre_tvp = vp;		/* save pre-traversed vnode	*/
2589 
2590 		/*
2591 		 * hold pre_tvp to counteract rele by traverse.  We will
2592 		 * need pre_tvp below if checkexport4 fails
2593 		 */
2594 		VN_HOLD(pre_tvp);
2595 		tvp = vp;
2596 		if ((error = traverse(&tvp)) != 0) {
2597 			VN_RELE(vp);
2598 			VN_RELE(pre_tvp);
2599 			return (puterrno4(error));
2600 		}
2601 		vp = tvp;
2602 		different_export = 1;
2603 	} else if (vp->v_vfsp != cs->vp->v_vfsp) {
2604 		/*
2605 		 * The vfsp comparison is to handle the case where
2606 		 * a LOFS mount is shared.  lo_lookup traverses mount points,
2607 		 * and NFS is unaware of local fs transistions because
2608 		 * v_vfsmountedhere isn't set.  For this special LOFS case,
2609 		 * the dir and the obj returned by lookup will have different
2610 		 * vfs ptrs.
2611 		 */
2612 		different_export = 1;
2613 	}
2614 
2615 	if (different_export) {
2616 
2617 		bzero(&fid, sizeof (fid));
2618 		fid.fid_len = MAXFIDSZ;
2619 		error = vop_fid_pseudo(vp, &fid);
2620 		if (error) {
2621 			VN_RELE(vp);
2622 			if (pre_tvp)
2623 				VN_RELE(pre_tvp);
2624 			return (puterrno4(error));
2625 		}
2626 
2627 		if (dotdot)
2628 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
2629 		else
2630 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
2631 
2632 		if (exi == NULL) {
2633 			if (pre_tvp) {
2634 				/*
2635 				 * If this vnode is a mounted-on vnode,
2636 				 * but the mounted-on file system is not
2637 				 * exported, send back the filehandle for
2638 				 * the mounted-on vnode, not the root of
2639 				 * the mounted-on file system.
2640 				 */
2641 				VN_RELE(vp);
2642 				vp = pre_tvp;
2643 				exi = pre_exi;
2644 			} else {
2645 				VN_RELE(vp);
2646 				return (puterrno4(EACCES));
2647 			}
2648 		} else if (pre_tvp) {
2649 			/* we're done with pre_tvp now. release extra hold */
2650 			VN_RELE(pre_tvp);
2651 		}
2652 
2653 		cs->exi = exi;
2654 
2655 		/*
2656 		 * Now we do a checkauth4. The reason is that
2657 		 * this client/user may not have access to the new
2658 		 * exported file system, and if he does,
2659 		 * the client/user may be mapped to a different uid.
2660 		 *
2661 		 * We start with a new cr, because the checkauth4 done
2662 		 * in the PUT*FH operation over wrote the cred's uid,
2663 		 * gid, etc, and we want the real thing before calling
2664 		 * checkauth4()
2665 		 */
2666 		crfree(cs->cr);
2667 		cs->cr = crdup(cs->basecr);
2668 
2669 		if (cs->vp)
2670 			oldvp = cs->vp;
2671 		cs->vp = vp;
2672 		is_newvp = TRUE;
2673 
2674 		stat = call_checkauth4(cs, req);
2675 		if (stat != NFS4_OK) {
2676 			VN_RELE(cs->vp);
2677 			cs->vp = oldvp;
2678 			return (stat);
2679 		}
2680 	}
2681 
2682 	/*
2683 	 * After various NFS checks, do a label check on the path
2684 	 * component. The label on this path should either be the
2685 	 * global zone's label or a zone's label. We are only
2686 	 * interested in the zone's label because exported files
2687 	 * in global zone is accessible (though read-only) to
2688 	 * clients. The exportability/visibility check is already
2689 	 * done before reaching this code.
2690 	 */
2691 	if (is_system_labeled()) {
2692 		bslabel_t *clabel;
2693 
2694 		ASSERT(req->rq_label != NULL);
2695 		clabel = req->rq_label;
2696 		DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *,
2697 		    "got client label from request(1)", struct svc_req *, req);
2698 
2699 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2700 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK)) {
2701 				error = EACCES;
2702 				goto err_out;
2703 			}
2704 		} else {
2705 			/*
2706 			 * We grant access to admin_low label clients
2707 			 * only if the client is trusted, i.e. also
2708 			 * running Solaris Trusted Extension.
2709 			 */
2710 			struct sockaddr	*ca;
2711 			int		addr_type;
2712 			void		*ipaddr;
2713 			tsol_tpc_t	*tp;
2714 
2715 			ca = (struct sockaddr *)svc_getrpccaller(
2716 			    req->rq_xprt)->buf;
2717 			if (ca->sa_family == AF_INET) {
2718 				addr_type = IPV4_VERSION;
2719 				ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
2720 			} else if (ca->sa_family == AF_INET6) {
2721 				addr_type = IPV6_VERSION;
2722 				ipaddr = &((struct sockaddr_in6 *)
2723 				    ca)->sin6_addr;
2724 			}
2725 			tp = find_tpc(ipaddr, addr_type, B_FALSE);
2726 			if (tp == NULL || tp->tpc_tp.tp_doi !=
2727 			    l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
2728 			    SUN_CIPSO) {
2729 				if (tp != NULL)
2730 					TPC_RELE(tp);
2731 				error = EACCES;
2732 				goto err_out;
2733 			}
2734 			TPC_RELE(tp);
2735 		}
2736 	}
2737 
2738 	error = makefh4(&cs->fh, vp, cs->exi);
2739 
2740 err_out:
2741 	if (error) {
2742 		if (is_newvp) {
2743 			VN_RELE(cs->vp);
2744 			cs->vp = oldvp;
2745 		} else
2746 			VN_RELE(vp);
2747 		return (puterrno4(error));
2748 	}
2749 
2750 	if (!is_newvp) {
2751 		if (cs->vp)
2752 			VN_RELE(cs->vp);
2753 		cs->vp = vp;
2754 	} else if (oldvp)
2755 		VN_RELE(oldvp);
2756 
2757 	/*
2758 	 * if did lookup on attrdir and didn't lookup .., set named
2759 	 * attr fh flag
2760 	 */
2761 	if (attrdir && ! dotdot)
2762 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
2763 
2764 	/* Assume false for now, open proc will set this */
2765 	cs->mandlock = FALSE;
2766 
2767 	return (NFS4_OK);
2768 }
2769 
2770 /* ARGSUSED */
2771 static void
2772 rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2773 	struct compound_state *cs)
2774 {
2775 	LOOKUP4args *args = &argop->nfs_argop4_u.oplookup;
2776 	LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup;
2777 	char *nm;
2778 	uint_t len;
2779 
2780 	DTRACE_NFSV4_2(op__lookup__start, struct compound_state *, cs,
2781 	    LOOKUP4args *, args);
2782 
2783 	if (cs->vp == NULL) {
2784 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2785 		goto out;
2786 	}
2787 
2788 	if (cs->vp->v_type == VLNK) {
2789 		*cs->statusp = resp->status = NFS4ERR_SYMLINK;
2790 		goto out;
2791 	}
2792 
2793 	if (cs->vp->v_type != VDIR) {
2794 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2795 		goto out;
2796 	}
2797 
2798 	if (!utf8_dir_verify(&args->objname)) {
2799 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2800 		goto out;
2801 	}
2802 
2803 	nm = utf8_to_str(&args->objname, &len, NULL);
2804 	if (nm == NULL) {
2805 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2806 		goto out;
2807 	}
2808 
2809 	if (len > MAXNAMELEN) {
2810 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
2811 		kmem_free(nm, len);
2812 		goto out;
2813 	}
2814 
2815 	*cs->statusp = resp->status = do_rfs4_op_lookup(nm, len, req, cs);
2816 
2817 	kmem_free(nm, len);
2818 
2819 out:
2820 	DTRACE_NFSV4_2(op__lookup__done, struct compound_state *, cs,
2821 	    LOOKUP4res *, resp);
2822 }
2823 
2824 /* ARGSUSED */
2825 static void
2826 rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
2827 	struct compound_state *cs)
2828 {
2829 	LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp;
2830 
2831 	DTRACE_NFSV4_1(op__lookupp__start, struct compound_state *, cs);
2832 
2833 	if (cs->vp == NULL) {
2834 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2835 		goto out;
2836 	}
2837 
2838 	if (cs->vp->v_type != VDIR) {
2839 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2840 		goto out;
2841 	}
2842 
2843 	*cs->statusp = resp->status = do_rfs4_op_lookup("..", 3, req, cs);
2844 
2845 	/*
2846 	 * From NFSV4 Specification, LOOKUPP should not check for
2847 	 * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead.
2848 	 */
2849 	if (resp->status == NFS4ERR_WRONGSEC) {
2850 		*cs->statusp = resp->status = NFS4_OK;
2851 	}
2852 
2853 out:
2854 	DTRACE_NFSV4_2(op__lookupp__done, struct compound_state *, cs,
2855 	    LOOKUPP4res *, resp);
2856 }
2857 
2858 
2859 /*ARGSUSED2*/
2860 static void
2861 rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2862 	struct compound_state *cs)
2863 {
2864 	OPENATTR4args	*args = &argop->nfs_argop4_u.opopenattr;
2865 	OPENATTR4res	*resp = &resop->nfs_resop4_u.opopenattr;
2866 	vnode_t		*avp = NULL;
2867 	int		lookup_flags = LOOKUP_XATTR, error;
2868 	int		exp_ro = 0;
2869 
2870 	DTRACE_NFSV4_2(op__openattr__start, struct compound_state *, cs,
2871 	    OPENATTR4args *, args);
2872 
2873 	if (cs->vp == NULL) {
2874 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2875 		goto out;
2876 	}
2877 
2878 	/*
2879 	 * Make a couple of checks made by copen()
2880 	 *
2881 	 * Check to make sure underlying fs supports xattrs.  This
2882 	 * is required because solaris filesystem implementations
2883 	 * (UFS/TMPFS) don't enforce the noxattr mount option
2884 	 * in VOP_LOOKUP(LOOKUP_XATTR).  If fs doesn't support this
2885 	 * pathconf cmd or if fs supports cmd but doesn't claim
2886 	 * support for xattr, return NOTSUPP.  It would be better
2887 	 * to use VOP_PATHCONF( _PC_XATTR_ENABLED) for this; however,
2888 	 * that cmd is not available to VOP_PATHCONF interface
2889 	 * (it's only implemented inside pathconf syscall)...
2890 	 *
2891 	 * Verify permission to put attributes on files (access
2892 	 * checks from copen).
2893 	 */
2894 
2895 	if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 &&
2896 	    !vfs_has_feature(cs->vp->v_vfsp, VFSFT_XVATTR)) {
2897 		*cs->statusp = resp->status = puterrno4(ENOTSUP);
2898 		goto out;
2899 	}
2900 
2901 	if ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) &&
2902 	    (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) &&
2903 	    (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0)) {
2904 		*cs->statusp = resp->status = puterrno4(EACCES);
2905 		goto out;
2906 	}
2907 
2908 	/*
2909 	 * The CREATE_XATTR_DIR VOP flag cannot be specified if
2910 	 * the file system is exported read-only -- regardless of
2911 	 * createdir flag.  Otherwise the attrdir would be created
2912 	 * (assuming server fs isn't mounted readonly locally).  If
2913 	 * VOP_LOOKUP returns ENOENT in this case, the error will
2914 	 * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
2915 	 * because specfs has no VOP_LOOKUP op, so the macro would
2916 	 * return ENOSYS.  EINVAL is returned by all (current)
2917 	 * Solaris file system implementations when any of their
2918 	 * restrictions are violated (xattr(dir) can't have xattrdir).
2919 	 * Returning NOTSUPP is more appropriate in this case
2920 	 * because the object will never be able to have an attrdir.
2921 	 */
2922 	if (args->createdir && ! (exp_ro = rdonly4(cs->exi, cs->vp, req)))
2923 		lookup_flags |= CREATE_XATTR_DIR;
2924 
2925 	error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr,
2926 	    NULL, NULL, NULL);
2927 
2928 	if (error) {
2929 		if (error == ENOENT && args->createdir && exp_ro)
2930 			*cs->statusp = resp->status = puterrno4(EROFS);
2931 		else if (error == EINVAL || error == ENOSYS)
2932 			*cs->statusp = resp->status = puterrno4(ENOTSUP);
2933 		else
2934 			*cs->statusp = resp->status = puterrno4(error);
2935 		goto out;
2936 	}
2937 
2938 	ASSERT(avp->v_flag & V_XATTRDIR);
2939 
2940 	error = makefh4(&cs->fh, avp, cs->exi);
2941 
2942 	if (error) {
2943 		VN_RELE(avp);
2944 		*cs->statusp = resp->status = puterrno4(error);
2945 		goto out;
2946 	}
2947 
2948 	VN_RELE(cs->vp);
2949 	cs->vp = avp;
2950 
2951 	/*
2952 	 * There is no requirement for an attrdir fh flag
2953 	 * because the attrdir has a vnode flag to distinguish
2954 	 * it from regular (non-xattr) directories.  The
2955 	 * FH4_ATTRDIR flag is set for future sanity checks.
2956 	 */
2957 	set_fh4_flag(&cs->fh, FH4_ATTRDIR);
2958 	*cs->statusp = resp->status = NFS4_OK;
2959 
2960 out:
2961 	DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs,
2962 	    OPENATTR4res *, resp);
2963 }
2964 
2965 static int
2966 do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred,
2967     caller_context_t *ct)
2968 {
2969 	int error;
2970 	int i;
2971 	clock_t delaytime;
2972 
2973 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
2974 
2975 	/*
2976 	 * Don't block on mandatory locks. If this routine returns
2977 	 * EAGAIN, the caller should return NFS4ERR_LOCKED.
2978 	 */
2979 	uio->uio_fmode = FNONBLOCK;
2980 
2981 	for (i = 0; i < rfs4_maxlock_tries; i++) {
2982 
2983 
2984 		if (direction == FREAD) {
2985 			(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct);
2986 			error = VOP_READ(vp, uio, ioflag, cred, ct);
2987 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct);
2988 		} else {
2989 			(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct);
2990 			error = VOP_WRITE(vp, uio, ioflag, cred, ct);
2991 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct);
2992 		}
2993 
2994 		if (error != EAGAIN)
2995 			break;
2996 
2997 		if (i < rfs4_maxlock_tries - 1) {
2998 			delay(delaytime);
2999 			delaytime *= 2;
3000 		}
3001 	}
3002 
3003 	return (error);
3004 }
3005 
3006 /* ARGSUSED */
3007 static void
3008 rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3009 	struct compound_state *cs)
3010 {
3011 	READ4args *args = &argop->nfs_argop4_u.opread;
3012 	READ4res *resp = &resop->nfs_resop4_u.opread;
3013 	int error;
3014 	int verror;
3015 	vnode_t *vp;
3016 	struct vattr va;
3017 	struct iovec iov;
3018 	struct uio uio;
3019 	u_offset_t offset;
3020 	bool_t *deleg = &cs->deleg;
3021 	nfsstat4 stat;
3022 	int in_crit = 0;
3023 	mblk_t *mp;
3024 	int alloc_err = 0;
3025 	caller_context_t ct;
3026 
3027 	DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs,
3028 	    READ4args, args);
3029 
3030 	vp = cs->vp;
3031 	if (vp == NULL) {
3032 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3033 		goto out;
3034 	}
3035 	if (cs->access == CS_ACCESS_DENIED) {
3036 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3037 		goto out;
3038 	}
3039 
3040 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3041 	    deleg, TRUE, &ct)) != NFS4_OK) {
3042 		*cs->statusp = resp->status = stat;
3043 		goto out;
3044 	}
3045 
3046 	/*
3047 	 * Enter the critical region before calling VOP_RWLOCK
3048 	 * to avoid a deadlock with write requests.
3049 	 */
3050 	if (nbl_need_check(vp)) {
3051 		nbl_start_crit(vp, RW_READER);
3052 		in_crit = 1;
3053 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
3054 		    &ct)) {
3055 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
3056 			goto out;
3057 		}
3058 	}
3059 
3060 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3061 	    deleg, TRUE, &ct)) != NFS4_OK) {
3062 		*cs->statusp = resp->status = stat;
3063 		goto out;
3064 	}
3065 
3066 	va.va_mask = AT_MODE|AT_SIZE|AT_UID;
3067 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3068 
3069 	/*
3070 	 * If we can't get the attributes, then we can't do the
3071 	 * right access checking.  So, we'll fail the request.
3072 	 */
3073 	if (verror) {
3074 		*cs->statusp = resp->status = puterrno4(verror);
3075 		goto out;
3076 	}
3077 
3078 	if (vp->v_type != VREG) {
3079 		*cs->statusp = resp->status =
3080 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
3081 		goto out;
3082 	}
3083 
3084 	if (crgetuid(cs->cr) != va.va_uid &&
3085 	    (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) &&
3086 	    (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) {
3087 		*cs->statusp = resp->status = puterrno4(error);
3088 		goto out;
3089 	}
3090 
3091 	if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
3092 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3093 		goto out;
3094 	}
3095 
3096 	offset = args->offset;
3097 	if (offset >= va.va_size) {
3098 		*cs->statusp = resp->status = NFS4_OK;
3099 		resp->eof = TRUE;
3100 		resp->data_len = 0;
3101 		resp->data_val = NULL;
3102 		resp->mblk = NULL;
3103 		*cs->statusp = resp->status = NFS4_OK;
3104 		goto out;
3105 	}
3106 
3107 	if (args->count == 0) {
3108 		*cs->statusp = resp->status = NFS4_OK;
3109 		resp->eof = FALSE;
3110 		resp->data_len = 0;
3111 		resp->data_val = NULL;
3112 		resp->mblk = NULL;
3113 		goto out;
3114 	}
3115 
3116 	/*
3117 	 * Do not allocate memory more than maximum allowed
3118 	 * transfer size
3119 	 */
3120 	if (args->count > rfs4_tsize(req))
3121 		args->count = rfs4_tsize(req);
3122 
3123 	/*
3124 	 * mp will contain the data to be sent out in the read reply.
3125 	 * It will be freed after the reply has been sent.
3126 	 * Let's roundup the data to a BYTES_PER_XDR_UNIT multiple,
3127 	 * so that the call to xdrmblk_putmblk() never fails.
3128 	 * If the first alloc of the requested size fails, then
3129 	 * decrease the size to something more reasonable and wait
3130 	 * for the allocation to occur.
3131 	 */
3132 	mp = allocb(RNDUP(args->count), BPRI_MED);
3133 	if (mp == NULL) {
3134 		if (args->count > MAXBSIZE)
3135 			args->count = MAXBSIZE;
3136 		mp = allocb_wait(RNDUP(args->count), BPRI_MED,
3137 		    STR_NOSIG, &alloc_err);
3138 	}
3139 	ASSERT(mp != NULL);
3140 	ASSERT(alloc_err == 0);
3141 
3142 	iov.iov_base = (caddr_t)mp->b_datap->db_base;
3143 	iov.iov_len = args->count;
3144 	uio.uio_iov = &iov;
3145 	uio.uio_iovcnt = 1;
3146 	uio.uio_segflg = UIO_SYSSPACE;
3147 	uio.uio_extflg = UIO_COPY_CACHED;
3148 	uio.uio_loffset = args->offset;
3149 	uio.uio_resid = args->count;
3150 
3151 	error = do_io(FREAD, vp, &uio, 0, cs->cr, &ct);
3152 
3153 	va.va_mask = AT_SIZE;
3154 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3155 
3156 	if (error) {
3157 		freeb(mp);
3158 		*cs->statusp = resp->status = puterrno4(error);
3159 		goto out;
3160 	}
3161 
3162 	*cs->statusp = resp->status = NFS4_OK;
3163 
3164 	ASSERT(uio.uio_resid >= 0);
3165 	resp->data_len = args->count - uio.uio_resid;
3166 	resp->data_val = (char *)mp->b_datap->db_base;
3167 	resp->mblk = mp;
3168 
3169 	if (!verror && offset + resp->data_len == va.va_size)
3170 		resp->eof = TRUE;
3171 	else
3172 		resp->eof = FALSE;
3173 
3174 out:
3175 	if (in_crit)
3176 		nbl_end_crit(vp);
3177 
3178 	DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs,
3179 	    READ4res *, resp);
3180 }
3181 
3182 static void
3183 rfs4_op_read_free(nfs_resop4 *resop)
3184 {
3185 	READ4res *resp = &resop->nfs_resop4_u.opread;
3186 
3187 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3188 		freeb(resp->mblk);
3189 		resp->mblk = NULL;
3190 		resp->data_val = NULL;
3191 		resp->data_len = 0;
3192 	}
3193 }
3194 
3195 static void
3196 rfs4_op_readdir_free(nfs_resop4 *resop)
3197 {
3198 	READDIR4res *resp = &resop->nfs_resop4_u.opreaddir;
3199 
3200 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3201 		freeb(resp->mblk);
3202 		resp->mblk = NULL;
3203 		resp->data_len = 0;
3204 	}
3205 }
3206 
3207 
3208 /* ARGSUSED */
3209 static void
3210 rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3211 	struct compound_state *cs)
3212 {
3213 	PUTPUBFH4res *resp = &resop->nfs_resop4_u.opputpubfh;
3214 	int error;
3215 	vnode_t *vp;
3216 	struct exportinfo *exi, *sav_exi;
3217 	nfs_fh4_fmt_t *fh_fmtp;
3218 
3219 	DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs);
3220 
3221 	if (cs->vp) {
3222 		VN_RELE(cs->vp);
3223 		cs->vp = NULL;
3224 	}
3225 
3226 	if (cs->cr)
3227 		crfree(cs->cr);
3228 
3229 	cs->cr = crdup(cs->basecr);
3230 
3231 	vp = exi_public->exi_vp;
3232 	if (vp == NULL) {
3233 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3234 		goto out;
3235 	}
3236 
3237 	if (is_system_labeled()) {
3238 		bslabel_t *clabel;
3239 
3240 		ASSERT(req->rq_label != NULL);
3241 		clabel = req->rq_label;
3242 		DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *,
3243 		    "got client label from request(1)",
3244 		    struct svc_req *, req);
3245 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
3246 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK)) {
3247 				*cs->statusp = resp->status =
3248 				    NFS4ERR_SERVERFAULT;
3249 				return;
3250 			}
3251 		}
3252 	}
3253 
3254 	error = makefh4(&cs->fh, vp, exi_public);
3255 	if (error != 0) {
3256 		*cs->statusp = resp->status = puterrno4(error);
3257 		goto out;
3258 	}
3259 	sav_exi = cs->exi;
3260 	if (exi_public == exi_root) {
3261 		/*
3262 		 * No filesystem is actually shared public, so we default
3263 		 * to exi_root. In this case, we must check whether root
3264 		 * is exported.
3265 		 */
3266 		fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
3267 
3268 		/*
3269 		 * if root filesystem is exported, the exportinfo struct that we
3270 		 * should use is what checkexport4 returns, because root_exi is
3271 		 * actually a mostly empty struct.
3272 		 */
3273 		exi = checkexport4(&fh_fmtp->fh4_fsid,
3274 		    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
3275 		cs->exi = ((exi != NULL) ? exi : exi_public);
3276 	} else {
3277 		/*
3278 		 * it's a properly shared filesystem
3279 		 */
3280 		cs->exi = exi_public;
3281 	}
3282 
3283 	VN_HOLD(vp);
3284 	cs->vp = vp;
3285 
3286 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3287 		VN_RELE(cs->vp);
3288 		cs->vp = NULL;
3289 		cs->exi = sav_exi;
3290 		goto out;
3291 	}
3292 
3293 	*cs->statusp = resp->status = NFS4_OK;
3294 out:
3295 	DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs,
3296 	    PUTPUBFH4res *, resp);
3297 }
3298 
3299 /*
3300  * XXX - issue with put*fh operations. Suppose /export/home is exported.
3301  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
3302  * or joe have restrictive search permissions, then we shouldn't let
3303  * the client get a file handle. This is easy to enforce. However, we
3304  * don't know what security flavor should be used until we resolve the
3305  * path name. Another complication is uid mapping. If root is
3306  * the user, then it will be mapped to the anonymous user by default,
3307  * but we won't know that till we've resolved the path name. And we won't
3308  * know what the anonymous user is.
3309  * Luckily, SECINFO is specified to take a full filename.
3310  * So what we will have to in rfs4_op_lookup is check that flavor of
3311  * the target object matches that of the request, and if root was the
3312  * caller, check for the root= and anon= options, and if necessary,
3313  * repeat the lookup using the right cred_t. But that's not done yet.
3314  */
3315 /* ARGSUSED */
3316 static void
3317 rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3318 	struct compound_state *cs)
3319 {
3320 	PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
3321 	PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
3322 	nfs_fh4_fmt_t *fh_fmtp;
3323 
3324 	DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs,
3325 	    PUTFH4args *, args);
3326 
3327 	if (cs->vp) {
3328 		VN_RELE(cs->vp);
3329 		cs->vp = NULL;
3330 	}
3331 
3332 	if (cs->cr) {
3333 		crfree(cs->cr);
3334 		cs->cr = NULL;
3335 	}
3336 
3337 
3338 	if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
3339 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
3340 		goto out;
3341 	}
3342 
3343 	fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
3344 	cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
3345 	    NULL);
3346 
3347 	if (cs->exi == NULL) {
3348 		*cs->statusp = resp->status = NFS4ERR_STALE;
3349 		goto out;
3350 	}
3351 
3352 	cs->cr = crdup(cs->basecr);
3353 
3354 	ASSERT(cs->cr != NULL);
3355 
3356 	if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
3357 		*cs->statusp = resp->status;
3358 		goto out;
3359 	}
3360 
3361 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3362 		VN_RELE(cs->vp);
3363 		cs->vp = NULL;
3364 		goto out;
3365 	}
3366 
3367 	nfs_fh4_copy(&args->object, &cs->fh);
3368 	*cs->statusp = resp->status = NFS4_OK;
3369 	cs->deleg = FALSE;
3370 
3371 out:
3372 	DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs,
3373 	    PUTFH4res *, resp);
3374 }
3375 
3376 /* ARGSUSED */
3377 static void
3378 rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3379 	struct compound_state *cs)
3380 
3381 {
3382 	PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
3383 	int error;
3384 	fid_t fid;
3385 	struct exportinfo *exi, *sav_exi;
3386 
3387 	DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs);
3388 
3389 	if (cs->vp) {
3390 		VN_RELE(cs->vp);
3391 		cs->vp = NULL;
3392 	}
3393 
3394 	if (cs->cr)
3395 		crfree(cs->cr);
3396 
3397 	cs->cr = crdup(cs->basecr);
3398 
3399 	/*
3400 	 * Using rootdir, the system root vnode,
3401 	 * get its fid.
3402 	 */
3403 	bzero(&fid, sizeof (fid));
3404 	fid.fid_len = MAXFIDSZ;
3405 	error = vop_fid_pseudo(rootdir, &fid);
3406 	if (error != 0) {
3407 		*cs->statusp = resp->status = puterrno4(error);
3408 		goto out;
3409 	}
3410 
3411 	/*
3412 	 * Then use the root fsid & fid it to find out if it's exported
3413 	 *
3414 	 * If the server root isn't exported directly, then
3415 	 * it should at least be a pseudo export based on
3416 	 * one or more exports further down in the server's
3417 	 * file tree.
3418 	 */
3419 	exi = checkexport4(&rootdir->v_vfsp->vfs_fsid, &fid, NULL);
3420 	if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
3421 		NFS4_DEBUG(rfs4_debug,
3422 			(CE_WARN, "rfs4_op_putrootfh: export check failure"));
3423 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3424 		goto out;
3425 	}
3426 
3427 	/*
3428 	 * Now make a filehandle based on the root
3429 	 * export and root vnode.
3430 	 */
3431 	error = makefh4(&cs->fh, rootdir, exi);
3432 	if (error != 0) {
3433 		*cs->statusp = resp->status = puterrno4(error);
3434 		goto out;
3435 	}
3436 
3437 	sav_exi = cs->exi;
3438 	cs->exi = exi;
3439 
3440 	VN_HOLD(rootdir);
3441 	cs->vp = rootdir;
3442 
3443 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3444 		VN_RELE(rootdir);
3445 		cs->vp = NULL;
3446 		cs->exi = sav_exi;
3447 		goto out;
3448 	}
3449 
3450 	*cs->statusp = resp->status = NFS4_OK;
3451 	cs->deleg = FALSE;
3452 out:
3453 	DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs,
3454 	    PUTROOTFH4res *, resp);
3455 }
3456 
3457 /*
3458  * A directory entry is a valid nfsv4 entry if
3459  * - it has a non-zero ino
3460  * - it is not a dot or dotdot name
3461  * - it is visible in a pseudo export or in a real export that can
3462  *   only have a limited view.
3463  */
3464 static bool_t
3465 valid_nfs4_entry(struct exportinfo *exi, struct dirent64 *dp,
3466 		int *expseudo, int check_visible)
3467 {
3468 	if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) {
3469 		*expseudo = 0;
3470 		return (FALSE);
3471 	}
3472 
3473 	if (! check_visible) {
3474 		*expseudo = 0;
3475 		return (TRUE);
3476 	}
3477 
3478 	return (nfs_visible_inode(exi, dp->d_ino, expseudo));
3479 }
3480 
3481 /*
3482  * set_rdattr_params sets up the variables used to manage what information
3483  * to get for each directory entry.
3484  */
3485 static nfsstat4
3486 set_rdattr_params(struct nfs4_svgetit_arg *sargp,
3487 		bitmap4 attrs, bool_t *need_to_lookup)
3488 {
3489 	uint_t	va_mask;
3490 	nfsstat4 status;
3491 	bitmap4 objbits;
3492 
3493 	status = bitmap4_to_attrmask(attrs, sargp);
3494 	if (status != NFS4_OK) {
3495 		/*
3496 		 * could not even figure attr mask
3497 		 */
3498 		return (status);
3499 	}
3500 	va_mask = sargp->vap->va_mask;
3501 
3502 	/*
3503 	 * dirent's d_ino is always correct value for mounted_on_fileid.
3504 	 * mntdfid_set is set once here, but mounted_on_fileid is
3505 	 * set in main dirent processing loop for each dirent.
3506 	 * The mntdfid_set is a simple optimization that lets the
3507 	 * server attr code avoid work when caller is readdir.
3508 	 */
3509 	sargp->mntdfid_set = TRUE;
3510 
3511 	/*
3512 	 * Lookup entry only if client asked for any of the following:
3513 	 * a) vattr attrs
3514 	 * b) vfs attrs
3515 	 * c) attrs w/per-object scope requested (change, filehandle, etc)
3516 	 *    other than mounted_on_fileid (which we can take from dirent)
3517 	 */
3518 	objbits = attrs ? attrs & NFS4_VP_ATTR_MASK : 0;
3519 
3520 	if (va_mask || sargp->sbp || (objbits & ~FATTR4_MOUNTED_ON_FILEID_MASK))
3521 		*need_to_lookup = TRUE;
3522 	else
3523 		*need_to_lookup = FALSE;
3524 
3525 	if (sargp->sbp == NULL)
3526 		return (NFS4_OK);
3527 
3528 	/*
3529 	 * If filesystem attrs are requested, get them now from the
3530 	 * directory vp, as most entries will have same filesystem. The only
3531 	 * exception are mounted over entries but we handle
3532 	 * those as we go (XXX mounted over detection not yet implemented).
3533 	 */
3534 	sargp->vap->va_mask = 0;	/* to avoid VOP_GETATTR */
3535 	status = bitmap4_get_sysattrs(sargp);
3536 	sargp->vap->va_mask = va_mask;
3537 
3538 	if ((status != NFS4_OK) && sargp->rdattr_error_req) {
3539 		/*
3540 		 * Failed to get filesystem attributes.
3541 		 * Return a rdattr_error for each entry, but don't fail.
3542 		 * However, don't get any obj-dependent attrs.
3543 		 */
3544 		sargp->rdattr_error = status;	/* for rdattr_error */
3545 		*need_to_lookup = FALSE;
3546 		/*
3547 		 * At least get fileid for regular readdir output
3548 		 */
3549 		sargp->vap->va_mask &= AT_NODEID;
3550 		status = NFS4_OK;
3551 	}
3552 
3553 	return (status);
3554 }
3555 
3556 /*
3557  * readlink: args: CURRENT_FH.
3558  *	res: status. If success - CURRENT_FH unchanged, return linktext.
3559  */
3560 
3561 /* ARGSUSED */
3562 static void
3563 rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3564 	struct compound_state *cs)
3565 {
3566 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3567 	int error;
3568 	vnode_t *vp;
3569 	struct iovec iov;
3570 	struct vattr va;
3571 	struct uio uio;
3572 	char *data;
3573 
3574 	DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs);
3575 
3576 	/* CURRENT_FH: directory */
3577 	vp = cs->vp;
3578 	if (vp == NULL) {
3579 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3580 		goto out;
3581 	}
3582 
3583 	if (cs->access == CS_ACCESS_DENIED) {
3584 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3585 		goto out;
3586 	}
3587 
3588 	if (vp->v_type == VDIR) {
3589 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
3590 		goto out;
3591 	}
3592 
3593 	if (vp->v_type != VLNK) {
3594 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3595 		goto out;
3596 	}
3597 
3598 	va.va_mask = AT_MODE;
3599 	error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
3600 	if (error) {
3601 		*cs->statusp = resp->status = puterrno4(error);
3602 		goto out;
3603 	}
3604 
3605 	if (MANDLOCK(vp, va.va_mode)) {
3606 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3607 		goto out;
3608 	}
3609 
3610 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
3611 
3612 	iov.iov_base = data;
3613 	iov.iov_len = MAXPATHLEN;
3614 	uio.uio_iov = &iov;
3615 	uio.uio_iovcnt = 1;
3616 	uio.uio_segflg = UIO_SYSSPACE;
3617 	uio.uio_extflg = UIO_COPY_CACHED;
3618 	uio.uio_loffset = 0;
3619 	uio.uio_resid = MAXPATHLEN;
3620 
3621 	error = VOP_READLINK(vp, &uio, cs->cr, NULL);
3622 
3623 	if (error) {
3624 		kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3625 		*cs->statusp = resp->status = puterrno4(error);
3626 		goto out;
3627 	}
3628 
3629 	*(data + MAXPATHLEN - uio.uio_resid) = '\0';
3630 
3631 	/*
3632 	 * treat link name as data
3633 	 */
3634 	(void) str_to_utf8(data, &resp->link);
3635 
3636 	kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3637 	*cs->statusp = resp->status = NFS4_OK;
3638 
3639 out:
3640 	DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs,
3641 	    READLINK4res *, resp);
3642 }
3643 
3644 static void
3645 rfs4_op_readlink_free(nfs_resop4 *resop)
3646 {
3647 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3648 	utf8string *symlink = &resp->link;
3649 
3650 	if (symlink->utf8string_val) {
3651 		UTF8STRING_FREE(*symlink)
3652 	}
3653 }
3654 
3655 /*
3656  * release_lockowner:
3657  *	Release any state associated with the supplied
3658  *	lockowner. Note if any lo_state is holding locks we will not
3659  *	rele that lo_state and thus the lockowner will not be destroyed.
3660  *	A client using lock after the lock owner stateid has been released
3661  *	will suffer the consequence of NFS4ERR_BAD_STATEID and would have
3662  *	to reissue the lock with new_lock_owner set to TRUE.
3663  *	args: lock_owner
3664  *	res:  status
3665  */
3666 /* ARGSUSED */
3667 static void
3668 rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
3669 	struct svc_req *req, struct compound_state *cs)
3670 {
3671 	RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
3672 	RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
3673 	rfs4_lockowner_t *lo;
3674 	rfs4_openowner_t *oop;
3675 	rfs4_state_t *sp;
3676 	rfs4_lo_state_t *lsp;
3677 	rfs4_client_t *cp;
3678 	bool_t create = FALSE;
3679 	locklist_t *llist;
3680 	sysid_t sysid;
3681 
3682 	DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *,
3683 	    cs, RELEASE_LOCKOWNER4args *, ap);
3684 
3685 	/* Make sure there is a clientid around for this request */
3686 	cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
3687 
3688 	if (cp == NULL) {
3689 		*cs->statusp = resp->status =
3690 		    rfs4_check_clientid(&ap->lock_owner.clientid, 0);
3691 		goto out;
3692 	}
3693 	rfs4_client_rele(cp);
3694 
3695 	lo = rfs4_findlockowner(&ap->lock_owner, &create);
3696 	if (lo == NULL) {
3697 		*cs->statusp = resp->status = NFS4_OK;
3698 		goto out;
3699 	}
3700 	ASSERT(lo->client != NULL);
3701 
3702 	/*
3703 	 * Check for EXPIRED client. If so will reap state with in a lease
3704 	 * period or on next set_clientid_confirm step
3705 	 */
3706 	if (rfs4_lease_expired(lo->client)) {
3707 		rfs4_lockowner_rele(lo);
3708 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
3709 		goto out;
3710 	}
3711 
3712 	/*
3713 	 * If no sysid has been assigned, then no locks exist; just return.
3714 	 */
3715 	rfs4_dbe_lock(lo->client->dbe);
3716 	if (lo->client->sysidt == LM_NOSYSID) {
3717 		rfs4_lockowner_rele(lo);
3718 		rfs4_dbe_unlock(lo->client->dbe);
3719 		goto out;
3720 	}
3721 
3722 	sysid = lo->client->sysidt;
3723 	rfs4_dbe_unlock(lo->client->dbe);
3724 
3725 	/*
3726 	 * Mark the lockowner invalid.
3727 	 */
3728 	rfs4_dbe_hide(lo->dbe);
3729 
3730 	/*
3731 	 * sysid-pid pair should now not be used since the lockowner is
3732 	 * invalid. If the client were to instantiate the lockowner again
3733 	 * it would be assigned a new pid. Thus we can get the list of
3734 	 * current locks.
3735 	 */
3736 
3737 	llist = flk_get_active_locks(sysid, lo->pid);
3738 	/* If we are still holding locks fail */
3739 	if (llist != NULL) {
3740 
3741 		*cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
3742 
3743 		flk_free_locklist(llist);
3744 		/*
3745 		 * We need to unhide the lockowner so the client can
3746 		 * try it again. The bad thing here is if the client
3747 		 * has a logic error that took it here in the first place
3748 		 * he probably has lost accounting of the locks that it
3749 		 * is holding. So we may have dangling state until the
3750 		 * open owner state is reaped via close. One scenario
3751 		 * that could possibly occur is that the client has
3752 		 * sent the unlock request(s) in separate threads
3753 		 * and has not waited for the replies before sending the
3754 		 * RELEASE_LOCKOWNER request. Presumably, it would expect
3755 		 * and deal appropriately with NFS4ERR_LOCKS_HELD, by
3756 		 * reissuing the request.
3757 		 */
3758 		rfs4_dbe_unhide(lo->dbe);
3759 		rfs4_lockowner_rele(lo);
3760 		goto out;
3761 	}
3762 
3763 	/*
3764 	 * For the corresponding client we need to check each open
3765 	 * owner for any opens that have lockowner state associated
3766 	 * with this lockowner.
3767 	 */
3768 
3769 	rfs4_dbe_lock(lo->client->dbe);
3770 	for (oop = lo->client->openownerlist.next->oop; oop != NULL;
3771 	    oop = oop->openownerlist.next->oop) {
3772 
3773 		rfs4_dbe_lock(oop->dbe);
3774 		for (sp = oop->ownerstateids.next->sp; sp != NULL;
3775 		    sp = sp->ownerstateids.next->sp) {
3776 
3777 			rfs4_dbe_lock(sp->dbe);
3778 			for (lsp = sp->lockownerlist.next->lsp;
3779 			    lsp != NULL; lsp = lsp->lockownerlist.next->lsp) {
3780 				if (lsp->locker == lo) {
3781 					rfs4_dbe_lock(lsp->dbe);
3782 					rfs4_dbe_invalidate(lsp->dbe);
3783 					rfs4_dbe_unlock(lsp->dbe);
3784 				}
3785 			}
3786 			rfs4_dbe_unlock(sp->dbe);
3787 		}
3788 		rfs4_dbe_unlock(oop->dbe);
3789 	}
3790 	rfs4_dbe_unlock(lo->client->dbe);
3791 
3792 	rfs4_lockowner_rele(lo);
3793 
3794 	*cs->statusp = resp->status = NFS4_OK;
3795 
3796 out:
3797 	DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *,
3798 	    cs, RELEASE_LOCKOWNER4res *, resp);
3799 }
3800 
3801 /*
3802  * short utility function to lookup a file and recall the delegation
3803  */
3804 static rfs4_file_t *
3805 rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
3806 	int *lkup_error, cred_t *cr)
3807 {
3808 	vnode_t *vp;
3809 	rfs4_file_t *fp = NULL;
3810 	bool_t fcreate = FALSE;
3811 	int error;
3812 
3813 	if (vpp)
3814 		*vpp = NULL;
3815 
3816 	if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL,
3817 	    NULL)) == 0) {
3818 		if (vp->v_type == VREG)
3819 			fp = rfs4_findfile(vp, NULL, &fcreate);
3820 		if (vpp)
3821 			*vpp = vp;
3822 		else
3823 			VN_RELE(vp);
3824 	}
3825 
3826 	if (lkup_error)
3827 		*lkup_error = error;
3828 
3829 	return (fp);
3830 }
3831 
3832 /*
3833  * remove: args: CURRENT_FH: directory; name.
3834  *	res: status. If success - CURRENT_FH unchanged, return change_info
3835  *		for directory.
3836  */
3837 /* ARGSUSED */
3838 static void
3839 rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3840 	struct compound_state *cs)
3841 {
3842 	REMOVE4args *args = &argop->nfs_argop4_u.opremove;
3843 	REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
3844 	int error;
3845 	vnode_t *dvp, *vp;
3846 	struct vattr bdva, idva, adva;
3847 	char *nm;
3848 	uint_t len;
3849 	rfs4_file_t *fp;
3850 	int in_crit = 0;
3851 	bslabel_t *clabel;
3852 
3853 	DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs,
3854 	    REMOVE4args *, args);
3855 
3856 	/* CURRENT_FH: directory */
3857 	dvp = cs->vp;
3858 	if (dvp == NULL) {
3859 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3860 		goto out;
3861 	}
3862 
3863 	if (cs->access == CS_ACCESS_DENIED) {
3864 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3865 		goto out;
3866 	}
3867 
3868 	/*
3869 	 * If there is an unshared filesystem mounted on this vnode,
3870 	 * Do not allow to remove anything in this directory.
3871 	 */
3872 	if (vn_ismntpt(dvp)) {
3873 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3874 		goto out;
3875 	}
3876 
3877 	if (dvp->v_type != VDIR) {
3878 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
3879 		goto out;
3880 	}
3881 
3882 	if (!utf8_dir_verify(&args->target)) {
3883 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3884 		goto out;
3885 	}
3886 
3887 	/*
3888 	 * Lookup the file so that we can check if it's a directory
3889 	 */
3890 	nm = utf8_to_fn(&args->target, &len, NULL);
3891 	if (nm == NULL) {
3892 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3893 		goto out;
3894 	}
3895 
3896 	if (len > MAXNAMELEN) {
3897 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
3898 		kmem_free(nm, len);
3899 		goto out;
3900 	}
3901 
3902 	if (rdonly4(cs->exi, cs->vp, req)) {
3903 		*cs->statusp = resp->status = NFS4ERR_ROFS;
3904 		kmem_free(nm, len);
3905 		goto out;
3906 	}
3907 
3908 	/*
3909 	 * Lookup the file to determine type and while we are see if
3910 	 * there is a file struct around and check for delegation.
3911 	 * We don't need to acquire va_seq before this lookup, if
3912 	 * it causes an update, cinfo.before will not match, which will
3913 	 * trigger a cache flush even if atomic is TRUE.
3914 	 */
3915 	if (fp = rfs4_lookup_and_findfile(dvp, nm, &vp, &error, cs->cr)) {
3916 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
3917 		    NULL)) {
3918 			VN_RELE(vp);
3919 			rfs4_file_rele(fp);
3920 			*cs->statusp = resp->status = NFS4ERR_DELAY;
3921 			kmem_free(nm, len);
3922 			goto out;
3923 		}
3924 	}
3925 
3926 	/* Didn't find anything to remove */
3927 	if (vp == NULL) {
3928 		*cs->statusp = resp->status = error;
3929 		kmem_free(nm, len);
3930 		goto out;
3931 	}
3932 
3933 	if (nbl_need_check(vp)) {
3934 		nbl_start_crit(vp, RW_READER);
3935 		in_crit = 1;
3936 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
3937 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
3938 			kmem_free(nm, len);
3939 			nbl_end_crit(vp);
3940 			VN_RELE(vp);
3941 			if (fp) {
3942 				rfs4_clear_dont_grant(fp);
3943 				rfs4_file_rele(fp);
3944 			}
3945 			goto out;
3946 		}
3947 	}
3948 
3949 	/* check label before allowing removal */
3950 	if (is_system_labeled()) {
3951 		ASSERT(req->rq_label != NULL);
3952 		clabel = req->rq_label;
3953 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
3954 		    "got client label from request(1)",
3955 		    struct svc_req *, req);
3956 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
3957 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK)) {
3958 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
3959 				kmem_free(nm, len);
3960 				if (in_crit)
3961 					nbl_end_crit(vp);
3962 				VN_RELE(vp);
3963 				if (fp) {
3964 					rfs4_clear_dont_grant(fp);
3965 					rfs4_file_rele(fp);
3966 				}
3967 				goto out;
3968 			}
3969 		}
3970 	}
3971 
3972 	/* Get dir "before" change value */
3973 	bdva.va_mask = AT_CTIME|AT_SEQ;
3974 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
3975 	if (error) {
3976 		*cs->statusp = resp->status = puterrno4(error);
3977 		kmem_free(nm, len);
3978 		goto out;
3979 	}
3980 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
3981 
3982 	/* Actually do the REMOVE operation */
3983 	if (vp->v_type == VDIR) {
3984 		/*
3985 		 * Can't remove a directory that has a mounted-on filesystem.
3986 		 */
3987 		if (vn_ismntpt(vp)) {
3988 			error = EACCES;
3989 		} else {
3990 			/*
3991 			 * System V defines rmdir to return EEXIST,
3992 			 * not * ENOTEMPTY, if the directory is not
3993 			 * empty.  A System V NFS server needs to map
3994 			 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
3995 			 * transmit over the wire.
3996 			 */
3997 			if ((error = VOP_RMDIR(dvp, nm, rootdir, cs->cr,
3998 			    NULL, 0)) == EEXIST)
3999 				error = ENOTEMPTY;
4000 		}
4001 	} else {
4002 		if ((error = VOP_REMOVE(dvp, nm, cs->cr, NULL, 0)) == 0 &&
4003 		    fp != NULL) {
4004 			struct vattr va;
4005 			vnode_t *tvp;
4006 
4007 			rfs4_dbe_lock(fp->dbe);
4008 			tvp = fp->vp;
4009 			if (tvp)
4010 				VN_HOLD(tvp);
4011 			rfs4_dbe_unlock(fp->dbe);
4012 
4013 			if (tvp) {
4014 				/*
4015 				 * This is va_seq safe because we are not
4016 				 * manipulating dvp.
4017 				 */
4018 				va.va_mask = AT_NLINK;
4019 				if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4020 				    va.va_nlink == 0) {
4021 					/* Remove state on file remove */
4022 					if (in_crit) {
4023 						nbl_end_crit(vp);
4024 						in_crit = 0;
4025 					}
4026 					rfs4_close_all_state(fp);
4027 				}
4028 				VN_RELE(tvp);
4029 			}
4030 		}
4031 	}
4032 
4033 	if (in_crit)
4034 		nbl_end_crit(vp);
4035 	VN_RELE(vp);
4036 
4037 	if (fp) {
4038 		rfs4_clear_dont_grant(fp);
4039 		rfs4_file_rele(fp);
4040 	}
4041 	kmem_free(nm, len);
4042 
4043 	if (error) {
4044 		*cs->statusp = resp->status = puterrno4(error);
4045 		goto out;
4046 	}
4047 
4048 	/*
4049 	 * Get the initial "after" sequence number, if it fails, set to zero
4050 	 */
4051 	idva.va_mask = AT_SEQ;
4052 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
4053 		idva.va_seq = 0;
4054 
4055 	/*
4056 	 * Force modified data and metadata out to stable storage.
4057 	 */
4058 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
4059 
4060 	/*
4061 	 * Get "after" change value, if it fails, simply return the
4062 	 * before value.
4063 	 */
4064 	adva.va_mask = AT_CTIME|AT_SEQ;
4065 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
4066 		adva.va_ctime = bdva.va_ctime;
4067 		adva.va_seq = 0;
4068 	}
4069 
4070 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
4071 
4072 	/*
4073 	 * The cinfo.atomic = TRUE only if we have
4074 	 * non-zero va_seq's, and it has incremented by exactly one
4075 	 * during the VOP_REMOVE/RMDIR and it didn't change during
4076 	 * the VOP_FSYNC.
4077 	 */
4078 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
4079 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
4080 		resp->cinfo.atomic = TRUE;
4081 	else
4082 		resp->cinfo.atomic = FALSE;
4083 
4084 	*cs->statusp = resp->status = NFS4_OK;
4085 
4086 out:
4087 	DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs,
4088 	    REMOVE4res *, resp);
4089 }
4090 
4091 /*
4092  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
4093  *		oldname and newname.
4094  *	res: status. If success - CURRENT_FH unchanged, return change_info
4095  *		for both from and target directories.
4096  */
4097 /* ARGSUSED */
4098 static void
4099 rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4100 	struct compound_state *cs)
4101 {
4102 	RENAME4args *args = &argop->nfs_argop4_u.oprename;
4103 	RENAME4res *resp = &resop->nfs_resop4_u.oprename;
4104 	int error;
4105 	vnode_t *odvp;
4106 	vnode_t *ndvp;
4107 	vnode_t *srcvp, *targvp;
4108 	struct vattr obdva, oidva, oadva;
4109 	struct vattr nbdva, nidva, nadva;
4110 	char *onm, *nnm;
4111 	uint_t olen, nlen;
4112 	rfs4_file_t *fp, *sfp;
4113 	int in_crit_src, in_crit_targ;
4114 	int fp_rele_grant_hold, sfp_rele_grant_hold;
4115 	bslabel_t *clabel;
4116 
4117 	DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs,
4118 	    RENAME4args *, args);
4119 
4120 	fp = sfp = NULL;
4121 	srcvp = targvp = NULL;
4122 	in_crit_src = in_crit_targ = 0;
4123 	fp_rele_grant_hold = sfp_rele_grant_hold = 0;
4124 
4125 	/* CURRENT_FH: target directory */
4126 	ndvp = cs->vp;
4127 	if (ndvp == NULL) {
4128 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4129 		goto out;
4130 	}
4131 
4132 	/* SAVED_FH: from directory */
4133 	odvp = cs->saved_vp;
4134 	if (odvp == NULL) {
4135 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4136 		goto out;
4137 	}
4138 
4139 	if (cs->access == CS_ACCESS_DENIED) {
4140 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4141 		goto out;
4142 	}
4143 
4144 	/*
4145 	 * If there is an unshared filesystem mounted on this vnode,
4146 	 * do not allow to rename objects in this directory.
4147 	 */
4148 	if (vn_ismntpt(odvp)) {
4149 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4150 		goto out;
4151 	}
4152 
4153 	/*
4154 	 * If there is an unshared filesystem mounted on this vnode,
4155 	 * do not allow to rename to this directory.
4156 	 */
4157 	if (vn_ismntpt(ndvp)) {
4158 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4159 		goto out;
4160 	}
4161 
4162 	if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
4163 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4164 		goto out;
4165 	}
4166 
4167 	if (cs->saved_exi != cs->exi) {
4168 		*cs->statusp = resp->status = NFS4ERR_XDEV;
4169 		goto out;
4170 	}
4171 
4172 	if (!utf8_dir_verify(&args->oldname)) {
4173 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4174 		goto out;
4175 	}
4176 
4177 	if (!utf8_dir_verify(&args->newname)) {
4178 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4179 		goto out;
4180 	}
4181 
4182 	onm = utf8_to_fn(&args->oldname, &olen, NULL);
4183 	if (onm == NULL) {
4184 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4185 		goto out;
4186 	}
4187 
4188 	nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4189 	if (nnm == NULL) {
4190 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4191 		kmem_free(onm, olen);
4192 		goto out;
4193 	}
4194 
4195 	if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
4196 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4197 		kmem_free(onm, olen);
4198 		kmem_free(nnm, nlen);
4199 		goto out;
4200 	}
4201 
4202 
4203 	if (rdonly4(cs->exi, cs->vp, req)) {
4204 		*cs->statusp = resp->status = NFS4ERR_ROFS;
4205 		kmem_free(onm, olen);
4206 		kmem_free(nnm, nlen);
4207 		goto out;
4208 	}
4209 
4210 	/* check label of the target dir */
4211 	if (is_system_labeled()) {
4212 		ASSERT(req->rq_label != NULL);
4213 		clabel = req->rq_label;
4214 		DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
4215 		    "got client label from request(1)",
4216 		    struct svc_req *, req);
4217 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4218 			if (!do_rfs_label_check(clabel, ndvp,
4219 			    EQUALITY_CHECK)) {
4220 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4221 				goto out;
4222 			}
4223 		}
4224 	}
4225 
4226 	/*
4227 	 * Is the source a file and have a delegation?
4228 	 * We don't need to acquire va_seq before these lookups, if
4229 	 * it causes an update, cinfo.before will not match, which will
4230 	 * trigger a cache flush even if atomic is TRUE.
4231 	 */
4232 	if (sfp = rfs4_lookup_and_findfile(odvp, onm, &srcvp, &error, cs->cr)) {
4233 		if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
4234 		    NULL)) {
4235 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4236 			goto err_out;
4237 		}
4238 	}
4239 
4240 	if (srcvp == NULL) {
4241 		*cs->statusp = resp->status = puterrno4(error);
4242 		kmem_free(onm, olen);
4243 		kmem_free(nnm, nlen);
4244 		goto out;
4245 	}
4246 
4247 	sfp_rele_grant_hold = 1;
4248 
4249 	/* Does the destination exist and a file and have a delegation? */
4250 	if (fp = rfs4_lookup_and_findfile(ndvp, nnm, &targvp, NULL, cs->cr)) {
4251 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4252 		    NULL)) {
4253 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4254 			goto err_out;
4255 		}
4256 	}
4257 	fp_rele_grant_hold = 1;
4258 
4259 
4260 	/* Check for NBMAND lock on both source and target */
4261 	if (nbl_need_check(srcvp)) {
4262 		nbl_start_crit(srcvp, RW_READER);
4263 		in_crit_src = 1;
4264 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) {
4265 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4266 			goto err_out;
4267 		}
4268 	}
4269 
4270 	if (targvp && nbl_need_check(targvp)) {
4271 		nbl_start_crit(targvp, RW_READER);
4272 		in_crit_targ = 1;
4273 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
4274 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4275 			goto err_out;
4276 		}
4277 	}
4278 
4279 	/* Get source "before" change value */
4280 	obdva.va_mask = AT_CTIME|AT_SEQ;
4281 	error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL);
4282 	if (!error) {
4283 		nbdva.va_mask = AT_CTIME|AT_SEQ;
4284 		error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL);
4285 	}
4286 	if (error) {
4287 		*cs->statusp = resp->status = puterrno4(error);
4288 		goto err_out;
4289 	}
4290 
4291 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
4292 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
4293 
4294 	if ((error = VOP_RENAME(odvp, onm, ndvp, nnm, cs->cr, NULL, 0)) == 0 &&
4295 	    fp != NULL) {
4296 		struct vattr va;
4297 		vnode_t *tvp;
4298 
4299 		rfs4_dbe_lock(fp->dbe);
4300 		tvp = fp->vp;
4301 		if (tvp)
4302 			VN_HOLD(tvp);
4303 		rfs4_dbe_unlock(fp->dbe);
4304 
4305 		if (tvp) {
4306 			va.va_mask = AT_NLINK;
4307 			if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4308 			    va.va_nlink == 0) {
4309 				/* The file is gone and so should the state */
4310 				if (in_crit_targ) {
4311 					nbl_end_crit(targvp);
4312 					in_crit_targ = 0;
4313 				}
4314 				rfs4_close_all_state(fp);
4315 			}
4316 			VN_RELE(tvp);
4317 		}
4318 	}
4319 	if (error == 0) {
4320 		char *tmp;
4321 
4322 		/* fix the path name for the renamed file */
4323 		mutex_enter(&srcvp->v_lock);
4324 		tmp = srcvp->v_path;
4325 		srcvp->v_path = NULL;
4326 		mutex_exit(&srcvp->v_lock);
4327 		vn_setpath(rootdir, ndvp, srcvp, nnm, nlen - 1);
4328 		if (tmp != NULL)
4329 			kmem_free(tmp, strlen(tmp) + 1);
4330 	}
4331 
4332 	if (in_crit_src)
4333 		nbl_end_crit(srcvp);
4334 	if (srcvp)
4335 		VN_RELE(srcvp);
4336 	if (in_crit_targ)
4337 		nbl_end_crit(targvp);
4338 	if (targvp)
4339 		VN_RELE(targvp);
4340 
4341 	if (sfp) {
4342 		rfs4_clear_dont_grant(sfp);
4343 		rfs4_file_rele(sfp);
4344 	}
4345 	if (fp) {
4346 		rfs4_clear_dont_grant(fp);
4347 		rfs4_file_rele(fp);
4348 	}
4349 
4350 	kmem_free(onm, olen);
4351 	kmem_free(nnm, nlen);
4352 
4353 	/*
4354 	 * Get the initial "after" sequence number, if it fails, set to zero
4355 	 */
4356 	oidva.va_mask = AT_SEQ;
4357 	if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL))
4358 		oidva.va_seq = 0;
4359 
4360 	nidva.va_mask = AT_SEQ;
4361 	if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL))
4362 		nidva.va_seq = 0;
4363 
4364 	/*
4365 	 * Force modified data and metadata out to stable storage.
4366 	 */
4367 	(void) VOP_FSYNC(odvp, 0, cs->cr, NULL);
4368 	(void) VOP_FSYNC(ndvp, 0, cs->cr, NULL);
4369 
4370 	if (error) {
4371 		*cs->statusp = resp->status = puterrno4(error);
4372 		goto out;
4373 	}
4374 
4375 	/*
4376 	 * Get "after" change values, if it fails, simply return the
4377 	 * before value.
4378 	 */
4379 	oadva.va_mask = AT_CTIME|AT_SEQ;
4380 	if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) {
4381 		oadva.va_ctime = obdva.va_ctime;
4382 		oadva.va_seq = 0;
4383 	}
4384 
4385 	nadva.va_mask = AT_CTIME|AT_SEQ;
4386 	if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) {
4387 		nadva.va_ctime = nbdva.va_ctime;
4388 		nadva.va_seq = 0;
4389 	}
4390 
4391 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
4392 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
4393 
4394 	/*
4395 	 * The cinfo.atomic = TRUE only if we have
4396 	 * non-zero va_seq's, and it has incremented by exactly one
4397 	 * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
4398 	 */
4399 	if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
4400 	    oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq)
4401 		resp->source_cinfo.atomic = TRUE;
4402 	else
4403 		resp->source_cinfo.atomic = FALSE;
4404 
4405 	if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
4406 	    nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq)
4407 		resp->target_cinfo.atomic = TRUE;
4408 	else
4409 		resp->target_cinfo.atomic = FALSE;
4410 
4411 #ifdef	VOLATILE_FH_TEST
4412 	{
4413 	extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
4414 
4415 	/*
4416 	 * Add the renamed file handle to the volatile rename list
4417 	 */
4418 	if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
4419 		/* file handles may expire on rename */
4420 		vnode_t *vp;
4421 
4422 		nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4423 		/*
4424 		 * Already know that nnm will be a valid string
4425 		 */
4426 		error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr,
4427 		    NULL, NULL, NULL);
4428 		kmem_free(nnm, nlen);
4429 		if (!error) {
4430 			add_volrnm_fh(cs->exi, vp);
4431 			VN_RELE(vp);
4432 		}
4433 	}
4434 	}
4435 #endif	/* VOLATILE_FH_TEST */
4436 
4437 	*cs->statusp = resp->status = NFS4_OK;
4438 out:
4439 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4440 	    RENAME4res *, resp);
4441 	return;
4442 
4443 err_out:
4444 	kmem_free(onm, olen);
4445 	kmem_free(nnm, nlen);
4446 
4447 	if (in_crit_src) nbl_end_crit(srcvp);
4448 	if (in_crit_targ) nbl_end_crit(targvp);
4449 	if (targvp) VN_RELE(targvp);
4450 	if (srcvp) VN_RELE(srcvp);
4451 	if (sfp) {
4452 		if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
4453 		rfs4_file_rele(sfp);
4454 	}
4455 	if (fp) {
4456 		if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
4457 		rfs4_file_rele(fp);
4458 	}
4459 
4460 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4461 	    RENAME4res *, resp);
4462 }
4463 
4464 /* ARGSUSED */
4465 static void
4466 rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4467 	struct compound_state *cs)
4468 {
4469 	RENEW4args *args = &argop->nfs_argop4_u.oprenew;
4470 	RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
4471 	rfs4_client_t *cp;
4472 
4473 	DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs,
4474 	    RENEW4args *, args);
4475 
4476 	if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
4477 		*cs->statusp = resp->status =
4478 		    rfs4_check_clientid(&args->clientid, 0);
4479 		goto out;
4480 	}
4481 
4482 	if (rfs4_lease_expired(cp)) {
4483 		rfs4_client_rele(cp);
4484 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
4485 		goto out;
4486 	}
4487 
4488 	rfs4_update_lease(cp);
4489 
4490 	mutex_enter(cp->cbinfo.cb_lock);
4491 	if (cp->cbinfo.cb_notified_of_cb_path_down == FALSE) {
4492 		cp->cbinfo.cb_notified_of_cb_path_down = TRUE;
4493 		*cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
4494 	} else {
4495 		*cs->statusp = resp->status = NFS4_OK;
4496 	}
4497 	mutex_exit(cp->cbinfo.cb_lock);
4498 
4499 	rfs4_client_rele(cp);
4500 
4501 out:
4502 	DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs,
4503 	    RENEW4res *, resp);
4504 }
4505 
4506 /* ARGSUSED */
4507 static void
4508 rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
4509 	struct compound_state *cs)
4510 {
4511 	RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
4512 
4513 	DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs);
4514 
4515 	/* No need to check cs->access - we are not accessing any object */
4516 	if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
4517 		*cs->statusp = resp->status = NFS4ERR_RESTOREFH;
4518 		goto out;
4519 	}
4520 	if (cs->vp != NULL) {
4521 		VN_RELE(cs->vp);
4522 	}
4523 	cs->vp = cs->saved_vp;
4524 	cs->saved_vp = NULL;
4525 	cs->exi = cs->saved_exi;
4526 	nfs_fh4_copy(&cs->saved_fh, &cs->fh);
4527 	*cs->statusp = resp->status = NFS4_OK;
4528 	cs->deleg = FALSE;
4529 
4530 out:
4531 	DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs,
4532 	    RESTOREFH4res *, resp);
4533 }
4534 
4535 /* ARGSUSED */
4536 static void
4537 rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4538 	struct compound_state *cs)
4539 {
4540 	SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
4541 
4542 	DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs);
4543 
4544 	/* No need to check cs->access - we are not accessing any object */
4545 	if (cs->vp == NULL) {
4546 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4547 		goto out;
4548 	}
4549 	if (cs->saved_vp != NULL) {
4550 		VN_RELE(cs->saved_vp);
4551 	}
4552 	cs->saved_vp = cs->vp;
4553 	VN_HOLD(cs->saved_vp);
4554 	cs->saved_exi = cs->exi;
4555 	/*
4556 	 * since SAVEFH is fairly rare, don't alloc space for its fh
4557 	 * unless necessary.
4558 	 */
4559 	if (cs->saved_fh.nfs_fh4_val == NULL) {
4560 		cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
4561 	}
4562 	nfs_fh4_copy(&cs->fh, &cs->saved_fh);
4563 	*cs->statusp = resp->status = NFS4_OK;
4564 
4565 out:
4566 	DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs,
4567 	    SAVEFH4res *, resp);
4568 }
4569 
4570 /*
4571  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
4572  * return the bitmap of attrs that were set successfully. It is also
4573  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
4574  * always be called only after rfs4_do_set_attrs().
4575  *
4576  * Verify that the attributes are same as the expected ones. sargp->vap
4577  * and sargp->sbp contain the input attributes as translated from fattr4.
4578  *
4579  * This function verifies only the attrs that correspond to a vattr or
4580  * vfsstat struct. That is because of the extra step needed to get the
4581  * corresponding system structs. Other attributes have already been set or
4582  * verified by do_rfs4_set_attrs.
4583  *
4584  * Return 0 if all attrs match, -1 if some don't, error if error processing.
4585  */
4586 static int
4587 rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
4588 	bitmap4 *resp, struct nfs4_ntov_table *ntovp)
4589 {
4590 	int error, ret_error = 0;
4591 	int i, k;
4592 	uint_t sva_mask = sargp->vap->va_mask;
4593 	uint_t vbit;
4594 	union nfs4_attr_u *na;
4595 	uint8_t *amap;
4596 	bool_t getsb = ntovp->vfsstat;
4597 
4598 	if (sva_mask != 0) {
4599 		/*
4600 		 * Okay to overwrite sargp->vap because we verify based
4601 		 * on the incoming values.
4602 		 */
4603 		ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
4604 		    sargp->cs->cr, NULL);
4605 		if (ret_error) {
4606 			if (resp == NULL)
4607 				return (ret_error);
4608 			/*
4609 			 * Must return bitmap of successful attrs
4610 			 */
4611 			sva_mask = 0;	/* to prevent checking vap later */
4612 		} else {
4613 			/*
4614 			 * Some file systems clobber va_mask. it is probably
4615 			 * wrong of them to do so, nonethless we practice
4616 			 * defensive coding.
4617 			 * See bug id 4276830.
4618 			 */
4619 			sargp->vap->va_mask = sva_mask;
4620 		}
4621 	}
4622 
4623 	if (getsb) {
4624 		/*
4625 		 * Now get the superblock and loop on the bitmap, as there is
4626 		 * no simple way of translating from superblock to bitmap4.
4627 		 */
4628 		ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
4629 		if (ret_error) {
4630 			if (resp == NULL)
4631 				goto errout;
4632 			getsb = FALSE;
4633 		}
4634 	}
4635 
4636 	/*
4637 	 * Now loop and verify each attribute which getattr returned
4638 	 * whether it's the same as the input.
4639 	 */
4640 	if (resp == NULL && !getsb && (sva_mask == 0))
4641 		goto errout;
4642 
4643 	na = ntovp->na;
4644 	amap = ntovp->amap;
4645 	k = 0;
4646 	for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
4647 		k = *amap;
4648 		ASSERT(nfs4_ntov_map[k].nval == k);
4649 		vbit = nfs4_ntov_map[k].vbit;
4650 
4651 		/*
4652 		 * If vattr attribute but VOP_GETATTR failed, or it's
4653 		 * superblock attribute but VFS_STATVFS failed, skip
4654 		 */
4655 		if (vbit) {
4656 			if ((vbit & sva_mask) == 0)
4657 				continue;
4658 		} else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
4659 			continue;
4660 		}
4661 		error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na);
4662 		if (resp != NULL) {
4663 			if (error)
4664 				ret_error = -1;	/* not all match */
4665 			else	/* update response bitmap */
4666 				*resp |= nfs4_ntov_map[k].fbit;
4667 			continue;
4668 		}
4669 		if (error) {
4670 			ret_error = -1;	/* not all match */
4671 			break;
4672 		}
4673 	}
4674 errout:
4675 	return (ret_error);
4676 }
4677 
4678 /*
4679  * Decode the attribute to be set/verified. If the attr requires a sys op
4680  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
4681  * call the sv_getit function for it, because the sys op hasn't yet been done.
4682  * Return 0 for success, error code if failed.
4683  *
4684  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
4685  */
4686 static int
4687 decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
4688 	int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
4689 {
4690 	int error = 0;
4691 	bool_t set_later;
4692 
4693 	sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
4694 
4695 	if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
4696 		set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
4697 		/*
4698 		 * don't verify yet if a vattr or sb dependent attr,
4699 		 * because we don't have their sys values yet.
4700 		 * Will be done later.
4701 		 */
4702 		if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
4703 			/*
4704 			 * ACLs are a special case, since setting the MODE
4705 			 * conflicts with setting the ACL.  We delay setting
4706 			 * the ACL until all other attributes have been set.
4707 			 * The ACL gets set in do_rfs4_op_setattr().
4708 			 */
4709 			if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
4710 				error = (*nfs4_ntov_map[k].sv_getit)(cmd,
4711 				    sargp, nap);
4712 				if (error) {
4713 					xdr_free(nfs4_ntov_map[k].xfunc,
4714 					    (caddr_t)nap);
4715 				}
4716 			}
4717 		}
4718 	} else {
4719 #ifdef  DEBUG
4720 		cmn_err(CE_NOTE, "decode_fattr4_attr: error "
4721 		    "decoding attribute %d\n", k);
4722 #endif
4723 		error = EINVAL;
4724 	}
4725 	if (!error && resp_bval && !set_later) {
4726 		*resp_bval |= nfs4_ntov_map[k].fbit;
4727 	}
4728 
4729 	return (error);
4730 }
4731 
4732 /*
4733  * Set vattr based on incoming fattr4 attrs - used by setattr.
4734  * Set response mask. Ignore any values that are not writable vattr attrs.
4735  */
4736 static nfsstat4
4737 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
4738 		struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
4739 		nfs4_attr_cmd_t cmd)
4740 {
4741 	int error = 0;
4742 	int i;
4743 	char *attrs = fattrp->attrlist4;
4744 	uint32_t attrslen = fattrp->attrlist4_len;
4745 	XDR xdr;
4746 	nfsstat4 status = NFS4_OK;
4747 	vnode_t *vp = cs->vp;
4748 	union nfs4_attr_u *na;
4749 	uint8_t *amap;
4750 
4751 #ifndef lint
4752 	/*
4753 	 * Make sure that maximum attribute number can be expressed as an
4754 	 * 8 bit quantity.
4755 	 */
4756 	ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
4757 #endif
4758 
4759 	if (vp == NULL) {
4760 		if (resp)
4761 			*resp = 0;
4762 		return (NFS4ERR_NOFILEHANDLE);
4763 	}
4764 	if (cs->access == CS_ACCESS_DENIED) {
4765 		if (resp)
4766 			*resp = 0;
4767 		return (NFS4ERR_ACCESS);
4768 	}
4769 
4770 	sargp->op = cmd;
4771 	sargp->cs = cs;
4772 	sargp->flag = 0;	/* may be set later */
4773 	sargp->vap->va_mask = 0;
4774 	sargp->rdattr_error = NFS4_OK;
4775 	sargp->rdattr_error_req = FALSE;
4776 	/* sargp->sbp is set by the caller */
4777 
4778 	xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
4779 
4780 	na = ntovp->na;
4781 	amap = ntovp->amap;
4782 
4783 	/*
4784 	 * The following loop iterates on the nfs4_ntov_map checking
4785 	 * if the fbit is set in the requested bitmap.
4786 	 * If set then we process the arguments using the
4787 	 * rfs4_fattr4 conversion functions to populate the setattr
4788 	 * vattr and va_mask. Any settable attrs that are not using vattr
4789 	 * will be set in this loop.
4790 	 */
4791 	for (i = 0; i < nfs4_ntov_map_size; i++) {
4792 		if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
4793 			continue;
4794 		}
4795 		/*
4796 		 * If setattr, must be a writable attr.
4797 		 * If verify/nverify, must be a readable attr.
4798 		 */
4799 		if ((error = (*nfs4_ntov_map[i].sv_getit)(
4800 		    NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
4801 			/*
4802 			 * Client tries to set/verify an
4803 			 * unsupported attribute, tries to set
4804 			 * a read only attr or verify a write
4805 			 * only one - error!
4806 			 */
4807 			break;
4808 		}
4809 		/*
4810 		 * Decode the attribute to set/verify
4811 		 */
4812 		error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
4813 		    &xdr, resp ? resp : NULL, na);
4814 		if (error)
4815 			break;
4816 		*amap++ = (uint8_t)nfs4_ntov_map[i].nval;
4817 		na++;
4818 		(ntovp->attrcnt)++;
4819 		if (nfs4_ntov_map[i].vfsstat)
4820 			ntovp->vfsstat = TRUE;
4821 	}
4822 
4823 	if (error != 0)
4824 		status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP :
4825 		    puterrno4(error));
4826 	/* xdrmem_destroy(&xdrs); */	/* NO-OP */
4827 	return (status);
4828 }
4829 
4830 static nfsstat4
4831 do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
4832 		stateid4 *stateid)
4833 {
4834 	int error = 0;
4835 	struct nfs4_svgetit_arg sarg;
4836 	bool_t trunc;
4837 
4838 	nfsstat4 status = NFS4_OK;
4839 	cred_t *cr = cs->cr;
4840 	vnode_t *vp = cs->vp;
4841 	struct nfs4_ntov_table ntov;
4842 	struct statvfs64 sb;
4843 	struct vattr bva;
4844 	struct flock64 bf;
4845 	int in_crit = 0;
4846 	uint_t saved_mask = 0;
4847 	caller_context_t ct;
4848 
4849 	*resp = 0;
4850 	sarg.sbp = &sb;
4851 	nfs4_ntov_table_init(&ntov);
4852 	status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
4853 	    NFS4ATTR_SETIT);
4854 	if (status != NFS4_OK) {
4855 		/*
4856 		 * failed set attrs
4857 		 */
4858 		goto done;
4859 	}
4860 	if ((sarg.vap->va_mask == 0) &&
4861 	    (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
4862 		/*
4863 		 * no further work to be done
4864 		 */
4865 		goto done;
4866 	}
4867 
4868 	/*
4869 	 * If we got a request to set the ACL and the MODE, only
4870 	 * allow changing VSUID, VSGID, and VSVTX.  Attempting
4871 	 * to change any other bits, along with setting an ACL,
4872 	 * gives NFS4ERR_INVAL.
4873 	 */
4874 	if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
4875 	    (fattrp->attrmask & FATTR4_MODE_MASK)) {
4876 		vattr_t va;
4877 
4878 		va.va_mask = AT_MODE;
4879 		error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
4880 		if (error) {
4881 			status = puterrno4(error);
4882 			goto done;
4883 		}
4884 		if ((sarg.vap->va_mode ^ va.va_mode) &
4885 		    ~(VSUID | VSGID | VSVTX)) {
4886 			status = NFS4ERR_INVAL;
4887 			goto done;
4888 		}
4889 	}
4890 
4891 	/* Check stateid only if size has been set */
4892 	if (sarg.vap->va_mask & AT_SIZE) {
4893 		trunc = (sarg.vap->va_size == 0);
4894 		status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
4895 		    trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct);
4896 		if (status != NFS4_OK)
4897 			goto done;
4898 	} else {
4899 		ct.cc_sysid = 0;
4900 		ct.cc_pid = 0;
4901 		ct.cc_caller_id = nfs4_srv_caller_id;
4902 		ct.cc_flags = CC_DONTBLOCK;
4903 	}
4904 
4905 	/* XXX start of possible race with delegations */
4906 
4907 	/*
4908 	 * We need to specially handle size changes because it is
4909 	 * possible for the client to create a file with read-only
4910 	 * modes, but with the file opened for writing. If the client
4911 	 * then tries to set the file size, e.g. ftruncate(3C),
4912 	 * fcntl(F_FREESP), the normal access checking done in
4913 	 * VOP_SETATTR would prevent the client from doing it even though
4914 	 * it should be allowed to do so.  To get around this, we do the
4915 	 * access checking for ourselves and use VOP_SPACE which doesn't
4916 	 * do the access checking.
4917 	 * Also the client should not be allowed to change the file
4918 	 * size if there is a conflicting non-blocking mandatory lock in
4919 	 * the region of the change.
4920 	 */
4921 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
4922 		u_offset_t offset;
4923 		ssize_t length;
4924 
4925 		/*
4926 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
4927 		 * before returning, sarg.vap->va_mask is used to
4928 		 * generate the setattr reply bitmap.  We also clear
4929 		 * AT_SIZE below before calling VOP_SPACE.  For both
4930 		 * of these cases, the va_mask needs to be saved here
4931 		 * and restored after calling VOP_SETATTR.
4932 		 */
4933 		saved_mask = sarg.vap->va_mask;
4934 
4935 		/*
4936 		 * Check any possible conflict due to NBMAND locks.
4937 		 * Get into critical region before VOP_GETATTR, so the
4938 		 * size attribute is valid when checking conflicts.
4939 		 */
4940 		if (nbl_need_check(vp)) {
4941 			nbl_start_crit(vp, RW_READER);
4942 			in_crit = 1;
4943 		}
4944 
4945 		bva.va_mask = AT_UID|AT_SIZE;
4946 		if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
4947 			status = puterrno4(error);
4948 			goto done;
4949 		}
4950 
4951 		if (in_crit) {
4952 			if (sarg.vap->va_size < bva.va_size) {
4953 				offset = sarg.vap->va_size;
4954 				length = bva.va_size - sarg.vap->va_size;
4955 			} else {
4956 				offset = bva.va_size;
4957 				length = sarg.vap->va_size - bva.va_size;
4958 			}
4959 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
4960 			    &ct)) {
4961 				status = NFS4ERR_LOCKED;
4962 				goto done;
4963 			}
4964 		}
4965 
4966 		if (crgetuid(cr) == bva.va_uid) {
4967 			sarg.vap->va_mask &= ~AT_SIZE;
4968 			bf.l_type = F_WRLCK;
4969 			bf.l_whence = 0;
4970 			bf.l_start = (off64_t)sarg.vap->va_size;
4971 			bf.l_len = 0;
4972 			bf.l_sysid = 0;
4973 			bf.l_pid = 0;
4974 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
4975 			    (offset_t)sarg.vap->va_size, cr, &ct);
4976 		}
4977 	}
4978 
4979 	if (!error && sarg.vap->va_mask != 0)
4980 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
4981 
4982 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
4983 	if (saved_mask & AT_SIZE)
4984 		sarg.vap->va_mask |= AT_SIZE;
4985 
4986 	/*
4987 	 * If an ACL was being set, it has been delayed until now,
4988 	 * in order to set the mode (via the VOP_SETATTR() above) first.
4989 	 */
4990 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
4991 		int i;
4992 
4993 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
4994 			if (ntov.amap[i] == FATTR4_ACL)
4995 				break;
4996 		if (i < NFS4_MAXNUM_ATTRS) {
4997 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
4998 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
4999 			if (error == 0) {
5000 				*resp |= FATTR4_ACL_MASK;
5001 			} else if (error == ENOTSUP) {
5002 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
5003 				status = NFS4ERR_ATTRNOTSUPP;
5004 				goto done;
5005 			}
5006 		} else {
5007 			NFS4_DEBUG(rfs4_debug,
5008 			    (CE_NOTE, "do_rfs4_op_setattr: "
5009 			    "unable to find ACL in fattr4"));
5010 			error = EINVAL;
5011 		}
5012 	}
5013 
5014 	if (error) {
5015 		/* check if a monitor detected a delegation conflict */
5016 		if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
5017 			status = NFS4ERR_DELAY;
5018 		else
5019 			status = puterrno4(error);
5020 
5021 		/*
5022 		 * Set the response bitmap when setattr failed.
5023 		 * If VOP_SETATTR partially succeeded, test by doing a
5024 		 * VOP_GETATTR on the object and comparing the data
5025 		 * to the setattr arguments.
5026 		 */
5027 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
5028 	} else {
5029 		/*
5030 		 * Force modified metadata out to stable storage.
5031 		 */
5032 		(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
5033 		/*
5034 		 * Set response bitmap
5035 		 */
5036 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
5037 	}
5038 
5039 /* Return early and already have a NFSv4 error */
5040 done:
5041 	/*
5042 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
5043 	 * conversion sets both readable and writeable NFS4 attrs
5044 	 * for AT_MTIME and AT_ATIME.  The line below masks out
5045 	 * unrequested attrs from the setattr result bitmap.  This
5046 	 * is placed after the done: label to catch the ATTRNOTSUP
5047 	 * case.
5048 	 */
5049 	*resp &= fattrp->attrmask;
5050 
5051 	if (in_crit)
5052 		nbl_end_crit(vp);
5053 
5054 	nfs4_ntov_table_free(&ntov, &sarg);
5055 
5056 	return (status);
5057 }
5058 
5059 /* ARGSUSED */
5060 static void
5061 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5062 	struct compound_state *cs)
5063 {
5064 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
5065 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
5066 	bslabel_t *clabel;
5067 
5068 	DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5069 	    SETATTR4args *, args);
5070 
5071 	if (cs->vp == NULL) {
5072 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5073 		goto out;
5074 	}
5075 
5076 	/*
5077 	 * If there is an unshared filesystem mounted on this vnode,
5078 	 * do not allow to setattr on this vnode.
5079 	 */
5080 	if (vn_ismntpt(cs->vp)) {
5081 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5082 		goto out;
5083 	}
5084 
5085 	resp->attrsset = 0;
5086 
5087 	if (rdonly4(cs->exi, cs->vp, req)) {
5088 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5089 		goto out;
5090 	}
5091 
5092 	/* check label before setting attributes */
5093 	if (is_system_labeled()) {
5094 		ASSERT(req->rq_label != NULL);
5095 		clabel = req->rq_label;
5096 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
5097 		    "got client label from request(1)",
5098 		    struct svc_req *, req);
5099 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
5100 			if (!do_rfs_label_check(clabel, cs->vp,
5101 			    EQUALITY_CHECK)) {
5102 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
5103 				goto out;
5104 			}
5105 		}
5106 	}
5107 
5108 	*cs->statusp = resp->status =
5109 	    do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
5110 	    &args->stateid);
5111 
5112 out:
5113 	DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5114 	    SETATTR4res *, resp);
5115 }
5116 
5117 /* ARGSUSED */
5118 static void
5119 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5120 	struct compound_state *cs)
5121 {
5122 	/*
5123 	 * verify and nverify are exactly the same, except that nverify
5124 	 * succeeds when some argument changed, and verify succeeds when
5125 	 * when none changed.
5126 	 */
5127 
5128 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
5129 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
5130 
5131 	int error;
5132 	struct nfs4_svgetit_arg sarg;
5133 	struct statvfs64 sb;
5134 	struct nfs4_ntov_table ntov;
5135 
5136 	DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5137 	    VERIFY4args *, args);
5138 
5139 	if (cs->vp == NULL) {
5140 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5141 		goto out;
5142 	}
5143 
5144 	sarg.sbp = &sb;
5145 	nfs4_ntov_table_init(&ntov);
5146 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5147 	    &sarg, &ntov, NFS4ATTR_VERIT);
5148 	if (resp->status != NFS4_OK) {
5149 		/*
5150 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5151 		 * so could return -1 for "no match".
5152 		 */
5153 		if (resp->status == -1)
5154 			resp->status = NFS4ERR_NOT_SAME;
5155 		goto done;
5156 	}
5157 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5158 	switch (error) {
5159 	case 0:
5160 		resp->status = NFS4_OK;
5161 		break;
5162 	case -1:
5163 		resp->status = NFS4ERR_NOT_SAME;
5164 		break;
5165 	default:
5166 		resp->status = puterrno4(error);
5167 		break;
5168 	}
5169 done:
5170 	*cs->statusp = resp->status;
5171 	nfs4_ntov_table_free(&ntov, &sarg);
5172 out:
5173 	DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5174 	    VERIFY4res *, resp);
5175 }
5176 
5177 /* ARGSUSED */
5178 static void
5179 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5180 	struct compound_state *cs)
5181 {
5182 	/*
5183 	 * verify and nverify are exactly the same, except that nverify
5184 	 * succeeds when some argument changed, and verify succeeds when
5185 	 * when none changed.
5186 	 */
5187 
5188 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
5189 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
5190 
5191 	int error;
5192 	struct nfs4_svgetit_arg sarg;
5193 	struct statvfs64 sb;
5194 	struct nfs4_ntov_table ntov;
5195 
5196 	DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5197 	    NVERIFY4args *, args);
5198 
5199 	if (cs->vp == NULL) {
5200 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5201 		DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5202 		    NVERIFY4res *, resp);
5203 		return;
5204 	}
5205 	sarg.sbp = &sb;
5206 	nfs4_ntov_table_init(&ntov);
5207 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5208 	    &sarg, &ntov, NFS4ATTR_VERIT);
5209 	if (resp->status != NFS4_OK) {
5210 		/*
5211 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5212 		 * so could return -1 for "no match".
5213 		 */
5214 		if (resp->status == -1)
5215 			resp->status = NFS4_OK;
5216 		goto done;
5217 	}
5218 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5219 	switch (error) {
5220 	case 0:
5221 		resp->status = NFS4ERR_SAME;
5222 		break;
5223 	case -1:
5224 		resp->status = NFS4_OK;
5225 		break;
5226 	default:
5227 		resp->status = puterrno4(error);
5228 		break;
5229 	}
5230 done:
5231 	*cs->statusp = resp->status;
5232 	nfs4_ntov_table_free(&ntov, &sarg);
5233 
5234 	DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5235 	    NVERIFY4res *, resp);
5236 }
5237 
5238 /*
5239  * XXX - This should live in an NFS header file.
5240  */
5241 #define	MAX_IOVECS	12
5242 
5243 /* ARGSUSED */
5244 static void
5245 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5246 	struct compound_state *cs)
5247 {
5248 	WRITE4args *args = &argop->nfs_argop4_u.opwrite;
5249 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
5250 	int error;
5251 	vnode_t *vp;
5252 	struct vattr bva;
5253 	u_offset_t rlimit;
5254 	struct uio uio;
5255 	struct iovec iov[MAX_IOVECS];
5256 	struct iovec *iovp;
5257 	int iovcnt;
5258 	int ioflag;
5259 	cred_t *savecred, *cr;
5260 	bool_t *deleg = &cs->deleg;
5261 	nfsstat4 stat;
5262 	int in_crit = 0;
5263 	caller_context_t ct;
5264 
5265 	DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5266 	    WRITE4args *, args);
5267 
5268 	vp = cs->vp;
5269 	if (vp == NULL) {
5270 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5271 		goto out;
5272 	}
5273 	if (cs->access == CS_ACCESS_DENIED) {
5274 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5275 		goto out;
5276 	}
5277 
5278 	cr = cs->cr;
5279 
5280 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5281 	    deleg, TRUE, &ct)) != NFS4_OK) {
5282 		*cs->statusp = resp->status = stat;
5283 		goto out;
5284 	}
5285 
5286 	/*
5287 	 * We have to enter the critical region before calling VOP_RWLOCK
5288 	 * to avoid a deadlock with ufs.
5289 	 */
5290 	if (nbl_need_check(vp)) {
5291 		nbl_start_crit(vp, RW_READER);
5292 		in_crit = 1;
5293 		if (nbl_conflict(vp, NBL_WRITE,
5294 		    args->offset, args->data_len, 0, &ct)) {
5295 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
5296 			goto out;
5297 		}
5298 	}
5299 
5300 	bva.va_mask = AT_MODE | AT_UID;
5301 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
5302 
5303 	/*
5304 	 * If we can't get the attributes, then we can't do the
5305 	 * right access checking.  So, we'll fail the request.
5306 	 */
5307 	if (error) {
5308 		*cs->statusp = resp->status = puterrno4(error);
5309 		goto out;
5310 	}
5311 
5312 	if (rdonly4(cs->exi, cs->vp, req)) {
5313 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5314 		goto out;
5315 	}
5316 
5317 	if (vp->v_type != VREG) {
5318 		*cs->statusp = resp->status =
5319 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
5320 		goto out;
5321 	}
5322 
5323 	if (crgetuid(cr) != bva.va_uid &&
5324 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
5325 		*cs->statusp = resp->status = puterrno4(error);
5326 		goto out;
5327 	}
5328 
5329 	if (MANDLOCK(vp, bva.va_mode)) {
5330 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5331 		goto out;
5332 	}
5333 
5334 	if (args->data_len == 0) {
5335 		*cs->statusp = resp->status = NFS4_OK;
5336 		resp->count = 0;
5337 		resp->committed = args->stable;
5338 		resp->writeverf = Write4verf;
5339 		goto out;
5340 	}
5341 
5342 	if (args->mblk != NULL) {
5343 		mblk_t *m;
5344 		uint_t bytes, round_len;
5345 
5346 		iovcnt = 0;
5347 		bytes = 0;
5348 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
5349 		for (m = args->mblk;
5350 		    m != NULL && bytes < round_len;
5351 		    m = m->b_cont) {
5352 			iovcnt++;
5353 			bytes += MBLKL(m);
5354 		}
5355 #ifdef DEBUG
5356 		/* should have ended on an mblk boundary */
5357 		if (bytes != round_len) {
5358 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
5359 			    bytes, round_len, args->data_len);
5360 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
5361 			    (void *)args->mblk, (void *)m);
5362 			ASSERT(bytes == round_len);
5363 		}
5364 #endif
5365 		if (iovcnt <= MAX_IOVECS) {
5366 			iovp = iov;
5367 		} else {
5368 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
5369 		}
5370 		mblk_to_iov(args->mblk, iovcnt, iovp);
5371 	} else {
5372 		iovcnt = 1;
5373 		iovp = iov;
5374 		iovp->iov_base = args->data_val;
5375 		iovp->iov_len = args->data_len;
5376 	}
5377 
5378 	uio.uio_iov = iovp;
5379 	uio.uio_iovcnt = iovcnt;
5380 
5381 	uio.uio_segflg = UIO_SYSSPACE;
5382 	uio.uio_extflg = UIO_COPY_DEFAULT;
5383 	uio.uio_loffset = args->offset;
5384 	uio.uio_resid = args->data_len;
5385 	uio.uio_llimit = curproc->p_fsz_ctl;
5386 	rlimit = uio.uio_llimit - args->offset;
5387 	if (rlimit < (u_offset_t)uio.uio_resid)
5388 		uio.uio_resid = (int)rlimit;
5389 
5390 	if (args->stable == UNSTABLE4)
5391 		ioflag = 0;
5392 	else if (args->stable == FILE_SYNC4)
5393 		ioflag = FSYNC;
5394 	else if (args->stable == DATA_SYNC4)
5395 		ioflag = FDSYNC;
5396 	else {
5397 		if (iovp != iov)
5398 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
5399 		*cs->statusp = resp->status = NFS4ERR_INVAL;
5400 		goto out;
5401 	}
5402 
5403 	/*
5404 	 * We're changing creds because VM may fault and we need
5405 	 * the cred of the current thread to be used if quota
5406 	 * checking is enabled.
5407 	 */
5408 	savecred = curthread->t_cred;
5409 	curthread->t_cred = cr;
5410 	error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
5411 	curthread->t_cred = savecred;
5412 
5413 	if (iovp != iov)
5414 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
5415 
5416 	if (error) {
5417 		*cs->statusp = resp->status = puterrno4(error);
5418 		goto out;
5419 	}
5420 
5421 	*cs->statusp = resp->status = NFS4_OK;
5422 	resp->count = args->data_len - uio.uio_resid;
5423 
5424 	if (ioflag == 0)
5425 		resp->committed = UNSTABLE4;
5426 	else
5427 		resp->committed = FILE_SYNC4;
5428 
5429 	resp->writeverf = Write4verf;
5430 
5431 out:
5432 	if (in_crit)
5433 		nbl_end_crit(vp);
5434 
5435 	DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5436 	    WRITE4res *, resp);
5437 }
5438 
5439 
5440 /* XXX put in a header file */
5441 extern int	sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
5442 
5443 void
5444 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
5445 	struct svc_req *req, cred_t *cr)
5446 {
5447 	uint_t i;
5448 	struct compound_state cs;
5449 
5450 	rfs4_init_compound_state(&cs);
5451 	/*
5452 	 * Form a reply tag by copying over the reqeuest tag.
5453 	 */
5454 	resp->tag.utf8string_val =
5455 	    kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5456 	resp->tag.utf8string_len = args->tag.utf8string_len;
5457 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5458 	    resp->tag.utf8string_len);
5459 
5460 	cs.statusp = &resp->status;
5461 	cs.req = req;
5462 
5463 	/*
5464 	 * XXX for now, minorversion should be zero
5465 	 */
5466 	if (args->minorversion != NFS4_MINORVERSION) {
5467 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5468 		    &cs, COMPOUND4args *, args);
5469 		resp->array_len = 0;
5470 		resp->array = NULL;
5471 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5472 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5473 		    &cs, COMPOUND4res *, resp);
5474 		resp->array_len = 0;
5475 		return;
5476 	}
5477 
5478 	resp->array_len = args->array_len;
5479 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5480 	    KM_SLEEP);
5481 
5482 	ASSERT(exi == NULL);
5483 	ASSERT(cr == NULL);
5484 
5485 	cr = crget();
5486 	ASSERT(cr != NULL);
5487 
5488 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5489 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5490 		    &cs, COMPOUND4args *, args);
5491 		crfree(cr);
5492 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5493 		    &cs, COMPOUND4res *, resp);
5494 		return;
5495 	}
5496 
5497 	cs.basecr = cr;
5498 
5499 	DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5500 	    COMPOUND4args *, args);
5501 
5502 	/*
5503 	 * For now, NFS4 compound processing must be protected by
5504 	 * exported_lock because it can access more than one exportinfo
5505 	 * per compound and share/unshare can now change multiple
5506 	 * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5507 	 * per proc (excluding public exinfo), and exi_count design
5508 	 * is sufficient to protect concurrent execution of NFS2/3
5509 	 * ops along with unexport.  This lock will be removed as
5510 	 * part of the NFSv4 phase 2 namespace redesign work.
5511 	 */
5512 	rw_enter(&exported_lock, RW_READER);
5513 
5514 	/*
5515 	 * If this is the first compound we've seen, we need to start all
5516 	 * new instances' grace periods.
5517 	 */
5518 	if (rfs4_seen_first_compound == 0) {
5519 		rfs4_grace_start_new();
5520 		/*
5521 		 * This must be set after rfs4_grace_start_new(), otherwise
5522 		 * another thread could proceed past here before the former
5523 		 * is finished.
5524 		 */
5525 		rfs4_seen_first_compound = 1;
5526 	}
5527 
5528 	for (i = 0; i < args->array_len && cs.cont; i++) {
5529 		nfs_argop4 *argop;
5530 		nfs_resop4 *resop;
5531 		uint_t op;
5532 
5533 		argop = &args->array[i];
5534 		resop = &resp->array[i];
5535 		resop->resop = argop->argop;
5536 		op = (uint_t)resop->resop;
5537 
5538 		if (op < rfsv4disp_cnt) {
5539 			/*
5540 			 * Count the individual ops here; NULL and COMPOUND
5541 			 * are counted in common_dispatch()
5542 			 */
5543 			rfsproccnt_v4_ptr[op].value.ui64++;
5544 
5545 			NFS4_DEBUG(rfs4_debug > 1,
5546 			    (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5547 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5548 			NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5549 			    rfs4_op_string[op], *cs.statusp));
5550 			if (*cs.statusp != NFS4_OK)
5551 				cs.cont = FALSE;
5552 		} else {
5553 			/*
5554 			 * This is effectively dead code since XDR code
5555 			 * will have already returned BADXDR if op doesn't
5556 			 * decode to legal value.  This only done for a
5557 			 * day when XDR code doesn't verify v4 opcodes.
5558 			 */
5559 			op = OP_ILLEGAL;
5560 			rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
5561 
5562 			rfs4_op_illegal(argop, resop, req, &cs);
5563 			cs.cont = FALSE;
5564 		}
5565 
5566 		/*
5567 		 * If not at last op, and if we are to stop, then
5568 		 * compact the results array.
5569 		 */
5570 		if ((i + 1) < args->array_len && !cs.cont) {
5571 			nfs_resop4 *new_res = kmem_alloc(
5572 			    (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5573 			bcopy(resp->array,
5574 			    new_res, (i+1) * sizeof (nfs_resop4));
5575 			kmem_free(resp->array,
5576 			    args->array_len * sizeof (nfs_resop4));
5577 
5578 			resp->array_len =  i + 1;
5579 			resp->array = new_res;
5580 		}
5581 	}
5582 
5583 	rw_exit(&exported_lock);
5584 
5585 	DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5586 	    COMPOUND4res *, resp);
5587 
5588 	if (cs.vp)
5589 		VN_RELE(cs.vp);
5590 	if (cs.saved_vp)
5591 		VN_RELE(cs.saved_vp);
5592 	if (cs.saved_fh.nfs_fh4_val)
5593 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5594 
5595 	if (cs.basecr)
5596 		crfree(cs.basecr);
5597 	if (cs.cr)
5598 		crfree(cs.cr);
5599 	/*
5600 	 * done with this compound request, free the label
5601 	 */
5602 
5603 	if (req->rq_label != NULL) {
5604 		kmem_free(req->rq_label, sizeof (bslabel_t));
5605 		req->rq_label = NULL;
5606 	}
5607 }
5608 
5609 /*
5610  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5611  * XXX zero out the tag and array values. Need to investigate why the
5612  * XXX calls occur, but at least prevent the panic for now.
5613  */
5614 void
5615 rfs4_compound_free(COMPOUND4res *resp)
5616 {
5617 	uint_t i;
5618 
5619 	if (resp->tag.utf8string_val) {
5620 		UTF8STRING_FREE(resp->tag)
5621 	}
5622 
5623 	for (i = 0; i < resp->array_len; i++) {
5624 		nfs_resop4 *resop;
5625 		uint_t op;
5626 
5627 		resop = &resp->array[i];
5628 		op = (uint_t)resop->resop;
5629 		if (op < rfsv4disp_cnt) {
5630 			(*rfsv4disptab[op].dis_resfree)(resop);
5631 		}
5632 	}
5633 	if (resp->array != NULL) {
5634 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
5635 	}
5636 }
5637 
5638 /*
5639  * Process the value of the compound request rpc flags, as a bit-AND
5640  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
5641  */
5642 void
5643 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
5644 {
5645 	int i;
5646 	int flag = RPC_ALL;
5647 
5648 	for (i = 0; flag && i < args->array_len; i++) {
5649 		uint_t op;
5650 
5651 		op = (uint_t)args->array[i].argop;
5652 
5653 		if (op < rfsv4disp_cnt)
5654 			flag &= rfsv4disptab[op].dis_flags;
5655 		else
5656 			flag = 0;
5657 	}
5658 	*flagp = flag;
5659 }
5660 
5661 nfsstat4
5662 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
5663 {
5664 	nfsstat4 e;
5665 
5666 	rfs4_dbe_lock(cp->dbe);
5667 
5668 	if (cp->sysidt != LM_NOSYSID) {
5669 		*sp = cp->sysidt;
5670 		e = NFS4_OK;
5671 
5672 	} else if ((cp->sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
5673 		*sp = cp->sysidt;
5674 		e = NFS4_OK;
5675 
5676 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
5677 		    "rfs4_client_sysid: allocated 0x%x\n", *sp));
5678 	} else
5679 		e = NFS4ERR_DELAY;
5680 
5681 	rfs4_dbe_unlock(cp->dbe);
5682 	return (e);
5683 }
5684 
5685 #if defined(DEBUG) && ! defined(lint)
5686 static void lock_print(char *str, int operation, struct flock64 *flk)
5687 {
5688 	char *op, *type;
5689 
5690 	switch (operation) {
5691 	case F_GETLK: op = "F_GETLK";
5692 		break;
5693 	case F_SETLK: op = "F_SETLK";
5694 		break;
5695 	case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
5696 		break;
5697 	default: op = "F_UNKNOWN";
5698 		break;
5699 	}
5700 	switch (flk->l_type) {
5701 	case F_UNLCK: type = "F_UNLCK";
5702 		break;
5703 	case F_RDLCK: type = "F_RDLCK";
5704 		break;
5705 	case F_WRLCK: type = "F_WRLCK";
5706 		break;
5707 	default: type = "F_UNKNOWN";
5708 		break;
5709 	}
5710 
5711 	ASSERT(flk->l_whence == 0);
5712 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
5713 	    str, op, type, (longlong_t)flk->l_start,
5714 	    flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
5715 }
5716 
5717 #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
5718 #else
5719 #define	LOCK_PRINT(d, s, t, f)
5720 #endif
5721 
5722 /*ARGSUSED*/
5723 static bool_t
5724 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
5725 {
5726 	return (TRUE);
5727 }
5728 
5729 /*
5730  * Look up the pathname using the vp in cs as the directory vnode.
5731  * cs->vp will be the vnode for the file on success
5732  */
5733 
5734 static nfsstat4
5735 rfs4_lookup(component4 *component, struct svc_req *req,
5736 	    struct compound_state *cs)
5737 {
5738 	char *nm;
5739 	uint32_t len;
5740 	nfsstat4 status;
5741 
5742 	if (cs->vp == NULL) {
5743 		return (NFS4ERR_NOFILEHANDLE);
5744 	}
5745 	if (cs->vp->v_type != VDIR) {
5746 		return (NFS4ERR_NOTDIR);
5747 	}
5748 
5749 	if (!utf8_dir_verify(component))
5750 		return (NFS4ERR_INVAL);
5751 
5752 	nm = utf8_to_fn(component, &len, NULL);
5753 	if (nm == NULL) {
5754 		return (NFS4ERR_INVAL);
5755 	}
5756 
5757 	if (len > MAXNAMELEN) {
5758 		kmem_free(nm, len);
5759 		return (NFS4ERR_NAMETOOLONG);
5760 	}
5761 
5762 	status = do_rfs4_op_lookup(nm, len, req, cs);
5763 
5764 	kmem_free(nm, len);
5765 
5766 	return (status);
5767 }
5768 
5769 static nfsstat4
5770 rfs4_lookupfile(component4 *component, struct svc_req *req,
5771 		struct compound_state *cs, uint32_t access,
5772 		change_info4 *cinfo)
5773 {
5774 	nfsstat4 status;
5775 	vnode_t *dvp = cs->vp;
5776 	vattr_t bva, ava, fva;
5777 	int error;
5778 
5779 	/* Get "before" change value */
5780 	bva.va_mask = AT_CTIME|AT_SEQ;
5781 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
5782 	if (error)
5783 		return (puterrno4(error));
5784 
5785 	/* rfs4_lookup may VN_RELE directory */
5786 	VN_HOLD(dvp);
5787 
5788 	status = rfs4_lookup(component, req, cs);
5789 	if (status != NFS4_OK) {
5790 		VN_RELE(dvp);
5791 		return (status);
5792 	}
5793 
5794 	/*
5795 	 * Get "after" change value, if it fails, simply return the
5796 	 * before value.
5797 	 */
5798 	ava.va_mask = AT_CTIME|AT_SEQ;
5799 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
5800 		ava.va_ctime = bva.va_ctime;
5801 		ava.va_seq = 0;
5802 	}
5803 	VN_RELE(dvp);
5804 
5805 	/*
5806 	 * Validate the file is a file
5807 	 */
5808 	fva.va_mask = AT_TYPE|AT_MODE;
5809 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
5810 	if (error)
5811 		return (puterrno4(error));
5812 
5813 	if (fva.va_type != VREG) {
5814 		if (fva.va_type == VDIR)
5815 			return (NFS4ERR_ISDIR);
5816 		if (fva.va_type == VLNK)
5817 			return (NFS4ERR_SYMLINK);
5818 		return (NFS4ERR_INVAL);
5819 	}
5820 
5821 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
5822 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
5823 
5824 	/*
5825 	 * It is undefined if VOP_LOOKUP will change va_seq, so
5826 	 * cinfo.atomic = TRUE only if we have
5827 	 * non-zero va_seq's, and they have not changed.
5828 	 */
5829 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
5830 		cinfo->atomic = TRUE;
5831 	else
5832 		cinfo->atomic = FALSE;
5833 
5834 	/* Check for mandatory locking */
5835 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
5836 	return (check_open_access(access, cs, req));
5837 }
5838 
5839 static nfsstat4
5840 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
5841 	    timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created)
5842 {
5843 	int error;
5844 	nfsstat4 status = NFS4_OK;
5845 	vattr_t va;
5846 
5847 tryagain:
5848 
5849 	/*
5850 	 * The file open mode used is VWRITE.  If the client needs
5851 	 * some other semantic, then it should do the access checking
5852 	 * itself.  It would have been nice to have the file open mode
5853 	 * passed as part of the arguments.
5854 	 */
5855 
5856 	*created = TRUE;
5857 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
5858 
5859 	if (error) {
5860 		*created = FALSE;
5861 
5862 		/*
5863 		 * If we got something other than file already exists
5864 		 * then just return this error.  Otherwise, we got
5865 		 * EEXIST.  If we were doing a GUARDED create, then
5866 		 * just return this error.  Otherwise, we need to
5867 		 * make sure that this wasn't a duplicate of an
5868 		 * exclusive create request.
5869 		 *
5870 		 * The assumption is made that a non-exclusive create
5871 		 * request will never return EEXIST.
5872 		 */
5873 
5874 		if (error != EEXIST || mode == GUARDED4) {
5875 			status = puterrno4(error);
5876 			return (status);
5877 		}
5878 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
5879 		    NULL, NULL, NULL);
5880 
5881 		if (error) {
5882 			/*
5883 			 * We couldn't find the file that we thought that
5884 			 * we just created.  So, we'll just try creating
5885 			 * it again.
5886 			 */
5887 			if (error == ENOENT)
5888 				goto tryagain;
5889 
5890 			status = puterrno4(error);
5891 			return (status);
5892 		}
5893 
5894 		if (mode == UNCHECKED4) {
5895 			/* existing object must be regular file */
5896 			if ((*vpp)->v_type != VREG) {
5897 				if ((*vpp)->v_type == VDIR)
5898 					status = NFS4ERR_ISDIR;
5899 				else if ((*vpp)->v_type == VLNK)
5900 					status = NFS4ERR_SYMLINK;
5901 				else
5902 					status = NFS4ERR_INVAL;
5903 				VN_RELE(*vpp);
5904 				return (status);
5905 			}
5906 
5907 			return (NFS4_OK);
5908 		}
5909 
5910 		/* Check for duplicate request */
5911 		ASSERT(mtime != 0);
5912 		va.va_mask = AT_MTIME;
5913 		error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
5914 		if (!error) {
5915 			/* We found the file */
5916 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
5917 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
5918 				/* but its not our creation */
5919 				VN_RELE(*vpp);
5920 				return (NFS4ERR_EXIST);
5921 			}
5922 			*created = TRUE; /* retrans of create == created */
5923 			return (NFS4_OK);
5924 		}
5925 		VN_RELE(*vpp);
5926 		return (NFS4ERR_EXIST);
5927 	}
5928 
5929 	return (NFS4_OK);
5930 }
5931 
5932 static nfsstat4
5933 check_open_access(uint32_t access,
5934 		struct compound_state *cs, struct svc_req *req)
5935 {
5936 	int error;
5937 	vnode_t *vp;
5938 	bool_t readonly;
5939 	cred_t *cr = cs->cr;
5940 
5941 	/* For now we don't allow mandatory locking as per V2/V3 */
5942 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
5943 		return (NFS4ERR_ACCESS);
5944 	}
5945 
5946 	vp = cs->vp;
5947 	ASSERT(cr != NULL && vp->v_type == VREG);
5948 
5949 	/*
5950 	 * If the file system is exported read only and we are trying
5951 	 * to open for write, then return NFS4ERR_ROFS
5952 	 */
5953 
5954 	readonly = rdonly4(cs->exi, cs->vp, req);
5955 
5956 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
5957 		return (NFS4ERR_ROFS);
5958 
5959 	if (access & OPEN4_SHARE_ACCESS_READ) {
5960 		if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
5961 		    (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
5962 			return (NFS4ERR_ACCESS);
5963 		}
5964 	}
5965 
5966 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
5967 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
5968 		if (error)
5969 			return (NFS4ERR_ACCESS);
5970 	}
5971 
5972 	return (NFS4_OK);
5973 }
5974 
5975 static nfsstat4
5976 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
5977 		change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
5978 {
5979 	struct nfs4_svgetit_arg sarg;
5980 	struct nfs4_ntov_table ntov;
5981 
5982 	bool_t ntov_table_init = FALSE;
5983 	struct statvfs64 sb;
5984 	nfsstat4 status;
5985 	vnode_t *vp;
5986 	vattr_t bva, ava, iva, cva, *vap;
5987 	vnode_t *dvp;
5988 	timespec32_t *mtime;
5989 	char *nm = NULL;
5990 	uint_t buflen;
5991 	bool_t created;
5992 	bool_t setsize = FALSE;
5993 	len_t reqsize;
5994 	int error;
5995 	bool_t trunc;
5996 	caller_context_t ct;
5997 	component4 *component;
5998 	bslabel_t *clabel;
5999 
6000 	sarg.sbp = &sb;
6001 
6002 	dvp = cs->vp;
6003 
6004 	/* Check if the file system is read only */
6005 	if (rdonly4(cs->exi, dvp, req))
6006 		return (NFS4ERR_ROFS);
6007 
6008 	/* check the label of including directory */
6009 	if (is_system_labeled()) {
6010 		ASSERT(req->rq_label != NULL);
6011 		clabel = req->rq_label;
6012 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6013 		    "got client label from request(1)",
6014 		    struct svc_req *, req);
6015 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
6016 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK)) {
6017 				return (NFS4ERR_ACCESS);
6018 			}
6019 		}
6020 	}
6021 
6022 	/*
6023 	 * Get the last component of path name in nm. cs will reference
6024 	 * the including directory on success.
6025 	 */
6026 	component = &args->open_claim4_u.file;
6027 	if (!utf8_dir_verify(component))
6028 		return (NFS4ERR_INVAL);
6029 
6030 	nm = utf8_to_fn(component, &buflen, NULL);
6031 
6032 	if (nm == NULL)
6033 		return (NFS4ERR_RESOURCE);
6034 
6035 	if (buflen > MAXNAMELEN) {
6036 		kmem_free(nm, buflen);
6037 		return (NFS4ERR_NAMETOOLONG);
6038 	}
6039 
6040 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6041 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6042 	if (error) {
6043 		kmem_free(nm, buflen);
6044 		return (puterrno4(error));
6045 	}
6046 
6047 	if (bva.va_type != VDIR) {
6048 		kmem_free(nm, buflen);
6049 		return (NFS4ERR_NOTDIR);
6050 	}
6051 
6052 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6053 
6054 	switch (args->mode) {
6055 	case GUARDED4:
6056 		/*FALLTHROUGH*/
6057 	case UNCHECKED4:
6058 		nfs4_ntov_table_init(&ntov);
6059 		ntov_table_init = TRUE;
6060 
6061 		*attrset = 0;
6062 		status = do_rfs4_set_attrs(attrset,
6063 		    &args->createhow4_u.createattrs,
6064 		    cs, &sarg, &ntov, NFS4ATTR_SETIT);
6065 
6066 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6067 		    sarg.vap->va_type != VREG) {
6068 			if (sarg.vap->va_type == VDIR)
6069 				status = NFS4ERR_ISDIR;
6070 			else if (sarg.vap->va_type == VLNK)
6071 				status = NFS4ERR_SYMLINK;
6072 			else
6073 				status = NFS4ERR_INVAL;
6074 		}
6075 
6076 		if (status != NFS4_OK) {
6077 			kmem_free(nm, buflen);
6078 			nfs4_ntov_table_free(&ntov, &sarg);
6079 			*attrset = 0;
6080 			return (status);
6081 		}
6082 
6083 		vap = sarg.vap;
6084 		vap->va_type = VREG;
6085 		vap->va_mask |= AT_TYPE;
6086 
6087 		if ((vap->va_mask & AT_MODE) == 0) {
6088 			vap->va_mask |= AT_MODE;
6089 			vap->va_mode = (mode_t)0600;
6090 		}
6091 
6092 		if (vap->va_mask & AT_SIZE) {
6093 
6094 			/* Disallow create with a non-zero size */
6095 
6096 			if ((reqsize = sarg.vap->va_size) != 0) {
6097 				kmem_free(nm, buflen);
6098 				nfs4_ntov_table_free(&ntov, &sarg);
6099 				*attrset = 0;
6100 				return (NFS4ERR_INVAL);
6101 			}
6102 			setsize = TRUE;
6103 		}
6104 		break;
6105 
6106 	case EXCLUSIVE4:
6107 		/* prohibit EXCL create of named attributes */
6108 		if (dvp->v_flag & V_XATTRDIR) {
6109 			kmem_free(nm, buflen);
6110 			*attrset = 0;
6111 			return (NFS4ERR_INVAL);
6112 		}
6113 
6114 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6115 		cva.va_type = VREG;
6116 		/*
6117 		 * Ensure no time overflows. Assumes underlying
6118 		 * filesystem supports at least 32 bits.
6119 		 * Truncate nsec to usec resolution to allow valid
6120 		 * compares even if the underlying filesystem truncates.
6121 		 */
6122 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
6123 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6124 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6125 		cva.va_mode = (mode_t)0;
6126 		vap = &cva;
6127 		break;
6128 	}
6129 
6130 	status = create_vnode(dvp, nm, vap, args->mode, mtime,
6131 	    cs->cr, &vp, &created);
6132 	kmem_free(nm, buflen);
6133 
6134 	if (status != NFS4_OK) {
6135 		if (ntov_table_init)
6136 			nfs4_ntov_table_free(&ntov, &sarg);
6137 		*attrset = 0;
6138 		return (status);
6139 	}
6140 
6141 	trunc = (setsize && !created);
6142 
6143 	if (args->mode != EXCLUSIVE4) {
6144 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6145 
6146 		/*
6147 		 * True verification that object was created with correct
6148 		 * attrs is impossible.  The attrs could have been changed
6149 		 * immediately after object creation.  If attributes did
6150 		 * not verify, the only recourse for the server is to
6151 		 * destroy the object.  Maybe if some attrs (like gid)
6152 		 * are set incorrectly, the object should be destroyed;
6153 		 * however, seems bad as a default policy.  Do we really
6154 		 * want to destroy an object over one of the times not
6155 		 * verifying correctly?  For these reasons, the server
6156 		 * currently sets bits in attrset for createattrs
6157 		 * that were set; however, no verification is done.
6158 		 *
6159 		 * vmask_to_nmask accounts for vattr bits set on create
6160 		 *	[do_rfs4_set_attrs() only sets resp bits for
6161 		 *	 non-vattr/vfs bits.]
6162 		 * Mask off any bits we set by default so as not to return
6163 		 * more attrset bits than were requested in createattrs
6164 		 */
6165 		if (created) {
6166 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6167 			*attrset &= createmask;
6168 		} else {
6169 			/*
6170 			 * We did not create the vnode (we tried but it
6171 			 * already existed).  In this case, the only createattr
6172 			 * that the spec allows the server to set is size,
6173 			 * and even then, it can only be set if it is 0.
6174 			 */
6175 			*attrset = 0;
6176 			if (trunc)
6177 				*attrset = FATTR4_SIZE_MASK;
6178 		}
6179 	}
6180 	if (ntov_table_init)
6181 		nfs4_ntov_table_free(&ntov, &sarg);
6182 
6183 	/*
6184 	 * Get the initial "after" sequence number, if it fails,
6185 	 * set to zero, time to before.
6186 	 */
6187 	iva.va_mask = AT_CTIME|AT_SEQ;
6188 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6189 		iva.va_seq = 0;
6190 		iva.va_ctime = bva.va_ctime;
6191 	}
6192 
6193 	/*
6194 	 * create_vnode attempts to create the file exclusive,
6195 	 * if it already exists the VOP_CREATE will fail and
6196 	 * may not increase va_seq. It is atomic if
6197 	 * we haven't changed the directory, but if it has changed
6198 	 * we don't know what changed it.
6199 	 */
6200 	if (!created) {
6201 		if (bva.va_seq && iva.va_seq &&
6202 		    bva.va_seq == iva.va_seq)
6203 			cinfo->atomic = TRUE;
6204 		else
6205 			cinfo->atomic = FALSE;
6206 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6207 	} else {
6208 		/*
6209 		 * The entry was created, we need to sync the
6210 		 * directory metadata.
6211 		 */
6212 		(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6213 
6214 		/*
6215 		 * Get "after" change value, if it fails, simply return the
6216 		 * before value.
6217 		 */
6218 		ava.va_mask = AT_CTIME|AT_SEQ;
6219 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6220 			ava.va_ctime = bva.va_ctime;
6221 			ava.va_seq = 0;
6222 		}
6223 
6224 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6225 
6226 		/*
6227 		 * The cinfo->atomic = TRUE only if we have
6228 		 * non-zero va_seq's, and it has incremented by exactly one
6229 		 * during the create_vnode and it didn't
6230 		 * change during the VOP_FSYNC.
6231 		 */
6232 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
6233 		    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6234 			cinfo->atomic = TRUE;
6235 		else
6236 			cinfo->atomic = FALSE;
6237 	}
6238 
6239 	/* Check for mandatory locking and that the size gets set. */
6240 	cva.va_mask = AT_MODE;
6241 	if (setsize)
6242 		cva.va_mask |= AT_SIZE;
6243 
6244 	/* Assume the worst */
6245 	cs->mandlock = TRUE;
6246 
6247 	if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6248 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6249 
6250 		/*
6251 		 * Truncate the file if necessary; this would be
6252 		 * the case for create over an existing file.
6253 		 */
6254 
6255 		if (trunc) {
6256 			int in_crit = 0;
6257 			rfs4_file_t *fp;
6258 			bool_t create = FALSE;
6259 
6260 			/*
6261 			 * We are writing over an existing file.
6262 			 * Check to see if we need to recall a delegation.
6263 			 */
6264 			rfs4_hold_deleg_policy();
6265 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6266 				if (rfs4_check_delegated_byfp(FWRITE, fp,
6267 				    (reqsize == 0), FALSE, FALSE, &clientid)) {
6268 					rfs4_file_rele(fp);
6269 					rfs4_rele_deleg_policy();
6270 					VN_RELE(vp);
6271 					*attrset = 0;
6272 					return (NFS4ERR_DELAY);
6273 				}
6274 				rfs4_file_rele(fp);
6275 			}
6276 			rfs4_rele_deleg_policy();
6277 
6278 			if (nbl_need_check(vp)) {
6279 				in_crit = 1;
6280 
6281 				ASSERT(reqsize == 0);
6282 
6283 				nbl_start_crit(vp, RW_READER);
6284 				if (nbl_conflict(vp, NBL_WRITE, 0,
6285 				    cva.va_size, 0, NULL)) {
6286 					in_crit = 0;
6287 					nbl_end_crit(vp);
6288 					VN_RELE(vp);
6289 					*attrset = 0;
6290 					return (NFS4ERR_ACCESS);
6291 				}
6292 			}
6293 			ct.cc_sysid = 0;
6294 			ct.cc_pid = 0;
6295 			ct.cc_caller_id = nfs4_srv_caller_id;
6296 			ct.cc_flags = CC_DONTBLOCK;
6297 
6298 			cva.va_mask = AT_SIZE;
6299 			cva.va_size = reqsize;
6300 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6301 			if (in_crit)
6302 				nbl_end_crit(vp);
6303 		}
6304 	}
6305 
6306 	error = makefh4(&cs->fh, vp, cs->exi);
6307 
6308 	/*
6309 	 * Force modified data and metadata out to stable storage.
6310 	 */
6311 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6312 
6313 	if (error) {
6314 		VN_RELE(vp);
6315 		*attrset = 0;
6316 		return (puterrno4(error));
6317 	}
6318 
6319 	/* if parent dir is attrdir, set namedattr fh flag */
6320 	if (dvp->v_flag & V_XATTRDIR)
6321 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6322 
6323 	if (cs->vp)
6324 		VN_RELE(cs->vp);
6325 
6326 	cs->vp = vp;
6327 
6328 	/*
6329 	 * if we did not create the file, we will need to check
6330 	 * the access bits on the file
6331 	 */
6332 
6333 	if (!created) {
6334 		if (setsize)
6335 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6336 		status = check_open_access(args->share_access, cs, req);
6337 		if (status != NFS4_OK)
6338 			*attrset = 0;
6339 	}
6340 	return (status);
6341 }
6342 
6343 /*ARGSUSED*/
6344 static void
6345 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6346 		rfs4_openowner_t *oo, delegreq_t deleg,
6347 		uint32_t access, uint32_t deny,
6348 		OPEN4res *resp, int deleg_cur)
6349 {
6350 	/* XXX Currently not using req  */
6351 	rfs4_state_t *state;
6352 	rfs4_file_t *file;
6353 	bool_t screate = TRUE;
6354 	bool_t fcreate = TRUE;
6355 	uint32_t amodes;
6356 	uint32_t dmodes;
6357 	rfs4_deleg_state_t *dsp;
6358 	struct shrlock shr;
6359 	struct shr_locowner shr_loco;
6360 	sysid_t sysid;
6361 	nfsstat4 status;
6362 	caller_context_t ct;
6363 	int fflags = 0;
6364 	int recall = 0;
6365 	int err;
6366 	int cmd;
6367 
6368 	/* get the file struct and hold a lock on it during initial open */
6369 	file = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6370 	if (file == NULL) {
6371 		NFS4_DEBUG(rfs4_debug,
6372 		    (CE_NOTE, "rfs4_do_open: can't find file"));
6373 		resp->status = NFS4ERR_SERVERFAULT;
6374 		return;
6375 	}
6376 
6377 	state = rfs4_findstate_by_owner_file(oo, file, &screate);
6378 	if (state == NULL) {
6379 		NFS4_DEBUG(rfs4_debug,
6380 		    (CE_NOTE, "rfs4_do_open: can't find state"));
6381 		resp->status = NFS4ERR_RESOURCE;
6382 		/* No need to keep any reference */
6383 		rfs4_file_rele_withunlock(file);
6384 		return;
6385 	}
6386 
6387 	/* try to get the sysid before continuing */
6388 	if ((status = rfs4_client_sysid(oo->client, &sysid)) != NFS4_OK) {
6389 		resp->status = status;
6390 		rfs4_file_rele(file);
6391 		/* Not a fully formed open; "close" it */
6392 		if (screate == TRUE)
6393 			rfs4_state_close(state, FALSE, FALSE, cs->cr);
6394 		rfs4_state_rele(state);
6395 		return;
6396 	}
6397 
6398 	/* Calculate the fflags for this OPEN. */
6399 	if (access & OPEN4_SHARE_ACCESS_READ)
6400 		fflags |= FREAD;
6401 	if (access & OPEN4_SHARE_ACCESS_WRITE)
6402 		fflags |= FWRITE;
6403 
6404 	/*
6405 	 * Calculate the new deny and access mode that this open is adding to
6406 	 * the file for this open owner;
6407 	 */
6408 	dmodes = (deny & ~state->share_deny);
6409 	amodes = (access & ~state->share_access);
6410 
6411 	/*
6412 	 * Check to see the client has already sent an open for this
6413 	 * open owner on this file with the same share/deny modes.
6414 	 * If so, we don't need to check for a conflict and we don't
6415 	 * need to add another shrlock.  If not, then we need to
6416 	 * check for conflicts in deny and access before checking for
6417 	 * conflicts in delegation.  We don't want to recall a
6418 	 * delegation based on an open that will eventually fail based
6419 	 * on shares modes.
6420 	 */
6421 
6422 	if (dmodes || amodes) {
6423 		shr.s_access = (short)access;
6424 		shr.s_deny = (short)deny;
6425 		shr.s_pid = rfs4_dbe_getid(oo->dbe);
6426 		shr.s_sysid = sysid;
6427 		shr_loco.sl_pid = shr.s_pid;
6428 		shr_loco.sl_id = shr.s_sysid;
6429 		shr.s_owner = (caddr_t)&shr_loco;
6430 		shr.s_own_len = sizeof (shr_loco);
6431 
6432 		cmd = nbl_need_check(cs->vp) ? F_SHARE_NBMAND : F_SHARE;
6433 		if ((err = vop_shrlock(cs->vp, cmd, &shr, fflags)) != 0) {
6434 
6435 			resp->status = err == EAGAIN ?
6436 			    NFS4ERR_SHARE_DENIED : puterrno4(err);
6437 
6438 			rfs4_file_rele(file);
6439 			/* Not a fully formed open; "close" it */
6440 			if (screate == TRUE)
6441 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6442 			rfs4_state_rele(state);
6443 			return;
6444 		}
6445 	}
6446 
6447 	rfs4_dbe_lock(state->dbe);
6448 	rfs4_dbe_lock(file->dbe);
6449 
6450 	/*
6451 	 * Check to see if this file is delegated and if so, if a
6452 	 * recall needs to be done.
6453 	 */
6454 	if (rfs4_check_recall(state, access)) {
6455 		rfs4_dbe_unlock(file->dbe);
6456 		rfs4_dbe_unlock(state->dbe);
6457 		rfs4_recall_deleg(file, FALSE, state->owner->client);
6458 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6459 		rfs4_dbe_lock(state->dbe);
6460 		rfs4_dbe_lock(file->dbe);
6461 		/* Let's see if the delegation was returned */
6462 		if (rfs4_check_recall(state, access)) {
6463 			rfs4_dbe_unlock(file->dbe);
6464 			rfs4_dbe_unlock(state->dbe);
6465 			rfs4_file_rele(file);
6466 			rfs4_update_lease(state->owner->client);
6467 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6468 			/* Not a fully formed open; "close" it */
6469 			if (screate == TRUE)
6470 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6471 			rfs4_state_rele(state);
6472 			resp->status = NFS4ERR_DELAY;
6473 			return;
6474 		}
6475 	}
6476 	/*
6477 	 * the share check passed and any delegation conflict has been
6478 	 * taken care of, now call vop_open.
6479 	 * if this is the first open then call vop_open with fflags.
6480 	 * if not, call vn_open_upgrade with just the upgrade flags.
6481 	 *
6482 	 * if the file has been opened already, it will have the current
6483 	 * access mode in the state struct.  if it has no share access, then
6484 	 * this is a new open.
6485 	 *
6486 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6487 	 * call VOP_OPEN(), just do the open upgrade.
6488 	 */
6489 	if (((state->share_access & OPEN4_SHARE_ACCESS_BOTH) == 0) &&
6490 	    !deleg_cur) {
6491 		ct.cc_sysid = sysid;
6492 		ct.cc_pid = shr.s_pid;
6493 		ct.cc_caller_id = nfs4_srv_caller_id;
6494 		ct.cc_flags = CC_DONTBLOCK;
6495 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6496 		if (err) {
6497 			rfs4_dbe_unlock(file->dbe);
6498 			rfs4_dbe_unlock(state->dbe);
6499 			rfs4_file_rele(file);
6500 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6501 			/* Not a fully formed open; "close" it */
6502 			if (screate == TRUE)
6503 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6504 			rfs4_state_rele(state);
6505 			resp->status = NFS4ERR_SERVERFAULT;
6506 			return;
6507 		}
6508 	} else { /* open upgrade */
6509 		/*
6510 		 * calculate the fflags for the new mode that is being added
6511 		 * by this upgrade.
6512 		 */
6513 		fflags = 0;
6514 		if (amodes & OPEN4_SHARE_ACCESS_READ)
6515 			fflags |= FREAD;
6516 		if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6517 			fflags |= FWRITE;
6518 		vn_open_upgrade(cs->vp, fflags);
6519 	}
6520 
6521 	if (dmodes & OPEN4_SHARE_DENY_READ)
6522 		file->deny_read++;
6523 	if (dmodes & OPEN4_SHARE_DENY_WRITE)
6524 		file->deny_write++;
6525 	file->share_deny |= deny;
6526 	state->share_deny |= deny;
6527 
6528 	if (amodes & OPEN4_SHARE_ACCESS_READ)
6529 		file->access_read++;
6530 	if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6531 		file->access_write++;
6532 	file->share_access |= access;
6533 	state->share_access |= access;
6534 
6535 	/*
6536 	 * Check for delegation here. if the deleg argument is not
6537 	 * DELEG_ANY, then this is a reclaim from a client and
6538 	 * we must honor the delegation requested. If necessary we can
6539 	 * set the recall flag.
6540 	 */
6541 
6542 	dsp = rfs4_grant_delegation(deleg, state, &recall);
6543 
6544 	cs->deleg = (file->dinfo->dtype == OPEN_DELEGATE_WRITE);
6545 
6546 	next_stateid(&state->stateid);
6547 
6548 	resp->stateid = state->stateid.stateid;
6549 
6550 	rfs4_dbe_unlock(file->dbe);
6551 	rfs4_dbe_unlock(state->dbe);
6552 
6553 	if (dsp) {
6554 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6555 		rfs4_deleg_state_rele(dsp);
6556 	}
6557 
6558 	rfs4_file_rele(file);
6559 	rfs4_state_rele(state);
6560 
6561 	resp->status = NFS4_OK;
6562 }
6563 
6564 /*ARGSUSED*/
6565 static void
6566 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6567 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6568 {
6569 	change_info4 *cinfo = &resp->cinfo;
6570 	bitmap4 *attrset = &resp->attrset;
6571 
6572 	if (args->opentype == OPEN4_NOCREATE)
6573 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6574 		    req, cs, args->share_access, cinfo);
6575 	else {
6576 		/* inhibit delegation grants during exclusive create */
6577 
6578 		if (args->mode == EXCLUSIVE4)
6579 			rfs4_disable_delegation();
6580 
6581 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
6582 		    oo->client->clientid);
6583 	}
6584 
6585 	if (resp->status == NFS4_OK) {
6586 
6587 		/* cs->vp cs->fh now reference the desired file */
6588 
6589 		rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access,
6590 		    args->share_deny, resp, 0);
6591 
6592 		/*
6593 		 * If rfs4_createfile set attrset, we must
6594 		 * clear this attrset before the response is copied.
6595 		 */
6596 		if (resp->status != NFS4_OK && resp->attrset) {
6597 			resp->attrset = 0;
6598 		}
6599 	}
6600 	else
6601 		*cs->statusp = resp->status;
6602 
6603 	if (args->mode == EXCLUSIVE4)
6604 		rfs4_enable_delegation();
6605 }
6606 
6607 /*ARGSUSED*/
6608 static void
6609 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
6610 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6611 {
6612 	change_info4 *cinfo = &resp->cinfo;
6613 	vattr_t va;
6614 	vtype_t v_type = cs->vp->v_type;
6615 	int error = 0;
6616 
6617 	/* Verify that we have a regular file */
6618 	if (v_type != VREG) {
6619 		if (v_type == VDIR)
6620 			resp->status = NFS4ERR_ISDIR;
6621 		else if (v_type == VLNK)
6622 			resp->status = NFS4ERR_SYMLINK;
6623 		else
6624 			resp->status = NFS4ERR_INVAL;
6625 		return;
6626 	}
6627 
6628 	va.va_mask = AT_MODE|AT_UID;
6629 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
6630 	if (error) {
6631 		resp->status = puterrno4(error);
6632 		return;
6633 	}
6634 
6635 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
6636 
6637 	/*
6638 	 * Check if we have access to the file, Note the the file
6639 	 * could have originally been open UNCHECKED or GUARDED
6640 	 * with mode bits that will now fail, but there is nothing
6641 	 * we can really do about that except in the case that the
6642 	 * owner of the file is the one requesting the open.
6643 	 */
6644 	if (crgetuid(cs->cr) != va.va_uid) {
6645 		resp->status = check_open_access(args->share_access, cs, req);
6646 		if (resp->status != NFS4_OK) {
6647 			return;
6648 		}
6649 	}
6650 
6651 	/*
6652 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
6653 	 */
6654 	cinfo->before = 0;
6655 	cinfo->after = 0;
6656 	cinfo->atomic = FALSE;
6657 
6658 	rfs4_do_open(cs, req, oo,
6659 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
6660 	    args->share_access, args->share_deny, resp, 0);
6661 }
6662 
6663 static void
6664 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
6665 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6666 {
6667 	int error;
6668 	nfsstat4 status;
6669 	stateid4 stateid =
6670 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
6671 	rfs4_deleg_state_t *dsp;
6672 
6673 	/*
6674 	 * Find the state info from the stateid and confirm that the
6675 	 * file is delegated.  If the state openowner is the same as
6676 	 * the supplied openowner we're done. If not, get the file
6677 	 * info from the found state info. Use that file info to
6678 	 * create the state for this lock owner. Note solaris doen't
6679 	 * really need the pathname to find the file. We may want to
6680 	 * lookup the pathname and make sure that the vp exist and
6681 	 * matches the vp in the file structure. However it is
6682 	 * possible that the pathname nolonger exists (local process
6683 	 * unlinks the file), so this may not be that useful.
6684 	 */
6685 
6686 	status = rfs4_get_deleg_state(&stateid, &dsp);
6687 	if (status != NFS4_OK) {
6688 		resp->status = status;
6689 		return;
6690 	}
6691 
6692 	ASSERT(dsp->finfo->dinfo->dtype != OPEN_DELEGATE_NONE);
6693 
6694 	/*
6695 	 * New lock owner, create state. Since this was probably called
6696 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
6697 	 */
6698 
6699 	ASSERT(cs->vp != NULL);
6700 	VN_RELE(cs->vp);
6701 	VN_HOLD(dsp->finfo->vp);
6702 	cs->vp = dsp->finfo->vp;
6703 
6704 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
6705 		rfs4_deleg_state_rele(dsp);
6706 		*cs->statusp = resp->status = puterrno4(error);
6707 		return;
6708 	}
6709 
6710 	/* Mark progress for delegation returns */
6711 	dsp->finfo->dinfo->time_lastwrite = gethrestime_sec();
6712 	rfs4_deleg_state_rele(dsp);
6713 	rfs4_do_open(cs, req, oo, DELEG_NONE,
6714 	    args->share_access, args->share_deny, resp, 1);
6715 }
6716 
6717 /*ARGSUSED*/
6718 static void
6719 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
6720 			OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6721 {
6722 	/*
6723 	 * Lookup the pathname, it must already exist since this file
6724 	 * was delegated.
6725 	 *
6726 	 * Find the file and state info for this vp and open owner pair.
6727 	 *	check that they are in fact delegated.
6728 	 *	check that the state access and deny modes are the same.
6729 	 *
6730 	 * Return the delgation possibly seting the recall flag.
6731 	 */
6732 	rfs4_file_t *file;
6733 	rfs4_state_t *state;
6734 	bool_t create = FALSE;
6735 	bool_t dcreate = FALSE;
6736 	rfs4_deleg_state_t *dsp;
6737 	nfsace4 *ace;
6738 
6739 
6740 	/* Note we ignore oflags */
6741 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
6742 	    req, cs, args->share_access, &resp->cinfo);
6743 
6744 	if (resp->status != NFS4_OK) {
6745 		return;
6746 	}
6747 
6748 	/* get the file struct and hold a lock on it during initial open */
6749 	file = rfs4_findfile_withlock(cs->vp, NULL, &create);
6750 	if (file == NULL) {
6751 		NFS4_DEBUG(rfs4_debug,
6752 		    (CE_NOTE, "rfs4_do_opendelprev: can't find file"));
6753 		resp->status = NFS4ERR_SERVERFAULT;
6754 		return;
6755 	}
6756 
6757 	state = rfs4_findstate_by_owner_file(oo, file, &create);
6758 	if (state == NULL) {
6759 		NFS4_DEBUG(rfs4_debug,
6760 		    (CE_NOTE, "rfs4_do_opendelprev: can't find state"));
6761 		resp->status = NFS4ERR_SERVERFAULT;
6762 		rfs4_file_rele_withunlock(file);
6763 		return;
6764 	}
6765 
6766 	rfs4_dbe_lock(state->dbe);
6767 	rfs4_dbe_lock(file->dbe);
6768 	if (args->share_access != state->share_access ||
6769 	    args->share_deny != state->share_deny ||
6770 	    state->finfo->dinfo->dtype == OPEN_DELEGATE_NONE) {
6771 		NFS4_DEBUG(rfs4_debug,
6772 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
6773 		rfs4_dbe_unlock(file->dbe);
6774 		rfs4_dbe_unlock(state->dbe);
6775 		rfs4_file_rele(file);
6776 		rfs4_state_rele(state);
6777 		resp->status = NFS4ERR_SERVERFAULT;
6778 		return;
6779 	}
6780 	rfs4_dbe_unlock(file->dbe);
6781 	rfs4_dbe_unlock(state->dbe);
6782 
6783 	dsp = rfs4_finddeleg(state, &dcreate);
6784 	if (dsp == NULL) {
6785 		rfs4_state_rele(state);
6786 		rfs4_file_rele(file);
6787 		resp->status = NFS4ERR_SERVERFAULT;
6788 		return;
6789 	}
6790 
6791 	next_stateid(&state->stateid);
6792 
6793 	resp->stateid = state->stateid.stateid;
6794 
6795 	resp->delegation.delegation_type = dsp->dtype;
6796 
6797 	if (dsp->dtype == OPEN_DELEGATE_READ) {
6798 		open_read_delegation4 *rv =
6799 		    &resp->delegation.open_delegation4_u.read;
6800 
6801 		rv->stateid = dsp->delegid.stateid;
6802 		rv->recall = FALSE; /* no policy in place to set to TRUE */
6803 		ace = &rv->permissions;
6804 	} else {
6805 		open_write_delegation4 *rv =
6806 		    &resp->delegation.open_delegation4_u.write;
6807 
6808 		rv->stateid = dsp->delegid.stateid;
6809 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
6810 		ace = &rv->permissions;
6811 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
6812 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
6813 	}
6814 
6815 	/* XXX For now */
6816 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
6817 	ace->flag = 0;
6818 	ace->access_mask = 0;
6819 	ace->who.utf8string_len = 0;
6820 	ace->who.utf8string_val = 0;
6821 
6822 	rfs4_deleg_state_rele(dsp);
6823 	rfs4_state_rele(state);
6824 	rfs4_file_rele(file);
6825 }
6826 
6827 typedef enum {
6828 	NFS4_CHKSEQ_OKAY = 0,
6829 	NFS4_CHKSEQ_REPLAY = 1,
6830 	NFS4_CHKSEQ_BAD = 2
6831 } rfs4_chkseq_t;
6832 
6833 /*
6834  * Generic function for sequence number checks.
6835  */
6836 static rfs4_chkseq_t
6837 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
6838 		seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
6839 {
6840 	/* Same sequence ids and matching operations? */
6841 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
6842 		if (copyres == TRUE) {
6843 			rfs4_free_reply(resop);
6844 			rfs4_copy_reply(resop, lastop);
6845 		}
6846 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6847 		    "Replayed SEQID %d\n", seqid));
6848 		return (NFS4_CHKSEQ_REPLAY);
6849 	}
6850 
6851 	/* If the incoming sequence is not the next expected then it is bad */
6852 	if (rqst_seq != seqid + 1) {
6853 		if (rqst_seq == seqid) {
6854 			NFS4_DEBUG(rfs4_debug,
6855 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
6856 			    "but last op was %d current op is %d\n",
6857 			    lastop->resop, resop->resop));
6858 			return (NFS4_CHKSEQ_BAD);
6859 		}
6860 		NFS4_DEBUG(rfs4_debug,
6861 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
6862 		    rqst_seq, seqid));
6863 		return (NFS4_CHKSEQ_BAD);
6864 	}
6865 
6866 	/* Everything okay -- next expected */
6867 	return (NFS4_CHKSEQ_OKAY);
6868 }
6869 
6870 
6871 static rfs4_chkseq_t
6872 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
6873 {
6874 	rfs4_chkseq_t rc;
6875 
6876 	rfs4_dbe_lock(op->dbe);
6877 	rc = rfs4_check_seqid(op->open_seqid, op->reply, seqid, resop, TRUE);
6878 	rfs4_dbe_unlock(op->dbe);
6879 
6880 	if (rc == NFS4_CHKSEQ_OKAY)
6881 		rfs4_update_lease(op->client);
6882 
6883 	return (rc);
6884 }
6885 
6886 static rfs4_chkseq_t
6887 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op,
6888 	nfs_resop4 *resop)
6889 {
6890 	rfs4_chkseq_t rc;
6891 
6892 	rfs4_dbe_lock(op->dbe);
6893 	rc = rfs4_check_seqid(op->open_seqid, op->reply,
6894 	    olo_seqid, resop, FALSE);
6895 	rfs4_dbe_unlock(op->dbe);
6896 
6897 	return (rc);
6898 }
6899 
6900 static rfs4_chkseq_t
6901 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lp, nfs_resop4 *resop)
6902 {
6903 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
6904 
6905 	rfs4_dbe_lock(lp->dbe);
6906 	if (!lp->skip_seqid_check)
6907 		rc = rfs4_check_seqid(lp->seqid, lp->reply, seqid, resop, TRUE);
6908 	rfs4_dbe_unlock(lp->dbe);
6909 
6910 	return (rc);
6911 }
6912 
6913 static void
6914 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
6915 	    struct svc_req *req, struct compound_state *cs)
6916 {
6917 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
6918 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
6919 	open_owner4 *owner = &args->owner;
6920 	open_claim_type4 claim = args->claim;
6921 	rfs4_client_t *cp;
6922 	rfs4_openowner_t *oo;
6923 	bool_t create;
6924 	bool_t replay = FALSE;
6925 	int can_reclaim;
6926 
6927 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
6928 	    OPEN4args *, args);
6929 
6930 	if (cs->vp == NULL) {
6931 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
6932 		goto end;
6933 	}
6934 
6935 	/*
6936 	 * Need to check clientid and lease expiration first based on
6937 	 * error ordering and incrementing sequence id.
6938 	 */
6939 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
6940 	if (cp == NULL) {
6941 		*cs->statusp = resp->status =
6942 		    rfs4_check_clientid(&owner->clientid, 0);
6943 		goto end;
6944 	}
6945 
6946 	if (rfs4_lease_expired(cp)) {
6947 		rfs4_client_close(cp);
6948 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
6949 		goto end;
6950 	}
6951 	can_reclaim = cp->can_reclaim;
6952 
6953 	/*
6954 	 * Find the open_owner for use from this point forward.  Take
6955 	 * care in updating the sequence id based on the type of error
6956 	 * being returned.
6957 	 */
6958 retry:
6959 	create = TRUE;
6960 	oo = rfs4_findopenowner(owner, &create, args->seqid);
6961 	if (oo == NULL) {
6962 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
6963 		rfs4_client_rele(cp);
6964 		goto end;
6965 	}
6966 
6967 	/* Hold off access to the sequence space while the open is done */
6968 	rfs4_sw_enter(&oo->oo_sw);
6969 
6970 	/*
6971 	 * If the open_owner existed before at the server, then check
6972 	 * the sequence id.
6973 	 */
6974 	if (!create && !oo->postpone_confirm) {
6975 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
6976 		case NFS4_CHKSEQ_BAD:
6977 			if ((args->seqid > oo->open_seqid) &&
6978 			    oo->need_confirm) {
6979 				rfs4_free_opens(oo, TRUE, FALSE);
6980 				rfs4_sw_exit(&oo->oo_sw);
6981 				rfs4_openowner_rele(oo);
6982 				goto retry;
6983 			}
6984 			resp->status = NFS4ERR_BAD_SEQID;
6985 			goto out;
6986 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
6987 			replay = TRUE;
6988 			goto out;
6989 		default:
6990 			break;
6991 		}
6992 
6993 		/*
6994 		 * Sequence was ok and open owner exists
6995 		 * check to see if we have yet to see an
6996 		 * open_confirm.
6997 		 */
6998 		if (oo->need_confirm) {
6999 			rfs4_free_opens(oo, TRUE, FALSE);
7000 			rfs4_sw_exit(&oo->oo_sw);
7001 			rfs4_openowner_rele(oo);
7002 			goto retry;
7003 		}
7004 	}
7005 	/* Grace only applies to regular-type OPENs */
7006 	if (rfs4_clnt_in_grace(cp) &&
7007 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7008 		*cs->statusp = resp->status = NFS4ERR_GRACE;
7009 		goto out;
7010 	}
7011 
7012 	/*
7013 	 * If previous state at the server existed then can_reclaim
7014 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
7015 	 * client.
7016 	 */
7017 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7018 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7019 		goto out;
7020 	}
7021 
7022 
7023 	/*
7024 	 * Reject the open if the client has missed the grace period
7025 	 */
7026 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7027 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7028 		goto out;
7029 	}
7030 
7031 	/* Couple of up-front bookkeeping items */
7032 	if (oo->need_confirm) {
7033 		/*
7034 		 * If this is a reclaim OPEN then we should not ask
7035 		 * for a confirmation of the open_owner per the
7036 		 * protocol specification.
7037 		 */
7038 		if (claim == CLAIM_PREVIOUS)
7039 			oo->need_confirm = FALSE;
7040 		else
7041 			resp->rflags |= OPEN4_RESULT_CONFIRM;
7042 	}
7043 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7044 
7045 	/*
7046 	 * If there is an unshared filesystem mounted on this vnode,
7047 	 * do not allow to open/create in this directory.
7048 	 */
7049 	if (vn_ismntpt(cs->vp)) {
7050 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
7051 		goto out;
7052 	}
7053 
7054 	/*
7055 	 * access must READ, WRITE, or BOTH.  No access is invalid.
7056 	 * deny can be READ, WRITE, BOTH, or NONE.
7057 	 * bits not defined for access/deny are invalid.
7058 	 */
7059 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7060 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7061 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7062 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7063 		goto out;
7064 	}
7065 
7066 
7067 	/*
7068 	 * make sure attrset is zero before response is built.
7069 	 */
7070 	resp->attrset = 0;
7071 
7072 	switch (claim) {
7073 	case CLAIM_NULL:
7074 		rfs4_do_opennull(cs, req, args, oo, resp);
7075 		break;
7076 	case CLAIM_PREVIOUS:
7077 		rfs4_do_openprev(cs, req, args, oo, resp);
7078 		break;
7079 	case CLAIM_DELEGATE_CUR:
7080 		rfs4_do_opendelcur(cs, req, args, oo, resp);
7081 		break;
7082 	case CLAIM_DELEGATE_PREV:
7083 		rfs4_do_opendelprev(cs, req, args, oo, resp);
7084 		break;
7085 	default:
7086 		resp->status = NFS4ERR_INVAL;
7087 		break;
7088 	}
7089 
7090 out:
7091 	rfs4_client_rele(cp);
7092 
7093 	/* Catch sequence id handling here to make it a little easier */
7094 	switch (resp->status) {
7095 	case NFS4ERR_BADXDR:
7096 	case NFS4ERR_BAD_SEQID:
7097 	case NFS4ERR_BAD_STATEID:
7098 	case NFS4ERR_NOFILEHANDLE:
7099 	case NFS4ERR_RESOURCE:
7100 	case NFS4ERR_STALE_CLIENTID:
7101 	case NFS4ERR_STALE_STATEID:
7102 		/*
7103 		 * The protocol states that if any of these errors are
7104 		 * being returned, the sequence id should not be
7105 		 * incremented.  Any other return requires an
7106 		 * increment.
7107 		 */
7108 		break;
7109 	default:
7110 		/* Always update the lease in this case */
7111 		rfs4_update_lease(oo->client);
7112 
7113 		/* Regular response - copy the result */
7114 		if (!replay)
7115 			rfs4_update_open_resp(oo, resop, &cs->fh);
7116 
7117 		/*
7118 		 * REPLAY case: Only if the previous response was OK
7119 		 * do we copy the filehandle.  If not OK, no
7120 		 * filehandle to copy.
7121 		 */
7122 		if (replay == TRUE &&
7123 		    resp->status == NFS4_OK &&
7124 		    oo->reply_fh.nfs_fh4_val) {
7125 			/*
7126 			 * If this is a replay, we must restore the
7127 			 * current filehandle/vp to that of what was
7128 			 * returned originally.  Try our best to do
7129 			 * it.
7130 			 */
7131 			nfs_fh4_fmt_t *fh_fmtp =
7132 			    (nfs_fh4_fmt_t *)oo->reply_fh.nfs_fh4_val;
7133 
7134 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7135 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7136 
7137 			if (cs->exi == NULL) {
7138 				resp->status = NFS4ERR_STALE;
7139 				goto finish;
7140 			}
7141 
7142 			VN_RELE(cs->vp);
7143 
7144 			cs->vp = nfs4_fhtovp(&oo->reply_fh, cs->exi,
7145 			    &resp->status);
7146 
7147 			if (cs->vp == NULL)
7148 				goto finish;
7149 
7150 			nfs_fh4_copy(&oo->reply_fh, &cs->fh);
7151 		}
7152 
7153 		/*
7154 		 * If this was a replay, no need to update the
7155 		 * sequence id. If the open_owner was not created on
7156 		 * this pass, then update.  The first use of an
7157 		 * open_owner will not bump the sequence id.
7158 		 */
7159 		if (replay == FALSE && !create)
7160 			rfs4_update_open_sequence(oo);
7161 		/*
7162 		 * If the client is receiving an error and the
7163 		 * open_owner needs to be confirmed, there is no way
7164 		 * to notify the client of this fact ignoring the fact
7165 		 * that the server has no method of returning a
7166 		 * stateid to confirm.  Therefore, the server needs to
7167 		 * mark this open_owner in a way as to avoid the
7168 		 * sequence id checking the next time the client uses
7169 		 * this open_owner.
7170 		 */
7171 		if (resp->status != NFS4_OK && oo->need_confirm)
7172 			oo->postpone_confirm = TRUE;
7173 		/*
7174 		 * If OK response then clear the postpone flag and
7175 		 * reset the sequence id to keep in sync with the
7176 		 * client.
7177 		 */
7178 		if (resp->status == NFS4_OK && oo->postpone_confirm) {
7179 			oo->postpone_confirm = FALSE;
7180 			oo->open_seqid = args->seqid;
7181 		}
7182 		break;
7183 	}
7184 
7185 finish:
7186 	*cs->statusp = resp->status;
7187 
7188 	rfs4_sw_exit(&oo->oo_sw);
7189 	rfs4_openowner_rele(oo);
7190 
7191 end:
7192 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7193 	    OPEN4res *, resp);
7194 }
7195 
7196 /*ARGSUSED*/
7197 void
7198 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7199 		    struct svc_req *req, struct compound_state *cs)
7200 {
7201 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7202 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7203 	rfs4_state_t *sp;
7204 	nfsstat4 status;
7205 
7206 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7207 	    OPEN_CONFIRM4args *, args);
7208 
7209 	if (cs->vp == NULL) {
7210 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7211 		goto out;
7212 	}
7213 
7214 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7215 	if (status != NFS4_OK) {
7216 		*cs->statusp = resp->status = status;
7217 		goto out;
7218 	}
7219 
7220 	/* Ensure specified filehandle matches */
7221 	if (cs->vp != sp->finfo->vp) {
7222 		rfs4_state_rele(sp);
7223 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7224 		goto out;
7225 	}
7226 
7227 	/* hold off other access to open_owner while we tinker */
7228 	rfs4_sw_enter(&sp->owner->oo_sw);
7229 
7230 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7231 	case NFS4_CHECK_STATEID_OKAY:
7232 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7233 		    resop) != 0) {
7234 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7235 			break;
7236 		}
7237 		/*
7238 		 * If it is the appropriate stateid and determined to
7239 		 * be "OKAY" then this means that the stateid does not
7240 		 * need to be confirmed and the client is in error for
7241 		 * sending an OPEN_CONFIRM.
7242 		 */
7243 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7244 		break;
7245 	case NFS4_CHECK_STATEID_OLD:
7246 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7247 		break;
7248 	case NFS4_CHECK_STATEID_BAD:
7249 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7250 		break;
7251 	case NFS4_CHECK_STATEID_EXPIRED:
7252 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7253 		break;
7254 	case NFS4_CHECK_STATEID_CLOSED:
7255 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7256 		break;
7257 	case NFS4_CHECK_STATEID_REPLAY:
7258 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7259 		case NFS4_CHKSEQ_OKAY:
7260 			/*
7261 			 * This is replayed stateid; if seqid matches
7262 			 * next expected, then client is using wrong seqid.
7263 			 */
7264 			/* fall through */
7265 		case NFS4_CHKSEQ_BAD:
7266 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7267 			break;
7268 		case NFS4_CHKSEQ_REPLAY:
7269 			/*
7270 			 * Note this case is the duplicate case so
7271 			 * resp->status is already set.
7272 			 */
7273 			*cs->statusp = resp->status;
7274 			rfs4_update_lease(sp->owner->client);
7275 			break;
7276 		}
7277 		break;
7278 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7279 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7280 		    resop) != NFS4_CHKSEQ_OKAY) {
7281 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7282 			break;
7283 		}
7284 		*cs->statusp = resp->status = NFS4_OK;
7285 
7286 		next_stateid(&sp->stateid);
7287 		resp->open_stateid = sp->stateid.stateid;
7288 		sp->owner->need_confirm = FALSE;
7289 		rfs4_update_lease(sp->owner->client);
7290 		rfs4_update_open_sequence(sp->owner);
7291 		rfs4_update_open_resp(sp->owner, resop, NULL);
7292 		break;
7293 	default:
7294 		ASSERT(FALSE);
7295 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7296 		break;
7297 	}
7298 	rfs4_sw_exit(&sp->owner->oo_sw);
7299 	rfs4_state_rele(sp);
7300 
7301 out:
7302 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7303 	    OPEN_CONFIRM4res *, resp);
7304 }
7305 
7306 /*ARGSUSED*/
7307 void
7308 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7309 		    struct svc_req *req, struct compound_state *cs)
7310 {
7311 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7312 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7313 	uint32_t access = args->share_access;
7314 	uint32_t deny = args->share_deny;
7315 	nfsstat4 status;
7316 	rfs4_state_t *sp;
7317 	rfs4_file_t *fp;
7318 	int fflags = 0;
7319 
7320 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7321 	    OPEN_DOWNGRADE4args *, args);
7322 
7323 	if (cs->vp == NULL) {
7324 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7325 		goto out;
7326 	}
7327 
7328 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7329 	if (status != NFS4_OK) {
7330 		*cs->statusp = resp->status = status;
7331 		goto out;
7332 	}
7333 
7334 	/* Ensure specified filehandle matches */
7335 	if (cs->vp != sp->finfo->vp) {
7336 		rfs4_state_rele(sp);
7337 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7338 		goto out;
7339 	}
7340 
7341 	/* hold off other access to open_owner while we tinker */
7342 	rfs4_sw_enter(&sp->owner->oo_sw);
7343 
7344 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7345 	case NFS4_CHECK_STATEID_OKAY:
7346 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7347 		    resop) != NFS4_CHKSEQ_OKAY) {
7348 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7349 			goto end;
7350 		}
7351 		break;
7352 	case NFS4_CHECK_STATEID_OLD:
7353 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7354 		goto end;
7355 	case NFS4_CHECK_STATEID_BAD:
7356 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7357 		goto end;
7358 	case NFS4_CHECK_STATEID_EXPIRED:
7359 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7360 		goto end;
7361 	case NFS4_CHECK_STATEID_CLOSED:
7362 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7363 		goto end;
7364 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7365 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7366 		goto end;
7367 	case NFS4_CHECK_STATEID_REPLAY:
7368 		/* Check the sequence id for the open owner */
7369 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7370 		case NFS4_CHKSEQ_OKAY:
7371 			/*
7372 			 * This is replayed stateid; if seqid matches
7373 			 * next expected, then client is using wrong seqid.
7374 			 */
7375 			/* fall through */
7376 		case NFS4_CHKSEQ_BAD:
7377 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7378 			goto end;
7379 		case NFS4_CHKSEQ_REPLAY:
7380 			/*
7381 			 * Note this case is the duplicate case so
7382 			 * resp->status is already set.
7383 			 */
7384 			*cs->statusp = resp->status;
7385 			rfs4_update_lease(sp->owner->client);
7386 			goto end;
7387 		}
7388 		break;
7389 	default:
7390 		ASSERT(FALSE);
7391 		break;
7392 	}
7393 
7394 	rfs4_dbe_lock(sp->dbe);
7395 	/*
7396 	 * Check that the new access modes and deny modes are valid.
7397 	 * Check that no invalid bits are set.
7398 	 */
7399 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7400 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7401 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7402 		rfs4_update_open_sequence(sp->owner);
7403 		rfs4_dbe_unlock(sp->dbe);
7404 		goto end;
7405 	}
7406 
7407 	/*
7408 	 * The new modes must be a subset of the current modes and
7409 	 * the access must specify at least one mode. To test that
7410 	 * the new mode is a subset of the current modes we bitwise
7411 	 * AND them together and check that the result equals the new
7412 	 * mode. For example:
7413 	 * New mode, access == R and current mode, sp->share_access  == RW
7414 	 * access & sp->share_access == R == access, so the new access mode
7415 	 * is valid. Consider access == RW, sp->share_access = R
7416 	 * access & sp->share_access == R != access, so the new access mode
7417 	 * is invalid.
7418 	 */
7419 	if ((access & sp->share_access) != access ||
7420 	    (deny & sp->share_deny) != deny ||
7421 	    (access &
7422 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7423 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7424 		rfs4_update_open_sequence(sp->owner);
7425 		rfs4_dbe_unlock(sp->dbe);
7426 		goto end;
7427 	}
7428 
7429 	/*
7430 	 * Release any share locks associated with this stateID.
7431 	 * Strictly speaking, this violates the spec because the
7432 	 * spec effectively requires that open downgrade be atomic.
7433 	 * At present, fs_shrlock does not have this capability.
7434 	 */
7435 	rfs4_dbe_unlock(sp->dbe);
7436 	rfs4_unshare(sp);
7437 	rfs4_dbe_lock(sp->dbe);
7438 
7439 	fp = sp->finfo;
7440 	rfs4_dbe_lock(fp->dbe);
7441 
7442 	/*
7443 	 * If the current mode has deny read and the new mode
7444 	 * does not, decrement the number of deny read mode bits
7445 	 * and if it goes to zero turn off the deny read bit
7446 	 * on the file.
7447 	 */
7448 	if ((sp->share_deny & OPEN4_SHARE_DENY_READ) &&
7449 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7450 		fp->deny_read--;
7451 		if (fp->deny_read == 0)
7452 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
7453 	}
7454 
7455 	/*
7456 	 * If the current mode has deny write and the new mode
7457 	 * does not, decrement the number of deny write mode bits
7458 	 * and if it goes to zero turn off the deny write bit
7459 	 * on the file.
7460 	 */
7461 	if ((sp->share_deny & OPEN4_SHARE_DENY_WRITE) &&
7462 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7463 		fp->deny_write--;
7464 		if (fp->deny_write == 0)
7465 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7466 	}
7467 
7468 	/*
7469 	 * If the current mode has access read and the new mode
7470 	 * does not, decrement the number of access read mode bits
7471 	 * and if it goes to zero turn off the access read bit
7472 	 * on the file.  set fflags to FREAD for the call to
7473 	 * vn_open_downgrade().
7474 	 */
7475 	if ((sp->share_access & OPEN4_SHARE_ACCESS_READ) &&
7476 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7477 		fp->access_read--;
7478 		if (fp->access_read == 0)
7479 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
7480 		fflags |= FREAD;
7481 	}
7482 
7483 	/*
7484 	 * If the current mode has access write and the new mode
7485 	 * does not, decrement the number of access write mode bits
7486 	 * and if it goes to zero turn off the access write bit
7487 	 * on the file.  set fflags to FWRITE for the call to
7488 	 * vn_open_downgrade().
7489 	 */
7490 	if ((sp->share_access & OPEN4_SHARE_ACCESS_WRITE) &&
7491 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7492 		fp->access_write--;
7493 		if (fp->access_write == 0)
7494 			fp->share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7495 		fflags |= FWRITE;
7496 	}
7497 
7498 	/* Set the new access and deny modes */
7499 	sp->share_access = access;
7500 	sp->share_deny = deny;
7501 	/* Check that the file is still accessible */
7502 	ASSERT(fp->share_access);
7503 
7504 	rfs4_dbe_unlock(fp->dbe);
7505 
7506 	rfs4_dbe_unlock(sp->dbe);
7507 
7508 	if ((status = rfs4_share(sp)) != NFS4_OK) {
7509 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7510 		rfs4_update_open_sequence(sp->owner);
7511 		goto end;
7512 	}
7513 
7514 	/*
7515 	 * we successfully downgraded the share lock, now we need to downgrade
7516 	 * the open.  it is possible that the downgrade was only for a deny
7517 	 * mode and we have nothing else to do.
7518 	 */
7519 	if ((fflags & (FREAD|FWRITE)) != 0)
7520 		vn_open_downgrade(cs->vp, fflags);
7521 
7522 	rfs4_dbe_lock(sp->dbe);
7523 
7524 	/* Update the stateid */
7525 	next_stateid(&sp->stateid);
7526 	resp->open_stateid = sp->stateid.stateid;
7527 
7528 	rfs4_dbe_unlock(sp->dbe);
7529 
7530 	*cs->statusp = resp->status = NFS4_OK;
7531 	/* Update the lease */
7532 	rfs4_update_lease(sp->owner->client);
7533 	/* And the sequence */
7534 	rfs4_update_open_sequence(sp->owner);
7535 	rfs4_update_open_resp(sp->owner, resop, NULL);
7536 
7537 end:
7538 	rfs4_sw_exit(&sp->owner->oo_sw);
7539 	rfs4_state_rele(sp);
7540 out:
7541 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7542 	    OPEN_DOWNGRADE4res *, resp);
7543 }
7544 
7545 /*
7546  * The logic behind this function is detailed in the NFSv4 RFC in the
7547  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7548  * that section for explicit guidance to server behavior for
7549  * SETCLIENTID.
7550  */
7551 void
7552 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
7553 		    struct svc_req *req, struct compound_state *cs)
7554 {
7555 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
7556 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
7557 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
7558 	bool_t create = TRUE;
7559 	char *addr, *netid;
7560 	int len;
7561 
7562 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
7563 	    SETCLIENTID4args *, args);
7564 retry:
7565 	newcp = cp_confirmed = cp_unconfirmed = NULL;
7566 
7567 	/*
7568 	 * In search of an EXISTING client matching the incoming
7569 	 * request to establish a new client identifier at the server
7570 	 */
7571 	create = TRUE;
7572 	cp = rfs4_findclient(&args->client, &create, NULL);
7573 
7574 	/* Should never happen */
7575 	ASSERT(cp != NULL);
7576 
7577 	if (cp == NULL) {
7578 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7579 		goto out;
7580 	}
7581 
7582 	/*
7583 	 * Easiest case. Client identifier is newly created and is
7584 	 * unconfirmed.  Also note that for this case, no other
7585 	 * entries exist for the client identifier.  Nothing else to
7586 	 * check.  Just setup the response and respond.
7587 	 */
7588 	if (create) {
7589 		*cs->statusp = res->status = NFS4_OK;
7590 		res->SETCLIENTID4res_u.resok4.clientid = cp->clientid;
7591 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7592 		    cp->confirm_verf;
7593 		/* Setup callback information; CB_NULL confirmation later */
7594 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
7595 
7596 		rfs4_client_rele(cp);
7597 		goto out;
7598 	}
7599 
7600 	/*
7601 	 * An existing, confirmed client may exist but it may not have
7602 	 * been active for at least one lease period.  If so, then
7603 	 * "close" the client and create a new client identifier
7604 	 */
7605 	if (rfs4_lease_expired(cp)) {
7606 		rfs4_client_close(cp);
7607 		goto retry;
7608 	}
7609 
7610 	if (cp->need_confirm == TRUE)
7611 		cp_unconfirmed = cp;
7612 	else
7613 		cp_confirmed = cp;
7614 
7615 	cp = NULL;
7616 
7617 	/*
7618 	 * We have a confirmed client, now check for an
7619 	 * unconfimred entry
7620 	 */
7621 	if (cp_confirmed) {
7622 		/* If creds don't match then client identifier is inuse */
7623 		if (!creds_ok(cp_confirmed->cr_set, req, cs)) {
7624 			rfs4_cbinfo_t *cbp;
7625 			/*
7626 			 * Some one else has established this client
7627 			 * id. Try and say * who they are. We will use
7628 			 * the call back address supplied by * the
7629 			 * first client.
7630 			 */
7631 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7632 
7633 			addr = netid = NULL;
7634 
7635 			cbp = &cp_confirmed->cbinfo;
7636 			if (cbp->cb_callback.cb_location.r_addr &&
7637 			    cbp->cb_callback.cb_location.r_netid) {
7638 				cb_client4 *cbcp = &cbp->cb_callback;
7639 
7640 				len = strlen(cbcp->cb_location.r_addr)+1;
7641 				addr = kmem_alloc(len, KM_SLEEP);
7642 				bcopy(cbcp->cb_location.r_addr, addr, len);
7643 				len = strlen(cbcp->cb_location.r_netid)+1;
7644 				netid = kmem_alloc(len, KM_SLEEP);
7645 				bcopy(cbcp->cb_location.r_netid, netid, len);
7646 			}
7647 
7648 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
7649 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
7650 
7651 			rfs4_client_rele(cp_confirmed);
7652 		}
7653 
7654 		/*
7655 		 * Confirmed, creds match, and verifier matches; must
7656 		 * be an update of the callback info
7657 		 */
7658 		if (cp_confirmed->nfs_client.verifier ==
7659 		    args->client.verifier) {
7660 			/* Setup callback information */
7661 			rfs4_client_setcb(cp_confirmed, &args->callback,
7662 			    args->callback_ident);
7663 
7664 			/* everything okay -- move ahead */
7665 			*cs->statusp = res->status = NFS4_OK;
7666 			res->SETCLIENTID4res_u.resok4.clientid =
7667 			    cp_confirmed->clientid;
7668 
7669 			/* update the confirm_verifier and return it */
7670 			rfs4_client_scv_next(cp_confirmed);
7671 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7672 			    cp_confirmed->confirm_verf;
7673 
7674 			rfs4_client_rele(cp_confirmed);
7675 			goto out;
7676 		}
7677 
7678 		/*
7679 		 * Creds match but the verifier doesn't.  Must search
7680 		 * for an unconfirmed client that would be replaced by
7681 		 * this request.
7682 		 */
7683 		create = FALSE;
7684 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
7685 		    cp_confirmed);
7686 	}
7687 
7688 	/*
7689 	 * At this point, we have taken care of the brand new client
7690 	 * struct, INUSE case, update of an existing, and confirmed
7691 	 * client struct.
7692 	 */
7693 
7694 	/*
7695 	 * check to see if things have changed while we originally
7696 	 * picked up the client struct.  If they have, then return and
7697 	 * retry the processing of this SETCLIENTID request.
7698 	 */
7699 	if (cp_unconfirmed) {
7700 		rfs4_dbe_lock(cp_unconfirmed->dbe);
7701 		if (!cp_unconfirmed->need_confirm) {
7702 			rfs4_dbe_unlock(cp_unconfirmed->dbe);
7703 			rfs4_client_rele(cp_unconfirmed);
7704 			if (cp_confirmed)
7705 				rfs4_client_rele(cp_confirmed);
7706 			goto retry;
7707 		}
7708 		/* do away with the old unconfirmed one */
7709 		rfs4_dbe_invalidate(cp_unconfirmed->dbe);
7710 		rfs4_dbe_unlock(cp_unconfirmed->dbe);
7711 		rfs4_client_rele(cp_unconfirmed);
7712 		cp_unconfirmed = NULL;
7713 	}
7714 
7715 	/*
7716 	 * This search will temporarily hide the confirmed client
7717 	 * struct while a new client struct is created as the
7718 	 * unconfirmed one.
7719 	 */
7720 	create = TRUE;
7721 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
7722 
7723 	ASSERT(newcp != NULL);
7724 
7725 	if (newcp == NULL) {
7726 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7727 		rfs4_client_rele(cp_confirmed);
7728 		goto out;
7729 	}
7730 
7731 	/*
7732 	 * If one was not created, then a similar request must be in
7733 	 * process so release and start over with this one
7734 	 */
7735 	if (create != TRUE) {
7736 		rfs4_client_rele(newcp);
7737 		if (cp_confirmed)
7738 			rfs4_client_rele(cp_confirmed);
7739 		goto retry;
7740 	}
7741 
7742 	*cs->statusp = res->status = NFS4_OK;
7743 	res->SETCLIENTID4res_u.resok4.clientid = newcp->clientid;
7744 	res->SETCLIENTID4res_u.resok4.setclientid_confirm = newcp->confirm_verf;
7745 	/* Setup callback information; CB_NULL confirmation later */
7746 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
7747 
7748 	newcp->cp_confirmed = cp_confirmed;
7749 
7750 	rfs4_client_rele(newcp);
7751 
7752 out:
7753 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
7754 	    SETCLIENTID4res *, res);
7755 }
7756 
7757 /*ARGSUSED*/
7758 void
7759 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7760 			    struct svc_req *req, struct compound_state *cs)
7761 {
7762 	SETCLIENTID_CONFIRM4args *args =
7763 	    &argop->nfs_argop4_u.opsetclientid_confirm;
7764 	SETCLIENTID_CONFIRM4res *res =
7765 	    &resop->nfs_resop4_u.opsetclientid_confirm;
7766 	rfs4_client_t *cp, *cptoclose = NULL;
7767 
7768 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
7769 	    struct compound_state *, cs,
7770 	    SETCLIENTID_CONFIRM4args *, args);
7771 
7772 	*cs->statusp = res->status = NFS4_OK;
7773 
7774 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
7775 
7776 	if (cp == NULL) {
7777 		*cs->statusp = res->status =
7778 		    rfs4_check_clientid(&args->clientid, 1);
7779 		goto out;
7780 	}
7781 
7782 	if (!creds_ok(cp, req, cs)) {
7783 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7784 		rfs4_client_rele(cp);
7785 		goto out;
7786 	}
7787 
7788 	/* If the verifier doesn't match, the record doesn't match */
7789 	if (cp->confirm_verf != args->setclientid_confirm) {
7790 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
7791 		rfs4_client_rele(cp);
7792 		goto out;
7793 	}
7794 
7795 	rfs4_dbe_lock(cp->dbe);
7796 	cp->need_confirm = FALSE;
7797 	if (cp->cp_confirmed) {
7798 		cptoclose = cp->cp_confirmed;
7799 		cptoclose->ss_remove = 1;
7800 		cp->cp_confirmed = NULL;
7801 	}
7802 
7803 	/*
7804 	 * Update the client's associated server instance, if it's changed
7805 	 * since the client was created.
7806 	 */
7807 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
7808 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
7809 
7810 	/*
7811 	 * Record clientid in stable storage.
7812 	 * Must be done after server instance has been assigned.
7813 	 */
7814 	rfs4_ss_clid(cp, req);
7815 
7816 	rfs4_dbe_unlock(cp->dbe);
7817 
7818 	if (cptoclose)
7819 		/* don't need to rele, client_close does it */
7820 		rfs4_client_close(cptoclose);
7821 
7822 	/* If needed, initiate CB_NULL call for callback path */
7823 	rfs4_deleg_cb_check(cp);
7824 	rfs4_update_lease(cp);
7825 
7826 	/*
7827 	 * Check to see if client can perform reclaims
7828 	 */
7829 	rfs4_ss_chkclid(cp);
7830 
7831 	rfs4_client_rele(cp);
7832 
7833 out:
7834 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
7835 	    struct compound_state *, cs,
7836 	    SETCLIENTID_CONFIRM4 *, res);
7837 }
7838 
7839 
7840 /*ARGSUSED*/
7841 void
7842 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
7843 	    struct svc_req *req, struct compound_state *cs)
7844 {
7845 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
7846 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
7847 	rfs4_state_t *sp;
7848 	nfsstat4 status;
7849 
7850 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
7851 	    CLOSE4args *, args);
7852 
7853 	if (cs->vp == NULL) {
7854 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7855 		goto out;
7856 	}
7857 
7858 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
7859 	if (status != NFS4_OK) {
7860 		*cs->statusp = resp->status = status;
7861 		goto out;
7862 	}
7863 
7864 	/* Ensure specified filehandle matches */
7865 	if (cs->vp != sp->finfo->vp) {
7866 		rfs4_state_rele(sp);
7867 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7868 		goto out;
7869 	}
7870 
7871 	/* hold off other access to open_owner while we tinker */
7872 	rfs4_sw_enter(&sp->owner->oo_sw);
7873 
7874 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7875 	case NFS4_CHECK_STATEID_OKAY:
7876 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7877 		    resop) != NFS4_CHKSEQ_OKAY) {
7878 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7879 			goto end;
7880 		}
7881 		break;
7882 	case NFS4_CHECK_STATEID_OLD:
7883 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7884 		goto end;
7885 	case NFS4_CHECK_STATEID_BAD:
7886 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7887 		goto end;
7888 	case NFS4_CHECK_STATEID_EXPIRED:
7889 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7890 		goto end;
7891 	case NFS4_CHECK_STATEID_CLOSED:
7892 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7893 		goto end;
7894 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7895 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7896 		goto end;
7897 	case NFS4_CHECK_STATEID_REPLAY:
7898 		/* Check the sequence id for the open owner */
7899 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7900 		case NFS4_CHKSEQ_OKAY:
7901 			/*
7902 			 * This is replayed stateid; if seqid matches
7903 			 * next expected, then client is using wrong seqid.
7904 			 */
7905 			/* FALL THROUGH */
7906 		case NFS4_CHKSEQ_BAD:
7907 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7908 			goto end;
7909 		case NFS4_CHKSEQ_REPLAY:
7910 			/*
7911 			 * Note this case is the duplicate case so
7912 			 * resp->status is already set.
7913 			 */
7914 			*cs->statusp = resp->status;
7915 			rfs4_update_lease(sp->owner->client);
7916 			goto end;
7917 		}
7918 		break;
7919 	default:
7920 		ASSERT(FALSE);
7921 		break;
7922 	}
7923 
7924 	rfs4_dbe_lock(sp->dbe);
7925 
7926 	/* Update the stateid. */
7927 	next_stateid(&sp->stateid);
7928 	resp->open_stateid = sp->stateid.stateid;
7929 
7930 	rfs4_dbe_unlock(sp->dbe);
7931 
7932 	rfs4_update_lease(sp->owner->client);
7933 	rfs4_update_open_sequence(sp->owner);
7934 	rfs4_update_open_resp(sp->owner, resop, NULL);
7935 
7936 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
7937 
7938 	*cs->statusp = resp->status = status;
7939 
7940 end:
7941 	rfs4_sw_exit(&sp->owner->oo_sw);
7942 	rfs4_state_rele(sp);
7943 out:
7944 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
7945 	    CLOSE4res *, resp);
7946 }
7947 
7948 /*
7949  * Manage the counts on the file struct and close all file locks
7950  */
7951 /*ARGSUSED*/
7952 void
7953 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
7954 	bool_t close_of_client)
7955 {
7956 	rfs4_file_t *fp = sp->finfo;
7957 	rfs4_lo_state_t *lsp;
7958 	struct shrlock shr;
7959 	struct shr_locowner shr_loco;
7960 	int fflags, s_access, s_deny;
7961 
7962 	fflags = s_access = s_deny = 0;
7963 	/*
7964 	 * Decrement the count for each access and deny bit that this
7965 	 * state has contributed to the file. If the file counts go to zero
7966 	 * clear the appropriate bit in the appropriate mask.
7967 	 */
7968 
7969 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
7970 		fp->access_read--;
7971 		fflags |= FREAD;
7972 		s_access |= F_RDACC;
7973 		if (fp->access_read == 0)
7974 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
7975 	}
7976 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
7977 		fp->access_write--;
7978 		fflags |= FWRITE;
7979 		s_access |= F_WRACC;
7980 		if (fp->access_write == 0)
7981 			fp->share_access &= ~OPEN4_SHARE_ACCESS_WRITE;
7982 	}
7983 	if (sp->share_deny & OPEN4_SHARE_DENY_READ) {
7984 		fp->deny_read--;
7985 		s_deny |= F_RDDNY;
7986 		if (fp->deny_read == 0)
7987 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
7988 	}
7989 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) {
7990 		fp->deny_write--;
7991 		s_deny |= F_WRDNY;
7992 		if (fp->deny_write == 0)
7993 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7994 	}
7995 
7996 	/*
7997 	 * If this call is part of the larger closing down of client
7998 	 * state then it is just easier to release all locks
7999 	 * associated with this client instead of going through each
8000 	 * individual file and cleaning locks there.
8001 	 */
8002 	if (close_of_client) {
8003 		if (sp->owner->client->unlksys_completed == FALSE &&
8004 		    sp->lockownerlist.next->lsp != NULL &&
8005 		    sp->owner->client->sysidt != LM_NOSYSID) {
8006 			/* Is the PxFS kernel module loaded? */
8007 			if (lm_remove_file_locks != NULL) {
8008 				int new_sysid;
8009 
8010 				/* Encode the cluster nodeid in new sysid */
8011 				new_sysid = sp->owner->client->sysidt;
8012 				lm_set_nlmid_flk(&new_sysid);
8013 
8014 				/*
8015 				 * This PxFS routine removes file locks for a
8016 				 * client over all nodes of a cluster.
8017 				 */
8018 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8019 				    "lm_remove_file_locks(sysid=0x%x)\n",
8020 				    new_sysid));
8021 				(*lm_remove_file_locks)(new_sysid);
8022 			} else {
8023 				struct flock64 flk;
8024 
8025 				/* Release all locks for this client */
8026 				flk.l_type = F_UNLKSYS;
8027 				flk.l_whence = 0;
8028 				flk.l_start = 0;
8029 				flk.l_len = 0;
8030 				flk.l_sysid = sp->owner->client->sysidt;
8031 				flk.l_pid = 0;
8032 				(void) VOP_FRLOCK(sp->finfo->vp, F_SETLK, &flk,
8033 				    F_REMOTELOCK | FREAD | FWRITE,
8034 				    (u_offset_t)0, NULL, CRED(), NULL);
8035 			}
8036 
8037 			sp->owner->client->unlksys_completed = TRUE;
8038 		}
8039 	}
8040 
8041 	/*
8042 	 * Release all locks on this file by this lock owner or at
8043 	 * least mark the locks as having been released
8044 	 */
8045 	for (lsp = sp->lockownerlist.next->lsp; lsp != NULL;
8046 	    lsp = lsp->lockownerlist.next->lsp) {
8047 
8048 		lsp->locks_cleaned = TRUE;
8049 
8050 		/* Was this already taken care of above? */
8051 		if (!close_of_client &&
8052 		    sp->owner->client->sysidt != LM_NOSYSID)
8053 			(void) cleanlocks(sp->finfo->vp, lsp->locker->pid,
8054 			    lsp->locker->client->sysidt);
8055 	}
8056 
8057 	/*
8058 	 * Release any shrlocks associated with this open state ID.
8059 	 * This must be done before the rfs4_state gets marked closed.
8060 	 */
8061 	if (sp->owner->client->sysidt != LM_NOSYSID) {
8062 		shr.s_access = s_access;
8063 		shr.s_deny = s_deny;
8064 		shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
8065 		shr.s_sysid = sp->owner->client->sysidt;
8066 		shr_loco.sl_pid = shr.s_pid;
8067 		shr_loco.sl_id = shr.s_sysid;
8068 		shr.s_owner = (caddr_t)&shr_loco;
8069 		shr.s_own_len = sizeof (shr_loco);
8070 		(void) vop_shrlock(sp->finfo->vp, F_UNSHARE, &shr, fflags);
8071 	}
8072 
8073 	(void) VOP_CLOSE(fp->vp, fflags, 1, (offset_t)0, cr, NULL);
8074 }
8075 
8076 /*
8077  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8078  */
8079 static nfsstat4
8080 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8081 {
8082 	rfs4_lockowner_t *lo;
8083 	rfs4_client_t *cp;
8084 	uint32_t len;
8085 
8086 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
8087 	if (lo != NULL) {
8088 		cp = lo->client;
8089 		if (rfs4_lease_expired(cp)) {
8090 			rfs4_lockowner_rele(lo);
8091 			rfs4_dbe_hold(cp->dbe);
8092 			rfs4_client_close(cp);
8093 			return (NFS4ERR_EXPIRED);
8094 		}
8095 		dp->owner.clientid = lo->owner.clientid;
8096 		len = lo->owner.owner_len;
8097 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8098 		bcopy(lo->owner.owner_val, dp->owner.owner_val, len);
8099 		dp->owner.owner_len = len;
8100 		rfs4_lockowner_rele(lo);
8101 		goto finish;
8102 	}
8103 
8104 	/*
8105 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
8106 	 * of the client id contain the boot time for a NFS4 lock. So we
8107 	 * fabricate and identity by setting clientid to the sysid, and
8108 	 * the lock owner to the pid.
8109 	 */
8110 	dp->owner.clientid = flk->l_sysid;
8111 	len = sizeof (pid_t);
8112 	dp->owner.owner_len = len;
8113 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8114 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
8115 finish:
8116 	dp->offset = flk->l_start;
8117 	dp->length = flk->l_len;
8118 
8119 	if (flk->l_type == F_RDLCK)
8120 		dp->locktype = READ_LT;
8121 	else if (flk->l_type == F_WRLCK)
8122 		dp->locktype = WRITE_LT;
8123 	else
8124 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
8125 
8126 	return (NFS4_OK);
8127 }
8128 
8129 static int
8130 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8131 {
8132 	int error;
8133 	struct flock64 flk;
8134 	int i;
8135 	clock_t delaytime;
8136 	int cmd;
8137 
8138 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8139 retry:
8140 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8141 
8142 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8143 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8144 		error = VOP_FRLOCK(vp, cmd,
8145 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
8146 
8147 		if (error != EAGAIN && error != EACCES)
8148 			break;
8149 
8150 		if (i < rfs4_maxlock_tries - 1) {
8151 			delay(delaytime);
8152 			delaytime *= 2;
8153 		}
8154 	}
8155 
8156 	if (error == EAGAIN || error == EACCES) {
8157 		/* Get the owner of the lock */
8158 		flk = *flock;
8159 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8160 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag,
8161 		    (u_offset_t)0, NULL, cred, NULL) == 0) {
8162 			if (flk.l_type == F_UNLCK) {
8163 				/* No longer locked, retry */
8164 				goto retry;
8165 			}
8166 			*flock = flk;
8167 			LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8168 			    F_GETLK, &flk);
8169 		}
8170 	}
8171 
8172 	return (error);
8173 }
8174 
8175 /*ARGSUSED*/
8176 static nfsstat4
8177 rfs4_do_lock(rfs4_lo_state_t *lp, nfs_lock_type4 locktype,
8178 	    seqid4 seqid, offset4 offset,
8179 	    length4 length, cred_t *cred, nfs_resop4 *resop)
8180 {
8181 	nfsstat4 status;
8182 	rfs4_lockowner_t *lo = lp->locker;
8183 	rfs4_state_t *sp = lp->state;
8184 	struct flock64 flock;
8185 	int16_t ltype;
8186 	int flag;
8187 	int error;
8188 	sysid_t sysid;
8189 	LOCK4res *lres;
8190 
8191 	if (rfs4_lease_expired(lo->client)) {
8192 		return (NFS4ERR_EXPIRED);
8193 	}
8194 
8195 	if ((status = rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8196 		return (status);
8197 
8198 	/* Check for zero length. To lock to end of file use all ones for V4 */
8199 	if (length == 0)
8200 		return (NFS4ERR_INVAL);
8201 	else if (length == (length4)(~0))
8202 		length = 0;		/* Posix to end of file  */
8203 
8204 retry:
8205 	rfs4_dbe_lock(sp->dbe);
8206 
8207 
8208 	if (resop->resop != OP_LOCKU) {
8209 		switch (locktype) {
8210 		case READ_LT:
8211 		case READW_LT:
8212 			if ((sp->share_access
8213 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8214 				rfs4_dbe_unlock(sp->dbe);
8215 
8216 				return (NFS4ERR_OPENMODE);
8217 			}
8218 			ltype = F_RDLCK;
8219 			break;
8220 		case WRITE_LT:
8221 		case WRITEW_LT:
8222 			if ((sp->share_access
8223 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8224 				rfs4_dbe_unlock(sp->dbe);
8225 
8226 				return (NFS4ERR_OPENMODE);
8227 			}
8228 			ltype = F_WRLCK;
8229 			break;
8230 		}
8231 	} else
8232 		ltype = F_UNLCK;
8233 
8234 	flock.l_type = ltype;
8235 	flock.l_whence = 0;		/* SEEK_SET */
8236 	flock.l_start = offset;
8237 	flock.l_len = length;
8238 	flock.l_sysid = sysid;
8239 	flock.l_pid = lp->locker->pid;
8240 
8241 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8242 	if (flock.l_len < 0 || flock.l_start < 0) {
8243 		rfs4_dbe_unlock(sp->dbe);
8244 		return (NFS4ERR_INVAL);
8245 	}
8246 
8247 	/*
8248 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8249 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8250 	 */
8251 	flag = (int)sp->share_access | F_REMOTELOCK;
8252 
8253 	error = setlock(sp->finfo->vp, &flock, flag, cred);
8254 	if (error == 0) {
8255 		rfs4_dbe_lock(lp->dbe);
8256 		next_stateid(&lp->lockid);
8257 		rfs4_dbe_unlock(lp->dbe);
8258 	}
8259 
8260 	rfs4_dbe_unlock(sp->dbe);
8261 
8262 	/*
8263 	 * N.B. We map error values to nfsv4 errors. This is differrent
8264 	 * than puterrno4 routine.
8265 	 */
8266 	switch (error) {
8267 	case 0:
8268 		status = NFS4_OK;
8269 		break;
8270 	case EAGAIN:
8271 	case EACCES:		/* Old value */
8272 		/* Can only get here if op is OP_LOCK */
8273 		ASSERT(resop->resop == OP_LOCK);
8274 		lres = &resop->nfs_resop4_u.oplock;
8275 		status = NFS4ERR_DENIED;
8276 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8277 		    == NFS4ERR_EXPIRED)
8278 			goto retry;
8279 		break;
8280 	case ENOLCK:
8281 		status = NFS4ERR_DELAY;
8282 		break;
8283 	case EOVERFLOW:
8284 		status = NFS4ERR_INVAL;
8285 		break;
8286 	case EINVAL:
8287 		status = NFS4ERR_NOTSUPP;
8288 		break;
8289 	default:
8290 		cmn_err(CE_WARN, "rfs4_do_lock: unexpected errno (%d)", error);
8291 		status = NFS4ERR_SERVERFAULT;
8292 		break;
8293 	}
8294 
8295 	return (status);
8296 }
8297 
8298 /*ARGSUSED*/
8299 void
8300 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8301 	    struct svc_req *req, struct compound_state *cs)
8302 {
8303 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
8304 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8305 	nfsstat4 status;
8306 	stateid4 *stateid;
8307 	rfs4_lockowner_t *lo;
8308 	rfs4_client_t *cp;
8309 	rfs4_state_t *sp = NULL;
8310 	rfs4_lo_state_t *lsp = NULL;
8311 	bool_t ls_sw_held = FALSE;
8312 	bool_t create = TRUE;
8313 	bool_t lcreate = TRUE;
8314 	bool_t dup_lock = FALSE;
8315 	int rc;
8316 
8317 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8318 	    LOCK4args *, args);
8319 
8320 	if (cs->vp == NULL) {
8321 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8322 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8323 		    cs, LOCK4res *, resp);
8324 		return;
8325 	}
8326 
8327 	if (args->locker.new_lock_owner) {
8328 		/* Create a new lockowner for this instance */
8329 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8330 
8331 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8332 
8333 		stateid = &olo->open_stateid;
8334 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8335 		if (status != NFS4_OK) {
8336 			NFS4_DEBUG(rfs4_debug,
8337 			    (CE_NOTE, "Get state failed in lock %d", status));
8338 			*cs->statusp = resp->status = status;
8339 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8340 			    cs, LOCK4res *, resp);
8341 			return;
8342 		}
8343 
8344 		/* Ensure specified filehandle matches */
8345 		if (cs->vp != sp->finfo->vp) {
8346 			rfs4_state_rele(sp);
8347 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8348 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8349 			    cs, LOCK4res *, resp);
8350 			return;
8351 		}
8352 
8353 		/* hold off other access to open_owner while we tinker */
8354 		rfs4_sw_enter(&sp->owner->oo_sw);
8355 
8356 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8357 		case NFS4_CHECK_STATEID_OLD:
8358 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8359 			goto end;
8360 		case NFS4_CHECK_STATEID_BAD:
8361 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8362 			goto end;
8363 		case NFS4_CHECK_STATEID_EXPIRED:
8364 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8365 			goto end;
8366 		case NFS4_CHECK_STATEID_UNCONFIRMED:
8367 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8368 			goto end;
8369 		case NFS4_CHECK_STATEID_CLOSED:
8370 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8371 			goto end;
8372 		case NFS4_CHECK_STATEID_OKAY:
8373 		case NFS4_CHECK_STATEID_REPLAY:
8374 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8375 			    sp->owner, resop)) {
8376 			case NFS4_CHKSEQ_OKAY:
8377 				if (rc == NFS4_CHECK_STATEID_OKAY)
8378 					break;
8379 				/*
8380 				 * This is replayed stateid; if seqid
8381 				 * matches next expected, then client
8382 				 * is using wrong seqid.
8383 				 */
8384 				/* FALLTHROUGH */
8385 			case NFS4_CHKSEQ_BAD:
8386 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8387 				goto end;
8388 			case NFS4_CHKSEQ_REPLAY:
8389 				/* This is a duplicate LOCK request */
8390 				dup_lock = TRUE;
8391 
8392 				/*
8393 				 * For a duplicate we do not want to
8394 				 * create a new lockowner as it should
8395 				 * already exist.
8396 				 * Turn off the lockowner create flag.
8397 				 */
8398 				lcreate = FALSE;
8399 			}
8400 			break;
8401 		}
8402 
8403 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8404 		if (lo == NULL) {
8405 			NFS4_DEBUG(rfs4_debug,
8406 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
8407 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
8408 			goto end;
8409 		}
8410 
8411 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8412 		if (lsp == NULL) {
8413 			rfs4_update_lease(sp->owner->client);
8414 			/*
8415 			 * Only update theh open_seqid if this is not
8416 			 * a duplicate request
8417 			 */
8418 			if (dup_lock == FALSE) {
8419 				rfs4_update_open_sequence(sp->owner);
8420 			}
8421 
8422 			NFS4_DEBUG(rfs4_debug,
8423 			    (CE_NOTE, "rfs4_op_lock: no state"));
8424 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8425 			rfs4_update_open_resp(sp->owner, resop, NULL);
8426 			rfs4_lockowner_rele(lo);
8427 			goto end;
8428 		}
8429 
8430 		/*
8431 		 * This is the new_lock_owner branch and the client is
8432 		 * supposed to be associating a new lock_owner with
8433 		 * the open file at this point.  If we find that a
8434 		 * lock_owner/state association already exists and a
8435 		 * successful LOCK request was returned to the client,
8436 		 * an error is returned to the client since this is
8437 		 * not appropriate.  The client should be using the
8438 		 * existing lock_owner branch.
8439 		 */
8440 		if (dup_lock == FALSE && create == FALSE) {
8441 			if (lsp->lock_completed == TRUE) {
8442 				*cs->statusp =
8443 				    resp->status = NFS4ERR_BAD_SEQID;
8444 				rfs4_lockowner_rele(lo);
8445 				goto end;
8446 			}
8447 		}
8448 
8449 		rfs4_update_lease(sp->owner->client);
8450 
8451 		/*
8452 		 * Only update theh open_seqid if this is not
8453 		 * a duplicate request
8454 		 */
8455 		if (dup_lock == FALSE) {
8456 			rfs4_update_open_sequence(sp->owner);
8457 		}
8458 
8459 		/*
8460 		 * If this is a duplicate lock request, just copy the
8461 		 * previously saved reply and return.
8462 		 */
8463 		if (dup_lock == TRUE) {
8464 			/* verify that lock_seqid's match */
8465 			if (lsp->seqid != olo->lock_seqid) {
8466 				NFS4_DEBUG(rfs4_debug,
8467 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
8468 				    "lsp->seqid=%d old->seqid=%d",
8469 				    lsp->seqid, olo->lock_seqid));
8470 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8471 			} else {
8472 				rfs4_copy_reply(resop, lsp->reply);
8473 				/*
8474 				 * Make sure to copy the just
8475 				 * retrieved reply status into the
8476 				 * overall compound status
8477 				 */
8478 				*cs->statusp = resp->status;
8479 			}
8480 			rfs4_lockowner_rele(lo);
8481 			goto end;
8482 		}
8483 
8484 		rfs4_dbe_lock(lsp->dbe);
8485 
8486 		/* Make sure to update the lock sequence id */
8487 		lsp->seqid = olo->lock_seqid;
8488 
8489 		NFS4_DEBUG(rfs4_debug,
8490 		    (CE_NOTE, "Lock seqid established as %d", lsp->seqid));
8491 
8492 		/*
8493 		 * This is used to signify the newly created lockowner
8494 		 * stateid and its sequence number.  The checks for
8495 		 * sequence number and increment don't occur on the
8496 		 * very first lock request for a lockowner.
8497 		 */
8498 		lsp->skip_seqid_check = TRUE;
8499 
8500 		/* hold off other access to lsp while we tinker */
8501 		rfs4_sw_enter(&lsp->ls_sw);
8502 		ls_sw_held = TRUE;
8503 
8504 		rfs4_dbe_unlock(lsp->dbe);
8505 
8506 		rfs4_lockowner_rele(lo);
8507 	} else {
8508 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
8509 		/* get lsp and hold the lock on the underlying file struct */
8510 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
8511 		    != NFS4_OK) {
8512 			*cs->statusp = resp->status = status;
8513 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8514 			    cs, LOCK4res *, resp);
8515 			return;
8516 		}
8517 		create = FALSE;	/* We didn't create lsp */
8518 
8519 		/* Ensure specified filehandle matches */
8520 		if (cs->vp != lsp->state->finfo->vp) {
8521 			rfs4_lo_state_rele(lsp, TRUE);
8522 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8523 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8524 			    cs, LOCK4res *, resp);
8525 			return;
8526 		}
8527 
8528 		/* hold off other access to lsp while we tinker */
8529 		rfs4_sw_enter(&lsp->ls_sw);
8530 		ls_sw_held = TRUE;
8531 
8532 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8533 		/*
8534 		 * The stateid looks like it was okay (expected to be
8535 		 * the next one)
8536 		 */
8537 		case NFS4_CHECK_STATEID_OKAY:
8538 			/*
8539 			 * The sequence id is now checked.  Determine
8540 			 * if this is a replay or if it is in the
8541 			 * expected (next) sequence.  In the case of a
8542 			 * replay, there are two replay conditions
8543 			 * that may occur.  The first is the normal
8544 			 * condition where a LOCK is done with a
8545 			 * NFS4_OK response and the stateid is
8546 			 * updated.  That case is handled below when
8547 			 * the stateid is identified as a REPLAY.  The
8548 			 * second is the case where an error is
8549 			 * returned, like NFS4ERR_DENIED, and the
8550 			 * sequence number is updated but the stateid
8551 			 * is not updated.  This second case is dealt
8552 			 * with here.  So it may seem odd that the
8553 			 * stateid is okay but the sequence id is a
8554 			 * replay but it is okay.
8555 			 */
8556 			switch (rfs4_check_lock_seqid(
8557 			    args->locker.locker4_u.lock_owner.lock_seqid,
8558 			    lsp, resop)) {
8559 			case NFS4_CHKSEQ_REPLAY:
8560 				if (resp->status != NFS4_OK) {
8561 					/*
8562 					 * Here is our replay and need
8563 					 * to verify that the last
8564 					 * response was an error.
8565 					 */
8566 					*cs->statusp = resp->status;
8567 					goto end;
8568 				}
8569 				/*
8570 				 * This is done since the sequence id
8571 				 * looked like a replay but it didn't
8572 				 * pass our check so a BAD_SEQID is
8573 				 * returned as a result.
8574 				 */
8575 				/*FALLTHROUGH*/
8576 			case NFS4_CHKSEQ_BAD:
8577 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8578 				goto end;
8579 			case NFS4_CHKSEQ_OKAY:
8580 				/* Everything looks okay move ahead */
8581 				break;
8582 			}
8583 			break;
8584 		case NFS4_CHECK_STATEID_OLD:
8585 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8586 			goto end;
8587 		case NFS4_CHECK_STATEID_BAD:
8588 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8589 			goto end;
8590 		case NFS4_CHECK_STATEID_EXPIRED:
8591 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8592 			goto end;
8593 		case NFS4_CHECK_STATEID_CLOSED:
8594 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8595 			goto end;
8596 		case NFS4_CHECK_STATEID_REPLAY:
8597 			switch (rfs4_check_lock_seqid(
8598 			    args->locker.locker4_u.lock_owner.lock_seqid,
8599 			    lsp, resop)) {
8600 			case NFS4_CHKSEQ_OKAY:
8601 				/*
8602 				 * This is a replayed stateid; if
8603 				 * seqid matches the next expected,
8604 				 * then client is using wrong seqid.
8605 				 */
8606 			case NFS4_CHKSEQ_BAD:
8607 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8608 				goto end;
8609 			case NFS4_CHKSEQ_REPLAY:
8610 				rfs4_update_lease(lsp->locker->client);
8611 				*cs->statusp = status = resp->status;
8612 				goto end;
8613 			}
8614 			break;
8615 		default:
8616 			ASSERT(FALSE);
8617 			break;
8618 		}
8619 
8620 		rfs4_update_lock_sequence(lsp);
8621 		rfs4_update_lease(lsp->locker->client);
8622 	}
8623 
8624 	/*
8625 	 * NFS4 only allows locking on regular files, so
8626 	 * verify type of object.
8627 	 */
8628 	if (cs->vp->v_type != VREG) {
8629 		if (cs->vp->v_type == VDIR)
8630 			status = NFS4ERR_ISDIR;
8631 		else
8632 			status = NFS4ERR_INVAL;
8633 		goto out;
8634 	}
8635 
8636 	cp = lsp->state->owner->client;
8637 
8638 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
8639 		status = NFS4ERR_GRACE;
8640 		goto out;
8641 	}
8642 
8643 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->can_reclaim) {
8644 		status = NFS4ERR_NO_GRACE;
8645 		goto out;
8646 	}
8647 
8648 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
8649 		status = NFS4ERR_NO_GRACE;
8650 		goto out;
8651 	}
8652 
8653 	if (lsp->state->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE)
8654 		cs->deleg = TRUE;
8655 
8656 	status = rfs4_do_lock(lsp, args->locktype,
8657 	    args->locker.locker4_u.lock_owner.lock_seqid,
8658 	    args->offset, args->length, cs->cr, resop);
8659 
8660 out:
8661 	lsp->skip_seqid_check = FALSE;
8662 
8663 	*cs->statusp = resp->status = status;
8664 
8665 	if (status == NFS4_OK) {
8666 		resp->LOCK4res_u.lock_stateid = lsp->lockid.stateid;
8667 		lsp->lock_completed = TRUE;
8668 	}
8669 	/*
8670 	 * Only update the "OPEN" response here if this was a new
8671 	 * lock_owner
8672 	 */
8673 	if (sp)
8674 		rfs4_update_open_resp(sp->owner, resop, NULL);
8675 
8676 	rfs4_update_lock_resp(lsp, resop);
8677 
8678 end:
8679 	if (lsp) {
8680 		if (ls_sw_held)
8681 			rfs4_sw_exit(&lsp->ls_sw);
8682 		/*
8683 		 * If an sp obtained, then the lsp does not represent
8684 		 * a lock on the file struct.
8685 		 */
8686 		if (sp != NULL)
8687 			rfs4_lo_state_rele(lsp, FALSE);
8688 		else
8689 			rfs4_lo_state_rele(lsp, TRUE);
8690 	}
8691 	if (sp) {
8692 		rfs4_sw_exit(&sp->owner->oo_sw);
8693 		rfs4_state_rele(sp);
8694 	}
8695 
8696 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
8697 	    LOCK4res *, resp);
8698 }
8699 
8700 /* free function for LOCK/LOCKT */
8701 static void
8702 lock_denied_free(nfs_resop4 *resop)
8703 {
8704 	LOCK4denied *dp = NULL;
8705 
8706 	switch (resop->resop) {
8707 	case OP_LOCK:
8708 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
8709 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
8710 		break;
8711 	case OP_LOCKT:
8712 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
8713 			dp = &resop->nfs_resop4_u.oplockt.denied;
8714 		break;
8715 	default:
8716 		break;
8717 	}
8718 
8719 	if (dp)
8720 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
8721 }
8722 
8723 /*ARGSUSED*/
8724 void
8725 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
8726 	    struct svc_req *req, struct compound_state *cs)
8727 {
8728 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
8729 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
8730 	nfsstat4 status;
8731 	stateid4 *stateid = &args->lock_stateid;
8732 	rfs4_lo_state_t *lsp;
8733 
8734 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
8735 	    LOCKU4args *, args);
8736 
8737 	if (cs->vp == NULL) {
8738 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8739 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8740 		    LOCKU4res *, resp);
8741 		return;
8742 	}
8743 
8744 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
8745 		*cs->statusp = resp->status = status;
8746 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8747 		    LOCKU4res *, resp);
8748 		return;
8749 	}
8750 
8751 	/* Ensure specified filehandle matches */
8752 	if (cs->vp != lsp->state->finfo->vp) {
8753 		rfs4_lo_state_rele(lsp, TRUE);
8754 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8755 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8756 		    LOCKU4res *, resp);
8757 		return;
8758 	}
8759 
8760 	/* hold off other access to lsp while we tinker */
8761 	rfs4_sw_enter(&lsp->ls_sw);
8762 
8763 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8764 	case NFS4_CHECK_STATEID_OKAY:
8765 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
8766 		    != NFS4_CHKSEQ_OKAY) {
8767 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8768 			goto end;
8769 		}
8770 		break;
8771 	case NFS4_CHECK_STATEID_OLD:
8772 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8773 		goto end;
8774 	case NFS4_CHECK_STATEID_BAD:
8775 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8776 		goto end;
8777 	case NFS4_CHECK_STATEID_EXPIRED:
8778 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8779 		goto end;
8780 	case NFS4_CHECK_STATEID_CLOSED:
8781 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8782 		goto end;
8783 	case NFS4_CHECK_STATEID_REPLAY:
8784 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
8785 		case NFS4_CHKSEQ_OKAY:
8786 				/*
8787 				 * This is a replayed stateid; if
8788 				 * seqid matches the next expected,
8789 				 * then client is using wrong seqid.
8790 				 */
8791 		case NFS4_CHKSEQ_BAD:
8792 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8793 			goto end;
8794 		case NFS4_CHKSEQ_REPLAY:
8795 			rfs4_update_lease(lsp->locker->client);
8796 			*cs->statusp = status = resp->status;
8797 			goto end;
8798 		}
8799 		break;
8800 	default:
8801 		ASSERT(FALSE);
8802 		break;
8803 	}
8804 
8805 	rfs4_update_lock_sequence(lsp);
8806 	rfs4_update_lease(lsp->locker->client);
8807 
8808 	/*
8809 	 * NFS4 only allows locking on regular files, so
8810 	 * verify type of object.
8811 	 */
8812 	if (cs->vp->v_type != VREG) {
8813 		if (cs->vp->v_type == VDIR)
8814 			status = NFS4ERR_ISDIR;
8815 		else
8816 			status = NFS4ERR_INVAL;
8817 		goto out;
8818 	}
8819 
8820 	if (rfs4_clnt_in_grace(lsp->state->owner->client)) {
8821 		status = NFS4ERR_GRACE;
8822 		goto out;
8823 	}
8824 
8825 	status = rfs4_do_lock(lsp, args->locktype,
8826 	    args->seqid, args->offset, args->length, cs->cr, resop);
8827 
8828 out:
8829 	*cs->statusp = resp->status = status;
8830 
8831 	if (status == NFS4_OK)
8832 		resp->lock_stateid = lsp->lockid.stateid;
8833 
8834 	rfs4_update_lock_resp(lsp, resop);
8835 
8836 end:
8837 	rfs4_sw_exit(&lsp->ls_sw);
8838 	rfs4_lo_state_rele(lsp, TRUE);
8839 
8840 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8841 	    LOCKU4res *, resp);
8842 }
8843 
8844 /*
8845  * LOCKT is a best effort routine, the client can not be guaranteed that
8846  * the status return is still in effect by the time the reply is received.
8847  * They are numerous race conditions in this routine, but we are not required
8848  * and can not be accurate.
8849  */
8850 /*ARGSUSED*/
8851 void
8852 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
8853 	    struct svc_req *req, struct compound_state *cs)
8854 {
8855 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
8856 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
8857 	rfs4_lockowner_t *lo;
8858 	rfs4_client_t *cp;
8859 	bool_t create = FALSE;
8860 	struct flock64 flk;
8861 	int error;
8862 	int flag = FREAD | FWRITE;
8863 	int ltype;
8864 	length4 posix_length;
8865 	sysid_t sysid;
8866 	pid_t pid;
8867 
8868 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
8869 	    LOCKT4args *, args);
8870 
8871 	if (cs->vp == NULL) {
8872 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8873 		goto out;
8874 	}
8875 
8876 	/*
8877 	 * NFS4 only allows locking on regular files, so
8878 	 * verify type of object.
8879 	 */
8880 	if (cs->vp->v_type != VREG) {
8881 		if (cs->vp->v_type == VDIR)
8882 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
8883 		else
8884 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
8885 		goto out;
8886 	}
8887 
8888 	/*
8889 	 * Check out the clientid to ensure the server knows about it
8890 	 * so that we correctly inform the client of a server reboot.
8891 	 */
8892 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
8893 	    == NULL) {
8894 		*cs->statusp = resp->status =
8895 		    rfs4_check_clientid(&args->owner.clientid, 0);
8896 		goto out;
8897 	}
8898 	if (rfs4_lease_expired(cp)) {
8899 		rfs4_client_close(cp);
8900 		/*
8901 		 * Protocol doesn't allow returning NFS4ERR_STALE as
8902 		 * other operations do on this check so STALE_CLIENTID
8903 		 * is returned instead
8904 		 */
8905 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
8906 		goto out;
8907 	}
8908 
8909 	if (rfs4_clnt_in_grace(cp)) {
8910 		*cs->statusp = resp->status = NFS4ERR_GRACE;
8911 		goto out;
8912 	}
8913 	rfs4_client_rele(cp);
8914 
8915 	resp->status = NFS4_OK;
8916 
8917 	switch (args->locktype) {
8918 	case READ_LT:
8919 	case READW_LT:
8920 		ltype = F_RDLCK;
8921 		break;
8922 	case WRITE_LT:
8923 	case WRITEW_LT:
8924 		ltype = F_WRLCK;
8925 		break;
8926 	}
8927 
8928 	posix_length = args->length;
8929 	/* Check for zero length. To lock to end of file use all ones for V4 */
8930 	if (posix_length == 0) {
8931 		*cs->statusp = resp->status = NFS4ERR_INVAL;
8932 		goto out;
8933 	} else if (posix_length == (length4)(~0)) {
8934 		posix_length = 0;	/* Posix to end of file  */
8935 	}
8936 
8937 	/* Find or create a lockowner */
8938 	lo = rfs4_findlockowner(&args->owner, &create);
8939 
8940 	if (lo) {
8941 		pid = lo->pid;
8942 		if ((resp->status =
8943 		    rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8944 			goto err;
8945 	} else {
8946 		pid = 0;
8947 		sysid = lockt_sysid;
8948 	}
8949 retry:
8950 	flk.l_type = ltype;
8951 	flk.l_whence = 0;		/* SEEK_SET */
8952 	flk.l_start = args->offset;
8953 	flk.l_len = posix_length;
8954 	flk.l_sysid = sysid;
8955 	flk.l_pid = pid;
8956 	flag |= F_REMOTELOCK;
8957 
8958 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
8959 
8960 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8961 	if (flk.l_len < 0 || flk.l_start < 0) {
8962 		resp->status = NFS4ERR_INVAL;
8963 		goto err;
8964 	}
8965 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
8966 	    NULL, cs->cr, NULL);
8967 
8968 	/*
8969 	 * N.B. We map error values to nfsv4 errors. This is differrent
8970 	 * than puterrno4 routine.
8971 	 */
8972 	switch (error) {
8973 	case 0:
8974 		if (flk.l_type == F_UNLCK)
8975 			resp->status = NFS4_OK;
8976 		else {
8977 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
8978 				goto retry;
8979 			resp->status = NFS4ERR_DENIED;
8980 		}
8981 		break;
8982 	case EOVERFLOW:
8983 		resp->status = NFS4ERR_INVAL;
8984 		break;
8985 	case EINVAL:
8986 		resp->status = NFS4ERR_NOTSUPP;
8987 		break;
8988 	default:
8989 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
8990 		    error);
8991 		resp->status = NFS4ERR_SERVERFAULT;
8992 		break;
8993 	}
8994 
8995 err:
8996 	if (lo)
8997 		rfs4_lockowner_rele(lo);
8998 	*cs->statusp = resp->status;
8999 out:
9000 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9001 	    LOCKT4res *, resp);
9002 }
9003 
9004 static int
9005 vop_shrlock(vnode_t *vp, int cmd, struct shrlock *sp, int fflags)
9006 {
9007 	int err;
9008 
9009 	if (cmd == F_UNSHARE && sp->s_deny == 0 && sp->s_access == 0)
9010 		return (0);
9011 
9012 	err = VOP_SHRLOCK(vp, cmd, sp, fflags, CRED(), NULL);
9013 
9014 	NFS4_DEBUG(rfs4_shrlock_debug,
9015 	    (CE_NOTE, "rfs4_shrlock %s vp=%p acc=%d dny=%d sysid=%d "
9016 	    "pid=%d err=%d\n", cmd == F_UNSHARE ? "UNSHR" : "SHARE",
9017 	    (void *) vp, sp->s_access, sp->s_deny, sp->s_sysid, sp->s_pid,
9018 	    err));
9019 
9020 	return (err);
9021 }
9022 
9023 static int
9024 rfs4_shrlock(rfs4_state_t *sp, int cmd)
9025 {
9026 	struct shrlock shr;
9027 	struct shr_locowner shr_loco;
9028 	int fflags;
9029 
9030 	fflags = shr.s_access = shr.s_deny = 0;
9031 
9032 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
9033 		fflags |= FREAD;
9034 		shr.s_access |= F_RDACC;
9035 	}
9036 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
9037 		fflags |= FWRITE;
9038 		shr.s_access |= F_WRACC;
9039 	}
9040 	if (sp->share_deny & OPEN4_SHARE_DENY_READ)
9041 		shr.s_deny |= F_RDDNY;
9042 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE)
9043 		shr.s_deny |= F_WRDNY;
9044 
9045 	shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
9046 	shr.s_sysid = sp->owner->client->sysidt;
9047 	shr_loco.sl_pid = shr.s_pid;
9048 	shr_loco.sl_id = shr.s_sysid;
9049 	shr.s_owner = (caddr_t)&shr_loco;
9050 	shr.s_own_len = sizeof (shr_loco);
9051 	return (vop_shrlock(sp->finfo->vp, cmd, &shr, fflags));
9052 }
9053 
9054 static int
9055 rfs4_share(rfs4_state_t *sp)
9056 {
9057 	int cmd;
9058 	cmd = nbl_need_check(sp->finfo->vp) ? F_SHARE_NBMAND : F_SHARE;
9059 	return (rfs4_shrlock(sp, cmd));
9060 }
9061 
9062 void
9063 rfs4_unshare(rfs4_state_t *sp)
9064 {
9065 	(void) rfs4_shrlock(sp, F_UNSHARE);
9066 }
9067