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