xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision d17be682a2c70b4505d43c830bbd2603da11918d)
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 		if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) {
2206 			sargp->sbp = NULL;	/* to identify error */
2207 			return (puterrno4(error));
2208 		}
2209 	}
2210 
2211 	return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr));
2212 }
2213 
2214 static void
2215 nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp)
2216 {
2217 	ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size,
2218 	    KM_SLEEP);
2219 	ntovp->attrcnt = 0;
2220 	ntovp->vfsstat = FALSE;
2221 }
2222 
2223 static void
2224 nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
2225     struct nfs4_svgetit_arg *sargp)
2226 {
2227 	int i;
2228 	union nfs4_attr_u *na;
2229 	uint8_t *amap;
2230 
2231 	/*
2232 	 * XXX Should do the same checks for whether the bit is set
2233 	 */
2234 	for (i = 0, na = ntovp->na, amap = ntovp->amap;
2235 	    i < ntovp->attrcnt; i++, na++, amap++) {
2236 		(void) (*nfs4_ntov_map[*amap].sv_getit)(
2237 		    NFS4ATTR_FREEIT, sargp, na);
2238 	}
2239 	if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) {
2240 		/*
2241 		 * xdr_free for getattr will be done later
2242 		 */
2243 		for (i = 0, na = ntovp->na, amap = ntovp->amap;
2244 		    i < ntovp->attrcnt; i++, na++, amap++) {
2245 			xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na);
2246 		}
2247 	}
2248 	kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size);
2249 }
2250 
2251 /*
2252  * do_rfs4_op_getattr gets the system attrs and converts into fattr4.
2253  */
2254 static nfsstat4
2255 do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp,
2256     struct nfs4_svgetit_arg *sargp)
2257 {
2258 	int error = 0;
2259 	int i, k;
2260 	struct nfs4_ntov_table ntov;
2261 	XDR xdr;
2262 	ulong_t xdr_size;
2263 	char *xdr_attrs;
2264 	nfsstat4 status = NFS4_OK;
2265 	nfsstat4 prev_rdattr_error = sargp->rdattr_error;
2266 	union nfs4_attr_u *na;
2267 	uint8_t *amap;
2268 
2269 	sargp->op = NFS4ATTR_GETIT;
2270 	sargp->flag = 0;
2271 
2272 	fattrp->attrmask = 0;
2273 	/* if no bits requested, then return empty fattr4 */
2274 	if (breq == 0) {
2275 		fattrp->attrlist4_len = 0;
2276 		fattrp->attrlist4 = NULL;
2277 		return (NFS4_OK);
2278 	}
2279 
2280 	/*
2281 	 * return NFS4ERR_INVAL when client requests write-only attrs
2282 	 */
2283 	if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK))
2284 		return (NFS4ERR_INVAL);
2285 
2286 	nfs4_ntov_table_init(&ntov);
2287 	na = ntov.na;
2288 	amap = ntov.amap;
2289 
2290 	/*
2291 	 * Now loop to get or verify the attrs
2292 	 */
2293 	for (i = 0; i < nfs4_ntov_map_size; i++) {
2294 		if (breq & nfs4_ntov_map[i].fbit) {
2295 			if ((*nfs4_ntov_map[i].sv_getit)(
2296 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0) {
2297 
2298 				error = (*nfs4_ntov_map[i].sv_getit)(
2299 				    NFS4ATTR_GETIT, sargp, na);
2300 
2301 				/*
2302 				 * Possible error values:
2303 				 * >0 if sv_getit failed to
2304 				 * get the attr; 0 if succeeded;
2305 				 * <0 if rdattr_error and the
2306 				 * attribute cannot be returned.
2307 				 */
2308 				if (error && !(sargp->rdattr_error_req))
2309 					goto done;
2310 				/*
2311 				 * If error then just for entry
2312 				 */
2313 				if (error == 0) {
2314 					fattrp->attrmask |=
2315 					    nfs4_ntov_map[i].fbit;
2316 					*amap++ =
2317 					    (uint8_t)nfs4_ntov_map[i].nval;
2318 					na++;
2319 					(ntov.attrcnt)++;
2320 				} else if ((error > 0) &&
2321 				    (sargp->rdattr_error == NFS4_OK)) {
2322 					sargp->rdattr_error = puterrno4(error);
2323 				}
2324 				error = 0;
2325 			}
2326 		}
2327 	}
2328 
2329 	/*
2330 	 * If rdattr_error was set after the return value for it was assigned,
2331 	 * update it.
2332 	 */
2333 	if (prev_rdattr_error != sargp->rdattr_error) {
2334 		na = ntov.na;
2335 		amap = ntov.amap;
2336 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2337 			k = *amap;
2338 			if (k < FATTR4_RDATTR_ERROR) {
2339 				continue;
2340 			}
2341 			if ((k == FATTR4_RDATTR_ERROR) &&
2342 			    ((*nfs4_ntov_map[k].sv_getit)(
2343 			    NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) {
2344 
2345 				(void) (*nfs4_ntov_map[k].sv_getit)(
2346 				    NFS4ATTR_GETIT, sargp, na);
2347 			}
2348 			break;
2349 		}
2350 	}
2351 
2352 	xdr_size = 0;
2353 	na = ntov.na;
2354 	amap = ntov.amap;
2355 	for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2356 		xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na);
2357 	}
2358 
2359 	fattrp->attrlist4_len = xdr_size;
2360 	if (xdr_size) {
2361 		/* freed by rfs4_op_getattr_free() */
2362 		fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP);
2363 
2364 		xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE);
2365 
2366 		na = ntov.na;
2367 		amap = ntov.amap;
2368 		for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2369 			if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) {
2370 				DTRACE_PROBE1(nfss__e__getattr4_encfail,
2371 				    int, *amap);
2372 				status = NFS4ERR_SERVERFAULT;
2373 				break;
2374 			}
2375 		}
2376 		/* xdrmem_destroy(&xdrs); */	/* NO-OP */
2377 	} else {
2378 		fattrp->attrlist4 = NULL;
2379 	}
2380 done:
2381 
2382 	nfs4_ntov_table_free(&ntov, sargp);
2383 
2384 	if (error != 0)
2385 		status = puterrno4(error);
2386 
2387 	return (status);
2388 }
2389 
2390 /* ARGSUSED */
2391 static void
2392 rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2393     struct compound_state *cs)
2394 {
2395 	GETATTR4args *args = &argop->nfs_argop4_u.opgetattr;
2396 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2397 	struct nfs4_svgetit_arg sarg;
2398 	struct statvfs64 sb;
2399 	nfsstat4 status;
2400 
2401 	DTRACE_NFSV4_2(op__getattr__start, struct compound_state *, cs,
2402 	    GETATTR4args *, args);
2403 
2404 	if (cs->vp == NULL) {
2405 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2406 		goto out;
2407 	}
2408 
2409 	if (cs->access == CS_ACCESS_DENIED) {
2410 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2411 		goto out;
2412 	}
2413 
2414 	sarg.sbp = &sb;
2415 	sarg.cs = cs;
2416 	sarg.is_referral = B_FALSE;
2417 
2418 	status = bitmap4_to_attrmask(args->attr_request, &sarg);
2419 	if (status == NFS4_OK) {
2420 
2421 		status = bitmap4_get_sysattrs(&sarg);
2422 		if (status == NFS4_OK) {
2423 
2424 			/* Is this a referral? */
2425 			if (vn_is_nfs_reparse(cs->vp, cs->cr)) {
2426 				/* Older V4 Solaris client sees a link */
2427 				if (client_is_downrev(req))
2428 					sarg.vap->va_type = VLNK;
2429 				else
2430 					sarg.is_referral = B_TRUE;
2431 			}
2432 
2433 			status = do_rfs4_op_getattr(args->attr_request,
2434 			    &resp->obj_attributes, &sarg);
2435 		}
2436 	}
2437 	*cs->statusp = resp->status = status;
2438 out:
2439 	DTRACE_NFSV4_2(op__getattr__done, struct compound_state *, cs,
2440 	    GETATTR4res *, resp);
2441 }
2442 
2443 static void
2444 rfs4_op_getattr_free(nfs_resop4 *resop)
2445 {
2446 	GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2447 
2448 	nfs4_fattr4_free(&resp->obj_attributes);
2449 }
2450 
2451 /* ARGSUSED */
2452 static void
2453 rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2454     struct compound_state *cs)
2455 {
2456 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2457 
2458 	DTRACE_NFSV4_1(op__getfh__start, struct compound_state *, cs);
2459 
2460 	if (cs->vp == NULL) {
2461 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2462 		goto out;
2463 	}
2464 	if (cs->access == CS_ACCESS_DENIED) {
2465 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2466 		goto out;
2467 	}
2468 
2469 	/* check for reparse point at the share point */
2470 	if (cs->exi->exi_moved || vn_is_nfs_reparse(cs->exi->exi_vp, cs->cr)) {
2471 		/* it's all bad */
2472 		cs->exi->exi_moved = 1;
2473 		*cs->statusp = resp->status = NFS4ERR_MOVED;
2474 		DTRACE_PROBE2(nfs4serv__func__referral__shared__moved,
2475 		    vnode_t *, cs->vp, char *, "rfs4_op_getfh");
2476 		return;
2477 	}
2478 
2479 	/* check for reparse point at vp */
2480 	if (vn_is_nfs_reparse(cs->vp, cs->cr) && !client_is_downrev(req)) {
2481 		/* it's not all bad */
2482 		*cs->statusp = resp->status = NFS4ERR_MOVED;
2483 		DTRACE_PROBE2(nfs4serv__func__referral__moved,
2484 		    vnode_t *, cs->vp, char *, "rfs4_op_getfh");
2485 		return;
2486 	}
2487 
2488 	resp->object.nfs_fh4_val =
2489 	    kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP);
2490 	nfs_fh4_copy(&cs->fh, &resp->object);
2491 	*cs->statusp = resp->status = NFS4_OK;
2492 out:
2493 	DTRACE_NFSV4_2(op__getfh__done, struct compound_state *, cs,
2494 	    GETFH4res *, resp);
2495 }
2496 
2497 static void
2498 rfs4_op_getfh_free(nfs_resop4 *resop)
2499 {
2500 	GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2501 
2502 	if (resp->status == NFS4_OK &&
2503 	    resp->object.nfs_fh4_val != NULL) {
2504 		kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len);
2505 		resp->object.nfs_fh4_val = NULL;
2506 		resp->object.nfs_fh4_len = 0;
2507 	}
2508 }
2509 
2510 /*
2511  * illegal: args: void
2512  *	    res : status (NFS4ERR_OP_ILLEGAL)
2513  */
2514 /* ARGSUSED */
2515 static void
2516 rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop,
2517     struct svc_req *req, struct compound_state *cs)
2518 {
2519 	ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal;
2520 
2521 	resop->resop = OP_ILLEGAL;
2522 	*cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
2523 }
2524 
2525 /*
2526  * link: args: SAVED_FH: file, CURRENT_FH: target directory
2527  *	 res: status. If success - CURRENT_FH unchanged, return change_info
2528  */
2529 /* ARGSUSED */
2530 static void
2531 rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2532     struct compound_state *cs)
2533 {
2534 	LINK4args *args = &argop->nfs_argop4_u.oplink;
2535 	LINK4res *resp = &resop->nfs_resop4_u.oplink;
2536 	int error;
2537 	vnode_t *vp;
2538 	vnode_t *dvp;
2539 	struct vattr bdva, idva, adva;
2540 	char *nm;
2541 	uint_t  len;
2542 	struct sockaddr *ca;
2543 	char *name = NULL;
2544 	nfsstat4 status;
2545 
2546 	DTRACE_NFSV4_2(op__link__start, struct compound_state *, cs,
2547 	    LINK4args *, args);
2548 
2549 	/* SAVED_FH: source object */
2550 	vp = cs->saved_vp;
2551 	if (vp == NULL) {
2552 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2553 		goto out;
2554 	}
2555 
2556 	/* CURRENT_FH: target directory */
2557 	dvp = cs->vp;
2558 	if (dvp == NULL) {
2559 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2560 		goto out;
2561 	}
2562 
2563 	/*
2564 	 * If there is a non-shared filesystem mounted on this vnode,
2565 	 * do not allow to link any file in this directory.
2566 	 */
2567 	if (vn_ismntpt(dvp)) {
2568 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2569 		goto out;
2570 	}
2571 
2572 	if (cs->access == CS_ACCESS_DENIED) {
2573 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
2574 		goto out;
2575 	}
2576 
2577 	/* Check source object's type validity */
2578 	if (vp->v_type == VDIR) {
2579 		*cs->statusp = resp->status = NFS4ERR_ISDIR;
2580 		goto out;
2581 	}
2582 
2583 	/* Check target directory's type */
2584 	if (dvp->v_type != VDIR) {
2585 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
2586 		goto out;
2587 	}
2588 
2589 	if (cs->saved_exi != cs->exi) {
2590 		*cs->statusp = resp->status = NFS4ERR_XDEV;
2591 		goto out;
2592 	}
2593 
2594 	status = utf8_dir_verify(&args->newname);
2595 	if (status != NFS4_OK) {
2596 		*cs->statusp = resp->status = status;
2597 		goto out;
2598 	}
2599 
2600 	nm = utf8_to_fn(&args->newname, &len, NULL);
2601 	if (nm == NULL) {
2602 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2603 		goto out;
2604 	}
2605 
2606 	if (len > MAXNAMELEN) {
2607 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
2608 		kmem_free(nm, len);
2609 		goto out;
2610 	}
2611 
2612 	if (rdonly4(req, cs)) {
2613 		*cs->statusp = resp->status = NFS4ERR_ROFS;
2614 		kmem_free(nm, len);
2615 		goto out;
2616 	}
2617 
2618 	/* Get "before" change value */
2619 	bdva.va_mask = AT_CTIME|AT_SEQ;
2620 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
2621 	if (error) {
2622 		*cs->statusp = resp->status = puterrno4(error);
2623 		kmem_free(nm, len);
2624 		goto out;
2625 	}
2626 
2627 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2628 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
2629 	    MAXPATHLEN  + 1);
2630 
2631 	if (name == NULL) {
2632 		*cs->statusp = resp->status = NFS4ERR_INVAL;
2633 		kmem_free(nm, len);
2634 		goto out;
2635 	}
2636 
2637 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
2638 
2639 	error = VOP_LINK(dvp, vp, name, cs->cr, NULL, 0);
2640 
2641 	if (nm != name)
2642 		kmem_free(name, MAXPATHLEN + 1);
2643 	kmem_free(nm, len);
2644 
2645 	/*
2646 	 * Get the initial "after" sequence number, if it fails, set to zero
2647 	 */
2648 	idva.va_mask = AT_SEQ;
2649 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
2650 		idva.va_seq = 0;
2651 
2652 	/*
2653 	 * Force modified data and metadata out to stable storage.
2654 	 */
2655 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
2656 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
2657 
2658 	if (error) {
2659 		*cs->statusp = resp->status = puterrno4(error);
2660 		goto out;
2661 	}
2662 
2663 	/*
2664 	 * Get "after" change value, if it fails, simply return the
2665 	 * before value.
2666 	 */
2667 	adva.va_mask = AT_CTIME|AT_SEQ;
2668 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
2669 		adva.va_ctime = bdva.va_ctime;
2670 		adva.va_seq = 0;
2671 	}
2672 
2673 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
2674 
2675 	/*
2676 	 * The cinfo.atomic = TRUE only if we have
2677 	 * non-zero va_seq's, and it has incremented by exactly one
2678 	 * during the VOP_LINK and it didn't change during the VOP_FSYNC.
2679 	 */
2680 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
2681 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
2682 		resp->cinfo.atomic = TRUE;
2683 	else
2684 		resp->cinfo.atomic = FALSE;
2685 
2686 	*cs->statusp = resp->status = NFS4_OK;
2687 out:
2688 	DTRACE_NFSV4_2(op__link__done, struct compound_state *, cs,
2689 	    LINK4res *, resp);
2690 }
2691 
2692 /*
2693  * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work.
2694  */
2695 
2696 /* ARGSUSED */
2697 static nfsstat4
2698 do_rfs4_op_lookup(char *nm, struct svc_req *req, struct compound_state *cs)
2699 {
2700 	int error;
2701 	int different_export = 0;
2702 	vnode_t *vp, *pre_tvp = NULL, *oldvp = NULL;
2703 	struct exportinfo *exi = NULL, *pre_exi = NULL;
2704 	nfsstat4 stat;
2705 	fid_t fid;
2706 	int attrdir, dotdot, walk;
2707 	bool_t is_newvp = FALSE;
2708 
2709 	if (cs->vp->v_flag & V_XATTRDIR) {
2710 		attrdir = 1;
2711 		ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2712 	} else {
2713 		attrdir = 0;
2714 		ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2715 	}
2716 
2717 	dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
2718 
2719 	/*
2720 	 * If dotdotting, then need to check whether it's
2721 	 * above the root of a filesystem, or above an
2722 	 * export point.
2723 	 */
2724 	if (dotdot) {
2725 		vnode_t *zone_rootvp;
2726 
2727 		ASSERT(cs->exi != NULL);
2728 		zone_rootvp = cs->exi->exi_ne->exi_root->exi_vp;
2729 		/*
2730 		 * If dotdotting at the root of a filesystem, then
2731 		 * need to traverse back to the mounted-on filesystem
2732 		 * and do the dotdot lookup there.
2733 		 */
2734 		if ((cs->vp->v_flag & VROOT) || VN_CMP(cs->vp, zone_rootvp)) {
2735 
2736 			/*
2737 			 * If at the system root, then can
2738 			 * go up no further.
2739 			 */
2740 			if (VN_CMP(cs->vp, zone_rootvp))
2741 				return (puterrno4(ENOENT));
2742 
2743 			/*
2744 			 * Traverse back to the mounted-on filesystem
2745 			 */
2746 			cs->vp = untraverse(cs->vp, zone_rootvp);
2747 
2748 			/*
2749 			 * Set the different_export flag so we remember
2750 			 * to pick up a new exportinfo entry for
2751 			 * this new filesystem.
2752 			 */
2753 			different_export = 1;
2754 		} else {
2755 
2756 			/*
2757 			 * If dotdotting above an export point then set
2758 			 * the different_export to get new export info.
2759 			 */
2760 			different_export = nfs_exported(cs->exi, cs->vp);
2761 		}
2762 	}
2763 
2764 	error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr,
2765 	    NULL, NULL, NULL);
2766 	if (error)
2767 		return (puterrno4(error));
2768 
2769 	/*
2770 	 * If the vnode is in a pseudo filesystem, check whether it is visible.
2771 	 *
2772 	 * XXX if the vnode is a symlink and it is not visible in
2773 	 * a pseudo filesystem, return ENOENT (not following symlink).
2774 	 * V4 client can not mount such symlink. This is a regression
2775 	 * from V2/V3.
2776 	 *
2777 	 * In the same exported filesystem, if the security flavor used
2778 	 * is not an explicitly shared flavor, limit the view to the visible
2779 	 * list entries only. This is not a WRONGSEC case because it's already
2780 	 * checked via PUTROOTFH/PUTPUBFH or PUTFH.
2781 	 */
2782 	if (!different_export &&
2783 	    (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
2784 	    cs->access & CS_ACCESS_LIMITED)) {
2785 		if (! nfs_visible(cs->exi, vp, &different_export)) {
2786 			VN_RELE(vp);
2787 			return (puterrno4(ENOENT));
2788 		}
2789 	}
2790 
2791 	/*
2792 	 * If it's a mountpoint, then traverse it.
2793 	 */
2794 	if (vn_ismntpt(vp)) {
2795 		pre_exi = cs->exi;	/* save pre-traversed exportinfo */
2796 		pre_tvp = vp;		/* save pre-traversed vnode	*/
2797 
2798 		/*
2799 		 * hold pre_tvp to counteract rele by traverse.  We will
2800 		 * need pre_tvp below if checkexport4 fails
2801 		 */
2802 		VN_HOLD(pre_tvp);
2803 		if ((error = traverse(&vp)) != 0) {
2804 			VN_RELE(vp);
2805 			VN_RELE(pre_tvp);
2806 			return (puterrno4(error));
2807 		}
2808 		different_export = 1;
2809 	} else if (vp->v_vfsp != cs->vp->v_vfsp) {
2810 		/*
2811 		 * The vfsp comparison is to handle the case where
2812 		 * a LOFS mount is shared.  lo_lookup traverses mount points,
2813 		 * and NFS is unaware of local fs transistions because
2814 		 * v_vfsmountedhere isn't set.  For this special LOFS case,
2815 		 * the dir and the obj returned by lookup will have different
2816 		 * vfs ptrs.
2817 		 */
2818 		different_export = 1;
2819 	}
2820 
2821 	if (different_export) {
2822 
2823 		bzero(&fid, sizeof (fid));
2824 		fid.fid_len = MAXFIDSZ;
2825 		error = vop_fid_pseudo(vp, &fid);
2826 		if (error) {
2827 			VN_RELE(vp);
2828 			if (pre_tvp)
2829 				VN_RELE(pre_tvp);
2830 			return (puterrno4(error));
2831 		}
2832 
2833 		if (dotdot)
2834 			exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
2835 		else
2836 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
2837 
2838 		if (exi == NULL) {
2839 			if (pre_tvp) {
2840 				/*
2841 				 * If this vnode is a mounted-on vnode,
2842 				 * but the mounted-on file system is not
2843 				 * exported, send back the filehandle for
2844 				 * the mounted-on vnode, not the root of
2845 				 * the mounted-on file system.
2846 				 */
2847 				VN_RELE(vp);
2848 				vp = pre_tvp;
2849 				exi = pre_exi;
2850 			} else {
2851 				VN_RELE(vp);
2852 				return (puterrno4(EACCES));
2853 			}
2854 		} else if (pre_tvp) {
2855 			/* we're done with pre_tvp now. release extra hold */
2856 			VN_RELE(pre_tvp);
2857 		}
2858 
2859 		cs->exi = exi;
2860 
2861 		/*
2862 		 * Now we do a checkauth4. The reason is that
2863 		 * this client/user may not have access to the new
2864 		 * exported file system, and if they do,
2865 		 * the client/user may be mapped to a different uid.
2866 		 *
2867 		 * We start with a new cr, because the checkauth4 done
2868 		 * in the PUT*FH operation over wrote the cred's uid,
2869 		 * gid, etc, and we want the real thing before calling
2870 		 * checkauth4()
2871 		 */
2872 		crfree(cs->cr);
2873 		cs->cr = crdup(cs->basecr);
2874 
2875 		oldvp = cs->vp;
2876 		cs->vp = vp;
2877 		is_newvp = TRUE;
2878 
2879 		stat = call_checkauth4(cs, req);
2880 		if (stat != NFS4_OK) {
2881 			VN_RELE(cs->vp);
2882 			cs->vp = oldvp;
2883 			return (stat);
2884 		}
2885 	}
2886 
2887 	/*
2888 	 * After various NFS checks, do a label check on the path
2889 	 * component. The label on this path should either be the
2890 	 * global zone's label or a zone's label. We are only
2891 	 * interested in the zone's label because exported files
2892 	 * in global zone is accessible (though read-only) to
2893 	 * clients. The exportability/visibility check is already
2894 	 * done before reaching this code.
2895 	 */
2896 	if (is_system_labeled()) {
2897 		bslabel_t *clabel;
2898 
2899 		ASSERT(req->rq_label != NULL);
2900 		clabel = req->rq_label;
2901 		DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *,
2902 		    "got client label from request(1)", struct svc_req *, req);
2903 
2904 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2905 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
2906 			    cs->exi)) {
2907 				error = EACCES;
2908 				goto err_out;
2909 			}
2910 		} else {
2911 			/*
2912 			 * We grant access to admin_low label clients
2913 			 * only if the client is trusted, i.e. also
2914 			 * running Solaris Trusted Extension.
2915 			 */
2916 			struct sockaddr	*ca;
2917 			int		addr_type;
2918 			void		*ipaddr;
2919 			tsol_tpc_t	*tp;
2920 
2921 			ca = (struct sockaddr *)svc_getrpccaller(
2922 			    req->rq_xprt)->buf;
2923 			if (ca->sa_family == AF_INET) {
2924 				addr_type = IPV4_VERSION;
2925 				ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
2926 			} else if (ca->sa_family == AF_INET6) {
2927 				addr_type = IPV6_VERSION;
2928 				ipaddr = &((struct sockaddr_in6 *)
2929 				    ca)->sin6_addr;
2930 			}
2931 			tp = find_tpc(ipaddr, addr_type, B_FALSE);
2932 			if (tp == NULL || tp->tpc_tp.tp_doi !=
2933 			    l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
2934 			    SUN_CIPSO) {
2935 				if (tp != NULL)
2936 					TPC_RELE(tp);
2937 				error = EACCES;
2938 				goto err_out;
2939 			}
2940 			TPC_RELE(tp);
2941 		}
2942 	}
2943 
2944 	error = makefh4(&cs->fh, vp, cs->exi);
2945 
2946 err_out:
2947 	if (error) {
2948 		if (is_newvp) {
2949 			VN_RELE(cs->vp);
2950 			cs->vp = oldvp;
2951 		} else
2952 			VN_RELE(vp);
2953 		return (puterrno4(error));
2954 	}
2955 
2956 	if (!is_newvp) {
2957 		if (cs->vp)
2958 			VN_RELE(cs->vp);
2959 		cs->vp = vp;
2960 	} else if (oldvp)
2961 		VN_RELE(oldvp);
2962 
2963 	/*
2964 	 * if did lookup on attrdir and didn't lookup .., set named
2965 	 * attr fh flag
2966 	 */
2967 	if (attrdir && ! dotdot)
2968 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
2969 
2970 	/* Assume false for now, open proc will set this */
2971 	cs->mandlock = FALSE;
2972 
2973 	return (NFS4_OK);
2974 }
2975 
2976 /* ARGSUSED */
2977 static void
2978 rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2979     struct compound_state *cs)
2980 {
2981 	LOOKUP4args *args = &argop->nfs_argop4_u.oplookup;
2982 	LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup;
2983 	char *nm;
2984 	uint_t len;
2985 	struct sockaddr *ca;
2986 	char *name = NULL;
2987 	nfsstat4 status;
2988 
2989 	DTRACE_NFSV4_2(op__lookup__start, struct compound_state *, cs,
2990 	    LOOKUP4args *, args);
2991 
2992 	if (cs->vp == NULL) {
2993 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2994 		goto out;
2995 	}
2996 
2997 	if (cs->vp->v_type == VLNK) {
2998 		*cs->statusp = resp->status = NFS4ERR_SYMLINK;
2999 		goto out;
3000 	}
3001 
3002 	if (cs->vp->v_type != VDIR) {
3003 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
3004 		goto out;
3005 	}
3006 
3007 	status = utf8_dir_verify(&args->objname);
3008 	if (status != NFS4_OK) {
3009 		*cs->statusp = resp->status = status;
3010 		goto out;
3011 	}
3012 
3013 	nm = utf8_to_str(&args->objname, &len, NULL);
3014 	if (nm == NULL) {
3015 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3016 		goto out;
3017 	}
3018 
3019 	if (len > MAXNAMELEN) {
3020 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
3021 		kmem_free(nm, len);
3022 		goto out;
3023 	}
3024 
3025 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
3026 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
3027 	    MAXPATHLEN  + 1);
3028 
3029 	if (name == NULL) {
3030 		*cs->statusp = resp->status = NFS4ERR_INVAL;
3031 		kmem_free(nm, len);
3032 		goto out;
3033 	}
3034 
3035 	*cs->statusp = resp->status = do_rfs4_op_lookup(name, req, cs);
3036 
3037 	if (name != nm)
3038 		kmem_free(name, MAXPATHLEN + 1);
3039 	kmem_free(nm, len);
3040 
3041 out:
3042 	DTRACE_NFSV4_2(op__lookup__done, struct compound_state *, cs,
3043 	    LOOKUP4res *, resp);
3044 }
3045 
3046 /* ARGSUSED */
3047 static void
3048 rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3049     struct compound_state *cs)
3050 {
3051 	LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp;
3052 
3053 	DTRACE_NFSV4_1(op__lookupp__start, struct compound_state *, cs);
3054 
3055 	if (cs->vp == NULL) {
3056 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3057 		goto out;
3058 	}
3059 
3060 	if (cs->vp->v_type != VDIR) {
3061 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
3062 		goto out;
3063 	}
3064 
3065 	*cs->statusp = resp->status = do_rfs4_op_lookup("..", req, cs);
3066 
3067 	/*
3068 	 * From NFSV4 Specification, LOOKUPP should not check for
3069 	 * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead.
3070 	 */
3071 	if (resp->status == NFS4ERR_WRONGSEC) {
3072 		*cs->statusp = resp->status = NFS4_OK;
3073 	}
3074 
3075 out:
3076 	DTRACE_NFSV4_2(op__lookupp__done, struct compound_state *, cs,
3077 	    LOOKUPP4res *, resp);
3078 }
3079 
3080 
3081 /*ARGSUSED2*/
3082 static void
3083 rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3084     struct compound_state *cs)
3085 {
3086 	OPENATTR4args	*args = &argop->nfs_argop4_u.opopenattr;
3087 	OPENATTR4res	*resp = &resop->nfs_resop4_u.opopenattr;
3088 	vnode_t		*avp = NULL;
3089 	int		lookup_flags = LOOKUP_XATTR, error;
3090 	int		exp_ro = 0;
3091 
3092 	DTRACE_NFSV4_2(op__openattr__start, struct compound_state *, cs,
3093 	    OPENATTR4args *, args);
3094 
3095 	if (cs->vp == NULL) {
3096 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3097 		goto out;
3098 	}
3099 
3100 	if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 &&
3101 	    !vfs_has_feature(cs->vp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
3102 		*cs->statusp = resp->status = puterrno4(ENOTSUP);
3103 		goto out;
3104 	}
3105 
3106 	/*
3107 	 * If file system supports passing ACE mask to VOP_ACCESS then
3108 	 * check for ACE_READ_NAMED_ATTRS, otherwise do legacy checks
3109 	 */
3110 
3111 	if (vfs_has_feature(cs->vp->v_vfsp, VFSFT_ACEMASKONACCESS))
3112 		error = VOP_ACCESS(cs->vp, ACE_READ_NAMED_ATTRS,
3113 		    V_ACE_MASK, cs->cr, NULL);
3114 	else
3115 		error = ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) &&
3116 		    (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) &&
3117 		    (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0));
3118 
3119 	if (error) {
3120 		*cs->statusp = resp->status = puterrno4(EACCES);
3121 		goto out;
3122 	}
3123 
3124 	/*
3125 	 * The CREATE_XATTR_DIR VOP flag cannot be specified if
3126 	 * the file system is exported read-only -- regardless of
3127 	 * createdir flag.  Otherwise the attrdir would be created
3128 	 * (assuming server fs isn't mounted readonly locally).  If
3129 	 * VOP_LOOKUP returns ENOENT in this case, the error will
3130 	 * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
3131 	 * because specfs has no VOP_LOOKUP op, so the macro would
3132 	 * return ENOSYS.  EINVAL is returned by all (current)
3133 	 * Solaris file system implementations when any of their
3134 	 * restrictions are violated (xattr(dir) can't have xattrdir).
3135 	 * Returning NOTSUPP is more appropriate in this case
3136 	 * because the object will never be able to have an attrdir.
3137 	 */
3138 	if (args->createdir && ! (exp_ro = rdonly4(req, cs)))
3139 		lookup_flags |= CREATE_XATTR_DIR;
3140 
3141 	error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr,
3142 	    NULL, NULL, NULL);
3143 
3144 	if (error) {
3145 		if (error == ENOENT && args->createdir && exp_ro)
3146 			*cs->statusp = resp->status = puterrno4(EROFS);
3147 		else if (error == EINVAL || error == ENOSYS)
3148 			*cs->statusp = resp->status = puterrno4(ENOTSUP);
3149 		else
3150 			*cs->statusp = resp->status = puterrno4(error);
3151 		goto out;
3152 	}
3153 
3154 	ASSERT(avp->v_flag & V_XATTRDIR);
3155 
3156 	error = makefh4(&cs->fh, avp, cs->exi);
3157 
3158 	if (error) {
3159 		VN_RELE(avp);
3160 		*cs->statusp = resp->status = puterrno4(error);
3161 		goto out;
3162 	}
3163 
3164 	VN_RELE(cs->vp);
3165 	cs->vp = avp;
3166 
3167 	/*
3168 	 * There is no requirement for an attrdir fh flag
3169 	 * because the attrdir has a vnode flag to distinguish
3170 	 * it from regular (non-xattr) directories.  The
3171 	 * FH4_ATTRDIR flag is set for future sanity checks.
3172 	 */
3173 	set_fh4_flag(&cs->fh, FH4_ATTRDIR);
3174 	*cs->statusp = resp->status = NFS4_OK;
3175 
3176 out:
3177 	DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs,
3178 	    OPENATTR4res *, resp);
3179 }
3180 
3181 static int
3182 do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred,
3183     caller_context_t *ct)
3184 {
3185 	int error;
3186 	int i;
3187 	clock_t delaytime;
3188 
3189 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
3190 
3191 	/*
3192 	 * Don't block on mandatory locks. If this routine returns
3193 	 * EAGAIN, the caller should return NFS4ERR_LOCKED.
3194 	 */
3195 	uio->uio_fmode = FNONBLOCK;
3196 
3197 	for (i = 0; i < rfs4_maxlock_tries; i++) {
3198 
3199 
3200 		if (direction == FREAD) {
3201 			(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct);
3202 			error = VOP_READ(vp, uio, ioflag, cred, ct);
3203 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct);
3204 		} else {
3205 			(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct);
3206 			error = VOP_WRITE(vp, uio, ioflag, cred, ct);
3207 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct);
3208 		}
3209 
3210 		if (error != EAGAIN)
3211 			break;
3212 
3213 		if (i < rfs4_maxlock_tries - 1) {
3214 			delay(delaytime);
3215 			delaytime *= 2;
3216 		}
3217 	}
3218 
3219 	return (error);
3220 }
3221 
3222 /* ARGSUSED */
3223 static void
3224 rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3225     struct compound_state *cs)
3226 {
3227 	READ4args *args = &argop->nfs_argop4_u.opread;
3228 	READ4res *resp = &resop->nfs_resop4_u.opread;
3229 	int error;
3230 	int verror;
3231 	vnode_t *vp;
3232 	struct vattr va;
3233 	struct iovec iov, *iovp = NULL;
3234 	int iovcnt;
3235 	struct uio uio;
3236 	u_offset_t offset;
3237 	bool_t *deleg = &cs->deleg;
3238 	nfsstat4 stat;
3239 	int in_crit = 0;
3240 	mblk_t *mp = NULL;
3241 	int alloc_err = 0;
3242 	int rdma_used = 0;
3243 	int loaned_buffers;
3244 	caller_context_t ct;
3245 	struct uio *uiop;
3246 
3247 	DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs,
3248 	    READ4args, args);
3249 
3250 	vp = cs->vp;
3251 	if (vp == NULL) {
3252 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3253 		goto out;
3254 	}
3255 	if (cs->access == CS_ACCESS_DENIED) {
3256 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3257 		goto out;
3258 	}
3259 
3260 	if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3261 	    deleg, TRUE, &ct)) != NFS4_OK) {
3262 		*cs->statusp = resp->status = stat;
3263 		goto out;
3264 	}
3265 
3266 	/*
3267 	 * Enter the critical region before calling VOP_RWLOCK
3268 	 * to avoid a deadlock with write requests.
3269 	 */
3270 	if (nbl_need_check(vp)) {
3271 		nbl_start_crit(vp, RW_READER);
3272 		in_crit = 1;
3273 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
3274 		    &ct)) {
3275 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
3276 			goto out;
3277 		}
3278 	}
3279 
3280 	if (args->wlist) {
3281 		if (args->count > clist_len(args->wlist)) {
3282 			*cs->statusp = resp->status = NFS4ERR_INVAL;
3283 			goto out;
3284 		}
3285 		rdma_used = 1;
3286 	}
3287 
3288 	/* use loaned buffers for TCP */
3289 	loaned_buffers = (nfs_loaned_buffers && !rdma_used) ? 1 : 0;
3290 
3291 	va.va_mask = AT_MODE|AT_SIZE|AT_UID;
3292 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3293 
3294 	/*
3295 	 * If we can't get the attributes, then we can't do the
3296 	 * right access checking.  So, we'll fail the request.
3297 	 */
3298 	if (verror) {
3299 		*cs->statusp = resp->status = puterrno4(verror);
3300 		goto out;
3301 	}
3302 
3303 	if (vp->v_type != VREG) {
3304 		*cs->statusp = resp->status =
3305 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
3306 		goto out;
3307 	}
3308 
3309 	if (crgetuid(cs->cr) != va.va_uid &&
3310 	    (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) &&
3311 	    (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) {
3312 		*cs->statusp = resp->status = puterrno4(error);
3313 		goto out;
3314 	}
3315 
3316 	if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
3317 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3318 		goto out;
3319 	}
3320 
3321 	offset = args->offset;
3322 	if (offset >= va.va_size) {
3323 		*cs->statusp = resp->status = NFS4_OK;
3324 		resp->eof = TRUE;
3325 		resp->data_len = 0;
3326 		resp->data_val = NULL;
3327 		resp->mblk = NULL;
3328 		/* RDMA */
3329 		resp->wlist = args->wlist;
3330 		resp->wlist_len = resp->data_len;
3331 		*cs->statusp = resp->status = NFS4_OK;
3332 		if (resp->wlist)
3333 			clist_zero_len(resp->wlist);
3334 		goto out;
3335 	}
3336 
3337 	if (args->count == 0) {
3338 		*cs->statusp = resp->status = NFS4_OK;
3339 		resp->eof = FALSE;
3340 		resp->data_len = 0;
3341 		resp->data_val = NULL;
3342 		resp->mblk = NULL;
3343 		/* RDMA */
3344 		resp->wlist = args->wlist;
3345 		resp->wlist_len = resp->data_len;
3346 		if (resp->wlist)
3347 			clist_zero_len(resp->wlist);
3348 		goto out;
3349 	}
3350 
3351 	/*
3352 	 * Do not allocate memory more than maximum allowed
3353 	 * transfer size
3354 	 */
3355 	if (args->count > rfs4_tsize(req))
3356 		args->count = rfs4_tsize(req);
3357 
3358 	if (loaned_buffers) {
3359 		uiop = (uio_t *)rfs_setup_xuio(vp);
3360 		ASSERT(uiop != NULL);
3361 		uiop->uio_segflg = UIO_SYSSPACE;
3362 		uiop->uio_loffset = args->offset;
3363 		uiop->uio_resid = args->count;
3364 
3365 		/* Jump to do the read if successful */
3366 		if (!VOP_REQZCBUF(vp, UIO_READ, (xuio_t *)uiop, cs->cr, &ct)) {
3367 			/*
3368 			 * Need to hold the vnode until after VOP_RETZCBUF()
3369 			 * is called.
3370 			 */
3371 			VN_HOLD(vp);
3372 			goto doio_read;
3373 		}
3374 
3375 		DTRACE_PROBE2(nfss__i__reqzcbuf_failed, int,
3376 		    uiop->uio_loffset, int, uiop->uio_resid);
3377 
3378 		uiop->uio_extflg = 0;
3379 
3380 		/* failure to setup for zero copy */
3381 		rfs_free_xuio((void *)uiop);
3382 		loaned_buffers = 0;
3383 	}
3384 
3385 	/*
3386 	 * If returning data via RDMA Write, then grab the chunk list. If we
3387 	 * aren't returning READ data w/RDMA_WRITE, then grab a mblk.
3388 	 */
3389 	if (rdma_used) {
3390 		mp = NULL;
3391 		(void) rdma_get_wchunk(req, &iov, args->wlist);
3392 		uio.uio_iov = &iov;
3393 		uio.uio_iovcnt = 1;
3394 	} else {
3395 		/*
3396 		 * mp will contain the data to be sent out in the read reply.
3397 		 * It will be freed after the reply has been sent.
3398 		 */
3399 		mp = rfs_read_alloc(args->count, &iovp, &iovcnt);
3400 		ASSERT(mp != NULL);
3401 		ASSERT(alloc_err == 0);
3402 		uio.uio_iov = iovp;
3403 		uio.uio_iovcnt = iovcnt;
3404 	}
3405 
3406 	uio.uio_segflg = UIO_SYSSPACE;
3407 	uio.uio_extflg = UIO_COPY_CACHED;
3408 	uio.uio_loffset = args->offset;
3409 	uio.uio_resid = args->count;
3410 	uiop = &uio;
3411 
3412 doio_read:
3413 	error = do_io(FREAD, vp, uiop, 0, cs->cr, &ct);
3414 
3415 	va.va_mask = AT_SIZE;
3416 	verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3417 
3418 	if (error) {
3419 		if (mp)
3420 			freemsg(mp);
3421 		*cs->statusp = resp->status = puterrno4(error);
3422 		goto out;
3423 	}
3424 
3425 	/* make mblk using zc buffers */
3426 	if (loaned_buffers) {
3427 		mp = uio_to_mblk(uiop);
3428 		ASSERT(mp != NULL);
3429 	}
3430 
3431 	*cs->statusp = resp->status = NFS4_OK;
3432 
3433 	ASSERT(uiop->uio_resid >= 0);
3434 	resp->data_len = args->count - uiop->uio_resid;
3435 	if (mp) {
3436 		resp->data_val = (char *)mp->b_datap->db_base;
3437 		rfs_rndup_mblks(mp, resp->data_len, loaned_buffers);
3438 	} else {
3439 		resp->data_val = (caddr_t)iov.iov_base;
3440 	}
3441 
3442 	resp->mblk = mp;
3443 
3444 	if (!verror && offset + resp->data_len == va.va_size)
3445 		resp->eof = TRUE;
3446 	else
3447 		resp->eof = FALSE;
3448 
3449 	if (rdma_used) {
3450 		if (!rdma_setup_read_data4(args, resp)) {
3451 			*cs->statusp = resp->status = NFS4ERR_INVAL;
3452 		}
3453 	} else {
3454 		resp->wlist = NULL;
3455 	}
3456 
3457 out:
3458 	if (in_crit)
3459 		nbl_end_crit(vp);
3460 
3461 	if (iovp != NULL)
3462 		kmem_free(iovp, iovcnt * sizeof (struct iovec));
3463 
3464 	DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs,
3465 	    READ4res *, resp);
3466 }
3467 
3468 static void
3469 rfs4_op_read_free(nfs_resop4 *resop)
3470 {
3471 	READ4res	*resp = &resop->nfs_resop4_u.opread;
3472 
3473 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3474 		freemsg(resp->mblk);
3475 		resp->mblk = NULL;
3476 		resp->data_val = NULL;
3477 		resp->data_len = 0;
3478 	}
3479 }
3480 
3481 static void
3482 rfs4_op_readdir_free(nfs_resop4 * resop)
3483 {
3484 	READDIR4res    *resp = &resop->nfs_resop4_u.opreaddir;
3485 
3486 	if (resp->status == NFS4_OK && resp->mblk != NULL) {
3487 		freeb(resp->mblk);
3488 		resp->mblk = NULL;
3489 		resp->data_len = 0;
3490 	}
3491 }
3492 
3493 
3494 /* ARGSUSED */
3495 static void
3496 rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3497     struct compound_state *cs)
3498 {
3499 	PUTPUBFH4res	*resp = &resop->nfs_resop4_u.opputpubfh;
3500 	int		error;
3501 	vnode_t		*vp;
3502 	struct exportinfo *exi, *sav_exi;
3503 	nfs_fh4_fmt_t	*fh_fmtp;
3504 	nfs_export_t *ne = nfs_get_export();
3505 
3506 	DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs);
3507 
3508 	if (cs->vp) {
3509 		VN_RELE(cs->vp);
3510 		cs->vp = NULL;
3511 	}
3512 
3513 	if (cs->cr)
3514 		crfree(cs->cr);
3515 
3516 	cs->cr = crdup(cs->basecr);
3517 
3518 	vp = ne->exi_public->exi_vp;
3519 	if (vp == NULL) {
3520 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3521 		goto out;
3522 	}
3523 
3524 	error = makefh4(&cs->fh, vp, ne->exi_public);
3525 	if (error != 0) {
3526 		*cs->statusp = resp->status = puterrno4(error);
3527 		goto out;
3528 	}
3529 	sav_exi = cs->exi;
3530 	if (ne->exi_public == ne->exi_root) {
3531 		/*
3532 		 * No filesystem is actually shared public, so we default
3533 		 * to exi_root. In this case, we must check whether root
3534 		 * is exported.
3535 		 */
3536 		fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
3537 
3538 		/*
3539 		 * if root filesystem is exported, the exportinfo struct that we
3540 		 * should use is what checkexport4 returns, because root_exi is
3541 		 * actually a mostly empty struct.
3542 		 */
3543 		exi = checkexport4(&fh_fmtp->fh4_fsid,
3544 		    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
3545 		cs->exi = ((exi != NULL) ? exi : ne->exi_public);
3546 	} else {
3547 		/*
3548 		 * it's a properly shared filesystem
3549 		 */
3550 		cs->exi = ne->exi_public;
3551 	}
3552 
3553 	if (is_system_labeled()) {
3554 		bslabel_t *clabel;
3555 
3556 		ASSERT(req->rq_label != NULL);
3557 		clabel = req->rq_label;
3558 		DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *,
3559 		    "got client label from request(1)",
3560 		    struct svc_req *, req);
3561 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
3562 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
3563 			    cs->exi)) {
3564 				*cs->statusp = resp->status =
3565 				    NFS4ERR_SERVERFAULT;
3566 				goto out;
3567 			}
3568 		}
3569 	}
3570 
3571 	VN_HOLD(vp);
3572 	cs->vp = vp;
3573 
3574 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3575 		VN_RELE(cs->vp);
3576 		cs->vp = NULL;
3577 		cs->exi = sav_exi;
3578 		goto out;
3579 	}
3580 
3581 	*cs->statusp = resp->status = NFS4_OK;
3582 out:
3583 	DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs,
3584 	    PUTPUBFH4res *, resp);
3585 }
3586 
3587 /*
3588  * XXX - issue with put*fh operations. Suppose /export/home is exported.
3589  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
3590  * or joe have restrictive search permissions, then we shouldn't let
3591  * the client get a file handle. This is easy to enforce. However, we
3592  * don't know what security flavor should be used until we resolve the
3593  * path name. Another complication is uid mapping. If root is
3594  * the user, then it will be mapped to the anonymous user by default,
3595  * but we won't know that till we've resolved the path name. And we won't
3596  * know what the anonymous user is.
3597  * Luckily, SECINFO is specified to take a full filename.
3598  * So what we will have to in rfs4_op_lookup is check that flavor of
3599  * the target object matches that of the request, and if root was the
3600  * caller, check for the root= and anon= options, and if necessary,
3601  * repeat the lookup using the right cred_t. But that's not done yet.
3602  */
3603 /* ARGSUSED */
3604 static void
3605 rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3606     struct compound_state *cs)
3607 {
3608 	PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
3609 	PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
3610 	nfs_fh4_fmt_t *fh_fmtp;
3611 
3612 	DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs,
3613 	    PUTFH4args *, args);
3614 
3615 	if (cs->vp) {
3616 		VN_RELE(cs->vp);
3617 		cs->vp = NULL;
3618 	}
3619 
3620 	if (cs->cr) {
3621 		crfree(cs->cr);
3622 		cs->cr = NULL;
3623 	}
3624 
3625 
3626 	if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
3627 		*cs->statusp = resp->status = NFS4ERR_BADHANDLE;
3628 		goto out;
3629 	}
3630 
3631 	fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
3632 	cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
3633 	    NULL);
3634 
3635 	if (cs->exi == NULL) {
3636 		*cs->statusp = resp->status = NFS4ERR_STALE;
3637 		goto out;
3638 	}
3639 
3640 	cs->cr = crdup(cs->basecr);
3641 
3642 	ASSERT(cs->cr != NULL);
3643 
3644 	if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
3645 		*cs->statusp = resp->status;
3646 		goto out;
3647 	}
3648 
3649 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3650 		VN_RELE(cs->vp);
3651 		cs->vp = NULL;
3652 		goto out;
3653 	}
3654 
3655 	nfs_fh4_copy(&args->object, &cs->fh);
3656 	*cs->statusp = resp->status = NFS4_OK;
3657 	cs->deleg = FALSE;
3658 
3659 out:
3660 	DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs,
3661 	    PUTFH4res *, resp);
3662 }
3663 
3664 /* ARGSUSED */
3665 static void
3666 rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3667     struct compound_state *cs)
3668 {
3669 	PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
3670 	int error;
3671 	fid_t fid;
3672 	struct exportinfo *exi, *sav_exi;
3673 
3674 	DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs);
3675 
3676 	if (cs->vp) {
3677 		VN_RELE(cs->vp);
3678 		cs->vp = NULL;
3679 	}
3680 
3681 	if (cs->cr)
3682 		crfree(cs->cr);
3683 
3684 	cs->cr = crdup(cs->basecr);
3685 
3686 	/*
3687 	 * Using rootdir, the system root vnode,
3688 	 * get its fid.
3689 	 */
3690 	bzero(&fid, sizeof (fid));
3691 	fid.fid_len = MAXFIDSZ;
3692 	error = vop_fid_pseudo(ZONE_ROOTVP(), &fid);
3693 	if (error != 0) {
3694 		*cs->statusp = resp->status = puterrno4(error);
3695 		goto out;
3696 	}
3697 
3698 	/*
3699 	 * Then use the root fsid & fid it to find out if it's exported
3700 	 *
3701 	 * If the server root isn't exported directly, then
3702 	 * it should at least be a pseudo export based on
3703 	 * one or more exports further down in the server's
3704 	 * file tree.
3705 	 */
3706 	exi = checkexport4(&ZONE_ROOTVP()->v_vfsp->vfs_fsid, &fid, NULL);
3707 	if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
3708 		NFS4_DEBUG(rfs4_debug,
3709 		    (CE_WARN, "rfs4_op_putrootfh: export check failure"));
3710 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3711 		goto out;
3712 	}
3713 
3714 	/*
3715 	 * Now make a filehandle based on the root
3716 	 * export and root vnode.
3717 	 */
3718 	error = makefh4(&cs->fh, ZONE_ROOTVP(), exi);
3719 	if (error != 0) {
3720 		*cs->statusp = resp->status = puterrno4(error);
3721 		goto out;
3722 	}
3723 
3724 	sav_exi = cs->exi;
3725 	cs->exi = exi;
3726 
3727 	VN_HOLD(ZONE_ROOTVP());
3728 	cs->vp = ZONE_ROOTVP();
3729 
3730 	if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3731 		VN_RELE(cs->vp);
3732 		cs->vp = NULL;
3733 		cs->exi = sav_exi;
3734 		goto out;
3735 	}
3736 
3737 	*cs->statusp = resp->status = NFS4_OK;
3738 	cs->deleg = FALSE;
3739 out:
3740 	DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs,
3741 	    PUTROOTFH4res *, resp);
3742 }
3743 
3744 /*
3745  * readlink: args: CURRENT_FH.
3746  *	res: status. If success - CURRENT_FH unchanged, return linktext.
3747  */
3748 
3749 /* ARGSUSED */
3750 static void
3751 rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3752     struct compound_state *cs)
3753 {
3754 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3755 	int error;
3756 	vnode_t *vp;
3757 	struct iovec iov;
3758 	struct vattr va;
3759 	struct uio uio;
3760 	char *data;
3761 	struct sockaddr *ca;
3762 	char *name = NULL;
3763 	int is_referral;
3764 
3765 	DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs);
3766 
3767 	/* CURRENT_FH: directory */
3768 	vp = cs->vp;
3769 	if (vp == NULL) {
3770 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3771 		goto out;
3772 	}
3773 
3774 	if (cs->access == CS_ACCESS_DENIED) {
3775 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3776 		goto out;
3777 	}
3778 
3779 	/* Is it a referral? */
3780 	if (vn_is_nfs_reparse(vp, cs->cr) && client_is_downrev(req)) {
3781 
3782 		is_referral = 1;
3783 
3784 	} else {
3785 
3786 		is_referral = 0;
3787 
3788 		if (vp->v_type == VDIR) {
3789 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
3790 			goto out;
3791 		}
3792 
3793 		if (vp->v_type != VLNK) {
3794 			*cs->statusp = resp->status = NFS4ERR_INVAL;
3795 			goto out;
3796 		}
3797 
3798 	}
3799 
3800 	va.va_mask = AT_MODE;
3801 	error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
3802 	if (error) {
3803 		*cs->statusp = resp->status = puterrno4(error);
3804 		goto out;
3805 	}
3806 
3807 	if (MANDLOCK(vp, va.va_mode)) {
3808 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
3809 		goto out;
3810 	}
3811 
3812 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
3813 
3814 	if (is_referral) {
3815 		char *s;
3816 		size_t strsz;
3817 		kstat_named_t *stat =
3818 		    cs->exi->exi_ne->ne_globals->svstat[NFS_V4];
3819 
3820 		/* Get an artificial symlink based on a referral */
3821 		s = build_symlink(vp, cs->cr, &strsz);
3822 		stat[NFS_REFERLINKS].value.ui64++;
3823 		DTRACE_PROBE2(nfs4serv__func__referral__reflink,
3824 		    vnode_t *, vp, char *, s);
3825 		if (s == NULL)
3826 			error = EINVAL;
3827 		else {
3828 			error = 0;
3829 			(void) strlcpy(data, s, MAXPATHLEN + 1);
3830 			kmem_free(s, strsz);
3831 		}
3832 
3833 	} else {
3834 
3835 		iov.iov_base = data;
3836 		iov.iov_len = MAXPATHLEN;
3837 		uio.uio_iov = &iov;
3838 		uio.uio_iovcnt = 1;
3839 		uio.uio_segflg = UIO_SYSSPACE;
3840 		uio.uio_extflg = UIO_COPY_CACHED;
3841 		uio.uio_loffset = 0;
3842 		uio.uio_resid = MAXPATHLEN;
3843 
3844 		error = VOP_READLINK(vp, &uio, cs->cr, NULL);
3845 
3846 		if (!error)
3847 			*(data + MAXPATHLEN - uio.uio_resid) = '\0';
3848 	}
3849 
3850 	if (error) {
3851 		kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3852 		*cs->statusp = resp->status = puterrno4(error);
3853 		goto out;
3854 	}
3855 
3856 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
3857 	name = nfscmd_convname(ca, cs->exi, data, NFSCMD_CONV_OUTBOUND,
3858 	    MAXPATHLEN  + 1);
3859 
3860 	if (name == NULL) {
3861 		/*
3862 		 * Even though the conversion failed, we return
3863 		 * something. We just don't translate it.
3864 		 */
3865 		name = data;
3866 	}
3867 
3868 	/*
3869 	 * treat link name as data
3870 	 */
3871 	(void) str_to_utf8(name, (utf8string *)&resp->link);
3872 
3873 	if (name != data)
3874 		kmem_free(name, MAXPATHLEN + 1);
3875 	kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3876 	*cs->statusp = resp->status = NFS4_OK;
3877 
3878 out:
3879 	DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs,
3880 	    READLINK4res *, resp);
3881 }
3882 
3883 static void
3884 rfs4_op_readlink_free(nfs_resop4 *resop)
3885 {
3886 	READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3887 	utf8string *symlink = (utf8string *)&resp->link;
3888 
3889 	if (symlink->utf8string_val) {
3890 		UTF8STRING_FREE(*symlink)
3891 	}
3892 }
3893 
3894 /*
3895  * release_lockowner:
3896  *	Release any state associated with the supplied
3897  *	lockowner. Note if any lo_state is holding locks we will not
3898  *	rele that lo_state and thus the lockowner will not be destroyed.
3899  *	A client using lock after the lock owner stateid has been released
3900  *	will suffer the consequence of NFS4ERR_BAD_STATEID and would have
3901  *	to reissue the lock with new_lock_owner set to TRUE.
3902  *	args: lock_owner
3903  *	res:  status
3904  */
3905 /* ARGSUSED */
3906 static void
3907 rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
3908     struct svc_req *req, struct compound_state *cs)
3909 {
3910 	RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
3911 	RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
3912 	rfs4_lockowner_t *lo;
3913 	rfs4_openowner_t *oo;
3914 	rfs4_state_t *sp;
3915 	rfs4_lo_state_t *lsp;
3916 	rfs4_client_t *cp;
3917 	bool_t create = FALSE;
3918 	locklist_t *llist;
3919 	sysid_t sysid;
3920 
3921 	DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *,
3922 	    cs, RELEASE_LOCKOWNER4args *, ap);
3923 
3924 	/* Make sure there is a clientid around for this request */
3925 	cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
3926 
3927 	if (cp == NULL) {
3928 		*cs->statusp = resp->status =
3929 		    rfs4_check_clientid(&ap->lock_owner.clientid, 0);
3930 		goto out;
3931 	}
3932 	rfs4_client_rele(cp);
3933 
3934 	lo = rfs4_findlockowner(&ap->lock_owner, &create);
3935 	if (lo == NULL) {
3936 		*cs->statusp = resp->status = NFS4_OK;
3937 		goto out;
3938 	}
3939 	ASSERT(lo->rl_client != NULL);
3940 
3941 	/*
3942 	 * Check for EXPIRED client. If so will reap state with in a lease
3943 	 * period or on next set_clientid_confirm step
3944 	 */
3945 	if (rfs4_lease_expired(lo->rl_client)) {
3946 		rfs4_lockowner_rele(lo);
3947 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
3948 		goto out;
3949 	}
3950 
3951 	/*
3952 	 * If no sysid has been assigned, then no locks exist; just return.
3953 	 */
3954 	rfs4_dbe_lock(lo->rl_client->rc_dbe);
3955 	if (lo->rl_client->rc_sysidt == LM_NOSYSID) {
3956 		rfs4_lockowner_rele(lo);
3957 		rfs4_dbe_unlock(lo->rl_client->rc_dbe);
3958 		goto out;
3959 	}
3960 
3961 	sysid = lo->rl_client->rc_sysidt;
3962 	rfs4_dbe_unlock(lo->rl_client->rc_dbe);
3963 
3964 	/*
3965 	 * Mark the lockowner invalid.
3966 	 */
3967 	rfs4_dbe_hide(lo->rl_dbe);
3968 
3969 	/*
3970 	 * sysid-pid pair should now not be used since the lockowner is
3971 	 * invalid. If the client were to instantiate the lockowner again
3972 	 * it would be assigned a new pid. Thus we can get the list of
3973 	 * current locks.
3974 	 */
3975 
3976 	llist = flk_get_active_locks(sysid, lo->rl_pid);
3977 	/* If we are still holding locks fail */
3978 	if (llist != NULL) {
3979 
3980 		*cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
3981 
3982 		flk_free_locklist(llist);
3983 		/*
3984 		 * We need to unhide the lockowner so the client can
3985 		 * try it again. The bad thing here is if the client
3986 		 * has a logic error that took it here in the first place
3987 		 * they probably have lost accounting of the locks that it
3988 		 * is holding. So we may have dangling state until the
3989 		 * open owner state is reaped via close. One scenario
3990 		 * that could possibly occur is that the client has
3991 		 * sent the unlock request(s) in separate threads
3992 		 * and has not waited for the replies before sending the
3993 		 * RELEASE_LOCKOWNER request. Presumably, it would expect
3994 		 * and deal appropriately with NFS4ERR_LOCKS_HELD, by
3995 		 * reissuing the request.
3996 		 */
3997 		rfs4_dbe_unhide(lo->rl_dbe);
3998 		rfs4_lockowner_rele(lo);
3999 		goto out;
4000 	}
4001 
4002 	/*
4003 	 * For the corresponding client we need to check each open
4004 	 * owner for any opens that have lockowner state associated
4005 	 * with this lockowner.
4006 	 */
4007 
4008 	rfs4_dbe_lock(lo->rl_client->rc_dbe);
4009 	for (oo = list_head(&lo->rl_client->rc_openownerlist); oo != NULL;
4010 	    oo = list_next(&lo->rl_client->rc_openownerlist, oo)) {
4011 
4012 		rfs4_dbe_lock(oo->ro_dbe);
4013 		for (sp = list_head(&oo->ro_statelist); sp != NULL;
4014 		    sp = list_next(&oo->ro_statelist, sp)) {
4015 
4016 			rfs4_dbe_lock(sp->rs_dbe);
4017 			for (lsp = list_head(&sp->rs_lostatelist);
4018 			    lsp != NULL;
4019 			    lsp = list_next(&sp->rs_lostatelist, lsp)) {
4020 				if (lsp->rls_locker == lo) {
4021 					rfs4_dbe_lock(lsp->rls_dbe);
4022 					rfs4_dbe_invalidate(lsp->rls_dbe);
4023 					rfs4_dbe_unlock(lsp->rls_dbe);
4024 				}
4025 			}
4026 			rfs4_dbe_unlock(sp->rs_dbe);
4027 		}
4028 		rfs4_dbe_unlock(oo->ro_dbe);
4029 	}
4030 	rfs4_dbe_unlock(lo->rl_client->rc_dbe);
4031 
4032 	rfs4_lockowner_rele(lo);
4033 
4034 	*cs->statusp = resp->status = NFS4_OK;
4035 
4036 out:
4037 	DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *,
4038 	    cs, RELEASE_LOCKOWNER4res *, resp);
4039 }
4040 
4041 /*
4042  * short utility function to lookup a file and recall the delegation
4043  */
4044 static rfs4_file_t *
4045 rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
4046     int *lkup_error, cred_t *cr)
4047 {
4048 	vnode_t *vp;
4049 	rfs4_file_t *fp = NULL;
4050 	bool_t fcreate = FALSE;
4051 	int error;
4052 
4053 	if (vpp)
4054 		*vpp = NULL;
4055 
4056 	if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL,
4057 	    NULL)) == 0) {
4058 		if (vp->v_type == VREG)
4059 			fp = rfs4_findfile(vp, NULL, &fcreate);
4060 		if (vpp)
4061 			*vpp = vp;
4062 		else
4063 			VN_RELE(vp);
4064 	}
4065 
4066 	if (lkup_error)
4067 		*lkup_error = error;
4068 
4069 	return (fp);
4070 }
4071 
4072 /*
4073  * remove: args: CURRENT_FH: directory; name.
4074  *	res: status. If success - CURRENT_FH unchanged, return change_info
4075  *		for directory.
4076  */
4077 /* ARGSUSED */
4078 static void
4079 rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4080     struct compound_state *cs)
4081 {
4082 	REMOVE4args *args = &argop->nfs_argop4_u.opremove;
4083 	REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
4084 	int error;
4085 	vnode_t *dvp, *vp;
4086 	struct vattr bdva, idva, adva;
4087 	char *nm;
4088 	uint_t len;
4089 	rfs4_file_t *fp;
4090 	int in_crit = 0;
4091 	bslabel_t *clabel;
4092 	struct sockaddr *ca;
4093 	char *name = NULL;
4094 	nfsstat4 status;
4095 
4096 	DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs,
4097 	    REMOVE4args *, args);
4098 
4099 	/* CURRENT_FH: directory */
4100 	dvp = cs->vp;
4101 	if (dvp == NULL) {
4102 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4103 		goto out;
4104 	}
4105 
4106 	if (cs->access == CS_ACCESS_DENIED) {
4107 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4108 		goto out;
4109 	}
4110 
4111 	/*
4112 	 * If there is an unshared filesystem mounted on this vnode,
4113 	 * Do not allow to remove anything in this directory.
4114 	 */
4115 	if (vn_ismntpt(dvp)) {
4116 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4117 		goto out;
4118 	}
4119 
4120 	if (dvp->v_type != VDIR) {
4121 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4122 		goto out;
4123 	}
4124 
4125 	status = utf8_dir_verify(&args->target);
4126 	if (status != NFS4_OK) {
4127 		*cs->statusp = resp->status = status;
4128 		goto out;
4129 	}
4130 
4131 	/*
4132 	 * Lookup the file so that we can check if it's a directory
4133 	 */
4134 	nm = utf8_to_fn(&args->target, &len, NULL);
4135 	if (nm == NULL) {
4136 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4137 		goto out;
4138 	}
4139 
4140 	if (len > MAXNAMELEN) {
4141 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4142 		kmem_free(nm, len);
4143 		goto out;
4144 	}
4145 
4146 	if (rdonly4(req, cs)) {
4147 		*cs->statusp = resp->status = NFS4ERR_ROFS;
4148 		kmem_free(nm, len);
4149 		goto out;
4150 	}
4151 
4152 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4153 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
4154 	    MAXPATHLEN  + 1);
4155 
4156 	if (name == NULL) {
4157 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4158 		kmem_free(nm, len);
4159 		goto out;
4160 	}
4161 
4162 	/*
4163 	 * Lookup the file to determine type and while we are see if
4164 	 * there is a file struct around and check for delegation.
4165 	 * We don't need to acquire va_seq before this lookup, if
4166 	 * it causes an update, cinfo.before will not match, which will
4167 	 * trigger a cache flush even if atomic is TRUE.
4168 	 */
4169 	if (fp = rfs4_lookup_and_findfile(dvp, name, &vp, &error, cs->cr)) {
4170 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4171 		    NULL)) {
4172 			VN_RELE(vp);
4173 			rfs4_file_rele(fp);
4174 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4175 			if (nm != name)
4176 				kmem_free(name, MAXPATHLEN + 1);
4177 			kmem_free(nm, len);
4178 			goto out;
4179 		}
4180 	}
4181 
4182 	/* Didn't find anything to remove */
4183 	if (vp == NULL) {
4184 		*cs->statusp = resp->status = error;
4185 		if (nm != name)
4186 			kmem_free(name, MAXPATHLEN + 1);
4187 		kmem_free(nm, len);
4188 		goto out;
4189 	}
4190 
4191 	if (nbl_need_check(vp)) {
4192 		nbl_start_crit(vp, RW_READER);
4193 		in_crit = 1;
4194 		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
4195 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4196 			if (nm != name)
4197 				kmem_free(name, MAXPATHLEN + 1);
4198 			kmem_free(nm, len);
4199 			nbl_end_crit(vp);
4200 			VN_RELE(vp);
4201 			if (fp) {
4202 				rfs4_clear_dont_grant(fp);
4203 				rfs4_file_rele(fp);
4204 			}
4205 			goto out;
4206 		}
4207 	}
4208 
4209 	/* check label before allowing removal */
4210 	if (is_system_labeled()) {
4211 		ASSERT(req->rq_label != NULL);
4212 		clabel = req->rq_label;
4213 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
4214 		    "got client label from request(1)",
4215 		    struct svc_req *, req);
4216 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4217 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
4218 			    cs->exi)) {
4219 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4220 				if (name != nm)
4221 					kmem_free(name, MAXPATHLEN + 1);
4222 				kmem_free(nm, len);
4223 				if (in_crit)
4224 					nbl_end_crit(vp);
4225 				VN_RELE(vp);
4226 				if (fp) {
4227 					rfs4_clear_dont_grant(fp);
4228 					rfs4_file_rele(fp);
4229 				}
4230 				goto out;
4231 			}
4232 		}
4233 	}
4234 
4235 	/* Get dir "before" change value */
4236 	bdva.va_mask = AT_CTIME|AT_SEQ;
4237 	error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
4238 	if (error) {
4239 		*cs->statusp = resp->status = puterrno4(error);
4240 		if (nm != name)
4241 			kmem_free(name, MAXPATHLEN + 1);
4242 		kmem_free(nm, len);
4243 		if (in_crit)
4244 			nbl_end_crit(vp);
4245 		VN_RELE(vp);
4246 		if (fp) {
4247 			rfs4_clear_dont_grant(fp);
4248 			rfs4_file_rele(fp);
4249 		}
4250 		goto out;
4251 	}
4252 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
4253 
4254 	/* Actually do the REMOVE operation */
4255 	if (vp->v_type == VDIR) {
4256 		/*
4257 		 * Can't remove a directory that has a mounted-on filesystem.
4258 		 */
4259 		if (vn_ismntpt(vp)) {
4260 			error = EACCES;
4261 		} else {
4262 			/*
4263 			 * System V defines rmdir to return EEXIST,
4264 			 * not ENOTEMPTY, if the directory is not
4265 			 * empty.  A System V NFS server needs to map
4266 			 * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
4267 			 * transmit over the wire.
4268 			 */
4269 			if ((error = VOP_RMDIR(dvp, name, ZONE_ROOTVP(), cs->cr,
4270 			    NULL, 0)) == EEXIST)
4271 				error = ENOTEMPTY;
4272 		}
4273 	} else {
4274 		if ((error = VOP_REMOVE(dvp, name, cs->cr, NULL, 0)) == 0 &&
4275 		    fp != NULL) {
4276 			struct vattr va;
4277 			vnode_t *tvp;
4278 
4279 			rfs4_dbe_lock(fp->rf_dbe);
4280 			tvp = fp->rf_vp;
4281 			if (tvp)
4282 				VN_HOLD(tvp);
4283 			rfs4_dbe_unlock(fp->rf_dbe);
4284 
4285 			if (tvp) {
4286 				/*
4287 				 * This is va_seq safe because we are not
4288 				 * manipulating dvp.
4289 				 */
4290 				va.va_mask = AT_NLINK;
4291 				if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4292 				    va.va_nlink == 0) {
4293 					/* Remove state on file remove */
4294 					if (in_crit) {
4295 						nbl_end_crit(vp);
4296 						in_crit = 0;
4297 					}
4298 					rfs4_close_all_state(fp);
4299 				}
4300 				VN_RELE(tvp);
4301 			}
4302 		}
4303 	}
4304 
4305 	if (in_crit)
4306 		nbl_end_crit(vp);
4307 	VN_RELE(vp);
4308 
4309 	if (fp) {
4310 		rfs4_clear_dont_grant(fp);
4311 		rfs4_file_rele(fp);
4312 	}
4313 	if (nm != name)
4314 		kmem_free(name, MAXPATHLEN + 1);
4315 	kmem_free(nm, len);
4316 
4317 	if (error) {
4318 		*cs->statusp = resp->status = puterrno4(error);
4319 		goto out;
4320 	}
4321 
4322 	/*
4323 	 * Get the initial "after" sequence number, if it fails, set to zero
4324 	 */
4325 	idva.va_mask = AT_SEQ;
4326 	if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
4327 		idva.va_seq = 0;
4328 
4329 	/*
4330 	 * Force modified data and metadata out to stable storage.
4331 	 */
4332 	(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
4333 
4334 	/*
4335 	 * Get "after" change value, if it fails, simply return the
4336 	 * before value.
4337 	 */
4338 	adva.va_mask = AT_CTIME|AT_SEQ;
4339 	if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
4340 		adva.va_ctime = bdva.va_ctime;
4341 		adva.va_seq = 0;
4342 	}
4343 
4344 	NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
4345 
4346 	/*
4347 	 * The cinfo.atomic = TRUE only if we have
4348 	 * non-zero va_seq's, and it has incremented by exactly one
4349 	 * during the VOP_REMOVE/RMDIR and it didn't change during
4350 	 * the VOP_FSYNC.
4351 	 */
4352 	if (bdva.va_seq && idva.va_seq && adva.va_seq &&
4353 	    idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
4354 		resp->cinfo.atomic = TRUE;
4355 	else
4356 		resp->cinfo.atomic = FALSE;
4357 
4358 	*cs->statusp = resp->status = NFS4_OK;
4359 
4360 out:
4361 	DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs,
4362 	    REMOVE4res *, resp);
4363 }
4364 
4365 /*
4366  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
4367  *		oldname and newname.
4368  *	res: status. If success - CURRENT_FH unchanged, return change_info
4369  *		for both from and target directories.
4370  */
4371 /* ARGSUSED */
4372 static void
4373 rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4374     struct compound_state *cs)
4375 {
4376 	RENAME4args *args = &argop->nfs_argop4_u.oprename;
4377 	RENAME4res *resp = &resop->nfs_resop4_u.oprename;
4378 	int error;
4379 	vnode_t *odvp;
4380 	vnode_t *ndvp;
4381 	vnode_t *srcvp, *targvp, *tvp;
4382 	struct vattr obdva, oidva, oadva;
4383 	struct vattr nbdva, nidva, nadva;
4384 	char *onm, *nnm;
4385 	uint_t olen, nlen;
4386 	rfs4_file_t *fp, *sfp;
4387 	int in_crit_src, in_crit_targ;
4388 	int fp_rele_grant_hold, sfp_rele_grant_hold;
4389 	int unlinked;
4390 	bslabel_t *clabel;
4391 	struct sockaddr *ca;
4392 	char *converted_onm = NULL;
4393 	char *converted_nnm = NULL;
4394 	nfsstat4 status;
4395 
4396 	DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs,
4397 	    RENAME4args *, args);
4398 
4399 	fp = sfp = NULL;
4400 	srcvp = targvp = tvp = NULL;
4401 	in_crit_src = in_crit_targ = 0;
4402 	fp_rele_grant_hold = sfp_rele_grant_hold = 0;
4403 	unlinked = 0;
4404 
4405 	/* CURRENT_FH: target directory */
4406 	ndvp = cs->vp;
4407 	if (ndvp == NULL) {
4408 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4409 		goto out;
4410 	}
4411 
4412 	/* SAVED_FH: from directory */
4413 	odvp = cs->saved_vp;
4414 	if (odvp == NULL) {
4415 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4416 		goto out;
4417 	}
4418 
4419 	if (cs->access == CS_ACCESS_DENIED) {
4420 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4421 		goto out;
4422 	}
4423 
4424 	/*
4425 	 * If there is an unshared filesystem mounted on this vnode,
4426 	 * do not allow to rename objects in this directory.
4427 	 */
4428 	if (vn_ismntpt(odvp)) {
4429 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4430 		goto out;
4431 	}
4432 
4433 	/*
4434 	 * If there is an unshared filesystem mounted on this vnode,
4435 	 * do not allow to rename to this directory.
4436 	 */
4437 	if (vn_ismntpt(ndvp)) {
4438 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
4439 		goto out;
4440 	}
4441 
4442 	if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
4443 		*cs->statusp = resp->status = NFS4ERR_NOTDIR;
4444 		goto out;
4445 	}
4446 
4447 	if (cs->saved_exi != cs->exi) {
4448 		*cs->statusp = resp->status = NFS4ERR_XDEV;
4449 		goto out;
4450 	}
4451 
4452 	status = utf8_dir_verify(&args->oldname);
4453 	if (status != NFS4_OK) {
4454 		*cs->statusp = resp->status = status;
4455 		goto out;
4456 	}
4457 
4458 	status = utf8_dir_verify(&args->newname);
4459 	if (status != NFS4_OK) {
4460 		*cs->statusp = resp->status = status;
4461 		goto out;
4462 	}
4463 
4464 	onm = utf8_to_fn(&args->oldname, &olen, NULL);
4465 	if (onm == NULL) {
4466 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4467 		goto out;
4468 	}
4469 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4470 	nlen = MAXPATHLEN + 1;
4471 	converted_onm = nfscmd_convname(ca, cs->exi, onm, NFSCMD_CONV_INBOUND,
4472 	    nlen);
4473 
4474 	if (converted_onm == NULL) {
4475 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4476 		kmem_free(onm, olen);
4477 		goto out;
4478 	}
4479 
4480 	nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4481 	if (nnm == NULL) {
4482 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4483 		if (onm != converted_onm)
4484 			kmem_free(converted_onm, MAXPATHLEN + 1);
4485 		kmem_free(onm, olen);
4486 		goto out;
4487 	}
4488 	converted_nnm = nfscmd_convname(ca, cs->exi, nnm, NFSCMD_CONV_INBOUND,
4489 	    MAXPATHLEN  + 1);
4490 
4491 	if (converted_nnm == NULL) {
4492 		*cs->statusp = resp->status = NFS4ERR_INVAL;
4493 		kmem_free(nnm, nlen);
4494 		nnm = NULL;
4495 		if (onm != converted_onm)
4496 			kmem_free(converted_onm, MAXPATHLEN + 1);
4497 		kmem_free(onm, olen);
4498 		goto out;
4499 	}
4500 
4501 
4502 	if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
4503 		*cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4504 		kmem_free(onm, olen);
4505 		kmem_free(nnm, nlen);
4506 		goto out;
4507 	}
4508 
4509 
4510 	if (rdonly4(req, cs)) {
4511 		*cs->statusp = resp->status = NFS4ERR_ROFS;
4512 		if (onm != converted_onm)
4513 			kmem_free(converted_onm, MAXPATHLEN + 1);
4514 		kmem_free(onm, olen);
4515 		if (nnm != converted_nnm)
4516 			kmem_free(converted_nnm, MAXPATHLEN + 1);
4517 		kmem_free(nnm, nlen);
4518 		goto out;
4519 	}
4520 
4521 	/* check label of the target dir */
4522 	if (is_system_labeled()) {
4523 		ASSERT(req->rq_label != NULL);
4524 		clabel = req->rq_label;
4525 		DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
4526 		    "got client label from request(1)",
4527 		    struct svc_req *, req);
4528 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
4529 			if (!do_rfs_label_check(clabel, ndvp,
4530 			    EQUALITY_CHECK, cs->exi)) {
4531 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
4532 				goto err_out;
4533 			}
4534 		}
4535 	}
4536 
4537 	/*
4538 	 * Is the source a file and have a delegation?
4539 	 * We don't need to acquire va_seq before these lookups, if
4540 	 * it causes an update, cinfo.before will not match, which will
4541 	 * trigger a cache flush even if atomic is TRUE.
4542 	 */
4543 	if (sfp = rfs4_lookup_and_findfile(odvp, converted_onm, &srcvp,
4544 	    &error, cs->cr)) {
4545 		if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
4546 		    NULL)) {
4547 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4548 			goto err_out;
4549 		}
4550 	}
4551 
4552 	if (srcvp == NULL) {
4553 		*cs->statusp = resp->status = puterrno4(error);
4554 		if (onm != converted_onm)
4555 			kmem_free(converted_onm, MAXPATHLEN + 1);
4556 		kmem_free(onm, olen);
4557 		if (nnm != converted_nnm)
4558 			kmem_free(converted_nnm, MAXPATHLEN + 1);
4559 		kmem_free(nnm, nlen);
4560 		goto out;
4561 	}
4562 
4563 	sfp_rele_grant_hold = 1;
4564 
4565 	/* Does the destination exist and a file and have a delegation? */
4566 	if (fp = rfs4_lookup_and_findfile(ndvp, converted_nnm, &targvp,
4567 	    NULL, cs->cr)) {
4568 		if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4569 		    NULL)) {
4570 			*cs->statusp = resp->status = NFS4ERR_DELAY;
4571 			goto err_out;
4572 		}
4573 	}
4574 	fp_rele_grant_hold = 1;
4575 
4576 	/* Check for NBMAND lock on both source and target */
4577 	if (nbl_need_check(srcvp)) {
4578 		nbl_start_crit(srcvp, RW_READER);
4579 		in_crit_src = 1;
4580 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) {
4581 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4582 			goto err_out;
4583 		}
4584 	}
4585 
4586 	if (targvp && nbl_need_check(targvp)) {
4587 		nbl_start_crit(targvp, RW_READER);
4588 		in_crit_targ = 1;
4589 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
4590 			*cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4591 			goto err_out;
4592 		}
4593 	}
4594 
4595 	/* Get source "before" change value */
4596 	obdva.va_mask = AT_CTIME|AT_SEQ;
4597 	error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL);
4598 	if (!error) {
4599 		nbdva.va_mask = AT_CTIME|AT_SEQ;
4600 		error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL);
4601 	}
4602 	if (error) {
4603 		*cs->statusp = resp->status = puterrno4(error);
4604 		goto err_out;
4605 	}
4606 
4607 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
4608 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
4609 
4610 	error = VOP_RENAME(odvp, converted_onm, ndvp, converted_nnm, cs->cr,
4611 	    NULL, 0);
4612 
4613 	/*
4614 	 * If target existed and was unlinked by VOP_RENAME, state will need
4615 	 * closed. To avoid deadlock, rfs4_close_all_state will be done after
4616 	 * any necessary nbl_end_crit on srcvp and tgtvp.
4617 	 */
4618 	if (error == 0 && fp != NULL) {
4619 		rfs4_dbe_lock(fp->rf_dbe);
4620 		tvp = fp->rf_vp;
4621 		if (tvp)
4622 			VN_HOLD(tvp);
4623 		rfs4_dbe_unlock(fp->rf_dbe);
4624 
4625 		if (tvp) {
4626 			struct vattr va;
4627 			va.va_mask = AT_NLINK;
4628 
4629 			if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4630 			    va.va_nlink == 0) {
4631 				unlinked = 1;
4632 
4633 				/* DEBUG data */
4634 				if ((srcvp == targvp) || (tvp != targvp)) {
4635 					cmn_err(CE_WARN, "rfs4_op_rename: "
4636 					    "srcvp %p, targvp: %p, tvp: %p",
4637 					    (void *)srcvp, (void *)targvp,
4638 					    (void *)tvp);
4639 				}
4640 			} else {
4641 				VN_RELE(tvp);
4642 			}
4643 		}
4644 	}
4645 	if (error == 0)
4646 		vn_renamepath(ndvp, srcvp, nnm, nlen - 1);
4647 
4648 	if (in_crit_src)
4649 		nbl_end_crit(srcvp);
4650 	if (srcvp)
4651 		VN_RELE(srcvp);
4652 	if (in_crit_targ)
4653 		nbl_end_crit(targvp);
4654 	if (targvp)
4655 		VN_RELE(targvp);
4656 
4657 	if (unlinked) {
4658 		ASSERT(fp != NULL);
4659 		ASSERT(tvp != NULL);
4660 
4661 		/* DEBUG data */
4662 		if (RW_READ_HELD(&tvp->v_nbllock)) {
4663 			cmn_err(CE_WARN, "rfs4_op_rename: "
4664 			    "RW_READ_HELD(%p)", (void *)tvp);
4665 		}
4666 
4667 		/* The file is gone and so should the state */
4668 		rfs4_close_all_state(fp);
4669 		VN_RELE(tvp);
4670 	}
4671 
4672 	if (sfp) {
4673 		rfs4_clear_dont_grant(sfp);
4674 		rfs4_file_rele(sfp);
4675 	}
4676 	if (fp) {
4677 		rfs4_clear_dont_grant(fp);
4678 		rfs4_file_rele(fp);
4679 	}
4680 
4681 	if (converted_onm != onm)
4682 		kmem_free(converted_onm, MAXPATHLEN + 1);
4683 	kmem_free(onm, olen);
4684 	if (converted_nnm != nnm)
4685 		kmem_free(converted_nnm, MAXPATHLEN + 1);
4686 	kmem_free(nnm, nlen);
4687 
4688 	/*
4689 	 * Get the initial "after" sequence number, if it fails, set to zero
4690 	 */
4691 	oidva.va_mask = AT_SEQ;
4692 	if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL))
4693 		oidva.va_seq = 0;
4694 
4695 	nidva.va_mask = AT_SEQ;
4696 	if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL))
4697 		nidva.va_seq = 0;
4698 
4699 	/*
4700 	 * Force modified data and metadata out to stable storage.
4701 	 */
4702 	(void) VOP_FSYNC(odvp, 0, cs->cr, NULL);
4703 	(void) VOP_FSYNC(ndvp, 0, cs->cr, NULL);
4704 
4705 	if (error) {
4706 		*cs->statusp = resp->status = puterrno4(error);
4707 		goto out;
4708 	}
4709 
4710 	/*
4711 	 * Get "after" change values, if it fails, simply return the
4712 	 * before value.
4713 	 */
4714 	oadva.va_mask = AT_CTIME|AT_SEQ;
4715 	if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) {
4716 		oadva.va_ctime = obdva.va_ctime;
4717 		oadva.va_seq = 0;
4718 	}
4719 
4720 	nadva.va_mask = AT_CTIME|AT_SEQ;
4721 	if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) {
4722 		nadva.va_ctime = nbdva.va_ctime;
4723 		nadva.va_seq = 0;
4724 	}
4725 
4726 	NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
4727 	NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
4728 
4729 	/*
4730 	 * The cinfo.atomic = TRUE only if we have
4731 	 * non-zero va_seq's, and it has incremented by exactly one
4732 	 * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
4733 	 */
4734 	if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
4735 	    oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq)
4736 		resp->source_cinfo.atomic = TRUE;
4737 	else
4738 		resp->source_cinfo.atomic = FALSE;
4739 
4740 	if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
4741 	    nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq)
4742 		resp->target_cinfo.atomic = TRUE;
4743 	else
4744 		resp->target_cinfo.atomic = FALSE;
4745 
4746 #ifdef	VOLATILE_FH_TEST
4747 	{
4748 	extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
4749 
4750 	/*
4751 	 * Add the renamed file handle to the volatile rename list
4752 	 */
4753 	if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
4754 		/* file handles may expire on rename */
4755 		vnode_t *vp;
4756 
4757 		nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4758 		/*
4759 		 * Already know that nnm will be a valid string
4760 		 */
4761 		error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr,
4762 		    NULL, NULL, NULL);
4763 		kmem_free(nnm, nlen);
4764 		if (!error) {
4765 			add_volrnm_fh(cs->exi, vp);
4766 			VN_RELE(vp);
4767 		}
4768 	}
4769 	}
4770 #endif	/* VOLATILE_FH_TEST */
4771 
4772 	*cs->statusp = resp->status = NFS4_OK;
4773 out:
4774 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4775 	    RENAME4res *, resp);
4776 	return;
4777 
4778 err_out:
4779 	if (onm != converted_onm)
4780 		kmem_free(converted_onm, MAXPATHLEN + 1);
4781 	if (onm != NULL)
4782 		kmem_free(onm, olen);
4783 	if (nnm != converted_nnm)
4784 		kmem_free(converted_nnm, MAXPATHLEN + 1);
4785 	if (nnm != NULL)
4786 		kmem_free(nnm, nlen);
4787 
4788 	if (in_crit_src) nbl_end_crit(srcvp);
4789 	if (in_crit_targ) nbl_end_crit(targvp);
4790 	if (targvp) VN_RELE(targvp);
4791 	if (srcvp) VN_RELE(srcvp);
4792 	if (sfp) {
4793 		if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
4794 		rfs4_file_rele(sfp);
4795 	}
4796 	if (fp) {
4797 		if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
4798 		rfs4_file_rele(fp);
4799 	}
4800 
4801 	DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4802 	    RENAME4res *, resp);
4803 }
4804 
4805 /* ARGSUSED */
4806 static void
4807 rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4808     struct compound_state *cs)
4809 {
4810 	RENEW4args *args = &argop->nfs_argop4_u.oprenew;
4811 	RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
4812 	rfs4_client_t *cp;
4813 
4814 	DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs,
4815 	    RENEW4args *, args);
4816 
4817 	if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
4818 		*cs->statusp = resp->status =
4819 		    rfs4_check_clientid(&args->clientid, 0);
4820 		goto out;
4821 	}
4822 
4823 	if (rfs4_lease_expired(cp)) {
4824 		rfs4_client_rele(cp);
4825 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
4826 		goto out;
4827 	}
4828 
4829 	rfs4_update_lease(cp);
4830 
4831 	mutex_enter(cp->rc_cbinfo.cb_lock);
4832 	if (cp->rc_cbinfo.cb_notified_of_cb_path_down == FALSE) {
4833 		cp->rc_cbinfo.cb_notified_of_cb_path_down = TRUE;
4834 		*cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
4835 	} else {
4836 		*cs->statusp = resp->status = NFS4_OK;
4837 	}
4838 	mutex_exit(cp->rc_cbinfo.cb_lock);
4839 
4840 	rfs4_client_rele(cp);
4841 
4842 out:
4843 	DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs,
4844 	    RENEW4res *, resp);
4845 }
4846 
4847 /* ARGSUSED */
4848 static void
4849 rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
4850     struct compound_state *cs)
4851 {
4852 	RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
4853 
4854 	DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs);
4855 
4856 	/* No need to check cs->access - we are not accessing any object */
4857 	if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
4858 		*cs->statusp = resp->status = NFS4ERR_RESTOREFH;
4859 		goto out;
4860 	}
4861 	if (cs->vp != NULL) {
4862 		VN_RELE(cs->vp);
4863 	}
4864 	cs->vp = cs->saved_vp;
4865 	cs->saved_vp = NULL;
4866 	cs->exi = cs->saved_exi;
4867 	nfs_fh4_copy(&cs->saved_fh, &cs->fh);
4868 	*cs->statusp = resp->status = NFS4_OK;
4869 	cs->deleg = FALSE;
4870 
4871 out:
4872 	DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs,
4873 	    RESTOREFH4res *, resp);
4874 }
4875 
4876 /* ARGSUSED */
4877 static void
4878 rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4879     struct compound_state *cs)
4880 {
4881 	SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
4882 
4883 	DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs);
4884 
4885 	/* No need to check cs->access - we are not accessing any object */
4886 	if (cs->vp == NULL) {
4887 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4888 		goto out;
4889 	}
4890 	if (cs->saved_vp != NULL) {
4891 		VN_RELE(cs->saved_vp);
4892 	}
4893 	cs->saved_vp = cs->vp;
4894 	VN_HOLD(cs->saved_vp);
4895 	cs->saved_exi = cs->exi;
4896 	/*
4897 	 * since SAVEFH is fairly rare, don't alloc space for its fh
4898 	 * unless necessary.
4899 	 */
4900 	if (cs->saved_fh.nfs_fh4_val == NULL) {
4901 		cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
4902 	}
4903 	nfs_fh4_copy(&cs->fh, &cs->saved_fh);
4904 	*cs->statusp = resp->status = NFS4_OK;
4905 
4906 out:
4907 	DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs,
4908 	    SAVEFH4res *, resp);
4909 }
4910 
4911 /*
4912  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
4913  * return the bitmap of attrs that were set successfully. It is also
4914  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
4915  * always be called only after rfs4_do_set_attrs().
4916  *
4917  * Verify that the attributes are same as the expected ones. sargp->vap
4918  * and sargp->sbp contain the input attributes as translated from fattr4.
4919  *
4920  * This function verifies only the attrs that correspond to a vattr or
4921  * vfsstat struct. That is because of the extra step needed to get the
4922  * corresponding system structs. Other attributes have already been set or
4923  * verified by do_rfs4_set_attrs.
4924  *
4925  * Return 0 if all attrs match, -1 if some don't, error if error processing.
4926  */
4927 static int
4928 rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
4929     bitmap4 *resp, struct nfs4_ntov_table *ntovp)
4930 {
4931 	int error, ret_error = 0;
4932 	int i, k;
4933 	uint_t sva_mask = sargp->vap->va_mask;
4934 	uint_t vbit;
4935 	union nfs4_attr_u *na;
4936 	uint8_t *amap;
4937 	bool_t getsb = ntovp->vfsstat;
4938 
4939 	if (sva_mask != 0) {
4940 		/*
4941 		 * Okay to overwrite sargp->vap because we verify based
4942 		 * on the incoming values.
4943 		 */
4944 		ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
4945 		    sargp->cs->cr, NULL);
4946 		if (ret_error) {
4947 			if (resp == NULL)
4948 				return (ret_error);
4949 			/*
4950 			 * Must return bitmap of successful attrs
4951 			 */
4952 			sva_mask = 0;	/* to prevent checking vap later */
4953 		} else {
4954 			/*
4955 			 * Some file systems clobber va_mask. it is probably
4956 			 * wrong of them to do so, nonethless we practice
4957 			 * defensive coding.
4958 			 * See bug id 4276830.
4959 			 */
4960 			sargp->vap->va_mask = sva_mask;
4961 		}
4962 	}
4963 
4964 	if (getsb) {
4965 		/*
4966 		 * Now get the superblock and loop on the bitmap, as there is
4967 		 * no simple way of translating from superblock to bitmap4.
4968 		 */
4969 		ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
4970 		if (ret_error) {
4971 			if (resp == NULL)
4972 				goto errout;
4973 			getsb = FALSE;
4974 		}
4975 	}
4976 
4977 	/*
4978 	 * Now loop and verify each attribute which getattr returned
4979 	 * whether it's the same as the input.
4980 	 */
4981 	if (resp == NULL && !getsb && (sva_mask == 0))
4982 		goto errout;
4983 
4984 	na = ntovp->na;
4985 	amap = ntovp->amap;
4986 	k = 0;
4987 	for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
4988 		k = *amap;
4989 		ASSERT(nfs4_ntov_map[k].nval == k);
4990 		vbit = nfs4_ntov_map[k].vbit;
4991 
4992 		/*
4993 		 * If vattr attribute but VOP_GETATTR failed, or it's
4994 		 * superblock attribute but VFS_STATVFS failed, skip
4995 		 */
4996 		if (vbit) {
4997 			if ((vbit & sva_mask) == 0)
4998 				continue;
4999 		} else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
5000 			continue;
5001 		}
5002 		error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na);
5003 		if (resp != NULL) {
5004 			if (error)
5005 				ret_error = -1;	/* not all match */
5006 			else	/* update response bitmap */
5007 				*resp |= nfs4_ntov_map[k].fbit;
5008 			continue;
5009 		}
5010 		if (error) {
5011 			ret_error = -1;	/* not all match */
5012 			break;
5013 		}
5014 	}
5015 errout:
5016 	return (ret_error);
5017 }
5018 
5019 /*
5020  * Decode the attribute to be set/verified. If the attr requires a sys op
5021  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
5022  * call the sv_getit function for it, because the sys op hasn't yet been done.
5023  * Return 0 for success, error code if failed.
5024  *
5025  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
5026  */
5027 static int
5028 decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
5029     int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
5030 {
5031 	int error = 0;
5032 	bool_t set_later;
5033 
5034 	sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
5035 
5036 	if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
5037 		set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
5038 		/*
5039 		 * don't verify yet if a vattr or sb dependent attr,
5040 		 * because we don't have their sys values yet.
5041 		 * Will be done later.
5042 		 */
5043 		if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
5044 			/*
5045 			 * ACLs are a special case, since setting the MODE
5046 			 * conflicts with setting the ACL.  We delay setting
5047 			 * the ACL until all other attributes have been set.
5048 			 * The ACL gets set in do_rfs4_op_setattr().
5049 			 */
5050 			if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
5051 				error = (*nfs4_ntov_map[k].sv_getit)(cmd,
5052 				    sargp, nap);
5053 				if (error) {
5054 					xdr_free(nfs4_ntov_map[k].xfunc,
5055 					    (caddr_t)nap);
5056 				}
5057 			}
5058 		}
5059 	} else {
5060 #ifdef  DEBUG
5061 		cmn_err(CE_NOTE, "decode_fattr4_attr: error "
5062 		    "decoding attribute %d\n", k);
5063 #endif
5064 		error = EINVAL;
5065 	}
5066 	if (!error && resp_bval && !set_later) {
5067 		*resp_bval |= nfs4_ntov_map[k].fbit;
5068 	}
5069 
5070 	return (error);
5071 }
5072 
5073 /*
5074  * Set vattr based on incoming fattr4 attrs - used by setattr.
5075  * Set response mask. Ignore any values that are not writable vattr attrs.
5076  */
5077 static nfsstat4
5078 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
5079     struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
5080     nfs4_attr_cmd_t cmd)
5081 {
5082 	int error = 0;
5083 	int i;
5084 	char *attrs = fattrp->attrlist4;
5085 	uint32_t attrslen = fattrp->attrlist4_len;
5086 	XDR xdr;
5087 	nfsstat4 status = NFS4_OK;
5088 	vnode_t *vp = cs->vp;
5089 	union nfs4_attr_u *na;
5090 	uint8_t *amap;
5091 
5092 #ifndef lint
5093 	/*
5094 	 * Make sure that maximum attribute number can be expressed as an
5095 	 * 8 bit quantity.
5096 	 */
5097 	ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
5098 #endif
5099 
5100 	if (vp == NULL) {
5101 		if (resp)
5102 			*resp = 0;
5103 		return (NFS4ERR_NOFILEHANDLE);
5104 	}
5105 	if (cs->access == CS_ACCESS_DENIED) {
5106 		if (resp)
5107 			*resp = 0;
5108 		return (NFS4ERR_ACCESS);
5109 	}
5110 
5111 	sargp->op = cmd;
5112 	sargp->cs = cs;
5113 	sargp->flag = 0;	/* may be set later */
5114 	sargp->vap->va_mask = 0;
5115 	sargp->rdattr_error = NFS4_OK;
5116 	sargp->rdattr_error_req = FALSE;
5117 	/* sargp->sbp is set by the caller */
5118 
5119 	xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
5120 
5121 	na = ntovp->na;
5122 	amap = ntovp->amap;
5123 
5124 	/*
5125 	 * The following loop iterates on the nfs4_ntov_map checking
5126 	 * if the fbit is set in the requested bitmap.
5127 	 * If set then we process the arguments using the
5128 	 * rfs4_fattr4 conversion functions to populate the setattr
5129 	 * vattr and va_mask. Any settable attrs that are not using vattr
5130 	 * will be set in this loop.
5131 	 */
5132 	for (i = 0; i < nfs4_ntov_map_size; i++) {
5133 		if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
5134 			continue;
5135 		}
5136 		/*
5137 		 * If setattr, must be a writable attr.
5138 		 * If verify/nverify, must be a readable attr.
5139 		 */
5140 		if ((error = (*nfs4_ntov_map[i].sv_getit)(
5141 		    NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
5142 			/*
5143 			 * Client tries to set/verify an
5144 			 * unsupported attribute, tries to set
5145 			 * a read only attr or verify a write
5146 			 * only one - error!
5147 			 */
5148 			break;
5149 		}
5150 		/*
5151 		 * Decode the attribute to set/verify
5152 		 */
5153 		error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
5154 		    &xdr, resp ? resp : NULL, na);
5155 		if (error)
5156 			break;
5157 		*amap++ = (uint8_t)nfs4_ntov_map[i].nval;
5158 		na++;
5159 		(ntovp->attrcnt)++;
5160 		if (nfs4_ntov_map[i].vfsstat)
5161 			ntovp->vfsstat = TRUE;
5162 	}
5163 
5164 	if (error != 0)
5165 		status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP :
5166 		    puterrno4(error));
5167 	/* xdrmem_destroy(&xdrs); */	/* NO-OP */
5168 	return (status);
5169 }
5170 
5171 static nfsstat4
5172 do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
5173     stateid4 *stateid)
5174 {
5175 	int error = 0;
5176 	struct nfs4_svgetit_arg sarg;
5177 	bool_t trunc;
5178 
5179 	nfsstat4 status = NFS4_OK;
5180 	cred_t *cr = cs->cr;
5181 	vnode_t *vp = cs->vp;
5182 	struct nfs4_ntov_table ntov;
5183 	struct statvfs64 sb;
5184 	struct vattr bva;
5185 	struct flock64 bf;
5186 	int in_crit = 0;
5187 	uint_t saved_mask = 0;
5188 	caller_context_t ct;
5189 
5190 	*resp = 0;
5191 	sarg.sbp = &sb;
5192 	sarg.is_referral = B_FALSE;
5193 	nfs4_ntov_table_init(&ntov);
5194 	status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
5195 	    NFS4ATTR_SETIT);
5196 	if (status != NFS4_OK) {
5197 		/*
5198 		 * failed set attrs
5199 		 */
5200 		goto done;
5201 	}
5202 	if ((sarg.vap->va_mask == 0) &&
5203 	    (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
5204 		/*
5205 		 * no further work to be done
5206 		 */
5207 		goto done;
5208 	}
5209 
5210 	/*
5211 	 * If we got a request to set the ACL and the MODE, only
5212 	 * allow changing VSUID, VSGID, and VSVTX.  Attempting
5213 	 * to change any other bits, along with setting an ACL,
5214 	 * gives NFS4ERR_INVAL.
5215 	 */
5216 	if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
5217 	    (fattrp->attrmask & FATTR4_MODE_MASK)) {
5218 		vattr_t va;
5219 
5220 		va.va_mask = AT_MODE;
5221 		error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
5222 		if (error) {
5223 			status = puterrno4(error);
5224 			goto done;
5225 		}
5226 		if ((sarg.vap->va_mode ^ va.va_mode) &
5227 		    ~(VSUID | VSGID | VSVTX)) {
5228 			status = NFS4ERR_INVAL;
5229 			goto done;
5230 		}
5231 	}
5232 
5233 	/* Check stateid only if size has been set */
5234 	if (sarg.vap->va_mask & AT_SIZE) {
5235 		trunc = (sarg.vap->va_size == 0);
5236 		status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
5237 		    trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct);
5238 		if (status != NFS4_OK)
5239 			goto done;
5240 	} else {
5241 		ct.cc_sysid = 0;
5242 		ct.cc_pid = 0;
5243 		ct.cc_caller_id = nfs4_srv_caller_id;
5244 		ct.cc_flags = CC_DONTBLOCK;
5245 	}
5246 
5247 	/* XXX start of possible race with delegations */
5248 
5249 	/*
5250 	 * We need to specially handle size changes because it is
5251 	 * possible for the client to create a file with read-only
5252 	 * modes, but with the file opened for writing. If the client
5253 	 * then tries to set the file size, e.g. ftruncate(3C),
5254 	 * fcntl(F_FREESP), the normal access checking done in
5255 	 * VOP_SETATTR would prevent the client from doing it even though
5256 	 * it should be allowed to do so.  To get around this, we do the
5257 	 * access checking for ourselves and use VOP_SPACE which doesn't
5258 	 * do the access checking.
5259 	 * Also the client should not be allowed to change the file
5260 	 * size if there is a conflicting non-blocking mandatory lock in
5261 	 * the region of the change.
5262 	 */
5263 	if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
5264 		u_offset_t offset;
5265 		ssize_t length;
5266 
5267 		/*
5268 		 * ufs_setattr clears AT_SIZE from vap->va_mask, but
5269 		 * before returning, sarg.vap->va_mask is used to
5270 		 * generate the setattr reply bitmap.  We also clear
5271 		 * AT_SIZE below before calling VOP_SPACE.  For both
5272 		 * of these cases, the va_mask needs to be saved here
5273 		 * and restored after calling VOP_SETATTR.
5274 		 */
5275 		saved_mask = sarg.vap->va_mask;
5276 
5277 		/*
5278 		 * Check any possible conflict due to NBMAND locks.
5279 		 * Get into critical region before VOP_GETATTR, so the
5280 		 * size attribute is valid when checking conflicts.
5281 		 */
5282 		if (nbl_need_check(vp)) {
5283 			nbl_start_crit(vp, RW_READER);
5284 			in_crit = 1;
5285 		}
5286 
5287 		bva.va_mask = AT_UID|AT_SIZE;
5288 		if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
5289 			status = puterrno4(error);
5290 			goto done;
5291 		}
5292 
5293 		if (in_crit) {
5294 			if (sarg.vap->va_size < bva.va_size) {
5295 				offset = sarg.vap->va_size;
5296 				length = bva.va_size - sarg.vap->va_size;
5297 			} else {
5298 				offset = bva.va_size;
5299 				length = sarg.vap->va_size - bva.va_size;
5300 			}
5301 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
5302 			    &ct)) {
5303 				status = NFS4ERR_LOCKED;
5304 				goto done;
5305 			}
5306 		}
5307 
5308 		if (crgetuid(cr) == bva.va_uid) {
5309 			sarg.vap->va_mask &= ~AT_SIZE;
5310 			bf.l_type = F_WRLCK;
5311 			bf.l_whence = 0;
5312 			bf.l_start = (off64_t)sarg.vap->va_size;
5313 			bf.l_len = 0;
5314 			bf.l_sysid = 0;
5315 			bf.l_pid = 0;
5316 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
5317 			    (offset_t)sarg.vap->va_size, cr, &ct);
5318 		}
5319 	}
5320 
5321 	if (!error && sarg.vap->va_mask != 0)
5322 		error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
5323 
5324 	/* restore va_mask -- ufs_setattr clears AT_SIZE */
5325 	if (saved_mask & AT_SIZE)
5326 		sarg.vap->va_mask |= AT_SIZE;
5327 
5328 	/*
5329 	 * If an ACL was being set, it has been delayed until now,
5330 	 * in order to set the mode (via the VOP_SETATTR() above) first.
5331 	 */
5332 	if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
5333 		int i;
5334 
5335 		for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
5336 			if (ntov.amap[i] == FATTR4_ACL)
5337 				break;
5338 		if (i < NFS4_MAXNUM_ATTRS) {
5339 			error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
5340 			    NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
5341 			if (error == 0) {
5342 				*resp |= FATTR4_ACL_MASK;
5343 			} else if (error == ENOTSUP) {
5344 				(void) rfs4_verify_attr(&sarg, resp, &ntov);
5345 				status = NFS4ERR_ATTRNOTSUPP;
5346 				goto done;
5347 			}
5348 		} else {
5349 			NFS4_DEBUG(rfs4_debug,
5350 			    (CE_NOTE, "do_rfs4_op_setattr: "
5351 			    "unable to find ACL in fattr4"));
5352 			error = EINVAL;
5353 		}
5354 	}
5355 
5356 	if (error) {
5357 		/* check if a monitor detected a delegation conflict */
5358 		if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
5359 			status = NFS4ERR_DELAY;
5360 		else
5361 			status = puterrno4(error);
5362 
5363 		/*
5364 		 * Set the response bitmap when setattr failed.
5365 		 * If VOP_SETATTR partially succeeded, test by doing a
5366 		 * VOP_GETATTR on the object and comparing the data
5367 		 * to the setattr arguments.
5368 		 */
5369 		(void) rfs4_verify_attr(&sarg, resp, &ntov);
5370 	} else {
5371 		/*
5372 		 * Force modified metadata out to stable storage.
5373 		 */
5374 		(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
5375 		/*
5376 		 * Set response bitmap
5377 		 */
5378 		nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
5379 	}
5380 
5381 /* Return early and already have a NFSv4 error */
5382 done:
5383 	/*
5384 	 * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
5385 	 * conversion sets both readable and writeable NFS4 attrs
5386 	 * for AT_MTIME and AT_ATIME.  The line below masks out
5387 	 * unrequested attrs from the setattr result bitmap.  This
5388 	 * is placed after the done: label to catch the ATTRNOTSUP
5389 	 * case.
5390 	 */
5391 	*resp &= fattrp->attrmask;
5392 
5393 	if (in_crit)
5394 		nbl_end_crit(vp);
5395 
5396 	nfs4_ntov_table_free(&ntov, &sarg);
5397 
5398 	return (status);
5399 }
5400 
5401 /* ARGSUSED */
5402 static void
5403 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5404     struct compound_state *cs)
5405 {
5406 	SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
5407 	SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
5408 	bslabel_t *clabel;
5409 
5410 	DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5411 	    SETATTR4args *, args);
5412 
5413 	if (cs->vp == NULL) {
5414 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5415 		goto out;
5416 	}
5417 
5418 	/*
5419 	 * If there is an unshared filesystem mounted on this vnode,
5420 	 * do not allow to setattr on this vnode.
5421 	 */
5422 	if (vn_ismntpt(cs->vp)) {
5423 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5424 		goto out;
5425 	}
5426 
5427 	resp->attrsset = 0;
5428 
5429 	if (rdonly4(req, cs)) {
5430 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5431 		goto out;
5432 	}
5433 
5434 	/* check label before setting attributes */
5435 	if (is_system_labeled()) {
5436 		ASSERT(req->rq_label != NULL);
5437 		clabel = req->rq_label;
5438 		DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
5439 		    "got client label from request(1)",
5440 		    struct svc_req *, req);
5441 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
5442 			if (!do_rfs_label_check(clabel, cs->vp,
5443 			    EQUALITY_CHECK, cs->exi)) {
5444 				*cs->statusp = resp->status = NFS4ERR_ACCESS;
5445 				goto out;
5446 			}
5447 		}
5448 	}
5449 
5450 	*cs->statusp = resp->status =
5451 	    do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
5452 	    &args->stateid);
5453 
5454 out:
5455 	DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5456 	    SETATTR4res *, resp);
5457 }
5458 
5459 /* ARGSUSED */
5460 static void
5461 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5462     struct compound_state *cs)
5463 {
5464 	/*
5465 	 * verify and nverify are exactly the same, except that nverify
5466 	 * succeeds when some argument changed, and verify succeeds when
5467 	 * when none changed.
5468 	 */
5469 
5470 	VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
5471 	VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
5472 
5473 	int error;
5474 	struct nfs4_svgetit_arg sarg;
5475 	struct statvfs64 sb;
5476 	struct nfs4_ntov_table ntov;
5477 
5478 	DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5479 	    VERIFY4args *, args);
5480 
5481 	if (cs->vp == NULL) {
5482 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5483 		goto out;
5484 	}
5485 
5486 	sarg.sbp = &sb;
5487 	sarg.is_referral = B_FALSE;
5488 	nfs4_ntov_table_init(&ntov);
5489 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5490 	    &sarg, &ntov, NFS4ATTR_VERIT);
5491 	if (resp->status != NFS4_OK) {
5492 		/*
5493 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5494 		 * so could return -1 for "no match".
5495 		 */
5496 		if (resp->status == -1)
5497 			resp->status = NFS4ERR_NOT_SAME;
5498 		goto done;
5499 	}
5500 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5501 	switch (error) {
5502 	case 0:
5503 		resp->status = NFS4_OK;
5504 		break;
5505 	case -1:
5506 		resp->status = NFS4ERR_NOT_SAME;
5507 		break;
5508 	default:
5509 		resp->status = puterrno4(error);
5510 		break;
5511 	}
5512 done:
5513 	*cs->statusp = resp->status;
5514 	nfs4_ntov_table_free(&ntov, &sarg);
5515 out:
5516 	DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5517 	    VERIFY4res *, resp);
5518 }
5519 
5520 /* ARGSUSED */
5521 static void
5522 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5523     struct compound_state *cs)
5524 {
5525 	/*
5526 	 * verify and nverify are exactly the same, except that nverify
5527 	 * succeeds when some argument changed, and verify succeeds when
5528 	 * when none changed.
5529 	 */
5530 
5531 	NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
5532 	NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
5533 
5534 	int error;
5535 	struct nfs4_svgetit_arg sarg;
5536 	struct statvfs64 sb;
5537 	struct nfs4_ntov_table ntov;
5538 
5539 	DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5540 	    NVERIFY4args *, args);
5541 
5542 	if (cs->vp == NULL) {
5543 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5544 		DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5545 		    NVERIFY4res *, resp);
5546 		return;
5547 	}
5548 	sarg.sbp = &sb;
5549 	sarg.is_referral = B_FALSE;
5550 	nfs4_ntov_table_init(&ntov);
5551 	resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5552 	    &sarg, &ntov, NFS4ATTR_VERIT);
5553 	if (resp->status != NFS4_OK) {
5554 		/*
5555 		 * do_rfs4_set_attrs will try to verify systemwide attrs,
5556 		 * so could return -1 for "no match".
5557 		 */
5558 		if (resp->status == -1)
5559 			resp->status = NFS4_OK;
5560 		goto done;
5561 	}
5562 	error = rfs4_verify_attr(&sarg, NULL, &ntov);
5563 	switch (error) {
5564 	case 0:
5565 		resp->status = NFS4ERR_SAME;
5566 		break;
5567 	case -1:
5568 		resp->status = NFS4_OK;
5569 		break;
5570 	default:
5571 		resp->status = puterrno4(error);
5572 		break;
5573 	}
5574 done:
5575 	*cs->statusp = resp->status;
5576 	nfs4_ntov_table_free(&ntov, &sarg);
5577 
5578 	DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5579 	    NVERIFY4res *, resp);
5580 }
5581 
5582 /*
5583  * XXX - This should live in an NFS header file.
5584  */
5585 #define	MAX_IOVECS	12
5586 
5587 /* ARGSUSED */
5588 static void
5589 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5590     struct compound_state *cs)
5591 {
5592 	WRITE4args *args = &argop->nfs_argop4_u.opwrite;
5593 	WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
5594 	int error;
5595 	vnode_t *vp;
5596 	struct vattr bva;
5597 	u_offset_t rlimit;
5598 	struct uio uio;
5599 	struct iovec iov[MAX_IOVECS];
5600 	struct iovec *iovp;
5601 	int iovcnt;
5602 	int ioflag;
5603 	cred_t *savecred, *cr;
5604 	bool_t *deleg = &cs->deleg;
5605 	nfsstat4 stat;
5606 	int in_crit = 0;
5607 	caller_context_t ct;
5608 	nfs4_srv_t *nsrv4;
5609 
5610 	DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5611 	    WRITE4args *, args);
5612 
5613 	vp = cs->vp;
5614 	if (vp == NULL) {
5615 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5616 		goto out;
5617 	}
5618 	if (cs->access == CS_ACCESS_DENIED) {
5619 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5620 		goto out;
5621 	}
5622 
5623 	cr = cs->cr;
5624 
5625 	if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5626 	    deleg, TRUE, &ct)) != NFS4_OK) {
5627 		*cs->statusp = resp->status = stat;
5628 		goto out;
5629 	}
5630 
5631 	/*
5632 	 * We have to enter the critical region before calling VOP_RWLOCK
5633 	 * to avoid a deadlock with ufs.
5634 	 */
5635 	if (nbl_need_check(vp)) {
5636 		nbl_start_crit(vp, RW_READER);
5637 		in_crit = 1;
5638 		if (nbl_conflict(vp, NBL_WRITE,
5639 		    args->offset, args->data_len, 0, &ct)) {
5640 			*cs->statusp = resp->status = NFS4ERR_LOCKED;
5641 			goto out;
5642 		}
5643 	}
5644 
5645 	bva.va_mask = AT_MODE | AT_UID;
5646 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
5647 
5648 	/*
5649 	 * If we can't get the attributes, then we can't do the
5650 	 * right access checking.  So, we'll fail the request.
5651 	 */
5652 	if (error) {
5653 		*cs->statusp = resp->status = puterrno4(error);
5654 		goto out;
5655 	}
5656 
5657 	if (rdonly4(req, cs)) {
5658 		*cs->statusp = resp->status = NFS4ERR_ROFS;
5659 		goto out;
5660 	}
5661 
5662 	if (vp->v_type != VREG) {
5663 		*cs->statusp = resp->status =
5664 		    ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
5665 		goto out;
5666 	}
5667 
5668 	if (crgetuid(cr) != bva.va_uid &&
5669 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
5670 		*cs->statusp = resp->status = puterrno4(error);
5671 		goto out;
5672 	}
5673 
5674 	if (MANDLOCK(vp, bva.va_mode)) {
5675 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
5676 		goto out;
5677 	}
5678 
5679 	nsrv4 = nfs4_get_srv();
5680 	if (args->data_len == 0) {
5681 		*cs->statusp = resp->status = NFS4_OK;
5682 		resp->count = 0;
5683 		resp->committed = args->stable;
5684 		resp->writeverf = nsrv4->write4verf;
5685 		goto out;
5686 	}
5687 
5688 	if (args->mblk != NULL) {
5689 		mblk_t *m;
5690 		uint_t bytes, round_len;
5691 
5692 		iovcnt = 0;
5693 		bytes = 0;
5694 		round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
5695 		for (m = args->mblk;
5696 		    m != NULL && bytes < round_len;
5697 		    m = m->b_cont) {
5698 			iovcnt++;
5699 			bytes += MBLKL(m);
5700 		}
5701 #ifdef DEBUG
5702 		/* should have ended on an mblk boundary */
5703 		if (bytes != round_len) {
5704 			printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
5705 			    bytes, round_len, args->data_len);
5706 			printf("args=%p, args->mblk=%p, m=%p", (void *)args,
5707 			    (void *)args->mblk, (void *)m);
5708 			ASSERT(bytes == round_len);
5709 		}
5710 #endif
5711 		if (iovcnt <= MAX_IOVECS) {
5712 			iovp = iov;
5713 		} else {
5714 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
5715 		}
5716 		mblk_to_iov(args->mblk, iovcnt, iovp);
5717 	} else if (args->rlist != NULL) {
5718 		iovcnt = 1;
5719 		iovp = iov;
5720 		iovp->iov_base = (char *)((args->rlist)->u.c_daddr3);
5721 		iovp->iov_len = args->data_len;
5722 	} else {
5723 		iovcnt = 1;
5724 		iovp = iov;
5725 		iovp->iov_base = args->data_val;
5726 		iovp->iov_len = args->data_len;
5727 	}
5728 
5729 	uio.uio_iov = iovp;
5730 	uio.uio_iovcnt = iovcnt;
5731 
5732 	uio.uio_segflg = UIO_SYSSPACE;
5733 	uio.uio_extflg = UIO_COPY_DEFAULT;
5734 	uio.uio_loffset = args->offset;
5735 	uio.uio_resid = args->data_len;
5736 	uio.uio_llimit = curproc->p_fsz_ctl;
5737 	rlimit = uio.uio_llimit - args->offset;
5738 	if (rlimit < (u_offset_t)uio.uio_resid)
5739 		uio.uio_resid = (int)rlimit;
5740 
5741 	if (args->stable == UNSTABLE4)
5742 		ioflag = 0;
5743 	else if (args->stable == FILE_SYNC4)
5744 		ioflag = FSYNC;
5745 	else if (args->stable == DATA_SYNC4)
5746 		ioflag = FDSYNC;
5747 	else {
5748 		if (iovp != iov)
5749 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
5750 		*cs->statusp = resp->status = NFS4ERR_INVAL;
5751 		goto out;
5752 	}
5753 
5754 	/*
5755 	 * We're changing creds because VM may fault and we need
5756 	 * the cred of the current thread to be used if quota
5757 	 * checking is enabled.
5758 	 */
5759 	savecred = curthread->t_cred;
5760 	curthread->t_cred = cr;
5761 	error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
5762 	curthread->t_cred = savecred;
5763 
5764 	if (iovp != iov)
5765 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
5766 
5767 	if (error) {
5768 		*cs->statusp = resp->status = puterrno4(error);
5769 		goto out;
5770 	}
5771 
5772 	*cs->statusp = resp->status = NFS4_OK;
5773 	resp->count = args->data_len - uio.uio_resid;
5774 
5775 	if (ioflag == 0)
5776 		resp->committed = UNSTABLE4;
5777 	else
5778 		resp->committed = FILE_SYNC4;
5779 
5780 	resp->writeverf = nsrv4->write4verf;
5781 
5782 out:
5783 	if (in_crit)
5784 		nbl_end_crit(vp);
5785 
5786 	DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5787 	    WRITE4res *, resp);
5788 }
5789 
5790 void
5791 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
5792     struct svc_req *req, cred_t *cr, int *rv)
5793 {
5794 	uint_t i;
5795 	struct compound_state cs;
5796 	nfs4_srv_t *nsrv4;
5797 	nfs_export_t *ne = nfs_get_export();
5798 
5799 	if (rv != NULL)
5800 		*rv = 0;
5801 	rfs4_init_compound_state(&cs);
5802 	/*
5803 	 * Form a reply tag by copying over the request tag.
5804 	 */
5805 	resp->tag.utf8string_len = args->tag.utf8string_len;
5806 	if (args->tag.utf8string_len != 0) {
5807 		resp->tag.utf8string_val =
5808 		    kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5809 		bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5810 		    resp->tag.utf8string_len);
5811 	} else {
5812 		resp->tag.utf8string_val = NULL;
5813 	}
5814 
5815 	cs.statusp = &resp->status;
5816 	cs.req = req;
5817 	resp->array = NULL;
5818 	resp->array_len = 0;
5819 
5820 	/*
5821 	 * XXX for now, minorversion should be zero
5822 	 */
5823 	if (args->minorversion != NFS4_MINORVERSION) {
5824 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5825 		    &cs, COMPOUND4args *, args);
5826 		resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5827 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5828 		    &cs, COMPOUND4res *, resp);
5829 		return;
5830 	}
5831 
5832 	if (args->array_len == 0) {
5833 		resp->status = NFS4_OK;
5834 		return;
5835 	}
5836 
5837 	ASSERT(exi == NULL);
5838 	ASSERT(cr == NULL);
5839 
5840 	cr = svc_xprt_cred(req->rq_xprt);
5841 	ASSERT(cr != NULL);
5842 
5843 	if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5844 		DTRACE_NFSV4_2(compound__start, struct compound_state *,
5845 		    &cs, COMPOUND4args *, args);
5846 		DTRACE_NFSV4_2(compound__done, struct compound_state *,
5847 		    &cs, COMPOUND4res *, resp);
5848 		svcerr_badcred(req->rq_xprt);
5849 		if (rv != NULL)
5850 			*rv = 1;
5851 		return;
5852 	}
5853 	resp->array_len = args->array_len;
5854 	resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5855 	    KM_SLEEP);
5856 
5857 	cs.basecr = cr;
5858 	nsrv4 = nfs4_get_srv();
5859 
5860 	DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5861 	    COMPOUND4args *, args);
5862 
5863 	/*
5864 	 * For now, NFS4 compound processing must be protected by
5865 	 * exported_lock because it can access more than one exportinfo
5866 	 * per compound and share/unshare can now change multiple
5867 	 * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5868 	 * per proc (excluding public exinfo), and exi_count design
5869 	 * is sufficient to protect concurrent execution of NFS2/3
5870 	 * ops along with unexport.  This lock will be removed as
5871 	 * part of the NFSv4 phase 2 namespace redesign work.
5872 	 */
5873 	rw_enter(&ne->exported_lock, RW_READER);
5874 
5875 	/*
5876 	 * If this is the first compound we've seen, we need to start all
5877 	 * new instances' grace periods.
5878 	 */
5879 	if (nsrv4->seen_first_compound == 0) {
5880 		rfs4_grace_start_new(nsrv4);
5881 		/*
5882 		 * This must be set after rfs4_grace_start_new(), otherwise
5883 		 * another thread could proceed past here before the former
5884 		 * is finished.
5885 		 */
5886 		nsrv4->seen_first_compound = 1;
5887 	}
5888 
5889 	for (i = 0; i < args->array_len && cs.cont; i++) {
5890 		nfs_argop4 *argop;
5891 		nfs_resop4 *resop;
5892 		uint_t op;
5893 		kstat_named_t *stat = ne->ne_globals->rfsproccnt[NFS_V4];
5894 
5895 		argop = &args->array[i];
5896 		resop = &resp->array[i];
5897 		resop->resop = argop->argop;
5898 		op = (uint_t)resop->resop;
5899 
5900 		if (op < rfsv4disp_cnt) {
5901 			/*
5902 			 * Count the individual ops here; NULL and COMPOUND
5903 			 * are counted in common_dispatch()
5904 			 */
5905 			stat[op].value.ui64++;
5906 
5907 			NFS4_DEBUG(rfs4_debug > 1,
5908 			    (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5909 			(*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5910 			NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5911 			    rfs4_op_string[op], *cs.statusp));
5912 			if (*cs.statusp != NFS4_OK)
5913 				cs.cont = FALSE;
5914 		} else {
5915 			/*
5916 			 * This is effectively dead code since XDR code
5917 			 * will have already returned BADXDR if op doesn't
5918 			 * decode to legal value.  This only done for a
5919 			 * day when XDR code doesn't verify v4 opcodes.
5920 			 */
5921 			op = OP_ILLEGAL;
5922 			stat[OP_ILLEGAL_IDX].value.ui64++;
5923 
5924 			rfs4_op_illegal(argop, resop, req, &cs);
5925 			cs.cont = FALSE;
5926 		}
5927 
5928 		/*
5929 		 * If not at last op, and if we are to stop, then
5930 		 * compact the results array.
5931 		 */
5932 		if ((i + 1) < args->array_len && !cs.cont) {
5933 			nfs_resop4 *new_res = kmem_alloc(
5934 			    (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5935 			bcopy(resp->array,
5936 			    new_res, (i+1) * sizeof (nfs_resop4));
5937 			kmem_free(resp->array,
5938 			    args->array_len * sizeof (nfs_resop4));
5939 
5940 			resp->array_len =  i + 1;
5941 			resp->array = new_res;
5942 		}
5943 	}
5944 
5945 	rw_exit(&ne->exported_lock);
5946 
5947 	/*
5948 	 * clear exportinfo and vnode fields from compound_state before dtrace
5949 	 * probe, to avoid tracing residual values for path and share path.
5950 	 */
5951 	if (cs.vp)
5952 		VN_RELE(cs.vp);
5953 	if (cs.saved_vp)
5954 		VN_RELE(cs.saved_vp);
5955 	cs.exi = cs.saved_exi = NULL;
5956 	cs.vp = cs.saved_vp = NULL;
5957 
5958 	DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5959 	    COMPOUND4res *, resp);
5960 
5961 	if (cs.saved_fh.nfs_fh4_val)
5962 		kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5963 
5964 	if (cs.cr)
5965 		crfree(cs.cr);
5966 	/*
5967 	 * done with this compound request, free the label
5968 	 */
5969 
5970 	if (req->rq_label != NULL) {
5971 		kmem_free(req->rq_label, sizeof (bslabel_t));
5972 		req->rq_label = NULL;
5973 	}
5974 }
5975 
5976 /*
5977  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5978  * XXX zero out the tag and array values. Need to investigate why the
5979  * XXX calls occur, but at least prevent the panic for now.
5980  */
5981 void
5982 rfs4_compound_free(COMPOUND4res *resp)
5983 {
5984 	uint_t i;
5985 
5986 	if (resp->tag.utf8string_val) {
5987 		UTF8STRING_FREE(resp->tag)
5988 	}
5989 
5990 	for (i = 0; i < resp->array_len; i++) {
5991 		nfs_resop4 *resop;
5992 		uint_t op;
5993 
5994 		resop = &resp->array[i];
5995 		op = (uint_t)resop->resop;
5996 		if (op < rfsv4disp_cnt) {
5997 			(*rfsv4disptab[op].dis_resfree)(resop);
5998 		}
5999 	}
6000 	if (resp->array != NULL) {
6001 		kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
6002 	}
6003 }
6004 
6005 /*
6006  * Process the value of the compound request rpc flags, as a bit-AND
6007  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
6008  */
6009 void
6010 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
6011 {
6012 	int i;
6013 	int flag = RPC_ALL;
6014 
6015 	for (i = 0; flag && i < args->array_len; i++) {
6016 		uint_t op;
6017 
6018 		op = (uint_t)args->array[i].argop;
6019 
6020 		if (op < rfsv4disp_cnt)
6021 			flag &= rfsv4disptab[op].dis_flags;
6022 		else
6023 			flag = 0;
6024 	}
6025 	*flagp = flag;
6026 }
6027 
6028 nfsstat4
6029 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
6030 {
6031 	nfsstat4 e;
6032 
6033 	rfs4_dbe_lock(cp->rc_dbe);
6034 
6035 	if (cp->rc_sysidt != LM_NOSYSID) {
6036 		*sp = cp->rc_sysidt;
6037 		e = NFS4_OK;
6038 
6039 	} else if ((cp->rc_sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
6040 		*sp = cp->rc_sysidt;
6041 		e = NFS4_OK;
6042 
6043 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6044 		    "rfs4_client_sysid: allocated 0x%x\n", *sp));
6045 	} else
6046 		e = NFS4ERR_DELAY;
6047 
6048 	rfs4_dbe_unlock(cp->rc_dbe);
6049 	return (e);
6050 }
6051 
6052 #if defined(DEBUG) && ! defined(lint)
6053 static void lock_print(char *str, int operation, struct flock64 *flk)
6054 {
6055 	char *op, *type;
6056 
6057 	switch (operation) {
6058 	case F_GETLK: op = "F_GETLK";
6059 		break;
6060 	case F_SETLK: op = "F_SETLK";
6061 		break;
6062 	case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
6063 		break;
6064 	default: op = "F_UNKNOWN";
6065 		break;
6066 	}
6067 	switch (flk->l_type) {
6068 	case F_UNLCK: type = "F_UNLCK";
6069 		break;
6070 	case F_RDLCK: type = "F_RDLCK";
6071 		break;
6072 	case F_WRLCK: type = "F_WRLCK";
6073 		break;
6074 	default: type = "F_UNKNOWN";
6075 		break;
6076 	}
6077 
6078 	ASSERT(flk->l_whence == 0);
6079 	cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
6080 	    str, op, type, (longlong_t)flk->l_start,
6081 	    flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
6082 }
6083 
6084 #define	LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
6085 #else
6086 #define	LOCK_PRINT(d, s, t, f)
6087 #endif
6088 
6089 /*ARGSUSED*/
6090 static bool_t
6091 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
6092 {
6093 	return (TRUE);
6094 }
6095 
6096 /*
6097  * Look up the pathname using the vp in cs as the directory vnode.
6098  * cs->vp will be the vnode for the file on success
6099  */
6100 
6101 static nfsstat4
6102 rfs4_lookup(component4 *component, struct svc_req *req,
6103     struct compound_state *cs)
6104 {
6105 	char *nm;
6106 	uint32_t len;
6107 	nfsstat4 status;
6108 	struct sockaddr *ca;
6109 	char *name;
6110 
6111 	if (cs->vp == NULL) {
6112 		return (NFS4ERR_NOFILEHANDLE);
6113 	}
6114 	if (cs->vp->v_type != VDIR) {
6115 		return (NFS4ERR_NOTDIR);
6116 	}
6117 
6118 	status = utf8_dir_verify(component);
6119 	if (status != NFS4_OK)
6120 		return (status);
6121 
6122 	nm = utf8_to_fn(component, &len, NULL);
6123 	if (nm == NULL) {
6124 		return (NFS4ERR_INVAL);
6125 	}
6126 
6127 	if (len > MAXNAMELEN) {
6128 		kmem_free(nm, len);
6129 		return (NFS4ERR_NAMETOOLONG);
6130 	}
6131 
6132 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6133 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6134 	    MAXPATHLEN + 1);
6135 
6136 	if (name == NULL) {
6137 		kmem_free(nm, len);
6138 		return (NFS4ERR_INVAL);
6139 	}
6140 
6141 	status = do_rfs4_op_lookup(name, req, cs);
6142 
6143 	if (name != nm)
6144 		kmem_free(name, MAXPATHLEN + 1);
6145 
6146 	kmem_free(nm, len);
6147 
6148 	return (status);
6149 }
6150 
6151 static nfsstat4
6152 rfs4_lookupfile(component4 *component, struct svc_req *req,
6153     struct compound_state *cs, uint32_t access, change_info4 *cinfo)
6154 {
6155 	nfsstat4 status;
6156 	vnode_t *dvp = cs->vp;
6157 	vattr_t bva, ava, fva;
6158 	int error;
6159 
6160 	/* Get "before" change value */
6161 	bva.va_mask = AT_CTIME|AT_SEQ;
6162 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6163 	if (error)
6164 		return (puterrno4(error));
6165 
6166 	/* rfs4_lookup may VN_RELE directory */
6167 	VN_HOLD(dvp);
6168 
6169 	status = rfs4_lookup(component, req, cs);
6170 	if (status != NFS4_OK) {
6171 		VN_RELE(dvp);
6172 		return (status);
6173 	}
6174 
6175 	/*
6176 	 * Get "after" change value, if it fails, simply return the
6177 	 * before value.
6178 	 */
6179 	ava.va_mask = AT_CTIME|AT_SEQ;
6180 	if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6181 		ava.va_ctime = bva.va_ctime;
6182 		ava.va_seq = 0;
6183 	}
6184 	VN_RELE(dvp);
6185 
6186 	/*
6187 	 * Validate the file is a file
6188 	 */
6189 	fva.va_mask = AT_TYPE|AT_MODE;
6190 	error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
6191 	if (error)
6192 		return (puterrno4(error));
6193 
6194 	if (fva.va_type != VREG) {
6195 		if (fva.va_type == VDIR)
6196 			return (NFS4ERR_ISDIR);
6197 		if (fva.va_type == VLNK)
6198 			return (NFS4ERR_SYMLINK);
6199 		return (NFS4ERR_INVAL);
6200 	}
6201 
6202 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
6203 	NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6204 
6205 	/*
6206 	 * It is undefined if VOP_LOOKUP will change va_seq, so
6207 	 * cinfo.atomic = TRUE only if we have
6208 	 * non-zero va_seq's, and they have not changed.
6209 	 */
6210 	if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
6211 		cinfo->atomic = TRUE;
6212 	else
6213 		cinfo->atomic = FALSE;
6214 
6215 	/* Check for mandatory locking */
6216 	cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
6217 	return (check_open_access(access, cs, req));
6218 }
6219 
6220 static nfsstat4
6221 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
6222     cred_t *cr, vnode_t **vpp, bool_t *created)
6223 {
6224 	int error;
6225 	nfsstat4 status = NFS4_OK;
6226 	vattr_t va;
6227 
6228 tryagain:
6229 
6230 	/*
6231 	 * The file open mode used is VWRITE.  If the client needs
6232 	 * some other semantic, then it should do the access checking
6233 	 * itself.  It would have been nice to have the file open mode
6234 	 * passed as part of the arguments.
6235 	 */
6236 
6237 	*created = TRUE;
6238 	error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
6239 
6240 	if (error) {
6241 		*created = FALSE;
6242 
6243 		/*
6244 		 * If we got something other than file already exists
6245 		 * then just return this error.  Otherwise, we got
6246 		 * EEXIST.  If we were doing a GUARDED create, then
6247 		 * just return this error.  Otherwise, we need to
6248 		 * make sure that this wasn't a duplicate of an
6249 		 * exclusive create request.
6250 		 *
6251 		 * The assumption is made that a non-exclusive create
6252 		 * request will never return EEXIST.
6253 		 */
6254 
6255 		if (error != EEXIST || mode == GUARDED4) {
6256 			status = puterrno4(error);
6257 			return (status);
6258 		}
6259 		error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
6260 		    NULL, NULL, NULL);
6261 
6262 		if (error) {
6263 			/*
6264 			 * We couldn't find the file that we thought that
6265 			 * we just created.  So, we'll just try creating
6266 			 * it again.
6267 			 */
6268 			if (error == ENOENT)
6269 				goto tryagain;
6270 
6271 			status = puterrno4(error);
6272 			return (status);
6273 		}
6274 
6275 		if (mode == UNCHECKED4) {
6276 			/* existing object must be regular file */
6277 			if ((*vpp)->v_type != VREG) {
6278 				if ((*vpp)->v_type == VDIR)
6279 					status = NFS4ERR_ISDIR;
6280 				else if ((*vpp)->v_type == VLNK)
6281 					status = NFS4ERR_SYMLINK;
6282 				else
6283 					status = NFS4ERR_INVAL;
6284 				VN_RELE(*vpp);
6285 				return (status);
6286 			}
6287 
6288 			return (NFS4_OK);
6289 		}
6290 
6291 		/* Check for duplicate request */
6292 		va.va_mask = AT_MTIME;
6293 		error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
6294 		if (!error) {
6295 			/* We found the file */
6296 			const timestruc_t *mtime = &vap->va_mtime;
6297 
6298 			if (va.va_mtime.tv_sec != mtime->tv_sec ||
6299 			    va.va_mtime.tv_nsec != mtime->tv_nsec) {
6300 				/* but its not our creation */
6301 				VN_RELE(*vpp);
6302 				return (NFS4ERR_EXIST);
6303 			}
6304 			*created = TRUE; /* retrans of create == created */
6305 			return (NFS4_OK);
6306 		}
6307 		VN_RELE(*vpp);
6308 		return (NFS4ERR_EXIST);
6309 	}
6310 
6311 	return (NFS4_OK);
6312 }
6313 
6314 static nfsstat4
6315 check_open_access(uint32_t access, struct compound_state *cs,
6316     struct svc_req *req)
6317 {
6318 	int error;
6319 	vnode_t *vp;
6320 	bool_t readonly;
6321 	cred_t *cr = cs->cr;
6322 
6323 	/* For now we don't allow mandatory locking as per V2/V3 */
6324 	if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
6325 		return (NFS4ERR_ACCESS);
6326 	}
6327 
6328 	vp = cs->vp;
6329 	ASSERT(cr != NULL && vp->v_type == VREG);
6330 
6331 	/*
6332 	 * If the file system is exported read only and we are trying
6333 	 * to open for write, then return NFS4ERR_ROFS
6334 	 */
6335 
6336 	readonly = rdonly4(req, cs);
6337 
6338 	if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
6339 		return (NFS4ERR_ROFS);
6340 
6341 	if (access & OPEN4_SHARE_ACCESS_READ) {
6342 		if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
6343 		    (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
6344 			return (NFS4ERR_ACCESS);
6345 		}
6346 	}
6347 
6348 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
6349 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
6350 		if (error)
6351 			return (NFS4ERR_ACCESS);
6352 	}
6353 
6354 	return (NFS4_OK);
6355 }
6356 
6357 static nfsstat4
6358 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
6359     change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
6360 {
6361 	struct nfs4_svgetit_arg sarg;
6362 	struct nfs4_ntov_table ntov;
6363 
6364 	bool_t ntov_table_init = FALSE;
6365 	struct statvfs64 sb;
6366 	nfsstat4 status;
6367 	vnode_t *vp;
6368 	vattr_t bva, ava, iva, cva, *vap;
6369 	vnode_t *dvp;
6370 	timespec32_t *mtime;
6371 	char *nm = NULL;
6372 	uint_t buflen;
6373 	bool_t created;
6374 	bool_t setsize = FALSE;
6375 	len_t reqsize;
6376 	int error;
6377 	bool_t trunc;
6378 	caller_context_t ct;
6379 	component4 *component;
6380 	bslabel_t *clabel;
6381 	struct sockaddr *ca;
6382 	char *name = NULL;
6383 
6384 	sarg.sbp = &sb;
6385 	sarg.is_referral = B_FALSE;
6386 
6387 	dvp = cs->vp;
6388 
6389 	/* Check if the file system is read only */
6390 	if (rdonly4(req, cs))
6391 		return (NFS4ERR_ROFS);
6392 
6393 	/* check the label of including directory */
6394 	if (is_system_labeled()) {
6395 		ASSERT(req->rq_label != NULL);
6396 		clabel = req->rq_label;
6397 		DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6398 		    "got client label from request(1)",
6399 		    struct svc_req *, req);
6400 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
6401 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
6402 			    cs->exi)) {
6403 				return (NFS4ERR_ACCESS);
6404 			}
6405 		}
6406 	}
6407 
6408 	/*
6409 	 * Get the last component of path name in nm. cs will reference
6410 	 * the including directory on success.
6411 	 */
6412 	component = &args->open_claim4_u.file;
6413 	status = utf8_dir_verify(component);
6414 	if (status != NFS4_OK)
6415 		return (status);
6416 
6417 	nm = utf8_to_fn(component, &buflen, NULL);
6418 
6419 	if (nm == NULL)
6420 		return (NFS4ERR_RESOURCE);
6421 
6422 	if (buflen > MAXNAMELEN) {
6423 		kmem_free(nm, buflen);
6424 		return (NFS4ERR_NAMETOOLONG);
6425 	}
6426 
6427 	bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6428 	error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6429 	if (error) {
6430 		kmem_free(nm, buflen);
6431 		return (puterrno4(error));
6432 	}
6433 
6434 	if (bva.va_type != VDIR) {
6435 		kmem_free(nm, buflen);
6436 		return (NFS4ERR_NOTDIR);
6437 	}
6438 
6439 	NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6440 
6441 	switch (args->mode) {
6442 	case GUARDED4:
6443 		/*FALLTHROUGH*/
6444 	case UNCHECKED4:
6445 		nfs4_ntov_table_init(&ntov);
6446 		ntov_table_init = TRUE;
6447 
6448 		*attrset = 0;
6449 		status = do_rfs4_set_attrs(attrset,
6450 		    &args->createhow4_u.createattrs,
6451 		    cs, &sarg, &ntov, NFS4ATTR_SETIT);
6452 
6453 		if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6454 		    sarg.vap->va_type != VREG) {
6455 			if (sarg.vap->va_type == VDIR)
6456 				status = NFS4ERR_ISDIR;
6457 			else if (sarg.vap->va_type == VLNK)
6458 				status = NFS4ERR_SYMLINK;
6459 			else
6460 				status = NFS4ERR_INVAL;
6461 		}
6462 
6463 		if (status != NFS4_OK) {
6464 			kmem_free(nm, buflen);
6465 			nfs4_ntov_table_free(&ntov, &sarg);
6466 			*attrset = 0;
6467 			return (status);
6468 		}
6469 
6470 		vap = sarg.vap;
6471 		vap->va_type = VREG;
6472 		vap->va_mask |= AT_TYPE;
6473 
6474 		if ((vap->va_mask & AT_MODE) == 0) {
6475 			vap->va_mask |= AT_MODE;
6476 			vap->va_mode = (mode_t)0600;
6477 		}
6478 
6479 		if (vap->va_mask & AT_SIZE) {
6480 
6481 			/* Disallow create with a non-zero size */
6482 
6483 			if ((reqsize = sarg.vap->va_size) != 0) {
6484 				kmem_free(nm, buflen);
6485 				nfs4_ntov_table_free(&ntov, &sarg);
6486 				*attrset = 0;
6487 				return (NFS4ERR_INVAL);
6488 			}
6489 			setsize = TRUE;
6490 		}
6491 		break;
6492 
6493 	case EXCLUSIVE4:
6494 		/* prohibit EXCL create of named attributes */
6495 		if (dvp->v_flag & V_XATTRDIR) {
6496 			kmem_free(nm, buflen);
6497 			*attrset = 0;
6498 			return (NFS4ERR_INVAL);
6499 		}
6500 
6501 		cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6502 		cva.va_type = VREG;
6503 		/*
6504 		 * Ensure no time overflows. Assumes underlying
6505 		 * filesystem supports at least 32 bits.
6506 		 * Truncate nsec to usec resolution to allow valid
6507 		 * compares even if the underlying filesystem truncates.
6508 		 */
6509 		mtime = (timespec32_t *)&args->createhow4_u.createverf;
6510 		cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6511 		cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6512 		cva.va_mode = (mode_t)0;
6513 		vap = &cva;
6514 
6515 		/*
6516 		 * For EXCL create, attrset is set to the server attr
6517 		 * used to cache the client's verifier.
6518 		 */
6519 		*attrset = FATTR4_TIME_MODIFY_MASK;
6520 		break;
6521 	}
6522 
6523 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6524 	name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6525 	    MAXPATHLEN  + 1);
6526 
6527 	if (name == NULL) {
6528 		kmem_free(nm, buflen);
6529 		return (NFS4ERR_SERVERFAULT);
6530 	}
6531 
6532 	status = create_vnode(dvp, name, vap, args->mode,
6533 	    cs->cr, &vp, &created);
6534 	if (nm != name)
6535 		kmem_free(name, MAXPATHLEN + 1);
6536 	kmem_free(nm, buflen);
6537 
6538 	if (status != NFS4_OK) {
6539 		if (ntov_table_init)
6540 			nfs4_ntov_table_free(&ntov, &sarg);
6541 		*attrset = 0;
6542 		return (status);
6543 	}
6544 
6545 	trunc = (setsize && !created);
6546 
6547 	if (args->mode != EXCLUSIVE4) {
6548 		bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6549 
6550 		/*
6551 		 * True verification that object was created with correct
6552 		 * attrs is impossible.  The attrs could have been changed
6553 		 * immediately after object creation.  If attributes did
6554 		 * not verify, the only recourse for the server is to
6555 		 * destroy the object.  Maybe if some attrs (like gid)
6556 		 * are set incorrectly, the object should be destroyed;
6557 		 * however, seems bad as a default policy.  Do we really
6558 		 * want to destroy an object over one of the times not
6559 		 * verifying correctly?  For these reasons, the server
6560 		 * currently sets bits in attrset for createattrs
6561 		 * that were set; however, no verification is done.
6562 		 *
6563 		 * vmask_to_nmask accounts for vattr bits set on create
6564 		 *	[do_rfs4_set_attrs() only sets resp bits for
6565 		 *	 non-vattr/vfs bits.]
6566 		 * Mask off any bits we set by default so as not to return
6567 		 * more attrset bits than were requested in createattrs
6568 		 */
6569 		if (created) {
6570 			nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6571 			*attrset &= createmask;
6572 		} else {
6573 			/*
6574 			 * We did not create the vnode (we tried but it
6575 			 * already existed).  In this case, the only createattr
6576 			 * that the spec allows the server to set is size,
6577 			 * and even then, it can only be set if it is 0.
6578 			 */
6579 			*attrset = 0;
6580 			if (trunc)
6581 				*attrset = FATTR4_SIZE_MASK;
6582 		}
6583 	}
6584 	if (ntov_table_init)
6585 		nfs4_ntov_table_free(&ntov, &sarg);
6586 
6587 	/*
6588 	 * Get the initial "after" sequence number, if it fails,
6589 	 * set to zero, time to before.
6590 	 */
6591 	iva.va_mask = AT_CTIME|AT_SEQ;
6592 	if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6593 		iva.va_seq = 0;
6594 		iva.va_ctime = bva.va_ctime;
6595 	}
6596 
6597 	/*
6598 	 * create_vnode attempts to create the file exclusive,
6599 	 * if it already exists the VOP_CREATE will fail and
6600 	 * may not increase va_seq. It is atomic if
6601 	 * we haven't changed the directory, but if it has changed
6602 	 * we don't know what changed it.
6603 	 */
6604 	if (!created) {
6605 		if (bva.va_seq && iva.va_seq &&
6606 		    bva.va_seq == iva.va_seq)
6607 			cinfo->atomic = TRUE;
6608 		else
6609 			cinfo->atomic = FALSE;
6610 		NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6611 	} else {
6612 		/*
6613 		 * The entry was created, we need to sync the
6614 		 * directory metadata.
6615 		 */
6616 		(void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6617 
6618 		/*
6619 		 * Get "after" change value, if it fails, simply return the
6620 		 * before value.
6621 		 */
6622 		ava.va_mask = AT_CTIME|AT_SEQ;
6623 		if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6624 			ava.va_ctime = bva.va_ctime;
6625 			ava.va_seq = 0;
6626 		}
6627 
6628 		NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6629 
6630 		/*
6631 		 * The cinfo->atomic = TRUE only if we have
6632 		 * non-zero va_seq's, and it has incremented by exactly one
6633 		 * during the create_vnode and it didn't
6634 		 * change during the VOP_FSYNC.
6635 		 */
6636 		if (bva.va_seq && iva.va_seq && ava.va_seq &&
6637 		    iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6638 			cinfo->atomic = TRUE;
6639 		else
6640 			cinfo->atomic = FALSE;
6641 	}
6642 
6643 	/* Check for mandatory locking and that the size gets set. */
6644 	cva.va_mask = AT_MODE;
6645 	if (setsize)
6646 		cva.va_mask |= AT_SIZE;
6647 
6648 	/* Assume the worst */
6649 	cs->mandlock = TRUE;
6650 
6651 	if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6652 		cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6653 
6654 		/*
6655 		 * Truncate the file if necessary; this would be
6656 		 * the case for create over an existing file.
6657 		 */
6658 
6659 		if (trunc) {
6660 			int in_crit = 0;
6661 			rfs4_file_t *fp;
6662 			nfs4_srv_t *nsrv4;
6663 			bool_t create = FALSE;
6664 
6665 			/*
6666 			 * We are writing over an existing file.
6667 			 * Check to see if we need to recall a delegation.
6668 			 */
6669 			nsrv4 = nfs4_get_srv();
6670 			rfs4_hold_deleg_policy(nsrv4);
6671 			if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6672 				if (rfs4_check_delegated_byfp(FWRITE, fp,
6673 				    (reqsize == 0), FALSE, FALSE, &clientid)) {
6674 					rfs4_file_rele(fp);
6675 					rfs4_rele_deleg_policy(nsrv4);
6676 					VN_RELE(vp);
6677 					*attrset = 0;
6678 					return (NFS4ERR_DELAY);
6679 				}
6680 				rfs4_file_rele(fp);
6681 			}
6682 			rfs4_rele_deleg_policy(nsrv4);
6683 
6684 			if (nbl_need_check(vp)) {
6685 				in_crit = 1;
6686 
6687 				ASSERT(reqsize == 0);
6688 
6689 				nbl_start_crit(vp, RW_READER);
6690 				if (nbl_conflict(vp, NBL_WRITE, 0,
6691 				    cva.va_size, 0, NULL)) {
6692 					in_crit = 0;
6693 					nbl_end_crit(vp);
6694 					VN_RELE(vp);
6695 					*attrset = 0;
6696 					return (NFS4ERR_ACCESS);
6697 				}
6698 			}
6699 			ct.cc_sysid = 0;
6700 			ct.cc_pid = 0;
6701 			ct.cc_caller_id = nfs4_srv_caller_id;
6702 			ct.cc_flags = CC_DONTBLOCK;
6703 
6704 			cva.va_mask = AT_SIZE;
6705 			cva.va_size = reqsize;
6706 			(void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6707 			if (in_crit)
6708 				nbl_end_crit(vp);
6709 		}
6710 	}
6711 
6712 	error = makefh4(&cs->fh, vp, cs->exi);
6713 
6714 	/*
6715 	 * Force modified data and metadata out to stable storage.
6716 	 */
6717 	(void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6718 
6719 	if (error) {
6720 		VN_RELE(vp);
6721 		*attrset = 0;
6722 		return (puterrno4(error));
6723 	}
6724 
6725 	/* if parent dir is attrdir, set namedattr fh flag */
6726 	if (dvp->v_flag & V_XATTRDIR)
6727 		set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6728 
6729 	if (cs->vp)
6730 		VN_RELE(cs->vp);
6731 
6732 	cs->vp = vp;
6733 
6734 	/*
6735 	 * if we did not create the file, we will need to check
6736 	 * the access bits on the file
6737 	 */
6738 
6739 	if (!created) {
6740 		if (setsize)
6741 			args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6742 		status = check_open_access(args->share_access, cs, req);
6743 		if (status != NFS4_OK)
6744 			*attrset = 0;
6745 	}
6746 	return (status);
6747 }
6748 
6749 /*ARGSUSED*/
6750 static void
6751 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6752     rfs4_openowner_t *oo, delegreq_t deleg,
6753     uint32_t access, uint32_t deny,
6754     OPEN4res *resp, int deleg_cur)
6755 {
6756 	/* XXX Currently not using req  */
6757 	rfs4_state_t *sp;
6758 	rfs4_file_t *fp;
6759 	bool_t screate = TRUE;
6760 	bool_t fcreate = TRUE;
6761 	uint32_t open_a, share_a;
6762 	uint32_t open_d, share_d;
6763 	rfs4_deleg_state_t *dsp;
6764 	sysid_t sysid;
6765 	nfsstat4 status;
6766 	caller_context_t ct;
6767 	int fflags = 0;
6768 	int recall = 0;
6769 	int err;
6770 	int first_open;
6771 
6772 	/* get the file struct and hold a lock on it during initial open */
6773 	fp = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6774 	if (fp == NULL) {
6775 		resp->status = NFS4ERR_RESOURCE;
6776 		DTRACE_PROBE1(nfss__e__do__open1, nfsstat4, resp->status);
6777 		return;
6778 	}
6779 
6780 	sp = rfs4_findstate_by_owner_file(oo, fp, &screate);
6781 	if (sp == NULL) {
6782 		resp->status = NFS4ERR_RESOURCE;
6783 		DTRACE_PROBE1(nfss__e__do__open2, nfsstat4, resp->status);
6784 		/* No need to keep any reference */
6785 		rw_exit(&fp->rf_file_rwlock);
6786 		rfs4_file_rele(fp);
6787 		return;
6788 	}
6789 
6790 	/* try to get the sysid before continuing */
6791 	if ((status = rfs4_client_sysid(oo->ro_client, &sysid)) != NFS4_OK) {
6792 		resp->status = status;
6793 		rfs4_file_rele(fp);
6794 		/* Not a fully formed open; "close" it */
6795 		if (screate == TRUE)
6796 			rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6797 		rfs4_state_rele(sp);
6798 		return;
6799 	}
6800 
6801 	/* Calculate the fflags for this OPEN. */
6802 	if (access & OPEN4_SHARE_ACCESS_READ)
6803 		fflags |= FREAD;
6804 	if (access & OPEN4_SHARE_ACCESS_WRITE)
6805 		fflags |= FWRITE;
6806 
6807 	rfs4_dbe_lock(sp->rs_dbe);
6808 
6809 	/*
6810 	 * Calculate the new deny and access mode that this open is adding to
6811 	 * the file for this open owner;
6812 	 */
6813 	open_d = (deny & ~sp->rs_open_deny);
6814 	open_a = (access & ~sp->rs_open_access);
6815 
6816 	/*
6817 	 * Calculate the new share access and share deny modes that this open
6818 	 * is adding to the file for this open owner;
6819 	 */
6820 	share_a = (access & ~sp->rs_share_access);
6821 	share_d = (deny & ~sp->rs_share_deny);
6822 
6823 	first_open = (sp->rs_open_access & OPEN4_SHARE_ACCESS_BOTH) == 0;
6824 
6825 	/*
6826 	 * Check to see the client has already sent an open for this
6827 	 * open owner on this file with the same share/deny modes.
6828 	 * If so, we don't need to check for a conflict and we don't
6829 	 * need to add another shrlock.  If not, then we need to
6830 	 * check for conflicts in deny and access before checking for
6831 	 * conflicts in delegation.  We don't want to recall a
6832 	 * delegation based on an open that will eventually fail based
6833 	 * on shares modes.
6834 	 */
6835 
6836 	if (share_a || share_d) {
6837 		if ((err = rfs4_share(sp, access, deny)) != 0) {
6838 			rfs4_dbe_unlock(sp->rs_dbe);
6839 			resp->status = err;
6840 
6841 			rfs4_file_rele(fp);
6842 			/* Not a fully formed open; "close" it */
6843 			if (screate == TRUE)
6844 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6845 			rfs4_state_rele(sp);
6846 			return;
6847 		}
6848 	}
6849 
6850 	rfs4_dbe_lock(fp->rf_dbe);
6851 
6852 	/*
6853 	 * Check to see if this file is delegated and if so, if a
6854 	 * recall needs to be done.
6855 	 */
6856 	if (rfs4_check_recall(sp, access)) {
6857 		rfs4_dbe_unlock(fp->rf_dbe);
6858 		rfs4_dbe_unlock(sp->rs_dbe);
6859 		rfs4_recall_deleg(fp, FALSE, sp->rs_owner->ro_client);
6860 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6861 		rfs4_dbe_lock(sp->rs_dbe);
6862 
6863 		/* if state closed while lock was dropped */
6864 		if (sp->rs_closed) {
6865 			if (share_a || share_d)
6866 				(void) rfs4_unshare(sp);
6867 			rfs4_dbe_unlock(sp->rs_dbe);
6868 			rfs4_file_rele(fp);
6869 			/* Not a fully formed open; "close" it */
6870 			if (screate == TRUE)
6871 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6872 			rfs4_state_rele(sp);
6873 			resp->status = NFS4ERR_OLD_STATEID;
6874 			return;
6875 		}
6876 
6877 		rfs4_dbe_lock(fp->rf_dbe);
6878 		/* Let's see if the delegation was returned */
6879 		if (rfs4_check_recall(sp, access)) {
6880 			rfs4_dbe_unlock(fp->rf_dbe);
6881 			if (share_a || share_d)
6882 				(void) rfs4_unshare(sp);
6883 			rfs4_dbe_unlock(sp->rs_dbe);
6884 			rfs4_file_rele(fp);
6885 			rfs4_update_lease(sp->rs_owner->ro_client);
6886 
6887 			/* Not a fully formed open; "close" it */
6888 			if (screate == TRUE)
6889 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6890 			rfs4_state_rele(sp);
6891 			resp->status = NFS4ERR_DELAY;
6892 			return;
6893 		}
6894 	}
6895 	/*
6896 	 * the share check passed and any delegation conflict has been
6897 	 * taken care of, now call vop_open.
6898 	 * if this is the first open then call vop_open with fflags.
6899 	 * if not, call vn_open_upgrade with just the upgrade flags.
6900 	 *
6901 	 * if the file has been opened already, it will have the current
6902 	 * access mode in the state struct.  if it has no share access, then
6903 	 * this is a new open.
6904 	 *
6905 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6906 	 * call VOP_OPEN(), just do the open upgrade.
6907 	 */
6908 	if (first_open && !deleg_cur) {
6909 		ct.cc_sysid = sysid;
6910 		ct.cc_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
6911 		ct.cc_caller_id = nfs4_srv_caller_id;
6912 		ct.cc_flags = CC_DONTBLOCK;
6913 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6914 		if (err) {
6915 			rfs4_dbe_unlock(fp->rf_dbe);
6916 			if (share_a || share_d)
6917 				(void) rfs4_unshare(sp);
6918 			rfs4_dbe_unlock(sp->rs_dbe);
6919 			rfs4_file_rele(fp);
6920 
6921 			/* Not a fully formed open; "close" it */
6922 			if (screate == TRUE)
6923 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6924 			rfs4_state_rele(sp);
6925 			/* check if a monitor detected a delegation conflict */
6926 			if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6927 				resp->status = NFS4ERR_DELAY;
6928 			else
6929 				resp->status = NFS4ERR_SERVERFAULT;
6930 			return;
6931 		}
6932 	} else { /* open upgrade */
6933 		/*
6934 		 * calculate the fflags for the new mode that is being added
6935 		 * by this upgrade.
6936 		 */
6937 		fflags = 0;
6938 		if (open_a & OPEN4_SHARE_ACCESS_READ)
6939 			fflags |= FREAD;
6940 		if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6941 			fflags |= FWRITE;
6942 		vn_open_upgrade(cs->vp, fflags);
6943 	}
6944 	sp->rs_open_access |= access;
6945 	sp->rs_open_deny |= deny;
6946 
6947 	if (open_d & OPEN4_SHARE_DENY_READ)
6948 		fp->rf_deny_read++;
6949 	if (open_d & OPEN4_SHARE_DENY_WRITE)
6950 		fp->rf_deny_write++;
6951 	fp->rf_share_deny |= deny;
6952 
6953 	if (open_a & OPEN4_SHARE_ACCESS_READ)
6954 		fp->rf_access_read++;
6955 	if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6956 		fp->rf_access_write++;
6957 	fp->rf_share_access |= access;
6958 
6959 	/*
6960 	 * Check for delegation here. if the deleg argument is not
6961 	 * DELEG_ANY, then this is a reclaim from a client and
6962 	 * we must honor the delegation requested. If necessary we can
6963 	 * set the recall flag.
6964 	 */
6965 
6966 	dsp = rfs4_grant_delegation(deleg, sp, &recall);
6967 
6968 	cs->deleg = (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE);
6969 
6970 	next_stateid(&sp->rs_stateid);
6971 
6972 	resp->stateid = sp->rs_stateid.stateid;
6973 
6974 	rfs4_dbe_unlock(fp->rf_dbe);
6975 	rfs4_dbe_unlock(sp->rs_dbe);
6976 
6977 	if (dsp) {
6978 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6979 		rfs4_deleg_state_rele(dsp);
6980 	}
6981 
6982 	rfs4_file_rele(fp);
6983 	rfs4_state_rele(sp);
6984 
6985 	resp->status = NFS4_OK;
6986 }
6987 
6988 /*ARGSUSED*/
6989 static void
6990 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6991     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6992 {
6993 	change_info4 *cinfo = &resp->cinfo;
6994 	bitmap4 *attrset = &resp->attrset;
6995 
6996 	if (args->opentype == OPEN4_NOCREATE)
6997 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6998 		    req, cs, args->share_access, cinfo);
6999 	else {
7000 		/* inhibit delegation grants during exclusive create */
7001 
7002 		if (args->mode == EXCLUSIVE4)
7003 			rfs4_disable_delegation();
7004 
7005 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
7006 		    oo->ro_client->rc_clientid);
7007 	}
7008 
7009 	if (resp->status == NFS4_OK) {
7010 
7011 		/* cs->vp cs->fh now reference the desired file */
7012 
7013 		rfs4_do_open(cs, req, oo,
7014 		    oo->ro_need_confirm ? DELEG_NONE : DELEG_ANY,
7015 		    args->share_access, args->share_deny, resp, 0);
7016 
7017 		/*
7018 		 * If rfs4_createfile set attrset, we must
7019 		 * clear this attrset before the response is copied.
7020 		 */
7021 		if (resp->status != NFS4_OK && resp->attrset) {
7022 			resp->attrset = 0;
7023 		}
7024 	}
7025 	else
7026 		*cs->statusp = resp->status;
7027 
7028 	if (args->mode == EXCLUSIVE4)
7029 		rfs4_enable_delegation();
7030 }
7031 
7032 /*ARGSUSED*/
7033 static void
7034 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
7035     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7036 {
7037 	change_info4 *cinfo = &resp->cinfo;
7038 	vattr_t va;
7039 	vtype_t v_type = cs->vp->v_type;
7040 	int error = 0;
7041 
7042 	/* Verify that we have a regular file */
7043 	if (v_type != VREG) {
7044 		if (v_type == VDIR)
7045 			resp->status = NFS4ERR_ISDIR;
7046 		else if (v_type == VLNK)
7047 			resp->status = NFS4ERR_SYMLINK;
7048 		else
7049 			resp->status = NFS4ERR_INVAL;
7050 		return;
7051 	}
7052 
7053 	va.va_mask = AT_MODE|AT_UID;
7054 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
7055 	if (error) {
7056 		resp->status = puterrno4(error);
7057 		return;
7058 	}
7059 
7060 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
7061 
7062 	/*
7063 	 * Check if we have access to the file, Note the the file
7064 	 * could have originally been open UNCHECKED or GUARDED
7065 	 * with mode bits that will now fail, but there is nothing
7066 	 * we can really do about that except in the case that the
7067 	 * owner of the file is the one requesting the open.
7068 	 */
7069 	if (crgetuid(cs->cr) != va.va_uid) {
7070 		resp->status = check_open_access(args->share_access, cs, req);
7071 		if (resp->status != NFS4_OK) {
7072 			return;
7073 		}
7074 	}
7075 
7076 	/*
7077 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
7078 	 */
7079 	cinfo->before = 0;
7080 	cinfo->after = 0;
7081 	cinfo->atomic = FALSE;
7082 
7083 	rfs4_do_open(cs, req, oo,
7084 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
7085 	    args->share_access, args->share_deny, resp, 0);
7086 }
7087 
7088 static void
7089 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
7090     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7091 {
7092 	int error;
7093 	nfsstat4 status;
7094 	stateid4 stateid =
7095 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
7096 	rfs4_deleg_state_t *dsp;
7097 
7098 	/*
7099 	 * Find the state info from the stateid and confirm that the
7100 	 * file is delegated.  If the state openowner is the same as
7101 	 * the supplied openowner we're done. If not, get the file
7102 	 * info from the found state info. Use that file info to
7103 	 * create the state for this lock owner. Note solaris doen't
7104 	 * really need the pathname to find the file. We may want to
7105 	 * lookup the pathname and make sure that the vp exist and
7106 	 * matches the vp in the file structure. However it is
7107 	 * possible that the pathname nolonger exists (local process
7108 	 * unlinks the file), so this may not be that useful.
7109 	 */
7110 
7111 	status = rfs4_get_deleg_state(&stateid, &dsp);
7112 	if (status != NFS4_OK) {
7113 		resp->status = status;
7114 		return;
7115 	}
7116 
7117 	ASSERT(dsp->rds_finfo->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE);
7118 
7119 	/*
7120 	 * New lock owner, create state. Since this was probably called
7121 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
7122 	 */
7123 
7124 	ASSERT(cs->vp != NULL);
7125 	VN_RELE(cs->vp);
7126 	VN_HOLD(dsp->rds_finfo->rf_vp);
7127 	cs->vp = dsp->rds_finfo->rf_vp;
7128 
7129 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
7130 		rfs4_deleg_state_rele(dsp);
7131 		*cs->statusp = resp->status = puterrno4(error);
7132 		return;
7133 	}
7134 
7135 	/* Mark progress for delegation returns */
7136 	dsp->rds_finfo->rf_dinfo.rd_time_lastwrite = gethrestime_sec();
7137 	rfs4_deleg_state_rele(dsp);
7138 	rfs4_do_open(cs, req, oo, DELEG_NONE,
7139 	    args->share_access, args->share_deny, resp, 1);
7140 }
7141 
7142 /*ARGSUSED*/
7143 static void
7144 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
7145     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7146 {
7147 	/*
7148 	 * Lookup the pathname, it must already exist since this file
7149 	 * was delegated.
7150 	 *
7151 	 * Find the file and state info for this vp and open owner pair.
7152 	 *	check that they are in fact delegated.
7153 	 *	check that the state access and deny modes are the same.
7154 	 *
7155 	 * Return the delgation possibly seting the recall flag.
7156 	 */
7157 	rfs4_file_t *fp;
7158 	rfs4_state_t *sp;
7159 	bool_t create = FALSE;
7160 	bool_t dcreate = FALSE;
7161 	rfs4_deleg_state_t *dsp;
7162 	nfsace4 *ace;
7163 
7164 	/* Note we ignore oflags */
7165 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
7166 	    req, cs, args->share_access, &resp->cinfo);
7167 
7168 	if (resp->status != NFS4_OK) {
7169 		return;
7170 	}
7171 
7172 	/* get the file struct and hold a lock on it during initial open */
7173 	fp = rfs4_findfile_withlock(cs->vp, NULL, &create);
7174 	if (fp == NULL) {
7175 		resp->status = NFS4ERR_RESOURCE;
7176 		DTRACE_PROBE1(nfss__e__do_opendelprev1, nfsstat4, resp->status);
7177 		return;
7178 	}
7179 
7180 	sp = rfs4_findstate_by_owner_file(oo, fp, &create);
7181 	if (sp == NULL) {
7182 		resp->status = NFS4ERR_SERVERFAULT;
7183 		DTRACE_PROBE1(nfss__e__do_opendelprev2, nfsstat4, resp->status);
7184 		rw_exit(&fp->rf_file_rwlock);
7185 		rfs4_file_rele(fp);
7186 		return;
7187 	}
7188 
7189 	rfs4_dbe_lock(sp->rs_dbe);
7190 	rfs4_dbe_lock(fp->rf_dbe);
7191 	if (args->share_access != sp->rs_share_access ||
7192 	    args->share_deny != sp->rs_share_deny ||
7193 	    sp->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
7194 		NFS4_DEBUG(rfs4_debug,
7195 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
7196 		rfs4_dbe_unlock(fp->rf_dbe);
7197 		rfs4_dbe_unlock(sp->rs_dbe);
7198 		rfs4_file_rele(fp);
7199 		rfs4_state_rele(sp);
7200 		resp->status = NFS4ERR_SERVERFAULT;
7201 		return;
7202 	}
7203 	rfs4_dbe_unlock(fp->rf_dbe);
7204 	rfs4_dbe_unlock(sp->rs_dbe);
7205 
7206 	dsp = rfs4_finddeleg(sp, &dcreate);
7207 	if (dsp == NULL) {
7208 		rfs4_state_rele(sp);
7209 		rfs4_file_rele(fp);
7210 		resp->status = NFS4ERR_SERVERFAULT;
7211 		return;
7212 	}
7213 
7214 	next_stateid(&sp->rs_stateid);
7215 
7216 	resp->stateid = sp->rs_stateid.stateid;
7217 
7218 	resp->delegation.delegation_type = dsp->rds_dtype;
7219 
7220 	if (dsp->rds_dtype == OPEN_DELEGATE_READ) {
7221 		open_read_delegation4 *rv =
7222 		    &resp->delegation.open_delegation4_u.read;
7223 
7224 		rv->stateid = dsp->rds_delegid.stateid;
7225 		rv->recall = FALSE; /* no policy in place to set to TRUE */
7226 		ace = &rv->permissions;
7227 	} else {
7228 		open_write_delegation4 *rv =
7229 		    &resp->delegation.open_delegation4_u.write;
7230 
7231 		rv->stateid = dsp->rds_delegid.stateid;
7232 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
7233 		ace = &rv->permissions;
7234 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
7235 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
7236 	}
7237 
7238 	/* XXX For now */
7239 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
7240 	ace->flag = 0;
7241 	ace->access_mask = 0;
7242 	ace->who.utf8string_len = 0;
7243 	ace->who.utf8string_val = 0;
7244 
7245 	rfs4_deleg_state_rele(dsp);
7246 	rfs4_state_rele(sp);
7247 	rfs4_file_rele(fp);
7248 }
7249 
7250 typedef enum {
7251 	NFS4_CHKSEQ_OKAY = 0,
7252 	NFS4_CHKSEQ_REPLAY = 1,
7253 	NFS4_CHKSEQ_BAD = 2
7254 } rfs4_chkseq_t;
7255 
7256 /*
7257  * Generic function for sequence number checks.
7258  */
7259 static rfs4_chkseq_t
7260 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
7261     seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
7262 {
7263 	/* Same sequence ids and matching operations? */
7264 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
7265 		if (copyres == TRUE) {
7266 			rfs4_free_reply(resop);
7267 			rfs4_copy_reply(resop, lastop);
7268 		}
7269 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
7270 		    "Replayed SEQID %d\n", seqid));
7271 		return (NFS4_CHKSEQ_REPLAY);
7272 	}
7273 
7274 	/* If the incoming sequence is not the next expected then it is bad */
7275 	if (rqst_seq != seqid + 1) {
7276 		if (rqst_seq == seqid) {
7277 			NFS4_DEBUG(rfs4_debug,
7278 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
7279 			    "but last op was %d current op is %d\n",
7280 			    lastop->resop, resop->resop));
7281 			return (NFS4_CHKSEQ_BAD);
7282 		}
7283 		NFS4_DEBUG(rfs4_debug,
7284 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
7285 		    rqst_seq, seqid));
7286 		return (NFS4_CHKSEQ_BAD);
7287 	}
7288 
7289 	/* Everything okay -- next expected */
7290 	return (NFS4_CHKSEQ_OKAY);
7291 }
7292 
7293 
7294 static rfs4_chkseq_t
7295 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7296 {
7297 	rfs4_chkseq_t rc;
7298 
7299 	rfs4_dbe_lock(op->ro_dbe);
7300 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply, seqid, resop,
7301 	    TRUE);
7302 	rfs4_dbe_unlock(op->ro_dbe);
7303 
7304 	if (rc == NFS4_CHKSEQ_OKAY)
7305 		rfs4_update_lease(op->ro_client);
7306 
7307 	return (rc);
7308 }
7309 
7310 static rfs4_chkseq_t
7311 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7312 {
7313 	rfs4_chkseq_t rc;
7314 
7315 	rfs4_dbe_lock(op->ro_dbe);
7316 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply,
7317 	    olo_seqid, resop, FALSE);
7318 	rfs4_dbe_unlock(op->ro_dbe);
7319 
7320 	return (rc);
7321 }
7322 
7323 static rfs4_chkseq_t
7324 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lsp, nfs_resop4 *resop)
7325 {
7326 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
7327 
7328 	rfs4_dbe_lock(lsp->rls_dbe);
7329 	if (!lsp->rls_skip_seqid_check)
7330 		rc = rfs4_check_seqid(lsp->rls_seqid, &lsp->rls_reply, seqid,
7331 		    resop, TRUE);
7332 	rfs4_dbe_unlock(lsp->rls_dbe);
7333 
7334 	return (rc);
7335 }
7336 
7337 static void
7338 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
7339     struct svc_req *req, struct compound_state *cs)
7340 {
7341 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
7342 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
7343 	open_owner4 *owner = &args->owner;
7344 	open_claim_type4 claim = args->claim;
7345 	rfs4_client_t *cp;
7346 	rfs4_openowner_t *oo;
7347 	bool_t create;
7348 	bool_t replay = FALSE;
7349 	int can_reclaim;
7350 
7351 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
7352 	    OPEN4args *, args);
7353 
7354 	if (cs->vp == NULL) {
7355 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7356 		goto end;
7357 	}
7358 
7359 	/*
7360 	 * Need to check clientid and lease expiration first based on
7361 	 * error ordering and incrementing sequence id.
7362 	 */
7363 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
7364 	if (cp == NULL) {
7365 		*cs->statusp = resp->status =
7366 		    rfs4_check_clientid(&owner->clientid, 0);
7367 		goto end;
7368 	}
7369 
7370 	if (rfs4_lease_expired(cp)) {
7371 		rfs4_client_close(cp);
7372 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7373 		goto end;
7374 	}
7375 	can_reclaim = cp->rc_can_reclaim;
7376 
7377 	/*
7378 	 * Find the open_owner for use from this point forward.  Take
7379 	 * care in updating the sequence id based on the type of error
7380 	 * being returned.
7381 	 */
7382 retry:
7383 	create = TRUE;
7384 	oo = rfs4_findopenowner(owner, &create, args->seqid);
7385 	if (oo == NULL) {
7386 		*cs->statusp = resp->status = NFS4ERR_RESOURCE;
7387 		rfs4_client_rele(cp);
7388 		goto end;
7389 	}
7390 
7391 	/* Hold off access to the sequence space while the open is done */
7392 	rfs4_sw_enter(&oo->ro_sw);
7393 
7394 	/*
7395 	 * If the open_owner existed before at the server, then check
7396 	 * the sequence id.
7397 	 */
7398 	if (!create && !oo->ro_postpone_confirm) {
7399 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
7400 		case NFS4_CHKSEQ_BAD:
7401 			if ((args->seqid > oo->ro_open_seqid) &&
7402 			    oo->ro_need_confirm) {
7403 				rfs4_free_opens(oo, TRUE, FALSE);
7404 				rfs4_sw_exit(&oo->ro_sw);
7405 				rfs4_openowner_rele(oo);
7406 				goto retry;
7407 			}
7408 			resp->status = NFS4ERR_BAD_SEQID;
7409 			goto out;
7410 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
7411 			replay = TRUE;
7412 			goto out;
7413 		default:
7414 			break;
7415 		}
7416 
7417 		/*
7418 		 * Sequence was ok and open owner exists
7419 		 * check to see if we have yet to see an
7420 		 * open_confirm.
7421 		 */
7422 		if (oo->ro_need_confirm) {
7423 			rfs4_free_opens(oo, TRUE, FALSE);
7424 			rfs4_sw_exit(&oo->ro_sw);
7425 			rfs4_openowner_rele(oo);
7426 			goto retry;
7427 		}
7428 	}
7429 	/* Grace only applies to regular-type OPENs */
7430 	if (rfs4_clnt_in_grace(cp) &&
7431 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7432 		*cs->statusp = resp->status = NFS4ERR_GRACE;
7433 		goto out;
7434 	}
7435 
7436 	/*
7437 	 * If previous state at the server existed then can_reclaim
7438 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
7439 	 * client.
7440 	 */
7441 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7442 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7443 		goto out;
7444 	}
7445 
7446 
7447 	/*
7448 	 * Reject the open if the client has missed the grace period
7449 	 */
7450 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7451 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7452 		goto out;
7453 	}
7454 
7455 	/* Couple of up-front bookkeeping items */
7456 	if (oo->ro_need_confirm) {
7457 		/*
7458 		 * If this is a reclaim OPEN then we should not ask
7459 		 * for a confirmation of the open_owner per the
7460 		 * protocol specification.
7461 		 */
7462 		if (claim == CLAIM_PREVIOUS)
7463 			oo->ro_need_confirm = FALSE;
7464 		else
7465 			resp->rflags |= OPEN4_RESULT_CONFIRM;
7466 	}
7467 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7468 
7469 	/*
7470 	 * If there is an unshared filesystem mounted on this vnode,
7471 	 * do not allow to open/create in this directory.
7472 	 */
7473 	if (vn_ismntpt(cs->vp)) {
7474 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
7475 		goto out;
7476 	}
7477 
7478 	/*
7479 	 * access must READ, WRITE, or BOTH.  No access is invalid.
7480 	 * deny can be READ, WRITE, BOTH, or NONE.
7481 	 * bits not defined for access/deny are invalid.
7482 	 */
7483 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7484 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7485 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7486 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7487 		goto out;
7488 	}
7489 
7490 
7491 	/*
7492 	 * make sure attrset is zero before response is built.
7493 	 */
7494 	resp->attrset = 0;
7495 
7496 	switch (claim) {
7497 	case CLAIM_NULL:
7498 		rfs4_do_opennull(cs, req, args, oo, resp);
7499 		break;
7500 	case CLAIM_PREVIOUS:
7501 		rfs4_do_openprev(cs, req, args, oo, resp);
7502 		break;
7503 	case CLAIM_DELEGATE_CUR:
7504 		rfs4_do_opendelcur(cs, req, args, oo, resp);
7505 		break;
7506 	case CLAIM_DELEGATE_PREV:
7507 		rfs4_do_opendelprev(cs, req, args, oo, resp);
7508 		break;
7509 	default:
7510 		resp->status = NFS4ERR_INVAL;
7511 		break;
7512 	}
7513 
7514 out:
7515 	rfs4_client_rele(cp);
7516 
7517 	/* Catch sequence id handling here to make it a little easier */
7518 	switch (resp->status) {
7519 	case NFS4ERR_BADXDR:
7520 	case NFS4ERR_BAD_SEQID:
7521 	case NFS4ERR_BAD_STATEID:
7522 	case NFS4ERR_NOFILEHANDLE:
7523 	case NFS4ERR_RESOURCE:
7524 	case NFS4ERR_STALE_CLIENTID:
7525 	case NFS4ERR_STALE_STATEID:
7526 		/*
7527 		 * The protocol states that if any of these errors are
7528 		 * being returned, the sequence id should not be
7529 		 * incremented.  Any other return requires an
7530 		 * increment.
7531 		 */
7532 		break;
7533 	default:
7534 		/* Always update the lease in this case */
7535 		rfs4_update_lease(oo->ro_client);
7536 
7537 		/* Regular response - copy the result */
7538 		if (!replay)
7539 			rfs4_update_open_resp(oo, resop, &cs->fh);
7540 
7541 		/*
7542 		 * REPLAY case: Only if the previous response was OK
7543 		 * do we copy the filehandle.  If not OK, no
7544 		 * filehandle to copy.
7545 		 */
7546 		if (replay == TRUE &&
7547 		    resp->status == NFS4_OK &&
7548 		    oo->ro_reply_fh.nfs_fh4_val) {
7549 			/*
7550 			 * If this is a replay, we must restore the
7551 			 * current filehandle/vp to that of what was
7552 			 * returned originally.  Try our best to do
7553 			 * it.
7554 			 */
7555 			nfs_fh4_fmt_t *fh_fmtp =
7556 			    (nfs_fh4_fmt_t *)oo->ro_reply_fh.nfs_fh4_val;
7557 
7558 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7559 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7560 
7561 			if (cs->exi == NULL) {
7562 				resp->status = NFS4ERR_STALE;
7563 				goto finish;
7564 			}
7565 
7566 			VN_RELE(cs->vp);
7567 
7568 			cs->vp = nfs4_fhtovp(&oo->ro_reply_fh, cs->exi,
7569 			    &resp->status);
7570 
7571 			if (cs->vp == NULL)
7572 				goto finish;
7573 
7574 			nfs_fh4_copy(&oo->ro_reply_fh, &cs->fh);
7575 		}
7576 
7577 		/*
7578 		 * If this was a replay, no need to update the
7579 		 * sequence id. If the open_owner was not created on
7580 		 * this pass, then update.  The first use of an
7581 		 * open_owner will not bump the sequence id.
7582 		 */
7583 		if (replay == FALSE && !create)
7584 			rfs4_update_open_sequence(oo);
7585 		/*
7586 		 * If the client is receiving an error and the
7587 		 * open_owner needs to be confirmed, there is no way
7588 		 * to notify the client of this fact ignoring the fact
7589 		 * that the server has no method of returning a
7590 		 * stateid to confirm.  Therefore, the server needs to
7591 		 * mark this open_owner in a way as to avoid the
7592 		 * sequence id checking the next time the client uses
7593 		 * this open_owner.
7594 		 */
7595 		if (resp->status != NFS4_OK && oo->ro_need_confirm)
7596 			oo->ro_postpone_confirm = TRUE;
7597 		/*
7598 		 * If OK response then clear the postpone flag and
7599 		 * reset the sequence id to keep in sync with the
7600 		 * client.
7601 		 */
7602 		if (resp->status == NFS4_OK && oo->ro_postpone_confirm) {
7603 			oo->ro_postpone_confirm = FALSE;
7604 			oo->ro_open_seqid = args->seqid;
7605 		}
7606 		break;
7607 	}
7608 
7609 finish:
7610 	*cs->statusp = resp->status;
7611 
7612 	rfs4_sw_exit(&oo->ro_sw);
7613 	rfs4_openowner_rele(oo);
7614 
7615 end:
7616 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7617 	    OPEN4res *, resp);
7618 }
7619 
7620 /*ARGSUSED*/
7621 void
7622 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7623     struct svc_req *req, struct compound_state *cs)
7624 {
7625 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7626 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7627 	rfs4_state_t *sp;
7628 	nfsstat4 status;
7629 
7630 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7631 	    OPEN_CONFIRM4args *, args);
7632 
7633 	if (cs->vp == NULL) {
7634 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7635 		goto out;
7636 	}
7637 
7638 	if (cs->vp->v_type != VREG) {
7639 		*cs->statusp = resp->status =
7640 		    cs->vp->v_type == VDIR ? NFS4ERR_ISDIR : NFS4ERR_INVAL;
7641 		return;
7642 	}
7643 
7644 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7645 	if (status != NFS4_OK) {
7646 		*cs->statusp = resp->status = status;
7647 		goto out;
7648 	}
7649 
7650 	/* Ensure specified filehandle matches */
7651 	if (cs->vp != sp->rs_finfo->rf_vp) {
7652 		rfs4_state_rele(sp);
7653 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7654 		goto out;
7655 	}
7656 
7657 	/* hold off other access to open_owner while we tinker */
7658 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
7659 
7660 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7661 	case NFS4_CHECK_STATEID_OKAY:
7662 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7663 		    resop) != 0) {
7664 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7665 			break;
7666 		}
7667 		/*
7668 		 * If it is the appropriate stateid and determined to
7669 		 * be "OKAY" then this means that the stateid does not
7670 		 * need to be confirmed and the client is in error for
7671 		 * sending an OPEN_CONFIRM.
7672 		 */
7673 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7674 		break;
7675 	case NFS4_CHECK_STATEID_OLD:
7676 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7677 		break;
7678 	case NFS4_CHECK_STATEID_BAD:
7679 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7680 		break;
7681 	case NFS4_CHECK_STATEID_EXPIRED:
7682 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7683 		break;
7684 	case NFS4_CHECK_STATEID_CLOSED:
7685 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7686 		break;
7687 	case NFS4_CHECK_STATEID_REPLAY:
7688 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7689 		    resop)) {
7690 		case NFS4_CHKSEQ_OKAY:
7691 			/*
7692 			 * This is replayed stateid; if seqid matches
7693 			 * next expected, then client is using wrong seqid.
7694 			 */
7695 			/* fall through */
7696 		case NFS4_CHKSEQ_BAD:
7697 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7698 			break;
7699 		case NFS4_CHKSEQ_REPLAY:
7700 			/*
7701 			 * Note this case is the duplicate case so
7702 			 * resp->status is already set.
7703 			 */
7704 			*cs->statusp = resp->status;
7705 			rfs4_update_lease(sp->rs_owner->ro_client);
7706 			break;
7707 		}
7708 		break;
7709 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7710 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7711 		    resop) != NFS4_CHKSEQ_OKAY) {
7712 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7713 			break;
7714 		}
7715 		*cs->statusp = resp->status = NFS4_OK;
7716 
7717 		next_stateid(&sp->rs_stateid);
7718 		resp->open_stateid = sp->rs_stateid.stateid;
7719 		sp->rs_owner->ro_need_confirm = FALSE;
7720 		rfs4_update_lease(sp->rs_owner->ro_client);
7721 		rfs4_update_open_sequence(sp->rs_owner);
7722 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7723 		break;
7724 	default:
7725 		ASSERT(FALSE);
7726 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7727 		break;
7728 	}
7729 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
7730 	rfs4_state_rele(sp);
7731 
7732 out:
7733 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7734 	    OPEN_CONFIRM4res *, resp);
7735 }
7736 
7737 /*ARGSUSED*/
7738 void
7739 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7740     struct svc_req *req, struct compound_state *cs)
7741 {
7742 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7743 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7744 	uint32_t access = args->share_access;
7745 	uint32_t deny = args->share_deny;
7746 	nfsstat4 status;
7747 	rfs4_state_t *sp;
7748 	rfs4_file_t *fp;
7749 	int fflags = 0;
7750 
7751 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7752 	    OPEN_DOWNGRADE4args *, args);
7753 
7754 	if (cs->vp == NULL) {
7755 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7756 		goto out;
7757 	}
7758 
7759 	if (cs->vp->v_type != VREG) {
7760 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7761 		return;
7762 	}
7763 
7764 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7765 	if (status != NFS4_OK) {
7766 		*cs->statusp = resp->status = status;
7767 		goto out;
7768 	}
7769 
7770 	/* Ensure specified filehandle matches */
7771 	if (cs->vp != sp->rs_finfo->rf_vp) {
7772 		rfs4_state_rele(sp);
7773 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7774 		goto out;
7775 	}
7776 
7777 	/* hold off other access to open_owner while we tinker */
7778 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
7779 
7780 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7781 	case NFS4_CHECK_STATEID_OKAY:
7782 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7783 		    resop) != NFS4_CHKSEQ_OKAY) {
7784 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7785 			goto end;
7786 		}
7787 		break;
7788 	case NFS4_CHECK_STATEID_OLD:
7789 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7790 		goto end;
7791 	case NFS4_CHECK_STATEID_BAD:
7792 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7793 		goto end;
7794 	case NFS4_CHECK_STATEID_EXPIRED:
7795 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7796 		goto end;
7797 	case NFS4_CHECK_STATEID_CLOSED:
7798 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7799 		goto end;
7800 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7801 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7802 		goto end;
7803 	case NFS4_CHECK_STATEID_REPLAY:
7804 		/* Check the sequence id for the open owner */
7805 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7806 		    resop)) {
7807 		case NFS4_CHKSEQ_OKAY:
7808 			/*
7809 			 * This is replayed stateid; if seqid matches
7810 			 * next expected, then client is using wrong seqid.
7811 			 */
7812 			/* fall through */
7813 		case NFS4_CHKSEQ_BAD:
7814 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7815 			goto end;
7816 		case NFS4_CHKSEQ_REPLAY:
7817 			/*
7818 			 * Note this case is the duplicate case so
7819 			 * resp->status is already set.
7820 			 */
7821 			*cs->statusp = resp->status;
7822 			rfs4_update_lease(sp->rs_owner->ro_client);
7823 			goto end;
7824 		}
7825 		break;
7826 	default:
7827 		ASSERT(FALSE);
7828 		break;
7829 	}
7830 
7831 	rfs4_dbe_lock(sp->rs_dbe);
7832 	/*
7833 	 * Check that the new access modes and deny modes are valid.
7834 	 * Check that no invalid bits are set.
7835 	 */
7836 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7837 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7838 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7839 		rfs4_update_open_sequence(sp->rs_owner);
7840 		rfs4_dbe_unlock(sp->rs_dbe);
7841 		goto end;
7842 	}
7843 
7844 	/*
7845 	 * The new modes must be a subset of the current modes and
7846 	 * the access must specify at least one mode. To test that
7847 	 * the new mode is a subset of the current modes we bitwise
7848 	 * AND them together and check that the result equals the new
7849 	 * mode. For example:
7850 	 * New mode, access == R and current mode, sp->rs_open_access  == RW
7851 	 * access & sp->rs_open_access == R == access, so the new access mode
7852 	 * is valid. Consider access == RW, sp->rs_open_access = R
7853 	 * access & sp->rs_open_access == R != access, so the new access mode
7854 	 * is invalid.
7855 	 */
7856 	if ((access & sp->rs_open_access) != access ||
7857 	    (deny & sp->rs_open_deny) != deny ||
7858 	    (access &
7859 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7860 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7861 		rfs4_update_open_sequence(sp->rs_owner);
7862 		rfs4_dbe_unlock(sp->rs_dbe);
7863 		goto end;
7864 	}
7865 
7866 	/*
7867 	 * Release any share locks associated with this stateID.
7868 	 * Strictly speaking, this violates the spec because the
7869 	 * spec effectively requires that open downgrade be atomic.
7870 	 * At present, fs_shrlock does not have this capability.
7871 	 */
7872 	(void) rfs4_unshare(sp);
7873 
7874 	status = rfs4_share(sp, access, deny);
7875 	if (status != NFS4_OK) {
7876 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7877 		rfs4_update_open_sequence(sp->rs_owner);
7878 		rfs4_dbe_unlock(sp->rs_dbe);
7879 		goto end;
7880 	}
7881 
7882 	fp = sp->rs_finfo;
7883 	rfs4_dbe_lock(fp->rf_dbe);
7884 
7885 	/*
7886 	 * If the current mode has deny read and the new mode
7887 	 * does not, decrement the number of deny read mode bits
7888 	 * and if it goes to zero turn off the deny read bit
7889 	 * on the file.
7890 	 */
7891 	if ((sp->rs_open_deny & OPEN4_SHARE_DENY_READ) &&
7892 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7893 		fp->rf_deny_read--;
7894 		if (fp->rf_deny_read == 0)
7895 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
7896 	}
7897 
7898 	/*
7899 	 * If the current mode has deny write and the new mode
7900 	 * does not, decrement the number of deny write mode bits
7901 	 * and if it goes to zero turn off the deny write bit
7902 	 * on the file.
7903 	 */
7904 	if ((sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) &&
7905 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7906 		fp->rf_deny_write--;
7907 		if (fp->rf_deny_write == 0)
7908 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7909 	}
7910 
7911 	/*
7912 	 * If the current mode has access read and the new mode
7913 	 * does not, decrement the number of access read mode bits
7914 	 * and if it goes to zero turn off the access read bit
7915 	 * on the file.  set fflags to FREAD for the call to
7916 	 * vn_open_downgrade().
7917 	 */
7918 	if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) &&
7919 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7920 		fp->rf_access_read--;
7921 		if (fp->rf_access_read == 0)
7922 			fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
7923 		fflags |= FREAD;
7924 	}
7925 
7926 	/*
7927 	 * If the current mode has access write and the new mode
7928 	 * does not, decrement the number of access write mode bits
7929 	 * and if it goes to zero turn off the access write bit
7930 	 * on the file.  set fflags to FWRITE for the call to
7931 	 * vn_open_downgrade().
7932 	 */
7933 	if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) &&
7934 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7935 		fp->rf_access_write--;
7936 		if (fp->rf_access_write == 0)
7937 			fp->rf_share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7938 		fflags |= FWRITE;
7939 	}
7940 
7941 	/* Check that the file is still accessible */
7942 	ASSERT(fp->rf_share_access);
7943 
7944 	rfs4_dbe_unlock(fp->rf_dbe);
7945 
7946 	/* now set the new open access and deny modes */
7947 	sp->rs_open_access = access;
7948 	sp->rs_open_deny = deny;
7949 
7950 	/*
7951 	 * we successfully downgraded the share lock, now we need to downgrade
7952 	 * the open. it is possible that the downgrade was only for a deny
7953 	 * mode and we have nothing else to do.
7954 	 */
7955 	if ((fflags & (FREAD|FWRITE)) != 0)
7956 		vn_open_downgrade(cs->vp, fflags);
7957 
7958 	/* Update the stateid */
7959 	next_stateid(&sp->rs_stateid);
7960 	resp->open_stateid = sp->rs_stateid.stateid;
7961 
7962 	rfs4_dbe_unlock(sp->rs_dbe);
7963 
7964 	*cs->statusp = resp->status = NFS4_OK;
7965 	/* Update the lease */
7966 	rfs4_update_lease(sp->rs_owner->ro_client);
7967 	/* And the sequence */
7968 	rfs4_update_open_sequence(sp->rs_owner);
7969 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7970 
7971 end:
7972 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
7973 	rfs4_state_rele(sp);
7974 out:
7975 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7976 	    OPEN_DOWNGRADE4res *, resp);
7977 }
7978 
7979 static void *
7980 memstr(const void *s1, const char *s2, size_t n)
7981 {
7982 	size_t l = strlen(s2);
7983 	char *p = (char *)s1;
7984 
7985 	while (n >= l) {
7986 		if (bcmp(p, s2, l) == 0)
7987 			return (p);
7988 		p++;
7989 		n--;
7990 	}
7991 
7992 	return (NULL);
7993 }
7994 
7995 /*
7996  * The logic behind this function is detailed in the NFSv4 RFC in the
7997  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7998  * that section for explicit guidance to server behavior for
7999  * SETCLIENTID.
8000  */
8001 void
8002 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
8003     struct svc_req *req, struct compound_state *cs)
8004 {
8005 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
8006 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
8007 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
8008 	rfs4_clntip_t *ci;
8009 	bool_t create;
8010 	char *addr, *netid;
8011 	int len;
8012 
8013 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
8014 	    SETCLIENTID4args *, args);
8015 retry:
8016 	newcp = cp_confirmed = cp_unconfirmed = NULL;
8017 
8018 	/*
8019 	 * Save the caller's IP address
8020 	 */
8021 	args->client.cl_addr =
8022 	    (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
8023 
8024 	/*
8025 	 * Record if it is a Solaris client that cannot handle referrals.
8026 	 */
8027 	if (memstr(args->client.id_val, "Solaris", args->client.id_len) &&
8028 	    !memstr(args->client.id_val, "+referrals", args->client.id_len)) {
8029 		/* Add a "yes, it's downrev" record */
8030 		create = TRUE;
8031 		ci = rfs4_find_clntip(args->client.cl_addr, &create);
8032 		ASSERT(ci != NULL);
8033 		rfs4_dbe_rele(ci->ri_dbe);
8034 	} else {
8035 		/* Remove any previous record */
8036 		rfs4_invalidate_clntip(args->client.cl_addr);
8037 	}
8038 
8039 	/*
8040 	 * In search of an EXISTING client matching the incoming
8041 	 * request to establish a new client identifier at the server
8042 	 */
8043 	create = TRUE;
8044 	cp = rfs4_findclient(&args->client, &create, NULL);
8045 
8046 	/* Should never happen */
8047 	ASSERT(cp != NULL);
8048 
8049 	if (cp == NULL) {
8050 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8051 		goto out;
8052 	}
8053 
8054 	/*
8055 	 * Easiest case. Client identifier is newly created and is
8056 	 * unconfirmed.  Also note that for this case, no other
8057 	 * entries exist for the client identifier.  Nothing else to
8058 	 * check.  Just setup the response and respond.
8059 	 */
8060 	if (create) {
8061 		*cs->statusp = res->status = NFS4_OK;
8062 		res->SETCLIENTID4res_u.resok4.clientid = cp->rc_clientid;
8063 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8064 		    cp->rc_confirm_verf;
8065 		/* Setup callback information; CB_NULL confirmation later */
8066 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
8067 
8068 		rfs4_client_rele(cp);
8069 		goto out;
8070 	}
8071 
8072 	/*
8073 	 * An existing, confirmed client may exist but it may not have
8074 	 * been active for at least one lease period.  If so, then
8075 	 * "close" the client and create a new client identifier
8076 	 */
8077 	if (rfs4_lease_expired(cp)) {
8078 		rfs4_client_close(cp);
8079 		goto retry;
8080 	}
8081 
8082 	if (cp->rc_need_confirm == TRUE)
8083 		cp_unconfirmed = cp;
8084 	else
8085 		cp_confirmed = cp;
8086 
8087 	cp = NULL;
8088 
8089 	/*
8090 	 * We have a confirmed client, now check for an
8091 	 * unconfimred entry
8092 	 */
8093 	if (cp_confirmed) {
8094 		/* If creds don't match then client identifier is inuse */
8095 		if (!creds_ok(cp_confirmed->rc_cr_set, req, cs)) {
8096 			rfs4_cbinfo_t *cbp;
8097 			/*
8098 			 * Some one else has established this client
8099 			 * id. Try and say * who they are. We will use
8100 			 * the call back address supplied by * the
8101 			 * first client.
8102 			 */
8103 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8104 
8105 			addr = netid = NULL;
8106 
8107 			cbp = &cp_confirmed->rc_cbinfo;
8108 			if (cbp->cb_callback.cb_location.r_addr &&
8109 			    cbp->cb_callback.cb_location.r_netid) {
8110 				cb_client4 *cbcp = &cbp->cb_callback;
8111 
8112 				len = strlen(cbcp->cb_location.r_addr)+1;
8113 				addr = kmem_alloc(len, KM_SLEEP);
8114 				bcopy(cbcp->cb_location.r_addr, addr, len);
8115 				len = strlen(cbcp->cb_location.r_netid)+1;
8116 				netid = kmem_alloc(len, KM_SLEEP);
8117 				bcopy(cbcp->cb_location.r_netid, netid, len);
8118 			}
8119 
8120 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
8121 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
8122 
8123 			rfs4_client_rele(cp_confirmed);
8124 		}
8125 
8126 		/*
8127 		 * Confirmed, creds match, and verifier matches; must
8128 		 * be an update of the callback info
8129 		 */
8130 		if (cp_confirmed->rc_nfs_client.verifier ==
8131 		    args->client.verifier) {
8132 			/* Setup callback information */
8133 			rfs4_client_setcb(cp_confirmed, &args->callback,
8134 			    args->callback_ident);
8135 
8136 			/* everything okay -- move ahead */
8137 			*cs->statusp = res->status = NFS4_OK;
8138 			res->SETCLIENTID4res_u.resok4.clientid =
8139 			    cp_confirmed->rc_clientid;
8140 
8141 			/* update the confirm_verifier and return it */
8142 			rfs4_client_scv_next(cp_confirmed);
8143 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8144 			    cp_confirmed->rc_confirm_verf;
8145 
8146 			rfs4_client_rele(cp_confirmed);
8147 			goto out;
8148 		}
8149 
8150 		/*
8151 		 * Creds match but the verifier doesn't.  Must search
8152 		 * for an unconfirmed client that would be replaced by
8153 		 * this request.
8154 		 */
8155 		create = FALSE;
8156 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
8157 		    cp_confirmed);
8158 	}
8159 
8160 	/*
8161 	 * At this point, we have taken care of the brand new client
8162 	 * struct, INUSE case, update of an existing, and confirmed
8163 	 * client struct.
8164 	 */
8165 
8166 	/*
8167 	 * check to see if things have changed while we originally
8168 	 * picked up the client struct.  If they have, then return and
8169 	 * retry the processing of this SETCLIENTID request.
8170 	 */
8171 	if (cp_unconfirmed) {
8172 		rfs4_dbe_lock(cp_unconfirmed->rc_dbe);
8173 		if (!cp_unconfirmed->rc_need_confirm) {
8174 			rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8175 			rfs4_client_rele(cp_unconfirmed);
8176 			if (cp_confirmed)
8177 				rfs4_client_rele(cp_confirmed);
8178 			goto retry;
8179 		}
8180 		/* do away with the old unconfirmed one */
8181 		rfs4_dbe_invalidate(cp_unconfirmed->rc_dbe);
8182 		rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8183 		rfs4_client_rele(cp_unconfirmed);
8184 		cp_unconfirmed = NULL;
8185 	}
8186 
8187 	/*
8188 	 * This search will temporarily hide the confirmed client
8189 	 * struct while a new client struct is created as the
8190 	 * unconfirmed one.
8191 	 */
8192 	create = TRUE;
8193 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
8194 
8195 	ASSERT(newcp != NULL);
8196 
8197 	if (newcp == NULL) {
8198 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8199 		rfs4_client_rele(cp_confirmed);
8200 		goto out;
8201 	}
8202 
8203 	/*
8204 	 * If one was not created, then a similar request must be in
8205 	 * process so release and start over with this one
8206 	 */
8207 	if (create != TRUE) {
8208 		rfs4_client_rele(newcp);
8209 		if (cp_confirmed)
8210 			rfs4_client_rele(cp_confirmed);
8211 		goto retry;
8212 	}
8213 
8214 	*cs->statusp = res->status = NFS4_OK;
8215 	res->SETCLIENTID4res_u.resok4.clientid = newcp->rc_clientid;
8216 	res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8217 	    newcp->rc_confirm_verf;
8218 	/* Setup callback information; CB_NULL confirmation later */
8219 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
8220 
8221 	newcp->rc_cp_confirmed = cp_confirmed;
8222 
8223 	rfs4_client_rele(newcp);
8224 
8225 out:
8226 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
8227 	    SETCLIENTID4res *, res);
8228 }
8229 
8230 /*ARGSUSED*/
8231 void
8232 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
8233     struct svc_req *req, struct compound_state *cs)
8234 {
8235 	SETCLIENTID_CONFIRM4args *args =
8236 	    &argop->nfs_argop4_u.opsetclientid_confirm;
8237 	SETCLIENTID_CONFIRM4res *res =
8238 	    &resop->nfs_resop4_u.opsetclientid_confirm;
8239 	rfs4_client_t *cp, *cptoclose = NULL;
8240 	nfs4_srv_t *nsrv4;
8241 
8242 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
8243 	    struct compound_state *, cs,
8244 	    SETCLIENTID_CONFIRM4args *, args);
8245 
8246 	nsrv4 = nfs4_get_srv();
8247 	*cs->statusp = res->status = NFS4_OK;
8248 
8249 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
8250 
8251 	if (cp == NULL) {
8252 		*cs->statusp = res->status =
8253 		    rfs4_check_clientid(&args->clientid, 1);
8254 		goto out;
8255 	}
8256 
8257 	if (!creds_ok(cp, req, cs)) {
8258 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8259 		rfs4_client_rele(cp);
8260 		goto out;
8261 	}
8262 
8263 	/* If the verifier doesn't match, the record doesn't match */
8264 	if (cp->rc_confirm_verf != args->setclientid_confirm) {
8265 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
8266 		rfs4_client_rele(cp);
8267 		goto out;
8268 	}
8269 
8270 	rfs4_dbe_lock(cp->rc_dbe);
8271 	cp->rc_need_confirm = FALSE;
8272 	if (cp->rc_cp_confirmed) {
8273 		cptoclose = cp->rc_cp_confirmed;
8274 		cptoclose->rc_ss_remove = 1;
8275 		cp->rc_cp_confirmed = NULL;
8276 	}
8277 
8278 	/*
8279 	 * Update the client's associated server instance, if it's changed
8280 	 * since the client was created.
8281 	 */
8282 	if (rfs4_servinst(cp) != nsrv4->nfs4_cur_servinst)
8283 		rfs4_servinst_assign(nsrv4, cp, nsrv4->nfs4_cur_servinst);
8284 
8285 	/*
8286 	 * Record clientid in stable storage.
8287 	 * Must be done after server instance has been assigned.
8288 	 */
8289 	rfs4_ss_clid(nsrv4, cp);
8290 
8291 	rfs4_dbe_unlock(cp->rc_dbe);
8292 
8293 	if (cptoclose)
8294 		/* don't need to rele, client_close does it */
8295 		rfs4_client_close(cptoclose);
8296 
8297 	/* If needed, initiate CB_NULL call for callback path */
8298 	rfs4_deleg_cb_check(cp);
8299 	rfs4_update_lease(cp);
8300 
8301 	/*
8302 	 * Check to see if client can perform reclaims
8303 	 */
8304 	rfs4_ss_chkclid(nsrv4, cp);
8305 
8306 	rfs4_client_rele(cp);
8307 
8308 out:
8309 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
8310 	    struct compound_state *, cs,
8311 	    SETCLIENTID_CONFIRM4 *, res);
8312 }
8313 
8314 
8315 /*ARGSUSED*/
8316 void
8317 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
8318     struct svc_req *req, struct compound_state *cs)
8319 {
8320 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
8321 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
8322 	rfs4_state_t *sp;
8323 	nfsstat4 status;
8324 
8325 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
8326 	    CLOSE4args *, args);
8327 
8328 	if (cs->vp == NULL) {
8329 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8330 		goto out;
8331 	}
8332 
8333 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
8334 	if (status != NFS4_OK) {
8335 		*cs->statusp = resp->status = status;
8336 		goto out;
8337 	}
8338 
8339 	/* Ensure specified filehandle matches */
8340 	if (cs->vp != sp->rs_finfo->rf_vp) {
8341 		rfs4_state_rele(sp);
8342 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8343 		goto out;
8344 	}
8345 
8346 	/* hold off other access to open_owner while we tinker */
8347 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
8348 
8349 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
8350 	case NFS4_CHECK_STATEID_OKAY:
8351 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8352 		    resop) != NFS4_CHKSEQ_OKAY) {
8353 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8354 			goto end;
8355 		}
8356 		break;
8357 	case NFS4_CHECK_STATEID_OLD:
8358 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8359 		goto end;
8360 	case NFS4_CHECK_STATEID_BAD:
8361 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8362 		goto end;
8363 	case NFS4_CHECK_STATEID_EXPIRED:
8364 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8365 		goto end;
8366 	case NFS4_CHECK_STATEID_CLOSED:
8367 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8368 		goto end;
8369 	case NFS4_CHECK_STATEID_UNCONFIRMED:
8370 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8371 		goto end;
8372 	case NFS4_CHECK_STATEID_REPLAY:
8373 		/* Check the sequence id for the open owner */
8374 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8375 		    resop)) {
8376 		case NFS4_CHKSEQ_OKAY:
8377 			/*
8378 			 * This is replayed stateid; if seqid matches
8379 			 * next expected, then client is using wrong seqid.
8380 			 */
8381 			/* FALL THROUGH */
8382 		case NFS4_CHKSEQ_BAD:
8383 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8384 			goto end;
8385 		case NFS4_CHKSEQ_REPLAY:
8386 			/*
8387 			 * Note this case is the duplicate case so
8388 			 * resp->status is already set.
8389 			 */
8390 			*cs->statusp = resp->status;
8391 			rfs4_update_lease(sp->rs_owner->ro_client);
8392 			goto end;
8393 		}
8394 		break;
8395 	default:
8396 		ASSERT(FALSE);
8397 		break;
8398 	}
8399 
8400 	rfs4_dbe_lock(sp->rs_dbe);
8401 
8402 	/* Update the stateid. */
8403 	next_stateid(&sp->rs_stateid);
8404 	resp->open_stateid = sp->rs_stateid.stateid;
8405 
8406 	rfs4_dbe_unlock(sp->rs_dbe);
8407 
8408 	rfs4_update_lease(sp->rs_owner->ro_client);
8409 	rfs4_update_open_sequence(sp->rs_owner);
8410 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8411 
8412 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
8413 
8414 	*cs->statusp = resp->status = status;
8415 
8416 end:
8417 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
8418 	rfs4_state_rele(sp);
8419 out:
8420 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
8421 	    CLOSE4res *, resp);
8422 }
8423 
8424 /*
8425  * Manage the counts on the file struct and close all file locks
8426  */
8427 /*ARGSUSED*/
8428 void
8429 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
8430     bool_t close_of_client)
8431 {
8432 	rfs4_file_t *fp = sp->rs_finfo;
8433 	rfs4_lo_state_t *lsp;
8434 	int fflags = 0;
8435 
8436 	/*
8437 	 * If this call is part of the larger closing down of client
8438 	 * state then it is just easier to release all locks
8439 	 * associated with this client instead of going through each
8440 	 * individual file and cleaning locks there.
8441 	 */
8442 	if (close_of_client) {
8443 		if (sp->rs_owner->ro_client->rc_unlksys_completed == FALSE &&
8444 		    !list_is_empty(&sp->rs_lostatelist) &&
8445 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID) {
8446 			/* Is the PxFS kernel module loaded? */
8447 			if (lm_remove_file_locks != NULL) {
8448 				int new_sysid;
8449 
8450 				/* Encode the cluster nodeid in new sysid */
8451 				new_sysid = sp->rs_owner->ro_client->rc_sysidt;
8452 				lm_set_nlmid_flk(&new_sysid);
8453 
8454 				/*
8455 				 * This PxFS routine removes file locks for a
8456 				 * client over all nodes of a cluster.
8457 				 */
8458 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8459 				    "lm_remove_file_locks(sysid=0x%x)\n",
8460 				    new_sysid));
8461 				(*lm_remove_file_locks)(new_sysid);
8462 			} else {
8463 				struct flock64 flk;
8464 
8465 				/* Release all locks for this client */
8466 				flk.l_type = F_UNLKSYS;
8467 				flk.l_whence = 0;
8468 				flk.l_start = 0;
8469 				flk.l_len = 0;
8470 				flk.l_sysid =
8471 				    sp->rs_owner->ro_client->rc_sysidt;
8472 				flk.l_pid = 0;
8473 				(void) VOP_FRLOCK(sp->rs_finfo->rf_vp, F_SETLK,
8474 				    &flk, F_REMOTELOCK | FREAD | FWRITE,
8475 				    (u_offset_t)0, NULL, CRED(), NULL);
8476 			}
8477 
8478 			sp->rs_owner->ro_client->rc_unlksys_completed = TRUE;
8479 		}
8480 	}
8481 
8482 	/*
8483 	 * Release all locks on this file by this lock owner or at
8484 	 * least mark the locks as having been released
8485 	 */
8486 	for (lsp = list_head(&sp->rs_lostatelist); lsp != NULL;
8487 	    lsp = list_next(&sp->rs_lostatelist, lsp)) {
8488 		lsp->rls_locks_cleaned = TRUE;
8489 
8490 		/* Was this already taken care of above? */
8491 		if (!close_of_client &&
8492 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8493 			(void) cleanlocks(sp->rs_finfo->rf_vp,
8494 			    lsp->rls_locker->rl_pid,
8495 			    lsp->rls_locker->rl_client->rc_sysidt);
8496 	}
8497 
8498 	/*
8499 	 * Release any shrlocks associated with this open state ID.
8500 	 * This must be done before the rfs4_state gets marked closed.
8501 	 */
8502 	if (sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8503 		(void) rfs4_unshare(sp);
8504 
8505 	if (sp->rs_open_access) {
8506 		rfs4_dbe_lock(fp->rf_dbe);
8507 
8508 		/*
8509 		 * Decrement the count for each access and deny bit that this
8510 		 * state has contributed to the file.
8511 		 * If the file counts go to zero
8512 		 * clear the appropriate bit in the appropriate mask.
8513 		 */
8514 		if (sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) {
8515 			fp->rf_access_read--;
8516 			fflags |= FREAD;
8517 			if (fp->rf_access_read == 0)
8518 				fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
8519 		}
8520 		if (sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) {
8521 			fp->rf_access_write--;
8522 			fflags |= FWRITE;
8523 			if (fp->rf_access_write == 0)
8524 				fp->rf_share_access &=
8525 				    ~OPEN4_SHARE_ACCESS_WRITE;
8526 		}
8527 		if (sp->rs_open_deny & OPEN4_SHARE_DENY_READ) {
8528 			fp->rf_deny_read--;
8529 			if (fp->rf_deny_read == 0)
8530 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
8531 		}
8532 		if (sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) {
8533 			fp->rf_deny_write--;
8534 			if (fp->rf_deny_write == 0)
8535 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
8536 		}
8537 
8538 		(void) VOP_CLOSE(fp->rf_vp, fflags, 1, (offset_t)0, cr, NULL);
8539 
8540 		rfs4_dbe_unlock(fp->rf_dbe);
8541 
8542 		sp->rs_open_access = 0;
8543 		sp->rs_open_deny = 0;
8544 	}
8545 }
8546 
8547 /*
8548  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8549  */
8550 static nfsstat4
8551 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8552 {
8553 	rfs4_lockowner_t *lo;
8554 	rfs4_client_t *cp;
8555 	uint32_t len;
8556 
8557 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
8558 	if (lo != NULL) {
8559 		cp = lo->rl_client;
8560 		if (rfs4_lease_expired(cp)) {
8561 			rfs4_lockowner_rele(lo);
8562 			rfs4_dbe_hold(cp->rc_dbe);
8563 			rfs4_client_close(cp);
8564 			return (NFS4ERR_EXPIRED);
8565 		}
8566 		dp->owner.clientid = lo->rl_owner.clientid;
8567 		len = lo->rl_owner.owner_len;
8568 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8569 		bcopy(lo->rl_owner.owner_val, dp->owner.owner_val, len);
8570 		dp->owner.owner_len = len;
8571 		rfs4_lockowner_rele(lo);
8572 		goto finish;
8573 	}
8574 
8575 	/*
8576 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
8577 	 * of the client id contain the boot time for a NFS4 lock. So we
8578 	 * fabricate and identity by setting clientid to the sysid, and
8579 	 * the lock owner to the pid.
8580 	 */
8581 	dp->owner.clientid = flk->l_sysid;
8582 	len = sizeof (pid_t);
8583 	dp->owner.owner_len = len;
8584 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8585 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
8586 finish:
8587 	dp->offset = flk->l_start;
8588 	dp->length = flk->l_len;
8589 
8590 	if (flk->l_type == F_RDLCK)
8591 		dp->locktype = READ_LT;
8592 	else if (flk->l_type == F_WRLCK)
8593 		dp->locktype = WRITE_LT;
8594 	else
8595 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
8596 
8597 	return (NFS4_OK);
8598 }
8599 
8600 /*
8601  * The NFSv4.0 LOCK operation does not support the blocking lock (at the
8602  * NFSv4.0 protocol level) so the client needs to resend the LOCK request in a
8603  * case the lock is denied by the NFSv4.0 server.  NFSv4.0 clients are prepared
8604  * for that (obviously); they are sending the LOCK requests with some delays
8605  * between the attempts.  See nfs4frlock() and nfs4_block_and_wait() for the
8606  * locking and delay implementation at the client side.
8607  *
8608  * To make the life of the clients easier, the NFSv4.0 server tries to do some
8609  * fast retries on its own (the for loop below) in a hope the lock will be
8610  * available soon.  And if not, the client won't need to resend the LOCK
8611  * requests so fast to check the lock availability.  This basically saves some
8612  * network traffic and tries to make sure the client gets the lock ASAP.
8613  */
8614 static int
8615 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8616 {
8617 	int error;
8618 	struct flock64 flk;
8619 	int i;
8620 	clock_t delaytime;
8621 	int cmd;
8622 	int spin_cnt = 0;
8623 
8624 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8625 retry:
8626 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8627 
8628 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8629 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8630 		error = VOP_FRLOCK(vp, cmd,
8631 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
8632 
8633 		if (error != EAGAIN && error != EACCES)
8634 			break;
8635 
8636 		if (i < rfs4_maxlock_tries - 1) {
8637 			delay(delaytime);
8638 			delaytime *= 2;
8639 		}
8640 	}
8641 
8642 	if (error == EAGAIN || error == EACCES) {
8643 		/* Get the owner of the lock */
8644 		flk = *flock;
8645 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8646 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag, 0, NULL, cred,
8647 		    NULL) == 0) {
8648 			/*
8649 			 * There's a race inherent in the current VOP_FRLOCK
8650 			 * design where:
8651 			 * a: "other guy" takes a lock that conflicts with a
8652 			 * lock we want
8653 			 * b: we attempt to take our lock (non-blocking) and
8654 			 * the attempt fails.
8655 			 * c: "other guy" releases the conflicting lock
8656 			 * d: we ask what lock conflicts with the lock we want,
8657 			 * getting F_UNLCK (no lock blocks us)
8658 			 *
8659 			 * If we retry the non-blocking lock attempt in this
8660 			 * case (restart at step 'b') there's some possibility
8661 			 * that many such attempts might fail.  However a test
8662 			 * designed to actually provoke this race shows that
8663 			 * the vast majority of cases require no retry, and
8664 			 * only a few took as many as three retries.  Here's
8665 			 * the test outcome:
8666 			 *
8667 			 *	   number of retries    how many times we needed
8668 			 *				that many retries
8669 			 *	   0			79461
8670 			 *	   1			  862
8671 			 *	   2			   49
8672 			 *	   3			    5
8673 			 *
8674 			 * Given those empirical results, we arbitrarily limit
8675 			 * the retry count to ten.
8676 			 *
8677 			 * If we actually make to ten retries and give up,
8678 			 * nothing catastrophic happens, but we're unable to
8679 			 * return the information about the conflicting lock to
8680 			 * the NFS client.  That's an acceptable trade off vs.
8681 			 * letting this retry loop run forever.
8682 			 */
8683 			if (flk.l_type == F_UNLCK) {
8684 				if (spin_cnt++ < 10) {
8685 					/* No longer locked, retry */
8686 					goto retry;
8687 				}
8688 			} else {
8689 				*flock = flk;
8690 				LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8691 				    F_GETLK, &flk);
8692 			}
8693 		}
8694 	}
8695 
8696 	return (error);
8697 }
8698 
8699 /*ARGSUSED*/
8700 static nfsstat4
8701 rfs4_do_lock(rfs4_lo_state_t *lsp, nfs_lock_type4 locktype,
8702     offset4 offset, length4 length, cred_t *cred, nfs_resop4 *resop)
8703 {
8704 	nfsstat4 status;
8705 	rfs4_lockowner_t *lo = lsp->rls_locker;
8706 	rfs4_state_t *sp = lsp->rls_state;
8707 	struct flock64 flock;
8708 	int16_t ltype;
8709 	int flag;
8710 	int error;
8711 	sysid_t sysid;
8712 	LOCK4res *lres;
8713 	vnode_t *vp;
8714 
8715 	if (rfs4_lease_expired(lo->rl_client)) {
8716 		return (NFS4ERR_EXPIRED);
8717 	}
8718 
8719 	if ((status = rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
8720 		return (status);
8721 
8722 	/* Check for zero length. To lock to end of file use all ones for V4 */
8723 	if (length == 0)
8724 		return (NFS4ERR_INVAL);
8725 	else if (length == (length4)(~0))
8726 		length = 0;		/* Posix to end of file  */
8727 
8728 retry:
8729 	rfs4_dbe_lock(sp->rs_dbe);
8730 	if (sp->rs_closed == TRUE) {
8731 		rfs4_dbe_unlock(sp->rs_dbe);
8732 		return (NFS4ERR_OLD_STATEID);
8733 	}
8734 
8735 	if (resop->resop != OP_LOCKU) {
8736 		switch (locktype) {
8737 		case READ_LT:
8738 		case READW_LT:
8739 			if ((sp->rs_share_access
8740 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8741 				rfs4_dbe_unlock(sp->rs_dbe);
8742 
8743 				return (NFS4ERR_OPENMODE);
8744 			}
8745 			ltype = F_RDLCK;
8746 			break;
8747 		case WRITE_LT:
8748 		case WRITEW_LT:
8749 			if ((sp->rs_share_access
8750 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8751 				rfs4_dbe_unlock(sp->rs_dbe);
8752 
8753 				return (NFS4ERR_OPENMODE);
8754 			}
8755 			ltype = F_WRLCK;
8756 			break;
8757 		}
8758 	} else
8759 		ltype = F_UNLCK;
8760 
8761 	flock.l_type = ltype;
8762 	flock.l_whence = 0;		/* SEEK_SET */
8763 	flock.l_start = offset;
8764 	flock.l_len = length;
8765 	flock.l_sysid = sysid;
8766 	flock.l_pid = lsp->rls_locker->rl_pid;
8767 
8768 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8769 	if (flock.l_len < 0 || flock.l_start < 0) {
8770 		rfs4_dbe_unlock(sp->rs_dbe);
8771 		return (NFS4ERR_INVAL);
8772 	}
8773 
8774 	/*
8775 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8776 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8777 	 */
8778 	flag = (int)sp->rs_share_access | F_REMOTELOCK;
8779 
8780 	vp = sp->rs_finfo->rf_vp;
8781 	VN_HOLD(vp);
8782 
8783 	/*
8784 	 * We need to unlock sp before we call the underlying filesystem to
8785 	 * acquire the file lock.
8786 	 */
8787 	rfs4_dbe_unlock(sp->rs_dbe);
8788 
8789 	error = setlock(vp, &flock, flag, cred);
8790 
8791 	/*
8792 	 * Make sure the file is still open.  In a case the file was closed in
8793 	 * the meantime, clean the lock we acquired using the setlock() call
8794 	 * above, and return the appropriate error.
8795 	 */
8796 	rfs4_dbe_lock(sp->rs_dbe);
8797 	if (sp->rs_closed == TRUE) {
8798 		cleanlocks(vp, lsp->rls_locker->rl_pid, sysid);
8799 		rfs4_dbe_unlock(sp->rs_dbe);
8800 
8801 		VN_RELE(vp);
8802 
8803 		return (NFS4ERR_OLD_STATEID);
8804 	}
8805 	rfs4_dbe_unlock(sp->rs_dbe);
8806 
8807 	VN_RELE(vp);
8808 
8809 	if (error == 0) {
8810 		rfs4_dbe_lock(lsp->rls_dbe);
8811 		next_stateid(&lsp->rls_lockid);
8812 		rfs4_dbe_unlock(lsp->rls_dbe);
8813 	}
8814 
8815 	/*
8816 	 * N.B. We map error values to nfsv4 errors. This is differrent
8817 	 * than puterrno4 routine.
8818 	 */
8819 	switch (error) {
8820 	case 0:
8821 		status = NFS4_OK;
8822 		break;
8823 	case EAGAIN:
8824 	case EACCES:		/* Old value */
8825 		/* Can only get here if op is OP_LOCK */
8826 		ASSERT(resop->resop == OP_LOCK);
8827 		lres = &resop->nfs_resop4_u.oplock;
8828 		status = NFS4ERR_DENIED;
8829 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8830 		    == NFS4ERR_EXPIRED)
8831 			goto retry;
8832 		break;
8833 	case ENOLCK:
8834 		status = NFS4ERR_DELAY;
8835 		break;
8836 	case EOVERFLOW:
8837 		status = NFS4ERR_INVAL;
8838 		break;
8839 	case EINVAL:
8840 		status = NFS4ERR_NOTSUPP;
8841 		break;
8842 	default:
8843 		status = NFS4ERR_SERVERFAULT;
8844 		break;
8845 	}
8846 
8847 	return (status);
8848 }
8849 
8850 /*ARGSUSED*/
8851 void
8852 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8853     struct svc_req *req, struct compound_state *cs)
8854 {
8855 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
8856 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8857 	nfsstat4 status;
8858 	stateid4 *stateid;
8859 	rfs4_lockowner_t *lo;
8860 	rfs4_client_t *cp;
8861 	rfs4_state_t *sp = NULL;
8862 	rfs4_lo_state_t *lsp = NULL;
8863 	bool_t ls_sw_held = FALSE;
8864 	bool_t create = TRUE;
8865 	bool_t lcreate = TRUE;
8866 	bool_t dup_lock = FALSE;
8867 	int rc;
8868 
8869 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8870 	    LOCK4args *, args);
8871 
8872 	if (cs->vp == NULL) {
8873 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8874 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8875 		    cs, LOCK4res *, resp);
8876 		return;
8877 	}
8878 
8879 	if (args->locker.new_lock_owner) {
8880 		/* Create a new lockowner for this instance */
8881 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8882 
8883 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8884 
8885 		stateid = &olo->open_stateid;
8886 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8887 		if (status != NFS4_OK) {
8888 			NFS4_DEBUG(rfs4_debug,
8889 			    (CE_NOTE, "Get state failed in lock %d", status));
8890 			*cs->statusp = resp->status = status;
8891 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8892 			    cs, LOCK4res *, resp);
8893 			return;
8894 		}
8895 
8896 		/* Ensure specified filehandle matches */
8897 		if (cs->vp != sp->rs_finfo->rf_vp) {
8898 			rfs4_state_rele(sp);
8899 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8900 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8901 			    cs, LOCK4res *, resp);
8902 			return;
8903 		}
8904 
8905 		/* hold off other access to open_owner while we tinker */
8906 		rfs4_sw_enter(&sp->rs_owner->ro_sw);
8907 
8908 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8909 		case NFS4_CHECK_STATEID_OLD:
8910 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8911 			goto end;
8912 		case NFS4_CHECK_STATEID_BAD:
8913 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8914 			goto end;
8915 		case NFS4_CHECK_STATEID_EXPIRED:
8916 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8917 			goto end;
8918 		case NFS4_CHECK_STATEID_UNCONFIRMED:
8919 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8920 			goto end;
8921 		case NFS4_CHECK_STATEID_CLOSED:
8922 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8923 			goto end;
8924 		case NFS4_CHECK_STATEID_OKAY:
8925 		case NFS4_CHECK_STATEID_REPLAY:
8926 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8927 			    sp->rs_owner, resop)) {
8928 			case NFS4_CHKSEQ_OKAY:
8929 				if (rc == NFS4_CHECK_STATEID_OKAY)
8930 					break;
8931 				/*
8932 				 * This is replayed stateid; if seqid
8933 				 * matches next expected, then client
8934 				 * is using wrong seqid.
8935 				 */
8936 				/* FALLTHROUGH */
8937 			case NFS4_CHKSEQ_BAD:
8938 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8939 				goto end;
8940 			case NFS4_CHKSEQ_REPLAY:
8941 				/* This is a duplicate LOCK request */
8942 				dup_lock = TRUE;
8943 
8944 				/*
8945 				 * For a duplicate we do not want to
8946 				 * create a new lockowner as it should
8947 				 * already exist.
8948 				 * Turn off the lockowner create flag.
8949 				 */
8950 				lcreate = FALSE;
8951 			}
8952 			break;
8953 		}
8954 
8955 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8956 		if (lo == NULL) {
8957 			NFS4_DEBUG(rfs4_debug,
8958 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
8959 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
8960 			goto end;
8961 		}
8962 
8963 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8964 		if (lsp == NULL) {
8965 			rfs4_update_lease(sp->rs_owner->ro_client);
8966 			/*
8967 			 * Only update theh open_seqid if this is not
8968 			 * a duplicate request
8969 			 */
8970 			if (dup_lock == FALSE) {
8971 				rfs4_update_open_sequence(sp->rs_owner);
8972 			}
8973 
8974 			NFS4_DEBUG(rfs4_debug,
8975 			    (CE_NOTE, "rfs4_op_lock: no state"));
8976 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8977 			rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8978 			rfs4_lockowner_rele(lo);
8979 			goto end;
8980 		}
8981 
8982 		/*
8983 		 * This is the new_lock_owner branch and the client is
8984 		 * supposed to be associating a new lock_owner with
8985 		 * the open file at this point.  If we find that a
8986 		 * lock_owner/state association already exists and a
8987 		 * successful LOCK request was returned to the client,
8988 		 * an error is returned to the client since this is
8989 		 * not appropriate.  The client should be using the
8990 		 * existing lock_owner branch.
8991 		 */
8992 		if (dup_lock == FALSE && create == FALSE) {
8993 			if (lsp->rls_lock_completed == TRUE) {
8994 				*cs->statusp =
8995 				    resp->status = NFS4ERR_BAD_SEQID;
8996 				rfs4_lockowner_rele(lo);
8997 				goto end;
8998 			}
8999 		}
9000 
9001 		rfs4_update_lease(sp->rs_owner->ro_client);
9002 
9003 		/*
9004 		 * Only update theh open_seqid if this is not
9005 		 * a duplicate request
9006 		 */
9007 		if (dup_lock == FALSE) {
9008 			rfs4_update_open_sequence(sp->rs_owner);
9009 		}
9010 
9011 		/*
9012 		 * If this is a duplicate lock request, just copy the
9013 		 * previously saved reply and return.
9014 		 */
9015 		if (dup_lock == TRUE) {
9016 			/* verify that lock_seqid's match */
9017 			if (lsp->rls_seqid != olo->lock_seqid) {
9018 				NFS4_DEBUG(rfs4_debug,
9019 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
9020 				    "lsp->seqid=%d old->seqid=%d",
9021 				    lsp->rls_seqid, olo->lock_seqid));
9022 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9023 			} else {
9024 				rfs4_copy_reply(resop, &lsp->rls_reply);
9025 				/*
9026 				 * Make sure to copy the just
9027 				 * retrieved reply status into the
9028 				 * overall compound status
9029 				 */
9030 				*cs->statusp = resp->status;
9031 			}
9032 			rfs4_lockowner_rele(lo);
9033 			goto end;
9034 		}
9035 
9036 		rfs4_dbe_lock(lsp->rls_dbe);
9037 
9038 		/* Make sure to update the lock sequence id */
9039 		lsp->rls_seqid = olo->lock_seqid;
9040 
9041 		NFS4_DEBUG(rfs4_debug,
9042 		    (CE_NOTE, "Lock seqid established as %d", lsp->rls_seqid));
9043 
9044 		/*
9045 		 * This is used to signify the newly created lockowner
9046 		 * stateid and its sequence number.  The checks for
9047 		 * sequence number and increment don't occur on the
9048 		 * very first lock request for a lockowner.
9049 		 */
9050 		lsp->rls_skip_seqid_check = TRUE;
9051 
9052 		/* hold off other access to lsp while we tinker */
9053 		rfs4_sw_enter(&lsp->rls_sw);
9054 		ls_sw_held = TRUE;
9055 
9056 		rfs4_dbe_unlock(lsp->rls_dbe);
9057 
9058 		rfs4_lockowner_rele(lo);
9059 	} else {
9060 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
9061 		/* get lsp and hold the lock on the underlying file struct */
9062 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
9063 		    != NFS4_OK) {
9064 			*cs->statusp = resp->status = status;
9065 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9066 			    cs, LOCK4res *, resp);
9067 			return;
9068 		}
9069 		create = FALSE;	/* We didn't create lsp */
9070 
9071 		/* Ensure specified filehandle matches */
9072 		if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9073 			rfs4_lo_state_rele(lsp, TRUE);
9074 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9075 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9076 			    cs, LOCK4res *, resp);
9077 			return;
9078 		}
9079 
9080 		/* hold off other access to lsp while we tinker */
9081 		rfs4_sw_enter(&lsp->rls_sw);
9082 		ls_sw_held = TRUE;
9083 
9084 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9085 		/*
9086 		 * The stateid looks like it was okay (expected to be
9087 		 * the next one)
9088 		 */
9089 		case NFS4_CHECK_STATEID_OKAY:
9090 			/*
9091 			 * The sequence id is now checked.  Determine
9092 			 * if this is a replay or if it is in the
9093 			 * expected (next) sequence.  In the case of a
9094 			 * replay, there are two replay conditions
9095 			 * that may occur.  The first is the normal
9096 			 * condition where a LOCK is done with a
9097 			 * NFS4_OK response and the stateid is
9098 			 * updated.  That case is handled below when
9099 			 * the stateid is identified as a REPLAY.  The
9100 			 * second is the case where an error is
9101 			 * returned, like NFS4ERR_DENIED, and the
9102 			 * sequence number is updated but the stateid
9103 			 * is not updated.  This second case is dealt
9104 			 * with here.  So it may seem odd that the
9105 			 * stateid is okay but the sequence id is a
9106 			 * replay but it is okay.
9107 			 */
9108 			switch (rfs4_check_lock_seqid(
9109 			    args->locker.locker4_u.lock_owner.lock_seqid,
9110 			    lsp, resop)) {
9111 			case NFS4_CHKSEQ_REPLAY:
9112 				if (resp->status != NFS4_OK) {
9113 					/*
9114 					 * Here is our replay and need
9115 					 * to verify that the last
9116 					 * response was an error.
9117 					 */
9118 					*cs->statusp = resp->status;
9119 					goto end;
9120 				}
9121 				/*
9122 				 * This is done since the sequence id
9123 				 * looked like a replay but it didn't
9124 				 * pass our check so a BAD_SEQID is
9125 				 * returned as a result.
9126 				 */
9127 				/*FALLTHROUGH*/
9128 			case NFS4_CHKSEQ_BAD:
9129 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9130 				goto end;
9131 			case NFS4_CHKSEQ_OKAY:
9132 				/* Everything looks okay move ahead */
9133 				break;
9134 			}
9135 			break;
9136 		case NFS4_CHECK_STATEID_OLD:
9137 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9138 			goto end;
9139 		case NFS4_CHECK_STATEID_BAD:
9140 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9141 			goto end;
9142 		case NFS4_CHECK_STATEID_EXPIRED:
9143 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
9144 			goto end;
9145 		case NFS4_CHECK_STATEID_CLOSED:
9146 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9147 			goto end;
9148 		case NFS4_CHECK_STATEID_REPLAY:
9149 			switch (rfs4_check_lock_seqid(
9150 			    args->locker.locker4_u.lock_owner.lock_seqid,
9151 			    lsp, resop)) {
9152 			case NFS4_CHKSEQ_OKAY:
9153 				/*
9154 				 * This is a replayed stateid; if
9155 				 * seqid matches the next expected,
9156 				 * then client is using wrong seqid.
9157 				 */
9158 			case NFS4_CHKSEQ_BAD:
9159 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9160 				goto end;
9161 			case NFS4_CHKSEQ_REPLAY:
9162 				rfs4_update_lease(lsp->rls_locker->rl_client);
9163 				*cs->statusp = status = resp->status;
9164 				goto end;
9165 			}
9166 			break;
9167 		default:
9168 			ASSERT(FALSE);
9169 			break;
9170 		}
9171 
9172 		rfs4_update_lock_sequence(lsp);
9173 		rfs4_update_lease(lsp->rls_locker->rl_client);
9174 	}
9175 
9176 	/*
9177 	 * NFS4 only allows locking on regular files, so
9178 	 * verify type of object.
9179 	 */
9180 	if (cs->vp->v_type != VREG) {
9181 		if (cs->vp->v_type == VDIR)
9182 			status = NFS4ERR_ISDIR;
9183 		else
9184 			status = NFS4ERR_INVAL;
9185 		goto out;
9186 	}
9187 
9188 	cp = lsp->rls_state->rs_owner->ro_client;
9189 
9190 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
9191 		status = NFS4ERR_GRACE;
9192 		goto out;
9193 	}
9194 
9195 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->rc_can_reclaim) {
9196 		status = NFS4ERR_NO_GRACE;
9197 		goto out;
9198 	}
9199 
9200 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
9201 		status = NFS4ERR_NO_GRACE;
9202 		goto out;
9203 	}
9204 
9205 	if (lsp->rls_state->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE)
9206 		cs->deleg = TRUE;
9207 
9208 	status = rfs4_do_lock(lsp, args->locktype,
9209 	    args->offset, args->length, cs->cr, resop);
9210 
9211 out:
9212 	lsp->rls_skip_seqid_check = FALSE;
9213 
9214 	*cs->statusp = resp->status = status;
9215 
9216 	if (status == NFS4_OK) {
9217 		resp->LOCK4res_u.lock_stateid = lsp->rls_lockid.stateid;
9218 		lsp->rls_lock_completed = TRUE;
9219 	}
9220 	/*
9221 	 * Only update the "OPEN" response here if this was a new
9222 	 * lock_owner
9223 	 */
9224 	if (sp)
9225 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
9226 
9227 	rfs4_update_lock_resp(lsp, resop);
9228 
9229 end:
9230 	if (lsp) {
9231 		if (ls_sw_held)
9232 			rfs4_sw_exit(&lsp->rls_sw);
9233 		/*
9234 		 * If an sp obtained, then the lsp does not represent
9235 		 * a lock on the file struct.
9236 		 */
9237 		if (sp != NULL)
9238 			rfs4_lo_state_rele(lsp, FALSE);
9239 		else
9240 			rfs4_lo_state_rele(lsp, TRUE);
9241 	}
9242 	if (sp) {
9243 		rfs4_sw_exit(&sp->rs_owner->ro_sw);
9244 		rfs4_state_rele(sp);
9245 	}
9246 
9247 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
9248 	    LOCK4res *, resp);
9249 }
9250 
9251 /* free function for LOCK/LOCKT */
9252 static void
9253 lock_denied_free(nfs_resop4 *resop)
9254 {
9255 	LOCK4denied *dp = NULL;
9256 
9257 	switch (resop->resop) {
9258 	case OP_LOCK:
9259 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
9260 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
9261 		break;
9262 	case OP_LOCKT:
9263 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
9264 			dp = &resop->nfs_resop4_u.oplockt.denied;
9265 		break;
9266 	default:
9267 		break;
9268 	}
9269 
9270 	if (dp)
9271 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
9272 }
9273 
9274 /*ARGSUSED*/
9275 void
9276 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
9277     struct svc_req *req, struct compound_state *cs)
9278 {
9279 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
9280 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
9281 	nfsstat4 status;
9282 	stateid4 *stateid = &args->lock_stateid;
9283 	rfs4_lo_state_t *lsp;
9284 
9285 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
9286 	    LOCKU4args *, args);
9287 
9288 	if (cs->vp == NULL) {
9289 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9290 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9291 		    LOCKU4res *, resp);
9292 		return;
9293 	}
9294 
9295 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
9296 		*cs->statusp = resp->status = status;
9297 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9298 		    LOCKU4res *, resp);
9299 		return;
9300 	}
9301 
9302 	/* Ensure specified filehandle matches */
9303 	if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9304 		rfs4_lo_state_rele(lsp, TRUE);
9305 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9306 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9307 		    LOCKU4res *, resp);
9308 		return;
9309 	}
9310 
9311 	/* hold off other access to lsp while we tinker */
9312 	rfs4_sw_enter(&lsp->rls_sw);
9313 
9314 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9315 	case NFS4_CHECK_STATEID_OKAY:
9316 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
9317 		    != NFS4_CHKSEQ_OKAY) {
9318 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9319 			goto end;
9320 		}
9321 		break;
9322 	case NFS4_CHECK_STATEID_OLD:
9323 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9324 		goto end;
9325 	case NFS4_CHECK_STATEID_BAD:
9326 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9327 		goto end;
9328 	case NFS4_CHECK_STATEID_EXPIRED:
9329 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
9330 		goto end;
9331 	case NFS4_CHECK_STATEID_CLOSED:
9332 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9333 		goto end;
9334 	case NFS4_CHECK_STATEID_REPLAY:
9335 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
9336 		case NFS4_CHKSEQ_OKAY:
9337 				/*
9338 				 * This is a replayed stateid; if
9339 				 * seqid matches the next expected,
9340 				 * then client is using wrong seqid.
9341 				 */
9342 		case NFS4_CHKSEQ_BAD:
9343 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9344 			goto end;
9345 		case NFS4_CHKSEQ_REPLAY:
9346 			rfs4_update_lease(lsp->rls_locker->rl_client);
9347 			*cs->statusp = status = resp->status;
9348 			goto end;
9349 		}
9350 		break;
9351 	default:
9352 		ASSERT(FALSE);
9353 		break;
9354 	}
9355 
9356 	rfs4_update_lock_sequence(lsp);
9357 	rfs4_update_lease(lsp->rls_locker->rl_client);
9358 
9359 	/*
9360 	 * NFS4 only allows locking on regular files, so
9361 	 * verify type of object.
9362 	 */
9363 	if (cs->vp->v_type != VREG) {
9364 		if (cs->vp->v_type == VDIR)
9365 			status = NFS4ERR_ISDIR;
9366 		else
9367 			status = NFS4ERR_INVAL;
9368 		goto out;
9369 	}
9370 
9371 	if (rfs4_clnt_in_grace(lsp->rls_state->rs_owner->ro_client)) {
9372 		status = NFS4ERR_GRACE;
9373 		goto out;
9374 	}
9375 
9376 	status = rfs4_do_lock(lsp, args->locktype,
9377 	    args->offset, args->length, cs->cr, resop);
9378 
9379 out:
9380 	*cs->statusp = resp->status = status;
9381 
9382 	if (status == NFS4_OK)
9383 		resp->lock_stateid = lsp->rls_lockid.stateid;
9384 
9385 	rfs4_update_lock_resp(lsp, resop);
9386 
9387 end:
9388 	rfs4_sw_exit(&lsp->rls_sw);
9389 	rfs4_lo_state_rele(lsp, TRUE);
9390 
9391 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9392 	    LOCKU4res *, resp);
9393 }
9394 
9395 /*
9396  * LOCKT is a best effort routine, the client can not be guaranteed that
9397  * the status return is still in effect by the time the reply is received.
9398  * They are numerous race conditions in this routine, but we are not required
9399  * and can not be accurate.
9400  */
9401 /*ARGSUSED*/
9402 void
9403 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
9404     struct svc_req *req, struct compound_state *cs)
9405 {
9406 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
9407 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
9408 	rfs4_lockowner_t *lo;
9409 	rfs4_client_t *cp;
9410 	bool_t create = FALSE;
9411 	struct flock64 flk;
9412 	int error;
9413 	int flag = FREAD | FWRITE;
9414 	int ltype;
9415 	length4 posix_length;
9416 	sysid_t sysid;
9417 	pid_t pid;
9418 
9419 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
9420 	    LOCKT4args *, args);
9421 
9422 	if (cs->vp == NULL) {
9423 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9424 		goto out;
9425 	}
9426 
9427 	/*
9428 	 * NFS4 only allows locking on regular files, so
9429 	 * verify type of object.
9430 	 */
9431 	if (cs->vp->v_type != VREG) {
9432 		if (cs->vp->v_type == VDIR)
9433 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
9434 		else
9435 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
9436 		goto out;
9437 	}
9438 
9439 	/*
9440 	 * Check out the clientid to ensure the server knows about it
9441 	 * so that we correctly inform the client of a server reboot.
9442 	 */
9443 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
9444 	    == NULL) {
9445 		*cs->statusp = resp->status =
9446 		    rfs4_check_clientid(&args->owner.clientid, 0);
9447 		goto out;
9448 	}
9449 	if (rfs4_lease_expired(cp)) {
9450 		rfs4_client_close(cp);
9451 		/*
9452 		 * Protocol doesn't allow returning NFS4ERR_STALE as
9453 		 * other operations do on this check so STALE_CLIENTID
9454 		 * is returned instead
9455 		 */
9456 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
9457 		goto out;
9458 	}
9459 
9460 	if (rfs4_clnt_in_grace(cp) && !(cp->rc_can_reclaim)) {
9461 		*cs->statusp = resp->status = NFS4ERR_GRACE;
9462 		rfs4_client_rele(cp);
9463 		goto out;
9464 	}
9465 	rfs4_client_rele(cp);
9466 
9467 	resp->status = NFS4_OK;
9468 
9469 	switch (args->locktype) {
9470 	case READ_LT:
9471 	case READW_LT:
9472 		ltype = F_RDLCK;
9473 		break;
9474 	case WRITE_LT:
9475 	case WRITEW_LT:
9476 		ltype = F_WRLCK;
9477 		break;
9478 	}
9479 
9480 	posix_length = args->length;
9481 	/* Check for zero length. To lock to end of file use all ones for V4 */
9482 	if (posix_length == 0) {
9483 		*cs->statusp = resp->status = NFS4ERR_INVAL;
9484 		goto out;
9485 	} else if (posix_length == (length4)(~0)) {
9486 		posix_length = 0;	/* Posix to end of file  */
9487 	}
9488 
9489 	/* Find or create a lockowner */
9490 	lo = rfs4_findlockowner(&args->owner, &create);
9491 
9492 	if (lo) {
9493 		pid = lo->rl_pid;
9494 		if ((resp->status =
9495 		    rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
9496 			goto err;
9497 	} else {
9498 		pid = 0;
9499 		sysid = lockt_sysid;
9500 	}
9501 retry:
9502 	flk.l_type = ltype;
9503 	flk.l_whence = 0;		/* SEEK_SET */
9504 	flk.l_start = args->offset;
9505 	flk.l_len = posix_length;
9506 	flk.l_sysid = sysid;
9507 	flk.l_pid = pid;
9508 	flag |= F_REMOTELOCK;
9509 
9510 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
9511 
9512 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
9513 	if (flk.l_len < 0 || flk.l_start < 0) {
9514 		resp->status = NFS4ERR_INVAL;
9515 		goto err;
9516 	}
9517 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9518 	    NULL, cs->cr, NULL);
9519 
9520 	/*
9521 	 * N.B. We map error values to nfsv4 errors. This is differrent
9522 	 * than puterrno4 routine.
9523 	 */
9524 	switch (error) {
9525 	case 0:
9526 		if (flk.l_type == F_UNLCK)
9527 			resp->status = NFS4_OK;
9528 		else {
9529 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
9530 				goto retry;
9531 			resp->status = NFS4ERR_DENIED;
9532 		}
9533 		break;
9534 	case EOVERFLOW:
9535 		resp->status = NFS4ERR_INVAL;
9536 		break;
9537 	case EINVAL:
9538 		resp->status = NFS4ERR_NOTSUPP;
9539 		break;
9540 	default:
9541 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
9542 		    error);
9543 		resp->status = NFS4ERR_SERVERFAULT;
9544 		break;
9545 	}
9546 
9547 err:
9548 	if (lo)
9549 		rfs4_lockowner_rele(lo);
9550 	*cs->statusp = resp->status;
9551 out:
9552 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9553 	    LOCKT4res *, resp);
9554 }
9555 
9556 int
9557 rfs4_share(rfs4_state_t *sp, uint32_t access, uint32_t deny)
9558 {
9559 	int err;
9560 	int cmd;
9561 	vnode_t *vp;
9562 	struct shrlock shr;
9563 	struct shr_locowner shr_loco;
9564 	int fflags = 0;
9565 
9566 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9567 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9568 
9569 	if (sp->rs_closed)
9570 		return (NFS4ERR_OLD_STATEID);
9571 
9572 	vp = sp->rs_finfo->rf_vp;
9573 	ASSERT(vp);
9574 
9575 	shr.s_access = shr.s_deny = 0;
9576 
9577 	if (access & OPEN4_SHARE_ACCESS_READ) {
9578 		fflags |= FREAD;
9579 		shr.s_access |= F_RDACC;
9580 	}
9581 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
9582 		fflags |= FWRITE;
9583 		shr.s_access |= F_WRACC;
9584 	}
9585 	ASSERT(shr.s_access);
9586 
9587 	if (deny & OPEN4_SHARE_DENY_READ)
9588 		shr.s_deny |= F_RDDNY;
9589 	if (deny & OPEN4_SHARE_DENY_WRITE)
9590 		shr.s_deny |= F_WRDNY;
9591 
9592 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9593 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9594 	shr_loco.sl_pid = shr.s_pid;
9595 	shr_loco.sl_id = shr.s_sysid;
9596 	shr.s_owner = (caddr_t)&shr_loco;
9597 	shr.s_own_len = sizeof (shr_loco);
9598 
9599 	cmd = nbl_need_check(vp) ? F_SHARE_NBMAND : F_SHARE;
9600 
9601 	err = VOP_SHRLOCK(vp, cmd, &shr, fflags, CRED(), NULL);
9602 	if (err != 0) {
9603 		if (err == EAGAIN)
9604 			err = NFS4ERR_SHARE_DENIED;
9605 		else
9606 			err = puterrno4(err);
9607 		return (err);
9608 	}
9609 
9610 	sp->rs_share_access |= access;
9611 	sp->rs_share_deny |= deny;
9612 
9613 	return (0);
9614 }
9615 
9616 int
9617 rfs4_unshare(rfs4_state_t *sp)
9618 {
9619 	int err;
9620 	struct shrlock shr;
9621 	struct shr_locowner shr_loco;
9622 
9623 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9624 
9625 	if (sp->rs_closed || sp->rs_share_access == 0)
9626 		return (0);
9627 
9628 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9629 	ASSERT(sp->rs_finfo->rf_vp);
9630 
9631 	shr.s_access = shr.s_deny = 0;
9632 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9633 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9634 	shr_loco.sl_pid = shr.s_pid;
9635 	shr_loco.sl_id = shr.s_sysid;
9636 	shr.s_owner = (caddr_t)&shr_loco;
9637 	shr.s_own_len = sizeof (shr_loco);
9638 
9639 	err = VOP_SHRLOCK(sp->rs_finfo->rf_vp, F_UNSHARE, &shr, 0, CRED(),
9640 	    NULL);
9641 	if (err != 0) {
9642 		err = puterrno4(err);
9643 		return (err);
9644 	}
9645 
9646 	sp->rs_share_access = 0;
9647 	sp->rs_share_deny = 0;
9648 
9649 	return (0);
9650 
9651 }
9652 
9653 static int
9654 rdma_setup_read_data4(READ4args *args, READ4res *rok)
9655 {
9656 	struct clist	*wcl;
9657 	count4		count = rok->data_len;
9658 	int		wlist_len;
9659 
9660 	wcl = args->wlist;
9661 	if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) {
9662 		return (FALSE);
9663 	}
9664 	wcl = args->wlist;
9665 	rok->wlist_len = wlist_len;
9666 	rok->wlist = wcl;
9667 	return (TRUE);
9668 }
9669 
9670 /* tunable to disable server referrals */
9671 int rfs4_no_referrals = 0;
9672 
9673 /*
9674  * Find an NFS record in reparse point data.
9675  * Returns 0 for success and <0 or an errno value on failure.
9676  */
9677 int
9678 vn_find_nfs_record(vnode_t *vp, nvlist_t **nvlp, char **svcp, char **datap)
9679 {
9680 	int err;
9681 	char *stype, *val;
9682 	nvlist_t *nvl;
9683 	nvpair_t *curr;
9684 
9685 	if ((nvl = reparse_init()) == NULL)
9686 		return (-1);
9687 
9688 	if ((err = reparse_vnode_parse(vp, nvl)) != 0) {
9689 		reparse_free(nvl);
9690 		return (err);
9691 	}
9692 
9693 	curr = NULL;
9694 	while ((curr = nvlist_next_nvpair(nvl, curr)) != NULL) {
9695 		if ((stype = nvpair_name(curr)) == NULL) {
9696 			reparse_free(nvl);
9697 			return (-2);
9698 		}
9699 		if (strncasecmp(stype, "NFS", 3) == 0)
9700 			break;
9701 	}
9702 
9703 	if ((curr == NULL) ||
9704 	    (nvpair_value_string(curr, &val))) {
9705 		reparse_free(nvl);
9706 		return (-3);
9707 	}
9708 	*nvlp = nvl;
9709 	*svcp = stype;
9710 	*datap = val;
9711 	return (0);
9712 }
9713 
9714 int
9715 vn_is_nfs_reparse(vnode_t *vp, cred_t *cr)
9716 {
9717 	nvlist_t *nvl;
9718 	char *s, *d;
9719 
9720 	if (rfs4_no_referrals != 0)
9721 		return (B_FALSE);
9722 
9723 	if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9724 		return (B_FALSE);
9725 
9726 	if (vn_find_nfs_record(vp, &nvl, &s, &d) != 0)
9727 		return (B_FALSE);
9728 
9729 	reparse_free(nvl);
9730 
9731 	return (B_TRUE);
9732 }
9733 
9734 /*
9735  * There is a user-level copy of this routine in ref_subr.c.
9736  * Changes should be kept in sync.
9737  */
9738 static int
9739 nfs4_create_components(char *path, component4 *comp4)
9740 {
9741 	int slen, plen, ncomp;
9742 	char *ori_path, *nxtc, buf[MAXNAMELEN];
9743 
9744 	if (path == NULL)
9745 		return (0);
9746 
9747 	plen = strlen(path) + 1;	/* include the terminator */
9748 	ori_path = path;
9749 	ncomp = 0;
9750 
9751 	/* count number of components in the path */
9752 	for (nxtc = path; nxtc < ori_path + plen; nxtc++) {
9753 		if (*nxtc == '/' || *nxtc == '\0' || *nxtc == '\n') {
9754 			if ((slen = nxtc - path) == 0) {
9755 				path = nxtc + 1;
9756 				continue;
9757 			}
9758 
9759 			if (comp4 != NULL) {
9760 				bcopy(path, buf, slen);
9761 				buf[slen] = '\0';
9762 				(void) str_to_utf8(buf, &comp4[ncomp]);
9763 			}
9764 
9765 			ncomp++;	/* 1 valid component */
9766 			path = nxtc + 1;
9767 		}
9768 		if (*nxtc == '\0' || *nxtc == '\n')
9769 			break;
9770 	}
9771 
9772 	return (ncomp);
9773 }
9774 
9775 /*
9776  * There is a user-level copy of this routine in ref_subr.c.
9777  * Changes should be kept in sync.
9778  */
9779 static int
9780 make_pathname4(char *path, pathname4 *pathname)
9781 {
9782 	int ncomp;
9783 	component4 *comp4;
9784 
9785 	if (pathname == NULL)
9786 		return (0);
9787 
9788 	if (path == NULL) {
9789 		pathname->pathname4_val = NULL;
9790 		pathname->pathname4_len = 0;
9791 		return (0);
9792 	}
9793 
9794 	/* count number of components to alloc buffer */
9795 	if ((ncomp = nfs4_create_components(path, NULL)) == 0) {
9796 		pathname->pathname4_val = NULL;
9797 		pathname->pathname4_len = 0;
9798 		return (0);
9799 	}
9800 	comp4 = kmem_zalloc(ncomp * sizeof (component4), KM_SLEEP);
9801 
9802 	/* copy components into allocated buffer */
9803 	ncomp = nfs4_create_components(path, comp4);
9804 
9805 	pathname->pathname4_val = comp4;
9806 	pathname->pathname4_len = ncomp;
9807 
9808 	return (ncomp);
9809 }
9810 
9811 #define	xdr_fs_locations4 xdr_fattr4_fs_locations
9812 
9813 fs_locations4 *
9814 fetch_referral(vnode_t *vp, cred_t *cr)
9815 {
9816 	nvlist_t *nvl;
9817 	char *stype, *sdata;
9818 	fs_locations4 *result;
9819 	char buf[1024];
9820 	size_t bufsize;
9821 	XDR xdr;
9822 	int err;
9823 
9824 	/*
9825 	 * Check attrs to ensure it's a reparse point
9826 	 */
9827 	if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9828 		return (NULL);
9829 
9830 	/*
9831 	 * Look for an NFS record and get the type and data
9832 	 */
9833 	if (vn_find_nfs_record(vp, &nvl, &stype, &sdata) != 0)
9834 		return (NULL);
9835 
9836 	/*
9837 	 * With the type and data, upcall to get the referral
9838 	 */
9839 	bufsize = sizeof (buf);
9840 	bzero(buf, sizeof (buf));
9841 	err = reparse_kderef((const char *)stype, (const char *)sdata,
9842 	    buf, &bufsize);
9843 	reparse_free(nvl);
9844 
9845 	DTRACE_PROBE4(nfs4serv__func__referral__upcall,
9846 	    char *, stype, char *, sdata, char *, buf, int, err);
9847 	if (err) {
9848 		cmn_err(CE_NOTE,
9849 		    "reparsed daemon not running: unable to get referral (%d)",
9850 		    err);
9851 		return (NULL);
9852 	}
9853 
9854 	/*
9855 	 * We get an XDR'ed record back from the kderef call
9856 	 */
9857 	xdrmem_create(&xdr, buf, bufsize, XDR_DECODE);
9858 	result = kmem_alloc(sizeof (fs_locations4), KM_SLEEP);
9859 	err = xdr_fs_locations4(&xdr, result);
9860 	XDR_DESTROY(&xdr);
9861 	if (err != TRUE) {
9862 		DTRACE_PROBE1(nfs4serv__func__referral__upcall__xdrfail,
9863 		    int, err);
9864 		return (NULL);
9865 	}
9866 
9867 	/*
9868 	 * Look at path to recover fs_root, ignoring the leading '/'
9869 	 */
9870 	(void) make_pathname4(vp->v_path, &result->fs_root);
9871 
9872 	return (result);
9873 }
9874 
9875 char *
9876 build_symlink(vnode_t *vp, cred_t *cr, size_t *strsz)
9877 {
9878 	fs_locations4 *fsl;
9879 	fs_location4 *fs;
9880 	char *server, *path, *symbuf;
9881 	static char *prefix = "/net/";
9882 	int i, size, npaths;
9883 	uint_t len;
9884 
9885 	/* Get the referral */
9886 	if ((fsl = fetch_referral(vp, cr)) == NULL)
9887 		return (NULL);
9888 
9889 	/* Deal with only the first location and first server */
9890 	fs = &fsl->locations_val[0];
9891 	server = utf8_to_str(&fs->server_val[0], &len, NULL);
9892 	if (server == NULL) {
9893 		rfs4_free_fs_locations4(fsl);
9894 		kmem_free(fsl, sizeof (fs_locations4));
9895 		return (NULL);
9896 	}
9897 
9898 	/* Figure out size for "/net/" + host + /path/path/path + NULL */
9899 	size = strlen(prefix) + len;
9900 	for (i = 0; i < fs->rootpath.pathname4_len; i++)
9901 		size += fs->rootpath.pathname4_val[i].utf8string_len + 1;
9902 
9903 	/* Allocate the symlink buffer and fill it */
9904 	symbuf = kmem_zalloc(size, KM_SLEEP);
9905 	(void) strcat(symbuf, prefix);
9906 	(void) strcat(symbuf, server);
9907 	kmem_free(server, len);
9908 
9909 	npaths = 0;
9910 	for (i = 0; i < fs->rootpath.pathname4_len; i++) {
9911 		path = utf8_to_str(&fs->rootpath.pathname4_val[i], &len, NULL);
9912 		if (path == NULL)
9913 			continue;
9914 		(void) strcat(symbuf, "/");
9915 		(void) strcat(symbuf, path);
9916 		npaths++;
9917 		kmem_free(path, len);
9918 	}
9919 
9920 	rfs4_free_fs_locations4(fsl);
9921 	kmem_free(fsl, sizeof (fs_locations4));
9922 
9923 	if (strsz != NULL)
9924 		*strsz = size;
9925 	return (symbuf);
9926 }
9927 
9928 /*
9929  * Check to see if we have a downrev Solaris client, so that we
9930  * can send it a symlink instead of a referral.
9931  */
9932 int
9933 client_is_downrev(struct svc_req *req)
9934 {
9935 	struct sockaddr *ca;
9936 	rfs4_clntip_t *ci;
9937 	bool_t create = FALSE;
9938 	int is_downrev;
9939 
9940 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
9941 	ASSERT(ca);
9942 	ci = rfs4_find_clntip(ca, &create);
9943 	if (ci == NULL)
9944 		return (0);
9945 	is_downrev = ci->ri_no_referrals;
9946 	rfs4_dbe_rele(ci->ri_dbe);
9947 	return (is_downrev);
9948 }
9949 
9950 /*
9951  * Do the main work of handling HA-NFSv4 Resource Group failover on
9952  * Sun Cluster.
9953  * We need to detect whether any RG admin paths have been added or removed,
9954  * and adjust resources accordingly.
9955  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
9956  * order to scale, the list and array of paths need to be held in more
9957  * suitable data structures.
9958  */
9959 static void
9960 hanfsv4_failover(nfs4_srv_t *nsrv4)
9961 {
9962 	int i, start_grace, numadded_paths = 0;
9963 	char **added_paths = NULL;
9964 	rfs4_dss_path_t *dss_path;
9965 
9966 	/*
9967 	 * Note: currently, dss_pathlist cannot be NULL, since
9968 	 * it will always include an entry for NFS4_DSS_VAR_DIR. If we
9969 	 * make the latter dynamically specified too, the following will
9970 	 * need to be adjusted.
9971 	 */
9972 
9973 	/*
9974 	 * First, look for removed paths: RGs that have been failed-over
9975 	 * away from this node.
9976 	 * Walk the "currently-serving" dss_pathlist and, for each
9977 	 * path, check if it is on the "passed-in" rfs4_dss_newpaths array
9978 	 * from nfsd. If not, that RG path has been removed.
9979 	 *
9980 	 * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
9981 	 * any duplicates.
9982 	 */
9983 	dss_path = nsrv4->dss_pathlist;
9984 	do {
9985 		int found = 0;
9986 		char *path = dss_path->path;
9987 
9988 		/* used only for non-HA so may not be removed */
9989 		if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
9990 			dss_path = dss_path->next;
9991 			continue;
9992 		}
9993 
9994 		for (i = 0; i < rfs4_dss_numnewpaths; i++) {
9995 			int cmpret;
9996 			char *newpath = rfs4_dss_newpaths[i];
9997 
9998 			/*
9999 			 * Since nfsd has sorted rfs4_dss_newpaths for us,
10000 			 * once the return from strcmp is negative we know
10001 			 * we've passed the point where "path" should be,
10002 			 * and can stop searching: "path" has been removed.
10003 			 */
10004 			cmpret = strcmp(path, newpath);
10005 			if (cmpret < 0)
10006 				break;
10007 			if (cmpret == 0) {
10008 				found = 1;
10009 				break;
10010 			}
10011 		}
10012 
10013 		if (found == 0) {
10014 			unsigned index = dss_path->index;
10015 			rfs4_servinst_t *sip = dss_path->sip;
10016 			rfs4_dss_path_t *path_next = dss_path->next;
10017 
10018 			/*
10019 			 * This path has been removed.
10020 			 * We must clear out the servinst reference to
10021 			 * it, since it's now owned by another
10022 			 * node: we should not attempt to touch it.
10023 			 */
10024 			ASSERT(dss_path == sip->dss_paths[index]);
10025 			sip->dss_paths[index] = NULL;
10026 
10027 			/* remove from "currently-serving" list, and destroy */
10028 			remque(dss_path);
10029 			/* allow for NUL */
10030 			kmem_free(dss_path->path, strlen(dss_path->path) + 1);
10031 			kmem_free(dss_path, sizeof (rfs4_dss_path_t));
10032 
10033 			dss_path = path_next;
10034 		} else {
10035 			/* path was found; not removed */
10036 			dss_path = dss_path->next;
10037 		}
10038 	} while (dss_path != nsrv4->dss_pathlist);
10039 
10040 	/*
10041 	 * Now, look for added paths: RGs that have been failed-over
10042 	 * to this node.
10043 	 * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
10044 	 * for each path, check if it is on the "currently-serving"
10045 	 * dss_pathlist. If not, that RG path has been added.
10046 	 *
10047 	 * Note: we don't do duplicate detection here; nfsd does that for us.
10048 	 *
10049 	 * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
10050 	 * an upper bound for the size needed for added_paths[numadded_paths].
10051 	 */
10052 
10053 	/* probably more space than we need, but guaranteed to be enough */
10054 	if (rfs4_dss_numnewpaths > 0) {
10055 		size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
10056 		added_paths = kmem_zalloc(sz, KM_SLEEP);
10057 	}
10058 
10059 	/* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
10060 	for (i = 0; i < rfs4_dss_numnewpaths; i++) {
10061 		int found = 0;
10062 		char *newpath = rfs4_dss_newpaths[i];
10063 
10064 		dss_path = nsrv4->dss_pathlist;
10065 		do {
10066 			char *path = dss_path->path;
10067 
10068 			/* used only for non-HA */
10069 			if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
10070 				dss_path = dss_path->next;
10071 				continue;
10072 			}
10073 
10074 			if (strncmp(path, newpath, strlen(path)) == 0) {
10075 				found = 1;
10076 				break;
10077 			}
10078 
10079 			dss_path = dss_path->next;
10080 		} while (dss_path != nsrv4->dss_pathlist);
10081 
10082 		if (found == 0) {
10083 			added_paths[numadded_paths] = newpath;
10084 			numadded_paths++;
10085 		}
10086 	}
10087 
10088 	/* did we find any added paths? */
10089 	if (numadded_paths > 0) {
10090 
10091 		/* create a new server instance, and start its grace period */
10092 		start_grace = 1;
10093 		/* CSTYLED */
10094 		rfs4_servinst_create(nsrv4, start_grace, numadded_paths, added_paths);
10095 
10096 		/* read in the stable storage state from these paths */
10097 		rfs4_dss_readstate(nsrv4, numadded_paths, added_paths);
10098 
10099 		/*
10100 		 * Multiple failovers during a grace period will cause
10101 		 * clients of the same resource group to be partitioned
10102 		 * into different server instances, with different
10103 		 * grace periods.  Since clients of the same resource
10104 		 * group must be subject to the same grace period,
10105 		 * we need to reset all currently active grace periods.
10106 		 */
10107 		rfs4_grace_reset_all(nsrv4);
10108 	}
10109 
10110 	if (rfs4_dss_numnewpaths > 0)
10111 		kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
10112 }
10113