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