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