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