xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_vnops.c (revision 5d9d9091f564c198a760790b0bfa72c44e17912b)
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 /*
23  * Copyright (c) 2016 STRATO AG. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
28  */
29 
30 /*
31  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
32  * Use is subject to license terms.
33  */
34 
35 /*
36  *	Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
37  *	All Rights Reserved
38  */
39 
40 /*
41  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
42  * Copyright 2022 Oxide Computer Company
43  */
44 
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/cred.h>
49 #include <sys/time.h>
50 #include <sys/vnode.h>
51 #include <sys/vfs.h>
52 #include <sys/vfs_opreg.h>
53 #include <sys/file.h>
54 #include <sys/filio.h>
55 #include <sys/uio.h>
56 #include <sys/buf.h>
57 #include <sys/mman.h>
58 #include <sys/pathname.h>
59 #include <sys/dirent.h>
60 #include <sys/debug.h>
61 #include <sys/vmsystm.h>
62 #include <sys/fcntl.h>
63 #include <sys/flock.h>
64 #include <sys/swap.h>
65 #include <sys/errno.h>
66 #include <sys/strsubr.h>
67 #include <sys/sysmacros.h>
68 #include <sys/kmem.h>
69 #include <sys/cmn_err.h>
70 #include <sys/pathconf.h>
71 #include <sys/utsname.h>
72 #include <sys/dnlc.h>
73 #include <sys/acl.h>
74 #include <sys/systeminfo.h>
75 #include <sys/policy.h>
76 #include <sys/sdt.h>
77 #include <sys/list.h>
78 #include <sys/stat.h>
79 #include <sys/zone.h>
80 
81 #include <rpc/types.h>
82 #include <rpc/auth.h>
83 #include <rpc/clnt.h>
84 
85 #include <nfs/nfs.h>
86 #include <nfs/nfs_clnt.h>
87 #include <nfs/nfs_acl.h>
88 #include <nfs/lm.h>
89 #include <nfs/nfs4.h>
90 #include <nfs/nfs4_kprot.h>
91 #include <nfs/rnode4.h>
92 #include <nfs/nfs4_clnt.h>
93 
94 #include <vm/hat.h>
95 #include <vm/as.h>
96 #include <vm/page.h>
97 #include <vm/pvn.h>
98 #include <vm/seg.h>
99 #include <vm/seg_map.h>
100 #include <vm/seg_kpm.h>
101 #include <vm/seg_vn.h>
102 
103 #include <fs/fs_subr.h>
104 
105 #include <sys/ddi.h>
106 #include <sys/int_fmtio.h>
107 #include <sys/fs/autofs.h>
108 
109 typedef struct {
110 	nfs4_ga_res_t	*di_garp;
111 	cred_t		*di_cred;
112 	hrtime_t	di_time_call;
113 } dirattr_info_t;
114 
115 typedef enum nfs4_acl_op {
116 	NFS4_ACL_GET,
117 	NFS4_ACL_SET
118 } nfs4_acl_op_t;
119 
120 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *);
121 
122 static void	nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
123 			char *, dirattr_info_t *);
124 
125 static void	nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
126 		    nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
127 		    nfs4_error_t *, int *);
128 static int	nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
129 			cred_t *);
130 static int	nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
131 			stable_how4 *);
132 static int	nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
133 			cred_t *, bool_t, struct uio *);
134 static int	nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
135 			vsecattr_t *);
136 static int	nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
137 static int	nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
138 static int	nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
139 static int	nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
140 static int	nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
141 static int	nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
142 			int, vnode_t **, cred_t *);
143 static int	nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
144 			cred_t *, int, int, enum createmode4, int);
145 static int	nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
146 			caller_context_t *);
147 static int	nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
148 			vnode_t *, char *, cred_t *, nfsstat4 *);
149 static int	nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
150 			vnode_t *, char *, cred_t *, nfsstat4 *);
151 static int	do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
152 static void	nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
153 static int	nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
154 static int	nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
155 			page_t *[], size_t, struct seg *, caddr_t,
156 			enum seg_rw, cred_t *);
157 static void	nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
158 			cred_t *);
159 static int	nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
160 			int, cred_t *);
161 static int	nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
162 			int, cred_t *);
163 static int	nfs4_commit(vnode_t *, offset4, count4, cred_t *);
164 static void	nfs4_set_mod(vnode_t *);
165 static void	nfs4_get_commit(vnode_t *);
166 static void	nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
167 static int	nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
168 static int	nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
169 static int	nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
170 			cred_t *);
171 static void	do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
172 			cred_t *);
173 static int	nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
174 			hrtime_t, vnode_t *, cred_t *);
175 static int	nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
176 static int	nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
177 static void	nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
178 			u_offset_t);
179 static int	nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
180 static int	nfs4_block_and_wait(clock_t *, rnode4_t *);
181 static cred_t  *state_to_cred(nfs4_open_stream_t *);
182 static void	denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
183 static pid_t	lo_to_pid(lock_owner4 *);
184 static void	nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
185 			cred_t *, nfs4_lock_owner_t *);
186 static void	push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
187 			nfs4_lock_owner_t *);
188 static int	open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
189 static void	nfs4_delmap_callback(struct as *, void *, uint_t);
190 static void	nfs4_free_delmapcall(nfs4_delmapcall_t *);
191 static nfs4_delmapcall_t	*nfs4_init_delmapcall();
192 static int	nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
193 static int	nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
194 static int	nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
195 			uid_t, gid_t, int);
196 
197 /*
198  * Routines that implement the setting of v4 args for the misc. ops
199  */
200 static void	nfs4args_lock_free(nfs_argop4 *);
201 static void	nfs4args_lockt_free(nfs_argop4 *);
202 static void	nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
203 			int, rnode4_t *, cred_t *, bitmap4, int *,
204 			nfs4_stateid_types_t *);
205 static void	nfs4args_setattr_free(nfs_argop4 *);
206 static int	nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
207 			bitmap4);
208 static void	nfs4args_verify_free(nfs_argop4 *);
209 static void	nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
210 			WRITE4args **, nfs4_stateid_types_t *);
211 
212 /*
213  * These are the vnode ops functions that implement the vnode interface to
214  * the networked file system.  See more comments below at nfs4_vnodeops.
215  */
216 static int	nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
217 static int	nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
218 			caller_context_t *);
219 static int	nfs4_read(vnode_t *, struct uio *, int, cred_t *,
220 			caller_context_t *);
221 static int	nfs4_write(vnode_t *, struct uio *, int, cred_t *,
222 			caller_context_t *);
223 static int	nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
224 			caller_context_t *);
225 static int	nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
226 			caller_context_t *);
227 static int	nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
228 static int	nfs4_readlink(vnode_t *, struct uio *, cred_t *,
229 			caller_context_t *);
230 static int	nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
231 static int	nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
232 			int, vnode_t **, cred_t *, int, caller_context_t *,
233 			vsecattr_t *);
234 static int	nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
235 			int);
236 static int	nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
237 			caller_context_t *, int);
238 static int	nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
239 			caller_context_t *, int);
240 static int	nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
241 			cred_t *, caller_context_t *, int, vsecattr_t *);
242 static int	nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
243 			caller_context_t *, int);
244 static int	nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
245 			cred_t *, caller_context_t *, int);
246 static int	nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
247 			caller_context_t *, int);
248 static int	nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
249 static int	nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
250 			page_t *[], size_t, struct seg *, caddr_t,
251 			enum seg_rw, cred_t *, caller_context_t *);
252 static int	nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
253 			caller_context_t *);
254 static int	nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
255 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
256 static int	nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
257 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
258 static int	nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
259 static int	nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
260 			struct flk_callback *, cred_t *, caller_context_t *);
261 static int	nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
262 			cred_t *, caller_context_t *);
263 static int	nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
264 			uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
265 static int	nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
266 			cred_t *, caller_context_t *);
267 static void	nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
268 			caller_context_t *);
269 static int	nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
270 			caller_context_t *);
271 /*
272  * These vnode ops are required to be called from outside this source file,
273  * e.g. by ephemeral mount stub vnode ops, and so may not be declared
274  * as static.
275  */
276 int	nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
277 	    caller_context_t *);
278 void	nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
279 int	nfs4_lookup(vnode_t *, char *, vnode_t **,
280 	    struct pathname *, int, vnode_t *, cred_t *,
281 	    caller_context_t *, int *, pathname_t *);
282 int	nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
283 int	nfs4_rwlock(vnode_t *, int, caller_context_t *);
284 void	nfs4_rwunlock(vnode_t *, int, caller_context_t *);
285 int	nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
286 int	nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
287 	    caller_context_t *);
288 int	nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
289 	    caller_context_t *);
290 int	nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
291 	    caller_context_t *);
292 
293 /*
294  * Used for nfs4_commit_vp() to indicate if we should
295  * wait on pending writes.
296  */
297 #define	NFS4_WRITE_NOWAIT	0
298 #define	NFS4_WRITE_WAIT		1
299 
300 #define	NFS4_BASE_WAIT_TIME 1	/* 1 second */
301 
302 /*
303  * Error flags used to pass information about certain special errors
304  * which need to be handled specially.
305  */
306 #define	NFS_EOF			-98
307 #define	NFS_VERF_MISMATCH	-97
308 
309 /*
310  * Flags used to differentiate between which operation drove the
311  * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
312  */
313 #define	NFS4_CLOSE_OP		0x1
314 #define	NFS4_DELMAP_OP		0x2
315 #define	NFS4_INACTIVE_OP	0x3
316 
317 #define	ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
318 
319 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
320 #define	ALIGN64(x, ptr, sz)						\
321 	x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1);		\
322 	if (x) {							\
323 		x = sizeof (uint64_t) - (x);				\
324 		sz -= (x);						\
325 		ptr += (x);						\
326 	}
327 
328 #ifdef DEBUG
329 int nfs4_client_attr_debug = 0;
330 int nfs4_client_state_debug = 0;
331 int nfs4_client_shadow_debug = 0;
332 int nfs4_client_lock_debug = 0;
333 int nfs4_seqid_sync = 0;
334 int nfs4_client_map_debug = 0;
335 static int nfs4_pageio_debug = 0;
336 int nfs4_client_inactive_debug = 0;
337 int nfs4_client_recov_debug = 0;
338 int nfs4_client_failover_debug = 0;
339 int nfs4_client_call_debug = 0;
340 int nfs4_client_lookup_debug = 0;
341 int nfs4_client_zone_debug = 0;
342 int nfs4_lost_rqst_debug = 0;
343 int nfs4_rdattrerr_debug = 0;
344 int nfs4_open_stream_debug = 0;
345 
346 int nfs4read_error_inject;
347 
348 static int nfs4_create_misses = 0;
349 
350 static int nfs4_readdir_cache_shorts = 0;
351 static int nfs4_readdir_readahead = 0;
352 
353 static int nfs4_bio_do_stop = 0;
354 
355 static int nfs4_lostpage = 0;	/* number of times we lost original page */
356 
357 int nfs4_mmap_debug = 0;
358 
359 static int nfs4_pathconf_cache_hits = 0;
360 static int nfs4_pathconf_cache_misses = 0;
361 
362 int nfs4close_all_cnt;
363 int nfs4close_one_debug = 0;
364 int nfs4close_notw_debug = 0;
365 
366 int denied_to_flk_debug = 0;
367 void *lockt_denied_debug;
368 
369 #endif
370 
371 /*
372  * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
373  * or NFS4ERR_RESOURCE.
374  */
375 static int confirm_retry_sec = 30;
376 
377 static int nfs4_lookup_neg_cache = 1;
378 
379 /*
380  * number of pages to read ahead
381  * optimized for 100 base-T.
382  */
383 static int nfs4_nra = 4;
384 
385 static int nfs4_do_symlink_cache = 1;
386 
387 static int nfs4_pathconf_disable_cache = 0;
388 
389 /*
390  * These are the vnode ops routines which implement the vnode interface to
391  * the networked file system.  These routines just take their parameters,
392  * make them look networkish by putting the right info into interface structs,
393  * and then calling the appropriate remote routine(s) to do the work.
394  *
395  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
396  * we purge the directory cache relative to that vnode.  This way, the
397  * user won't get burned by the cache repeatedly.  See <nfs/rnode4.h> for
398  * more details on rnode locking.
399  */
400 
401 struct vnodeops *nfs4_vnodeops;
402 
403 const fs_operation_def_t nfs4_vnodeops_template[] = {
404 	VOPNAME_OPEN,		{ .vop_open = nfs4_open },
405 	VOPNAME_CLOSE,		{ .vop_close = nfs4_close },
406 	VOPNAME_READ,		{ .vop_read = nfs4_read },
407 	VOPNAME_WRITE,		{ .vop_write = nfs4_write },
408 	VOPNAME_IOCTL,		{ .vop_ioctl = nfs4_ioctl },
409 	VOPNAME_GETATTR,	{ .vop_getattr = nfs4_getattr },
410 	VOPNAME_SETATTR,	{ .vop_setattr = nfs4_setattr },
411 	VOPNAME_ACCESS,		{ .vop_access = nfs4_access },
412 	VOPNAME_LOOKUP,		{ .vop_lookup = nfs4_lookup },
413 	VOPNAME_CREATE,		{ .vop_create = nfs4_create },
414 	VOPNAME_REMOVE,		{ .vop_remove = nfs4_remove },
415 	VOPNAME_LINK,		{ .vop_link = nfs4_link },
416 	VOPNAME_RENAME,		{ .vop_rename = nfs4_rename },
417 	VOPNAME_MKDIR,		{ .vop_mkdir = nfs4_mkdir },
418 	VOPNAME_RMDIR,		{ .vop_rmdir = nfs4_rmdir },
419 	VOPNAME_READDIR,	{ .vop_readdir = nfs4_readdir },
420 	VOPNAME_SYMLINK,	{ .vop_symlink = nfs4_symlink },
421 	VOPNAME_READLINK,	{ .vop_readlink = nfs4_readlink },
422 	VOPNAME_FSYNC,		{ .vop_fsync = nfs4_fsync },
423 	VOPNAME_INACTIVE,	{ .vop_inactive = nfs4_inactive },
424 	VOPNAME_FID,		{ .vop_fid = nfs4_fid },
425 	VOPNAME_RWLOCK,		{ .vop_rwlock = nfs4_rwlock },
426 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = nfs4_rwunlock },
427 	VOPNAME_SEEK,		{ .vop_seek = nfs4_seek },
428 	VOPNAME_FRLOCK,		{ .vop_frlock = nfs4_frlock },
429 	VOPNAME_SPACE,		{ .vop_space = nfs4_space },
430 	VOPNAME_REALVP,		{ .vop_realvp = nfs4_realvp },
431 	VOPNAME_GETPAGE,	{ .vop_getpage = nfs4_getpage },
432 	VOPNAME_PUTPAGE,	{ .vop_putpage = nfs4_putpage },
433 	VOPNAME_MAP,		{ .vop_map = nfs4_map },
434 	VOPNAME_ADDMAP,		{ .vop_addmap = nfs4_addmap },
435 	VOPNAME_DELMAP,		{ .vop_delmap = nfs4_delmap },
436 	/* no separate nfs4_dump */
437 	VOPNAME_DUMP,		{ .vop_dump = nfs_dump },
438 	VOPNAME_PATHCONF,	{ .vop_pathconf = nfs4_pathconf },
439 	VOPNAME_PAGEIO,		{ .vop_pageio = nfs4_pageio },
440 	VOPNAME_DISPOSE,	{ .vop_dispose = nfs4_dispose },
441 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = nfs4_setsecattr },
442 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = nfs4_getsecattr },
443 	VOPNAME_SHRLOCK,	{ .vop_shrlock = nfs4_shrlock },
444 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
445 	NULL,			NULL
446 };
447 
448 /*
449  * The following are subroutines and definitions to set args or get res
450  * for the different nfsv4 ops
451  */
452 
453 void
nfs4args_lookup_free(nfs_argop4 * argop,int arglen)454 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
455 {
456 	int		i;
457 
458 	for (i = 0; i < arglen; i++) {
459 		if (argop[i].argop == OP_LOOKUP) {
460 			kmem_free(
461 			    argop[i].nfs_argop4_u.oplookup.
462 			    objname.utf8string_val,
463 			    argop[i].nfs_argop4_u.oplookup.
464 			    objname.utf8string_len);
465 		}
466 	}
467 }
468 
469 static void
nfs4args_lock_free(nfs_argop4 * argop)470 nfs4args_lock_free(nfs_argop4 *argop)
471 {
472 	locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
473 
474 	if (locker->new_lock_owner == TRUE) {
475 		open_to_lock_owner4 *open_owner;
476 
477 		open_owner = &locker->locker4_u.open_owner;
478 		if (open_owner->lock_owner.owner_val != NULL) {
479 			kmem_free(open_owner->lock_owner.owner_val,
480 			    open_owner->lock_owner.owner_len);
481 		}
482 	}
483 }
484 
485 static void
nfs4args_lockt_free(nfs_argop4 * argop)486 nfs4args_lockt_free(nfs_argop4 *argop)
487 {
488 	lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
489 
490 	if (lowner->owner_val != NULL) {
491 		kmem_free(lowner->owner_val, lowner->owner_len);
492 	}
493 }
494 
495 static void
nfs4args_setattr(nfs_argop4 * argop,vattr_t * vap,vsecattr_t * vsap,int flags,rnode4_t * rp,cred_t * cr,bitmap4 supp,int * error,nfs4_stateid_types_t * sid_types)496 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
497     rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
498     nfs4_stateid_types_t *sid_types)
499 {
500 	fattr4		*attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
501 	mntinfo4_t	*mi;
502 
503 	argop->argop = OP_SETATTR;
504 	/*
505 	 * The stateid is set to 0 if client is not modifying the size
506 	 * and otherwise to whatever nfs4_get_stateid() returns.
507 	 *
508 	 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
509 	 * state struct could be found for the process/file pair.  We may
510 	 * want to change this in the future (by OPENing the file).  See
511 	 * bug # 4474852.
512 	 */
513 	if (vap->va_mask & AT_SIZE) {
514 
515 		ASSERT(rp != NULL);
516 		mi = VTOMI4(RTOV4(rp));
517 
518 		argop->nfs_argop4_u.opsetattr.stateid =
519 		    nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
520 		    OP_SETATTR, sid_types, FALSE);
521 	} else {
522 		bzero(&argop->nfs_argop4_u.opsetattr.stateid,
523 		    sizeof (stateid4));
524 	}
525 
526 	*error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
527 	if (*error)
528 		bzero(attr, sizeof (*attr));
529 }
530 
531 static void
nfs4args_setattr_free(nfs_argop4 * argop)532 nfs4args_setattr_free(nfs_argop4 *argop)
533 {
534 	nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
535 }
536 
537 static int
nfs4args_verify(nfs_argop4 * argop,vattr_t * vap,enum nfs_opnum4 op,bitmap4 supp)538 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
539     bitmap4 supp)
540 {
541 	fattr4 *attr;
542 	int error = 0;
543 
544 	argop->argop = op;
545 	switch (op) {
546 	case OP_VERIFY:
547 		attr = &argop->nfs_argop4_u.opverify.obj_attributes;
548 		break;
549 	case OP_NVERIFY:
550 		attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
551 		break;
552 	default:
553 		return (EINVAL);
554 	}
555 	if (!error)
556 		error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
557 	if (error)
558 		bzero(attr, sizeof (*attr));
559 	return (error);
560 }
561 
562 static void
nfs4args_verify_free(nfs_argop4 * argop)563 nfs4args_verify_free(nfs_argop4 *argop)
564 {
565 	switch (argop->argop) {
566 	case OP_VERIFY:
567 		nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
568 		break;
569 	case OP_NVERIFY:
570 		nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
571 		break;
572 	default:
573 		break;
574 	}
575 }
576 
577 static void
nfs4args_write(nfs_argop4 * argop,stable_how4 stable,rnode4_t * rp,cred_t * cr,WRITE4args ** wargs_pp,nfs4_stateid_types_t * sid_tp)578 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
579     WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
580 {
581 	WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
582 	mntinfo4_t *mi = VTOMI4(RTOV4(rp));
583 
584 	argop->argop = OP_WRITE;
585 	wargs->stable = stable;
586 	wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
587 	    mi, OP_WRITE, sid_tp);
588 	wargs->mblk = NULL;
589 	*wargs_pp = wargs;
590 }
591 
592 void
nfs4args_copen_free(OPEN4cargs * open_args)593 nfs4args_copen_free(OPEN4cargs *open_args)
594 {
595 	if (open_args->owner.owner_val) {
596 		kmem_free(open_args->owner.owner_val,
597 		    open_args->owner.owner_len);
598 	}
599 	if ((open_args->opentype == OPEN4_CREATE) &&
600 	    (open_args->mode != EXCLUSIVE4)) {
601 		nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
602 	}
603 }
604 
605 /*
606  * XXX:  This is referenced in modstubs.S
607  */
608 struct vnodeops *
nfs4_getvnodeops(void)609 nfs4_getvnodeops(void)
610 {
611 	return (nfs4_vnodeops);
612 }
613 
614 /*
615  * The OPEN operation opens a regular file.
616  */
617 /*ARGSUSED3*/
618 static int
nfs4_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)619 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
620 {
621 	vnode_t *dvp = NULL;
622 	rnode4_t *rp, *drp;
623 	int error;
624 	int just_been_created;
625 	char fn[MAXNAMELEN];
626 
627 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
628 	if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
629 		return (EIO);
630 	rp = VTOR4(*vpp);
631 
632 	/*
633 	 * Check to see if opening something besides a regular file;
634 	 * if so skip the OTW call
635 	 */
636 	if ((*vpp)->v_type != VREG) {
637 		error = nfs4_open_non_reg_file(vpp, flag, cr);
638 		return (error);
639 	}
640 
641 	/*
642 	 * XXX - would like a check right here to know if the file is
643 	 * executable or not, so as to skip OTW
644 	 */
645 
646 	if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
647 		return (error);
648 
649 	drp = VTOR4(dvp);
650 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
651 		return (EINTR);
652 
653 	if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
654 		nfs_rw_exit(&drp->r_rwlock);
655 		return (error);
656 	}
657 
658 	/*
659 	 * See if this file has just been CREATEd.
660 	 * If so, clear the flag and update the dnlc, which was previously
661 	 * skipped in nfs4_create.
662 	 * XXX need better serilization on this.
663 	 * XXX move this into the nf4open_otw call, after we have
664 	 * XXX acquired the open owner seqid sync.
665 	 */
666 	mutex_enter(&rp->r_statev4_lock);
667 	if (rp->created_v4) {
668 		rp->created_v4 = 0;
669 		mutex_exit(&rp->r_statev4_lock);
670 
671 		dnlc_update(dvp, fn, *vpp);
672 		/* This is needed so we don't bump the open ref count */
673 		just_been_created = 1;
674 	} else {
675 		mutex_exit(&rp->r_statev4_lock);
676 		just_been_created = 0;
677 	}
678 
679 	/*
680 	 * If caller specified O_TRUNC/FTRUNC, then be sure to set
681 	 * FWRITE (to drive successful setattr(size=0) after open)
682 	 */
683 	if (flag & FTRUNC)
684 		flag |= FWRITE;
685 
686 	error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
687 	    just_been_created);
688 
689 	if (!error && !((*vpp)->v_flag & VROOT))
690 		dnlc_update(dvp, fn, *vpp);
691 
692 	nfs_rw_exit(&drp->r_rwlock);
693 
694 	/* release the hold from vtodv */
695 	VN_RELE(dvp);
696 
697 	/* exchange the shadow for the master vnode, if needed */
698 
699 	if (error == 0 && IS_SHADOW(*vpp, rp))
700 		sv_exchange(vpp);
701 
702 	return (error);
703 }
704 
705 /*
706  * See if there's a "lost open" request to be saved and recovered.
707  */
708 static void
nfs4open_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,nfs4_open_owner_t * oop,cred_t * cr,vnode_t * vp,vnode_t * dvp,OPEN4cargs * open_args)709 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
710     nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
711     vnode_t *dvp, OPEN4cargs *open_args)
712 {
713 	vfs_t *vfsp;
714 	char *srccfp;
715 
716 	vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
717 
718 	if (error != ETIMEDOUT && error != EINTR &&
719 	    !NFS4_FRC_UNMT_ERR(error, vfsp)) {
720 		lost_rqstp->lr_op = 0;
721 		return;
722 	}
723 
724 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
725 	    "nfs4open_save_lost_rqst: error %d", error));
726 
727 	lost_rqstp->lr_op = OP_OPEN;
728 
729 	/*
730 	 * The vp (if it is not NULL) and dvp are held and rele'd via
731 	 * the recovery code.  See nfs4_save_lost_rqst.
732 	 */
733 	lost_rqstp->lr_vp = vp;
734 	lost_rqstp->lr_dvp = dvp;
735 	lost_rqstp->lr_oop = oop;
736 	lost_rqstp->lr_osp = NULL;
737 	lost_rqstp->lr_lop = NULL;
738 	lost_rqstp->lr_cr = cr;
739 	lost_rqstp->lr_flk = NULL;
740 	lost_rqstp->lr_oacc = open_args->share_access;
741 	lost_rqstp->lr_odeny = open_args->share_deny;
742 	lost_rqstp->lr_oclaim = open_args->claim;
743 	if (open_args->claim == CLAIM_DELEGATE_CUR) {
744 		lost_rqstp->lr_ostateid =
745 		    open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
746 		srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
747 	} else {
748 		srccfp = open_args->open_claim4_u.cfile;
749 	}
750 	lost_rqstp->lr_ofile.utf8string_len = 0;
751 	lost_rqstp->lr_ofile.utf8string_val = NULL;
752 	(void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
753 	lost_rqstp->lr_putfirst = FALSE;
754 }
755 
756 struct nfs4_excl_time {
757 	uint32 seconds;
758 	uint32 nseconds;
759 };
760 
761 /*
762  * The OPEN operation creates and/or opens a regular file
763  *
764  * ARGSUSED
765  */
766 static int
nfs4open_otw(vnode_t * dvp,char * file_name,struct vattr * in_va,vnode_t ** vpp,cred_t * cr,int create_flag,int open_flag,enum createmode4 createmode,int file_just_been_created)767 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
768     vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
769     enum createmode4 createmode, int file_just_been_created)
770 {
771 	rnode4_t *rp;
772 	rnode4_t *drp = VTOR4(dvp);
773 	vnode_t *vp = NULL;
774 	vnode_t *vpi = *vpp;
775 	bool_t needrecov = FALSE;
776 
777 	int doqueue = 1;
778 
779 	COMPOUND4args_clnt args;
780 	COMPOUND4res_clnt res;
781 	nfs_argop4 *argop;
782 	nfs_resop4 *resop;
783 	int argoplist_size;
784 	int idx_open, idx_fattr;
785 
786 	GETFH4res *gf_res = NULL;
787 	OPEN4res *op_res = NULL;
788 	nfs4_ga_res_t *garp;
789 	fattr4 *attr = NULL;
790 	struct nfs4_excl_time verf;
791 	bool_t did_excl_setup = FALSE;
792 	int created_osp;
793 
794 	OPEN4cargs *open_args;
795 	nfs4_open_owner_t	*oop = NULL;
796 	nfs4_open_stream_t	*osp = NULL;
797 	seqid4 seqid = 0;
798 	bool_t retry_open = FALSE;
799 	nfs4_recov_state_t recov_state;
800 	nfs4_lost_rqst_t lost_rqst;
801 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
802 	hrtime_t t;
803 	int acc = 0;
804 	cred_t *cred_otw = NULL;	/* cred used to do the RPC call */
805 	cred_t *ncr = NULL;
806 
807 	nfs4_sharedfh_t *otw_sfh;
808 	nfs4_sharedfh_t *orig_sfh;
809 	int fh_differs = 0;
810 	int numops, setgid_flag;
811 	int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
812 
813 	/*
814 	 * Make sure we properly deal with setting the right gid on
815 	 * a newly created file to reflect the parent's setgid bit
816 	 */
817 	setgid_flag = 0;
818 	if (create_flag && in_va) {
819 
820 		/*
821 		 * If there is grpid mount flag used or
822 		 * the parent's directory has the setgid bit set
823 		 * _and_ the client was able to get a valid mapping
824 		 * for the parent dir's owner_group, we want to
825 		 * append NVERIFY(owner_group == dva.va_gid) and
826 		 * SETATTR to the CREATE compound.
827 		 */
828 		mutex_enter(&drp->r_statelock);
829 		if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
830 		    drp->r_attr.va_mode & VSGID) &&
831 		    drp->r_attr.va_gid != GID_NOBODY) {
832 			in_va->va_mask |= AT_GID;
833 			in_va->va_gid = drp->r_attr.va_gid;
834 			setgid_flag = 1;
835 		}
836 		mutex_exit(&drp->r_statelock);
837 	}
838 
839 	/*
840 	 * Normal/non-create compound:
841 	 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
842 	 *
843 	 * Open(create) compound no setgid:
844 	 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
845 	 * RESTOREFH + GETATTR
846 	 *
847 	 * Open(create) setgid:
848 	 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
849 	 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
850 	 * NVERIFY(grp) + SETATTR
851 	 */
852 	if (setgid_flag) {
853 		numops = 10;
854 		idx_open = 1;
855 		idx_fattr = 3;
856 	} else if (create_flag) {
857 		numops = 7;
858 		idx_open = 2;
859 		idx_fattr = 4;
860 	} else {
861 		numops = 4;
862 		idx_open = 1;
863 		idx_fattr = 3;
864 	}
865 
866 	args.array_len = numops;
867 	argoplist_size = numops * sizeof (nfs_argop4);
868 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
869 
870 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
871 	    "open %s open flag 0x%x cred %p", file_name, open_flag,
872 	    (void *)cr));
873 
874 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
875 	if (create_flag) {
876 		/*
877 		 * We are to create a file.  Initialize the passed in vnode
878 		 * pointer.
879 		 */
880 		vpi = NULL;
881 	} else {
882 		/*
883 		 * Check to see if the client owns a read delegation and is
884 		 * trying to open for write.  If so, then return the delegation
885 		 * to avoid the server doing a cb_recall and returning DELAY.
886 		 * NB - we don't use the statev4_lock here because we'd have
887 		 * to drop the lock anyway and the result would be stale.
888 		 */
889 		if ((open_flag & FWRITE) &&
890 		    VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
891 			(void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
892 
893 		/*
894 		 * If the file has a delegation, then do an access check up
895 		 * front.  This avoids having to an access check later after
896 		 * we've already done start_op, which could deadlock.
897 		 */
898 		if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
899 			if (open_flag & FREAD &&
900 			    nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
901 				acc |= VREAD;
902 			if (open_flag & FWRITE &&
903 			    nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
904 				acc |= VWRITE;
905 		}
906 	}
907 
908 	drp = VTOR4(dvp);
909 
910 	recov_state.rs_flags = 0;
911 	recov_state.rs_num_retry_despite_err = 0;
912 	cred_otw = cr;
913 
914 recov_retry:
915 	fh_differs = 0;
916 	nfs4_error_zinit(&e);
917 
918 	e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
919 	if (e.error) {
920 		if (ncr != NULL)
921 			crfree(ncr);
922 		kmem_free(argop, argoplist_size);
923 		return (e.error);
924 	}
925 
926 	args.ctag = TAG_OPEN;
927 	args.array_len = numops;
928 	args.array = argop;
929 
930 	/* putfh directory fh */
931 	argop[0].argop = OP_CPUTFH;
932 	argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
933 
934 	/* OPEN: either op 1 or op 2 depending upon create/setgid flags */
935 	argop[idx_open].argop = OP_COPEN;
936 	open_args = &argop[idx_open].nfs_argop4_u.opcopen;
937 	open_args->claim = CLAIM_NULL;
938 
939 	/* name of file */
940 	open_args->open_claim4_u.cfile = file_name;
941 	open_args->owner.owner_len = 0;
942 	open_args->owner.owner_val = NULL;
943 
944 	if (create_flag) {
945 		/* CREATE a file */
946 		open_args->opentype = OPEN4_CREATE;
947 		open_args->mode = createmode;
948 		if (createmode == EXCLUSIVE4) {
949 			if (did_excl_setup == FALSE) {
950 				verf.seconds = zone_get_hostid(NULL);
951 				if (verf.seconds != 0)
952 					verf.nseconds = newnum();
953 				else {
954 					timestruc_t now;
955 
956 					gethrestime(&now);
957 					verf.seconds = now.tv_sec;
958 					verf.nseconds = now.tv_nsec;
959 				}
960 				/*
961 				 * Since the server will use this value for the
962 				 * mtime, make sure that it can't overflow. Zero
963 				 * out the MSB. The actual value does not matter
964 				 * here, only its uniqeness.
965 				 */
966 				verf.seconds &= INT32_MAX;
967 				did_excl_setup = TRUE;
968 			}
969 
970 			/* Now copy over verifier to OPEN4args. */
971 			open_args->createhow4_u.createverf = *(uint64_t *)&verf;
972 		} else {
973 			int v_error;
974 			bitmap4 supp_attrs;
975 			servinfo4_t *svp;
976 
977 			attr = &open_args->createhow4_u.createattrs;
978 
979 			svp = drp->r_server;
980 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
981 			supp_attrs = svp->sv_supp_attrs;
982 			nfs_rw_exit(&svp->sv_lock);
983 
984 			/* GUARDED4 or UNCHECKED4 */
985 			v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
986 			    supp_attrs);
987 			if (v_error) {
988 				bzero(attr, sizeof (*attr));
989 				nfs4args_copen_free(open_args);
990 				nfs4_end_op(VTOMI4(dvp), dvp, vpi,
991 				    &recov_state, FALSE);
992 				if (ncr != NULL)
993 					crfree(ncr);
994 				kmem_free(argop, argoplist_size);
995 				return (v_error);
996 			}
997 		}
998 	} else {
999 		/* NO CREATE */
1000 		open_args->opentype = OPEN4_NOCREATE;
1001 	}
1002 
1003 	if (recov_state.rs_sp != NULL) {
1004 		mutex_enter(&recov_state.rs_sp->s_lock);
1005 		open_args->owner.clientid = recov_state.rs_sp->clientid;
1006 		mutex_exit(&recov_state.rs_sp->s_lock);
1007 	} else {
1008 		/* XXX should we just fail here? */
1009 		open_args->owner.clientid = 0;
1010 	}
1011 
1012 	/*
1013 	 * This increments oop's ref count or creates a temporary 'just_created'
1014 	 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1015 	 * completes.
1016 	 */
1017 	mutex_enter(&VTOMI4(dvp)->mi_lock);
1018 
1019 	/* See if a permanent or just created open owner exists */
1020 	oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1021 	if (!oop) {
1022 		/*
1023 		 * This open owner does not exist so create a temporary
1024 		 * just created one.
1025 		 */
1026 		oop = create_open_owner(cr, VTOMI4(dvp));
1027 		ASSERT(oop != NULL);
1028 	}
1029 	mutex_exit(&VTOMI4(dvp)->mi_lock);
1030 
1031 	/* this length never changes, do alloc before seqid sync */
1032 	open_args->owner.owner_len = sizeof (oop->oo_name);
1033 	open_args->owner.owner_val =
1034 	    kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1035 
1036 	e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1037 	if (e.error == EAGAIN) {
1038 		open_owner_rele(oop);
1039 		nfs4args_copen_free(open_args);
1040 		nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1041 		if (ncr != NULL) {
1042 			crfree(ncr);
1043 			ncr = NULL;
1044 		}
1045 		goto recov_retry;
1046 	}
1047 
1048 	/* Check to see if we need to do the OTW call */
1049 	if (!create_flag) {
1050 		if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1051 		    file_just_been_created, &e.error, acc, &recov_state)) {
1052 
1053 			/*
1054 			 * The OTW open is not necessary.  Either
1055 			 * the open can succeed without it (eg.
1056 			 * delegation, error == 0) or the open
1057 			 * must fail due to an access failure
1058 			 * (error != 0).  In either case, tidy
1059 			 * up and return.
1060 			 */
1061 
1062 			nfs4_end_open_seqid_sync(oop);
1063 			open_owner_rele(oop);
1064 			nfs4args_copen_free(open_args);
1065 			nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1066 			if (ncr != NULL)
1067 				crfree(ncr);
1068 			kmem_free(argop, argoplist_size);
1069 			return (e.error);
1070 		}
1071 	}
1072 
1073 	bcopy(&oop->oo_name, open_args->owner.owner_val,
1074 	    open_args->owner.owner_len);
1075 
1076 	seqid = nfs4_get_open_seqid(oop) + 1;
1077 	open_args->seqid = seqid;
1078 	open_args->share_access = 0;
1079 	if (open_flag & FREAD)
1080 		open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1081 	if (open_flag & FWRITE)
1082 		open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1083 	open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1084 
1085 
1086 
1087 	/*
1088 	 * getfh w/sanity check for idx_open/idx_fattr
1089 	 */
1090 	ASSERT((idx_open + 1) == (idx_fattr - 1));
1091 	argop[idx_open + 1].argop = OP_GETFH;
1092 
1093 	/* getattr */
1094 	argop[idx_fattr].argop = OP_GETATTR;
1095 	argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1096 	argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1097 
1098 	if (setgid_flag) {
1099 		vattr_t	_v;
1100 		servinfo4_t *svp;
1101 		bitmap4	supp_attrs;
1102 
1103 		svp = drp->r_server;
1104 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1105 		supp_attrs = svp->sv_supp_attrs;
1106 		nfs_rw_exit(&svp->sv_lock);
1107 
1108 		/*
1109 		 * For setgid case, we need to:
1110 		 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1111 		 */
1112 		argop[4].argop = OP_SAVEFH;
1113 
1114 		argop[5].argop = OP_CPUTFH;
1115 		argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1116 
1117 		argop[6].argop = OP_GETATTR;
1118 		argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1119 		argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1120 
1121 		argop[7].argop = OP_RESTOREFH;
1122 
1123 		/*
1124 		 * nverify
1125 		 */
1126 		_v.va_mask = AT_GID;
1127 		_v.va_gid = in_va->va_gid;
1128 		if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1129 		    supp_attrs))) {
1130 
1131 			/*
1132 			 * setattr
1133 			 *
1134 			 * We _know_ we're not messing with AT_SIZE or
1135 			 * AT_XTIME, so no need for stateid or flags.
1136 			 * Also we specify NULL rp since we're only
1137 			 * interested in setting owner_group attributes.
1138 			 */
1139 			nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1140 			    supp_attrs, &e.error, 0);
1141 			if (e.error)
1142 				nfs4args_verify_free(&argop[8]);
1143 		}
1144 
1145 		if (e.error) {
1146 			/*
1147 			 * XXX - Revisit the last argument to nfs4_end_op()
1148 			 *	 once 5020486 is fixed.
1149 			 */
1150 			nfs4_end_open_seqid_sync(oop);
1151 			open_owner_rele(oop);
1152 			nfs4args_copen_free(open_args);
1153 			nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1154 			if (ncr != NULL)
1155 				crfree(ncr);
1156 			kmem_free(argop, argoplist_size);
1157 			return (e.error);
1158 		}
1159 	} else if (create_flag) {
1160 		argop[1].argop = OP_SAVEFH;
1161 
1162 		argop[5].argop = OP_RESTOREFH;
1163 
1164 		argop[6].argop = OP_GETATTR;
1165 		argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1166 		argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1167 	}
1168 
1169 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1170 	    "nfs4open_otw: %s call, nm %s, rp %s",
1171 	    needrecov ? "recov" : "first", file_name,
1172 	    rnode4info(VTOR4(dvp))));
1173 
1174 	t = gethrtime();
1175 
1176 	rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1177 
1178 	if (!e.error && nfs4_need_to_bump_seqid(&res))
1179 		nfs4_set_open_seqid(seqid, oop, args.ctag);
1180 
1181 	needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1182 
1183 	if (e.error || needrecov) {
1184 		bool_t abort = FALSE;
1185 
1186 		if (needrecov) {
1187 			nfs4_bseqid_entry_t *bsep = NULL;
1188 
1189 			nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1190 			    cred_otw, vpi, dvp, open_args);
1191 
1192 			if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1193 				bsep = nfs4_create_bseqid_entry(oop, NULL,
1194 				    vpi, 0, args.ctag, open_args->seqid);
1195 				num_bseqid_retry--;
1196 			}
1197 
1198 			abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1199 			    NULL, lost_rqst.lr_op == OP_OPEN ?
1200 			    &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1201 
1202 			if (bsep)
1203 				kmem_free(bsep, sizeof (*bsep));
1204 			/* give up if we keep getting BAD_SEQID */
1205 			if (num_bseqid_retry == 0)
1206 				abort = TRUE;
1207 			if (abort == TRUE && e.error == 0)
1208 				e.error = geterrno4(res.status);
1209 		}
1210 		nfs4_end_open_seqid_sync(oop);
1211 		open_owner_rele(oop);
1212 		nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1213 		nfs4args_copen_free(open_args);
1214 		if (setgid_flag) {
1215 			nfs4args_verify_free(&argop[8]);
1216 			nfs4args_setattr_free(&argop[9]);
1217 		}
1218 		if (!e.error)
1219 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1220 		if (ncr != NULL) {
1221 			crfree(ncr);
1222 			ncr = NULL;
1223 		}
1224 		if (!needrecov || abort == TRUE || e.error == EINTR ||
1225 		    NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1226 			kmem_free(argop, argoplist_size);
1227 			return (e.error);
1228 		}
1229 		goto recov_retry;
1230 	}
1231 
1232 	/*
1233 	 * Will check and update lease after checking the rflag for
1234 	 * OPEN_CONFIRM in the successful OPEN call.
1235 	 */
1236 	if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1237 
1238 		/*
1239 		 * XXX what if we're crossing mount points from server1:/drp
1240 		 * to server2:/drp/rp.
1241 		 */
1242 
1243 		/* Signal our end of use of the open seqid */
1244 		nfs4_end_open_seqid_sync(oop);
1245 
1246 		/*
1247 		 * This will destroy the open owner if it was just created,
1248 		 * and no one else has put a reference on it.
1249 		 */
1250 		open_owner_rele(oop);
1251 		if (create_flag && (createmode != EXCLUSIVE4) &&
1252 		    res.status == NFS4ERR_BADOWNER)
1253 			nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1254 
1255 		e.error = geterrno4(res.status);
1256 		nfs4args_copen_free(open_args);
1257 		if (setgid_flag) {
1258 			nfs4args_verify_free(&argop[8]);
1259 			nfs4args_setattr_free(&argop[9]);
1260 		}
1261 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1262 		nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1263 		/*
1264 		 * If the reply is NFS4ERR_ACCESS, it may be because
1265 		 * we are root (no root net access).  If the real uid
1266 		 * is not root, then retry with the real uid instead.
1267 		 */
1268 		if (ncr != NULL) {
1269 			crfree(ncr);
1270 			ncr = NULL;
1271 		}
1272 		if (res.status == NFS4ERR_ACCESS &&
1273 		    (ncr = crnetadjust(cred_otw)) != NULL) {
1274 			cred_otw = ncr;
1275 			goto recov_retry;
1276 		}
1277 		kmem_free(argop, argoplist_size);
1278 		return (e.error);
1279 	}
1280 
1281 	resop = &res.array[idx_open];  /* open res */
1282 	op_res = &resop->nfs_resop4_u.opopen;
1283 
1284 #ifdef DEBUG
1285 	/*
1286 	 * verify attrset bitmap
1287 	 */
1288 	if (create_flag &&
1289 	    (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1290 		/* make sure attrset returned is what we asked for */
1291 		/* XXX Ignore this 'error' for now */
1292 		if (attr->attrmask != op_res->attrset)
1293 			/* EMPTY */;
1294 	}
1295 #endif
1296 
1297 	if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1298 		mutex_enter(&VTOMI4(dvp)->mi_lock);
1299 		VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1300 		mutex_exit(&VTOMI4(dvp)->mi_lock);
1301 	}
1302 
1303 	resop = &res.array[idx_open + 1];  /* getfh res */
1304 	gf_res = &resop->nfs_resop4_u.opgetfh;
1305 
1306 	otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1307 
1308 	/*
1309 	 * The open stateid has been updated on the server but not
1310 	 * on the client yet.  There is a path: makenfs4node->nfs4_attr_cache->
1311 	 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1312 	 * WRITE call.  That, however, will use the old stateid, so go ahead
1313 	 * and upate the open stateid now, before any call to makenfs4node.
1314 	 */
1315 	if (vpi) {
1316 		nfs4_open_stream_t	*tmp_osp;
1317 		rnode4_t		*tmp_rp = VTOR4(vpi);
1318 
1319 		tmp_osp = find_open_stream(oop, tmp_rp);
1320 		if (tmp_osp) {
1321 			tmp_osp->open_stateid = op_res->stateid;
1322 			mutex_exit(&tmp_osp->os_sync_lock);
1323 			open_stream_rele(tmp_osp, tmp_rp);
1324 		}
1325 
1326 		/*
1327 		 * We must determine if the file handle given by the otw open
1328 		 * is the same as the file handle which was passed in with
1329 		 * *vpp.  This case can be reached if the file we are trying
1330 		 * to open has been removed and another file has been created
1331 		 * having the same file name.  The passed in vnode is released
1332 		 * later.
1333 		 */
1334 		orig_sfh = VTOR4(vpi)->r_fh;
1335 		fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1336 	}
1337 
1338 	garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1339 
1340 	if (create_flag || fh_differs) {
1341 		int rnode_err = 0;
1342 
1343 		vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1344 		    dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1345 
1346 		if (e.error)
1347 			PURGE_ATTRCACHE4(vp);
1348 		/*
1349 		 * For the newly created vp case, make sure the rnode
1350 		 * isn't bad before using it.
1351 		 */
1352 		mutex_enter(&(VTOR4(vp))->r_statelock);
1353 		if (VTOR4(vp)->r_flags & R4RECOVERR)
1354 			rnode_err = EIO;
1355 		mutex_exit(&(VTOR4(vp))->r_statelock);
1356 
1357 		if (rnode_err) {
1358 			nfs4_end_open_seqid_sync(oop);
1359 			nfs4args_copen_free(open_args);
1360 			if (setgid_flag) {
1361 				nfs4args_verify_free(&argop[8]);
1362 				nfs4args_setattr_free(&argop[9]);
1363 			}
1364 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1365 			nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1366 			    needrecov);
1367 			open_owner_rele(oop);
1368 			VN_RELE(vp);
1369 			if (ncr != NULL)
1370 				crfree(ncr);
1371 			sfh4_rele(&otw_sfh);
1372 			kmem_free(argop, argoplist_size);
1373 			return (EIO);
1374 		}
1375 	} else {
1376 		vp = vpi;
1377 	}
1378 	sfh4_rele(&otw_sfh);
1379 
1380 	/*
1381 	 * It seems odd to get a full set of attrs and then not update
1382 	 * the object's attrcache in the non-create case.  Create case uses
1383 	 * the attrs since makenfs4node checks to see if the attrs need to
1384 	 * be updated (and then updates them).  The non-create case should
1385 	 * update attrs also.
1386 	 */
1387 	if (! create_flag && ! fh_differs && !e.error) {
1388 		nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1389 	}
1390 
1391 	nfs4_error_zinit(&e);
1392 	if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1393 		/* This does not do recovery for vp explicitly. */
1394 		nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1395 		    &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1396 
1397 		if (e.error || e.stat) {
1398 			nfs4_end_open_seqid_sync(oop);
1399 			nfs4args_copen_free(open_args);
1400 			if (setgid_flag) {
1401 				nfs4args_verify_free(&argop[8]);
1402 				nfs4args_setattr_free(&argop[9]);
1403 			}
1404 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1405 			nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1406 			    needrecov);
1407 			open_owner_rele(oop);
1408 			if (create_flag || fh_differs) {
1409 				/* rele the makenfs4node */
1410 				VN_RELE(vp);
1411 			}
1412 			if (ncr != NULL) {
1413 				crfree(ncr);
1414 				ncr = NULL;
1415 			}
1416 			if (retry_open == TRUE) {
1417 				NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1418 				    "nfs4open_otw: retry the open since OPEN "
1419 				    "CONFIRM failed with error %d stat %d",
1420 				    e.error, e.stat));
1421 				if (create_flag && createmode == GUARDED4) {
1422 					NFS4_DEBUG(nfs4_client_recov_debug,
1423 					    (CE_NOTE, "nfs4open_otw: switch "
1424 					    "createmode from GUARDED4 to "
1425 					    "UNCHECKED4"));
1426 					createmode = UNCHECKED4;
1427 				}
1428 				goto recov_retry;
1429 			}
1430 			if (!e.error) {
1431 				if (create_flag && (createmode != EXCLUSIVE4) &&
1432 				    e.stat == NFS4ERR_BADOWNER)
1433 					nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1434 
1435 				e.error = geterrno4(e.stat);
1436 			}
1437 			kmem_free(argop, argoplist_size);
1438 			return (e.error);
1439 		}
1440 	}
1441 
1442 	rp = VTOR4(vp);
1443 
1444 	mutex_enter(&rp->r_statev4_lock);
1445 	if (create_flag)
1446 		rp->created_v4 = 1;
1447 	mutex_exit(&rp->r_statev4_lock);
1448 
1449 	mutex_enter(&oop->oo_lock);
1450 	/* Doesn't matter if 'oo_just_created' already was set as this */
1451 	oop->oo_just_created = NFS4_PERM_CREATED;
1452 	if (oop->oo_cred_otw)
1453 		crfree(oop->oo_cred_otw);
1454 	oop->oo_cred_otw = cred_otw;
1455 	crhold(oop->oo_cred_otw);
1456 	mutex_exit(&oop->oo_lock);
1457 
1458 	/* returns with 'os_sync_lock' held */
1459 	osp = find_or_create_open_stream(oop, rp, &created_osp);
1460 	if (!osp) {
1461 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1462 		    "nfs4open_otw: failed to create an open stream"));
1463 		NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1464 		    "signal our end of use of the open seqid"));
1465 
1466 		nfs4_end_open_seqid_sync(oop);
1467 		open_owner_rele(oop);
1468 		nfs4args_copen_free(open_args);
1469 		if (setgid_flag) {
1470 			nfs4args_verify_free(&argop[8]);
1471 			nfs4args_setattr_free(&argop[9]);
1472 		}
1473 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1474 		nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1475 		if (create_flag || fh_differs)
1476 			VN_RELE(vp);
1477 		if (ncr != NULL)
1478 			crfree(ncr);
1479 
1480 		kmem_free(argop, argoplist_size);
1481 		return (EINVAL);
1482 
1483 	}
1484 
1485 	osp->open_stateid = op_res->stateid;
1486 
1487 	if (open_flag & FREAD)
1488 		osp->os_share_acc_read++;
1489 	if (open_flag & FWRITE)
1490 		osp->os_share_acc_write++;
1491 	osp->os_share_deny_none++;
1492 
1493 	/*
1494 	 * Need to reset this bitfield for the possible case where we were
1495 	 * going to OTW CLOSE the file, got a non-recoverable error, and before
1496 	 * we could retry the CLOSE, OPENed the file again.
1497 	 */
1498 	ASSERT(osp->os_open_owner->oo_seqid_inuse);
1499 	osp->os_final_close = 0;
1500 	osp->os_force_close = 0;
1501 #ifdef DEBUG
1502 	if (osp->os_failed_reopen)
1503 		NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1504 		    " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1505 		    (void *)osp, (void *)cr, rnode4info(rp)));
1506 #endif
1507 	osp->os_failed_reopen = 0;
1508 
1509 	mutex_exit(&osp->os_sync_lock);
1510 
1511 	nfs4_end_open_seqid_sync(oop);
1512 
1513 	if (created_osp && recov_state.rs_sp != NULL) {
1514 		mutex_enter(&recov_state.rs_sp->s_lock);
1515 		nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1516 		mutex_exit(&recov_state.rs_sp->s_lock);
1517 	}
1518 
1519 	/* get rid of our reference to find oop */
1520 	open_owner_rele(oop);
1521 
1522 	open_stream_rele(osp, rp);
1523 
1524 	/* accept delegation, if any */
1525 	nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1526 
1527 	nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1528 
1529 	if (createmode == EXCLUSIVE4 &&
1530 	    (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1531 		NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1532 		    " EXCLUSIVE4: sending a SETATTR"));
1533 		/*
1534 		 * If doing an exclusive create, then generate
1535 		 * a SETATTR to set the initial attributes.
1536 		 * Try to set the mtime and the atime to the
1537 		 * server's current time.  It is somewhat
1538 		 * expected that these fields will be used to
1539 		 * store the exclusive create cookie.  If not,
1540 		 * server implementors will need to know that
1541 		 * a SETATTR will follow an exclusive create
1542 		 * and the cookie should be destroyed if
1543 		 * appropriate.
1544 		 *
1545 		 * The AT_GID and AT_SIZE bits are turned off
1546 		 * so that the SETATTR request will not attempt
1547 		 * to process these.  The gid will be set
1548 		 * separately if appropriate.  The size is turned
1549 		 * off because it is assumed that a new file will
1550 		 * be created empty and if the file wasn't empty,
1551 		 * then the exclusive create will have failed
1552 		 * because the file must have existed already.
1553 		 * Therefore, no truncate operation is needed.
1554 		 */
1555 		in_va->va_mask &= ~(AT_GID | AT_SIZE);
1556 		in_va->va_mask |= (AT_MTIME | AT_ATIME);
1557 
1558 		e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1559 		if (e.error) {
1560 			nfs4_error_t err;
1561 
1562 			/*
1563 			 * Couldn't correct the attributes of
1564 			 * the newly created file and the
1565 			 * attributes are wrong.  Remove the
1566 			 * file and return an error to the
1567 			 * application.
1568 			 */
1569 			/* XXX will this take care of client state ? */
1570 			NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1571 			    "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1572 			    " remove file", e.error));
1573 
1574 			/*
1575 			 * The file is currently open so try to close it first.
1576 			 *
1577 			 * If we do not close the file explicitly here then the
1578 			 * VN_RELE() would do an (implicit and asynchronous)
1579 			 * close for us.  But such async close could race with
1580 			 * the nfs4_remove() below.  If the async close is
1581 			 * slower than nfs4_remove() then nfs4_remove()
1582 			 * wouldn't remove the file but rename it to .nfsXXXX
1583 			 * instead.
1584 			 */
1585 			nfs4close_one(vp, NULL, cr, open_flag, NULL, &err,
1586 			    CLOSE_NORM, 0, 0, 0);
1587 			VN_RELE(vp);
1588 			(void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1589 
1590 			/*
1591 			 * Since we've reled the vnode and removed
1592 			 * the file we now need to return the error.
1593 			 * At this point we don't want to update the
1594 			 * dircaches, call nfs4_waitfor_purge_complete
1595 			 * or set vpp to vp so we need to skip these
1596 			 * as well.
1597 			 */
1598 			goto skip_update_dircaches;
1599 		}
1600 	}
1601 
1602 	/*
1603 	 * If we created or found the correct vnode, due to create_flag or
1604 	 * fh_differs being set, then update directory cache attribute, readdir
1605 	 * and dnlc caches.
1606 	 */
1607 	if (create_flag || fh_differs) {
1608 		dirattr_info_t dinfo, *dinfop;
1609 
1610 		/*
1611 		 * Make sure getattr succeeded before using results.
1612 		 * note: op 7 is getattr(dir) for both flavors of
1613 		 * open(create).
1614 		 */
1615 		if (create_flag && res.status == NFS4_OK) {
1616 			dinfo.di_time_call = t;
1617 			dinfo.di_cred = cr;
1618 			dinfo.di_garp =
1619 			    &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1620 			dinfop = &dinfo;
1621 		} else {
1622 			dinfop = NULL;
1623 		}
1624 
1625 		nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1626 		    dinfop);
1627 	}
1628 
1629 	/*
1630 	 * If the page cache for this file was flushed from actions
1631 	 * above, it was done asynchronously and if that is true,
1632 	 * there is a need to wait here for it to complete.  This must
1633 	 * be done outside of start_fop/end_fop.
1634 	 */
1635 	(void) nfs4_waitfor_purge_complete(vp);
1636 
1637 	/*
1638 	 * It is implicit that we are in the open case (create_flag == 0) since
1639 	 * fh_differs can only be set to a non-zero value in the open case.
1640 	 */
1641 	if (fh_differs != 0 && vpi != NULL)
1642 		VN_RELE(vpi);
1643 
1644 	/*
1645 	 * Be sure to set *vpp to the correct value before returning.
1646 	 */
1647 	*vpp = vp;
1648 
1649 skip_update_dircaches:
1650 
1651 	nfs4args_copen_free(open_args);
1652 	if (setgid_flag) {
1653 		nfs4args_verify_free(&argop[8]);
1654 		nfs4args_setattr_free(&argop[9]);
1655 	}
1656 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1657 
1658 	if (ncr)
1659 		crfree(ncr);
1660 	kmem_free(argop, argoplist_size);
1661 	return (e.error);
1662 }
1663 
1664 /*
1665  * Reopen an open instance.  cf. nfs4open_otw().
1666  *
1667  * Errors are returned by the nfs4_error_t parameter.
1668  * - ep->error contains an errno value or zero.
1669  * - if it is zero, ep->stat is set to an NFS status code, if any.
1670  *   If the file could not be reopened, but the caller should continue, the
1671  *   file is marked dead and no error values are returned.  If the caller
1672  *   should stop recovering open files and start over, either the ep->error
1673  *   value or ep->stat will indicate an error (either something that requires
1674  *   recovery or EAGAIN).  Note that some recovery (e.g., expired volatile
1675  *   filehandles) may be handled silently by this routine.
1676  * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1677  *   will be started, so the caller should not do it.
1678  *
1679  * Gotos:
1680  * - kill_file : reopen failed in such a fashion to constitute marking the
1681  *    file dead and setting the open stream's 'os_failed_reopen' as 1.  This
1682  *   is for cases where recovery is not possible.
1683  * - failed_reopen : same as above, except that the file has already been
1684  *   marked dead, so no need to do it again.
1685  * - bailout : reopen failed but we are able to recover and retry the reopen -
1686  *   either within this function immediately or via the calling function.
1687  */
1688 
1689 void
nfs4_reopen(vnode_t * vp,nfs4_open_stream_t * osp,nfs4_error_t * ep,open_claim_type4 claim,bool_t frc_use_claim_previous,bool_t is_recov)1690 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1691     open_claim_type4 claim, bool_t frc_use_claim_previous,
1692     bool_t is_recov)
1693 {
1694 	COMPOUND4args_clnt args;
1695 	COMPOUND4res_clnt res;
1696 	nfs_argop4 argop[4];
1697 	nfs_resop4 *resop;
1698 	OPEN4res *op_res = NULL;
1699 	OPEN4cargs *open_args;
1700 	GETFH4res *gf_res;
1701 	rnode4_t *rp = VTOR4(vp);
1702 	int doqueue = 1;
1703 	cred_t *cr = NULL, *cred_otw = NULL;
1704 	nfs4_open_owner_t *oop = NULL;
1705 	seqid4 seqid;
1706 	nfs4_ga_res_t *garp;
1707 	char fn[MAXNAMELEN];
1708 	nfs4_recov_state_t recov = {NULL, 0};
1709 	nfs4_lost_rqst_t lost_rqst;
1710 	mntinfo4_t *mi = VTOMI4(vp);
1711 	bool_t abort;
1712 	char *failed_msg = "";
1713 	int fh_different;
1714 	hrtime_t t;
1715 	nfs4_bseqid_entry_t *bsep = NULL;
1716 
1717 	ASSERT(nfs4_consistent_type(vp));
1718 	ASSERT(nfs_zone() == mi->mi_zone);
1719 
1720 	nfs4_error_zinit(ep);
1721 
1722 	/* this is the cred used to find the open owner */
1723 	cr = state_to_cred(osp);
1724 	if (cr == NULL) {
1725 		failed_msg = "Couldn't reopen: no cred";
1726 		goto kill_file;
1727 	}
1728 	/* use this cred for OTW operations */
1729 	cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1730 
1731 top:
1732 	nfs4_error_zinit(ep);
1733 
1734 	if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1735 		/* File system has been unmounted, quit */
1736 		ep->error = EIO;
1737 		failed_msg = "Couldn't reopen: file system has been unmounted";
1738 		goto kill_file;
1739 	}
1740 
1741 	oop = osp->os_open_owner;
1742 
1743 	ASSERT(oop != NULL);
1744 	if (oop == NULL) {	/* be defensive in non-DEBUG */
1745 		failed_msg = "can't reopen: no open owner";
1746 		goto kill_file;
1747 	}
1748 	open_owner_hold(oop);
1749 
1750 	ep->error = nfs4_start_open_seqid_sync(oop, mi);
1751 	if (ep->error) {
1752 		open_owner_rele(oop);
1753 		oop = NULL;
1754 		goto bailout;
1755 	}
1756 
1757 	/*
1758 	 * If the rnode has a delegation and the delegation has been
1759 	 * recovered and the server didn't request a recall and the caller
1760 	 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1761 	 * recovery) and the rnode hasn't been marked dead, then install
1762 	 * the delegation stateid in the open stream.  Otherwise, proceed
1763 	 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1764 	 */
1765 	mutex_enter(&rp->r_statev4_lock);
1766 	if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1767 	    !rp->r_deleg_return_pending &&
1768 	    (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1769 	    !rp->r_deleg_needs_recall &&
1770 	    claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1771 	    !(rp->r_flags & R4RECOVERR)) {
1772 		mutex_enter(&osp->os_sync_lock);
1773 		osp->os_delegation = 1;
1774 		osp->open_stateid = rp->r_deleg_stateid;
1775 		mutex_exit(&osp->os_sync_lock);
1776 		mutex_exit(&rp->r_statev4_lock);
1777 		goto bailout;
1778 	}
1779 	mutex_exit(&rp->r_statev4_lock);
1780 
1781 	/*
1782 	 * If the file failed recovery, just quit.  This failure need not
1783 	 * affect other reopens, so don't return an error.
1784 	 */
1785 	mutex_enter(&rp->r_statelock);
1786 	if (rp->r_flags & R4RECOVERR) {
1787 		mutex_exit(&rp->r_statelock);
1788 		ep->error = 0;
1789 		goto failed_reopen;
1790 	}
1791 	mutex_exit(&rp->r_statelock);
1792 
1793 	/*
1794 	 * argop is empty here
1795 	 *
1796 	 * PUTFH, OPEN, GETATTR
1797 	 */
1798 	args.ctag = TAG_REOPEN;
1799 	args.array_len = 4;
1800 	args.array = argop;
1801 
1802 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1803 	    "nfs4_reopen: file is type %d, id %s",
1804 	    vp->v_type, rnode4info(VTOR4(vp))));
1805 
1806 	argop[0].argop = OP_CPUTFH;
1807 
1808 	if (claim != CLAIM_PREVIOUS) {
1809 		/*
1810 		 * if this is a file mount then
1811 		 * use the mntinfo parentfh
1812 		 */
1813 		argop[0].nfs_argop4_u.opcputfh.sfh =
1814 		    (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1815 		    VTOSV(vp)->sv_dfh;
1816 	} else {
1817 		/* putfh fh to reopen */
1818 		argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1819 	}
1820 
1821 	argop[1].argop = OP_COPEN;
1822 	open_args = &argop[1].nfs_argop4_u.opcopen;
1823 	open_args->claim = claim;
1824 
1825 	if (claim == CLAIM_NULL) {
1826 
1827 		if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1828 			nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1829 			    "failed for vp 0x%p for CLAIM_NULL with %m",
1830 			    (void *)vp);
1831 			failed_msg = "Couldn't reopen: vtoname failed for "
1832 			    "CLAIM_NULL";
1833 			/* nothing allocated yet */
1834 			goto kill_file;
1835 		}
1836 
1837 		open_args->open_claim4_u.cfile = fn;
1838 	} else if (claim == CLAIM_PREVIOUS) {
1839 
1840 		/*
1841 		 * We have two cases to deal with here:
1842 		 * 1) We're being called to reopen files in order to satisfy
1843 		 *    a lock operation request which requires us to explicitly
1844 		 *    reopen files which were opened under a delegation.  If
1845 		 *    we're in recovery, we *must* use CLAIM_PREVIOUS.  In
1846 		 *    that case, frc_use_claim_previous is TRUE and we must
1847 		 *    use the rnode's current delegation type (r_deleg_type).
1848 		 * 2) We're reopening files during some form of recovery.
1849 		 *    In this case, frc_use_claim_previous is FALSE and we
1850 		 *    use the delegation type appropriate for recovery
1851 		 *    (r_deleg_needs_recovery).
1852 		 */
1853 		mutex_enter(&rp->r_statev4_lock);
1854 		open_args->open_claim4_u.delegate_type =
1855 		    frc_use_claim_previous ?
1856 		    rp->r_deleg_type :
1857 		    rp->r_deleg_needs_recovery;
1858 		mutex_exit(&rp->r_statev4_lock);
1859 
1860 	} else if (claim == CLAIM_DELEGATE_CUR) {
1861 
1862 		if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1863 			nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1864 			    "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1865 			    "with %m", (void *)vp);
1866 			failed_msg = "Couldn't reopen: vtoname failed for "
1867 			    "CLAIM_DELEGATE_CUR";
1868 			/* nothing allocated yet */
1869 			goto kill_file;
1870 		}
1871 
1872 		mutex_enter(&rp->r_statev4_lock);
1873 		open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1874 		    rp->r_deleg_stateid;
1875 		mutex_exit(&rp->r_statev4_lock);
1876 
1877 		open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1878 	}
1879 	open_args->opentype = OPEN4_NOCREATE;
1880 	open_args->owner.clientid = mi2clientid(mi);
1881 	open_args->owner.owner_len = sizeof (oop->oo_name);
1882 	open_args->owner.owner_val =
1883 	    kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1884 	bcopy(&oop->oo_name, open_args->owner.owner_val,
1885 	    open_args->owner.owner_len);
1886 	open_args->share_access = 0;
1887 	open_args->share_deny = 0;
1888 
1889 	mutex_enter(&osp->os_sync_lock);
1890 	NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1891 	    "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1892 	    "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1893 	    (void *)osp, (void *)rp, osp->os_share_acc_read,
1894 	    osp->os_share_acc_write, osp->os_open_ref_count,
1895 	    osp->os_mmap_read, osp->os_mmap_write, claim));
1896 
1897 	if (osp->os_share_acc_read || osp->os_mmap_read)
1898 		open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1899 	if (osp->os_share_acc_write || osp->os_mmap_write)
1900 		open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1901 	if (osp->os_share_deny_read)
1902 		open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1903 	if (osp->os_share_deny_write)
1904 		open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1905 	mutex_exit(&osp->os_sync_lock);
1906 
1907 	seqid = nfs4_get_open_seqid(oop) + 1;
1908 	open_args->seqid = seqid;
1909 
1910 	/* Construct the getfh part of the compound */
1911 	argop[2].argop = OP_GETFH;
1912 
1913 	/* Construct the getattr part of the compound */
1914 	argop[3].argop = OP_GETATTR;
1915 	argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1916 	argop[3].nfs_argop4_u.opgetattr.mi = mi;
1917 
1918 	t = gethrtime();
1919 
1920 	rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1921 
1922 	if (ep->error) {
1923 		if (!is_recov && !frc_use_claim_previous &&
1924 		    (ep->error == EINTR || ep->error == ETIMEDOUT ||
1925 		    NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1926 			nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1927 			    cred_otw, vp, NULL, open_args);
1928 			abort = nfs4_start_recovery(ep,
1929 			    VTOMI4(vp), vp, NULL, NULL,
1930 			    lost_rqst.lr_op == OP_OPEN ?
1931 			    &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1932 			nfs4args_copen_free(open_args);
1933 			goto bailout;
1934 		}
1935 
1936 		nfs4args_copen_free(open_args);
1937 
1938 		if (ep->error == EACCES && cred_otw != cr) {
1939 			crfree(cred_otw);
1940 			cred_otw = cr;
1941 			crhold(cred_otw);
1942 			nfs4_end_open_seqid_sync(oop);
1943 			open_owner_rele(oop);
1944 			oop = NULL;
1945 			goto top;
1946 		}
1947 		if (ep->error == ETIMEDOUT)
1948 			goto bailout;
1949 		failed_msg = "Couldn't reopen: rpc error";
1950 		goto kill_file;
1951 	}
1952 
1953 	if (nfs4_need_to_bump_seqid(&res))
1954 		nfs4_set_open_seqid(seqid, oop, args.ctag);
1955 
1956 	switch (res.status) {
1957 	case NFS4_OK:
1958 		if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1959 			mutex_enter(&rp->r_statelock);
1960 			rp->r_delay_interval = 0;
1961 			mutex_exit(&rp->r_statelock);
1962 		}
1963 		break;
1964 	case NFS4ERR_BAD_SEQID:
1965 		bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1966 		    args.ctag, open_args->seqid);
1967 
1968 		abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1969 		    NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1970 		    NULL, OP_OPEN, bsep, NULL, NULL);
1971 
1972 		nfs4args_copen_free(open_args);
1973 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1974 		nfs4_end_open_seqid_sync(oop);
1975 		open_owner_rele(oop);
1976 		oop = NULL;
1977 		kmem_free(bsep, sizeof (*bsep));
1978 
1979 		goto kill_file;
1980 	case NFS4ERR_NO_GRACE:
1981 		nfs4args_copen_free(open_args);
1982 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1983 		nfs4_end_open_seqid_sync(oop);
1984 		open_owner_rele(oop);
1985 		oop = NULL;
1986 		if (claim == CLAIM_PREVIOUS) {
1987 			/*
1988 			 * Retry as a plain open. We don't need to worry about
1989 			 * checking the changeinfo: it is acceptable for a
1990 			 * client to re-open a file and continue processing
1991 			 * (in the absence of locks).
1992 			 */
1993 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1994 			    "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1995 			    "will retry as CLAIM_NULL"));
1996 			claim = CLAIM_NULL;
1997 			nfs4_mi_kstat_inc_no_grace(mi);
1998 			goto top;
1999 		}
2000 		failed_msg =
2001 		    "Couldn't reopen: tried reclaim outside grace period. ";
2002 		goto kill_file;
2003 	case NFS4ERR_GRACE:
2004 		nfs4_set_grace_wait(mi);
2005 		nfs4args_copen_free(open_args);
2006 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2007 		nfs4_end_open_seqid_sync(oop);
2008 		open_owner_rele(oop);
2009 		oop = NULL;
2010 		ep->error = nfs4_wait_for_grace(mi, &recov);
2011 		if (ep->error != 0)
2012 			goto bailout;
2013 		goto top;
2014 	case NFS4ERR_DELAY:
2015 		nfs4_set_delay_wait(vp);
2016 		nfs4args_copen_free(open_args);
2017 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2018 		nfs4_end_open_seqid_sync(oop);
2019 		open_owner_rele(oop);
2020 		oop = NULL;
2021 		ep->error = nfs4_wait_for_delay(vp, &recov);
2022 		nfs4_mi_kstat_inc_delay(mi);
2023 		if (ep->error != 0)
2024 			goto bailout;
2025 		goto top;
2026 	case NFS4ERR_FHEXPIRED:
2027 		/* recover filehandle and retry */
2028 		abort = nfs4_start_recovery(ep,
2029 		    mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2030 		nfs4args_copen_free(open_args);
2031 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2032 		nfs4_end_open_seqid_sync(oop);
2033 		open_owner_rele(oop);
2034 		oop = NULL;
2035 		if (abort == FALSE)
2036 			goto top;
2037 		failed_msg = "Couldn't reopen: recovery aborted";
2038 		goto kill_file;
2039 	case NFS4ERR_RESOURCE:
2040 	case NFS4ERR_STALE_CLIENTID:
2041 	case NFS4ERR_WRONGSEC:
2042 	case NFS4ERR_EXPIRED:
2043 		/*
2044 		 * Do not mark the file dead and let the calling
2045 		 * function initiate recovery.
2046 		 */
2047 		nfs4args_copen_free(open_args);
2048 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2049 		nfs4_end_open_seqid_sync(oop);
2050 		open_owner_rele(oop);
2051 		oop = NULL;
2052 		goto bailout;
2053 	case NFS4ERR_ACCESS:
2054 		if (cred_otw != cr) {
2055 			crfree(cred_otw);
2056 			cred_otw = cr;
2057 			crhold(cred_otw);
2058 			nfs4args_copen_free(open_args);
2059 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2060 			nfs4_end_open_seqid_sync(oop);
2061 			open_owner_rele(oop);
2062 			oop = NULL;
2063 			goto top;
2064 		}
2065 		/* fall through */
2066 	default:
2067 		NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2068 		    "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2069 		    (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2070 		    rnode4info(VTOR4(vp))));
2071 		failed_msg = "Couldn't reopen: NFSv4 error";
2072 		nfs4args_copen_free(open_args);
2073 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2074 		goto kill_file;
2075 	}
2076 
2077 	resop = &res.array[1];  /* open res */
2078 	op_res = &resop->nfs_resop4_u.opopen;
2079 
2080 	garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2081 
2082 	/*
2083 	 * Check if the path we reopened really is the same
2084 	 * file. We could end up in a situation where the file
2085 	 * was removed and a new file created with the same name.
2086 	 */
2087 	resop = &res.array[2];
2088 	gf_res = &resop->nfs_resop4_u.opgetfh;
2089 	(void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2090 	fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2091 	if (fh_different) {
2092 		if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2093 		    mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2094 			/* Oops, we don't have the same file */
2095 			if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2096 				failed_msg = "Couldn't reopen: Persistent "
2097 				    "file handle changed";
2098 			else
2099 				failed_msg = "Couldn't reopen: Volatile "
2100 				    "(no expire on open) file handle changed";
2101 
2102 			nfs4args_copen_free(open_args);
2103 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2104 			nfs_rw_exit(&mi->mi_fh_lock);
2105 			goto kill_file;
2106 
2107 		} else {
2108 			/*
2109 			 * We have volatile file handles that don't compare.
2110 			 * If the fids are the same then we assume that the
2111 			 * file handle expired but the rnode still refers to
2112 			 * the same file object.
2113 			 *
2114 			 * First check that we have fids or not.
2115 			 * If we don't we have a dumb server so we will
2116 			 * just assume every thing is ok for now.
2117 			 */
2118 			if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2119 			    rp->r_attr.va_mask & AT_NODEID &&
2120 			    rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2121 				/*
2122 				 * We have fids, but they don't
2123 				 * compare. So kill the file.
2124 				 */
2125 				failed_msg =
2126 				    "Couldn't reopen: file handle changed"
2127 				    " due to mismatched fids";
2128 				nfs4args_copen_free(open_args);
2129 				xdr_free(xdr_COMPOUND4res_clnt,
2130 				    (caddr_t)&res);
2131 				nfs_rw_exit(&mi->mi_fh_lock);
2132 				goto kill_file;
2133 			} else {
2134 				/*
2135 				 * We have volatile file handles that refers
2136 				 * to the same file (at least they have the
2137 				 * same fid) or we don't have fids so we
2138 				 * can't tell. :(. We'll be a kind and accepting
2139 				 * client so we'll update the rnode's file
2140 				 * handle with the otw handle.
2141 				 *
2142 				 * We need to drop mi->mi_fh_lock since
2143 				 * sh4_update acquires it. Since there is
2144 				 * only one recovery thread there is no
2145 				 * race.
2146 				 */
2147 				nfs_rw_exit(&mi->mi_fh_lock);
2148 				sfh4_update(rp->r_fh, &gf_res->object);
2149 			}
2150 		}
2151 	} else {
2152 		nfs_rw_exit(&mi->mi_fh_lock);
2153 	}
2154 
2155 	ASSERT(nfs4_consistent_type(vp));
2156 
2157 	/*
2158 	 * If the server wanted an OPEN_CONFIRM but that fails, just start
2159 	 * over.  Presumably if there is a persistent error it will show up
2160 	 * when we resend the OPEN.
2161 	 */
2162 	if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2163 		bool_t retry_open = FALSE;
2164 
2165 		nfs4open_confirm(vp, &seqid, &op_res->stateid,
2166 		    cred_otw, is_recov, &retry_open,
2167 		    oop, FALSE, ep, NULL);
2168 		if (ep->error || ep->stat) {
2169 			nfs4args_copen_free(open_args);
2170 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2171 			nfs4_end_open_seqid_sync(oop);
2172 			open_owner_rele(oop);
2173 			oop = NULL;
2174 			goto top;
2175 		}
2176 	}
2177 
2178 	mutex_enter(&osp->os_sync_lock);
2179 	osp->open_stateid = op_res->stateid;
2180 	osp->os_delegation = 0;
2181 	/*
2182 	 * Need to reset this bitfield for the possible case where we were
2183 	 * going to OTW CLOSE the file, got a non-recoverable error, and before
2184 	 * we could retry the CLOSE, OPENed the file again.
2185 	 */
2186 	ASSERT(osp->os_open_owner->oo_seqid_inuse);
2187 	osp->os_final_close = 0;
2188 	osp->os_force_close = 0;
2189 	if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2190 		osp->os_dc_openacc = open_args->share_access;
2191 	mutex_exit(&osp->os_sync_lock);
2192 
2193 	nfs4_end_open_seqid_sync(oop);
2194 
2195 	/* accept delegation, if any */
2196 	nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2197 
2198 	nfs4args_copen_free(open_args);
2199 
2200 	nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2201 
2202 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2203 
2204 	ASSERT(nfs4_consistent_type(vp));
2205 
2206 	open_owner_rele(oop);
2207 	crfree(cr);
2208 	crfree(cred_otw);
2209 	return;
2210 
2211 kill_file:
2212 	nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2213 failed_reopen:
2214 	NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2215 	    "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2216 	    (void *)osp, (void *)cr, rnode4info(rp)));
2217 	mutex_enter(&osp->os_sync_lock);
2218 	osp->os_failed_reopen = 1;
2219 	mutex_exit(&osp->os_sync_lock);
2220 bailout:
2221 	if (oop != NULL) {
2222 		nfs4_end_open_seqid_sync(oop);
2223 		open_owner_rele(oop);
2224 	}
2225 	if (cr != NULL)
2226 		crfree(cr);
2227 	if (cred_otw != NULL)
2228 		crfree(cred_otw);
2229 }
2230 
2231 /* for . and .. OPENs */
2232 /* ARGSUSED */
2233 static int
nfs4_open_non_reg_file(vnode_t ** vpp,int flag,cred_t * cr)2234 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2235 {
2236 	rnode4_t *rp;
2237 	nfs4_ga_res_t gar;
2238 
2239 	ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2240 
2241 	/*
2242 	 * If close-to-open consistency checking is turned off or
2243 	 * if there is no cached data, we can avoid
2244 	 * the over the wire getattr.  Otherwise, force a
2245 	 * call to the server to get fresh attributes and to
2246 	 * check caches. This is required for close-to-open
2247 	 * consistency.
2248 	 */
2249 	rp = VTOR4(*vpp);
2250 	if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2251 	    (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2252 		return (0);
2253 
2254 	return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2255 }
2256 
2257 /*
2258  * CLOSE a file
2259  */
2260 /* ARGSUSED */
2261 static int
nfs4_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)2262 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2263     caller_context_t *ct)
2264 {
2265 	rnode4_t	*rp;
2266 	int		 error = 0;
2267 	int		 r_error = 0;
2268 	int		 n4error = 0;
2269 	nfs4_error_t	 e = { 0, NFS4_OK, RPC_SUCCESS };
2270 
2271 	/*
2272 	 * Remove client state for this (lockowner, file) pair.
2273 	 * Issue otw v4 call to have the server do the same.
2274 	 */
2275 
2276 	rp = VTOR4(vp);
2277 
2278 	/*
2279 	 * zone_enter(2) prevents processes from changing zones with NFS files
2280 	 * open; if we happen to get here from the wrong zone we can't do
2281 	 * anything over the wire.
2282 	 */
2283 	if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2284 		/*
2285 		 * We could attempt to clean up locks, except we're sure
2286 		 * that the current process didn't acquire any locks on
2287 		 * the file: any attempt to lock a file belong to another zone
2288 		 * will fail, and one can't lock an NFS file and then change
2289 		 * zones, as that fails too.
2290 		 *
2291 		 * Returning an error here is the sane thing to do.  A
2292 		 * subsequent call to VN_RELE() which translates to a
2293 		 * nfs4_inactive() will clean up state: if the zone of the
2294 		 * vnode's origin is still alive and kicking, the inactive
2295 		 * thread will handle the request (from the correct zone), and
2296 		 * everything (minus the OTW close call) should be OK.  If the
2297 		 * zone is going away nfs4_async_inactive() will throw away
2298 		 * delegations, open streams and cached pages inline.
2299 		 */
2300 		return (EIO);
2301 	}
2302 
2303 	/*
2304 	 * If we are using local locking for this filesystem, then
2305 	 * release all of the SYSV style record locks.  Otherwise,
2306 	 * we are doing network locking and we need to release all
2307 	 * of the network locks.  All of the locks held by this
2308 	 * process on this file are released no matter what the
2309 	 * incoming reference count is.
2310 	 */
2311 	if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2312 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2313 		cleanshares(vp, ttoproc(curthread)->p_pid);
2314 	} else
2315 		e.error = nfs4_lockrelease(vp, flag, offset, cr);
2316 
2317 	if (e.error) {
2318 		struct lm_sysid *lmsid;
2319 		lmsid = nfs4_find_sysid(VTOMI4(vp));
2320 		if (lmsid == NULL) {
2321 			DTRACE_PROBE2(unknown__sysid, int, e.error,
2322 			    vnode_t *, vp);
2323 		} else {
2324 			cleanlocks(vp, ttoproc(curthread)->p_pid,
2325 			    (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2326 
2327 			lm_rel_sysid(lmsid);
2328 		}
2329 		return (e.error);
2330 	}
2331 
2332 	if (count > 1)
2333 		return (0);
2334 
2335 	/*
2336 	 * If the file has been `unlinked', then purge the
2337 	 * DNLC so that this vnode will get reycled quicker
2338 	 * and the .nfs* file on the server will get removed.
2339 	 */
2340 	if (rp->r_unldvp != NULL)
2341 		dnlc_purge_vp(vp);
2342 
2343 	/*
2344 	 * If the file was open for write and there are pages,
2345 	 * do a synchronous flush and commit of all of the
2346 	 * dirty and uncommitted pages.
2347 	 */
2348 	ASSERT(!e.error);
2349 	if ((flag & FWRITE) && nfs4_has_pages(vp))
2350 		error = nfs4_putpage_commit(vp, 0, 0, cr);
2351 
2352 	mutex_enter(&rp->r_statelock);
2353 	r_error = rp->r_error;
2354 	rp->r_error = 0;
2355 	mutex_exit(&rp->r_statelock);
2356 
2357 	/*
2358 	 * If this file type is one for which no explicit 'open' was
2359 	 * done, then bail now (ie. no need for protocol 'close'). If
2360 	 * there was an error w/the vm subsystem, return _that_ error,
2361 	 * otherwise, return any errors that may've been reported via
2362 	 * the rnode.
2363 	 */
2364 	if (vp->v_type != VREG)
2365 		return (error ? error : r_error);
2366 
2367 	/*
2368 	 * The sync putpage commit may have failed above, but since
2369 	 * we're working w/a regular file, we need to do the protocol
2370 	 * 'close' (nfs4close_one will figure out if an otw close is
2371 	 * needed or not). Report any errors _after_ doing the protocol
2372 	 * 'close'.
2373 	 */
2374 	nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2375 	n4error = e.error ? e.error : geterrno4(e.stat);
2376 
2377 	/*
2378 	 * Error reporting prio (Hi -> Lo)
2379 	 *
2380 	 *   i) nfs4_putpage_commit (error)
2381 	 *  ii) rnode's (r_error)
2382 	 * iii) nfs4close_one (n4error)
2383 	 */
2384 	return (error ? error : (r_error ? r_error : n4error));
2385 }
2386 
2387 /*
2388  * Initialize *lost_rqstp.
2389  */
2390 
2391 static void
nfs4close_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,cred_t * cr,vnode_t * vp)2392 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2393     nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2394     vnode_t *vp)
2395 {
2396 	if (error != ETIMEDOUT && error != EINTR &&
2397 	    !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2398 		lost_rqstp->lr_op = 0;
2399 		return;
2400 	}
2401 
2402 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2403 	    "nfs4close_save_lost_rqst: error %d", error));
2404 
2405 	lost_rqstp->lr_op = OP_CLOSE;
2406 	/*
2407 	 * The vp is held and rele'd via the recovery code.
2408 	 * See nfs4_save_lost_rqst.
2409 	 */
2410 	lost_rqstp->lr_vp = vp;
2411 	lost_rqstp->lr_dvp = NULL;
2412 	lost_rqstp->lr_oop = oop;
2413 	lost_rqstp->lr_osp = osp;
2414 	ASSERT(osp != NULL);
2415 	ASSERT(mutex_owned(&osp->os_sync_lock));
2416 	osp->os_pending_close = 1;
2417 	lost_rqstp->lr_lop = NULL;
2418 	lost_rqstp->lr_cr = cr;
2419 	lost_rqstp->lr_flk = NULL;
2420 	lost_rqstp->lr_putfirst = FALSE;
2421 }
2422 
2423 /*
2424  * Assumes you already have the open seqid sync grabbed as well as the
2425  * 'os_sync_lock'.  Note: this will release the open seqid sync and
2426  * 'os_sync_lock' if client recovery starts.  Calling functions have to
2427  * be prepared to handle this.
2428  *
2429  * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2430  * was needed and was started, and that the calling function should retry
2431  * this function; otherwise it is returned as 0.
2432  *
2433  * Errors are returned via the nfs4_error_t parameter.
2434  */
2435 static void
nfs4close_otw(rnode4_t * rp,cred_t * cred_otw,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,int * recov,int * did_start_seqid_syncp,nfs4_close_type_t close_type,nfs4_error_t * ep,int * have_sync_lockp)2436 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2437     nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2438     nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2439 {
2440 	COMPOUND4args_clnt args;
2441 	COMPOUND4res_clnt res;
2442 	CLOSE4args *close_args;
2443 	nfs_resop4 *resop;
2444 	nfs_argop4 argop[3];
2445 	int doqueue = 1;
2446 	mntinfo4_t *mi;
2447 	seqid4 seqid;
2448 	vnode_t *vp;
2449 	bool_t needrecov = FALSE;
2450 	nfs4_lost_rqst_t lost_rqst;
2451 	hrtime_t t;
2452 
2453 	ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2454 
2455 	ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2456 
2457 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2458 
2459 	/* Only set this to 1 if recovery is started */
2460 	*recov = 0;
2461 
2462 	/* do the OTW call to close the file */
2463 
2464 	if (close_type == CLOSE_RESEND)
2465 		args.ctag = TAG_CLOSE_LOST;
2466 	else if (close_type == CLOSE_AFTER_RESEND)
2467 		args.ctag = TAG_CLOSE_UNDO;
2468 	else
2469 		args.ctag = TAG_CLOSE;
2470 
2471 	args.array_len = 3;
2472 	args.array = argop;
2473 
2474 	vp = RTOV4(rp);
2475 
2476 	mi = VTOMI4(vp);
2477 
2478 	/* putfh target fh */
2479 	argop[0].argop = OP_CPUTFH;
2480 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2481 
2482 	argop[1].argop = OP_GETATTR;
2483 	argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2484 	argop[1].nfs_argop4_u.opgetattr.mi = mi;
2485 
2486 	argop[2].argop = OP_CLOSE;
2487 	close_args = &argop[2].nfs_argop4_u.opclose;
2488 
2489 	seqid = nfs4_get_open_seqid(oop) + 1;
2490 
2491 	close_args->seqid = seqid;
2492 	close_args->open_stateid = osp->open_stateid;
2493 
2494 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2495 	    "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2496 	    rnode4info(rp)));
2497 
2498 	t = gethrtime();
2499 
2500 	rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2501 
2502 	if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2503 		nfs4_set_open_seqid(seqid, oop, args.ctag);
2504 	}
2505 
2506 	needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2507 	if (ep->error && !needrecov) {
2508 		/*
2509 		 * if there was an error and no recovery is to be done
2510 		 * then then set up the file to flush its cache if
2511 		 * needed for the next caller.
2512 		 */
2513 		mutex_enter(&rp->r_statelock);
2514 		PURGE_ATTRCACHE4_LOCKED(rp);
2515 		rp->r_flags &= ~R4WRITEMODIFIED;
2516 		mutex_exit(&rp->r_statelock);
2517 		return;
2518 	}
2519 
2520 	if (needrecov) {
2521 		bool_t abort;
2522 		nfs4_bseqid_entry_t *bsep = NULL;
2523 
2524 		if (close_type != CLOSE_RESEND)
2525 			nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2526 			    osp, cred_otw, vp);
2527 
2528 		if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2529 			bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2530 			    0, args.ctag, close_args->seqid);
2531 
2532 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2533 		    "nfs4close_otw: initiating recovery. error %d "
2534 		    "res.status %d", ep->error, res.status));
2535 
2536 		/*
2537 		 * Drop the 'os_sync_lock' here so we don't hit
2538 		 * a potential recursive mutex_enter via an
2539 		 * 'open_stream_hold()'.
2540 		 */
2541 		mutex_exit(&osp->os_sync_lock);
2542 		*have_sync_lockp = 0;
2543 		abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2544 		    (close_type != CLOSE_RESEND &&
2545 		    lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2546 		    OP_CLOSE, bsep, NULL, NULL);
2547 
2548 		/* drop open seq sync, and let the calling function regrab it */
2549 		nfs4_end_open_seqid_sync(oop);
2550 		*did_start_seqid_syncp = 0;
2551 
2552 		if (bsep)
2553 			kmem_free(bsep, sizeof (*bsep));
2554 		/*
2555 		 * For signals, the caller wants to quit, so don't say to
2556 		 * retry.  For forced unmount, if it's a user thread, it
2557 		 * wants to quit.  If it's a recovery thread, the retry
2558 		 * will happen higher-up on the call stack.  Either way,
2559 		 * don't say to retry.
2560 		 */
2561 		if (abort == FALSE && ep->error != EINTR &&
2562 		    !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2563 		    close_type != CLOSE_RESEND &&
2564 		    close_type != CLOSE_AFTER_RESEND)
2565 			*recov = 1;
2566 		else
2567 			*recov = 0;
2568 
2569 		if (!ep->error)
2570 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2571 		return;
2572 	}
2573 
2574 	if (res.status) {
2575 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2576 		return;
2577 	}
2578 
2579 	mutex_enter(&rp->r_statev4_lock);
2580 	rp->created_v4 = 0;
2581 	mutex_exit(&rp->r_statev4_lock);
2582 
2583 	resop = &res.array[2];
2584 	osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2585 	osp->os_valid = 0;
2586 
2587 	/*
2588 	 * This removes the reference obtained at OPEN; ie, when the
2589 	 * open stream structure was created.
2590 	 *
2591 	 * We don't have to worry about calling 'open_stream_rele'
2592 	 * since we our currently holding a reference to the open
2593 	 * stream which means the count cannot go to 0 with this
2594 	 * decrement.
2595 	 */
2596 	ASSERT(osp->os_ref_count >= 2);
2597 	osp->os_ref_count--;
2598 
2599 	if (ep->error == 0) {
2600 		mutex_exit(&osp->os_sync_lock);
2601 		*have_sync_lockp = 0;
2602 
2603 		nfs4_attr_cache(vp,
2604 		    &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2605 		    t, cred_otw, TRUE, NULL);
2606 	}
2607 
2608 	NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2609 	    " returning %d", ep->error));
2610 
2611 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2612 }
2613 
2614 /* ARGSUSED */
2615 static int
nfs4_read(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2616 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2617     caller_context_t *ct)
2618 {
2619 	rnode4_t *rp;
2620 	u_offset_t off;
2621 	offset_t diff;
2622 	uint_t on;
2623 	uint_t n;
2624 	caddr_t base;
2625 	uint_t flags;
2626 	int error;
2627 	mntinfo4_t *mi;
2628 
2629 	rp = VTOR4(vp);
2630 
2631 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2632 
2633 	if (IS_SHADOW(vp, rp))
2634 		vp = RTOV4(rp);
2635 
2636 	if (vp->v_type != VREG)
2637 		return (EISDIR);
2638 
2639 	mi = VTOMI4(vp);
2640 
2641 	if (nfs_zone() != mi->mi_zone)
2642 		return (EIO);
2643 
2644 	if (uiop->uio_resid == 0)
2645 		return (0);
2646 
2647 	if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2648 		return (EINVAL);
2649 
2650 	mutex_enter(&rp->r_statelock);
2651 	if (rp->r_flags & R4RECOVERRP)
2652 		error = (rp->r_error ? rp->r_error : EIO);
2653 	else
2654 		error = 0;
2655 	mutex_exit(&rp->r_statelock);
2656 	if (error)
2657 		return (error);
2658 
2659 	/*
2660 	 * Bypass VM if caching has been disabled (e.g., locking) or if
2661 	 * using client-side direct I/O and the file is not mmap'd and
2662 	 * there are no cached pages.
2663 	 */
2664 	if ((vp->v_flag & VNOCACHE) ||
2665 	    (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2666 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2667 		size_t resid = 0;
2668 
2669 		return (nfs4read(vp, NULL, uiop->uio_loffset,
2670 		    uiop->uio_resid, &resid, cr, FALSE, uiop));
2671 	}
2672 
2673 	error = 0;
2674 
2675 	do {
2676 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2677 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2678 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
2679 
2680 		if (error = nfs4_validate_caches(vp, cr))
2681 			break;
2682 
2683 		mutex_enter(&rp->r_statelock);
2684 		while (rp->r_flags & R4INCACHEPURGE) {
2685 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2686 				mutex_exit(&rp->r_statelock);
2687 				return (EINTR);
2688 			}
2689 		}
2690 		diff = rp->r_size - uiop->uio_loffset;
2691 		mutex_exit(&rp->r_statelock);
2692 		if (diff <= 0)
2693 			break;
2694 		if (diff < n)
2695 			n = (uint_t)diff;
2696 
2697 		if (vpm_enable) {
2698 			/*
2699 			 * Copy data.
2700 			 */
2701 			error = vpm_data_copy(vp, off + on, n, uiop,
2702 			    1, NULL, 0, S_READ);
2703 		} else {
2704 			base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2705 			    S_READ);
2706 
2707 			error = uiomove(base + on, n, UIO_READ, uiop);
2708 		}
2709 
2710 		if (!error) {
2711 			/*
2712 			 * If read a whole block or read to eof,
2713 			 * won't need this buffer again soon.
2714 			 */
2715 			mutex_enter(&rp->r_statelock);
2716 			if (n + on == MAXBSIZE ||
2717 			    uiop->uio_loffset == rp->r_size)
2718 				flags = SM_DONTNEED;
2719 			else
2720 				flags = 0;
2721 			mutex_exit(&rp->r_statelock);
2722 			if (vpm_enable) {
2723 				error = vpm_sync_pages(vp, off, n, flags);
2724 			} else {
2725 				error = segmap_release(segkmap, base, flags);
2726 			}
2727 		} else {
2728 			if (vpm_enable) {
2729 				(void) vpm_sync_pages(vp, off, n, 0);
2730 			} else {
2731 				(void) segmap_release(segkmap, base, 0);
2732 			}
2733 		}
2734 	} while (!error && uiop->uio_resid > 0);
2735 
2736 	return (error);
2737 }
2738 
2739 /* ARGSUSED */
2740 static int
nfs4_write(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2741 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2742     caller_context_t *ct)
2743 {
2744 	rlim64_t limit = uiop->uio_llimit;
2745 	rnode4_t *rp;
2746 	u_offset_t off;
2747 	caddr_t base;
2748 	uint_t flags;
2749 	int remainder;
2750 	size_t n;
2751 	int on;
2752 	int error;
2753 	int resid;
2754 	u_offset_t offset;
2755 	mntinfo4_t *mi;
2756 	uint_t bsize;
2757 
2758 	rp = VTOR4(vp);
2759 
2760 	if (IS_SHADOW(vp, rp))
2761 		vp = RTOV4(rp);
2762 
2763 	if (vp->v_type != VREG)
2764 		return (EISDIR);
2765 
2766 	mi = VTOMI4(vp);
2767 
2768 	if (nfs_zone() != mi->mi_zone)
2769 		return (EIO);
2770 
2771 	if (uiop->uio_resid == 0)
2772 		return (0);
2773 
2774 	mutex_enter(&rp->r_statelock);
2775 	if (rp->r_flags & R4RECOVERRP)
2776 		error = (rp->r_error ? rp->r_error : EIO);
2777 	else
2778 		error = 0;
2779 	mutex_exit(&rp->r_statelock);
2780 	if (error)
2781 		return (error);
2782 
2783 	if (ioflag & FAPPEND) {
2784 		struct vattr va;
2785 
2786 		/*
2787 		 * Must serialize if appending.
2788 		 */
2789 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2790 			nfs_rw_exit(&rp->r_rwlock);
2791 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2792 			    INTR4(vp)))
2793 				return (EINTR);
2794 		}
2795 
2796 		va.va_mask = AT_SIZE;
2797 		error = nfs4getattr(vp, &va, cr);
2798 		if (error)
2799 			return (error);
2800 		uiop->uio_loffset = va.va_size;
2801 	}
2802 
2803 	offset = uiop->uio_loffset + uiop->uio_resid;
2804 
2805 	if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2806 		return (EINVAL);
2807 
2808 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2809 		limit = MAXOFFSET_T;
2810 
2811 	/*
2812 	 * Check to make sure that the process will not exceed
2813 	 * its limit on file size.  It is okay to write up to
2814 	 * the limit, but not beyond.  Thus, the write which
2815 	 * reaches the limit will be short and the next write
2816 	 * will return an error.
2817 	 */
2818 	remainder = 0;
2819 	if (offset > uiop->uio_llimit) {
2820 		remainder = offset - uiop->uio_llimit;
2821 		uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2822 		if (uiop->uio_resid <= 0) {
2823 			proc_t *p = ttoproc(curthread);
2824 
2825 			uiop->uio_resid += remainder;
2826 			mutex_enter(&p->p_lock);
2827 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2828 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2829 			mutex_exit(&p->p_lock);
2830 			return (EFBIG);
2831 		}
2832 	}
2833 
2834 	/* update the change attribute, if we have a write delegation */
2835 
2836 	mutex_enter(&rp->r_statev4_lock);
2837 	if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2838 		rp->r_deleg_change++;
2839 
2840 	mutex_exit(&rp->r_statev4_lock);
2841 
2842 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2843 		return (EINTR);
2844 
2845 	/*
2846 	 * Bypass VM if caching has been disabled (e.g., locking) or if
2847 	 * using client-side direct I/O and the file is not mmap'd and
2848 	 * there are no cached pages.
2849 	 */
2850 	if ((vp->v_flag & VNOCACHE) ||
2851 	    (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2852 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2853 		size_t bufsize;
2854 		int count;
2855 		u_offset_t org_offset;
2856 		stable_how4 stab_comm;
2857 nfs4_fwrite:
2858 		if (rp->r_flags & R4STALE) {
2859 			resid = uiop->uio_resid;
2860 			offset = uiop->uio_loffset;
2861 			error = rp->r_error;
2862 			/*
2863 			 * A close may have cleared r_error, if so,
2864 			 * propagate ESTALE error return properly
2865 			 */
2866 			if (error == 0)
2867 				error = ESTALE;
2868 			goto bottom;
2869 		}
2870 
2871 		bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2872 		base = kmem_alloc(bufsize, KM_SLEEP);
2873 		do {
2874 			if (ioflag & FDSYNC)
2875 				stab_comm = DATA_SYNC4;
2876 			else
2877 				stab_comm = FILE_SYNC4;
2878 			resid = uiop->uio_resid;
2879 			offset = uiop->uio_loffset;
2880 			count = MIN(uiop->uio_resid, bufsize);
2881 			org_offset = uiop->uio_loffset;
2882 			error = uiomove(base, count, UIO_WRITE, uiop);
2883 			if (!error) {
2884 				error = nfs4write(vp, base, org_offset,
2885 				    count, cr, &stab_comm);
2886 				if (!error) {
2887 					mutex_enter(&rp->r_statelock);
2888 					if (rp->r_size < uiop->uio_loffset)
2889 						rp->r_size = uiop->uio_loffset;
2890 					mutex_exit(&rp->r_statelock);
2891 				}
2892 			}
2893 		} while (!error && uiop->uio_resid > 0);
2894 		kmem_free(base, bufsize);
2895 		goto bottom;
2896 	}
2897 
2898 	bsize = vp->v_vfsp->vfs_bsize;
2899 
2900 	do {
2901 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2902 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2903 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
2904 
2905 		resid = uiop->uio_resid;
2906 		offset = uiop->uio_loffset;
2907 
2908 		if (rp->r_flags & R4STALE) {
2909 			error = rp->r_error;
2910 			/*
2911 			 * A close may have cleared r_error, if so,
2912 			 * propagate ESTALE error return properly
2913 			 */
2914 			if (error == 0)
2915 				error = ESTALE;
2916 			break;
2917 		}
2918 
2919 		/*
2920 		 * Don't create dirty pages faster than they
2921 		 * can be cleaned so that the system doesn't
2922 		 * get imbalanced.  If the async queue is
2923 		 * maxed out, then wait for it to drain before
2924 		 * creating more dirty pages.  Also, wait for
2925 		 * any threads doing pagewalks in the vop_getattr
2926 		 * entry points so that they don't block for
2927 		 * long periods.
2928 		 */
2929 		mutex_enter(&rp->r_statelock);
2930 		while ((mi->mi_max_threads != 0 &&
2931 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
2932 		    rp->r_gcount > 0) {
2933 			if (INTR4(vp)) {
2934 				klwp_t *lwp = ttolwp(curthread);
2935 
2936 				if (lwp != NULL)
2937 					lwp->lwp_nostop++;
2938 				if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2939 					mutex_exit(&rp->r_statelock);
2940 					if (lwp != NULL)
2941 						lwp->lwp_nostop--;
2942 					error = EINTR;
2943 					goto bottom;
2944 				}
2945 				if (lwp != NULL)
2946 					lwp->lwp_nostop--;
2947 			} else
2948 				cv_wait(&rp->r_cv, &rp->r_statelock);
2949 		}
2950 		mutex_exit(&rp->r_statelock);
2951 
2952 		/*
2953 		 * Touch the page and fault it in if it is not in core
2954 		 * before segmap_getmapflt or vpm_data_copy can lock it.
2955 		 * This is to avoid the deadlock if the buffer is mapped
2956 		 * to the same file through mmap which we want to write.
2957 		 */
2958 		uio_prefaultpages((long)n, uiop);
2959 
2960 		if (vpm_enable) {
2961 			/*
2962 			 * It will use kpm mappings, so no need to
2963 			 * pass an address.
2964 			 */
2965 			error = writerp4(rp, NULL, n, uiop, 0);
2966 		} else  {
2967 			if (segmap_kpm) {
2968 				int pon = uiop->uio_loffset & PAGEOFFSET;
2969 				size_t pn = MIN(PAGESIZE - pon,
2970 				    uiop->uio_resid);
2971 				int pagecreate;
2972 
2973 				mutex_enter(&rp->r_statelock);
2974 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
2975 				    uiop->uio_loffset + pn >= rp->r_size);
2976 				mutex_exit(&rp->r_statelock);
2977 
2978 				base = segmap_getmapflt(segkmap, vp, off + on,
2979 				    pn, !pagecreate, S_WRITE);
2980 
2981 				error = writerp4(rp, base + pon, n, uiop,
2982 				    pagecreate);
2983 
2984 			} else {
2985 				base = segmap_getmapflt(segkmap, vp, off + on,
2986 				    n, 0, S_READ);
2987 				error = writerp4(rp, base + on, n, uiop, 0);
2988 			}
2989 		}
2990 
2991 		if (!error) {
2992 			if (mi->mi_flags & MI4_NOAC)
2993 				flags = SM_WRITE;
2994 			else if ((uiop->uio_loffset % bsize) == 0 ||
2995 			    IS_SWAPVP(vp)) {
2996 				/*
2997 				 * Have written a whole block.
2998 				 * Start an asynchronous write
2999 				 * and mark the buffer to
3000 				 * indicate that it won't be
3001 				 * needed again soon.
3002 				 */
3003 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
3004 			} else
3005 				flags = 0;
3006 			if ((ioflag & (FSYNC|FDSYNC)) ||
3007 			    (rp->r_flags & R4OUTOFSPACE)) {
3008 				flags &= ~SM_ASYNC;
3009 				flags |= SM_WRITE;
3010 			}
3011 			if (vpm_enable) {
3012 				error = vpm_sync_pages(vp, off, n, flags);
3013 			} else {
3014 				error = segmap_release(segkmap, base, flags);
3015 			}
3016 		} else {
3017 			if (vpm_enable) {
3018 				(void) vpm_sync_pages(vp, off, n, 0);
3019 			} else {
3020 				(void) segmap_release(segkmap, base, 0);
3021 			}
3022 			/*
3023 			 * In the event that we got an access error while
3024 			 * faulting in a page for a write-only file just
3025 			 * force a write.
3026 			 */
3027 			if (error == EACCES)
3028 				goto nfs4_fwrite;
3029 		}
3030 	} while (!error && uiop->uio_resid > 0);
3031 
3032 bottom:
3033 	if (error) {
3034 		uiop->uio_resid = resid + remainder;
3035 		uiop->uio_loffset = offset;
3036 	} else {
3037 		uiop->uio_resid += remainder;
3038 
3039 		mutex_enter(&rp->r_statev4_lock);
3040 		if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3041 			gethrestime(&rp->r_attr.va_mtime);
3042 			rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3043 		}
3044 		mutex_exit(&rp->r_statev4_lock);
3045 	}
3046 
3047 	nfs_rw_exit(&rp->r_lkserlock);
3048 
3049 	return (error);
3050 }
3051 
3052 /*
3053  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3054  */
3055 static int
nfs4_rdwrlbn(vnode_t * vp,page_t * pp,u_offset_t off,size_t len,int flags,cred_t * cr)3056 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3057     int flags, cred_t *cr)
3058 {
3059 	struct buf *bp;
3060 	int error;
3061 	page_t *savepp;
3062 	uchar_t fsdata;
3063 	stable_how4 stab_comm;
3064 
3065 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3066 	bp = pageio_setup(pp, len, vp, flags);
3067 	ASSERT(bp != NULL);
3068 
3069 	/*
3070 	 * pageio_setup should have set b_addr to 0.  This
3071 	 * is correct since we want to do I/O on a page
3072 	 * boundary.  bp_mapin will use this addr to calculate
3073 	 * an offset, and then set b_addr to the kernel virtual
3074 	 * address it allocated for us.
3075 	 */
3076 	ASSERT(bp->b_un.b_addr == 0);
3077 
3078 	bp->b_edev = 0;
3079 	bp->b_dev = 0;
3080 	bp->b_lblkno = lbtodb(off);
3081 	bp->b_file = vp;
3082 	bp->b_offset = (offset_t)off;
3083 	bp_mapin(bp);
3084 
3085 	if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3086 	    freemem > desfree)
3087 		stab_comm = UNSTABLE4;
3088 	else
3089 		stab_comm = FILE_SYNC4;
3090 
3091 	error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3092 
3093 	bp_mapout(bp);
3094 	pageio_done(bp);
3095 
3096 	if (stab_comm == UNSTABLE4)
3097 		fsdata = C_DELAYCOMMIT;
3098 	else
3099 		fsdata = C_NOCOMMIT;
3100 
3101 	savepp = pp;
3102 	do {
3103 		pp->p_fsdata = fsdata;
3104 	} while ((pp = pp->p_next) != savepp);
3105 
3106 	return (error);
3107 }
3108 
3109 /*
3110  */
3111 static int
nfs4rdwr_check_osid(vnode_t * vp,nfs4_error_t * ep,cred_t * cr)3112 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3113 {
3114 	nfs4_open_owner_t	*oop;
3115 	nfs4_open_stream_t	*osp;
3116 	rnode4_t		*rp = VTOR4(vp);
3117 	mntinfo4_t		*mi = VTOMI4(vp);
3118 	int			reopen_needed;
3119 
3120 	ASSERT(nfs_zone() == mi->mi_zone);
3121 
3122 
3123 	oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3124 	if (!oop)
3125 		return (EIO);
3126 
3127 	/* returns with 'os_sync_lock' held */
3128 	osp = find_open_stream(oop, rp);
3129 	if (!osp) {
3130 		open_owner_rele(oop);
3131 		return (EIO);
3132 	}
3133 
3134 	if (osp->os_failed_reopen) {
3135 		mutex_exit(&osp->os_sync_lock);
3136 		open_stream_rele(osp, rp);
3137 		open_owner_rele(oop);
3138 		return (EIO);
3139 	}
3140 
3141 	/*
3142 	 * Determine whether a reopen is needed.  If this
3143 	 * is a delegation open stream, then the os_delegation bit
3144 	 * should be set.
3145 	 */
3146 
3147 	reopen_needed = osp->os_delegation;
3148 
3149 	mutex_exit(&osp->os_sync_lock);
3150 	open_owner_rele(oop);
3151 
3152 	if (reopen_needed) {
3153 		nfs4_error_zinit(ep);
3154 		nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3155 		mutex_enter(&osp->os_sync_lock);
3156 		if (ep->error || ep->stat || osp->os_failed_reopen) {
3157 			mutex_exit(&osp->os_sync_lock);
3158 			open_stream_rele(osp, rp);
3159 			return (EIO);
3160 		}
3161 		mutex_exit(&osp->os_sync_lock);
3162 	}
3163 	open_stream_rele(osp, rp);
3164 
3165 	return (0);
3166 }
3167 
3168 /*
3169  * Write to file.  Writes to remote server in largest size
3170  * chunks that the server can handle.  Write is synchronous.
3171  */
3172 static int
nfs4write(vnode_t * vp,caddr_t base,u_offset_t offset,int count,cred_t * cr,stable_how4 * stab_comm)3173 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3174     stable_how4 *stab_comm)
3175 {
3176 	mntinfo4_t *mi;
3177 	COMPOUND4args_clnt args;
3178 	COMPOUND4res_clnt res;
3179 	WRITE4args *wargs;
3180 	WRITE4res *wres;
3181 	nfs_argop4 argop[2];
3182 	nfs_resop4 *resop;
3183 	int tsize;
3184 	stable_how4 stable;
3185 	rnode4_t *rp;
3186 	int doqueue = 1;
3187 	bool_t needrecov;
3188 	nfs4_recov_state_t recov_state;
3189 	nfs4_stateid_types_t sid_types;
3190 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3191 	int recov;
3192 
3193 	rp = VTOR4(vp);
3194 	mi = VTOMI4(vp);
3195 
3196 	ASSERT(nfs_zone() == mi->mi_zone);
3197 
3198 	stable = *stab_comm;
3199 	*stab_comm = FILE_SYNC4;
3200 
3201 	needrecov = FALSE;
3202 	recov_state.rs_flags = 0;
3203 	recov_state.rs_num_retry_despite_err = 0;
3204 	nfs4_init_stateid_types(&sid_types);
3205 
3206 	/* Is curthread the recovery thread? */
3207 	mutex_enter(&mi->mi_lock);
3208 	recov = (mi->mi_recovthread == curthread);
3209 	mutex_exit(&mi->mi_lock);
3210 
3211 recov_retry:
3212 	args.ctag = TAG_WRITE;
3213 	args.array_len = 2;
3214 	args.array = argop;
3215 
3216 	if (!recov) {
3217 		e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3218 		    &recov_state, NULL);
3219 		if (e.error)
3220 			return (e.error);
3221 	}
3222 
3223 	/* 0. putfh target fh */
3224 	argop[0].argop = OP_CPUTFH;
3225 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3226 
3227 	/* 1. write */
3228 	nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3229 
3230 	do {
3231 
3232 		wargs->offset = (offset4)offset;
3233 		wargs->data_val = base;
3234 
3235 		if (mi->mi_io_kstats) {
3236 			mutex_enter(&mi->mi_lock);
3237 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3238 			mutex_exit(&mi->mi_lock);
3239 		}
3240 
3241 		if ((vp->v_flag & VNOCACHE) ||
3242 		    (rp->r_flags & R4DIRECTIO) ||
3243 		    (mi->mi_flags & MI4_DIRECTIO))
3244 			tsize = MIN(mi->mi_stsize, count);
3245 		else
3246 			tsize = MIN(mi->mi_curwrite, count);
3247 		wargs->data_len = (uint_t)tsize;
3248 		rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3249 
3250 		if (mi->mi_io_kstats) {
3251 			mutex_enter(&mi->mi_lock);
3252 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3253 			mutex_exit(&mi->mi_lock);
3254 		}
3255 
3256 		if (!recov) {
3257 			needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3258 			if (e.error && !needrecov) {
3259 				nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3260 				    &recov_state, needrecov);
3261 				return (e.error);
3262 			}
3263 		} else {
3264 			if (e.error)
3265 				return (e.error);
3266 		}
3267 
3268 		/*
3269 		 * Do handling of OLD_STATEID outside
3270 		 * of the normal recovery framework.
3271 		 *
3272 		 * If write receives a BAD stateid error while using a
3273 		 * delegation stateid, retry using the open stateid (if it
3274 		 * exists).  If it doesn't have an open stateid, reopen the
3275 		 * file first, then retry.
3276 		 */
3277 		if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3278 		    sid_types.cur_sid_type != SPEC_SID) {
3279 			nfs4_save_stateid(&wargs->stateid, &sid_types);
3280 			if (!recov)
3281 				nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3282 				    &recov_state, needrecov);
3283 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3284 			goto recov_retry;
3285 		} else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3286 		    sid_types.cur_sid_type == DEL_SID) {
3287 			nfs4_save_stateid(&wargs->stateid, &sid_types);
3288 			mutex_enter(&rp->r_statev4_lock);
3289 			rp->r_deleg_return_pending = TRUE;
3290 			mutex_exit(&rp->r_statev4_lock);
3291 			if (nfs4rdwr_check_osid(vp, &e, cr)) {
3292 				if (!recov)
3293 					nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3294 					    &recov_state, needrecov);
3295 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3296 				return (EIO);
3297 			}
3298 			if (!recov)
3299 				nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3300 				    &recov_state, needrecov);
3301 			/* hold needed for nfs4delegreturn_thread */
3302 			VN_HOLD(vp);
3303 			nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3304 			    NFS4_DR_DISCARD), FALSE);
3305 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3306 			goto recov_retry;
3307 		}
3308 
3309 		if (needrecov) {
3310 			bool_t abort;
3311 
3312 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3313 			    "nfs4write: client got error %d, res.status %d"
3314 			    ", so start recovery", e.error, res.status));
3315 
3316 			abort = nfs4_start_recovery(&e,
3317 			    VTOMI4(vp), vp, NULL, &wargs->stateid,
3318 			    NULL, OP_WRITE, NULL, NULL, NULL);
3319 			if (!e.error) {
3320 				e.error = geterrno4(res.status);
3321 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3322 			}
3323 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3324 			    &recov_state, needrecov);
3325 			if (abort == FALSE)
3326 				goto recov_retry;
3327 			return (e.error);
3328 		}
3329 
3330 		if (res.status) {
3331 			e.error = geterrno4(res.status);
3332 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3333 			if (!recov)
3334 				nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3335 				    &recov_state, needrecov);
3336 			return (e.error);
3337 		}
3338 
3339 		resop = &res.array[1];	/* write res */
3340 		wres = &resop->nfs_resop4_u.opwrite;
3341 
3342 		if ((int)wres->count > tsize) {
3343 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3344 
3345 			zcmn_err(getzoneid(), CE_WARN,
3346 			    "nfs4write: server wrote %u, requested was %u",
3347 			    (int)wres->count, tsize);
3348 			if (!recov)
3349 				nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3350 				    &recov_state, needrecov);
3351 			return (EIO);
3352 		}
3353 		if (wres->committed == UNSTABLE4) {
3354 			*stab_comm = UNSTABLE4;
3355 			if (wargs->stable == DATA_SYNC4 ||
3356 			    wargs->stable == FILE_SYNC4) {
3357 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3358 				zcmn_err(getzoneid(), CE_WARN,
3359 				    "nfs4write: server %s did not commit "
3360 				    "to stable storage",
3361 				    rp->r_server->sv_hostname);
3362 				if (!recov)
3363 					nfs4_end_fop(VTOMI4(vp), vp, NULL,
3364 					    OH_WRITE, &recov_state, needrecov);
3365 				return (EIO);
3366 			}
3367 		}
3368 
3369 		tsize = (int)wres->count;
3370 		count -= tsize;
3371 		base += tsize;
3372 		offset += tsize;
3373 		if (mi->mi_io_kstats) {
3374 			mutex_enter(&mi->mi_lock);
3375 			KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3376 			KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3377 			    tsize;
3378 			mutex_exit(&mi->mi_lock);
3379 		}
3380 		lwp_stat_update(LWP_STAT_OUBLK, 1);
3381 		mutex_enter(&rp->r_statelock);
3382 		if (rp->r_flags & R4HAVEVERF) {
3383 			if (rp->r_writeverf != wres->writeverf) {
3384 				nfs4_set_mod(vp);
3385 				rp->r_writeverf = wres->writeverf;
3386 			}
3387 		} else {
3388 			rp->r_writeverf = wres->writeverf;
3389 			rp->r_flags |= R4HAVEVERF;
3390 		}
3391 		PURGE_ATTRCACHE4_LOCKED(rp);
3392 		rp->r_flags |= R4WRITEMODIFIED;
3393 		gethrestime(&rp->r_attr.va_mtime);
3394 		rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3395 		mutex_exit(&rp->r_statelock);
3396 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3397 	} while (count);
3398 
3399 	if (!recov)
3400 		nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3401 		    needrecov);
3402 
3403 	return (e.error);
3404 }
3405 
3406 /*
3407  * Read from a file.  Reads data in largest chunks our interface can handle.
3408  */
3409 static int
nfs4read(vnode_t * vp,caddr_t base,offset_t offset,int count,size_t * residp,cred_t * cr,bool_t async,struct uio * uiop)3410 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3411     size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3412 {
3413 	mntinfo4_t *mi;
3414 	COMPOUND4args_clnt args;
3415 	COMPOUND4res_clnt res;
3416 	READ4args *rargs;
3417 	nfs_argop4 argop[2];
3418 	int tsize;
3419 	int doqueue;
3420 	rnode4_t *rp;
3421 	int data_len;
3422 	bool_t is_eof;
3423 	bool_t needrecov = FALSE;
3424 	nfs4_recov_state_t recov_state;
3425 	nfs4_stateid_types_t sid_types;
3426 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3427 
3428 	rp = VTOR4(vp);
3429 	mi = VTOMI4(vp);
3430 	doqueue = 1;
3431 
3432 	ASSERT(nfs_zone() == mi->mi_zone);
3433 
3434 	args.ctag = async ? TAG_READAHEAD : TAG_READ;
3435 
3436 	args.array_len = 2;
3437 	args.array = argop;
3438 
3439 	nfs4_init_stateid_types(&sid_types);
3440 
3441 	recov_state.rs_flags = 0;
3442 	recov_state.rs_num_retry_despite_err = 0;
3443 
3444 recov_retry:
3445 	e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3446 	    &recov_state, NULL);
3447 	if (e.error)
3448 		return (e.error);
3449 
3450 	/* putfh target fh */
3451 	argop[0].argop = OP_CPUTFH;
3452 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3453 
3454 	/* read */
3455 	argop[1].argop = OP_READ;
3456 	rargs = &argop[1].nfs_argop4_u.opread;
3457 	rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3458 	    OP_READ, &sid_types, async);
3459 
3460 	do {
3461 		if (mi->mi_io_kstats) {
3462 			mutex_enter(&mi->mi_lock);
3463 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3464 			mutex_exit(&mi->mi_lock);
3465 		}
3466 
3467 		NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3468 		    "nfs4read: %s call, rp %s",
3469 		    needrecov ? "recov" : "first",
3470 		    rnode4info(rp)));
3471 
3472 		if ((vp->v_flag & VNOCACHE) ||
3473 		    (rp->r_flags & R4DIRECTIO) ||
3474 		    (mi->mi_flags & MI4_DIRECTIO))
3475 			tsize = MIN(mi->mi_tsize, count);
3476 		else
3477 			tsize = MIN(mi->mi_curread, count);
3478 
3479 		rargs->offset = (offset4)offset;
3480 		rargs->count = (count4)tsize;
3481 		rargs->res_data_val_alt = NULL;
3482 		rargs->res_mblk = NULL;
3483 		rargs->res_uiop = NULL;
3484 		rargs->res_maxsize = 0;
3485 		rargs->wlist = NULL;
3486 
3487 		if (uiop)
3488 			rargs->res_uiop = uiop;
3489 		else
3490 			rargs->res_data_val_alt = base;
3491 		rargs->res_maxsize = tsize;
3492 
3493 		rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3494 #ifdef	DEBUG
3495 		if (nfs4read_error_inject) {
3496 			res.status = nfs4read_error_inject;
3497 			nfs4read_error_inject = 0;
3498 		}
3499 #endif
3500 
3501 		if (mi->mi_io_kstats) {
3502 			mutex_enter(&mi->mi_lock);
3503 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3504 			mutex_exit(&mi->mi_lock);
3505 		}
3506 
3507 		needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3508 		if (e.error != 0 && !needrecov) {
3509 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3510 			    &recov_state, needrecov);
3511 			return (e.error);
3512 		}
3513 
3514 		/*
3515 		 * Do proper retry for OLD and BAD stateid errors outside
3516 		 * of the normal recovery framework.  There are two differences
3517 		 * between async and sync reads.  The first is that we allow
3518 		 * retry on BAD_STATEID for async reads, but not sync reads.
3519 		 * The second is that we mark the file dead for a failed
3520 		 * attempt with a special stateid for sync reads, but just
3521 		 * return EIO for async reads.
3522 		 *
3523 		 * If a sync read receives a BAD stateid error while using a
3524 		 * delegation stateid, retry using the open stateid (if it
3525 		 * exists).  If it doesn't have an open stateid, reopen the
3526 		 * file first, then retry.
3527 		 */
3528 		if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3529 		    res.status == NFS4ERR_BAD_STATEID) && async) {
3530 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3531 			    &recov_state, needrecov);
3532 			if (sid_types.cur_sid_type == SPEC_SID) {
3533 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3534 				return (EIO);
3535 			}
3536 			nfs4_save_stateid(&rargs->stateid, &sid_types);
3537 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3538 			goto recov_retry;
3539 		} else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3540 		    !async && sid_types.cur_sid_type != SPEC_SID) {
3541 			nfs4_save_stateid(&rargs->stateid, &sid_types);
3542 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3543 			    &recov_state, needrecov);
3544 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3545 			goto recov_retry;
3546 		} else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3547 		    sid_types.cur_sid_type == DEL_SID) {
3548 			nfs4_save_stateid(&rargs->stateid, &sid_types);
3549 			mutex_enter(&rp->r_statev4_lock);
3550 			rp->r_deleg_return_pending = TRUE;
3551 			mutex_exit(&rp->r_statev4_lock);
3552 			if (nfs4rdwr_check_osid(vp, &e, cr)) {
3553 				nfs4_end_fop(mi, vp, NULL, OH_READ,
3554 				    &recov_state, needrecov);
3555 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3556 				return (EIO);
3557 			}
3558 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3559 			    &recov_state, needrecov);
3560 			/* hold needed for nfs4delegreturn_thread */
3561 			VN_HOLD(vp);
3562 			nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3563 			    NFS4_DR_DISCARD), FALSE);
3564 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3565 			goto recov_retry;
3566 		}
3567 		if (needrecov) {
3568 			bool_t abort;
3569 
3570 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3571 			    "nfs4read: initiating recovery\n"));
3572 			abort = nfs4_start_recovery(&e,
3573 			    mi, vp, NULL, &rargs->stateid,
3574 			    NULL, OP_READ, NULL, NULL, NULL);
3575 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3576 			    &recov_state, needrecov);
3577 			/*
3578 			 * Do not retry if we got OLD_STATEID using a special
3579 			 * stateid.  This avoids looping with a broken server.
3580 			 */
3581 			if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3582 			    sid_types.cur_sid_type == SPEC_SID)
3583 				abort = TRUE;
3584 
3585 			if (abort == FALSE) {
3586 				/*
3587 				 * Need to retry all possible stateids in
3588 				 * case the recovery error wasn't stateid
3589 				 * related or the stateids have become
3590 				 * stale (server reboot).
3591 				 */
3592 				nfs4_init_stateid_types(&sid_types);
3593 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3594 				goto recov_retry;
3595 			}
3596 
3597 			if (!e.error) {
3598 				e.error = geterrno4(res.status);
3599 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3600 			}
3601 			return (e.error);
3602 		}
3603 
3604 		if (res.status) {
3605 			e.error = geterrno4(res.status);
3606 			nfs4_end_fop(mi, vp, NULL, OH_READ,
3607 			    &recov_state, needrecov);
3608 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3609 			return (e.error);
3610 		}
3611 
3612 		data_len = res.array[1].nfs_resop4_u.opread.data_len;
3613 		count -= data_len;
3614 		if (base)
3615 			base += data_len;
3616 		offset += data_len;
3617 		if (mi->mi_io_kstats) {
3618 			mutex_enter(&mi->mi_lock);
3619 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3620 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3621 			mutex_exit(&mi->mi_lock);
3622 		}
3623 		lwp_stat_update(LWP_STAT_INBLK, 1);
3624 		is_eof = res.array[1].nfs_resop4_u.opread.eof;
3625 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3626 
3627 	} while (count && !is_eof);
3628 
3629 	*residp = count;
3630 
3631 	nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3632 
3633 	return (e.error);
3634 }
3635 
3636 /* ARGSUSED */
3637 static int
nfs4_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)3638 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3639     caller_context_t *ct)
3640 {
3641 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
3642 		return (EIO);
3643 	switch (cmd) {
3644 		case _FIODIRECTIO:
3645 			return (nfs4_directio(vp, (int)arg, cr));
3646 		default:
3647 			return (ENOTTY);
3648 	}
3649 }
3650 
3651 /* ARGSUSED */
3652 int
nfs4_getattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)3653 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3654     caller_context_t *ct)
3655 {
3656 	int error;
3657 	rnode4_t *rp = VTOR4(vp);
3658 
3659 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
3660 		return (EIO);
3661 	/*
3662 	 * If it has been specified that the return value will
3663 	 * just be used as a hint, and we are only being asked
3664 	 * for size, fsid or rdevid, then return the client's
3665 	 * notion of these values without checking to make sure
3666 	 * that the attribute cache is up to date.
3667 	 * The whole point is to avoid an over the wire GETATTR
3668 	 * call.
3669 	 */
3670 	if (flags & ATTR_HINT) {
3671 		if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3672 			mutex_enter(&rp->r_statelock);
3673 			if (vap->va_mask & AT_SIZE)
3674 				vap->va_size = rp->r_size;
3675 			if (vap->va_mask & AT_FSID)
3676 				vap->va_fsid = rp->r_attr.va_fsid;
3677 			if (vap->va_mask & AT_RDEV)
3678 				vap->va_rdev = rp->r_attr.va_rdev;
3679 			mutex_exit(&rp->r_statelock);
3680 			return (0);
3681 		}
3682 	}
3683 
3684 	/*
3685 	 * Only need to flush pages if asking for the mtime
3686 	 * and if there any dirty pages or any outstanding
3687 	 * asynchronous (write) requests for this file.
3688 	 */
3689 	if (vap->va_mask & AT_MTIME) {
3690 		rp = VTOR4(vp);
3691 		if (nfs4_has_pages(vp)) {
3692 			mutex_enter(&rp->r_statev4_lock);
3693 			if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3694 				mutex_exit(&rp->r_statev4_lock);
3695 				if (rp->r_flags & R4DIRTY ||
3696 				    rp->r_awcount > 0) {
3697 					mutex_enter(&rp->r_statelock);
3698 					rp->r_gcount++;
3699 					mutex_exit(&rp->r_statelock);
3700 					error =
3701 					    nfs4_putpage(vp, (u_offset_t)0,
3702 					    0, 0, cr, NULL);
3703 					mutex_enter(&rp->r_statelock);
3704 					if (error && (error == ENOSPC ||
3705 					    error == EDQUOT)) {
3706 						if (!rp->r_error)
3707 							rp->r_error = error;
3708 					}
3709 					if (--rp->r_gcount == 0)
3710 						cv_broadcast(&rp->r_cv);
3711 					mutex_exit(&rp->r_statelock);
3712 				}
3713 			} else {
3714 				mutex_exit(&rp->r_statev4_lock);
3715 			}
3716 		}
3717 	}
3718 	return (nfs4getattr(vp, vap, cr));
3719 }
3720 
3721 int
nfs4_compare_modes(mode_t from_server,mode_t on_client)3722 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3723 {
3724 	/*
3725 	 * If these are the only two bits cleared
3726 	 * on the server then return 0 (OK) else
3727 	 * return 1 (BAD).
3728 	 */
3729 	on_client &= ~(S_ISUID|S_ISGID);
3730 	if (on_client == from_server)
3731 		return (0);
3732 	else
3733 		return (1);
3734 }
3735 
3736 /*ARGSUSED4*/
3737 static int
nfs4_setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)3738 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3739     caller_context_t *ct)
3740 {
3741 	int error;
3742 
3743 	if (vap->va_mask & AT_NOSET)
3744 		return (EINVAL);
3745 
3746 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
3747 		return (EIO);
3748 
3749 	/*
3750 	 * Don't call secpolicy_vnode_setattr, the client cannot
3751 	 * use its cached attributes to make security decisions
3752 	 * as the server may be faking mode bits or mapping uid/gid.
3753 	 * Always just let the server to the checking.
3754 	 * If we provide the ability to remove basic priviledges
3755 	 * to setattr (e.g. basic without chmod) then we will
3756 	 * need to add a check here before calling the server.
3757 	 */
3758 	error = nfs4setattr(vp, vap, flags, cr, NULL);
3759 
3760 	if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3761 		vnevent_truncate(vp, ct);
3762 
3763 	return (error);
3764 }
3765 
3766 /*
3767  * To replace the "guarded" version 3 setattr, we use two types of compound
3768  * setattr requests:
3769  * 1. The "normal" setattr, used when the size of the file isn't being
3770  *    changed - { Putfh <fh>; Setattr; Getattr }/
3771  * 2. If the size is changed, precede Setattr with: Getattr; Verify
3772  *    with only ctime as the argument. If the server ctime differs from
3773  *    what is cached on the client, the verify will fail, but we would
3774  *    already have the ctime from the preceding getattr, so just set it
3775  *    and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3776  *	Setattr; Getattr }.
3777  *
3778  * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3779  * this setattr and NULL if they are not.
3780  */
3781 static int
nfs4setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,vsecattr_t * vsap)3782 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3783     vsecattr_t *vsap)
3784 {
3785 	COMPOUND4args_clnt args;
3786 	COMPOUND4res_clnt res, *resp = NULL;
3787 	nfs4_ga_res_t *garp = NULL;
3788 	int numops = 3;			/* { Putfh; Setattr; Getattr } */
3789 	nfs_argop4 argop[5];
3790 	int verify_argop = -1;
3791 	int setattr_argop = 1;
3792 	nfs_resop4 *resop;
3793 	vattr_t va;
3794 	rnode4_t *rp;
3795 	int doqueue = 1;
3796 	uint_t mask = vap->va_mask;
3797 	mode_t omode;
3798 	vsecattr_t *vsp;
3799 	timestruc_t ctime;
3800 	bool_t needrecov = FALSE;
3801 	nfs4_recov_state_t recov_state;
3802 	nfs4_stateid_types_t sid_types;
3803 	stateid4 stateid;
3804 	hrtime_t t;
3805 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3806 	servinfo4_t *svp;
3807 	bitmap4 supp_attrs;
3808 
3809 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3810 	rp = VTOR4(vp);
3811 	nfs4_init_stateid_types(&sid_types);
3812 
3813 	/*
3814 	 * Only need to flush pages if there are any pages and
3815 	 * if the file is marked as dirty in some fashion.  The
3816 	 * file must be flushed so that we can accurately
3817 	 * determine the size of the file and the cached data
3818 	 * after the SETATTR returns.  A file is considered to
3819 	 * be dirty if it is either marked with R4DIRTY, has
3820 	 * outstanding i/o's active, or is mmap'd.  In this
3821 	 * last case, we can't tell whether there are dirty
3822 	 * pages, so we flush just to be sure.
3823 	 */
3824 	if (nfs4_has_pages(vp) &&
3825 	    ((rp->r_flags & R4DIRTY) ||
3826 	    rp->r_count > 0 ||
3827 	    rp->r_mapcnt > 0)) {
3828 		ASSERT(vp->v_type != VCHR);
3829 		e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3830 		if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3831 			mutex_enter(&rp->r_statelock);
3832 			if (!rp->r_error)
3833 				rp->r_error = e.error;
3834 			mutex_exit(&rp->r_statelock);
3835 		}
3836 	}
3837 
3838 	if (mask & AT_SIZE) {
3839 		/*
3840 		 * Verification setattr compound for non-deleg AT_SIZE:
3841 		 *	{ Putfh; Getattr; Verify; Setattr; Getattr }
3842 		 * Set ctime local here (outside the do_again label)
3843 		 * so that subsequent retries (after failed VERIFY)
3844 		 * will use ctime from GETATTR results (from failed
3845 		 * verify compound) as VERIFY arg.
3846 		 * If file has delegation, then VERIFY(time_metadata)
3847 		 * is of little added value, so don't bother.
3848 		 */
3849 		mutex_enter(&rp->r_statev4_lock);
3850 		if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3851 		    rp->r_deleg_return_pending) {
3852 			numops = 5;
3853 			ctime = rp->r_attr.va_ctime;
3854 		}
3855 		mutex_exit(&rp->r_statev4_lock);
3856 	}
3857 
3858 	recov_state.rs_flags = 0;
3859 	recov_state.rs_num_retry_despite_err = 0;
3860 
3861 	args.ctag = TAG_SETATTR;
3862 do_again:
3863 recov_retry:
3864 	setattr_argop = numops - 2;
3865 
3866 	args.array = argop;
3867 	args.array_len = numops;
3868 
3869 	e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3870 	if (e.error)
3871 		return (e.error);
3872 
3873 
3874 	/* putfh target fh */
3875 	argop[0].argop = OP_CPUTFH;
3876 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3877 
3878 	if (numops == 5) {
3879 		/*
3880 		 * We only care about the ctime, but need to get mtime
3881 		 * and size for proper cache update.
3882 		 */
3883 		/* getattr */
3884 		argop[1].argop = OP_GETATTR;
3885 		argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3886 		argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3887 
3888 		/* verify - set later in loop */
3889 		verify_argop = 2;
3890 	}
3891 
3892 	/* setattr */
3893 	svp = rp->r_server;
3894 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3895 	supp_attrs = svp->sv_supp_attrs;
3896 	nfs_rw_exit(&svp->sv_lock);
3897 
3898 	nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3899 	    supp_attrs, &e.error, &sid_types);
3900 	stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3901 	if (e.error) {
3902 		/* req time field(s) overflow - return immediately */
3903 		nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3904 		nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3905 		    opsetattr.obj_attributes);
3906 		return (e.error);
3907 	}
3908 	omode = rp->r_attr.va_mode;
3909 
3910 	/* getattr */
3911 	argop[numops-1].argop = OP_GETATTR;
3912 	argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3913 	/*
3914 	 * If we are setting the ACL (indicated only by vsap != NULL), request
3915 	 * the ACL in this getattr.  The ACL returned from this getattr will be
3916 	 * used in updating the ACL cache.
3917 	 */
3918 	if (vsap != NULL)
3919 		argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3920 		    FATTR4_ACL_MASK;
3921 	argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3922 
3923 	/*
3924 	 * setattr iterates if the object size is set and the cached ctime
3925 	 * does not match the file ctime. In that case, verify the ctime first.
3926 	 */
3927 
3928 	do {
3929 		if (verify_argop != -1) {
3930 			/*
3931 			 * Verify that the ctime match before doing setattr.
3932 			 */
3933 			va.va_mask = AT_CTIME;
3934 			va.va_ctime = ctime;
3935 			svp = rp->r_server;
3936 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3937 			supp_attrs = svp->sv_supp_attrs;
3938 			nfs_rw_exit(&svp->sv_lock);
3939 			e.error = nfs4args_verify(&argop[verify_argop], &va,
3940 			    OP_VERIFY, supp_attrs);
3941 			if (e.error) {
3942 				/* req time field(s) overflow - return */
3943 				nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3944 				    needrecov);
3945 				break;
3946 			}
3947 		}
3948 
3949 		doqueue = 1;
3950 
3951 		t = gethrtime();
3952 
3953 		rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3954 
3955 		/*
3956 		 * Purge the access cache and ACL cache if changing either the
3957 		 * owner of the file, the group owner, or the mode.  These may
3958 		 * change the access permissions of the file, so purge old
3959 		 * information and start over again.
3960 		 */
3961 		if (mask & (AT_UID | AT_GID | AT_MODE)) {
3962 			(void) nfs4_access_purge_rp(rp);
3963 			if (rp->r_secattr != NULL) {
3964 				mutex_enter(&rp->r_statelock);
3965 				vsp = rp->r_secattr;
3966 				rp->r_secattr = NULL;
3967 				mutex_exit(&rp->r_statelock);
3968 				if (vsp != NULL)
3969 					nfs4_acl_free_cache(vsp);
3970 			}
3971 		}
3972 
3973 		/*
3974 		 * If res.array_len == numops, then everything succeeded,
3975 		 * except for possibly the final getattr.  If only the
3976 		 * last getattr failed, give up, and don't try recovery.
3977 		 */
3978 		if (res.array_len == numops) {
3979 			nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3980 			    needrecov);
3981 			if (! e.error)
3982 				resp = &res;
3983 			break;
3984 		}
3985 
3986 		/*
3987 		 * if either rpc call failed or completely succeeded - done
3988 		 */
3989 		needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3990 		if (e.error) {
3991 			PURGE_ATTRCACHE4(vp);
3992 			if (!needrecov) {
3993 				nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3994 				    needrecov);
3995 				break;
3996 			}
3997 		}
3998 
3999 		/*
4000 		 * Do proper retry for OLD_STATEID outside of the normal
4001 		 * recovery framework.
4002 		 */
4003 		if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4004 		    sid_types.cur_sid_type != SPEC_SID &&
4005 		    sid_types.cur_sid_type != NO_SID) {
4006 			nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4007 			    needrecov);
4008 			nfs4_save_stateid(&stateid, &sid_types);
4009 			nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4010 			    opsetattr.obj_attributes);
4011 			if (verify_argop != -1) {
4012 				nfs4args_verify_free(&argop[verify_argop]);
4013 				verify_argop = -1;
4014 			}
4015 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4016 			goto recov_retry;
4017 		}
4018 
4019 		if (needrecov) {
4020 			bool_t abort;
4021 
4022 			abort = nfs4_start_recovery(&e,
4023 			    VTOMI4(vp), vp, NULL, NULL, NULL,
4024 			    OP_SETATTR, NULL, NULL, NULL);
4025 			nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4026 			    needrecov);
4027 			/*
4028 			 * Do not retry if we failed with OLD_STATEID using
4029 			 * a special stateid.  This is done to avoid looping
4030 			 * with a broken server.
4031 			 */
4032 			if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4033 			    (sid_types.cur_sid_type == SPEC_SID ||
4034 			    sid_types.cur_sid_type == NO_SID))
4035 				abort = TRUE;
4036 			if (!e.error) {
4037 				if (res.status == NFS4ERR_BADOWNER)
4038 					nfs4_log_badowner(VTOMI4(vp),
4039 					    OP_SETATTR);
4040 
4041 				e.error = geterrno4(res.status);
4042 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4043 			}
4044 			nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4045 			    opsetattr.obj_attributes);
4046 			if (verify_argop != -1) {
4047 				nfs4args_verify_free(&argop[verify_argop]);
4048 				verify_argop = -1;
4049 			}
4050 			if (abort == FALSE) {
4051 				/*
4052 				 * Need to retry all possible stateids in
4053 				 * case the recovery error wasn't stateid
4054 				 * related or the stateids have become
4055 				 * stale (server reboot).
4056 				 */
4057 				nfs4_init_stateid_types(&sid_types);
4058 				goto recov_retry;
4059 			}
4060 			return (e.error);
4061 		}
4062 
4063 		/*
4064 		 * Need to call nfs4_end_op before nfs4getattr to
4065 		 * avoid potential nfs4_start_op deadlock. See RFE
4066 		 * 4777612.  Calls to nfs4_invalidate_pages() and
4067 		 * nfs4_purge_stale_fh() might also generate over the
4068 		 * wire calls which my cause nfs4_start_op() deadlock.
4069 		 */
4070 		nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4071 
4072 		/*
4073 		 * Check to update lease.
4074 		 */
4075 		resp = &res;
4076 		if (res.status == NFS4_OK) {
4077 			break;
4078 		}
4079 
4080 		/*
4081 		 * Check if verify failed to see if try again
4082 		 */
4083 		if ((verify_argop == -1) || (res.array_len != 3)) {
4084 			/*
4085 			 * can't continue...
4086 			 */
4087 			if (res.status == NFS4ERR_BADOWNER)
4088 				nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4089 
4090 			e.error = geterrno4(res.status);
4091 		} else {
4092 			/*
4093 			 * When the verify request fails, the client ctime is
4094 			 * not in sync with the server. This is the same as
4095 			 * the version 3 "not synchronized" error, and we
4096 			 * handle it in a similar manner (XXX do we need to???).
4097 			 * Use the ctime returned in the first getattr for
4098 			 * the input to the next verify.
4099 			 * If we couldn't get the attributes, then we give up
4100 			 * because we can't complete the operation as required.
4101 			 */
4102 			garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4103 		}
4104 		if (e.error) {
4105 			PURGE_ATTRCACHE4(vp);
4106 			nfs4_purge_stale_fh(e.error, vp, cr);
4107 		} else {
4108 			/*
4109 			 * retry with a new verify value
4110 			 */
4111 			ctime = garp->n4g_va.va_ctime;
4112 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4113 			resp = NULL;
4114 		}
4115 		if (!e.error) {
4116 			nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4117 			    opsetattr.obj_attributes);
4118 			if (verify_argop != -1) {
4119 				nfs4args_verify_free(&argop[verify_argop]);
4120 				verify_argop = -1;
4121 			}
4122 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4123 			goto do_again;
4124 		}
4125 	} while (!e.error);
4126 
4127 	if (e.error) {
4128 		/*
4129 		 * If we are here, rfs4call has an irrecoverable error - return
4130 		 */
4131 		nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4132 		    opsetattr.obj_attributes);
4133 		if (verify_argop != -1) {
4134 			nfs4args_verify_free(&argop[verify_argop]);
4135 			verify_argop = -1;
4136 		}
4137 		if (resp)
4138 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4139 		return (e.error);
4140 	}
4141 
4142 
4143 
4144 	/*
4145 	 * If changing the size of the file, invalidate
4146 	 * any local cached data which is no longer part
4147 	 * of the file.  We also possibly invalidate the
4148 	 * last page in the file.  We could use
4149 	 * pvn_vpzero(), but this would mark the page as
4150 	 * modified and require it to be written back to
4151 	 * the server for no particularly good reason.
4152 	 * This way, if we access it, then we bring it
4153 	 * back in.  A read should be cheaper than a
4154 	 * write.
4155 	 */
4156 	if (mask & AT_SIZE) {
4157 		nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4158 	}
4159 
4160 	/* either no error or one of the postop getattr failed */
4161 
4162 	/*
4163 	 * XXX Perform a simplified version of wcc checking. Instead of
4164 	 * have another getattr to get pre-op, just purge cache if
4165 	 * any of the ops prior to and including the getattr failed.
4166 	 * If the getattr succeeded then update the attrcache accordingly.
4167 	 */
4168 
4169 	garp = NULL;
4170 	if (res.status == NFS4_OK) {
4171 		/*
4172 		 * Last getattr
4173 		 */
4174 		resop = &res.array[numops - 1];
4175 		garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4176 	}
4177 	/*
4178 	 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4179 	 * rather than filling it.  See the function itself for details.
4180 	 */
4181 	e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4182 	if (garp != NULL) {
4183 		if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4184 			nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4185 			vs_ace4_destroy(&garp->n4g_vsa);
4186 		} else {
4187 			if (vsap != NULL) {
4188 				/*
4189 				 * The ACL was supposed to be set and to be
4190 				 * returned in the last getattr of this
4191 				 * compound, but for some reason the getattr
4192 				 * result doesn't contain the ACL.  In this
4193 				 * case, purge the ACL cache.
4194 				 */
4195 				if (rp->r_secattr != NULL) {
4196 					mutex_enter(&rp->r_statelock);
4197 					vsp = rp->r_secattr;
4198 					rp->r_secattr = NULL;
4199 					mutex_exit(&rp->r_statelock);
4200 					if (vsp != NULL)
4201 						nfs4_acl_free_cache(vsp);
4202 				}
4203 			}
4204 		}
4205 	}
4206 
4207 	if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4208 		/*
4209 		 * Set the size, rather than relying on getting it updated
4210 		 * via a GETATTR.  With delegations the client tries to
4211 		 * suppress GETATTR calls.
4212 		 */
4213 		mutex_enter(&rp->r_statelock);
4214 		rp->r_size = vap->va_size;
4215 		mutex_exit(&rp->r_statelock);
4216 	}
4217 
4218 	/*
4219 	 * Can free up request args and res
4220 	 */
4221 	nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4222 	    opsetattr.obj_attributes);
4223 	if (verify_argop != -1) {
4224 		nfs4args_verify_free(&argop[verify_argop]);
4225 		verify_argop = -1;
4226 	}
4227 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4228 
4229 	/*
4230 	 * Some servers will change the mode to clear the setuid
4231 	 * and setgid bits when changing the uid or gid.  The
4232 	 * client needs to compensate appropriately.
4233 	 */
4234 	if (mask & (AT_UID | AT_GID)) {
4235 		int terror, do_setattr;
4236 
4237 		do_setattr = 0;
4238 		va.va_mask = AT_MODE;
4239 		terror = nfs4getattr(vp, &va, cr);
4240 		if (!terror &&
4241 		    (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4242 		    (!(mask & AT_MODE) && va.va_mode != omode))) {
4243 			va.va_mask = AT_MODE;
4244 			if (mask & AT_MODE) {
4245 				/*
4246 				 * We asked the mode to be changed and what
4247 				 * we just got from the server in getattr is
4248 				 * not what we wanted it to be, so set it now.
4249 				 */
4250 				va.va_mode = vap->va_mode;
4251 				do_setattr = 1;
4252 			} else {
4253 				/*
4254 				 * We did not ask the mode to be changed,
4255 				 * Check to see that the server just cleared
4256 				 * I_SUID and I_GUID from it. If not then
4257 				 * set mode to omode with UID/GID cleared.
4258 				 */
4259 				if (nfs4_compare_modes(va.va_mode, omode)) {
4260 					omode &= ~(S_ISUID|S_ISGID);
4261 					va.va_mode = omode;
4262 					do_setattr = 1;
4263 				}
4264 			}
4265 
4266 			if (do_setattr)
4267 				(void) nfs4setattr(vp, &va, 0, cr, NULL);
4268 		}
4269 	}
4270 
4271 	return (e.error);
4272 }
4273 
4274 /* ARGSUSED */
4275 static int
nfs4_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)4276 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4277 {
4278 	COMPOUND4args_clnt args;
4279 	COMPOUND4res_clnt res;
4280 	int doqueue;
4281 	uint32_t acc, resacc, argacc;
4282 	rnode4_t *rp;
4283 	cred_t *cred, *ncr, *ncrfree = NULL;
4284 	nfs4_access_type_t cacc;
4285 	int num_ops;
4286 	nfs_argop4 argop[3];
4287 	nfs_resop4 *resop;
4288 	bool_t needrecov = FALSE, do_getattr;
4289 	nfs4_recov_state_t recov_state;
4290 	int rpc_error;
4291 	hrtime_t t;
4292 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4293 	mntinfo4_t *mi = VTOMI4(vp);
4294 
4295 	if (nfs_zone() != mi->mi_zone)
4296 		return (EIO);
4297 
4298 	acc = 0;
4299 	if (mode & VREAD)
4300 		acc |= ACCESS4_READ;
4301 	if (mode & VWRITE) {
4302 		if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4303 			return (EROFS);
4304 		if (vp->v_type == VDIR)
4305 			acc |= ACCESS4_DELETE;
4306 		acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4307 	}
4308 	if (mode & VEXEC) {
4309 		if (vp->v_type == VDIR)
4310 			acc |= ACCESS4_LOOKUP;
4311 		else
4312 			acc |= ACCESS4_EXECUTE;
4313 	}
4314 
4315 	if (VTOR4(vp)->r_acache != NULL) {
4316 		e.error = nfs4_validate_caches(vp, cr);
4317 		if (e.error)
4318 			return (e.error);
4319 	}
4320 
4321 	rp = VTOR4(vp);
4322 	if (vp->v_type == VDIR)
4323 		argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4324 		    ACCESS4_EXTEND | ACCESS4_LOOKUP;
4325 	else
4326 		argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4327 		    ACCESS4_EXECUTE;
4328 	recov_state.rs_flags = 0;
4329 	recov_state.rs_num_retry_despite_err = 0;
4330 
4331 	cred = cr;
4332 	/*
4333 	 * ncr and ncrfree both initially
4334 	 * point to the memory area returned
4335 	 * by crnetadjust();
4336 	 * ncrfree not NULL when exiting means
4337 	 * that we need to release it
4338 	 */
4339 	ncr = crnetadjust(cred);
4340 	ncrfree = ncr;
4341 
4342 tryagain:
4343 	cacc = nfs4_access_check(rp, acc, cred);
4344 	if (cacc == NFS4_ACCESS_ALLOWED) {
4345 		if (ncrfree != NULL)
4346 			crfree(ncrfree);
4347 		return (0);
4348 	}
4349 	if (cacc == NFS4_ACCESS_DENIED) {
4350 		/*
4351 		 * If the cred can be adjusted, try again
4352 		 * with the new cred.
4353 		 */
4354 		if (ncr != NULL) {
4355 			cred = ncr;
4356 			ncr = NULL;
4357 			goto tryagain;
4358 		}
4359 		if (ncrfree != NULL)
4360 			crfree(ncrfree);
4361 		return (EACCES);
4362 	}
4363 
4364 recov_retry:
4365 	/*
4366 	 * Don't take with r_statev4_lock here. r_deleg_type could
4367 	 * change as soon as lock is released.  Since it is an int,
4368 	 * there is no atomicity issue.
4369 	 */
4370 	do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4371 	num_ops = do_getattr ? 3 : 2;
4372 
4373 	args.ctag = TAG_ACCESS;
4374 
4375 	args.array_len = num_ops;
4376 	args.array = argop;
4377 
4378 	if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4379 	    &recov_state, NULL)) {
4380 		if (ncrfree != NULL)
4381 			crfree(ncrfree);
4382 		return (e.error);
4383 	}
4384 
4385 	/* putfh target fh */
4386 	argop[0].argop = OP_CPUTFH;
4387 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4388 
4389 	/* access */
4390 	argop[1].argop = OP_ACCESS;
4391 	argop[1].nfs_argop4_u.opaccess.access = argacc;
4392 
4393 	/* getattr */
4394 	if (do_getattr) {
4395 		argop[2].argop = OP_GETATTR;
4396 		argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4397 		argop[2].nfs_argop4_u.opgetattr.mi = mi;
4398 	}
4399 
4400 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4401 	    "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4402 	    rnode4info(VTOR4(vp))));
4403 
4404 	doqueue = 1;
4405 	t = gethrtime();
4406 	rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4407 	rpc_error = e.error;
4408 
4409 	needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4410 	if (needrecov) {
4411 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4412 		    "nfs4_access: initiating recovery\n"));
4413 
4414 		if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4415 		    NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4416 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4417 			    &recov_state, needrecov);
4418 			if (!e.error)
4419 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4420 			goto recov_retry;
4421 		}
4422 	}
4423 	nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4424 
4425 	if (e.error)
4426 		goto out;
4427 
4428 	if (res.status) {
4429 		e.error = geterrno4(res.status);
4430 		/*
4431 		 * This might generate over the wire calls throught
4432 		 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4433 		 * here to avoid a deadlock.
4434 		 */
4435 		nfs4_purge_stale_fh(e.error, vp, cr);
4436 		goto out;
4437 	}
4438 	resop = &res.array[1];	/* access res */
4439 
4440 	resacc = resop->nfs_resop4_u.opaccess.access;
4441 
4442 	if (do_getattr) {
4443 		resop++;	/* getattr res */
4444 		nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4445 		    t, cr, FALSE, NULL);
4446 	}
4447 
4448 	if (!e.error) {
4449 		nfs4_access_cache(rp, argacc, resacc, cred);
4450 		/*
4451 		 * we just cached results with cred; if cred is the
4452 		 * adjusted credentials from crnetadjust, we do not want
4453 		 * to release them before exiting: hence setting ncrfree
4454 		 * to NULL
4455 		 */
4456 		if (cred != cr)
4457 			ncrfree = NULL;
4458 		/* XXX check the supported bits too? */
4459 		if ((acc & resacc) != acc) {
4460 			/*
4461 			 * The following code implements the semantic
4462 			 * that a setuid root program has *at least* the
4463 			 * permissions of the user that is running the
4464 			 * program.  See rfs3call() for more portions
4465 			 * of the implementation of this functionality.
4466 			 */
4467 			/* XXX-LP */
4468 			if (ncr != NULL) {
4469 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4470 				cred = ncr;
4471 				ncr = NULL;
4472 				goto tryagain;
4473 			}
4474 			e.error = EACCES;
4475 		}
4476 	}
4477 
4478 out:
4479 	if (!rpc_error)
4480 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4481 
4482 	if (ncrfree != NULL)
4483 		crfree(ncrfree);
4484 
4485 	return (e.error);
4486 }
4487 
4488 /* ARGSUSED */
4489 static int
nfs4_readlink(vnode_t * vp,struct uio * uiop,cred_t * cr,caller_context_t * ct)4490 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4491 {
4492 	COMPOUND4args_clnt args;
4493 	COMPOUND4res_clnt res;
4494 	int doqueue;
4495 	rnode4_t *rp;
4496 	nfs_argop4 argop[3];
4497 	nfs_resop4 *resop;
4498 	READLINK4res *lr_res;
4499 	nfs4_ga_res_t *garp;
4500 	uint_t len;
4501 	char *linkdata;
4502 	bool_t needrecov = FALSE;
4503 	nfs4_recov_state_t recov_state;
4504 	hrtime_t t;
4505 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4506 
4507 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
4508 		return (EIO);
4509 	/*
4510 	 * Can't readlink anything other than a symbolic link.
4511 	 */
4512 	if (vp->v_type != VLNK)
4513 		return (EINVAL);
4514 
4515 	rp = VTOR4(vp);
4516 	if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4517 		e.error = nfs4_validate_caches(vp, cr);
4518 		if (e.error)
4519 			return (e.error);
4520 		mutex_enter(&rp->r_statelock);
4521 		if (rp->r_symlink.contents != NULL) {
4522 			e.error = uiomove(rp->r_symlink.contents,
4523 			    rp->r_symlink.len, UIO_READ, uiop);
4524 			mutex_exit(&rp->r_statelock);
4525 			return (e.error);
4526 		}
4527 		mutex_exit(&rp->r_statelock);
4528 	}
4529 	recov_state.rs_flags = 0;
4530 	recov_state.rs_num_retry_despite_err = 0;
4531 
4532 recov_retry:
4533 	args.array_len = 3;
4534 	args.array = argop;
4535 	args.ctag = TAG_READLINK;
4536 
4537 	e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4538 	if (e.error) {
4539 		return (e.error);
4540 	}
4541 
4542 	/* 0. putfh symlink fh */
4543 	argop[0].argop = OP_CPUTFH;
4544 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4545 
4546 	/* 1. readlink */
4547 	argop[1].argop = OP_READLINK;
4548 
4549 	/* 2. getattr */
4550 	argop[2].argop = OP_GETATTR;
4551 	argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4552 	argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4553 
4554 	doqueue = 1;
4555 
4556 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4557 	    "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4558 	    rnode4info(VTOR4(vp))));
4559 
4560 	t = gethrtime();
4561 
4562 	rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4563 
4564 	needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4565 	if (needrecov) {
4566 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4567 		    "nfs4_readlink: initiating recovery\n"));
4568 
4569 		if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4570 		    NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4571 			if (!e.error)
4572 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4573 
4574 			nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4575 			    needrecov);
4576 			goto recov_retry;
4577 		}
4578 	}
4579 
4580 	nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4581 
4582 	if (e.error)
4583 		return (e.error);
4584 
4585 	/*
4586 	 * There is an path in the code below which calls
4587 	 * nfs4_purge_stale_fh(), which may generate otw calls through
4588 	 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4589 	 * here to avoid nfs4_start_op() deadlock.
4590 	 */
4591 
4592 	if (res.status && (res.array_len < args.array_len)) {
4593 		/*
4594 		 * either Putfh or Link failed
4595 		 */
4596 		e.error = geterrno4(res.status);
4597 		nfs4_purge_stale_fh(e.error, vp, cr);
4598 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4599 		return (e.error);
4600 	}
4601 
4602 	resop = &res.array[1];	/* readlink res */
4603 	lr_res = &resop->nfs_resop4_u.opreadlink;
4604 
4605 	/*
4606 	 * treat symlink names as data
4607 	 */
4608 	linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4609 	if (linkdata != NULL) {
4610 		int uio_len = len - 1;
4611 		/* len includes null byte, which we won't uiomove */
4612 		e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4613 		if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4614 			mutex_enter(&rp->r_statelock);
4615 			if (rp->r_symlink.contents == NULL) {
4616 				rp->r_symlink.contents = linkdata;
4617 				rp->r_symlink.len = uio_len;
4618 				rp->r_symlink.size = len;
4619 				mutex_exit(&rp->r_statelock);
4620 			} else {
4621 				mutex_exit(&rp->r_statelock);
4622 				kmem_free(linkdata, len);
4623 			}
4624 		} else {
4625 			kmem_free(linkdata, len);
4626 		}
4627 	}
4628 	if (res.status == NFS4_OK) {
4629 		resop++;	/* getattr res */
4630 		garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4631 	}
4632 	e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4633 
4634 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4635 
4636 	/*
4637 	 * The over the wire error for attempting to readlink something
4638 	 * other than a symbolic link is ENXIO.  However, we need to
4639 	 * return EINVAL instead of ENXIO, so we map it here.
4640 	 */
4641 	return (e.error == ENXIO ? EINVAL : e.error);
4642 }
4643 
4644 /*
4645  * Flush local dirty pages to stable storage on the server.
4646  *
4647  * If FNODSYNC is specified, then there is nothing to do because
4648  * metadata changes are not cached on the client before being
4649  * sent to the server.
4650  */
4651 /* ARGSUSED */
4652 static int
nfs4_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)4653 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4654 {
4655 	int error;
4656 
4657 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4658 		return (0);
4659 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
4660 		return (EIO);
4661 	error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4662 	if (!error)
4663 		error = VTOR4(vp)->r_error;
4664 	return (error);
4665 }
4666 
4667 /*
4668  * Weirdness: if the file was removed or the target of a rename
4669  * operation while it was open, it got renamed instead.  Here we
4670  * remove the renamed file.
4671  */
4672 /* ARGSUSED */
4673 void
nfs4_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)4674 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4675 {
4676 	rnode4_t *rp;
4677 
4678 	ASSERT(vp != DNLC_NO_VNODE);
4679 
4680 	rp = VTOR4(vp);
4681 
4682 	if (IS_SHADOW(vp, rp)) {
4683 		sv_inactive(vp);
4684 		return;
4685 	}
4686 
4687 	/*
4688 	 * If this is coming from the wrong zone, we let someone in the right
4689 	 * zone take care of it asynchronously.  We can get here due to
4690 	 * VN_RELE() being called from pageout() or fsflush().  This call may
4691 	 * potentially turn into an expensive no-op if, for instance, v_count
4692 	 * gets incremented in the meantime, but it's still correct.
4693 	 */
4694 	if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4695 		nfs4_async_inactive(vp, cr);
4696 		return;
4697 	}
4698 
4699 	/*
4700 	 * Some of the cleanup steps might require over-the-wire
4701 	 * operations.  Since VOP_INACTIVE can get called as a result of
4702 	 * other over-the-wire operations (e.g., an attribute cache update
4703 	 * can lead to a DNLC purge), doing those steps now would lead to a
4704 	 * nested call to the recovery framework, which can deadlock.  So
4705 	 * do any over-the-wire cleanups asynchronously, in a separate
4706 	 * thread.
4707 	 */
4708 
4709 	mutex_enter(&rp->r_os_lock);
4710 	mutex_enter(&rp->r_statelock);
4711 	mutex_enter(&rp->r_statev4_lock);
4712 
4713 	if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4714 		mutex_exit(&rp->r_statev4_lock);
4715 		mutex_exit(&rp->r_statelock);
4716 		mutex_exit(&rp->r_os_lock);
4717 		nfs4_async_inactive(vp, cr);
4718 		return;
4719 	}
4720 
4721 	if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4722 	    rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4723 		mutex_exit(&rp->r_statev4_lock);
4724 		mutex_exit(&rp->r_statelock);
4725 		mutex_exit(&rp->r_os_lock);
4726 		nfs4_async_inactive(vp, cr);
4727 		return;
4728 	}
4729 
4730 	if (rp->r_unldvp != NULL) {
4731 		mutex_exit(&rp->r_statev4_lock);
4732 		mutex_exit(&rp->r_statelock);
4733 		mutex_exit(&rp->r_os_lock);
4734 		nfs4_async_inactive(vp, cr);
4735 		return;
4736 	}
4737 	mutex_exit(&rp->r_statev4_lock);
4738 	mutex_exit(&rp->r_statelock);
4739 	mutex_exit(&rp->r_os_lock);
4740 
4741 	rp4_addfree(rp, cr);
4742 }
4743 
4744 /*
4745  * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4746  * various bits of state.  The caller must not refer to vp after this call.
4747  */
4748 
4749 void
nfs4_inactive_otw(vnode_t * vp,cred_t * cr)4750 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4751 {
4752 	rnode4_t *rp = VTOR4(vp);
4753 	nfs4_recov_state_t recov_state;
4754 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4755 	vnode_t *unldvp;
4756 	char *unlname;
4757 	cred_t *unlcred;
4758 	COMPOUND4args_clnt args;
4759 	COMPOUND4res_clnt res, *resp;
4760 	nfs_argop4 argop[2];
4761 	int doqueue;
4762 #ifdef DEBUG
4763 	char *name;
4764 #endif
4765 
4766 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4767 	ASSERT(!IS_SHADOW(vp, rp));
4768 
4769 #ifdef DEBUG
4770 	name = fn_name(VTOSV(vp)->sv_name);
4771 	NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4772 	    "release vnode %s", name));
4773 	kmem_free(name, MAXNAMELEN);
4774 #endif
4775 
4776 	if (vp->v_type == VREG) {
4777 		bool_t recov_failed = FALSE;
4778 
4779 		e.error = nfs4close_all(vp, cr);
4780 		if (e.error) {
4781 			/* Check to see if recovery failed */
4782 			mutex_enter(&(VTOMI4(vp)->mi_lock));
4783 			if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4784 				recov_failed = TRUE;
4785 			mutex_exit(&(VTOMI4(vp)->mi_lock));
4786 			if (!recov_failed) {
4787 				mutex_enter(&rp->r_statelock);
4788 				if (rp->r_flags & R4RECOVERR)
4789 					recov_failed = TRUE;
4790 				mutex_exit(&rp->r_statelock);
4791 			}
4792 			if (recov_failed) {
4793 				NFS4_DEBUG(nfs4_client_recov_debug,
4794 				    (CE_NOTE, "nfs4_inactive_otw: "
4795 				    "close failed (recovery failure)"));
4796 			}
4797 		}
4798 	}
4799 
4800 redo:
4801 	if (rp->r_unldvp == NULL) {
4802 		rp4_addfree(rp, cr);
4803 		return;
4804 	}
4805 
4806 	/*
4807 	 * Save the vnode pointer for the directory where the
4808 	 * unlinked-open file got renamed, then set it to NULL
4809 	 * to prevent another thread from getting here before
4810 	 * we're done with the remove.  While we have the
4811 	 * statelock, make local copies of the pertinent rnode
4812 	 * fields.  If we weren't to do this in an atomic way, the
4813 	 * the unl* fields could become inconsistent with respect
4814 	 * to each other due to a race condition between this
4815 	 * code and nfs_remove().  See bug report 1034328.
4816 	 */
4817 	mutex_enter(&rp->r_statelock);
4818 	if (rp->r_unldvp == NULL) {
4819 		mutex_exit(&rp->r_statelock);
4820 		rp4_addfree(rp, cr);
4821 		return;
4822 	}
4823 
4824 	unldvp = rp->r_unldvp;
4825 	rp->r_unldvp = NULL;
4826 	unlname = rp->r_unlname;
4827 	rp->r_unlname = NULL;
4828 	unlcred = rp->r_unlcred;
4829 	rp->r_unlcred = NULL;
4830 	mutex_exit(&rp->r_statelock);
4831 
4832 	/*
4833 	 * If there are any dirty pages left, then flush
4834 	 * them.  This is unfortunate because they just
4835 	 * may get thrown away during the remove operation,
4836 	 * but we have to do this for correctness.
4837 	 */
4838 	if (nfs4_has_pages(vp) &&
4839 	    ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4840 		ASSERT(vp->v_type != VCHR);
4841 		e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4842 		if (e.error) {
4843 			mutex_enter(&rp->r_statelock);
4844 			if (!rp->r_error)
4845 				rp->r_error = e.error;
4846 			mutex_exit(&rp->r_statelock);
4847 		}
4848 	}
4849 
4850 	recov_state.rs_flags = 0;
4851 	recov_state.rs_num_retry_despite_err = 0;
4852 recov_retry_remove:
4853 	/*
4854 	 * Do the remove operation on the renamed file
4855 	 */
4856 	args.ctag = TAG_INACTIVE;
4857 
4858 	/*
4859 	 * Remove ops: putfh dir; remove
4860 	 */
4861 	args.array_len = 2;
4862 	args.array = argop;
4863 
4864 	e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4865 	if (e.error) {
4866 		kmem_free(unlname, MAXNAMELEN);
4867 		crfree(unlcred);
4868 		VN_RELE(unldvp);
4869 		/*
4870 		 * Try again; this time around r_unldvp will be NULL, so we'll
4871 		 * just call rp4_addfree() and return.
4872 		 */
4873 		goto redo;
4874 	}
4875 
4876 	/* putfh directory */
4877 	argop[0].argop = OP_CPUTFH;
4878 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4879 
4880 	/* remove */
4881 	argop[1].argop = OP_CREMOVE;
4882 	argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4883 
4884 	doqueue = 1;
4885 	resp = &res;
4886 
4887 #if 0 /* notyet */
4888 	/*
4889 	 * Can't do this yet.  We may be being called from
4890 	 * dnlc_purge_XXX while that routine is holding a
4891 	 * mutex lock to the nc_rele list.  The calls to
4892 	 * nfs3_cache_wcc_data may result in calls to
4893 	 * dnlc_purge_XXX.  This will result in a deadlock.
4894 	 */
4895 	rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4896 	if (e.error) {
4897 		PURGE_ATTRCACHE4(unldvp);
4898 		resp = NULL;
4899 	} else if (res.status) {
4900 		e.error = geterrno4(res.status);
4901 		PURGE_ATTRCACHE4(unldvp);
4902 		/*
4903 		 * This code is inactive right now
4904 		 * but if made active there should
4905 		 * be a nfs4_end_op() call before
4906 		 * nfs4_purge_stale_fh to avoid start_op()
4907 		 * deadlock. See BugId: 4948726
4908 		 */
4909 		nfs4_purge_stale_fh(error, unldvp, cr);
4910 	} else {
4911 		nfs_resop4 *resop;
4912 		REMOVE4res *rm_res;
4913 
4914 		resop = &res.array[1];
4915 		rm_res = &resop->nfs_resop4_u.opremove;
4916 		/*
4917 		 * Update directory cache attribute,
4918 		 * readdir and dnlc caches.
4919 		 */
4920 		nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4921 	}
4922 #else
4923 	rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4924 
4925 	PURGE_ATTRCACHE4(unldvp);
4926 #endif
4927 
4928 	if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4929 		if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4930 		    NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4931 			if (!e.error)
4932 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4933 			nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4934 			    &recov_state, TRUE);
4935 			goto recov_retry_remove;
4936 		}
4937 	}
4938 	nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4939 
4940 	/*
4941 	 * Release stuff held for the remove
4942 	 */
4943 	VN_RELE(unldvp);
4944 	if (!e.error && resp)
4945 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4946 
4947 	kmem_free(unlname, MAXNAMELEN);
4948 	crfree(unlcred);
4949 	goto redo;
4950 }
4951 
4952 /*
4953  * Remote file system operations having to do with directory manipulation.
4954  */
4955 /* ARGSUSED3 */
4956 int
nfs4_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)4957 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4958     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4959     int *direntflags, pathname_t *realpnp)
4960 {
4961 	int error;
4962 	vnode_t *vp, *avp = NULL;
4963 	rnode4_t *drp;
4964 
4965 	*vpp = NULL;
4966 	if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4967 		return (EPERM);
4968 	/*
4969 	 * if LOOKUP_XATTR, must replace dvp (object) with
4970 	 * object's attrdir before continuing with lookup
4971 	 */
4972 	if (flags & LOOKUP_XATTR) {
4973 		error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4974 		if (error)
4975 			return (error);
4976 
4977 		dvp = avp;
4978 
4979 		/*
4980 		 * If lookup is for "", just return dvp now.  The attrdir
4981 		 * has already been activated (from nfs4lookup_xattr), and
4982 		 * the caller will RELE the original dvp -- not
4983 		 * the attrdir.  So, set vpp and return.
4984 		 * Currently, when the LOOKUP_XATTR flag is
4985 		 * passed to VOP_LOOKUP, the name is always empty, and
4986 		 * shortcircuiting here avoids 3 unneeded lock/unlock
4987 		 * pairs.
4988 		 *
4989 		 * If a non-empty name was provided, then it is the
4990 		 * attribute name, and it will be looked up below.
4991 		 */
4992 		if (*nm == '\0') {
4993 			*vpp = dvp;
4994 			return (0);
4995 		}
4996 
4997 		/*
4998 		 * The vfs layer never sends a name when asking for the
4999 		 * attrdir, so we should never get here (unless of course
5000 		 * name is passed at some time in future -- at which time
5001 		 * we'll blow up here).
5002 		 */
5003 		ASSERT(0);
5004 	}
5005 
5006 	drp = VTOR4(dvp);
5007 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5008 		return (EINTR);
5009 
5010 	error = nfs4lookup(dvp, nm, vpp, cr, 0);
5011 	nfs_rw_exit(&drp->r_rwlock);
5012 
5013 	/*
5014 	 * If vnode is a device, create special vnode.
5015 	 */
5016 	if (!error && ISVDEV((*vpp)->v_type)) {
5017 		vp = *vpp;
5018 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5019 		VN_RELE(vp);
5020 	}
5021 
5022 	return (error);
5023 }
5024 
5025 /* ARGSUSED */
5026 static int
nfs4lookup_xattr(vnode_t * dvp,char * nm,vnode_t ** vpp,int flags,cred_t * cr)5027 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5028 {
5029 	int error;
5030 	rnode4_t *drp;
5031 	int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5032 	mntinfo4_t *mi;
5033 
5034 	mi = VTOMI4(dvp);
5035 	if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5036 	    !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5037 		return (EINVAL);
5038 
5039 	drp = VTOR4(dvp);
5040 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5041 		return (EINTR);
5042 
5043 	mutex_enter(&drp->r_statelock);
5044 	/*
5045 	 * If the server doesn't support xattrs just return EINVAL
5046 	 */
5047 	if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5048 		mutex_exit(&drp->r_statelock);
5049 		nfs_rw_exit(&drp->r_rwlock);
5050 		return (EINVAL);
5051 	}
5052 
5053 	/*
5054 	 * If there is a cached xattr directory entry,
5055 	 * use it as long as the attributes are valid. If the
5056 	 * attributes are not valid, take the simple approach and
5057 	 * free the cached value and re-fetch a new value.
5058 	 *
5059 	 * We don't negative entry cache for now, if we did we
5060 	 * would need to check if the file has changed on every
5061 	 * lookup. But xattrs don't exist very often and failing
5062 	 * an openattr is not much more expensive than and NVERIFY or GETATTR
5063 	 * so do an openattr over the wire for now.
5064 	 */
5065 	if (drp->r_xattr_dir != NULL) {
5066 		if (ATTRCACHE4_VALID(dvp)) {
5067 			VN_HOLD(drp->r_xattr_dir);
5068 			*vpp = drp->r_xattr_dir;
5069 			mutex_exit(&drp->r_statelock);
5070 			nfs_rw_exit(&drp->r_rwlock);
5071 			return (0);
5072 		}
5073 		VN_RELE(drp->r_xattr_dir);
5074 		drp->r_xattr_dir = NULL;
5075 	}
5076 	mutex_exit(&drp->r_statelock);
5077 
5078 	error = nfs4openattr(dvp, vpp, cflag, cr);
5079 
5080 	nfs_rw_exit(&drp->r_rwlock);
5081 
5082 	return (error);
5083 }
5084 
5085 static int
nfs4lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr,int skipdnlc)5086 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5087 {
5088 	int error;
5089 	rnode4_t *drp;
5090 
5091 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5092 
5093 	/*
5094 	 * If lookup is for "", just return dvp.  Don't need
5095 	 * to send it over the wire, look it up in the dnlc,
5096 	 * or perform any access checks.
5097 	 */
5098 	if (*nm == '\0') {
5099 		VN_HOLD(dvp);
5100 		*vpp = dvp;
5101 		return (0);
5102 	}
5103 
5104 	/*
5105 	 * Can't do lookups in non-directories.
5106 	 */
5107 	if (dvp->v_type != VDIR)
5108 		return (ENOTDIR);
5109 
5110 	/*
5111 	 * If lookup is for ".", just return dvp.  Don't need
5112 	 * to send it over the wire or look it up in the dnlc,
5113 	 * just need to check access.
5114 	 */
5115 	if (nm[0] == '.' && nm[1] == '\0') {
5116 		error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5117 		if (error)
5118 			return (error);
5119 		VN_HOLD(dvp);
5120 		*vpp = dvp;
5121 		return (0);
5122 	}
5123 
5124 	drp = VTOR4(dvp);
5125 	if (!(drp->r_flags & R4LOOKUP)) {
5126 		mutex_enter(&drp->r_statelock);
5127 		drp->r_flags |= R4LOOKUP;
5128 		mutex_exit(&drp->r_statelock);
5129 	}
5130 
5131 	*vpp = NULL;
5132 	/*
5133 	 * Lookup this name in the DNLC.  If there is no entry
5134 	 * lookup over the wire.
5135 	 */
5136 	if (!skipdnlc)
5137 		*vpp = dnlc_lookup(dvp, nm);
5138 	if (*vpp == NULL) {
5139 		/*
5140 		 * We need to go over the wire to lookup the name.
5141 		 */
5142 		return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5143 	}
5144 
5145 	/*
5146 	 * We hit on the dnlc
5147 	 */
5148 	if (*vpp != DNLC_NO_VNODE ||
5149 	    (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5150 		/*
5151 		 * But our attrs may not be valid.
5152 		 */
5153 		if (ATTRCACHE4_VALID(dvp)) {
5154 			error = nfs4_waitfor_purge_complete(dvp);
5155 			if (error) {
5156 				VN_RELE(*vpp);
5157 				*vpp = NULL;
5158 				return (error);
5159 			}
5160 
5161 			/*
5162 			 * If after the purge completes, check to make sure
5163 			 * our attrs are still valid.
5164 			 */
5165 			if (ATTRCACHE4_VALID(dvp)) {
5166 				/*
5167 				 * If we waited for a purge we may have
5168 				 * lost our vnode so look it up again.
5169 				 */
5170 				VN_RELE(*vpp);
5171 				*vpp = dnlc_lookup(dvp, nm);
5172 				if (*vpp == NULL)
5173 					return (nfs4lookupnew_otw(dvp,
5174 					    nm, vpp, cr));
5175 
5176 				/*
5177 				 * The access cache should almost always hit
5178 				 */
5179 				error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5180 
5181 				if (error) {
5182 					VN_RELE(*vpp);
5183 					*vpp = NULL;
5184 					return (error);
5185 				}
5186 				if (*vpp == DNLC_NO_VNODE) {
5187 					VN_RELE(*vpp);
5188 					*vpp = NULL;
5189 					return (ENOENT);
5190 				}
5191 				return (0);
5192 			}
5193 		}
5194 	}
5195 
5196 	ASSERT(*vpp != NULL);
5197 
5198 	/*
5199 	 * We may have gotten here we have one of the following cases:
5200 	 *	1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5201 	 *		need to validate them.
5202 	 *	2) vpp == DNLC_NO_VNODE, a negative entry that we always
5203 	 *		must validate.
5204 	 *
5205 	 * Go to the server and check if the directory has changed, if
5206 	 * it hasn't we are done and can use the dnlc entry.
5207 	 */
5208 	return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5209 }
5210 
5211 /*
5212  * Go to the server and check if the directory has changed, if
5213  * it hasn't we are done and can use the dnlc entry.  If it
5214  * has changed we get a new copy of its attributes and check
5215  * the access for VEXEC, then relookup the filename and
5216  * get its filehandle and attributes.
5217  *
5218  * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5219  *	if the NVERIFY failed we must
5220  *		purge the caches
5221  *		cache new attributes (will set r_time_attr_inval)
5222  *		cache new access
5223  *		recheck VEXEC access
5224  *		add name to dnlc, possibly negative
5225  *		if LOOKUP succeeded
5226  *			cache new attributes
5227  *	else
5228  *		set a new r_time_attr_inval for dvp
5229  *		check to make sure we have access
5230  *
5231  * The vpp returned is the vnode passed in if the directory is valid,
5232  * a new vnode if successful lookup, or NULL on error.
5233  */
5234 static int
nfs4lookupvalidate_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)5235 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5236 {
5237 	COMPOUND4args_clnt args;
5238 	COMPOUND4res_clnt res;
5239 	fattr4 *ver_fattr;
5240 	fattr4_change dchange;
5241 	int32_t *ptr;
5242 	int argoplist_size  = 7 * sizeof (nfs_argop4);
5243 	nfs_argop4 *argop;
5244 	int doqueue;
5245 	mntinfo4_t *mi;
5246 	nfs4_recov_state_t recov_state;
5247 	hrtime_t t;
5248 	int isdotdot;
5249 	vnode_t *nvp;
5250 	nfs_fh4 *fhp;
5251 	nfs4_sharedfh_t *sfhp;
5252 	nfs4_access_type_t cacc;
5253 	rnode4_t *nrp;
5254 	rnode4_t *drp = VTOR4(dvp);
5255 	nfs4_ga_res_t *garp = NULL;
5256 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5257 
5258 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5259 	ASSERT(nm != NULL);
5260 	ASSERT(nm[0] != '\0');
5261 	ASSERT(dvp->v_type == VDIR);
5262 	ASSERT(nm[0] != '.' || nm[1] != '\0');
5263 	ASSERT(*vpp != NULL);
5264 
5265 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5266 		isdotdot = 1;
5267 		args.ctag = TAG_LOOKUP_VPARENT;
5268 	} else {
5269 		/*
5270 		 * If dvp were a stub, it should have triggered and caused
5271 		 * a mount for us to get this far.
5272 		 */
5273 		ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5274 
5275 		isdotdot = 0;
5276 		args.ctag = TAG_LOOKUP_VALID;
5277 	}
5278 
5279 	mi = VTOMI4(dvp);
5280 	recov_state.rs_flags = 0;
5281 	recov_state.rs_num_retry_despite_err = 0;
5282 
5283 	nvp = NULL;
5284 
5285 	/* Save the original mount point security information */
5286 	(void) save_mnt_secinfo(mi->mi_curr_serv);
5287 
5288 recov_retry:
5289 	e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5290 	    &recov_state, NULL);
5291 	if (e.error) {
5292 		(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5293 		VN_RELE(*vpp);
5294 		*vpp = NULL;
5295 		return (e.error);
5296 	}
5297 
5298 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
5299 
5300 	/* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5301 	args.array_len = 7;
5302 	args.array = argop;
5303 
5304 	/* 0. putfh file */
5305 	argop[0].argop = OP_CPUTFH;
5306 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5307 
5308 	/* 1. nverify the change info */
5309 	argop[1].argop = OP_NVERIFY;
5310 	ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5311 	ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5312 	ver_fattr->attrlist4 = (char *)&dchange;
5313 	ptr = (int32_t *)&dchange;
5314 	IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5315 	ver_fattr->attrlist4_len = sizeof (fattr4_change);
5316 
5317 	/* 2. getattr directory */
5318 	argop[2].argop = OP_GETATTR;
5319 	argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5320 	argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5321 
5322 	/* 3. access directory */
5323 	argop[3].argop = OP_ACCESS;
5324 	argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5325 	    ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5326 
5327 	/* 4. lookup name */
5328 	if (isdotdot) {
5329 		argop[4].argop = OP_LOOKUPP;
5330 	} else {
5331 		argop[4].argop = OP_CLOOKUP;
5332 		argop[4].nfs_argop4_u.opclookup.cname = nm;
5333 	}
5334 
5335 	/* 5. resulting file handle */
5336 	argop[5].argop = OP_GETFH;
5337 
5338 	/* 6. resulting file attributes */
5339 	argop[6].argop = OP_GETATTR;
5340 	argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5341 	argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5342 
5343 	doqueue = 1;
5344 	t = gethrtime();
5345 
5346 	rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5347 
5348 	if (!isdotdot && res.status == NFS4ERR_MOVED) {
5349 		e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5350 		if (e.error != 0 && *vpp != NULL)
5351 			VN_RELE(*vpp);
5352 		nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5353 		    &recov_state, FALSE);
5354 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5355 		kmem_free(argop, argoplist_size);
5356 		return (e.error);
5357 	}
5358 
5359 	if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5360 		/*
5361 		 * For WRONGSEC of a non-dotdot case, send secinfo directly
5362 		 * from this thread, do not go thru the recovery thread since
5363 		 * we need the nm information.
5364 		 *
5365 		 * Not doing dotdot case because there is no specification
5366 		 * for (PUTFH, SECINFO "..") yet.
5367 		 */
5368 		if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5369 			if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5370 				nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5371 				    &recov_state, FALSE);
5372 			else
5373 				nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5374 				    &recov_state, TRUE);
5375 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5376 			kmem_free(argop, argoplist_size);
5377 			if (!e.error)
5378 				goto recov_retry;
5379 			(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5380 			VN_RELE(*vpp);
5381 			*vpp = NULL;
5382 			return (e.error);
5383 		}
5384 
5385 		if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5386 		    OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5387 			nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5388 			    &recov_state, TRUE);
5389 
5390 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5391 			kmem_free(argop, argoplist_size);
5392 			goto recov_retry;
5393 		}
5394 	}
5395 
5396 	nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5397 
5398 	if (e.error || res.array_len == 0) {
5399 		/*
5400 		 * If e.error isn't set, then reply has no ops (or we couldn't
5401 		 * be here).  The only legal way to reply without an op array
5402 		 * is via NFS4ERR_MINOR_VERS_MISMATCH.  An ops array should
5403 		 * be in the reply for all other status values.
5404 		 *
5405 		 * For valid replies without an ops array, return ENOTSUP
5406 		 * (geterrno4 xlation of VERS_MISMATCH).  For illegal replies,
5407 		 * return EIO -- don't trust status.
5408 		 */
5409 		if (e.error == 0)
5410 			e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5411 			    ENOTSUP : EIO;
5412 		VN_RELE(*vpp);
5413 		*vpp = NULL;
5414 		kmem_free(argop, argoplist_size);
5415 		(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5416 		return (e.error);
5417 	}
5418 
5419 	if (res.status != NFS4ERR_SAME) {
5420 		e.error = geterrno4(res.status);
5421 
5422 		/*
5423 		 * The NVERIFY "failed" so the directory has changed
5424 		 * First make sure PUTFH succeeded and NVERIFY "failed"
5425 		 * cleanly.
5426 		 */
5427 		if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5428 		    (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5429 			nfs4_purge_stale_fh(e.error, dvp, cr);
5430 			VN_RELE(*vpp);
5431 			*vpp = NULL;
5432 			goto exit;
5433 		}
5434 
5435 		/*
5436 		 * We know the NVERIFY "failed" so we must:
5437 		 *	purge the caches (access and indirectly dnlc if needed)
5438 		 */
5439 		nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5440 
5441 		if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5442 			nfs4_purge_stale_fh(e.error, dvp, cr);
5443 			VN_RELE(*vpp);
5444 			*vpp = NULL;
5445 			goto exit;
5446 		}
5447 
5448 		/*
5449 		 * Install new cached attributes for the directory
5450 		 */
5451 		nfs4_attr_cache(dvp,
5452 		    &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5453 		    t, cr, FALSE, NULL);
5454 
5455 		if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5456 			nfs4_purge_stale_fh(e.error, dvp, cr);
5457 			VN_RELE(*vpp);
5458 			*vpp = NULL;
5459 			e.error = geterrno4(res.status);
5460 			goto exit;
5461 		}
5462 
5463 		/*
5464 		 * Now we know the directory is valid,
5465 		 * cache new directory access
5466 		 */
5467 		nfs4_access_cache(drp,
5468 		    args.array[3].nfs_argop4_u.opaccess.access,
5469 		    res.array[3].nfs_resop4_u.opaccess.access, cr);
5470 
5471 		/*
5472 		 * recheck VEXEC access
5473 		 */
5474 		cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5475 		if (cacc != NFS4_ACCESS_ALLOWED) {
5476 			/*
5477 			 * Directory permissions might have been revoked
5478 			 */
5479 			if (cacc == NFS4_ACCESS_DENIED) {
5480 				e.error = EACCES;
5481 				VN_RELE(*vpp);
5482 				*vpp = NULL;
5483 				goto exit;
5484 			}
5485 
5486 			/*
5487 			 * Somehow we must not have asked for enough
5488 			 * so try a singleton ACCESS, should never happen.
5489 			 */
5490 			e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5491 			if (e.error) {
5492 				VN_RELE(*vpp);
5493 				*vpp = NULL;
5494 				goto exit;
5495 			}
5496 		}
5497 
5498 		e.error = geterrno4(res.status);
5499 		if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5500 			/*
5501 			 * The lookup failed, probably no entry
5502 			 */
5503 			if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5504 				dnlc_update(dvp, nm, DNLC_NO_VNODE);
5505 			} else {
5506 				/*
5507 				 * Might be some other error, so remove
5508 				 * the dnlc entry to make sure we start all
5509 				 * over again, next time.
5510 				 */
5511 				dnlc_remove(dvp, nm);
5512 			}
5513 			VN_RELE(*vpp);
5514 			*vpp = NULL;
5515 			goto exit;
5516 		}
5517 
5518 		if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5519 			/*
5520 			 * The file exists but we can't get its fh for
5521 			 * some unknown reason.  Remove it from the dnlc
5522 			 * and error out to be safe.
5523 			 */
5524 			dnlc_remove(dvp, nm);
5525 			VN_RELE(*vpp);
5526 			*vpp = NULL;
5527 			goto exit;
5528 		}
5529 		fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5530 		if (fhp->nfs_fh4_len == 0) {
5531 			/*
5532 			 * The file exists but a bogus fh
5533 			 * some unknown reason.  Remove it from the dnlc
5534 			 * and error out to be safe.
5535 			 */
5536 			e.error = ENOENT;
5537 			dnlc_remove(dvp, nm);
5538 			VN_RELE(*vpp);
5539 			*vpp = NULL;
5540 			goto exit;
5541 		}
5542 		sfhp = sfh4_get(fhp, mi);
5543 
5544 		if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5545 			garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5546 
5547 		/*
5548 		 * Make the new rnode
5549 		 */
5550 		if (isdotdot) {
5551 			e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5552 			if (e.error) {
5553 				sfh4_rele(&sfhp);
5554 				VN_RELE(*vpp);
5555 				*vpp = NULL;
5556 				goto exit;
5557 			}
5558 			/*
5559 			 * XXX if nfs4_make_dotdot uses an existing rnode
5560 			 * XXX it doesn't update the attributes.
5561 			 * XXX for now just save them again to save an OTW
5562 			 */
5563 			nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5564 		} else {
5565 			nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5566 			    dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5567 			/*
5568 			 * If v_type == VNON, then garp was NULL because
5569 			 * the last op in the compound failed and makenfs4node
5570 			 * could not find the vnode for sfhp. It created
5571 			 * a new vnode, so we have nothing to purge here.
5572 			 */
5573 			if (nvp->v_type == VNON) {
5574 				vattr_t vattr;
5575 
5576 				vattr.va_mask = AT_TYPE;
5577 				/*
5578 				 * N.B. We've already called nfs4_end_fop above.
5579 				 */
5580 				e.error = nfs4getattr(nvp, &vattr, cr);
5581 				if (e.error) {
5582 					sfh4_rele(&sfhp);
5583 					VN_RELE(*vpp);
5584 					*vpp = NULL;
5585 					VN_RELE(nvp);
5586 					goto exit;
5587 				}
5588 				nvp->v_type = vattr.va_type;
5589 			}
5590 		}
5591 		sfh4_rele(&sfhp);
5592 
5593 		nrp = VTOR4(nvp);
5594 		mutex_enter(&nrp->r_statev4_lock);
5595 		if (!nrp->created_v4) {
5596 			mutex_exit(&nrp->r_statev4_lock);
5597 			dnlc_update(dvp, nm, nvp);
5598 		} else
5599 			mutex_exit(&nrp->r_statev4_lock);
5600 
5601 		VN_RELE(*vpp);
5602 		*vpp = nvp;
5603 	} else {
5604 		hrtime_t now;
5605 		hrtime_t delta = 0;
5606 
5607 		e.error = 0;
5608 
5609 		/*
5610 		 * Because the NVERIFY "succeeded" we know that the
5611 		 * directory attributes are still valid
5612 		 * so update r_time_attr_inval
5613 		 */
5614 		now = gethrtime();
5615 		mutex_enter(&drp->r_statelock);
5616 		if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5617 			delta = now - drp->r_time_attr_saved;
5618 			if (delta < mi->mi_acdirmin)
5619 				delta = mi->mi_acdirmin;
5620 			else if (delta > mi->mi_acdirmax)
5621 				delta = mi->mi_acdirmax;
5622 		}
5623 		drp->r_time_attr_inval = now + delta;
5624 		mutex_exit(&drp->r_statelock);
5625 		dnlc_update(dvp, nm, *vpp);
5626 
5627 		/*
5628 		 * Even though we have a valid directory attr cache
5629 		 * and dnlc entry, we may not have access.
5630 		 * This should almost always hit the cache.
5631 		 */
5632 		e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5633 		if (e.error) {
5634 			VN_RELE(*vpp);
5635 			*vpp = NULL;
5636 		}
5637 
5638 		if (*vpp == DNLC_NO_VNODE) {
5639 			VN_RELE(*vpp);
5640 			*vpp = NULL;
5641 			e.error = ENOENT;
5642 		}
5643 	}
5644 
5645 exit:
5646 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5647 	kmem_free(argop, argoplist_size);
5648 	(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5649 	return (e.error);
5650 }
5651 
5652 /*
5653  * We need to go over the wire to lookup the name, but
5654  * while we are there verify the directory has not
5655  * changed but if it has, get new attributes and check access
5656  *
5657  * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5658  *					NVERIFY GETATTR ACCESS
5659  *
5660  * With the results:
5661  *	if the NVERIFY failed we must purge the caches, add new attributes,
5662  *		and cache new access.
5663  *	set a new r_time_attr_inval
5664  *	add name to dnlc, possibly negative
5665  *	if LOOKUP succeeded
5666  *		cache new attributes
5667  */
5668 static int
nfs4lookupnew_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)5669 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5670 {
5671 	COMPOUND4args_clnt args;
5672 	COMPOUND4res_clnt res;
5673 	fattr4 *ver_fattr;
5674 	fattr4_change dchange;
5675 	int32_t *ptr;
5676 	nfs4_ga_res_t *garp = NULL;
5677 	int argoplist_size  = 9 * sizeof (nfs_argop4);
5678 	nfs_argop4 *argop;
5679 	int doqueue;
5680 	mntinfo4_t *mi;
5681 	nfs4_recov_state_t recov_state;
5682 	hrtime_t t;
5683 	int isdotdot;
5684 	vnode_t *nvp;
5685 	nfs_fh4 *fhp;
5686 	nfs4_sharedfh_t *sfhp;
5687 	nfs4_access_type_t cacc;
5688 	rnode4_t *nrp;
5689 	rnode4_t *drp = VTOR4(dvp);
5690 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5691 
5692 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5693 	ASSERT(nm != NULL);
5694 	ASSERT(nm[0] != '\0');
5695 	ASSERT(dvp->v_type == VDIR);
5696 	ASSERT(nm[0] != '.' || nm[1] != '\0');
5697 	ASSERT(*vpp == NULL);
5698 
5699 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5700 		isdotdot = 1;
5701 		args.ctag = TAG_LOOKUP_PARENT;
5702 	} else {
5703 		/*
5704 		 * If dvp were a stub, it should have triggered and caused
5705 		 * a mount for us to get this far.
5706 		 */
5707 		ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5708 
5709 		isdotdot = 0;
5710 		args.ctag = TAG_LOOKUP;
5711 	}
5712 
5713 	mi = VTOMI4(dvp);
5714 	recov_state.rs_flags = 0;
5715 	recov_state.rs_num_retry_despite_err = 0;
5716 
5717 	nvp = NULL;
5718 
5719 	/* Save the original mount point security information */
5720 	(void) save_mnt_secinfo(mi->mi_curr_serv);
5721 
5722 recov_retry:
5723 	e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5724 	    &recov_state, NULL);
5725 	if (e.error) {
5726 		(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5727 		return (e.error);
5728 	}
5729 
5730 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
5731 
5732 	/* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5733 	args.array_len = 9;
5734 	args.array = argop;
5735 
5736 	/* 0. putfh file */
5737 	argop[0].argop = OP_CPUTFH;
5738 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5739 
5740 	/* 1. savefh for the nverify */
5741 	argop[1].argop = OP_SAVEFH;
5742 
5743 	/* 2. lookup name */
5744 	if (isdotdot) {
5745 		argop[2].argop = OP_LOOKUPP;
5746 	} else {
5747 		argop[2].argop = OP_CLOOKUP;
5748 		argop[2].nfs_argop4_u.opclookup.cname = nm;
5749 	}
5750 
5751 	/* 3. resulting file handle */
5752 	argop[3].argop = OP_GETFH;
5753 
5754 	/* 4. resulting file attributes */
5755 	argop[4].argop = OP_GETATTR;
5756 	argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5757 	argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5758 
5759 	/* 5. restorefh back the directory for the nverify */
5760 	argop[5].argop = OP_RESTOREFH;
5761 
5762 	/* 6. nverify the change info */
5763 	argop[6].argop = OP_NVERIFY;
5764 	ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5765 	ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5766 	ver_fattr->attrlist4 = (char *)&dchange;
5767 	ptr = (int32_t *)&dchange;
5768 	IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5769 	ver_fattr->attrlist4_len = sizeof (fattr4_change);
5770 
5771 	/* 7. getattr directory */
5772 	argop[7].argop = OP_GETATTR;
5773 	argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5774 	argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5775 
5776 	/* 8. access directory */
5777 	argop[8].argop = OP_ACCESS;
5778 	argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5779 	    ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5780 
5781 	doqueue = 1;
5782 	t = gethrtime();
5783 
5784 	rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5785 
5786 	if (!isdotdot && res.status == NFS4ERR_MOVED) {
5787 		e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5788 		if (e.error != 0 && *vpp != NULL)
5789 			VN_RELE(*vpp);
5790 		nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5791 		    &recov_state, FALSE);
5792 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5793 		kmem_free(argop, argoplist_size);
5794 		return (e.error);
5795 	}
5796 
5797 	if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5798 		/*
5799 		 * For WRONGSEC of a non-dotdot case, send secinfo directly
5800 		 * from this thread, do not go thru the recovery thread since
5801 		 * we need the nm information.
5802 		 *
5803 		 * Not doing dotdot case because there is no specification
5804 		 * for (PUTFH, SECINFO "..") yet.
5805 		 */
5806 		if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5807 			if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5808 				nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5809 				    &recov_state, FALSE);
5810 			else
5811 				nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5812 				    &recov_state, TRUE);
5813 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5814 			kmem_free(argop, argoplist_size);
5815 			if (!e.error)
5816 				goto recov_retry;
5817 			(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5818 			return (e.error);
5819 		}
5820 
5821 		if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5822 		    OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5823 			nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5824 			    &recov_state, TRUE);
5825 
5826 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5827 			kmem_free(argop, argoplist_size);
5828 			goto recov_retry;
5829 		}
5830 	}
5831 
5832 	nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5833 
5834 	if (e.error || res.array_len == 0) {
5835 		/*
5836 		 * If e.error isn't set, then reply has no ops (or we couldn't
5837 		 * be here).  The only legal way to reply without an op array
5838 		 * is via NFS4ERR_MINOR_VERS_MISMATCH.  An ops array should
5839 		 * be in the reply for all other status values.
5840 		 *
5841 		 * For valid replies without an ops array, return ENOTSUP
5842 		 * (geterrno4 xlation of VERS_MISMATCH).  For illegal replies,
5843 		 * return EIO -- don't trust status.
5844 		 */
5845 		if (e.error == 0)
5846 			e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5847 			    ENOTSUP : EIO;
5848 
5849 		kmem_free(argop, argoplist_size);
5850 		(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5851 		return (e.error);
5852 	}
5853 
5854 	e.error = geterrno4(res.status);
5855 
5856 	/*
5857 	 * The PUTFH and SAVEFH may have failed.
5858 	 */
5859 	if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5860 	    (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5861 		nfs4_purge_stale_fh(e.error, dvp, cr);
5862 		goto exit;
5863 	}
5864 
5865 	/*
5866 	 * Check if the file exists, if it does delay entering
5867 	 * into the dnlc until after we update the directory
5868 	 * attributes so we don't cause it to get purged immediately.
5869 	 */
5870 	if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5871 		/*
5872 		 * The lookup failed, probably no entry
5873 		 */
5874 		if (e.error == ENOENT && nfs4_lookup_neg_cache)
5875 			dnlc_update(dvp, nm, DNLC_NO_VNODE);
5876 		goto exit;
5877 	}
5878 
5879 	if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5880 		/*
5881 		 * The file exists but we can't get its fh for
5882 		 * some unknown reason. Error out to be safe.
5883 		 */
5884 		goto exit;
5885 	}
5886 
5887 	fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5888 	if (fhp->nfs_fh4_len == 0) {
5889 		/*
5890 		 * The file exists but a bogus fh
5891 		 * some unknown reason.  Error out to be safe.
5892 		 */
5893 		e.error = EIO;
5894 		goto exit;
5895 	}
5896 	sfhp = sfh4_get(fhp, mi);
5897 
5898 	if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5899 		sfh4_rele(&sfhp);
5900 		goto exit;
5901 	}
5902 	garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5903 
5904 	/*
5905 	 * The RESTOREFH may have failed
5906 	 */
5907 	if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5908 		sfh4_rele(&sfhp);
5909 		e.error = EIO;
5910 		goto exit;
5911 	}
5912 
5913 	if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5914 		/*
5915 		 * First make sure the NVERIFY failed as we expected,
5916 		 * if it didn't then be conservative and error out
5917 		 * as we can't trust the directory.
5918 		 */
5919 		if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5920 			sfh4_rele(&sfhp);
5921 			e.error = EIO;
5922 			goto exit;
5923 		}
5924 
5925 		/*
5926 		 * We know the NVERIFY "failed" so the directory has changed,
5927 		 * so we must:
5928 		 *	purge the caches (access and indirectly dnlc if needed)
5929 		 */
5930 		nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5931 
5932 		if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5933 			sfh4_rele(&sfhp);
5934 			goto exit;
5935 		}
5936 		nfs4_attr_cache(dvp,
5937 		    &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5938 		    t, cr, FALSE, NULL);
5939 
5940 		if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5941 			nfs4_purge_stale_fh(e.error, dvp, cr);
5942 			sfh4_rele(&sfhp);
5943 			e.error = geterrno4(res.status);
5944 			goto exit;
5945 		}
5946 
5947 		/*
5948 		 * Now we know the directory is valid,
5949 		 * cache new directory access
5950 		 */
5951 		nfs4_access_cache(drp,
5952 		    args.array[8].nfs_argop4_u.opaccess.access,
5953 		    res.array[8].nfs_resop4_u.opaccess.access, cr);
5954 
5955 		/*
5956 		 * recheck VEXEC access
5957 		 */
5958 		cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5959 		if (cacc != NFS4_ACCESS_ALLOWED) {
5960 			/*
5961 			 * Directory permissions might have been revoked
5962 			 */
5963 			if (cacc == NFS4_ACCESS_DENIED) {
5964 				sfh4_rele(&sfhp);
5965 				e.error = EACCES;
5966 				goto exit;
5967 			}
5968 
5969 			/*
5970 			 * Somehow we must not have asked for enough
5971 			 * so try a singleton ACCESS should never happen
5972 			 */
5973 			e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5974 			if (e.error) {
5975 				sfh4_rele(&sfhp);
5976 				goto exit;
5977 			}
5978 		}
5979 
5980 		e.error = geterrno4(res.status);
5981 	} else {
5982 		hrtime_t now;
5983 		hrtime_t delta = 0;
5984 
5985 		e.error = 0;
5986 
5987 		/*
5988 		 * Because the NVERIFY "succeeded" we know that the
5989 		 * directory attributes are still valid
5990 		 * so update r_time_attr_inval
5991 		 */
5992 		now = gethrtime();
5993 		mutex_enter(&drp->r_statelock);
5994 		if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5995 			delta = now - drp->r_time_attr_saved;
5996 			if (delta < mi->mi_acdirmin)
5997 				delta = mi->mi_acdirmin;
5998 			else if (delta > mi->mi_acdirmax)
5999 				delta = mi->mi_acdirmax;
6000 		}
6001 		drp->r_time_attr_inval = now + delta;
6002 		mutex_exit(&drp->r_statelock);
6003 
6004 		/*
6005 		 * Even though we have a valid directory attr cache,
6006 		 * we may not have access.
6007 		 * This should almost always hit the cache.
6008 		 */
6009 		e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
6010 		if (e.error) {
6011 			sfh4_rele(&sfhp);
6012 			goto exit;
6013 		}
6014 	}
6015 
6016 	/*
6017 	 * Now we have successfully completed the lookup, if the
6018 	 * directory has changed we now have the valid attributes.
6019 	 * We also know we have directory access.
6020 	 * Create the new rnode and insert it in the dnlc.
6021 	 */
6022 	if (isdotdot) {
6023 		e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6024 		if (e.error) {
6025 			sfh4_rele(&sfhp);
6026 			goto exit;
6027 		}
6028 		/*
6029 		 * XXX if nfs4_make_dotdot uses an existing rnode
6030 		 * XXX it doesn't update the attributes.
6031 		 * XXX for now just save them again to save an OTW
6032 		 */
6033 		nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6034 	} else {
6035 		nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6036 		    dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6037 	}
6038 	sfh4_rele(&sfhp);
6039 
6040 	nrp = VTOR4(nvp);
6041 	mutex_enter(&nrp->r_statev4_lock);
6042 	if (!nrp->created_v4) {
6043 		mutex_exit(&nrp->r_statev4_lock);
6044 		dnlc_update(dvp, nm, nvp);
6045 	} else
6046 		mutex_exit(&nrp->r_statev4_lock);
6047 
6048 	*vpp = nvp;
6049 
6050 exit:
6051 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6052 	kmem_free(argop, argoplist_size);
6053 	(void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6054 	return (e.error);
6055 }
6056 
6057 #ifdef DEBUG
6058 void
nfs4lookup_dump_compound(char * where,nfs_argop4 * argbase,int argcnt)6059 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6060 {
6061 	uint_t i, len;
6062 	zoneid_t zoneid = getzoneid();
6063 	char *s;
6064 
6065 	zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6066 	for (i = 0; i < argcnt; i++) {
6067 		nfs_argop4 *op = &argbase[i];
6068 		switch (op->argop) {
6069 		case OP_CPUTFH:
6070 		case OP_PUTFH:
6071 			zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6072 			break;
6073 		case OP_PUTROOTFH:
6074 			zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6075 			break;
6076 		case OP_CLOOKUP:
6077 			s = op->nfs_argop4_u.opclookup.cname;
6078 			zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6079 			break;
6080 		case OP_LOOKUP:
6081 			s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6082 			    &len, NULL);
6083 			zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6084 			kmem_free(s, len);
6085 			break;
6086 		case OP_LOOKUPP:
6087 			zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6088 			break;
6089 		case OP_GETFH:
6090 			zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6091 			break;
6092 		case OP_GETATTR:
6093 			zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6094 			break;
6095 		case OP_OPENATTR:
6096 			zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6097 			break;
6098 		default:
6099 			zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6100 			    op->argop);
6101 			break;
6102 		}
6103 	}
6104 }
6105 #endif
6106 
6107 /*
6108  * nfs4lookup_setup - constructs a multi-lookup compound request.
6109  *
6110  * Given the path "nm1/nm2/.../nmn", the following compound requests
6111  * may be created:
6112  *
6113  * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6114  * is faster, for now.
6115  *
6116  * l4_getattrs indicates the type of compound requested.
6117  *
6118  * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6119  *
6120  *	compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ...  Lookup {nmn} }
6121  *
6122  *   total number of ops is n + 1.
6123  *
6124  * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6125  *      attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6126  *      before the last component, and only get attributes
6127  *      for the last component.  Note that the second-to-last
6128  *	pathname component is XATTR_RPATH, which does NOT go
6129  *	over-the-wire as a lookup.
6130  *
6131  *      compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6132  *		Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6133  *
6134  *   and total number of ops is n + 5.
6135  *
6136  * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6137  *      attribute directory: create lookups plus an OPENATTR
6138  *	replacing the last lookup.  Note that the last pathname
6139  *	component is XATTR_RPATH, which does NOT go over-the-wire
6140  *	as a lookup.
6141  *
6142  *      compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6143  *		Openattr; Getfh; Getattr }
6144  *
6145  *   and total number of ops is n + 5.
6146  *
6147  * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6148  *	nodes too.
6149  *
6150  *	compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6151  *		Lookup {nm2}; ...  Lookup {nmn}; Getfh; Getattr }
6152  *
6153  *   and total number of ops is 3*n + 1.
6154  *
6155  * All cases: returns the index in the arg array of the final LOOKUP op, or
6156  * -1 if no LOOKUPs were used.
6157  */
6158 int
nfs4lookup_setup(char * nm,lookup4_param_t * lookupargp,int needgetfh)6159 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6160 {
6161 	enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6162 	nfs_argop4 *argbase, *argop;
6163 	int arglen, argcnt;
6164 	int n = 1;	/* number of components */
6165 	int nga = 1;	/* number of Getattr's in request */
6166 	char c = '\0', *s, *p;
6167 	int lookup_idx = -1;
6168 	int argoplist_size;
6169 
6170 	/* set lookuparg response result to 0 */
6171 	lookupargp->resp->status = NFS4_OK;
6172 
6173 	/* skip leading "/" or "." e.g. ".//./" if there is */
6174 	for (; ; nm++) {
6175 		if (*nm != '/' && *nm != '.')
6176 			break;
6177 
6178 		/* ".." is counted as 1 component */
6179 		if (*nm == '.' && *(nm + 1) != '/')
6180 			break;
6181 	}
6182 
6183 	/*
6184 	 * Find n = number of components - nm must be null terminated
6185 	 * Skip "." components.
6186 	 */
6187 	if (*nm != '\0')
6188 		for (n = 1, s = nm; *s != '\0'; s++) {
6189 			if ((*s == '/') && (*(s + 1) != '/') &&
6190 			    (*(s + 1) != '\0') &&
6191 			    !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6192 			    *(s + 2) == '\0')))
6193 				n++;
6194 		}
6195 	else
6196 		n = 0;
6197 
6198 	/*
6199 	 * nga is number of components that need Getfh+Getattr
6200 	 */
6201 	switch (l4_getattrs) {
6202 	case LKP4_NO_ATTRIBUTES:
6203 		nga = 0;
6204 		break;
6205 	case LKP4_ALL_ATTRIBUTES:
6206 		nga = n;
6207 		/*
6208 		 * Always have at least 1 getfh, getattr pair
6209 		 */
6210 		if (nga == 0)
6211 			nga++;
6212 		break;
6213 	case LKP4_LAST_ATTRDIR:
6214 	case LKP4_LAST_NAMED_ATTR:
6215 		nga = n+1;
6216 		break;
6217 	}
6218 
6219 	/*
6220 	 * If change to use the filehandle attr instead of getfh
6221 	 * the following line can be deleted.
6222 	 */
6223 	nga *= 2;
6224 
6225 	/*
6226 	 * calculate number of ops in request as
6227 	 * header + trailer + lookups + getattrs
6228 	 */
6229 	arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6230 
6231 	argoplist_size = arglen * sizeof (nfs_argop4);
6232 	argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6233 	lookupargp->argsp->array = argop;
6234 
6235 	argcnt = lookupargp->header_len;
6236 	argop += argcnt;
6237 
6238 	/*
6239 	 * loop and create a lookup op and possibly getattr/getfh for
6240 	 * each component. Skip "." components.
6241 	 */
6242 	for (s = nm; *s != '\0'; s = p) {
6243 		/*
6244 		 * Set up a pathname struct for each component if needed
6245 		 */
6246 		while (*s == '/')
6247 			s++;
6248 		if (*s == '\0')
6249 			break;
6250 
6251 		for (p = s; (*p != '/') && (*p != '\0'); p++)
6252 			;
6253 		c = *p;
6254 		*p = '\0';
6255 
6256 		if (s[0] == '.' && s[1] == '\0') {
6257 			*p = c;
6258 			continue;
6259 		}
6260 		if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6261 		    strcmp(s, XATTR_RPATH) == 0) {
6262 			/* getfh XXX may not be needed in future */
6263 			argop->argop = OP_GETFH;
6264 			argop++;
6265 			argcnt++;
6266 
6267 			/* getattr */
6268 			argop->argop = OP_GETATTR;
6269 			argop->nfs_argop4_u.opgetattr.attr_request =
6270 			    lookupargp->ga_bits;
6271 			argop->nfs_argop4_u.opgetattr.mi =
6272 			    lookupargp->mi;
6273 			argop++;
6274 			argcnt++;
6275 
6276 			/* openattr */
6277 			argop->argop = OP_OPENATTR;
6278 		} else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6279 		    strcmp(s, XATTR_RPATH) == 0) {
6280 			/* openattr */
6281 			argop->argop = OP_OPENATTR;
6282 			argop++;
6283 			argcnt++;
6284 
6285 			/* getfh XXX may not be needed in future */
6286 			argop->argop = OP_GETFH;
6287 			argop++;
6288 			argcnt++;
6289 
6290 			/* getattr */
6291 			argop->argop = OP_GETATTR;
6292 			argop->nfs_argop4_u.opgetattr.attr_request =
6293 			    lookupargp->ga_bits;
6294 			argop->nfs_argop4_u.opgetattr.mi =
6295 			    lookupargp->mi;
6296 			argop++;
6297 			argcnt++;
6298 			*p = c;
6299 			continue;
6300 		} else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6301 			/* lookupp */
6302 			argop->argop = OP_LOOKUPP;
6303 		} else {
6304 			/* lookup */
6305 			argop->argop = OP_LOOKUP;
6306 			(void) str_to_utf8(s,
6307 			    &argop->nfs_argop4_u.oplookup.objname);
6308 		}
6309 		lookup_idx = argcnt;
6310 		argop++;
6311 		argcnt++;
6312 
6313 		*p = c;
6314 
6315 		if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6316 			/* getfh XXX may not be needed in future */
6317 			argop->argop = OP_GETFH;
6318 			argop++;
6319 			argcnt++;
6320 
6321 			/* getattr */
6322 			argop->argop = OP_GETATTR;
6323 			argop->nfs_argop4_u.opgetattr.attr_request =
6324 			    lookupargp->ga_bits;
6325 			argop->nfs_argop4_u.opgetattr.mi =
6326 			    lookupargp->mi;
6327 			argop++;
6328 			argcnt++;
6329 		}
6330 	}
6331 
6332 	if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6333 	    ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6334 		if (needgetfh) {
6335 			/* stick in a post-lookup getfh */
6336 			argop->argop = OP_GETFH;
6337 			argcnt++;
6338 			argop++;
6339 		}
6340 		/* post-lookup getattr */
6341 		argop->argop = OP_GETATTR;
6342 		argop->nfs_argop4_u.opgetattr.attr_request =
6343 		    lookupargp->ga_bits;
6344 		argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6345 		argcnt++;
6346 	}
6347 	argcnt += lookupargp->trailer_len;	/* actual op count */
6348 	lookupargp->argsp->array_len = argcnt;
6349 	lookupargp->arglen = arglen;
6350 
6351 #ifdef DEBUG
6352 	if (nfs4_client_lookup_debug)
6353 		nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6354 #endif
6355 
6356 	return (lookup_idx);
6357 }
6358 
6359 static int
nfs4openattr(vnode_t * dvp,vnode_t ** avp,int cflag,cred_t * cr)6360 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6361 {
6362 	COMPOUND4args_clnt	args;
6363 	COMPOUND4res_clnt	res;
6364 	GETFH4res	*gf_res = NULL;
6365 	nfs_argop4	argop[4];
6366 	nfs_resop4	*resop = NULL;
6367 	nfs4_sharedfh_t *sfhp;
6368 	hrtime_t t;
6369 	nfs4_error_t	e;
6370 
6371 	rnode4_t	*drp;
6372 	int		doqueue = 1;
6373 	vnode_t		*vp;
6374 	int		needrecov = 0;
6375 	nfs4_recov_state_t recov_state;
6376 
6377 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6378 
6379 	*avp = NULL;
6380 	recov_state.rs_flags = 0;
6381 	recov_state.rs_num_retry_despite_err = 0;
6382 
6383 recov_retry:
6384 	/* COMPOUND: putfh, openattr, getfh, getattr */
6385 	args.array_len = 4;
6386 	args.array = argop;
6387 	args.ctag = TAG_OPENATTR;
6388 
6389 	e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6390 	if (e.error)
6391 		return (e.error);
6392 
6393 	drp = VTOR4(dvp);
6394 
6395 	/* putfh */
6396 	argop[0].argop = OP_CPUTFH;
6397 	argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6398 
6399 	/* openattr */
6400 	argop[1].argop = OP_OPENATTR;
6401 	argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6402 
6403 	/* getfh */
6404 	argop[2].argop = OP_GETFH;
6405 
6406 	/* getattr */
6407 	argop[3].argop = OP_GETATTR;
6408 	argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6409 	argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6410 
6411 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6412 	    "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6413 	    rnode4info(drp)));
6414 
6415 	t = gethrtime();
6416 
6417 	rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6418 
6419 	needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6420 	if (needrecov) {
6421 		bool_t abort;
6422 
6423 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6424 		    "nfs4openattr: initiating recovery\n"));
6425 
6426 		abort = nfs4_start_recovery(&e,
6427 		    VTOMI4(dvp), dvp, NULL, NULL, NULL,
6428 		    OP_OPENATTR, NULL, NULL, NULL);
6429 		nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6430 		if (!e.error) {
6431 			e.error = geterrno4(res.status);
6432 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6433 		}
6434 		if (abort == FALSE)
6435 			goto recov_retry;
6436 		return (e.error);
6437 	}
6438 
6439 	if (e.error) {
6440 		nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6441 		return (e.error);
6442 	}
6443 
6444 	if (res.status) {
6445 		/*
6446 		 * If OTW errro is NOTSUPP, then it should be
6447 		 * translated to EINVAL.  All Solaris file system
6448 		 * implementations return EINVAL to the syscall layer
6449 		 * when the attrdir cannot be created due to an
6450 		 * implementation restriction or noxattr mount option.
6451 		 */
6452 		if (res.status == NFS4ERR_NOTSUPP) {
6453 			mutex_enter(&drp->r_statelock);
6454 			if (drp->r_xattr_dir)
6455 				VN_RELE(drp->r_xattr_dir);
6456 			VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6457 			drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6458 			mutex_exit(&drp->r_statelock);
6459 
6460 			e.error = EINVAL;
6461 		} else {
6462 			e.error = geterrno4(res.status);
6463 		}
6464 
6465 		if (e.error) {
6466 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6467 			nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6468 			    needrecov);
6469 			return (e.error);
6470 		}
6471 	}
6472 
6473 	resop = &res.array[0];  /* putfh res */
6474 	ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6475 
6476 	resop = &res.array[1];  /* openattr res */
6477 	ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6478 
6479 	resop = &res.array[2];  /* getfh res */
6480 	gf_res = &resop->nfs_resop4_u.opgetfh;
6481 	if (gf_res->object.nfs_fh4_len == 0) {
6482 		*avp = NULL;
6483 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6484 		nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6485 		return (ENOENT);
6486 	}
6487 
6488 	sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6489 	vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6490 	    dvp->v_vfsp, t, cr, dvp,
6491 	    fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6492 	sfh4_rele(&sfhp);
6493 
6494 	if (e.error)
6495 		PURGE_ATTRCACHE4(vp);
6496 
6497 	mutex_enter(&vp->v_lock);
6498 	vp->v_flag |= V_XATTRDIR;
6499 	mutex_exit(&vp->v_lock);
6500 
6501 	*avp = vp;
6502 
6503 	mutex_enter(&drp->r_statelock);
6504 	if (drp->r_xattr_dir)
6505 		VN_RELE(drp->r_xattr_dir);
6506 	VN_HOLD(vp);
6507 	drp->r_xattr_dir = vp;
6508 
6509 	/*
6510 	 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6511 	 * NULL.  xattrs could be created at any time, and we have no
6512 	 * way to update pc4_xattr_exists in the base object if/when
6513 	 * it happens.
6514 	 */
6515 	drp->r_pathconf.pc4_xattr_valid = 0;
6516 
6517 	mutex_exit(&drp->r_statelock);
6518 
6519 	nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6520 
6521 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6522 
6523 	return (0);
6524 }
6525 
6526 /* ARGSUSED */
6527 static int
nfs4_create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int flags,caller_context_t * ct,vsecattr_t * vsecp)6528 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6529     int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6530     vsecattr_t *vsecp)
6531 {
6532 	int error;
6533 	vnode_t *vp = NULL;
6534 	rnode4_t *rp;
6535 	struct vattr vattr;
6536 	rnode4_t *drp;
6537 	vnode_t *tempvp;
6538 	enum createmode4 createmode;
6539 	bool_t must_trunc = FALSE;
6540 	int	truncating = 0;
6541 
6542 	if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6543 		return (EPERM);
6544 	if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6545 		return (EINVAL);
6546 	}
6547 
6548 	/* . and .. have special meaning in the protocol, reject them. */
6549 
6550 	if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6551 		return (EISDIR);
6552 
6553 	drp = VTOR4(dvp);
6554 
6555 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6556 		return (EINTR);
6557 
6558 top:
6559 	/*
6560 	 * We make a copy of the attributes because the caller does not
6561 	 * expect us to change what va points to.
6562 	 */
6563 	vattr = *va;
6564 
6565 	/*
6566 	 * If the pathname is "", then dvp is the root vnode of
6567 	 * a remote file mounted over a local directory.
6568 	 * All that needs to be done is access
6569 	 * checking and truncation.  Note that we avoid doing
6570 	 * open w/ create because the parent directory might
6571 	 * be in pseudo-fs and the open would fail.
6572 	 */
6573 	if (*nm == '\0') {
6574 		error = 0;
6575 		VN_HOLD(dvp);
6576 		vp = dvp;
6577 		must_trunc = TRUE;
6578 	} else {
6579 		/*
6580 		 * We need to go over the wire, just to be sure whether the
6581 		 * file exists or not.  Using the DNLC can be dangerous in
6582 		 * this case when making a decision regarding existence.
6583 		 */
6584 		error = nfs4lookup(dvp, nm, &vp, cr, 1);
6585 	}
6586 
6587 	if (exclusive)
6588 		createmode = EXCLUSIVE4;
6589 	else
6590 		createmode = GUARDED4;
6591 
6592 	/*
6593 	 * error would be set if the file does not exist on the
6594 	 * server, so lets go create it.
6595 	 */
6596 	if (error) {
6597 		goto create_otw;
6598 	}
6599 
6600 	/*
6601 	 * File does exist on the server
6602 	 */
6603 	if (exclusive == EXCL)
6604 		error = EEXIST;
6605 	else if (vp->v_type == VDIR && (mode & VWRITE))
6606 		error = EISDIR;
6607 	else {
6608 		/*
6609 		 * If vnode is a device, create special vnode.
6610 		 */
6611 		if (ISVDEV(vp->v_type)) {
6612 			tempvp = vp;
6613 			vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6614 			VN_RELE(tempvp);
6615 		}
6616 		if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6617 			if ((vattr.va_mask & AT_SIZE) &&
6618 			    vp->v_type == VREG) {
6619 				rp = VTOR4(vp);
6620 				/*
6621 				 * Check here for large file handled
6622 				 * by LF-unaware process (as
6623 				 * ufs_create() does)
6624 				 */
6625 				if (!(flags & FOFFMAX)) {
6626 					mutex_enter(&rp->r_statelock);
6627 					if (rp->r_size > MAXOFF32_T)
6628 						error = EOVERFLOW;
6629 					mutex_exit(&rp->r_statelock);
6630 				}
6631 
6632 				/* if error is set then we need to return */
6633 				if (error) {
6634 					nfs_rw_exit(&drp->r_rwlock);
6635 					VN_RELE(vp);
6636 					return (error);
6637 				}
6638 
6639 				if (must_trunc) {
6640 					vattr.va_mask = AT_SIZE;
6641 					error = nfs4setattr(vp, &vattr, 0, cr,
6642 					    NULL);
6643 				} else {
6644 				/*
6645 				 * we know we have a regular file that already
6646 				 * exists and we may end up truncating the file
6647 				 * as a result of the open_otw, so flush out
6648 				 * any dirty pages for this file first.
6649 				 */
6650 					if (nfs4_has_pages(vp) &&
6651 					    ((rp->r_flags & R4DIRTY) ||
6652 					    rp->r_count > 0 ||
6653 					    rp->r_mapcnt > 0)) {
6654 						error = nfs4_putpage(vp,
6655 						    (offset_t)0, 0, 0, cr, ct);
6656 						if (error && (error == ENOSPC ||
6657 						    error == EDQUOT)) {
6658 							mutex_enter(
6659 							    &rp->r_statelock);
6660 							if (!rp->r_error)
6661 								rp->r_error =
6662 								    error;
6663 							mutex_exit(
6664 							    &rp->r_statelock);
6665 						}
6666 					}
6667 					vattr.va_mask = (AT_SIZE |
6668 					    AT_TYPE | AT_MODE);
6669 					vattr.va_type = VREG;
6670 					createmode = UNCHECKED4;
6671 					truncating = 1;
6672 					goto create_otw;
6673 				}
6674 			}
6675 		}
6676 	}
6677 	nfs_rw_exit(&drp->r_rwlock);
6678 	if (error) {
6679 		VN_RELE(vp);
6680 	} else {
6681 		vnode_t *tvp;
6682 		rnode4_t *trp;
6683 		tvp = vp;
6684 		if (vp->v_type == VREG) {
6685 			trp = VTOR4(vp);
6686 			if (IS_SHADOW(vp, trp))
6687 				tvp = RTOV4(trp);
6688 		}
6689 
6690 		if (must_trunc) {
6691 			/*
6692 			 * existing file got truncated, notify.
6693 			 */
6694 			vnevent_create(tvp, ct);
6695 		}
6696 
6697 		*vpp = vp;
6698 	}
6699 	return (error);
6700 
6701 create_otw:
6702 	dnlc_remove(dvp, nm);
6703 
6704 	ASSERT(vattr.va_mask & AT_TYPE);
6705 
6706 	/*
6707 	 * If not a regular file let nfs4mknod() handle it.
6708 	 */
6709 	if (vattr.va_type != VREG) {
6710 		error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6711 		nfs_rw_exit(&drp->r_rwlock);
6712 		return (error);
6713 	}
6714 
6715 	/*
6716 	 * It _is_ a regular file.
6717 	 */
6718 	ASSERT(vattr.va_mask & AT_MODE);
6719 	if (MANDMODE(vattr.va_mode)) {
6720 		nfs_rw_exit(&drp->r_rwlock);
6721 		return (EACCES);
6722 	}
6723 
6724 	/*
6725 	 * If this happens to be a mknod of a regular file, then flags will
6726 	 * have neither FREAD or FWRITE.  However, we must set at least one
6727 	 * for the call to nfs4open_otw.  If it's open(O_CREAT) driving
6728 	 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6729 	 * set (based on openmode specified by app).
6730 	 */
6731 	if ((flags & (FREAD|FWRITE)) == 0)
6732 		flags |= (FREAD|FWRITE);
6733 
6734 	error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6735 
6736 	if (vp != NULL) {
6737 		/* if create was successful, throw away the file's pages */
6738 		if (!error && (vattr.va_mask & AT_SIZE))
6739 			nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6740 			    cr);
6741 		/* release the lookup hold */
6742 		VN_RELE(vp);
6743 		vp = NULL;
6744 	}
6745 
6746 	/*
6747 	 * validate that we opened a regular file. This handles a misbehaving
6748 	 * server that returns an incorrect FH.
6749 	 */
6750 	if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6751 		error = EISDIR;
6752 		VN_RELE(*vpp);
6753 	}
6754 
6755 	/*
6756 	 * If this is not an exclusive create, then the CREATE
6757 	 * request will be made with the GUARDED mode set.  This
6758 	 * means that the server will return EEXIST if the file
6759 	 * exists.  The file could exist because of a retransmitted
6760 	 * request.  In this case, we recover by starting over and
6761 	 * checking to see whether the file exists.  This second
6762 	 * time through it should and a CREATE request will not be
6763 	 * sent.
6764 	 *
6765 	 * This handles the problem of a dangling CREATE request
6766 	 * which contains attributes which indicate that the file
6767 	 * should be truncated.  This retransmitted request could
6768 	 * possibly truncate valid data in the file if not caught
6769 	 * by the duplicate request mechanism on the server or if
6770 	 * not caught by other means.  The scenario is:
6771 	 *
6772 	 * Client transmits CREATE request with size = 0
6773 	 * Client times out, retransmits request.
6774 	 * Response to the first request arrives from the server
6775 	 *  and the client proceeds on.
6776 	 * Client writes data to the file.
6777 	 * The server now processes retransmitted CREATE request
6778 	 *  and truncates file.
6779 	 *
6780 	 * The use of the GUARDED CREATE request prevents this from
6781 	 * happening because the retransmitted CREATE would fail
6782 	 * with EEXIST and would not truncate the file.
6783 	 */
6784 	if (error == EEXIST && exclusive == NONEXCL) {
6785 #ifdef DEBUG
6786 		nfs4_create_misses++;
6787 #endif
6788 		goto top;
6789 	}
6790 	nfs_rw_exit(&drp->r_rwlock);
6791 	if (truncating && !error && *vpp) {
6792 		vnode_t *tvp;
6793 		rnode4_t *trp;
6794 		/*
6795 		 * existing file got truncated, notify.
6796 		 */
6797 		tvp = *vpp;
6798 		trp = VTOR4(tvp);
6799 		if (IS_SHADOW(tvp, trp))
6800 			tvp = RTOV4(trp);
6801 		vnevent_create(tvp, ct);
6802 	}
6803 	return (error);
6804 }
6805 
6806 /*
6807  * Create compound (for mkdir, mknod, symlink):
6808  * { Putfh <dfh>; Create; Getfh; Getattr }
6809  * It's okay if setattr failed to set gid - this is not considered
6810  * an error, but purge attrs in that case.
6811  */
6812 static int
call_nfs4_create_req(vnode_t * dvp,char * nm,void * data,struct vattr * va,vnode_t ** vpp,cred_t * cr,nfs_ftype4 type)6813 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6814     vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6815 {
6816 	int need_end_op = FALSE;
6817 	COMPOUND4args_clnt args;
6818 	COMPOUND4res_clnt res, *resp = NULL;
6819 	nfs_argop4 *argop;
6820 	nfs_resop4 *resop;
6821 	int doqueue;
6822 	mntinfo4_t *mi;
6823 	rnode4_t *drp = VTOR4(dvp);
6824 	change_info4 *cinfo;
6825 	GETFH4res *gf_res;
6826 	struct vattr vattr;
6827 	vnode_t *vp;
6828 	fattr4 *crattr;
6829 	bool_t needrecov = FALSE;
6830 	nfs4_recov_state_t recov_state;
6831 	nfs4_sharedfh_t *sfhp = NULL;
6832 	hrtime_t t;
6833 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6834 	int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6835 	dirattr_info_t dinfo, *dinfop;
6836 	servinfo4_t *svp;
6837 	bitmap4 supp_attrs;
6838 
6839 	ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6840 	    type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6841 
6842 	mi = VTOMI4(dvp);
6843 
6844 	/*
6845 	 * Make sure we properly deal with setting the right gid
6846 	 * on a new directory to reflect the parent's setgid bit
6847 	 */
6848 	setgid_flag = 0;
6849 	if (type == NF4DIR) {
6850 		struct vattr dva;
6851 
6852 		va->va_mode &= ~VSGID;
6853 		dva.va_mask = AT_MODE | AT_GID;
6854 		if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6855 
6856 			/*
6857 			 * If the parent's directory has the setgid bit set
6858 			 * _and_ the client was able to get a valid mapping
6859 			 * for the parent dir's owner_group, we want to
6860 			 * append NVERIFY(owner_group == dva.va_gid) and
6861 			 * SETTATTR to the CREATE compound.
6862 			 */
6863 			if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6864 				setgid_flag = 1;
6865 				va->va_mode |= VSGID;
6866 				if (dva.va_gid != GID_NOBODY) {
6867 					va->va_mask |= AT_GID;
6868 					va->va_gid = dva.va_gid;
6869 				}
6870 			}
6871 		}
6872 	}
6873 
6874 	/*
6875 	 * Create ops:
6876 	 *	0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6877 	 *	5:restorefh(dir) 6:getattr(dir)
6878 	 *
6879 	 * if (setgid)
6880 	 *	0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6881 	 *	4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6882 	 *	8:nverify 9:setattr
6883 	 */
6884 	if (setgid_flag) {
6885 		numops = 10;
6886 		idx_create = 1;
6887 		idx_fattr = 3;
6888 	} else {
6889 		numops = 7;
6890 		idx_create = 2;
6891 		idx_fattr = 4;
6892 	}
6893 
6894 	ASSERT(nfs_zone() == mi->mi_zone);
6895 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6896 		return (EINTR);
6897 	}
6898 	recov_state.rs_flags = 0;
6899 	recov_state.rs_num_retry_despite_err = 0;
6900 
6901 	argoplist_size = numops * sizeof (nfs_argop4);
6902 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
6903 
6904 recov_retry:
6905 	if (type == NF4LNK)
6906 		args.ctag = TAG_SYMLINK;
6907 	else if (type == NF4DIR)
6908 		args.ctag = TAG_MKDIR;
6909 	else
6910 		args.ctag = TAG_MKNOD;
6911 
6912 	args.array_len = numops;
6913 	args.array = argop;
6914 
6915 	if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6916 		nfs_rw_exit(&drp->r_rwlock);
6917 		kmem_free(argop, argoplist_size);
6918 		return (e.error);
6919 	}
6920 	need_end_op = TRUE;
6921 
6922 
6923 	/* 0: putfh directory */
6924 	argop[0].argop = OP_CPUTFH;
6925 	argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6926 
6927 	/* 1/2: Create object */
6928 	argop[idx_create].argop = OP_CCREATE;
6929 	argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6930 	argop[idx_create].nfs_argop4_u.opccreate.type = type;
6931 	if (type == NF4LNK) {
6932 		/*
6933 		 * symlink, treat name as data
6934 		 */
6935 		ASSERT(data != NULL);
6936 		argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6937 		    (char *)data;
6938 	}
6939 	if (type == NF4BLK || type == NF4CHR) {
6940 		ASSERT(data != NULL);
6941 		argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6942 		    *((specdata4 *)data);
6943 	}
6944 
6945 	crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6946 
6947 	svp = drp->r_server;
6948 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6949 	supp_attrs = svp->sv_supp_attrs;
6950 	nfs_rw_exit(&svp->sv_lock);
6951 
6952 	if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6953 		nfs_rw_exit(&drp->r_rwlock);
6954 		nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6955 		e.error = EINVAL;
6956 		kmem_free(argop, argoplist_size);
6957 		return (e.error);
6958 	}
6959 
6960 	/* 2/3: getfh fh of created object */
6961 	ASSERT(idx_create + 1 == idx_fattr - 1);
6962 	argop[idx_create + 1].argop = OP_GETFH;
6963 
6964 	/* 3/4: getattr of new object */
6965 	argop[idx_fattr].argop = OP_GETATTR;
6966 	argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6967 	argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6968 
6969 	if (setgid_flag) {
6970 		vattr_t	_v;
6971 
6972 		argop[4].argop = OP_SAVEFH;
6973 
6974 		argop[5].argop = OP_CPUTFH;
6975 		argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6976 
6977 		argop[6].argop = OP_GETATTR;
6978 		argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6979 		argop[6].nfs_argop4_u.opgetattr.mi = mi;
6980 
6981 		argop[7].argop = OP_RESTOREFH;
6982 
6983 		/*
6984 		 * nverify
6985 		 *
6986 		 * XXX - Revisit the last argument to nfs4_end_op()
6987 		 *	 once 5020486 is fixed.
6988 		 */
6989 		_v.va_mask = AT_GID;
6990 		_v.va_gid = va->va_gid;
6991 		if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6992 		    supp_attrs)) {
6993 			nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6994 			nfs_rw_exit(&drp->r_rwlock);
6995 			nfs4_fattr4_free(crattr);
6996 			kmem_free(argop, argoplist_size);
6997 			return (e.error);
6998 		}
6999 
7000 		/*
7001 		 * setattr
7002 		 *
7003 		 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
7004 		 * so no need for stateid or flags. Also we specify NULL
7005 		 * rp since we're only interested in setting owner_group
7006 		 * attributes.
7007 		 */
7008 		nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
7009 		    &e.error, 0);
7010 
7011 		if (e.error) {
7012 			nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7013 			nfs_rw_exit(&drp->r_rwlock);
7014 			nfs4_fattr4_free(crattr);
7015 			nfs4args_verify_free(&argop[8]);
7016 			kmem_free(argop, argoplist_size);
7017 			return (e.error);
7018 		}
7019 	} else {
7020 		argop[1].argop = OP_SAVEFH;
7021 
7022 		argop[5].argop = OP_RESTOREFH;
7023 
7024 		argop[6].argop = OP_GETATTR;
7025 		argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7026 		argop[6].nfs_argop4_u.opgetattr.mi = mi;
7027 	}
7028 
7029 	dnlc_remove(dvp, nm);
7030 
7031 	doqueue = 1;
7032 	t = gethrtime();
7033 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7034 
7035 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7036 	if (e.error) {
7037 		PURGE_ATTRCACHE4(dvp);
7038 		if (!needrecov)
7039 			goto out;
7040 	}
7041 
7042 	if (needrecov) {
7043 		if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7044 		    OP_CREATE, NULL, NULL, NULL) == FALSE) {
7045 			nfs4_end_op(mi, dvp, NULL, &recov_state,
7046 			    needrecov);
7047 			need_end_op = FALSE;
7048 			nfs4_fattr4_free(crattr);
7049 			if (setgid_flag) {
7050 				nfs4args_verify_free(&argop[8]);
7051 				nfs4args_setattr_free(&argop[9]);
7052 			}
7053 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7054 			goto recov_retry;
7055 		}
7056 	}
7057 
7058 	resp = &res;
7059 
7060 	if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7061 
7062 		if (res.status == NFS4ERR_BADOWNER)
7063 			nfs4_log_badowner(mi, OP_CREATE);
7064 
7065 		e.error = geterrno4(res.status);
7066 
7067 		/*
7068 		 * This check is left over from when create was implemented
7069 		 * using a setattr op (instead of createattrs).  If the
7070 		 * putfh/create/getfh failed, the error was returned.  If
7071 		 * setattr/getattr failed, we keep going.
7072 		 *
7073 		 * It might be better to get rid of the GETFH also, and just
7074 		 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7075 		 * Then if any of the operations failed, we could return the
7076 		 * error now, and remove much of the error code below.
7077 		 */
7078 		if (res.array_len <= idx_fattr) {
7079 			/*
7080 			 * Either Putfh, Create or Getfh failed.
7081 			 */
7082 			PURGE_ATTRCACHE4(dvp);
7083 			/*
7084 			 * nfs4_purge_stale_fh() may generate otw calls through
7085 			 * nfs4_invalidate_pages. Hence the need to call
7086 			 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7087 			 */
7088 			nfs4_end_op(mi, dvp, NULL, &recov_state,
7089 			    needrecov);
7090 			need_end_op = FALSE;
7091 			nfs4_purge_stale_fh(e.error, dvp, cr);
7092 			goto out;
7093 		}
7094 	}
7095 
7096 	resop = &res.array[idx_create];	/* create res */
7097 	cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7098 
7099 	resop = &res.array[idx_create + 1]; /* getfh res */
7100 	gf_res = &resop->nfs_resop4_u.opgetfh;
7101 
7102 	sfhp = sfh4_get(&gf_res->object, mi);
7103 	if (e.error) {
7104 		*vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7105 		    fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7106 		if (vp->v_type == VNON) {
7107 			vattr.va_mask = AT_TYPE;
7108 			/*
7109 			 * Need to call nfs4_end_op before nfs4getattr to avoid
7110 			 * potential nfs4_start_op deadlock. See RFE 4777612.
7111 			 */
7112 			nfs4_end_op(mi, dvp, NULL, &recov_state,
7113 			    needrecov);
7114 			need_end_op = FALSE;
7115 			e.error = nfs4getattr(vp, &vattr, cr);
7116 			if (e.error) {
7117 				VN_RELE(vp);
7118 				*vpp = NULL;
7119 				goto out;
7120 			}
7121 			vp->v_type = vattr.va_type;
7122 		}
7123 		e.error = 0;
7124 	} else {
7125 		*vpp = vp = makenfs4node(sfhp,
7126 		    &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7127 		    dvp->v_vfsp, t, cr,
7128 		    dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7129 	}
7130 
7131 	/*
7132 	 * If compound succeeded, then update dir attrs
7133 	 */
7134 	if (res.status == NFS4_OK) {
7135 		dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7136 		dinfo.di_cred = cr;
7137 		dinfo.di_time_call = t;
7138 		dinfop = &dinfo;
7139 	} else
7140 		dinfop = NULL;
7141 
7142 	/* Update directory cache attribute, readdir and dnlc caches */
7143 	nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7144 
7145 out:
7146 	if (sfhp != NULL)
7147 		sfh4_rele(&sfhp);
7148 	nfs_rw_exit(&drp->r_rwlock);
7149 	nfs4_fattr4_free(crattr);
7150 	if (setgid_flag) {
7151 		nfs4args_verify_free(&argop[8]);
7152 		nfs4args_setattr_free(&argop[9]);
7153 	}
7154 	if (resp)
7155 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7156 	if (need_end_op)
7157 		nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7158 
7159 	kmem_free(argop, argoplist_size);
7160 	return (e.error);
7161 }
7162 
7163 /* ARGSUSED */
7164 static int
nfs4mknod(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr)7165 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7166     int mode, vnode_t **vpp, cred_t *cr)
7167 {
7168 	int error;
7169 	vnode_t *vp;
7170 	nfs_ftype4 type;
7171 	specdata4 spec, *specp = NULL;
7172 
7173 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7174 
7175 	switch (va->va_type) {
7176 	case VCHR:
7177 	case VBLK:
7178 		type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7179 		spec.specdata1 = getmajor(va->va_rdev);
7180 		spec.specdata2 = getminor(va->va_rdev);
7181 		specp = &spec;
7182 		break;
7183 
7184 	case VFIFO:
7185 		type = NF4FIFO;
7186 		break;
7187 	case VSOCK:
7188 		type = NF4SOCK;
7189 		break;
7190 
7191 	default:
7192 		return (EINVAL);
7193 	}
7194 
7195 	error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7196 	if (error) {
7197 		return (error);
7198 	}
7199 
7200 	/*
7201 	 * This might not be needed any more; special case to deal
7202 	 * with problematic v2/v3 servers.  Since create was unable
7203 	 * to set group correctly, not sure what hope setattr has.
7204 	 */
7205 	if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7206 		va->va_mask = AT_GID;
7207 		(void) nfs4setattr(vp, va, 0, cr, NULL);
7208 	}
7209 
7210 	/*
7211 	 * If vnode is a device create special vnode
7212 	 */
7213 	if (ISVDEV(vp->v_type)) {
7214 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7215 		VN_RELE(vp);
7216 	} else {
7217 		*vpp = vp;
7218 	}
7219 	return (error);
7220 }
7221 
7222 /*
7223  * Remove requires that the current fh be the target directory.
7224  * After the operation, the current fh is unchanged.
7225  * The compound op structure is:
7226  *      PUTFH(targetdir), REMOVE
7227  *
7228  * Weirdness: if the vnode to be removed is open
7229  * we rename it instead of removing it and nfs_inactive
7230  * will remove the new name.
7231  */
7232 /* ARGSUSED */
7233 static int
nfs4_remove(vnode_t * dvp,char * nm,cred_t * cr,caller_context_t * ct,int flags)7234 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7235 {
7236 	COMPOUND4args_clnt args;
7237 	COMPOUND4res_clnt res, *resp = NULL;
7238 	REMOVE4res *rm_res;
7239 	nfs_argop4 argop[3];
7240 	nfs_resop4 *resop;
7241 	vnode_t *vp;
7242 	char *tmpname;
7243 	int doqueue;
7244 	mntinfo4_t *mi;
7245 	rnode4_t *rp;
7246 	rnode4_t *drp;
7247 	int needrecov = 0;
7248 	nfs4_recov_state_t recov_state;
7249 	int isopen;
7250 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7251 	dirattr_info_t dinfo;
7252 
7253 	if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7254 		return (EPERM);
7255 	drp = VTOR4(dvp);
7256 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7257 		return (EINTR);
7258 
7259 	e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7260 	if (e.error) {
7261 		nfs_rw_exit(&drp->r_rwlock);
7262 		return (e.error);
7263 	}
7264 
7265 	if (vp->v_type == VDIR) {
7266 		VN_RELE(vp);
7267 		nfs_rw_exit(&drp->r_rwlock);
7268 		return (EISDIR);
7269 	}
7270 
7271 	/*
7272 	 * First just remove the entry from the name cache, as it
7273 	 * is most likely the only entry for this vp.
7274 	 */
7275 	dnlc_remove(dvp, nm);
7276 
7277 	rp = VTOR4(vp);
7278 
7279 	/*
7280 	 * For regular file types, check to see if the file is open by looking
7281 	 * at the open streams.
7282 	 * For all other types, check the reference count on the vnode.  Since
7283 	 * they are not opened OTW they never have an open stream.
7284 	 *
7285 	 * If the file is open, rename it to .nfsXXXX.
7286 	 */
7287 	if (vp->v_type != VREG) {
7288 		/*
7289 		 * If the file has a v_count > 1 then there may be more than one
7290 		 * entry in the name cache due multiple links or an open file,
7291 		 * but we don't have the real reference count so flush all
7292 		 * possible entries.
7293 		 */
7294 		if (vp->v_count > 1)
7295 			dnlc_purge_vp(vp);
7296 
7297 		/*
7298 		 * Now we have the real reference count.
7299 		 */
7300 		isopen = vp->v_count > 1;
7301 	} else {
7302 		mutex_enter(&rp->r_os_lock);
7303 		isopen = list_head(&rp->r_open_streams) != NULL;
7304 		mutex_exit(&rp->r_os_lock);
7305 	}
7306 
7307 	mutex_enter(&rp->r_statelock);
7308 	if (isopen &&
7309 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7310 		mutex_exit(&rp->r_statelock);
7311 		tmpname = newname();
7312 		e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7313 		if (e.error)
7314 			kmem_free(tmpname, MAXNAMELEN);
7315 		else {
7316 			mutex_enter(&rp->r_statelock);
7317 			if (rp->r_unldvp == NULL) {
7318 				VN_HOLD(dvp);
7319 				rp->r_unldvp = dvp;
7320 				if (rp->r_unlcred != NULL)
7321 					crfree(rp->r_unlcred);
7322 				crhold(cr);
7323 				rp->r_unlcred = cr;
7324 				rp->r_unlname = tmpname;
7325 			} else {
7326 				kmem_free(rp->r_unlname, MAXNAMELEN);
7327 				rp->r_unlname = tmpname;
7328 			}
7329 			mutex_exit(&rp->r_statelock);
7330 		}
7331 		VN_RELE(vp);
7332 		nfs_rw_exit(&drp->r_rwlock);
7333 		return (e.error);
7334 	}
7335 	/*
7336 	 * Actually remove the file/dir
7337 	 */
7338 	mutex_exit(&rp->r_statelock);
7339 
7340 	/*
7341 	 * We need to flush any dirty pages which happen to
7342 	 * be hanging around before removing the file.
7343 	 * This shouldn't happen very often since in NFSv4
7344 	 * we should be close to open consistent.
7345 	 */
7346 	if (nfs4_has_pages(vp) &&
7347 	    ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7348 		e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7349 		if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7350 			mutex_enter(&rp->r_statelock);
7351 			if (!rp->r_error)
7352 				rp->r_error = e.error;
7353 			mutex_exit(&rp->r_statelock);
7354 		}
7355 	}
7356 
7357 	mi = VTOMI4(dvp);
7358 
7359 	(void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7360 	recov_state.rs_flags = 0;
7361 	recov_state.rs_num_retry_despite_err = 0;
7362 
7363 recov_retry:
7364 	/*
7365 	 * Remove ops: putfh dir; remove
7366 	 */
7367 	args.ctag = TAG_REMOVE;
7368 	args.array_len = 3;
7369 	args.array = argop;
7370 
7371 	e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7372 	if (e.error) {
7373 		nfs_rw_exit(&drp->r_rwlock);
7374 		VN_RELE(vp);
7375 		return (e.error);
7376 	}
7377 
7378 	/* putfh directory */
7379 	argop[0].argop = OP_CPUTFH;
7380 	argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7381 
7382 	/* remove */
7383 	argop[1].argop = OP_CREMOVE;
7384 	argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7385 
7386 	/* getattr dir */
7387 	argop[2].argop = OP_GETATTR;
7388 	argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7389 	argop[2].nfs_argop4_u.opgetattr.mi = mi;
7390 
7391 	doqueue = 1;
7392 	dinfo.di_time_call = gethrtime();
7393 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7394 
7395 	PURGE_ATTRCACHE4(vp);
7396 
7397 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7398 	if (e.error)
7399 		PURGE_ATTRCACHE4(dvp);
7400 
7401 	if (needrecov) {
7402 		if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7403 		    NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7404 			if (!e.error)
7405 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7406 			nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7407 			    needrecov);
7408 			goto recov_retry;
7409 		}
7410 	}
7411 
7412 	/*
7413 	 * Matching nfs4_end_op() for start_op() above.
7414 	 * There is a path in the code below which calls
7415 	 * nfs4_purge_stale_fh(), which may generate otw calls through
7416 	 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7417 	 * here to avoid nfs4_start_op() deadlock.
7418 	 */
7419 	nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7420 
7421 	if (!e.error) {
7422 		resp = &res;
7423 
7424 		if (res.status) {
7425 			e.error = geterrno4(res.status);
7426 			PURGE_ATTRCACHE4(dvp);
7427 			nfs4_purge_stale_fh(e.error, dvp, cr);
7428 		} else {
7429 			resop = &res.array[1];	/* remove res */
7430 			rm_res = &resop->nfs_resop4_u.opremove;
7431 
7432 			dinfo.di_garp =
7433 			    &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7434 			dinfo.di_cred = cr;
7435 
7436 			/* Update directory attr, readdir and dnlc caches */
7437 			nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7438 			    &dinfo);
7439 		}
7440 	}
7441 	nfs_rw_exit(&drp->r_rwlock);
7442 	if (resp)
7443 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7444 
7445 	if (e.error == 0) {
7446 		vnode_t *tvp;
7447 		rnode4_t *trp;
7448 		trp = VTOR4(vp);
7449 		tvp = vp;
7450 		if (IS_SHADOW(vp, trp))
7451 			tvp = RTOV4(trp);
7452 		vnevent_remove(tvp, dvp, nm, ct);
7453 	}
7454 	VN_RELE(vp);
7455 	return (e.error);
7456 }
7457 
7458 /*
7459  * Link requires that the current fh be the target directory and the
7460  * saved fh be the source fh. After the operation, the current fh is unchanged.
7461  * Thus the compound op structure is:
7462  *	PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7463  *	GETATTR(file)
7464  */
7465 /* ARGSUSED */
7466 static int
nfs4_link(vnode_t * tdvp,vnode_t * svp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)7467 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7468     caller_context_t *ct, int flags)
7469 {
7470 	COMPOUND4args_clnt args;
7471 	COMPOUND4res_clnt res, *resp = NULL;
7472 	LINK4res *ln_res;
7473 	int argoplist_size  = 7 * sizeof (nfs_argop4);
7474 	nfs_argop4 *argop;
7475 	nfs_resop4 *resop;
7476 	vnode_t *realvp, *nvp;
7477 	int doqueue;
7478 	mntinfo4_t *mi;
7479 	rnode4_t *tdrp;
7480 	bool_t needrecov = FALSE;
7481 	nfs4_recov_state_t recov_state;
7482 	hrtime_t t;
7483 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7484 	dirattr_info_t dinfo;
7485 
7486 	ASSERT(*tnm != '\0');
7487 	ASSERT(tdvp->v_type == VDIR);
7488 	ASSERT(nfs4_consistent_type(tdvp));
7489 	ASSERT(nfs4_consistent_type(svp));
7490 
7491 	if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7492 		return (EPERM);
7493 	if (VOP_REALVP(svp, &realvp, ct) == 0) {
7494 		svp = realvp;
7495 		ASSERT(nfs4_consistent_type(svp));
7496 	}
7497 
7498 	tdrp = VTOR4(tdvp);
7499 	mi = VTOMI4(svp);
7500 
7501 	if (!(mi->mi_flags & MI4_LINK)) {
7502 		return (EOPNOTSUPP);
7503 	}
7504 	recov_state.rs_flags = 0;
7505 	recov_state.rs_num_retry_despite_err = 0;
7506 
7507 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7508 		return (EINTR);
7509 
7510 recov_retry:
7511 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
7512 
7513 	args.ctag = TAG_LINK;
7514 
7515 	/*
7516 	 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7517 	 * restorefh; getattr(fl)
7518 	 */
7519 	args.array_len = 7;
7520 	args.array = argop;
7521 
7522 	e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7523 	if (e.error) {
7524 		kmem_free(argop, argoplist_size);
7525 		nfs_rw_exit(&tdrp->r_rwlock);
7526 		return (e.error);
7527 	}
7528 
7529 	/* 0. putfh file */
7530 	argop[0].argop = OP_CPUTFH;
7531 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7532 
7533 	/* 1. save current fh to free up the space for the dir */
7534 	argop[1].argop = OP_SAVEFH;
7535 
7536 	/* 2. putfh targetdir */
7537 	argop[2].argop = OP_CPUTFH;
7538 	argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7539 
7540 	/* 3. link: current_fh is targetdir, saved_fh is source */
7541 	argop[3].argop = OP_CLINK;
7542 	argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7543 
7544 	/* 4. Get attributes of dir */
7545 	argop[4].argop = OP_GETATTR;
7546 	argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7547 	argop[4].nfs_argop4_u.opgetattr.mi = mi;
7548 
7549 	/* 5. If link was successful, restore current vp to file */
7550 	argop[5].argop = OP_RESTOREFH;
7551 
7552 	/* 6. Get attributes of linked object */
7553 	argop[6].argop = OP_GETATTR;
7554 	argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7555 	argop[6].nfs_argop4_u.opgetattr.mi = mi;
7556 
7557 	dnlc_remove(tdvp, tnm);
7558 
7559 	doqueue = 1;
7560 	t = gethrtime();
7561 
7562 	rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7563 
7564 	needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7565 	if (e.error != 0 && !needrecov) {
7566 		PURGE_ATTRCACHE4(tdvp);
7567 		PURGE_ATTRCACHE4(svp);
7568 		nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7569 		goto out;
7570 	}
7571 
7572 	if (needrecov) {
7573 		bool_t abort;
7574 
7575 		abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7576 		    NULL, NULL, OP_LINK, NULL, NULL, NULL);
7577 		if (abort == FALSE) {
7578 			nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7579 			    needrecov);
7580 			kmem_free(argop, argoplist_size);
7581 			if (!e.error)
7582 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7583 			goto recov_retry;
7584 		} else {
7585 			if (e.error != 0) {
7586 				PURGE_ATTRCACHE4(tdvp);
7587 				PURGE_ATTRCACHE4(svp);
7588 				nfs4_end_op(VTOMI4(svp), svp, tdvp,
7589 				    &recov_state, needrecov);
7590 				goto out;
7591 			}
7592 			/* fall through for res.status case */
7593 		}
7594 	}
7595 
7596 	nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7597 
7598 	resp = &res;
7599 	if (res.status) {
7600 		/* If link succeeded, then don't return error */
7601 		e.error = geterrno4(res.status);
7602 		if (res.array_len <= 4) {
7603 			/*
7604 			 * Either Putfh, Savefh, Putfh dir, or Link failed
7605 			 */
7606 			PURGE_ATTRCACHE4(svp);
7607 			PURGE_ATTRCACHE4(tdvp);
7608 			if (e.error == EOPNOTSUPP) {
7609 				mutex_enter(&mi->mi_lock);
7610 				mi->mi_flags &= ~MI4_LINK;
7611 				mutex_exit(&mi->mi_lock);
7612 			}
7613 			/* Remap EISDIR to EPERM for non-root user for SVVS */
7614 			/* XXX-LP */
7615 			if (e.error == EISDIR && crgetuid(cr) != 0)
7616 				e.error = EPERM;
7617 			goto out;
7618 		}
7619 	}
7620 
7621 	/* either no error or one of the postop getattr failed */
7622 
7623 	/*
7624 	 * XXX - if LINK succeeded, but no attrs were returned for link
7625 	 * file, purge its cache.
7626 	 *
7627 	 * XXX Perform a simplified version of wcc checking. Instead of
7628 	 * have another getattr to get pre-op, just purge cache if
7629 	 * any of the ops prior to and including the getattr failed.
7630 	 * If the getattr succeeded then update the attrcache accordingly.
7631 	 */
7632 
7633 	/*
7634 	 * update cache with link file postattrs.
7635 	 * Note: at this point resop points to link res.
7636 	 */
7637 	resop = &res.array[3];	/* link res */
7638 	ln_res = &resop->nfs_resop4_u.oplink;
7639 	if (res.status == NFS4_OK)
7640 		e.error = nfs4_update_attrcache(res.status,
7641 		    &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7642 		    t, svp, cr);
7643 
7644 	/*
7645 	 * Call makenfs4node to create the new shadow vp for tnm.
7646 	 * We pass NULL attrs because we just cached attrs for
7647 	 * the src object.  All we're trying to accomplish is to
7648 	 * to create the new shadow vnode.
7649 	 */
7650 	nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7651 	    tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7652 
7653 	/* Update target cache attribute, readdir and dnlc caches */
7654 	dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7655 	dinfo.di_time_call = t;
7656 	dinfo.di_cred = cr;
7657 
7658 	nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7659 	ASSERT(nfs4_consistent_type(tdvp));
7660 	ASSERT(nfs4_consistent_type(svp));
7661 	ASSERT(nfs4_consistent_type(nvp));
7662 	VN_RELE(nvp);
7663 
7664 	if (!e.error) {
7665 		vnode_t *tvp;
7666 		rnode4_t *trp;
7667 		/*
7668 		 * Notify the source file of this link operation.
7669 		 */
7670 		trp = VTOR4(svp);
7671 		tvp = svp;
7672 		if (IS_SHADOW(svp, trp))
7673 			tvp = RTOV4(trp);
7674 		vnevent_link(tvp, ct);
7675 	}
7676 out:
7677 	kmem_free(argop, argoplist_size);
7678 	if (resp)
7679 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7680 
7681 	nfs_rw_exit(&tdrp->r_rwlock);
7682 
7683 	return (e.error);
7684 }
7685 
7686 /* ARGSUSED */
7687 static int
nfs4_rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct,int flags)7688 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7689     caller_context_t *ct, int flags)
7690 {
7691 	vnode_t *realvp;
7692 
7693 	if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7694 		return (EPERM);
7695 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7696 		ndvp = realvp;
7697 
7698 	return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7699 }
7700 
7701 /*
7702  * nfs4rename does the real work of renaming in NFS Version 4.
7703  *
7704  * A file handle is considered volatile for renaming purposes if either
7705  * of the volatile bits are turned on. However, the compound may differ
7706  * based on the likelihood of the filehandle to change during rename.
7707  */
7708 static int
nfs4rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct)7709 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7710     caller_context_t *ct)
7711 {
7712 	int error;
7713 	mntinfo4_t *mi;
7714 	vnode_t *nvp = NULL;
7715 	vnode_t *ovp = NULL;
7716 	char *tmpname = NULL;
7717 	rnode4_t *rp;
7718 	rnode4_t *odrp;
7719 	rnode4_t *ndrp;
7720 	int did_link = 0;
7721 	int do_link = 1;
7722 	nfsstat4 stat = NFS4_OK;
7723 
7724 	ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7725 	ASSERT(nfs4_consistent_type(odvp));
7726 	ASSERT(nfs4_consistent_type(ndvp));
7727 
7728 	if (onm[0] == '.' && (onm[1] == '\0' ||
7729 	    (onm[1] == '.' && onm[2] == '\0')))
7730 		return (EINVAL);
7731 
7732 	if (nnm[0] == '.' && (nnm[1] == '\0' ||
7733 	    (nnm[1] == '.' && nnm[2] == '\0')))
7734 		return (EINVAL);
7735 
7736 	odrp = VTOR4(odvp);
7737 	ndrp = VTOR4(ndvp);
7738 	if ((intptr_t)odrp < (intptr_t)ndrp) {
7739 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7740 			return (EINTR);
7741 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7742 			nfs_rw_exit(&odrp->r_rwlock);
7743 			return (EINTR);
7744 		}
7745 	} else {
7746 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7747 			return (EINTR);
7748 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7749 			nfs_rw_exit(&ndrp->r_rwlock);
7750 			return (EINTR);
7751 		}
7752 	}
7753 
7754 	/*
7755 	 * Lookup the target file.  If it exists, it needs to be
7756 	 * checked to see whether it is a mount point and whether
7757 	 * it is active (open).
7758 	 */
7759 	error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7760 	if (!error) {
7761 		int	isactive;
7762 
7763 		ASSERT(nfs4_consistent_type(nvp));
7764 		/*
7765 		 * If this file has been mounted on, then just
7766 		 * return busy because renaming to it would remove
7767 		 * the mounted file system from the name space.
7768 		 */
7769 		if (vn_ismntpt(nvp)) {
7770 			VN_RELE(nvp);
7771 			nfs_rw_exit(&odrp->r_rwlock);
7772 			nfs_rw_exit(&ndrp->r_rwlock);
7773 			return (EBUSY);
7774 		}
7775 
7776 		/*
7777 		 * First just remove the entry from the name cache, as it
7778 		 * is most likely the only entry for this vp.
7779 		 */
7780 		dnlc_remove(ndvp, nnm);
7781 
7782 		rp = VTOR4(nvp);
7783 
7784 		if (nvp->v_type != VREG) {
7785 			/*
7786 			 * Purge the name cache of all references to this vnode
7787 			 * so that we can check the reference count to infer
7788 			 * whether it is active or not.
7789 			 */
7790 			if (nvp->v_count > 1)
7791 				dnlc_purge_vp(nvp);
7792 
7793 			isactive = nvp->v_count > 1;
7794 		} else {
7795 			mutex_enter(&rp->r_os_lock);
7796 			isactive = list_head(&rp->r_open_streams) != NULL;
7797 			mutex_exit(&rp->r_os_lock);
7798 		}
7799 
7800 		/*
7801 		 * If the vnode is active and is not a directory,
7802 		 * arrange to rename it to a
7803 		 * temporary file so that it will continue to be
7804 		 * accessible.  This implements the "unlink-open-file"
7805 		 * semantics for the target of a rename operation.
7806 		 * Before doing this though, make sure that the
7807 		 * source and target files are not already the same.
7808 		 */
7809 		if (isactive && nvp->v_type != VDIR) {
7810 			/*
7811 			 * Lookup the source name.
7812 			 */
7813 			error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7814 
7815 			/*
7816 			 * The source name *should* already exist.
7817 			 */
7818 			if (error) {
7819 				VN_RELE(nvp);
7820 				nfs_rw_exit(&odrp->r_rwlock);
7821 				nfs_rw_exit(&ndrp->r_rwlock);
7822 				return (error);
7823 			}
7824 
7825 			ASSERT(nfs4_consistent_type(ovp));
7826 
7827 			/*
7828 			 * Compare the two vnodes.  If they are the same,
7829 			 * just release all held vnodes and return success.
7830 			 */
7831 			if (VN_CMP(ovp, nvp)) {
7832 				VN_RELE(ovp);
7833 				VN_RELE(nvp);
7834 				nfs_rw_exit(&odrp->r_rwlock);
7835 				nfs_rw_exit(&ndrp->r_rwlock);
7836 				return (0);
7837 			}
7838 
7839 			/*
7840 			 * Can't mix and match directories and non-
7841 			 * directories in rename operations.  We already
7842 			 * know that the target is not a directory.  If
7843 			 * the source is a directory, return an error.
7844 			 */
7845 			if (ovp->v_type == VDIR) {
7846 				VN_RELE(ovp);
7847 				VN_RELE(nvp);
7848 				nfs_rw_exit(&odrp->r_rwlock);
7849 				nfs_rw_exit(&ndrp->r_rwlock);
7850 				return (ENOTDIR);
7851 			}
7852 link_call:
7853 			/*
7854 			 * The target file exists, is not the same as
7855 			 * the source file, and is active.  We first
7856 			 * try to Link it to a temporary filename to
7857 			 * avoid having the server removing the file
7858 			 * completely (which could cause data loss to
7859 			 * the user's POV in the event the Rename fails
7860 			 * -- see bug 1165874).
7861 			 */
7862 			/*
7863 			 * The do_link and did_link booleans are
7864 			 * introduced in the event we get NFS4ERR_FILE_OPEN
7865 			 * returned for the Rename.  Some servers can
7866 			 * not Rename over an Open file, so they return
7867 			 * this error.  The client needs to Remove the
7868 			 * newly created Link and do two Renames, just
7869 			 * as if the server didn't support LINK.
7870 			 */
7871 			tmpname = newname();
7872 			error = 0;
7873 
7874 			if (do_link) {
7875 				error = nfs4_link(ndvp, nvp, tmpname, cr,
7876 				    NULL, 0);
7877 			}
7878 			if (error == EOPNOTSUPP || !do_link) {
7879 				error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7880 				    cr, NULL, 0);
7881 				did_link = 0;
7882 			} else {
7883 				did_link = 1;
7884 			}
7885 			if (error) {
7886 				kmem_free(tmpname, MAXNAMELEN);
7887 				VN_RELE(ovp);
7888 				VN_RELE(nvp);
7889 				nfs_rw_exit(&odrp->r_rwlock);
7890 				nfs_rw_exit(&ndrp->r_rwlock);
7891 				return (error);
7892 			}
7893 
7894 			mutex_enter(&rp->r_statelock);
7895 			if (rp->r_unldvp == NULL) {
7896 				VN_HOLD(ndvp);
7897 				rp->r_unldvp = ndvp;
7898 				if (rp->r_unlcred != NULL)
7899 					crfree(rp->r_unlcred);
7900 				crhold(cr);
7901 				rp->r_unlcred = cr;
7902 				rp->r_unlname = tmpname;
7903 			} else {
7904 				if (rp->r_unlname)
7905 					kmem_free(rp->r_unlname, MAXNAMELEN);
7906 				rp->r_unlname = tmpname;
7907 			}
7908 			mutex_exit(&rp->r_statelock);
7909 		}
7910 
7911 		(void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7912 
7913 		ASSERT(nfs4_consistent_type(nvp));
7914 	}
7915 
7916 	if (ovp == NULL) {
7917 		/*
7918 		 * When renaming directories to be a subdirectory of a
7919 		 * different parent, the dnlc entry for ".." will no
7920 		 * longer be valid, so it must be removed.
7921 		 *
7922 		 * We do a lookup here to determine whether we are renaming
7923 		 * a directory and we need to check if we are renaming
7924 		 * an unlinked file.  This might have already been done
7925 		 * in previous code, so we check ovp == NULL to avoid
7926 		 * doing it twice.
7927 		 */
7928 		error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7929 		/*
7930 		 * The source name *should* already exist.
7931 		 */
7932 		if (error) {
7933 			nfs_rw_exit(&odrp->r_rwlock);
7934 			nfs_rw_exit(&ndrp->r_rwlock);
7935 			if (nvp) {
7936 				VN_RELE(nvp);
7937 			}
7938 			return (error);
7939 		}
7940 		ASSERT(ovp != NULL);
7941 		ASSERT(nfs4_consistent_type(ovp));
7942 	}
7943 
7944 	/*
7945 	 * Is the object being renamed a dir, and if so, is
7946 	 * it being renamed to a child of itself?  The underlying
7947 	 * fs should ultimately return EINVAL for this case;
7948 	 * however, buggy beta non-Solaris NFSv4 servers at
7949 	 * interop testing events have allowed this behavior,
7950 	 * and it caused our client to panic due to a recursive
7951 	 * mutex_enter in fn_move.
7952 	 *
7953 	 * The tedious locking in fn_move could be changed to
7954 	 * deal with this case, and the client could avoid the
7955 	 * panic; however, the client would just confuse itself
7956 	 * later and misbehave.  A better way to handle the broken
7957 	 * server is to detect this condition and return EINVAL
7958 	 * without ever sending the the bogus rename to the server.
7959 	 * We know the rename is invalid -- just fail it now.
7960 	 */
7961 	if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7962 		VN_RELE(ovp);
7963 		nfs_rw_exit(&odrp->r_rwlock);
7964 		nfs_rw_exit(&ndrp->r_rwlock);
7965 		if (nvp) {
7966 			VN_RELE(nvp);
7967 		}
7968 		return (EINVAL);
7969 	}
7970 
7971 	(void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7972 
7973 	/*
7974 	 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7975 	 * possible for the filehandle to change due to the rename.
7976 	 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7977 	 * the fh will not change because of the rename, but we still need
7978 	 * to update its rnode entry with the new name for
7979 	 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7980 	 * has no effect on these for now, but for future improvements,
7981 	 * we might want to use it too to simplify handling of files
7982 	 * that are open with that flag on. (XXX)
7983 	 */
7984 	mi = VTOMI4(odvp);
7985 	if (NFS4_VOLATILE_FH(mi))
7986 		error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7987 		    &stat);
7988 	else
7989 		error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7990 		    &stat);
7991 
7992 	ASSERT(nfs4_consistent_type(odvp));
7993 	ASSERT(nfs4_consistent_type(ndvp));
7994 	ASSERT(nfs4_consistent_type(ovp));
7995 
7996 	if (stat == NFS4ERR_FILE_OPEN && did_link) {
7997 		do_link = 0;
7998 		/*
7999 		 * Before the 'link_call' code, we did a nfs4_lookup
8000 		 * that puts a VN_HOLD on nvp.  After the nfs4_link
8001 		 * call we call VN_RELE to match that hold.  We need
8002 		 * to place an additional VN_HOLD here since we will
8003 		 * be hitting that VN_RELE again.
8004 		 */
8005 		VN_HOLD(nvp);
8006 
8007 		(void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
8008 
8009 		/* Undo the unlinked file naming stuff we just did */
8010 		mutex_enter(&rp->r_statelock);
8011 		if (rp->r_unldvp) {
8012 			VN_RELE(ndvp);
8013 			rp->r_unldvp = NULL;
8014 			if (rp->r_unlcred != NULL)
8015 				crfree(rp->r_unlcred);
8016 			rp->r_unlcred = NULL;
8017 			/* rp->r_unlanme points to tmpname */
8018 			if (rp->r_unlname)
8019 				kmem_free(rp->r_unlname, MAXNAMELEN);
8020 			rp->r_unlname = NULL;
8021 		}
8022 		mutex_exit(&rp->r_statelock);
8023 
8024 		if (nvp) {
8025 			VN_RELE(nvp);
8026 		}
8027 		goto link_call;
8028 	}
8029 
8030 	if (error) {
8031 		VN_RELE(ovp);
8032 		nfs_rw_exit(&odrp->r_rwlock);
8033 		nfs_rw_exit(&ndrp->r_rwlock);
8034 		if (nvp) {
8035 			VN_RELE(nvp);
8036 		}
8037 		return (error);
8038 	}
8039 
8040 	/*
8041 	 * when renaming directories to be a subdirectory of a
8042 	 * different parent, the dnlc entry for ".." will no
8043 	 * longer be valid, so it must be removed
8044 	 */
8045 	rp = VTOR4(ovp);
8046 	if (ndvp != odvp) {
8047 		if (ovp->v_type == VDIR) {
8048 			dnlc_remove(ovp, "..");
8049 			if (rp->r_dir != NULL)
8050 				nfs4_purge_rddir_cache(ovp);
8051 		}
8052 	}
8053 
8054 	/*
8055 	 * If we are renaming the unlinked file, update the
8056 	 * r_unldvp and r_unlname as needed.
8057 	 */
8058 	mutex_enter(&rp->r_statelock);
8059 	if (rp->r_unldvp != NULL) {
8060 		if (strcmp(rp->r_unlname, onm) == 0) {
8061 			(void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8062 			rp->r_unlname[MAXNAMELEN - 1] = '\0';
8063 			if (ndvp != rp->r_unldvp) {
8064 				VN_RELE(rp->r_unldvp);
8065 				rp->r_unldvp = ndvp;
8066 				VN_HOLD(ndvp);
8067 			}
8068 		}
8069 	}
8070 	mutex_exit(&rp->r_statelock);
8071 
8072 	/*
8073 	 * Notify the rename vnevents to source vnode, and to the target
8074 	 * vnode if it already existed.
8075 	 */
8076 	if (error == 0) {
8077 		vnode_t *tvp;
8078 		rnode4_t *trp;
8079 		/*
8080 		 * Notify the vnode. Each links is represented by
8081 		 * a different vnode, in nfsv4.
8082 		 */
8083 		if (nvp) {
8084 			trp = VTOR4(nvp);
8085 			tvp = nvp;
8086 			if (IS_SHADOW(nvp, trp))
8087 				tvp = RTOV4(trp);
8088 			vnevent_rename_dest(tvp, ndvp, nnm, ct);
8089 		}
8090 
8091 		/*
8092 		 * if the source and destination directory are not the
8093 		 * same notify the destination directory.
8094 		 */
8095 		if (VTOR4(odvp) != VTOR4(ndvp)) {
8096 			trp = VTOR4(ndvp);
8097 			tvp = ndvp;
8098 			if (IS_SHADOW(ndvp, trp))
8099 				tvp = RTOV4(trp);
8100 			vnevent_rename_dest_dir(tvp, ct);
8101 		}
8102 
8103 		trp = VTOR4(ovp);
8104 		tvp = ovp;
8105 		if (IS_SHADOW(ovp, trp))
8106 			tvp = RTOV4(trp);
8107 		vnevent_rename_src(tvp, odvp, onm, ct);
8108 	}
8109 
8110 	if (nvp) {
8111 		VN_RELE(nvp);
8112 	}
8113 	VN_RELE(ovp);
8114 
8115 	nfs_rw_exit(&odrp->r_rwlock);
8116 	nfs_rw_exit(&ndrp->r_rwlock);
8117 
8118 	return (error);
8119 }
8120 
8121 /*
8122  * When the parent directory has changed, sv_dfh must be updated
8123  */
8124 static void
update_parentdir_sfh(vnode_t * vp,vnode_t * ndvp)8125 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8126 {
8127 	svnode_t *sv = VTOSV(vp);
8128 	nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8129 	nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8130 
8131 	sfh4_hold(new_dfh);
8132 	sv->sv_dfh = new_dfh;
8133 	sfh4_rele(&old_dfh);
8134 }
8135 
8136 /*
8137  * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8138  * when it is known that the filehandle is persistent through rename.
8139  *
8140  * Rename requires that the current fh be the target directory and the
8141  * saved fh be the source directory. After the operation, the current fh
8142  * is unchanged.
8143  * The compound op structure for persistent fh rename is:
8144  *      PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8145  * Rather than bother with the directory postop args, we'll simply
8146  * update that a change occurred in the cache, so no post-op getattrs.
8147  */
8148 static int
nfs4rename_persistent_fh(vnode_t * odvp,char * onm,vnode_t * renvp,vnode_t * ndvp,char * nnm,cred_t * cr,nfsstat4 * statp)8149 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8150     vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8151 {
8152 	COMPOUND4args_clnt args;
8153 	COMPOUND4res_clnt res, *resp = NULL;
8154 	nfs_argop4 *argop;
8155 	nfs_resop4 *resop;
8156 	int doqueue, argoplist_size;
8157 	mntinfo4_t *mi;
8158 	rnode4_t *odrp = VTOR4(odvp);
8159 	rnode4_t *ndrp = VTOR4(ndvp);
8160 	RENAME4res *rn_res;
8161 	bool_t needrecov;
8162 	nfs4_recov_state_t recov_state;
8163 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8164 	dirattr_info_t dinfo, *dinfop;
8165 
8166 	ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8167 
8168 	recov_state.rs_flags = 0;
8169 	recov_state.rs_num_retry_despite_err = 0;
8170 
8171 	/*
8172 	 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8173 	 *
8174 	 * If source/target are different dirs, then append putfh(src); getattr
8175 	 */
8176 	args.array_len = (odvp == ndvp) ? 5 : 7;
8177 	argoplist_size = args.array_len * sizeof (nfs_argop4);
8178 	args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8179 
8180 recov_retry:
8181 	*statp = NFS4_OK;
8182 
8183 	/* No need to Lookup the file, persistent fh */
8184 	args.ctag = TAG_RENAME;
8185 
8186 	mi = VTOMI4(odvp);
8187 	e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8188 	if (e.error) {
8189 		kmem_free(argop, argoplist_size);
8190 		return (e.error);
8191 	}
8192 
8193 	/* 0: putfh source directory */
8194 	argop[0].argop = OP_CPUTFH;
8195 	argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8196 
8197 	/* 1: Save source fh to free up current for target */
8198 	argop[1].argop = OP_SAVEFH;
8199 
8200 	/* 2: putfh targetdir */
8201 	argop[2].argop = OP_CPUTFH;
8202 	argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8203 
8204 	/* 3: current_fh is targetdir, saved_fh is sourcedir */
8205 	argop[3].argop = OP_CRENAME;
8206 	argop[3].nfs_argop4_u.opcrename.coldname = onm;
8207 	argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8208 
8209 	/* 4: getattr (targetdir) */
8210 	argop[4].argop = OP_GETATTR;
8211 	argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8212 	argop[4].nfs_argop4_u.opgetattr.mi = mi;
8213 
8214 	if (ndvp != odvp) {
8215 
8216 		/* 5: putfh (sourcedir) */
8217 		argop[5].argop = OP_CPUTFH;
8218 		argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8219 
8220 		/* 6: getattr (sourcedir) */
8221 		argop[6].argop = OP_GETATTR;
8222 		argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8223 		argop[6].nfs_argop4_u.opgetattr.mi = mi;
8224 	}
8225 
8226 	dnlc_remove(odvp, onm);
8227 	dnlc_remove(ndvp, nnm);
8228 
8229 	doqueue = 1;
8230 	dinfo.di_time_call = gethrtime();
8231 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8232 
8233 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8234 	if (e.error) {
8235 		PURGE_ATTRCACHE4(odvp);
8236 		PURGE_ATTRCACHE4(ndvp);
8237 	} else {
8238 		*statp = res.status;
8239 	}
8240 
8241 	if (needrecov) {
8242 		if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8243 		    OP_RENAME, NULL, NULL, NULL) == FALSE) {
8244 			nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8245 			if (!e.error)
8246 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8247 			goto recov_retry;
8248 		}
8249 	}
8250 
8251 	if (!e.error) {
8252 		resp = &res;
8253 		/*
8254 		 * as long as OP_RENAME
8255 		 */
8256 		if (res.status != NFS4_OK && res.array_len <= 4) {
8257 			e.error = geterrno4(res.status);
8258 			PURGE_ATTRCACHE4(odvp);
8259 			PURGE_ATTRCACHE4(ndvp);
8260 			/*
8261 			 * System V defines rename to return EEXIST, not
8262 			 * ENOTEMPTY if the target directory is not empty.
8263 			 * Over the wire, the error is NFSERR_ENOTEMPTY
8264 			 * which geterrno4 maps to ENOTEMPTY.
8265 			 */
8266 			if (e.error == ENOTEMPTY)
8267 				e.error = EEXIST;
8268 		} else {
8269 
8270 			resop = &res.array[3];	/* rename res */
8271 			rn_res = &resop->nfs_resop4_u.oprename;
8272 
8273 			if (res.status == NFS4_OK) {
8274 				/*
8275 				 * Update target attribute, readdir and dnlc
8276 				 * caches.
8277 				 */
8278 				dinfo.di_garp =
8279 				    &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8280 				dinfo.di_cred = cr;
8281 				dinfop = &dinfo;
8282 			} else
8283 				dinfop = NULL;
8284 
8285 			nfs4_update_dircaches(&rn_res->target_cinfo,
8286 			    ndvp, NULL, NULL, dinfop);
8287 
8288 			/*
8289 			 * Update source attribute, readdir and dnlc caches
8290 			 *
8291 			 */
8292 			if (ndvp != odvp) {
8293 				update_parentdir_sfh(renvp, ndvp);
8294 
8295 				if (dinfop)
8296 					dinfo.di_garp =
8297 					    &(res.array[6].nfs_resop4_u.
8298 					    opgetattr.ga_res);
8299 
8300 				nfs4_update_dircaches(&rn_res->source_cinfo,
8301 				    odvp, NULL, NULL, dinfop);
8302 			}
8303 
8304 			fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8305 			    nnm);
8306 		}
8307 	}
8308 
8309 	if (resp)
8310 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8311 	nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8312 	kmem_free(argop, argoplist_size);
8313 
8314 	return (e.error);
8315 }
8316 
8317 /*
8318  * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8319  * it is possible for the filehandle to change due to the rename.
8320  *
8321  * The compound req in this case includes a post-rename lookup and getattr
8322  * to ensure that we have the correct fh and attributes for the object.
8323  *
8324  * Rename requires that the current fh be the target directory and the
8325  * saved fh be the source directory. After the operation, the current fh
8326  * is unchanged.
8327  *
8328  * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8329  * update the filehandle for the renamed object.  We also get the old
8330  * filehandle for historical reasons; this should be taken out sometime.
8331  * This results in a rather cumbersome compound...
8332  *
8333  *    PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8334  *    PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8335  *
8336  */
8337 static int
nfs4rename_volatile_fh(vnode_t * odvp,char * onm,vnode_t * ovp,vnode_t * ndvp,char * nnm,cred_t * cr,nfsstat4 * statp)8338 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8339     vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8340 {
8341 	COMPOUND4args_clnt args;
8342 	COMPOUND4res_clnt res, *resp = NULL;
8343 	int argoplist_size;
8344 	nfs_argop4 *argop;
8345 	nfs_resop4 *resop;
8346 	int doqueue;
8347 	mntinfo4_t *mi;
8348 	rnode4_t *odrp = VTOR4(odvp);	/* old directory */
8349 	rnode4_t *ndrp = VTOR4(ndvp);	/* new directory */
8350 	rnode4_t *orp = VTOR4(ovp);	/* object being renamed */
8351 	RENAME4res *rn_res;
8352 	GETFH4res *ngf_res;
8353 	bool_t needrecov;
8354 	nfs4_recov_state_t recov_state;
8355 	hrtime_t t;
8356 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8357 	dirattr_info_t dinfo, *dinfop = &dinfo;
8358 
8359 	ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8360 
8361 	recov_state.rs_flags = 0;
8362 	recov_state.rs_num_retry_despite_err = 0;
8363 
8364 recov_retry:
8365 	*statp = NFS4_OK;
8366 
8367 	/*
8368 	 * There is a window between the RPC and updating the path and
8369 	 * filehandle stored in the rnode.  Lock out the FHEXPIRED recovery
8370 	 * code, so that it doesn't try to use the old path during that
8371 	 * window.
8372 	 */
8373 	mutex_enter(&orp->r_statelock);
8374 	while (orp->r_flags & R4RECEXPFH) {
8375 		klwp_t *lwp = ttolwp(curthread);
8376 
8377 		if (lwp != NULL)
8378 			lwp->lwp_nostop++;
8379 		if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8380 			mutex_exit(&orp->r_statelock);
8381 			if (lwp != NULL)
8382 				lwp->lwp_nostop--;
8383 			return (EINTR);
8384 		}
8385 		if (lwp != NULL)
8386 			lwp->lwp_nostop--;
8387 	}
8388 	orp->r_flags |= R4RECEXPFH;
8389 	mutex_exit(&orp->r_statelock);
8390 
8391 	mi = VTOMI4(odvp);
8392 
8393 	args.ctag = TAG_RENAME_VFH;
8394 	args.array_len = (odvp == ndvp) ? 10 : 12;
8395 	argoplist_size  = args.array_len * sizeof (nfs_argop4);
8396 	argop = kmem_alloc(argoplist_size, KM_SLEEP);
8397 
8398 	/*
8399 	 * Rename ops:
8400 	 *    PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8401 	 *    PUTFH(targetdir), RENAME, GETATTR(targetdir)
8402 	 *    LOOKUP(trgt), GETFH(new), GETATTR,
8403 	 *
8404 	 *    if (odvp != ndvp)
8405 	 *	add putfh(sourcedir), getattr(sourcedir) }
8406 	 */
8407 	args.array = argop;
8408 
8409 	e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8410 	    &recov_state, NULL);
8411 	if (e.error) {
8412 		kmem_free(argop, argoplist_size);
8413 		mutex_enter(&orp->r_statelock);
8414 		orp->r_flags &= ~R4RECEXPFH;
8415 		cv_broadcast(&orp->r_cv);
8416 		mutex_exit(&orp->r_statelock);
8417 		return (e.error);
8418 	}
8419 
8420 	/* 0: putfh source directory */
8421 	argop[0].argop = OP_CPUTFH;
8422 	argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8423 
8424 	/* 1: Save source fh to free up current for target */
8425 	argop[1].argop = OP_SAVEFH;
8426 
8427 	/* 2: Lookup pre-rename fh of renamed object */
8428 	argop[2].argop = OP_CLOOKUP;
8429 	argop[2].nfs_argop4_u.opclookup.cname = onm;
8430 
8431 	/* 3: getfh fh of renamed object (before rename) */
8432 	argop[3].argop = OP_GETFH;
8433 
8434 	/* 4: putfh targetdir */
8435 	argop[4].argop = OP_CPUTFH;
8436 	argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8437 
8438 	/* 5: current_fh is targetdir, saved_fh is sourcedir */
8439 	argop[5].argop = OP_CRENAME;
8440 	argop[5].nfs_argop4_u.opcrename.coldname = onm;
8441 	argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8442 
8443 	/* 6: getattr of target dir (post op attrs) */
8444 	argop[6].argop = OP_GETATTR;
8445 	argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8446 	argop[6].nfs_argop4_u.opgetattr.mi = mi;
8447 
8448 	/* 7: Lookup post-rename fh of renamed object */
8449 	argop[7].argop = OP_CLOOKUP;
8450 	argop[7].nfs_argop4_u.opclookup.cname = nnm;
8451 
8452 	/* 8: getfh fh of renamed object (after rename) */
8453 	argop[8].argop = OP_GETFH;
8454 
8455 	/* 9: getattr of renamed object */
8456 	argop[9].argop = OP_GETATTR;
8457 	argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8458 	argop[9].nfs_argop4_u.opgetattr.mi = mi;
8459 
8460 	/*
8461 	 * If source/target dirs are different, then get new post-op
8462 	 * attrs for source dir also.
8463 	 */
8464 	if (ndvp != odvp) {
8465 		/* 10: putfh (sourcedir) */
8466 		argop[10].argop = OP_CPUTFH;
8467 		argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8468 
8469 		/* 11: getattr (sourcedir) */
8470 		argop[11].argop = OP_GETATTR;
8471 		argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8472 		argop[11].nfs_argop4_u.opgetattr.mi = mi;
8473 	}
8474 
8475 	dnlc_remove(odvp, onm);
8476 	dnlc_remove(ndvp, nnm);
8477 
8478 	doqueue = 1;
8479 	t = gethrtime();
8480 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8481 
8482 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8483 	if (e.error) {
8484 		PURGE_ATTRCACHE4(odvp);
8485 		PURGE_ATTRCACHE4(ndvp);
8486 		if (!needrecov) {
8487 			nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8488 			    &recov_state, needrecov);
8489 			goto out;
8490 		}
8491 	} else {
8492 		*statp = res.status;
8493 	}
8494 
8495 	if (needrecov) {
8496 		bool_t abort;
8497 
8498 		abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8499 		    OP_RENAME, NULL, NULL, NULL);
8500 		if (abort == FALSE) {
8501 			nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8502 			    &recov_state, needrecov);
8503 			kmem_free(argop, argoplist_size);
8504 			if (!e.error)
8505 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8506 			mutex_enter(&orp->r_statelock);
8507 			orp->r_flags &= ~R4RECEXPFH;
8508 			cv_broadcast(&orp->r_cv);
8509 			mutex_exit(&orp->r_statelock);
8510 			goto recov_retry;
8511 		} else {
8512 			if (e.error != 0) {
8513 				nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8514 				    &recov_state, needrecov);
8515 				goto out;
8516 			}
8517 			/* fall through for res.status case */
8518 		}
8519 	}
8520 
8521 	resp = &res;
8522 	/*
8523 	 * If OP_RENAME (or any prev op) failed, then return an error.
8524 	 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8525 	 */
8526 	if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8527 		/*
8528 		 * Error in an op other than last Getattr
8529 		 */
8530 		e.error = geterrno4(res.status);
8531 		PURGE_ATTRCACHE4(odvp);
8532 		PURGE_ATTRCACHE4(ndvp);
8533 		/*
8534 		 * System V defines rename to return EEXIST, not
8535 		 * ENOTEMPTY if the target directory is not empty.
8536 		 * Over the wire, the error is NFSERR_ENOTEMPTY
8537 		 * which geterrno4 maps to ENOTEMPTY.
8538 		 */
8539 		if (e.error == ENOTEMPTY)
8540 			e.error = EEXIST;
8541 		nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8542 		    needrecov);
8543 		goto out;
8544 	}
8545 
8546 	/* rename results */
8547 	rn_res = &res.array[5].nfs_resop4_u.oprename;
8548 
8549 	if (res.status == NFS4_OK) {
8550 		/* Update target attribute, readdir and dnlc caches */
8551 		dinfo.di_garp =
8552 		    &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8553 		dinfo.di_cred = cr;
8554 		dinfo.di_time_call = t;
8555 	} else
8556 		dinfop = NULL;
8557 
8558 	/* Update source cache attribute, readdir and dnlc caches */
8559 	nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8560 
8561 	/* Update source cache attribute, readdir and dnlc caches */
8562 	if (ndvp != odvp) {
8563 		update_parentdir_sfh(ovp, ndvp);
8564 
8565 		/*
8566 		 * If dinfop is non-NULL, then compound succeded, so
8567 		 * set di_garp to attrs for source dir.  dinfop is only
8568 		 * set to NULL when compound fails.
8569 		 */
8570 		if (dinfop)
8571 			dinfo.di_garp =
8572 			    &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8573 		nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8574 		    dinfop);
8575 	}
8576 
8577 	/*
8578 	 * Update the rnode with the new component name and args,
8579 	 * and if the file handle changed, also update it with the new fh.
8580 	 * This is only necessary if the target object has an rnode
8581 	 * entry and there is no need to create one for it.
8582 	 */
8583 	resop = &res.array[8];	/* getfh new res */
8584 	ngf_res = &resop->nfs_resop4_u.opgetfh;
8585 
8586 	/*
8587 	 * Update the path and filehandle for the renamed object.
8588 	 */
8589 	nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8590 
8591 	nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8592 
8593 	if (res.status == NFS4_OK) {
8594 		resop++;	/* getattr res */
8595 		e.error = nfs4_update_attrcache(res.status,
8596 		    &resop->nfs_resop4_u.opgetattr.ga_res,
8597 		    t, ovp, cr);
8598 	}
8599 
8600 out:
8601 	kmem_free(argop, argoplist_size);
8602 	if (resp)
8603 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8604 	mutex_enter(&orp->r_statelock);
8605 	orp->r_flags &= ~R4RECEXPFH;
8606 	cv_broadcast(&orp->r_cv);
8607 	mutex_exit(&orp->r_statelock);
8608 
8609 	return (e.error);
8610 }
8611 
8612 /* ARGSUSED */
8613 static int
nfs4_mkdir(vnode_t * dvp,char * nm,struct vattr * va,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)8614 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8615     caller_context_t *ct, int flags, vsecattr_t *vsecp)
8616 {
8617 	int error;
8618 	vnode_t *vp;
8619 
8620 	if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8621 		return (EPERM);
8622 	/*
8623 	 * As ".." has special meaning and rather than send a mkdir
8624 	 * over the wire to just let the server freak out, we just
8625 	 * short circuit it here and return EEXIST
8626 	 */
8627 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8628 		return (EEXIST);
8629 
8630 	/*
8631 	 * Decision to get the right gid and setgid bit of the
8632 	 * new directory is now made in call_nfs4_create_req.
8633 	 */
8634 	va->va_mask |= AT_MODE;
8635 	error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8636 	if (error)
8637 		return (error);
8638 
8639 	*vpp = vp;
8640 	return (0);
8641 }
8642 
8643 
8644 /*
8645  * rmdir is using the same remove v4 op as does remove.
8646  * Remove requires that the current fh be the target directory.
8647  * After the operation, the current fh is unchanged.
8648  * The compound op structure is:
8649  *      PUTFH(targetdir), REMOVE
8650  */
8651 /*ARGSUSED4*/
8652 static int
nfs4_rmdir(vnode_t * dvp,char * nm,vnode_t * cdir,cred_t * cr,caller_context_t * ct,int flags)8653 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8654     caller_context_t *ct, int flags)
8655 {
8656 	int need_end_op = FALSE;
8657 	COMPOUND4args_clnt args;
8658 	COMPOUND4res_clnt res, *resp = NULL;
8659 	REMOVE4res *rm_res;
8660 	nfs_argop4 argop[3];
8661 	nfs_resop4 *resop;
8662 	vnode_t *vp;
8663 	int doqueue;
8664 	mntinfo4_t *mi;
8665 	rnode4_t *drp;
8666 	bool_t needrecov = FALSE;
8667 	nfs4_recov_state_t recov_state;
8668 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8669 	dirattr_info_t dinfo, *dinfop;
8670 
8671 	if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8672 		return (EPERM);
8673 	/*
8674 	 * As ".." has special meaning and rather than send a rmdir
8675 	 * over the wire to just let the server freak out, we just
8676 	 * short circuit it here and return EEXIST
8677 	 */
8678 	if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8679 		return (EEXIST);
8680 
8681 	drp = VTOR4(dvp);
8682 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8683 		return (EINTR);
8684 
8685 	/*
8686 	 * Attempt to prevent a rmdir(".") from succeeding.
8687 	 */
8688 	e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8689 	if (e.error) {
8690 		nfs_rw_exit(&drp->r_rwlock);
8691 		return (e.error);
8692 	}
8693 	if (vp == cdir) {
8694 		VN_RELE(vp);
8695 		nfs_rw_exit(&drp->r_rwlock);
8696 		return (EINVAL);
8697 	}
8698 
8699 	/*
8700 	 * Since nfsv4 remove op works on both files and directories,
8701 	 * check that the removed object is indeed a directory.
8702 	 */
8703 	if (vp->v_type != VDIR) {
8704 		VN_RELE(vp);
8705 		nfs_rw_exit(&drp->r_rwlock);
8706 		return (ENOTDIR);
8707 	}
8708 
8709 	/*
8710 	 * First just remove the entry from the name cache, as it
8711 	 * is most likely an entry for this vp.
8712 	 */
8713 	dnlc_remove(dvp, nm);
8714 
8715 	/*
8716 	 * If there vnode reference count is greater than one, then
8717 	 * there may be additional references in the DNLC which will
8718 	 * need to be purged.  First, trying removing the entry for
8719 	 * the parent directory and see if that removes the additional
8720 	 * reference(s).  If that doesn't do it, then use dnlc_purge_vp
8721 	 * to completely remove any references to the directory which
8722 	 * might still exist in the DNLC.
8723 	 */
8724 	if (vp->v_count > 1) {
8725 		dnlc_remove(vp, "..");
8726 		if (vp->v_count > 1)
8727 			dnlc_purge_vp(vp);
8728 	}
8729 
8730 	mi = VTOMI4(dvp);
8731 	recov_state.rs_flags = 0;
8732 	recov_state.rs_num_retry_despite_err = 0;
8733 
8734 recov_retry:
8735 	args.ctag = TAG_RMDIR;
8736 
8737 	/*
8738 	 * Rmdir ops: putfh dir; remove
8739 	 */
8740 	args.array_len = 3;
8741 	args.array = argop;
8742 
8743 	e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8744 	if (e.error) {
8745 		nfs_rw_exit(&drp->r_rwlock);
8746 		return (e.error);
8747 	}
8748 	need_end_op = TRUE;
8749 
8750 	/* putfh directory */
8751 	argop[0].argop = OP_CPUTFH;
8752 	argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8753 
8754 	/* remove */
8755 	argop[1].argop = OP_CREMOVE;
8756 	argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8757 
8758 	/* getattr (postop attrs for dir that contained removed dir) */
8759 	argop[2].argop = OP_GETATTR;
8760 	argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8761 	argop[2].nfs_argop4_u.opgetattr.mi = mi;
8762 
8763 	dinfo.di_time_call = gethrtime();
8764 	doqueue = 1;
8765 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8766 
8767 	PURGE_ATTRCACHE4(vp);
8768 
8769 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8770 	if (e.error) {
8771 		PURGE_ATTRCACHE4(dvp);
8772 	}
8773 
8774 	if (needrecov) {
8775 		if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8776 		    NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8777 			if (!e.error)
8778 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8779 
8780 			nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8781 			    needrecov);
8782 			need_end_op = FALSE;
8783 			goto recov_retry;
8784 		}
8785 	}
8786 
8787 	if (!e.error) {
8788 		resp = &res;
8789 
8790 		/*
8791 		 * Only return error if first 2 ops (OP_REMOVE or earlier)
8792 		 * failed.
8793 		 */
8794 		if (res.status != NFS4_OK && res.array_len <= 2) {
8795 			e.error = geterrno4(res.status);
8796 			PURGE_ATTRCACHE4(dvp);
8797 			nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8798 			    &recov_state, needrecov);
8799 			need_end_op = FALSE;
8800 			nfs4_purge_stale_fh(e.error, dvp, cr);
8801 			/*
8802 			 * System V defines rmdir to return EEXIST, not
8803 			 * ENOTEMPTY if the directory is not empty.  Over
8804 			 * the wire, the error is NFSERR_ENOTEMPTY which
8805 			 * geterrno4 maps to ENOTEMPTY.
8806 			 */
8807 			if (e.error == ENOTEMPTY)
8808 				e.error = EEXIST;
8809 		} else {
8810 			resop = &res.array[1];	/* remove res */
8811 			rm_res = &resop->nfs_resop4_u.opremove;
8812 
8813 			if (res.status == NFS4_OK) {
8814 				resop = &res.array[2];	/* dir attrs */
8815 				dinfo.di_garp =
8816 				    &resop->nfs_resop4_u.opgetattr.ga_res;
8817 				dinfo.di_cred = cr;
8818 				dinfop = &dinfo;
8819 			} else
8820 				dinfop = NULL;
8821 
8822 			/* Update dir attribute, readdir and dnlc caches */
8823 			nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8824 			    dinfop);
8825 
8826 			/* destroy rddir cache for dir that was removed */
8827 			if (VTOR4(vp)->r_dir != NULL)
8828 				nfs4_purge_rddir_cache(vp);
8829 		}
8830 	}
8831 
8832 	if (need_end_op)
8833 		nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8834 
8835 	nfs_rw_exit(&drp->r_rwlock);
8836 
8837 	if (resp)
8838 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8839 
8840 	if (e.error == 0) {
8841 		vnode_t *tvp;
8842 		rnode4_t *trp;
8843 		trp = VTOR4(vp);
8844 		tvp = vp;
8845 		if (IS_SHADOW(vp, trp))
8846 			tvp = RTOV4(trp);
8847 		vnevent_rmdir(tvp, dvp, nm, ct);
8848 	}
8849 
8850 	VN_RELE(vp);
8851 
8852 	return (e.error);
8853 }
8854 
8855 /* ARGSUSED */
8856 static int
nfs4_symlink(vnode_t * dvp,char * lnm,struct vattr * tva,char * tnm,cred_t * cr,caller_context_t * ct,int flags)8857 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8858     caller_context_t *ct, int flags)
8859 {
8860 	int error;
8861 	vnode_t *vp;
8862 	rnode4_t *rp;
8863 	char *contents;
8864 	mntinfo4_t *mi = VTOMI4(dvp);
8865 
8866 	if (nfs_zone() != mi->mi_zone)
8867 		return (EPERM);
8868 	if (!(mi->mi_flags & MI4_SYMLINK))
8869 		return (EOPNOTSUPP);
8870 
8871 	error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8872 	if (error)
8873 		return (error);
8874 
8875 	ASSERT(nfs4_consistent_type(vp));
8876 	rp = VTOR4(vp);
8877 	if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8878 
8879 		contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8880 
8881 		if (contents != NULL) {
8882 			mutex_enter(&rp->r_statelock);
8883 			if (rp->r_symlink.contents == NULL) {
8884 				rp->r_symlink.len = strlen(tnm);
8885 				bcopy(tnm, contents, rp->r_symlink.len);
8886 				rp->r_symlink.contents = contents;
8887 				rp->r_symlink.size = MAXPATHLEN;
8888 				mutex_exit(&rp->r_statelock);
8889 			} else {
8890 				mutex_exit(&rp->r_statelock);
8891 				kmem_free((void *)contents, MAXPATHLEN);
8892 			}
8893 		}
8894 	}
8895 	VN_RELE(vp);
8896 
8897 	return (error);
8898 }
8899 
8900 
8901 /*
8902  * Read directory entries.
8903  * There are some weird things to look out for here.  The uio_loffset
8904  * field is either 0 or it is the offset returned from a previous
8905  * readdir.  It is an opaque value used by the server to find the
8906  * correct directory block to read. The count field is the number
8907  * of blocks to read on the server.  This is advisory only, the server
8908  * may return only one block's worth of entries.  Entries may be compressed
8909  * on the server.
8910  */
8911 /* ARGSUSED */
8912 static int
nfs4_readdir(vnode_t * vp,struct uio * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)8913 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8914     caller_context_t *ct, int flags)
8915 {
8916 	int error;
8917 	uint_t count;
8918 	rnode4_t *rp;
8919 	rddir4_cache *rdc;
8920 	rddir4_cache *rrdc;
8921 
8922 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
8923 		return (EIO);
8924 	rp = VTOR4(vp);
8925 
8926 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8927 
8928 	/*
8929 	 * Make sure that the directory cache is valid.
8930 	 */
8931 	if (rp->r_dir != NULL) {
8932 		if (nfs_disable_rddir_cache != 0) {
8933 			/*
8934 			 * Setting nfs_disable_rddir_cache in /etc/system
8935 			 * allows interoperability with servers that do not
8936 			 * properly update the attributes of directories.
8937 			 * Any cached information gets purged before an
8938 			 * access is made to it.
8939 			 */
8940 			nfs4_purge_rddir_cache(vp);
8941 		}
8942 
8943 		error = nfs4_validate_caches(vp, cr);
8944 		if (error)
8945 			return (error);
8946 	}
8947 
8948 	count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8949 
8950 	/*
8951 	 * Short circuit last readdir which always returns 0 bytes.
8952 	 * This can be done after the directory has been read through
8953 	 * completely at least once.  This will set r_direof which
8954 	 * can be used to find the value of the last cookie.
8955 	 */
8956 	mutex_enter(&rp->r_statelock);
8957 	if (rp->r_direof != NULL &&
8958 	    uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8959 		mutex_exit(&rp->r_statelock);
8960 #ifdef DEBUG
8961 		nfs4_readdir_cache_shorts++;
8962 #endif
8963 		if (eofp)
8964 			*eofp = 1;
8965 		return (0);
8966 	}
8967 
8968 	/*
8969 	 * Look for a cache entry.  Cache entries are identified
8970 	 * by the NFS cookie value and the byte count requested.
8971 	 */
8972 	rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8973 
8974 	/*
8975 	 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8976 	 */
8977 	if (rdc == NULL) {
8978 		mutex_exit(&rp->r_statelock);
8979 		return (EINTR);
8980 	}
8981 
8982 	/*
8983 	 * Check to see if we need to fill this entry in.
8984 	 */
8985 	if (rdc->flags & RDDIRREQ) {
8986 		rdc->flags &= ~RDDIRREQ;
8987 		rdc->flags |= RDDIR;
8988 		mutex_exit(&rp->r_statelock);
8989 
8990 		/*
8991 		 * Do the readdir.
8992 		 */
8993 		nfs4readdir(vp, rdc, cr);
8994 
8995 		/*
8996 		 * Reacquire the lock, so that we can continue
8997 		 */
8998 		mutex_enter(&rp->r_statelock);
8999 		/*
9000 		 * The entry is now complete
9001 		 */
9002 		rdc->flags &= ~RDDIR;
9003 	}
9004 
9005 	ASSERT(!(rdc->flags & RDDIR));
9006 
9007 	/*
9008 	 * If an error occurred while attempting
9009 	 * to fill the cache entry, mark the entry invalid and
9010 	 * just return the error.
9011 	 */
9012 	if (rdc->error) {
9013 		error = rdc->error;
9014 		rdc->flags |= RDDIRREQ;
9015 		rddir4_cache_rele(rp, rdc);
9016 		mutex_exit(&rp->r_statelock);
9017 		return (error);
9018 	}
9019 
9020 	/*
9021 	 * The cache entry is complete and good,
9022 	 * copyout the dirent structs to the calling
9023 	 * thread.
9024 	 */
9025 	error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9026 
9027 	/*
9028 	 * If no error occurred during the copyout,
9029 	 * update the offset in the uio struct to
9030 	 * contain the value of the next NFS 4 cookie
9031 	 * and set the eof value appropriately.
9032 	 */
9033 	if (!error) {
9034 		uiop->uio_loffset = rdc->nfs4_ncookie;
9035 		if (eofp)
9036 			*eofp = rdc->eof;
9037 	}
9038 
9039 	/*
9040 	 * Decide whether to do readahead.  Don't if we
9041 	 * have already read to the end of directory.
9042 	 */
9043 	if (rdc->eof) {
9044 		/*
9045 		 * Make the entry the direof only if it is cached
9046 		 */
9047 		if (rdc->flags & RDDIRCACHED)
9048 			rp->r_direof = rdc;
9049 		rddir4_cache_rele(rp, rdc);
9050 		mutex_exit(&rp->r_statelock);
9051 		return (error);
9052 	}
9053 
9054 	/* Determine if a readdir readahead should be done */
9055 	if (!(rp->r_flags & R4LOOKUP)) {
9056 		rddir4_cache_rele(rp, rdc);
9057 		mutex_exit(&rp->r_statelock);
9058 		return (error);
9059 	}
9060 
9061 	/*
9062 	 * Now look for a readahead entry.
9063 	 *
9064 	 * Check to see whether we found an entry for the readahead.
9065 	 * If so, we don't need to do anything further, so free the new
9066 	 * entry if one was allocated.  Otherwise, allocate a new entry, add
9067 	 * it to the cache, and then initiate an asynchronous readdir
9068 	 * operation to fill it.
9069 	 */
9070 	rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9071 
9072 	/*
9073 	 * A readdir cache entry could not be obtained for the readahead.  In
9074 	 * this case we skip the readahead and return.
9075 	 */
9076 	if (rrdc == NULL) {
9077 		rddir4_cache_rele(rp, rdc);
9078 		mutex_exit(&rp->r_statelock);
9079 		return (error);
9080 	}
9081 
9082 	/*
9083 	 * Check to see if we need to fill this entry in.
9084 	 */
9085 	if (rrdc->flags & RDDIRREQ) {
9086 		rrdc->flags &= ~RDDIRREQ;
9087 		rrdc->flags |= RDDIR;
9088 		rddir4_cache_rele(rp, rdc);
9089 		mutex_exit(&rp->r_statelock);
9090 #ifdef DEBUG
9091 		nfs4_readdir_readahead++;
9092 #endif
9093 		/*
9094 		 * Do the readdir.
9095 		 */
9096 		nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9097 		return (error);
9098 	}
9099 
9100 	rddir4_cache_rele(rp, rrdc);
9101 	rddir4_cache_rele(rp, rdc);
9102 	mutex_exit(&rp->r_statelock);
9103 	return (error);
9104 }
9105 
9106 static int
do_nfs4readdir(vnode_t * vp,rddir4_cache * rdc,cred_t * cr)9107 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9108 {
9109 	int error;
9110 	rnode4_t *rp;
9111 
9112 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9113 
9114 	rp = VTOR4(vp);
9115 
9116 	/*
9117 	 * Obtain the readdir results for the caller.
9118 	 */
9119 	nfs4readdir(vp, rdc, cr);
9120 
9121 	mutex_enter(&rp->r_statelock);
9122 	/*
9123 	 * The entry is now complete
9124 	 */
9125 	rdc->flags &= ~RDDIR;
9126 
9127 	error = rdc->error;
9128 	if (error)
9129 		rdc->flags |= RDDIRREQ;
9130 	rddir4_cache_rele(rp, rdc);
9131 	mutex_exit(&rp->r_statelock);
9132 
9133 	return (error);
9134 }
9135 
9136 /*
9137  * Read directory entries.
9138  * There are some weird things to look out for here.  The uio_loffset
9139  * field is either 0 or it is the offset returned from a previous
9140  * readdir.  It is an opaque value used by the server to find the
9141  * correct directory block to read. The count field is the number
9142  * of blocks to read on the server.  This is advisory only, the server
9143  * may return only one block's worth of entries.  Entries may be compressed
9144  * on the server.
9145  *
9146  * Generates the following compound request:
9147  * 1. If readdir offset is zero and no dnlc entry for parent exists,
9148  *    must include a Lookupp as well. In this case, send:
9149  *    { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9150  * 2. Otherwise just do: { Putfh <fh>; Readdir }
9151  *
9152  * Get complete attributes and filehandles for entries if this is the
9153  * first read of the directory. Otherwise, just get fileid's.
9154  */
9155 static void
nfs4readdir(vnode_t * vp,rddir4_cache * rdc,cred_t * cr)9156 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9157 {
9158 	COMPOUND4args_clnt args;
9159 	COMPOUND4res_clnt res;
9160 	READDIR4args *rargs;
9161 	READDIR4res_clnt *rd_res;
9162 	bitmap4 rd_bitsval;
9163 	nfs_argop4 argop[5];
9164 	nfs_resop4 *resop;
9165 	rnode4_t *rp = VTOR4(vp);
9166 	mntinfo4_t *mi = VTOMI4(vp);
9167 	int doqueue;
9168 	u_longlong_t nodeid, pnodeid;	/* id's of dir and its parents */
9169 	vnode_t *dvp;
9170 	nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9171 	int num_ops, res_opcnt;
9172 	bool_t needrecov = FALSE;
9173 	nfs4_recov_state_t recov_state;
9174 	hrtime_t t;
9175 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9176 
9177 	ASSERT(nfs_zone() == mi->mi_zone);
9178 	ASSERT(rdc->flags & RDDIR);
9179 	ASSERT(rdc->entries == NULL);
9180 
9181 	/*
9182 	 * If rp were a stub, it should have triggered and caused
9183 	 * a mount for us to get this far.
9184 	 */
9185 	ASSERT(!RP_ISSTUB(rp));
9186 
9187 	num_ops = 2;
9188 	if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9189 		/*
9190 		 * Since nfsv4 readdir may not return entries for "." and "..",
9191 		 * the client must recreate them:
9192 		 * To find the correct nodeid, do the following:
9193 		 * For current node, get nodeid from dnlc.
9194 		 * - if current node is rootvp, set pnodeid to nodeid.
9195 		 * - else if parent is in the dnlc, get its nodeid from there.
9196 		 * - else add LOOKUPP+GETATTR to compound.
9197 		 */
9198 		nodeid = rp->r_attr.va_nodeid;
9199 		if (vp->v_flag & VROOT) {
9200 			pnodeid = nodeid;	/* root of mount point */
9201 		} else {
9202 			dvp = dnlc_lookup(vp, "..");
9203 			if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9204 				/* parent in dnlc cache - no need for otw */
9205 				pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9206 			} else {
9207 				/*
9208 				 * parent not in dnlc cache,
9209 				 * do lookupp to get its id
9210 				 */
9211 				num_ops = 5;
9212 				pnodeid = 0; /* set later by getattr parent */
9213 			}
9214 			if (dvp)
9215 				VN_RELE(dvp);
9216 		}
9217 	}
9218 	recov_state.rs_flags = 0;
9219 	recov_state.rs_num_retry_despite_err = 0;
9220 
9221 	/* Save the original mount point security flavor */
9222 	(void) save_mnt_secinfo(mi->mi_curr_serv);
9223 
9224 recov_retry:
9225 	args.ctag = TAG_READDIR;
9226 
9227 	args.array = argop;
9228 	args.array_len = num_ops;
9229 
9230 	if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9231 	    &recov_state, NULL)) {
9232 		/*
9233 		 * If readdir a node that is a stub for a crossed mount point,
9234 		 * keep the original secinfo flavor for the current file
9235 		 * system, not the crossed one.
9236 		 */
9237 		(void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9238 		rdc->error = e.error;
9239 		return;
9240 	}
9241 
9242 	/*
9243 	 * Determine which attrs to request for dirents.  This code
9244 	 * must be protected by nfs4_start/end_fop because of r_server
9245 	 * (which will change during failover recovery).
9246 	 *
9247 	 */
9248 	if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9249 		/*
9250 		 * Get all vattr attrs plus filehandle and rdattr_error
9251 		 */
9252 		rd_bitsval = NFS4_VATTR_MASK |
9253 		    FATTR4_RDATTR_ERROR_MASK |
9254 		    FATTR4_FILEHANDLE_MASK;
9255 
9256 		if (rp->r_flags & R4READDIRWATTR) {
9257 			mutex_enter(&rp->r_statelock);
9258 			rp->r_flags &= ~R4READDIRWATTR;
9259 			mutex_exit(&rp->r_statelock);
9260 		}
9261 	} else {
9262 		servinfo4_t *svp = rp->r_server;
9263 
9264 		/*
9265 		 * Already read directory. Use readdir with
9266 		 * no attrs (except for mounted_on_fileid) for updates.
9267 		 */
9268 		rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9269 
9270 		/*
9271 		 * request mounted on fileid if supported, else request
9272 		 * fileid.  maybe we should verify that fileid is supported
9273 		 * and request something else if not.
9274 		 */
9275 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9276 		if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9277 			rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9278 		nfs_rw_exit(&svp->sv_lock);
9279 	}
9280 
9281 	/* putfh directory fh */
9282 	argop[0].argop = OP_CPUTFH;
9283 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9284 
9285 	argop[1].argop = OP_READDIR;
9286 	rargs = &argop[1].nfs_argop4_u.opreaddir;
9287 	/*
9288 	 * 1 and 2 are reserved for client "." and ".." entry offset.
9289 	 * cookie 0 should be used over-the-wire to start reading at
9290 	 * the beginning of the directory excluding "." and "..".
9291 	 */
9292 	if (rdc->nfs4_cookie == 0 ||
9293 	    rdc->nfs4_cookie == 1 ||
9294 	    rdc->nfs4_cookie == 2) {
9295 		rargs->cookie = (nfs_cookie4)0;
9296 		rargs->cookieverf = 0;
9297 	} else {
9298 		rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9299 		mutex_enter(&rp->r_statelock);
9300 		rargs->cookieverf = rp->r_cookieverf4;
9301 		mutex_exit(&rp->r_statelock);
9302 	}
9303 	rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9304 	rargs->maxcount = mi->mi_tsize;
9305 	rargs->attr_request = rd_bitsval;
9306 	rargs->rdc = rdc;
9307 	rargs->dvp = vp;
9308 	rargs->mi = mi;
9309 	rargs->cr = cr;
9310 
9311 
9312 	/*
9313 	 * If count < than the minimum required, we return no entries
9314 	 * and fail with EINVAL
9315 	 */
9316 	if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9317 		rdc->error = EINVAL;
9318 		goto out;
9319 	}
9320 
9321 	if (args.array_len == 5) {
9322 		/*
9323 		 * Add lookupp and getattr for parent nodeid.
9324 		 */
9325 		argop[2].argop = OP_LOOKUPP;
9326 
9327 		argop[3].argop = OP_GETFH;
9328 
9329 		/* getattr parent */
9330 		argop[4].argop = OP_GETATTR;
9331 		argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9332 		argop[4].nfs_argop4_u.opgetattr.mi = mi;
9333 	}
9334 
9335 	doqueue = 1;
9336 
9337 	if (mi->mi_io_kstats) {
9338 		mutex_enter(&mi->mi_lock);
9339 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9340 		mutex_exit(&mi->mi_lock);
9341 	}
9342 
9343 	/* capture the time of this call */
9344 	rargs->t = t = gethrtime();
9345 
9346 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9347 
9348 	if (mi->mi_io_kstats) {
9349 		mutex_enter(&mi->mi_lock);
9350 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9351 		mutex_exit(&mi->mi_lock);
9352 	}
9353 
9354 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9355 
9356 	/*
9357 	 * If RPC error occurred and it isn't an error that
9358 	 * triggers recovery, then go ahead and fail now.
9359 	 */
9360 	if (e.error != 0 && !needrecov) {
9361 		rdc->error = e.error;
9362 		goto out;
9363 	}
9364 
9365 	if (needrecov) {
9366 		bool_t abort;
9367 
9368 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9369 		    "nfs4readdir: initiating recovery.\n"));
9370 
9371 		abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9372 		    NULL, OP_READDIR, NULL, NULL, NULL);
9373 		if (abort == FALSE) {
9374 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9375 			    &recov_state, needrecov);
9376 			if (!e.error)
9377 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9378 			if (rdc->entries != NULL) {
9379 				kmem_free(rdc->entries, rdc->entlen);
9380 				rdc->entries = NULL;
9381 			}
9382 			goto recov_retry;
9383 		}
9384 
9385 		if (e.error != 0) {
9386 			rdc->error = e.error;
9387 			goto out;
9388 		}
9389 
9390 		/* fall through for res.status case */
9391 	}
9392 
9393 	res_opcnt = res.array_len;
9394 
9395 	/*
9396 	 * If compound failed first 2 ops (PUTFH+READDIR), then return
9397 	 * failure here.  Subsequent ops are for filling out dot-dot
9398 	 * dirent, and if they fail, we still want to give the caller
9399 	 * the dirents returned by (the successful) READDIR op, so we need
9400 	 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9401 	 *
9402 	 * One example where PUTFH+READDIR ops would succeed but
9403 	 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9404 	 * but lacks x.  In this case, a POSIX server's VOP_READDIR
9405 	 * would succeed; however, VOP_LOOKUP(..) would fail since no
9406 	 * x perm.  We need to come up with a non-vendor-specific way
9407 	 * for a POSIX server to return d_ino from dotdot's dirent if
9408 	 * client only requests mounted_on_fileid, and just say the
9409 	 * LOOKUPP succeeded and fill out the GETATTR.  However, if
9410 	 * client requested any mandatory attrs, server would be required
9411 	 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9412 	 * for dotdot.
9413 	 */
9414 
9415 	if (res.status) {
9416 		if (res_opcnt <= 2) {
9417 			e.error = geterrno4(res.status);
9418 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9419 			    &recov_state, needrecov);
9420 			nfs4_purge_stale_fh(e.error, vp, cr);
9421 			rdc->error = e.error;
9422 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9423 			if (rdc->entries != NULL) {
9424 				kmem_free(rdc->entries, rdc->entlen);
9425 				rdc->entries = NULL;
9426 			}
9427 			/*
9428 			 * If readdir a node that is a stub for a
9429 			 * crossed mount point, keep the original
9430 			 * secinfo flavor for the current file system,
9431 			 * not the crossed one.
9432 			 */
9433 			(void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9434 			return;
9435 		}
9436 	}
9437 
9438 	resop = &res.array[1];	/* readdir res */
9439 	rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9440 
9441 	mutex_enter(&rp->r_statelock);
9442 	rp->r_cookieverf4 = rd_res->cookieverf;
9443 	mutex_exit(&rp->r_statelock);
9444 
9445 	/*
9446 	 * For "." and ".." entries
9447 	 * e.g.
9448 	 *	seek(cookie=0) -> "." entry with d_off = 1
9449 	 *	seek(cookie=1) -> ".." entry with d_off = 2
9450 	 */
9451 	if (cookie == (nfs_cookie4) 0) {
9452 		if (rd_res->dotp)
9453 			rd_res->dotp->d_ino = nodeid;
9454 		if (rd_res->dotdotp)
9455 			rd_res->dotdotp->d_ino = pnodeid;
9456 	}
9457 	if (cookie == (nfs_cookie4) 1) {
9458 		if (rd_res->dotdotp)
9459 			rd_res->dotdotp->d_ino = pnodeid;
9460 	}
9461 
9462 
9463 	/* LOOKUPP+GETATTR attemped */
9464 	if (args.array_len == 5 && rd_res->dotdotp) {
9465 		if (res.status == NFS4_OK && res_opcnt == 5) {
9466 			nfs_fh4 *fhp;
9467 			nfs4_sharedfh_t *sfhp;
9468 			vnode_t *pvp;
9469 			nfs4_ga_res_t *garp;
9470 
9471 			resop++;	/* lookupp */
9472 			resop++;	/* getfh   */
9473 			fhp = &resop->nfs_resop4_u.opgetfh.object;
9474 
9475 			resop++;	/* getattr of parent */
9476 
9477 			/*
9478 			 * First, take care of finishing the
9479 			 * readdir results.
9480 			 */
9481 			garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9482 			/*
9483 			 * The d_ino of .. must be the inode number
9484 			 * of the mounted filesystem.
9485 			 */
9486 			if (garp->n4g_va.va_mask & AT_NODEID)
9487 				rd_res->dotdotp->d_ino =
9488 				    garp->n4g_va.va_nodeid;
9489 
9490 
9491 			/*
9492 			 * Next, create the ".." dnlc entry
9493 			 */
9494 			sfhp = sfh4_get(fhp, mi);
9495 			if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9496 				dnlc_update(vp, "..", pvp);
9497 				VN_RELE(pvp);
9498 			}
9499 			sfh4_rele(&sfhp);
9500 		}
9501 	}
9502 
9503 	if (mi->mi_io_kstats) {
9504 		mutex_enter(&mi->mi_lock);
9505 		KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9506 		KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9507 		mutex_exit(&mi->mi_lock);
9508 	}
9509 
9510 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9511 
9512 out:
9513 	/*
9514 	 * If readdir a node that is a stub for a crossed mount point,
9515 	 * keep the original secinfo flavor for the current file system,
9516 	 * not the crossed one.
9517 	 */
9518 	(void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9519 
9520 	nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9521 }
9522 
9523 
9524 static int
nfs4_bio(struct buf * bp,stable_how4 * stab_comm,cred_t * cr,bool_t readahead)9525 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9526 {
9527 	rnode4_t *rp = VTOR4(bp->b_vp);
9528 	int count;
9529 	int error;
9530 	cred_t *cred_otw = NULL;
9531 	offset_t offset;
9532 	nfs4_open_stream_t *osp = NULL;
9533 	bool_t first_time = TRUE;	/* first time getting otw cred */
9534 	bool_t last_time = FALSE;	/* last time getting otw cred */
9535 
9536 	ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9537 
9538 	DTRACE_IO1(start, struct buf *, bp);
9539 	offset = ldbtob(bp->b_lblkno);
9540 
9541 	if (bp->b_flags & B_READ) {
9542 	read_again:
9543 		/*
9544 		 * Releases the osp, if it is provided.
9545 		 * Puts a hold on the cred_otw and the new osp (if found).
9546 		 */
9547 		cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9548 		    &first_time, &last_time);
9549 		error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9550 		    offset, bp->b_bcount, &bp->b_resid, cred_otw,
9551 		    readahead, NULL);
9552 		crfree(cred_otw);
9553 		if (!error) {
9554 			if (bp->b_resid) {
9555 				/*
9556 				 * Didn't get it all because we hit EOF,
9557 				 * zero all the memory beyond the EOF.
9558 				 */
9559 				/* bzero(rdaddr + */
9560 				bzero(bp->b_un.b_addr +
9561 				    bp->b_bcount - bp->b_resid, bp->b_resid);
9562 			}
9563 			mutex_enter(&rp->r_statelock);
9564 			if (bp->b_resid == bp->b_bcount &&
9565 			    offset >= rp->r_size) {
9566 				/*
9567 				 * We didn't read anything at all as we are
9568 				 * past EOF.  Return an error indicator back
9569 				 * but don't destroy the pages (yet).
9570 				 */
9571 				error = NFS_EOF;
9572 			}
9573 			mutex_exit(&rp->r_statelock);
9574 		} else if (error == EACCES && last_time == FALSE) {
9575 				goto read_again;
9576 		}
9577 	} else {
9578 		if (!(rp->r_flags & R4STALE)) {
9579 write_again:
9580 			/*
9581 			 * Releases the osp, if it is provided.
9582 			 * Puts a hold on the cred_otw and the new
9583 			 * osp (if found).
9584 			 */
9585 			cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9586 			    &first_time, &last_time);
9587 			mutex_enter(&rp->r_statelock);
9588 			count = MIN(bp->b_bcount, rp->r_size - offset);
9589 			mutex_exit(&rp->r_statelock);
9590 			if (count < 0)
9591 				cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9592 #ifdef DEBUG
9593 			if (count == 0) {
9594 				zoneid_t zoneid = getzoneid();
9595 
9596 				zcmn_err(zoneid, CE_WARN,
9597 				    "nfs4_bio: zero length write at %lld",
9598 				    offset);
9599 				zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9600 				    "b_bcount=%ld, file size=%lld",
9601 				    rp->r_flags, (long)bp->b_bcount,
9602 				    rp->r_size);
9603 				sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9604 				if (nfs4_bio_do_stop)
9605 					debug_enter("nfs4_bio");
9606 			}
9607 #endif
9608 			error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9609 			    count, cred_otw, stab_comm);
9610 			if (error == EACCES && last_time == FALSE) {
9611 				crfree(cred_otw);
9612 				goto write_again;
9613 			}
9614 			bp->b_error = error;
9615 			if (error && error != EINTR &&
9616 			    !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9617 				/*
9618 				 * Don't print EDQUOT errors on the console.
9619 				 * Don't print asynchronous EACCES errors.
9620 				 * Don't print EFBIG errors.
9621 				 * Print all other write errors.
9622 				 */
9623 				if (error != EDQUOT && error != EFBIG &&
9624 				    (error != EACCES ||
9625 				    !(bp->b_flags & B_ASYNC)))
9626 					nfs4_write_error(bp->b_vp,
9627 					    error, cred_otw);
9628 				/*
9629 				 * Update r_error and r_flags as appropriate.
9630 				 * If the error was ESTALE, then mark the
9631 				 * rnode as not being writeable and save
9632 				 * the error status.  Otherwise, save any
9633 				 * errors which occur from asynchronous
9634 				 * page invalidations.  Any errors occurring
9635 				 * from other operations should be saved
9636 				 * by the caller.
9637 				 */
9638 				mutex_enter(&rp->r_statelock);
9639 				if (error == ESTALE) {
9640 					rp->r_flags |= R4STALE;
9641 					if (!rp->r_error)
9642 						rp->r_error = error;
9643 				} else if (!rp->r_error &&
9644 				    (bp->b_flags &
9645 				    (B_INVAL|B_FORCE|B_ASYNC)) ==
9646 				    (B_INVAL|B_FORCE|B_ASYNC)) {
9647 					rp->r_error = error;
9648 				}
9649 				mutex_exit(&rp->r_statelock);
9650 			}
9651 			crfree(cred_otw);
9652 		} else {
9653 			error = rp->r_error;
9654 			/*
9655 			 * A close may have cleared r_error, if so,
9656 			 * propagate ESTALE error return properly
9657 			 */
9658 			if (error == 0)
9659 				error = ESTALE;
9660 		}
9661 	}
9662 
9663 	if (error != 0 && error != NFS_EOF)
9664 		bp->b_flags |= B_ERROR;
9665 
9666 	if (osp)
9667 		open_stream_rele(osp, rp);
9668 
9669 	DTRACE_IO1(done, struct buf *, bp);
9670 
9671 	return (error);
9672 }
9673 
9674 /* ARGSUSED */
9675 int
nfs4_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)9676 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9677 {
9678 	return (EREMOTE);
9679 }
9680 
9681 /* ARGSUSED2 */
9682 int
nfs4_rwlock(vnode_t * vp,int write_lock,caller_context_t * ctp)9683 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9684 {
9685 	rnode4_t *rp = VTOR4(vp);
9686 
9687 	if (!write_lock) {
9688 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9689 		return (V_WRITELOCK_FALSE);
9690 	}
9691 
9692 	if ((rp->r_flags & R4DIRECTIO) ||
9693 	    (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9694 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9695 		if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9696 			return (V_WRITELOCK_FALSE);
9697 		nfs_rw_exit(&rp->r_rwlock);
9698 	}
9699 
9700 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9701 	return (V_WRITELOCK_TRUE);
9702 }
9703 
9704 /* ARGSUSED */
9705 void
nfs4_rwunlock(vnode_t * vp,int write_lock,caller_context_t * ctp)9706 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9707 {
9708 	rnode4_t *rp = VTOR4(vp);
9709 
9710 	nfs_rw_exit(&rp->r_rwlock);
9711 }
9712 
9713 /* ARGSUSED */
9714 static int
nfs4_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)9715 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9716 {
9717 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
9718 		return (EIO);
9719 
9720 	/*
9721 	 * Because we stuff the readdir cookie into the offset field
9722 	 * someone may attempt to do an lseek with the cookie which
9723 	 * we want to succeed.
9724 	 */
9725 	if (vp->v_type == VDIR)
9726 		return (0);
9727 	if (*noffp < 0)
9728 		return (EINVAL);
9729 	return (0);
9730 }
9731 
9732 
9733 /*
9734  * Return all the pages from [off..off+len) in file
9735  */
9736 /* ARGSUSED */
9737 static int
nfs4_getpage(vnode_t * vp,offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr,caller_context_t * ct)9738 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9739     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9740     enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9741 {
9742 	rnode4_t *rp;
9743 	int error;
9744 	mntinfo4_t *mi;
9745 
9746 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
9747 		return (EIO);
9748 	rp = VTOR4(vp);
9749 	if (IS_SHADOW(vp, rp))
9750 		vp = RTOV4(rp);
9751 
9752 	if (vp->v_flag & VNOMAP)
9753 		return (ENOSYS);
9754 
9755 	if (protp != NULL)
9756 		*protp = PROT_ALL;
9757 
9758 	/*
9759 	 * Now validate that the caches are up to date.
9760 	 */
9761 	if (error = nfs4_validate_caches(vp, cr))
9762 		return (error);
9763 
9764 	mi = VTOMI4(vp);
9765 retry:
9766 	mutex_enter(&rp->r_statelock);
9767 
9768 	/*
9769 	 * Don't create dirty pages faster than they
9770 	 * can be cleaned so that the system doesn't
9771 	 * get imbalanced.  If the async queue is
9772 	 * maxed out, then wait for it to drain before
9773 	 * creating more dirty pages.  Also, wait for
9774 	 * any threads doing pagewalks in the vop_getattr
9775 	 * entry points so that they don't block for
9776 	 * long periods.
9777 	 */
9778 	if (rw == S_CREATE) {
9779 		while ((mi->mi_max_threads != 0 &&
9780 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
9781 		    rp->r_gcount > 0)
9782 			cv_wait(&rp->r_cv, &rp->r_statelock);
9783 	}
9784 
9785 	/*
9786 	 * If we are getting called as a side effect of an nfs_write()
9787 	 * operation the local file size might not be extended yet.
9788 	 * In this case we want to be able to return pages of zeroes.
9789 	 */
9790 	if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9791 		NFS4_DEBUG(nfs4_pageio_debug,
9792 		    (CE_NOTE, "getpage beyond EOF: off=%lld, "
9793 		    "len=%llu, size=%llu, attrsize =%llu", off,
9794 		    (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9795 		mutex_exit(&rp->r_statelock);
9796 		return (EFAULT);		/* beyond EOF */
9797 	}
9798 
9799 	mutex_exit(&rp->r_statelock);
9800 
9801 	error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9802 	    pl, plsz, seg, addr, rw, cr);
9803 	NFS4_DEBUG(nfs4_pageio_debug && error,
9804 	    (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9805 	    error, off, (u_longlong_t)len));
9806 
9807 	switch (error) {
9808 	case NFS_EOF:
9809 		nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9810 		goto retry;
9811 	case ESTALE:
9812 		nfs4_purge_stale_fh(error, vp, cr);
9813 	}
9814 
9815 	return (error);
9816 }
9817 
9818 /*
9819  * Called from pvn_getpages to get a particular page.
9820  */
9821 /* ARGSUSED */
9822 static int
nfs4_getapage(vnode_t * vp,u_offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr)9823 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9824     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9825     enum seg_rw rw, cred_t *cr)
9826 {
9827 	rnode4_t *rp;
9828 	uint_t bsize;
9829 	struct buf *bp;
9830 	page_t *pp;
9831 	u_offset_t lbn;
9832 	u_offset_t io_off;
9833 	u_offset_t blkoff;
9834 	u_offset_t rablkoff;
9835 	size_t io_len;
9836 	uint_t blksize;
9837 	int error;
9838 	int readahead;
9839 	int readahead_issued = 0;
9840 	int ra_window; /* readahead window */
9841 	page_t *pagefound;
9842 	page_t *savepp;
9843 
9844 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
9845 		return (EIO);
9846 
9847 	rp = VTOR4(vp);
9848 	ASSERT(!IS_SHADOW(vp, rp));
9849 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9850 
9851 reread:
9852 	bp = NULL;
9853 	pp = NULL;
9854 	pagefound = NULL;
9855 
9856 	if (pl != NULL)
9857 		pl[0] = NULL;
9858 
9859 	error = 0;
9860 	lbn = off / bsize;
9861 	blkoff = lbn * bsize;
9862 
9863 	/*
9864 	 * Queueing up the readahead before doing the synchronous read
9865 	 * results in a significant increase in read throughput because
9866 	 * of the increased parallelism between the async threads and
9867 	 * the process context.
9868 	 */
9869 	if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9870 	    rw != S_CREATE &&
9871 	    !(vp->v_flag & VNOCACHE)) {
9872 		mutex_enter(&rp->r_statelock);
9873 
9874 		/*
9875 		 * Calculate the number of readaheads to do.
9876 		 * a) No readaheads at offset = 0.
9877 		 * b) Do maximum(nfs4_nra) readaheads when the readahead
9878 		 *    window is closed.
9879 		 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9880 		 *    upon how far the readahead window is open or close.
9881 		 * d) No readaheads if rp->r_nextr is not within the scope
9882 		 *    of the readahead window (random i/o).
9883 		 */
9884 
9885 		if (off == 0)
9886 			readahead = 0;
9887 		else if (blkoff == rp->r_nextr)
9888 			readahead = nfs4_nra;
9889 		else if (rp->r_nextr > blkoff &&
9890 		    ((ra_window = (rp->r_nextr - blkoff) / bsize)
9891 		    <= (nfs4_nra - 1)))
9892 			readahead = nfs4_nra - ra_window;
9893 		else
9894 			readahead = 0;
9895 
9896 		rablkoff = rp->r_nextr;
9897 		while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9898 			mutex_exit(&rp->r_statelock);
9899 			if (nfs4_async_readahead(vp, rablkoff + bsize,
9900 			    addr + (rablkoff + bsize - off),
9901 			    seg, cr, nfs4_readahead) < 0) {
9902 				mutex_enter(&rp->r_statelock);
9903 				break;
9904 			}
9905 			readahead--;
9906 			rablkoff += bsize;
9907 			/*
9908 			 * Indicate that we did a readahead so
9909 			 * readahead offset is not updated
9910 			 * by the synchronous read below.
9911 			 */
9912 			readahead_issued = 1;
9913 			mutex_enter(&rp->r_statelock);
9914 			/*
9915 			 * set readahead offset to
9916 			 * offset of last async readahead
9917 			 * request.
9918 			 */
9919 			rp->r_nextr = rablkoff;
9920 		}
9921 		mutex_exit(&rp->r_statelock);
9922 	}
9923 
9924 again:
9925 	if ((pagefound = page_exists(vp, off)) == NULL) {
9926 		if (pl == NULL) {
9927 			(void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9928 			    nfs4_readahead);
9929 		} else if (rw == S_CREATE) {
9930 			/*
9931 			 * Block for this page is not allocated, or the offset
9932 			 * is beyond the current allocation size, or we're
9933 			 * allocating a swap slot and the page was not found,
9934 			 * so allocate it and return a zero page.
9935 			 */
9936 			if ((pp = page_create_va(vp, off,
9937 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9938 				cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9939 			io_len = PAGESIZE;
9940 			mutex_enter(&rp->r_statelock);
9941 			rp->r_nextr = off + PAGESIZE;
9942 			mutex_exit(&rp->r_statelock);
9943 		} else {
9944 			/*
9945 			 * Need to go to server to get a block
9946 			 */
9947 			mutex_enter(&rp->r_statelock);
9948 			if (blkoff < rp->r_size &&
9949 			    blkoff + bsize > rp->r_size) {
9950 				/*
9951 				 * If less than a block left in
9952 				 * file read less than a block.
9953 				 */
9954 				if (rp->r_size <= off) {
9955 					/*
9956 					 * Trying to access beyond EOF,
9957 					 * set up to get at least one page.
9958 					 */
9959 					blksize = off + PAGESIZE - blkoff;
9960 				} else
9961 					blksize = rp->r_size - blkoff;
9962 			} else if ((off == 0) ||
9963 			    (off != rp->r_nextr && !readahead_issued)) {
9964 				blksize = PAGESIZE;
9965 				blkoff = off; /* block = page here */
9966 			} else
9967 				blksize = bsize;
9968 			mutex_exit(&rp->r_statelock);
9969 
9970 			pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9971 			    &io_len, blkoff, blksize, 0);
9972 
9973 			/*
9974 			 * Some other thread has entered the page,
9975 			 * so just use it.
9976 			 */
9977 			if (pp == NULL)
9978 				goto again;
9979 
9980 			/*
9981 			 * Now round the request size up to page boundaries.
9982 			 * This ensures that the entire page will be
9983 			 * initialized to zeroes if EOF is encountered.
9984 			 */
9985 			io_len = ptob(btopr(io_len));
9986 
9987 			bp = pageio_setup(pp, io_len, vp, B_READ);
9988 			ASSERT(bp != NULL);
9989 
9990 			/*
9991 			 * pageio_setup should have set b_addr to 0.  This
9992 			 * is correct since we want to do I/O on a page
9993 			 * boundary.  bp_mapin will use this addr to calculate
9994 			 * an offset, and then set b_addr to the kernel virtual
9995 			 * address it allocated for us.
9996 			 */
9997 			ASSERT(bp->b_un.b_addr == 0);
9998 
9999 			bp->b_edev = 0;
10000 			bp->b_dev = 0;
10001 			bp->b_lblkno = lbtodb(io_off);
10002 			bp->b_file = vp;
10003 			bp->b_offset = (offset_t)off;
10004 			bp_mapin(bp);
10005 
10006 			/*
10007 			 * If doing a write beyond what we believe is EOF,
10008 			 * don't bother trying to read the pages from the
10009 			 * server, we'll just zero the pages here.  We
10010 			 * don't check that the rw flag is S_WRITE here
10011 			 * because some implementations may attempt a
10012 			 * read access to the buffer before copying data.
10013 			 */
10014 			mutex_enter(&rp->r_statelock);
10015 			if (io_off >= rp->r_size && seg == segkmap) {
10016 				mutex_exit(&rp->r_statelock);
10017 				bzero(bp->b_un.b_addr, io_len);
10018 			} else {
10019 				mutex_exit(&rp->r_statelock);
10020 				error = nfs4_bio(bp, NULL, cr, FALSE);
10021 			}
10022 
10023 			/*
10024 			 * Unmap the buffer before freeing it.
10025 			 */
10026 			bp_mapout(bp);
10027 			pageio_done(bp);
10028 
10029 			savepp = pp;
10030 			do {
10031 				pp->p_fsdata = C_NOCOMMIT;
10032 			} while ((pp = pp->p_next) != savepp);
10033 
10034 			if (error == NFS_EOF) {
10035 				/*
10036 				 * If doing a write system call just return
10037 				 * zeroed pages, else user tried to get pages
10038 				 * beyond EOF, return error.  We don't check
10039 				 * that the rw flag is S_WRITE here because
10040 				 * some implementations may attempt a read
10041 				 * access to the buffer before copying data.
10042 				 */
10043 				if (seg == segkmap)
10044 					error = 0;
10045 				else
10046 					error = EFAULT;
10047 			}
10048 
10049 			if (!readahead_issued && !error) {
10050 				mutex_enter(&rp->r_statelock);
10051 				rp->r_nextr = io_off + io_len;
10052 				mutex_exit(&rp->r_statelock);
10053 			}
10054 		}
10055 	}
10056 
10057 out:
10058 	if (pl == NULL)
10059 		return (error);
10060 
10061 	if (error) {
10062 		if (pp != NULL)
10063 			pvn_read_done(pp, B_ERROR);
10064 		return (error);
10065 	}
10066 
10067 	if (pagefound) {
10068 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10069 
10070 		/*
10071 		 * Page exists in the cache, acquire the appropriate lock.
10072 		 * If this fails, start all over again.
10073 		 */
10074 		if ((pp = page_lookup(vp, off, se)) == NULL) {
10075 #ifdef DEBUG
10076 			nfs4_lostpage++;
10077 #endif
10078 			goto reread;
10079 		}
10080 		pl[0] = pp;
10081 		pl[1] = NULL;
10082 		return (0);
10083 	}
10084 
10085 	if (pp != NULL)
10086 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10087 
10088 	return (error);
10089 }
10090 
10091 static void
nfs4_readahead(vnode_t * vp,u_offset_t blkoff,caddr_t addr,struct seg * seg,cred_t * cr)10092 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10093     cred_t *cr)
10094 {
10095 	int error;
10096 	page_t *pp;
10097 	u_offset_t io_off;
10098 	size_t io_len;
10099 	struct buf *bp;
10100 	uint_t bsize, blksize;
10101 	rnode4_t *rp = VTOR4(vp);
10102 	page_t *savepp;
10103 
10104 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10105 
10106 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10107 
10108 	mutex_enter(&rp->r_statelock);
10109 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10110 		/*
10111 		 * If less than a block left in file read less
10112 		 * than a block.
10113 		 */
10114 		blksize = rp->r_size - blkoff;
10115 	} else
10116 		blksize = bsize;
10117 	mutex_exit(&rp->r_statelock);
10118 
10119 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10120 	    &io_off, &io_len, blkoff, blksize, 1);
10121 	/*
10122 	 * The isra flag passed to the kluster function is 1, we may have
10123 	 * gotten a return value of NULL for a variety of reasons (# of free
10124 	 * pages < minfree, someone entered the page on the vnode etc). In all
10125 	 * cases, we want to punt on the readahead.
10126 	 */
10127 	if (pp == NULL)
10128 		return;
10129 
10130 	/*
10131 	 * Now round the request size up to page boundaries.
10132 	 * This ensures that the entire page will be
10133 	 * initialized to zeroes if EOF is encountered.
10134 	 */
10135 	io_len = ptob(btopr(io_len));
10136 
10137 	bp = pageio_setup(pp, io_len, vp, B_READ);
10138 	ASSERT(bp != NULL);
10139 
10140 	/*
10141 	 * pageio_setup should have set b_addr to 0.  This is correct since
10142 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10143 	 * to calculate an offset, and then set b_addr to the kernel virtual
10144 	 * address it allocated for us.
10145 	 */
10146 	ASSERT(bp->b_un.b_addr == 0);
10147 
10148 	bp->b_edev = 0;
10149 	bp->b_dev = 0;
10150 	bp->b_lblkno = lbtodb(io_off);
10151 	bp->b_file = vp;
10152 	bp->b_offset = (offset_t)blkoff;
10153 	bp_mapin(bp);
10154 
10155 	/*
10156 	 * If doing a write beyond what we believe is EOF, don't bother trying
10157 	 * to read the pages from the server, we'll just zero the pages here.
10158 	 * We don't check that the rw flag is S_WRITE here because some
10159 	 * implementations may attempt a read access to the buffer before
10160 	 * copying data.
10161 	 */
10162 	mutex_enter(&rp->r_statelock);
10163 	if (io_off >= rp->r_size && seg == segkmap) {
10164 		mutex_exit(&rp->r_statelock);
10165 		bzero(bp->b_un.b_addr, io_len);
10166 		error = 0;
10167 	} else {
10168 		mutex_exit(&rp->r_statelock);
10169 		error = nfs4_bio(bp, NULL, cr, TRUE);
10170 		if (error == NFS_EOF)
10171 			error = 0;
10172 	}
10173 
10174 	/*
10175 	 * Unmap the buffer before freeing it.
10176 	 */
10177 	bp_mapout(bp);
10178 	pageio_done(bp);
10179 
10180 	savepp = pp;
10181 	do {
10182 		pp->p_fsdata = C_NOCOMMIT;
10183 	} while ((pp = pp->p_next) != savepp);
10184 
10185 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10186 
10187 	/*
10188 	 * In case of error set readahead offset
10189 	 * to the lowest offset.
10190 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10191 	 */
10192 	if (error && rp->r_nextr > io_off) {
10193 		mutex_enter(&rp->r_statelock);
10194 		if (rp->r_nextr > io_off)
10195 			rp->r_nextr = io_off;
10196 		mutex_exit(&rp->r_statelock);
10197 	}
10198 }
10199 
10200 /*
10201  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10202  * If len == 0, do from off to EOF.
10203  *
10204  * The normal cases should be len == 0 && off == 0 (entire vp list) or
10205  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10206  * (from pageout).
10207  */
10208 /* ARGSUSED */
10209 static int
nfs4_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)10210 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10211     caller_context_t *ct)
10212 {
10213 	int error;
10214 	rnode4_t *rp;
10215 
10216 	ASSERT(cr != NULL);
10217 
10218 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10219 		return (EIO);
10220 
10221 	rp = VTOR4(vp);
10222 	if (IS_SHADOW(vp, rp))
10223 		vp = RTOV4(rp);
10224 
10225 	/*
10226 	 * XXX - Why should this check be made here?
10227 	 */
10228 	if (vp->v_flag & VNOMAP)
10229 		return (ENOSYS);
10230 
10231 	if (len == 0 && !(flags & B_INVAL) &&
10232 	    (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10233 		return (0);
10234 
10235 	mutex_enter(&rp->r_statelock);
10236 	rp->r_count++;
10237 	mutex_exit(&rp->r_statelock);
10238 	error = nfs4_putpages(vp, off, len, flags, cr);
10239 	mutex_enter(&rp->r_statelock);
10240 	rp->r_count--;
10241 	cv_broadcast(&rp->r_cv);
10242 	mutex_exit(&rp->r_statelock);
10243 
10244 	return (error);
10245 }
10246 
10247 /*
10248  * Write out a single page, possibly klustering adjacent dirty pages.
10249  */
10250 int
nfs4_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)10251 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10252     int flags, cred_t *cr)
10253 {
10254 	u_offset_t io_off;
10255 	u_offset_t lbn_off;
10256 	u_offset_t lbn;
10257 	size_t io_len;
10258 	uint_t bsize;
10259 	int error;
10260 	rnode4_t *rp;
10261 
10262 	ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10263 	ASSERT(pp != NULL);
10264 	ASSERT(cr != NULL);
10265 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10266 
10267 	rp = VTOR4(vp);
10268 	ASSERT(rp->r_count > 0);
10269 	ASSERT(!IS_SHADOW(vp, rp));
10270 
10271 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10272 	lbn = pp->p_offset / bsize;
10273 	lbn_off = lbn * bsize;
10274 
10275 	/*
10276 	 * Find a kluster that fits in one block, or in
10277 	 * one page if pages are bigger than blocks.  If
10278 	 * there is less file space allocated than a whole
10279 	 * page, we'll shorten the i/o request below.
10280 	 */
10281 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10282 	    roundup(bsize, PAGESIZE), flags);
10283 
10284 	/*
10285 	 * pvn_write_kluster shouldn't have returned a page with offset
10286 	 * behind the original page we were given.  Verify that.
10287 	 */
10288 	ASSERT((pp->p_offset / bsize) >= lbn);
10289 
10290 	/*
10291 	 * Now pp will have the list of kept dirty pages marked for
10292 	 * write back.  It will also handle invalidation and freeing
10293 	 * of pages that are not dirty.  Check for page length rounding
10294 	 * problems.
10295 	 */
10296 	if (io_off + io_len > lbn_off + bsize) {
10297 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10298 		io_len = lbn_off + bsize - io_off;
10299 	}
10300 	/*
10301 	 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10302 	 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10303 	 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10304 	 * progress and the r_size has not been made consistent with the
10305 	 * new size of the file. When the uiomove() completes the r_size is
10306 	 * updated and the R4MODINPROGRESS flag is cleared.
10307 	 *
10308 	 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10309 	 * consistent value of r_size. Without this handshaking, it is
10310 	 * possible that nfs4_bio() picks  up the old value of r_size
10311 	 * before the uiomove() in writerp4() completes. This will result
10312 	 * in the write through nfs4_bio() being dropped.
10313 	 *
10314 	 * More precisely, there is a window between the time the uiomove()
10315 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10316 	 * operation intervenes in this window, the page will be picked up,
10317 	 * because it is dirty (it will be unlocked, unless it was
10318 	 * pagecreate'd). When the page is picked up as dirty, the dirty
10319 	 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10320 	 * checked. This will still be the old size. Therefore the page will
10321 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10322 	 * the page will be found to be clean and the write will be dropped.
10323 	 */
10324 	if (rp->r_flags & R4MODINPROGRESS) {
10325 		mutex_enter(&rp->r_statelock);
10326 		if ((rp->r_flags & R4MODINPROGRESS) &&
10327 		    rp->r_modaddr + MAXBSIZE > io_off &&
10328 		    rp->r_modaddr < io_off + io_len) {
10329 			page_t *plist;
10330 			/*
10331 			 * A write is in progress for this region of the file.
10332 			 * If we did not detect R4MODINPROGRESS here then this
10333 			 * path through nfs_putapage() would eventually go to
10334 			 * nfs4_bio() and may not write out all of the data
10335 			 * in the pages. We end up losing data. So we decide
10336 			 * to set the modified bit on each page in the page
10337 			 * list and mark the rnode with R4DIRTY. This write
10338 			 * will be restarted at some later time.
10339 			 */
10340 			plist = pp;
10341 			while (plist != NULL) {
10342 				pp = plist;
10343 				page_sub(&plist, pp);
10344 				hat_setmod(pp);
10345 				page_io_unlock(pp);
10346 				page_unlock(pp);
10347 			}
10348 			rp->r_flags |= R4DIRTY;
10349 			mutex_exit(&rp->r_statelock);
10350 			if (offp)
10351 				*offp = io_off;
10352 			if (lenp)
10353 				*lenp = io_len;
10354 			return (0);
10355 		}
10356 		mutex_exit(&rp->r_statelock);
10357 	}
10358 
10359 	if (flags & B_ASYNC) {
10360 		error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10361 		    nfs4_sync_putapage);
10362 	} else
10363 		error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10364 
10365 	if (offp)
10366 		*offp = io_off;
10367 	if (lenp)
10368 		*lenp = io_len;
10369 	return (error);
10370 }
10371 
10372 static int
nfs4_sync_putapage(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)10373 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10374     int flags, cred_t *cr)
10375 {
10376 	int error;
10377 	rnode4_t *rp;
10378 
10379 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10380 
10381 	flags |= B_WRITE;
10382 
10383 	error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10384 
10385 	rp = VTOR4(vp);
10386 
10387 	if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10388 	    error == EACCES) &&
10389 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10390 		if (!(rp->r_flags & R4OUTOFSPACE)) {
10391 			mutex_enter(&rp->r_statelock);
10392 			rp->r_flags |= R4OUTOFSPACE;
10393 			mutex_exit(&rp->r_statelock);
10394 		}
10395 		flags |= B_ERROR;
10396 		pvn_write_done(pp, flags);
10397 		/*
10398 		 * If this was not an async thread, then try again to
10399 		 * write out the pages, but this time, also destroy
10400 		 * them whether or not the write is successful.  This
10401 		 * will prevent memory from filling up with these
10402 		 * pages and destroying them is the only alternative
10403 		 * if they can't be written out.
10404 		 *
10405 		 * Don't do this if this is an async thread because
10406 		 * when the pages are unlocked in pvn_write_done,
10407 		 * some other thread could have come along, locked
10408 		 * them, and queued for an async thread.  It would be
10409 		 * possible for all of the async threads to be tied
10410 		 * up waiting to lock the pages again and they would
10411 		 * all already be locked and waiting for an async
10412 		 * thread to handle them.  Deadlock.
10413 		 */
10414 		if (!(flags & B_ASYNC)) {
10415 			error = nfs4_putpage(vp, io_off, io_len,
10416 			    B_INVAL | B_FORCE, cr, NULL);
10417 		}
10418 	} else {
10419 		if (error)
10420 			flags |= B_ERROR;
10421 		else if (rp->r_flags & R4OUTOFSPACE) {
10422 			mutex_enter(&rp->r_statelock);
10423 			rp->r_flags &= ~R4OUTOFSPACE;
10424 			mutex_exit(&rp->r_statelock);
10425 		}
10426 		pvn_write_done(pp, flags);
10427 		if (freemem < desfree)
10428 			(void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10429 			    NFS4_WRITE_NOWAIT);
10430 	}
10431 
10432 	return (error);
10433 }
10434 
10435 #ifdef DEBUG
10436 int nfs4_force_open_before_mmap = 0;
10437 #endif
10438 
10439 /* ARGSUSED */
10440 static int
nfs4_map(vnode_t * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)10441 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10442     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10443     caller_context_t *ct)
10444 {
10445 	struct segvn_crargs vn_a;
10446 	int error = 0;
10447 	rnode4_t *rp = VTOR4(vp);
10448 	mntinfo4_t *mi = VTOMI4(vp);
10449 
10450 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
10451 		return (EIO);
10452 
10453 	if (vp->v_flag & VNOMAP)
10454 		return (ENOSYS);
10455 
10456 	if (off < 0 || (off + len) < 0)
10457 		return (ENXIO);
10458 
10459 	if (vp->v_type != VREG)
10460 		return (ENODEV);
10461 
10462 	/*
10463 	 * If the file is delegated to the client don't do anything.
10464 	 * If the file is not delegated, then validate the data cache.
10465 	 */
10466 	mutex_enter(&rp->r_statev4_lock);
10467 	if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10468 		mutex_exit(&rp->r_statev4_lock);
10469 		error = nfs4_validate_caches(vp, cr);
10470 		if (error)
10471 			return (error);
10472 	} else {
10473 		mutex_exit(&rp->r_statev4_lock);
10474 	}
10475 
10476 	/*
10477 	 * Check to see if the vnode is currently marked as not cachable.
10478 	 * This means portions of the file are locked (through VOP_FRLOCK).
10479 	 * In this case the map request must be refused.  We use
10480 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10481 	 *
10482 	 * Atomically increment r_inmap after acquiring r_rwlock. The
10483 	 * idea here is to acquire r_rwlock to block read/write and
10484 	 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10485 	 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10486 	 * and we can prevent the deadlock that would have occurred
10487 	 * when nfs4_addmap() would have acquired it out of order.
10488 	 *
10489 	 * Since we are not protecting r_inmap by any lock, we do not
10490 	 * hold any lock when we decrement it. We atomically decrement
10491 	 * r_inmap after we release r_lkserlock.
10492 	 */
10493 
10494 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10495 		return (EINTR);
10496 	atomic_inc_uint(&rp->r_inmap);
10497 	nfs_rw_exit(&rp->r_rwlock);
10498 
10499 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10500 		atomic_dec_uint(&rp->r_inmap);
10501 		return (EINTR);
10502 	}
10503 
10504 	if (vp->v_flag & VNOCACHE) {
10505 		error = EAGAIN;
10506 		goto done;
10507 	}
10508 
10509 	/*
10510 	 * Don't allow concurrent locks and mapping if mandatory locking is
10511 	 * enabled.
10512 	 */
10513 	if (flk_has_remote_locks(vp)) {
10514 		struct vattr va;
10515 		va.va_mask = AT_MODE;
10516 		error = nfs4getattr(vp, &va, cr);
10517 		if (error != 0)
10518 			goto done;
10519 		if (MANDLOCK(vp, va.va_mode)) {
10520 			error = EAGAIN;
10521 			goto done;
10522 		}
10523 	}
10524 
10525 	/*
10526 	 * It is possible that the rnode has a lost lock request that we
10527 	 * are still trying to recover, and that the request conflicts with
10528 	 * this map request.
10529 	 *
10530 	 * An alternative approach would be for nfs4_safemap() to consider
10531 	 * queued lock requests when deciding whether to set or clear
10532 	 * VNOCACHE.  This would require the frlock code path to call
10533 	 * nfs4_safemap() after enqueing a lost request.
10534 	 */
10535 	if (nfs4_map_lost_lock_conflict(vp)) {
10536 		error = EAGAIN;
10537 		goto done;
10538 	}
10539 
10540 	as_rangelock(as);
10541 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10542 	if (error != 0) {
10543 		as_rangeunlock(as);
10544 		goto done;
10545 	}
10546 
10547 	if (vp->v_type == VREG) {
10548 		/*
10549 		 * We need to retrieve the open stream
10550 		 */
10551 		nfs4_open_stream_t	*osp = NULL;
10552 		nfs4_open_owner_t	*oop = NULL;
10553 
10554 		oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10555 		if (oop != NULL) {
10556 			/* returns with 'os_sync_lock' held */
10557 			osp = find_open_stream(oop, rp);
10558 			open_owner_rele(oop);
10559 		}
10560 		if (osp == NULL) {
10561 #ifdef DEBUG
10562 			if (nfs4_force_open_before_mmap) {
10563 				error = EIO;
10564 				goto done;
10565 			}
10566 #endif
10567 			/* returns with 'os_sync_lock' held */
10568 			error = open_and_get_osp(vp, cr, &osp);
10569 			if (osp == NULL) {
10570 				NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10571 				    "nfs4_map: we tried to OPEN the file "
10572 				    "but again no osp, so fail with EIO"));
10573 				goto done;
10574 			}
10575 		}
10576 
10577 		if (osp->os_failed_reopen) {
10578 			mutex_exit(&osp->os_sync_lock);
10579 			open_stream_rele(osp, rp);
10580 			NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10581 			    "nfs4_map: os_failed_reopen set on "
10582 			    "osp %p, cr %p, rp %s", (void *)osp,
10583 			    (void *)cr, rnode4info(rp)));
10584 			error = EIO;
10585 			goto done;
10586 		}
10587 		mutex_exit(&osp->os_sync_lock);
10588 		open_stream_rele(osp, rp);
10589 	}
10590 
10591 	vn_a.vp = vp;
10592 	vn_a.offset = off;
10593 	vn_a.type = (flags & MAP_TYPE);
10594 	vn_a.prot = (uchar_t)prot;
10595 	vn_a.maxprot = (uchar_t)maxprot;
10596 	vn_a.flags = (flags & ~MAP_TYPE);
10597 	vn_a.cred = cr;
10598 	vn_a.amp = NULL;
10599 	vn_a.szc = 0;
10600 	vn_a.lgrp_mem_policy_flags = 0;
10601 
10602 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
10603 	as_rangeunlock(as);
10604 
10605 done:
10606 	nfs_rw_exit(&rp->r_lkserlock);
10607 	atomic_dec_uint(&rp->r_inmap);
10608 	return (error);
10609 }
10610 
10611 /*
10612  * We're most likely dealing with a kernel module that likes to READ
10613  * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10614  * officially OPEN the file to create the necessary client state
10615  * for bookkeeping of os_mmap_read/write counts.
10616  *
10617  * Since VOP_MAP only passes in a pointer to the vnode rather than
10618  * a double pointer, we can't handle the case where nfs4open_otw()
10619  * returns a different vnode than the one passed into VOP_MAP (since
10620  * VOP_DELMAP will not see the vnode nfs4open_otw used).  In this case,
10621  * we return NULL and let nfs4_map() fail.  Note: the only case where
10622  * this should happen is if the file got removed and replaced with the
10623  * same name on the server (in addition to the fact that we're trying
10624  * to VOP_MAP withouth VOP_OPENing the file in the first place).
10625  */
10626 static int
open_and_get_osp(vnode_t * map_vp,cred_t * cr,nfs4_open_stream_t ** ospp)10627 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10628 {
10629 	rnode4_t		*rp, *drp;
10630 	vnode_t			*dvp, *open_vp;
10631 	char			file_name[MAXNAMELEN];
10632 	int			just_created;
10633 	nfs4_open_stream_t	*osp;
10634 	nfs4_open_owner_t	*oop;
10635 	int			error;
10636 
10637 	*ospp = NULL;
10638 	open_vp = map_vp;
10639 
10640 	rp = VTOR4(open_vp);
10641 	if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10642 		return (error);
10643 	drp = VTOR4(dvp);
10644 
10645 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10646 		VN_RELE(dvp);
10647 		return (EINTR);
10648 	}
10649 
10650 	if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10651 		nfs_rw_exit(&drp->r_rwlock);
10652 		VN_RELE(dvp);
10653 		return (error);
10654 	}
10655 
10656 	mutex_enter(&rp->r_statev4_lock);
10657 	if (rp->created_v4) {
10658 		rp->created_v4 = 0;
10659 		mutex_exit(&rp->r_statev4_lock);
10660 
10661 		dnlc_update(dvp, file_name, open_vp);
10662 		/* This is needed so we don't bump the open ref count */
10663 		just_created = 1;
10664 	} else {
10665 		mutex_exit(&rp->r_statev4_lock);
10666 		just_created = 0;
10667 	}
10668 
10669 	VN_HOLD(map_vp);
10670 
10671 	error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10672 	    just_created);
10673 	if (error) {
10674 		nfs_rw_exit(&drp->r_rwlock);
10675 		VN_RELE(dvp);
10676 		VN_RELE(map_vp);
10677 		return (error);
10678 	}
10679 
10680 	nfs_rw_exit(&drp->r_rwlock);
10681 	VN_RELE(dvp);
10682 
10683 	/*
10684 	 * If nfs4open_otw() returned a different vnode then "undo"
10685 	 * the open and return failure to the caller.
10686 	 */
10687 	if (!VN_CMP(open_vp, map_vp)) {
10688 		nfs4_error_t e;
10689 
10690 		NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10691 		    "open returned a different vnode"));
10692 		/*
10693 		 * If there's an error, ignore it,
10694 		 * and let VOP_INACTIVE handle it.
10695 		 */
10696 		(void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10697 		    CLOSE_NORM, 0, 0, 0);
10698 		VN_RELE(map_vp);
10699 		return (EIO);
10700 	}
10701 
10702 	VN_RELE(map_vp);
10703 
10704 	oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10705 	if (!oop) {
10706 		nfs4_error_t e;
10707 
10708 		NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10709 		    "no open owner"));
10710 		/*
10711 		 * If there's an error, ignore it,
10712 		 * and let VOP_INACTIVE handle it.
10713 		 */
10714 		(void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10715 		    CLOSE_NORM, 0, 0, 0);
10716 		return (EIO);
10717 	}
10718 	osp = find_open_stream(oop, rp);
10719 	open_owner_rele(oop);
10720 	*ospp = osp;
10721 	return (0);
10722 }
10723 
10724 /*
10725  * Please be aware that when this function is called, the address space write
10726  * a_lock is held.  Do not put over the wire calls in this function.
10727  */
10728 /* ARGSUSED */
10729 static int
nfs4_addmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)10730 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10731     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10732     caller_context_t *ct)
10733 {
10734 	rnode4_t		*rp;
10735 	int			error = 0;
10736 	mntinfo4_t		*mi;
10737 
10738 	mi = VTOMI4(vp);
10739 	rp = VTOR4(vp);
10740 
10741 	if (nfs_zone() != mi->mi_zone)
10742 		return (EIO);
10743 	if (vp->v_flag & VNOMAP)
10744 		return (ENOSYS);
10745 
10746 	/*
10747 	 * Don't need to update the open stream first, since this
10748 	 * mmap can't add any additional share access that isn't
10749 	 * already contained in the open stream (for the case where we
10750 	 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10751 	 * take into account os_mmap_read[write] counts).
10752 	 */
10753 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10754 
10755 	if (vp->v_type == VREG) {
10756 		/*
10757 		 * We need to retrieve the open stream and update the counts.
10758 		 * If there is no open stream here, something is wrong.
10759 		 */
10760 		nfs4_open_stream_t	*osp = NULL;
10761 		nfs4_open_owner_t	*oop = NULL;
10762 
10763 		oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10764 		if (oop != NULL) {
10765 			/* returns with 'os_sync_lock' held */
10766 			osp = find_open_stream(oop, rp);
10767 			open_owner_rele(oop);
10768 		}
10769 		if (osp == NULL) {
10770 			NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10771 			    "nfs4_addmap: we should have an osp"
10772 			    "but we don't, so fail with EIO"));
10773 			error = EIO;
10774 			goto out;
10775 		}
10776 
10777 		NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10778 		    " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10779 
10780 		/*
10781 		 * Update the map count in the open stream.
10782 		 * This is necessary in the case where we
10783 		 * open/mmap/close/, then the server reboots, and we
10784 		 * attempt to reopen.  If the mmap doesn't add share
10785 		 * access then we send an invalid reopen with
10786 		 * access = NONE.
10787 		 *
10788 		 * We need to specifically check each PROT_* so a mmap
10789 		 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10790 		 * read and write access.  A simple comparison of prot
10791 		 * to ~PROT_WRITE to determine read access is insufficient
10792 		 * since prot can be |= with PROT_USER, etc.
10793 		 */
10794 
10795 		/*
10796 		 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10797 		 */
10798 		if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10799 			osp->os_mmap_write += btopr(len);
10800 		if (maxprot & PROT_READ)
10801 			osp->os_mmap_read += btopr(len);
10802 		if (maxprot & PROT_EXEC)
10803 			osp->os_mmap_read += btopr(len);
10804 		/*
10805 		 * Ensure that os_mmap_read gets incremented, even if
10806 		 * maxprot were to look like PROT_NONE.
10807 		 */
10808 		if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10809 		    !(maxprot & PROT_EXEC))
10810 			osp->os_mmap_read += btopr(len);
10811 		osp->os_mapcnt += btopr(len);
10812 		mutex_exit(&osp->os_sync_lock);
10813 		open_stream_rele(osp, rp);
10814 	}
10815 
10816 out:
10817 	/*
10818 	 * If we got an error, then undo our
10819 	 * incrementing of 'r_mapcnt'.
10820 	 */
10821 
10822 	if (error) {
10823 		atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10824 		ASSERT(rp->r_mapcnt >= 0);
10825 	}
10826 	return (error);
10827 }
10828 
10829 /* ARGSUSED */
10830 static int
nfs4_cmp(vnode_t * vp1,vnode_t * vp2,caller_context_t * ct)10831 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10832 {
10833 
10834 	return (VTOR4(vp1) == VTOR4(vp2));
10835 }
10836 
10837 /* ARGSUSED */
10838 static int
nfs4_frlock(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,cred_t * cr,caller_context_t * ct)10839 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10840     offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10841     caller_context_t *ct)
10842 {
10843 	int rc;
10844 	u_offset_t start, end;
10845 	rnode4_t *rp;
10846 	int error = 0, intr = INTR4(vp);
10847 	nfs4_error_t e;
10848 
10849 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
10850 		return (EIO);
10851 
10852 	/* check for valid cmd parameter */
10853 	switch (cmd) {
10854 	case F_FLOCK:
10855 	case F_FLOCKW:
10856 	case F_OFD_GETLK:
10857 	case F_OFD_SETLK:
10858 	case F_OFD_SETLKW:
10859 		return (EOPNOTSUPP);
10860 	case F_GETLK:
10861 	case F_SETLK:
10862 	case F_SETLKW:
10863 		break;
10864 	default:
10865 		return (EINVAL);
10866 	}
10867 
10868 	/* Verify l_type. */
10869 	switch (bfp->l_type) {
10870 	case F_RDLCK:
10871 		if (cmd != F_GETLK && !(flag & FREAD))
10872 			return (EBADF);
10873 		break;
10874 	case F_WRLCK:
10875 		if (cmd != F_GETLK && !(flag & FWRITE))
10876 			return (EBADF);
10877 		break;
10878 	case F_UNLCK:
10879 		intr = 0;
10880 		break;
10881 
10882 	default:
10883 		return (EINVAL);
10884 	}
10885 
10886 	/* check the validity of the lock range */
10887 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10888 		return (rc);
10889 	if (rc = flk_check_lock_data(start, end, MAXEND))
10890 		return (rc);
10891 
10892 	/*
10893 	 * If the filesystem is mounted using local locking, pass the
10894 	 * request off to the local locking code.
10895 	 */
10896 	if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10897 		if (cmd == F_SETLK || cmd == F_SETLKW) {
10898 			/*
10899 			 * For complete safety, we should be holding
10900 			 * r_lkserlock.  However, we can't call
10901 			 * nfs4_safelock and then fs_frlock while
10902 			 * holding r_lkserlock, so just invoke
10903 			 * nfs4_safelock and expect that this will
10904 			 * catch enough of the cases.
10905 			 */
10906 			if (!nfs4_safelock(vp, bfp, cr))
10907 				return (EAGAIN);
10908 		}
10909 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10910 	}
10911 
10912 	rp = VTOR4(vp);
10913 
10914 	/*
10915 	 * Check whether the given lock request can proceed, given the
10916 	 * current file mappings.
10917 	 */
10918 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10919 		return (EINTR);
10920 	if (cmd == F_SETLK || cmd == F_SETLKW) {
10921 		if (!nfs4_safelock(vp, bfp, cr)) {
10922 			rc = EAGAIN;
10923 			goto done;
10924 		}
10925 	}
10926 
10927 	/*
10928 	 * Flush the cache after waiting for async I/O to finish.  For new
10929 	 * locks, this is so that the process gets the latest bits from the
10930 	 * server.  For unlocks, this is so that other clients see the
10931 	 * latest bits once the file has been unlocked.  If currently dirty
10932 	 * pages can't be flushed, then don't allow a lock to be set.  But
10933 	 * allow unlocks to succeed, to avoid having orphan locks on the
10934 	 * server.
10935 	 */
10936 	if (cmd != F_GETLK) {
10937 		mutex_enter(&rp->r_statelock);
10938 		while (rp->r_count > 0) {
10939 			if (intr) {
10940 				klwp_t *lwp = ttolwp(curthread);
10941 
10942 				if (lwp != NULL)
10943 					lwp->lwp_nostop++;
10944 				if (cv_wait_sig(&rp->r_cv,
10945 				    &rp->r_statelock) == 0) {
10946 					if (lwp != NULL)
10947 						lwp->lwp_nostop--;
10948 					rc = EINTR;
10949 					break;
10950 				}
10951 				if (lwp != NULL)
10952 					lwp->lwp_nostop--;
10953 			} else {
10954 				cv_wait(&rp->r_cv, &rp->r_statelock);
10955 			}
10956 		}
10957 		mutex_exit(&rp->r_statelock);
10958 		if (rc != 0)
10959 			goto done;
10960 		error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10961 		if (error) {
10962 			if (error == ENOSPC || error == EDQUOT) {
10963 				mutex_enter(&rp->r_statelock);
10964 				if (!rp->r_error)
10965 					rp->r_error = error;
10966 				mutex_exit(&rp->r_statelock);
10967 			}
10968 			if (bfp->l_type != F_UNLCK) {
10969 				rc = ENOLCK;
10970 				goto done;
10971 			}
10972 		}
10973 	}
10974 
10975 	/*
10976 	 * Call the lock manager to do the real work of contacting
10977 	 * the server and obtaining the lock.
10978 	 */
10979 	nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10980 	    cr, &e, NULL, NULL);
10981 	rc = e.error;
10982 
10983 	if (rc == 0)
10984 		nfs4_lockcompletion(vp, cmd);
10985 
10986 done:
10987 	nfs_rw_exit(&rp->r_lkserlock);
10988 
10989 	return (rc);
10990 }
10991 
10992 /*
10993  * Free storage space associated with the specified vnode.  The portion
10994  * to be freed is specified by bfp->l_start and bfp->l_len (already
10995  * normalized to a "whence" of 0).
10996  *
10997  * This is an experimental facility whose continued existence is not
10998  * guaranteed.  Currently, we only support the special case
10999  * of l_len == 0, meaning free to end of file.
11000  */
11001 /* ARGSUSED */
11002 static int
nfs4_space(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)11003 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
11004     offset_t offset, cred_t *cr, caller_context_t *ct)
11005 {
11006 	int error;
11007 
11008 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
11009 		return (EIO);
11010 	ASSERT(vp->v_type == VREG);
11011 	if (cmd != F_FREESP)
11012 		return (EINVAL);
11013 
11014 	error = convoff(vp, bfp, 0, offset);
11015 	if (!error) {
11016 		ASSERT(bfp->l_start >= 0);
11017 		if (bfp->l_len == 0) {
11018 			struct vattr va;
11019 
11020 			va.va_mask = AT_SIZE;
11021 			va.va_size = bfp->l_start;
11022 			error = nfs4setattr(vp, &va, 0, cr, NULL);
11023 
11024 			if (error == 0 && bfp->l_start == 0)
11025 				vnevent_truncate(vp, ct);
11026 		} else
11027 			error = EINVAL;
11028 	}
11029 
11030 	return (error);
11031 }
11032 
11033 /* ARGSUSED */
11034 int
nfs4_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)11035 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11036 {
11037 	rnode4_t *rp;
11038 	rp = VTOR4(vp);
11039 
11040 	if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11041 		vp = RTOV4(rp);
11042 	}
11043 	*vpp = vp;
11044 	return (0);
11045 }
11046 
11047 /*
11048  * Setup and add an address space callback to do the work of the delmap call.
11049  * The callback will (and must be) deleted in the actual callback function.
11050  *
11051  * This is done in order to take care of the problem that we have with holding
11052  * the address space's a_lock for a long period of time (e.g. if the NFS server
11053  * is down).  Callbacks will be executed in the address space code while the
11054  * a_lock is not held.  Holding the address space's a_lock causes things such
11055  * as ps and fork to hang because they are trying to acquire this lock as well.
11056  */
11057 /* ARGSUSED */
11058 static int
nfs4_delmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)11059 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11060     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11061     caller_context_t *ct)
11062 {
11063 	int			caller_found;
11064 	int			error;
11065 	rnode4_t		*rp;
11066 	nfs4_delmap_args_t	*dmapp;
11067 	nfs4_delmapcall_t	*delmap_call;
11068 
11069 	if (vp->v_flag & VNOMAP)
11070 		return (ENOSYS);
11071 
11072 	/*
11073 	 * A process may not change zones if it has NFS pages mmap'ed
11074 	 * in, so we can't legitimately get here from the wrong zone.
11075 	 */
11076 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11077 
11078 	rp = VTOR4(vp);
11079 
11080 	/*
11081 	 * The way that the address space of this process deletes its mapping
11082 	 * of this file is via the following call chains:
11083 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11084 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11085 	 *
11086 	 * With the use of address space callbacks we are allowed to drop the
11087 	 * address space lock, a_lock, while executing the NFS operations that
11088 	 * need to go over the wire.  Returning EAGAIN to the caller of this
11089 	 * function is what drives the execution of the callback that we add
11090 	 * below.  The callback will be executed by the address space code
11091 	 * after dropping the a_lock.  When the callback is finished, since
11092 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11093 	 * is called again on the same segment to finish the rest of the work
11094 	 * that needs to happen during unmapping.
11095 	 *
11096 	 * This action of calling back into the segment driver causes
11097 	 * nfs4_delmap() to get called again, but since the callback was
11098 	 * already executed at this point, it already did the work and there
11099 	 * is nothing left for us to do.
11100 	 *
11101 	 * To Summarize:
11102 	 * - The first time nfs4_delmap is called by the current thread is when
11103 	 * we add the caller associated with this delmap to the delmap caller
11104 	 * list, add the callback, and return EAGAIN.
11105 	 * - The second time in this call chain when nfs4_delmap is called we
11106 	 * will find this caller in the delmap caller list and realize there
11107 	 * is no more work to do thus removing this caller from the list and
11108 	 * returning the error that was set in the callback execution.
11109 	 */
11110 	caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11111 	if (caller_found) {
11112 		/*
11113 		 * 'error' is from the actual delmap operations.  To avoid
11114 		 * hangs, we need to handle the return of EAGAIN differently
11115 		 * since this is what drives the callback execution.
11116 		 * In this case, we don't want to return EAGAIN and do the
11117 		 * callback execution because there are none to execute.
11118 		 */
11119 		if (error == EAGAIN)
11120 			return (0);
11121 		else
11122 			return (error);
11123 	}
11124 
11125 	/* current caller was not in the list */
11126 	delmap_call = nfs4_init_delmapcall();
11127 
11128 	mutex_enter(&rp->r_statelock);
11129 	list_insert_tail(&rp->r_indelmap, delmap_call);
11130 	mutex_exit(&rp->r_statelock);
11131 
11132 	dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11133 
11134 	dmapp->vp = vp;
11135 	dmapp->off = off;
11136 	dmapp->addr = addr;
11137 	dmapp->len = len;
11138 	dmapp->prot = prot;
11139 	dmapp->maxprot = maxprot;
11140 	dmapp->flags = flags;
11141 	dmapp->cr = cr;
11142 	dmapp->caller = delmap_call;
11143 
11144 	error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11145 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11146 
11147 	return (error ? error : EAGAIN);
11148 }
11149 
11150 static nfs4_delmapcall_t *
nfs4_init_delmapcall()11151 nfs4_init_delmapcall()
11152 {
11153 	nfs4_delmapcall_t	*delmap_call;
11154 
11155 	delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11156 	delmap_call->call_id = curthread;
11157 	delmap_call->error = 0;
11158 
11159 	return (delmap_call);
11160 }
11161 
11162 static void
nfs4_free_delmapcall(nfs4_delmapcall_t * delmap_call)11163 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11164 {
11165 	kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11166 }
11167 
11168 /*
11169  * Searches for the current delmap caller (based on curthread) in the list of
11170  * callers.  If it is found, we remove it and free the delmap caller.
11171  * Returns:
11172  *      0 if the caller wasn't found
11173  *      1 if the caller was found, removed and freed.  *errp will be set
11174  *	to what the result of the delmap was.
11175  */
11176 static int
nfs4_find_and_delete_delmapcall(rnode4_t * rp,int * errp)11177 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11178 {
11179 	nfs4_delmapcall_t	*delmap_call;
11180 
11181 	/*
11182 	 * If the list doesn't exist yet, we create it and return
11183 	 * that the caller wasn't found.  No list = no callers.
11184 	 */
11185 	mutex_enter(&rp->r_statelock);
11186 	if (!(rp->r_flags & R4DELMAPLIST)) {
11187 		/* The list does not exist */
11188 		list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11189 		    offsetof(nfs4_delmapcall_t, call_node));
11190 		rp->r_flags |= R4DELMAPLIST;
11191 		mutex_exit(&rp->r_statelock);
11192 		return (0);
11193 	} else {
11194 		/* The list exists so search it */
11195 		for (delmap_call = list_head(&rp->r_indelmap);
11196 		    delmap_call != NULL;
11197 		    delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11198 			if (delmap_call->call_id == curthread) {
11199 				/* current caller is in the list */
11200 				*errp = delmap_call->error;
11201 				list_remove(&rp->r_indelmap, delmap_call);
11202 				mutex_exit(&rp->r_statelock);
11203 				nfs4_free_delmapcall(delmap_call);
11204 				return (1);
11205 			}
11206 		}
11207 	}
11208 	mutex_exit(&rp->r_statelock);
11209 	return (0);
11210 }
11211 
11212 /*
11213  * Remove some pages from an mmap'd vnode.  Just update the
11214  * count of pages.  If doing close-to-open, then flush and
11215  * commit all of the pages associated with this file.
11216  * Otherwise, start an asynchronous page flush to write out
11217  * any dirty pages.  This will also associate a credential
11218  * with the rnode which can be used to write the pages.
11219  */
11220 /* ARGSUSED */
11221 static void
nfs4_delmap_callback(struct as * as,void * arg,uint_t event)11222 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11223 {
11224 	nfs4_error_t		e = { 0, NFS4_OK, RPC_SUCCESS };
11225 	rnode4_t		*rp;
11226 	mntinfo4_t		*mi;
11227 	nfs4_delmap_args_t	*dmapp = (nfs4_delmap_args_t *)arg;
11228 
11229 	rp = VTOR4(dmapp->vp);
11230 	mi = VTOMI4(dmapp->vp);
11231 
11232 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11233 	ASSERT(rp->r_mapcnt >= 0);
11234 
11235 	/*
11236 	 * Initiate a page flush and potential commit if there are
11237 	 * pages, the file system was not mounted readonly, the segment
11238 	 * was mapped shared, and the pages themselves were writeable.
11239 	 */
11240 	if (nfs4_has_pages(dmapp->vp) &&
11241 	    !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11242 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11243 		mutex_enter(&rp->r_statelock);
11244 		rp->r_flags |= R4DIRTY;
11245 		mutex_exit(&rp->r_statelock);
11246 		e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11247 		    dmapp->len, dmapp->cr);
11248 		if (!e.error) {
11249 			mutex_enter(&rp->r_statelock);
11250 			e.error = rp->r_error;
11251 			rp->r_error = 0;
11252 			mutex_exit(&rp->r_statelock);
11253 		}
11254 	} else
11255 		e.error = 0;
11256 
11257 	if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11258 		(void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11259 		    B_INVAL, dmapp->cr, NULL);
11260 
11261 	if (e.error) {
11262 		e.stat = puterrno4(e.error);
11263 		nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11264 		    OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11265 		dmapp->caller->error = e.error;
11266 	}
11267 
11268 	/* Check to see if we need to close the file */
11269 
11270 	if (dmapp->vp->v_type == VREG) {
11271 		nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11272 		    CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11273 
11274 		if (e.error != 0 || e.stat != NFS4_OK) {
11275 			/*
11276 			 * Since it is possible that e.error == 0 and
11277 			 * e.stat != NFS4_OK (and vice versa),
11278 			 * we do the proper checking in order to get both
11279 			 * e.error and e.stat reporting the correct info.
11280 			 */
11281 			if (e.stat == NFS4_OK)
11282 				e.stat = puterrno4(e.error);
11283 			if (e.error == 0)
11284 				e.error = geterrno4(e.stat);
11285 
11286 			nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11287 			    OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11288 			dmapp->caller->error = e.error;
11289 		}
11290 	}
11291 
11292 	(void) as_delete_callback(as, arg);
11293 	kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11294 }
11295 
11296 
11297 static uint_t
fattr4_maxfilesize_to_bits(uint64_t ll)11298 fattr4_maxfilesize_to_bits(uint64_t ll)
11299 {
11300 	uint_t l = 1;
11301 
11302 	if (ll == 0) {
11303 		return (0);
11304 	}
11305 
11306 	if (ll & 0xffffffff00000000) {
11307 		l += 32; ll >>= 32;
11308 	}
11309 	if (ll & 0xffff0000) {
11310 		l += 16; ll >>= 16;
11311 	}
11312 	if (ll & 0xff00) {
11313 		l += 8; ll >>= 8;
11314 	}
11315 	if (ll & 0xf0) {
11316 		l += 4; ll >>= 4;
11317 	}
11318 	if (ll & 0xc) {
11319 		l += 2; ll >>= 2;
11320 	}
11321 	if (ll & 0x2) {
11322 		l += 1;
11323 	}
11324 	return (l);
11325 }
11326 
11327 static int
nfs4_have_xattrs(vnode_t * vp,ulong_t * valp,cred_t * cr)11328 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11329 {
11330 	vnode_t *avp = NULL;
11331 	int error;
11332 
11333 	if ((error = nfs4lookup_xattr(vp, "", &avp,
11334 	    LOOKUP_XATTR, cr)) == 0)
11335 		error = do_xattr_exists_check(avp, valp, cr);
11336 	if (avp)
11337 		VN_RELE(avp);
11338 
11339 	return (error);
11340 }
11341 
11342 /* ARGSUSED */
11343 int
nfs4_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)11344 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11345     caller_context_t *ct)
11346 {
11347 	int error;
11348 	hrtime_t t;
11349 	rnode4_t *rp;
11350 	nfs4_ga_res_t gar;
11351 	nfs4_ga_ext_res_t ger;
11352 
11353 	gar.n4g_ext_res = &ger;
11354 
11355 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
11356 		return (EIO);
11357 	if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11358 		*valp = MAXPATHLEN;
11359 		return (0);
11360 	}
11361 	if (cmd == _PC_ACL_ENABLED) {
11362 		*valp = _ACL_ACE_ENABLED;
11363 		return (0);
11364 	}
11365 
11366 	rp = VTOR4(vp);
11367 	if (cmd == _PC_XATTR_EXISTS) {
11368 		/*
11369 		 * The existence of the xattr directory is not sufficient
11370 		 * for determining whether generic user attributes exists.
11371 		 * The attribute directory could only be a transient directory
11372 		 * used for Solaris sysattr support.  Do a small readdir
11373 		 * to verify if the only entries are sysattrs or not.
11374 		 *
11375 		 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11376 		 * is NULL.  Once the xadir vp exists, we can create xattrs,
11377 		 * and we don't have any way to update the "base" object's
11378 		 * pc4_xattr_exists from the xattr or xadir.  Maybe FEM
11379 		 * could help out.
11380 		 */
11381 		if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11382 		    rp->r_xattr_dir == NULL) {
11383 			return (nfs4_have_xattrs(vp, valp, cr));
11384 		}
11385 	} else {  /* OLD CODE */
11386 		if (ATTRCACHE4_VALID(vp)) {
11387 			mutex_enter(&rp->r_statelock);
11388 			if (rp->r_pathconf.pc4_cache_valid) {
11389 				error = 0;
11390 				switch (cmd) {
11391 				case _PC_FILESIZEBITS:
11392 					*valp =
11393 					    rp->r_pathconf.pc4_filesizebits;
11394 					break;
11395 				case _PC_LINK_MAX:
11396 					*valp =
11397 					    rp->r_pathconf.pc4_link_max;
11398 					break;
11399 				case _PC_NAME_MAX:
11400 					*valp =
11401 					    rp->r_pathconf.pc4_name_max;
11402 					break;
11403 				case _PC_CHOWN_RESTRICTED:
11404 					*valp =
11405 					    rp->r_pathconf.pc4_chown_restricted;
11406 					break;
11407 				case _PC_NO_TRUNC:
11408 					*valp =
11409 					    rp->r_pathconf.pc4_no_trunc;
11410 					break;
11411 				default:
11412 					error = EINVAL;
11413 					break;
11414 				}
11415 				mutex_exit(&rp->r_statelock);
11416 #ifdef DEBUG
11417 				nfs4_pathconf_cache_hits++;
11418 #endif
11419 				return (error);
11420 			}
11421 			mutex_exit(&rp->r_statelock);
11422 		}
11423 	}
11424 #ifdef DEBUG
11425 	nfs4_pathconf_cache_misses++;
11426 #endif
11427 
11428 	t = gethrtime();
11429 
11430 	error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11431 
11432 	if (error) {
11433 		mutex_enter(&rp->r_statelock);
11434 		rp->r_pathconf.pc4_cache_valid = FALSE;
11435 		rp->r_pathconf.pc4_xattr_valid = FALSE;
11436 		mutex_exit(&rp->r_statelock);
11437 		return (error);
11438 	}
11439 
11440 	/* interpret the max filesize */
11441 	gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11442 	    fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11443 
11444 	/* Store the attributes we just received */
11445 	nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11446 
11447 	switch (cmd) {
11448 	case _PC_FILESIZEBITS:
11449 		*valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11450 		break;
11451 	case _PC_LINK_MAX:
11452 		*valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11453 		break;
11454 	case _PC_NAME_MAX:
11455 		*valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11456 		break;
11457 	case _PC_CHOWN_RESTRICTED:
11458 		*valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11459 		break;
11460 	case _PC_NO_TRUNC:
11461 		*valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11462 		break;
11463 	case _PC_XATTR_EXISTS:
11464 		if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11465 			if (error = nfs4_have_xattrs(vp, valp, cr))
11466 				return (error);
11467 		}
11468 		break;
11469 	default:
11470 		return (EINVAL);
11471 	}
11472 
11473 	return (0);
11474 }
11475 
11476 /*
11477  * Called by async thread to do synchronous pageio. Do the i/o, wait
11478  * for it to complete, and cleanup the page list when done.
11479  */
11480 static int
nfs4_sync_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)11481 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11482     int flags, cred_t *cr)
11483 {
11484 	int error;
11485 
11486 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11487 
11488 	error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11489 	if (flags & B_READ)
11490 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11491 	else
11492 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11493 	return (error);
11494 }
11495 
11496 /* ARGSUSED */
11497 static int
nfs4_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr,caller_context_t * ct)11498 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11499     int flags, cred_t *cr, caller_context_t *ct)
11500 {
11501 	int error;
11502 	rnode4_t *rp;
11503 
11504 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11505 		return (EIO);
11506 
11507 	if (pp == NULL)
11508 		return (EINVAL);
11509 
11510 	rp = VTOR4(vp);
11511 	mutex_enter(&rp->r_statelock);
11512 	rp->r_count++;
11513 	mutex_exit(&rp->r_statelock);
11514 
11515 	if (flags & B_ASYNC) {
11516 		error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11517 		    nfs4_sync_pageio);
11518 	} else
11519 		error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11520 	mutex_enter(&rp->r_statelock);
11521 	rp->r_count--;
11522 	cv_broadcast(&rp->r_cv);
11523 	mutex_exit(&rp->r_statelock);
11524 	return (error);
11525 }
11526 
11527 /* ARGSUSED */
11528 static void
nfs4_dispose(vnode_t * vp,page_t * pp,int fl,int dn,cred_t * cr,caller_context_t * ct)11529 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11530     caller_context_t *ct)
11531 {
11532 	int error;
11533 	rnode4_t *rp;
11534 	page_t *plist;
11535 	page_t *pptr;
11536 	offset3 offset;
11537 	count3 len;
11538 	k_sigset_t smask;
11539 
11540 	/*
11541 	 * We should get called with fl equal to either B_FREE or
11542 	 * B_INVAL.  Any other value is illegal.
11543 	 *
11544 	 * The page that we are either supposed to free or destroy
11545 	 * should be exclusive locked and its io lock should not
11546 	 * be held.
11547 	 */
11548 	ASSERT(fl == B_FREE || fl == B_INVAL);
11549 	ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11550 
11551 	rp = VTOR4(vp);
11552 
11553 	/*
11554 	 * If the page doesn't need to be committed or we shouldn't
11555 	 * even bother attempting to commit it, then just make sure
11556 	 * that the p_fsdata byte is clear and then either free or
11557 	 * destroy the page as appropriate.
11558 	 */
11559 	if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11560 		pp->p_fsdata = C_NOCOMMIT;
11561 		if (fl == B_FREE)
11562 			page_free(pp, dn);
11563 		else
11564 			page_destroy(pp, dn);
11565 		return;
11566 	}
11567 
11568 	/*
11569 	 * If there is a page invalidation operation going on, then
11570 	 * if this is one of the pages being destroyed, then just
11571 	 * clear the p_fsdata byte and then either free or destroy
11572 	 * the page as appropriate.
11573 	 */
11574 	mutex_enter(&rp->r_statelock);
11575 	if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11576 		mutex_exit(&rp->r_statelock);
11577 		pp->p_fsdata = C_NOCOMMIT;
11578 		if (fl == B_FREE)
11579 			page_free(pp, dn);
11580 		else
11581 			page_destroy(pp, dn);
11582 		return;
11583 	}
11584 
11585 	/*
11586 	 * If we are freeing this page and someone else is already
11587 	 * waiting to do a commit, then just unlock the page and
11588 	 * return.  That other thread will take care of commiting
11589 	 * this page.  The page can be freed sometime after the
11590 	 * commit has finished.  Otherwise, if the page is marked
11591 	 * as delay commit, then we may be getting called from
11592 	 * pvn_write_done, one page at a time.   This could result
11593 	 * in one commit per page, so we end up doing lots of small
11594 	 * commits instead of fewer larger commits.  This is bad,
11595 	 * we want do as few commits as possible.
11596 	 */
11597 	if (fl == B_FREE) {
11598 		if (rp->r_flags & R4COMMITWAIT) {
11599 			page_unlock(pp);
11600 			mutex_exit(&rp->r_statelock);
11601 			return;
11602 		}
11603 		if (pp->p_fsdata == C_DELAYCOMMIT) {
11604 			pp->p_fsdata = C_COMMIT;
11605 			page_unlock(pp);
11606 			mutex_exit(&rp->r_statelock);
11607 			return;
11608 		}
11609 	}
11610 
11611 	/*
11612 	 * Check to see if there is a signal which would prevent an
11613 	 * attempt to commit the pages from being successful.  If so,
11614 	 * then don't bother with all of the work to gather pages and
11615 	 * generate the unsuccessful RPC.  Just return from here and
11616 	 * let the page be committed at some later time.
11617 	 */
11618 	sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11619 	if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11620 		sigunintr(&smask);
11621 		page_unlock(pp);
11622 		mutex_exit(&rp->r_statelock);
11623 		return;
11624 	}
11625 	sigunintr(&smask);
11626 
11627 	/*
11628 	 * We are starting to need to commit pages, so let's try
11629 	 * to commit as many as possible at once to reduce the
11630 	 * overhead.
11631 	 *
11632 	 * Set the `commit inprogress' state bit.  We must
11633 	 * first wait until any current one finishes.  Then
11634 	 * we initialize the c_pages list with this page.
11635 	 */
11636 	while (rp->r_flags & R4COMMIT) {
11637 		rp->r_flags |= R4COMMITWAIT;
11638 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11639 		rp->r_flags &= ~R4COMMITWAIT;
11640 	}
11641 	rp->r_flags |= R4COMMIT;
11642 	mutex_exit(&rp->r_statelock);
11643 	ASSERT(rp->r_commit.c_pages == NULL);
11644 	rp->r_commit.c_pages = pp;
11645 	rp->r_commit.c_commbase = (offset3)pp->p_offset;
11646 	rp->r_commit.c_commlen = PAGESIZE;
11647 
11648 	/*
11649 	 * Gather together all other pages which can be committed.
11650 	 * They will all be chained off r_commit.c_pages.
11651 	 */
11652 	nfs4_get_commit(vp);
11653 
11654 	/*
11655 	 * Clear the `commit inprogress' status and disconnect
11656 	 * the list of pages to be committed from the rnode.
11657 	 * At this same time, we also save the starting offset
11658 	 * and length of data to be committed on the server.
11659 	 */
11660 	plist = rp->r_commit.c_pages;
11661 	rp->r_commit.c_pages = NULL;
11662 	offset = rp->r_commit.c_commbase;
11663 	len = rp->r_commit.c_commlen;
11664 	mutex_enter(&rp->r_statelock);
11665 	rp->r_flags &= ~R4COMMIT;
11666 	cv_broadcast(&rp->r_commit.c_cv);
11667 	mutex_exit(&rp->r_statelock);
11668 
11669 	if (curproc == proc_pageout || curproc == proc_fsflush ||
11670 	    nfs_zone() != VTOMI4(vp)->mi_zone) {
11671 		nfs4_async_commit(vp, plist, offset, len,
11672 		    cr, do_nfs4_async_commit);
11673 		return;
11674 	}
11675 
11676 	/*
11677 	 * Actually generate the COMMIT op over the wire operation.
11678 	 */
11679 	error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11680 
11681 	/*
11682 	 * If we got an error during the commit, just unlock all
11683 	 * of the pages.  The pages will get retransmitted to the
11684 	 * server during a putpage operation.
11685 	 */
11686 	if (error) {
11687 		while (plist != NULL) {
11688 			pptr = plist;
11689 			page_sub(&plist, pptr);
11690 			page_unlock(pptr);
11691 		}
11692 		return;
11693 	}
11694 
11695 	/*
11696 	 * We've tried as hard as we can to commit the data to stable
11697 	 * storage on the server.  We just unlock the rest of the pages
11698 	 * and clear the commit required state.  They will be put
11699 	 * onto the tail of the cachelist if they are nolonger
11700 	 * mapped.
11701 	 */
11702 	while (plist != pp) {
11703 		pptr = plist;
11704 		page_sub(&plist, pptr);
11705 		pptr->p_fsdata = C_NOCOMMIT;
11706 		page_unlock(pptr);
11707 	}
11708 
11709 	/*
11710 	 * It is possible that nfs4_commit didn't return error but
11711 	 * some other thread has modified the page we are going
11712 	 * to free/destroy.
11713 	 *    In this case we need to rewrite the page. Do an explicit check
11714 	 * before attempting to free/destroy the page. If modified, needs to
11715 	 * be rewritten so unlock the page and return.
11716 	 */
11717 	if (hat_ismod(pp)) {
11718 		pp->p_fsdata = C_NOCOMMIT;
11719 		page_unlock(pp);
11720 		return;
11721 	}
11722 
11723 	/*
11724 	 * Now, as appropriate, either free or destroy the page
11725 	 * that we were called with.
11726 	 */
11727 	pp->p_fsdata = C_NOCOMMIT;
11728 	if (fl == B_FREE)
11729 		page_free(pp, dn);
11730 	else
11731 		page_destroy(pp, dn);
11732 }
11733 
11734 /*
11735  * Commit requires that the current fh be the file written to.
11736  * The compound op structure is:
11737  *      PUTFH(file), COMMIT
11738  */
11739 static int
nfs4_commit(vnode_t * vp,offset4 offset,count4 count,cred_t * cr)11740 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11741 {
11742 	COMPOUND4args_clnt args;
11743 	COMPOUND4res_clnt res;
11744 	COMMIT4res *cm_res;
11745 	nfs_argop4 argop[2];
11746 	nfs_resop4 *resop;
11747 	int doqueue;
11748 	mntinfo4_t *mi;
11749 	rnode4_t *rp;
11750 	cred_t *cred_otw = NULL;
11751 	bool_t needrecov = FALSE;
11752 	nfs4_recov_state_t recov_state;
11753 	nfs4_open_stream_t *osp = NULL;
11754 	bool_t first_time = TRUE;	/* first time getting OTW cred */
11755 	bool_t last_time = FALSE;	/* last time getting OTW cred */
11756 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11757 
11758 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11759 
11760 	rp = VTOR4(vp);
11761 
11762 	mi = VTOMI4(vp);
11763 	recov_state.rs_flags = 0;
11764 	recov_state.rs_num_retry_despite_err = 0;
11765 get_commit_cred:
11766 	/*
11767 	 * Releases the osp, if a valid open stream is provided.
11768 	 * Puts a hold on the cred_otw and the new osp (if found).
11769 	 */
11770 	cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11771 	    &first_time, &last_time);
11772 	args.ctag = TAG_COMMIT;
11773 recov_retry:
11774 	/*
11775 	 * Commit ops: putfh file; commit
11776 	 */
11777 	args.array_len = 2;
11778 	args.array = argop;
11779 
11780 	e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11781 	    &recov_state, NULL);
11782 	if (e.error) {
11783 		crfree(cred_otw);
11784 		if (osp != NULL)
11785 			open_stream_rele(osp, rp);
11786 		return (e.error);
11787 	}
11788 
11789 	/* putfh directory */
11790 	argop[0].argop = OP_CPUTFH;
11791 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11792 
11793 	/* commit */
11794 	argop[1].argop = OP_COMMIT;
11795 	argop[1].nfs_argop4_u.opcommit.offset = offset;
11796 	argop[1].nfs_argop4_u.opcommit.count = count;
11797 
11798 	doqueue = 1;
11799 	rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11800 
11801 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11802 	if (!needrecov && e.error) {
11803 		nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11804 		    needrecov);
11805 		crfree(cred_otw);
11806 		if (e.error == EACCES && last_time == FALSE)
11807 			goto get_commit_cred;
11808 		if (osp != NULL)
11809 			open_stream_rele(osp, rp);
11810 		return (e.error);
11811 	}
11812 
11813 	if (needrecov) {
11814 		if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11815 		    NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11816 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11817 			    &recov_state, needrecov);
11818 			if (!e.error)
11819 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11820 			goto recov_retry;
11821 		}
11822 		if (e.error) {
11823 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11824 			    &recov_state, needrecov);
11825 			crfree(cred_otw);
11826 			if (osp != NULL)
11827 				open_stream_rele(osp, rp);
11828 			return (e.error);
11829 		}
11830 		/* fall through for res.status case */
11831 	}
11832 
11833 	if (res.status) {
11834 		e.error = geterrno4(res.status);
11835 		if (e.error == EACCES && last_time == FALSE) {
11836 			crfree(cred_otw);
11837 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11838 			    &recov_state, needrecov);
11839 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11840 			goto get_commit_cred;
11841 		}
11842 		/*
11843 		 * Can't do a nfs4_purge_stale_fh here because this
11844 		 * can cause a deadlock.  nfs4_commit can
11845 		 * be called from nfs4_dispose which can be called
11846 		 * indirectly via pvn_vplist_dirty.  nfs4_purge_stale_fh
11847 		 * can call back to pvn_vplist_dirty.
11848 		 */
11849 		if (e.error == ESTALE) {
11850 			mutex_enter(&rp->r_statelock);
11851 			rp->r_flags |= R4STALE;
11852 			if (!rp->r_error)
11853 				rp->r_error = e.error;
11854 			mutex_exit(&rp->r_statelock);
11855 			PURGE_ATTRCACHE4(vp);
11856 		} else {
11857 			mutex_enter(&rp->r_statelock);
11858 			if (!rp->r_error)
11859 				rp->r_error = e.error;
11860 			mutex_exit(&rp->r_statelock);
11861 		}
11862 	} else {
11863 		ASSERT(rp->r_flags & R4HAVEVERF);
11864 		resop = &res.array[1];	/* commit res */
11865 		cm_res = &resop->nfs_resop4_u.opcommit;
11866 		mutex_enter(&rp->r_statelock);
11867 		if (cm_res->writeverf == rp->r_writeverf) {
11868 			mutex_exit(&rp->r_statelock);
11869 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11870 			nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11871 			    &recov_state, needrecov);
11872 			crfree(cred_otw);
11873 			if (osp != NULL)
11874 				open_stream_rele(osp, rp);
11875 			return (0);
11876 		}
11877 		nfs4_set_mod(vp);
11878 		rp->r_writeverf = cm_res->writeverf;
11879 		mutex_exit(&rp->r_statelock);
11880 		e.error = NFS_VERF_MISMATCH;
11881 	}
11882 
11883 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11884 	nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11885 	crfree(cred_otw);
11886 	if (osp != NULL)
11887 		open_stream_rele(osp, rp);
11888 
11889 	return (e.error);
11890 }
11891 
11892 static void
nfs4_set_mod(vnode_t * vp)11893 nfs4_set_mod(vnode_t *vp)
11894 {
11895 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11896 
11897 	/* make sure we're looking at the master vnode, not a shadow */
11898 	pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11899 }
11900 
11901 /*
11902  * This function is used to gather a page list of the pages which
11903  * can be committed on the server.
11904  *
11905  * The calling thread must have set R4COMMIT.  This bit is used to
11906  * serialize access to the commit structure in the rnode.  As long
11907  * as the thread has set R4COMMIT, then it can manipulate the commit
11908  * structure without requiring any other locks.
11909  *
11910  * When this function is called from nfs4_dispose() the page passed
11911  * into nfs4_dispose() will be SE_EXCL locked, and so this function
11912  * will skip it. This is not a problem since we initially add the
11913  * page to the r_commit page list.
11914  *
11915  */
11916 static void
nfs4_get_commit(vnode_t * vp)11917 nfs4_get_commit(vnode_t *vp)
11918 {
11919 	rnode4_t *rp;
11920 	page_t *pp;
11921 	kmutex_t *vphm;
11922 
11923 	rp = VTOR4(vp);
11924 
11925 	ASSERT(rp->r_flags & R4COMMIT);
11926 
11927 	/* make sure we're looking at the master vnode, not a shadow */
11928 
11929 	if (IS_SHADOW(vp, rp))
11930 		vp = RTOV4(rp);
11931 
11932 	vphm = page_vnode_mutex(vp);
11933 	mutex_enter(vphm);
11934 
11935 	/*
11936 	 * If there are no pages associated with this vnode, then
11937 	 * just return.
11938 	 */
11939 	if ((pp = vp->v_pages) == NULL) {
11940 		mutex_exit(vphm);
11941 		return;
11942 	}
11943 
11944 	/*
11945 	 * Step through all of the pages associated with this vnode
11946 	 * looking for pages which need to be committed.
11947 	 */
11948 	do {
11949 		/* Skip marker pages. */
11950 		if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11951 			continue;
11952 
11953 		/*
11954 		 * First short-cut everything (without the page_lock)
11955 		 * and see if this page does not need to be committed
11956 		 * or is modified if so then we'll just skip it.
11957 		 */
11958 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11959 			continue;
11960 
11961 		/*
11962 		 * Attempt to lock the page.  If we can't, then
11963 		 * someone else is messing with it or we have been
11964 		 * called from nfs4_dispose and this is the page that
11965 		 * nfs4_dispose was called with.. anyway just skip it.
11966 		 */
11967 		if (!page_trylock(pp, SE_EXCL))
11968 			continue;
11969 
11970 		/*
11971 		 * Lets check again now that we have the page lock.
11972 		 */
11973 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11974 			page_unlock(pp);
11975 			continue;
11976 		}
11977 
11978 		/* this had better not be a free page */
11979 		ASSERT(PP_ISFREE(pp) == 0);
11980 
11981 		/*
11982 		 * The page needs to be committed and we locked it.
11983 		 * Update the base and length parameters and add it
11984 		 * to r_pages.
11985 		 */
11986 		if (rp->r_commit.c_pages == NULL) {
11987 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
11988 			rp->r_commit.c_commlen = PAGESIZE;
11989 		} else if (pp->p_offset < rp->r_commit.c_commbase) {
11990 			rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11991 			    (offset3)pp->p_offset + rp->r_commit.c_commlen;
11992 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
11993 		} else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11994 		    <= pp->p_offset) {
11995 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
11996 			    rp->r_commit.c_commbase + PAGESIZE;
11997 		}
11998 		page_add(&rp->r_commit.c_pages, pp);
11999 	} while ((pp = pp->p_vpnext) != vp->v_pages);
12000 
12001 	mutex_exit(vphm);
12002 }
12003 
12004 /*
12005  * This routine is used to gather together a page list of the pages
12006  * which are to be committed on the server.  This routine must not
12007  * be called if the calling thread holds any locked pages.
12008  *
12009  * The calling thread must have set R4COMMIT.  This bit is used to
12010  * serialize access to the commit structure in the rnode.  As long
12011  * as the thread has set R4COMMIT, then it can manipulate the commit
12012  * structure without requiring any other locks.
12013  */
12014 static void
nfs4_get_commit_range(vnode_t * vp,u_offset_t soff,size_t len)12015 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
12016 {
12017 
12018 	rnode4_t *rp;
12019 	page_t *pp;
12020 	u_offset_t end;
12021 	u_offset_t off;
12022 	ASSERT(len != 0);
12023 	rp = VTOR4(vp);
12024 	ASSERT(rp->r_flags & R4COMMIT);
12025 
12026 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12027 
12028 	/* make sure we're looking at the master vnode, not a shadow */
12029 
12030 	if (IS_SHADOW(vp, rp))
12031 		vp = RTOV4(rp);
12032 
12033 	/*
12034 	 * If there are no pages associated with this vnode, then
12035 	 * just return.
12036 	 */
12037 	if ((pp = vp->v_pages) == NULL)
12038 		return;
12039 	/*
12040 	 * Calculate the ending offset.
12041 	 */
12042 	end = soff + len;
12043 	for (off = soff; off < end; off += PAGESIZE) {
12044 		/*
12045 		 * Lookup each page by vp, offset.
12046 		 */
12047 		if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12048 			continue;
12049 		/*
12050 		 * If this page does not need to be committed or is
12051 		 * modified, then just skip it.
12052 		 */
12053 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12054 			page_unlock(pp);
12055 			continue;
12056 		}
12057 
12058 		ASSERT(PP_ISFREE(pp) == 0);
12059 		/*
12060 		 * The page needs to be committed and we locked it.
12061 		 * Update the base and length parameters and add it
12062 		 * to r_pages.
12063 		 */
12064 		if (rp->r_commit.c_pages == NULL) {
12065 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
12066 			rp->r_commit.c_commlen = PAGESIZE;
12067 		} else {
12068 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
12069 			    rp->r_commit.c_commbase + PAGESIZE;
12070 		}
12071 		page_add(&rp->r_commit.c_pages, pp);
12072 	}
12073 }
12074 
12075 /*
12076  * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12077  * Flushes and commits data to the server.
12078  */
12079 static int
nfs4_putpage_commit(vnode_t * vp,offset_t poff,size_t plen,cred_t * cr)12080 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12081 {
12082 	int error;
12083 	verifier4 write_verf;
12084 	rnode4_t *rp = VTOR4(vp);
12085 
12086 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12087 
12088 	/*
12089 	 * Flush the data portion of the file and then commit any
12090 	 * portions which need to be committed.  This may need to
12091 	 * be done twice if the server has changed state since
12092 	 * data was last written.  The data will need to be
12093 	 * rewritten to the server and then a new commit done.
12094 	 *
12095 	 * In fact, this may need to be done several times if the
12096 	 * server is having problems and crashing while we are
12097 	 * attempting to do this.
12098 	 */
12099 
12100 top:
12101 	/*
12102 	 * Do a flush based on the poff and plen arguments.  This
12103 	 * will synchronously write out any modified pages in the
12104 	 * range specified by (poff, plen). This starts all of the
12105 	 * i/o operations which will be waited for in the next
12106 	 * call to nfs4_putpage
12107 	 */
12108 
12109 	mutex_enter(&rp->r_statelock);
12110 	write_verf = rp->r_writeverf;
12111 	mutex_exit(&rp->r_statelock);
12112 
12113 	error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12114 	if (error == EAGAIN)
12115 		error = 0;
12116 
12117 	/*
12118 	 * Do a flush based on the poff and plen arguments.  This
12119 	 * will synchronously write out any modified pages in the
12120 	 * range specified by (poff, plen) and wait until all of
12121 	 * the asynchronous i/o's in that range are done as well.
12122 	 */
12123 	if (!error)
12124 		error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12125 
12126 	if (error)
12127 		return (error);
12128 
12129 	mutex_enter(&rp->r_statelock);
12130 	if (rp->r_writeverf != write_verf) {
12131 		mutex_exit(&rp->r_statelock);
12132 		goto top;
12133 	}
12134 	mutex_exit(&rp->r_statelock);
12135 
12136 	/*
12137 	 * Now commit any pages which might need to be committed.
12138 	 * If the error, NFS_VERF_MISMATCH, is returned, then
12139 	 * start over with the flush operation.
12140 	 */
12141 	error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12142 
12143 	if (error == NFS_VERF_MISMATCH)
12144 		goto top;
12145 
12146 	return (error);
12147 }
12148 
12149 /*
12150  * nfs4_commit_vp()  will wait for other pending commits and
12151  * will either commit the whole file or a range, plen dictates
12152  * if we commit whole file. a value of zero indicates the whole
12153  * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12154  */
12155 static int
nfs4_commit_vp(vnode_t * vp,u_offset_t poff,size_t plen,cred_t * cr,int wait_on_writes)12156 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12157     cred_t *cr, int wait_on_writes)
12158 {
12159 	rnode4_t *rp;
12160 	page_t *plist;
12161 	offset3 offset;
12162 	count3 len;
12163 
12164 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12165 
12166 	rp = VTOR4(vp);
12167 
12168 	/*
12169 	 *  before we gather commitable pages make
12170 	 *  sure there are no outstanding async writes
12171 	 */
12172 	if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12173 		mutex_enter(&rp->r_statelock);
12174 		while (rp->r_count > 0) {
12175 			cv_wait(&rp->r_cv, &rp->r_statelock);
12176 		}
12177 		mutex_exit(&rp->r_statelock);
12178 	}
12179 
12180 	/*
12181 	 * Set the `commit inprogress' state bit.  We must
12182 	 * first wait until any current one finishes.
12183 	 */
12184 	mutex_enter(&rp->r_statelock);
12185 	while (rp->r_flags & R4COMMIT) {
12186 		rp->r_flags |= R4COMMITWAIT;
12187 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12188 		rp->r_flags &= ~R4COMMITWAIT;
12189 	}
12190 	rp->r_flags |= R4COMMIT;
12191 	mutex_exit(&rp->r_statelock);
12192 
12193 	/*
12194 	 * Gather all of the pages which need to be
12195 	 * committed.
12196 	 */
12197 	if (plen == 0)
12198 		nfs4_get_commit(vp);
12199 	else
12200 		nfs4_get_commit_range(vp, poff, plen);
12201 
12202 	/*
12203 	 * Clear the `commit inprogress' bit and disconnect the
12204 	 * page list which was gathered by nfs4_get_commit.
12205 	 */
12206 	plist = rp->r_commit.c_pages;
12207 	rp->r_commit.c_pages = NULL;
12208 	offset = rp->r_commit.c_commbase;
12209 	len = rp->r_commit.c_commlen;
12210 	mutex_enter(&rp->r_statelock);
12211 	rp->r_flags &= ~R4COMMIT;
12212 	cv_broadcast(&rp->r_commit.c_cv);
12213 	mutex_exit(&rp->r_statelock);
12214 
12215 	/*
12216 	 * If any pages need to be committed, commit them and
12217 	 * then unlock them so that they can be freed some
12218 	 * time later.
12219 	 */
12220 	if (plist == NULL)
12221 		return (0);
12222 
12223 	/*
12224 	 * No error occurred during the flush portion
12225 	 * of this operation, so now attempt to commit
12226 	 * the data to stable storage on the server.
12227 	 *
12228 	 * This will unlock all of the pages on the list.
12229 	 */
12230 	return (nfs4_sync_commit(vp, plist, offset, len, cr));
12231 }
12232 
12233 static int
nfs4_sync_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)12234 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12235     cred_t *cr)
12236 {
12237 	int error;
12238 	page_t *pp;
12239 
12240 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12241 
12242 	error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12243 
12244 	/*
12245 	 * If we got an error, then just unlock all of the pages
12246 	 * on the list.
12247 	 */
12248 	if (error) {
12249 		while (plist != NULL) {
12250 			pp = plist;
12251 			page_sub(&plist, pp);
12252 			page_unlock(pp);
12253 		}
12254 		return (error);
12255 	}
12256 	/*
12257 	 * We've tried as hard as we can to commit the data to stable
12258 	 * storage on the server.  We just unlock the pages and clear
12259 	 * the commit required state.  They will get freed later.
12260 	 */
12261 	while (plist != NULL) {
12262 		pp = plist;
12263 		page_sub(&plist, pp);
12264 		pp->p_fsdata = C_NOCOMMIT;
12265 		page_unlock(pp);
12266 	}
12267 
12268 	return (error);
12269 }
12270 
12271 static void
do_nfs4_async_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)12272 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12273     cred_t *cr)
12274 {
12275 
12276 	(void) nfs4_sync_commit(vp, plist, offset, count, cr);
12277 }
12278 
12279 /*ARGSUSED*/
12280 static int
nfs4_setsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)12281 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12282     caller_context_t *ct)
12283 {
12284 	int		error = 0;
12285 	mntinfo4_t	*mi;
12286 	vattr_t		va;
12287 	vsecattr_t	nfsace4_vsap;
12288 
12289 	mi = VTOMI4(vp);
12290 	if (nfs_zone() != mi->mi_zone)
12291 		return (EIO);
12292 	if (mi->mi_flags & MI4_ACL) {
12293 		/* if we have a delegation, return it */
12294 		if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12295 			(void) nfs4delegreturn(VTOR4(vp),
12296 			    NFS4_DR_REOPEN|NFS4_DR_PUSH);
12297 
12298 		error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12299 		    NFS4_ACL_SET);
12300 		if (error) /* EINVAL */
12301 			return (error);
12302 
12303 		if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12304 			/*
12305 			 * These are aclent_t type entries.
12306 			 */
12307 			error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12308 			    vp->v_type == VDIR, FALSE);
12309 			if (error)
12310 				return (error);
12311 		} else {
12312 			/*
12313 			 * These are ace_t type entries.
12314 			 */
12315 			error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12316 			    FALSE);
12317 			if (error)
12318 				return (error);
12319 		}
12320 		bzero(&va, sizeof (va));
12321 		error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12322 		vs_ace4_destroy(&nfsace4_vsap);
12323 		return (error);
12324 	}
12325 	return (ENOSYS);
12326 }
12327 
12328 /* ARGSUSED */
12329 int
nfs4_getsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)12330 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12331     caller_context_t *ct)
12332 {
12333 	int		error;
12334 	mntinfo4_t	*mi;
12335 	nfs4_ga_res_t	gar;
12336 	rnode4_t	*rp = VTOR4(vp);
12337 
12338 	mi = VTOMI4(vp);
12339 	if (nfs_zone() != mi->mi_zone)
12340 		return (EIO);
12341 
12342 	bzero(&gar, sizeof (gar));
12343 	gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12344 
12345 	/*
12346 	 * vsecattr->vsa_mask holds the original acl request mask.
12347 	 * This is needed when determining what to return.
12348 	 * (See: nfs4_create_getsecattr_return())
12349 	 */
12350 	error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12351 	if (error) /* EINVAL */
12352 		return (error);
12353 
12354 	/*
12355 	 * If this is a referral stub, don't try to go OTW for an ACL
12356 	 */
12357 	if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12358 		return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12359 
12360 	if (mi->mi_flags & MI4_ACL) {
12361 		/*
12362 		 * Check if the data is cached and the cache is valid.  If it
12363 		 * is we don't go over the wire.
12364 		 */
12365 		if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12366 			mutex_enter(&rp->r_statelock);
12367 			if (rp->r_secattr != NULL) {
12368 				error = nfs4_create_getsecattr_return(
12369 				    rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12370 				    rp->r_attr.va_gid,
12371 				    vp->v_type == VDIR);
12372 				if (!error) { /* error == 0 - Success! */
12373 					mutex_exit(&rp->r_statelock);
12374 					return (error);
12375 				}
12376 			}
12377 			mutex_exit(&rp->r_statelock);
12378 		}
12379 
12380 		/*
12381 		 * The getattr otw call will always get both the acl, in
12382 		 * the form of a list of nfsace4's, and the number of acl
12383 		 * entries; independent of the value of gar.n4g_va.va_mask.
12384 		 */
12385 		error =  nfs4_getattr_otw(vp, &gar, cr, 1);
12386 		if (error) {
12387 			vs_ace4_destroy(&gar.n4g_vsa);
12388 			if (error == ENOTSUP || error == EOPNOTSUPP)
12389 				error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12390 			return (error);
12391 		}
12392 
12393 		if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12394 			/*
12395 			 * No error was returned, but according to the response
12396 			 * bitmap, neither was an acl.
12397 			 */
12398 			vs_ace4_destroy(&gar.n4g_vsa);
12399 			error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12400 			return (error);
12401 		}
12402 
12403 		/*
12404 		 * Update the cache with the ACL.
12405 		 */
12406 		nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12407 
12408 		error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12409 		    vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12410 		    vp->v_type == VDIR);
12411 		vs_ace4_destroy(&gar.n4g_vsa);
12412 		if ((error) && (vsecattr->vsa_mask &
12413 		    (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12414 		    (error != EACCES)) {
12415 			error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12416 		}
12417 		return (error);
12418 	}
12419 	error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12420 	return (error);
12421 }
12422 
12423 /*
12424  * The function returns:
12425  *	- 0 (zero) if the passed in "acl_mask" is a valid request.
12426  *	- EINVAL if the passed in "acl_mask" is an invalid request.
12427  *
12428  * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12429  * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12430  *
12431  * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12432  * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12433  * - We have a count field set without the corresponding acl field set. (e.g. -
12434  * VSA_ACECNT is set, but VSA_ACE is not)
12435  */
12436 static int
nfs4_is_acl_mask_valid(uint_t acl_mask,nfs4_acl_op_t op)12437 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12438 {
12439 	/* Shortcut the masks that are always valid. */
12440 	if (acl_mask == (VSA_ACE | VSA_ACECNT))
12441 		return (0);
12442 	if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12443 		return (0);
12444 
12445 	if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12446 		/*
12447 		 * We can't have any VSA_ACL type stuff in the mask now.
12448 		 */
12449 		if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12450 		    VSA_DFACLCNT))
12451 			return (EINVAL);
12452 
12453 		if (op == NFS4_ACL_SET) {
12454 			if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12455 				return (EINVAL);
12456 		}
12457 	}
12458 
12459 	if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12460 		/*
12461 		 * We can't have any VSA_ACE type stuff in the mask now.
12462 		 */
12463 		if (acl_mask & (VSA_ACE | VSA_ACECNT))
12464 			return (EINVAL);
12465 
12466 		if (op == NFS4_ACL_SET) {
12467 			if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12468 				return (EINVAL);
12469 
12470 			if ((acl_mask & VSA_DFACLCNT) &&
12471 			    !(acl_mask & VSA_DFACL))
12472 				return (EINVAL);
12473 		}
12474 	}
12475 	return (0);
12476 }
12477 
12478 /*
12479  * The theory behind creating the correct getsecattr return is simply this:
12480  * "Don't return anything that the caller is not expecting to have to free."
12481  */
12482 static int
nfs4_create_getsecattr_return(vsecattr_t * filled_vsap,vsecattr_t * vsap,uid_t uid,gid_t gid,int isdir)12483 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12484     uid_t uid, gid_t gid, int isdir)
12485 {
12486 	int error = 0;
12487 	/* Save the mask since the translators modify it. */
12488 	uint_t	orig_mask = vsap->vsa_mask;
12489 
12490 	if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12491 		error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12492 
12493 		if (error)
12494 			return (error);
12495 
12496 		/*
12497 		 * If the caller only asked for the ace count (VSA_ACECNT)
12498 		 * don't give them the full acl (VSA_ACE), free it.
12499 		 */
12500 		if (!orig_mask & VSA_ACE) {
12501 			if (vsap->vsa_aclentp != NULL) {
12502 				kmem_free(vsap->vsa_aclentp,
12503 				    vsap->vsa_aclcnt * sizeof (ace_t));
12504 				vsap->vsa_aclentp = NULL;
12505 			}
12506 		}
12507 		vsap->vsa_mask = orig_mask;
12508 
12509 	} else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12510 	    VSA_DFACLCNT)) {
12511 		error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12512 		    isdir, FALSE);
12513 
12514 		if (error)
12515 			return (error);
12516 
12517 		/*
12518 		 * If the caller only asked for the acl count (VSA_ACLCNT)
12519 		 * and/or the default acl count (VSA_DFACLCNT) don't give them
12520 		 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12521 		 */
12522 		if (!orig_mask & VSA_ACL) {
12523 			if (vsap->vsa_aclentp != NULL) {
12524 				kmem_free(vsap->vsa_aclentp,
12525 				    vsap->vsa_aclcnt * sizeof (aclent_t));
12526 				vsap->vsa_aclentp = NULL;
12527 			}
12528 		}
12529 
12530 		if (!orig_mask & VSA_DFACL) {
12531 			if (vsap->vsa_dfaclentp != NULL) {
12532 				kmem_free(vsap->vsa_dfaclentp,
12533 				    vsap->vsa_dfaclcnt * sizeof (aclent_t));
12534 				vsap->vsa_dfaclentp = NULL;
12535 			}
12536 		}
12537 		vsap->vsa_mask = orig_mask;
12538 	}
12539 	return (0);
12540 }
12541 
12542 /* ARGSUSED */
12543 int
nfs4_shrlock(vnode_t * vp,int cmd,struct shrlock * shr,int flag,cred_t * cr,caller_context_t * ct)12544 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12545     caller_context_t *ct)
12546 {
12547 	int error;
12548 
12549 	if (nfs_zone() != VTOMI4(vp)->mi_zone)
12550 		return (EIO);
12551 	/*
12552 	 * check for valid cmd parameter
12553 	 */
12554 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12555 		return (EINVAL);
12556 
12557 	/*
12558 	 * Check access permissions
12559 	 */
12560 	if ((cmd & F_SHARE) &&
12561 	    (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12562 	    (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12563 		return (EBADF);
12564 
12565 	/*
12566 	 * If the filesystem is mounted using local locking, pass the
12567 	 * request off to the local share code.
12568 	 */
12569 	if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12570 		return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12571 
12572 	switch (cmd) {
12573 	case F_SHARE:
12574 	case F_UNSHARE:
12575 		/*
12576 		 * This will be properly implemented later,
12577 		 * see RFE: 4823948 .
12578 		 */
12579 		error = EAGAIN;
12580 		break;
12581 
12582 	case F_HASREMOTELOCKS:
12583 		/*
12584 		 * NFS client can't store remote locks itself
12585 		 */
12586 		shr->s_access = 0;
12587 		error = 0;
12588 		break;
12589 
12590 	default:
12591 		error = EINVAL;
12592 		break;
12593 	}
12594 
12595 	return (error);
12596 }
12597 
12598 /*
12599  * Common code called by directory ops to update the attrcache
12600  */
12601 static int
nfs4_update_attrcache(nfsstat4 status,nfs4_ga_res_t * garp,hrtime_t t,vnode_t * vp,cred_t * cr)12602 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12603     hrtime_t t, vnode_t *vp, cred_t *cr)
12604 {
12605 	int error = 0;
12606 
12607 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12608 
12609 	if (status != NFS4_OK) {
12610 		/* getattr not done or failed */
12611 		PURGE_ATTRCACHE4(vp);
12612 		return (error);
12613 	}
12614 
12615 	if (garp) {
12616 		nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12617 	} else {
12618 		PURGE_ATTRCACHE4(vp);
12619 	}
12620 	return (error);
12621 }
12622 
12623 /*
12624  * Update directory caches for directory modification ops (link, rename, etc.)
12625  * When dinfo is NULL, manage dircaches in the old way.
12626  */
12627 static void
nfs4_update_dircaches(change_info4 * cinfo,vnode_t * dvp,vnode_t * vp,char * nm,dirattr_info_t * dinfo)12628 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12629     dirattr_info_t *dinfo)
12630 {
12631 	rnode4_t	*drp = VTOR4(dvp);
12632 
12633 	ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12634 
12635 	/* Purge rddir cache for dir since it changed */
12636 	if (drp->r_dir != NULL)
12637 		nfs4_purge_rddir_cache(dvp);
12638 
12639 	/*
12640 	 * If caller provided dinfo, then use it to manage dir caches.
12641 	 */
12642 	if (dinfo != NULL) {
12643 		if (vp != NULL) {
12644 			mutex_enter(&VTOR4(vp)->r_statev4_lock);
12645 			if (!VTOR4(vp)->created_v4) {
12646 				mutex_exit(&VTOR4(vp)->r_statev4_lock);
12647 				dnlc_update(dvp, nm, vp);
12648 			} else {
12649 				/*
12650 				 * XXX don't update if the created_v4 flag is
12651 				 * set
12652 				 */
12653 				mutex_exit(&VTOR4(vp)->r_statev4_lock);
12654 				NFS4_DEBUG(nfs4_client_state_debug,
12655 				    (CE_NOTE, "nfs4_update_dircaches: "
12656 				    "don't update dnlc: created_v4 flag"));
12657 			}
12658 		}
12659 
12660 		nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12661 		    dinfo->di_cred, FALSE, cinfo);
12662 
12663 		return;
12664 	}
12665 
12666 	/*
12667 	 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12668 	 * Since caller modified dir but didn't receive post-dirmod-op dir
12669 	 * attrs, the dir's attrs must be purged.
12670 	 *
12671 	 * XXX this check and dnlc update/purge should really be atomic,
12672 	 * XXX but can't use rnode statelock because it'll deadlock in
12673 	 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12674 	 * XXX does occur.
12675 	 *
12676 	 * XXX We also may want to check that atomic is true in the
12677 	 * XXX change_info struct. If it is not, the change_info may
12678 	 * XXX reflect changes by more than one clients which means that
12679 	 * XXX our cache may not be valid.
12680 	 */
12681 	PURGE_ATTRCACHE4(dvp);
12682 	if (drp->r_change == cinfo->before) {
12683 		/* no changes took place in the directory prior to our link */
12684 		if (vp != NULL) {
12685 			mutex_enter(&VTOR4(vp)->r_statev4_lock);
12686 			if (!VTOR4(vp)->created_v4) {
12687 				mutex_exit(&VTOR4(vp)->r_statev4_lock);
12688 				dnlc_update(dvp, nm, vp);
12689 			} else {
12690 				/*
12691 				 * XXX dont' update if the created_v4 flag
12692 				 * is set
12693 				 */
12694 				mutex_exit(&VTOR4(vp)->r_statev4_lock);
12695 				NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12696 				    "nfs4_update_dircaches: don't"
12697 				    " update dnlc: created_v4 flag"));
12698 			}
12699 		}
12700 	} else {
12701 		/* Another client modified directory - purge its dnlc cache */
12702 		dnlc_purge_vp(dvp);
12703 	}
12704 }
12705 
12706 /*
12707  * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12708  * file.
12709  *
12710  * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12711  * file (ie: client recovery) and otherwise set to FALSE.
12712  *
12713  * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12714  * initiated) calling functions.
12715  *
12716  * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12717  * of resending a 'lost' open request.
12718  *
12719  * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12720  * server that hands out BAD_SEQID on open confirm.
12721  *
12722  * Errors are returned via the nfs4_error_t parameter.
12723  */
12724 void
nfs4open_confirm(vnode_t * vp,seqid4 * seqid,stateid4 * stateid,cred_t * cr,bool_t reopening_file,bool_t * retry_open,nfs4_open_owner_t * oop,bool_t resend,nfs4_error_t * ep,int * num_bseqid_retryp)12725 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12726     bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12727     bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12728 {
12729 	COMPOUND4args_clnt args;
12730 	COMPOUND4res_clnt res;
12731 	nfs_argop4 argop[2];
12732 	nfs_resop4 *resop;
12733 	int doqueue = 1;
12734 	mntinfo4_t *mi;
12735 	OPEN_CONFIRM4args *open_confirm_args;
12736 	int needrecov;
12737 
12738 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12739 #if DEBUG
12740 	mutex_enter(&oop->oo_lock);
12741 	ASSERT(oop->oo_seqid_inuse);
12742 	mutex_exit(&oop->oo_lock);
12743 #endif
12744 
12745 recov_retry_confirm:
12746 	nfs4_error_zinit(ep);
12747 	*retry_open = FALSE;
12748 
12749 	if (resend)
12750 		args.ctag = TAG_OPEN_CONFIRM_LOST;
12751 	else
12752 		args.ctag = TAG_OPEN_CONFIRM;
12753 
12754 	args.array_len = 2;
12755 	args.array = argop;
12756 
12757 	/* putfh target fh */
12758 	argop[0].argop = OP_CPUTFH;
12759 	argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12760 
12761 	argop[1].argop = OP_OPEN_CONFIRM;
12762 	open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12763 
12764 	(*seqid) += 1;
12765 	open_confirm_args->seqid = *seqid;
12766 	open_confirm_args->open_stateid = *stateid;
12767 
12768 	mi = VTOMI4(vp);
12769 
12770 	rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12771 
12772 	if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12773 		nfs4_set_open_seqid((*seqid), oop, args.ctag);
12774 	}
12775 
12776 	needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12777 	if (!needrecov && ep->error)
12778 		return;
12779 
12780 	if (needrecov) {
12781 		bool_t abort = FALSE;
12782 
12783 		if (reopening_file == FALSE) {
12784 			nfs4_bseqid_entry_t *bsep = NULL;
12785 
12786 			if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12787 				bsep = nfs4_create_bseqid_entry(oop, NULL,
12788 				    vp, 0, args.ctag,
12789 				    open_confirm_args->seqid);
12790 
12791 			abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12792 			    NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12793 			if (bsep) {
12794 				kmem_free(bsep, sizeof (*bsep));
12795 				if (num_bseqid_retryp &&
12796 				    --(*num_bseqid_retryp) == 0)
12797 					abort = TRUE;
12798 			}
12799 		}
12800 		if ((ep->error == ETIMEDOUT ||
12801 		    res.status == NFS4ERR_RESOURCE) &&
12802 		    abort == FALSE && resend == FALSE) {
12803 			if (!ep->error)
12804 				xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12805 
12806 			delay(SEC_TO_TICK(confirm_retry_sec));
12807 			goto recov_retry_confirm;
12808 		}
12809 		/* State may have changed so retry the entire OPEN op */
12810 		if (abort == FALSE)
12811 			*retry_open = TRUE;
12812 		else
12813 			*retry_open = FALSE;
12814 		if (!ep->error)
12815 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12816 		return;
12817 	}
12818 
12819 	if (res.status) {
12820 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12821 		return;
12822 	}
12823 
12824 	resop = &res.array[1];  /* open confirm res */
12825 	bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12826 	    stateid, sizeof (*stateid));
12827 
12828 	xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12829 }
12830 
12831 /*
12832  * Return the credentials associated with a client state object.  The
12833  * caller is responsible for freeing the credentials.
12834  */
12835 
12836 static cred_t *
state_to_cred(nfs4_open_stream_t * osp)12837 state_to_cred(nfs4_open_stream_t *osp)
12838 {
12839 	cred_t *cr;
12840 
12841 	/*
12842 	 * It's ok to not lock the open stream and open owner to get
12843 	 * the oo_cred since this is only written once (upon creation)
12844 	 * and will not change.
12845 	 */
12846 	cr = osp->os_open_owner->oo_cred;
12847 	crhold(cr);
12848 
12849 	return (cr);
12850 }
12851 
12852 /*
12853  * nfs4_find_sysid
12854  *
12855  * Find the sysid for the knetconfig associated with the given mi.
12856  */
12857 static struct lm_sysid *
nfs4_find_sysid(mntinfo4_t * mi)12858 nfs4_find_sysid(mntinfo4_t *mi)
12859 {
12860 	ASSERT(nfs_zone() == mi->mi_zone);
12861 
12862 	/*
12863 	 * Switch from RDMA knconf to original mount knconf
12864 	 */
12865 	return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12866 	    mi->mi_curr_serv->sv_hostname, NULL));
12867 }
12868 
12869 #ifdef DEBUG
12870 /*
12871  * Return a string version of the call type for easy reading.
12872  */
12873 static char *
nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)12874 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12875 {
12876 	switch (ctype) {
12877 	case NFS4_LCK_CTYPE_NORM:
12878 		return ("NORMAL");
12879 	case NFS4_LCK_CTYPE_RECLAIM:
12880 		return ("RECLAIM");
12881 	case NFS4_LCK_CTYPE_RESEND:
12882 		return ("RESEND");
12883 	case NFS4_LCK_CTYPE_REINSTATE:
12884 		return ("REINSTATE");
12885 	default:
12886 		cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12887 		    "type %d", ctype);
12888 		return ("");
12889 	}
12890 }
12891 #endif
12892 
12893 /*
12894  * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12895  * Unlock requests don't have an over-the-wire locktype, so we just return
12896  * something non-threatening.
12897  */
12898 
12899 static nfs_lock_type4
flk_to_locktype(int cmd,int l_type)12900 flk_to_locktype(int cmd, int l_type)
12901 {
12902 	ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12903 
12904 	switch (l_type) {
12905 	case F_UNLCK:
12906 		return (READ_LT);
12907 	case F_RDLCK:
12908 		if (cmd == F_SETLK)
12909 			return (READ_LT);
12910 		else
12911 			return (READW_LT);
12912 	case F_WRLCK:
12913 		if (cmd == F_SETLK)
12914 			return (WRITE_LT);
12915 		else
12916 			return (WRITEW_LT);
12917 	}
12918 	panic("flk_to_locktype");
12919 	/*NOTREACHED*/
12920 }
12921 
12922 /*
12923  * Do some preliminary checks for nfs4frlock.
12924  */
12925 static int
nfs4frlock_validate_args(int cmd,flock64_t * flk,int flag,vnode_t * vp,u_offset_t offset)12926 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12927     u_offset_t offset)
12928 {
12929 	int error = 0;
12930 
12931 	/*
12932 	 * If we are setting a lock, check that the file is opened
12933 	 * with the correct mode.
12934 	 */
12935 	if (cmd == F_SETLK || cmd == F_SETLKW) {
12936 		if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12937 		    (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12938 			NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12939 			    "nfs4frlock_validate_args: file was opened with "
12940 			    "incorrect mode"));
12941 			return (EBADF);
12942 		}
12943 	}
12944 
12945 	/* Convert the offset. It may need to be restored before returning. */
12946 	if (error = convoff(vp, flk, 0, offset)) {
12947 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12948 		    "nfs4frlock_validate_args: convoff  =>  error= %d\n",
12949 		    error));
12950 		return (error);
12951 	}
12952 
12953 	return (error);
12954 }
12955 
12956 /*
12957  * Set the flock64's lm_sysid for nfs4frlock.
12958  */
12959 static int
nfs4frlock_get_sysid(struct lm_sysid ** lspp,vnode_t * vp,flock64_t * flk)12960 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12961 {
12962 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12963 
12964 	/* Find the lm_sysid */
12965 	*lspp = nfs4_find_sysid(VTOMI4(vp));
12966 
12967 	if (*lspp == NULL) {
12968 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12969 		    "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12970 		return (ENOLCK);
12971 	}
12972 
12973 	flk->l_sysid = lm_sysidt(*lspp);
12974 
12975 	return (0);
12976 }
12977 
12978 /*
12979  * Do the remaining preliminary setup for nfs4frlock.
12980  */
12981 static void
nfs4frlock_pre_setup(clock_t * tick_delayp,nfs4_recov_state_t * recov_statep,flock64_t * flk,short * whencep,vnode_t * vp,cred_t * search_cr,cred_t ** cred_otw)12982 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12983     flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12984     cred_t **cred_otw)
12985 {
12986 	/*
12987 	 * set tick_delay to the base delay time.
12988 	 * (NFS4_BASE_WAIT_TIME is in secs)
12989 	 */
12990 
12991 	*tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12992 
12993 	/*
12994 	 * If lock is relative to EOF, we need the newest length of the
12995 	 * file. Therefore invalidate the ATTR_CACHE.
12996 	 */
12997 
12998 	*whencep = flk->l_whence;
12999 
13000 	if (*whencep == 2)		/* SEEK_END */
13001 		PURGE_ATTRCACHE4(vp);
13002 
13003 	recov_statep->rs_flags = 0;
13004 	recov_statep->rs_num_retry_despite_err = 0;
13005 	*cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
13006 }
13007 
13008 /*
13009  * Initialize and allocate the data structures necessary for
13010  * the nfs4frlock call.
13011  * Allocates argsp's op array.
13012  */
13013 static void
nfs4frlock_call_init(COMPOUND4args_clnt * argsp,COMPOUND4args_clnt ** argspp,nfs_argop4 ** argopp,nfs4_op_hint_t * op_hintp,flock64_t * flk,int cmd,bool_t * retry,bool_t * did_start_fop,COMPOUND4res_clnt ** respp,bool_t * skip_get_err,nfs4_lost_rqst_t * lost_rqstp)13014 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
13015     nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13016     bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13017     bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13018 {
13019 	int		argoplist_size;
13020 	int		num_ops = 2;
13021 
13022 	*retry = FALSE;
13023 	*did_start_fop = FALSE;
13024 	*skip_get_err = FALSE;
13025 	lost_rqstp->lr_op = 0;
13026 	argoplist_size  = num_ops * sizeof (nfs_argop4);
13027 	/* fill array with zero */
13028 	*argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13029 
13030 	*argspp = argsp;
13031 	*respp = NULL;
13032 
13033 	argsp->array_len = num_ops;
13034 	argsp->array = *argopp;
13035 
13036 	/* initialize in case of error; will get real value down below */
13037 	argsp->ctag = TAG_NONE;
13038 
13039 	if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13040 		*op_hintp = OH_LOCKU;
13041 	else
13042 		*op_hintp = OH_OTHER;
13043 }
13044 
13045 /*
13046  * Call the nfs4_start_fop() for nfs4frlock, if necessary.  Assign
13047  * the proper nfs4_server_t for this instance of nfs4frlock.
13048  * Returns 0 (success) or an errno value.
13049  */
13050 static int
nfs4frlock_start_call(nfs4_lock_call_type_t ctype,vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,bool_t * did_start_fop,bool_t * startrecovp)13051 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13052     nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13053     bool_t *did_start_fop, bool_t *startrecovp)
13054 {
13055 	int error = 0;
13056 	rnode4_t *rp;
13057 
13058 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13059 
13060 	if (ctype == NFS4_LCK_CTYPE_NORM) {
13061 		error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13062 		    recov_statep, startrecovp);
13063 		if (error)
13064 			return (error);
13065 		*did_start_fop = TRUE;
13066 	} else {
13067 		*did_start_fop = FALSE;
13068 		*startrecovp = FALSE;
13069 	}
13070 
13071 	if (!error) {
13072 		rp = VTOR4(vp);
13073 
13074 		/* If the file failed recovery, just quit. */
13075 		mutex_enter(&rp->r_statelock);
13076 		if (rp->r_flags & R4RECOVERR) {
13077 			error = EIO;
13078 		}
13079 		mutex_exit(&rp->r_statelock);
13080 	}
13081 
13082 	return (error);
13083 }
13084 
13085 /*
13086  * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request.  A
13087  * resend nfs4frlock call is initiated by the recovery framework.
13088  * Acquires the lop and oop seqid synchronization.
13089  */
13090 static void
nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t * resend_rqstp,COMPOUND4args_clnt * argsp,nfs_argop4 * argop,nfs4_lock_owner_t ** lopp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,LOCK4args ** lock_argsp,LOCKU4args ** locku_argsp)13091 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13092     COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13093     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13094     LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13095 {
13096 	mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13097 	int error;
13098 
13099 	NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13100 	    (CE_NOTE,
13101 	    "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13102 	ASSERT(resend_rqstp != NULL);
13103 	ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13104 	    resend_rqstp->lr_op == OP_LOCKU);
13105 
13106 	*oopp = resend_rqstp->lr_oop;
13107 	if (resend_rqstp->lr_oop) {
13108 		open_owner_hold(resend_rqstp->lr_oop);
13109 		error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13110 		ASSERT(error == 0);	/* recov thread always succeeds */
13111 	}
13112 
13113 	/* Must resend this lost lock/locku request. */
13114 	ASSERT(resend_rqstp->lr_lop != NULL);
13115 	*lopp = resend_rqstp->lr_lop;
13116 	lock_owner_hold(resend_rqstp->lr_lop);
13117 	error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13118 	ASSERT(error == 0);	/* recov thread always succeeds */
13119 
13120 	*ospp = resend_rqstp->lr_osp;
13121 	if (*ospp)
13122 		open_stream_hold(resend_rqstp->lr_osp);
13123 
13124 	if (resend_rqstp->lr_op == OP_LOCK) {
13125 		LOCK4args *lock_args;
13126 
13127 		argop->argop = OP_LOCK;
13128 		*lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13129 		lock_args->locktype = resend_rqstp->lr_locktype;
13130 		lock_args->reclaim =
13131 		    (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13132 		lock_args->offset = resend_rqstp->lr_flk->l_start;
13133 		lock_args->length = resend_rqstp->lr_flk->l_len;
13134 		if (lock_args->length == 0)
13135 			lock_args->length = ~lock_args->length;
13136 		nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13137 		    mi2clientid(mi), &lock_args->locker);
13138 
13139 		switch (resend_rqstp->lr_ctype) {
13140 		case NFS4_LCK_CTYPE_RESEND:
13141 			argsp->ctag = TAG_LOCK_RESEND;
13142 			break;
13143 		case NFS4_LCK_CTYPE_REINSTATE:
13144 			argsp->ctag = TAG_LOCK_REINSTATE;
13145 			break;
13146 		case NFS4_LCK_CTYPE_RECLAIM:
13147 			argsp->ctag = TAG_LOCK_RECLAIM;
13148 			break;
13149 		default:
13150 			argsp->ctag = TAG_LOCK_UNKNOWN;
13151 			break;
13152 		}
13153 	} else {
13154 		LOCKU4args *locku_args;
13155 		nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13156 
13157 		argop->argop = OP_LOCKU;
13158 		*locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13159 		locku_args->locktype = READ_LT;
13160 		locku_args->seqid = lop->lock_seqid + 1;
13161 		mutex_enter(&lop->lo_lock);
13162 		locku_args->lock_stateid = lop->lock_stateid;
13163 		mutex_exit(&lop->lo_lock);
13164 		locku_args->offset = resend_rqstp->lr_flk->l_start;
13165 		locku_args->length = resend_rqstp->lr_flk->l_len;
13166 		if (locku_args->length == 0)
13167 			locku_args->length = ~locku_args->length;
13168 
13169 		switch (resend_rqstp->lr_ctype) {
13170 		case NFS4_LCK_CTYPE_RESEND:
13171 			argsp->ctag = TAG_LOCKU_RESEND;
13172 			break;
13173 		case NFS4_LCK_CTYPE_REINSTATE:
13174 			argsp->ctag = TAG_LOCKU_REINSTATE;
13175 			break;
13176 		default:
13177 			argsp->ctag = TAG_LOCK_UNKNOWN;
13178 			break;
13179 		}
13180 	}
13181 }
13182 
13183 /*
13184  * Setup the LOCKT4 arguments.
13185  */
13186 static void
nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype,nfs_argop4 * argop,LOCKT4args ** lockt_argsp,COMPOUND4args_clnt * argsp,flock64_t * flk,rnode4_t * rp)13187 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13188     LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13189     rnode4_t *rp)
13190 {
13191 	LOCKT4args *lockt_args;
13192 
13193 	ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13194 	ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13195 	argop->argop = OP_LOCKT;
13196 	argsp->ctag = TAG_LOCKT;
13197 	lockt_args = &argop->nfs_argop4_u.oplockt;
13198 
13199 	/*
13200 	 * The locktype will be READ_LT unless it's
13201 	 * a write lock. We do this because the Solaris
13202 	 * system call allows the combination of
13203 	 * F_UNLCK and F_GETLK* and so in that case the
13204 	 * unlock is mapped to a read.
13205 	 */
13206 	if (flk->l_type == F_WRLCK)
13207 		lockt_args->locktype = WRITE_LT;
13208 	else
13209 		lockt_args->locktype = READ_LT;
13210 
13211 	lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13212 	/* set the lock owner4 args */
13213 	nfs4_setlockowner_args(&lockt_args->owner, rp,
13214 	    ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13215 	    flk->l_pid);
13216 	lockt_args->offset = flk->l_start;
13217 	lockt_args->length = flk->l_len;
13218 	if (flk->l_len == 0)
13219 		lockt_args->length = ~lockt_args->length;
13220 
13221 	*lockt_argsp = lockt_args;
13222 }
13223 
13224 /*
13225  * If the client is holding a delegation, and the open stream to be used
13226  * with this lock request is a delegation open stream, then re-open the stream.
13227  * Sets the nfs4_error_t to all zeros unless the open stream has already
13228  * failed a reopen or we couldn't find the open stream.  NFS4ERR_DELAY
13229  * means the caller should retry (like a recovery retry).
13230  */
13231 static void
nfs4frlock_check_deleg(vnode_t * vp,nfs4_error_t * ep,cred_t * cr,int lt)13232 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13233 {
13234 	open_delegation_type4	dt;
13235 	bool_t			reopen_needed, force;
13236 	nfs4_open_stream_t	*osp;
13237 	open_claim_type4	oclaim;
13238 	rnode4_t		*rp = VTOR4(vp);
13239 	mntinfo4_t		*mi = VTOMI4(vp);
13240 
13241 	ASSERT(nfs_zone() == mi->mi_zone);
13242 
13243 	nfs4_error_zinit(ep);
13244 
13245 	mutex_enter(&rp->r_statev4_lock);
13246 	dt = rp->r_deleg_type;
13247 	mutex_exit(&rp->r_statev4_lock);
13248 
13249 	if (dt != OPEN_DELEGATE_NONE) {
13250 		nfs4_open_owner_t	*oop;
13251 
13252 		oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13253 		if (!oop) {
13254 			ep->stat = NFS4ERR_IO;
13255 			return;
13256 		}
13257 		/* returns with 'os_sync_lock' held */
13258 		osp = find_open_stream(oop, rp);
13259 		if (!osp) {
13260 			open_owner_rele(oop);
13261 			ep->stat = NFS4ERR_IO;
13262 			return;
13263 		}
13264 
13265 		if (osp->os_failed_reopen) {
13266 			NFS4_DEBUG((nfs4_open_stream_debug ||
13267 			    nfs4_client_lock_debug), (CE_NOTE,
13268 			    "nfs4frlock_check_deleg: os_failed_reopen set "
13269 			    "for osp %p, cr %p, rp %s", (void *)osp,
13270 			    (void *)cr, rnode4info(rp)));
13271 			mutex_exit(&osp->os_sync_lock);
13272 			open_stream_rele(osp, rp);
13273 			open_owner_rele(oop);
13274 			ep->stat = NFS4ERR_IO;
13275 			return;
13276 		}
13277 
13278 		/*
13279 		 * Determine whether a reopen is needed.  If this
13280 		 * is a delegation open stream, then send the open
13281 		 * to the server to give visibility to the open owner.
13282 		 * Even if it isn't a delegation open stream, we need
13283 		 * to check if the previous open CLAIM_DELEGATE_CUR
13284 		 * was sufficient.
13285 		 */
13286 
13287 		reopen_needed = osp->os_delegation ||
13288 		    ((lt == F_RDLCK &&
13289 		    !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13290 		    (lt == F_WRLCK &&
13291 		    !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13292 
13293 		mutex_exit(&osp->os_sync_lock);
13294 		open_owner_rele(oop);
13295 
13296 		if (reopen_needed) {
13297 			/*
13298 			 * Always use CLAIM_PREVIOUS after server reboot.
13299 			 * The server will reject CLAIM_DELEGATE_CUR if
13300 			 * it is used during the grace period.
13301 			 */
13302 			mutex_enter(&mi->mi_lock);
13303 			if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13304 				oclaim = CLAIM_PREVIOUS;
13305 				force = TRUE;
13306 			} else {
13307 				oclaim = CLAIM_DELEGATE_CUR;
13308 				force = FALSE;
13309 			}
13310 			mutex_exit(&mi->mi_lock);
13311 
13312 			nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13313 			if (ep->error == EAGAIN) {
13314 				nfs4_error_zinit(ep);
13315 				ep->stat = NFS4ERR_DELAY;
13316 			}
13317 		}
13318 		open_stream_rele(osp, rp);
13319 		osp = NULL;
13320 	}
13321 }
13322 
13323 /*
13324  * Setup the LOCKU4 arguments.
13325  * Returns errors via the nfs4_error_t.
13326  * NFS4_OK		no problems.  *go_otwp is TRUE if call should go
13327  *			over-the-wire.  The caller must release the
13328  *			reference on *lopp.
13329  * NFS4ERR_DELAY	caller should retry (like recovery retry)
13330  * (other)		unrecoverable error.
13331  */
13332 static void
nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype,nfs_argop4 * argop,LOCKU4args ** locku_argsp,flock64_t * flk,nfs4_lock_owner_t ** lopp,nfs4_error_t * ep,COMPOUND4args_clnt * argsp,vnode_t * vp,int flag,u_offset_t offset,cred_t * cr,bool_t * skip_get_err,bool_t * go_otwp)13333 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13334     LOCKU4args **locku_argsp, flock64_t *flk,
13335     nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13336     vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13337     bool_t *skip_get_err, bool_t *go_otwp)
13338 {
13339 	nfs4_lock_owner_t	*lop = NULL;
13340 	LOCKU4args		*locku_args;
13341 	pid_t			pid;
13342 	bool_t			is_spec = FALSE;
13343 	rnode4_t		*rp = VTOR4(vp);
13344 
13345 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13346 	ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13347 
13348 	nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13349 	if (ep->error || ep->stat)
13350 		return;
13351 
13352 	argop->argop = OP_LOCKU;
13353 	if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13354 		argsp->ctag = TAG_LOCKU_REINSTATE;
13355 	else
13356 		argsp->ctag = TAG_LOCKU;
13357 	locku_args = &argop->nfs_argop4_u.oplocku;
13358 	*locku_argsp = locku_args;
13359 
13360 	/* locktype should be set to any legal value */
13361 	locku_args->locktype = READ_LT;
13362 
13363 	pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13364 	    flk->l_pid;
13365 
13366 	/*
13367 	 * Get the lock owner stateid.  If no lock owner
13368 	 * exists, return success.
13369 	 */
13370 	lop = find_lock_owner(rp, pid, LOWN_ANY);
13371 	*lopp = lop;
13372 	if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13373 		is_spec = TRUE;
13374 	if (!lop || is_spec) {
13375 		/*
13376 		 * No lock owner so no locks to unlock.
13377 		 * Return success.  If there was a failed
13378 		 * reclaim earlier, the lock might still be
13379 		 * registered with the local locking code,
13380 		 * so notify it of the unlock.
13381 		 *
13382 		 * If the lockowner is using a special stateid,
13383 		 * then the original lock request (that created
13384 		 * this lockowner) was never successful, so we
13385 		 * have no lock to undo OTW.
13386 		 */
13387 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13388 		    "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13389 		    "(%ld) so return success", (long)pid));
13390 
13391 		if (ctype == NFS4_LCK_CTYPE_NORM)
13392 			flk->l_pid = curproc->p_pid;
13393 		nfs4_register_lock_locally(vp, flk, flag, offset);
13394 		/*
13395 		 * Release our hold and NULL out so final_cleanup
13396 		 * doesn't try to end a lock seqid sync we
13397 		 * never started.
13398 		 */
13399 		if (is_spec) {
13400 			lock_owner_rele(lop);
13401 			*lopp = NULL;
13402 		}
13403 		*skip_get_err = TRUE;
13404 		*go_otwp = FALSE;
13405 		return;
13406 	}
13407 
13408 	ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13409 	if (ep->error == EAGAIN) {
13410 		lock_owner_rele(lop);
13411 		*lopp = NULL;
13412 		return;
13413 	}
13414 
13415 	mutex_enter(&lop->lo_lock);
13416 	locku_args->lock_stateid = lop->lock_stateid;
13417 	mutex_exit(&lop->lo_lock);
13418 	locku_args->seqid = lop->lock_seqid + 1;
13419 
13420 	/* leave the ref count on lop, rele after RPC call */
13421 
13422 	locku_args->offset = flk->l_start;
13423 	locku_args->length = flk->l_len;
13424 	if (flk->l_len == 0)
13425 		locku_args->length = ~locku_args->length;
13426 
13427 	*go_otwp = TRUE;
13428 }
13429 
13430 /*
13431  * Setup the LOCK4 arguments.
13432  *
13433  * Returns errors via the nfs4_error_t.
13434  * NFS4_OK		no problems
13435  * NFS4ERR_DELAY	caller should retry (like recovery retry)
13436  * (other)		unrecoverable error
13437  */
13438 static void
nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype,LOCK4args ** lock_argsp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,nfs_argop4 * argop,COMPOUND4args_clnt * argsp,flock64_t * flk,int cmd,vnode_t * vp,cred_t * cr,nfs4_error_t * ep)13439 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13440     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13441     nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13442     flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13443 {
13444 	LOCK4args		*lock_args;
13445 	nfs4_open_owner_t	*oop = NULL;
13446 	nfs4_open_stream_t	*osp = NULL;
13447 	nfs4_lock_owner_t	*lop = NULL;
13448 	pid_t			pid;
13449 	rnode4_t		*rp = VTOR4(vp);
13450 
13451 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13452 
13453 	nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13454 	if (ep->error || ep->stat != NFS4_OK)
13455 		return;
13456 
13457 	argop->argop = OP_LOCK;
13458 	if (ctype == NFS4_LCK_CTYPE_NORM)
13459 		argsp->ctag = TAG_LOCK;
13460 	else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13461 		argsp->ctag = TAG_RELOCK;
13462 	else
13463 		argsp->ctag = TAG_LOCK_REINSTATE;
13464 	lock_args = &argop->nfs_argop4_u.oplock;
13465 	lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13466 	lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13467 	/*
13468 	 * Get the lock owner.  If no lock owner exists,
13469 	 * create a 'temporary' one and grab the open seqid
13470 	 * synchronization (which puts a hold on the open
13471 	 * owner and open stream).
13472 	 * This also grabs the lock seqid synchronization.
13473 	 */
13474 	pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13475 	ep->stat =
13476 	    nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13477 
13478 	if (ep->stat != NFS4_OK)
13479 		goto out;
13480 
13481 	nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13482 	    &lock_args->locker);
13483 
13484 	lock_args->offset = flk->l_start;
13485 	lock_args->length = flk->l_len;
13486 	if (flk->l_len == 0)
13487 		lock_args->length = ~lock_args->length;
13488 	*lock_argsp = lock_args;
13489 out:
13490 	*oopp = oop;
13491 	*ospp = osp;
13492 	*lopp = lop;
13493 }
13494 
13495 /*
13496  * After we get the reply from the server, record the proper information
13497  * for possible resend lock requests.
13498  */
13499 static void
nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype,int error,nfs_lock_type4 locktype,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,nfs4_lock_owner_t * lop,flock64_t * flk,nfs4_lost_rqst_t * lost_rqstp,cred_t * cr,vnode_t * vp)13500 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13501     nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13502     nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13503     nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13504 {
13505 	bool_t unlock = (flk->l_type == F_UNLCK);
13506 
13507 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13508 	ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13509 	    ctype == NFS4_LCK_CTYPE_REINSTATE);
13510 
13511 	if (error != 0 && !unlock) {
13512 		NFS4_DEBUG((nfs4_lost_rqst_debug ||
13513 		    nfs4_client_lock_debug), (CE_NOTE,
13514 		    "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13515 		    " for lop %p", (void *)lop));
13516 		ASSERT(lop != NULL);
13517 		mutex_enter(&lop->lo_lock);
13518 		lop->lo_pending_rqsts = 1;
13519 		mutex_exit(&lop->lo_lock);
13520 	}
13521 
13522 	lost_rqstp->lr_putfirst = FALSE;
13523 	lost_rqstp->lr_op = 0;
13524 
13525 	/*
13526 	 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13527 	 * recovery purposes so that the lock request that was sent
13528 	 * can be saved and re-issued later.  Ditto for EIO from a forced
13529 	 * unmount.  This is done to have the client's local locking state
13530 	 * match the v4 server's state; that is, the request was
13531 	 * potentially received and accepted by the server but the client
13532 	 * thinks it was not.
13533 	 */
13534 	if (error == ETIMEDOUT || error == EINTR ||
13535 	    NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13536 		NFS4_DEBUG((nfs4_lost_rqst_debug ||
13537 		    nfs4_client_lock_debug), (CE_NOTE,
13538 		    "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13539 		    "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13540 		    (void *)lop, (void *)oop, (void *)osp));
13541 		if (unlock)
13542 			lost_rqstp->lr_op = OP_LOCKU;
13543 		else {
13544 			lost_rqstp->lr_op = OP_LOCK;
13545 			lost_rqstp->lr_locktype = locktype;
13546 		}
13547 		/*
13548 		 * Objects are held and rele'd via the recovery code.
13549 		 * See nfs4_save_lost_rqst.
13550 		 */
13551 		lost_rqstp->lr_vp = vp;
13552 		lost_rqstp->lr_dvp = NULL;
13553 		lost_rqstp->lr_oop = oop;
13554 		lost_rqstp->lr_osp = osp;
13555 		lost_rqstp->lr_lop = lop;
13556 		lost_rqstp->lr_cr = cr;
13557 		switch (ctype) {
13558 		case NFS4_LCK_CTYPE_NORM:
13559 			flk->l_pid = ttoproc(curthread)->p_pid;
13560 			lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13561 			break;
13562 		case NFS4_LCK_CTYPE_REINSTATE:
13563 			lost_rqstp->lr_putfirst = TRUE;
13564 			lost_rqstp->lr_ctype = ctype;
13565 			break;
13566 		default:
13567 			break;
13568 		}
13569 		lost_rqstp->lr_flk = flk;
13570 	}
13571 }
13572 
13573 /*
13574  * Update lop's seqid.  Also update the seqid stored in a resend request,
13575  * if any.  (Some recovery errors increment the seqid, and we may have to
13576  * send the resend request again.)
13577  */
13578 
13579 static void
nfs4frlock_bump_seqid(LOCK4args * lock_args,LOCKU4args * locku_args,nfs4_open_owner_t * oop,nfs4_lock_owner_t * lop,nfs4_tag_type_t tag_type)13580 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13581     nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13582 {
13583 	if (lock_args) {
13584 		if (lock_args->locker.new_lock_owner == TRUE)
13585 			nfs4_get_and_set_next_open_seqid(oop, tag_type);
13586 		else {
13587 			ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13588 			nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13589 		}
13590 	} else if (locku_args) {
13591 		ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13592 		nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13593 	}
13594 }
13595 
13596 /*
13597  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13598  * COMPOUND4 args/res for calls that need to retry.
13599  * Switches the *cred_otwp to base_cr.
13600  */
13601 static void
nfs4frlock_check_access(vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,bool_t * did_start_fop,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,int error,nfs4_lock_owner_t ** lopp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,cred_t * base_cr,cred_t ** cred_otwp)13602 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13603     nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13604     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13605     nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13606     nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13607 {
13608 	nfs4_open_owner_t	*oop = *oopp;
13609 	nfs4_open_stream_t	*osp = *ospp;
13610 	nfs4_lock_owner_t	*lop = *lopp;
13611 	nfs_argop4		*argop = (*argspp)->array;
13612 
13613 	if (*did_start_fop) {
13614 		nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13615 		    needrecov);
13616 		*did_start_fop = FALSE;
13617 	}
13618 	ASSERT((*argspp)->array_len == 2);
13619 	if (argop[1].argop == OP_LOCK)
13620 		nfs4args_lock_free(&argop[1]);
13621 	else if (argop[1].argop == OP_LOCKT)
13622 		nfs4args_lockt_free(&argop[1]);
13623 	kmem_free(argop, 2 * sizeof (nfs_argop4));
13624 	if (!error)
13625 		xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13626 	*argspp = NULL;
13627 	*respp = NULL;
13628 
13629 	if (lop) {
13630 		nfs4_end_lock_seqid_sync(lop);
13631 		lock_owner_rele(lop);
13632 		*lopp = NULL;
13633 	}
13634 
13635 	/* need to free up the reference on osp for lock args */
13636 	if (osp != NULL) {
13637 		open_stream_rele(osp, VTOR4(vp));
13638 		*ospp = NULL;
13639 	}
13640 
13641 	/* need to free up the reference on oop for lock args */
13642 	if (oop != NULL) {
13643 		nfs4_end_open_seqid_sync(oop);
13644 		open_owner_rele(oop);
13645 		*oopp = NULL;
13646 	}
13647 
13648 	crfree(*cred_otwp);
13649 	*cred_otwp = base_cr;
13650 	crhold(*cred_otwp);
13651 }
13652 
13653 /*
13654  * Function to process the client's recovery for nfs4frlock.
13655  * Returns TRUE if we should retry the lock request; FALSE otherwise.
13656  *
13657  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13658  * COMPOUND4 args/res for calls that need to retry.
13659  *
13660  * Note: the rp's r_lkserlock is *not* dropped during this path.
13661  */
13662 static bool_t
nfs4frlock_recovery(int needrecov,nfs4_error_t * ep,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,LOCK4args * lock_args,LOCKU4args * locku_args,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,rnode4_t * rp,vnode_t * vp,nfs4_recov_state_t * recov_statep,nfs4_op_hint_t op_hint,bool_t * did_start_fop,nfs4_lost_rqst_t * lost_rqstp,flock64_t * flk)13663 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13664     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13665     LOCK4args *lock_args, LOCKU4args *locku_args,
13666     nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13667     nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13668     nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13669     bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13670 {
13671 	nfs4_open_owner_t	*oop = *oopp;
13672 	nfs4_open_stream_t	*osp = *ospp;
13673 	nfs4_lock_owner_t	*lop = *lopp;
13674 
13675 	bool_t abort, retry;
13676 
13677 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13678 	ASSERT((*argspp) != NULL);
13679 	ASSERT((*respp) != NULL);
13680 	if (lock_args || locku_args)
13681 		ASSERT(lop != NULL);
13682 
13683 	NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13684 	    (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13685 
13686 	retry = TRUE;
13687 	abort = FALSE;
13688 	if (needrecov) {
13689 		nfs4_bseqid_entry_t *bsep = NULL;
13690 		nfs_opnum4 op;
13691 
13692 		op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13693 
13694 		if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13695 			seqid4 seqid;
13696 
13697 			if (lock_args) {
13698 				if (lock_args->locker.new_lock_owner == TRUE)
13699 					seqid = lock_args->locker.locker4_u.
13700 					    open_owner.open_seqid;
13701 				else
13702 					seqid = lock_args->locker.locker4_u.
13703 					    lock_owner.lock_seqid;
13704 			} else if (locku_args) {
13705 				seqid = locku_args->seqid;
13706 			} else {
13707 				seqid = 0;
13708 			}
13709 
13710 			bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13711 			    flk->l_pid, (*argspp)->ctag, seqid);
13712 		}
13713 
13714 		abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13715 		    (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13716 		    lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13717 		    NULL, op, bsep, NULL, NULL);
13718 
13719 		if (bsep)
13720 			kmem_free(bsep, sizeof (*bsep));
13721 	}
13722 
13723 	/*
13724 	 * Return that we do not want to retry the request for 3 cases:
13725 	 * 1. If we received EINTR or are bailing out because of a forced
13726 	 *    unmount, we came into this code path just for the sake of
13727 	 *    initiating recovery, we now need to return the error.
13728 	 * 2. If we have aborted recovery.
13729 	 * 3. We received NFS4ERR_BAD_SEQID.
13730 	 */
13731 	if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13732 	    abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13733 		retry = FALSE;
13734 
13735 	if (*did_start_fop == TRUE) {
13736 		nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13737 		    needrecov);
13738 		*did_start_fop = FALSE;
13739 	}
13740 
13741 	if (retry == TRUE) {
13742 		nfs_argop4	*argop;
13743 
13744 		argop = (*argspp)->array;
13745 		ASSERT((*argspp)->array_len == 2);
13746 
13747 		if (argop[1].argop == OP_LOCK)
13748 			nfs4args_lock_free(&argop[1]);
13749 		else if (argop[1].argop == OP_LOCKT)
13750 			nfs4args_lockt_free(&argop[1]);
13751 		kmem_free(argop, 2 * sizeof (nfs_argop4));
13752 		if (!ep->error)
13753 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13754 		*respp = NULL;
13755 		*argspp = NULL;
13756 	}
13757 
13758 	if (lop != NULL) {
13759 		nfs4_end_lock_seqid_sync(lop);
13760 		lock_owner_rele(lop);
13761 	}
13762 
13763 	*lopp = NULL;
13764 
13765 	/* need to free up the reference on osp for lock args */
13766 	if (osp != NULL) {
13767 		open_stream_rele(osp, rp);
13768 		*ospp = NULL;
13769 	}
13770 
13771 	/* need to free up the reference on oop for lock args */
13772 	if (oop != NULL) {
13773 		nfs4_end_open_seqid_sync(oop);
13774 		open_owner_rele(oop);
13775 		*oopp = NULL;
13776 	}
13777 
13778 	return (retry);
13779 }
13780 
13781 /*
13782  * Handles the successful reply from the server for nfs4frlock.
13783  */
13784 static void
nfs4frlock_results_ok(nfs4_lock_call_type_t ctype,int cmd,flock64_t * flk,vnode_t * vp,int flag,u_offset_t offset,nfs4_lost_rqst_t * resend_rqstp)13785 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13786     vnode_t *vp, int flag, u_offset_t offset,
13787     nfs4_lost_rqst_t *resend_rqstp)
13788 {
13789 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13790 	if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13791 	    (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13792 		if (ctype == NFS4_LCK_CTYPE_NORM) {
13793 			flk->l_pid = ttoproc(curthread)->p_pid;
13794 			/*
13795 			 * We do not register lost locks locally in
13796 			 * the 'resend' case since the user/application
13797 			 * doesn't think we have the lock.
13798 			 */
13799 			ASSERT(!resend_rqstp);
13800 			nfs4_register_lock_locally(vp, flk, flag, offset);
13801 		}
13802 	}
13803 }
13804 
13805 /*
13806  * Handle the DENIED reply from the server for nfs4frlock.
13807  * Returns TRUE if we should retry the request; FALSE otherwise.
13808  *
13809  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13810  * COMPOUND4 args/res for calls that need to retry.  Can also
13811  * drop and regrab the r_lkserlock.
13812  */
13813 static bool_t
nfs4frlock_results_denied(nfs4_lock_call_type_t ctype,LOCK4args * lock_args,LOCKT4args * lockt_args,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,int cmd,vnode_t * vp,flock64_t * flk,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,clock_t * tick_delayp,short * whencep,int * errorp,nfs_resop4 * resop,cred_t * cr,bool_t * did_start_fop,bool_t * skip_get_err)13814 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13815     LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13816     nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13817     vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13818     nfs4_recov_state_t *recov_statep, int needrecov,
13819     COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13820     clock_t *tick_delayp, short *whencep, int *errorp,
13821     nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13822     bool_t *skip_get_err)
13823 {
13824 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13825 
13826 	if (lock_args) {
13827 		nfs4_open_owner_t	*oop = *oopp;
13828 		nfs4_open_stream_t	*osp = *ospp;
13829 		nfs4_lock_owner_t	*lop = *lopp;
13830 		int			intr;
13831 
13832 		/*
13833 		 * Blocking lock needs to sleep and retry from the request.
13834 		 *
13835 		 * Do not block and wait for 'resend' or 'reinstate'
13836 		 * lock requests, just return the error.
13837 		 *
13838 		 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13839 		 */
13840 		if (cmd == F_SETLKW) {
13841 			rnode4_t *rp = VTOR4(vp);
13842 			nfs_argop4 *argop = (*argspp)->array;
13843 
13844 			ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13845 
13846 			nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13847 			    recov_statep, needrecov);
13848 			*did_start_fop = FALSE;
13849 			ASSERT((*argspp)->array_len == 2);
13850 			if (argop[1].argop == OP_LOCK)
13851 				nfs4args_lock_free(&argop[1]);
13852 			else if (argop[1].argop == OP_LOCKT)
13853 				nfs4args_lockt_free(&argop[1]);
13854 			kmem_free(argop, 2 * sizeof (nfs_argop4));
13855 			if (*respp)
13856 				xdr_free(xdr_COMPOUND4res_clnt,
13857 				    (caddr_t)*respp);
13858 			*argspp = NULL;
13859 			*respp = NULL;
13860 			nfs4_end_lock_seqid_sync(lop);
13861 			lock_owner_rele(lop);
13862 			*lopp = NULL;
13863 			if (osp != NULL) {
13864 				open_stream_rele(osp, rp);
13865 				*ospp = NULL;
13866 			}
13867 			if (oop != NULL) {
13868 				nfs4_end_open_seqid_sync(oop);
13869 				open_owner_rele(oop);
13870 				*oopp = NULL;
13871 			}
13872 
13873 			nfs_rw_exit(&rp->r_lkserlock);
13874 
13875 			intr = nfs4_block_and_wait(tick_delayp, rp);
13876 
13877 			if (intr) {
13878 				(void) nfs_rw_enter_sig(&rp->r_lkserlock,
13879 				    RW_WRITER, FALSE);
13880 				*errorp = EINTR;
13881 				return (FALSE);
13882 			}
13883 
13884 			(void) nfs_rw_enter_sig(&rp->r_lkserlock,
13885 			    RW_WRITER, FALSE);
13886 
13887 			/*
13888 			 * Make sure we are still safe to lock with
13889 			 * regards to mmapping.
13890 			 */
13891 			if (!nfs4_safelock(vp, flk, cr)) {
13892 				*errorp = EAGAIN;
13893 				return (FALSE);
13894 			}
13895 
13896 			return (TRUE);
13897 		}
13898 		if (ctype == NFS4_LCK_CTYPE_NORM)
13899 			*errorp = EAGAIN;
13900 		*skip_get_err = TRUE;
13901 		flk->l_whence = 0;
13902 		*whencep = 0;
13903 		return (FALSE);
13904 	} else if (lockt_args) {
13905 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13906 		    "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13907 
13908 		denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13909 		    flk, lockt_args);
13910 
13911 		/* according to NLM code */
13912 		*errorp = 0;
13913 		*whencep = 0;
13914 		*skip_get_err = TRUE;
13915 		return (FALSE);
13916 	}
13917 	return (FALSE);
13918 }
13919 
13920 /*
13921  * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13922  */
13923 static void
nfs4frlock_results_default(COMPOUND4res_clnt * resp,int * errorp)13924 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13925 {
13926 	switch (resp->status) {
13927 	case NFS4ERR_ACCESS:
13928 	case NFS4ERR_ADMIN_REVOKED:
13929 	case NFS4ERR_BADHANDLE:
13930 	case NFS4ERR_BAD_RANGE:
13931 	case NFS4ERR_BAD_SEQID:
13932 	case NFS4ERR_BAD_STATEID:
13933 	case NFS4ERR_BADXDR:
13934 	case NFS4ERR_DEADLOCK:
13935 	case NFS4ERR_DELAY:
13936 	case NFS4ERR_EXPIRED:
13937 	case NFS4ERR_FHEXPIRED:
13938 	case NFS4ERR_GRACE:
13939 	case NFS4ERR_INVAL:
13940 	case NFS4ERR_ISDIR:
13941 	case NFS4ERR_LEASE_MOVED:
13942 	case NFS4ERR_LOCK_NOTSUPP:
13943 	case NFS4ERR_LOCK_RANGE:
13944 	case NFS4ERR_MOVED:
13945 	case NFS4ERR_NOFILEHANDLE:
13946 	case NFS4ERR_NO_GRACE:
13947 	case NFS4ERR_OLD_STATEID:
13948 	case NFS4ERR_OPENMODE:
13949 	case NFS4ERR_RECLAIM_BAD:
13950 	case NFS4ERR_RECLAIM_CONFLICT:
13951 	case NFS4ERR_RESOURCE:
13952 	case NFS4ERR_SERVERFAULT:
13953 	case NFS4ERR_STALE:
13954 	case NFS4ERR_STALE_CLIENTID:
13955 	case NFS4ERR_STALE_STATEID:
13956 		return;
13957 	default:
13958 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13959 		    "nfs4frlock_results_default: got unrecognizable "
13960 		    "res.status %d", resp->status));
13961 		*errorp = NFS4ERR_INVAL;
13962 	}
13963 }
13964 
13965 /*
13966  * The lock request was successful, so update the client's state.
13967  */
13968 static void
nfs4frlock_update_state(LOCK4args * lock_args,LOCKU4args * locku_args,LOCKT4args * lockt_args,nfs_resop4 * resop,nfs4_lock_owner_t * lop,vnode_t * vp,flock64_t * flk,cred_t * cr,nfs4_lost_rqst_t * resend_rqstp)13969 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13970     LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13971     vnode_t *vp, flock64_t *flk, cred_t *cr,
13972     nfs4_lost_rqst_t *resend_rqstp)
13973 {
13974 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13975 
13976 	if (lock_args) {
13977 		LOCK4res *lock_res;
13978 
13979 		lock_res = &resop->nfs_resop4_u.oplock;
13980 		/* update the stateid with server's response */
13981 
13982 		if (lock_args->locker.new_lock_owner == TRUE) {
13983 			mutex_enter(&lop->lo_lock);
13984 			lop->lo_just_created = NFS4_PERM_CREATED;
13985 			mutex_exit(&lop->lo_lock);
13986 		}
13987 
13988 		nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13989 
13990 		/*
13991 		 * If the lock was the result of a resending a lost
13992 		 * request, we've synched up the stateid and seqid
13993 		 * with the server, but now the server might be out of sync
13994 		 * with what the application thinks it has for locks.
13995 		 * Clean that up here.  It's unclear whether we should do
13996 		 * this even if the filesystem has been forcibly unmounted.
13997 		 * For most servers, it's probably wasted effort, but
13998 		 * RFC 7530 lets servers require that unlocks exactly match
13999 		 * the locks that are held.
14000 		 */
14001 		if (resend_rqstp != NULL &&
14002 		    resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
14003 			nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
14004 		} else {
14005 			flk->l_whence = 0;
14006 		}
14007 	} else if (locku_args) {
14008 		LOCKU4res *locku_res;
14009 
14010 		locku_res = &resop->nfs_resop4_u.oplocku;
14011 
14012 		/* Update the stateid with the server's response */
14013 		nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14014 	} else if (lockt_args) {
14015 		/* Switch the lock type to express success, see fcntl */
14016 		flk->l_type = F_UNLCK;
14017 		flk->l_whence = 0;
14018 	}
14019 }
14020 
14021 /*
14022  * Do final cleanup before exiting nfs4frlock.
14023  * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14024  * COMPOUND4 args/res for calls that haven't already.
14025  */
14026 static void
nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype,COMPOUND4args_clnt * argsp,COMPOUND4res_clnt * resp,vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,nfs4_lock_owner_t * lop,flock64_t * flk,short whence,u_offset_t offset,struct lm_sysid * ls,int * errorp,LOCK4args * lock_args,LOCKU4args * locku_args,bool_t did_start_fop,bool_t skip_get_err,cred_t * cred_otw,cred_t * cred)14027 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14028     COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14029     nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14030     nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14031     short whence, u_offset_t offset, struct lm_sysid *ls,
14032     int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14033     bool_t did_start_fop, bool_t skip_get_err,
14034     cred_t *cred_otw, cred_t *cred)
14035 {
14036 	mntinfo4_t	*mi = VTOMI4(vp);
14037 	rnode4_t	*rp = VTOR4(vp);
14038 	int		error = *errorp;
14039 	nfs_argop4	*argop;
14040 	int	do_flush_pages = 0;
14041 
14042 	ASSERT(nfs_zone() == mi->mi_zone);
14043 	/*
14044 	 * The client recovery code wants the raw status information,
14045 	 * so don't map the NFS status code to an errno value for
14046 	 * non-normal call types.
14047 	 */
14048 	if (ctype == NFS4_LCK_CTYPE_NORM) {
14049 		if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14050 			*errorp = geterrno4(resp->status);
14051 		if (did_start_fop == TRUE)
14052 			nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14053 			    needrecov);
14054 
14055 		/*
14056 		 * We've established a new lock on the server, so invalidate
14057 		 * the pages associated with the vnode to get the most up to
14058 		 * date pages from the server after acquiring the lock. We
14059 		 * want to be sure that the read operation gets the newest data.
14060 		 * N.B.
14061 		 * We used to do this in nfs4frlock_results_ok but that doesn't
14062 		 * work since VOP_PUTPAGE can call nfs4_commit which calls
14063 		 * nfs4_start_fop. We flush the pages below after calling
14064 		 * nfs4_end_fop above
14065 		 * The flush of the page cache must be done after
14066 		 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14067 		 */
14068 		if (!error && resp && resp->status == NFS4_OK)
14069 			do_flush_pages = 1;
14070 	}
14071 	if (argsp) {
14072 		ASSERT(argsp->array_len == 2);
14073 		argop = argsp->array;
14074 		if (argop[1].argop == OP_LOCK)
14075 			nfs4args_lock_free(&argop[1]);
14076 		else if (argop[1].argop == OP_LOCKT)
14077 			nfs4args_lockt_free(&argop[1]);
14078 		kmem_free(argop, 2 * sizeof (nfs_argop4));
14079 		if (resp)
14080 			xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14081 	}
14082 
14083 	/* free the reference on the lock owner */
14084 	if (lop != NULL) {
14085 		nfs4_end_lock_seqid_sync(lop);
14086 		lock_owner_rele(lop);
14087 	}
14088 
14089 	/* need to free up the reference on osp for lock args */
14090 	if (osp != NULL)
14091 		open_stream_rele(osp, rp);
14092 
14093 	/* need to free up the reference on oop for lock args */
14094 	if (oop != NULL) {
14095 		nfs4_end_open_seqid_sync(oop);
14096 		open_owner_rele(oop);
14097 	}
14098 
14099 	if (do_flush_pages)
14100 		nfs4_flush_pages(vp, cred);
14101 
14102 	(void) convoff(vp, flk, whence, offset);
14103 
14104 	lm_rel_sysid(ls);
14105 
14106 	/*
14107 	 * Record debug information in the event we get EINVAL.
14108 	 */
14109 	mutex_enter(&mi->mi_lock);
14110 	if (*errorp == EINVAL && (lock_args || locku_args) &&
14111 	    (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14112 		if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14113 			zcmn_err(getzoneid(), CE_NOTE,
14114 			    "%s operation failed with "
14115 			    "EINVAL probably since the server, %s,"
14116 			    " doesn't support POSIX style locking",
14117 			    lock_args ? "LOCK" : "LOCKU",
14118 			    mi->mi_curr_serv->sv_hostname);
14119 			mi->mi_flags |= MI4_LOCK_DEBUG;
14120 		}
14121 	}
14122 	mutex_exit(&mi->mi_lock);
14123 
14124 	if (cred_otw)
14125 		crfree(cred_otw);
14126 }
14127 
14128 /*
14129  * This calls the server and the local locking code.
14130  *
14131  * Client locks are registerred locally by oring the sysid with
14132  * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14133  * We need to distinguish between the two to avoid collision in case one
14134  * machine is used as both client and server.
14135  *
14136  * Blocking lock requests will continually retry to acquire the lock
14137  * forever.
14138  *
14139  * The ctype is defined as follows:
14140  * NFS4_LCK_CTYPE_NORM: normal lock request.
14141  *
14142  * NFS4_LCK_CTYPE_RECLAIM:  bypass the usual calls for synchronizing with client
14143  * recovery, get the pid from flk instead of curproc, and don't reregister
14144  * the lock locally.
14145  *
14146  * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14147  * that we will use the information passed in via resend_rqstp to setup the
14148  * lock/locku request.  This resend is the exact same request as the 'lost
14149  * lock', and is initiated by the recovery framework. A successful resend
14150  * request can initiate one or more reinstate requests.
14151  *
14152  * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14153  * does not trigger additional reinstate requests.  This lock call type is
14154  * set for setting the v4 server's locking state back to match what the
14155  * client's local locking state is in the event of a received 'lost lock'.
14156  *
14157  * Errors are returned via the nfs4_error_t parameter.
14158  */
14159 void
nfs4frlock(nfs4_lock_call_type_t ctype,vnode_t * vp,int cmd,flock64_t * flk,int flag,u_offset_t offset,cred_t * cr,nfs4_error_t * ep,nfs4_lost_rqst_t * resend_rqstp,int * did_reclaimp)14160 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14161     int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14162     nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14163 {
14164 	COMPOUND4args_clnt	args, *argsp = NULL;
14165 	COMPOUND4res_clnt	res, *resp = NULL;
14166 	nfs_argop4	*argop;
14167 	nfs_resop4	*resop;
14168 	rnode4_t	*rp;
14169 	int		doqueue = 1;
14170 	clock_t		tick_delay;  /* delay in clock ticks */
14171 	struct lm_sysid	*ls;
14172 	LOCK4args	*lock_args = NULL;
14173 	LOCKU4args	*locku_args = NULL;
14174 	LOCKT4args	*lockt_args = NULL;
14175 	nfs4_open_owner_t *oop = NULL;
14176 	nfs4_open_stream_t *osp = NULL;
14177 	nfs4_lock_owner_t *lop = NULL;
14178 	bool_t		needrecov = FALSE;
14179 	nfs4_recov_state_t recov_state;
14180 	short		whence;
14181 	nfs4_op_hint_t	op_hint;
14182 	nfs4_lost_rqst_t lost_rqst;
14183 	bool_t		retry = FALSE;
14184 	bool_t		did_start_fop = FALSE;
14185 	bool_t		skip_get_err = FALSE;
14186 	cred_t		*cred_otw = NULL;
14187 	bool_t		recovonly;	/* just queue request */
14188 	int		frc_no_reclaim = 0;
14189 #ifdef DEBUG
14190 	char *name;
14191 #endif
14192 
14193 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14194 
14195 #ifdef DEBUG
14196 	name = fn_name(VTOSV(vp)->sv_name);
14197 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14198 	    "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14199 	    "length %"PRIu64", pid %d, sysid %d, call type %s, "
14200 	    "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14201 	    flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14202 	    flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14203 	    resend_rqstp ? "TRUE" : "FALSE"));
14204 	kmem_free(name, MAXNAMELEN);
14205 #endif
14206 
14207 	nfs4_error_zinit(ep);
14208 	ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14209 	if (ep->error)
14210 		return;
14211 	ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14212 	if (ep->error)
14213 		return;
14214 	nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14215 	    vp, cr, &cred_otw);
14216 
14217 recov_retry:
14218 	nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14219 	    &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14220 	rp = VTOR4(vp);
14221 
14222 	ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14223 	    &did_start_fop, &recovonly);
14224 
14225 	if (ep->error)
14226 		goto out;
14227 
14228 	if (recovonly) {
14229 		/*
14230 		 * Leave the request for the recovery system to deal with.
14231 		 */
14232 		ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14233 		ASSERT(cmd != F_GETLK);
14234 		ASSERT(flk->l_type == F_UNLCK);
14235 
14236 		nfs4_error_init(ep, EINTR);
14237 		needrecov = TRUE;
14238 		lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14239 		if (lop != NULL) {
14240 			nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14241 			    NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14242 			(void) nfs4_start_recovery(ep,
14243 			    VTOMI4(vp), vp, NULL, NULL,
14244 			    (lost_rqst.lr_op == OP_LOCK ||
14245 			    lost_rqst.lr_op == OP_LOCKU) ?
14246 			    &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14247 			lock_owner_rele(lop);
14248 			lop = NULL;
14249 		}
14250 		flk->l_pid = curproc->p_pid;
14251 		nfs4_register_lock_locally(vp, flk, flag, offset);
14252 		goto out;
14253 	}
14254 
14255 	/* putfh directory fh */
14256 	argop[0].argop = OP_CPUTFH;
14257 	argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14258 
14259 	/*
14260 	 * Set up the over-the-wire arguments and get references to the
14261 	 * open owner, etc.
14262 	 */
14263 
14264 	if (ctype == NFS4_LCK_CTYPE_RESEND ||
14265 	    ctype == NFS4_LCK_CTYPE_REINSTATE) {
14266 		nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14267 		    &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14268 	} else {
14269 		bool_t go_otw = TRUE;
14270 
14271 		ASSERT(resend_rqstp == NULL);
14272 
14273 		switch (cmd) {
14274 		case F_GETLK:
14275 			nfs4frlock_setup_lockt_args(ctype, &argop[1],
14276 			    &lockt_args, argsp, flk, rp);
14277 			break;
14278 		case F_SETLKW:
14279 		case F_SETLK:
14280 			if (flk->l_type == F_UNLCK)
14281 				nfs4frlock_setup_locku_args(ctype,
14282 				    &argop[1], &locku_args, flk,
14283 				    &lop, ep, argsp,
14284 				    vp, flag, offset, cr,
14285 				    &skip_get_err, &go_otw);
14286 			else
14287 				nfs4frlock_setup_lock_args(ctype,
14288 				    &lock_args, &oop, &osp, &lop, &argop[1],
14289 				    argsp, flk, cmd, vp, cr, ep);
14290 
14291 			if (ep->error)
14292 				goto out;
14293 
14294 			switch (ep->stat) {
14295 			case NFS4_OK:
14296 				break;
14297 			case NFS4ERR_DELAY:
14298 				/* recov thread never gets this error */
14299 				ASSERT(resend_rqstp == NULL);
14300 				ASSERT(did_start_fop);
14301 
14302 				nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14303 				    &recov_state, TRUE);
14304 				did_start_fop = FALSE;
14305 				if (argop[1].argop == OP_LOCK)
14306 					nfs4args_lock_free(&argop[1]);
14307 				else if (argop[1].argop == OP_LOCKT)
14308 					nfs4args_lockt_free(&argop[1]);
14309 				kmem_free(argop, 2 * sizeof (nfs_argop4));
14310 				argsp = NULL;
14311 				goto recov_retry;
14312 			default:
14313 				ep->error = EIO;
14314 				goto out;
14315 			}
14316 			break;
14317 		default:
14318 			NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14319 			    "nfs4_frlock: invalid cmd %d", cmd));
14320 			ep->error = EINVAL;
14321 			goto out;
14322 		}
14323 
14324 		if (!go_otw)
14325 			goto out;
14326 	}
14327 
14328 	/* XXX should we use the local reclock as a cache ? */
14329 	/*
14330 	 * Unregister the lock with the local locking code before
14331 	 * contacting the server.  This avoids a potential race where
14332 	 * another process gets notified that it has been granted a lock
14333 	 * before we can unregister ourselves locally.
14334 	 */
14335 	if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14336 		if (ctype == NFS4_LCK_CTYPE_NORM)
14337 			flk->l_pid = ttoproc(curthread)->p_pid;
14338 		nfs4_register_lock_locally(vp, flk, flag, offset);
14339 	}
14340 
14341 	/*
14342 	 * Send the server the lock request.  Continually loop with a delay
14343 	 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14344 	 */
14345 	resp = &res;
14346 
14347 	NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14348 	    (CE_NOTE,
14349 	    "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14350 	    rnode4info(rp)));
14351 
14352 	if (lock_args && frc_no_reclaim) {
14353 		ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14354 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14355 		    "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14356 		lock_args->reclaim = FALSE;
14357 		if (did_reclaimp)
14358 			*did_reclaimp = 0;
14359 	}
14360 
14361 	/*
14362 	 * Do the OTW call.
14363 	 */
14364 	rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14365 
14366 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14367 	    "nfs4frlock: error %d, status %d", ep->error, resp->status));
14368 
14369 	needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14370 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14371 	    "nfs4frlock: needrecov %d", needrecov));
14372 
14373 	if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14374 		nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14375 		    args.ctag);
14376 
14377 	/*
14378 	 * Check if one of these mutually exclusive error cases has
14379 	 * happened:
14380 	 *   need to swap credentials due to access error
14381 	 *   recovery is needed
14382 	 *   different error (only known case is missing Kerberos ticket)
14383 	 */
14384 
14385 	if ((ep->error == EACCES ||
14386 	    (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14387 	    cred_otw != cr) {
14388 		nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14389 		    &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14390 		    cr, &cred_otw);
14391 		goto recov_retry;
14392 	}
14393 
14394 	if (needrecov) {
14395 		/*
14396 		 * LOCKT requests don't need to recover from lost
14397 		 * requests since they don't create/modify state.
14398 		 */
14399 		if ((ep->error == EINTR ||
14400 		    NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14401 		    lockt_args)
14402 			goto out;
14403 		/*
14404 		 * Do not attempt recovery for requests initiated by
14405 		 * the recovery framework.  Let the framework redrive them.
14406 		 */
14407 		if (ctype != NFS4_LCK_CTYPE_NORM)
14408 			goto out;
14409 		else {
14410 			ASSERT(resend_rqstp == NULL);
14411 		}
14412 
14413 		nfs4frlock_save_lost_rqst(ctype, ep->error,
14414 		    flk_to_locktype(cmd, flk->l_type),
14415 		    oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14416 
14417 		retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14418 		    &resp, lock_args, locku_args, &oop, &osp, &lop,
14419 		    rp, vp, &recov_state, op_hint, &did_start_fop,
14420 		    cmd != F_GETLK ? &lost_rqst : NULL, flk);
14421 
14422 		if (retry) {
14423 			ASSERT(oop == NULL);
14424 			ASSERT(osp == NULL);
14425 			ASSERT(lop == NULL);
14426 			goto recov_retry;
14427 		}
14428 		goto out;
14429 	}
14430 
14431 	/*
14432 	 * Bail out if have reached this point with ep->error set. Can
14433 	 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14434 	 * This happens if Kerberos ticket has expired or has been
14435 	 * destroyed.
14436 	 */
14437 	if (ep->error != 0)
14438 		goto out;
14439 
14440 	/*
14441 	 * Process the reply.
14442 	 */
14443 	switch (resp->status) {
14444 	case NFS4_OK:
14445 		resop = &resp->array[1];
14446 		nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14447 		    resend_rqstp);
14448 		/*
14449 		 * Have a successful lock operation, now update state.
14450 		 */
14451 		nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14452 		    resop, lop, vp, flk, cr, resend_rqstp);
14453 		break;
14454 
14455 	case NFS4ERR_DENIED:
14456 		resop = &resp->array[1];
14457 		retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14458 		    &oop, &osp, &lop, cmd, vp, flk, op_hint,
14459 		    &recov_state, needrecov, &argsp, &resp,
14460 		    &tick_delay, &whence, &ep->error, resop, cr,
14461 		    &did_start_fop, &skip_get_err);
14462 
14463 		if (retry) {
14464 			ASSERT(oop == NULL);
14465 			ASSERT(osp == NULL);
14466 			ASSERT(lop == NULL);
14467 			goto recov_retry;
14468 		}
14469 		break;
14470 	/*
14471 	 * If the server won't let us reclaim, fall-back to trying to lock
14472 	 * the file from scratch. Code elsewhere will check the changeinfo
14473 	 * to ensure the file hasn't been changed.
14474 	 */
14475 	case NFS4ERR_NO_GRACE:
14476 		if (lock_args && lock_args->reclaim == TRUE) {
14477 			ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14478 			NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14479 			    "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14480 			frc_no_reclaim = 1;
14481 			/* clean up before retrying */
14482 			needrecov = 0;
14483 			(void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14484 			    lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14485 			    &recov_state, op_hint, &did_start_fop, NULL, flk);
14486 			goto recov_retry;
14487 		}
14488 		/* FALLTHROUGH */
14489 
14490 	default:
14491 		nfs4frlock_results_default(resp, &ep->error);
14492 		break;
14493 	}
14494 out:
14495 	/*
14496 	 * Process and cleanup from error.  Make interrupted unlock
14497 	 * requests look successful, since they will be handled by the
14498 	 * client recovery code.
14499 	 */
14500 	nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14501 	    needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14502 	    lock_args, locku_args, did_start_fop,
14503 	    skip_get_err, cred_otw, cr);
14504 
14505 	if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14506 	    (cmd == F_SETLK || cmd == F_SETLKW))
14507 		ep->error = 0;
14508 }
14509 
14510 /*
14511  * nfs4_safelock:
14512  *
14513  * Return non-zero if the given lock request can be handled without
14514  * violating the constraints on concurrent mapping and locking.
14515  */
14516 
14517 static int
nfs4_safelock(vnode_t * vp,const struct flock64 * bfp,cred_t * cr)14518 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14519 {
14520 	rnode4_t *rp = VTOR4(vp);
14521 	struct vattr va;
14522 	int error;
14523 
14524 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14525 	ASSERT(rp->r_mapcnt >= 0);
14526 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14527 	    "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14528 	    "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14529 	    bfp->l_start, bfp->l_len, rp->r_mapcnt));
14530 
14531 	if (rp->r_mapcnt == 0)
14532 		return (1);		/* always safe if not mapped */
14533 
14534 	/*
14535 	 * If the file is already mapped and there are locks, then they
14536 	 * should be all safe locks.  So adding or removing a lock is safe
14537 	 * as long as the new request is safe (i.e., whole-file, meaning
14538 	 * length and starting offset are both zero).
14539 	 */
14540 
14541 	if (bfp->l_start != 0 || bfp->l_len != 0) {
14542 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14543 		    "cannot lock a memory mapped file unless locking the "
14544 		    "entire file: start %"PRIx64", len %"PRIx64,
14545 		    bfp->l_start, bfp->l_len));
14546 		return (0);
14547 	}
14548 
14549 	/* mandatory locking and mapping don't mix */
14550 	va.va_mask = AT_MODE;
14551 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14552 	if (error != 0) {
14553 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14554 		    "getattr error %d", error));
14555 		return (0);		/* treat errors conservatively */
14556 	}
14557 	if (MANDLOCK(vp, va.va_mode)) {
14558 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14559 		    "cannot mandatory lock and mmap a file"));
14560 		return (0);
14561 	}
14562 
14563 	return (1);
14564 }
14565 
14566 
14567 /*
14568  * Register the lock locally within Solaris.
14569  * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14570  * recording locks locally.
14571  *
14572  * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14573  * are registered locally.
14574  */
14575 void
nfs4_register_lock_locally(vnode_t * vp,struct flock64 * flk,int flag,u_offset_t offset)14576 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14577     u_offset_t offset)
14578 {
14579 	int oldsysid;
14580 	int error;
14581 #ifdef DEBUG
14582 	char *name;
14583 #endif
14584 
14585 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14586 
14587 #ifdef DEBUG
14588 	name = fn_name(VTOSV(vp)->sv_name);
14589 	NFS4_DEBUG(nfs4_client_lock_debug,
14590 	    (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14591 	    "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14592 	    name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14593 	    flk->l_sysid));
14594 	kmem_free(name, MAXNAMELEN);
14595 #endif
14596 
14597 	/* register the lock with local locking */
14598 	oldsysid = flk->l_sysid;
14599 	flk->l_sysid |= LM_SYSID_CLIENT;
14600 	error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14601 #ifdef DEBUG
14602 	if (error != 0) {
14603 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14604 		    "nfs4_register_lock_locally: could not register with"
14605 		    " local locking"));
14606 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14607 		    "error %d, vp 0x%p, pid %d, sysid 0x%x",
14608 		    error, (void *)vp, flk->l_pid, flk->l_sysid));
14609 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14610 		    "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14611 		    flk->l_type, flk->l_start, flk->l_len));
14612 		(void) reclock(vp, flk, 0, flag, offset, NULL);
14613 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14614 		    "blocked by pid %d sysid 0x%x type %d "
14615 		    "off 0x%" PRIx64 " len 0x%" PRIx64,
14616 		    flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14617 		    flk->l_len));
14618 	}
14619 #endif
14620 	flk->l_sysid = oldsysid;
14621 }
14622 
14623 /*
14624  * nfs4_lockrelease:
14625  *
14626  * Release any locks on the given vnode that are held by the current
14627  * process.  Also removes the lock owner (if one exists) from the rnode's
14628  * list.
14629  */
14630 static int
nfs4_lockrelease(vnode_t * vp,int flag,offset_t offset,cred_t * cr)14631 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14632 {
14633 	flock64_t ld;
14634 	int ret, error;
14635 	rnode4_t *rp;
14636 	nfs4_lock_owner_t *lop;
14637 	nfs4_recov_state_t recov_state;
14638 	mntinfo4_t *mi;
14639 	bool_t possible_orphan = FALSE;
14640 	bool_t recovonly;
14641 
14642 	ASSERT((uintptr_t)vp > KERNELBASE);
14643 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14644 
14645 	rp = VTOR4(vp);
14646 	mi = VTOMI4(vp);
14647 
14648 	/*
14649 	 * If we have not locked anything then we can
14650 	 * just return since we have no work to do.
14651 	 */
14652 	if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14653 		return (0);
14654 	}
14655 
14656 	/*
14657 	 * We need to comprehend that another thread may
14658 	 * kick off recovery and the lock_owner we have stashed
14659 	 * in lop might be invalid so we should NOT cache it
14660 	 * locally!
14661 	 */
14662 	recov_state.rs_flags = 0;
14663 	recov_state.rs_num_retry_despite_err = 0;
14664 	error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14665 	    &recovonly);
14666 	if (error) {
14667 		mutex_enter(&rp->r_statelock);
14668 		rp->r_flags |= R4LODANGLERS;
14669 		mutex_exit(&rp->r_statelock);
14670 		return (error);
14671 	}
14672 
14673 	lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14674 
14675 	/*
14676 	 * Check if the lock owner might have a lock (request was sent but
14677 	 * no response was received).  Also check if there are any remote
14678 	 * locks on the file.  (In theory we shouldn't have to make this
14679 	 * second check if there's no lock owner, but for now we'll be
14680 	 * conservative and do it anyway.)  If either condition is true,
14681 	 * send an unlock for the entire file to the server.
14682 	 *
14683 	 * Note that no explicit synchronization is needed here.  At worst,
14684 	 * flk_has_remote_locks() will return a false positive, in which case
14685 	 * the unlock call wastes time but doesn't harm correctness.
14686 	 */
14687 
14688 	if (lop) {
14689 		mutex_enter(&lop->lo_lock);
14690 		possible_orphan = lop->lo_pending_rqsts;
14691 		mutex_exit(&lop->lo_lock);
14692 		lock_owner_rele(lop);
14693 	}
14694 
14695 	nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14696 
14697 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14698 	    "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14699 	    "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14700 	    (void *)lop));
14701 
14702 	if (possible_orphan || flk_has_remote_locks(vp)) {
14703 		ld.l_type = F_UNLCK;    /* set to unlock entire file */
14704 		ld.l_whence = 0;	/* unlock from start of file */
14705 		ld.l_start = 0;
14706 		ld.l_len = 0;		/* do entire file */
14707 
14708 		ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14709 		    cr, NULL);
14710 
14711 		if (ret != 0) {
14712 			/*
14713 			 * If VOP_FRLOCK fails, make sure we unregister
14714 			 * local locks before we continue.
14715 			 */
14716 			ld.l_pid = ttoproc(curthread)->p_pid;
14717 			nfs4_register_lock_locally(vp, &ld, flag, offset);
14718 			NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14719 			    "nfs4_lockrelease: lock release error on vp"
14720 			    " %p: error %d.\n", (void *)vp, ret));
14721 		}
14722 	}
14723 
14724 	recov_state.rs_flags = 0;
14725 	recov_state.rs_num_retry_despite_err = 0;
14726 	error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14727 	    &recovonly);
14728 	if (error) {
14729 		mutex_enter(&rp->r_statelock);
14730 		rp->r_flags |= R4LODANGLERS;
14731 		mutex_exit(&rp->r_statelock);
14732 		return (error);
14733 	}
14734 
14735 	/*
14736 	 * So, here we're going to need to retrieve the lock-owner
14737 	 * again (in case recovery has done a switch-a-roo) and
14738 	 * remove it because we can.
14739 	 */
14740 	lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14741 
14742 	if (lop) {
14743 		nfs4_rnode_remove_lock_owner(rp, lop);
14744 		lock_owner_rele(lop);
14745 	}
14746 
14747 	nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14748 	return (0);
14749 }
14750 
14751 /*
14752  * Wait for 'tick_delay' clock ticks.
14753  * Implement exponential backoff until hit the lease_time of this nfs4_server.
14754  * NOTE: lock_lease_time is in seconds.
14755  *
14756  * XXX For future improvements, should implement a waiting queue scheme.
14757  */
14758 static int
nfs4_block_and_wait(clock_t * tick_delay,rnode4_t * rp)14759 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14760 {
14761 	long milliseconds_delay;
14762 	time_t lock_lease_time;
14763 
14764 	/* wait tick_delay clock ticks or siginteruptus */
14765 	if (delay_sig(*tick_delay)) {
14766 		return (EINTR);
14767 	}
14768 	NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14769 	    "reissue the lock request: blocked for %ld clock ticks: %ld "
14770 	    "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14771 
14772 	/* get the lease time */
14773 	lock_lease_time = r2lease_time(rp);
14774 
14775 	/* drv_hztousec converts ticks to microseconds */
14776 	milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14777 	if (milliseconds_delay < lock_lease_time * 1000) {
14778 		*tick_delay = 2 * *tick_delay;
14779 		if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14780 			*tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14781 	}
14782 	return (0);
14783 }
14784 
14785 
14786 void
nfs4_vnops_init(void)14787 nfs4_vnops_init(void)
14788 {
14789 }
14790 
14791 void
nfs4_vnops_fini(void)14792 nfs4_vnops_fini(void)
14793 {
14794 }
14795 
14796 /*
14797  * Return a reference to the directory (parent) vnode for a given vnode,
14798  * using the saved pathname information and the directory file handle.  The
14799  * caller is responsible for disposing of the reference.
14800  * Returns zero or an errno value.
14801  *
14802  * Caller should set need_start_op to FALSE if it is the recovery
14803  * thread, or if a start_fop has already been done.  Otherwise, TRUE.
14804  */
14805 int
vtodv(vnode_t * vp,vnode_t ** dvpp,cred_t * cr,bool_t need_start_op)14806 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14807 {
14808 	svnode_t *svnp;
14809 	vnode_t *dvp = NULL;
14810 	servinfo4_t *svp;
14811 	nfs4_fname_t *mfname;
14812 	int error;
14813 
14814 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14815 
14816 	if (vp->v_flag & VROOT) {
14817 		nfs4_sharedfh_t *sfh;
14818 		nfs_fh4 fh;
14819 		mntinfo4_t *mi;
14820 
14821 		ASSERT(vp->v_type == VREG);
14822 
14823 		mi = VTOMI4(vp);
14824 		svp = mi->mi_curr_serv;
14825 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14826 		fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14827 		fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14828 		sfh = sfh4_get(&fh, VTOMI4(vp));
14829 		nfs_rw_exit(&svp->sv_lock);
14830 		mfname = mi->mi_fname;
14831 		fn_hold(mfname);
14832 		dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14833 		sfh4_rele(&sfh);
14834 
14835 		if (dvp->v_type == VNON)
14836 			dvp->v_type = VDIR;
14837 		*dvpp = dvp;
14838 		return (0);
14839 	}
14840 
14841 	svnp = VTOSV(vp);
14842 
14843 	if (svnp == NULL) {
14844 		NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14845 		    "shadow node is NULL"));
14846 		return (EINVAL);
14847 	}
14848 
14849 	if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14850 		NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14851 		    "shadow node name or dfh val == NULL"));
14852 		return (EINVAL);
14853 	}
14854 
14855 	error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14856 	    (int)need_start_op);
14857 	if (error != 0) {
14858 		NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14859 		    "nfs4_make_dotdot returned %d", error));
14860 		return (error);
14861 	}
14862 	if (!dvp) {
14863 		NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14864 		    "nfs4_make_dotdot returned a NULL dvp"));
14865 		return (EIO);
14866 	}
14867 	if (dvp->v_type == VNON)
14868 		dvp->v_type = VDIR;
14869 	ASSERT(dvp->v_type == VDIR);
14870 	if (VTOR4(vp)->r_flags & R4ISXATTR) {
14871 		mutex_enter(&dvp->v_lock);
14872 		dvp->v_flag |= V_XATTRDIR;
14873 		mutex_exit(&dvp->v_lock);
14874 	}
14875 	*dvpp = dvp;
14876 	return (0);
14877 }
14878 
14879 /*
14880  * Copy the (final) component name of vp to fnamep.  maxlen is the maximum
14881  * length that fnamep can accept, including the trailing null.
14882  * Returns 0 if okay, returns an errno value if there was a problem.
14883  */
14884 
14885 int
vtoname(vnode_t * vp,char * fnamep,ssize_t maxlen)14886 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14887 {
14888 	char *fn;
14889 	int err = 0;
14890 	servinfo4_t *svp;
14891 	svnode_t *shvp;
14892 
14893 	/*
14894 	 * If the file being opened has VROOT set, then this is
14895 	 * a "file" mount.  sv_name will not be interesting, so
14896 	 * go back to the servinfo4 to get the original mount
14897 	 * path and strip off all but the final edge.  Otherwise
14898 	 * just return the name from the shadow vnode.
14899 	 */
14900 
14901 	if (vp->v_flag & VROOT) {
14902 
14903 		svp = VTOMI4(vp)->mi_curr_serv;
14904 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14905 
14906 		fn = strrchr(svp->sv_path, '/');
14907 		if (fn == NULL)
14908 			err = EINVAL;
14909 		else
14910 			fn++;
14911 	} else {
14912 		shvp = VTOSV(vp);
14913 		fn = fn_name(shvp->sv_name);
14914 	}
14915 
14916 	if (err == 0)
14917 		if (strlen(fn) < maxlen)
14918 			(void) strcpy(fnamep, fn);
14919 		else
14920 			err = ENAMETOOLONG;
14921 
14922 	if (vp->v_flag & VROOT)
14923 		nfs_rw_exit(&svp->sv_lock);
14924 	else
14925 		kmem_free(fn, MAXNAMELEN);
14926 
14927 	return (err);
14928 }
14929 
14930 /*
14931  * Bookkeeping for a close that doesn't need to go over the wire.
14932  * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14933  * it is left at 1.
14934  */
14935 void
nfs4close_notw(vnode_t * vp,nfs4_open_stream_t * osp,int * have_lockp)14936 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14937 {
14938 	rnode4_t		*rp;
14939 	mntinfo4_t		*mi;
14940 
14941 	mi = VTOMI4(vp);
14942 	rp = VTOR4(vp);
14943 
14944 	NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14945 	    "rp=%p osp=%p", (void *)rp, (void *)osp));
14946 	ASSERT(nfs_zone() == mi->mi_zone);
14947 	ASSERT(mutex_owned(&osp->os_sync_lock));
14948 	ASSERT(*have_lockp);
14949 
14950 	if (!osp->os_valid ||
14951 	    osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14952 		return;
14953 	}
14954 
14955 	/*
14956 	 * This removes the reference obtained at OPEN; ie,
14957 	 * when the open stream structure was created.
14958 	 *
14959 	 * We don't have to worry about calling 'open_stream_rele'
14960 	 * since we our currently holding a reference to this
14961 	 * open stream which means the count can not go to 0 with
14962 	 * this decrement.
14963 	 */
14964 	ASSERT(osp->os_ref_count >= 2);
14965 	osp->os_ref_count--;
14966 	osp->os_valid = 0;
14967 	mutex_exit(&osp->os_sync_lock);
14968 	*have_lockp = 0;
14969 
14970 	nfs4_dec_state_ref_count(mi);
14971 }
14972 
14973 /*
14974  * Close all remaining open streams on the rnode.  These open streams
14975  * could be here because:
14976  * - The close attempted at either close or delmap failed
14977  * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14978  * - Someone did mknod on a regular file but never opened it
14979  */
14980 int
nfs4close_all(vnode_t * vp,cred_t * cr)14981 nfs4close_all(vnode_t *vp, cred_t *cr)
14982 {
14983 	nfs4_open_stream_t *osp;
14984 	int error;
14985 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14986 	rnode4_t *rp;
14987 
14988 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14989 
14990 	error = 0;
14991 	rp = VTOR4(vp);
14992 
14993 	/*
14994 	 * At this point, all we know is that the last time
14995 	 * someone called vn_rele, the count was 1.  Since then,
14996 	 * the vnode could have been re-activated.  We want to
14997 	 * loop through the open streams and close each one, but
14998 	 * we have to be careful since once we release the rnode
14999 	 * hash bucket lock, someone else is free to come in and
15000 	 * re-activate the rnode and add new open streams.  The
15001 	 * strategy is take the rnode hash bucket lock, verify that
15002 	 * the count is still 1, grab the open stream off the
15003 	 * head of the list and mark it invalid, then release the
15004 	 * rnode hash bucket lock and proceed with that open stream.
15005 	 * This is ok because nfs4close_one() will acquire the proper
15006 	 * open/create to close/destroy synchronization for open
15007 	 * streams, and will ensure that if someone has reopened
15008 	 * the open stream after we've dropped the hash bucket lock
15009 	 * then we'll just simply return without destroying the
15010 	 * open stream.
15011 	 * Repeat until the list is empty.
15012 	 */
15013 
15014 	for (;;) {
15015 
15016 		/* make sure vnode hasn't been reactivated */
15017 		rw_enter(&rp->r_hashq->r_lock, RW_READER);
15018 		mutex_enter(&vp->v_lock);
15019 		if (vp->v_count > 1) {
15020 			mutex_exit(&vp->v_lock);
15021 			rw_exit(&rp->r_hashq->r_lock);
15022 			break;
15023 		}
15024 		/*
15025 		 * Grabbing r_os_lock before releasing v_lock prevents
15026 		 * a window where the rnode/open stream could get
15027 		 * reactivated (and os_force_close set to 0) before we
15028 		 * had a chance to set os_force_close to 1.
15029 		 */
15030 		mutex_enter(&rp->r_os_lock);
15031 		mutex_exit(&vp->v_lock);
15032 
15033 		osp = list_head(&rp->r_open_streams);
15034 		if (!osp) {
15035 			/* nothing left to CLOSE OTW, so return */
15036 			mutex_exit(&rp->r_os_lock);
15037 			rw_exit(&rp->r_hashq->r_lock);
15038 			break;
15039 		}
15040 
15041 		mutex_enter(&rp->r_statev4_lock);
15042 		/* the file can't still be mem mapped */
15043 		ASSERT(rp->r_mapcnt == 0);
15044 		if (rp->created_v4)
15045 			rp->created_v4 = 0;
15046 		mutex_exit(&rp->r_statev4_lock);
15047 
15048 		/*
15049 		 * Grab a ref on this open stream; nfs4close_one
15050 		 * will mark it as invalid
15051 		 */
15052 		mutex_enter(&osp->os_sync_lock);
15053 		osp->os_ref_count++;
15054 		osp->os_force_close = 1;
15055 		mutex_exit(&osp->os_sync_lock);
15056 		mutex_exit(&rp->r_os_lock);
15057 		rw_exit(&rp->r_hashq->r_lock);
15058 
15059 		nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15060 
15061 		/* Update error if it isn't already non-zero */
15062 		if (error == 0) {
15063 			if (e.error)
15064 				error = e.error;
15065 			else if (e.stat)
15066 				error = geterrno4(e.stat);
15067 		}
15068 
15069 #ifdef	DEBUG
15070 		nfs4close_all_cnt++;
15071 #endif
15072 		/* Release the ref on osp acquired above. */
15073 		open_stream_rele(osp, rp);
15074 
15075 		/* Proceed to the next open stream, if any */
15076 	}
15077 	return (error);
15078 }
15079 
15080 /*
15081  * nfs4close_one - close one open stream for a file if needed.
15082  *
15083  * "close_type" indicates which close path this is:
15084  * CLOSE_NORM: close initiated via VOP_CLOSE.
15085  * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15086  * CLOSE_FORCE: close initiated via VOP_INACTIVE.  This path forces
15087  *	the close and release of client state for this open stream
15088  *	(unless someone else has the open stream open).
15089  * CLOSE_RESEND: indicates the request is a replay of an earlier request
15090  *	(e.g., due to abort because of a signal).
15091  * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15092  *
15093  * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15094  * recovery.  Instead, the caller is expected to deal with retries.
15095  *
15096  * The caller can either pass in the osp ('provided_osp') or not.
15097  *
15098  * 'access_bits' represents the access we are closing/downgrading.
15099  *
15100  * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP.  'len' is the
15101  * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15102  * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15103  *
15104  * Errors are returned via the nfs4_error_t.
15105  */
15106 void
nfs4close_one(vnode_t * vp,nfs4_open_stream_t * provided_osp,cred_t * cr,int access_bits,nfs4_lost_rqst_t * lrp,nfs4_error_t * ep,nfs4_close_type_t close_type,size_t len,uint_t maxprot,uint_t mmap_flags)15107 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15108     int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15109     nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15110     uint_t mmap_flags)
15111 {
15112 	nfs4_open_owner_t *oop;
15113 	nfs4_open_stream_t *osp = NULL;
15114 	int retry = 0;
15115 	int num_retries = NFS4_NUM_RECOV_RETRIES;
15116 	rnode4_t *rp;
15117 	mntinfo4_t *mi;
15118 	nfs4_recov_state_t recov_state;
15119 	cred_t *cred_otw = NULL;
15120 	bool_t recovonly = FALSE;
15121 	int isrecov;
15122 	int force_close;
15123 	int close_failed = 0;
15124 	int did_dec_count = 0;
15125 	int did_start_op = 0;
15126 	int did_force_recovlock = 0;
15127 	int did_start_seqid_sync = 0;
15128 	int have_sync_lock = 0;
15129 
15130 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15131 
15132 	NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15133 	    "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15134 	    (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15135 	    len, maxprot, mmap_flags, access_bits));
15136 
15137 	nfs4_error_zinit(ep);
15138 	rp = VTOR4(vp);
15139 	mi = VTOMI4(vp);
15140 	isrecov = (close_type == CLOSE_RESEND ||
15141 	    close_type == CLOSE_AFTER_RESEND);
15142 
15143 	/*
15144 	 * First get the open owner.
15145 	 */
15146 	if (!provided_osp) {
15147 		oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15148 	} else {
15149 		oop = provided_osp->os_open_owner;
15150 		ASSERT(oop != NULL);
15151 		open_owner_hold(oop);
15152 	}
15153 
15154 	if (!oop) {
15155 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15156 		    "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15157 		    "close type %d", (void *)rp, (void *)mi, (void *)cr,
15158 		    (void *)provided_osp, close_type));
15159 		ep->error = EIO;
15160 		goto out;
15161 	}
15162 
15163 	cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15164 recov_retry:
15165 	osp = NULL;
15166 	close_failed = 0;
15167 	force_close = (close_type == CLOSE_FORCE);
15168 	retry = 0;
15169 	did_start_op = 0;
15170 	did_force_recovlock = 0;
15171 	did_start_seqid_sync = 0;
15172 	have_sync_lock = 0;
15173 	recovonly = FALSE;
15174 	recov_state.rs_flags = 0;
15175 	recov_state.rs_num_retry_despite_err = 0;
15176 
15177 	/*
15178 	 * Second synchronize with recovery.
15179 	 */
15180 	if (!isrecov) {
15181 		ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15182 		    &recov_state, &recovonly);
15183 		if (!ep->error) {
15184 			did_start_op = 1;
15185 		} else {
15186 			close_failed = 1;
15187 			/*
15188 			 * If we couldn't get start_fop, but have to
15189 			 * cleanup state, then at least acquire the
15190 			 * mi_recovlock so we can synchronize with
15191 			 * recovery.
15192 			 */
15193 			if (close_type == CLOSE_FORCE) {
15194 				(void) nfs_rw_enter_sig(&mi->mi_recovlock,
15195 				    RW_READER, FALSE);
15196 				did_force_recovlock = 1;
15197 			} else
15198 				goto out;
15199 		}
15200 	}
15201 
15202 	/*
15203 	 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15204 	 * set 'recovonly' to TRUE since most likely this is due to
15205 	 * reovery being active (MI4_RECOV_ACTIV).  If recovery is active,
15206 	 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15207 	 * to retry, causing us to loop until recovery finishes.  Plus we
15208 	 * don't need protection over the open seqid since we're not going
15209 	 * OTW, hence don't need to use the seqid.
15210 	 */
15211 	if (recovonly == FALSE) {
15212 		/* need to grab the open owner sync before 'os_sync_lock' */
15213 		ep->error = nfs4_start_open_seqid_sync(oop, mi);
15214 		if (ep->error == EAGAIN) {
15215 			ASSERT(!isrecov);
15216 			if (did_start_op)
15217 				nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15218 				    &recov_state, TRUE);
15219 			if (did_force_recovlock)
15220 				nfs_rw_exit(&mi->mi_recovlock);
15221 			goto recov_retry;
15222 		}
15223 		did_start_seqid_sync = 1;
15224 	}
15225 
15226 	/*
15227 	 * Third get an open stream and acquire 'os_sync_lock' to
15228 	 * sychronize the opening/creating of an open stream with the
15229 	 * closing/destroying of an open stream.
15230 	 */
15231 	if (!provided_osp) {
15232 		/* returns with 'os_sync_lock' held */
15233 		osp = find_open_stream(oop, rp);
15234 		if (!osp) {
15235 			ep->error = EIO;
15236 			goto out;
15237 		}
15238 	} else {
15239 		osp = provided_osp;
15240 		open_stream_hold(osp);
15241 		mutex_enter(&osp->os_sync_lock);
15242 	}
15243 	have_sync_lock = 1;
15244 
15245 	ASSERT(oop == osp->os_open_owner);
15246 
15247 	/*
15248 	 * Fourth, do any special pre-OTW CLOSE processing
15249 	 * based on the specific close type.
15250 	 */
15251 	if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15252 	    !did_dec_count) {
15253 		ASSERT(osp->os_open_ref_count > 0);
15254 		osp->os_open_ref_count--;
15255 		did_dec_count = 1;
15256 		if (osp->os_open_ref_count == 0)
15257 			osp->os_final_close = 1;
15258 	}
15259 
15260 	if (close_type == CLOSE_FORCE) {
15261 		/* see if somebody reopened the open stream. */
15262 		if (!osp->os_force_close) {
15263 			NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15264 			    "nfs4close_one: skip CLOSE_FORCE as osp %p "
15265 			    "was reopened, vp %p", (void *)osp, (void *)vp));
15266 			ep->error = 0;
15267 			ep->stat = NFS4_OK;
15268 			goto out;
15269 		}
15270 
15271 		if (!osp->os_final_close && !did_dec_count) {
15272 			osp->os_open_ref_count--;
15273 			did_dec_count = 1;
15274 		}
15275 
15276 		/*
15277 		 * We can't depend on os_open_ref_count being 0 due to the
15278 		 * way executables are opened (VN_RELE to match a VOP_OPEN).
15279 		 */
15280 #ifdef	NOTYET
15281 		ASSERT(osp->os_open_ref_count == 0);
15282 #endif
15283 		if (osp->os_open_ref_count != 0) {
15284 			NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15285 			    "nfs4close_one: should panic here on an "
15286 			    "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15287 			    "since this is probably the exec problem."));
15288 
15289 			osp->os_open_ref_count = 0;
15290 		}
15291 
15292 		/*
15293 		 * There is the possibility that nfs4close_one()
15294 		 * for close_type == CLOSE_DELMAP couldn't find the
15295 		 * open stream, thus couldn't decrement its os_mapcnt;
15296 		 * therefore we can't use this ASSERT yet.
15297 		 */
15298 #ifdef	NOTYET
15299 		ASSERT(osp->os_mapcnt == 0);
15300 #endif
15301 		osp->os_mapcnt = 0;
15302 	}
15303 
15304 	if (close_type == CLOSE_DELMAP && !did_dec_count) {
15305 		ASSERT(osp->os_mapcnt >= btopr(len));
15306 
15307 		if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15308 			osp->os_mmap_write -= btopr(len);
15309 		if (maxprot & PROT_READ)
15310 			osp->os_mmap_read -= btopr(len);
15311 		if (maxprot & PROT_EXEC)
15312 			osp->os_mmap_read -= btopr(len);
15313 		/* mirror the PROT_NONE check in nfs4_addmap() */
15314 		if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15315 		    !(maxprot & PROT_EXEC))
15316 			osp->os_mmap_read -= btopr(len);
15317 		osp->os_mapcnt -= btopr(len);
15318 		did_dec_count = 1;
15319 	}
15320 
15321 	if (recovonly) {
15322 		nfs4_lost_rqst_t lost_rqst;
15323 
15324 		/* request should not already be in recovery queue */
15325 		ASSERT(lrp == NULL);
15326 		nfs4_error_init(ep, EINTR);
15327 		nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15328 		    osp, cred_otw, vp);
15329 		mutex_exit(&osp->os_sync_lock);
15330 		have_sync_lock = 0;
15331 		(void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15332 		    lost_rqst.lr_op == OP_CLOSE ?
15333 		    &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15334 		close_failed = 1;
15335 		force_close = 0;
15336 		goto close_cleanup;
15337 	}
15338 
15339 	/*
15340 	 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15341 	 * we stopped operating on the open owner's <old oo_name, old seqid>
15342 	 * space, which means we stopped operating on the open stream
15343 	 * too.  So don't go OTW (as the seqid is likely bad, and the
15344 	 * stateid could be stale, potentially triggering a false
15345 	 * setclientid), and just clean up the client's internal state.
15346 	 */
15347 	if (osp->os_orig_oo_name != oop->oo_name) {
15348 		NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15349 		    (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15350 		    "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15351 		    "oo_name %" PRIx64")",
15352 		    (void *)osp, (void *)oop, osp->os_orig_oo_name,
15353 		    oop->oo_name));
15354 		close_failed = 1;
15355 	}
15356 
15357 	/* If the file failed recovery, just quit. */
15358 	mutex_enter(&rp->r_statelock);
15359 	if (rp->r_flags & R4RECOVERR) {
15360 		close_failed = 1;
15361 	}
15362 	mutex_exit(&rp->r_statelock);
15363 
15364 	/*
15365 	 * If the force close path failed to obtain start_fop
15366 	 * then skip the OTW close and just remove the state.
15367 	 */
15368 	if (close_failed)
15369 		goto close_cleanup;
15370 
15371 	/*
15372 	 * Fifth, check to see if there are still mapped pages or other
15373 	 * opens using this open stream.  If there are then we can't
15374 	 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15375 	 */
15376 	if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15377 		nfs4_lost_rqst_t	new_lost_rqst;
15378 		bool_t			needrecov = FALSE;
15379 		cred_t			*odg_cred_otw = NULL;
15380 		seqid4			open_dg_seqid = 0;
15381 
15382 		if (osp->os_delegation) {
15383 			/*
15384 			 * If this open stream was never OPENed OTW then we
15385 			 * surely can't DOWNGRADE it (especially since the
15386 			 * osp->open_stateid is really a delegation stateid
15387 			 * when os_delegation is 1).
15388 			 */
15389 			if (access_bits & FREAD)
15390 				osp->os_share_acc_read--;
15391 			if (access_bits & FWRITE)
15392 				osp->os_share_acc_write--;
15393 			osp->os_share_deny_none--;
15394 			nfs4_error_zinit(ep);
15395 			goto out;
15396 		}
15397 		nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15398 		    lrp, ep, &odg_cred_otw, &open_dg_seqid);
15399 		needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15400 		if (needrecov && !isrecov) {
15401 			bool_t abort;
15402 			nfs4_bseqid_entry_t *bsep = NULL;
15403 
15404 			if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15405 				bsep = nfs4_create_bseqid_entry(oop, NULL,
15406 				    vp, 0,
15407 				    lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15408 				    open_dg_seqid);
15409 
15410 			nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15411 			    oop, osp, odg_cred_otw, vp, access_bits, 0);
15412 			mutex_exit(&osp->os_sync_lock);
15413 			have_sync_lock = 0;
15414 			abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15415 			    new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15416 			    &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15417 			    bsep, NULL, NULL);
15418 			if (odg_cred_otw)
15419 				crfree(odg_cred_otw);
15420 			if (bsep)
15421 				kmem_free(bsep, sizeof (*bsep));
15422 
15423 			if (abort == TRUE)
15424 				goto out;
15425 
15426 			if (did_start_seqid_sync) {
15427 				nfs4_end_open_seqid_sync(oop);
15428 				did_start_seqid_sync = 0;
15429 			}
15430 			open_stream_rele(osp, rp);
15431 
15432 			if (did_start_op)
15433 				nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15434 				    &recov_state, FALSE);
15435 			if (did_force_recovlock)
15436 				nfs_rw_exit(&mi->mi_recovlock);
15437 
15438 			goto recov_retry;
15439 		} else {
15440 			if (odg_cred_otw)
15441 				crfree(odg_cred_otw);
15442 		}
15443 		goto out;
15444 	}
15445 
15446 	/*
15447 	 * If this open stream was created as the results of an open
15448 	 * while holding a delegation, then just release it; no need
15449 	 * to do an OTW close.  Otherwise do a "normal" OTW close.
15450 	 */
15451 	if (osp->os_delegation) {
15452 		nfs4close_notw(vp, osp, &have_sync_lock);
15453 		nfs4_error_zinit(ep);
15454 		goto out;
15455 	}
15456 
15457 	/*
15458 	 * If this stream is not valid, we're done.
15459 	 */
15460 	if (!osp->os_valid) {
15461 		nfs4_error_zinit(ep);
15462 		goto out;
15463 	}
15464 
15465 	/*
15466 	 * Last open or mmap ref has vanished, need to do an OTW close.
15467 	 * First check to see if a close is still necessary.
15468 	 */
15469 	if (osp->os_failed_reopen) {
15470 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15471 		    "don't close OTW osp %p since reopen failed.",
15472 		    (void *)osp));
15473 		/*
15474 		 * Reopen of the open stream failed, hence the
15475 		 * stateid of the open stream is invalid/stale, and
15476 		 * sending this OTW would incorrectly cause another
15477 		 * round of recovery.  In this case, we need to set
15478 		 * the 'os_valid' bit to 0 so another thread doesn't
15479 		 * come in and re-open this open stream before
15480 		 * this "closing" thread cleans up state (decrementing
15481 		 * the nfs4_server_t's state_ref_count and decrementing
15482 		 * the os_ref_count).
15483 		 */
15484 		osp->os_valid = 0;
15485 		/*
15486 		 * This removes the reference obtained at OPEN; ie,
15487 		 * when the open stream structure was created.
15488 		 *
15489 		 * We don't have to worry about calling 'open_stream_rele'
15490 		 * since we our currently holding a reference to this
15491 		 * open stream which means the count can not go to 0 with
15492 		 * this decrement.
15493 		 */
15494 		ASSERT(osp->os_ref_count >= 2);
15495 		osp->os_ref_count--;
15496 		nfs4_error_zinit(ep);
15497 		close_failed = 0;
15498 		goto close_cleanup;
15499 	}
15500 
15501 	ASSERT(osp->os_ref_count > 1);
15502 
15503 	/*
15504 	 * Sixth, try the CLOSE OTW.
15505 	 */
15506 	nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15507 	    close_type, ep, &have_sync_lock);
15508 
15509 	if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15510 		/*
15511 		 * Let the recovery thread be responsible for
15512 		 * removing the state for CLOSE.
15513 		 */
15514 		close_failed = 1;
15515 		force_close = 0;
15516 		retry = 0;
15517 	}
15518 
15519 	/* See if we need to retry with a different cred */
15520 	if ((ep->error == EACCES ||
15521 	    (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15522 	    cred_otw != cr) {
15523 		crfree(cred_otw);
15524 		cred_otw = cr;
15525 		crhold(cred_otw);
15526 		retry = 1;
15527 	}
15528 
15529 	if (ep->error || ep->stat)
15530 		close_failed = 1;
15531 
15532 	if (retry && !isrecov && num_retries-- > 0) {
15533 		if (have_sync_lock) {
15534 			mutex_exit(&osp->os_sync_lock);
15535 			have_sync_lock = 0;
15536 		}
15537 		if (did_start_seqid_sync) {
15538 			nfs4_end_open_seqid_sync(oop);
15539 			did_start_seqid_sync = 0;
15540 		}
15541 		open_stream_rele(osp, rp);
15542 
15543 		if (did_start_op)
15544 			nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15545 			    &recov_state, FALSE);
15546 		if (did_force_recovlock)
15547 			nfs_rw_exit(&mi->mi_recovlock);
15548 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15549 		    "nfs4close_one: need to retry the close "
15550 		    "operation"));
15551 		goto recov_retry;
15552 	}
15553 close_cleanup:
15554 	/*
15555 	 * Seventh and lastly, process our results.
15556 	 */
15557 	if (close_failed && force_close) {
15558 		/*
15559 		 * It's ok to drop and regrab the 'os_sync_lock' since
15560 		 * nfs4close_notw() will recheck to make sure the
15561 		 * "close"/removal of state should happen.
15562 		 */
15563 		if (!have_sync_lock) {
15564 			mutex_enter(&osp->os_sync_lock);
15565 			have_sync_lock = 1;
15566 		}
15567 		/*
15568 		 * This is last call, remove the ref on the open
15569 		 * stream created by open and clean everything up.
15570 		 */
15571 		osp->os_pending_close = 0;
15572 		nfs4close_notw(vp, osp, &have_sync_lock);
15573 		nfs4_error_zinit(ep);
15574 	}
15575 
15576 	if (!close_failed) {
15577 		if (have_sync_lock) {
15578 			osp->os_pending_close = 0;
15579 			mutex_exit(&osp->os_sync_lock);
15580 			have_sync_lock = 0;
15581 		} else {
15582 			mutex_enter(&osp->os_sync_lock);
15583 			osp->os_pending_close = 0;
15584 			mutex_exit(&osp->os_sync_lock);
15585 		}
15586 		if (did_start_op && recov_state.rs_sp != NULL) {
15587 			mutex_enter(&recov_state.rs_sp->s_lock);
15588 			nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15589 			mutex_exit(&recov_state.rs_sp->s_lock);
15590 		} else {
15591 			nfs4_dec_state_ref_count(mi);
15592 		}
15593 		nfs4_error_zinit(ep);
15594 	}
15595 
15596 out:
15597 	if (have_sync_lock)
15598 		mutex_exit(&osp->os_sync_lock);
15599 	if (did_start_op)
15600 		nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15601 		    recovonly ? TRUE : FALSE);
15602 	if (did_force_recovlock)
15603 		nfs_rw_exit(&mi->mi_recovlock);
15604 	if (cred_otw)
15605 		crfree(cred_otw);
15606 	if (osp)
15607 		open_stream_rele(osp, rp);
15608 	if (oop) {
15609 		if (did_start_seqid_sync)
15610 			nfs4_end_open_seqid_sync(oop);
15611 		open_owner_rele(oop);
15612 	}
15613 }
15614 
15615 /*
15616  * Convert information returned by the server in the LOCK4denied
15617  * structure to the form required by fcntl.
15618  */
15619 static void
denied_to_flk(LOCK4denied * lockt_denied,flock64_t * flk,LOCKT4args * lockt_args)15620 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15621 {
15622 	nfs4_lo_name_t *lo;
15623 
15624 #ifdef	DEBUG
15625 	if (denied_to_flk_debug) {
15626 		lockt_denied_debug = lockt_denied;
15627 		debug_enter("lockt_denied");
15628 	}
15629 #endif
15630 
15631 	flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15632 	flk->l_whence = 0;	/* aka SEEK_SET */
15633 	flk->l_start = lockt_denied->offset;
15634 	flk->l_len = lockt_denied->length;
15635 
15636 	/*
15637 	 * If the blocking clientid matches our client id, then we can
15638 	 * interpret the lockowner (since we built it).  If not, then
15639 	 * fabricate a sysid and pid.  Note that the l_sysid field
15640 	 * in *flk already has the local sysid.
15641 	 */
15642 
15643 	if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15644 
15645 		if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15646 			lo = (nfs4_lo_name_t *)
15647 			    lockt_denied->owner.owner_val;
15648 
15649 			flk->l_pid = lo->ln_pid;
15650 		} else {
15651 			NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15652 			    "denied_to_flk: bad lock owner length\n"));
15653 
15654 			flk->l_pid = lo_to_pid(&lockt_denied->owner);
15655 		}
15656 	} else {
15657 		NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15658 		"denied_to_flk: foreign clientid\n"));
15659 
15660 		/*
15661 		 * Construct a new sysid which should be different from
15662 		 * sysids of other systems.
15663 		 */
15664 
15665 		flk->l_sysid++;
15666 		flk->l_pid = lo_to_pid(&lockt_denied->owner);
15667 	}
15668 }
15669 
15670 static pid_t
lo_to_pid(lock_owner4 * lop)15671 lo_to_pid(lock_owner4 *lop)
15672 {
15673 	pid_t pid = 0;
15674 	uchar_t *cp;
15675 	int i;
15676 
15677 	cp = (uchar_t *)&lop->clientid;
15678 
15679 	for (i = 0; i < sizeof (lop->clientid); i++)
15680 		pid += (pid_t)*cp++;
15681 
15682 	cp = (uchar_t *)lop->owner_val;
15683 
15684 	for (i = 0; i < lop->owner_len; i++)
15685 		pid += (pid_t)*cp++;
15686 
15687 	return (pid);
15688 }
15689 
15690 /*
15691  * Given a lock pointer, returns the length of that lock.
15692  * "end" is the last locked offset the "l_len" covers from
15693  * the start of the lock.
15694  */
15695 static off64_t
lock_to_end(flock64_t * lock)15696 lock_to_end(flock64_t *lock)
15697 {
15698 	off64_t lock_end;
15699 
15700 	if (lock->l_len == 0)
15701 		lock_end = (off64_t)MAXEND;
15702 	else
15703 		lock_end = lock->l_start + lock->l_len - 1;
15704 
15705 	return (lock_end);
15706 }
15707 
15708 /*
15709  * Given the end of a lock, it will return you the length "l_len" for that lock.
15710  */
15711 static off64_t
end_to_len(off64_t start,off64_t end)15712 end_to_len(off64_t start, off64_t end)
15713 {
15714 	off64_t lock_len;
15715 
15716 	ASSERT(end >= start);
15717 	if (end == MAXEND)
15718 		lock_len = 0;
15719 	else
15720 		lock_len = end - start + 1;
15721 
15722 	return (lock_len);
15723 }
15724 
15725 /*
15726  * On given end for a lock it determines if it is the last locked offset
15727  * or not, if so keeps it as is, else adds one to return the length for
15728  * valid start.
15729  */
15730 static off64_t
start_check(off64_t x)15731 start_check(off64_t x)
15732 {
15733 	if (x == MAXEND)
15734 		return (x);
15735 	else
15736 		return (x + 1);
15737 }
15738 
15739 /*
15740  * See if these two locks overlap, and if so return 1;
15741  * otherwise, return 0.
15742  */
15743 static int
locks_intersect(flock64_t * llfp,flock64_t * curfp)15744 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15745 {
15746 	off64_t llfp_end, curfp_end;
15747 
15748 	llfp_end = lock_to_end(llfp);
15749 	curfp_end = lock_to_end(curfp);
15750 
15751 	if (((llfp_end >= curfp->l_start) &&
15752 	    (llfp->l_start <= curfp->l_start)) ||
15753 	    ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15754 		return (1);
15755 	return (0);
15756 }
15757 
15758 /*
15759  * Determine what the intersecting lock region is, and add that to the
15760  * 'nl_llpp' locklist in increasing order (by l_start).
15761  */
15762 static void
nfs4_add_lock_range(flock64_t * lost_flp,flock64_t * local_flp,locklist_t ** nl_llpp,vnode_t * vp)15763 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15764     locklist_t **nl_llpp, vnode_t *vp)
15765 {
15766 	locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15767 	off64_t lost_flp_end, local_flp_end, len, start;
15768 
15769 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15770 
15771 	if (!locks_intersect(lost_flp, local_flp))
15772 		return;
15773 
15774 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15775 	    "locks intersect"));
15776 
15777 	lost_flp_end = lock_to_end(lost_flp);
15778 	local_flp_end = lock_to_end(local_flp);
15779 
15780 	/* Find the starting point of the intersecting region */
15781 	if (local_flp->l_start > lost_flp->l_start)
15782 		start = local_flp->l_start;
15783 	else
15784 		start = lost_flp->l_start;
15785 
15786 	/* Find the lenght of the intersecting region */
15787 	if (lost_flp_end < local_flp_end)
15788 		len = end_to_len(start, lost_flp_end);
15789 	else
15790 		len = end_to_len(start, local_flp_end);
15791 
15792 	/*
15793 	 * Prepare the flock structure for the intersection found and insert
15794 	 * it into the new list in increasing l_start order. This list contains
15795 	 * intersections of locks registered by the client with the local host
15796 	 * and the lost lock.
15797 	 * The lock type of this lock is the same as that of the local_flp.
15798 	 */
15799 	intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15800 	intersect_llp->ll_flock.l_start = start;
15801 	intersect_llp->ll_flock.l_len = len;
15802 	intersect_llp->ll_flock.l_type = local_flp->l_type;
15803 	intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15804 	intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15805 	intersect_llp->ll_flock.l_whence = 0;	/* aka SEEK_SET */
15806 	intersect_llp->ll_vp = vp;
15807 
15808 	tmp_fllp = *nl_llpp;
15809 	cur_fllp = NULL;
15810 	while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15811 	    intersect_llp->ll_flock.l_start) {
15812 			cur_fllp = tmp_fllp;
15813 			tmp_fllp = tmp_fllp->ll_next;
15814 	}
15815 	if (cur_fllp == NULL) {
15816 		/* first on the list */
15817 		intersect_llp->ll_next = *nl_llpp;
15818 		*nl_llpp = intersect_llp;
15819 	} else {
15820 		intersect_llp->ll_next = cur_fllp->ll_next;
15821 		cur_fllp->ll_next = intersect_llp;
15822 	}
15823 
15824 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15825 	    "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15826 	    intersect_llp->ll_flock.l_start,
15827 	    intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15828 	    intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15829 }
15830 
15831 /*
15832  * Our local locking current state is potentially different than
15833  * what the NFSv4 server thinks we have due to a lost lock that was
15834  * resent and then received.  We need to reset our "NFSv4" locking
15835  * state to match the current local locking state for this pid since
15836  * that is what the user/application sees as what the world is.
15837  *
15838  * We cannot afford to drop the open/lock seqid sync since then we can
15839  * get confused about what the current local locking state "is" versus
15840  * "was".
15841  *
15842  * If we are unable to fix up the locks, we send SIGLOST to the affected
15843  * process.  This is not done if the filesystem has been forcibly
15844  * unmounted, in case the process has already exited and a new process
15845  * exists with the same pid.
15846  */
15847 static void
nfs4_reinstitute_local_lock_state(vnode_t * vp,flock64_t * lost_flp,cred_t * cr,nfs4_lock_owner_t * lop)15848 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15849     nfs4_lock_owner_t *lop)
15850 {
15851 	locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15852 	mntinfo4_t *mi = VTOMI4(vp);
15853 	const int cmd = F_SETLK;
15854 	off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15855 	flock64_t ul_fl;
15856 
15857 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15858 	    "nfs4_reinstitute_local_lock_state"));
15859 
15860 	/*
15861 	 * Find active locks for this vp from the local locking code.
15862 	 * Scan through this list and find out the locks that intersect with
15863 	 * the lost lock. Once we find the lock that intersects, add the
15864 	 * intersection area as a new lock to a new list "ri_llp". The lock
15865 	 * type of the intersection region lock added to ri_llp is the same
15866 	 * as that found in the active lock list, "list". The intersecting
15867 	 * region locks are added to ri_llp in increasing l_start order.
15868 	 */
15869 	ASSERT(nfs_zone() == mi->mi_zone);
15870 
15871 	locks = flk_active_locks_for_vp(vp);
15872 	ri_llp = NULL;
15873 
15874 	for (llp = locks; llp != NULL; llp = llp->ll_next) {
15875 		ASSERT(llp->ll_vp == vp);
15876 		/*
15877 		 * Pick locks that belong to this pid/lockowner
15878 		 */
15879 		if (llp->ll_flock.l_pid != lost_flp->l_pid)
15880 			continue;
15881 
15882 		nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15883 	}
15884 
15885 	/*
15886 	 * Now we have the list of intersections with the lost lock. These are
15887 	 * the locks that were/are active before the server replied to the
15888 	 * last/lost lock. Issue these locks to the server here. Playing these
15889 	 * locks to the server will re-establish our current local locking state
15890 	 * with the v4 server.
15891 	 * If we get an error, send SIGLOST to the application for that lock.
15892 	 */
15893 
15894 	for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15895 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15896 		    "nfs4_reinstitute_local_lock_state: need to issue "
15897 		    "flock: [%"PRIx64" - %"PRIx64"] : %s",
15898 		    llp->ll_flock.l_start,
15899 		    llp->ll_flock.l_start + llp->ll_flock.l_len,
15900 		    llp->ll_flock.l_type == F_RDLCK ? "READ" :
15901 		    llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15902 		/*
15903 		 * No need to relock what we already have
15904 		 */
15905 		if (llp->ll_flock.l_type == lost_flp->l_type)
15906 			continue;
15907 
15908 		push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15909 	}
15910 
15911 	/*
15912 	 * Now keeping the start of the lost lock as our reference parse the
15913 	 * newly created ri_llp locklist to find the ranges that we have locked
15914 	 * with the v4 server but not in the current local locking. We need
15915 	 * to unlock these ranges.
15916 	 * These ranges can also be reffered to as those ranges, where the lost
15917 	 * lock does not overlap with the locks in the ri_llp but are locked
15918 	 * since the server replied to the lost lock.
15919 	 */
15920 	cur_start = lost_flp->l_start;
15921 	lost_flp_end = lock_to_end(lost_flp);
15922 
15923 	ul_fl.l_type = F_UNLCK;
15924 	ul_fl.l_whence = 0;	/* aka SEEK_SET */
15925 	ul_fl.l_sysid = lost_flp->l_sysid;
15926 	ul_fl.l_pid = lost_flp->l_pid;
15927 
15928 	for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15929 		llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15930 
15931 		if (llp->ll_flock.l_start <= cur_start) {
15932 			cur_start = start_check(llp_ll_flock_end);
15933 			continue;
15934 		}
15935 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15936 		    "nfs4_reinstitute_local_lock_state: "
15937 		    "UNLOCK [%"PRIx64" - %"PRIx64"]",
15938 		    cur_start, llp->ll_flock.l_start));
15939 
15940 		ul_fl.l_start = cur_start;
15941 		ul_fl.l_len = end_to_len(cur_start,
15942 		    (llp->ll_flock.l_start - 1));
15943 
15944 		push_reinstate(vp, cmd, &ul_fl, cr, lop);
15945 		cur_start = start_check(llp_ll_flock_end);
15946 	}
15947 
15948 	/*
15949 	 * In the case where the lost lock ends after all intersecting locks,
15950 	 * unlock the last part of the lost lock range.
15951 	 */
15952 	if (cur_start != start_check(lost_flp_end)) {
15953 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15954 		    "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15955 		    "lost lock region [%"PRIx64" - %"PRIx64"]",
15956 		    cur_start, lost_flp->l_start + lost_flp->l_len));
15957 
15958 		ul_fl.l_start = cur_start;
15959 		/*
15960 		 * Is it an to-EOF lock? if so unlock till the end
15961 		 */
15962 		if (lost_flp->l_len == 0)
15963 			ul_fl.l_len = 0;
15964 		else
15965 			ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15966 
15967 		push_reinstate(vp, cmd, &ul_fl, cr, lop);
15968 	}
15969 
15970 	if (locks != NULL)
15971 		flk_free_locklist(locks);
15972 
15973 	/* Free up our newly created locklist */
15974 	for (llp = ri_llp; llp != NULL; ) {
15975 		tmp_llp = llp->ll_next;
15976 		kmem_free(llp, sizeof (locklist_t));
15977 		llp = tmp_llp;
15978 	}
15979 
15980 	/*
15981 	 * Now return back to the original calling nfs4frlock()
15982 	 * and let us naturally drop our seqid syncs.
15983 	 */
15984 }
15985 
15986 /*
15987  * Create a lost state record for the given lock reinstantiation request
15988  * and push it onto the lost state queue.
15989  */
15990 static void
push_reinstate(vnode_t * vp,int cmd,flock64_t * flk,cred_t * cr,nfs4_lock_owner_t * lop)15991 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15992     nfs4_lock_owner_t *lop)
15993 {
15994 	nfs4_lost_rqst_t req;
15995 	nfs_lock_type4 locktype;
15996 	nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15997 
15998 	ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15999 
16000 	locktype = flk_to_locktype(cmd, flk->l_type);
16001 	nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
16002 	    NULL, NULL, lop, flk, &req, cr, vp);
16003 	(void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
16004 	    (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
16005 	    &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
16006 	    NULL, NULL, NULL);
16007 }
16008