xref: /titanic_41/usr/src/uts/common/fs/nfs/nfs4_srv.c (revision e0ad97e30ea0a9af63c42d71690b5f387c763420)
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 amodes;
6590 	uint32_t dmodes;
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 	dmodes = (deny & ~sp->rs_share_deny);
6644 	amodes = (access & ~sp->rs_share_access);
6645 
6646 	first_open = (sp->rs_share_access & OPEN4_SHARE_ACCESS_BOTH) == 0;
6647 
6648 	/*
6649 	 * Check to see the client has already sent an open for this
6650 	 * open owner on this file with the same share/deny modes.
6651 	 * If so, we don't need to check for a conflict and we don't
6652 	 * need to add another shrlock.  If not, then we need to
6653 	 * check for conflicts in deny and access before checking for
6654 	 * conflicts in delegation.  We don't want to recall a
6655 	 * delegation based on an open that will eventually fail based
6656 	 * on shares modes.
6657 	 */
6658 
6659 	if (dmodes || amodes) {
6660 		if ((err = rfs4_share(sp, access, deny)) != 0) {
6661 			rfs4_dbe_unlock(sp->rs_dbe);
6662 			resp->status = err;
6663 
6664 			rfs4_file_rele(fp);
6665 			/* Not a fully formed open; "close" it */
6666 			if (screate == TRUE)
6667 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6668 			rfs4_state_rele(sp);
6669 			return;
6670 		}
6671 	}
6672 
6673 	rfs4_dbe_lock(fp->rf_dbe);
6674 
6675 	/*
6676 	 * Check to see if this file is delegated and if so, if a
6677 	 * recall needs to be done.
6678 	 */
6679 	if (rfs4_check_recall(sp, access)) {
6680 		rfs4_dbe_unlock(fp->rf_dbe);
6681 		rfs4_dbe_unlock(sp->rs_dbe);
6682 		rfs4_recall_deleg(fp, FALSE, sp->rs_owner->ro_client);
6683 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
6684 		rfs4_dbe_lock(sp->rs_dbe);
6685 
6686 		/* if state closed while lock was dropped */
6687 		if (sp->rs_closed) {
6688 			if (dmodes || amodes)
6689 				(void) rfs4_unshare(sp);
6690 			rfs4_dbe_unlock(sp->rs_dbe);
6691 			rfs4_file_rele(fp);
6692 			/* Not a fully formed open; "close" it */
6693 			if (screate == TRUE)
6694 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6695 			rfs4_state_rele(sp);
6696 			resp->status = NFS4ERR_OLD_STATEID;
6697 			return;
6698 		}
6699 
6700 		rfs4_dbe_lock(fp->rf_dbe);
6701 		/* Let's see if the delegation was returned */
6702 		if (rfs4_check_recall(sp, access)) {
6703 			rfs4_dbe_unlock(fp->rf_dbe);
6704 			if (dmodes || amodes)
6705 				(void) rfs4_unshare(sp);
6706 			rfs4_dbe_unlock(sp->rs_dbe);
6707 			rfs4_file_rele(fp);
6708 			rfs4_update_lease(sp->rs_owner->ro_client);
6709 
6710 			/* Not a fully formed open; "close" it */
6711 			if (screate == TRUE)
6712 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6713 			rfs4_state_rele(sp);
6714 			resp->status = NFS4ERR_DELAY;
6715 			return;
6716 		}
6717 	}
6718 	/*
6719 	 * the share check passed and any delegation conflict has been
6720 	 * taken care of, now call vop_open.
6721 	 * if this is the first open then call vop_open with fflags.
6722 	 * if not, call vn_open_upgrade with just the upgrade flags.
6723 	 *
6724 	 * if the file has been opened already, it will have the current
6725 	 * access mode in the state struct.  if it has no share access, then
6726 	 * this is a new open.
6727 	 *
6728 	 * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6729 	 * call VOP_OPEN(), just do the open upgrade.
6730 	 */
6731 	if (first_open && !deleg_cur) {
6732 		ct.cc_sysid = sysid;
6733 		ct.cc_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
6734 		ct.cc_caller_id = nfs4_srv_caller_id;
6735 		ct.cc_flags = CC_DONTBLOCK;
6736 		err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6737 		if (err) {
6738 			rfs4_dbe_unlock(fp->rf_dbe);
6739 			if (dmodes || amodes)
6740 				(void) rfs4_unshare(sp);
6741 			rfs4_dbe_unlock(sp->rs_dbe);
6742 			rfs4_file_rele(fp);
6743 
6744 			/* Not a fully formed open; "close" it */
6745 			if (screate == TRUE)
6746 				rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6747 			rfs4_state_rele(sp);
6748 			/* check if a monitor detected a delegation conflict */
6749 			if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6750 				resp->status = NFS4ERR_DELAY;
6751 			else
6752 				resp->status = NFS4ERR_SERVERFAULT;
6753 			return;
6754 		}
6755 	} else { /* open upgrade */
6756 		/*
6757 		 * calculate the fflags for the new mode that is being added
6758 		 * by this upgrade.
6759 		 */
6760 		fflags = 0;
6761 		if (amodes & OPEN4_SHARE_ACCESS_READ)
6762 			fflags |= FREAD;
6763 		if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6764 			fflags |= FWRITE;
6765 		vn_open_upgrade(cs->vp, fflags);
6766 	}
6767 	sp->rs_opened = TRUE;
6768 
6769 	if (dmodes & OPEN4_SHARE_DENY_READ)
6770 		fp->rf_deny_read++;
6771 	if (dmodes & OPEN4_SHARE_DENY_WRITE)
6772 		fp->rf_deny_write++;
6773 	fp->rf_share_deny |= deny;
6774 
6775 	if (amodes & OPEN4_SHARE_ACCESS_READ)
6776 		fp->rf_access_read++;
6777 	if (amodes & OPEN4_SHARE_ACCESS_WRITE)
6778 		fp->rf_access_write++;
6779 	fp->rf_share_access |= access;
6780 
6781 	/*
6782 	 * Check for delegation here. if the deleg argument is not
6783 	 * DELEG_ANY, then this is a reclaim from a client and
6784 	 * we must honor the delegation requested. If necessary we can
6785 	 * set the recall flag.
6786 	 */
6787 
6788 	dsp = rfs4_grant_delegation(deleg, sp, &recall);
6789 
6790 	cs->deleg = (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE);
6791 
6792 	next_stateid(&sp->rs_stateid);
6793 
6794 	resp->stateid = sp->rs_stateid.stateid;
6795 
6796 	rfs4_dbe_unlock(fp->rf_dbe);
6797 	rfs4_dbe_unlock(sp->rs_dbe);
6798 
6799 	if (dsp) {
6800 		rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6801 		rfs4_deleg_state_rele(dsp);
6802 	}
6803 
6804 	rfs4_file_rele(fp);
6805 	rfs4_state_rele(sp);
6806 
6807 	resp->status = NFS4_OK;
6808 }
6809 
6810 /*ARGSUSED*/
6811 static void
6812 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6813     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6814 {
6815 	change_info4 *cinfo = &resp->cinfo;
6816 	bitmap4 *attrset = &resp->attrset;
6817 
6818 	if (args->opentype == OPEN4_NOCREATE)
6819 		resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6820 		    req, cs, args->share_access, cinfo);
6821 	else {
6822 		/* inhibit delegation grants during exclusive create */
6823 
6824 		if (args->mode == EXCLUSIVE4)
6825 			rfs4_disable_delegation();
6826 
6827 		resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
6828 		    oo->ro_client->rc_clientid);
6829 	}
6830 
6831 	if (resp->status == NFS4_OK) {
6832 
6833 		/* cs->vp cs->fh now reference the desired file */
6834 
6835 		rfs4_do_open(cs, req, oo, DELEG_ANY, args->share_access,
6836 		    args->share_deny, resp, 0);
6837 
6838 		/*
6839 		 * If rfs4_createfile set attrset, we must
6840 		 * clear this attrset before the response is copied.
6841 		 */
6842 		if (resp->status != NFS4_OK && resp->attrset) {
6843 			resp->attrset = 0;
6844 		}
6845 	}
6846 	else
6847 		*cs->statusp = resp->status;
6848 
6849 	if (args->mode == EXCLUSIVE4)
6850 		rfs4_enable_delegation();
6851 }
6852 
6853 /*ARGSUSED*/
6854 static void
6855 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
6856     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6857 {
6858 	change_info4 *cinfo = &resp->cinfo;
6859 	vattr_t va;
6860 	vtype_t v_type = cs->vp->v_type;
6861 	int error = 0;
6862 
6863 	/* Verify that we have a regular file */
6864 	if (v_type != VREG) {
6865 		if (v_type == VDIR)
6866 			resp->status = NFS4ERR_ISDIR;
6867 		else if (v_type == VLNK)
6868 			resp->status = NFS4ERR_SYMLINK;
6869 		else
6870 			resp->status = NFS4ERR_INVAL;
6871 		return;
6872 	}
6873 
6874 	va.va_mask = AT_MODE|AT_UID;
6875 	error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
6876 	if (error) {
6877 		resp->status = puterrno4(error);
6878 		return;
6879 	}
6880 
6881 	cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
6882 
6883 	/*
6884 	 * Check if we have access to the file, Note the the file
6885 	 * could have originally been open UNCHECKED or GUARDED
6886 	 * with mode bits that will now fail, but there is nothing
6887 	 * we can really do about that except in the case that the
6888 	 * owner of the file is the one requesting the open.
6889 	 */
6890 	if (crgetuid(cs->cr) != va.va_uid) {
6891 		resp->status = check_open_access(args->share_access, cs, req);
6892 		if (resp->status != NFS4_OK) {
6893 			return;
6894 		}
6895 	}
6896 
6897 	/*
6898 	 * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
6899 	 */
6900 	cinfo->before = 0;
6901 	cinfo->after = 0;
6902 	cinfo->atomic = FALSE;
6903 
6904 	rfs4_do_open(cs, req, oo,
6905 	    NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
6906 	    args->share_access, args->share_deny, resp, 0);
6907 }
6908 
6909 static void
6910 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
6911     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6912 {
6913 	int error;
6914 	nfsstat4 status;
6915 	stateid4 stateid =
6916 	    args->open_claim4_u.delegate_cur_info.delegate_stateid;
6917 	rfs4_deleg_state_t *dsp;
6918 
6919 	/*
6920 	 * Find the state info from the stateid and confirm that the
6921 	 * file is delegated.  If the state openowner is the same as
6922 	 * the supplied openowner we're done. If not, get the file
6923 	 * info from the found state info. Use that file info to
6924 	 * create the state for this lock owner. Note solaris doen't
6925 	 * really need the pathname to find the file. We may want to
6926 	 * lookup the pathname and make sure that the vp exist and
6927 	 * matches the vp in the file structure. However it is
6928 	 * possible that the pathname nolonger exists (local process
6929 	 * unlinks the file), so this may not be that useful.
6930 	 */
6931 
6932 	status = rfs4_get_deleg_state(&stateid, &dsp);
6933 	if (status != NFS4_OK) {
6934 		resp->status = status;
6935 		return;
6936 	}
6937 
6938 	ASSERT(dsp->rds_finfo->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE);
6939 
6940 	/*
6941 	 * New lock owner, create state. Since this was probably called
6942 	 * in response to a CB_RECALL we set deleg to DELEG_NONE
6943 	 */
6944 
6945 	ASSERT(cs->vp != NULL);
6946 	VN_RELE(cs->vp);
6947 	VN_HOLD(dsp->rds_finfo->rf_vp);
6948 	cs->vp = dsp->rds_finfo->rf_vp;
6949 
6950 	if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
6951 		rfs4_deleg_state_rele(dsp);
6952 		*cs->statusp = resp->status = puterrno4(error);
6953 		return;
6954 	}
6955 
6956 	/* Mark progress for delegation returns */
6957 	dsp->rds_finfo->rf_dinfo.rd_time_lastwrite = gethrestime_sec();
6958 	rfs4_deleg_state_rele(dsp);
6959 	rfs4_do_open(cs, req, oo, DELEG_NONE,
6960 	    args->share_access, args->share_deny, resp, 1);
6961 }
6962 
6963 /*ARGSUSED*/
6964 static void
6965 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
6966     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6967 {
6968 	/*
6969 	 * Lookup the pathname, it must already exist since this file
6970 	 * was delegated.
6971 	 *
6972 	 * Find the file and state info for this vp and open owner pair.
6973 	 *	check that they are in fact delegated.
6974 	 *	check that the state access and deny modes are the same.
6975 	 *
6976 	 * Return the delgation possibly seting the recall flag.
6977 	 */
6978 	rfs4_file_t *fp;
6979 	rfs4_state_t *sp;
6980 	bool_t create = FALSE;
6981 	bool_t dcreate = FALSE;
6982 	rfs4_deleg_state_t *dsp;
6983 	nfsace4 *ace;
6984 
6985 	/* Note we ignore oflags */
6986 	resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
6987 	    req, cs, args->share_access, &resp->cinfo);
6988 
6989 	if (resp->status != NFS4_OK) {
6990 		return;
6991 	}
6992 
6993 	/* get the file struct and hold a lock on it during initial open */
6994 	fp = rfs4_findfile_withlock(cs->vp, NULL, &create);
6995 	if (fp == NULL) {
6996 		NFS4_DEBUG(rfs4_debug,
6997 		    (CE_NOTE, "rfs4_do_opendelprev: can't find file"));
6998 		resp->status = NFS4ERR_SERVERFAULT;
6999 		return;
7000 	}
7001 
7002 	sp = rfs4_findstate_by_owner_file(oo, fp, &create);
7003 	if (sp == NULL) {
7004 		NFS4_DEBUG(rfs4_debug,
7005 		    (CE_NOTE, "rfs4_do_opendelprev: can't find state"));
7006 		resp->status = NFS4ERR_SERVERFAULT;
7007 		rw_exit(&fp->rf_file_rwlock);
7008 		rfs4_file_rele(fp);
7009 		return;
7010 	}
7011 
7012 	rfs4_dbe_lock(sp->rs_dbe);
7013 	rfs4_dbe_lock(fp->rf_dbe);
7014 	if (args->share_access != sp->rs_share_access ||
7015 	    args->share_deny != sp->rs_share_deny ||
7016 	    sp->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
7017 		NFS4_DEBUG(rfs4_debug,
7018 		    (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
7019 		rfs4_dbe_unlock(fp->rf_dbe);
7020 		rfs4_dbe_unlock(sp->rs_dbe);
7021 		rfs4_file_rele(fp);
7022 		rfs4_state_rele(sp);
7023 		resp->status = NFS4ERR_SERVERFAULT;
7024 		return;
7025 	}
7026 	rfs4_dbe_unlock(fp->rf_dbe);
7027 	rfs4_dbe_unlock(sp->rs_dbe);
7028 
7029 	dsp = rfs4_finddeleg(sp, &dcreate);
7030 	if (dsp == NULL) {
7031 		rfs4_state_rele(sp);
7032 		rfs4_file_rele(fp);
7033 		resp->status = NFS4ERR_SERVERFAULT;
7034 		return;
7035 	}
7036 
7037 	next_stateid(&sp->rs_stateid);
7038 
7039 	resp->stateid = sp->rs_stateid.stateid;
7040 
7041 	resp->delegation.delegation_type = dsp->rds_dtype;
7042 
7043 	if (dsp->rds_dtype == OPEN_DELEGATE_READ) {
7044 		open_read_delegation4 *rv =
7045 		    &resp->delegation.open_delegation4_u.read;
7046 
7047 		rv->stateid = dsp->rds_delegid.stateid;
7048 		rv->recall = FALSE; /* no policy in place to set to TRUE */
7049 		ace = &rv->permissions;
7050 	} else {
7051 		open_write_delegation4 *rv =
7052 		    &resp->delegation.open_delegation4_u.write;
7053 
7054 		rv->stateid = dsp->rds_delegid.stateid;
7055 		rv->recall = FALSE;  /* no policy in place to set to TRUE */
7056 		ace = &rv->permissions;
7057 		rv->space_limit.limitby = NFS_LIMIT_SIZE;
7058 		rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
7059 	}
7060 
7061 	/* XXX For now */
7062 	ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
7063 	ace->flag = 0;
7064 	ace->access_mask = 0;
7065 	ace->who.utf8string_len = 0;
7066 	ace->who.utf8string_val = 0;
7067 
7068 	rfs4_deleg_state_rele(dsp);
7069 	rfs4_state_rele(sp);
7070 	rfs4_file_rele(fp);
7071 }
7072 
7073 typedef enum {
7074 	NFS4_CHKSEQ_OKAY = 0,
7075 	NFS4_CHKSEQ_REPLAY = 1,
7076 	NFS4_CHKSEQ_BAD = 2
7077 } rfs4_chkseq_t;
7078 
7079 /*
7080  * Generic function for sequence number checks.
7081  */
7082 static rfs4_chkseq_t
7083 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
7084     seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
7085 {
7086 	/* Same sequence ids and matching operations? */
7087 	if (seqid == rqst_seq && resop->resop == lastop->resop) {
7088 		if (copyres == TRUE) {
7089 			rfs4_free_reply(resop);
7090 			rfs4_copy_reply(resop, lastop);
7091 		}
7092 		NFS4_DEBUG(rfs4_debug, (CE_NOTE,
7093 		    "Replayed SEQID %d\n", seqid));
7094 		return (NFS4_CHKSEQ_REPLAY);
7095 	}
7096 
7097 	/* If the incoming sequence is not the next expected then it is bad */
7098 	if (rqst_seq != seqid + 1) {
7099 		if (rqst_seq == seqid) {
7100 			NFS4_DEBUG(rfs4_debug,
7101 			    (CE_NOTE, "BAD SEQID: Replayed sequence id "
7102 			    "but last op was %d current op is %d\n",
7103 			    lastop->resop, resop->resop));
7104 			return (NFS4_CHKSEQ_BAD);
7105 		}
7106 		NFS4_DEBUG(rfs4_debug,
7107 		    (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
7108 		    rqst_seq, seqid));
7109 		return (NFS4_CHKSEQ_BAD);
7110 	}
7111 
7112 	/* Everything okay -- next expected */
7113 	return (NFS4_CHKSEQ_OKAY);
7114 }
7115 
7116 
7117 static rfs4_chkseq_t
7118 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7119 {
7120 	rfs4_chkseq_t rc;
7121 
7122 	rfs4_dbe_lock(op->ro_dbe);
7123 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply, seqid, resop,
7124 	    TRUE);
7125 	rfs4_dbe_unlock(op->ro_dbe);
7126 
7127 	if (rc == NFS4_CHKSEQ_OKAY)
7128 		rfs4_update_lease(op->ro_client);
7129 
7130 	return (rc);
7131 }
7132 
7133 static rfs4_chkseq_t
7134 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7135 {
7136 	rfs4_chkseq_t rc;
7137 
7138 	rfs4_dbe_lock(op->ro_dbe);
7139 	rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply,
7140 	    olo_seqid, resop, FALSE);
7141 	rfs4_dbe_unlock(op->ro_dbe);
7142 
7143 	return (rc);
7144 }
7145 
7146 static rfs4_chkseq_t
7147 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lsp, nfs_resop4 *resop)
7148 {
7149 	rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
7150 
7151 	rfs4_dbe_lock(lsp->rls_dbe);
7152 	if (!lsp->rls_skip_seqid_check)
7153 		rc = rfs4_check_seqid(lsp->rls_seqid, &lsp->rls_reply, seqid,
7154 		    resop, TRUE);
7155 	rfs4_dbe_unlock(lsp->rls_dbe);
7156 
7157 	return (rc);
7158 }
7159 
7160 static void
7161 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
7162     struct svc_req *req, struct compound_state *cs)
7163 {
7164 	OPEN4args *args = &argop->nfs_argop4_u.opopen;
7165 	OPEN4res *resp = &resop->nfs_resop4_u.opopen;
7166 	open_owner4 *owner = &args->owner;
7167 	open_claim_type4 claim = args->claim;
7168 	rfs4_client_t *cp;
7169 	rfs4_openowner_t *oo;
7170 	bool_t create;
7171 	bool_t replay = FALSE;
7172 	int can_reclaim;
7173 
7174 	DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
7175 	    OPEN4args *, args);
7176 
7177 	if (cs->vp == NULL) {
7178 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7179 		goto end;
7180 	}
7181 
7182 	/*
7183 	 * Need to check clientid and lease expiration first based on
7184 	 * error ordering and incrementing sequence id.
7185 	 */
7186 	cp = rfs4_findclient_by_id(owner->clientid, FALSE);
7187 	if (cp == NULL) {
7188 		*cs->statusp = resp->status =
7189 		    rfs4_check_clientid(&owner->clientid, 0);
7190 		goto end;
7191 	}
7192 
7193 	if (rfs4_lease_expired(cp)) {
7194 		rfs4_client_close(cp);
7195 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7196 		goto end;
7197 	}
7198 	can_reclaim = cp->rc_can_reclaim;
7199 
7200 	/*
7201 	 * Find the open_owner for use from this point forward.  Take
7202 	 * care in updating the sequence id based on the type of error
7203 	 * being returned.
7204 	 */
7205 retry:
7206 	create = TRUE;
7207 	oo = rfs4_findopenowner(owner, &create, args->seqid);
7208 	if (oo == NULL) {
7209 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
7210 		rfs4_client_rele(cp);
7211 		goto end;
7212 	}
7213 
7214 	/* Hold off access to the sequence space while the open is done */
7215 	rfs4_sw_enter(&oo->ro_sw);
7216 
7217 	/*
7218 	 * If the open_owner existed before at the server, then check
7219 	 * the sequence id.
7220 	 */
7221 	if (!create && !oo->ro_postpone_confirm) {
7222 		switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
7223 		case NFS4_CHKSEQ_BAD:
7224 			if ((args->seqid > oo->ro_open_seqid) &&
7225 			    oo->ro_need_confirm) {
7226 				rfs4_free_opens(oo, TRUE, FALSE);
7227 				rfs4_sw_exit(&oo->ro_sw);
7228 				rfs4_openowner_rele(oo);
7229 				goto retry;
7230 			}
7231 			resp->status = NFS4ERR_BAD_SEQID;
7232 			goto out;
7233 		case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
7234 			replay = TRUE;
7235 			goto out;
7236 		default:
7237 			break;
7238 		}
7239 
7240 		/*
7241 		 * Sequence was ok and open owner exists
7242 		 * check to see if we have yet to see an
7243 		 * open_confirm.
7244 		 */
7245 		if (oo->ro_need_confirm) {
7246 			rfs4_free_opens(oo, TRUE, FALSE);
7247 			rfs4_sw_exit(&oo->ro_sw);
7248 			rfs4_openowner_rele(oo);
7249 			goto retry;
7250 		}
7251 	}
7252 	/* Grace only applies to regular-type OPENs */
7253 	if (rfs4_clnt_in_grace(cp) &&
7254 	    (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7255 		*cs->statusp = resp->status = NFS4ERR_GRACE;
7256 		goto out;
7257 	}
7258 
7259 	/*
7260 	 * If previous state at the server existed then can_reclaim
7261 	 * will be set. If not reply NFS4ERR_NO_GRACE to the
7262 	 * client.
7263 	 */
7264 	if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7265 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7266 		goto out;
7267 	}
7268 
7269 
7270 	/*
7271 	 * Reject the open if the client has missed the grace period
7272 	 */
7273 	if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7274 		*cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7275 		goto out;
7276 	}
7277 
7278 	/* Couple of up-front bookkeeping items */
7279 	if (oo->ro_need_confirm) {
7280 		/*
7281 		 * If this is a reclaim OPEN then we should not ask
7282 		 * for a confirmation of the open_owner per the
7283 		 * protocol specification.
7284 		 */
7285 		if (claim == CLAIM_PREVIOUS)
7286 			oo->ro_need_confirm = FALSE;
7287 		else
7288 			resp->rflags |= OPEN4_RESULT_CONFIRM;
7289 	}
7290 	resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7291 
7292 	/*
7293 	 * If there is an unshared filesystem mounted on this vnode,
7294 	 * do not allow to open/create in this directory.
7295 	 */
7296 	if (vn_ismntpt(cs->vp)) {
7297 		*cs->statusp = resp->status = NFS4ERR_ACCESS;
7298 		goto out;
7299 	}
7300 
7301 	/*
7302 	 * access must READ, WRITE, or BOTH.  No access is invalid.
7303 	 * deny can be READ, WRITE, BOTH, or NONE.
7304 	 * bits not defined for access/deny are invalid.
7305 	 */
7306 	if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7307 	    (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7308 	    (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7309 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7310 		goto out;
7311 	}
7312 
7313 
7314 	/*
7315 	 * make sure attrset is zero before response is built.
7316 	 */
7317 	resp->attrset = 0;
7318 
7319 	switch (claim) {
7320 	case CLAIM_NULL:
7321 		rfs4_do_opennull(cs, req, args, oo, resp);
7322 		break;
7323 	case CLAIM_PREVIOUS:
7324 		rfs4_do_openprev(cs, req, args, oo, resp);
7325 		break;
7326 	case CLAIM_DELEGATE_CUR:
7327 		rfs4_do_opendelcur(cs, req, args, oo, resp);
7328 		break;
7329 	case CLAIM_DELEGATE_PREV:
7330 		rfs4_do_opendelprev(cs, req, args, oo, resp);
7331 		break;
7332 	default:
7333 		resp->status = NFS4ERR_INVAL;
7334 		break;
7335 	}
7336 
7337 out:
7338 	rfs4_client_rele(cp);
7339 
7340 	/* Catch sequence id handling here to make it a little easier */
7341 	switch (resp->status) {
7342 	case NFS4ERR_BADXDR:
7343 	case NFS4ERR_BAD_SEQID:
7344 	case NFS4ERR_BAD_STATEID:
7345 	case NFS4ERR_NOFILEHANDLE:
7346 	case NFS4ERR_RESOURCE:
7347 	case NFS4ERR_STALE_CLIENTID:
7348 	case NFS4ERR_STALE_STATEID:
7349 		/*
7350 		 * The protocol states that if any of these errors are
7351 		 * being returned, the sequence id should not be
7352 		 * incremented.  Any other return requires an
7353 		 * increment.
7354 		 */
7355 		break;
7356 	default:
7357 		/* Always update the lease in this case */
7358 		rfs4_update_lease(oo->ro_client);
7359 
7360 		/* Regular response - copy the result */
7361 		if (!replay)
7362 			rfs4_update_open_resp(oo, resop, &cs->fh);
7363 
7364 		/*
7365 		 * REPLAY case: Only if the previous response was OK
7366 		 * do we copy the filehandle.  If not OK, no
7367 		 * filehandle to copy.
7368 		 */
7369 		if (replay == TRUE &&
7370 		    resp->status == NFS4_OK &&
7371 		    oo->ro_reply_fh.nfs_fh4_val) {
7372 			/*
7373 			 * If this is a replay, we must restore the
7374 			 * current filehandle/vp to that of what was
7375 			 * returned originally.  Try our best to do
7376 			 * it.
7377 			 */
7378 			nfs_fh4_fmt_t *fh_fmtp =
7379 			    (nfs_fh4_fmt_t *)oo->ro_reply_fh.nfs_fh4_val;
7380 
7381 			cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7382 			    (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7383 
7384 			if (cs->exi == NULL) {
7385 				resp->status = NFS4ERR_STALE;
7386 				goto finish;
7387 			}
7388 
7389 			VN_RELE(cs->vp);
7390 
7391 			cs->vp = nfs4_fhtovp(&oo->ro_reply_fh, cs->exi,
7392 			    &resp->status);
7393 
7394 			if (cs->vp == NULL)
7395 				goto finish;
7396 
7397 			nfs_fh4_copy(&oo->ro_reply_fh, &cs->fh);
7398 		}
7399 
7400 		/*
7401 		 * If this was a replay, no need to update the
7402 		 * sequence id. If the open_owner was not created on
7403 		 * this pass, then update.  The first use of an
7404 		 * open_owner will not bump the sequence id.
7405 		 */
7406 		if (replay == FALSE && !create)
7407 			rfs4_update_open_sequence(oo);
7408 		/*
7409 		 * If the client is receiving an error and the
7410 		 * open_owner needs to be confirmed, there is no way
7411 		 * to notify the client of this fact ignoring the fact
7412 		 * that the server has no method of returning a
7413 		 * stateid to confirm.  Therefore, the server needs to
7414 		 * mark this open_owner in a way as to avoid the
7415 		 * sequence id checking the next time the client uses
7416 		 * this open_owner.
7417 		 */
7418 		if (resp->status != NFS4_OK && oo->ro_need_confirm)
7419 			oo->ro_postpone_confirm = TRUE;
7420 		/*
7421 		 * If OK response then clear the postpone flag and
7422 		 * reset the sequence id to keep in sync with the
7423 		 * client.
7424 		 */
7425 		if (resp->status == NFS4_OK && oo->ro_postpone_confirm) {
7426 			oo->ro_postpone_confirm = FALSE;
7427 			oo->ro_open_seqid = args->seqid;
7428 		}
7429 		break;
7430 	}
7431 
7432 finish:
7433 	*cs->statusp = resp->status;
7434 
7435 	rfs4_sw_exit(&oo->ro_sw);
7436 	rfs4_openowner_rele(oo);
7437 
7438 end:
7439 	DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7440 	    OPEN4res *, resp);
7441 }
7442 
7443 /*ARGSUSED*/
7444 void
7445 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7446     struct svc_req *req, struct compound_state *cs)
7447 {
7448 	OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7449 	OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7450 	rfs4_state_t *sp;
7451 	nfsstat4 status;
7452 
7453 	DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7454 	    OPEN_CONFIRM4args *, args);
7455 
7456 	if (cs->vp == NULL) {
7457 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7458 		goto out;
7459 	}
7460 
7461 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7462 	if (status != NFS4_OK) {
7463 		*cs->statusp = resp->status = status;
7464 		goto out;
7465 	}
7466 
7467 	/* Ensure specified filehandle matches */
7468 	if (cs->vp != sp->rs_finfo->rf_vp) {
7469 		rfs4_state_rele(sp);
7470 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7471 		goto out;
7472 	}
7473 
7474 	/* hold off other access to open_owner while we tinker */
7475 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
7476 
7477 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7478 	case NFS4_CHECK_STATEID_OKAY:
7479 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7480 		    resop) != 0) {
7481 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7482 			break;
7483 		}
7484 		/*
7485 		 * If it is the appropriate stateid and determined to
7486 		 * be "OKAY" then this means that the stateid does not
7487 		 * need to be confirmed and the client is in error for
7488 		 * sending an OPEN_CONFIRM.
7489 		 */
7490 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7491 		break;
7492 	case NFS4_CHECK_STATEID_OLD:
7493 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7494 		break;
7495 	case NFS4_CHECK_STATEID_BAD:
7496 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7497 		break;
7498 	case NFS4_CHECK_STATEID_EXPIRED:
7499 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7500 		break;
7501 	case NFS4_CHECK_STATEID_CLOSED:
7502 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7503 		break;
7504 	case NFS4_CHECK_STATEID_REPLAY:
7505 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7506 		    resop)) {
7507 		case NFS4_CHKSEQ_OKAY:
7508 			/*
7509 			 * This is replayed stateid; if seqid matches
7510 			 * next expected, then client is using wrong seqid.
7511 			 */
7512 			/* fall through */
7513 		case NFS4_CHKSEQ_BAD:
7514 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7515 			break;
7516 		case NFS4_CHKSEQ_REPLAY:
7517 			/*
7518 			 * Note this case is the duplicate case so
7519 			 * resp->status is already set.
7520 			 */
7521 			*cs->statusp = resp->status;
7522 			rfs4_update_lease(sp->rs_owner->ro_client);
7523 			break;
7524 		}
7525 		break;
7526 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7527 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7528 		    resop) != NFS4_CHKSEQ_OKAY) {
7529 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7530 			break;
7531 		}
7532 		*cs->statusp = resp->status = NFS4_OK;
7533 
7534 		next_stateid(&sp->rs_stateid);
7535 		resp->open_stateid = sp->rs_stateid.stateid;
7536 		sp->rs_owner->ro_need_confirm = FALSE;
7537 		rfs4_update_lease(sp->rs_owner->ro_client);
7538 		rfs4_update_open_sequence(sp->rs_owner);
7539 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7540 		break;
7541 	default:
7542 		ASSERT(FALSE);
7543 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7544 		break;
7545 	}
7546 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
7547 	rfs4_state_rele(sp);
7548 
7549 out:
7550 	DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7551 	    OPEN_CONFIRM4res *, resp);
7552 }
7553 
7554 /*ARGSUSED*/
7555 void
7556 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7557     struct svc_req *req, struct compound_state *cs)
7558 {
7559 	OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7560 	OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7561 	uint32_t access = args->share_access;
7562 	uint32_t deny = args->share_deny;
7563 	nfsstat4 status;
7564 	rfs4_state_t *sp;
7565 	rfs4_file_t *fp;
7566 	int fflags = 0;
7567 
7568 	DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7569 	    OPEN_DOWNGRADE4args *, args);
7570 
7571 	if (cs->vp == NULL) {
7572 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7573 		goto out;
7574 	}
7575 
7576 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7577 	if (status != NFS4_OK) {
7578 		*cs->statusp = resp->status = status;
7579 		goto out;
7580 	}
7581 
7582 	/* Ensure specified filehandle matches */
7583 	if (cs->vp != sp->rs_finfo->rf_vp) {
7584 		rfs4_state_rele(sp);
7585 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7586 		goto out;
7587 	}
7588 
7589 	/* hold off other access to open_owner while we tinker */
7590 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
7591 
7592 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7593 	case NFS4_CHECK_STATEID_OKAY:
7594 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7595 		    resop) != NFS4_CHKSEQ_OKAY) {
7596 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7597 			goto end;
7598 		}
7599 		break;
7600 	case NFS4_CHECK_STATEID_OLD:
7601 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7602 		goto end;
7603 	case NFS4_CHECK_STATEID_BAD:
7604 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7605 		goto end;
7606 	case NFS4_CHECK_STATEID_EXPIRED:
7607 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
7608 		goto end;
7609 	case NFS4_CHECK_STATEID_CLOSED:
7610 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7611 		goto end;
7612 	case NFS4_CHECK_STATEID_UNCONFIRMED:
7613 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7614 		goto end;
7615 	case NFS4_CHECK_STATEID_REPLAY:
7616 		/* Check the sequence id for the open owner */
7617 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7618 		    resop)) {
7619 		case NFS4_CHKSEQ_OKAY:
7620 			/*
7621 			 * This is replayed stateid; if seqid matches
7622 			 * next expected, then client is using wrong seqid.
7623 			 */
7624 			/* fall through */
7625 		case NFS4_CHKSEQ_BAD:
7626 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7627 			goto end;
7628 		case NFS4_CHKSEQ_REPLAY:
7629 			/*
7630 			 * Note this case is the duplicate case so
7631 			 * resp->status is already set.
7632 			 */
7633 			*cs->statusp = resp->status;
7634 			rfs4_update_lease(sp->rs_owner->ro_client);
7635 			goto end;
7636 		}
7637 		break;
7638 	default:
7639 		ASSERT(FALSE);
7640 		break;
7641 	}
7642 
7643 	rfs4_dbe_lock(sp->rs_dbe);
7644 	/*
7645 	 * Check that the new access modes and deny modes are valid.
7646 	 * Check that no invalid bits are set.
7647 	 */
7648 	if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7649 	    (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7650 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7651 		rfs4_update_open_sequence(sp->rs_owner);
7652 		rfs4_dbe_unlock(sp->rs_dbe);
7653 		goto end;
7654 	}
7655 
7656 	/*
7657 	 * The new modes must be a subset of the current modes and
7658 	 * the access must specify at least one mode. To test that
7659 	 * the new mode is a subset of the current modes we bitwise
7660 	 * AND them together and check that the result equals the new
7661 	 * mode. For example:
7662 	 * New mode, access == R and current mode, sp->rs_share_access  == RW
7663 	 * access & sp->rs_share_access == R == access, so the new access mode
7664 	 * is valid. Consider access == RW, sp->rs_share_access = R
7665 	 * access & sp->rs_share_access == R != access, so the new access mode
7666 	 * is invalid.
7667 	 */
7668 	if ((access & sp->rs_share_access) != access ||
7669 	    (deny & sp->rs_share_deny) != deny ||
7670 	    (access &
7671 	    (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7672 		*cs->statusp = resp->status = NFS4ERR_INVAL;
7673 		rfs4_update_open_sequence(sp->rs_owner);
7674 		rfs4_dbe_unlock(sp->rs_dbe);
7675 		goto end;
7676 	}
7677 
7678 	/*
7679 	 * Release any share locks associated with this stateID.
7680 	 * Strictly speaking, this violates the spec because the
7681 	 * spec effectively requires that open downgrade be atomic.
7682 	 * At present, fs_shrlock does not have this capability.
7683 	 */
7684 	(void) rfs4_unshare(sp);
7685 
7686 	fp = sp->rs_finfo;
7687 	rfs4_dbe_lock(fp->rf_dbe);
7688 
7689 	/*
7690 	 * If the current mode has deny read and the new mode
7691 	 * does not, decrement the number of deny read mode bits
7692 	 * and if it goes to zero turn off the deny read bit
7693 	 * on the file.
7694 	 */
7695 	if ((sp->rs_share_deny & OPEN4_SHARE_DENY_READ) &&
7696 	    (deny & OPEN4_SHARE_DENY_READ) == 0) {
7697 		fp->rf_deny_read--;
7698 		if (fp->rf_deny_read == 0)
7699 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
7700 	}
7701 
7702 	/*
7703 	 * If the current mode has deny write and the new mode
7704 	 * does not, decrement the number of deny write mode bits
7705 	 * and if it goes to zero turn off the deny write bit
7706 	 * on the file.
7707 	 */
7708 	if ((sp->rs_share_deny & OPEN4_SHARE_DENY_WRITE) &&
7709 	    (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7710 		fp->rf_deny_write--;
7711 		if (fp->rf_deny_write == 0)
7712 			fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7713 	}
7714 
7715 	/*
7716 	 * If the current mode has access read and the new mode
7717 	 * does not, decrement the number of access read mode bits
7718 	 * and if it goes to zero turn off the access read bit
7719 	 * on the file.  set fflags to FREAD for the call to
7720 	 * vn_open_downgrade().
7721 	 */
7722 	if ((sp->rs_share_access & OPEN4_SHARE_ACCESS_READ) &&
7723 	    (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7724 		fp->rf_access_read--;
7725 		if (fp->rf_access_read == 0)
7726 			fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
7727 		fflags |= FREAD;
7728 	}
7729 
7730 	/*
7731 	 * If the current mode has access write and the new mode
7732 	 * does not, decrement the number of access write mode bits
7733 	 * and if it goes to zero turn off the access write bit
7734 	 * on the file.  set fflags to FWRITE for the call to
7735 	 * vn_open_downgrade().
7736 	 */
7737 	if ((sp->rs_share_access & OPEN4_SHARE_ACCESS_WRITE) &&
7738 	    (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7739 		fp->rf_access_write--;
7740 		if (fp->rf_access_write == 0)
7741 			fp->rf_share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7742 		fflags |= FWRITE;
7743 	}
7744 
7745 	/* Check that the file is still accessible */
7746 	ASSERT(fp->rf_share_access);
7747 
7748 	rfs4_dbe_unlock(fp->rf_dbe);
7749 
7750 	status = rfs4_share(sp, access, deny);
7751 	rfs4_dbe_unlock(sp->rs_dbe);
7752 
7753 	if (status != NFS4_OK) {
7754 		*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7755 		rfs4_update_open_sequence(sp->rs_owner);
7756 		goto end;
7757 	}
7758 
7759 	/*
7760 	 * we successfully downgraded the share lock, now we need to downgrade
7761 	 * the open.  it is possible that the downgrade was only for a deny
7762 	 * mode and we have nothing else to do.
7763 	 */
7764 	if ((fflags & (FREAD|FWRITE)) != 0)
7765 		vn_open_downgrade(cs->vp, fflags);
7766 
7767 	rfs4_dbe_lock(sp->rs_dbe);
7768 
7769 	/* Update the stateid */
7770 	next_stateid(&sp->rs_stateid);
7771 	resp->open_stateid = sp->rs_stateid.stateid;
7772 
7773 	rfs4_dbe_unlock(sp->rs_dbe);
7774 
7775 	*cs->statusp = resp->status = NFS4_OK;
7776 	/* Update the lease */
7777 	rfs4_update_lease(sp->rs_owner->ro_client);
7778 	/* And the sequence */
7779 	rfs4_update_open_sequence(sp->rs_owner);
7780 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7781 
7782 end:
7783 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
7784 	rfs4_state_rele(sp);
7785 out:
7786 	DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7787 	    OPEN_DOWNGRADE4res *, resp);
7788 }
7789 
7790 /*
7791  * The logic behind this function is detailed in the NFSv4 RFC in the
7792  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7793  * that section for explicit guidance to server behavior for
7794  * SETCLIENTID.
7795  */
7796 void
7797 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
7798     struct svc_req *req, struct compound_state *cs)
7799 {
7800 	SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
7801 	SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
7802 	rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
7803 	bool_t create = TRUE;
7804 	char *addr, *netid;
7805 	int len;
7806 
7807 	DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
7808 	    SETCLIENTID4args *, args);
7809 retry:
7810 	newcp = cp_confirmed = cp_unconfirmed = NULL;
7811 
7812 	/*
7813 	 * In search of an EXISTING client matching the incoming
7814 	 * request to establish a new client identifier at the server
7815 	 */
7816 	create = TRUE;
7817 	cp = rfs4_findclient(&args->client, &create, NULL);
7818 
7819 	/* Should never happen */
7820 	ASSERT(cp != NULL);
7821 
7822 	if (cp == NULL) {
7823 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7824 		goto out;
7825 	}
7826 
7827 	/*
7828 	 * Easiest case. Client identifier is newly created and is
7829 	 * unconfirmed.  Also note that for this case, no other
7830 	 * entries exist for the client identifier.  Nothing else to
7831 	 * check.  Just setup the response and respond.
7832 	 */
7833 	if (create) {
7834 		*cs->statusp = res->status = NFS4_OK;
7835 		res->SETCLIENTID4res_u.resok4.clientid = cp->rc_clientid;
7836 		res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7837 		    cp->rc_confirm_verf;
7838 		/* Setup callback information; CB_NULL confirmation later */
7839 		rfs4_client_setcb(cp, &args->callback, args->callback_ident);
7840 
7841 		rfs4_client_rele(cp);
7842 		goto out;
7843 	}
7844 
7845 	/*
7846 	 * An existing, confirmed client may exist but it may not have
7847 	 * been active for at least one lease period.  If so, then
7848 	 * "close" the client and create a new client identifier
7849 	 */
7850 	if (rfs4_lease_expired(cp)) {
7851 		rfs4_client_close(cp);
7852 		goto retry;
7853 	}
7854 
7855 	if (cp->rc_need_confirm == TRUE)
7856 		cp_unconfirmed = cp;
7857 	else
7858 		cp_confirmed = cp;
7859 
7860 	cp = NULL;
7861 
7862 	/*
7863 	 * We have a confirmed client, now check for an
7864 	 * unconfimred entry
7865 	 */
7866 	if (cp_confirmed) {
7867 		/* If creds don't match then client identifier is inuse */
7868 		if (!creds_ok(cp_confirmed->rc_cr_set, req, cs)) {
7869 			rfs4_cbinfo_t *cbp;
7870 			/*
7871 			 * Some one else has established this client
7872 			 * id. Try and say * who they are. We will use
7873 			 * the call back address supplied by * the
7874 			 * first client.
7875 			 */
7876 			*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
7877 
7878 			addr = netid = NULL;
7879 
7880 			cbp = &cp_confirmed->rc_cbinfo;
7881 			if (cbp->cb_callback.cb_location.r_addr &&
7882 			    cbp->cb_callback.cb_location.r_netid) {
7883 				cb_client4 *cbcp = &cbp->cb_callback;
7884 
7885 				len = strlen(cbcp->cb_location.r_addr)+1;
7886 				addr = kmem_alloc(len, KM_SLEEP);
7887 				bcopy(cbcp->cb_location.r_addr, addr, len);
7888 				len = strlen(cbcp->cb_location.r_netid)+1;
7889 				netid = kmem_alloc(len, KM_SLEEP);
7890 				bcopy(cbcp->cb_location.r_netid, netid, len);
7891 			}
7892 
7893 			res->SETCLIENTID4res_u.client_using.r_addr = addr;
7894 			res->SETCLIENTID4res_u.client_using.r_netid = netid;
7895 
7896 			rfs4_client_rele(cp_confirmed);
7897 		}
7898 
7899 		/*
7900 		 * Confirmed, creds match, and verifier matches; must
7901 		 * be an update of the callback info
7902 		 */
7903 		if (cp_confirmed->rc_nfs_client.verifier ==
7904 		    args->client.verifier) {
7905 			/* Setup callback information */
7906 			rfs4_client_setcb(cp_confirmed, &args->callback,
7907 			    args->callback_ident);
7908 
7909 			/* everything okay -- move ahead */
7910 			*cs->statusp = res->status = NFS4_OK;
7911 			res->SETCLIENTID4res_u.resok4.clientid =
7912 			    cp_confirmed->rc_clientid;
7913 
7914 			/* update the confirm_verifier and return it */
7915 			rfs4_client_scv_next(cp_confirmed);
7916 			res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7917 			    cp_confirmed->rc_confirm_verf;
7918 
7919 			rfs4_client_rele(cp_confirmed);
7920 			goto out;
7921 		}
7922 
7923 		/*
7924 		 * Creds match but the verifier doesn't.  Must search
7925 		 * for an unconfirmed client that would be replaced by
7926 		 * this request.
7927 		 */
7928 		create = FALSE;
7929 		cp_unconfirmed = rfs4_findclient(&args->client, &create,
7930 		    cp_confirmed);
7931 	}
7932 
7933 	/*
7934 	 * At this point, we have taken care of the brand new client
7935 	 * struct, INUSE case, update of an existing, and confirmed
7936 	 * client struct.
7937 	 */
7938 
7939 	/*
7940 	 * check to see if things have changed while we originally
7941 	 * picked up the client struct.  If they have, then return and
7942 	 * retry the processing of this SETCLIENTID request.
7943 	 */
7944 	if (cp_unconfirmed) {
7945 		rfs4_dbe_lock(cp_unconfirmed->rc_dbe);
7946 		if (!cp_unconfirmed->rc_need_confirm) {
7947 			rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
7948 			rfs4_client_rele(cp_unconfirmed);
7949 			if (cp_confirmed)
7950 				rfs4_client_rele(cp_confirmed);
7951 			goto retry;
7952 		}
7953 		/* do away with the old unconfirmed one */
7954 		rfs4_dbe_invalidate(cp_unconfirmed->rc_dbe);
7955 		rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
7956 		rfs4_client_rele(cp_unconfirmed);
7957 		cp_unconfirmed = NULL;
7958 	}
7959 
7960 	/*
7961 	 * This search will temporarily hide the confirmed client
7962 	 * struct while a new client struct is created as the
7963 	 * unconfirmed one.
7964 	 */
7965 	create = TRUE;
7966 	newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
7967 
7968 	ASSERT(newcp != NULL);
7969 
7970 	if (newcp == NULL) {
7971 		*cs->statusp = res->status = NFS4ERR_SERVERFAULT;
7972 		rfs4_client_rele(cp_confirmed);
7973 		goto out;
7974 	}
7975 
7976 	/*
7977 	 * If one was not created, then a similar request must be in
7978 	 * process so release and start over with this one
7979 	 */
7980 	if (create != TRUE) {
7981 		rfs4_client_rele(newcp);
7982 		if (cp_confirmed)
7983 			rfs4_client_rele(cp_confirmed);
7984 		goto retry;
7985 	}
7986 
7987 	*cs->statusp = res->status = NFS4_OK;
7988 	res->SETCLIENTID4res_u.resok4.clientid = newcp->rc_clientid;
7989 	res->SETCLIENTID4res_u.resok4.setclientid_confirm =
7990 	    newcp->rc_confirm_verf;
7991 	/* Setup callback information; CB_NULL confirmation later */
7992 	rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
7993 
7994 	newcp->rc_cp_confirmed = cp_confirmed;
7995 
7996 	rfs4_client_rele(newcp);
7997 
7998 out:
7999 	DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
8000 	    SETCLIENTID4res *, res);
8001 }
8002 
8003 /*ARGSUSED*/
8004 void
8005 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
8006     struct svc_req *req, struct compound_state *cs)
8007 {
8008 	SETCLIENTID_CONFIRM4args *args =
8009 	    &argop->nfs_argop4_u.opsetclientid_confirm;
8010 	SETCLIENTID_CONFIRM4res *res =
8011 	    &resop->nfs_resop4_u.opsetclientid_confirm;
8012 	rfs4_client_t *cp, *cptoclose = NULL;
8013 
8014 	DTRACE_NFSV4_2(op__setclientid__confirm__start,
8015 	    struct compound_state *, cs,
8016 	    SETCLIENTID_CONFIRM4args *, args);
8017 
8018 	*cs->statusp = res->status = NFS4_OK;
8019 
8020 	cp = rfs4_findclient_by_id(args->clientid, TRUE);
8021 
8022 	if (cp == NULL) {
8023 		*cs->statusp = res->status =
8024 		    rfs4_check_clientid(&args->clientid, 1);
8025 		goto out;
8026 	}
8027 
8028 	if (!creds_ok(cp, req, cs)) {
8029 		*cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8030 		rfs4_client_rele(cp);
8031 		goto out;
8032 	}
8033 
8034 	/* If the verifier doesn't match, the record doesn't match */
8035 	if (cp->rc_confirm_verf != args->setclientid_confirm) {
8036 		*cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
8037 		rfs4_client_rele(cp);
8038 		goto out;
8039 	}
8040 
8041 	rfs4_dbe_lock(cp->rc_dbe);
8042 	cp->rc_need_confirm = FALSE;
8043 	if (cp->rc_cp_confirmed) {
8044 		cptoclose = cp->rc_cp_confirmed;
8045 		cptoclose->rc_ss_remove = 1;
8046 		cp->rc_cp_confirmed = NULL;
8047 	}
8048 
8049 	/*
8050 	 * Update the client's associated server instance, if it's changed
8051 	 * since the client was created.
8052 	 */
8053 	if (rfs4_servinst(cp) != rfs4_cur_servinst)
8054 		rfs4_servinst_assign(cp, rfs4_cur_servinst);
8055 
8056 	/*
8057 	 * Record clientid in stable storage.
8058 	 * Must be done after server instance has been assigned.
8059 	 */
8060 	rfs4_ss_clid(cp, req);
8061 
8062 	rfs4_dbe_unlock(cp->rc_dbe);
8063 
8064 	if (cptoclose)
8065 		/* don't need to rele, client_close does it */
8066 		rfs4_client_close(cptoclose);
8067 
8068 	/* If needed, initiate CB_NULL call for callback path */
8069 	rfs4_deleg_cb_check(cp);
8070 	rfs4_update_lease(cp);
8071 
8072 	/*
8073 	 * Check to see if client can perform reclaims
8074 	 */
8075 	rfs4_ss_chkclid(cp);
8076 
8077 	rfs4_client_rele(cp);
8078 
8079 out:
8080 	DTRACE_NFSV4_2(op__setclientid__confirm__done,
8081 	    struct compound_state *, cs,
8082 	    SETCLIENTID_CONFIRM4 *, res);
8083 }
8084 
8085 
8086 /*ARGSUSED*/
8087 void
8088 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
8089     struct svc_req *req, struct compound_state *cs)
8090 {
8091 	CLOSE4args *args = &argop->nfs_argop4_u.opclose;
8092 	CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
8093 	rfs4_state_t *sp;
8094 	nfsstat4 status;
8095 
8096 	DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
8097 	    CLOSE4args *, args);
8098 
8099 	if (cs->vp == NULL) {
8100 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8101 		goto out;
8102 	}
8103 
8104 	status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
8105 	if (status != NFS4_OK) {
8106 		*cs->statusp = resp->status = status;
8107 		goto out;
8108 	}
8109 
8110 	/* Ensure specified filehandle matches */
8111 	if (cs->vp != sp->rs_finfo->rf_vp) {
8112 		rfs4_state_rele(sp);
8113 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8114 		goto out;
8115 	}
8116 
8117 	/* hold off other access to open_owner while we tinker */
8118 	rfs4_sw_enter(&sp->rs_owner->ro_sw);
8119 
8120 	switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
8121 	case NFS4_CHECK_STATEID_OKAY:
8122 		if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8123 		    resop) != NFS4_CHKSEQ_OKAY) {
8124 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8125 			goto end;
8126 		}
8127 		break;
8128 	case NFS4_CHECK_STATEID_OLD:
8129 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8130 		goto end;
8131 	case NFS4_CHECK_STATEID_BAD:
8132 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8133 		goto end;
8134 	case NFS4_CHECK_STATEID_EXPIRED:
8135 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8136 		goto end;
8137 	case NFS4_CHECK_STATEID_CLOSED:
8138 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8139 		goto end;
8140 	case NFS4_CHECK_STATEID_UNCONFIRMED:
8141 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8142 		goto end;
8143 	case NFS4_CHECK_STATEID_REPLAY:
8144 		/* Check the sequence id for the open owner */
8145 		switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8146 		    resop)) {
8147 		case NFS4_CHKSEQ_OKAY:
8148 			/*
8149 			 * This is replayed stateid; if seqid matches
8150 			 * next expected, then client is using wrong seqid.
8151 			 */
8152 			/* FALL THROUGH */
8153 		case NFS4_CHKSEQ_BAD:
8154 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8155 			goto end;
8156 		case NFS4_CHKSEQ_REPLAY:
8157 			/*
8158 			 * Note this case is the duplicate case so
8159 			 * resp->status is already set.
8160 			 */
8161 			*cs->statusp = resp->status;
8162 			rfs4_update_lease(sp->rs_owner->ro_client);
8163 			goto end;
8164 		}
8165 		break;
8166 	default:
8167 		ASSERT(FALSE);
8168 		break;
8169 	}
8170 
8171 	rfs4_dbe_lock(sp->rs_dbe);
8172 
8173 	/* Update the stateid. */
8174 	next_stateid(&sp->rs_stateid);
8175 	resp->open_stateid = sp->rs_stateid.stateid;
8176 
8177 	rfs4_dbe_unlock(sp->rs_dbe);
8178 
8179 	rfs4_update_lease(sp->rs_owner->ro_client);
8180 	rfs4_update_open_sequence(sp->rs_owner);
8181 	rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8182 
8183 	rfs4_state_close(sp, FALSE, FALSE, cs->cr);
8184 
8185 	*cs->statusp = resp->status = status;
8186 
8187 end:
8188 	rfs4_sw_exit(&sp->rs_owner->ro_sw);
8189 	rfs4_state_rele(sp);
8190 out:
8191 	DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
8192 	    CLOSE4res *, resp);
8193 }
8194 
8195 /*
8196  * Manage the counts on the file struct and close all file locks
8197  */
8198 /*ARGSUSED*/
8199 void
8200 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
8201     bool_t close_of_client)
8202 {
8203 	rfs4_file_t *fp = sp->rs_finfo;
8204 	rfs4_lo_state_t *lsp;
8205 	int fflags = 0;
8206 
8207 	/*
8208 	 * If this call is part of the larger closing down of client
8209 	 * state then it is just easier to release all locks
8210 	 * associated with this client instead of going through each
8211 	 * individual file and cleaning locks there.
8212 	 */
8213 	if (close_of_client) {
8214 		if (sp->rs_owner->ro_client->rc_unlksys_completed == FALSE &&
8215 		    !list_is_empty(&sp->rs_lostatelist) &&
8216 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID) {
8217 			/* Is the PxFS kernel module loaded? */
8218 			if (lm_remove_file_locks != NULL) {
8219 				int new_sysid;
8220 
8221 				/* Encode the cluster nodeid in new sysid */
8222 				new_sysid = sp->rs_owner->ro_client->rc_sysidt;
8223 				lm_set_nlmid_flk(&new_sysid);
8224 
8225 				/*
8226 				 * This PxFS routine removes file locks for a
8227 				 * client over all nodes of a cluster.
8228 				 */
8229 				NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8230 				    "lm_remove_file_locks(sysid=0x%x)\n",
8231 				    new_sysid));
8232 				(*lm_remove_file_locks)(new_sysid);
8233 			} else {
8234 				struct flock64 flk;
8235 
8236 				/* Release all locks for this client */
8237 				flk.l_type = F_UNLKSYS;
8238 				flk.l_whence = 0;
8239 				flk.l_start = 0;
8240 				flk.l_len = 0;
8241 				flk.l_sysid =
8242 				    sp->rs_owner->ro_client->rc_sysidt;
8243 				flk.l_pid = 0;
8244 				(void) VOP_FRLOCK(sp->rs_finfo->rf_vp, F_SETLK,
8245 				    &flk, F_REMOTELOCK | FREAD | FWRITE,
8246 				    (u_offset_t)0, NULL, CRED(), NULL);
8247 			}
8248 
8249 			sp->rs_owner->ro_client->rc_unlksys_completed = TRUE;
8250 		}
8251 	}
8252 
8253 	/*
8254 	 * Release all locks on this file by this lock owner or at
8255 	 * least mark the locks as having been released
8256 	 */
8257 	for (lsp = list_head(&sp->rs_lostatelist); lsp != NULL;
8258 	    lsp = list_next(&sp->rs_lostatelist, lsp)) {
8259 		lsp->rls_locks_cleaned = TRUE;
8260 
8261 		/* Was this already taken care of above? */
8262 		if (!close_of_client &&
8263 		    sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8264 			(void) cleanlocks(sp->rs_finfo->rf_vp,
8265 			    lsp->rls_locker->rl_pid,
8266 			    lsp->rls_locker->rl_client->rc_sysidt);
8267 	}
8268 
8269 	/*
8270 	 * Release any shrlocks associated with this open state ID.
8271 	 * This must be done before the rfs4_state gets marked closed.
8272 	 */
8273 	if (sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8274 		(void) rfs4_unshare(sp);
8275 
8276 	if (sp->rs_opened) {
8277 		/*
8278 		 * Decrement the count for each access and deny bit that this
8279 		 * state has contributed to the file.
8280 		 * If the file counts go to zero
8281 		 * clear the appropriate bit in the appropriate mask.
8282 		 */
8283 		if (sp->rs_share_access & OPEN4_SHARE_ACCESS_READ) {
8284 			fp->rf_access_read--;
8285 			fflags |= FREAD;
8286 			if (fp->rf_access_read == 0)
8287 				fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
8288 		}
8289 		if (sp->rs_share_access & OPEN4_SHARE_ACCESS_WRITE) {
8290 			fp->rf_access_write--;
8291 			fflags |= FWRITE;
8292 			if (fp->rf_access_write == 0)
8293 				fp->rf_share_access &=
8294 				    ~OPEN4_SHARE_ACCESS_WRITE;
8295 		}
8296 		if (sp->rs_share_deny & OPEN4_SHARE_DENY_READ) {
8297 			fp->rf_deny_read--;
8298 			if (fp->rf_deny_read == 0)
8299 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
8300 		}
8301 		if (sp->rs_share_deny & OPEN4_SHARE_DENY_WRITE) {
8302 			fp->rf_deny_write--;
8303 			if (fp->rf_deny_write == 0)
8304 				fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
8305 		}
8306 
8307 		(void) VOP_CLOSE(fp->rf_vp, fflags, 1, (offset_t)0, cr, NULL);
8308 
8309 		sp->rs_opened = FALSE;
8310 	}
8311 }
8312 
8313 /*
8314  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8315  */
8316 static nfsstat4
8317 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8318 {
8319 	rfs4_lockowner_t *lo;
8320 	rfs4_client_t *cp;
8321 	uint32_t len;
8322 
8323 	lo = rfs4_findlockowner_by_pid(flk->l_pid);
8324 	if (lo != NULL) {
8325 		cp = lo->rl_client;
8326 		if (rfs4_lease_expired(cp)) {
8327 			rfs4_lockowner_rele(lo);
8328 			rfs4_dbe_hold(cp->rc_dbe);
8329 			rfs4_client_close(cp);
8330 			return (NFS4ERR_EXPIRED);
8331 		}
8332 		dp->owner.clientid = lo->rl_owner.clientid;
8333 		len = lo->rl_owner.owner_len;
8334 		dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8335 		bcopy(lo->rl_owner.owner_val, dp->owner.owner_val, len);
8336 		dp->owner.owner_len = len;
8337 		rfs4_lockowner_rele(lo);
8338 		goto finish;
8339 	}
8340 
8341 	/*
8342 	 * Its not a NFS4 lock. We take advantage that the upper 32 bits
8343 	 * of the client id contain the boot time for a NFS4 lock. So we
8344 	 * fabricate and identity by setting clientid to the sysid, and
8345 	 * the lock owner to the pid.
8346 	 */
8347 	dp->owner.clientid = flk->l_sysid;
8348 	len = sizeof (pid_t);
8349 	dp->owner.owner_len = len;
8350 	dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8351 	bcopy(&flk->l_pid, dp->owner.owner_val, len);
8352 finish:
8353 	dp->offset = flk->l_start;
8354 	dp->length = flk->l_len;
8355 
8356 	if (flk->l_type == F_RDLCK)
8357 		dp->locktype = READ_LT;
8358 	else if (flk->l_type == F_WRLCK)
8359 		dp->locktype = WRITE_LT;
8360 	else
8361 		return (NFS4ERR_INVAL);	/* no mapping from POSIX ltype to v4 */
8362 
8363 	return (NFS4_OK);
8364 }
8365 
8366 static int
8367 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8368 {
8369 	int error;
8370 	struct flock64 flk;
8371 	int i;
8372 	clock_t delaytime;
8373 	int cmd;
8374 
8375 	cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8376 retry:
8377 	delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8378 
8379 	for (i = 0; i < rfs4_maxlock_tries; i++) {
8380 		LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8381 		error = VOP_FRLOCK(vp, cmd,
8382 		    flock, flag, (u_offset_t)0, NULL, cred, NULL);
8383 
8384 		if (error != EAGAIN && error != EACCES)
8385 			break;
8386 
8387 		if (i < rfs4_maxlock_tries - 1) {
8388 			delay(delaytime);
8389 			delaytime *= 2;
8390 		}
8391 	}
8392 
8393 	if (error == EAGAIN || error == EACCES) {
8394 		/* Get the owner of the lock */
8395 		flk = *flock;
8396 		LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8397 		if (VOP_FRLOCK(vp, F_GETLK, &flk, flag,
8398 		    (u_offset_t)0, NULL, cred, NULL) == 0) {
8399 			if (flk.l_type == F_UNLCK) {
8400 				/* No longer locked, retry */
8401 				goto retry;
8402 			}
8403 			*flock = flk;
8404 			LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8405 			    F_GETLK, &flk);
8406 		}
8407 	}
8408 
8409 	return (error);
8410 }
8411 
8412 /*ARGSUSED*/
8413 static nfsstat4
8414 rfs4_do_lock(rfs4_lo_state_t *lsp, nfs_lock_type4 locktype,
8415     offset4 offset, length4 length, cred_t *cred, nfs_resop4 *resop)
8416 {
8417 	nfsstat4 status;
8418 	rfs4_lockowner_t *lo = lsp->rls_locker;
8419 	rfs4_state_t *sp = lsp->rls_state;
8420 	struct flock64 flock;
8421 	int16_t ltype;
8422 	int flag;
8423 	int error;
8424 	sysid_t sysid;
8425 	LOCK4res *lres;
8426 
8427 	if (rfs4_lease_expired(lo->rl_client)) {
8428 		return (NFS4ERR_EXPIRED);
8429 	}
8430 
8431 	if ((status = rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
8432 		return (status);
8433 
8434 	/* Check for zero length. To lock to end of file use all ones for V4 */
8435 	if (length == 0)
8436 		return (NFS4ERR_INVAL);
8437 	else if (length == (length4)(~0))
8438 		length = 0;		/* Posix to end of file  */
8439 
8440 retry:
8441 	rfs4_dbe_lock(sp->rs_dbe);
8442 	if (sp->rs_closed) {
8443 		rfs4_dbe_unlock(sp->rs_dbe);
8444 		return (NFS4ERR_OLD_STATEID);
8445 	}
8446 
8447 	if (resop->resop != OP_LOCKU) {
8448 		switch (locktype) {
8449 		case READ_LT:
8450 		case READW_LT:
8451 			if ((sp->rs_share_access
8452 			    & OPEN4_SHARE_ACCESS_READ) == 0) {
8453 				rfs4_dbe_unlock(sp->rs_dbe);
8454 
8455 				return (NFS4ERR_OPENMODE);
8456 			}
8457 			ltype = F_RDLCK;
8458 			break;
8459 		case WRITE_LT:
8460 		case WRITEW_LT:
8461 			if ((sp->rs_share_access
8462 			    & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8463 				rfs4_dbe_unlock(sp->rs_dbe);
8464 
8465 				return (NFS4ERR_OPENMODE);
8466 			}
8467 			ltype = F_WRLCK;
8468 			break;
8469 		}
8470 	} else
8471 		ltype = F_UNLCK;
8472 
8473 	flock.l_type = ltype;
8474 	flock.l_whence = 0;		/* SEEK_SET */
8475 	flock.l_start = offset;
8476 	flock.l_len = length;
8477 	flock.l_sysid = sysid;
8478 	flock.l_pid = lsp->rls_locker->rl_pid;
8479 
8480 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
8481 	if (flock.l_len < 0 || flock.l_start < 0) {
8482 		rfs4_dbe_unlock(sp->rs_dbe);
8483 		return (NFS4ERR_INVAL);
8484 	}
8485 
8486 	/*
8487 	 * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8488 	 * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8489 	 */
8490 	flag = (int)sp->rs_share_access | F_REMOTELOCK;
8491 
8492 	error = setlock(sp->rs_finfo->rf_vp, &flock, flag, cred);
8493 	if (error == 0) {
8494 		rfs4_dbe_lock(lsp->rls_dbe);
8495 		next_stateid(&lsp->rls_lockid);
8496 		rfs4_dbe_unlock(lsp->rls_dbe);
8497 	}
8498 
8499 	rfs4_dbe_unlock(sp->rs_dbe);
8500 
8501 	/*
8502 	 * N.B. We map error values to nfsv4 errors. This is differrent
8503 	 * than puterrno4 routine.
8504 	 */
8505 	switch (error) {
8506 	case 0:
8507 		status = NFS4_OK;
8508 		break;
8509 	case EAGAIN:
8510 	case EACCES:		/* Old value */
8511 		/* Can only get here if op is OP_LOCK */
8512 		ASSERT(resop->resop == OP_LOCK);
8513 		lres = &resop->nfs_resop4_u.oplock;
8514 		status = NFS4ERR_DENIED;
8515 		if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8516 		    == NFS4ERR_EXPIRED)
8517 			goto retry;
8518 		break;
8519 	case ENOLCK:
8520 		status = NFS4ERR_DELAY;
8521 		break;
8522 	case EOVERFLOW:
8523 		status = NFS4ERR_INVAL;
8524 		break;
8525 	case EINVAL:
8526 		status = NFS4ERR_NOTSUPP;
8527 		break;
8528 	default:
8529 		status = NFS4ERR_SERVERFAULT;
8530 		break;
8531 	}
8532 
8533 	return (status);
8534 }
8535 
8536 /*ARGSUSED*/
8537 void
8538 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8539     struct svc_req *req, struct compound_state *cs)
8540 {
8541 	LOCK4args *args = &argop->nfs_argop4_u.oplock;
8542 	LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8543 	nfsstat4 status;
8544 	stateid4 *stateid;
8545 	rfs4_lockowner_t *lo;
8546 	rfs4_client_t *cp;
8547 	rfs4_state_t *sp = NULL;
8548 	rfs4_lo_state_t *lsp = NULL;
8549 	bool_t ls_sw_held = FALSE;
8550 	bool_t create = TRUE;
8551 	bool_t lcreate = TRUE;
8552 	bool_t dup_lock = FALSE;
8553 	int rc;
8554 
8555 	DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8556 	    LOCK4args *, args);
8557 
8558 	if (cs->vp == NULL) {
8559 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8560 		DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8561 		    cs, LOCK4res *, resp);
8562 		return;
8563 	}
8564 
8565 	if (args->locker.new_lock_owner) {
8566 		/* Create a new lockowner for this instance */
8567 		open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8568 
8569 		NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8570 
8571 		stateid = &olo->open_stateid;
8572 		status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8573 		if (status != NFS4_OK) {
8574 			NFS4_DEBUG(rfs4_debug,
8575 			    (CE_NOTE, "Get state failed in lock %d", status));
8576 			*cs->statusp = resp->status = status;
8577 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8578 			    cs, LOCK4res *, resp);
8579 			return;
8580 		}
8581 
8582 		/* Ensure specified filehandle matches */
8583 		if (cs->vp != sp->rs_finfo->rf_vp) {
8584 			rfs4_state_rele(sp);
8585 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8586 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8587 			    cs, LOCK4res *, resp);
8588 			return;
8589 		}
8590 
8591 		/* hold off other access to open_owner while we tinker */
8592 		rfs4_sw_enter(&sp->rs_owner->ro_sw);
8593 
8594 		switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8595 		case NFS4_CHECK_STATEID_OLD:
8596 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8597 			goto end;
8598 		case NFS4_CHECK_STATEID_BAD:
8599 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8600 			goto end;
8601 		case NFS4_CHECK_STATEID_EXPIRED:
8602 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8603 			goto end;
8604 		case NFS4_CHECK_STATEID_UNCONFIRMED:
8605 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8606 			goto end;
8607 		case NFS4_CHECK_STATEID_CLOSED:
8608 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8609 			goto end;
8610 		case NFS4_CHECK_STATEID_OKAY:
8611 		case NFS4_CHECK_STATEID_REPLAY:
8612 			switch (rfs4_check_olo_seqid(olo->open_seqid,
8613 			    sp->rs_owner, resop)) {
8614 			case NFS4_CHKSEQ_OKAY:
8615 				if (rc == NFS4_CHECK_STATEID_OKAY)
8616 					break;
8617 				/*
8618 				 * This is replayed stateid; if seqid
8619 				 * matches next expected, then client
8620 				 * is using wrong seqid.
8621 				 */
8622 				/* FALLTHROUGH */
8623 			case NFS4_CHKSEQ_BAD:
8624 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8625 				goto end;
8626 			case NFS4_CHKSEQ_REPLAY:
8627 				/* This is a duplicate LOCK request */
8628 				dup_lock = TRUE;
8629 
8630 				/*
8631 				 * For a duplicate we do not want to
8632 				 * create a new lockowner as it should
8633 				 * already exist.
8634 				 * Turn off the lockowner create flag.
8635 				 */
8636 				lcreate = FALSE;
8637 			}
8638 			break;
8639 		}
8640 
8641 		lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8642 		if (lo == NULL) {
8643 			NFS4_DEBUG(rfs4_debug,
8644 			    (CE_NOTE, "rfs4_op_lock: no lock owner"));
8645 			*cs->statusp = resp->status = NFS4ERR_RESOURCE;
8646 			goto end;
8647 		}
8648 
8649 		lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8650 		if (lsp == NULL) {
8651 			rfs4_update_lease(sp->rs_owner->ro_client);
8652 			/*
8653 			 * Only update theh open_seqid if this is not
8654 			 * a duplicate request
8655 			 */
8656 			if (dup_lock == FALSE) {
8657 				rfs4_update_open_sequence(sp->rs_owner);
8658 			}
8659 
8660 			NFS4_DEBUG(rfs4_debug,
8661 			    (CE_NOTE, "rfs4_op_lock: no state"));
8662 			*cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8663 			rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8664 			rfs4_lockowner_rele(lo);
8665 			goto end;
8666 		}
8667 
8668 		/*
8669 		 * This is the new_lock_owner branch and the client is
8670 		 * supposed to be associating a new lock_owner with
8671 		 * the open file at this point.  If we find that a
8672 		 * lock_owner/state association already exists and a
8673 		 * successful LOCK request was returned to the client,
8674 		 * an error is returned to the client since this is
8675 		 * not appropriate.  The client should be using the
8676 		 * existing lock_owner branch.
8677 		 */
8678 		if (dup_lock == FALSE && create == FALSE) {
8679 			if (lsp->rls_lock_completed == TRUE) {
8680 				*cs->statusp =
8681 				    resp->status = NFS4ERR_BAD_SEQID;
8682 				rfs4_lockowner_rele(lo);
8683 				goto end;
8684 			}
8685 		}
8686 
8687 		rfs4_update_lease(sp->rs_owner->ro_client);
8688 
8689 		/*
8690 		 * Only update theh open_seqid if this is not
8691 		 * a duplicate request
8692 		 */
8693 		if (dup_lock == FALSE) {
8694 			rfs4_update_open_sequence(sp->rs_owner);
8695 		}
8696 
8697 		/*
8698 		 * If this is a duplicate lock request, just copy the
8699 		 * previously saved reply and return.
8700 		 */
8701 		if (dup_lock == TRUE) {
8702 			/* verify that lock_seqid's match */
8703 			if (lsp->rls_seqid != olo->lock_seqid) {
8704 				NFS4_DEBUG(rfs4_debug,
8705 				    (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
8706 				    "lsp->seqid=%d old->seqid=%d",
8707 				    lsp->rls_seqid, olo->lock_seqid));
8708 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8709 			} else {
8710 				rfs4_copy_reply(resop, &lsp->rls_reply);
8711 				/*
8712 				 * Make sure to copy the just
8713 				 * retrieved reply status into the
8714 				 * overall compound status
8715 				 */
8716 				*cs->statusp = resp->status;
8717 			}
8718 			rfs4_lockowner_rele(lo);
8719 			goto end;
8720 		}
8721 
8722 		rfs4_dbe_lock(lsp->rls_dbe);
8723 
8724 		/* Make sure to update the lock sequence id */
8725 		lsp->rls_seqid = olo->lock_seqid;
8726 
8727 		NFS4_DEBUG(rfs4_debug,
8728 		    (CE_NOTE, "Lock seqid established as %d", lsp->rls_seqid));
8729 
8730 		/*
8731 		 * This is used to signify the newly created lockowner
8732 		 * stateid and its sequence number.  The checks for
8733 		 * sequence number and increment don't occur on the
8734 		 * very first lock request for a lockowner.
8735 		 */
8736 		lsp->rls_skip_seqid_check = TRUE;
8737 
8738 		/* hold off other access to lsp while we tinker */
8739 		rfs4_sw_enter(&lsp->rls_sw);
8740 		ls_sw_held = TRUE;
8741 
8742 		rfs4_dbe_unlock(lsp->rls_dbe);
8743 
8744 		rfs4_lockowner_rele(lo);
8745 	} else {
8746 		stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
8747 		/* get lsp and hold the lock on the underlying file struct */
8748 		if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
8749 		    != NFS4_OK) {
8750 			*cs->statusp = resp->status = status;
8751 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8752 			    cs, LOCK4res *, resp);
8753 			return;
8754 		}
8755 		create = FALSE;	/* We didn't create lsp */
8756 
8757 		/* Ensure specified filehandle matches */
8758 		if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
8759 			rfs4_lo_state_rele(lsp, TRUE);
8760 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8761 			DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8762 			    cs, LOCK4res *, resp);
8763 			return;
8764 		}
8765 
8766 		/* hold off other access to lsp while we tinker */
8767 		rfs4_sw_enter(&lsp->rls_sw);
8768 		ls_sw_held = TRUE;
8769 
8770 		switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
8771 		/*
8772 		 * The stateid looks like it was okay (expected to be
8773 		 * the next one)
8774 		 */
8775 		case NFS4_CHECK_STATEID_OKAY:
8776 			/*
8777 			 * The sequence id is now checked.  Determine
8778 			 * if this is a replay or if it is in the
8779 			 * expected (next) sequence.  In the case of a
8780 			 * replay, there are two replay conditions
8781 			 * that may occur.  The first is the normal
8782 			 * condition where a LOCK is done with a
8783 			 * NFS4_OK response and the stateid is
8784 			 * updated.  That case is handled below when
8785 			 * the stateid is identified as a REPLAY.  The
8786 			 * second is the case where an error is
8787 			 * returned, like NFS4ERR_DENIED, and the
8788 			 * sequence number is updated but the stateid
8789 			 * is not updated.  This second case is dealt
8790 			 * with here.  So it may seem odd that the
8791 			 * stateid is okay but the sequence id is a
8792 			 * replay but it is okay.
8793 			 */
8794 			switch (rfs4_check_lock_seqid(
8795 			    args->locker.locker4_u.lock_owner.lock_seqid,
8796 			    lsp, resop)) {
8797 			case NFS4_CHKSEQ_REPLAY:
8798 				if (resp->status != NFS4_OK) {
8799 					/*
8800 					 * Here is our replay and need
8801 					 * to verify that the last
8802 					 * response was an error.
8803 					 */
8804 					*cs->statusp = resp->status;
8805 					goto end;
8806 				}
8807 				/*
8808 				 * This is done since the sequence id
8809 				 * looked like a replay but it didn't
8810 				 * pass our check so a BAD_SEQID is
8811 				 * returned as a result.
8812 				 */
8813 				/*FALLTHROUGH*/
8814 			case NFS4_CHKSEQ_BAD:
8815 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8816 				goto end;
8817 			case NFS4_CHKSEQ_OKAY:
8818 				/* Everything looks okay move ahead */
8819 				break;
8820 			}
8821 			break;
8822 		case NFS4_CHECK_STATEID_OLD:
8823 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8824 			goto end;
8825 		case NFS4_CHECK_STATEID_BAD:
8826 			*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8827 			goto end;
8828 		case NFS4_CHECK_STATEID_EXPIRED:
8829 			*cs->statusp = resp->status = NFS4ERR_EXPIRED;
8830 			goto end;
8831 		case NFS4_CHECK_STATEID_CLOSED:
8832 			*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8833 			goto end;
8834 		case NFS4_CHECK_STATEID_REPLAY:
8835 			switch (rfs4_check_lock_seqid(
8836 			    args->locker.locker4_u.lock_owner.lock_seqid,
8837 			    lsp, resop)) {
8838 			case NFS4_CHKSEQ_OKAY:
8839 				/*
8840 				 * This is a replayed stateid; if
8841 				 * seqid matches the next expected,
8842 				 * then client is using wrong seqid.
8843 				 */
8844 			case NFS4_CHKSEQ_BAD:
8845 				*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8846 				goto end;
8847 			case NFS4_CHKSEQ_REPLAY:
8848 				rfs4_update_lease(lsp->rls_locker->rl_client);
8849 				*cs->statusp = status = resp->status;
8850 				goto end;
8851 			}
8852 			break;
8853 		default:
8854 			ASSERT(FALSE);
8855 			break;
8856 		}
8857 
8858 		rfs4_update_lock_sequence(lsp);
8859 		rfs4_update_lease(lsp->rls_locker->rl_client);
8860 	}
8861 
8862 	/*
8863 	 * NFS4 only allows locking on regular files, so
8864 	 * verify type of object.
8865 	 */
8866 	if (cs->vp->v_type != VREG) {
8867 		if (cs->vp->v_type == VDIR)
8868 			status = NFS4ERR_ISDIR;
8869 		else
8870 			status = NFS4ERR_INVAL;
8871 		goto out;
8872 	}
8873 
8874 	cp = lsp->rls_state->rs_owner->ro_client;
8875 
8876 	if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
8877 		status = NFS4ERR_GRACE;
8878 		goto out;
8879 	}
8880 
8881 	if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->rc_can_reclaim) {
8882 		status = NFS4ERR_NO_GRACE;
8883 		goto out;
8884 	}
8885 
8886 	if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
8887 		status = NFS4ERR_NO_GRACE;
8888 		goto out;
8889 	}
8890 
8891 	if (lsp->rls_state->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE)
8892 		cs->deleg = TRUE;
8893 
8894 	status = rfs4_do_lock(lsp, args->locktype,
8895 	    args->offset, args->length, cs->cr, resop);
8896 
8897 out:
8898 	lsp->rls_skip_seqid_check = FALSE;
8899 
8900 	*cs->statusp = resp->status = status;
8901 
8902 	if (status == NFS4_OK) {
8903 		resp->LOCK4res_u.lock_stateid = lsp->rls_lockid.stateid;
8904 		lsp->rls_lock_completed = TRUE;
8905 	}
8906 	/*
8907 	 * Only update the "OPEN" response here if this was a new
8908 	 * lock_owner
8909 	 */
8910 	if (sp)
8911 		rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8912 
8913 	rfs4_update_lock_resp(lsp, resop);
8914 
8915 end:
8916 	if (lsp) {
8917 		if (ls_sw_held)
8918 			rfs4_sw_exit(&lsp->rls_sw);
8919 		/*
8920 		 * If an sp obtained, then the lsp does not represent
8921 		 * a lock on the file struct.
8922 		 */
8923 		if (sp != NULL)
8924 			rfs4_lo_state_rele(lsp, FALSE);
8925 		else
8926 			rfs4_lo_state_rele(lsp, TRUE);
8927 	}
8928 	if (sp) {
8929 		rfs4_sw_exit(&sp->rs_owner->ro_sw);
8930 		rfs4_state_rele(sp);
8931 	}
8932 
8933 	DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
8934 	    LOCK4res *, resp);
8935 }
8936 
8937 /* free function for LOCK/LOCKT */
8938 static void
8939 lock_denied_free(nfs_resop4 *resop)
8940 {
8941 	LOCK4denied *dp = NULL;
8942 
8943 	switch (resop->resop) {
8944 	case OP_LOCK:
8945 		if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
8946 			dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
8947 		break;
8948 	case OP_LOCKT:
8949 		if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
8950 			dp = &resop->nfs_resop4_u.oplockt.denied;
8951 		break;
8952 	default:
8953 		break;
8954 	}
8955 
8956 	if (dp)
8957 		kmem_free(dp->owner.owner_val, dp->owner.owner_len);
8958 }
8959 
8960 /*ARGSUSED*/
8961 void
8962 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
8963     struct svc_req *req, struct compound_state *cs)
8964 {
8965 	LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
8966 	LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
8967 	nfsstat4 status;
8968 	stateid4 *stateid = &args->lock_stateid;
8969 	rfs4_lo_state_t *lsp;
8970 
8971 	DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
8972 	    LOCKU4args *, args);
8973 
8974 	if (cs->vp == NULL) {
8975 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8976 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8977 		    LOCKU4res *, resp);
8978 		return;
8979 	}
8980 
8981 	if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
8982 		*cs->statusp = resp->status = status;
8983 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8984 		    LOCKU4res *, resp);
8985 		return;
8986 	}
8987 
8988 	/* Ensure specified filehandle matches */
8989 	if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
8990 		rfs4_lo_state_rele(lsp, TRUE);
8991 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8992 		DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
8993 		    LOCKU4res *, resp);
8994 		return;
8995 	}
8996 
8997 	/* hold off other access to lsp while we tinker */
8998 	rfs4_sw_enter(&lsp->rls_sw);
8999 
9000 	switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9001 	case NFS4_CHECK_STATEID_OKAY:
9002 		if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
9003 		    != NFS4_CHKSEQ_OKAY) {
9004 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9005 			goto end;
9006 		}
9007 		break;
9008 	case NFS4_CHECK_STATEID_OLD:
9009 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9010 		goto end;
9011 	case NFS4_CHECK_STATEID_BAD:
9012 		*cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9013 		goto end;
9014 	case NFS4_CHECK_STATEID_EXPIRED:
9015 		*cs->statusp = resp->status = NFS4ERR_EXPIRED;
9016 		goto end;
9017 	case NFS4_CHECK_STATEID_CLOSED:
9018 		*cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9019 		goto end;
9020 	case NFS4_CHECK_STATEID_REPLAY:
9021 		switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
9022 		case NFS4_CHKSEQ_OKAY:
9023 				/*
9024 				 * This is a replayed stateid; if
9025 				 * seqid matches the next expected,
9026 				 * then client is using wrong seqid.
9027 				 */
9028 		case NFS4_CHKSEQ_BAD:
9029 			*cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9030 			goto end;
9031 		case NFS4_CHKSEQ_REPLAY:
9032 			rfs4_update_lease(lsp->rls_locker->rl_client);
9033 			*cs->statusp = status = resp->status;
9034 			goto end;
9035 		}
9036 		break;
9037 	default:
9038 		ASSERT(FALSE);
9039 		break;
9040 	}
9041 
9042 	rfs4_update_lock_sequence(lsp);
9043 	rfs4_update_lease(lsp->rls_locker->rl_client);
9044 
9045 	/*
9046 	 * NFS4 only allows locking on regular files, so
9047 	 * verify type of object.
9048 	 */
9049 	if (cs->vp->v_type != VREG) {
9050 		if (cs->vp->v_type == VDIR)
9051 			status = NFS4ERR_ISDIR;
9052 		else
9053 			status = NFS4ERR_INVAL;
9054 		goto out;
9055 	}
9056 
9057 	if (rfs4_clnt_in_grace(lsp->rls_state->rs_owner->ro_client)) {
9058 		status = NFS4ERR_GRACE;
9059 		goto out;
9060 	}
9061 
9062 	status = rfs4_do_lock(lsp, args->locktype,
9063 	    args->offset, args->length, cs->cr, resop);
9064 
9065 out:
9066 	*cs->statusp = resp->status = status;
9067 
9068 	if (status == NFS4_OK)
9069 		resp->lock_stateid = lsp->rls_lockid.stateid;
9070 
9071 	rfs4_update_lock_resp(lsp, resop);
9072 
9073 end:
9074 	rfs4_sw_exit(&lsp->rls_sw);
9075 	rfs4_lo_state_rele(lsp, TRUE);
9076 
9077 	DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9078 	    LOCKU4res *, resp);
9079 }
9080 
9081 /*
9082  * LOCKT is a best effort routine, the client can not be guaranteed that
9083  * the status return is still in effect by the time the reply is received.
9084  * They are numerous race conditions in this routine, but we are not required
9085  * and can not be accurate.
9086  */
9087 /*ARGSUSED*/
9088 void
9089 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
9090     struct svc_req *req, struct compound_state *cs)
9091 {
9092 	LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
9093 	LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
9094 	rfs4_lockowner_t *lo;
9095 	rfs4_client_t *cp;
9096 	bool_t create = FALSE;
9097 	struct flock64 flk;
9098 	int error;
9099 	int flag = FREAD | FWRITE;
9100 	int ltype;
9101 	length4 posix_length;
9102 	sysid_t sysid;
9103 	pid_t pid;
9104 
9105 	DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
9106 	    LOCKT4args *, args);
9107 
9108 	if (cs->vp == NULL) {
9109 		*cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9110 		goto out;
9111 	}
9112 
9113 	/*
9114 	 * NFS4 only allows locking on regular files, so
9115 	 * verify type of object.
9116 	 */
9117 	if (cs->vp->v_type != VREG) {
9118 		if (cs->vp->v_type == VDIR)
9119 			*cs->statusp = resp->status = NFS4ERR_ISDIR;
9120 		else
9121 			*cs->statusp = resp->status =  NFS4ERR_INVAL;
9122 		goto out;
9123 	}
9124 
9125 	/*
9126 	 * Check out the clientid to ensure the server knows about it
9127 	 * so that we correctly inform the client of a server reboot.
9128 	 */
9129 	if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
9130 	    == NULL) {
9131 		*cs->statusp = resp->status =
9132 		    rfs4_check_clientid(&args->owner.clientid, 0);
9133 		goto out;
9134 	}
9135 	if (rfs4_lease_expired(cp)) {
9136 		rfs4_client_close(cp);
9137 		/*
9138 		 * Protocol doesn't allow returning NFS4ERR_STALE as
9139 		 * other operations do on this check so STALE_CLIENTID
9140 		 * is returned instead
9141 		 */
9142 		*cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
9143 		goto out;
9144 	}
9145 
9146 	if (rfs4_clnt_in_grace(cp) && !(cp->rc_can_reclaim)) {
9147 		*cs->statusp = resp->status = NFS4ERR_GRACE;
9148 		rfs4_client_rele(cp);
9149 		goto out;
9150 	}
9151 	rfs4_client_rele(cp);
9152 
9153 	resp->status = NFS4_OK;
9154 
9155 	switch (args->locktype) {
9156 	case READ_LT:
9157 	case READW_LT:
9158 		ltype = F_RDLCK;
9159 		break;
9160 	case WRITE_LT:
9161 	case WRITEW_LT:
9162 		ltype = F_WRLCK;
9163 		break;
9164 	}
9165 
9166 	posix_length = args->length;
9167 	/* Check for zero length. To lock to end of file use all ones for V4 */
9168 	if (posix_length == 0) {
9169 		*cs->statusp = resp->status = NFS4ERR_INVAL;
9170 		goto out;
9171 	} else if (posix_length == (length4)(~0)) {
9172 		posix_length = 0;	/* Posix to end of file  */
9173 	}
9174 
9175 	/* Find or create a lockowner */
9176 	lo = rfs4_findlockowner(&args->owner, &create);
9177 
9178 	if (lo) {
9179 		pid = lo->rl_pid;
9180 		if ((resp->status =
9181 		    rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
9182 			goto err;
9183 	} else {
9184 		pid = 0;
9185 		sysid = lockt_sysid;
9186 	}
9187 retry:
9188 	flk.l_type = ltype;
9189 	flk.l_whence = 0;		/* SEEK_SET */
9190 	flk.l_start = args->offset;
9191 	flk.l_len = posix_length;
9192 	flk.l_sysid = sysid;
9193 	flk.l_pid = pid;
9194 	flag |= F_REMOTELOCK;
9195 
9196 	LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
9197 
9198 	/* Note that length4 is uint64_t but l_len and l_start are off64_t */
9199 	if (flk.l_len < 0 || flk.l_start < 0) {
9200 		resp->status = NFS4ERR_INVAL;
9201 		goto err;
9202 	}
9203 	error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9204 	    NULL, cs->cr, NULL);
9205 
9206 	/*
9207 	 * N.B. We map error values to nfsv4 errors. This is differrent
9208 	 * than puterrno4 routine.
9209 	 */
9210 	switch (error) {
9211 	case 0:
9212 		if (flk.l_type == F_UNLCK)
9213 			resp->status = NFS4_OK;
9214 		else {
9215 			if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
9216 				goto retry;
9217 			resp->status = NFS4ERR_DENIED;
9218 		}
9219 		break;
9220 	case EOVERFLOW:
9221 		resp->status = NFS4ERR_INVAL;
9222 		break;
9223 	case EINVAL:
9224 		resp->status = NFS4ERR_NOTSUPP;
9225 		break;
9226 	default:
9227 		cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
9228 		    error);
9229 		resp->status = NFS4ERR_SERVERFAULT;
9230 		break;
9231 	}
9232 
9233 err:
9234 	if (lo)
9235 		rfs4_lockowner_rele(lo);
9236 	*cs->statusp = resp->status;
9237 out:
9238 	DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9239 	    LOCKT4res *, resp);
9240 }
9241 
9242 int
9243 rfs4_share(rfs4_state_t *sp, uint32_t access, uint32_t deny)
9244 {
9245 	int err;
9246 	int cmd;
9247 	vnode_t *vp;
9248 	struct shrlock shr;
9249 	struct shr_locowner shr_loco;
9250 	int fflags = 0;
9251 
9252 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9253 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9254 
9255 	if (sp->rs_closed)
9256 		return (NFS4ERR_OLD_STATEID);
9257 
9258 	vp = sp->rs_finfo->rf_vp;
9259 	ASSERT(vp);
9260 
9261 	shr.s_access = shr.s_deny = 0;
9262 
9263 	if (access & OPEN4_SHARE_ACCESS_READ) {
9264 		fflags |= FREAD;
9265 		shr.s_access |= F_RDACC;
9266 	}
9267 	if (access & OPEN4_SHARE_ACCESS_WRITE) {
9268 		fflags |= FWRITE;
9269 		shr.s_access |= F_WRACC;
9270 	}
9271 	ASSERT(shr.s_access);
9272 
9273 	if (deny & OPEN4_SHARE_DENY_READ)
9274 		shr.s_deny |= F_RDDNY;
9275 	if (deny & OPEN4_SHARE_DENY_WRITE)
9276 		shr.s_deny |= F_WRDNY;
9277 
9278 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9279 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9280 	shr_loco.sl_pid = shr.s_pid;
9281 	shr_loco.sl_id = shr.s_sysid;
9282 	shr.s_owner = (caddr_t)&shr_loco;
9283 	shr.s_own_len = sizeof (shr_loco);
9284 
9285 	cmd = nbl_need_check(vp) ? F_SHARE_NBMAND : F_SHARE;
9286 
9287 	err = VOP_SHRLOCK(vp, cmd, &shr, fflags, CRED(), NULL);
9288 	if (err != 0) {
9289 		if (err == EAGAIN)
9290 			err = NFS4ERR_SHARE_DENIED;
9291 		else
9292 			err = puterrno4(err);
9293 		return (err);
9294 	}
9295 
9296 	sp->rs_share_access |= access;
9297 	sp->rs_share_deny |= deny;
9298 
9299 	return (0);
9300 }
9301 
9302 int
9303 rfs4_unshare(rfs4_state_t *sp)
9304 {
9305 	int err;
9306 	struct shrlock shr;
9307 	struct shr_locowner shr_loco;
9308 
9309 	ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9310 
9311 	if (sp->rs_closed || sp->rs_share_access == 0)
9312 		return (0);
9313 
9314 	ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9315 	ASSERT(sp->rs_finfo->rf_vp);
9316 
9317 	shr.s_access = shr.s_deny = 0;
9318 	shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9319 	shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9320 	shr_loco.sl_pid = shr.s_pid;
9321 	shr_loco.sl_id = shr.s_sysid;
9322 	shr.s_owner = (caddr_t)&shr_loco;
9323 	shr.s_own_len = sizeof (shr_loco);
9324 
9325 	err = VOP_SHRLOCK(sp->rs_finfo->rf_vp, F_UNSHARE, &shr, 0, CRED(),
9326 	    NULL);
9327 	if (err != 0) {
9328 		err = puterrno4(err);
9329 		return (err);
9330 	}
9331 
9332 	sp->rs_share_access = 0;
9333 	sp->rs_share_deny = 0;
9334 
9335 	return (0);
9336 
9337 }
9338 
9339 static int
9340 rdma_setup_read_data4(READ4args *args, READ4res *rok)
9341 {
9342 	struct clist	*wcl;
9343 	count4		count = rok->data_len;
9344 	int		wlist_len;
9345 
9346 	wcl = args->wlist;
9347 	if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) {
9348 		return (FALSE);
9349 	}
9350 	wcl = args->wlist;
9351 	rok->wlist_len = wlist_len;
9352 	rok->wlist = wcl;
9353 	return (TRUE);
9354 }
9355