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