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