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