xref: /titanic_44/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision e4b86885570d77af552e9cf94f142f4d744fb8c8)
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 	/*
2888 	 * Make a couple of checks made by copen()
2889 	 *
2890 	 * Check to make sure underlying fs supports xattrs.  This
2891 	 * is required because solaris filesystem implementations
2892 	 * (UFS/TMPFS) don't enforce the noxattr mount option
2893 	 * in VOP_LOOKUP(LOOKUP_XATTR).  If fs doesn't support this
2894 	 * pathconf cmd or if fs supports cmd but doesn't claim
2895 	 * support for xattr, return NOTSUPP.  It would be better
2896 	 * to use VOP_PATHCONF( _PC_XATTR_ENABLED) for this; however,
2897 	 * that cmd is not available to VOP_PATHCONF interface
2898 	 * (it's only implemented inside pathconf syscall)...
2899 	 *
2900 	 * Verify permission to put attributes on files (access
2901 	 * checks from copen).
2902 	 */
2903 
2904 	if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 &&
2905 	    !vfs_has_feature(cs->vp->v_vfsp, VFSFT_XVATTR)) {
2906 		*cs->statusp = resp->status = puterrno4(ENOTSUP);
2907 		goto out;
2908 	}
2909 
2910 	/*
2911 	 * If file system supports passing ACE mask to VOP_ACCESS then
2912 	 * check for ACE_READ_NAMED_ATTRS, otherwise do legacy checks
2913 	 */
2914 
2915 	if (vfs_has_feature(cs->vp->v_vfsp, VFSFT_ACEMASKONACCESS))
2916 		error = VOP_ACCESS(cs->vp, ACE_READ_NAMED_ATTRS,
2917 		    V_ACE_MASK, cs->cr, NULL);
2918 	else
2919 		error = ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) &&
2920 		    (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) &&
2921 		    (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0));
2922 
2923 	if (error) {
2924 		*cs->statusp = resp->status = puterrno4(EACCES);
2925 		goto out;
2926 	}
2927 
2928 	/*
2929 	 * The CREATE_XATTR_DIR VOP flag cannot be specified if
2930 	 * the file system is exported read-only -- regardless of
2931 	 * createdir flag.  Otherwise the attrdir would be created
2932 	 * (assuming server fs isn't mounted readonly locally).  If
2933 	 * VOP_LOOKUP returns ENOENT in this case, the error will
2934 	 * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
2935 	 * because specfs has no VOP_LOOKUP op, so the macro would
2936 	 * return ENOSYS.  EINVAL is returned by all (current)
2937 	 * Solaris file system implementations when any of their
2938 	 * restrictions are violated (xattr(dir) can't have xattrdir).
2939 	 * Returning NOTSUPP is more appropriate in this case
2940 	 * because the object will never be able to have an attrdir.
2941 	 */
2942 	if (args->createdir && ! (exp_ro = rdonly4(cs->exi, cs->vp, req)))
2943 		lookup_flags |= CREATE_XATTR_DIR;
2944 
2945 	error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr,
2946 	    NULL, NULL, NULL);
2947 
2948 	if (error) {
2949 		if (error == ENOENT && args->createdir && exp_ro)
2950 			*cs->statusp = resp->status = puterrno4(EROFS);
2951 		else if (error == EINVAL || error == ENOSYS)
2952 			*cs->statusp = resp->status = puterrno4(ENOTSUP);
2953 		else
2954 			*cs->statusp = resp->status = puterrno4(error);
2955 		goto out;
2956 	}
2957 
2958 	ASSERT(avp->v_flag & V_XATTRDIR);
2959 
2960 	error = makefh4(&cs->fh, avp, cs->exi);
2961 
2962 	if (error) {
2963 		VN_RELE(avp);
2964 		*cs->statusp = resp->status = puterrno4(error);
2965 		goto out;
2966 	}
2967 
2968 	VN_RELE(cs->vp);
2969 	cs->vp = avp;
2970 
2971 	/*
2972 	 * There is no requirement for an attrdir fh flag
2973 	 * because the attrdir has a vnode flag to distinguish
2974 	 * it from regular (non-xattr) directories.  The
2975 	 * FH4_ATTRDIR flag is set for future sanity checks.
2976 	 */
2977 	set_fh4_flag(&cs->fh, FH4_ATTRDIR);
2978 	*cs->statusp = resp->status = NFS4_OK;
2979 
2980 out:
2981 	DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs,
2982 	    OPENATTR4res *, resp);
2983 }
2984 
2985 static int
2986 do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred,
2987     caller_context_t *ct)
2988 {
2989 	int error;
2990 	int i;
2991 	clock_t delaytime;
2992 
2993 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
2994 
2995 	/*
2996 	 * Don't block on mandatory locks. If this routine returns
2997 	 * EAGAIN, the caller should return NFS4ERR_LOCKED.
2998 	 */
2999 	uio->uio_fmode = FNONBLOCK;
3000 
3001 	for (i = 0; i < rfs4_maxlock_tries; i++) {
3002 
3003 
3004 		if (direction == FREAD) {
3005 			(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct);
3006 			error = VOP_READ(vp, uio, ioflag, cred, ct);
3007 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct);
3008 		} else {
3009 			(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct);
3010 			error = VOP_WRITE(vp, uio, ioflag, cred, ct);
3011 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct);
3012 		}
3013 
3014 		if (error != EAGAIN)
3015 			break;
3016 
3017 		if (i < rfs4_maxlock_tries - 1) {
3018 			delay(delaytime);
3019 			delaytime *= 2;
3020 		}
3021 	}
3022 
3023 	return (error);
3024 }
3025 
3026 /* ARGSUSED */
3027 static void
3028 rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3029 	struct compound_state *cs)
3030 {
3031 	READ4args *args = &argop->nfs_argop4_u.opread;
3032 	READ4res *resp = &resop->nfs_resop4_u.opread;
3033 	int error;
3034 	int verror;
3035 	vnode_t *vp;
3036 	struct vattr va;
3037 	struct iovec iov;
3038 	struct uio uio;
3039 	u_offset_t offset;
3040 	bool_t *deleg = &cs->deleg;
3041 	nfsstat4 stat;
3042 	int in_crit = 0;
3043 	mblk_t *mp;
3044 	int alloc_err = 0;
3045 	caller_context_t ct;
3046 
3047 	DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs,
3048 	    READ4args, args);
3049 
3050 	vp = cs->vp;
3051 	if (vp == NULL) {
3052 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3053 		goto out;
3054 	}
3055 	if (cs->access == CS_ACCESS_DENIED) {
3056 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3057 		goto out;
3058 	}
3059 
3060 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3061 	    deleg, TRUE, &ct)) != NFS4_OK) {
3062 		*cs->statusp = resp->status = stat;
3063 		goto out;
3064 	}
3065 
3066 	/*
3067 	 * Enter the critical region before calling VOP_RWLOCK
3068 	 * to avoid a deadlock with write requests.
3069 	 */
3070 	if (nbl_need_check(vp)) {
3071 		nbl_start_crit(vp, RW_READER);
3072 		in_crit = 1;
3073 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
3074 		    &ct)) {
3075 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
3076 			goto out;
3077 		}
3078 	}
3079 
3080 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3081 	    deleg, TRUE, &ct)) != NFS4_OK) {
3082 		*cs->statusp = resp->status = stat;
3083 		goto out;
3084 	}
3085 
3086 	va.va_mask = AT_MODE|AT_SIZE|AT_UID;
3087 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3088 
3089 	/*
3090 	 * If we can't get the attributes, then we can't do the
3091 	 * right access checking.  So, we'll fail the request.
3092 	 */
3093 	if (verror) {
3094 		*cs->statusp = resp->status = puterrno4(verror);
3095 		goto out;
3096 	}
3097 
3098 	if (vp->v_type != VREG) {
3099 		*cs->statusp = resp->status =
3100 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
3101 		goto out;
3102 	}
3103 
3104 	if (crgetuid(cs->cr) != va.va_uid &&
3105 	    (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) &&
3106 	    (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) {
3107 		*cs->statusp = resp->status = puterrno4(error);
3108 		goto out;
3109 	}
3110 
3111 	if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
3112 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3113 		goto out;
3114 	}
3115 
3116 	offset = args->offset;
3117 	if (offset >= va.va_size) {
3118 		*cs->statusp = resp->status = NFS4_OK;
3119 		resp->eof = TRUE;
3120 		resp->data_len = 0;
3121 		resp->data_val = NULL;
3122 		resp->mblk = NULL;
3123 		/* RDMA */
3124 		resp->wlist = args->wlist;
3125 		resp->wlist_len = resp->data_len;
3126 		*cs->statusp = resp->status = NFS4_OK;
3127 		goto out;
3128 	}
3129 
3130 	if (args->count == 0) {
3131 		*cs->statusp = resp->status = NFS4_OK;
3132 		resp->eof = FALSE;
3133 		resp->data_len = 0;
3134 		resp->data_val = NULL;
3135 		resp->mblk = NULL;
3136 		/* RDMA */
3137 		resp->wlist = args->wlist;
3138 		resp->wlist_len = resp->data_len;
3139 		goto out;
3140 	}
3141 
3142 	/*
3143 	 * Do not allocate memory more than maximum allowed
3144 	 * transfer size
3145 	 */
3146 	if (args->count > rfs4_tsize(req))
3147 		args->count = rfs4_tsize(req);
3148 
3149 	/*
3150 	 * If returning data via RDMA Write, then grab the chunk list. If we
3151 	 * aren't returning READ data w/RDMA_WRITE, then grab a mblk.
3152 	 */
3153 	if (args->wlist) {
3154 		mp = NULL;
3155 		(void) rdma_get_wchunk(req, &iov, args->wlist);
3156 	} else {
3157 		/*
3158 		 * mp will contain the data to be sent out in the read reply.
3159 		 * It will be freed after the reply has been sent. Let's
3160 		 * roundup the data to a BYTES_PER_XDR_UNIT multiple, so that
3161 		 * the call to xdrmblk_putmblk() never fails. If the first
3162 		 * alloc of the requested size fails, then decrease the size to
3163 		 * something more reasonable and wait for the allocation to
3164 		 * occur.
3165 		 */
3166 		mp = allocb(RNDUP(args->count), BPRI_MED);
3167 		if (mp == NULL) {
3168 			if (args->count > MAXBSIZE)
3169 				args->count = MAXBSIZE;
3170 			mp = allocb_wait(RNDUP(args->count), BPRI_MED,
3171 			    STR_NOSIG, &alloc_err);
3172 		}
3173 		ASSERT(mp != NULL);
3174 		ASSERT(alloc_err == 0);
3175 
3176 		iov.iov_base = (caddr_t)mp->b_datap->db_base;
3177 		iov.iov_len = args->count;
3178 	}
3179 
3180 	uio.uio_iov = &iov;
3181 	uio.uio_iovcnt = 1;
3182 	uio.uio_segflg = UIO_SYSSPACE;
3183 	uio.uio_extflg = UIO_COPY_CACHED;
3184 	uio.uio_loffset = args->offset;
3185 	uio.uio_resid = args->count;
3186 
3187 	error = do_io(FREAD, vp, &uio, 0, cs->cr, &ct);
3188 
3189 	va.va_mask = AT_SIZE;
3190 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3191 
3192 	if (error) {
3193 		freeb(mp);
3194 		*cs->statusp = resp->status = puterrno4(error);
3195 		goto out;
3196 	}
3197 
3198 	*cs->statusp = resp->status = NFS4_OK;
3199 
3200 	ASSERT(uio.uio_resid >= 0);
3201 	resp->data_len = args->count - uio.uio_resid;
3202 	if (mp) {
3203 		resp->data_val = (char *)mp->b_datap->db_base;
3204 	} else {
3205 		resp->data_val = (caddr_t)iov.iov_base;
3206 	}
3207 	resp->mblk = mp;
3208 
3209 	if (!verror && offset + resp->data_len == va.va_size)
3210 		resp->eof = TRUE;
3211 	else
3212 		resp->eof = FALSE;
3213 
3214 	if (args->wlist) {
3215 		if (!rdma_setup_read_data4(args, resp)) {
3216 			*cs->statusp = resp->status = NFS4ERR_INVAL;
3217 		}
3218 	} else {
3219 		resp->wlist = NULL;
3220 	}
3221 
3222 out:
3223 	if (in_crit)
3224 		nbl_end_crit(vp);
3225 
3226 	DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs,
3227 	    READ4res *, resp);
3228 }
3229 
3230 static void
3231 rfs4_op_read_free(nfs_resop4 *resop)
3232 {
3233 	READ4res	*resp = &resop->nfs_resop4_u.opread;
3234 
3235 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3236 		freeb(resp->mblk);
3237 		resp->mblk = NULL;
3238 		resp->data_val = NULL;
3239 		resp->data_len = 0;
3240 	}
3241 }
3242 
3243 static void
3244 rfs4_op_readdir_free(nfs_resop4 * resop)
3245 {
3246 	READDIR4res    *resp = &resop->nfs_resop4_u.opreaddir;
3247 
3248 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3249 		freeb(resp->mblk);
3250 		resp->mblk = NULL;
3251 		resp->data_len = 0;
3252 	}
3253 }
3254 
3255 
3256 /* ARGSUSED */
3257 static void
3258 rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3259     struct compound_state *cs)
3260 {
3261 	PUTPUBFH4res	*resp = &resop->nfs_resop4_u.opputpubfh;
3262 	int		error;
3263 	vnode_t		*vp;
3264 	struct exportinfo *exi, *sav_exi;
3265 	nfs_fh4_fmt_t	*fh_fmtp;
3266 
3267 	DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs);
3268 
3269 	if (cs->vp) {
3270 		VN_RELE(cs->vp);
3271 		cs->vp = NULL;
3272 	}
3273 
3274 	if (cs->cr)
3275 		crfree(cs->cr);
3276 
3277 	cs->cr = crdup(cs->basecr);
3278 
3279 	vp = exi_public->exi_vp;
3280 	if (vp == NULL) {
3281 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3282 		goto out;
3283 	}
3284 
3285 	if (is_system_labeled()) {
3286 		bslabel_t *clabel;
3287 
3288 		ASSERT(req->rq_label != NULL);
3289 		clabel = req->rq_label;
3290 		DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *,
3291 		    "got client label from request(1)",
3292 		    struct svc_req *, req);
3293 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
3294 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK)) {
3295 				*cs->statusp = resp->status =
3296 				    NFS4ERR_SERVERFAULT;
3297 				return;
3298 			}
3299 		}
3300 	}
3301 
3302 	error = makefh4(&cs->fh, vp, exi_public);
3303 	if (error != 0) {
3304 		*cs->statusp = resp->status = puterrno4(error);
3305 		goto out;
3306 	}
3307 	sav_exi = cs->exi;
3308 	if (exi_public == exi_root) {
3309 		/*
3310 		 * No filesystem is actually shared public, so we default
3311 		 * to exi_root. In this case, we must check whether root
3312 		 * is exported.
3313 		 */
3314 		fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
3315 
3316 		/*
3317 		 * if root filesystem is exported, the exportinfo struct that we
3318 		 * should use is what checkexport4 returns, because root_exi is
3319 		 * actually a mostly empty struct.
3320 		 */
3321 		exi = checkexport4(&fh_fmtp->fh4_fsid,
3322 		    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
3323 		cs->exi = ((exi != NULL) ? exi : exi_public);
3324 	} else {
3325 		/*
3326 		 * it's a properly shared filesystem
3327 		 */
3328 		cs->exi = exi_public;
3329 	}
3330 
3331 	VN_HOLD(vp);
3332 	cs->vp = vp;
3333 
3334 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3335 		VN_RELE(cs->vp);
3336 		cs->vp = NULL;
3337 		cs->exi = sav_exi;
3338 		goto out;
3339 	}
3340 
3341 	*cs->statusp = resp->status = NFS4_OK;
3342 out:
3343 	DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs,
3344 	    PUTPUBFH4res *, resp);
3345 }
3346 
3347 /*
3348  * XXX - issue with put*fh operations. Suppose /export/home is exported.
3349  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
3350  * or joe have restrictive search permissions, then we shouldn't let
3351  * the client get a file handle. This is easy to enforce. However, we
3352  * don't know what security flavor should be used until we resolve the
3353  * path name. Another complication is uid mapping. If root is
3354  * the user, then it will be mapped to the anonymous user by default,
3355  * but we won't know that till we've resolved the path name. And we won't
3356  * know what the anonymous user is.
3357  * Luckily, SECINFO is specified to take a full filename.
3358  * So what we will have to in rfs4_op_lookup is check that flavor of
3359  * the target object matches that of the request, and if root was the
3360  * caller, check for the root= and anon= options, and if necessary,
3361  * repeat the lookup using the right cred_t. But that's not done yet.
3362  */
3363 /* ARGSUSED */
3364 static void
3365 rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3366 	struct compound_state *cs)
3367 {
3368 	PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
3369 	PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
3370 	nfs_fh4_fmt_t *fh_fmtp;
3371 
3372 	DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs,
3373 	    PUTFH4args *, args);
3374 
3375 	if (cs->vp) {
3376 		VN_RELE(cs->vp);
3377 		cs->vp = NULL;
3378 	}
3379 
3380 	if (cs->cr) {
3381 		crfree(cs->cr);
3382 		cs->cr = NULL;
3383 	}
3384 
3385 
3386 	if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
3387 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
3388 		goto out;
3389 	}
3390 
3391 	fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
3392 	cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
3393 	    NULL);
3394 
3395 	if (cs->exi == NULL) {
3396 		*cs->statusp = resp->status = NFS4ERR_STALE;
3397 		goto out;
3398 	}
3399 
3400 	cs->cr = crdup(cs->basecr);
3401 
3402 	ASSERT(cs->cr != NULL);
3403 
3404 	if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
3405 		*cs->statusp = resp->status;
3406 		goto out;
3407 	}
3408 
3409 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3410 		VN_RELE(cs->vp);
3411 		cs->vp = NULL;
3412 		goto out;
3413 	}
3414 
3415 	nfs_fh4_copy(&args->object, &cs->fh);
3416 	*cs->statusp = resp->status = NFS4_OK;
3417 	cs->deleg = FALSE;
3418 
3419 out:
3420 	DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs,
3421 	    PUTFH4res *, resp);
3422 }
3423 
3424 /* ARGSUSED */
3425 static void
3426 rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3427 	struct compound_state *cs)
3428 
3429 {
3430 	PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
3431 	int error;
3432 	fid_t fid;
3433 	struct exportinfo *exi, *sav_exi;
3434 
3435 	DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs);
3436 
3437 	if (cs->vp) {
3438 		VN_RELE(cs->vp);
3439 		cs->vp = NULL;
3440 	}
3441 
3442 	if (cs->cr)
3443 		crfree(cs->cr);
3444 
3445 	cs->cr = crdup(cs->basecr);
3446 
3447 	/*
3448 	 * Using rootdir, the system root vnode,
3449 	 * get its fid.
3450 	 */
3451 	bzero(&fid, sizeof (fid));
3452 	fid.fid_len = MAXFIDSZ;
3453 	error = vop_fid_pseudo(rootdir, &fid);
3454 	if (error != 0) {
3455 		*cs->statusp = resp->status = puterrno4(error);
3456 		goto out;
3457 	}
3458 
3459 	/*
3460 	 * Then use the root fsid & fid it to find out if it's exported
3461 	 *
3462 	 * If the server root isn't exported directly, then
3463 	 * it should at least be a pseudo export based on
3464 	 * one or more exports further down in the server's
3465 	 * file tree.
3466 	 */
3467 	exi = checkexport4(&rootdir->v_vfsp->vfs_fsid, &fid, NULL);
3468 	if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
3469 		NFS4_DEBUG(rfs4_debug,
3470 			(CE_WARN, "rfs4_op_putrootfh: export check failure"));
3471 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3472 		goto out;
3473 	}
3474 
3475 	/*
3476 	 * Now make a filehandle based on the root
3477 	 * export and root vnode.
3478 	 */
3479 	error = makefh4(&cs->fh, rootdir, exi);
3480 	if (error != 0) {
3481 		*cs->statusp = resp->status = puterrno4(error);
3482 		goto out;
3483 	}
3484 
3485 	sav_exi = cs->exi;
3486 	cs->exi = exi;
3487 
3488 	VN_HOLD(rootdir);
3489 	cs->vp = rootdir;
3490 
3491 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3492 		VN_RELE(rootdir);
3493 		cs->vp = NULL;
3494 		cs->exi = sav_exi;
3495 		goto out;
3496 	}
3497 
3498 	*cs->statusp = resp->status = NFS4_OK;
3499 	cs->deleg = FALSE;
3500 out:
3501 	DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs,
3502 	    PUTROOTFH4res *, resp);
3503 }
3504 
3505 /*
3506  * A directory entry is a valid nfsv4 entry if
3507  * - it has a non-zero ino
3508  * - it is not a dot or dotdot name
3509  * - it is visible in a pseudo export or in a real export that can
3510  *   only have a limited view.
3511  */
3512 static bool_t
3513 valid_nfs4_entry(struct exportinfo *exi, struct dirent64 *dp,
3514 		int *expseudo, int check_visible)
3515 {
3516 	if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) {
3517 		*expseudo = 0;
3518 		return (FALSE);
3519 	}
3520 
3521 	if (! check_visible) {
3522 		*expseudo = 0;
3523 		return (TRUE);
3524 	}
3525 
3526 	return (nfs_visible_inode(exi, dp->d_ino, expseudo));
3527 }
3528 
3529 /*
3530  * set_rdattr_params sets up the variables used to manage what information
3531  * to get for each directory entry.
3532  */
3533 static nfsstat4
3534 set_rdattr_params(struct nfs4_svgetit_arg *sargp,
3535 		bitmap4 attrs, bool_t *need_to_lookup)
3536 {
3537 	uint_t	va_mask;
3538 	nfsstat4 status;
3539 	bitmap4 objbits;
3540 
3541 	status = bitmap4_to_attrmask(attrs, sargp);
3542 	if (status != NFS4_OK) {
3543 		/*
3544 		 * could not even figure attr mask
3545 		 */
3546 		return (status);
3547 	}
3548 	va_mask = sargp->vap->va_mask;
3549 
3550 	/*
3551 	 * dirent's d_ino is always correct value for mounted_on_fileid.
3552 	 * mntdfid_set is set once here, but mounted_on_fileid is
3553 	 * set in main dirent processing loop for each dirent.
3554 	 * The mntdfid_set is a simple optimization that lets the
3555 	 * server attr code avoid work when caller is readdir.
3556 	 */
3557 	sargp->mntdfid_set = TRUE;
3558 
3559 	/*
3560 	 * Lookup entry only if client asked for any of the following:
3561 	 * a) vattr attrs
3562 	 * b) vfs attrs
3563 	 * c) attrs w/per-object scope requested (change, filehandle, etc)
3564 	 *    other than mounted_on_fileid (which we can take from dirent)
3565 	 */
3566 	objbits = attrs ? attrs & NFS4_VP_ATTR_MASK : 0;
3567 
3568 	if (va_mask || sargp->sbp || (objbits & ~FATTR4_MOUNTED_ON_FILEID_MASK))
3569 		*need_to_lookup = TRUE;
3570 	else
3571 		*need_to_lookup = FALSE;
3572 
3573 	if (sargp->sbp == NULL)
3574 		return (NFS4_OK);
3575 
3576 	/*
3577 	 * If filesystem attrs are requested, get them now from the
3578 	 * directory vp, as most entries will have same filesystem. The only
3579 	 * exception are mounted over entries but we handle
3580 	 * those as we go (XXX mounted over detection not yet implemented).
3581 	 */
3582 	sargp->vap->va_mask = 0;	/* to avoid VOP_GETATTR */
3583 	status = bitmap4_get_sysattrs(sargp);
3584 	sargp->vap->va_mask = va_mask;
3585 
3586 	if ((status != NFS4_OK) && sargp->rdattr_error_req) {
3587 		/*
3588 		 * Failed to get filesystem attributes.
3589 		 * Return a rdattr_error for each entry, but don't fail.
3590 		 * However, don't get any obj-dependent attrs.
3591 		 */
3592 		sargp->rdattr_error = status;	/* for rdattr_error */
3593 		*need_to_lookup = FALSE;
3594 		/*
3595 		 * At least get fileid for regular readdir output
3596 		 */
3597 		sargp->vap->va_mask &= AT_NODEID;
3598 		status = NFS4_OK;
3599 	}
3600 
3601 	return (status);
3602 }
3603 
3604 /*
3605  * readlink: args: CURRENT_FH.
3606  *	res: status. If success - CURRENT_FH unchanged, return linktext.
3607  */
3608 
3609 /* ARGSUSED */
3610 static void
3611 rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3612 	struct compound_state *cs)
3613 {
3614 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3615 	int error;
3616 	vnode_t *vp;
3617 	struct iovec iov;
3618 	struct vattr va;
3619 	struct uio uio;
3620 	char *data;
3621 
3622 	DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs);
3623 
3624 	/* CURRENT_FH: directory */
3625 	vp = cs->vp;
3626 	if (vp == NULL) {
3627 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3628 		goto out;
3629 	}
3630 
3631 	if (cs->access == CS_ACCESS_DENIED) {
3632 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3633 		goto out;
3634 	}
3635 
3636 	if (vp->v_type == VDIR) {
3637 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
3638 		goto out;
3639 	}
3640 
3641 	if (vp->v_type != VLNK) {
3642 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3643 		goto out;
3644 	}
3645 
3646 	va.va_mask = AT_MODE;
3647 	error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
3648 	if (error) {
3649 		*cs->statusp = resp->status = puterrno4(error);
3650 		goto out;
3651 	}
3652 
3653 	if (MANDLOCK(vp, va.va_mode)) {
3654 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3655 		goto out;
3656 	}
3657 
3658 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
3659 
3660 	iov.iov_base = data;
3661 	iov.iov_len = MAXPATHLEN;
3662 	uio.uio_iov = &iov;
3663 	uio.uio_iovcnt = 1;
3664 	uio.uio_segflg = UIO_SYSSPACE;
3665 	uio.uio_extflg = UIO_COPY_CACHED;
3666 	uio.uio_loffset = 0;
3667 	uio.uio_resid = MAXPATHLEN;
3668 
3669 	error = VOP_READLINK(vp, &uio, cs->cr, NULL);
3670 
3671 	if (error) {
3672 		kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3673 		*cs->statusp = resp->status = puterrno4(error);
3674 		goto out;
3675 	}
3676 
3677 	*(data + MAXPATHLEN - uio.uio_resid) = '\0';
3678 
3679 	/*
3680 	 * treat link name as data
3681 	 */
3682 	(void) str_to_utf8(data, &resp->link);
3683 
3684 	kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3685 	*cs->statusp = resp->status = NFS4_OK;
3686 
3687 out:
3688 	DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs,
3689 	    READLINK4res *, resp);
3690 }
3691 
3692 static void
3693 rfs4_op_readlink_free(nfs_resop4 *resop)
3694 {
3695 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3696 	utf8string *symlink = &resp->link;
3697 
3698 	if (symlink->utf8string_val) {
3699 		UTF8STRING_FREE(*symlink)
3700 	}
3701 }
3702 
3703 /*
3704  * release_lockowner:
3705  *	Release any state associated with the supplied
3706  *	lockowner. Note if any lo_state is holding locks we will not
3707  *	rele that lo_state and thus the lockowner will not be destroyed.
3708  *	A client using lock after the lock owner stateid has been released
3709  *	will suffer the consequence of NFS4ERR_BAD_STATEID and would have
3710  *	to reissue the lock with new_lock_owner set to TRUE.
3711  *	args: lock_owner
3712  *	res:  status
3713  */
3714 /* ARGSUSED */
3715 static void
3716 rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
3717 	struct svc_req *req, struct compound_state *cs)
3718 {
3719 	RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
3720 	RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
3721 	rfs4_lockowner_t *lo;
3722 	rfs4_openowner_t *oop;
3723 	rfs4_state_t *sp;
3724 	rfs4_lo_state_t *lsp;
3725 	rfs4_client_t *cp;
3726 	bool_t create = FALSE;
3727 	locklist_t *llist;
3728 	sysid_t sysid;
3729 
3730 	DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *,
3731 	    cs, RELEASE_LOCKOWNER4args *, ap);
3732 
3733 	/* Make sure there is a clientid around for this request */
3734 	cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
3735 
3736 	if (cp == NULL) {
3737 		*cs->statusp = resp->status =
3738 		    rfs4_check_clientid(&ap->lock_owner.clientid, 0);
3739 		goto out;
3740 	}
3741 	rfs4_client_rele(cp);
3742 
3743 	lo = rfs4_findlockowner(&ap->lock_owner, &create);
3744 	if (lo == NULL) {
3745 		*cs->statusp = resp->status = NFS4_OK;
3746 		goto out;
3747 	}
3748 	ASSERT(lo->client != NULL);
3749 
3750 	/*
3751 	 * Check for EXPIRED client. If so will reap state with in a lease
3752 	 * period or on next set_clientid_confirm step
3753 	 */
3754 	if (rfs4_lease_expired(lo->client)) {
3755 		rfs4_lockowner_rele(lo);
3756 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
3757 		goto out;
3758 	}
3759 
3760 	/*
3761 	 * If no sysid has been assigned, then no locks exist; just return.
3762 	 */
3763 	rfs4_dbe_lock(lo->client->dbe);
3764 	if (lo->client->sysidt == LM_NOSYSID) {
3765 		rfs4_lockowner_rele(lo);
3766 		rfs4_dbe_unlock(lo->client->dbe);
3767 		goto out;
3768 	}
3769 
3770 	sysid = lo->client->sysidt;
3771 	rfs4_dbe_unlock(lo->client->dbe);
3772 
3773 	/*
3774 	 * Mark the lockowner invalid.
3775 	 */
3776 	rfs4_dbe_hide(lo->dbe);
3777 
3778 	/*
3779 	 * sysid-pid pair should now not be used since the lockowner is
3780 	 * invalid. If the client were to instantiate the lockowner again
3781 	 * it would be assigned a new pid. Thus we can get the list of
3782 	 * current locks.
3783 	 */
3784 
3785 	llist = flk_get_active_locks(sysid, lo->pid);
3786 	/* If we are still holding locks fail */
3787 	if (llist != NULL) {
3788 
3789 		*cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
3790 
3791 		flk_free_locklist(llist);
3792 		/*
3793 		 * We need to unhide the lockowner so the client can
3794 		 * try it again. The bad thing here is if the client
3795 		 * has a logic error that took it here in the first place
3796 		 * he probably has lost accounting of the locks that it
3797 		 * is holding. So we may have dangling state until the
3798 		 * open owner state is reaped via close. One scenario
3799 		 * that could possibly occur is that the client has
3800 		 * sent the unlock request(s) in separate threads
3801 		 * and has not waited for the replies before sending the
3802 		 * RELEASE_LOCKOWNER request. Presumably, it would expect
3803 		 * and deal appropriately with NFS4ERR_LOCKS_HELD, by
3804 		 * reissuing the request.
3805 		 */
3806 		rfs4_dbe_unhide(lo->dbe);
3807 		rfs4_lockowner_rele(lo);
3808 		goto out;
3809 	}
3810 
3811 	/*
3812 	 * For the corresponding client we need to check each open
3813 	 * owner for any opens that have lockowner state associated
3814 	 * with this lockowner.
3815 	 */
3816 
3817 	rfs4_dbe_lock(lo->client->dbe);
3818 	for (oop = lo->client->openownerlist.next->oop; oop != NULL;
3819 	    oop = oop->openownerlist.next->oop) {
3820 
3821 		rfs4_dbe_lock(oop->dbe);
3822 		for (sp = oop->ownerstateids.next->sp; sp != NULL;
3823 		    sp = sp->ownerstateids.next->sp) {
3824 
3825 			rfs4_dbe_lock(sp->dbe);
3826 			for (lsp = sp->lockownerlist.next->lsp;
3827 			    lsp != NULL; lsp = lsp->lockownerlist.next->lsp) {
3828 				if (lsp->locker == lo) {
3829 					rfs4_dbe_lock(lsp->dbe);
3830 					rfs4_dbe_invalidate(lsp->dbe);
3831 					rfs4_dbe_unlock(lsp->dbe);
3832 				}
3833 			}
3834 			rfs4_dbe_unlock(sp->dbe);
3835 		}
3836 		rfs4_dbe_unlock(oop->dbe);
3837 	}
3838 	rfs4_dbe_unlock(lo->client->dbe);
3839 
3840 	rfs4_lockowner_rele(lo);
3841 
3842 	*cs->statusp = resp->status = NFS4_OK;
3843 
3844 out:
3845 	DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *,
3846 	    cs, RELEASE_LOCKOWNER4res *, resp);
3847 }
3848 
3849 /*
3850  * short utility function to lookup a file and recall the delegation
3851  */
3852 static rfs4_file_t *
3853 rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
3854 	int *lkup_error, cred_t *cr)
3855 {
3856 	vnode_t *vp;
3857 	rfs4_file_t *fp = NULL;
3858 	bool_t fcreate = FALSE;
3859 	int error;
3860 
3861 	if (vpp)
3862 		*vpp = NULL;
3863 
3864 	if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL,
3865 	    NULL)) == 0) {
3866 		if (vp->v_type == VREG)
3867 			fp = rfs4_findfile(vp, NULL, &fcreate);
3868 		if (vpp)
3869 			*vpp = vp;
3870 		else
3871 			VN_RELE(vp);
3872 	}
3873 
3874 	if (lkup_error)
3875 		*lkup_error = error;
3876 
3877 	return (fp);
3878 }
3879 
3880 /*
3881  * remove: args: CURRENT_FH: directory; name.
3882  *	res: status. If success - CURRENT_FH unchanged, return change_info
3883  *		for directory.
3884  */
3885 /* ARGSUSED */
3886 static void
3887 rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3888 	struct compound_state *cs)
3889 {
3890 	REMOVE4args *args = &argop->nfs_argop4_u.opremove;
3891 	REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
3892 	int error;
3893 	vnode_t *dvp, *vp;
3894 	struct vattr bdva, idva, adva;
3895 	char *nm;
3896 	uint_t len;
3897 	rfs4_file_t *fp;
3898 	int in_crit = 0;
3899 	bslabel_t *clabel;
3900 
3901 	DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs,
3902 	    REMOVE4args *, args);
3903 
3904 	/* CURRENT_FH: directory */
3905 	dvp = cs->vp;
3906 	if (dvp == NULL) {
3907 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3908 		goto out;
3909 	}
3910 
3911 	if (cs->access == CS_ACCESS_DENIED) {
3912 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3913 		goto out;
3914 	}
3915 
3916 	/*
3917 	 * If there is an unshared filesystem mounted on this vnode,
3918 	 * Do not allow to remove anything in this directory.
3919 	 */
3920 	if (vn_ismntpt(dvp)) {
3921 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3922 		goto out;
3923 	}
3924 
3925 	if (dvp->v_type != VDIR) {
3926 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
3927 		goto out;
3928 	}
3929 
3930 	if (!utf8_dir_verify(&args->target)) {
3931 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3932 		goto out;
3933 	}
3934 
3935 	/*
3936 	 * Lookup the file so that we can check if it's a directory
3937 	 */
3938 	nm = utf8_to_fn(&args->target, &len, NULL);
3939 	if (nm == NULL) {
3940 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3941 		goto out;
3942 	}
3943 
3944 	if (len > MAXNAMELEN) {
3945 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
3946 		kmem_free(nm, len);
3947 		goto out;
3948 	}
3949 
3950 	if (rdonly4(cs->exi, cs->vp, req)) {
3951 		*cs->statusp = resp->status = NFS4ERR_ROFS;
3952 		kmem_free(nm, len);
3953 		goto out;
3954 	}
3955 
3956 	/*
3957 	 * Lookup the file to determine type and while we are see if
3958 	 * there is a file struct around and check for delegation.
3959 	 * We don't need to acquire va_seq before this lookup, if
3960 	 * it causes an update, cinfo.before will not match, which will
3961 	 * trigger a cache flush even if atomic is TRUE.
3962 	 */
3963 	if (fp = rfs4_lookup_and_findfile(dvp, nm, &vp, &error, cs->cr)) {
3964 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
3965 		    NULL)) {
3966 			VN_RELE(vp);
3967 			rfs4_file_rele(fp);
3968 			*cs->statusp = resp->status = NFS4ERR_DELAY;
3969 			kmem_free(nm, len);
3970 			goto out;
3971 		}
3972 	}
3973 
3974 	/* Didn't find anything to remove */
3975 	if (vp == NULL) {
3976 		*cs->statusp = resp->status = error;
3977 		kmem_free(nm, len);
3978 		goto out;
3979 	}
3980 
3981 	if (nbl_need_check(vp)) {
3982 		nbl_start_crit(vp, RW_READER);
3983 		in_crit = 1;
3984 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
3985 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
3986 			kmem_free(nm, len);
3987 			nbl_end_crit(vp);
3988 			VN_RELE(vp);
3989 			if (fp) {
3990 				rfs4_clear_dont_grant(fp);
3991 				rfs4_file_rele(fp);
3992 			}
3993 			goto out;
3994 		}
3995 	}
3996 
3997 	/* check label before allowing removal */
3998 	if (is_system_labeled()) {
3999 		ASSERT(req->rq_label != NULL);
4000 		clabel = req->rq_label;
4001 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
4002 		    "got client label from request(1)",
4003 		    struct svc_req *, req);
4004 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4005 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK)) {
4006 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4007 				kmem_free(nm, len);
4008 				if (in_crit)
4009 					nbl_end_crit(vp);
4010 				VN_RELE(vp);
4011 				if (fp) {
4012 					rfs4_clear_dont_grant(fp);
4013 					rfs4_file_rele(fp);
4014 				}
4015 				goto out;
4016 			}
4017 		}
4018 	}
4019 
4020 	/* Get dir "before" change value */
4021 	bdva.va_mask = AT_CTIME|AT_SEQ;
4022 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
4023 	if (error) {
4024 		*cs->statusp = resp->status = puterrno4(error);
4025 		kmem_free(nm, len);
4026 		goto out;
4027 	}
4028 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
4029 
4030 	/* Actually do the REMOVE operation */
4031 	if (vp->v_type == VDIR) {
4032 		/*
4033 		 * Can't remove a directory that has a mounted-on filesystem.
4034 		 */
4035 		if (vn_ismntpt(vp)) {
4036 			error = EACCES;
4037 		} else {
4038 			/*
4039 			 * System V defines rmdir to return EEXIST,
4040 			 * not * ENOTEMPTY, if the directory is not
4041 			 * empty.  A System V NFS server needs to map
4042 			 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
4043 			 * transmit over the wire.
4044 			 */
4045 			if ((error = VOP_RMDIR(dvp, nm, rootdir, cs->cr,
4046 			    NULL, 0)) == EEXIST)
4047 				error = ENOTEMPTY;
4048 		}
4049 	} else {
4050 		if ((error = VOP_REMOVE(dvp, nm, cs->cr, NULL, 0)) == 0 &&
4051 		    fp != NULL) {
4052 			struct vattr va;
4053 			vnode_t *tvp;
4054 
4055 			rfs4_dbe_lock(fp->dbe);
4056 			tvp = fp->vp;
4057 			if (tvp)
4058 				VN_HOLD(tvp);
4059 			rfs4_dbe_unlock(fp->dbe);
4060 
4061 			if (tvp) {
4062 				/*
4063 				 * This is va_seq safe because we are not
4064 				 * manipulating dvp.
4065 				 */
4066 				va.va_mask = AT_NLINK;
4067 				if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4068 				    va.va_nlink == 0) {
4069 					/* Remove state on file remove */
4070 					if (in_crit) {
4071 						nbl_end_crit(vp);
4072 						in_crit = 0;
4073 					}
4074 					rfs4_close_all_state(fp);
4075 				}
4076 				VN_RELE(tvp);
4077 			}
4078 		}
4079 	}
4080 
4081 	if (in_crit)
4082 		nbl_end_crit(vp);
4083 	VN_RELE(vp);
4084 
4085 	if (fp) {
4086 		rfs4_clear_dont_grant(fp);
4087 		rfs4_file_rele(fp);
4088 	}
4089 	kmem_free(nm, len);
4090 
4091 	if (error) {
4092 		*cs->statusp = resp->status = puterrno4(error);
4093 		goto out;
4094 	}
4095 
4096 	/*
4097 	 * Get the initial "after" sequence number, if it fails, set to zero
4098 	 */
4099 	idva.va_mask = AT_SEQ;
4100 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
4101 		idva.va_seq = 0;
4102 
4103 	/*
4104 	 * Force modified data and metadata out to stable storage.
4105 	 */
4106 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
4107 
4108 	/*
4109 	 * Get "after" change value, if it fails, simply return the
4110 	 * before value.
4111 	 */
4112 	adva.va_mask = AT_CTIME|AT_SEQ;
4113 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
4114 		adva.va_ctime = bdva.va_ctime;
4115 		adva.va_seq = 0;
4116 	}
4117 
4118 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
4119 
4120 	/*
4121 	 * The cinfo.atomic = TRUE only if we have
4122 	 * non-zero va_seq's, and it has incremented by exactly one
4123 	 * during the VOP_REMOVE/RMDIR and it didn't change during
4124 	 * the VOP_FSYNC.
4125 	 */
4126 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
4127 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
4128 		resp->cinfo.atomic = TRUE;
4129 	else
4130 		resp->cinfo.atomic = FALSE;
4131 
4132 	*cs->statusp = resp->status = NFS4_OK;
4133 
4134 out:
4135 	DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs,
4136 	    REMOVE4res *, resp);
4137 }
4138 
4139 /*
4140  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
4141  *		oldname and newname.
4142  *	res: status. If success - CURRENT_FH unchanged, return change_info
4143  *		for both from and target directories.
4144  */
4145 /* ARGSUSED */
4146 static void
4147 rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4148 	struct compound_state *cs)
4149 {
4150 	RENAME4args *args = &argop->nfs_argop4_u.oprename;
4151 	RENAME4res *resp = &resop->nfs_resop4_u.oprename;
4152 	int error;
4153 	vnode_t *odvp;
4154 	vnode_t *ndvp;
4155 	vnode_t *srcvp, *targvp;
4156 	struct vattr obdva, oidva, oadva;
4157 	struct vattr nbdva, nidva, nadva;
4158 	char *onm, *nnm;
4159 	uint_t olen, nlen;
4160 	rfs4_file_t *fp, *sfp;
4161 	int in_crit_src, in_crit_targ;
4162 	int fp_rele_grant_hold, sfp_rele_grant_hold;
4163 	bslabel_t *clabel;
4164 
4165 	DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs,
4166 	    RENAME4args *, args);
4167 
4168 	fp = sfp = NULL;
4169 	srcvp = targvp = NULL;
4170 	in_crit_src = in_crit_targ = 0;
4171 	fp_rele_grant_hold = sfp_rele_grant_hold = 0;
4172 
4173 	/* CURRENT_FH: target directory */
4174 	ndvp = cs->vp;
4175 	if (ndvp == NULL) {
4176 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4177 		goto out;
4178 	}
4179 
4180 	/* SAVED_FH: from directory */
4181 	odvp = cs->saved_vp;
4182 	if (odvp == NULL) {
4183 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4184 		goto out;
4185 	}
4186 
4187 	if (cs->access == CS_ACCESS_DENIED) {
4188 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4189 		goto out;
4190 	}
4191 
4192 	/*
4193 	 * If there is an unshared filesystem mounted on this vnode,
4194 	 * do not allow to rename objects in this directory.
4195 	 */
4196 	if (vn_ismntpt(odvp)) {
4197 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4198 		goto out;
4199 	}
4200 
4201 	/*
4202 	 * If there is an unshared filesystem mounted on this vnode,
4203 	 * do not allow to rename to this directory.
4204 	 */
4205 	if (vn_ismntpt(ndvp)) {
4206 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4207 		goto out;
4208 	}
4209 
4210 	if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
4211 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4212 		goto out;
4213 	}
4214 
4215 	if (cs->saved_exi != cs->exi) {
4216 		*cs->statusp = resp->status = NFS4ERR_XDEV;
4217 		goto out;
4218 	}
4219 
4220 	if (!utf8_dir_verify(&args->oldname)) {
4221 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4222 		goto out;
4223 	}
4224 
4225 	if (!utf8_dir_verify(&args->newname)) {
4226 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4227 		goto out;
4228 	}
4229 
4230 	onm = utf8_to_fn(&args->oldname, &olen, NULL);
4231 	if (onm == NULL) {
4232 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4233 		goto out;
4234 	}
4235 
4236 	nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4237 	if (nnm == NULL) {
4238 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4239 		kmem_free(onm, olen);
4240 		goto out;
4241 	}
4242 
4243 	if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
4244 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4245 		kmem_free(onm, olen);
4246 		kmem_free(nnm, nlen);
4247 		goto out;
4248 	}
4249 
4250 
4251 	if (rdonly4(cs->exi, cs->vp, req)) {
4252 		*cs->statusp = resp->status = NFS4ERR_ROFS;
4253 		kmem_free(onm, olen);
4254 		kmem_free(nnm, nlen);
4255 		goto out;
4256 	}
4257 
4258 	/* check label of the target dir */
4259 	if (is_system_labeled()) {
4260 		ASSERT(req->rq_label != NULL);
4261 		clabel = req->rq_label;
4262 		DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
4263 		    "got client label from request(1)",
4264 		    struct svc_req *, req);
4265 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4266 			if (!do_rfs_label_check(clabel, ndvp,
4267 			    EQUALITY_CHECK)) {
4268 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4269 				goto out;
4270 			}
4271 		}
4272 	}
4273 
4274 	/*
4275 	 * Is the source a file and have a delegation?
4276 	 * We don't need to acquire va_seq before these lookups, if
4277 	 * it causes an update, cinfo.before will not match, which will
4278 	 * trigger a cache flush even if atomic is TRUE.
4279 	 */
4280 	if (sfp = rfs4_lookup_and_findfile(odvp, onm, &srcvp, &error, cs->cr)) {
4281 		if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
4282 		    NULL)) {
4283 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4284 			goto err_out;
4285 		}
4286 	}
4287 
4288 	if (srcvp == NULL) {
4289 		*cs->statusp = resp->status = puterrno4(error);
4290 		kmem_free(onm, olen);
4291 		kmem_free(nnm, nlen);
4292 		goto out;
4293 	}
4294 
4295 	sfp_rele_grant_hold = 1;
4296 
4297 	/* Does the destination exist and a file and have a delegation? */
4298 	if (fp = rfs4_lookup_and_findfile(ndvp, nnm, &targvp, NULL, cs->cr)) {
4299 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4300 		    NULL)) {
4301 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4302 			goto err_out;
4303 		}
4304 	}
4305 	fp_rele_grant_hold = 1;
4306 
4307 
4308 	/* Check for NBMAND lock on both source and target */
4309 	if (nbl_need_check(srcvp)) {
4310 		nbl_start_crit(srcvp, RW_READER);
4311 		in_crit_src = 1;
4312 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) {
4313 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4314 			goto err_out;
4315 		}
4316 	}
4317 
4318 	if (targvp && nbl_need_check(targvp)) {
4319 		nbl_start_crit(targvp, RW_READER);
4320 		in_crit_targ = 1;
4321 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
4322 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4323 			goto err_out;
4324 		}
4325 	}
4326 
4327 	/* Get source "before" change value */
4328 	obdva.va_mask = AT_CTIME|AT_SEQ;
4329 	error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL);
4330 	if (!error) {
4331 		nbdva.va_mask = AT_CTIME|AT_SEQ;
4332 		error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL);
4333 	}
4334 	if (error) {
4335 		*cs->statusp = resp->status = puterrno4(error);
4336 		goto err_out;
4337 	}
4338 
4339 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
4340 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
4341 
4342 	if ((error = VOP_RENAME(odvp, onm, ndvp, nnm, cs->cr, NULL, 0)) == 0 &&
4343 	    fp != NULL) {
4344 		struct vattr va;
4345 		vnode_t *tvp;
4346 
4347 		rfs4_dbe_lock(fp->dbe);
4348 		tvp = fp->vp;
4349 		if (tvp)
4350 			VN_HOLD(tvp);
4351 		rfs4_dbe_unlock(fp->dbe);
4352 
4353 		if (tvp) {
4354 			va.va_mask = AT_NLINK;
4355 			if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4356 			    va.va_nlink == 0) {
4357 				/* The file is gone and so should the state */
4358 				if (in_crit_targ) {
4359 					nbl_end_crit(targvp);
4360 					in_crit_targ = 0;
4361 				}
4362 				rfs4_close_all_state(fp);
4363 			}
4364 			VN_RELE(tvp);
4365 		}
4366 	}
4367 	if (error == 0)
4368 		vn_renamepath(ndvp, srcvp, nnm, nlen - 1);
4369 
4370 	if (in_crit_src)
4371 		nbl_end_crit(srcvp);
4372 	if (srcvp)
4373 		VN_RELE(srcvp);
4374 	if (in_crit_targ)
4375 		nbl_end_crit(targvp);
4376 	if (targvp)
4377 		VN_RELE(targvp);
4378 
4379 	if (sfp) {
4380 		rfs4_clear_dont_grant(sfp);
4381 		rfs4_file_rele(sfp);
4382 	}
4383 	if (fp) {
4384 		rfs4_clear_dont_grant(fp);
4385 		rfs4_file_rele(fp);
4386 	}
4387 
4388 	kmem_free(onm, olen);
4389 	kmem_free(nnm, nlen);
4390 
4391 	/*
4392 	 * Get the initial "after" sequence number, if it fails, set to zero
4393 	 */
4394 	oidva.va_mask = AT_SEQ;
4395 	if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL))
4396 		oidva.va_seq = 0;
4397 
4398 	nidva.va_mask = AT_SEQ;
4399 	if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL))
4400 		nidva.va_seq = 0;
4401 
4402 	/*
4403 	 * Force modified data and metadata out to stable storage.
4404 	 */
4405 	(void) VOP_FSYNC(odvp, 0, cs->cr, NULL);
4406 	(void) VOP_FSYNC(ndvp, 0, cs->cr, NULL);
4407 
4408 	if (error) {
4409 		*cs->statusp = resp->status = puterrno4(error);
4410 		goto out;
4411 	}
4412 
4413 	/*
4414 	 * Get "after" change values, if it fails, simply return the
4415 	 * before value.
4416 	 */
4417 	oadva.va_mask = AT_CTIME|AT_SEQ;
4418 	if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) {
4419 		oadva.va_ctime = obdva.va_ctime;
4420 		oadva.va_seq = 0;
4421 	}
4422 
4423 	nadva.va_mask = AT_CTIME|AT_SEQ;
4424 	if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) {
4425 		nadva.va_ctime = nbdva.va_ctime;
4426 		nadva.va_seq = 0;
4427 	}
4428 
4429 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
4430 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
4431 
4432 	/*
4433 	 * The cinfo.atomic = TRUE only if we have
4434 	 * non-zero va_seq's, and it has incremented by exactly one
4435 	 * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
4436 	 */
4437 	if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
4438 	    oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq)
4439 		resp->source_cinfo.atomic = TRUE;
4440 	else
4441 		resp->source_cinfo.atomic = FALSE;
4442 
4443 	if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
4444 	    nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq)
4445 		resp->target_cinfo.atomic = TRUE;
4446 	else
4447 		resp->target_cinfo.atomic = FALSE;
4448 
4449 #ifdef	VOLATILE_FH_TEST
4450 	{
4451 	extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
4452 
4453 	/*
4454 	 * Add the renamed file handle to the volatile rename list
4455 	 */
4456 	if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
4457 		/* file handles may expire on rename */
4458 		vnode_t *vp;
4459 
4460 		nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4461 		/*
4462 		 * Already know that nnm will be a valid string
4463 		 */
4464 		error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr,
4465 		    NULL, NULL, NULL);
4466 		kmem_free(nnm, nlen);
4467 		if (!error) {
4468 			add_volrnm_fh(cs->exi, vp);
4469 			VN_RELE(vp);
4470 		}
4471 	}
4472 	}
4473 #endif	/* VOLATILE_FH_TEST */
4474 
4475 	*cs->statusp = resp->status = NFS4_OK;
4476 out:
4477 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4478 	    RENAME4res *, resp);
4479 	return;
4480 
4481 err_out:
4482 	kmem_free(onm, olen);
4483 	kmem_free(nnm, nlen);
4484 
4485 	if (in_crit_src) nbl_end_crit(srcvp);
4486 	if (in_crit_targ) nbl_end_crit(targvp);
4487 	if (targvp) VN_RELE(targvp);
4488 	if (srcvp) VN_RELE(srcvp);
4489 	if (sfp) {
4490 		if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
4491 		rfs4_file_rele(sfp);
4492 	}
4493 	if (fp) {
4494 		if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
4495 		rfs4_file_rele(fp);
4496 	}
4497 
4498 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4499 	    RENAME4res *, resp);
4500 }
4501 
4502 /* ARGSUSED */
4503 static void
4504 rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4505 	struct compound_state *cs)
4506 {
4507 	RENEW4args *args = &argop->nfs_argop4_u.oprenew;
4508 	RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
4509 	rfs4_client_t *cp;
4510 
4511 	DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs,
4512 	    RENEW4args *, args);
4513 
4514 	if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
4515 		*cs->statusp = resp->status =
4516 		    rfs4_check_clientid(&args->clientid, 0);
4517 		goto out;
4518 	}
4519 
4520 	if (rfs4_lease_expired(cp)) {
4521 		rfs4_client_rele(cp);
4522 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
4523 		goto out;
4524 	}
4525 
4526 	rfs4_update_lease(cp);
4527 
4528 	mutex_enter(cp->cbinfo.cb_lock);
4529 	if (cp->cbinfo.cb_notified_of_cb_path_down == FALSE) {
4530 		cp->cbinfo.cb_notified_of_cb_path_down = TRUE;
4531 		*cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
4532 	} else {
4533 		*cs->statusp = resp->status = NFS4_OK;
4534 	}
4535 	mutex_exit(cp->cbinfo.cb_lock);
4536 
4537 	rfs4_client_rele(cp);
4538 
4539 out:
4540 	DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs,
4541 	    RENEW4res *, resp);
4542 }
4543 
4544 /* ARGSUSED */
4545 static void
4546 rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
4547 	struct compound_state *cs)
4548 {
4549 	RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
4550 
4551 	DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs);
4552 
4553 	/* No need to check cs->access - we are not accessing any object */
4554 	if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
4555 		*cs->statusp = resp->status = NFS4ERR_RESTOREFH;
4556 		goto out;
4557 	}
4558 	if (cs->vp != NULL) {
4559 		VN_RELE(cs->vp);
4560 	}
4561 	cs->vp = cs->saved_vp;
4562 	cs->saved_vp = NULL;
4563 	cs->exi = cs->saved_exi;
4564 	nfs_fh4_copy(&cs->saved_fh, &cs->fh);
4565 	*cs->statusp = resp->status = NFS4_OK;
4566 	cs->deleg = FALSE;
4567 
4568 out:
4569 	DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs,
4570 	    RESTOREFH4res *, resp);
4571 }
4572 
4573 /* ARGSUSED */
4574 static void
4575 rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4576 	struct compound_state *cs)
4577 {
4578 	SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
4579 
4580 	DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs);
4581 
4582 	/* No need to check cs->access - we are not accessing any object */
4583 	if (cs->vp == NULL) {
4584 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4585 		goto out;
4586 	}
4587 	if (cs->saved_vp != NULL) {
4588 		VN_RELE(cs->saved_vp);
4589 	}
4590 	cs->saved_vp = cs->vp;
4591 	VN_HOLD(cs->saved_vp);
4592 	cs->saved_exi = cs->exi;
4593 	/*
4594 	 * since SAVEFH is fairly rare, don't alloc space for its fh
4595 	 * unless necessary.
4596 	 */
4597 	if (cs->saved_fh.nfs_fh4_val == NULL) {
4598 		cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
4599 	}
4600 	nfs_fh4_copy(&cs->fh, &cs->saved_fh);
4601 	*cs->statusp = resp->status = NFS4_OK;
4602 
4603 out:
4604 	DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs,
4605 	    SAVEFH4res *, resp);
4606 }
4607 
4608 /*
4609  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
4610  * return the bitmap of attrs that were set successfully. It is also
4611  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
4612  * always be called only after rfs4_do_set_attrs().
4613  *
4614  * Verify that the attributes are same as the expected ones. sargp->vap
4615  * and sargp->sbp contain the input attributes as translated from fattr4.
4616  *
4617  * This function verifies only the attrs that correspond to a vattr or
4618  * vfsstat struct. That is because of the extra step needed to get the
4619  * corresponding system structs. Other attributes have already been set or
4620  * verified by do_rfs4_set_attrs.
4621  *
4622  * Return 0 if all attrs match, -1 if some don't, error if error processing.
4623  */
4624 static int
4625 rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
4626 	bitmap4 *resp, struct nfs4_ntov_table *ntovp)
4627 {
4628 	int error, ret_error = 0;
4629 	int i, k;
4630 	uint_t sva_mask = sargp->vap->va_mask;
4631 	uint_t vbit;
4632 	union nfs4_attr_u *na;
4633 	uint8_t *amap;
4634 	bool_t getsb = ntovp->vfsstat;
4635 
4636 	if (sva_mask != 0) {
4637 		/*
4638 		 * Okay to overwrite sargp->vap because we verify based
4639 		 * on the incoming values.
4640 		 */
4641 		ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
4642 		    sargp->cs->cr, NULL);
4643 		if (ret_error) {
4644 			if (resp == NULL)
4645 				return (ret_error);
4646 			/*
4647 			 * Must return bitmap of successful attrs
4648 			 */
4649 			sva_mask = 0;	/* to prevent checking vap later */
4650 		} else {
4651 			/*
4652 			 * Some file systems clobber va_mask. it is probably
4653 			 * wrong of them to do so, nonethless we practice
4654 			 * defensive coding.
4655 			 * See bug id 4276830.
4656 			 */
4657 			sargp->vap->va_mask = sva_mask;
4658 		}
4659 	}
4660 
4661 	if (getsb) {
4662 		/*
4663 		 * Now get the superblock and loop on the bitmap, as there is
4664 		 * no simple way of translating from superblock to bitmap4.
4665 		 */
4666 		ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
4667 		if (ret_error) {
4668 			if (resp == NULL)
4669 				goto errout;
4670 			getsb = FALSE;
4671 		}
4672 	}
4673 
4674 	/*
4675 	 * Now loop and verify each attribute which getattr returned
4676 	 * whether it's the same as the input.
4677 	 */
4678 	if (resp == NULL && !getsb && (sva_mask == 0))
4679 		goto errout;
4680 
4681 	na = ntovp->na;
4682 	amap = ntovp->amap;
4683 	k = 0;
4684 	for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
4685 		k = *amap;
4686 		ASSERT(nfs4_ntov_map[k].nval == k);
4687 		vbit = nfs4_ntov_map[k].vbit;
4688 
4689 		/*
4690 		 * If vattr attribute but VOP_GETATTR failed, or it's
4691 		 * superblock attribute but VFS_STATVFS failed, skip
4692 		 */
4693 		if (vbit) {
4694 			if ((vbit & sva_mask) == 0)
4695 				continue;
4696 		} else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
4697 			continue;
4698 		}
4699 		error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na);
4700 		if (resp != NULL) {
4701 			if (error)
4702 				ret_error = -1;	/* not all match */
4703 			else	/* update response bitmap */
4704 				*resp |= nfs4_ntov_map[k].fbit;
4705 			continue;
4706 		}
4707 		if (error) {
4708 			ret_error = -1;	/* not all match */
4709 			break;
4710 		}
4711 	}
4712 errout:
4713 	return (ret_error);
4714 }
4715 
4716 /*
4717  * Decode the attribute to be set/verified. If the attr requires a sys op
4718  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
4719  * call the sv_getit function for it, because the sys op hasn't yet been done.
4720  * Return 0 for success, error code if failed.
4721  *
4722  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
4723  */
4724 static int
4725 decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
4726 	int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
4727 {
4728 	int error = 0;
4729 	bool_t set_later;
4730 
4731 	sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
4732 
4733 	if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
4734 		set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
4735 		/*
4736 		 * don't verify yet if a vattr or sb dependent attr,
4737 		 * because we don't have their sys values yet.
4738 		 * Will be done later.
4739 		 */
4740 		if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
4741 			/*
4742 			 * ACLs are a special case, since setting the MODE
4743 			 * conflicts with setting the ACL.  We delay setting
4744 			 * the ACL until all other attributes have been set.
4745 			 * The ACL gets set in do_rfs4_op_setattr().
4746 			 */
4747 			if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
4748 				error = (*nfs4_ntov_map[k].sv_getit)(cmd,
4749 				    sargp, nap);
4750 				if (error) {
4751 					xdr_free(nfs4_ntov_map[k].xfunc,
4752 					    (caddr_t)nap);
4753 				}
4754 			}
4755 		}
4756 	} else {
4757 #ifdef  DEBUG
4758 		cmn_err(CE_NOTE, "decode_fattr4_attr: error "
4759 		    "decoding attribute %d\n", k);
4760 #endif
4761 		error = EINVAL;
4762 	}
4763 	if (!error && resp_bval && !set_later) {
4764 		*resp_bval |= nfs4_ntov_map[k].fbit;
4765 	}
4766 
4767 	return (error);
4768 }
4769 
4770 /*
4771  * Set vattr based on incoming fattr4 attrs - used by setattr.
4772  * Set response mask. Ignore any values that are not writable vattr attrs.
4773  */
4774 static nfsstat4
4775 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
4776 		struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
4777 		nfs4_attr_cmd_t cmd)
4778 {
4779 	int error = 0;
4780 	int i;
4781 	char *attrs = fattrp->attrlist4;
4782 	uint32_t attrslen = fattrp->attrlist4_len;
4783 	XDR xdr;
4784 	nfsstat4 status = NFS4_OK;
4785 	vnode_t *vp = cs->vp;
4786 	union nfs4_attr_u *na;
4787 	uint8_t *amap;
4788 
4789 #ifndef lint
4790 	/*
4791 	 * Make sure that maximum attribute number can be expressed as an
4792 	 * 8 bit quantity.
4793 	 */
4794 	ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
4795 #endif
4796 
4797 	if (vp == NULL) {
4798 		if (resp)
4799 			*resp = 0;
4800 		return (NFS4ERR_NOFILEHANDLE);
4801 	}
4802 	if (cs->access == CS_ACCESS_DENIED) {
4803 		if (resp)
4804 			*resp = 0;
4805 		return (NFS4ERR_ACCESS);
4806 	}
4807 
4808 	sargp->op = cmd;
4809 	sargp->cs = cs;
4810 	sargp->flag = 0;	/* may be set later */
4811 	sargp->vap->va_mask = 0;
4812 	sargp->rdattr_error = NFS4_OK;
4813 	sargp->rdattr_error_req = FALSE;
4814 	/* sargp->sbp is set by the caller */
4815 
4816 	xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
4817 
4818 	na = ntovp->na;
4819 	amap = ntovp->amap;
4820 
4821 	/*
4822 	 * The following loop iterates on the nfs4_ntov_map checking
4823 	 * if the fbit is set in the requested bitmap.
4824 	 * If set then we process the arguments using the
4825 	 * rfs4_fattr4 conversion functions to populate the setattr
4826 	 * vattr and va_mask. Any settable attrs that are not using vattr
4827 	 * will be set in this loop.
4828 	 */
4829 	for (i = 0; i < nfs4_ntov_map_size; i++) {
4830 		if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
4831 			continue;
4832 		}
4833 		/*
4834 		 * If setattr, must be a writable attr.
4835 		 * If verify/nverify, must be a readable attr.
4836 		 */
4837 		if ((error = (*nfs4_ntov_map[i].sv_getit)(
4838 		    NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
4839 			/*
4840 			 * Client tries to set/verify an
4841 			 * unsupported attribute, tries to set
4842 			 * a read only attr or verify a write
4843 			 * only one - error!
4844 			 */
4845 			break;
4846 		}
4847 		/*
4848 		 * Decode the attribute to set/verify
4849 		 */
4850 		error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
4851 		    &xdr, resp ? resp : NULL, na);
4852 		if (error)
4853 			break;
4854 		*amap++ = (uint8_t)nfs4_ntov_map[i].nval;
4855 		na++;
4856 		(ntovp->attrcnt)++;
4857 		if (nfs4_ntov_map[i].vfsstat)
4858 			ntovp->vfsstat = TRUE;
4859 	}
4860 
4861 	if (error != 0)
4862 		status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP :
4863 		    puterrno4(error));
4864 	/* xdrmem_destroy(&xdrs); */	/* NO-OP */
4865 	return (status);
4866 }
4867 
4868 static nfsstat4
4869 do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
4870 		stateid4 *stateid)
4871 {
4872 	int error = 0;
4873 	struct nfs4_svgetit_arg sarg;
4874 	bool_t trunc;
4875 
4876 	nfsstat4 status = NFS4_OK;
4877 	cred_t *cr = cs->cr;
4878 	vnode_t *vp = cs->vp;
4879 	struct nfs4_ntov_table ntov;
4880 	struct statvfs64 sb;
4881 	struct vattr bva;
4882 	struct flock64 bf;
4883 	int in_crit = 0;
4884 	uint_t saved_mask = 0;
4885 	caller_context_t ct;
4886 
4887 	*resp = 0;
4888 	sarg.sbp = &sb;
4889 	nfs4_ntov_table_init(&ntov);
4890 	status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
4891 	    NFS4ATTR_SETIT);
4892 	if (status != NFS4_OK) {
4893 		/*
4894 		 * failed set attrs
4895 		 */
4896 		goto done;
4897 	}
4898 	if ((sarg.vap->va_mask == 0) &&
4899 	    (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
4900 		/*
4901 		 * no further work to be done
4902 		 */
4903 		goto done;
4904 	}
4905 
4906 	/*
4907 	 * If we got a request to set the ACL and the MODE, only
4908 	 * allow changing VSUID, VSGID, and VSVTX.  Attempting
4909 	 * to change any other bits, along with setting an ACL,
4910 	 * gives NFS4ERR_INVAL.
4911 	 */
4912 	if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
4913 	    (fattrp->attrmask & FATTR4_MODE_MASK)) {
4914 		vattr_t va;
4915 
4916 		va.va_mask = AT_MODE;
4917 		error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
4918 		if (error) {
4919 			status = puterrno4(error);
4920 			goto done;
4921 		}
4922 		if ((sarg.vap->va_mode ^ va.va_mode) &
4923 		    ~(VSUID | VSGID | VSVTX)) {
4924 			status = NFS4ERR_INVAL;
4925 			goto done;
4926 		}
4927 	}
4928 
4929 	/* Check stateid only if size has been set */
4930 	if (sarg.vap->va_mask & AT_SIZE) {
4931 		trunc = (sarg.vap->va_size == 0);
4932 		status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
4933 		    trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct);
4934 		if (status != NFS4_OK)
4935 			goto done;
4936 	} else {
4937 		ct.cc_sysid = 0;
4938 		ct.cc_pid = 0;
4939 		ct.cc_caller_id = nfs4_srv_caller_id;
4940 		ct.cc_flags = CC_DONTBLOCK;
4941 	}
4942 
4943 	/* XXX start of possible race with delegations */
4944 
4945 	/*
4946 	 * We need to specially handle size changes because it is
4947 	 * possible for the client to create a file with read-only
4948 	 * modes, but with the file opened for writing. If the client
4949 	 * then tries to set the file size, e.g. ftruncate(3C),
4950 	 * fcntl(F_FREESP), the normal access checking done in
4951 	 * VOP_SETATTR would prevent the client from doing it even though
4952 	 * it should be allowed to do so.  To get around this, we do the
4953 	 * access checking for ourselves and use VOP_SPACE which doesn't
4954 	 * do the access checking.
4955 	 * Also the client should not be allowed to change the file
4956 	 * size if there is a conflicting non-blocking mandatory lock in
4957 	 * the region of the change.
4958 	 */
4959 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
4960 		u_offset_t offset;
4961 		ssize_t length;
4962 
4963 		/*
4964 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
4965 		 * before returning, sarg.vap->va_mask is used to
4966 		 * generate the setattr reply bitmap.  We also clear
4967 		 * AT_SIZE below before calling VOP_SPACE.  For both
4968 		 * of these cases, the va_mask needs to be saved here
4969 		 * and restored after calling VOP_SETATTR.
4970 		 */
4971 		saved_mask = sarg.vap->va_mask;
4972 
4973 		/*
4974 		 * Check any possible conflict due to NBMAND locks.
4975 		 * Get into critical region before VOP_GETATTR, so the
4976 		 * size attribute is valid when checking conflicts.
4977 		 */
4978 		if (nbl_need_check(vp)) {
4979 			nbl_start_crit(vp, RW_READER);
4980 			in_crit = 1;
4981 		}
4982 
4983 		bva.va_mask = AT_UID|AT_SIZE;
4984 		if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
4985 			status = puterrno4(error);
4986 			goto done;
4987 		}
4988 
4989 		if (in_crit) {
4990 			if (sarg.vap->va_size < bva.va_size) {
4991 				offset = sarg.vap->va_size;
4992 				length = bva.va_size - sarg.vap->va_size;
4993 			} else {
4994 				offset = bva.va_size;
4995 				length = sarg.vap->va_size - bva.va_size;
4996 			}
4997 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
4998 			    &ct)) {
4999 				status = NFS4ERR_LOCKED;
5000 				goto done;
5001 			}
5002 		}
5003 
5004 		if (crgetuid(cr) == bva.va_uid) {
5005 			sarg.vap->va_mask &= ~AT_SIZE;
5006 			bf.l_type = F_WRLCK;
5007 			bf.l_whence = 0;
5008 			bf.l_start = (off64_t)sarg.vap->va_size;
5009 			bf.l_len = 0;
5010 			bf.l_sysid = 0;
5011 			bf.l_pid = 0;
5012 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
5013 			    (offset_t)sarg.vap->va_size, cr, &ct);
5014 		}
5015 	}
5016 
5017 	if (!error && sarg.vap->va_mask != 0)
5018 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
5019 
5020 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
5021 	if (saved_mask & AT_SIZE)
5022 		sarg.vap->va_mask |= AT_SIZE;
5023 
5024 	/*
5025 	 * If an ACL was being set, it has been delayed until now,
5026 	 * in order to set the mode (via the VOP_SETATTR() above) first.
5027 	 */
5028 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
5029 		int i;
5030 
5031 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
5032 			if (ntov.amap[i] == FATTR4_ACL)
5033 				break;
5034 		if (i < NFS4_MAXNUM_ATTRS) {
5035 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
5036 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
5037 			if (error == 0) {
5038 				*resp |= FATTR4_ACL_MASK;
5039 			} else if (error == ENOTSUP) {
5040 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
5041 				status = NFS4ERR_ATTRNOTSUPP;
5042 				goto done;
5043 			}
5044 		} else {
5045 			NFS4_DEBUG(rfs4_debug,
5046 			    (CE_NOTE, "do_rfs4_op_setattr: "
5047 			    "unable to find ACL in fattr4"));
5048 			error = EINVAL;
5049 		}
5050 	}
5051 
5052 	if (error) {
5053 		/* check if a monitor detected a delegation conflict */
5054 		if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
5055 			status = NFS4ERR_DELAY;
5056 		else
5057 			status = puterrno4(error);
5058 
5059 		/*
5060 		 * Set the response bitmap when setattr failed.
5061 		 * If VOP_SETATTR partially succeeded, test by doing a
5062 		 * VOP_GETATTR on the object and comparing the data
5063 		 * to the setattr arguments.
5064 		 */
5065 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
5066 	} else {
5067 		/*
5068 		 * Force modified metadata out to stable storage.
5069 		 */
5070 		(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
5071 		/*
5072 		 * Set response bitmap
5073 		 */
5074 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
5075 	}
5076 
5077 /* Return early and already have a NFSv4 error */
5078 done:
5079 	/*
5080 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
5081 	 * conversion sets both readable and writeable NFS4 attrs
5082 	 * for AT_MTIME and AT_ATIME.  The line below masks out
5083 	 * unrequested attrs from the setattr result bitmap.  This
5084 	 * is placed after the done: label to catch the ATTRNOTSUP
5085 	 * case.
5086 	 */
5087 	*resp &= fattrp->attrmask;
5088 
5089 	if (in_crit)
5090 		nbl_end_crit(vp);
5091 
5092 	nfs4_ntov_table_free(&ntov, &sarg);
5093 
5094 	return (status);
5095 }
5096 
5097 /* ARGSUSED */
5098 static void
5099 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5100 	struct compound_state *cs)
5101 {
5102 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
5103 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
5104 	bslabel_t *clabel;
5105 
5106 	DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5107 	    SETATTR4args *, args);
5108 
5109 	if (cs->vp == NULL) {
5110 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5111 		goto out;
5112 	}
5113 
5114 	/*
5115 	 * If there is an unshared filesystem mounted on this vnode,
5116 	 * do not allow to setattr on this vnode.
5117 	 */
5118 	if (vn_ismntpt(cs->vp)) {
5119 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5120 		goto out;
5121 	}
5122 
5123 	resp->attrsset = 0;
5124 
5125 	if (rdonly4(cs->exi, cs->vp, req)) {
5126 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5127 		goto out;
5128 	}
5129 
5130 	/* check label before setting attributes */
5131 	if (is_system_labeled()) {
5132 		ASSERT(req->rq_label != NULL);
5133 		clabel = req->rq_label;
5134 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
5135 		    "got client label from request(1)",
5136 		    struct svc_req *, req);
5137 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
5138 			if (!do_rfs_label_check(clabel, cs->vp,
5139 			    EQUALITY_CHECK)) {
5140 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
5141 				goto out;
5142 			}
5143 		}
5144 	}
5145 
5146 	*cs->statusp = resp->status =
5147 	    do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
5148 	    &args->stateid);
5149 
5150 out:
5151 	DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5152 	    SETATTR4res *, resp);
5153 }
5154 
5155 /* ARGSUSED */
5156 static void
5157 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5158 	struct compound_state *cs)
5159 {
5160 	/*
5161 	 * verify and nverify are exactly the same, except that nverify
5162 	 * succeeds when some argument changed, and verify succeeds when
5163 	 * when none changed.
5164 	 */
5165 
5166 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
5167 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
5168 
5169 	int error;
5170 	struct nfs4_svgetit_arg sarg;
5171 	struct statvfs64 sb;
5172 	struct nfs4_ntov_table ntov;
5173 
5174 	DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5175 	    VERIFY4args *, args);
5176 
5177 	if (cs->vp == NULL) {
5178 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5179 		goto out;
5180 	}
5181 
5182 	sarg.sbp = &sb;
5183 	nfs4_ntov_table_init(&ntov);
5184 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5185 	    &sarg, &ntov, NFS4ATTR_VERIT);
5186 	if (resp->status != NFS4_OK) {
5187 		/*
5188 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5189 		 * so could return -1 for "no match".
5190 		 */
5191 		if (resp->status == -1)
5192 			resp->status = NFS4ERR_NOT_SAME;
5193 		goto done;
5194 	}
5195 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5196 	switch (error) {
5197 	case 0:
5198 		resp->status = NFS4_OK;
5199 		break;
5200 	case -1:
5201 		resp->status = NFS4ERR_NOT_SAME;
5202 		break;
5203 	default:
5204 		resp->status = puterrno4(error);
5205 		break;
5206 	}
5207 done:
5208 	*cs->statusp = resp->status;
5209 	nfs4_ntov_table_free(&ntov, &sarg);
5210 out:
5211 	DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5212 	    VERIFY4res *, resp);
5213 }
5214 
5215 /* ARGSUSED */
5216 static void
5217 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5218 	struct compound_state *cs)
5219 {
5220 	/*
5221 	 * verify and nverify are exactly the same, except that nverify
5222 	 * succeeds when some argument changed, and verify succeeds when
5223 	 * when none changed.
5224 	 */
5225 
5226 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
5227 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
5228 
5229 	int error;
5230 	struct nfs4_svgetit_arg sarg;
5231 	struct statvfs64 sb;
5232 	struct nfs4_ntov_table ntov;
5233 
5234 	DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5235 	    NVERIFY4args *, args);
5236 
5237 	if (cs->vp == NULL) {
5238 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5239 		DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5240 		    NVERIFY4res *, resp);
5241 		return;
5242 	}
5243 	sarg.sbp = &sb;
5244 	nfs4_ntov_table_init(&ntov);
5245 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5246 	    &sarg, &ntov, NFS4ATTR_VERIT);
5247 	if (resp->status != NFS4_OK) {
5248 		/*
5249 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5250 		 * so could return -1 for "no match".
5251 		 */
5252 		if (resp->status == -1)
5253 			resp->status = NFS4_OK;
5254 		goto done;
5255 	}
5256 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5257 	switch (error) {
5258 	case 0:
5259 		resp->status = NFS4ERR_SAME;
5260 		break;
5261 	case -1:
5262 		resp->status = NFS4_OK;
5263 		break;
5264 	default:
5265 		resp->status = puterrno4(error);
5266 		break;
5267 	}
5268 done:
5269 	*cs->statusp = resp->status;
5270 	nfs4_ntov_table_free(&ntov, &sarg);
5271 
5272 	DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5273 	    NVERIFY4res *, resp);
5274 }
5275 
5276 /*
5277  * XXX - This should live in an NFS header file.
5278  */
5279 #define	MAX_IOVECS	12
5280 
5281 /* ARGSUSED */
5282 static void
5283 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5284 	struct compound_state *cs)
5285 {
5286 	WRITE4args *args = &argop->nfs_argop4_u.opwrite;
5287 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
5288 	int error;
5289 	vnode_t *vp;
5290 	struct vattr bva;
5291 	u_offset_t rlimit;
5292 	struct uio uio;
5293 	struct iovec iov[MAX_IOVECS];
5294 	struct iovec *iovp;
5295 	int iovcnt;
5296 	int ioflag;
5297 	cred_t *savecred, *cr;
5298 	bool_t *deleg = &cs->deleg;
5299 	nfsstat4 stat;
5300 	int in_crit = 0;
5301 	caller_context_t ct;
5302 
5303 	DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5304 	    WRITE4args *, args);
5305 
5306 	vp = cs->vp;
5307 	if (vp == NULL) {
5308 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5309 		goto out;
5310 	}
5311 	if (cs->access == CS_ACCESS_DENIED) {
5312 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5313 		goto out;
5314 	}
5315 
5316 	cr = cs->cr;
5317 
5318 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5319 	    deleg, TRUE, &ct)) != NFS4_OK) {
5320 		*cs->statusp = resp->status = stat;
5321 		goto out;
5322 	}
5323 
5324 	/*
5325 	 * We have to enter the critical region before calling VOP_RWLOCK
5326 	 * to avoid a deadlock with ufs.
5327 	 */
5328 	if (nbl_need_check(vp)) {
5329 		nbl_start_crit(vp, RW_READER);
5330 		in_crit = 1;
5331 		if (nbl_conflict(vp, NBL_WRITE,
5332 		    args->offset, args->data_len, 0, &ct)) {
5333 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
5334 			goto out;
5335 		}
5336 	}
5337 
5338 	bva.va_mask = AT_MODE | AT_UID;
5339 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
5340 
5341 	/*
5342 	 * If we can't get the attributes, then we can't do the
5343 	 * right access checking.  So, we'll fail the request.
5344 	 */
5345 	if (error) {
5346 		*cs->statusp = resp->status = puterrno4(error);
5347 		goto out;
5348 	}
5349 
5350 	if (rdonly4(cs->exi, cs->vp, req)) {
5351 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5352 		goto out;
5353 	}
5354 
5355 	if (vp->v_type != VREG) {
5356 		*cs->statusp = resp->status =
5357 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
5358 		goto out;
5359 	}
5360 
5361 	if (crgetuid(cr) != bva.va_uid &&
5362 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
5363 		*cs->statusp = resp->status = puterrno4(error);
5364 		goto out;
5365 	}
5366 
5367 	if (MANDLOCK(vp, bva.va_mode)) {
5368 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5369 		goto out;
5370 	}
5371 
5372 	if (args->data_len == 0) {
5373 		*cs->statusp = resp->status = NFS4_OK;
5374 		resp->count = 0;
5375 		resp->committed = args->stable;
5376 		resp->writeverf = Write4verf;
5377 		goto out;
5378 	}
5379 
5380 	if (args->mblk != NULL) {
5381 		mblk_t *m;
5382 		uint_t bytes, round_len;
5383 
5384 		iovcnt = 0;
5385 		bytes = 0;
5386 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
5387 		for (m = args->mblk;
5388 		    m != NULL && bytes < round_len;
5389 		    m = m->b_cont) {
5390 			iovcnt++;
5391 			bytes += MBLKL(m);
5392 		}
5393 #ifdef DEBUG
5394 		/* should have ended on an mblk boundary */
5395 		if (bytes != round_len) {
5396 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
5397 			    bytes, round_len, args->data_len);
5398 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
5399 			    (void *)args->mblk, (void *)m);
5400 			ASSERT(bytes == round_len);
5401 		}
5402 #endif
5403 		if (iovcnt <= MAX_IOVECS) {
5404 			iovp = iov;
5405 		} else {
5406 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
5407 		}
5408 		mblk_to_iov(args->mblk, iovcnt, iovp);
5409 	} else if (args->rlist != NULL) {
5410 		iovcnt = 1;
5411 		iovp = iov;
5412 		iovp->iov_base = (char *)((args->rlist)->u.c_daddr3);
5413 		iovp->iov_len = args->data_len;
5414 	} else {
5415 		iovcnt = 1;
5416 		iovp = iov;
5417 		iovp->iov_base = args->data_val;
5418 		iovp->iov_len = args->data_len;
5419 	}
5420 
5421 	uio.uio_iov = iovp;
5422 	uio.uio_iovcnt = iovcnt;
5423 
5424 	uio.uio_segflg = UIO_SYSSPACE;
5425 	uio.uio_extflg = UIO_COPY_DEFAULT;
5426 	uio.uio_loffset = args->offset;
5427 	uio.uio_resid = args->data_len;
5428 	uio.uio_llimit = curproc->p_fsz_ctl;
5429 	rlimit = uio.uio_llimit - args->offset;
5430 	if (rlimit < (u_offset_t)uio.uio_resid)
5431 		uio.uio_resid = (int)rlimit;
5432 
5433 	if (args->stable == UNSTABLE4)
5434 		ioflag = 0;
5435 	else if (args->stable == FILE_SYNC4)
5436 		ioflag = FSYNC;
5437 	else if (args->stable == DATA_SYNC4)
5438 		ioflag = FDSYNC;
5439 	else {
5440 		if (iovp != iov)
5441 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
5442 		*cs->statusp = resp->status = NFS4ERR_INVAL;
5443 		goto out;
5444 	}
5445 
5446 	/*
5447 	 * We're changing creds because VM may fault and we need
5448 	 * the cred of the current thread to be used if quota
5449 	 * checking is enabled.
5450 	 */
5451 	savecred = curthread->t_cred;
5452 	curthread->t_cred = cr;
5453 	error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
5454 	curthread->t_cred = savecred;
5455 
5456 	if (iovp != iov)
5457 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
5458 
5459 	if (error) {
5460 		*cs->statusp = resp->status = puterrno4(error);
5461 		goto out;
5462 	}
5463 
5464 	*cs->statusp = resp->status = NFS4_OK;
5465 	resp->count = args->data_len - uio.uio_resid;
5466 
5467 	if (ioflag == 0)
5468 		resp->committed = UNSTABLE4;
5469 	else
5470 		resp->committed = FILE_SYNC4;
5471 
5472 	resp->writeverf = Write4verf;
5473 
5474 out:
5475 	if (in_crit)
5476 		nbl_end_crit(vp);
5477 
5478 	DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5479 	    WRITE4res *, resp);
5480 }
5481 
5482 
5483 /* XXX put in a header file */
5484 extern int	sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
5485 
5486 void
5487 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
5488 	struct svc_req *req, cred_t *cr, int *rv)
5489 {
5490 	uint_t i;
5491 	struct compound_state cs;
5492 
5493 	if (rv != NULL)
5494 		*rv = 0;
5495 	rfs4_init_compound_state(&cs);
5496 	/*
5497 	 * Form a reply tag by copying over the reqeuest tag.
5498 	 */
5499 	resp->tag.utf8string_val =
5500 	    kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5501 	resp->tag.utf8string_len = args->tag.utf8string_len;
5502 	bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5503 	    resp->tag.utf8string_len);
5504 
5505 	cs.statusp = &resp->status;
5506 	cs.req = req;
5507 
5508 	/*
5509 	 * XXX for now, minorversion should be zero
5510 	 */
5511 	if (args->minorversion != NFS4_MINORVERSION) {
5512 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5513 		    &cs, COMPOUND4args *, args);
5514 		resp->array_len = 0;
5515 		resp->array = NULL;
5516 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5517 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5518 		    &cs, COMPOUND4res *, resp);
5519 		return;
5520 	}
5521 
5522 	ASSERT(exi == NULL);
5523 	ASSERT(cr == NULL);
5524 
5525 	cr = crget();
5526 	ASSERT(cr != NULL);
5527 
5528 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5529 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5530 		    &cs, COMPOUND4args *, args);
5531 		crfree(cr);
5532 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5533 		    &cs, COMPOUND4res *, resp);
5534 		svcerr_badcred(req->rq_xprt);
5535 		if (rv != NULL)
5536 			*rv = 1;
5537 		return;
5538 	}
5539 	resp->array_len = args->array_len;
5540 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5541 	    KM_SLEEP);
5542 
5543 	cs.basecr = cr;
5544 
5545 	DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5546 	    COMPOUND4args *, args);
5547 
5548 	/*
5549 	 * For now, NFS4 compound processing must be protected by
5550 	 * exported_lock because it can access more than one exportinfo
5551 	 * per compound and share/unshare can now change multiple
5552 	 * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5553 	 * per proc (excluding public exinfo), and exi_count design
5554 	 * is sufficient to protect concurrent execution of NFS2/3
5555 	 * ops along with unexport.  This lock will be removed as
5556 	 * part of the NFSv4 phase 2 namespace redesign work.
5557 	 */
5558 	rw_enter(&exported_lock, RW_READER);
5559 
5560 	/*
5561 	 * If this is the first compound we've seen, we need to start all
5562 	 * new instances' grace periods.
5563 	 */
5564 	if (rfs4_seen_first_compound == 0) {
5565 		rfs4_grace_start_new();
5566 		/*
5567 		 * This must be set after rfs4_grace_start_new(), otherwise
5568 		 * another thread could proceed past here before the former
5569 		 * is finished.
5570 		 */
5571 		rfs4_seen_first_compound = 1;
5572 	}
5573 
5574 	for (i = 0; i < args->array_len && cs.cont; i++) {
5575 		nfs_argop4 *argop;
5576 		nfs_resop4 *resop;
5577 		uint_t op;
5578 
5579 		argop = &args->array[i];
5580 		resop = &resp->array[i];
5581 		resop->resop = argop->argop;
5582 		op = (uint_t)resop->resop;
5583 
5584 		if (op < rfsv4disp_cnt) {
5585 			/*
5586 			 * Count the individual ops here; NULL and COMPOUND
5587 			 * are counted in common_dispatch()
5588 			 */
5589 			rfsproccnt_v4_ptr[op].value.ui64++;
5590 
5591 			NFS4_DEBUG(rfs4_debug > 1,
5592 			    (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5593 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5594 			NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5595 			    rfs4_op_string[op], *cs.statusp));
5596 			if (*cs.statusp != NFS4_OK)
5597 				cs.cont = FALSE;
5598 		} else {
5599 			/*
5600 			 * This is effectively dead code since XDR code
5601 			 * will have already returned BADXDR if op doesn't
5602 			 * decode to legal value.  This only done for a
5603 			 * day when XDR code doesn't verify v4 opcodes.
5604 			 */
5605 			op = OP_ILLEGAL;
5606 			rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
5607 
5608 			rfs4_op_illegal(argop, resop, req, &cs);
5609 			cs.cont = FALSE;
5610 		}
5611 
5612 		/*
5613 		 * If not at last op, and if we are to stop, then
5614 		 * compact the results array.
5615 		 */
5616 		if ((i + 1) < args->array_len && !cs.cont) {
5617 			nfs_resop4 *new_res = kmem_alloc(
5618 			    (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5619 			bcopy(resp->array,
5620 			    new_res, (i+1) * sizeof (nfs_resop4));
5621 			kmem_free(resp->array,
5622 			    args->array_len * sizeof (nfs_resop4));
5623 
5624 			resp->array_len =  i + 1;
5625 			resp->array = new_res;
5626 		}
5627 	}
5628 
5629 	rw_exit(&exported_lock);
5630 
5631 	DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5632 	    COMPOUND4res *, resp);
5633 
5634 	if (cs.vp)
5635 		VN_RELE(cs.vp);
5636 	if (cs.saved_vp)
5637 		VN_RELE(cs.saved_vp);
5638 	if (cs.saved_fh.nfs_fh4_val)
5639 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5640 
5641 	if (cs.basecr)
5642 		crfree(cs.basecr);
5643 	if (cs.cr)
5644 		crfree(cs.cr);
5645 	/*
5646 	 * done with this compound request, free the label
5647 	 */
5648 
5649 	if (req->rq_label != NULL) {
5650 		kmem_free(req->rq_label, sizeof (bslabel_t));
5651 		req->rq_label = NULL;
5652 	}
5653 }
5654 
5655 /*
5656  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5657  * XXX zero out the tag and array values. Need to investigate why the
5658  * XXX calls occur, but at least prevent the panic for now.
5659  */
5660 void
5661 rfs4_compound_free(COMPOUND4res *resp)
5662 {
5663 	uint_t i;
5664 
5665 	if (resp->tag.utf8string_val) {
5666 		UTF8STRING_FREE(resp->tag)
5667 	}
5668 
5669 	for (i = 0; i < resp->array_len; i++) {
5670 		nfs_resop4 *resop;
5671 		uint_t op;
5672 
5673 		resop = &resp->array[i];
5674 		op = (uint_t)resop->resop;
5675 		if (op < rfsv4disp_cnt) {
5676 			(*rfsv4disptab[op].dis_resfree)(resop);
5677 		}
5678 	}
5679 	if (resp->array != NULL) {
5680 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
5681 	}
5682 }
5683 
5684 /*
5685  * Process the value of the compound request rpc flags, as a bit-AND
5686  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
5687  */
5688 void
5689 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
5690 {
5691 	int i;
5692 	int flag = RPC_ALL;
5693 
5694 	for (i = 0; flag && i < args->array_len; i++) {
5695 		uint_t op;
5696 
5697 		op = (uint_t)args->array[i].argop;
5698 
5699 		if (op < rfsv4disp_cnt)
5700 			flag &= rfsv4disptab[op].dis_flags;
5701 		else
5702 			flag = 0;
5703 	}
5704 	*flagp = flag;
5705 }
5706 
5707 nfsstat4
5708 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
5709 {
5710 	nfsstat4 e;
5711 
5712 	rfs4_dbe_lock(cp->dbe);
5713 
5714 	if (cp->sysidt != LM_NOSYSID) {
5715 		*sp = cp->sysidt;
5716 		e = NFS4_OK;
5717 
5718 	} else if ((cp->sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
5719 		*sp = cp->sysidt;
5720 		e = NFS4_OK;
5721 
5722 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
5723 		    "rfs4_client_sysid: allocated 0x%x\n", *sp));
5724 	} else
5725 		e = NFS4ERR_DELAY;
5726 
5727 	rfs4_dbe_unlock(cp->dbe);
5728 	return (e);
5729 }
5730 
5731 #if defined(DEBUG) && ! defined(lint)
5732 static void lock_print(char *str, int operation, struct flock64 *flk)
5733 {
5734 	char *op, *type;
5735 
5736 	switch (operation) {
5737 	case F_GETLK: op = "F_GETLK";
5738 		break;
5739 	case F_SETLK: op = "F_SETLK";
5740 		break;
5741 	case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
5742 		break;
5743 	default: op = "F_UNKNOWN";
5744 		break;
5745 	}
5746 	switch (flk->l_type) {
5747 	case F_UNLCK: type = "F_UNLCK";
5748 		break;
5749 	case F_RDLCK: type = "F_RDLCK";
5750 		break;
5751 	case F_WRLCK: type = "F_WRLCK";
5752 		break;
5753 	default: type = "F_UNKNOWN";
5754 		break;
5755 	}
5756 
5757 	ASSERT(flk->l_whence == 0);
5758 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
5759 	    str, op, type, (longlong_t)flk->l_start,
5760 	    flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
5761 }
5762 
5763 #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
5764 #else
5765 #define	LOCK_PRINT(d, s, t, f)
5766 #endif
5767 
5768 /*ARGSUSED*/
5769 static bool_t
5770 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
5771 {
5772 	return (TRUE);
5773 }
5774 
5775 /*
5776  * Look up the pathname using the vp in cs as the directory vnode.
5777  * cs->vp will be the vnode for the file on success
5778  */
5779 
5780 static nfsstat4
5781 rfs4_lookup(component4 *component, struct svc_req *req,
5782 	    struct compound_state *cs)
5783 {
5784 	char *nm;
5785 	uint32_t len;
5786 	nfsstat4 status;
5787 
5788 	if (cs->vp == NULL) {
5789 		return (NFS4ERR_NOFILEHANDLE);
5790 	}
5791 	if (cs->vp->v_type != VDIR) {
5792 		return (NFS4ERR_NOTDIR);
5793 	}
5794 
5795 	if (!utf8_dir_verify(component))
5796 		return (NFS4ERR_INVAL);
5797 
5798 	nm = utf8_to_fn(component, &len, NULL);
5799 	if (nm == NULL) {
5800 		return (NFS4ERR_INVAL);
5801 	}
5802 
5803 	if (len > MAXNAMELEN) {
5804 		kmem_free(nm, len);
5805 		return (NFS4ERR_NAMETOOLONG);
5806 	}
5807 
5808 	status = do_rfs4_op_lookup(nm, len, req, cs);
5809 
5810 	kmem_free(nm, len);
5811 
5812 	return (status);
5813 }
5814 
5815 static nfsstat4
5816 rfs4_lookupfile(component4 *component, struct svc_req *req,
5817 		struct compound_state *cs, uint32_t access,
5818 		change_info4 *cinfo)
5819 {
5820 	nfsstat4 status;
5821 	vnode_t *dvp = cs->vp;
5822 	vattr_t bva, ava, fva;
5823 	int error;
5824 
5825 	/* Get "before" change value */
5826 	bva.va_mask = AT_CTIME|AT_SEQ;
5827 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
5828 	if (error)
5829 		return (puterrno4(error));
5830 
5831 	/* rfs4_lookup may VN_RELE directory */
5832 	VN_HOLD(dvp);
5833 
5834 	status = rfs4_lookup(component, req, cs);
5835 	if (status != NFS4_OK) {
5836 		VN_RELE(dvp);
5837 		return (status);
5838 	}
5839 
5840 	/*
5841 	 * Get "after" change value, if it fails, simply return the
5842 	 * before value.
5843 	 */
5844 	ava.va_mask = AT_CTIME|AT_SEQ;
5845 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
5846 		ava.va_ctime = bva.va_ctime;
5847 		ava.va_seq = 0;
5848 	}
5849 	VN_RELE(dvp);
5850 
5851 	/*
5852 	 * Validate the file is a file
5853 	 */
5854 	fva.va_mask = AT_TYPE|AT_MODE;
5855 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
5856 	if (error)
5857 		return (puterrno4(error));
5858 
5859 	if (fva.va_type != VREG) {
5860 		if (fva.va_type == VDIR)
5861 			return (NFS4ERR_ISDIR);
5862 		if (fva.va_type == VLNK)
5863 			return (NFS4ERR_SYMLINK);
5864 		return (NFS4ERR_INVAL);
5865 	}
5866 
5867 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
5868 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
5869 
5870 	/*
5871 	 * It is undefined if VOP_LOOKUP will change va_seq, so
5872 	 * cinfo.atomic = TRUE only if we have
5873 	 * non-zero va_seq's, and they have not changed.
5874 	 */
5875 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
5876 		cinfo->atomic = TRUE;
5877 	else
5878 		cinfo->atomic = FALSE;
5879 
5880 	/* Check for mandatory locking */
5881 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
5882 	return (check_open_access(access, cs, req));
5883 }
5884 
5885 static nfsstat4
5886 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
5887 	    timespec32_t *mtime, cred_t *cr, vnode_t **vpp, bool_t *created)
5888 {
5889 	int error;
5890 	nfsstat4 status = NFS4_OK;
5891 	vattr_t va;
5892 
5893 tryagain:
5894 
5895 	/*
5896 	 * The file open mode used is VWRITE.  If the client needs
5897 	 * some other semantic, then it should do the access checking
5898 	 * itself.  It would have been nice to have the file open mode
5899 	 * passed as part of the arguments.
5900 	 */
5901 
5902 	*created = TRUE;
5903 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
5904 
5905 	if (error) {
5906 		*created = FALSE;
5907 
5908 		/*
5909 		 * If we got something other than file already exists
5910 		 * then just return this error.  Otherwise, we got
5911 		 * EEXIST.  If we were doing a GUARDED create, then
5912 		 * just return this error.  Otherwise, we need to
5913 		 * make sure that this wasn't a duplicate of an
5914 		 * exclusive create request.
5915 		 *
5916 		 * The assumption is made that a non-exclusive create
5917 		 * request will never return EEXIST.
5918 		 */
5919 
5920 		if (error != EEXIST || mode == GUARDED4) {
5921 			status = puterrno4(error);
5922 			return (status);
5923 		}
5924 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
5925 		    NULL, NULL, NULL);
5926 
5927 		if (error) {
5928 			/*
5929 			 * We couldn't find the file that we thought that
5930 			 * we just created.  So, we'll just try creating
5931 			 * it again.
5932 			 */
5933 			if (error == ENOENT)
5934 				goto tryagain;
5935 
5936 			status = puterrno4(error);
5937 			return (status);
5938 		}
5939 
5940 		if (mode == UNCHECKED4) {
5941 			/* existing object must be regular file */
5942 			if ((*vpp)->v_type != VREG) {
5943 				if ((*vpp)->v_type == VDIR)
5944 					status = NFS4ERR_ISDIR;
5945 				else if ((*vpp)->v_type == VLNK)
5946 					status = NFS4ERR_SYMLINK;
5947 				else
5948 					status = NFS4ERR_INVAL;
5949 				VN_RELE(*vpp);
5950 				return (status);
5951 			}
5952 
5953 			return (NFS4_OK);
5954 		}
5955 
5956 		/* Check for duplicate request */
5957 		ASSERT(mtime != 0);
5958 		va.va_mask = AT_MTIME;
5959 		error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
5960 		if (!error) {
5961 			/* We found the file */
5962 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
5963 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
5964 				/* but its not our creation */
5965 				VN_RELE(*vpp);
5966 				return (NFS4ERR_EXIST);
5967 			}
5968 			*created = TRUE; /* retrans of create == created */
5969 			return (NFS4_OK);
5970 		}
5971 		VN_RELE(*vpp);
5972 		return (NFS4ERR_EXIST);
5973 	}
5974 
5975 	return (NFS4_OK);
5976 }
5977 
5978 static nfsstat4
5979 check_open_access(uint32_t access,
5980 		struct compound_state *cs, struct svc_req *req)
5981 {
5982 	int error;
5983 	vnode_t *vp;
5984 	bool_t readonly;
5985 	cred_t *cr = cs->cr;
5986 
5987 	/* For now we don't allow mandatory locking as per V2/V3 */
5988 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
5989 		return (NFS4ERR_ACCESS);
5990 	}
5991 
5992 	vp = cs->vp;
5993 	ASSERT(cr != NULL && vp->v_type == VREG);
5994 
5995 	/*
5996 	 * If the file system is exported read only and we are trying
5997 	 * to open for write, then return NFS4ERR_ROFS
5998 	 */
5999 
6000 	readonly = rdonly4(cs->exi, cs->vp, req);
6001 
6002 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
6003 		return (NFS4ERR_ROFS);
6004 
6005 	if (access & OPEN4_SHARE_ACCESS_READ) {
6006 		if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
6007 		    (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
6008 			return (NFS4ERR_ACCESS);
6009 		}
6010 	}
6011 
6012 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
6013 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
6014 		if (error)
6015 			return (NFS4ERR_ACCESS);
6016 	}
6017 
6018 	return (NFS4_OK);
6019 }
6020 
6021 static nfsstat4
6022 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
6023 		change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
6024 {
6025 	struct nfs4_svgetit_arg sarg;
6026 	struct nfs4_ntov_table ntov;
6027 
6028 	bool_t ntov_table_init = FALSE;
6029 	struct statvfs64 sb;
6030 	nfsstat4 status;
6031 	vnode_t *vp;
6032 	vattr_t bva, ava, iva, cva, *vap;
6033 	vnode_t *dvp;
6034 	timespec32_t *mtime;
6035 	char *nm = NULL;
6036 	uint_t buflen;
6037 	bool_t created;
6038 	bool_t setsize = FALSE;
6039 	len_t reqsize;
6040 	int error;
6041 	bool_t trunc;
6042 	caller_context_t ct;
6043 	component4 *component;
6044 	bslabel_t *clabel;
6045 
6046 	sarg.sbp = &sb;
6047 
6048 	dvp = cs->vp;
6049 
6050 	/* Check if the file system is read only */
6051 	if (rdonly4(cs->exi, dvp, req))
6052 		return (NFS4ERR_ROFS);
6053 
6054 	/* check the label of including directory */
6055 	if (is_system_labeled()) {
6056 		ASSERT(req->rq_label != NULL);
6057 		clabel = req->rq_label;
6058 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6059 		    "got client label from request(1)",
6060 		    struct svc_req *, req);
6061 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
6062 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK)) {
6063 				return (NFS4ERR_ACCESS);
6064 			}
6065 		}
6066 	}
6067 
6068 	/*
6069 	 * Get the last component of path name in nm. cs will reference
6070 	 * the including directory on success.
6071 	 */
6072 	component = &args->open_claim4_u.file;
6073 	if (!utf8_dir_verify(component))
6074 		return (NFS4ERR_INVAL);
6075 
6076 	nm = utf8_to_fn(component, &buflen, NULL);
6077 
6078 	if (nm == NULL)
6079 		return (NFS4ERR_RESOURCE);
6080 
6081 	if (buflen > MAXNAMELEN) {
6082 		kmem_free(nm, buflen);
6083 		return (NFS4ERR_NAMETOOLONG);
6084 	}
6085 
6086 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6087 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6088 	if (error) {
6089 		kmem_free(nm, buflen);
6090 		return (puterrno4(error));
6091 	}
6092 
6093 	if (bva.va_type != VDIR) {
6094 		kmem_free(nm, buflen);
6095 		return (NFS4ERR_NOTDIR);
6096 	}
6097 
6098 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6099 
6100 	switch (args->mode) {
6101 	case GUARDED4:
6102 		/*FALLTHROUGH*/
6103 	case UNCHECKED4:
6104 		nfs4_ntov_table_init(&ntov);
6105 		ntov_table_init = TRUE;
6106 
6107 		*attrset = 0;
6108 		status = do_rfs4_set_attrs(attrset,
6109 		    &args->createhow4_u.createattrs,
6110 		    cs, &sarg, &ntov, NFS4ATTR_SETIT);
6111 
6112 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6113 		    sarg.vap->va_type != VREG) {
6114 			if (sarg.vap->va_type == VDIR)
6115 				status = NFS4ERR_ISDIR;
6116 			else if (sarg.vap->va_type == VLNK)
6117 				status = NFS4ERR_SYMLINK;
6118 			else
6119 				status = NFS4ERR_INVAL;
6120 		}
6121 
6122 		if (status != NFS4_OK) {
6123 			kmem_free(nm, buflen);
6124 			nfs4_ntov_table_free(&ntov, &sarg);
6125 			*attrset = 0;
6126 			return (status);
6127 		}
6128 
6129 		vap = sarg.vap;
6130 		vap->va_type = VREG;
6131 		vap->va_mask |= AT_TYPE;
6132 
6133 		if ((vap->va_mask & AT_MODE) == 0) {
6134 			vap->va_mask |= AT_MODE;
6135 			vap->va_mode = (mode_t)0600;
6136 		}
6137 
6138 		if (vap->va_mask & AT_SIZE) {
6139 
6140 			/* Disallow create with a non-zero size */
6141 
6142 			if ((reqsize = sarg.vap->va_size) != 0) {
6143 				kmem_free(nm, buflen);
6144 				nfs4_ntov_table_free(&ntov, &sarg);
6145 				*attrset = 0;
6146 				return (NFS4ERR_INVAL);
6147 			}
6148 			setsize = TRUE;
6149 		}
6150 		break;
6151 
6152 	case EXCLUSIVE4:
6153 		/* prohibit EXCL create of named attributes */
6154 		if (dvp->v_flag & V_XATTRDIR) {
6155 			kmem_free(nm, buflen);
6156 			*attrset = 0;
6157 			return (NFS4ERR_INVAL);
6158 		}
6159 
6160 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6161 		cva.va_type = VREG;
6162 		/*
6163 		 * Ensure no time overflows. Assumes underlying
6164 		 * filesystem supports at least 32 bits.
6165 		 * Truncate nsec to usec resolution to allow valid
6166 		 * compares even if the underlying filesystem truncates.
6167 		 */
6168 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
6169 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6170 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6171 		cva.va_mode = (mode_t)0;
6172 		vap = &cva;
6173 		break;
6174 	}
6175 
6176 	status = create_vnode(dvp, nm, vap, args->mode, mtime,
6177 	    cs->cr, &vp, &created);
6178 	kmem_free(nm, buflen);
6179 
6180 	if (status != NFS4_OK) {
6181 		if (ntov_table_init)
6182 			nfs4_ntov_table_free(&ntov, &sarg);
6183 		*attrset = 0;
6184 		return (status);
6185 	}
6186 
6187 	trunc = (setsize && !created);
6188 
6189 	if (args->mode != EXCLUSIVE4) {
6190 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6191 
6192 		/*
6193 		 * True verification that object was created with correct
6194 		 * attrs is impossible.  The attrs could have been changed
6195 		 * immediately after object creation.  If attributes did
6196 		 * not verify, the only recourse for the server is to
6197 		 * destroy the object.  Maybe if some attrs (like gid)
6198 		 * are set incorrectly, the object should be destroyed;
6199 		 * however, seems bad as a default policy.  Do we really
6200 		 * want to destroy an object over one of the times not
6201 		 * verifying correctly?  For these reasons, the server
6202 		 * currently sets bits in attrset for createattrs
6203 		 * that were set; however, no verification is done.
6204 		 *
6205 		 * vmask_to_nmask accounts for vattr bits set on create
6206 		 *	[do_rfs4_set_attrs() only sets resp bits for
6207 		 *	 non-vattr/vfs bits.]
6208 		 * Mask off any bits we set by default so as not to return
6209 		 * more attrset bits than were requested in createattrs
6210 		 */
6211 		if (created) {
6212 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6213 			*attrset &= createmask;
6214 		} else {
6215 			/*
6216 			 * We did not create the vnode (we tried but it
6217 			 * already existed).  In this case, the only createattr
6218 			 * that the spec allows the server to set is size,
6219 			 * and even then, it can only be set if it is 0.
6220 			 */
6221 			*attrset = 0;
6222 			if (trunc)
6223 				*attrset = FATTR4_SIZE_MASK;
6224 		}
6225 	}
6226 	if (ntov_table_init)
6227 		nfs4_ntov_table_free(&ntov, &sarg);
6228 
6229 	/*
6230 	 * Get the initial "after" sequence number, if it fails,
6231 	 * set to zero, time to before.
6232 	 */
6233 	iva.va_mask = AT_CTIME|AT_SEQ;
6234 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6235 		iva.va_seq = 0;
6236 		iva.va_ctime = bva.va_ctime;
6237 	}
6238 
6239 	/*
6240 	 * create_vnode attempts to create the file exclusive,
6241 	 * if it already exists the VOP_CREATE will fail and
6242 	 * may not increase va_seq. It is atomic if
6243 	 * we haven't changed the directory, but if it has changed
6244 	 * we don't know what changed it.
6245 	 */
6246 	if (!created) {
6247 		if (bva.va_seq && iva.va_seq &&
6248 		    bva.va_seq == iva.va_seq)
6249 			cinfo->atomic = TRUE;
6250 		else
6251 			cinfo->atomic = FALSE;
6252 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6253 	} else {
6254 		/*
6255 		 * The entry was created, we need to sync the
6256 		 * directory metadata.
6257 		 */
6258 		(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6259 
6260 		/*
6261 		 * Get "after" change value, if it fails, simply return the
6262 		 * before value.
6263 		 */
6264 		ava.va_mask = AT_CTIME|AT_SEQ;
6265 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6266 			ava.va_ctime = bva.va_ctime;
6267 			ava.va_seq = 0;
6268 		}
6269 
6270 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6271 
6272 		/*
6273 		 * The cinfo->atomic = TRUE only if we have
6274 		 * non-zero va_seq's, and it has incremented by exactly one
6275 		 * during the create_vnode and it didn't
6276 		 * change during the VOP_FSYNC.
6277 		 */
6278 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
6279 		    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6280 			cinfo->atomic = TRUE;
6281 		else
6282 			cinfo->atomic = FALSE;
6283 	}
6284 
6285 	/* Check for mandatory locking and that the size gets set. */
6286 	cva.va_mask = AT_MODE;
6287 	if (setsize)
6288 		cva.va_mask |= AT_SIZE;
6289 
6290 	/* Assume the worst */
6291 	cs->mandlock = TRUE;
6292 
6293 	if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6294 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6295 
6296 		/*
6297 		 * Truncate the file if necessary; this would be
6298 		 * the case for create over an existing file.
6299 		 */
6300 
6301 		if (trunc) {
6302 			int in_crit = 0;
6303 			rfs4_file_t *fp;
6304 			bool_t create = FALSE;
6305 
6306 			/*
6307 			 * We are writing over an existing file.
6308 			 * Check to see if we need to recall a delegation.
6309 			 */
6310 			rfs4_hold_deleg_policy();
6311 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6312 				if (rfs4_check_delegated_byfp(FWRITE, fp,
6313 				    (reqsize == 0), FALSE, FALSE, &clientid)) {
6314 					rfs4_file_rele(fp);
6315 					rfs4_rele_deleg_policy();
6316 					VN_RELE(vp);
6317 					*attrset = 0;
6318 					return (NFS4ERR_DELAY);
6319 				}
6320 				rfs4_file_rele(fp);
6321 			}
6322 			rfs4_rele_deleg_policy();
6323 
6324 			if (nbl_need_check(vp)) {
6325 				in_crit = 1;
6326 
6327 				ASSERT(reqsize == 0);
6328 
6329 				nbl_start_crit(vp, RW_READER);
6330 				if (nbl_conflict(vp, NBL_WRITE, 0,
6331 				    cva.va_size, 0, NULL)) {
6332 					in_crit = 0;
6333 					nbl_end_crit(vp);
6334 					VN_RELE(vp);
6335 					*attrset = 0;
6336 					return (NFS4ERR_ACCESS);
6337 				}
6338 			}
6339 			ct.cc_sysid = 0;
6340 			ct.cc_pid = 0;
6341 			ct.cc_caller_id = nfs4_srv_caller_id;
6342 			ct.cc_flags = CC_DONTBLOCK;
6343 
6344 			cva.va_mask = AT_SIZE;
6345 			cva.va_size = reqsize;
6346 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6347 			if (in_crit)
6348 				nbl_end_crit(vp);
6349 		}
6350 	}
6351 
6352 	error = makefh4(&cs->fh, vp, cs->exi);
6353 
6354 	/*
6355 	 * Force modified data and metadata out to stable storage.
6356 	 */
6357 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6358 
6359 	if (error) {
6360 		VN_RELE(vp);
6361 		*attrset = 0;
6362 		return (puterrno4(error));
6363 	}
6364 
6365 	/* if parent dir is attrdir, set namedattr fh flag */
6366 	if (dvp->v_flag & V_XATTRDIR)
6367 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6368 
6369 	if (cs->vp)
6370 		VN_RELE(cs->vp);
6371 
6372 	cs->vp = vp;
6373 
6374 	/*
6375 	 * if we did not create the file, we will need to check
6376 	 * the access bits on the file
6377 	 */
6378 
6379 	if (!created) {
6380 		if (setsize)
6381 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6382 		status = check_open_access(args->share_access, cs, req);
6383 		if (status != NFS4_OK)
6384 			*attrset = 0;
6385 	}
6386 	return (status);
6387 }
6388 
6389 /*ARGSUSED*/
6390 static void
6391 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6392 		rfs4_openowner_t *oo, delegreq_t deleg,
6393 		uint32_t access, uint32_t deny,
6394 		OPEN4res *resp, int deleg_cur)
6395 {
6396 	/* XXX Currently not using req  */
6397 	rfs4_state_t *state;
6398 	rfs4_file_t *file;
6399 	bool_t screate = TRUE;
6400 	bool_t fcreate = TRUE;
6401 	uint32_t amodes;
6402 	uint32_t dmodes;
6403 	rfs4_deleg_state_t *dsp;
6404 	struct shrlock shr;
6405 	struct shr_locowner shr_loco;
6406 	sysid_t sysid;
6407 	nfsstat4 status;
6408 	caller_context_t ct;
6409 	int fflags = 0;
6410 	int recall = 0;
6411 	int err;
6412 	int cmd;
6413 
6414 	/* get the file struct and hold a lock on it during initial open */
6415 	file = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6416 	if (file == NULL) {
6417 		NFS4_DEBUG(rfs4_debug,
6418 		    (CE_NOTE, "rfs4_do_open: can't find file"));
6419 		resp->status = NFS4ERR_SERVERFAULT;
6420 		return;
6421 	}
6422 
6423 	state = rfs4_findstate_by_owner_file(oo, file, &screate);
6424 	if (state == NULL) {
6425 		NFS4_DEBUG(rfs4_debug,
6426 		    (CE_NOTE, "rfs4_do_open: can't find state"));
6427 		resp->status = NFS4ERR_RESOURCE;
6428 		/* No need to keep any reference */
6429 		rfs4_file_rele_withunlock(file);
6430 		return;
6431 	}
6432 
6433 	/* try to get the sysid before continuing */
6434 	if ((status = rfs4_client_sysid(oo->client, &sysid)) != NFS4_OK) {
6435 		resp->status = status;
6436 		rfs4_file_rele(file);
6437 		/* Not a fully formed open; "close" it */
6438 		if (screate == TRUE)
6439 			rfs4_state_close(state, FALSE, FALSE, cs->cr);
6440 		rfs4_state_rele(state);
6441 		return;
6442 	}
6443 
6444 	/* Calculate the fflags for this OPEN. */
6445 	if (access & OPEN4_SHARE_ACCESS_READ)
6446 		fflags |= FREAD;
6447 	if (access & OPEN4_SHARE_ACCESS_WRITE)
6448 		fflags |= FWRITE;
6449 
6450 	/*
6451 	 * Calculate the new deny and access mode that this open is adding to
6452 	 * the file for this open owner;
6453 	 */
6454 	dmodes = (deny & ~state->share_deny);
6455 	amodes = (access & ~state->share_access);
6456 
6457 	/*
6458 	 * Check to see the client has already sent an open for this
6459 	 * open owner on this file with the same share/deny modes.
6460 	 * If so, we don't need to check for a conflict and we don't
6461 	 * need to add another shrlock.  If not, then we need to
6462 	 * check for conflicts in deny and access before checking for
6463 	 * conflicts in delegation.  We don't want to recall a
6464 	 * delegation based on an open that will eventually fail based
6465 	 * on shares modes.
6466 	 */
6467 
6468 	if (dmodes || amodes) {
6469 		shr.s_access = (short)access;
6470 		shr.s_deny = (short)deny;
6471 		shr.s_pid = rfs4_dbe_getid(oo->dbe);
6472 		shr.s_sysid = sysid;
6473 		shr_loco.sl_pid = shr.s_pid;
6474 		shr_loco.sl_id = shr.s_sysid;
6475 		shr.s_owner = (caddr_t)&shr_loco;
6476 		shr.s_own_len = sizeof (shr_loco);
6477 
6478 		cmd = nbl_need_check(cs->vp) ? F_SHARE_NBMAND : F_SHARE;
6479 		if ((err = vop_shrlock(cs->vp, cmd, &shr, fflags)) != 0) {
6480 
6481 			resp->status = err == EAGAIN ?
6482 			    NFS4ERR_SHARE_DENIED : puterrno4(err);
6483 
6484 			rfs4_file_rele(file);
6485 			/* Not a fully formed open; "close" it */
6486 			if (screate == TRUE)
6487 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6488 			rfs4_state_rele(state);
6489 			return;
6490 		}
6491 	}
6492 
6493 	rfs4_dbe_lock(state->dbe);
6494 	rfs4_dbe_lock(file->dbe);
6495 
6496 	/*
6497 	 * Check to see if this file is delegated and if so, if a
6498 	 * recall needs to be done.
6499 	 */
6500 	if (rfs4_check_recall(state, access)) {
6501 		rfs4_dbe_unlock(file->dbe);
6502 		rfs4_dbe_unlock(state->dbe);
6503 		rfs4_recall_deleg(file, FALSE, state->owner->client);
6504 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6505 		rfs4_dbe_lock(state->dbe);
6506 		rfs4_dbe_lock(file->dbe);
6507 		/* Let's see if the delegation was returned */
6508 		if (rfs4_check_recall(state, access)) {
6509 			rfs4_dbe_unlock(file->dbe);
6510 			rfs4_dbe_unlock(state->dbe);
6511 			rfs4_file_rele(file);
6512 			rfs4_update_lease(state->owner->client);
6513 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6514 			/* Not a fully formed open; "close" it */
6515 			if (screate == TRUE)
6516 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6517 			rfs4_state_rele(state);
6518 			resp->status = NFS4ERR_DELAY;
6519 			return;
6520 		}
6521 	}
6522 	/*
6523 	 * the share check passed and any delegation conflict has been
6524 	 * taken care of, now call vop_open.
6525 	 * if this is the first open then call vop_open with fflags.
6526 	 * if not, call vn_open_upgrade with just the upgrade flags.
6527 	 *
6528 	 * if the file has been opened already, it will have the current
6529 	 * access mode in the state struct.  if it has no share access, then
6530 	 * this is a new open.
6531 	 *
6532 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6533 	 * call VOP_OPEN(), just do the open upgrade.
6534 	 */
6535 	if (((state->share_access & OPEN4_SHARE_ACCESS_BOTH) == 0) &&
6536 	    !deleg_cur) {
6537 		ct.cc_sysid = sysid;
6538 		ct.cc_pid = shr.s_pid;
6539 		ct.cc_caller_id = nfs4_srv_caller_id;
6540 		ct.cc_flags = CC_DONTBLOCK;
6541 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6542 		if (err) {
6543 			rfs4_dbe_unlock(file->dbe);
6544 			rfs4_dbe_unlock(state->dbe);
6545 			rfs4_file_rele(file);
6546 			(void) vop_shrlock(cs->vp, F_UNSHARE, &shr, fflags);
6547 			/* Not a fully formed open; "close" it */
6548 			if (screate == TRUE)
6549 				rfs4_state_close(state, FALSE, FALSE, cs->cr);
6550 			rfs4_state_rele(state);
6551 			/* check if a monitor detected a delegation conflict */
6552 			if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6553 				resp->status = NFS4ERR_DELAY;
6554 			else
6555 				resp->status = NFS4ERR_SERVERFAULT;
6556 			return;
6557 		}
6558 	} else { /* open upgrade */
6559 		/*
6560 		 * calculate the fflags for the new mode that is being added
6561 		 * by this upgrade.
6562 		 */
6563 		fflags = 0;
6564 		if (amodes & OPEN4_SHARE_ACCESS_READ)
6565 			fflags |= FREAD;
6566 		if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6567 			fflags |= FWRITE;
6568 		vn_open_upgrade(cs->vp, fflags);
6569 	}
6570 
6571 	if (dmodes & OPEN4_SHARE_DENY_READ)
6572 		file->deny_read++;
6573 	if (dmodes & OPEN4_SHARE_DENY_WRITE)
6574 		file->deny_write++;
6575 	file->share_deny |= deny;
6576 	state->share_deny |= deny;
6577 
6578 	if (amodes & OPEN4_SHARE_ACCESS_READ)
6579 		file->access_read++;
6580 	if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6581 		file->access_write++;
6582 	file->share_access |= access;
6583 	state->share_access |= access;
6584 
6585 	/*
6586 	 * Check for delegation here. if the deleg argument is not
6587 	 * DELEG_ANY, then this is a reclaim from a client and
6588 	 * we must honor the delegation requested. If necessary we can
6589 	 * set the recall flag.
6590 	 */
6591 
6592 	dsp = rfs4_grant_delegation(deleg, state, &recall);
6593 
6594 	cs->deleg = (file->dinfo->dtype == OPEN_DELEGATE_WRITE);
6595 
6596 	next_stateid(&state->stateid);
6597 
6598 	resp->stateid = state->stateid.stateid;
6599 
6600 	rfs4_dbe_unlock(file->dbe);
6601 	rfs4_dbe_unlock(state->dbe);
6602 
6603 	if (dsp) {
6604 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6605 		rfs4_deleg_state_rele(dsp);
6606 	}
6607 
6608 	rfs4_file_rele(file);
6609 	rfs4_state_rele(state);
6610 
6611 	resp->status = NFS4_OK;
6612 }
6613 
6614 /*ARGSUSED*/
6615 static void
6616 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6617 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6618 {
6619 	change_info4 *cinfo = &resp->cinfo;
6620 	bitmap4 *attrset = &resp->attrset;
6621 
6622 	if (args->opentype == OPEN4_NOCREATE)
6623 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6624 		    req, cs, args->share_access, cinfo);
6625 	else {
6626 		/* inhibit delegation grants during exclusive create */
6627 
6628 		if (args->mode == EXCLUSIVE4)
6629 			rfs4_disable_delegation();
6630 
6631 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
6632 		    oo->client->clientid);
6633 	}
6634 
6635 	if (resp->status == NFS4_OK) {
6636 
6637 		/* cs->vp cs->fh now reference the desired file */
6638 
6639 		rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access,
6640 		    args->share_deny, resp, 0);
6641 
6642 		/*
6643 		 * If rfs4_createfile set attrset, we must
6644 		 * clear this attrset before the response is copied.
6645 		 */
6646 		if (resp->status != NFS4_OK && resp->attrset) {
6647 			resp->attrset = 0;
6648 		}
6649 	}
6650 	else
6651 		*cs->statusp = resp->status;
6652 
6653 	if (args->mode == EXCLUSIVE4)
6654 		rfs4_enable_delegation();
6655 }
6656 
6657 /*ARGSUSED*/
6658 static void
6659 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
6660 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6661 {
6662 	change_info4 *cinfo = &resp->cinfo;
6663 	vattr_t va;
6664 	vtype_t v_type = cs->vp->v_type;
6665 	int error = 0;
6666 
6667 	/* Verify that we have a regular file */
6668 	if (v_type != VREG) {
6669 		if (v_type == VDIR)
6670 			resp->status = NFS4ERR_ISDIR;
6671 		else if (v_type == VLNK)
6672 			resp->status = NFS4ERR_SYMLINK;
6673 		else
6674 			resp->status = NFS4ERR_INVAL;
6675 		return;
6676 	}
6677 
6678 	va.va_mask = AT_MODE|AT_UID;
6679 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
6680 	if (error) {
6681 		resp->status = puterrno4(error);
6682 		return;
6683 	}
6684 
6685 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
6686 
6687 	/*
6688 	 * Check if we have access to the file, Note the the file
6689 	 * could have originally been open UNCHECKED or GUARDED
6690 	 * with mode bits that will now fail, but there is nothing
6691 	 * we can really do about that except in the case that the
6692 	 * owner of the file is the one requesting the open.
6693 	 */
6694 	if (crgetuid(cs->cr) != va.va_uid) {
6695 		resp->status = check_open_access(args->share_access, cs, req);
6696 		if (resp->status != NFS4_OK) {
6697 			return;
6698 		}
6699 	}
6700 
6701 	/*
6702 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
6703 	 */
6704 	cinfo->before = 0;
6705 	cinfo->after = 0;
6706 	cinfo->atomic = FALSE;
6707 
6708 	rfs4_do_open(cs, req, oo,
6709 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
6710 	    args->share_access, args->share_deny, resp, 0);
6711 }
6712 
6713 static void
6714 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
6715 		OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6716 {
6717 	int error;
6718 	nfsstat4 status;
6719 	stateid4 stateid =
6720 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
6721 	rfs4_deleg_state_t *dsp;
6722 
6723 	/*
6724 	 * Find the state info from the stateid and confirm that the
6725 	 * file is delegated.  If the state openowner is the same as
6726 	 * the supplied openowner we're done. If not, get the file
6727 	 * info from the found state info. Use that file info to
6728 	 * create the state for this lock owner. Note solaris doen't
6729 	 * really need the pathname to find the file. We may want to
6730 	 * lookup the pathname and make sure that the vp exist and
6731 	 * matches the vp in the file structure. However it is
6732 	 * possible that the pathname nolonger exists (local process
6733 	 * unlinks the file), so this may not be that useful.
6734 	 */
6735 
6736 	status = rfs4_get_deleg_state(&stateid, &dsp);
6737 	if (status != NFS4_OK) {
6738 		resp->status = status;
6739 		return;
6740 	}
6741 
6742 	ASSERT(dsp->finfo->dinfo->dtype != OPEN_DELEGATE_NONE);
6743 
6744 	/*
6745 	 * New lock owner, create state. Since this was probably called
6746 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
6747 	 */
6748 
6749 	ASSERT(cs->vp != NULL);
6750 	VN_RELE(cs->vp);
6751 	VN_HOLD(dsp->finfo->vp);
6752 	cs->vp = dsp->finfo->vp;
6753 
6754 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
6755 		rfs4_deleg_state_rele(dsp);
6756 		*cs->statusp = resp->status = puterrno4(error);
6757 		return;
6758 	}
6759 
6760 	/* Mark progress for delegation returns */
6761 	dsp->finfo->dinfo->time_lastwrite = gethrestime_sec();
6762 	rfs4_deleg_state_rele(dsp);
6763 	rfs4_do_open(cs, req, oo, DELEG_NONE,
6764 	    args->share_access, args->share_deny, resp, 1);
6765 }
6766 
6767 /*ARGSUSED*/
6768 static void
6769 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
6770 			OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6771 {
6772 	/*
6773 	 * Lookup the pathname, it must already exist since this file
6774 	 * was delegated.
6775 	 *
6776 	 * Find the file and state info for this vp and open owner pair.
6777 	 *	check that they are in fact delegated.
6778 	 *	check that the state access and deny modes are the same.
6779 	 *
6780 	 * Return the delgation possibly seting the recall flag.
6781 	 */
6782 	rfs4_file_t *file;
6783 	rfs4_state_t *state;
6784 	bool_t create = FALSE;
6785 	bool_t dcreate = FALSE;
6786 	rfs4_deleg_state_t *dsp;
6787 	nfsace4 *ace;
6788 
6789 
6790 	/* Note we ignore oflags */
6791 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
6792 	    req, cs, args->share_access, &resp->cinfo);
6793 
6794 	if (resp->status != NFS4_OK) {
6795 		return;
6796 	}
6797 
6798 	/* get the file struct and hold a lock on it during initial open */
6799 	file = rfs4_findfile_withlock(cs->vp, NULL, &create);
6800 	if (file == NULL) {
6801 		NFS4_DEBUG(rfs4_debug,
6802 		    (CE_NOTE, "rfs4_do_opendelprev: can't find file"));
6803 		resp->status = NFS4ERR_SERVERFAULT;
6804 		return;
6805 	}
6806 
6807 	state = rfs4_findstate_by_owner_file(oo, file, &create);
6808 	if (state == NULL) {
6809 		NFS4_DEBUG(rfs4_debug,
6810 		    (CE_NOTE, "rfs4_do_opendelprev: can't find state"));
6811 		resp->status = NFS4ERR_SERVERFAULT;
6812 		rfs4_file_rele_withunlock(file);
6813 		return;
6814 	}
6815 
6816 	rfs4_dbe_lock(state->dbe);
6817 	rfs4_dbe_lock(file->dbe);
6818 	if (args->share_access != state->share_access ||
6819 	    args->share_deny != state->share_deny ||
6820 	    state->finfo->dinfo->dtype == OPEN_DELEGATE_NONE) {
6821 		NFS4_DEBUG(rfs4_debug,
6822 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
6823 		rfs4_dbe_unlock(file->dbe);
6824 		rfs4_dbe_unlock(state->dbe);
6825 		rfs4_file_rele(file);
6826 		rfs4_state_rele(state);
6827 		resp->status = NFS4ERR_SERVERFAULT;
6828 		return;
6829 	}
6830 	rfs4_dbe_unlock(file->dbe);
6831 	rfs4_dbe_unlock(state->dbe);
6832 
6833 	dsp = rfs4_finddeleg(state, &dcreate);
6834 	if (dsp == NULL) {
6835 		rfs4_state_rele(state);
6836 		rfs4_file_rele(file);
6837 		resp->status = NFS4ERR_SERVERFAULT;
6838 		return;
6839 	}
6840 
6841 	next_stateid(&state->stateid);
6842 
6843 	resp->stateid = state->stateid.stateid;
6844 
6845 	resp->delegation.delegation_type = dsp->dtype;
6846 
6847 	if (dsp->dtype == OPEN_DELEGATE_READ) {
6848 		open_read_delegation4 *rv =
6849 		    &resp->delegation.open_delegation4_u.read;
6850 
6851 		rv->stateid = dsp->delegid.stateid;
6852 		rv->recall = FALSE; /* no policy in place to set to TRUE */
6853 		ace = &rv->permissions;
6854 	} else {
6855 		open_write_delegation4 *rv =
6856 		    &resp->delegation.open_delegation4_u.write;
6857 
6858 		rv->stateid = dsp->delegid.stateid;
6859 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
6860 		ace = &rv->permissions;
6861 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
6862 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
6863 	}
6864 
6865 	/* XXX For now */
6866 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
6867 	ace->flag = 0;
6868 	ace->access_mask = 0;
6869 	ace->who.utf8string_len = 0;
6870 	ace->who.utf8string_val = 0;
6871 
6872 	rfs4_deleg_state_rele(dsp);
6873 	rfs4_state_rele(state);
6874 	rfs4_file_rele(file);
6875 }
6876 
6877 typedef enum {
6878 	NFS4_CHKSEQ_OKAY = 0,
6879 	NFS4_CHKSEQ_REPLAY = 1,
6880 	NFS4_CHKSEQ_BAD = 2
6881 } rfs4_chkseq_t;
6882 
6883 /*
6884  * Generic function for sequence number checks.
6885  */
6886 static rfs4_chkseq_t
6887 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
6888 		seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
6889 {
6890 	/* Same sequence ids and matching operations? */
6891 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
6892 		if (copyres == TRUE) {
6893 			rfs4_free_reply(resop);
6894 			rfs4_copy_reply(resop, lastop);
6895 		}
6896 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6897 		    "Replayed SEQID %d\n", seqid));
6898 		return (NFS4_CHKSEQ_REPLAY);
6899 	}
6900 
6901 	/* If the incoming sequence is not the next expected then it is bad */
6902 	if (rqst_seq != seqid + 1) {
6903 		if (rqst_seq == seqid) {
6904 			NFS4_DEBUG(rfs4_debug,
6905 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
6906 			    "but last op was %d current op is %d\n",
6907 			    lastop->resop, resop->resop));
6908 			return (NFS4_CHKSEQ_BAD);
6909 		}
6910 		NFS4_DEBUG(rfs4_debug,
6911 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
6912 		    rqst_seq, seqid));
6913 		return (NFS4_CHKSEQ_BAD);
6914 	}
6915 
6916 	/* Everything okay -- next expected */
6917 	return (NFS4_CHKSEQ_OKAY);
6918 }
6919 
6920 
6921 static rfs4_chkseq_t
6922 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
6923 {
6924 	rfs4_chkseq_t rc;
6925 
6926 	rfs4_dbe_lock(op->dbe);
6927 	rc = rfs4_check_seqid(op->open_seqid, op->reply, seqid, resop, TRUE);
6928 	rfs4_dbe_unlock(op->dbe);
6929 
6930 	if (rc == NFS4_CHKSEQ_OKAY)
6931 		rfs4_update_lease(op->client);
6932 
6933 	return (rc);
6934 }
6935 
6936 static rfs4_chkseq_t
6937 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op,
6938 	nfs_resop4 *resop)
6939 {
6940 	rfs4_chkseq_t rc;
6941 
6942 	rfs4_dbe_lock(op->dbe);
6943 	rc = rfs4_check_seqid(op->open_seqid, op->reply,
6944 	    olo_seqid, resop, FALSE);
6945 	rfs4_dbe_unlock(op->dbe);
6946 
6947 	return (rc);
6948 }
6949 
6950 static rfs4_chkseq_t
6951 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lp, nfs_resop4 *resop)
6952 {
6953 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
6954 
6955 	rfs4_dbe_lock(lp->dbe);
6956 	if (!lp->skip_seqid_check)
6957 		rc = rfs4_check_seqid(lp->seqid, lp->reply, seqid, resop, TRUE);
6958 	rfs4_dbe_unlock(lp->dbe);
6959 
6960 	return (rc);
6961 }
6962 
6963 static void
6964 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
6965 	    struct svc_req *req, struct compound_state *cs)
6966 {
6967 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
6968 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
6969 	open_owner4 *owner = &args->owner;
6970 	open_claim_type4 claim = args->claim;
6971 	rfs4_client_t *cp;
6972 	rfs4_openowner_t *oo;
6973 	bool_t create;
6974 	bool_t replay = FALSE;
6975 	int can_reclaim;
6976 
6977 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
6978 	    OPEN4args *, args);
6979 
6980 	if (cs->vp == NULL) {
6981 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
6982 		goto end;
6983 	}
6984 
6985 	/*
6986 	 * Need to check clientid and lease expiration first based on
6987 	 * error ordering and incrementing sequence id.
6988 	 */
6989 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
6990 	if (cp == NULL) {
6991 		*cs->statusp = resp->status =
6992 		    rfs4_check_clientid(&owner->clientid, 0);
6993 		goto end;
6994 	}
6995 
6996 	if (rfs4_lease_expired(cp)) {
6997 		rfs4_client_close(cp);
6998 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
6999 		goto end;
7000 	}
7001 	can_reclaim = cp->can_reclaim;
7002 
7003 	/*
7004 	 * Find the open_owner for use from this point forward.  Take
7005 	 * care in updating the sequence id based on the type of error
7006 	 * being returned.
7007 	 */
7008 retry:
7009 	create = TRUE;
7010 	oo = rfs4_findopenowner(owner, &create, args->seqid);
7011 	if (oo == NULL) {
7012 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
7013 		rfs4_client_rele(cp);
7014 		goto end;
7015 	}
7016 
7017 	/* Hold off access to the sequence space while the open is done */
7018 	rfs4_sw_enter(&oo->oo_sw);
7019 
7020 	/*
7021 	 * If the open_owner existed before at the server, then check
7022 	 * the sequence id.
7023 	 */
7024 	if (!create && !oo->postpone_confirm) {
7025 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
7026 		case NFS4_CHKSEQ_BAD:
7027 			if ((args->seqid > oo->open_seqid) &&
7028 			    oo->need_confirm) {
7029 				rfs4_free_opens(oo, TRUE, FALSE);
7030 				rfs4_sw_exit(&oo->oo_sw);
7031 				rfs4_openowner_rele(oo);
7032 				goto retry;
7033 			}
7034 			resp->status = NFS4ERR_BAD_SEQID;
7035 			goto out;
7036 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
7037 			replay = TRUE;
7038 			goto out;
7039 		default:
7040 			break;
7041 		}
7042 
7043 		/*
7044 		 * Sequence was ok and open owner exists
7045 		 * check to see if we have yet to see an
7046 		 * open_confirm.
7047 		 */
7048 		if (oo->need_confirm) {
7049 			rfs4_free_opens(oo, TRUE, FALSE);
7050 			rfs4_sw_exit(&oo->oo_sw);
7051 			rfs4_openowner_rele(oo);
7052 			goto retry;
7053 		}
7054 	}
7055 	/* Grace only applies to regular-type OPENs */
7056 	if (rfs4_clnt_in_grace(cp) &&
7057 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7058 		*cs->statusp = resp->status = NFS4ERR_GRACE;
7059 		goto out;
7060 	}
7061 
7062 	/*
7063 	 * If previous state at the server existed then can_reclaim
7064 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
7065 	 * client.
7066 	 */
7067 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7068 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7069 		goto out;
7070 	}
7071 
7072 
7073 	/*
7074 	 * Reject the open if the client has missed the grace period
7075 	 */
7076 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7077 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7078 		goto out;
7079 	}
7080 
7081 	/* Couple of up-front bookkeeping items */
7082 	if (oo->need_confirm) {
7083 		/*
7084 		 * If this is a reclaim OPEN then we should not ask
7085 		 * for a confirmation of the open_owner per the
7086 		 * protocol specification.
7087 		 */
7088 		if (claim == CLAIM_PREVIOUS)
7089 			oo->need_confirm = FALSE;
7090 		else
7091 			resp->rflags |= OPEN4_RESULT_CONFIRM;
7092 	}
7093 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7094 
7095 	/*
7096 	 * If there is an unshared filesystem mounted on this vnode,
7097 	 * do not allow to open/create in this directory.
7098 	 */
7099 	if (vn_ismntpt(cs->vp)) {
7100 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
7101 		goto out;
7102 	}
7103 
7104 	/*
7105 	 * access must READ, WRITE, or BOTH.  No access is invalid.
7106 	 * deny can be READ, WRITE, BOTH, or NONE.
7107 	 * bits not defined for access/deny are invalid.
7108 	 */
7109 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7110 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7111 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7112 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7113 		goto out;
7114 	}
7115 
7116 
7117 	/*
7118 	 * make sure attrset is zero before response is built.
7119 	 */
7120 	resp->attrset = 0;
7121 
7122 	switch (claim) {
7123 	case CLAIM_NULL:
7124 		rfs4_do_opennull(cs, req, args, oo, resp);
7125 		break;
7126 	case CLAIM_PREVIOUS:
7127 		rfs4_do_openprev(cs, req, args, oo, resp);
7128 		break;
7129 	case CLAIM_DELEGATE_CUR:
7130 		rfs4_do_opendelcur(cs, req, args, oo, resp);
7131 		break;
7132 	case CLAIM_DELEGATE_PREV:
7133 		rfs4_do_opendelprev(cs, req, args, oo, resp);
7134 		break;
7135 	default:
7136 		resp->status = NFS4ERR_INVAL;
7137 		break;
7138 	}
7139 
7140 out:
7141 	rfs4_client_rele(cp);
7142 
7143 	/* Catch sequence id handling here to make it a little easier */
7144 	switch (resp->status) {
7145 	case NFS4ERR_BADXDR:
7146 	case NFS4ERR_BAD_SEQID:
7147 	case NFS4ERR_BAD_STATEID:
7148 	case NFS4ERR_NOFILEHANDLE:
7149 	case NFS4ERR_RESOURCE:
7150 	case NFS4ERR_STALE_CLIENTID:
7151 	case NFS4ERR_STALE_STATEID:
7152 		/*
7153 		 * The protocol states that if any of these errors are
7154 		 * being returned, the sequence id should not be
7155 		 * incremented.  Any other return requires an
7156 		 * increment.
7157 		 */
7158 		break;
7159 	default:
7160 		/* Always update the lease in this case */
7161 		rfs4_update_lease(oo->client);
7162 
7163 		/* Regular response - copy the result */
7164 		if (!replay)
7165 			rfs4_update_open_resp(oo, resop, &cs->fh);
7166 
7167 		/*
7168 		 * REPLAY case: Only if the previous response was OK
7169 		 * do we copy the filehandle.  If not OK, no
7170 		 * filehandle to copy.
7171 		 */
7172 		if (replay == TRUE &&
7173 		    resp->status == NFS4_OK &&
7174 		    oo->reply_fh.nfs_fh4_val) {
7175 			/*
7176 			 * If this is a replay, we must restore the
7177 			 * current filehandle/vp to that of what was
7178 			 * returned originally.  Try our best to do
7179 			 * it.
7180 			 */
7181 			nfs_fh4_fmt_t *fh_fmtp =
7182 			    (nfs_fh4_fmt_t *)oo->reply_fh.nfs_fh4_val;
7183 
7184 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7185 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7186 
7187 			if (cs->exi == NULL) {
7188 				resp->status = NFS4ERR_STALE;
7189 				goto finish;
7190 			}
7191 
7192 			VN_RELE(cs->vp);
7193 
7194 			cs->vp = nfs4_fhtovp(&oo->reply_fh, cs->exi,
7195 			    &resp->status);
7196 
7197 			if (cs->vp == NULL)
7198 				goto finish;
7199 
7200 			nfs_fh4_copy(&oo->reply_fh, &cs->fh);
7201 		}
7202 
7203 		/*
7204 		 * If this was a replay, no need to update the
7205 		 * sequence id. If the open_owner was not created on
7206 		 * this pass, then update.  The first use of an
7207 		 * open_owner will not bump the sequence id.
7208 		 */
7209 		if (replay == FALSE && !create)
7210 			rfs4_update_open_sequence(oo);
7211 		/*
7212 		 * If the client is receiving an error and the
7213 		 * open_owner needs to be confirmed, there is no way
7214 		 * to notify the client of this fact ignoring the fact
7215 		 * that the server has no method of returning a
7216 		 * stateid to confirm.  Therefore, the server needs to
7217 		 * mark this open_owner in a way as to avoid the
7218 		 * sequence id checking the next time the client uses
7219 		 * this open_owner.
7220 		 */
7221 		if (resp->status != NFS4_OK && oo->need_confirm)
7222 			oo->postpone_confirm = TRUE;
7223 		/*
7224 		 * If OK response then clear the postpone flag and
7225 		 * reset the sequence id to keep in sync with the
7226 		 * client.
7227 		 */
7228 		if (resp->status == NFS4_OK && oo->postpone_confirm) {
7229 			oo->postpone_confirm = FALSE;
7230 			oo->open_seqid = args->seqid;
7231 		}
7232 		break;
7233 	}
7234 
7235 finish:
7236 	*cs->statusp = resp->status;
7237 
7238 	rfs4_sw_exit(&oo->oo_sw);
7239 	rfs4_openowner_rele(oo);
7240 
7241 end:
7242 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7243 	    OPEN4res *, resp);
7244 }
7245 
7246 /*ARGSUSED*/
7247 void
7248 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7249 		    struct svc_req *req, struct compound_state *cs)
7250 {
7251 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7252 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7253 	rfs4_state_t *sp;
7254 	nfsstat4 status;
7255 
7256 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7257 	    OPEN_CONFIRM4args *, args);
7258 
7259 	if (cs->vp == NULL) {
7260 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7261 		goto out;
7262 	}
7263 
7264 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7265 	if (status != NFS4_OK) {
7266 		*cs->statusp = resp->status = status;
7267 		goto out;
7268 	}
7269 
7270 	/* Ensure specified filehandle matches */
7271 	if (cs->vp != sp->finfo->vp) {
7272 		rfs4_state_rele(sp);
7273 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7274 		goto out;
7275 	}
7276 
7277 	/* hold off other access to open_owner while we tinker */
7278 	rfs4_sw_enter(&sp->owner->oo_sw);
7279 
7280 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7281 	case NFS4_CHECK_STATEID_OKAY:
7282 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7283 		    resop) != 0) {
7284 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7285 			break;
7286 		}
7287 		/*
7288 		 * If it is the appropriate stateid and determined to
7289 		 * be "OKAY" then this means that the stateid does not
7290 		 * need to be confirmed and the client is in error for
7291 		 * sending an OPEN_CONFIRM.
7292 		 */
7293 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7294 		break;
7295 	case NFS4_CHECK_STATEID_OLD:
7296 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7297 		break;
7298 	case NFS4_CHECK_STATEID_BAD:
7299 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7300 		break;
7301 	case NFS4_CHECK_STATEID_EXPIRED:
7302 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7303 		break;
7304 	case NFS4_CHECK_STATEID_CLOSED:
7305 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7306 		break;
7307 	case NFS4_CHECK_STATEID_REPLAY:
7308 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7309 		case NFS4_CHKSEQ_OKAY:
7310 			/*
7311 			 * This is replayed stateid; if seqid matches
7312 			 * next expected, then client is using wrong seqid.
7313 			 */
7314 			/* fall through */
7315 		case NFS4_CHKSEQ_BAD:
7316 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7317 			break;
7318 		case NFS4_CHKSEQ_REPLAY:
7319 			/*
7320 			 * Note this case is the duplicate case so
7321 			 * resp->status is already set.
7322 			 */
7323 			*cs->statusp = resp->status;
7324 			rfs4_update_lease(sp->owner->client);
7325 			break;
7326 		}
7327 		break;
7328 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7329 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7330 		    resop) != NFS4_CHKSEQ_OKAY) {
7331 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7332 			break;
7333 		}
7334 		*cs->statusp = resp->status = NFS4_OK;
7335 
7336 		next_stateid(&sp->stateid);
7337 		resp->open_stateid = sp->stateid.stateid;
7338 		sp->owner->need_confirm = FALSE;
7339 		rfs4_update_lease(sp->owner->client);
7340 		rfs4_update_open_sequence(sp->owner);
7341 		rfs4_update_open_resp(sp->owner, resop, NULL);
7342 		break;
7343 	default:
7344 		ASSERT(FALSE);
7345 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7346 		break;
7347 	}
7348 	rfs4_sw_exit(&sp->owner->oo_sw);
7349 	rfs4_state_rele(sp);
7350 
7351 out:
7352 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7353 	    OPEN_CONFIRM4res *, resp);
7354 }
7355 
7356 /*ARGSUSED*/
7357 void
7358 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7359 		    struct svc_req *req, struct compound_state *cs)
7360 {
7361 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7362 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7363 	uint32_t access = args->share_access;
7364 	uint32_t deny = args->share_deny;
7365 	nfsstat4 status;
7366 	rfs4_state_t *sp;
7367 	rfs4_file_t *fp;
7368 	int fflags = 0;
7369 
7370 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7371 	    OPEN_DOWNGRADE4args *, args);
7372 
7373 	if (cs->vp == NULL) {
7374 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7375 		goto out;
7376 	}
7377 
7378 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7379 	if (status != NFS4_OK) {
7380 		*cs->statusp = resp->status = status;
7381 		goto out;
7382 	}
7383 
7384 	/* Ensure specified filehandle matches */
7385 	if (cs->vp != sp->finfo->vp) {
7386 		rfs4_state_rele(sp);
7387 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7388 		goto out;
7389 	}
7390 
7391 	/* hold off other access to open_owner while we tinker */
7392 	rfs4_sw_enter(&sp->owner->oo_sw);
7393 
7394 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7395 	case NFS4_CHECK_STATEID_OKAY:
7396 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7397 		    resop) != NFS4_CHKSEQ_OKAY) {
7398 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7399 			goto end;
7400 		}
7401 		break;
7402 	case NFS4_CHECK_STATEID_OLD:
7403 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7404 		goto end;
7405 	case NFS4_CHECK_STATEID_BAD:
7406 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7407 		goto end;
7408 	case NFS4_CHECK_STATEID_EXPIRED:
7409 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7410 		goto end;
7411 	case NFS4_CHECK_STATEID_CLOSED:
7412 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7413 		goto end;
7414 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7415 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7416 		goto end;
7417 	case NFS4_CHECK_STATEID_REPLAY:
7418 		/* Check the sequence id for the open owner */
7419 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7420 		case NFS4_CHKSEQ_OKAY:
7421 			/*
7422 			 * This is replayed stateid; if seqid matches
7423 			 * next expected, then client is using wrong seqid.
7424 			 */
7425 			/* fall through */
7426 		case NFS4_CHKSEQ_BAD:
7427 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7428 			goto end;
7429 		case NFS4_CHKSEQ_REPLAY:
7430 			/*
7431 			 * Note this case is the duplicate case so
7432 			 * resp->status is already set.
7433 			 */
7434 			*cs->statusp = resp->status;
7435 			rfs4_update_lease(sp->owner->client);
7436 			goto end;
7437 		}
7438 		break;
7439 	default:
7440 		ASSERT(FALSE);
7441 		break;
7442 	}
7443 
7444 	rfs4_dbe_lock(sp->dbe);
7445 	/*
7446 	 * Check that the new access modes and deny modes are valid.
7447 	 * Check that no invalid bits are set.
7448 	 */
7449 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7450 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7451 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7452 		rfs4_update_open_sequence(sp->owner);
7453 		rfs4_dbe_unlock(sp->dbe);
7454 		goto end;
7455 	}
7456 
7457 	/*
7458 	 * The new modes must be a subset of the current modes and
7459 	 * the access must specify at least one mode. To test that
7460 	 * the new mode is a subset of the current modes we bitwise
7461 	 * AND them together and check that the result equals the new
7462 	 * mode. For example:
7463 	 * New mode, access == R and current mode, sp->share_access  == RW
7464 	 * access & sp->share_access == R == access, so the new access mode
7465 	 * is valid. Consider access == RW, sp->share_access = R
7466 	 * access & sp->share_access == R != access, so the new access mode
7467 	 * is invalid.
7468 	 */
7469 	if ((access & sp->share_access) != access ||
7470 	    (deny & sp->share_deny) != deny ||
7471 	    (access &
7472 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7473 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7474 		rfs4_update_open_sequence(sp->owner);
7475 		rfs4_dbe_unlock(sp->dbe);
7476 		goto end;
7477 	}
7478 
7479 	/*
7480 	 * Release any share locks associated with this stateID.
7481 	 * Strictly speaking, this violates the spec because the
7482 	 * spec effectively requires that open downgrade be atomic.
7483 	 * At present, fs_shrlock does not have this capability.
7484 	 */
7485 	rfs4_dbe_unlock(sp->dbe);
7486 	rfs4_unshare(sp);
7487 	rfs4_dbe_lock(sp->dbe);
7488 
7489 	fp = sp->finfo;
7490 	rfs4_dbe_lock(fp->dbe);
7491 
7492 	/*
7493 	 * If the current mode has deny read and the new mode
7494 	 * does not, decrement the number of deny read mode bits
7495 	 * and if it goes to zero turn off the deny read bit
7496 	 * on the file.
7497 	 */
7498 	if ((sp->share_deny & OPEN4_SHARE_DENY_READ) &&
7499 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7500 		fp->deny_read--;
7501 		if (fp->deny_read == 0)
7502 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
7503 	}
7504 
7505 	/*
7506 	 * If the current mode has deny write and the new mode
7507 	 * does not, decrement the number of deny write mode bits
7508 	 * and if it goes to zero turn off the deny write bit
7509 	 * on the file.
7510 	 */
7511 	if ((sp->share_deny & OPEN4_SHARE_DENY_WRITE) &&
7512 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7513 		fp->deny_write--;
7514 		if (fp->deny_write == 0)
7515 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7516 	}
7517 
7518 	/*
7519 	 * If the current mode has access read and the new mode
7520 	 * does not, decrement the number of access read mode bits
7521 	 * and if it goes to zero turn off the access read bit
7522 	 * on the file.  set fflags to FREAD for the call to
7523 	 * vn_open_downgrade().
7524 	 */
7525 	if ((sp->share_access & OPEN4_SHARE_ACCESS_READ) &&
7526 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7527 		fp->access_read--;
7528 		if (fp->access_read == 0)
7529 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
7530 		fflags |= FREAD;
7531 	}
7532 
7533 	/*
7534 	 * If the current mode has access write and the new mode
7535 	 * does not, decrement the number of access write mode bits
7536 	 * and if it goes to zero turn off the access write bit
7537 	 * on the file.  set fflags to FWRITE for the call to
7538 	 * vn_open_downgrade().
7539 	 */
7540 	if ((sp->share_access & OPEN4_SHARE_ACCESS_WRITE) &&
7541 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7542 		fp->access_write--;
7543 		if (fp->access_write == 0)
7544 			fp->share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7545 		fflags |= FWRITE;
7546 	}
7547 
7548 	/* Set the new access and deny modes */
7549 	sp->share_access = access;
7550 	sp->share_deny = deny;
7551 	/* Check that the file is still accessible */
7552 	ASSERT(fp->share_access);
7553 
7554 	rfs4_dbe_unlock(fp->dbe);
7555 
7556 	rfs4_dbe_unlock(sp->dbe);
7557 
7558 	if ((status = rfs4_share(sp)) != NFS4_OK) {
7559 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7560 		rfs4_update_open_sequence(sp->owner);
7561 		goto end;
7562 	}
7563 
7564 	/*
7565 	 * we successfully downgraded the share lock, now we need to downgrade
7566 	 * the open.  it is possible that the downgrade was only for a deny
7567 	 * mode and we have nothing else to do.
7568 	 */
7569 	if ((fflags & (FREAD|FWRITE)) != 0)
7570 		vn_open_downgrade(cs->vp, fflags);
7571 
7572 	rfs4_dbe_lock(sp->dbe);
7573 
7574 	/* Update the stateid */
7575 	next_stateid(&sp->stateid);
7576 	resp->open_stateid = sp->stateid.stateid;
7577 
7578 	rfs4_dbe_unlock(sp->dbe);
7579 
7580 	*cs->statusp = resp->status = NFS4_OK;
7581 	/* Update the lease */
7582 	rfs4_update_lease(sp->owner->client);
7583 	/* And the sequence */
7584 	rfs4_update_open_sequence(sp->owner);
7585 	rfs4_update_open_resp(sp->owner, resop, NULL);
7586 
7587 end:
7588 	rfs4_sw_exit(&sp->owner->oo_sw);
7589 	rfs4_state_rele(sp);
7590 out:
7591 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7592 	    OPEN_DOWNGRADE4res *, resp);
7593 }
7594 
7595 /*
7596  * The logic behind this function is detailed in the NFSv4 RFC in the
7597  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7598  * that section for explicit guidance to server behavior for
7599  * SETCLIENTID.
7600  */
7601 void
7602 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
7603 		    struct svc_req *req, struct compound_state *cs)
7604 {
7605 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
7606 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
7607 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
7608 	bool_t create = TRUE;
7609 	char *addr, *netid;
7610 	int len;
7611 
7612 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
7613 	    SETCLIENTID4args *, args);
7614 retry:
7615 	newcp = cp_confirmed = cp_unconfirmed = NULL;
7616 
7617 	/*
7618 	 * In search of an EXISTING client matching the incoming
7619 	 * request to establish a new client identifier at the server
7620 	 */
7621 	create = TRUE;
7622 	cp = rfs4_findclient(&args->client, &create, NULL);
7623 
7624 	/* Should never happen */
7625 	ASSERT(cp != NULL);
7626 
7627 	if (cp == NULL) {
7628 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7629 		goto out;
7630 	}
7631 
7632 	/*
7633 	 * Easiest case. Client identifier is newly created and is
7634 	 * unconfirmed.  Also note that for this case, no other
7635 	 * entries exist for the client identifier.  Nothing else to
7636 	 * check.  Just setup the response and respond.
7637 	 */
7638 	if (create) {
7639 		*cs->statusp = res->status = NFS4_OK;
7640 		res->SETCLIENTID4res_u.resok4.clientid = cp->clientid;
7641 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7642 		    cp->confirm_verf;
7643 		/* Setup callback information; CB_NULL confirmation later */
7644 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
7645 
7646 		rfs4_client_rele(cp);
7647 		goto out;
7648 	}
7649 
7650 	/*
7651 	 * An existing, confirmed client may exist but it may not have
7652 	 * been active for at least one lease period.  If so, then
7653 	 * "close" the client and create a new client identifier
7654 	 */
7655 	if (rfs4_lease_expired(cp)) {
7656 		rfs4_client_close(cp);
7657 		goto retry;
7658 	}
7659 
7660 	if (cp->need_confirm == TRUE)
7661 		cp_unconfirmed = cp;
7662 	else
7663 		cp_confirmed = cp;
7664 
7665 	cp = NULL;
7666 
7667 	/*
7668 	 * We have a confirmed client, now check for an
7669 	 * unconfimred entry
7670 	 */
7671 	if (cp_confirmed) {
7672 		/* If creds don't match then client identifier is inuse */
7673 		if (!creds_ok(cp_confirmed->cr_set, req, cs)) {
7674 			rfs4_cbinfo_t *cbp;
7675 			/*
7676 			 * Some one else has established this client
7677 			 * id. Try and say * who they are. We will use
7678 			 * the call back address supplied by * the
7679 			 * first client.
7680 			 */
7681 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7682 
7683 			addr = netid = NULL;
7684 
7685 			cbp = &cp_confirmed->cbinfo;
7686 			if (cbp->cb_callback.cb_location.r_addr &&
7687 			    cbp->cb_callback.cb_location.r_netid) {
7688 				cb_client4 *cbcp = &cbp->cb_callback;
7689 
7690 				len = strlen(cbcp->cb_location.r_addr)+1;
7691 				addr = kmem_alloc(len, KM_SLEEP);
7692 				bcopy(cbcp->cb_location.r_addr, addr, len);
7693 				len = strlen(cbcp->cb_location.r_netid)+1;
7694 				netid = kmem_alloc(len, KM_SLEEP);
7695 				bcopy(cbcp->cb_location.r_netid, netid, len);
7696 			}
7697 
7698 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
7699 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
7700 
7701 			rfs4_client_rele(cp_confirmed);
7702 		}
7703 
7704 		/*
7705 		 * Confirmed, creds match, and verifier matches; must
7706 		 * be an update of the callback info
7707 		 */
7708 		if (cp_confirmed->nfs_client.verifier ==
7709 		    args->client.verifier) {
7710 			/* Setup callback information */
7711 			rfs4_client_setcb(cp_confirmed, &args->callback,
7712 			    args->callback_ident);
7713 
7714 			/* everything okay -- move ahead */
7715 			*cs->statusp = res->status = NFS4_OK;
7716 			res->SETCLIENTID4res_u.resok4.clientid =
7717 			    cp_confirmed->clientid;
7718 
7719 			/* update the confirm_verifier and return it */
7720 			rfs4_client_scv_next(cp_confirmed);
7721 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7722 			    cp_confirmed->confirm_verf;
7723 
7724 			rfs4_client_rele(cp_confirmed);
7725 			goto out;
7726 		}
7727 
7728 		/*
7729 		 * Creds match but the verifier doesn't.  Must search
7730 		 * for an unconfirmed client that would be replaced by
7731 		 * this request.
7732 		 */
7733 		create = FALSE;
7734 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
7735 		    cp_confirmed);
7736 	}
7737 
7738 	/*
7739 	 * At this point, we have taken care of the brand new client
7740 	 * struct, INUSE case, update of an existing, and confirmed
7741 	 * client struct.
7742 	 */
7743 
7744 	/*
7745 	 * check to see if things have changed while we originally
7746 	 * picked up the client struct.  If they have, then return and
7747 	 * retry the processing of this SETCLIENTID request.
7748 	 */
7749 	if (cp_unconfirmed) {
7750 		rfs4_dbe_lock(cp_unconfirmed->dbe);
7751 		if (!cp_unconfirmed->need_confirm) {
7752 			rfs4_dbe_unlock(cp_unconfirmed->dbe);
7753 			rfs4_client_rele(cp_unconfirmed);
7754 			if (cp_confirmed)
7755 				rfs4_client_rele(cp_confirmed);
7756 			goto retry;
7757 		}
7758 		/* do away with the old unconfirmed one */
7759 		rfs4_dbe_invalidate(cp_unconfirmed->dbe);
7760 		rfs4_dbe_unlock(cp_unconfirmed->dbe);
7761 		rfs4_client_rele(cp_unconfirmed);
7762 		cp_unconfirmed = NULL;
7763 	}
7764 
7765 	/*
7766 	 * This search will temporarily hide the confirmed client
7767 	 * struct while a new client struct is created as the
7768 	 * unconfirmed one.
7769 	 */
7770 	create = TRUE;
7771 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
7772 
7773 	ASSERT(newcp != NULL);
7774 
7775 	if (newcp == NULL) {
7776 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7777 		rfs4_client_rele(cp_confirmed);
7778 		goto out;
7779 	}
7780 
7781 	/*
7782 	 * If one was not created, then a similar request must be in
7783 	 * process so release and start over with this one
7784 	 */
7785 	if (create != TRUE) {
7786 		rfs4_client_rele(newcp);
7787 		if (cp_confirmed)
7788 			rfs4_client_rele(cp_confirmed);
7789 		goto retry;
7790 	}
7791 
7792 	*cs->statusp = res->status = NFS4_OK;
7793 	res->SETCLIENTID4res_u.resok4.clientid = newcp->clientid;
7794 	res->SETCLIENTID4res_u.resok4.setclientid_confirm = newcp->confirm_verf;
7795 	/* Setup callback information; CB_NULL confirmation later */
7796 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
7797 
7798 	newcp->cp_confirmed = cp_confirmed;
7799 
7800 	rfs4_client_rele(newcp);
7801 
7802 out:
7803 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
7804 	    SETCLIENTID4res *, res);
7805 }
7806 
7807 /*ARGSUSED*/
7808 void
7809 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7810 			    struct svc_req *req, struct compound_state *cs)
7811 {
7812 	SETCLIENTID_CONFIRM4args *args =
7813 	    &argop->nfs_argop4_u.opsetclientid_confirm;
7814 	SETCLIENTID_CONFIRM4res *res =
7815 	    &resop->nfs_resop4_u.opsetclientid_confirm;
7816 	rfs4_client_t *cp, *cptoclose = NULL;
7817 
7818 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
7819 	    struct compound_state *, cs,
7820 	    SETCLIENTID_CONFIRM4args *, args);
7821 
7822 	*cs->statusp = res->status = NFS4_OK;
7823 
7824 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
7825 
7826 	if (cp == NULL) {
7827 		*cs->statusp = res->status =
7828 		    rfs4_check_clientid(&args->clientid, 1);
7829 		goto out;
7830 	}
7831 
7832 	if (!creds_ok(cp, req, cs)) {
7833 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7834 		rfs4_client_rele(cp);
7835 		goto out;
7836 	}
7837 
7838 	/* If the verifier doesn't match, the record doesn't match */
7839 	if (cp->confirm_verf != args->setclientid_confirm) {
7840 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
7841 		rfs4_client_rele(cp);
7842 		goto out;
7843 	}
7844 
7845 	rfs4_dbe_lock(cp->dbe);
7846 	cp->need_confirm = FALSE;
7847 	if (cp->cp_confirmed) {
7848 		cptoclose = cp->cp_confirmed;
7849 		cptoclose->ss_remove = 1;
7850 		cp->cp_confirmed = NULL;
7851 	}
7852 
7853 	/*
7854 	 * Update the client's associated server instance, if it's changed
7855 	 * since the client was created.
7856 	 */
7857 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
7858 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
7859 
7860 	/*
7861 	 * Record clientid in stable storage.
7862 	 * Must be done after server instance has been assigned.
7863 	 */
7864 	rfs4_ss_clid(cp, req);
7865 
7866 	rfs4_dbe_unlock(cp->dbe);
7867 
7868 	if (cptoclose)
7869 		/* don't need to rele, client_close does it */
7870 		rfs4_client_close(cptoclose);
7871 
7872 	/* If needed, initiate CB_NULL call for callback path */
7873 	rfs4_deleg_cb_check(cp);
7874 	rfs4_update_lease(cp);
7875 
7876 	/*
7877 	 * Check to see if client can perform reclaims
7878 	 */
7879 	rfs4_ss_chkclid(cp);
7880 
7881 	rfs4_client_rele(cp);
7882 
7883 out:
7884 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
7885 	    struct compound_state *, cs,
7886 	    SETCLIENTID_CONFIRM4 *, res);
7887 }
7888 
7889 
7890 /*ARGSUSED*/
7891 void
7892 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
7893 	    struct svc_req *req, struct compound_state *cs)
7894 {
7895 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
7896 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
7897 	rfs4_state_t *sp;
7898 	nfsstat4 status;
7899 
7900 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
7901 	    CLOSE4args *, args);
7902 
7903 	if (cs->vp == NULL) {
7904 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7905 		goto out;
7906 	}
7907 
7908 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
7909 	if (status != NFS4_OK) {
7910 		*cs->statusp = resp->status = status;
7911 		goto out;
7912 	}
7913 
7914 	/* Ensure specified filehandle matches */
7915 	if (cs->vp != sp->finfo->vp) {
7916 		rfs4_state_rele(sp);
7917 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7918 		goto out;
7919 	}
7920 
7921 	/* hold off other access to open_owner while we tinker */
7922 	rfs4_sw_enter(&sp->owner->oo_sw);
7923 
7924 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7925 	case NFS4_CHECK_STATEID_OKAY:
7926 		if (rfs4_check_open_seqid(args->seqid, sp->owner,
7927 		    resop) != NFS4_CHKSEQ_OKAY) {
7928 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7929 			goto end;
7930 		}
7931 		break;
7932 	case NFS4_CHECK_STATEID_OLD:
7933 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7934 		goto end;
7935 	case NFS4_CHECK_STATEID_BAD:
7936 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7937 		goto end;
7938 	case NFS4_CHECK_STATEID_EXPIRED:
7939 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7940 		goto end;
7941 	case NFS4_CHECK_STATEID_CLOSED:
7942 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7943 		goto end;
7944 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7945 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7946 		goto end;
7947 	case NFS4_CHECK_STATEID_REPLAY:
7948 		/* Check the sequence id for the open owner */
7949 		switch (rfs4_check_open_seqid(args->seqid, sp->owner, resop)) {
7950 		case NFS4_CHKSEQ_OKAY:
7951 			/*
7952 			 * This is replayed stateid; if seqid matches
7953 			 * next expected, then client is using wrong seqid.
7954 			 */
7955 			/* FALL THROUGH */
7956 		case NFS4_CHKSEQ_BAD:
7957 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7958 			goto end;
7959 		case NFS4_CHKSEQ_REPLAY:
7960 			/*
7961 			 * Note this case is the duplicate case so
7962 			 * resp->status is already set.
7963 			 */
7964 			*cs->statusp = resp->status;
7965 			rfs4_update_lease(sp->owner->client);
7966 			goto end;
7967 		}
7968 		break;
7969 	default:
7970 		ASSERT(FALSE);
7971 		break;
7972 	}
7973 
7974 	rfs4_dbe_lock(sp->dbe);
7975 
7976 	/* Update the stateid. */
7977 	next_stateid(&sp->stateid);
7978 	resp->open_stateid = sp->stateid.stateid;
7979 
7980 	rfs4_dbe_unlock(sp->dbe);
7981 
7982 	rfs4_update_lease(sp->owner->client);
7983 	rfs4_update_open_sequence(sp->owner);
7984 	rfs4_update_open_resp(sp->owner, resop, NULL);
7985 
7986 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
7987 
7988 	*cs->statusp = resp->status = status;
7989 
7990 end:
7991 	rfs4_sw_exit(&sp->owner->oo_sw);
7992 	rfs4_state_rele(sp);
7993 out:
7994 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
7995 	    CLOSE4res *, resp);
7996 }
7997 
7998 /*
7999  * Manage the counts on the file struct and close all file locks
8000  */
8001 /*ARGSUSED*/
8002 void
8003 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
8004 	bool_t close_of_client)
8005 {
8006 	rfs4_file_t *fp = sp->finfo;
8007 	rfs4_lo_state_t *lsp;
8008 	struct shrlock shr;
8009 	struct shr_locowner shr_loco;
8010 	int fflags, s_access, s_deny;
8011 
8012 	fflags = s_access = s_deny = 0;
8013 	/*
8014 	 * Decrement the count for each access and deny bit that this
8015 	 * state has contributed to the file. If the file counts go to zero
8016 	 * clear the appropriate bit in the appropriate mask.
8017 	 */
8018 
8019 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
8020 		fp->access_read--;
8021 		fflags |= FREAD;
8022 		s_access |= F_RDACC;
8023 		if (fp->access_read == 0)
8024 			fp->share_access &= ~OPEN4_SHARE_ACCESS_READ;
8025 	}
8026 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
8027 		fp->access_write--;
8028 		fflags |= FWRITE;
8029 		s_access |= F_WRACC;
8030 		if (fp->access_write == 0)
8031 			fp->share_access &= ~OPEN4_SHARE_ACCESS_WRITE;
8032 	}
8033 	if (sp->share_deny & OPEN4_SHARE_DENY_READ) {
8034 		fp->deny_read--;
8035 		s_deny |= F_RDDNY;
8036 		if (fp->deny_read == 0)
8037 			fp->share_deny &= ~OPEN4_SHARE_DENY_READ;
8038 	}
8039 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE) {
8040 		fp->deny_write--;
8041 		s_deny |= F_WRDNY;
8042 		if (fp->deny_write == 0)
8043 			fp->share_deny &= ~OPEN4_SHARE_DENY_WRITE;
8044 	}
8045 
8046 	/*
8047 	 * If this call is part of the larger closing down of client
8048 	 * state then it is just easier to release all locks
8049 	 * associated with this client instead of going through each
8050 	 * individual file and cleaning locks there.
8051 	 */
8052 	if (close_of_client) {
8053 		if (sp->owner->client->unlksys_completed == FALSE &&
8054 		    sp->lockownerlist.next->lsp != NULL &&
8055 		    sp->owner->client->sysidt != LM_NOSYSID) {
8056 			/* Is the PxFS kernel module loaded? */
8057 			if (lm_remove_file_locks != NULL) {
8058 				int new_sysid;
8059 
8060 				/* Encode the cluster nodeid in new sysid */
8061 				new_sysid = sp->owner->client->sysidt;
8062 				lm_set_nlmid_flk(&new_sysid);
8063 
8064 				/*
8065 				 * This PxFS routine removes file locks for a
8066 				 * client over all nodes of a cluster.
8067 				 */
8068 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8069 				    "lm_remove_file_locks(sysid=0x%x)\n",
8070 				    new_sysid));
8071 				(*lm_remove_file_locks)(new_sysid);
8072 			} else {
8073 				struct flock64 flk;
8074 
8075 				/* Release all locks for this client */
8076 				flk.l_type = F_UNLKSYS;
8077 				flk.l_whence = 0;
8078 				flk.l_start = 0;
8079 				flk.l_len = 0;
8080 				flk.l_sysid = sp->owner->client->sysidt;
8081 				flk.l_pid = 0;
8082 				(void) VOP_FRLOCK(sp->finfo->vp, F_SETLK, &flk,
8083 				    F_REMOTELOCK | FREAD | FWRITE,
8084 				    (u_offset_t)0, NULL, CRED(), NULL);
8085 			}
8086 
8087 			sp->owner->client->unlksys_completed = TRUE;
8088 		}
8089 	}
8090 
8091 	/*
8092 	 * Release all locks on this file by this lock owner or at
8093 	 * least mark the locks as having been released
8094 	 */
8095 	for (lsp = sp->lockownerlist.next->lsp; lsp != NULL;
8096 	    lsp = lsp->lockownerlist.next->lsp) {
8097 
8098 		lsp->locks_cleaned = TRUE;
8099 
8100 		/* Was this already taken care of above? */
8101 		if (!close_of_client &&
8102 		    sp->owner->client->sysidt != LM_NOSYSID)
8103 			(void) cleanlocks(sp->finfo->vp, lsp->locker->pid,
8104 			    lsp->locker->client->sysidt);
8105 	}
8106 
8107 	/*
8108 	 * Release any shrlocks associated with this open state ID.
8109 	 * This must be done before the rfs4_state gets marked closed.
8110 	 */
8111 	if (sp->owner->client->sysidt != LM_NOSYSID) {
8112 		shr.s_access = s_access;
8113 		shr.s_deny = s_deny;
8114 		shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
8115 		shr.s_sysid = sp->owner->client->sysidt;
8116 		shr_loco.sl_pid = shr.s_pid;
8117 		shr_loco.sl_id = shr.s_sysid;
8118 		shr.s_owner = (caddr_t)&shr_loco;
8119 		shr.s_own_len = sizeof (shr_loco);
8120 		(void) vop_shrlock(sp->finfo->vp, F_UNSHARE, &shr, fflags);
8121 	}
8122 
8123 	(void) VOP_CLOSE(fp->vp, fflags, 1, (offset_t)0, cr, NULL);
8124 }
8125 
8126 /*
8127  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8128  */
8129 static nfsstat4
8130 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8131 {
8132 	rfs4_lockowner_t *lo;
8133 	rfs4_client_t *cp;
8134 	uint32_t len;
8135 
8136 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
8137 	if (lo != NULL) {
8138 		cp = lo->client;
8139 		if (rfs4_lease_expired(cp)) {
8140 			rfs4_lockowner_rele(lo);
8141 			rfs4_dbe_hold(cp->dbe);
8142 			rfs4_client_close(cp);
8143 			return (NFS4ERR_EXPIRED);
8144 		}
8145 		dp->owner.clientid = lo->owner.clientid;
8146 		len = lo->owner.owner_len;
8147 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8148 		bcopy(lo->owner.owner_val, dp->owner.owner_val, len);
8149 		dp->owner.owner_len = len;
8150 		rfs4_lockowner_rele(lo);
8151 		goto finish;
8152 	}
8153 
8154 	/*
8155 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
8156 	 * of the client id contain the boot time for a NFS4 lock. So we
8157 	 * fabricate and identity by setting clientid to the sysid, and
8158 	 * the lock owner to the pid.
8159 	 */
8160 	dp->owner.clientid = flk->l_sysid;
8161 	len = sizeof (pid_t);
8162 	dp->owner.owner_len = len;
8163 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8164 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
8165 finish:
8166 	dp->offset = flk->l_start;
8167 	dp->length = flk->l_len;
8168 
8169 	if (flk->l_type == F_RDLCK)
8170 		dp->locktype = READ_LT;
8171 	else if (flk->l_type == F_WRLCK)
8172 		dp->locktype = WRITE_LT;
8173 	else
8174 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
8175 
8176 	return (NFS4_OK);
8177 }
8178 
8179 static int
8180 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8181 {
8182 	int error;
8183 	struct flock64 flk;
8184 	int i;
8185 	clock_t delaytime;
8186 	int cmd;
8187 
8188 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8189 retry:
8190 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8191 
8192 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8193 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8194 		error = VOP_FRLOCK(vp, cmd,
8195 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
8196 
8197 		if (error != EAGAIN && error != EACCES)
8198 			break;
8199 
8200 		if (i < rfs4_maxlock_tries - 1) {
8201 			delay(delaytime);
8202 			delaytime *= 2;
8203 		}
8204 	}
8205 
8206 	if (error == EAGAIN || error == EACCES) {
8207 		/* Get the owner of the lock */
8208 		flk = *flock;
8209 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8210 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag,
8211 		    (u_offset_t)0, NULL, cred, NULL) == 0) {
8212 			if (flk.l_type == F_UNLCK) {
8213 				/* No longer locked, retry */
8214 				goto retry;
8215 			}
8216 			*flock = flk;
8217 			LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8218 			    F_GETLK, &flk);
8219 		}
8220 	}
8221 
8222 	return (error);
8223 }
8224 
8225 /*ARGSUSED*/
8226 static nfsstat4
8227 rfs4_do_lock(rfs4_lo_state_t *lp, nfs_lock_type4 locktype,
8228 	    seqid4 seqid, offset4 offset,
8229 	    length4 length, cred_t *cred, nfs_resop4 *resop)
8230 {
8231 	nfsstat4 status;
8232 	rfs4_lockowner_t *lo = lp->locker;
8233 	rfs4_state_t *sp = lp->state;
8234 	struct flock64 flock;
8235 	int16_t ltype;
8236 	int flag;
8237 	int error;
8238 	sysid_t sysid;
8239 	LOCK4res *lres;
8240 
8241 	if (rfs4_lease_expired(lo->client)) {
8242 		return (NFS4ERR_EXPIRED);
8243 	}
8244 
8245 	if ((status = rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8246 		return (status);
8247 
8248 	/* Check for zero length. To lock to end of file use all ones for V4 */
8249 	if (length == 0)
8250 		return (NFS4ERR_INVAL);
8251 	else if (length == (length4)(~0))
8252 		length = 0;		/* Posix to end of file  */
8253 
8254 retry:
8255 	rfs4_dbe_lock(sp->dbe);
8256 
8257 
8258 	if (resop->resop != OP_LOCKU) {
8259 		switch (locktype) {
8260 		case READ_LT:
8261 		case READW_LT:
8262 			if ((sp->share_access
8263 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8264 				rfs4_dbe_unlock(sp->dbe);
8265 
8266 				return (NFS4ERR_OPENMODE);
8267 			}
8268 			ltype = F_RDLCK;
8269 			break;
8270 		case WRITE_LT:
8271 		case WRITEW_LT:
8272 			if ((sp->share_access
8273 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8274 				rfs4_dbe_unlock(sp->dbe);
8275 
8276 				return (NFS4ERR_OPENMODE);
8277 			}
8278 			ltype = F_WRLCK;
8279 			break;
8280 		}
8281 	} else
8282 		ltype = F_UNLCK;
8283 
8284 	flock.l_type = ltype;
8285 	flock.l_whence = 0;		/* SEEK_SET */
8286 	flock.l_start = offset;
8287 	flock.l_len = length;
8288 	flock.l_sysid = sysid;
8289 	flock.l_pid = lp->locker->pid;
8290 
8291 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8292 	if (flock.l_len < 0 || flock.l_start < 0) {
8293 		rfs4_dbe_unlock(sp->dbe);
8294 		return (NFS4ERR_INVAL);
8295 	}
8296 
8297 	/*
8298 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8299 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8300 	 */
8301 	flag = (int)sp->share_access | F_REMOTELOCK;
8302 
8303 	error = setlock(sp->finfo->vp, &flock, flag, cred);
8304 	if (error == 0) {
8305 		rfs4_dbe_lock(lp->dbe);
8306 		next_stateid(&lp->lockid);
8307 		rfs4_dbe_unlock(lp->dbe);
8308 	}
8309 
8310 	rfs4_dbe_unlock(sp->dbe);
8311 
8312 	/*
8313 	 * N.B. We map error values to nfsv4 errors. This is differrent
8314 	 * than puterrno4 routine.
8315 	 */
8316 	switch (error) {
8317 	case 0:
8318 		status = NFS4_OK;
8319 		break;
8320 	case EAGAIN:
8321 	case EACCES:		/* Old value */
8322 		/* Can only get here if op is OP_LOCK */
8323 		ASSERT(resop->resop == OP_LOCK);
8324 		lres = &resop->nfs_resop4_u.oplock;
8325 		status = NFS4ERR_DENIED;
8326 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8327 		    == NFS4ERR_EXPIRED)
8328 			goto retry;
8329 		break;
8330 	case ENOLCK:
8331 		status = NFS4ERR_DELAY;
8332 		break;
8333 	case EOVERFLOW:
8334 		status = NFS4ERR_INVAL;
8335 		break;
8336 	case EINVAL:
8337 		status = NFS4ERR_NOTSUPP;
8338 		break;
8339 	default:
8340 		cmn_err(CE_WARN, "rfs4_do_lock: unexpected errno (%d)", error);
8341 		status = NFS4ERR_SERVERFAULT;
8342 		break;
8343 	}
8344 
8345 	return (status);
8346 }
8347 
8348 /*ARGSUSED*/
8349 void
8350 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8351 	    struct svc_req *req, struct compound_state *cs)
8352 {
8353 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
8354 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8355 	nfsstat4 status;
8356 	stateid4 *stateid;
8357 	rfs4_lockowner_t *lo;
8358 	rfs4_client_t *cp;
8359 	rfs4_state_t *sp = NULL;
8360 	rfs4_lo_state_t *lsp = NULL;
8361 	bool_t ls_sw_held = FALSE;
8362 	bool_t create = TRUE;
8363 	bool_t lcreate = TRUE;
8364 	bool_t dup_lock = FALSE;
8365 	int rc;
8366 
8367 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8368 	    LOCK4args *, args);
8369 
8370 	if (cs->vp == NULL) {
8371 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8372 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8373 		    cs, LOCK4res *, resp);
8374 		return;
8375 	}
8376 
8377 	if (args->locker.new_lock_owner) {
8378 		/* Create a new lockowner for this instance */
8379 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8380 
8381 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8382 
8383 		stateid = &olo->open_stateid;
8384 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8385 		if (status != NFS4_OK) {
8386 			NFS4_DEBUG(rfs4_debug,
8387 			    (CE_NOTE, "Get state failed in lock %d", status));
8388 			*cs->statusp = resp->status = status;
8389 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8390 			    cs, LOCK4res *, resp);
8391 			return;
8392 		}
8393 
8394 		/* Ensure specified filehandle matches */
8395 		if (cs->vp != sp->finfo->vp) {
8396 			rfs4_state_rele(sp);
8397 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8398 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8399 			    cs, LOCK4res *, resp);
8400 			return;
8401 		}
8402 
8403 		/* hold off other access to open_owner while we tinker */
8404 		rfs4_sw_enter(&sp->owner->oo_sw);
8405 
8406 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8407 		case NFS4_CHECK_STATEID_OLD:
8408 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8409 			goto end;
8410 		case NFS4_CHECK_STATEID_BAD:
8411 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8412 			goto end;
8413 		case NFS4_CHECK_STATEID_EXPIRED:
8414 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8415 			goto end;
8416 		case NFS4_CHECK_STATEID_UNCONFIRMED:
8417 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8418 			goto end;
8419 		case NFS4_CHECK_STATEID_CLOSED:
8420 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8421 			goto end;
8422 		case NFS4_CHECK_STATEID_OKAY:
8423 		case NFS4_CHECK_STATEID_REPLAY:
8424 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8425 			    sp->owner, resop)) {
8426 			case NFS4_CHKSEQ_OKAY:
8427 				if (rc == NFS4_CHECK_STATEID_OKAY)
8428 					break;
8429 				/*
8430 				 * This is replayed stateid; if seqid
8431 				 * matches next expected, then client
8432 				 * is using wrong seqid.
8433 				 */
8434 				/* FALLTHROUGH */
8435 			case NFS4_CHKSEQ_BAD:
8436 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8437 				goto end;
8438 			case NFS4_CHKSEQ_REPLAY:
8439 				/* This is a duplicate LOCK request */
8440 				dup_lock = TRUE;
8441 
8442 				/*
8443 				 * For a duplicate we do not want to
8444 				 * create a new lockowner as it should
8445 				 * already exist.
8446 				 * Turn off the lockowner create flag.
8447 				 */
8448 				lcreate = FALSE;
8449 			}
8450 			break;
8451 		}
8452 
8453 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8454 		if (lo == NULL) {
8455 			NFS4_DEBUG(rfs4_debug,
8456 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
8457 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
8458 			goto end;
8459 		}
8460 
8461 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8462 		if (lsp == NULL) {
8463 			rfs4_update_lease(sp->owner->client);
8464 			/*
8465 			 * Only update theh open_seqid if this is not
8466 			 * a duplicate request
8467 			 */
8468 			if (dup_lock == FALSE) {
8469 				rfs4_update_open_sequence(sp->owner);
8470 			}
8471 
8472 			NFS4_DEBUG(rfs4_debug,
8473 			    (CE_NOTE, "rfs4_op_lock: no state"));
8474 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8475 			rfs4_update_open_resp(sp->owner, resop, NULL);
8476 			rfs4_lockowner_rele(lo);
8477 			goto end;
8478 		}
8479 
8480 		/*
8481 		 * This is the new_lock_owner branch and the client is
8482 		 * supposed to be associating a new lock_owner with
8483 		 * the open file at this point.  If we find that a
8484 		 * lock_owner/state association already exists and a
8485 		 * successful LOCK request was returned to the client,
8486 		 * an error is returned to the client since this is
8487 		 * not appropriate.  The client should be using the
8488 		 * existing lock_owner branch.
8489 		 */
8490 		if (dup_lock == FALSE && create == FALSE) {
8491 			if (lsp->lock_completed == TRUE) {
8492 				*cs->statusp =
8493 				    resp->status = NFS4ERR_BAD_SEQID;
8494 				rfs4_lockowner_rele(lo);
8495 				goto end;
8496 			}
8497 		}
8498 
8499 		rfs4_update_lease(sp->owner->client);
8500 
8501 		/*
8502 		 * Only update theh open_seqid if this is not
8503 		 * a duplicate request
8504 		 */
8505 		if (dup_lock == FALSE) {
8506 			rfs4_update_open_sequence(sp->owner);
8507 		}
8508 
8509 		/*
8510 		 * If this is a duplicate lock request, just copy the
8511 		 * previously saved reply and return.
8512 		 */
8513 		if (dup_lock == TRUE) {
8514 			/* verify that lock_seqid's match */
8515 			if (lsp->seqid != olo->lock_seqid) {
8516 				NFS4_DEBUG(rfs4_debug,
8517 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
8518 				    "lsp->seqid=%d old->seqid=%d",
8519 				    lsp->seqid, olo->lock_seqid));
8520 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8521 			} else {
8522 				rfs4_copy_reply(resop, lsp->reply);
8523 				/*
8524 				 * Make sure to copy the just
8525 				 * retrieved reply status into the
8526 				 * overall compound status
8527 				 */
8528 				*cs->statusp = resp->status;
8529 			}
8530 			rfs4_lockowner_rele(lo);
8531 			goto end;
8532 		}
8533 
8534 		rfs4_dbe_lock(lsp->dbe);
8535 
8536 		/* Make sure to update the lock sequence id */
8537 		lsp->seqid = olo->lock_seqid;
8538 
8539 		NFS4_DEBUG(rfs4_debug,
8540 		    (CE_NOTE, "Lock seqid established as %d", lsp->seqid));
8541 
8542 		/*
8543 		 * This is used to signify the newly created lockowner
8544 		 * stateid and its sequence number.  The checks for
8545 		 * sequence number and increment don't occur on the
8546 		 * very first lock request for a lockowner.
8547 		 */
8548 		lsp->skip_seqid_check = TRUE;
8549 
8550 		/* hold off other access to lsp while we tinker */
8551 		rfs4_sw_enter(&lsp->ls_sw);
8552 		ls_sw_held = TRUE;
8553 
8554 		rfs4_dbe_unlock(lsp->dbe);
8555 
8556 		rfs4_lockowner_rele(lo);
8557 	} else {
8558 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
8559 		/* get lsp and hold the lock on the underlying file struct */
8560 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
8561 		    != NFS4_OK) {
8562 			*cs->statusp = resp->status = status;
8563 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8564 			    cs, LOCK4res *, resp);
8565 			return;
8566 		}
8567 		create = FALSE;	/* We didn't create lsp */
8568 
8569 		/* Ensure specified filehandle matches */
8570 		if (cs->vp != lsp->state->finfo->vp) {
8571 			rfs4_lo_state_rele(lsp, TRUE);
8572 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8573 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8574 			    cs, LOCK4res *, resp);
8575 			return;
8576 		}
8577 
8578 		/* hold off other access to lsp while we tinker */
8579 		rfs4_sw_enter(&lsp->ls_sw);
8580 		ls_sw_held = TRUE;
8581 
8582 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8583 		/*
8584 		 * The stateid looks like it was okay (expected to be
8585 		 * the next one)
8586 		 */
8587 		case NFS4_CHECK_STATEID_OKAY:
8588 			/*
8589 			 * The sequence id is now checked.  Determine
8590 			 * if this is a replay or if it is in the
8591 			 * expected (next) sequence.  In the case of a
8592 			 * replay, there are two replay conditions
8593 			 * that may occur.  The first is the normal
8594 			 * condition where a LOCK is done with a
8595 			 * NFS4_OK response and the stateid is
8596 			 * updated.  That case is handled below when
8597 			 * the stateid is identified as a REPLAY.  The
8598 			 * second is the case where an error is
8599 			 * returned, like NFS4ERR_DENIED, and the
8600 			 * sequence number is updated but the stateid
8601 			 * is not updated.  This second case is dealt
8602 			 * with here.  So it may seem odd that the
8603 			 * stateid is okay but the sequence id is a
8604 			 * replay but it is okay.
8605 			 */
8606 			switch (rfs4_check_lock_seqid(
8607 			    args->locker.locker4_u.lock_owner.lock_seqid,
8608 			    lsp, resop)) {
8609 			case NFS4_CHKSEQ_REPLAY:
8610 				if (resp->status != NFS4_OK) {
8611 					/*
8612 					 * Here is our replay and need
8613 					 * to verify that the last
8614 					 * response was an error.
8615 					 */
8616 					*cs->statusp = resp->status;
8617 					goto end;
8618 				}
8619 				/*
8620 				 * This is done since the sequence id
8621 				 * looked like a replay but it didn't
8622 				 * pass our check so a BAD_SEQID is
8623 				 * returned as a result.
8624 				 */
8625 				/*FALLTHROUGH*/
8626 			case NFS4_CHKSEQ_BAD:
8627 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8628 				goto end;
8629 			case NFS4_CHKSEQ_OKAY:
8630 				/* Everything looks okay move ahead */
8631 				break;
8632 			}
8633 			break;
8634 		case NFS4_CHECK_STATEID_OLD:
8635 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8636 			goto end;
8637 		case NFS4_CHECK_STATEID_BAD:
8638 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8639 			goto end;
8640 		case NFS4_CHECK_STATEID_EXPIRED:
8641 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8642 			goto end;
8643 		case NFS4_CHECK_STATEID_CLOSED:
8644 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8645 			goto end;
8646 		case NFS4_CHECK_STATEID_REPLAY:
8647 			switch (rfs4_check_lock_seqid(
8648 			    args->locker.locker4_u.lock_owner.lock_seqid,
8649 			    lsp, resop)) {
8650 			case NFS4_CHKSEQ_OKAY:
8651 				/*
8652 				 * This is a replayed stateid; if
8653 				 * seqid matches the next expected,
8654 				 * then client is using wrong seqid.
8655 				 */
8656 			case NFS4_CHKSEQ_BAD:
8657 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8658 				goto end;
8659 			case NFS4_CHKSEQ_REPLAY:
8660 				rfs4_update_lease(lsp->locker->client);
8661 				*cs->statusp = status = resp->status;
8662 				goto end;
8663 			}
8664 			break;
8665 		default:
8666 			ASSERT(FALSE);
8667 			break;
8668 		}
8669 
8670 		rfs4_update_lock_sequence(lsp);
8671 		rfs4_update_lease(lsp->locker->client);
8672 	}
8673 
8674 	/*
8675 	 * NFS4 only allows locking on regular files, so
8676 	 * verify type of object.
8677 	 */
8678 	if (cs->vp->v_type != VREG) {
8679 		if (cs->vp->v_type == VDIR)
8680 			status = NFS4ERR_ISDIR;
8681 		else
8682 			status = NFS4ERR_INVAL;
8683 		goto out;
8684 	}
8685 
8686 	cp = lsp->state->owner->client;
8687 
8688 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
8689 		status = NFS4ERR_GRACE;
8690 		goto out;
8691 	}
8692 
8693 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->can_reclaim) {
8694 		status = NFS4ERR_NO_GRACE;
8695 		goto out;
8696 	}
8697 
8698 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
8699 		status = NFS4ERR_NO_GRACE;
8700 		goto out;
8701 	}
8702 
8703 	if (lsp->state->finfo->dinfo->dtype == OPEN_DELEGATE_WRITE)
8704 		cs->deleg = TRUE;
8705 
8706 	status = rfs4_do_lock(lsp, args->locktype,
8707 	    args->locker.locker4_u.lock_owner.lock_seqid,
8708 	    args->offset, args->length, cs->cr, resop);
8709 
8710 out:
8711 	lsp->skip_seqid_check = FALSE;
8712 
8713 	*cs->statusp = resp->status = status;
8714 
8715 	if (status == NFS4_OK) {
8716 		resp->LOCK4res_u.lock_stateid = lsp->lockid.stateid;
8717 		lsp->lock_completed = TRUE;
8718 	}
8719 	/*
8720 	 * Only update the "OPEN" response here if this was a new
8721 	 * lock_owner
8722 	 */
8723 	if (sp)
8724 		rfs4_update_open_resp(sp->owner, resop, NULL);
8725 
8726 	rfs4_update_lock_resp(lsp, resop);
8727 
8728 end:
8729 	if (lsp) {
8730 		if (ls_sw_held)
8731 			rfs4_sw_exit(&lsp->ls_sw);
8732 		/*
8733 		 * If an sp obtained, then the lsp does not represent
8734 		 * a lock on the file struct.
8735 		 */
8736 		if (sp != NULL)
8737 			rfs4_lo_state_rele(lsp, FALSE);
8738 		else
8739 			rfs4_lo_state_rele(lsp, TRUE);
8740 	}
8741 	if (sp) {
8742 		rfs4_sw_exit(&sp->owner->oo_sw);
8743 		rfs4_state_rele(sp);
8744 	}
8745 
8746 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
8747 	    LOCK4res *, resp);
8748 }
8749 
8750 /* free function for LOCK/LOCKT */
8751 static void
8752 lock_denied_free(nfs_resop4 *resop)
8753 {
8754 	LOCK4denied *dp = NULL;
8755 
8756 	switch (resop->resop) {
8757 	case OP_LOCK:
8758 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
8759 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
8760 		break;
8761 	case OP_LOCKT:
8762 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
8763 			dp = &resop->nfs_resop4_u.oplockt.denied;
8764 		break;
8765 	default:
8766 		break;
8767 	}
8768 
8769 	if (dp)
8770 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
8771 }
8772 
8773 /*ARGSUSED*/
8774 void
8775 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
8776 	    struct svc_req *req, struct compound_state *cs)
8777 {
8778 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
8779 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
8780 	nfsstat4 status;
8781 	stateid4 *stateid = &args->lock_stateid;
8782 	rfs4_lo_state_t *lsp;
8783 
8784 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
8785 	    LOCKU4args *, args);
8786 
8787 	if (cs->vp == NULL) {
8788 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8789 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8790 		    LOCKU4res *, resp);
8791 		return;
8792 	}
8793 
8794 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
8795 		*cs->statusp = resp->status = status;
8796 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8797 		    LOCKU4res *, resp);
8798 		return;
8799 	}
8800 
8801 	/* Ensure specified filehandle matches */
8802 	if (cs->vp != lsp->state->finfo->vp) {
8803 		rfs4_lo_state_rele(lsp, TRUE);
8804 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8805 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8806 		    LOCKU4res *, resp);
8807 		return;
8808 	}
8809 
8810 	/* hold off other access to lsp while we tinker */
8811 	rfs4_sw_enter(&lsp->ls_sw);
8812 
8813 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8814 	case NFS4_CHECK_STATEID_OKAY:
8815 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
8816 		    != NFS4_CHKSEQ_OKAY) {
8817 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8818 			goto end;
8819 		}
8820 		break;
8821 	case NFS4_CHECK_STATEID_OLD:
8822 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8823 		goto end;
8824 	case NFS4_CHECK_STATEID_BAD:
8825 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8826 		goto end;
8827 	case NFS4_CHECK_STATEID_EXPIRED:
8828 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8829 		goto end;
8830 	case NFS4_CHECK_STATEID_CLOSED:
8831 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8832 		goto end;
8833 	case NFS4_CHECK_STATEID_REPLAY:
8834 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
8835 		case NFS4_CHKSEQ_OKAY:
8836 				/*
8837 				 * This is a replayed stateid; if
8838 				 * seqid matches the next expected,
8839 				 * then client is using wrong seqid.
8840 				 */
8841 		case NFS4_CHKSEQ_BAD:
8842 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8843 			goto end;
8844 		case NFS4_CHKSEQ_REPLAY:
8845 			rfs4_update_lease(lsp->locker->client);
8846 			*cs->statusp = status = resp->status;
8847 			goto end;
8848 		}
8849 		break;
8850 	default:
8851 		ASSERT(FALSE);
8852 		break;
8853 	}
8854 
8855 	rfs4_update_lock_sequence(lsp);
8856 	rfs4_update_lease(lsp->locker->client);
8857 
8858 	/*
8859 	 * NFS4 only allows locking on regular files, so
8860 	 * verify type of object.
8861 	 */
8862 	if (cs->vp->v_type != VREG) {
8863 		if (cs->vp->v_type == VDIR)
8864 			status = NFS4ERR_ISDIR;
8865 		else
8866 			status = NFS4ERR_INVAL;
8867 		goto out;
8868 	}
8869 
8870 	if (rfs4_clnt_in_grace(lsp->state->owner->client)) {
8871 		status = NFS4ERR_GRACE;
8872 		goto out;
8873 	}
8874 
8875 	status = rfs4_do_lock(lsp, args->locktype,
8876 	    args->seqid, args->offset, args->length, cs->cr, resop);
8877 
8878 out:
8879 	*cs->statusp = resp->status = status;
8880 
8881 	if (status == NFS4_OK)
8882 		resp->lock_stateid = lsp->lockid.stateid;
8883 
8884 	rfs4_update_lock_resp(lsp, resop);
8885 
8886 end:
8887 	rfs4_sw_exit(&lsp->ls_sw);
8888 	rfs4_lo_state_rele(lsp, TRUE);
8889 
8890 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8891 	    LOCKU4res *, resp);
8892 }
8893 
8894 /*
8895  * LOCKT is a best effort routine, the client can not be guaranteed that
8896  * the status return is still in effect by the time the reply is received.
8897  * They are numerous race conditions in this routine, but we are not required
8898  * and can not be accurate.
8899  */
8900 /*ARGSUSED*/
8901 void
8902 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
8903 	    struct svc_req *req, struct compound_state *cs)
8904 {
8905 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
8906 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
8907 	rfs4_lockowner_t *lo;
8908 	rfs4_client_t *cp;
8909 	bool_t create = FALSE;
8910 	struct flock64 flk;
8911 	int error;
8912 	int flag = FREAD | FWRITE;
8913 	int ltype;
8914 	length4 posix_length;
8915 	sysid_t sysid;
8916 	pid_t pid;
8917 
8918 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
8919 	    LOCKT4args *, args);
8920 
8921 	if (cs->vp == NULL) {
8922 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8923 		goto out;
8924 	}
8925 
8926 	/*
8927 	 * NFS4 only allows locking on regular files, so
8928 	 * verify type of object.
8929 	 */
8930 	if (cs->vp->v_type != VREG) {
8931 		if (cs->vp->v_type == VDIR)
8932 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
8933 		else
8934 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
8935 		goto out;
8936 	}
8937 
8938 	/*
8939 	 * Check out the clientid to ensure the server knows about it
8940 	 * so that we correctly inform the client of a server reboot.
8941 	 */
8942 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
8943 	    == NULL) {
8944 		*cs->statusp = resp->status =
8945 		    rfs4_check_clientid(&args->owner.clientid, 0);
8946 		goto out;
8947 	}
8948 	if (rfs4_lease_expired(cp)) {
8949 		rfs4_client_close(cp);
8950 		/*
8951 		 * Protocol doesn't allow returning NFS4ERR_STALE as
8952 		 * other operations do on this check so STALE_CLIENTID
8953 		 * is returned instead
8954 		 */
8955 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
8956 		goto out;
8957 	}
8958 
8959 	if (rfs4_clnt_in_grace(cp) && !(cp->can_reclaim)) {
8960 		*cs->statusp = resp->status = NFS4ERR_GRACE;
8961 		rfs4_client_rele(cp);
8962 		goto out;
8963 	}
8964 	rfs4_client_rele(cp);
8965 
8966 	resp->status = NFS4_OK;
8967 
8968 	switch (args->locktype) {
8969 	case READ_LT:
8970 	case READW_LT:
8971 		ltype = F_RDLCK;
8972 		break;
8973 	case WRITE_LT:
8974 	case WRITEW_LT:
8975 		ltype = F_WRLCK;
8976 		break;
8977 	}
8978 
8979 	posix_length = args->length;
8980 	/* Check for zero length. To lock to end of file use all ones for V4 */
8981 	if (posix_length == 0) {
8982 		*cs->statusp = resp->status = NFS4ERR_INVAL;
8983 		goto out;
8984 	} else if (posix_length == (length4)(~0)) {
8985 		posix_length = 0;	/* Posix to end of file  */
8986 	}
8987 
8988 	/* Find or create a lockowner */
8989 	lo = rfs4_findlockowner(&args->owner, &create);
8990 
8991 	if (lo) {
8992 		pid = lo->pid;
8993 		if ((resp->status =
8994 		    rfs4_client_sysid(lo->client, &sysid)) != NFS4_OK)
8995 			goto err;
8996 	} else {
8997 		pid = 0;
8998 		sysid = lockt_sysid;
8999 	}
9000 retry:
9001 	flk.l_type = ltype;
9002 	flk.l_whence = 0;		/* SEEK_SET */
9003 	flk.l_start = args->offset;
9004 	flk.l_len = posix_length;
9005 	flk.l_sysid = sysid;
9006 	flk.l_pid = pid;
9007 	flag |= F_REMOTELOCK;
9008 
9009 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
9010 
9011 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
9012 	if (flk.l_len < 0 || flk.l_start < 0) {
9013 		resp->status = NFS4ERR_INVAL;
9014 		goto err;
9015 	}
9016 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9017 	    NULL, cs->cr, NULL);
9018 
9019 	/*
9020 	 * N.B. We map error values to nfsv4 errors. This is differrent
9021 	 * than puterrno4 routine.
9022 	 */
9023 	switch (error) {
9024 	case 0:
9025 		if (flk.l_type == F_UNLCK)
9026 			resp->status = NFS4_OK;
9027 		else {
9028 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
9029 				goto retry;
9030 			resp->status = NFS4ERR_DENIED;
9031 		}
9032 		break;
9033 	case EOVERFLOW:
9034 		resp->status = NFS4ERR_INVAL;
9035 		break;
9036 	case EINVAL:
9037 		resp->status = NFS4ERR_NOTSUPP;
9038 		break;
9039 	default:
9040 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
9041 		    error);
9042 		resp->status = NFS4ERR_SERVERFAULT;
9043 		break;
9044 	}
9045 
9046 err:
9047 	if (lo)
9048 		rfs4_lockowner_rele(lo);
9049 	*cs->statusp = resp->status;
9050 out:
9051 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9052 	    LOCKT4res *, resp);
9053 }
9054 
9055 static int
9056 vop_shrlock(vnode_t *vp, int cmd, struct shrlock *sp, int fflags)
9057 {
9058 	int err;
9059 
9060 	if (cmd == F_UNSHARE && sp->s_deny == 0 && sp->s_access == 0)
9061 		return (0);
9062 
9063 	err = VOP_SHRLOCK(vp, cmd, sp, fflags, CRED(), NULL);
9064 
9065 	NFS4_DEBUG(rfs4_shrlock_debug,
9066 	    (CE_NOTE, "rfs4_shrlock %s vp=%p acc=%d dny=%d sysid=%d "
9067 	    "pid=%d err=%d\n", cmd == F_UNSHARE ? "UNSHR" : "SHARE",
9068 	    (void *) vp, sp->s_access, sp->s_deny, sp->s_sysid, sp->s_pid,
9069 	    err));
9070 
9071 	return (err);
9072 }
9073 
9074 static int
9075 rfs4_shrlock(rfs4_state_t *sp, int cmd)
9076 {
9077 	struct shrlock shr;
9078 	struct shr_locowner shr_loco;
9079 	int fflags;
9080 
9081 	fflags = shr.s_access = shr.s_deny = 0;
9082 
9083 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ) {
9084 		fflags |= FREAD;
9085 		shr.s_access |= F_RDACC;
9086 	}
9087 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE) {
9088 		fflags |= FWRITE;
9089 		shr.s_access |= F_WRACC;
9090 	}
9091 	if (sp->share_deny & OPEN4_SHARE_DENY_READ)
9092 		shr.s_deny |= F_RDDNY;
9093 	if (sp->share_deny & OPEN4_SHARE_DENY_WRITE)
9094 		shr.s_deny |= F_WRDNY;
9095 
9096 	shr.s_pid = rfs4_dbe_getid(sp->owner->dbe);
9097 	shr.s_sysid = sp->owner->client->sysidt;
9098 	shr_loco.sl_pid = shr.s_pid;
9099 	shr_loco.sl_id = shr.s_sysid;
9100 	shr.s_owner = (caddr_t)&shr_loco;
9101 	shr.s_own_len = sizeof (shr_loco);
9102 	return (vop_shrlock(sp->finfo->vp, cmd, &shr, fflags));
9103 }
9104 
9105 static int
9106 rfs4_share(rfs4_state_t *sp)
9107 {
9108 	int cmd;
9109 	cmd = nbl_need_check(sp->finfo->vp) ? F_SHARE_NBMAND : F_SHARE;
9110 	return (rfs4_shrlock(sp, cmd));
9111 }
9112 
9113 void
9114 rfs4_unshare(rfs4_state_t *sp)
9115 {
9116 	(void) rfs4_shrlock(sp, F_UNSHARE);
9117 }
9118 
9119 static int
9120 rdma_setup_read_data4(READ4args * args, READ4res * rok)
9121 {
9122 	struct clist	*wcl;
9123 	int		data_len, avail_len, num;
9124 	count4		count = rok->data_len;
9125 
9126 	data_len = num = avail_len = 0;
9127 
9128 	wcl = args->wlist;
9129 	while (wcl != NULL) {
9130 		if (wcl->c_dmemhandle.mrc_rmr == 0)
9131 			break;
9132 
9133 		avail_len += wcl->c_len;
9134 		if (wcl->c_len < count) {
9135 			data_len += wcl->c_len;
9136 		} else {
9137 			/* Can make the rest chunks all 0-len */
9138 			data_len += count;
9139 			wcl->c_len = count;
9140 		}
9141 		count -= wcl->c_len;
9142 		num++;
9143 		wcl = wcl->c_next;
9144 	}
9145 
9146 	/*
9147 	 * MUST fail if there are still more data
9148 	 */
9149 	if (count > 0) {
9150 		DTRACE_PROBE2(nfss__e__read4_wlist_fail,
9151 		    int, data_len, int, count);
9152 		return (FALSE);
9153 	}
9154 	wcl = args->wlist;
9155 	rok->data_len = data_len;
9156 	rok->wlist_len = data_len;
9157 	rok->wlist = wcl;
9158 
9159 	return (TRUE);
9160 }
9161