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