xref: /titanic_52/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision 1ce1951135b81c803c8dcf2f3c756009b1b0170a)
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 2007 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 	}
4903 
4904 	/* XXX start of possible race with delegations */
4905 
4906 	/*
4907 	 * We need to specially handle size changes because it is
4908 	 * possible for the client to create a file with read-only
4909 	 * modes, but with the file opened for writing. If the client
4910 	 * then tries to set the file size, e.g. ftruncate(3C),
4911 	 * fcntl(F_FREESP), the normal access checking done in
4912 	 * VOP_SETATTR would prevent the client from doing it even though
4913 	 * it should be allowed to do so.  To get around this, we do the
4914 	 * access checking for ourselves and use VOP_SPACE which doesn't
4915 	 * do the access checking.
4916 	 * Also the client should not be allowed to change the file
4917 	 * size if there is a conflicting non-blocking mandatory lock in
4918 	 * the region of the change.
4919 	 */
4920 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
4921 		u_offset_t offset;
4922 		ssize_t length;
4923 
4924 		/*
4925 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
4926 		 * before returning, sarg.vap->va_mask is used to
4927 		 * generate the setattr reply bitmap.  We also clear
4928 		 * AT_SIZE below before calling VOP_SPACE.  For both
4929 		 * of these cases, the va_mask needs to be saved here
4930 		 * and restored after calling VOP_SETATTR.
4931 		 */
4932 		saved_mask = sarg.vap->va_mask;
4933 
4934 		/*
4935 		 * Check any possible conflict due to NBMAND locks.
4936 		 * Get into critical region before VOP_GETATTR, so the
4937 		 * size attribute is valid when checking conflicts.
4938 		 */
4939 		if (nbl_need_check(vp)) {
4940 			nbl_start_crit(vp, RW_READER);
4941 			in_crit = 1;
4942 		}
4943 
4944 		bva.va_mask = AT_UID|AT_SIZE;
4945 		if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
4946 			status = puterrno4(error);
4947 			goto done;
4948 		}
4949 
4950 		if (in_crit) {
4951 			if (sarg.vap->va_size < bva.va_size) {
4952 				offset = sarg.vap->va_size;
4953 				length = bva.va_size - sarg.vap->va_size;
4954 			} else {
4955 				offset = bva.va_size;
4956 				length = sarg.vap->va_size - bva.va_size;
4957 			}
4958 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
4959 			    &ct)) {
4960 				status = NFS4ERR_LOCKED;
4961 				goto done;
4962 			}
4963 		}
4964 
4965 		if (crgetuid(cr) == bva.va_uid) {
4966 			sarg.vap->va_mask &= ~AT_SIZE;
4967 			bf.l_type = F_WRLCK;
4968 			bf.l_whence = 0;
4969 			bf.l_start = (off64_t)sarg.vap->va_size;
4970 			bf.l_len = 0;
4971 			bf.l_sysid = 0;
4972 			bf.l_pid = 0;
4973 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
4974 			    (offset_t)sarg.vap->va_size, cr, &ct);
4975 		}
4976 	}
4977 
4978 	if (!error && sarg.vap->va_mask != 0)
4979 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
4980 
4981 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
4982 	if (saved_mask & AT_SIZE)
4983 		sarg.vap->va_mask |= AT_SIZE;
4984 
4985 	/*
4986 	 * If an ACL was being set, it has been delayed until now,
4987 	 * in order to set the mode (via the VOP_SETATTR() above) first.
4988 	 */
4989 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
4990 		int i;
4991 
4992 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
4993 			if (ntov.amap[i] == FATTR4_ACL)
4994 				break;
4995 		if (i < NFS4_MAXNUM_ATTRS) {
4996 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
4997 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
4998 			if (error == 0) {
4999 				*resp |= FATTR4_ACL_MASK;
5000 			} else if (error == ENOTSUP) {
5001 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
5002 				status = NFS4ERR_ATTRNOTSUPP;
5003 				goto done;
5004 			}
5005 		} else {
5006 			NFS4_DEBUG(rfs4_debug,
5007 			    (CE_NOTE, "do_rfs4_op_setattr: "
5008 			    "unable to find ACL in fattr4"));
5009 			error = EINVAL;
5010 		}
5011 	}
5012 
5013 	if (error) {
5014 		status = puterrno4(error);
5015 
5016 		/*
5017 		 * Set the response bitmap when setattr failed.
5018 		 * If VOP_SETATTR partially succeeded, test by doing a
5019 		 * VOP_GETATTR on the object and comparing the data
5020 		 * to the setattr arguments.
5021 		 */
5022 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
5023 	} else {
5024 		/*
5025 		 * Force modified metadata out to stable storage.
5026 		 */
5027 		(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
5028 		/*
5029 		 * Set response bitmap
5030 		 */
5031 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
5032 	}
5033 
5034 /* Return early and already have a NFSv4 error */
5035 done:
5036 	/*
5037 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
5038 	 * conversion sets both readable and writeable NFS4 attrs
5039 	 * for AT_MTIME and AT_ATIME.  The line below masks out
5040 	 * unrequested attrs from the setattr result bitmap.  This
5041 	 * is placed after the done: label to catch the ATTRNOTSUP
5042 	 * case.
5043 	 */
5044 	*resp &= fattrp->attrmask;
5045 
5046 	if (in_crit)
5047 		nbl_end_crit(vp);
5048 
5049 	nfs4_ntov_table_free(&ntov, &sarg);
5050 
5051 	return (status);
5052 }
5053 
5054 /* ARGSUSED */
5055 static void
5056 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5057 	struct compound_state *cs)
5058 {
5059 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
5060 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
5061 	bslabel_t *clabel;
5062 
5063 	DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5064 	    SETATTR4args *, args);
5065 
5066 	if (cs->vp == NULL) {
5067 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5068 		goto out;
5069 	}
5070 
5071 	/*
5072 	 * If there is an unshared filesystem mounted on this vnode,
5073 	 * do not allow to setattr on this vnode.
5074 	 */
5075 	if (vn_ismntpt(cs->vp)) {
5076 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5077 		goto out;
5078 	}
5079 
5080 	resp->attrsset = 0;
5081 
5082 	if (rdonly4(cs->exi, cs->vp, req)) {
5083 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5084 		goto out;
5085 	}
5086 
5087 	/* check label before setting attributes */
5088 	if (is_system_labeled()) {
5089 		ASSERT(req->rq_label != NULL);
5090 		clabel = req->rq_label;
5091 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
5092 		    "got client label from request(1)",
5093 		    struct svc_req *, req);
5094 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
5095 			if (!do_rfs_label_check(clabel, cs->vp,
5096 			    EQUALITY_CHECK)) {
5097 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
5098 				goto out;
5099 			}
5100 		}
5101 	}
5102 
5103 	*cs->statusp = resp->status =
5104 	    do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
5105 	    &args->stateid);
5106 
5107 out:
5108 	DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5109 	    SETATTR4res *, resp);
5110 }
5111 
5112 /* ARGSUSED */
5113 static void
5114 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5115 	struct compound_state *cs)
5116 {
5117 	/*
5118 	 * verify and nverify are exactly the same, except that nverify
5119 	 * succeeds when some argument changed, and verify succeeds when
5120 	 * when none changed.
5121 	 */
5122 
5123 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
5124 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
5125 
5126 	int error;
5127 	struct nfs4_svgetit_arg sarg;
5128 	struct statvfs64 sb;
5129 	struct nfs4_ntov_table ntov;
5130 
5131 	DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5132 	    VERIFY4args *, args);
5133 
5134 	if (cs->vp == NULL) {
5135 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5136 		goto out;
5137 	}
5138 
5139 	sarg.sbp = &sb;
5140 	nfs4_ntov_table_init(&ntov);
5141 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5142 	    &sarg, &ntov, NFS4ATTR_VERIT);
5143 	if (resp->status != NFS4_OK) {
5144 		/*
5145 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5146 		 * so could return -1 for "no match".
5147 		 */
5148 		if (resp->status == -1)
5149 			resp->status = NFS4ERR_NOT_SAME;
5150 		goto done;
5151 	}
5152 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5153 	switch (error) {
5154 	case 0:
5155 		resp->status = NFS4_OK;
5156 		break;
5157 	case -1:
5158 		resp->status = NFS4ERR_NOT_SAME;
5159 		break;
5160 	default:
5161 		resp->status = puterrno4(error);
5162 		break;
5163 	}
5164 done:
5165 	*cs->statusp = resp->status;
5166 	nfs4_ntov_table_free(&ntov, &sarg);
5167 out:
5168 	DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5169 	    VERIFY4res *, resp);
5170 }
5171 
5172 /* ARGSUSED */
5173 static void
5174 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5175 	struct compound_state *cs)
5176 {
5177 	/*
5178 	 * verify and nverify are exactly the same, except that nverify
5179 	 * succeeds when some argument changed, and verify succeeds when
5180 	 * when none changed.
5181 	 */
5182 
5183 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
5184 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
5185 
5186 	int error;
5187 	struct nfs4_svgetit_arg sarg;
5188 	struct statvfs64 sb;
5189 	struct nfs4_ntov_table ntov;
5190 
5191 	DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5192 	    NVERIFY4args *, args);
5193 
5194 	if (cs->vp == NULL) {
5195 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5196 		DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5197 		    NVERIFY4res *, resp);
5198 		return;
5199 	}
5200 	sarg.sbp = &sb;
5201 	nfs4_ntov_table_init(&ntov);
5202 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5203 	    &sarg, &ntov, NFS4ATTR_VERIT);
5204 	if (resp->status != NFS4_OK) {
5205 		/*
5206 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5207 		 * so could return -1 for "no match".
5208 		 */
5209 		if (resp->status == -1)
5210 			resp->status = NFS4_OK;
5211 		goto done;
5212 	}
5213 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5214 	switch (error) {
5215 	case 0:
5216 		resp->status = NFS4ERR_SAME;
5217 		break;
5218 	case -1:
5219 		resp->status = NFS4_OK;
5220 		break;
5221 	default:
5222 		resp->status = puterrno4(error);
5223 		break;
5224 	}
5225 done:
5226 	*cs->statusp = resp->status;
5227 	nfs4_ntov_table_free(&ntov, &sarg);
5228 
5229 	DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5230 	    NVERIFY4res *, resp);
5231 }
5232 
5233 /*
5234  * XXX - This should live in an NFS header file.
5235  */
5236 #define	MAX_IOVECS	12
5237 
5238 /* ARGSUSED */
5239 static void
5240 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5241 	struct compound_state *cs)
5242 {
5243 	WRITE4args *args = &argop->nfs_argop4_u.opwrite;
5244 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
5245 	int error;
5246 	vnode_t *vp;
5247 	struct vattr bva;
5248 	u_offset_t rlimit;
5249 	struct uio uio;
5250 	struct iovec iov[MAX_IOVECS];
5251 	struct iovec *iovp;
5252 	int iovcnt;
5253 	int ioflag;
5254 	cred_t *savecred, *cr;
5255 	bool_t *deleg = &cs->deleg;
5256 	nfsstat4 stat;
5257 	int in_crit = 0;
5258 	caller_context_t ct;
5259 
5260 	DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5261 	    WRITE4args *, args);
5262 
5263 	vp = cs->vp;
5264 	if (vp == NULL) {
5265 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5266 		goto out;
5267 	}
5268 	if (cs->access == CS_ACCESS_DENIED) {
5269 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5270 		goto out;
5271 	}
5272 
5273 	cr = cs->cr;
5274 
5275 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5276 	    deleg, TRUE, &ct)) != NFS4_OK) {
5277 		*cs->statusp = resp->status = stat;
5278 		goto out;
5279 	}
5280 
5281 	/*
5282 	 * We have to enter the critical region before calling VOP_RWLOCK
5283 	 * to avoid a deadlock with ufs.
5284 	 */
5285 	if (nbl_need_check(vp)) {
5286 		nbl_start_crit(vp, RW_READER);
5287 		in_crit = 1;
5288 		if (nbl_conflict(vp, NBL_WRITE,
5289 		    args->offset, args->data_len, 0, &ct)) {
5290 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
5291 			goto out;
5292 		}
5293 	}
5294 
5295 	bva.va_mask = AT_MODE | AT_UID;
5296 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
5297 
5298 	/*
5299 	 * If we can't get the attributes, then we can't do the
5300 	 * right access checking.  So, we'll fail the request.
5301 	 */
5302 	if (error) {
5303 		*cs->statusp = resp->status = puterrno4(error);
5304 		goto out;
5305 	}
5306 
5307 	if (rdonly4(cs->exi, cs->vp, req)) {
5308 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5309 		goto out;
5310 	}
5311 
5312 	if (vp->v_type != VREG) {
5313 		*cs->statusp = resp->status =
5314 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
5315 		goto out;
5316 	}
5317 
5318 	if (crgetuid(cr) != bva.va_uid &&
5319 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
5320 		*cs->statusp = resp->status = puterrno4(error);
5321 		goto out;
5322 	}
5323 
5324 	if (MANDLOCK(vp, bva.va_mode)) {
5325 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5326 		goto out;
5327 	}
5328 
5329 	if (args->data_len == 0) {
5330 		*cs->statusp = resp->status = NFS4_OK;
5331 		resp->count = 0;
5332 		resp->committed = args->stable;
5333 		resp->writeverf = Write4verf;
5334 		goto out;
5335 	}
5336 
5337 	if (args->mblk != NULL) {
5338 		mblk_t *m;
5339 		uint_t bytes, round_len;
5340 
5341 		iovcnt = 0;
5342 		bytes = 0;
5343 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
5344 		for (m = args->mblk;
5345 		    m != NULL && bytes < round_len;
5346 		    m = m->b_cont) {
5347 			iovcnt++;
5348 			bytes += MBLKL(m);
5349 		}
5350 #ifdef DEBUG
5351 		/* should have ended on an mblk boundary */
5352 		if (bytes != round_len) {
5353 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
5354 			    bytes, round_len, args->data_len);
5355 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
5356 			    (void *)args->mblk, (void *)m);
5357 			ASSERT(bytes == round_len);
5358 		}
5359 #endif
5360 		if (iovcnt <= MAX_IOVECS) {
5361 			iovp = iov;
5362 		} else {
5363 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
5364 		}
5365 		mblk_to_iov(args->mblk, iovcnt, iovp);
5366 	} else {
5367 		iovcnt = 1;
5368 		iovp = iov;
5369 		iovp->iov_base = args->data_val;
5370 		iovp->iov_len = args->data_len;
5371 	}
5372 
5373 	uio.uio_iov = iovp;
5374 	uio.uio_iovcnt = iovcnt;
5375 
5376 	uio.uio_segflg = UIO_SYSSPACE;
5377 	uio.uio_extflg = UIO_COPY_DEFAULT;
5378 	uio.uio_loffset = args->offset;
5379 	uio.uio_resid = args->data_len;
5380 	uio.uio_llimit = curproc->p_fsz_ctl;
5381 	rlimit = uio.uio_llimit - args->offset;
5382 	if (rlimit < (u_offset_t)uio.uio_resid)
5383 		uio.uio_resid = (int)rlimit;
5384 
5385 	if (args->stable == UNSTABLE4)
5386 		ioflag = 0;
5387 	else if (args->stable == FILE_SYNC4)
5388 		ioflag = FSYNC;
5389 	else if (args->stable == DATA_SYNC4)
5390 		ioflag = FDSYNC;
5391 	else {
5392 		if (iovp != iov)
5393 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
5394 		*cs->statusp = resp->status = NFS4ERR_INVAL;
5395 		goto out;
5396 	}
5397 
5398 	/*
5399 	 * We're changing creds because VM may fault and we need
5400 	 * the cred of the current thread to be used if quota
5401 	 * checking is enabled.
5402 	 */
5403 	savecred = curthread->t_cred;
5404 	curthread->t_cred = cr;
5405 	error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
5406 	curthread->t_cred = savecred;
5407 
5408 	if (iovp != iov)
5409 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
5410 
5411 	if (error) {
5412 		*cs->statusp = resp->status = puterrno4(error);
5413 		goto out;
5414 	}
5415 
5416 	*cs->statusp = resp->status = NFS4_OK;
5417 	resp->count = args->data_len - uio.uio_resid;
5418 
5419 	if (ioflag == 0)
5420 		resp->committed = UNSTABLE4;
5421 	else
5422 		resp->committed = FILE_SYNC4;
5423 
5424 	resp->writeverf = Write4verf;
5425 
5426 out:
5427 	if (in_crit)
5428 		nbl_end_crit(vp);
5429 
5430 	DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5431 	    WRITE4res *, resp);
5432 }
5433 
5434 
5435 /* XXX put in a header file */
5436 extern int	sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
5437 
5438 void
5439 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
5440 	struct svc_req *req, cred_t *cr)
5441 {
5442 	uint_t i;
5443 	struct compound_state cs;
5444 
5445 	rfs4_init_compound_state(&cs);
5446 	/*
5447 	 * Form a reply tag by copying over the reqeuest tag.
5448 	 */
5449 	resp->tag.utf8string_val =
5450 	    kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5451 	resp->tag.utf8string_len = args->tag.utf8string_len;
5452 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5453 	    resp->tag.utf8string_len);
5454 
5455 	cs.statusp = &resp->status;
5456 	cs.req = req;
5457 
5458 	/*
5459 	 * XXX for now, minorversion should be zero
5460 	 */
5461 	if (args->minorversion != NFS4_MINORVERSION) {
5462 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5463 		    &cs, COMPOUND4args *, args);
5464 		resp->array_len = 0;
5465 		resp->array = NULL;
5466 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5467 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5468 		    &cs, COMPOUND4res *, resp);
5469 		resp->array_len = 0;
5470 		return;
5471 	}
5472 
5473 	resp->array_len = args->array_len;
5474 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5475 	    KM_SLEEP);
5476 
5477 	ASSERT(exi == NULL);
5478 	ASSERT(cr == NULL);
5479 
5480 	cr = crget();
5481 	ASSERT(cr != NULL);
5482 
5483 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5484 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5485 		    &cs, COMPOUND4args *, args);
5486 		crfree(cr);
5487 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5488 		    &cs, COMPOUND4res *, resp);
5489 		return;
5490 	}
5491 
5492 	cs.basecr = cr;
5493 
5494 	DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5495 	    COMPOUND4args *, args);
5496 
5497 	/*
5498 	 * For now, NFS4 compound processing must be protected by
5499 	 * exported_lock because it can access more than one exportinfo
5500 	 * per compound and share/unshare can now change multiple
5501 	 * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5502 	 * per proc (excluding public exinfo), and exi_count design
5503 	 * is sufficient to protect concurrent execution of NFS2/3
5504 	 * ops along with unexport.  This lock will be removed as
5505 	 * part of the NFSv4 phase 2 namespace redesign work.
5506 	 */
5507 	rw_enter(&exported_lock, RW_READER);
5508 
5509 	/*
5510 	 * If this is the first compound we've seen, we need to start all
5511 	 * new instances' grace periods.
5512 	 */
5513 	if (rfs4_seen_first_compound == 0) {
5514 		rfs4_grace_start_new();
5515 		/*
5516 		 * This must be set after rfs4_grace_start_new(), otherwise
5517 		 * another thread could proceed past here before the former
5518 		 * is finished.
5519 		 */
5520 		rfs4_seen_first_compound = 1;
5521 	}
5522 
5523 	for (i = 0; i < args->array_len && cs.cont; i++) {
5524 		nfs_argop4 *argop;
5525 		nfs_resop4 *resop;
5526 		uint_t op;
5527 
5528 		argop = &args->array[i];
5529 		resop = &resp->array[i];
5530 		resop->resop = argop->argop;
5531 		op = (uint_t)resop->resop;
5532 
5533 		if (op < rfsv4disp_cnt) {
5534 			/*
5535 			 * Count the individual ops here; NULL and COMPOUND
5536 			 * are counted in common_dispatch()
5537 			 */
5538 			rfsproccnt_v4_ptr[op].value.ui64++;
5539 
5540 			NFS4_DEBUG(rfs4_debug > 1,
5541 			    (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5542 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5543 			NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5544 			    rfs4_op_string[op], *cs.statusp));
5545 			if (*cs.statusp != NFS4_OK)
5546 				cs.cont = FALSE;
5547 		} else {
5548 			/*
5549 			 * This is effectively dead code since XDR code
5550 			 * will have already returned BADXDR if op doesn't
5551 			 * decode to legal value.  This only done for a
5552 			 * day when XDR code doesn't verify v4 opcodes.
5553 			 */
5554 			op = OP_ILLEGAL;
5555 			rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
5556 
5557 			rfs4_op_illegal(argop, resop, req, &cs);
5558 			cs.cont = FALSE;
5559 		}
5560 
5561 		/*
5562 		 * If not at last op, and if we are to stop, then
5563 		 * compact the results array.
5564 		 */
5565 		if ((i + 1) < args->array_len && !cs.cont) {
5566 			nfs_resop4 *new_res = kmem_alloc(
5567 			    (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5568 			bcopy(resp->array,
5569 			    new_res, (i+1) * sizeof (nfs_resop4));
5570 			kmem_free(resp->array,
5571 			    args->array_len * sizeof (nfs_resop4));
5572 
5573 			resp->array_len =  i + 1;
5574 			resp->array = new_res;
5575 		}
5576 	}
5577 
5578 	rw_exit(&exported_lock);
5579 
5580 	DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5581 	    COMPOUND4res *, resp);
5582 
5583 	if (cs.vp)
5584 		VN_RELE(cs.vp);
5585 	if (cs.saved_vp)
5586 		VN_RELE(cs.saved_vp);
5587 	if (cs.saved_fh.nfs_fh4_val)
5588 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5589 
5590 	if (cs.basecr)
5591 		crfree(cs.basecr);
5592 	if (cs.cr)
5593 		crfree(cs.cr);
5594 	/*
5595 	 * done with this compound request, free the label
5596 	 */
5597 
5598 	if (req->rq_label != NULL) {
5599 		kmem_free(req->rq_label, sizeof (bslabel_t));
5600 		req->rq_label = NULL;
5601 	}
5602 }
5603 
5604 /*
5605  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5606  * XXX zero out the tag and array values. Need to investigate why the
5607  * XXX calls occur, but at least prevent the panic for now.
5608  */
5609 void
5610 rfs4_compound_free(COMPOUND4res *resp)
5611 {
5612 	uint_t i;
5613 
5614 	if (resp->tag.utf8string_val) {
5615 		UTF8STRING_FREE(resp->tag)
5616 	}
5617 
5618 	for (i = 0; i < resp->array_len; i++) {
5619 		nfs_resop4 *resop;
5620 		uint_t op;
5621 
5622 		resop = &resp->array[i];
5623 		op = (uint_t)resop->resop;
5624 		if (op < rfsv4disp_cnt) {
5625 			(*rfsv4disptab[op].dis_resfree)(resop);
5626 		}
5627 	}
5628 	if (resp->array != NULL) {
5629 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
5630 	}
5631 }
5632 
5633 /*
5634  * Process the value of the compound request rpc flags, as a bit-AND
5635  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
5636  */
5637 void
5638 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
5639 {
5640 	int i;
5641 	int flag = RPC_ALL;
5642 
5643 	for (i = 0; flag && i < args->array_len; i++) {
5644 		uint_t op;
5645 
5646 		op = (uint_t)args->array[i].argop;
5647 
5648 		if (op < rfsv4disp_cnt)
5649 			flag &= rfsv4disptab[op].dis_flags;
5650 		else
5651 			flag = 0;
5652 	}
5653 	*flagp = flag;
5654 }
5655 
5656 nfsstat4
5657 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
5658 {
5659 	nfsstat4 e;
5660 
5661 	rfs4_dbe_lock(cp->dbe);
5662 
5663 	if (cp->sysidt != LM_NOSYSID) {
5664 		*sp = cp->sysidt;
5665 		e = NFS4_OK;
5666 
5667 	} else if ((cp->sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
5668 		*sp = cp->sysidt;
5669 		e = NFS4_OK;
5670 
5671 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
5672 		    "rfs4_client_sysid: allocated 0x%x\n", *sp));
5673 	} else
5674 		e = NFS4ERR_DELAY;
5675 
5676 	rfs4_dbe_unlock(cp->dbe);
5677 	return (e);
5678 }
5679 
5680 #if defined(DEBUG) && ! defined(lint)
5681 static void lock_print(char *str, int operation, struct flock64 *flk)
5682 {
5683 	char *op, *type;
5684 
5685 	switch (operation) {
5686 	case F_GETLK: op = "F_GETLK";
5687 		break;
5688 	case F_SETLK: op = "F_SETLK";
5689 		break;
5690 	case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
5691 		break;
5692 	default: op = "F_UNKNOWN";
5693 		break;
5694 	}
5695 	switch (flk->l_type) {
5696 	case F_UNLCK: type = "F_UNLCK";
5697 		break;
5698 	case F_RDLCK: type = "F_RDLCK";
5699 		break;
5700 	case F_WRLCK: type = "F_WRLCK";
5701 		break;
5702 	default: type = "F_UNKNOWN";
5703 		break;
5704 	}
5705 
5706 	ASSERT(flk->l_whence == 0);
5707 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
5708 	    str, op, type, (longlong_t)flk->l_start,
5709 	    flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
5710 }
5711 
5712 #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
5713 #else
5714 #define	LOCK_PRINT(d, s, t, f)
5715 #endif
5716 
5717 /*ARGSUSED*/
5718 static bool_t
5719 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
5720 {
5721 	return (TRUE);
5722 }
5723 
5724 /*
5725  * Look up the pathname using the vp in cs as the directory vnode.
5726  * cs->vp will be the vnode for the file on success
5727  */
5728 
5729 static nfsstat4
5730 rfs4_lookup(component4 *component, struct svc_req *req,
5731 	    struct compound_state *cs)
5732 {
5733 	char *nm;
5734 	uint32_t len;
5735 	nfsstat4 status;
5736 
5737 	if (cs->vp == NULL) {
5738 		return (NFS4ERR_NOFILEHANDLE);
5739 	}
5740 	if (cs->vp->v_type != VDIR) {
5741 		return (NFS4ERR_NOTDIR);
5742 	}
5743 
5744 	if (!utf8_dir_verify(component))
5745 		return (NFS4ERR_INVAL);
5746 
5747 	nm = utf8_to_fn(component, &len, NULL);
5748 	if (nm == NULL) {
5749 		return (NFS4ERR_INVAL);
5750 	}
5751 
5752 	if (len > MAXNAMELEN) {
5753 		kmem_free(nm, len);
5754 		return (NFS4ERR_NAMETOOLONG);
5755 	}
5756 
5757 	status = do_rfs4_op_lookup(nm, len, req, cs);
5758 
5759 	kmem_free(nm, len);
5760 
5761 	return (status);
5762 }
5763 
5764 static nfsstat4
5765 rfs4_lookupfile(component4 *component, struct svc_req *req,
5766 		struct compound_state *cs, uint32_t access,
5767 		change_info4 *cinfo)
5768 {
5769 	nfsstat4 status;
5770 	vnode_t *dvp = cs->vp;
5771 	vattr_t bva, ava, fva;
5772 	int error;
5773 
5774 	/* Get "before" change value */
5775 	bva.va_mask = AT_CTIME|AT_SEQ;
5776 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
5777 	if (error)
5778 		return (puterrno4(error));
5779 
5780 	/* rfs4_lookup may VN_RELE directory */
5781 	VN_HOLD(dvp);
5782 
5783 	status = rfs4_lookup(component, req, cs);
5784 	if (status != NFS4_OK) {
5785 		VN_RELE(dvp);
5786 		return (status);
5787 	}
5788 
5789 	/*
5790 	 * Get "after" change value, if it fails, simply return the
5791 	 * before value.
5792 	 */
5793 	ava.va_mask = AT_CTIME|AT_SEQ;
5794 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
5795 		ava.va_ctime = bva.va_ctime;
5796 		ava.va_seq = 0;
5797 	}
5798 	VN_RELE(dvp);
5799 
5800 	/*
5801 	 * Validate the file is a file
5802 	 */
5803 	fva.va_mask = AT_TYPE|AT_MODE;
5804 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
5805 	if (error)
5806 		return (puterrno4(error));
5807 
5808 	if (fva.va_type != VREG) {
5809 		if (fva.va_type == VDIR)
5810 			return (NFS4ERR_ISDIR);
5811 		if (fva.va_type == VLNK)
5812 			return (NFS4ERR_SYMLINK);
5813 		return (NFS4ERR_INVAL);
5814 	}
5815 
5816 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
5817 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
5818 
5819 	/*
5820 	 * It is undefined if VOP_LOOKUP will change va_seq, so
5821 	 * cinfo.atomic = TRUE only if we have
5822 	 * non-zero va_seq's, and they have not changed.
5823 	 */
5824 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
5825 		cinfo->atomic = TRUE;
5826 	else
5827 		cinfo->atomic = FALSE;
5828 
5829 	/* Check for mandatory locking */
5830 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
5831 	return (check_open_access(access, cs, req));
5832 }
5833 
5834 static nfsstat4
5835 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
5836 	    timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created)
5837 {
5838 	int error;
5839 	nfsstat4 status = NFS4_OK;
5840 	vattr_t va;
5841 
5842 tryagain:
5843 
5844 	/*
5845 	 * The file open mode used is VWRITE.  If the client needs
5846 	 * some other semantic, then it should do the access checking
5847 	 * itself.  It would have been nice to have the file open mode
5848 	 * passed as part of the arguments.
5849 	 */
5850 
5851 	*created = TRUE;
5852 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
5853 
5854 	if (error) {
5855 		*created = FALSE;
5856 
5857 		/*
5858 		 * If we got something other than file already exists
5859 		 * then just return this error.  Otherwise, we got
5860 		 * EEXIST.  If we were doing a GUARDED create, then
5861 		 * just return this error.  Otherwise, we need to
5862 		 * make sure that this wasn't a duplicate of an
5863 		 * exclusive create request.
5864 		 *
5865 		 * The assumption is made that a non-exclusive create
5866 		 * request will never return EEXIST.
5867 		 */
5868 
5869 		if (error != EEXIST || mode == GUARDED4) {
5870 			status = puterrno4(error);
5871 			return (status);
5872 		}
5873 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
5874 		    NULL, NULL, NULL);
5875 
5876 		if (error) {
5877 			/*
5878 			 * We couldn't find the file that we thought that
5879 			 * we just created.  So, we'll just try creating
5880 			 * it again.
5881 			 */
5882 			if (error == ENOENT)
5883 				goto tryagain;
5884 
5885 			status = puterrno4(error);
5886 			return (status);
5887 		}
5888 
5889 		if (mode == UNCHECKED4) {
5890 			/* existing object must be regular file */
5891 			if ((*vpp)->v_type != VREG) {
5892 				if ((*vpp)->v_type == VDIR)
5893 					status = NFS4ERR_ISDIR;
5894 				else if ((*vpp)->v_type == VLNK)
5895 					status = NFS4ERR_SYMLINK;
5896 				else
5897 					status = NFS4ERR_INVAL;
5898 				VN_RELE(*vpp);
5899 				return (status);
5900 			}
5901 
5902 			return (NFS4_OK);
5903 		}
5904 
5905 		/* Check for duplicate request */
5906 		ASSERT(mtime != 0);
5907 		va.va_mask = AT_MTIME;
5908 		error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
5909 		if (!error) {
5910 			/* We found the file */
5911 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
5912 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
5913 				/* but its not our creation */
5914 				VN_RELE(*vpp);
5915 				return (NFS4ERR_EXIST);
5916 			}
5917 			*created = TRUE; /* retrans of create == created */
5918 			return (NFS4_OK);
5919 		}
5920 		VN_RELE(*vpp);
5921 		return (NFS4ERR_EXIST);
5922 	}
5923 
5924 	return (NFS4_OK);
5925 }
5926 
5927 static nfsstat4
5928 check_open_access(uint32_t access,
5929 		struct compound_state *cs, struct svc_req *req)
5930 {
5931 	int error;
5932 	vnode_t *vp;
5933 	bool_t readonly;
5934 	cred_t *cr = cs->cr;
5935 
5936 	/* For now we don't allow mandatory locking as per V2/V3 */
5937 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
5938 		return (NFS4ERR_ACCESS);
5939 	}
5940 
5941 	vp = cs->vp;
5942 	ASSERT(cr != NULL && vp->v_type == VREG);
5943 
5944 	/*
5945 	 * If the file system is exported read only and we are trying
5946 	 * to open for write, then return NFS4ERR_ROFS
5947 	 */
5948 
5949 	readonly = rdonly4(cs->exi, cs->vp, req);
5950 
5951 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
5952 		return (NFS4ERR_ROFS);
5953 
5954 	if (access & OPEN4_SHARE_ACCESS_READ) {
5955 		if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
5956 		    (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
5957 			return (NFS4ERR_ACCESS);
5958 		}
5959 	}
5960 
5961 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
5962 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
5963 		if (error)
5964 			return (NFS4ERR_ACCESS);
5965 	}
5966 
5967 	return (NFS4_OK);
5968 }
5969 
5970 static nfsstat4
5971 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
5972 		change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
5973 {
5974 	struct nfs4_svgetit_arg sarg;
5975 	struct nfs4_ntov_table ntov;
5976 
5977 	bool_t ntov_table_init = FALSE;
5978 	struct statvfs64 sb;
5979 	nfsstat4 status;
5980 	vnode_t *vp;
5981 	vattr_t bva, ava, iva, cva, *vap;
5982 	vnode_t *dvp;
5983 	timespec32_t *mtime;
5984 	char *nm = NULL;
5985 	uint_t buflen;
5986 	bool_t created;
5987 	bool_t setsize = FALSE;
5988 	len_t reqsize;
5989 	int error;
5990 	bool_t trunc;
5991 	caller_context_t ct;
5992 	component4 *component;
5993 	bslabel_t *clabel;
5994 
5995 	sarg.sbp = &sb;
5996 
5997 	dvp = cs->vp;
5998 
5999 	/* Check if the file system is read only */
6000 	if (rdonly4(cs->exi, dvp, req))
6001 		return (NFS4ERR_ROFS);
6002 
6003 	/* check the label of including directory */
6004 	if (is_system_labeled()) {
6005 		ASSERT(req->rq_label != NULL);
6006 		clabel = req->rq_label;
6007 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6008 		    "got client label from request(1)",
6009 		    struct svc_req *, req);
6010 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
6011 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK)) {
6012 				return (NFS4ERR_ACCESS);
6013 			}
6014 		}
6015 	}
6016 
6017 	/*
6018 	 * Get the last component of path name in nm. cs will reference
6019 	 * the including directory on success.
6020 	 */
6021 	component = &args->open_claim4_u.file;
6022 	if (!utf8_dir_verify(component))
6023 		return (NFS4ERR_INVAL);
6024 
6025 	nm = utf8_to_fn(component, &buflen, NULL);
6026 
6027 	if (nm == NULL)
6028 		return (NFS4ERR_RESOURCE);
6029 
6030 	if (buflen > MAXNAMELEN) {
6031 		kmem_free(nm, buflen);
6032 		return (NFS4ERR_NAMETOOLONG);
6033 	}
6034 
6035 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6036 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6037 	if (error) {
6038 		kmem_free(nm, buflen);
6039 		return (puterrno4(error));
6040 	}
6041 
6042 	if (bva.va_type != VDIR) {
6043 		kmem_free(nm, buflen);
6044 		return (NFS4ERR_NOTDIR);
6045 	}
6046 
6047 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6048 
6049 	switch (args->mode) {
6050 	case GUARDED4:
6051 		/*FALLTHROUGH*/
6052 	case UNCHECKED4:
6053 		nfs4_ntov_table_init(&ntov);
6054 		ntov_table_init = TRUE;
6055 
6056 		*attrset = 0;
6057 		status = do_rfs4_set_attrs(attrset,
6058 		    &args->createhow4_u.createattrs,
6059 		    cs, &sarg, &ntov, NFS4ATTR_SETIT);
6060 
6061 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6062 		    sarg.vap->va_type != VREG) {
6063 			if (sarg.vap->va_type == VDIR)
6064 				status = NFS4ERR_ISDIR;
6065 			else if (sarg.vap->va_type == VLNK)
6066 				status = NFS4ERR_SYMLINK;
6067 			else
6068 				status = NFS4ERR_INVAL;
6069 		}
6070 
6071 		if (status != NFS4_OK) {
6072 			kmem_free(nm, buflen);
6073 			nfs4_ntov_table_free(&ntov, &sarg);
6074 			*attrset = 0;
6075 			return (status);
6076 		}
6077 
6078 		vap = sarg.vap;
6079 		vap->va_type = VREG;
6080 		vap->va_mask |= AT_TYPE;
6081 
6082 		if ((vap->va_mask & AT_MODE) == 0) {
6083 			vap->va_mask |= AT_MODE;
6084 			vap->va_mode = (mode_t)0600;
6085 		}
6086 
6087 		if (vap->va_mask & AT_SIZE) {
6088 
6089 			/* Disallow create with a non-zero size */
6090 
6091 			if ((reqsize = sarg.vap->va_size) != 0) {
6092 				kmem_free(nm, buflen);
6093 				nfs4_ntov_table_free(&ntov, &sarg);
6094 				*attrset = 0;
6095 				return (NFS4ERR_INVAL);
6096 			}
6097 			setsize = TRUE;
6098 		}
6099 		break;
6100 
6101 	case EXCLUSIVE4:
6102 		/* prohibit EXCL create of named attributes */
6103 		if (dvp->v_flag & V_XATTRDIR) {
6104 			kmem_free(nm, buflen);
6105 			*attrset = 0;
6106 			return (NFS4ERR_INVAL);
6107 		}
6108 
6109 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6110 		cva.va_type = VREG;
6111 		/*
6112 		 * Ensure no time overflows. Assumes underlying
6113 		 * filesystem supports at least 32 bits.
6114 		 * Truncate nsec to usec resolution to allow valid
6115 		 * compares even if the underlying filesystem truncates.
6116 		 */
6117 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
6118 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6119 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6120 		cva.va_mode = (mode_t)0;
6121 		vap = &cva;
6122 		break;
6123 	}
6124 
6125 	status = create_vnode(dvp, nm, vap, args->mode, mtime,
6126 	    cs->cr, &vp, &created);
6127 	kmem_free(nm, buflen);
6128 
6129 	if (status != NFS4_OK) {
6130 		if (ntov_table_init)
6131 			nfs4_ntov_table_free(&ntov, &sarg);
6132 		*attrset = 0;
6133 		return (status);
6134 	}
6135 
6136 	trunc = (setsize && !created);
6137 
6138 	if (args->mode != EXCLUSIVE4) {
6139 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6140 
6141 		/*
6142 		 * True verification that object was created with correct
6143 		 * attrs is impossible.  The attrs could have been changed
6144 		 * immediately after object creation.  If attributes did
6145 		 * not verify, the only recourse for the server is to
6146 		 * destroy the object.  Maybe if some attrs (like gid)
6147 		 * are set incorrectly, the object should be destroyed;
6148 		 * however, seems bad as a default policy.  Do we really
6149 		 * want to destroy an object over one of the times not
6150 		 * verifying correctly?  For these reasons, the server
6151 		 * currently sets bits in attrset for createattrs
6152 		 * that were set; however, no verification is done.
6153 		 *
6154 		 * vmask_to_nmask accounts for vattr bits set on create
6155 		 *	[do_rfs4_set_attrs() only sets resp bits for
6156 		 *	 non-vattr/vfs bits.]
6157 		 * Mask off any bits we set by default so as not to return
6158 		 * more attrset bits than were requested in createattrs
6159 		 */
6160 		if (created) {
6161 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6162 			*attrset &= createmask;
6163 		} else {
6164 			/*
6165 			 * We did not create the vnode (we tried but it
6166 			 * already existed).  In this case, the only createattr
6167 			 * that the spec allows the server to set is size,
6168 			 * and even then, it can only be set if it is 0.
6169 			 */
6170 			*attrset = 0;
6171 			if (trunc)
6172 				*attrset = FATTR4_SIZE_MASK;
6173 		}
6174 	}
6175 	if (ntov_table_init)
6176 		nfs4_ntov_table_free(&ntov, &sarg);
6177 
6178 	/*
6179 	 * Get the initial "after" sequence number, if it fails,
6180 	 * set to zero, time to before.
6181 	 */
6182 	iva.va_mask = AT_CTIME|AT_SEQ;
6183 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6184 		iva.va_seq = 0;
6185 		iva.va_ctime = bva.va_ctime;
6186 	}
6187 
6188 	/*
6189 	 * create_vnode attempts to create the file exclusive,
6190 	 * if it already exists the VOP_CREATE will fail and
6191 	 * may not increase va_seq. It is atomic if
6192 	 * we haven't changed the directory, but if it has changed
6193 	 * we don't know what changed it.
6194 	 */
6195 	if (!created) {
6196 		if (bva.va_seq && iva.va_seq &&
6197 		    bva.va_seq == iva.va_seq)
6198 			cinfo->atomic = TRUE;
6199 		else
6200 			cinfo->atomic = FALSE;
6201 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6202 	} else {
6203 		/*
6204 		 * The entry was created, we need to sync the
6205 		 * directory metadata.
6206 		 */
6207 		(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6208 
6209 		/*
6210 		 * Get "after" change value, if it fails, simply return the
6211 		 * before value.
6212 		 */
6213 		ava.va_mask = AT_CTIME|AT_SEQ;
6214 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6215 			ava.va_ctime = bva.va_ctime;
6216 			ava.va_seq = 0;
6217 		}
6218 
6219 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6220 
6221 		/*
6222 		 * The cinfo->atomic = TRUE only if we have
6223 		 * non-zero va_seq's, and it has incremented by exactly one
6224 		 * during the create_vnode and it didn't
6225 		 * change during the VOP_FSYNC.
6226 		 */
6227 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
6228 		    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6229 			cinfo->atomic = TRUE;
6230 		else
6231 			cinfo->atomic = FALSE;
6232 	}
6233 
6234 	/* Check for mandatory locking and that the size gets set. */
6235 	cva.va_mask = AT_MODE;
6236 	if (setsize)
6237 		cva.va_mask |= AT_SIZE;
6238 
6239 	/* Assume the worst */
6240 	cs->mandlock = TRUE;
6241 
6242 	if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6243 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6244 
6245 		/*
6246 		 * Truncate the file if necessary; this would be
6247 		 * the case for create over an existing file.
6248 		 */
6249 
6250 		if (trunc) {
6251 			int in_crit = 0;
6252 			rfs4_file_t *fp;
6253 			bool_t create = FALSE;
6254 
6255 			/*
6256 			 * We are writing over an existing file.
6257 			 * Check to see if we need to recall a delegation.
6258 			 */
6259 			rfs4_hold_deleg_policy();
6260 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6261 				if (rfs4_check_delegated_byfp(FWRITE, fp,
6262 				    (reqsize == 0), FALSE, FALSE, &clientid)) {
6263 					rfs4_file_rele(fp);
6264 					rfs4_rele_deleg_policy();
6265 					VN_RELE(vp);
6266 					*attrset = 0;
6267 					return (NFS4ERR_DELAY);
6268 				}
6269 				rfs4_file_rele(fp);
6270 			}
6271 			rfs4_rele_deleg_policy();
6272 
6273 			if (nbl_need_check(vp)) {
6274 				in_crit = 1;
6275 
6276 				ASSERT(reqsize == 0);
6277 
6278 				nbl_start_crit(vp, RW_READER);
6279 				if (nbl_conflict(vp, NBL_WRITE, 0,
6280 				    cva.va_size, 0, NULL)) {
6281 					in_crit = 0;
6282 					nbl_end_crit(vp);
6283 					VN_RELE(vp);
6284 					*attrset = 0;
6285 					return (NFS4ERR_ACCESS);
6286 				}
6287 			}
6288 			ct.cc_sysid = 0;
6289 			ct.cc_pid = 0;
6290 			ct.cc_caller_id = nfs4_srv_caller_id;
6291 
6292 			cva.va_mask = AT_SIZE;
6293 			cva.va_size = reqsize;
6294 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6295 			if (in_crit)
6296 				nbl_end_crit(vp);
6297 		}
6298 	}
6299 
6300 	error = makefh4(&cs->fh, vp, cs->exi);
6301 
6302 	/*
6303 	 * Force modified data and metadata out to stable storage.
6304 	 */
6305 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6306 
6307 	if (error) {
6308 		VN_RELE(vp);
6309 		*attrset = 0;
6310 		return (puterrno4(error));
6311 	}
6312 
6313 	/* if parent dir is attrdir, set namedattr fh flag */
6314 	if (dvp->v_flag & V_XATTRDIR)
6315 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6316 
6317 	if (cs->vp)
6318 		VN_RELE(cs->vp);
6319 
6320 	cs->vp = vp;
6321 
6322 	/*
6323 	 * if we did not create the file, we will need to check
6324 	 * the access bits on the file
6325 	 */
6326 
6327 	if (!created) {
6328 		if (setsize)
6329 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6330 		status = check_open_access(args->share_access, cs, req);
6331 		if (status != NFS4_OK)
6332 			*attrset = 0;
6333 	}
6334 	return (status);
6335 }
6336 
6337 /*ARGSUSED*/
6338 static void
6339 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6340 		rfs4_openowner_t *oo, delegreq_t deleg,
6341 		uint32_t access, uint32_t deny,
6342 		OPEN4res *resp, int deleg_cur)
6343 {
6344 	/* XXX Currently not using req  */
6345 	rfs4_state_t *state;
6346 	rfs4_file_t *file;
6347 	bool_t screate = TRUE;
6348 	bool_t fcreate = TRUE;
6349 	uint32_t amodes;
6350 	uint32_t dmodes;
6351 	rfs4_deleg_state_t *dsp;
6352 	struct shrlock shr;
6353 	struct shr_locowner shr_loco;
6354 	sysid_t sysid;
6355 	nfsstat4 status;
6356 	caller_context_t ct;
6357 	int fflags = 0;
6358 	int recall = 0;
6359 	int err;
6360 	int cmd;
6361 
6362 	/* get the file struct and hold a lock on it during initial open */
6363 	file = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6364 	if (file == NULL) {
6365 		NFS4_DEBUG(rfs4_debug,
6366 		    (CE_NOTE, "rfs4_do_open: can't find file"));
6367 		resp->status = NFS4ERR_SERVERFAULT;
6368 		return;
6369 	}
6370 
6371 	state = rfs4_findstate_by_owner_file(oo, file, &screate);
6372 	if (state == NULL) {
6373 		NFS4_DEBUG(rfs4_debug,
6374 		    (CE_NOTE, "rfs4_do_open: can't find state"));
6375 		resp->status = NFS4ERR_RESOURCE;
6376 		/* No need to keep any reference */
6377 		rfs4_file_rele_withunlock(file);
6378 		return;
6379 	}
6380 
6381 	/* try to get the sysid before continuing */
6382 	if ((status = rfs4_client_sysid(oo->client, &sysid)) != NFS4_OK) {
6383 		resp->status = status;
6384 		rfs4_file_rele(file);
6385 		/* Not a fully formed open; "close" it */
6386 		if (screate == TRUE)
6387 			rfs4_state_close(state, FALSE, FALSE, cs->cr);
6388 		rfs4_state_rele(state);
6389 		return;
6390 	}
6391 
6392 	/* Calculate the fflags for this OPEN. */
6393 	if (access & OPEN4_SHARE_ACCESS_READ)
6394 		fflags |= FREAD;
6395 	if (access & OPEN4_SHARE_ACCESS_WRITE)
6396 		fflags |= FWRITE;
6397 
6398 	/*
6399 	 * Calculate the new deny and access mode that this open is adding to
6400 	 * the file for this open owner;
6401 	 */
6402 	dmodes = (deny & ~state->share_deny);
6403 	amodes = (access & ~state->share_access);
6404 
6405 	/*
6406 	 * Check to see the client has already sent an open for this
6407 	 * open owner on this file with the same share/deny modes.
6408 	 * If so, we don't need to check for a conflict and we don't
6409 	 * need to add another shrlock.  If not, then we need to
6410 	 * check for conflicts in deny and access before checking for
6411 	 * conflicts in delegation.  We don't want to recall a
6412 	 * delegation based on an open that will eventually fail based
6413 	 * on shares modes.
6414 	 */
6415 
6416 	if (dmodes || amodes) {
6417 		shr.s_access = (short)access;
6418 		shr.s_deny = (short)deny;
6419 		shr.s_pid = rfs4_dbe_getid(oo->dbe);
6420 		shr.s_sysid = sysid;
6421 		shr_loco.sl_pid = shr.s_pid;
6422 		shr_loco.sl_id = shr.s_sysid;
6423 		shr.s_owner = (caddr_t)&shr_loco;
6424 		shr.s_own_len = sizeof (shr_loco);
6425 
6426 		cmd = nbl_need_check(cs->vp) ? F_SHARE_NBMAND : F_SHARE;
6427 		if ((err = vop_shrlock(cs->vp, cmd, &shr, fflags)) != 0) {
6428 
6429 			resp->status = err == EAGAIN ?
6430 			    NFS4ERR_SHARE_DENIED : puterrno4(err);
6431 
6432 			rfs4_file_rele(file);
6433 			/* Not a fully formed open; "close" it */
6434 			if (screate == TRUE)
6435 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6436 			rfs4_state_rele(state);
6437 			return;
6438 		}
6439 	}
6440 
6441 	rfs4_dbe_lock(state->dbe);
6442 	rfs4_dbe_lock(file->dbe);
6443 
6444 	/*
6445 	 * Check to see if this file is delegated and if so, if a
6446 	 * recall needs to be done.
6447 	 */
6448 	if (rfs4_check_recall(state, access)) {
6449 		rfs4_dbe_unlock(file->dbe);
6450 		rfs4_dbe_unlock(state->dbe);
6451 		rfs4_recall_deleg(file, FALSE, state->owner->client);
6452 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6453 		rfs4_dbe_lock(state->dbe);
6454 		rfs4_dbe_lock(file->dbe);
6455 		/* Let's see if the delegation was returned */
6456 		if (rfs4_check_recall(state, access)) {
6457 			rfs4_dbe_unlock(file->dbe);
6458 			rfs4_dbe_unlock(state->dbe);
6459 			rfs4_file_rele(file);
6460 			rfs4_update_lease(state->owner->client);
6461 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6462 			/* Not a fully formed open; "close" it */
6463 			if (screate == TRUE)
6464 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6465 			rfs4_state_rele(state);
6466 			resp->status = NFS4ERR_DELAY;
6467 			return;
6468 		}
6469 	}
6470 	/*
6471 	 * the share check passed and any delegation conflict has been
6472 	 * taken care of, now call vop_open.
6473 	 * if this is the first open then call vop_open with fflags.
6474 	 * if not, call vn_open_upgrade with just the upgrade flags.
6475 	 *
6476 	 * if the file has been opened already, it will have the current
6477 	 * access mode in the state struct.  if it has no share access, then
6478 	 * this is a new open.
6479 	 *
6480 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6481 	 * call VOP_OPEN(), just do the open upgrade.
6482 	 */
6483 	if (((state->share_access & OPEN4_SHARE_ACCESS_BOTH) == 0) &&
6484 	    !deleg_cur) {
6485 		ct.cc_sysid = sysid;
6486 		ct.cc_pid = shr.s_pid;
6487 		ct.cc_caller_id = nfs4_srv_caller_id;
6488 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6489 		if (err) {
6490 			rfs4_dbe_unlock(file->dbe);
6491 			rfs4_dbe_unlock(state->dbe);
6492 			rfs4_file_rele(file);
6493 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6494 			/* Not a fully formed open; "close" it */
6495 			if (screate == TRUE)
6496 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6497 			rfs4_state_rele(state);
6498 			resp->status = NFS4ERR_SERVERFAULT;
6499 			return;
6500 		}
6501 	} else { /* open upgrade */
6502 		/*
6503 		 * calculate the fflags for the new mode that is being added
6504 		 * by this upgrade.
6505 		 */
6506 		fflags = 0;
6507 		if (amodes & OPEN4_SHARE_ACCESS_READ)
6508 			fflags |= FREAD;
6509 		if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6510 			fflags |= FWRITE;
6511 		vn_open_upgrade(cs->vp, fflags);
6512 	}
6513 
6514 	if (dmodes & OPEN4_SHARE_DENY_READ)
6515 		file->deny_read++;
6516 	if (dmodes & OPEN4_SHARE_DENY_WRITE)
6517 		file->deny_write++;
6518 	file->share_deny |= deny;
6519 	state->share_deny |= deny;
6520 
6521 	if (amodes & OPEN4_SHARE_ACCESS_READ)
6522 		file->access_read++;
6523 	if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6524 		file->access_write++;
6525 	file->share_access |= access;
6526 	state->share_access |= access;
6527 
6528 	/*
6529 	 * Check for delegation here. if the deleg argument is not
6530 	 * DELEG_ANY, then this is a reclaim from a client and
6531 	 * we must honor the delegation requested. If necessary we can
6532 	 * set the recall flag.
6533 	 */
6534 
6535 	dsp = rfs4_grant_delegation(deleg, state, &recall);
6536 
6537 	cs->deleg = (file->dinfo->dtype == OPEN_DELEGATE_WRITE);
6538 
6539 	next_stateid(&state->stateid);
6540 
6541 	resp->stateid = state->stateid.stateid;
6542 
6543 	rfs4_dbe_unlock(file->dbe);
6544 	rfs4_dbe_unlock(state->dbe);
6545 
6546 	if (dsp) {
6547 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6548 		rfs4_deleg_state_rele(dsp);
6549 	}
6550 
6551 	rfs4_file_rele(file);
6552 	rfs4_state_rele(state);
6553 
6554 	resp->status = NFS4_OK;
6555 }
6556 
6557 /*ARGSUSED*/
6558 static void
6559 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6560 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6561 {
6562 	change_info4 *cinfo = &resp->cinfo;
6563 	bitmap4 *attrset = &resp->attrset;
6564 
6565 	if (args->opentype == OPEN4_NOCREATE)
6566 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6567 		    req, cs, args->share_access, cinfo);
6568 	else {
6569 		/* inhibit delegation grants during exclusive create */
6570 
6571 		if (args->mode == EXCLUSIVE4)
6572 			rfs4_disable_delegation();
6573 
6574 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
6575 		    oo->client->clientid);
6576 	}
6577 
6578 	if (resp->status == NFS4_OK) {
6579 
6580 		/* cs->vp cs->fh now reference the desired file */
6581 
6582 		rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access,
6583 		    args->share_deny, resp, 0);
6584 
6585 		/*
6586 		 * If rfs4_createfile set attrset, we must
6587 		 * clear this attrset before the response is copied.
6588 		 */
6589 		if (resp->status != NFS4_OK && resp->attrset) {
6590 			resp->attrset = 0;
6591 		}
6592 	}
6593 	else
6594 		*cs->statusp = resp->status;
6595 
6596 	if (args->mode == EXCLUSIVE4)
6597 		rfs4_enable_delegation();
6598 }
6599 
6600 /*ARGSUSED*/
6601 static void
6602 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
6603 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6604 {
6605 	change_info4 *cinfo = &resp->cinfo;
6606 	vattr_t va;
6607 	vtype_t v_type = cs->vp->v_type;
6608 	int error = 0;
6609 
6610 	/* Verify that we have a regular file */
6611 	if (v_type != VREG) {
6612 		if (v_type == VDIR)
6613 			resp->status = NFS4ERR_ISDIR;
6614 		else if (v_type == VLNK)
6615 			resp->status = NFS4ERR_SYMLINK;
6616 		else
6617 			resp->status = NFS4ERR_INVAL;
6618 		return;
6619 	}
6620 
6621 	va.va_mask = AT_MODE|AT_UID;
6622 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
6623 	if (error) {
6624 		resp->status = puterrno4(error);
6625 		return;
6626 	}
6627 
6628 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
6629 
6630 	/*
6631 	 * Check if we have access to the file, Note the the file
6632 	 * could have originally been open UNCHECKED or GUARDED
6633 	 * with mode bits that will now fail, but there is nothing
6634 	 * we can really do about that except in the case that the
6635 	 * owner of the file is the one requesting the open.
6636 	 */
6637 	if (crgetuid(cs->cr) != va.va_uid) {
6638 		resp->status = check_open_access(args->share_access, cs, req);
6639 		if (resp->status != NFS4_OK) {
6640 			return;
6641 		}
6642 	}
6643 
6644 	/*
6645 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
6646 	 */
6647 	cinfo->before = 0;
6648 	cinfo->after = 0;
6649 	cinfo->atomic = FALSE;
6650 
6651 	rfs4_do_open(cs, req, oo,
6652 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
6653 	    args->share_access, args->share_deny, resp, 0);
6654 }
6655 
6656 static void
6657 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
6658 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6659 {
6660 	int error;
6661 	nfsstat4 status;
6662 	stateid4 stateid =
6663 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
6664 	rfs4_deleg_state_t *dsp;
6665 
6666 	/*
6667 	 * Find the state info from the stateid and confirm that the
6668 	 * file is delegated.  If the state openowner is the same as
6669 	 * the supplied openowner we're done. If not, get the file
6670 	 * info from the found state info. Use that file info to
6671 	 * create the state for this lock owner. Note solaris doen't
6672 	 * really need the pathname to find the file. We may want to
6673 	 * lookup the pathname and make sure that the vp exist and
6674 	 * matches the vp in the file structure. However it is
6675 	 * possible that the pathname nolonger exists (local process
6676 	 * unlinks the file), so this may not be that useful.
6677 	 */
6678 
6679 	status = rfs4_get_deleg_state(&stateid, &dsp);
6680 	if (status != NFS4_OK) {
6681 		resp->status = status;
6682 		return;
6683 	}
6684 
6685 	ASSERT(dsp->finfo->dinfo->dtype != OPEN_DELEGATE_NONE);
6686 
6687 	/*
6688 	 * New lock owner, create state. Since this was probably called
6689 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
6690 	 */
6691 
6692 	ASSERT(cs->vp != NULL);
6693 	VN_RELE(cs->vp);
6694 	VN_HOLD(dsp->finfo->vp);
6695 	cs->vp = dsp->finfo->vp;
6696 
6697 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
6698 		rfs4_deleg_state_rele(dsp);
6699 		*cs->statusp = resp->status = puterrno4(error);
6700 		return;
6701 	}
6702 
6703 	/* Mark progress for delegation returns */
6704 	dsp->finfo->dinfo->time_lastwrite = gethrestime_sec();
6705 	rfs4_deleg_state_rele(dsp);
6706 	rfs4_do_open(cs, req, oo, DELEG_NONE,
6707 	    args->share_access, args->share_deny, resp, 1);
6708 }
6709 
6710 /*ARGSUSED*/
6711 static void
6712 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
6713 			OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6714 {
6715 	/*
6716 	 * Lookup the pathname, it must already exist since this file
6717 	 * was delegated.
6718 	 *
6719 	 * Find the file and state info for this vp and open owner pair.
6720 	 *	check that they are in fact delegated.
6721 	 *	check that the state access and deny modes are the same.
6722 	 *
6723 	 * Return the delgation possibly seting the recall flag.
6724 	 */
6725 	rfs4_file_t *file;
6726 	rfs4_state_t *state;
6727 	bool_t create = FALSE;
6728 	bool_t dcreate = FALSE;
6729 	rfs4_deleg_state_t *dsp;
6730 	nfsace4 *ace;
6731 
6732 
6733 	/* Note we ignore oflags */
6734 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
6735 	    req, cs, args->share_access, &resp->cinfo);
6736 
6737 	if (resp->status != NFS4_OK) {
6738 		return;
6739 	}
6740 
6741 	/* get the file struct and hold a lock on it during initial open */
6742 	file = rfs4_findfile_withlock(cs->vp, NULL, &create);
6743 	if (file == NULL) {
6744 		NFS4_DEBUG(rfs4_debug,
6745 		    (CE_NOTE, "rfs4_do_opendelprev: can't find file"));
6746 		resp->status = NFS4ERR_SERVERFAULT;
6747 		return;
6748 	}
6749 
6750 	state = rfs4_findstate_by_owner_file(oo, file, &create);
6751 	if (state == NULL) {
6752 		NFS4_DEBUG(rfs4_debug,
6753 		    (CE_NOTE, "rfs4_do_opendelprev: can't find state"));
6754 		resp->status = NFS4ERR_SERVERFAULT;
6755 		rfs4_file_rele_withunlock(file);
6756 		return;
6757 	}
6758 
6759 	rfs4_dbe_lock(state->dbe);
6760 	rfs4_dbe_lock(file->dbe);
6761 	if (args->share_access != state->share_access ||
6762 	    args->share_deny != state->share_deny ||
6763 	    state->finfo->dinfo->dtype == OPEN_DELEGATE_NONE) {
6764 		NFS4_DEBUG(rfs4_debug,
6765 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
6766 		rfs4_dbe_unlock(file->dbe);
6767 		rfs4_dbe_unlock(state->dbe);
6768 		rfs4_file_rele(file);
6769 		rfs4_state_rele(state);
6770 		resp->status = NFS4ERR_SERVERFAULT;
6771 		return;
6772 	}
6773 	rfs4_dbe_unlock(file->dbe);
6774 	rfs4_dbe_unlock(state->dbe);
6775 
6776 	dsp = rfs4_finddeleg(state, &dcreate);
6777 	if (dsp == NULL) {
6778 		rfs4_state_rele(state);
6779 		rfs4_file_rele(file);
6780 		resp->status = NFS4ERR_SERVERFAULT;
6781 		return;
6782 	}
6783 
6784 	next_stateid(&state->stateid);
6785 
6786 	resp->stateid = state->stateid.stateid;
6787 
6788 	resp->delegation.delegation_type = dsp->dtype;
6789 
6790 	if (dsp->dtype == OPEN_DELEGATE_READ) {
6791 		open_read_delegation4 *rv =
6792 		    &resp->delegation.open_delegation4_u.read;
6793 
6794 		rv->stateid = dsp->delegid.stateid;
6795 		rv->recall = FALSE; /* no policy in place to set to TRUE */
6796 		ace = &rv->permissions;
6797 	} else {
6798 		open_write_delegation4 *rv =
6799 		    &resp->delegation.open_delegation4_u.write;
6800 
6801 		rv->stateid = dsp->delegid.stateid;
6802 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
6803 		ace = &rv->permissions;
6804 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
6805 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
6806 	}
6807 
6808 	/* XXX For now */
6809 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
6810 	ace->flag = 0;
6811 	ace->access_mask = 0;
6812 	ace->who.utf8string_len = 0;
6813 	ace->who.utf8string_val = 0;
6814 
6815 	rfs4_deleg_state_rele(dsp);
6816 	rfs4_state_rele(state);
6817 	rfs4_file_rele(file);
6818 }
6819 
6820 typedef enum {
6821 	NFS4_CHKSEQ_OKAY = 0,
6822 	NFS4_CHKSEQ_REPLAY = 1,
6823 	NFS4_CHKSEQ_BAD = 2
6824 } rfs4_chkseq_t;
6825 
6826 /*
6827  * Generic function for sequence number checks.
6828  */
6829 static rfs4_chkseq_t
6830 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
6831 		seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
6832 {
6833 	/* Same sequence ids and matching operations? */
6834 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
6835 		if (copyres == TRUE) {
6836 			rfs4_free_reply(resop);
6837 			rfs4_copy_reply(resop, lastop);
6838 		}
6839 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6840 		    "Replayed SEQID %d\n", seqid));
6841 		return (NFS4_CHKSEQ_REPLAY);
6842 	}
6843 
6844 	/* If the incoming sequence is not the next expected then it is bad */
6845 	if (rqst_seq != seqid + 1) {
6846 		if (rqst_seq == seqid) {
6847 			NFS4_DEBUG(rfs4_debug,
6848 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
6849 			    "but last op was %d current op is %d\n",
6850 			    lastop->resop, resop->resop));
6851 			return (NFS4_CHKSEQ_BAD);
6852 		}
6853 		NFS4_DEBUG(rfs4_debug,
6854 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
6855 		    rqst_seq, seqid));
6856 		return (NFS4_CHKSEQ_BAD);
6857 	}
6858 
6859 	/* Everything okay -- next expected */
6860 	return (NFS4_CHKSEQ_OKAY);
6861 }
6862 
6863 
6864 static rfs4_chkseq_t
6865 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
6866 {
6867 	rfs4_chkseq_t rc;
6868 
6869 	rfs4_dbe_lock(op->dbe);
6870 	rc = rfs4_check_seqid(op->open_seqid, op->reply, seqid, resop, TRUE);
6871 	rfs4_dbe_unlock(op->dbe);
6872 
6873 	if (rc == NFS4_CHKSEQ_OKAY)
6874 		rfs4_update_lease(op->client);
6875 
6876 	return (rc);
6877 }
6878 
6879 static rfs4_chkseq_t
6880 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op,
6881 	nfs_resop4 *resop)
6882 {
6883 	rfs4_chkseq_t rc;
6884 
6885 	rfs4_dbe_lock(op->dbe);
6886 	rc = rfs4_check_seqid(op->open_seqid, op->reply,
6887 	    olo_seqid, resop, FALSE);
6888 	rfs4_dbe_unlock(op->dbe);
6889 
6890 	return (rc);
6891 }
6892 
6893 static rfs4_chkseq_t
6894 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lp, nfs_resop4 *resop)
6895 {
6896 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
6897 
6898 	rfs4_dbe_lock(lp->dbe);
6899 	if (!lp->skip_seqid_check)
6900 		rc = rfs4_check_seqid(lp->seqid, lp->reply, seqid, resop, TRUE);
6901 	rfs4_dbe_unlock(lp->dbe);
6902 
6903 	return (rc);
6904 }
6905 
6906 static void
6907 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
6908 	    struct svc_req *req, struct compound_state *cs)
6909 {
6910 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
6911 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
6912 	open_owner4 *owner = &args->owner;
6913 	open_claim_type4 claim = args->claim;
6914 	rfs4_client_t *cp;
6915 	rfs4_openowner_t *oo;
6916 	bool_t create;
6917 	bool_t replay = FALSE;
6918 	int can_reclaim;
6919 
6920 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
6921 	    OPEN4args *, args);
6922 
6923 	if (cs->vp == NULL) {
6924 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
6925 		goto end;
6926 	}
6927 
6928 	/*
6929 	 * Need to check clientid and lease expiration first based on
6930 	 * error ordering and incrementing sequence id.
6931 	 */
6932 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
6933 	if (cp == NULL) {
6934 		*cs->statusp = resp->status =
6935 		    rfs4_check_clientid(&owner->clientid, 0);
6936 		goto end;
6937 	}
6938 
6939 	if (rfs4_lease_expired(cp)) {
6940 		rfs4_client_close(cp);
6941 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
6942 		goto end;
6943 	}
6944 	can_reclaim = cp->can_reclaim;
6945 
6946 	/*
6947 	 * Find the open_owner for use from this point forward.  Take
6948 	 * care in updating the sequence id based on the type of error
6949 	 * being returned.
6950 	 */
6951 retry:
6952 	create = TRUE;
6953 	oo = rfs4_findopenowner(owner, &create, args->seqid);
6954 	if (oo == NULL) {
6955 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
6956 		rfs4_client_rele(cp);
6957 		goto end;
6958 	}
6959 
6960 	/* Hold off access to the sequence space while the open is done */
6961 	rfs4_sw_enter(&oo->oo_sw);
6962 
6963 	/*
6964 	 * If the open_owner existed before at the server, then check
6965 	 * the sequence id.
6966 	 */
6967 	if (!create && !oo->postpone_confirm) {
6968 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
6969 		case NFS4_CHKSEQ_BAD:
6970 			if ((args->seqid > oo->open_seqid) &&
6971 			    oo->need_confirm) {
6972 				rfs4_free_opens(oo, TRUE, FALSE);
6973 				rfs4_sw_exit(&oo->oo_sw);
6974 				rfs4_openowner_rele(oo);
6975 				goto retry;
6976 			}
6977 			resp->status = NFS4ERR_BAD_SEQID;
6978 			goto out;
6979 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
6980 			replay = TRUE;
6981 			goto out;
6982 		default:
6983 			break;
6984 		}
6985 
6986 		/*
6987 		 * Sequence was ok and open owner exists
6988 		 * check to see if we have yet to see an
6989 		 * open_confirm.
6990 		 */
6991 		if (oo->need_confirm) {
6992 			rfs4_free_opens(oo, TRUE, FALSE);
6993 			rfs4_sw_exit(&oo->oo_sw);
6994 			rfs4_openowner_rele(oo);
6995 			goto retry;
6996 		}
6997 	}
6998 	/* Grace only applies to regular-type OPENs */
6999 	if (rfs4_clnt_in_grace(cp) &&
7000 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7001 		*cs->statusp = resp->status = NFS4ERR_GRACE;
7002 		goto out;
7003 	}
7004 
7005 	/*
7006 	 * If previous state at the server existed then can_reclaim
7007 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
7008 	 * client.
7009 	 */
7010 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7011 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7012 		goto out;
7013 	}
7014 
7015 
7016 	/*
7017 	 * Reject the open if the client has missed the grace period
7018 	 */
7019 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7020 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7021 		goto out;
7022 	}
7023 
7024 	/* Couple of up-front bookkeeping items */
7025 	if (oo->need_confirm) {
7026 		/*
7027 		 * If this is a reclaim OPEN then we should not ask
7028 		 * for a confirmation of the open_owner per the
7029 		 * protocol specification.
7030 		 */
7031 		if (claim == CLAIM_PREVIOUS)
7032 			oo->need_confirm = FALSE;
7033 		else
7034 			resp->rflags |= OPEN4_RESULT_CONFIRM;
7035 	}
7036 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7037 
7038 	/*
7039 	 * If there is an unshared filesystem mounted on this vnode,
7040 	 * do not allow to open/create in this directory.
7041 	 */
7042 	if (vn_ismntpt(cs->vp)) {
7043 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
7044 		goto out;
7045 	}
7046 
7047 	/*
7048 	 * access must READ, WRITE, or BOTH.  No access is invalid.
7049 	 * deny can be READ, WRITE, BOTH, or NONE.
7050 	 * bits not defined for access/deny are invalid.
7051 	 */
7052 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7053 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7054 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7055 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7056 		goto out;
7057 	}
7058 
7059 
7060 	/*
7061 	 * make sure attrset is zero before response is built.
7062 	 */
7063 	resp->attrset = 0;
7064 
7065 	switch (claim) {
7066 	case CLAIM_NULL:
7067 		rfs4_do_opennull(cs, req, args, oo, resp);
7068 		break;
7069 	case CLAIM_PREVIOUS:
7070 		rfs4_do_openprev(cs, req, args, oo, resp);
7071 		break;
7072 	case CLAIM_DELEGATE_CUR:
7073 		rfs4_do_opendelcur(cs, req, args, oo, resp);
7074 		break;
7075 	case CLAIM_DELEGATE_PREV:
7076 		rfs4_do_opendelprev(cs, req, args, oo, resp);
7077 		break;
7078 	default:
7079 		resp->status = NFS4ERR_INVAL;
7080 		break;
7081 	}
7082 
7083 out:
7084 	rfs4_client_rele(cp);
7085 
7086 	/* Catch sequence id handling here to make it a little easier */
7087 	switch (resp->status) {
7088 	case NFS4ERR_BADXDR:
7089 	case NFS4ERR_BAD_SEQID:
7090 	case NFS4ERR_BAD_STATEID:
7091 	case NFS4ERR_NOFILEHANDLE:
7092 	case NFS4ERR_RESOURCE:
7093 	case NFS4ERR_STALE_CLIENTID:
7094 	case NFS4ERR_STALE_STATEID:
7095 		/*
7096 		 * The protocol states that if any of these errors are
7097 		 * being returned, the sequence id should not be
7098 		 * incremented.  Any other return requires an
7099 		 * increment.
7100 		 */
7101 		break;
7102 	default:
7103 		/* Always update the lease in this case */
7104 		rfs4_update_lease(oo->client);
7105 
7106 		/* Regular response - copy the result */
7107 		if (!replay)
7108 			rfs4_update_open_resp(oo, resop, &cs->fh);
7109 
7110 		/*
7111 		 * REPLAY case: Only if the previous response was OK
7112 		 * do we copy the filehandle.  If not OK, no
7113 		 * filehandle to copy.
7114 		 */
7115 		if (replay == TRUE &&
7116 		    resp->status == NFS4_OK &&
7117 		    oo->reply_fh.nfs_fh4_val) {
7118 			/*
7119 			 * If this is a replay, we must restore the
7120 			 * current filehandle/vp to that of what was
7121 			 * returned originally.  Try our best to do
7122 			 * it.
7123 			 */
7124 			nfs_fh4_fmt_t *fh_fmtp =
7125 			    (nfs_fh4_fmt_t *)oo->reply_fh.nfs_fh4_val;
7126 
7127 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7128 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7129 
7130 			if (cs->exi == NULL) {
7131 				resp->status = NFS4ERR_STALE;
7132 				goto finish;
7133 			}
7134 
7135 			VN_RELE(cs->vp);
7136 
7137 			cs->vp = nfs4_fhtovp(&oo->reply_fh, cs->exi,
7138 			    &resp->status);
7139 
7140 			if (cs->vp == NULL)
7141 				goto finish;
7142 
7143 			nfs_fh4_copy(&oo->reply_fh, &cs->fh);
7144 		}
7145 
7146 		/*
7147 		 * If this was a replay, no need to update the
7148 		 * sequence id. If the open_owner was not created on
7149 		 * this pass, then update.  The first use of an
7150 		 * open_owner will not bump the sequence id.
7151 		 */
7152 		if (replay == FALSE && !create)
7153 			rfs4_update_open_sequence(oo);
7154 		/*
7155 		 * If the client is receiving an error and the
7156 		 * open_owner needs to be confirmed, there is no way
7157 		 * to notify the client of this fact ignoring the fact
7158 		 * that the server has no method of returning a
7159 		 * stateid to confirm.  Therefore, the server needs to
7160 		 * mark this open_owner in a way as to avoid the
7161 		 * sequence id checking the next time the client uses
7162 		 * this open_owner.
7163 		 */
7164 		if (resp->status != NFS4_OK && oo->need_confirm)
7165 			oo->postpone_confirm = TRUE;
7166 		/*
7167 		 * If OK response then clear the postpone flag and
7168 		 * reset the sequence id to keep in sync with the
7169 		 * client.
7170 		 */
7171 		if (resp->status == NFS4_OK && oo->postpone_confirm) {
7172 			oo->postpone_confirm = FALSE;
7173 			oo->open_seqid = args->seqid;
7174 		}
7175 		break;
7176 	}
7177 
7178 finish:
7179 	*cs->statusp = resp->status;
7180 
7181 	rfs4_sw_exit(&oo->oo_sw);
7182 	rfs4_openowner_rele(oo);
7183 
7184 end:
7185 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7186 	    OPEN4res *, resp);
7187 }
7188 
7189 /*ARGSUSED*/
7190 void
7191 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7192 		    struct svc_req *req, struct compound_state *cs)
7193 {
7194 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7195 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7196 	rfs4_state_t *sp;
7197 	nfsstat4 status;
7198 
7199 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7200 	    OPEN_CONFIRM4args *, args);
7201 
7202 	if (cs->vp == NULL) {
7203 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7204 		goto out;
7205 	}
7206 
7207 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7208 	if (status != NFS4_OK) {
7209 		*cs->statusp = resp->status = status;
7210 		goto out;
7211 	}
7212 
7213 	/* Ensure specified filehandle matches */
7214 	if (cs->vp != sp->finfo->vp) {
7215 		rfs4_state_rele(sp);
7216 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7217 		goto out;
7218 	}
7219 
7220 	/* hold off other access to open_owner while we tinker */
7221 	rfs4_sw_enter(&sp->owner->oo_sw);
7222 
7223 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7224 	case NFS4_CHECK_STATEID_OKAY:
7225 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7226 		    resop) != 0) {
7227 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7228 			break;
7229 		}
7230 		/*
7231 		 * If it is the appropriate stateid and determined to
7232 		 * be "OKAY" then this means that the stateid does not
7233 		 * need to be confirmed and the client is in error for
7234 		 * sending an OPEN_CONFIRM.
7235 		 */
7236 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7237 		break;
7238 	case NFS4_CHECK_STATEID_OLD:
7239 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7240 		break;
7241 	case NFS4_CHECK_STATEID_BAD:
7242 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7243 		break;
7244 	case NFS4_CHECK_STATEID_EXPIRED:
7245 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7246 		break;
7247 	case NFS4_CHECK_STATEID_CLOSED:
7248 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7249 		break;
7250 	case NFS4_CHECK_STATEID_REPLAY:
7251 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7252 		case NFS4_CHKSEQ_OKAY:
7253 			/*
7254 			 * This is replayed stateid; if seqid matches
7255 			 * next expected, then client is using wrong seqid.
7256 			 */
7257 			/* fall through */
7258 		case NFS4_CHKSEQ_BAD:
7259 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7260 			break;
7261 		case NFS4_CHKSEQ_REPLAY:
7262 			/*
7263 			 * Note this case is the duplicate case so
7264 			 * resp->status is already set.
7265 			 */
7266 			*cs->statusp = resp->status;
7267 			rfs4_update_lease(sp->owner->client);
7268 			break;
7269 		}
7270 		break;
7271 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7272 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7273 		    resop) != NFS4_CHKSEQ_OKAY) {
7274 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7275 			break;
7276 		}
7277 		*cs->statusp = resp->status = NFS4_OK;
7278 
7279 		next_stateid(&sp->stateid);
7280 		resp->open_stateid = sp->stateid.stateid;
7281 		sp->owner->need_confirm = FALSE;
7282 		rfs4_update_lease(sp->owner->client);
7283 		rfs4_update_open_sequence(sp->owner);
7284 		rfs4_update_open_resp(sp->owner, resop, NULL);
7285 		break;
7286 	default:
7287 		ASSERT(FALSE);
7288 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7289 		break;
7290 	}
7291 	rfs4_sw_exit(&sp->owner->oo_sw);
7292 	rfs4_state_rele(sp);
7293 
7294 out:
7295 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7296 	    OPEN_CONFIRM4res *, resp);
7297 }
7298 
7299 /*ARGSUSED*/
7300 void
7301 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7302 		    struct svc_req *req, struct compound_state *cs)
7303 {
7304 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7305 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7306 	uint32_t access = args->share_access;
7307 	uint32_t deny = args->share_deny;
7308 	nfsstat4 status;
7309 	rfs4_state_t *sp;
7310 	rfs4_file_t *fp;
7311 	int fflags = 0;
7312 
7313 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7314 	    OPEN_DOWNGRADE4args *, args);
7315 
7316 	if (cs->vp == NULL) {
7317 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7318 		goto out;
7319 	}
7320 
7321 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7322 	if (status != NFS4_OK) {
7323 		*cs->statusp = resp->status = status;
7324 		goto out;
7325 	}
7326 
7327 	/* Ensure specified filehandle matches */
7328 	if (cs->vp != sp->finfo->vp) {
7329 		rfs4_state_rele(sp);
7330 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7331 		goto out;
7332 	}
7333 
7334 	/* hold off other access to open_owner while we tinker */
7335 	rfs4_sw_enter(&sp->owner->oo_sw);
7336 
7337 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7338 	case NFS4_CHECK_STATEID_OKAY:
7339 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7340 		    resop) != NFS4_CHKSEQ_OKAY) {
7341 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7342 			goto end;
7343 		}
7344 		break;
7345 	case NFS4_CHECK_STATEID_OLD:
7346 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7347 		goto end;
7348 	case NFS4_CHECK_STATEID_BAD:
7349 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7350 		goto end;
7351 	case NFS4_CHECK_STATEID_EXPIRED:
7352 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7353 		goto end;
7354 	case NFS4_CHECK_STATEID_CLOSED:
7355 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7356 		goto end;
7357 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7358 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7359 		goto end;
7360 	case NFS4_CHECK_STATEID_REPLAY:
7361 		/* Check the sequence id for the open owner */
7362 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7363 		case NFS4_CHKSEQ_OKAY:
7364 			/*
7365 			 * This is replayed stateid; if seqid matches
7366 			 * next expected, then client is using wrong seqid.
7367 			 */
7368 			/* fall through */
7369 		case NFS4_CHKSEQ_BAD:
7370 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7371 			goto end;
7372 		case NFS4_CHKSEQ_REPLAY:
7373 			/*
7374 			 * Note this case is the duplicate case so
7375 			 * resp->status is already set.
7376 			 */
7377 			*cs->statusp = resp->status;
7378 			rfs4_update_lease(sp->owner->client);
7379 			goto end;
7380 		}
7381 		break;
7382 	default:
7383 		ASSERT(FALSE);
7384 		break;
7385 	}
7386 
7387 	rfs4_dbe_lock(sp->dbe);
7388 	/*
7389 	 * Check that the new access modes and deny modes are valid.
7390 	 * Check that no invalid bits are set.
7391 	 */
7392 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7393 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7394 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7395 		rfs4_update_open_sequence(sp->owner);
7396 		rfs4_dbe_unlock(sp->dbe);
7397 		goto end;
7398 	}
7399 
7400 	/*
7401 	 * The new modes must be a subset of the current modes and
7402 	 * the access must specify at least one mode. To test that
7403 	 * the new mode is a subset of the current modes we bitwise
7404 	 * AND them together and check that the result equals the new
7405 	 * mode. For example:
7406 	 * New mode, access == R and current mode, sp->share_access  == RW
7407 	 * access & sp->share_access == R == access, so the new access mode
7408 	 * is valid. Consider access == RW, sp->share_access = R
7409 	 * access & sp->share_access == R != access, so the new access mode
7410 	 * is invalid.
7411 	 */
7412 	if ((access & sp->share_access) != access ||
7413 	    (deny & sp->share_deny) != deny ||
7414 	    (access &
7415 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7416 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7417 		rfs4_update_open_sequence(sp->owner);
7418 		rfs4_dbe_unlock(sp->dbe);
7419 		goto end;
7420 	}
7421 
7422 	/*
7423 	 * Release any share locks associated with this stateID.
7424 	 * Strictly speaking, this violates the spec because the
7425 	 * spec effectively requires that open downgrade be atomic.
7426 	 * At present, fs_shrlock does not have this capability.
7427 	 */
7428 	rfs4_dbe_unlock(sp->dbe);
7429 	rfs4_unshare(sp);
7430 	rfs4_dbe_lock(sp->dbe);
7431 
7432 	fp = sp->finfo;
7433 	rfs4_dbe_lock(fp->dbe);
7434 
7435 	/*
7436 	 * If the current mode has deny read and the new mode
7437 	 * does not, decrement the number of deny read mode bits
7438 	 * and if it goes to zero turn off the deny read bit
7439 	 * on the file.
7440 	 */
7441 	if ((sp->share_deny & OPEN4_SHARE_DENY_READ) &&
7442 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7443 		fp->deny_read--;
7444 		if (fp->deny_read == 0)
7445 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
7446 	}
7447 
7448 	/*
7449 	 * If the current mode has deny write and the new mode
7450 	 * does not, decrement the number of deny write mode bits
7451 	 * and if it goes to zero turn off the deny write bit
7452 	 * on the file.
7453 	 */
7454 	if ((sp->share_deny & OPEN4_SHARE_DENY_WRITE) &&
7455 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7456 		fp->deny_write--;
7457 		if (fp->deny_write == 0)
7458 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7459 	}
7460 
7461 	/*
7462 	 * If the current mode has access read and the new mode
7463 	 * does not, decrement the number of access read mode bits
7464 	 * and if it goes to zero turn off the access read bit
7465 	 * on the file.  set fflags to FREAD for the call to
7466 	 * vn_open_downgrade().
7467 	 */
7468 	if ((sp->share_access & OPEN4_SHARE_ACCESS_READ) &&
7469 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7470 		fp->access_read--;
7471 		if (fp->access_read == 0)
7472 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
7473 		fflags |= FREAD;
7474 	}
7475 
7476 	/*
7477 	 * If the current mode has access write and the new mode
7478 	 * does not, decrement the number of access write mode bits
7479 	 * and if it goes to zero turn off the access write bit
7480 	 * on the file.  set fflags to FWRITE for the call to
7481 	 * vn_open_downgrade().
7482 	 */
7483 	if ((sp->share_access & OPEN4_SHARE_ACCESS_WRITE) &&
7484 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7485 		fp->access_write--;
7486 		if (fp->access_write == 0)
7487 			fp->share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7488 		fflags |= FWRITE;
7489 	}
7490 
7491 	/* Set the new access and deny modes */
7492 	sp->share_access = access;
7493 	sp->share_deny = deny;
7494 	/* Check that the file is still accessible */
7495 	ASSERT(fp->share_access);
7496 
7497 	rfs4_dbe_unlock(fp->dbe);
7498 
7499 	rfs4_dbe_unlock(sp->dbe);
7500 
7501 	if ((status = rfs4_share(sp)) != NFS4_OK) {
7502 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7503 		rfs4_update_open_sequence(sp->owner);
7504 		goto end;
7505 	}
7506 
7507 	/*
7508 	 * we successfully downgraded the share lock, now we need to downgrade
7509 	 * the open.  it is possible that the downgrade was only for a deny
7510 	 * mode and we have nothing else to do.
7511 	 */
7512 	if ((fflags & (FREAD|FWRITE)) != 0)
7513 		vn_open_downgrade(cs->vp, fflags);
7514 
7515 	rfs4_dbe_lock(sp->dbe);
7516 
7517 	/* Update the stateid */
7518 	next_stateid(&sp->stateid);
7519 	resp->open_stateid = sp->stateid.stateid;
7520 
7521 	rfs4_dbe_unlock(sp->dbe);
7522 
7523 	*cs->statusp = resp->status = NFS4_OK;
7524 	/* Update the lease */
7525 	rfs4_update_lease(sp->owner->client);
7526 	/* And the sequence */
7527 	rfs4_update_open_sequence(sp->owner);
7528 	rfs4_update_open_resp(sp->owner, resop, NULL);
7529 
7530 end:
7531 	rfs4_sw_exit(&sp->owner->oo_sw);
7532 	rfs4_state_rele(sp);
7533 out:
7534 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7535 	    OPEN_DOWNGRADE4res *, resp);
7536 }
7537 
7538 /*
7539  * The logic behind this function is detailed in the NFSv4 RFC in the
7540  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7541  * that section for explicit guidance to server behavior for
7542  * SETCLIENTID.
7543  */
7544 void
7545 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
7546 		    struct svc_req *req, struct compound_state *cs)
7547 {
7548 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
7549 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
7550 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
7551 	bool_t create = TRUE;
7552 	char *addr, *netid;
7553 	int len;
7554 
7555 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
7556 	    SETCLIENTID4args *, args);
7557 retry:
7558 	newcp = cp_confirmed = cp_unconfirmed = NULL;
7559 
7560 	/*
7561 	 * In search of an EXISTING client matching the incoming
7562 	 * request to establish a new client identifier at the server
7563 	 */
7564 	create = TRUE;
7565 	cp = rfs4_findclient(&args->client, &create, NULL);
7566 
7567 	/* Should never happen */
7568 	ASSERT(cp != NULL);
7569 
7570 	if (cp == NULL) {
7571 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7572 		goto out;
7573 	}
7574 
7575 	/*
7576 	 * Easiest case. Client identifier is newly created and is
7577 	 * unconfirmed.  Also note that for this case, no other
7578 	 * entries exist for the client identifier.  Nothing else to
7579 	 * check.  Just setup the response and respond.
7580 	 */
7581 	if (create) {
7582 		*cs->statusp = res->status = NFS4_OK;
7583 		res->SETCLIENTID4res_u.resok4.clientid = cp->clientid;
7584 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7585 		    cp->confirm_verf;
7586 		/* Setup callback information; CB_NULL confirmation later */
7587 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
7588 
7589 		rfs4_client_rele(cp);
7590 		goto out;
7591 	}
7592 
7593 	/*
7594 	 * An existing, confirmed client may exist but it may not have
7595 	 * been active for at least one lease period.  If so, then
7596 	 * "close" the client and create a new client identifier
7597 	 */
7598 	if (rfs4_lease_expired(cp)) {
7599 		rfs4_client_close(cp);
7600 		goto retry;
7601 	}
7602 
7603 	if (cp->need_confirm == TRUE)
7604 		cp_unconfirmed = cp;
7605 	else
7606 		cp_confirmed = cp;
7607 
7608 	cp = NULL;
7609 
7610 	/*
7611 	 * We have a confirmed client, now check for an
7612 	 * unconfimred entry
7613 	 */
7614 	if (cp_confirmed) {
7615 		/* If creds don't match then client identifier is inuse */
7616 		if (!creds_ok(cp_confirmed->cr_set, req, cs)) {
7617 			rfs4_cbinfo_t *cbp;
7618 			/*
7619 			 * Some one else has established this client
7620 			 * id. Try and say * who they are. We will use
7621 			 * the call back address supplied by * the
7622 			 * first client.
7623 			 */
7624 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7625 
7626 			addr = netid = NULL;
7627 
7628 			cbp = &cp_confirmed->cbinfo;
7629 			if (cbp->cb_callback.cb_location.r_addr &&
7630 			    cbp->cb_callback.cb_location.r_netid) {
7631 				cb_client4 *cbcp = &cbp->cb_callback;
7632 
7633 				len = strlen(cbcp->cb_location.r_addr)+1;
7634 				addr = kmem_alloc(len, KM_SLEEP);
7635 				bcopy(cbcp->cb_location.r_addr, addr, len);
7636 				len = strlen(cbcp->cb_location.r_netid)+1;
7637 				netid = kmem_alloc(len, KM_SLEEP);
7638 				bcopy(cbcp->cb_location.r_netid, netid, len);
7639 			}
7640 
7641 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
7642 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
7643 
7644 			rfs4_client_rele(cp_confirmed);
7645 		}
7646 
7647 		/*
7648 		 * Confirmed, creds match, and verifier matches; must
7649 		 * be an update of the callback info
7650 		 */
7651 		if (cp_confirmed->nfs_client.verifier ==
7652 		    args->client.verifier) {
7653 			/* Setup callback information */
7654 			rfs4_client_setcb(cp_confirmed, &args->callback,
7655 			    args->callback_ident);
7656 
7657 			/* everything okay -- move ahead */
7658 			*cs->statusp = res->status = NFS4_OK;
7659 			res->SETCLIENTID4res_u.resok4.clientid =
7660 			    cp_confirmed->clientid;
7661 
7662 			/* update the confirm_verifier and return it */
7663 			rfs4_client_scv_next(cp_confirmed);
7664 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7665 			    cp_confirmed->confirm_verf;
7666 
7667 			rfs4_client_rele(cp_confirmed);
7668 			goto out;
7669 		}
7670 
7671 		/*
7672 		 * Creds match but the verifier doesn't.  Must search
7673 		 * for an unconfirmed client that would be replaced by
7674 		 * this request.
7675 		 */
7676 		create = FALSE;
7677 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
7678 		    cp_confirmed);
7679 	}
7680 
7681 	/*
7682 	 * At this point, we have taken care of the brand new client
7683 	 * struct, INUSE case, update of an existing, and confirmed
7684 	 * client struct.
7685 	 */
7686 
7687 	/*
7688 	 * check to see if things have changed while we originally
7689 	 * picked up the client struct.  If they have, then return and
7690 	 * retry the processing of this SETCLIENTID request.
7691 	 */
7692 	if (cp_unconfirmed) {
7693 		rfs4_dbe_lock(cp_unconfirmed->dbe);
7694 		if (!cp_unconfirmed->need_confirm) {
7695 			rfs4_dbe_unlock(cp_unconfirmed->dbe);
7696 			rfs4_client_rele(cp_unconfirmed);
7697 			if (cp_confirmed)
7698 				rfs4_client_rele(cp_confirmed);
7699 			goto retry;
7700 		}
7701 		/* do away with the old unconfirmed one */
7702 		rfs4_dbe_invalidate(cp_unconfirmed->dbe);
7703 		rfs4_dbe_unlock(cp_unconfirmed->dbe);
7704 		rfs4_client_rele(cp_unconfirmed);
7705 		cp_unconfirmed = NULL;
7706 	}
7707 
7708 	/*
7709 	 * This search will temporarily hide the confirmed client
7710 	 * struct while a new client struct is created as the
7711 	 * unconfirmed one.
7712 	 */
7713 	create = TRUE;
7714 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
7715 
7716 	ASSERT(newcp != NULL);
7717 
7718 	if (newcp == NULL) {
7719 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7720 		rfs4_client_rele(cp_confirmed);
7721 		goto out;
7722 	}
7723 
7724 	/*
7725 	 * If one was not created, then a similar request must be in
7726 	 * process so release and start over with this one
7727 	 */
7728 	if (create != TRUE) {
7729 		rfs4_client_rele(newcp);
7730 		if (cp_confirmed)
7731 			rfs4_client_rele(cp_confirmed);
7732 		goto retry;
7733 	}
7734 
7735 	*cs->statusp = res->status = NFS4_OK;
7736 	res->SETCLIENTID4res_u.resok4.clientid = newcp->clientid;
7737 	res->SETCLIENTID4res_u.resok4.setclientid_confirm = newcp->confirm_verf;
7738 	/* Setup callback information; CB_NULL confirmation later */
7739 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
7740 
7741 	newcp->cp_confirmed = cp_confirmed;
7742 
7743 	rfs4_client_rele(newcp);
7744 
7745 out:
7746 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
7747 	    SETCLIENTID4res *, res);
7748 }
7749 
7750 /*ARGSUSED*/
7751 void
7752 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7753 			    struct svc_req *req, struct compound_state *cs)
7754 {
7755 	SETCLIENTID_CONFIRM4args *args =
7756 	    &argop->nfs_argop4_u.opsetclientid_confirm;
7757 	SETCLIENTID_CONFIRM4res *res =
7758 	    &resop->nfs_resop4_u.opsetclientid_confirm;
7759 	rfs4_client_t *cp, *cptoclose = NULL;
7760 
7761 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
7762 	    struct compound_state *, cs,
7763 	    SETCLIENTID_CONFIRM4args *, args);
7764 
7765 	*cs->statusp = res->status = NFS4_OK;
7766 
7767 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
7768 
7769 	if (cp == NULL) {
7770 		*cs->statusp = res->status =
7771 		    rfs4_check_clientid(&args->clientid, 1);
7772 		goto out;
7773 	}
7774 
7775 	if (!creds_ok(cp, req, cs)) {
7776 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7777 		rfs4_client_rele(cp);
7778 		goto out;
7779 	}
7780 
7781 	/* If the verifier doesn't match, the record doesn't match */
7782 	if (cp->confirm_verf != args->setclientid_confirm) {
7783 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
7784 		rfs4_client_rele(cp);
7785 		goto out;
7786 	}
7787 
7788 	rfs4_dbe_lock(cp->dbe);
7789 	cp->need_confirm = FALSE;
7790 	if (cp->cp_confirmed) {
7791 		cptoclose = cp->cp_confirmed;
7792 		cptoclose->ss_remove = 1;
7793 		cp->cp_confirmed = NULL;
7794 	}
7795 
7796 	/*
7797 	 * Update the client's associated server instance, if it's changed
7798 	 * since the client was created.
7799 	 */
7800 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
7801 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
7802 
7803 	/*
7804 	 * Record clientid in stable storage.
7805 	 * Must be done after server instance has been assigned.
7806 	 */
7807 	rfs4_ss_clid(cp, req);
7808 
7809 	rfs4_dbe_unlock(cp->dbe);
7810 
7811 	if (cptoclose)
7812 		/* don't need to rele, client_close does it */
7813 		rfs4_client_close(cptoclose);
7814 
7815 	/* If needed, initiate CB_NULL call for callback path */
7816 	rfs4_deleg_cb_check(cp);
7817 	rfs4_update_lease(cp);
7818 
7819 	/*
7820 	 * Check to see if client can perform reclaims
7821 	 */
7822 	rfs4_ss_chkclid(cp);
7823 
7824 	rfs4_client_rele(cp);
7825 
7826 out:
7827 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
7828 	    struct compound_state *, cs,
7829 	    SETCLIENTID_CONFIRM4 *, res);
7830 }
7831 
7832 
7833 /*ARGSUSED*/
7834 void
7835 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
7836 	    struct svc_req *req, struct compound_state *cs)
7837 {
7838 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
7839 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
7840 	rfs4_state_t *sp;
7841 	nfsstat4 status;
7842 
7843 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
7844 	    CLOSE4args *, args);
7845 
7846 	if (cs->vp == NULL) {
7847 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7848 		goto out;
7849 	}
7850 
7851 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
7852 	if (status != NFS4_OK) {
7853 		*cs->statusp = resp->status = status;
7854 		goto out;
7855 	}
7856 
7857 	/* Ensure specified filehandle matches */
7858 	if (cs->vp != sp->finfo->vp) {
7859 		rfs4_state_rele(sp);
7860 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7861 		goto out;
7862 	}
7863 
7864 	/* hold off other access to open_owner while we tinker */
7865 	rfs4_sw_enter(&sp->owner->oo_sw);
7866 
7867 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7868 	case NFS4_CHECK_STATEID_OKAY:
7869 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7870 		    resop) != NFS4_CHKSEQ_OKAY) {
7871 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7872 			goto end;
7873 		}
7874 		break;
7875 	case NFS4_CHECK_STATEID_OLD:
7876 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7877 		goto end;
7878 	case NFS4_CHECK_STATEID_BAD:
7879 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7880 		goto end;
7881 	case NFS4_CHECK_STATEID_EXPIRED:
7882 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7883 		goto end;
7884 	case NFS4_CHECK_STATEID_CLOSED:
7885 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7886 		goto end;
7887 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7888 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7889 		goto end;
7890 	case NFS4_CHECK_STATEID_REPLAY:
7891 		/* Check the sequence id for the open owner */
7892 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7893 		case NFS4_CHKSEQ_OKAY:
7894 			/*
7895 			 * This is replayed stateid; if seqid matches
7896 			 * next expected, then client is using wrong seqid.
7897 			 */
7898 			/* FALL THROUGH */
7899 		case NFS4_CHKSEQ_BAD:
7900 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7901 			goto end;
7902 		case NFS4_CHKSEQ_REPLAY:
7903 			/*
7904 			 * Note this case is the duplicate case so
7905 			 * resp->status is already set.
7906 			 */
7907 			*cs->statusp = resp->status;
7908 			rfs4_update_lease(sp->owner->client);
7909 			goto end;
7910 		}
7911 		break;
7912 	default:
7913 		ASSERT(FALSE);
7914 		break;
7915 	}
7916 
7917 	rfs4_dbe_lock(sp->dbe);
7918 
7919 	/* Update the stateid. */
7920 	next_stateid(&sp->stateid);
7921 	resp->open_stateid = sp->stateid.stateid;
7922 
7923 	rfs4_dbe_unlock(sp->dbe);
7924 
7925 	rfs4_update_lease(sp->owner->client);
7926 	rfs4_update_open_sequence(sp->owner);
7927 	rfs4_update_open_resp(sp->owner, resop, NULL);
7928 
7929 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
7930 
7931 	*cs->statusp = resp->status = status;
7932 
7933 end:
7934 	rfs4_sw_exit(&sp->owner->oo_sw);
7935 	rfs4_state_rele(sp);
7936 out:
7937 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
7938 	    CLOSE4res *, resp);
7939 }
7940 
7941 /*
7942  * Manage the counts on the file struct and close all file locks
7943  */
7944 /*ARGSUSED*/
7945 void
7946 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
7947 	bool_t close_of_client)
7948 {
7949 	rfs4_file_t *fp = sp->finfo;
7950 	rfs4_lo_state_t *lsp;
7951 	struct shrlock shr;
7952 	struct shr_locowner shr_loco;
7953 	int fflags, s_access, s_deny;
7954 
7955 	fflags = s_access = s_deny = 0;
7956 	/*
7957 	 * Decrement the count for each access and deny bit that this
7958 	 * state has contributed to the file. If the file counts go to zero
7959 	 * clear the appropriate bit in the appropriate mask.
7960 	 */
7961 
7962 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
7963 		fp->access_read--;
7964 		fflags |= FREAD;
7965 		s_access |= F_RDACC;
7966 		if (fp->access_read == 0)
7967 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
7968 	}
7969 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
7970 		fp->access_write--;
7971 		fflags |= FWRITE;
7972 		s_access |= F_WRACC;
7973 		if (fp->access_write == 0)
7974 			fp->share_access &= ~OPEN4_SHARE_ACCESS_WRITE;
7975 	}
7976 	if (sp->share_deny & OPEN4_SHARE_DENY_READ) {
7977 		fp->deny_read--;
7978 		s_deny |= F_RDDNY;
7979 		if (fp->deny_read == 0)
7980 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
7981 	}
7982 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) {
7983 		fp->deny_write--;
7984 		s_deny |= F_WRDNY;
7985 		if (fp->deny_write == 0)
7986 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7987 	}
7988 
7989 	/*
7990 	 * If this call is part of the larger closing down of client
7991 	 * state then it is just easier to release all locks
7992 	 * associated with this client instead of going through each
7993 	 * individual file and cleaning locks there.
7994 	 */
7995 	if (close_of_client) {
7996 		if (sp->owner->client->unlksys_completed == FALSE &&
7997 		    sp->lockownerlist.next->lsp != NULL &&
7998 		    sp->owner->client->sysidt != LM_NOSYSID) {
7999 			/* Is the PxFS kernel module loaded? */
8000 			if (lm_remove_file_locks != NULL) {
8001 				int new_sysid;
8002 
8003 				/* Encode the cluster nodeid in new sysid */
8004 				new_sysid = sp->owner->client->sysidt;
8005 				lm_set_nlmid_flk(&new_sysid);
8006 
8007 				/*
8008 				 * This PxFS routine removes file locks for a
8009 				 * client over all nodes of a cluster.
8010 				 */
8011 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8012 				    "lm_remove_file_locks(sysid=0x%x)\n",
8013 				    new_sysid));
8014 				(*lm_remove_file_locks)(new_sysid);
8015 			} else {
8016 				struct flock64 flk;
8017 
8018 				/* Release all locks for this client */
8019 				flk.l_type = F_UNLKSYS;
8020 				flk.l_whence = 0;
8021 				flk.l_start = 0;
8022 				flk.l_len = 0;
8023 				flk.l_sysid = sp->owner->client->sysidt;
8024 				flk.l_pid = 0;
8025 				(void) VOP_FRLOCK(sp->finfo->vp, F_SETLK, &flk,
8026 				    F_REMOTELOCK | FREAD | FWRITE,
8027 				    (u_offset_t)0, NULL, CRED(), NULL);
8028 			}
8029 
8030 			sp->owner->client->unlksys_completed = TRUE;
8031 		}
8032 	}
8033 
8034 	/*
8035 	 * Release all locks on this file by this lock owner or at
8036 	 * least mark the locks as having been released
8037 	 */
8038 	for (lsp = sp->lockownerlist.next->lsp; lsp != NULL;
8039 	    lsp = lsp->lockownerlist.next->lsp) {
8040 
8041 		lsp->locks_cleaned = TRUE;
8042 
8043 		/* Was this already taken care of above? */
8044 		if (!close_of_client &&
8045 		    sp->owner->client->sysidt != LM_NOSYSID)
8046 			(void) cleanlocks(sp->finfo->vp, lsp->locker->pid,
8047 			    lsp->locker->client->sysidt);
8048 	}
8049 
8050 	/*
8051 	 * Release any shrlocks associated with this open state ID.
8052 	 * This must be done before the rfs4_state gets marked closed.
8053 	 */
8054 	if (sp->owner->client->sysidt != LM_NOSYSID) {
8055 		shr.s_access = s_access;
8056 		shr.s_deny = s_deny;
8057 		shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
8058 		shr.s_sysid = sp->owner->client->sysidt;
8059 		shr_loco.sl_pid = shr.s_pid;
8060 		shr_loco.sl_id = shr.s_sysid;
8061 		shr.s_owner = (caddr_t)&shr_loco;
8062 		shr.s_own_len = sizeof (shr_loco);
8063 		(void) vop_shrlock(sp->finfo->vp, F_UNSHARE, &shr, fflags);
8064 	}
8065 
8066 	(void) VOP_CLOSE(fp->vp, fflags, 1, (offset_t)0, cr, NULL);
8067 }
8068 
8069 /*
8070  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8071  */
8072 static nfsstat4
8073 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8074 {
8075 	rfs4_lockowner_t *lo;
8076 	rfs4_client_t *cp;
8077 	uint32_t len;
8078 
8079 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
8080 	if (lo != NULL) {
8081 		cp = lo->client;
8082 		if (rfs4_lease_expired(cp)) {
8083 			rfs4_lockowner_rele(lo);
8084 			rfs4_dbe_hold(cp->dbe);
8085 			rfs4_client_close(cp);
8086 			return (NFS4ERR_EXPIRED);
8087 		}
8088 		dp->owner.clientid = lo->owner.clientid;
8089 		len = lo->owner.owner_len;
8090 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8091 		bcopy(lo->owner.owner_val, dp->owner.owner_val, len);
8092 		dp->owner.owner_len = len;
8093 		rfs4_lockowner_rele(lo);
8094 		goto finish;
8095 	}
8096 
8097 	/*
8098 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
8099 	 * of the client id contain the boot time for a NFS4 lock. So we
8100 	 * fabricate and identity by setting clientid to the sysid, and
8101 	 * the lock owner to the pid.
8102 	 */
8103 	dp->owner.clientid = flk->l_sysid;
8104 	len = sizeof (pid_t);
8105 	dp->owner.owner_len = len;
8106 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8107 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
8108 finish:
8109 	dp->offset = flk->l_start;
8110 	dp->length = flk->l_len;
8111 
8112 	if (flk->l_type == F_RDLCK)
8113 		dp->locktype = READ_LT;
8114 	else if (flk->l_type == F_WRLCK)
8115 		dp->locktype = WRITE_LT;
8116 	else
8117 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
8118 
8119 	return (NFS4_OK);
8120 }
8121 
8122 static int
8123 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8124 {
8125 	int error;
8126 	struct flock64 flk;
8127 	int i;
8128 	clock_t delaytime;
8129 	int cmd;
8130 
8131 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8132 retry:
8133 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8134 
8135 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8136 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8137 		error = VOP_FRLOCK(vp, cmd,
8138 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
8139 
8140 		if (error != EAGAIN && error != EACCES)
8141 			break;
8142 
8143 		if (i < rfs4_maxlock_tries - 1) {
8144 			delay(delaytime);
8145 			delaytime *= 2;
8146 		}
8147 	}
8148 
8149 	if (error == EAGAIN || error == EACCES) {
8150 		/* Get the owner of the lock */
8151 		flk = *flock;
8152 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8153 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag,
8154 		    (u_offset_t)0, NULL, cred, NULL) == 0) {
8155 			if (flk.l_type == F_UNLCK) {
8156 				/* No longer locked, retry */
8157 				goto retry;
8158 			}
8159 			*flock = flk;
8160 			LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8161 			    F_GETLK, &flk);
8162 		}
8163 	}
8164 
8165 	return (error);
8166 }
8167 
8168 /*ARGSUSED*/
8169 static nfsstat4
8170 rfs4_do_lock(rfs4_lo_state_t *lp, nfs_lock_type4 locktype,
8171 	    seqid4 seqid, offset4 offset,
8172 	    length4 length, cred_t *cred, nfs_resop4 *resop)
8173 {
8174 	nfsstat4 status;
8175 	rfs4_lockowner_t *lo = lp->locker;
8176 	rfs4_state_t *sp = lp->state;
8177 	struct flock64 flock;
8178 	int16_t ltype;
8179 	int flag;
8180 	int error;
8181 	sysid_t sysid;
8182 	LOCK4res *lres;
8183 
8184 	if (rfs4_lease_expired(lo->client)) {
8185 		return (NFS4ERR_EXPIRED);
8186 	}
8187 
8188 	if ((status = rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8189 		return (status);
8190 
8191 	/* Check for zero length. To lock to end of file use all ones for V4 */
8192 	if (length == 0)
8193 		return (NFS4ERR_INVAL);
8194 	else if (length == (length4)(~0))
8195 		length = 0;		/* Posix to end of file  */
8196 
8197 retry:
8198 	rfs4_dbe_lock(sp->dbe);
8199 
8200 
8201 	if (resop->resop != OP_LOCKU) {
8202 		switch (locktype) {
8203 		case READ_LT:
8204 		case READW_LT:
8205 			if ((sp->share_access
8206 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8207 				rfs4_dbe_unlock(sp->dbe);
8208 
8209 				return (NFS4ERR_OPENMODE);
8210 			}
8211 			ltype = F_RDLCK;
8212 			break;
8213 		case WRITE_LT:
8214 		case WRITEW_LT:
8215 			if ((sp->share_access
8216 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8217 				rfs4_dbe_unlock(sp->dbe);
8218 
8219 				return (NFS4ERR_OPENMODE);
8220 			}
8221 			ltype = F_WRLCK;
8222 			break;
8223 		}
8224 	} else
8225 		ltype = F_UNLCK;
8226 
8227 	flock.l_type = ltype;
8228 	flock.l_whence = 0;		/* SEEK_SET */
8229 	flock.l_start = offset;
8230 	flock.l_len = length;
8231 	flock.l_sysid = sysid;
8232 	flock.l_pid = lp->locker->pid;
8233 
8234 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8235 	if (flock.l_len < 0 || flock.l_start < 0) {
8236 		rfs4_dbe_unlock(sp->dbe);
8237 		return (NFS4ERR_INVAL);
8238 	}
8239 
8240 	/*
8241 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8242 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8243 	 */
8244 	flag = (int)sp->share_access | F_REMOTELOCK;
8245 
8246 	error = setlock(sp->finfo->vp, &flock, flag, cred);
8247 	if (error == 0) {
8248 		rfs4_dbe_lock(lp->dbe);
8249 		next_stateid(&lp->lockid);
8250 		rfs4_dbe_unlock(lp->dbe);
8251 	}
8252 
8253 	rfs4_dbe_unlock(sp->dbe);
8254 
8255 	/*
8256 	 * N.B. We map error values to nfsv4 errors. This is differrent
8257 	 * than puterrno4 routine.
8258 	 */
8259 	switch (error) {
8260 	case 0:
8261 		status = NFS4_OK;
8262 		break;
8263 	case EAGAIN:
8264 	case EACCES:		/* Old value */
8265 		/* Can only get here if op is OP_LOCK */
8266 		ASSERT(resop->resop == OP_LOCK);
8267 		lres = &resop->nfs_resop4_u.oplock;
8268 		status = NFS4ERR_DENIED;
8269 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8270 		    == NFS4ERR_EXPIRED)
8271 			goto retry;
8272 		break;
8273 	case ENOLCK:
8274 		status = NFS4ERR_DELAY;
8275 		break;
8276 	case EOVERFLOW:
8277 		status = NFS4ERR_INVAL;
8278 		break;
8279 	case EINVAL:
8280 		status = NFS4ERR_NOTSUPP;
8281 		break;
8282 	default:
8283 		cmn_err(CE_WARN, "rfs4_do_lock: unexpected errno (%d)", error);
8284 		status = NFS4ERR_SERVERFAULT;
8285 		break;
8286 	}
8287 
8288 	return (status);
8289 }
8290 
8291 /*ARGSUSED*/
8292 void
8293 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8294 	    struct svc_req *req, struct compound_state *cs)
8295 {
8296 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
8297 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8298 	nfsstat4 status;
8299 	stateid4 *stateid;
8300 	rfs4_lockowner_t *lo;
8301 	rfs4_client_t *cp;
8302 	rfs4_state_t *sp = NULL;
8303 	rfs4_lo_state_t *lsp = NULL;
8304 	bool_t ls_sw_held = FALSE;
8305 	bool_t create = TRUE;
8306 	bool_t lcreate = TRUE;
8307 	bool_t dup_lock = FALSE;
8308 	int rc;
8309 
8310 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8311 	    LOCK4args *, args);
8312 
8313 	if (cs->vp == NULL) {
8314 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8315 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8316 		    cs, LOCK4res *, resp);
8317 		return;
8318 	}
8319 
8320 	if (args->locker.new_lock_owner) {
8321 		/* Create a new lockowner for this instance */
8322 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8323 
8324 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8325 
8326 		stateid = &olo->open_stateid;
8327 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8328 		if (status != NFS4_OK) {
8329 			NFS4_DEBUG(rfs4_debug,
8330 			    (CE_NOTE, "Get state failed in lock %d", status));
8331 			*cs->statusp = resp->status = status;
8332 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8333 			    cs, LOCK4res *, resp);
8334 			return;
8335 		}
8336 
8337 		/* Ensure specified filehandle matches */
8338 		if (cs->vp != sp->finfo->vp) {
8339 			rfs4_state_rele(sp);
8340 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8341 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8342 			    cs, LOCK4res *, resp);
8343 			return;
8344 		}
8345 
8346 		/* hold off other access to open_owner while we tinker */
8347 		rfs4_sw_enter(&sp->owner->oo_sw);
8348 
8349 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8350 		case NFS4_CHECK_STATEID_OLD:
8351 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8352 			goto end;
8353 		case NFS4_CHECK_STATEID_BAD:
8354 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8355 			goto end;
8356 		case NFS4_CHECK_STATEID_EXPIRED:
8357 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8358 			goto end;
8359 		case NFS4_CHECK_STATEID_UNCONFIRMED:
8360 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8361 			goto end;
8362 		case NFS4_CHECK_STATEID_CLOSED:
8363 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8364 			goto end;
8365 		case NFS4_CHECK_STATEID_OKAY:
8366 		case NFS4_CHECK_STATEID_REPLAY:
8367 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8368 			    sp->owner, resop)) {
8369 			case NFS4_CHKSEQ_OKAY:
8370 				if (rc == NFS4_CHECK_STATEID_OKAY)
8371 					break;
8372 				/*
8373 				 * This is replayed stateid; if seqid
8374 				 * matches next expected, then client
8375 				 * is using wrong seqid.
8376 				 */
8377 				/* FALLTHROUGH */
8378 			case NFS4_CHKSEQ_BAD:
8379 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8380 				goto end;
8381 			case NFS4_CHKSEQ_REPLAY:
8382 				/* This is a duplicate LOCK request */
8383 				dup_lock = TRUE;
8384 
8385 				/*
8386 				 * For a duplicate we do not want to
8387 				 * create a new lockowner as it should
8388 				 * already exist.
8389 				 * Turn off the lockowner create flag.
8390 				 */
8391 				lcreate = FALSE;
8392 			}
8393 			break;
8394 		}
8395 
8396 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8397 		if (lo == NULL) {
8398 			NFS4_DEBUG(rfs4_debug,
8399 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
8400 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
8401 			goto end;
8402 		}
8403 
8404 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8405 		if (lsp == NULL) {
8406 			rfs4_update_lease(sp->owner->client);
8407 			/*
8408 			 * Only update theh open_seqid if this is not
8409 			 * a duplicate request
8410 			 */
8411 			if (dup_lock == FALSE) {
8412 				rfs4_update_open_sequence(sp->owner);
8413 			}
8414 
8415 			NFS4_DEBUG(rfs4_debug,
8416 			    (CE_NOTE, "rfs4_op_lock: no state"));
8417 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8418 			rfs4_update_open_resp(sp->owner, resop, NULL);
8419 			rfs4_lockowner_rele(lo);
8420 			goto end;
8421 		}
8422 
8423 		/*
8424 		 * This is the new_lock_owner branch and the client is
8425 		 * supposed to be associating a new lock_owner with
8426 		 * the open file at this point.  If we find that a
8427 		 * lock_owner/state association already exists and a
8428 		 * successful LOCK request was returned to the client,
8429 		 * an error is returned to the client since this is
8430 		 * not appropriate.  The client should be using the
8431 		 * existing lock_owner branch.
8432 		 */
8433 		if (dup_lock == FALSE && create == FALSE) {
8434 			if (lsp->lock_completed == TRUE) {
8435 				*cs->statusp =
8436 				    resp->status = NFS4ERR_BAD_SEQID;
8437 				rfs4_lockowner_rele(lo);
8438 				goto end;
8439 			}
8440 		}
8441 
8442 		rfs4_update_lease(sp->owner->client);
8443 
8444 		/*
8445 		 * Only update theh open_seqid if this is not
8446 		 * a duplicate request
8447 		 */
8448 		if (dup_lock == FALSE) {
8449 			rfs4_update_open_sequence(sp->owner);
8450 		}
8451 
8452 		/*
8453 		 * If this is a duplicate lock request, just copy the
8454 		 * previously saved reply and return.
8455 		 */
8456 		if (dup_lock == TRUE) {
8457 			/* verify that lock_seqid's match */
8458 			if (lsp->seqid != olo->lock_seqid) {
8459 				NFS4_DEBUG(rfs4_debug,
8460 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
8461 				    "lsp->seqid=%d old->seqid=%d",
8462 				    lsp->seqid, olo->lock_seqid));
8463 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8464 			} else {
8465 				rfs4_copy_reply(resop, lsp->reply);
8466 				/*
8467 				 * Make sure to copy the just
8468 				 * retrieved reply status into the
8469 				 * overall compound status
8470 				 */
8471 				*cs->statusp = resp->status;
8472 			}
8473 			rfs4_lockowner_rele(lo);
8474 			goto end;
8475 		}
8476 
8477 		rfs4_dbe_lock(lsp->dbe);
8478 
8479 		/* Make sure to update the lock sequence id */
8480 		lsp->seqid = olo->lock_seqid;
8481 
8482 		NFS4_DEBUG(rfs4_debug,
8483 		    (CE_NOTE, "Lock seqid established as %d", lsp->seqid));
8484 
8485 		/*
8486 		 * This is used to signify the newly created lockowner
8487 		 * stateid and its sequence number.  The checks for
8488 		 * sequence number and increment don't occur on the
8489 		 * very first lock request for a lockowner.
8490 		 */
8491 		lsp->skip_seqid_check = TRUE;
8492 
8493 		/* hold off other access to lsp while we tinker */
8494 		rfs4_sw_enter(&lsp->ls_sw);
8495 		ls_sw_held = TRUE;
8496 
8497 		rfs4_dbe_unlock(lsp->dbe);
8498 
8499 		rfs4_lockowner_rele(lo);
8500 	} else {
8501 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
8502 		/* get lsp and hold the lock on the underlying file struct */
8503 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
8504 		    != NFS4_OK) {
8505 			*cs->statusp = resp->status = status;
8506 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8507 			    cs, LOCK4res *, resp);
8508 			return;
8509 		}
8510 		create = FALSE;	/* We didn't create lsp */
8511 
8512 		/* Ensure specified filehandle matches */
8513 		if (cs->vp != lsp->state->finfo->vp) {
8514 			rfs4_lo_state_rele(lsp, TRUE);
8515 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8516 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8517 			    cs, LOCK4res *, resp);
8518 			return;
8519 		}
8520 
8521 		/* hold off other access to lsp while we tinker */
8522 		rfs4_sw_enter(&lsp->ls_sw);
8523 		ls_sw_held = TRUE;
8524 
8525 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8526 		/*
8527 		 * The stateid looks like it was okay (expected to be
8528 		 * the next one)
8529 		 */
8530 		case NFS4_CHECK_STATEID_OKAY:
8531 			/*
8532 			 * The sequence id is now checked.  Determine
8533 			 * if this is a replay or if it is in the
8534 			 * expected (next) sequence.  In the case of a
8535 			 * replay, there are two replay conditions
8536 			 * that may occur.  The first is the normal
8537 			 * condition where a LOCK is done with a
8538 			 * NFS4_OK response and the stateid is
8539 			 * updated.  That case is handled below when
8540 			 * the stateid is identified as a REPLAY.  The
8541 			 * second is the case where an error is
8542 			 * returned, like NFS4ERR_DENIED, and the
8543 			 * sequence number is updated but the stateid
8544 			 * is not updated.  This second case is dealt
8545 			 * with here.  So it may seem odd that the
8546 			 * stateid is okay but the sequence id is a
8547 			 * replay but it is okay.
8548 			 */
8549 			switch (rfs4_check_lock_seqid(
8550 			    args->locker.locker4_u.lock_owner.lock_seqid,
8551 			    lsp, resop)) {
8552 			case NFS4_CHKSEQ_REPLAY:
8553 				if (resp->status != NFS4_OK) {
8554 					/*
8555 					 * Here is our replay and need
8556 					 * to verify that the last
8557 					 * response was an error.
8558 					 */
8559 					*cs->statusp = resp->status;
8560 					goto end;
8561 				}
8562 				/*
8563 				 * This is done since the sequence id
8564 				 * looked like a replay but it didn't
8565 				 * pass our check so a BAD_SEQID is
8566 				 * returned as a result.
8567 				 */
8568 				/*FALLTHROUGH*/
8569 			case NFS4_CHKSEQ_BAD:
8570 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8571 				goto end;
8572 			case NFS4_CHKSEQ_OKAY:
8573 				/* Everything looks okay move ahead */
8574 				break;
8575 			}
8576 			break;
8577 		case NFS4_CHECK_STATEID_OLD:
8578 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8579 			goto end;
8580 		case NFS4_CHECK_STATEID_BAD:
8581 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8582 			goto end;
8583 		case NFS4_CHECK_STATEID_EXPIRED:
8584 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8585 			goto end;
8586 		case NFS4_CHECK_STATEID_CLOSED:
8587 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8588 			goto end;
8589 		case NFS4_CHECK_STATEID_REPLAY:
8590 			switch (rfs4_check_lock_seqid(
8591 			    args->locker.locker4_u.lock_owner.lock_seqid,
8592 			    lsp, resop)) {
8593 			case NFS4_CHKSEQ_OKAY:
8594 				/*
8595 				 * This is a replayed stateid; if
8596 				 * seqid matches the next expected,
8597 				 * then client is using wrong seqid.
8598 				 */
8599 			case NFS4_CHKSEQ_BAD:
8600 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8601 				goto end;
8602 			case NFS4_CHKSEQ_REPLAY:
8603 				rfs4_update_lease(lsp->locker->client);
8604 				*cs->statusp = status = resp->status;
8605 				goto end;
8606 			}
8607 			break;
8608 		default:
8609 			ASSERT(FALSE);
8610 			break;
8611 		}
8612 
8613 		rfs4_update_lock_sequence(lsp);
8614 		rfs4_update_lease(lsp->locker->client);
8615 	}
8616 
8617 	/*
8618 	 * NFS4 only allows locking on regular files, so
8619 	 * verify type of object.
8620 	 */
8621 	if (cs->vp->v_type != VREG) {
8622 		if (cs->vp->v_type == VDIR)
8623 			status = NFS4ERR_ISDIR;
8624 		else
8625 			status = NFS4ERR_INVAL;
8626 		goto out;
8627 	}
8628 
8629 	cp = lsp->state->owner->client;
8630 
8631 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
8632 		status = NFS4ERR_GRACE;
8633 		goto out;
8634 	}
8635 
8636 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->can_reclaim) {
8637 		status = NFS4ERR_NO_GRACE;
8638 		goto out;
8639 	}
8640 
8641 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
8642 		status = NFS4ERR_NO_GRACE;
8643 		goto out;
8644 	}
8645 
8646 	if (lsp->state->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE)
8647 		cs->deleg = TRUE;
8648 
8649 	status = rfs4_do_lock(lsp, args->locktype,
8650 	    args->locker.locker4_u.lock_owner.lock_seqid,
8651 	    args->offset, args->length, cs->cr, resop);
8652 
8653 out:
8654 	lsp->skip_seqid_check = FALSE;
8655 
8656 	*cs->statusp = resp->status = status;
8657 
8658 	if (status == NFS4_OK) {
8659 		resp->LOCK4res_u.lock_stateid = lsp->lockid.stateid;
8660 		lsp->lock_completed = TRUE;
8661 	}
8662 	/*
8663 	 * Only update the "OPEN" response here if this was a new
8664 	 * lock_owner
8665 	 */
8666 	if (sp)
8667 		rfs4_update_open_resp(sp->owner, resop, NULL);
8668 
8669 	rfs4_update_lock_resp(lsp, resop);
8670 
8671 end:
8672 	if (lsp) {
8673 		if (ls_sw_held)
8674 			rfs4_sw_exit(&lsp->ls_sw);
8675 		/*
8676 		 * If an sp obtained, then the lsp does not represent
8677 		 * a lock on the file struct.
8678 		 */
8679 		if (sp != NULL)
8680 			rfs4_lo_state_rele(lsp, FALSE);
8681 		else
8682 			rfs4_lo_state_rele(lsp, TRUE);
8683 	}
8684 	if (sp) {
8685 		rfs4_sw_exit(&sp->owner->oo_sw);
8686 		rfs4_state_rele(sp);
8687 	}
8688 
8689 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
8690 	    LOCK4res *, resp);
8691 }
8692 
8693 /* free function for LOCK/LOCKT */
8694 static void
8695 lock_denied_free(nfs_resop4 *resop)
8696 {
8697 	LOCK4denied *dp = NULL;
8698 
8699 	switch (resop->resop) {
8700 	case OP_LOCK:
8701 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
8702 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
8703 		break;
8704 	case OP_LOCKT:
8705 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
8706 			dp = &resop->nfs_resop4_u.oplockt.denied;
8707 		break;
8708 	default:
8709 		break;
8710 	}
8711 
8712 	if (dp)
8713 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
8714 }
8715 
8716 /*ARGSUSED*/
8717 void
8718 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
8719 	    struct svc_req *req, struct compound_state *cs)
8720 {
8721 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
8722 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
8723 	nfsstat4 status;
8724 	stateid4 *stateid = &args->lock_stateid;
8725 	rfs4_lo_state_t *lsp;
8726 
8727 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
8728 	    LOCKU4args *, args);
8729 
8730 	if (cs->vp == NULL) {
8731 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8732 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8733 		    LOCKU4res *, resp);
8734 		return;
8735 	}
8736 
8737 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
8738 		*cs->statusp = resp->status = status;
8739 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8740 		    LOCKU4res *, resp);
8741 		return;
8742 	}
8743 
8744 	/* Ensure specified filehandle matches */
8745 	if (cs->vp != lsp->state->finfo->vp) {
8746 		rfs4_lo_state_rele(lsp, TRUE);
8747 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8748 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8749 		    LOCKU4res *, resp);
8750 		return;
8751 	}
8752 
8753 	/* hold off other access to lsp while we tinker */
8754 	rfs4_sw_enter(&lsp->ls_sw);
8755 
8756 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8757 	case NFS4_CHECK_STATEID_OKAY:
8758 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
8759 		    != NFS4_CHKSEQ_OKAY) {
8760 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8761 			goto end;
8762 		}
8763 		break;
8764 	case NFS4_CHECK_STATEID_OLD:
8765 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8766 		goto end;
8767 	case NFS4_CHECK_STATEID_BAD:
8768 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8769 		goto end;
8770 	case NFS4_CHECK_STATEID_EXPIRED:
8771 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8772 		goto end;
8773 	case NFS4_CHECK_STATEID_CLOSED:
8774 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8775 		goto end;
8776 	case NFS4_CHECK_STATEID_REPLAY:
8777 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
8778 		case NFS4_CHKSEQ_OKAY:
8779 				/*
8780 				 * This is a replayed stateid; if
8781 				 * seqid matches the next expected,
8782 				 * then client is using wrong seqid.
8783 				 */
8784 		case NFS4_CHKSEQ_BAD:
8785 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8786 			goto end;
8787 		case NFS4_CHKSEQ_REPLAY:
8788 			rfs4_update_lease(lsp->locker->client);
8789 			*cs->statusp = status = resp->status;
8790 			goto end;
8791 		}
8792 		break;
8793 	default:
8794 		ASSERT(FALSE);
8795 		break;
8796 	}
8797 
8798 	rfs4_update_lock_sequence(lsp);
8799 	rfs4_update_lease(lsp->locker->client);
8800 
8801 	/*
8802 	 * NFS4 only allows locking on regular files, so
8803 	 * verify type of object.
8804 	 */
8805 	if (cs->vp->v_type != VREG) {
8806 		if (cs->vp->v_type == VDIR)
8807 			status = NFS4ERR_ISDIR;
8808 		else
8809 			status = NFS4ERR_INVAL;
8810 		goto out;
8811 	}
8812 
8813 	if (rfs4_clnt_in_grace(lsp->state->owner->client)) {
8814 		status = NFS4ERR_GRACE;
8815 		goto out;
8816 	}
8817 
8818 	status = rfs4_do_lock(lsp, args->locktype,
8819 	    args->seqid, args->offset, args->length, cs->cr, resop);
8820 
8821 out:
8822 	*cs->statusp = resp->status = status;
8823 
8824 	if (status == NFS4_OK)
8825 		resp->lock_stateid = lsp->lockid.stateid;
8826 
8827 	rfs4_update_lock_resp(lsp, resop);
8828 
8829 end:
8830 	rfs4_sw_exit(&lsp->ls_sw);
8831 	rfs4_lo_state_rele(lsp, TRUE);
8832 
8833 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8834 	    LOCKU4res *, resp);
8835 }
8836 
8837 /*
8838  * LOCKT is a best effort routine, the client can not be guaranteed that
8839  * the status return is still in effect by the time the reply is received.
8840  * They are numerous race conditions in this routine, but we are not required
8841  * and can not be accurate.
8842  */
8843 /*ARGSUSED*/
8844 void
8845 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
8846 	    struct svc_req *req, struct compound_state *cs)
8847 {
8848 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
8849 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
8850 	rfs4_lockowner_t *lo;
8851 	rfs4_client_t *cp;
8852 	bool_t create = FALSE;
8853 	struct flock64 flk;
8854 	int error;
8855 	int flag = FREAD | FWRITE;
8856 	int ltype;
8857 	length4 posix_length;
8858 	sysid_t sysid;
8859 	pid_t pid;
8860 
8861 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
8862 	    LOCKT4args *, args);
8863 
8864 	if (cs->vp == NULL) {
8865 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8866 		goto out;
8867 	}
8868 
8869 	/*
8870 	 * NFS4 only allows locking on regular files, so
8871 	 * verify type of object.
8872 	 */
8873 	if (cs->vp->v_type != VREG) {
8874 		if (cs->vp->v_type == VDIR)
8875 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
8876 		else
8877 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
8878 		goto out;
8879 	}
8880 
8881 	/*
8882 	 * Check out the clientid to ensure the server knows about it
8883 	 * so that we correctly inform the client of a server reboot.
8884 	 */
8885 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
8886 	    == NULL) {
8887 		*cs->statusp = resp->status =
8888 		    rfs4_check_clientid(&args->owner.clientid, 0);
8889 		goto out;
8890 	}
8891 	if (rfs4_lease_expired(cp)) {
8892 		rfs4_client_close(cp);
8893 		/*
8894 		 * Protocol doesn't allow returning NFS4ERR_STALE as
8895 		 * other operations do on this check so STALE_CLIENTID
8896 		 * is returned instead
8897 		 */
8898 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
8899 		goto out;
8900 	}
8901 
8902 	if (rfs4_clnt_in_grace(cp)) {
8903 		*cs->statusp = resp->status = NFS4ERR_GRACE;
8904 		goto out;
8905 	}
8906 	rfs4_client_rele(cp);
8907 
8908 	resp->status = NFS4_OK;
8909 
8910 	switch (args->locktype) {
8911 	case READ_LT:
8912 	case READW_LT:
8913 		ltype = F_RDLCK;
8914 		break;
8915 	case WRITE_LT:
8916 	case WRITEW_LT:
8917 		ltype = F_WRLCK;
8918 		break;
8919 	}
8920 
8921 	posix_length = args->length;
8922 	/* Check for zero length. To lock to end of file use all ones for V4 */
8923 	if (posix_length == 0) {
8924 		*cs->statusp = resp->status = NFS4ERR_INVAL;
8925 		goto out;
8926 	} else if (posix_length == (length4)(~0)) {
8927 		posix_length = 0;	/* Posix to end of file  */
8928 	}
8929 
8930 	/* Find or create a lockowner */
8931 	lo = rfs4_findlockowner(&args->owner, &create);
8932 
8933 	if (lo) {
8934 		pid = lo->pid;
8935 		if ((resp->status =
8936 		    rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8937 			goto err;
8938 		goto out;
8939 	} else {
8940 		pid = 0;
8941 		sysid = lockt_sysid;
8942 	}
8943 retry:
8944 	flk.l_type = ltype;
8945 	flk.l_whence = 0;		/* SEEK_SET */
8946 	flk.l_start = args->offset;
8947 	flk.l_len = posix_length;
8948 	flk.l_sysid = sysid;
8949 	flk.l_pid = pid;
8950 	flag |= F_REMOTELOCK;
8951 
8952 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
8953 
8954 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8955 	if (flk.l_len < 0 || flk.l_start < 0) {
8956 		resp->status = NFS4ERR_INVAL;
8957 		goto err;
8958 	}
8959 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
8960 	    NULL, cs->cr, NULL);
8961 
8962 	/*
8963 	 * N.B. We map error values to nfsv4 errors. This is differrent
8964 	 * than puterrno4 routine.
8965 	 */
8966 	switch (error) {
8967 	case 0:
8968 		if (flk.l_type == F_UNLCK)
8969 			resp->status = NFS4_OK;
8970 		else {
8971 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
8972 				goto retry;
8973 			resp->status = NFS4ERR_DENIED;
8974 		}
8975 		break;
8976 	case EOVERFLOW:
8977 		resp->status = NFS4ERR_INVAL;
8978 		break;
8979 	case EINVAL:
8980 		resp->status = NFS4ERR_NOTSUPP;
8981 		break;
8982 	default:
8983 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
8984 		    error);
8985 		resp->status = NFS4ERR_SERVERFAULT;
8986 		break;
8987 	}
8988 
8989 err:
8990 	if (lo)
8991 		rfs4_lockowner_rele(lo);
8992 	*cs->statusp = resp->status;
8993 out:
8994 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
8995 	    LOCKT4res *, resp);
8996 }
8997 
8998 static int
8999 vop_shrlock(vnode_t *vp, int cmd, struct shrlock *sp, int fflags)
9000 {
9001 	int err;
9002 
9003 	if (cmd == F_UNSHARE && sp->s_deny == 0 && sp->s_access == 0)
9004 		return (0);
9005 
9006 	err = VOP_SHRLOCK(vp, cmd, sp, fflags, CRED(), NULL);
9007 
9008 	NFS4_DEBUG(rfs4_shrlock_debug,
9009 	    (CE_NOTE, "rfs4_shrlock %s vp=%p acc=%d dny=%d sysid=%d "
9010 	    "pid=%d err=%d\n", cmd == F_UNSHARE ? "UNSHR" : "SHARE",
9011 	    (void *) vp, sp->s_access, sp->s_deny, sp->s_sysid, sp->s_pid,
9012 	    err));
9013 
9014 	return (err);
9015 }
9016 
9017 static int
9018 rfs4_shrlock(rfs4_state_t *sp, int cmd)
9019 {
9020 	struct shrlock shr;
9021 	struct shr_locowner shr_loco;
9022 	int fflags;
9023 
9024 	fflags = shr.s_access = shr.s_deny = 0;
9025 
9026 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
9027 		fflags |= FREAD;
9028 		shr.s_access |= F_RDACC;
9029 	}
9030 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
9031 		fflags |= FWRITE;
9032 		shr.s_access |= F_WRACC;
9033 	}
9034 	if (sp->share_deny & OPEN4_SHARE_DENY_READ)
9035 		shr.s_deny |= F_RDDNY;
9036 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE)
9037 		shr.s_deny |= F_WRDNY;
9038 
9039 	shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
9040 	shr.s_sysid = sp->owner->client->sysidt;
9041 	shr_loco.sl_pid = shr.s_pid;
9042 	shr_loco.sl_id = shr.s_sysid;
9043 	shr.s_owner = (caddr_t)&shr_loco;
9044 	shr.s_own_len = sizeof (shr_loco);
9045 	return (vop_shrlock(sp->finfo->vp, cmd, &shr, fflags));
9046 }
9047 
9048 static int
9049 rfs4_share(rfs4_state_t *sp)
9050 {
9051 	int cmd;
9052 	cmd = nbl_need_check(sp->finfo->vp) ? F_SHARE_NBMAND : F_SHARE;
9053 	return (rfs4_shrlock(sp, cmd));
9054 }
9055 
9056 void
9057 rfs4_unshare(rfs4_state_t *sp)
9058 {
9059 	(void) rfs4_shrlock(sp, F_UNSHARE);
9060 }
9061