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