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