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