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