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