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