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