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