xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs3_vnops.c (revision 33efde4275d24731ef87927237b0ffb0630b6b2d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28  *	All rights reserved.
29  */
30 
31 /*
32  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
34  * Copyright 2022 Oxide Computer Company
35  * Copyright 2024 RackTop Systems, Inc.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/cred.h>
42 #include <sys/time.h>
43 #include <sys/vnode.h>
44 #include <sys/vfs.h>
45 #include <sys/vfs_opreg.h>
46 #include <sys/file.h>
47 #include <sys/filio.h>
48 #include <sys/uio.h>
49 #include <sys/buf.h>
50 #include <sys/mman.h>
51 #include <sys/pathname.h>
52 #include <sys/dirent.h>
53 #include <sys/debug.h>
54 #include <sys/vmsystm.h>
55 #include <sys/fcntl.h>
56 #include <sys/flock.h>
57 #include <sys/swap.h>
58 #include <sys/errno.h>
59 #include <sys/strsubr.h>
60 #include <sys/sysmacros.h>
61 #include <sys/kmem.h>
62 #include <sys/cmn_err.h>
63 #include <sys/pathconf.h>
64 #include <sys/utsname.h>
65 #include <sys/dnlc.h>
66 #include <sys/acl.h>
67 #include <sys/systeminfo.h>
68 #include <sys/atomic.h>
69 #include <sys/policy.h>
70 #include <sys/sdt.h>
71 #include <sys/zone.h>
72 
73 #include <rpc/types.h>
74 #include <rpc/auth.h>
75 #include <rpc/clnt.h>
76 #include <rpc/rpc_rdma.h>
77 
78 #include <nfs/nfs.h>
79 #include <nfs/nfs_clnt.h>
80 #include <nfs/rnode.h>
81 #include <nfs/nfs_acl.h>
82 #include <nfs/lm.h>
83 
84 #include <vm/hat.h>
85 #include <vm/as.h>
86 #include <vm/page.h>
87 #include <vm/pvn.h>
88 #include <vm/seg.h>
89 #include <vm/seg_map.h>
90 #include <vm/seg_kpm.h>
91 #include <vm/seg_vn.h>
92 
93 #include <fs/fs_subr.h>
94 
95 #include <sys/ddi.h>
96 
97 static int	nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
98 			cred_t *);
99 static int	nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
100 			stable_how *);
101 static int	nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *);
102 static int	nfs3setattr(vnode_t *, struct vattr *, int, cred_t *);
103 static int	nfs3_accessx(void *, int, cred_t *);
104 static int	nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
105 static int	nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
106 static int	nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl,
107 			int, vnode_t **, cred_t *, int);
108 static int	nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *);
109 static int	nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
110 			int, vnode_t **, cred_t *);
111 static int	nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
112 			caller_context_t *);
113 static int	do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
114 static void	nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
115 static void	nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *);
116 static int	nfs3_bio(struct buf *, stable_how *, cred_t *);
117 static int	nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
118 			page_t *[], size_t, struct seg *, caddr_t,
119 			enum seg_rw, cred_t *);
120 static void	nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
121 			cred_t *);
122 static int	nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
123 			int, cred_t *);
124 static int	nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
125 			int, cred_t *);
126 static int	nfs3_commit(vnode_t *, offset3, count3, cred_t *);
127 static void	nfs3_set_mod(vnode_t *);
128 static void	nfs3_get_commit(vnode_t *);
129 static void	nfs3_get_commit_range(vnode_t *, u_offset_t, size_t);
130 static int	nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
131 static int	nfs3_commit_vp(vnode_t *, u_offset_t, size_t,  cred_t *);
132 static int	nfs3_sync_commit(vnode_t *, page_t *, offset3, count3,
133 			cred_t *);
134 static void	nfs3_async_commit(vnode_t *, page_t *, offset3, count3,
135 			cred_t *);
136 static void	nfs3_delmap_callback(struct as *, void *, uint_t);
137 
138 /*
139  * Error flags used to pass information about certain special errors
140  * which need to be handled specially.
141  */
142 #define	NFS_EOF			-98
143 #define	NFS_VERF_MISMATCH	-97
144 
145 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
146 #define	ALIGN64(x, ptr, sz)						\
147 	x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1);		\
148 	if (x) {							\
149 		x = sizeof (uint64_t) - (x);				\
150 		sz -= (x);						\
151 		ptr += (x);						\
152 	}
153 
154 /*
155  * These are the vnode ops routines which implement the vnode interface to
156  * the networked file system.  These routines just take their parameters,
157  * make them look networkish by putting the right info into interface structs,
158  * and then calling the appropriate remote routine(s) to do the work.
159  *
160  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
161  * we purge the directory cache relative to that vnode.  This way, the
162  * user won't get burned by the cache repeatedly.  See <nfs/rnode.h> for
163  * more details on rnode locking.
164  */
165 
166 static int	nfs3_open(vnode_t **, int, cred_t *, caller_context_t *);
167 static int	nfs3_close(vnode_t *, int, int, offset_t, cred_t *,
168 			caller_context_t *);
169 static int	nfs3_read(vnode_t *, struct uio *, int, cred_t *,
170 			caller_context_t *);
171 static int	nfs3_write(vnode_t *, struct uio *, int, cred_t *,
172 			caller_context_t *);
173 static int	nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
174 			caller_context_t *);
175 static int	nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *,
176 			caller_context_t *);
177 static int	nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *,
178 			caller_context_t *);
179 static int	nfs3_access(vnode_t *, int, int, cred_t *, caller_context_t *);
180 static int	nfs3_readlink(vnode_t *, struct uio *, cred_t *,
181 			caller_context_t *);
182 static int	nfs3_fsync(vnode_t *, int, cred_t *, caller_context_t *);
183 static void	nfs3_inactive(vnode_t *, cred_t *, caller_context_t *);
184 static int	nfs3_lookup(vnode_t *, char *, vnode_t **,
185 			struct pathname *, int, vnode_t *, cred_t *,
186 			caller_context_t *, int *, pathname_t *);
187 static int	nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl,
188 			int, vnode_t **, cred_t *, int, caller_context_t *,
189 			vsecattr_t *);
190 static int	nfs3_remove(vnode_t *, char *, cred_t *, caller_context_t *,
191 			int);
192 static int	nfs3_link(vnode_t *, vnode_t *, char *, cred_t *,
193 			caller_context_t *, int);
194 static int	nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
195 			caller_context_t *, int);
196 static int	nfs3_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
197 			cred_t *, caller_context_t *, int, vsecattr_t *);
198 static int	nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
199 			caller_context_t *, int);
200 static int	nfs3_symlink(vnode_t *, char *, struct vattr *, char *,
201 			cred_t *, caller_context_t *, int);
202 static int	nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *,
203 			caller_context_t *, int);
204 static int	nfs3_fid(vnode_t *, fid_t *, caller_context_t *);
205 static int	nfs3_rwlock(vnode_t *, int, caller_context_t *);
206 static void	nfs3_rwunlock(vnode_t *, int, caller_context_t *);
207 static int	nfs3_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
208 static int	nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *,
209 			page_t *[], size_t, struct seg *, caddr_t,
210 			enum seg_rw, cred_t *, caller_context_t *);
211 static int	nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
212 			caller_context_t *);
213 static int	nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
214 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
215 static int	nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
216 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
217 static int	nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
218 			struct flk_callback *, cred_t *, caller_context_t *);
219 static int	nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t,
220 			cred_t *, caller_context_t *);
221 static int	nfs3_realvp(vnode_t *, vnode_t **, caller_context_t *);
222 static int	nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
223 			uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
224 static int	nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *,
225 			caller_context_t *);
226 static int	nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
227 			cred_t *, caller_context_t *);
228 static void	nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *,
229 			caller_context_t *);
230 static int	nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
231 			caller_context_t *);
232 static int	nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
233 			caller_context_t *);
234 static int	nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
235 			caller_context_t *);
236 
237 struct vnodeops *nfs3_vnodeops;
238 
239 const fs_operation_def_t nfs3_vnodeops_template[] = {
240 	VOPNAME_OPEN,		{ .vop_open = nfs3_open },
241 	VOPNAME_CLOSE,		{ .vop_close = nfs3_close },
242 	VOPNAME_READ,		{ .vop_read = nfs3_read },
243 	VOPNAME_WRITE,		{ .vop_write = nfs3_write },
244 	VOPNAME_IOCTL,		{ .vop_ioctl = nfs3_ioctl },
245 	VOPNAME_GETATTR,	{ .vop_getattr = nfs3_getattr },
246 	VOPNAME_SETATTR,	{ .vop_setattr = nfs3_setattr },
247 	VOPNAME_ACCESS,		{ .vop_access = nfs3_access },
248 	VOPNAME_LOOKUP,		{ .vop_lookup = nfs3_lookup },
249 	VOPNAME_CREATE,		{ .vop_create = nfs3_create },
250 	VOPNAME_REMOVE,		{ .vop_remove = nfs3_remove },
251 	VOPNAME_LINK,		{ .vop_link = nfs3_link },
252 	VOPNAME_RENAME,		{ .vop_rename = nfs3_rename },
253 	VOPNAME_MKDIR,		{ .vop_mkdir = nfs3_mkdir },
254 	VOPNAME_RMDIR,		{ .vop_rmdir = nfs3_rmdir },
255 	VOPNAME_READDIR,	{ .vop_readdir = nfs3_readdir },
256 	VOPNAME_SYMLINK,	{ .vop_symlink = nfs3_symlink },
257 	VOPNAME_READLINK,	{ .vop_readlink = nfs3_readlink },
258 	VOPNAME_FSYNC,		{ .vop_fsync = nfs3_fsync },
259 	VOPNAME_INACTIVE,	{ .vop_inactive = nfs3_inactive },
260 	VOPNAME_FID,		{ .vop_fid = nfs3_fid },
261 	VOPNAME_RWLOCK,		{ .vop_rwlock = nfs3_rwlock },
262 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = nfs3_rwunlock },
263 	VOPNAME_SEEK,		{ .vop_seek = nfs3_seek },
264 	VOPNAME_FRLOCK,		{ .vop_frlock = nfs3_frlock },
265 	VOPNAME_SPACE,		{ .vop_space = nfs3_space },
266 	VOPNAME_REALVP,		{ .vop_realvp = nfs3_realvp },
267 	VOPNAME_GETPAGE,	{ .vop_getpage = nfs3_getpage },
268 	VOPNAME_PUTPAGE,	{ .vop_putpage = nfs3_putpage },
269 	VOPNAME_MAP,		{ .vop_map = nfs3_map },
270 	VOPNAME_ADDMAP,		{ .vop_addmap = nfs3_addmap },
271 	VOPNAME_DELMAP,		{ .vop_delmap = nfs3_delmap },
272 	/* no separate nfs3_dump */
273 	VOPNAME_DUMP,		{ .vop_dump = nfs_dump },
274 	VOPNAME_PATHCONF,	{ .vop_pathconf = nfs3_pathconf },
275 	VOPNAME_PAGEIO,		{ .vop_pageio = nfs3_pageio },
276 	VOPNAME_DISPOSE,	{ .vop_dispose = nfs3_dispose },
277 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = nfs3_setsecattr },
278 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = nfs3_getsecattr },
279 	VOPNAME_SHRLOCK,	{ .vop_shrlock = nfs3_shrlock },
280 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
281 	NULL,			NULL
282 };
283 
284 /*
285  * XXX:  This is referenced in modstubs.S
286  */
287 struct vnodeops *
nfs3_getvnodeops(void)288 nfs3_getvnodeops(void)
289 {
290 	return (nfs3_vnodeops);
291 }
292 
293 /* ARGSUSED */
294 static int
nfs3_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)295 nfs3_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
296 {
297 	int error;
298 	struct vattr va;
299 	rnode_t *rp;
300 	vnode_t *vp;
301 
302 	vp = *vpp;
303 	if (nfs_zone() != VTOMI(vp)->mi_zone)
304 		return (EIO);
305 	rp = VTOR(vp);
306 	mutex_enter(&rp->r_statelock);
307 	if (rp->r_cred == NULL) {
308 		crhold(cr);
309 		rp->r_cred = cr;
310 	}
311 	mutex_exit(&rp->r_statelock);
312 
313 	/*
314 	 * If there is no cached data or if close-to-open
315 	 * consistency checking is turned off, we can avoid
316 	 * the over the wire getattr.  Otherwise, if the
317 	 * file system is mounted readonly, then just verify
318 	 * the caches are up to date using the normal mechanism.
319 	 * Else, if the file is not mmap'd, then just mark
320 	 * the attributes as timed out.  They will be refreshed
321 	 * and the caches validated prior to being used.
322 	 * Else, the file system is mounted writeable so
323 	 * force an over the wire GETATTR in order to ensure
324 	 * that all cached data is valid.
325 	 */
326 	if (vp->v_count > 1 ||
327 	    ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
328 	    !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
329 		if (vn_is_readonly(vp))
330 			error = nfs3_validate_caches(vp, cr);
331 		else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
332 			PURGE_ATTRCACHE(vp);
333 			error = 0;
334 		} else {
335 			va.va_mask = AT_ALL;
336 			error = nfs3_getattr_otw(vp, &va, cr);
337 		}
338 	} else
339 		error = 0;
340 
341 	return (error);
342 }
343 
344 /* ARGSUSED */
345 static int
nfs3_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)346 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
347     caller_context_t *ct)
348 {
349 	rnode_t *rp;
350 	int error;
351 	struct vattr va;
352 
353 	/*
354 	 * zone_enter(2) prevents processes from changing zones with NFS files
355 	 * open; if we happen to get here from the wrong zone we can't do
356 	 * anything over the wire.
357 	 */
358 	if (VTOMI(vp)->mi_zone != nfs_zone()) {
359 		/*
360 		 * We could attempt to clean up locks, except we're sure
361 		 * that the current process didn't acquire any locks on
362 		 * the file: any attempt to lock a file belong to another zone
363 		 * will fail, and one can't lock an NFS file and then change
364 		 * zones, as that fails too.
365 		 *
366 		 * Returning an error here is the sane thing to do.  A
367 		 * subsequent call to VN_RELE() which translates to a
368 		 * nfs3_inactive() will clean up state: if the zone of the
369 		 * vnode's origin is still alive and kicking, an async worker
370 		 * thread will handle the request (from the correct zone), and
371 		 * everything (minus the commit and final nfs3_getattr_otw()
372 		 * call) should be OK. If the zone is going away
373 		 * nfs_async_inactive() will throw away cached pages inline.
374 		 */
375 		return (EIO);
376 	}
377 
378 	/*
379 	 * If we are using local locking for this filesystem, then
380 	 * release all of the SYSV style record locks.  Otherwise,
381 	 * we are doing network locking and we need to release all
382 	 * of the network locks.  All of the locks held by this
383 	 * process on this file are released no matter what the
384 	 * incoming reference count is.
385 	 */
386 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
387 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
388 		cleanshares(vp, ttoproc(curthread)->p_pid);
389 	} else
390 		nfs_lockrelease(vp, flag, offset, cr);
391 
392 	if (count > 1)
393 		return (0);
394 
395 	/*
396 	 * If the file has been `unlinked', then purge the
397 	 * DNLC so that this vnode will get reycled quicker
398 	 * and the .nfs* file on the server will get removed.
399 	 */
400 	rp = VTOR(vp);
401 	if (rp->r_unldvp != NULL)
402 		dnlc_purge_vp(vp);
403 
404 	/*
405 	 * If the file was open for write and there are pages,
406 	 * then if the file system was mounted using the "no-close-
407 	 *	to-open" semantics, then start an asynchronous flush
408 	 *	of the all of the pages in the file.
409 	 * else the file system was not mounted using the "no-close-
410 	 *	to-open" semantics, then do a synchronous flush and
411 	 *	commit of all of the dirty and uncommitted pages.
412 	 *
413 	 * The asynchronous flush of the pages in the "nocto" path
414 	 * mostly just associates a cred pointer with the rnode so
415 	 * writes which happen later will have a better chance of
416 	 * working.  It also starts the data being written to the
417 	 * server, but without unnecessarily delaying the application.
418 	 */
419 	if ((flag & FWRITE) && vn_has_cached_data(vp)) {
420 		if (VTOMI(vp)->mi_flags & MI_NOCTO) {
421 			error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC,
422 			    cr, ct);
423 			if (error == EAGAIN)
424 				error = 0;
425 		} else
426 			error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
427 		if (!error) {
428 			mutex_enter(&rp->r_statelock);
429 			error = rp->r_error;
430 			rp->r_error = 0;
431 			mutex_exit(&rp->r_statelock);
432 		}
433 	} else {
434 		mutex_enter(&rp->r_statelock);
435 		error = rp->r_error;
436 		rp->r_error = 0;
437 		mutex_exit(&rp->r_statelock);
438 	}
439 
440 	/*
441 	 * If RWRITEATTR is set, then issue an over the wire GETATTR to
442 	 * refresh the attribute cache with a set of attributes which
443 	 * weren't returned from a WRITE.  This will enable the close-
444 	 * to-open processing to work.
445 	 */
446 	if (rp->r_flags & RWRITEATTR)
447 		(void) nfs3_getattr_otw(vp, &va, cr);
448 
449 	return (error);
450 }
451 
452 /* ARGSUSED */
453 static int
nfs3_directio_read(vnode_t * vp,struct uio * uiop,cred_t * cr)454 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr)
455 {
456 	mntinfo_t *mi;
457 	READ3args args;
458 	READ3uiores res;
459 	int tsize;
460 	offset_t offset;
461 	ssize_t count;
462 	int error;
463 	int douprintf;
464 	failinfo_t fi;
465 	char *sv_hostname;
466 
467 	mi = VTOMI(vp);
468 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
469 	sv_hostname = VTOR(vp)->r_server->sv_hostname;
470 
471 	douprintf = 1;
472 	args.file = *VTOFH3(vp);
473 	fi.vp = vp;
474 	fi.fhp = (caddr_t)&args.file;
475 	fi.copyproc = nfs3copyfh;
476 	fi.lookupproc = nfs3lookup;
477 	fi.xattrdirproc = acl_getxattrdir3;
478 
479 	res.uiop = uiop;
480 
481 	res.wlist = NULL;
482 
483 	offset = uiop->uio_loffset;
484 	count = uiop->uio_resid;
485 
486 	do {
487 		if (mi->mi_io_kstats) {
488 			mutex_enter(&mi->mi_lock);
489 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
490 			mutex_exit(&mi->mi_lock);
491 		}
492 
493 		do {
494 			tsize = MIN(mi->mi_tsize, count);
495 			args.offset = (offset3)offset;
496 			args.count = (count3)tsize;
497 			res.size = (uint_t)tsize;
498 			args.res_uiop = uiop;
499 			args.res_data_val_alt = NULL;
500 
501 			error = rfs3call(mi, NFSPROC3_READ,
502 			    xdr_READ3args, (caddr_t)&args,
503 			    xdr_READ3uiores, (caddr_t)&res, cr,
504 			    &douprintf, &res.status, 0, &fi);
505 		} while (error == ENFS_TRYAGAIN);
506 
507 		if (mi->mi_io_kstats) {
508 			mutex_enter(&mi->mi_lock);
509 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
510 			mutex_exit(&mi->mi_lock);
511 		}
512 
513 		if (error)
514 			return (error);
515 
516 		error = geterrno3(res.status);
517 		if (error)
518 			return (error);
519 
520 		if (res.count != res.size) {
521 			zcmn_err(getzoneid(), CE_WARN,
522 "nfs3_directio_read: server %s returned incorrect amount",
523 			    sv_hostname);
524 			return (EIO);
525 		}
526 		count -= res.count;
527 		offset += res.count;
528 		if (mi->mi_io_kstats) {
529 			mutex_enter(&mi->mi_lock);
530 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
531 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
532 			mutex_exit(&mi->mi_lock);
533 		}
534 		lwp_stat_update(LWP_STAT_INBLK, 1);
535 	} while (count && !res.eof);
536 
537 	return (0);
538 }
539 
540 /* ARGSUSED */
541 static int
nfs3_read(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)542 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
543     caller_context_t *ct)
544 {
545 	rnode_t *rp;
546 	u_offset_t off;
547 	offset_t diff;
548 	int on;
549 	size_t n;
550 	caddr_t base;
551 	uint_t flags;
552 	int error = 0;
553 	mntinfo_t *mi;
554 
555 	rp = VTOR(vp);
556 	mi = VTOMI(vp);
557 
558 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
559 
560 	if (nfs_zone() != mi->mi_zone)
561 		return (EIO);
562 
563 	if (vp->v_type != VREG)
564 		return (EISDIR);
565 
566 	if (uiop->uio_resid == 0)
567 		return (0);
568 
569 	if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
570 		return (EINVAL);
571 
572 	/*
573 	 * Bypass VM if caching has been disabled (e.g., locking) or if
574 	 * using client-side direct I/O and the file is not mmap'd and
575 	 * there are no cached pages.
576 	 */
577 	if ((vp->v_flag & VNOCACHE) ||
578 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
579 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
580 	    !vn_has_cached_data(vp))) {
581 		return (nfs3_directio_read(vp, uiop, cr));
582 	}
583 
584 	do {
585 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
586 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
587 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
588 
589 		error = nfs3_validate_caches(vp, cr);
590 		if (error)
591 			break;
592 
593 		mutex_enter(&rp->r_statelock);
594 		while (rp->r_flags & RINCACHEPURGE) {
595 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
596 				mutex_exit(&rp->r_statelock);
597 				return (EINTR);
598 			}
599 		}
600 		diff = rp->r_size - uiop->uio_loffset;
601 		mutex_exit(&rp->r_statelock);
602 		if (diff <= 0)
603 			break;
604 		if (diff < n)
605 			n = (size_t)diff;
606 
607 		if (vpm_enable) {
608 			/*
609 			 * Copy data.
610 			 */
611 			error = vpm_data_copy(vp, off + on, n, uiop,
612 			    1, NULL, 0, S_READ);
613 		} else {
614 			base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
615 			    S_READ);
616 
617 			error = uiomove(base + on, n, UIO_READ, uiop);
618 		}
619 
620 		if (!error) {
621 			/*
622 			 * If read a whole block or read to eof,
623 			 * won't need this buffer again soon.
624 			 */
625 			mutex_enter(&rp->r_statelock);
626 			if (n + on == MAXBSIZE ||
627 			    uiop->uio_loffset == rp->r_size)
628 				flags = SM_DONTNEED;
629 			else
630 				flags = 0;
631 			mutex_exit(&rp->r_statelock);
632 			if (vpm_enable) {
633 				error = vpm_sync_pages(vp, off, n, flags);
634 			} else {
635 				error = segmap_release(segkmap, base, flags);
636 			}
637 		} else {
638 			if (vpm_enable) {
639 				(void) vpm_sync_pages(vp, off, n, 0);
640 			} else {
641 				(void) segmap_release(segkmap, base, 0);
642 			}
643 		}
644 	} while (!error && uiop->uio_resid > 0);
645 
646 	return (error);
647 }
648 
649 /* ARGSUSED */
650 static int
nfs3_write(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)651 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
652     caller_context_t *ct)
653 {
654 	rlim64_t limit = uiop->uio_llimit;
655 	rnode_t *rp;
656 	u_offset_t off;
657 	caddr_t base;
658 	uint_t flags;
659 	int remainder;
660 	size_t n;
661 	int on;
662 	int error;
663 	int resid;
664 	offset_t offset;
665 	mntinfo_t *mi;
666 	uint_t bsize;
667 
668 	rp = VTOR(vp);
669 
670 	if (vp->v_type != VREG)
671 		return (EISDIR);
672 
673 	mi = VTOMI(vp);
674 	if (nfs_zone() != mi->mi_zone)
675 		return (EIO);
676 	if (uiop->uio_resid == 0)
677 		return (0);
678 
679 	if (ioflag & FAPPEND) {
680 		struct vattr va;
681 
682 		/*
683 		 * Must serialize if appending.
684 		 */
685 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
686 			nfs_rw_exit(&rp->r_rwlock);
687 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
688 			    INTR(vp)))
689 				return (EINTR);
690 		}
691 
692 		va.va_mask = AT_SIZE;
693 		error = nfs3getattr(vp, &va, cr);
694 		if (error)
695 			return (error);
696 		uiop->uio_loffset = va.va_size;
697 	}
698 
699 	offset = uiop->uio_loffset + uiop->uio_resid;
700 
701 	if (uiop->uio_loffset < 0 || offset < 0)
702 		return (EINVAL);
703 
704 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
705 		limit = MAXOFFSET_T;
706 
707 	/*
708 	 * Check to make sure that the process will not exceed
709 	 * its limit on file size.  It is okay to write up to
710 	 * the limit, but not beyond.  Thus, the write which
711 	 * reaches the limit will be short and the next write
712 	 * will return an error.
713 	 */
714 	remainder = 0;
715 	if (offset > limit) {
716 		remainder = offset - limit;
717 		uiop->uio_resid = limit - uiop->uio_loffset;
718 		if (uiop->uio_resid <= 0) {
719 			proc_t *p = ttoproc(curthread);
720 
721 			uiop->uio_resid += remainder;
722 			mutex_enter(&p->p_lock);
723 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
724 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
725 			mutex_exit(&p->p_lock);
726 			return (EFBIG);
727 		}
728 	}
729 
730 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
731 		return (EINTR);
732 
733 	/*
734 	 * Bypass VM if caching has been disabled (e.g., locking) or if
735 	 * using client-side direct I/O and the file is not mmap'd and
736 	 * there are no cached pages.
737 	 */
738 	if ((vp->v_flag & VNOCACHE) ||
739 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
740 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
741 	    !vn_has_cached_data(vp))) {
742 		size_t bufsize;
743 		int count;
744 		u_offset_t org_offset;
745 		stable_how stab_comm;
746 
747 nfs3_fwrite:
748 		if (rp->r_flags & RSTALE) {
749 			resid = uiop->uio_resid;
750 			offset = uiop->uio_loffset;
751 			error = rp->r_error;
752 			/*
753 			 * A close may have cleared r_error, if so,
754 			 * propagate ESTALE error return properly
755 			 */
756 			if (error == 0)
757 				error = ESTALE;
758 			goto bottom;
759 		}
760 		bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
761 		base = kmem_alloc(bufsize, KM_SLEEP);
762 		do {
763 			if (ioflag & FDSYNC)
764 				stab_comm = DATA_SYNC;
765 			else
766 				stab_comm = FILE_SYNC;
767 			resid = uiop->uio_resid;
768 			offset = uiop->uio_loffset;
769 			count = MIN(uiop->uio_resid, bufsize);
770 			org_offset = uiop->uio_loffset;
771 			error = uiomove(base, count, UIO_WRITE, uiop);
772 			if (!error) {
773 				error = nfs3write(vp, base, org_offset,
774 				    count, cr, &stab_comm);
775 			}
776 		} while (!error && uiop->uio_resid > 0);
777 		kmem_free(base, bufsize);
778 		goto bottom;
779 	}
780 
781 
782 	bsize = vp->v_vfsp->vfs_bsize;
783 
784 	do {
785 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
786 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
787 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
788 
789 		resid = uiop->uio_resid;
790 		offset = uiop->uio_loffset;
791 
792 		if (rp->r_flags & RSTALE) {
793 			error = rp->r_error;
794 			/*
795 			 * A close may have cleared r_error, if so,
796 			 * propagate ESTALE error return properly
797 			 */
798 			if (error == 0)
799 				error = ESTALE;
800 			break;
801 		}
802 
803 		/*
804 		 * Don't create dirty pages faster than they
805 		 * can be cleaned so that the system doesn't
806 		 * get imbalanced.  If the async queue is
807 		 * maxed out, then wait for it to drain before
808 		 * creating more dirty pages.  Also, wait for
809 		 * any threads doing pagewalks in the vop_getattr
810 		 * entry points so that they don't block for
811 		 * long periods.
812 		 */
813 		mutex_enter(&rp->r_statelock);
814 		while ((mi->mi_max_threads != 0 &&
815 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
816 		    rp->r_gcount > 0) {
817 			if (INTR(vp)) {
818 				klwp_t *lwp = ttolwp(curthread);
819 
820 				if (lwp != NULL)
821 					lwp->lwp_nostop++;
822 				if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
823 					mutex_exit(&rp->r_statelock);
824 					if (lwp != NULL)
825 						lwp->lwp_nostop--;
826 					error = EINTR;
827 					goto bottom;
828 				}
829 				if (lwp != NULL)
830 					lwp->lwp_nostop--;
831 			} else
832 				cv_wait(&rp->r_cv, &rp->r_statelock);
833 		}
834 		mutex_exit(&rp->r_statelock);
835 
836 		/*
837 		 * Touch the page and fault it in if it is not in core
838 		 * before segmap_getmapflt or vpm_data_copy can lock it.
839 		 * This is to avoid the deadlock if the buffer is mapped
840 		 * to the same file through mmap which we want to write.
841 		 */
842 		uio_prefaultpages((long)n, uiop);
843 
844 		if (vpm_enable) {
845 			/*
846 			 * It will use kpm mappings, so no need to
847 			 * pass an address.
848 			 */
849 			error = writerp(rp, NULL, n, uiop, 0);
850 		} else  {
851 			if (segmap_kpm) {
852 				int pon = uiop->uio_loffset & PAGEOFFSET;
853 				size_t pn = MIN(PAGESIZE - pon,
854 				    uiop->uio_resid);
855 				int pagecreate;
856 
857 				mutex_enter(&rp->r_statelock);
858 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
859 				    uiop->uio_loffset + pn >= rp->r_size);
860 				mutex_exit(&rp->r_statelock);
861 
862 				base = segmap_getmapflt(segkmap, vp, off + on,
863 				    pn, !pagecreate, S_WRITE);
864 
865 				error = writerp(rp, base + pon, n, uiop,
866 				    pagecreate);
867 
868 			} else {
869 				base = segmap_getmapflt(segkmap, vp, off + on,
870 				    n, 0, S_READ);
871 				error = writerp(rp, base + on, n, uiop, 0);
872 			}
873 		}
874 
875 		if (!error) {
876 			if (mi->mi_flags & MI_NOAC)
877 				flags = SM_WRITE;
878 			else if ((uiop->uio_loffset % bsize) == 0 ||
879 			    IS_SWAPVP(vp)) {
880 				/*
881 				 * Have written a whole block.
882 				 * Start an asynchronous write
883 				 * and mark the buffer to
884 				 * indicate that it won't be
885 				 * needed again soon.
886 				 */
887 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
888 			} else
889 				flags = 0;
890 			if ((ioflag & (FSYNC|FDSYNC)) ||
891 			    (rp->r_flags & ROUTOFSPACE)) {
892 				flags &= ~SM_ASYNC;
893 				flags |= SM_WRITE;
894 			}
895 			if (vpm_enable) {
896 				error = vpm_sync_pages(vp, off, n, flags);
897 			} else {
898 				error = segmap_release(segkmap, base, flags);
899 			}
900 		} else {
901 			if (vpm_enable) {
902 				(void) vpm_sync_pages(vp, off, n, 0);
903 			} else {
904 				(void) segmap_release(segkmap, base, 0);
905 			}
906 			/*
907 			 * In the event that we got an access error while
908 			 * faulting in a page for a write-only file just
909 			 * force a write.
910 			 */
911 			if (error == EACCES)
912 				goto nfs3_fwrite;
913 		}
914 	} while (!error && uiop->uio_resid > 0);
915 
916 bottom:
917 	if (error) {
918 		uiop->uio_resid = resid + remainder;
919 		uiop->uio_loffset = offset;
920 	} else
921 		uiop->uio_resid += remainder;
922 
923 	nfs_rw_exit(&rp->r_lkserlock);
924 
925 	return (error);
926 }
927 
928 /*
929  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
930  */
931 static int
nfs3_rdwrlbn(vnode_t * vp,page_t * pp,u_offset_t off,size_t len,int flags,cred_t * cr)932 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
933     int flags, cred_t *cr)
934 {
935 	struct buf *bp;
936 	int error;
937 	page_t *savepp;
938 	uchar_t fsdata;
939 	stable_how stab_comm;
940 
941 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
942 	bp = pageio_setup(pp, len, vp, flags);
943 	ASSERT(bp != NULL);
944 
945 	/*
946 	 * pageio_setup should have set b_addr to 0.  This
947 	 * is correct since we want to do I/O on a page
948 	 * boundary.  bp_mapin will use this addr to calculate
949 	 * an offset, and then set b_addr to the kernel virtual
950 	 * address it allocated for us.
951 	 */
952 	ASSERT(bp->b_un.b_addr == 0);
953 
954 	bp->b_edev = 0;
955 	bp->b_dev = 0;
956 	bp->b_lblkno = lbtodb(off);
957 	bp->b_file = vp;
958 	bp->b_offset = (offset_t)off;
959 	bp_mapin(bp);
960 
961 	/*
962 	 * Calculate the desired level of stability to write data
963 	 * on the server and then mark all of the pages to reflect
964 	 * this.
965 	 */
966 	if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
967 	    freemem > desfree) {
968 		stab_comm = UNSTABLE;
969 		fsdata = C_DELAYCOMMIT;
970 	} else {
971 		stab_comm = FILE_SYNC;
972 		fsdata = C_NOCOMMIT;
973 	}
974 
975 	savepp = pp;
976 	do {
977 		pp->p_fsdata = fsdata;
978 	} while ((pp = pp->p_next) != savepp);
979 
980 	error = nfs3_bio(bp, &stab_comm, cr);
981 
982 	bp_mapout(bp);
983 	pageio_done(bp);
984 
985 	/*
986 	 * If the server wrote pages in a more stable fashion than
987 	 * was requested, then clear all of the marks in the pages
988 	 * indicating that COMMIT operations were required.
989 	 */
990 	if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) {
991 		do {
992 			pp->p_fsdata = C_NOCOMMIT;
993 		} while ((pp = pp->p_next) != savepp);
994 	}
995 
996 	return (error);
997 }
998 
999 /*
1000  * Write to file.  Writes to remote server in largest size
1001  * chunks that the server can handle.  Write is synchronous.
1002  */
1003 static int
nfs3write(vnode_t * vp,caddr_t base,u_offset_t offset,int count,cred_t * cr,stable_how * stab_comm)1004 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
1005     stable_how *stab_comm)
1006 {
1007 	mntinfo_t *mi;
1008 	WRITE3args args;
1009 	WRITE3res res;
1010 	int error;
1011 	int tsize;
1012 	rnode_t *rp;
1013 	int douprintf;
1014 
1015 	rp = VTOR(vp);
1016 	mi = VTOMI(vp);
1017 
1018 	ASSERT(nfs_zone() == mi->mi_zone);
1019 
1020 	args.file = *VTOFH3(vp);
1021 	args.stable = *stab_comm;
1022 
1023 	*stab_comm = FILE_SYNC;
1024 
1025 	douprintf = 1;
1026 
1027 	do {
1028 		if ((vp->v_flag & VNOCACHE) ||
1029 		    (rp->r_flags & RDIRECTIO) ||
1030 		    (mi->mi_flags & MI_DIRECTIO))
1031 			tsize = MIN(mi->mi_stsize, count);
1032 		else
1033 			tsize = MIN(mi->mi_curwrite, count);
1034 		args.offset = (offset3)offset;
1035 		args.count = (count3)tsize;
1036 		args.data.data_len = (uint_t)tsize;
1037 		args.data.data_val = base;
1038 
1039 		if (mi->mi_io_kstats) {
1040 			mutex_enter(&mi->mi_lock);
1041 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1042 			mutex_exit(&mi->mi_lock);
1043 		}
1044 		args.mblk = NULL;
1045 		do {
1046 			error = rfs3call(mi, NFSPROC3_WRITE,
1047 			    xdr_WRITE3args, (caddr_t)&args,
1048 			    xdr_WRITE3res, (caddr_t)&res, cr,
1049 			    &douprintf, &res.status, 0, NULL);
1050 		} while (error == ENFS_TRYAGAIN);
1051 		if (mi->mi_io_kstats) {
1052 			mutex_enter(&mi->mi_lock);
1053 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1054 			mutex_exit(&mi->mi_lock);
1055 		}
1056 
1057 		if (error)
1058 			return (error);
1059 		error = geterrno3(res.status);
1060 		if (!error) {
1061 			if (res.resok.count > args.count) {
1062 				zcmn_err(getzoneid(), CE_WARN,
1063 				    "nfs3write: server %s wrote %u, "
1064 				    "requested was %u",
1065 				    rp->r_server->sv_hostname,
1066 				    res.resok.count, args.count);
1067 				return (EIO);
1068 			}
1069 			if (res.resok.committed == UNSTABLE) {
1070 				*stab_comm = UNSTABLE;
1071 				if (args.stable == DATA_SYNC ||
1072 				    args.stable == FILE_SYNC) {
1073 					zcmn_err(getzoneid(), CE_WARN,
1074 			"nfs3write: server %s did not commit to stable storage",
1075 					    rp->r_server->sv_hostname);
1076 					return (EIO);
1077 				}
1078 			}
1079 			tsize = (int)res.resok.count;
1080 			count -= tsize;
1081 			base += tsize;
1082 			offset += tsize;
1083 			if (mi->mi_io_kstats) {
1084 				mutex_enter(&mi->mi_lock);
1085 				KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
1086 				KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
1087 				    tsize;
1088 				mutex_exit(&mi->mi_lock);
1089 			}
1090 			lwp_stat_update(LWP_STAT_OUBLK, 1);
1091 			mutex_enter(&rp->r_statelock);
1092 			if (rp->r_flags & RHAVEVERF) {
1093 				if (rp->r_verf != res.resok.verf) {
1094 					nfs3_set_mod(vp);
1095 					rp->r_verf = res.resok.verf;
1096 					/*
1097 					 * If the data was written UNSTABLE,
1098 					 * then might as well stop because
1099 					 * the whole block will have to get
1100 					 * rewritten anyway.
1101 					 */
1102 					if (*stab_comm == UNSTABLE) {
1103 						mutex_exit(&rp->r_statelock);
1104 						break;
1105 					}
1106 				}
1107 			} else {
1108 				rp->r_verf = res.resok.verf;
1109 				rp->r_flags |= RHAVEVERF;
1110 			}
1111 			/*
1112 			 * Mark the attribute cache as timed out and
1113 			 * set RWRITEATTR to indicate that the file
1114 			 * was modified with a WRITE operation and
1115 			 * that the attributes can not be trusted.
1116 			 */
1117 			PURGE_ATTRCACHE_LOCKED(rp);
1118 			rp->r_flags |= RWRITEATTR;
1119 			mutex_exit(&rp->r_statelock);
1120 		}
1121 	} while (!error && count);
1122 
1123 	return (error);
1124 }
1125 
1126 /*
1127  * Read from a file.  Reads data in largest chunks our interface can handle.
1128  */
1129 static int
nfs3read(vnode_t * vp,caddr_t base,offset_t offset,int count,size_t * residp,cred_t * cr)1130 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count, size_t *residp,
1131     cred_t *cr)
1132 {
1133 	mntinfo_t *mi;
1134 	READ3args args;
1135 	READ3vres res;
1136 	int tsize;
1137 	int error;
1138 	int douprintf;
1139 	failinfo_t fi;
1140 	rnode_t *rp;
1141 	struct vattr va;
1142 	hrtime_t t;
1143 
1144 	rp = VTOR(vp);
1145 	mi = VTOMI(vp);
1146 	ASSERT(nfs_zone() == mi->mi_zone);
1147 	douprintf = 1;
1148 
1149 	args.file = *VTOFH3(vp);
1150 	fi.vp = vp;
1151 	fi.fhp = (caddr_t)&args.file;
1152 	fi.copyproc = nfs3copyfh;
1153 	fi.lookupproc = nfs3lookup;
1154 	fi.xattrdirproc = acl_getxattrdir3;
1155 
1156 	res.pov.fres.vp = vp;
1157 	res.pov.fres.vap = &va;
1158 
1159 	res.wlist = NULL;
1160 	*residp = count;
1161 	do {
1162 		if (mi->mi_io_kstats) {
1163 			mutex_enter(&mi->mi_lock);
1164 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1165 			mutex_exit(&mi->mi_lock);
1166 		}
1167 
1168 		do {
1169 			if ((vp->v_flag & VNOCACHE) ||
1170 			    (rp->r_flags & RDIRECTIO) ||
1171 			    (mi->mi_flags & MI_DIRECTIO))
1172 				tsize = MIN(mi->mi_tsize, count);
1173 			else
1174 				tsize = MIN(mi->mi_curread, count);
1175 			res.data.data_val = base;
1176 			res.data.data_len = tsize;
1177 			args.offset = (offset3)offset;
1178 			args.count = (count3)tsize;
1179 			args.res_uiop = NULL;
1180 			args.res_data_val_alt = base;
1181 
1182 			t = gethrtime();
1183 			error = rfs3call(mi, NFSPROC3_READ,
1184 			    xdr_READ3args, (caddr_t)&args,
1185 			    xdr_READ3vres, (caddr_t)&res, cr,
1186 			    &douprintf, &res.status, 0, &fi);
1187 		} while (error == ENFS_TRYAGAIN);
1188 
1189 		if (mi->mi_io_kstats) {
1190 			mutex_enter(&mi->mi_lock);
1191 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1192 			mutex_exit(&mi->mi_lock);
1193 		}
1194 
1195 		if (error)
1196 			return (error);
1197 
1198 		error = geterrno3(res.status);
1199 		if (error)
1200 			return (error);
1201 
1202 		if (res.count != res.data.data_len) {
1203 			zcmn_err(getzoneid(), CE_WARN,
1204 			    "nfs3read: server %s returned incorrect amount",
1205 			    rp->r_server->sv_hostname);
1206 			return (EIO);
1207 		}
1208 
1209 		count -= res.count;
1210 		*residp = count;
1211 		base += res.count;
1212 		offset += res.count;
1213 		if (mi->mi_io_kstats) {
1214 			mutex_enter(&mi->mi_lock);
1215 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1216 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
1217 			mutex_exit(&mi->mi_lock);
1218 		}
1219 		lwp_stat_update(LWP_STAT_INBLK, 1);
1220 	} while (count && !res.eof);
1221 
1222 	if (res.pov.attributes) {
1223 		mutex_enter(&rp->r_statelock);
1224 		if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) {
1225 			mutex_exit(&rp->r_statelock);
1226 			PURGE_ATTRCACHE(vp);
1227 		} else {
1228 			if (rp->r_mtime <= t)
1229 				nfs_attrcache_va(vp, &va);
1230 			mutex_exit(&rp->r_statelock);
1231 		}
1232 	}
1233 
1234 	return (0);
1235 }
1236 
1237 /* ARGSUSED */
1238 static int
nfs3_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)1239 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1240     caller_context_t *ct)
1241 {
1242 
1243 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1244 		return (EIO);
1245 	switch (cmd) {
1246 		case _FIODIRECTIO:
1247 			return (nfs_directio(vp, (int)arg, cr));
1248 		default:
1249 			return (ENOTTY);
1250 	}
1251 }
1252 
1253 /* ARGSUSED */
1254 static int
nfs3_getattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)1255 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1256     caller_context_t *ct)
1257 {
1258 	int error;
1259 	rnode_t *rp;
1260 
1261 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1262 		return (EIO);
1263 	/*
1264 	 * If it has been specified that the return value will
1265 	 * just be used as a hint, and we are only being asked
1266 	 * for size, fsid or rdevid, then return the client's
1267 	 * notion of these values without checking to make sure
1268 	 * that the attribute cache is up to date.
1269 	 * The whole point is to avoid an over the wire GETATTR
1270 	 * call.
1271 	 */
1272 	rp = VTOR(vp);
1273 	if (flags & ATTR_HINT) {
1274 		if (vap->va_mask ==
1275 		    (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1276 			mutex_enter(&rp->r_statelock);
1277 			if (vap->va_mask | AT_SIZE)
1278 				vap->va_size = rp->r_size;
1279 			if (vap->va_mask | AT_FSID)
1280 				vap->va_fsid = rp->r_attr.va_fsid;
1281 			if (vap->va_mask | AT_RDEV)
1282 				vap->va_rdev = rp->r_attr.va_rdev;
1283 			mutex_exit(&rp->r_statelock);
1284 			return (0);
1285 		}
1286 	}
1287 
1288 	/*
1289 	 * Only need to flush pages if asking for the mtime
1290 	 * and if there any dirty pages or any outstanding
1291 	 * asynchronous (write) requests for this file.
1292 	 */
1293 	if (vap->va_mask & AT_MTIME) {
1294 		if (vn_has_cached_data(vp) &&
1295 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1296 			mutex_enter(&rp->r_statelock);
1297 			rp->r_gcount++;
1298 			mutex_exit(&rp->r_statelock);
1299 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1300 			mutex_enter(&rp->r_statelock);
1301 			if (error && (error == ENOSPC || error == EDQUOT)) {
1302 				if (!rp->r_error)
1303 					rp->r_error = error;
1304 			}
1305 			if (--rp->r_gcount == 0)
1306 				cv_broadcast(&rp->r_cv);
1307 			mutex_exit(&rp->r_statelock);
1308 		}
1309 	}
1310 
1311 	return (nfs3getattr(vp, vap, cr));
1312 }
1313 
1314 /*ARGSUSED4*/
1315 static int
nfs3_setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)1316 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1317     caller_context_t *ct)
1318 {
1319 	int error;
1320 	struct vattr va;
1321 
1322 	if (vap->va_mask & AT_NOSET)
1323 		return (EINVAL);
1324 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1325 		return (EIO);
1326 
1327 	va.va_mask = AT_UID | AT_MODE;
1328 	error = nfs3getattr(vp, &va, cr);
1329 	if (error)
1330 		return (error);
1331 
1332 	error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx,
1333 	    vp);
1334 	if (error)
1335 		return (error);
1336 
1337 	error = nfs3setattr(vp, vap, flags, cr);
1338 
1339 	if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
1340 		vnevent_truncate(vp, ct);
1341 
1342 	return (error);
1343 }
1344 
1345 static int
nfs3setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr)1346 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1347 {
1348 	int error;
1349 	uint_t mask;
1350 	SETATTR3args args;
1351 	SETATTR3res res;
1352 	int douprintf;
1353 	rnode_t *rp;
1354 	struct vattr va;
1355 	mode_t omode;
1356 	vsecattr_t *vsp;
1357 	hrtime_t t;
1358 
1359 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1360 	mask = vap->va_mask;
1361 
1362 	rp = VTOR(vp);
1363 
1364 	/*
1365 	 * Only need to flush pages if there are any pages and
1366 	 * if the file is marked as dirty in some fashion.  The
1367 	 * file must be flushed so that we can accurately
1368 	 * determine the size of the file and the cached data
1369 	 * after the SETATTR returns.  A file is considered to
1370 	 * be dirty if it is either marked with RDIRTY, has
1371 	 * outstanding i/o's active, or is mmap'd.  In this
1372 	 * last case, we can't tell whether there are dirty
1373 	 * pages, so we flush just to be sure.
1374 	 */
1375 	if (vn_has_cached_data(vp) &&
1376 	    ((rp->r_flags & RDIRTY) ||
1377 	    rp->r_count > 0 ||
1378 	    rp->r_mapcnt > 0)) {
1379 		ASSERT(vp->v_type != VCHR);
1380 		error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1381 		if (error && (error == ENOSPC || error == EDQUOT)) {
1382 			mutex_enter(&rp->r_statelock);
1383 			if (!rp->r_error)
1384 				rp->r_error = error;
1385 			mutex_exit(&rp->r_statelock);
1386 		}
1387 	}
1388 
1389 	args.object = *RTOFH3(rp);
1390 	/*
1391 	 * If the intent is for the server to set the times,
1392 	 * there is no point in have the mask indicating set mtime or
1393 	 * atime, because the vap values may be junk, and so result
1394 	 * in an overflow error. Remove these flags from the vap mask
1395 	 * before calling in this case, and restore them afterwards.
1396 	 */
1397 	if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) {
1398 		/* Use server times, so don't set the args time fields */
1399 		vap->va_mask &= ~(AT_ATIME | AT_MTIME);
1400 		error = vattr_to_sattr3(vap, &args.new_attributes);
1401 		vap->va_mask |= (mask & (AT_ATIME | AT_MTIME));
1402 		if (mask & AT_ATIME) {
1403 			args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
1404 		}
1405 		if (mask & AT_MTIME) {
1406 			args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
1407 		}
1408 	} else {
1409 		/* Either do not set times or use the client specified times */
1410 		error = vattr_to_sattr3(vap, &args.new_attributes);
1411 	}
1412 
1413 	if (error) {
1414 		/* req time field(s) overflow - return immediately */
1415 		return (error);
1416 	}
1417 
1418 	va.va_mask = AT_MODE | AT_CTIME;
1419 	error = nfs3getattr(vp, &va, cr);
1420 	if (error)
1421 		return (error);
1422 	omode = va.va_mode;
1423 
1424 tryagain:
1425 	if (mask & AT_SIZE) {
1426 		args.guard.check = TRUE;
1427 		args.guard.obj_ctime.seconds = va.va_ctime.tv_sec;
1428 		args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec;
1429 	} else
1430 		args.guard.check = FALSE;
1431 
1432 	douprintf = 1;
1433 
1434 	t = gethrtime();
1435 
1436 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
1437 	    xdr_SETATTR3args, (caddr_t)&args,
1438 	    xdr_SETATTR3res, (caddr_t)&res, cr,
1439 	    &douprintf, &res.status, 0, NULL);
1440 
1441 	/*
1442 	 * Purge the access cache and ACL cache if changing either the
1443 	 * owner of the file, the group owner, or the mode.  These may
1444 	 * change the access permissions of the file, so purge old
1445 	 * information and start over again.
1446 	 */
1447 	if (mask & (AT_UID | AT_GID | AT_MODE)) {
1448 		(void) nfs_access_purge_rp(rp);
1449 		if (rp->r_secattr != NULL) {
1450 			mutex_enter(&rp->r_statelock);
1451 			vsp = rp->r_secattr;
1452 			rp->r_secattr = NULL;
1453 			mutex_exit(&rp->r_statelock);
1454 			if (vsp != NULL)
1455 				nfs_acl_free(vsp);
1456 		}
1457 	}
1458 
1459 	if (error) {
1460 		PURGE_ATTRCACHE(vp);
1461 		return (error);
1462 	}
1463 
1464 	error = geterrno3(res.status);
1465 	if (!error) {
1466 		/*
1467 		 * If changing the size of the file, invalidate
1468 		 * any local cached data which is no longer part
1469 		 * of the file.  We also possibly invalidate the
1470 		 * last page in the file.  We could use
1471 		 * pvn_vpzero(), but this would mark the page as
1472 		 * modified and require it to be written back to
1473 		 * the server for no particularly good reason.
1474 		 * This way, if we access it, then we bring it
1475 		 * back in.  A read should be cheaper than a
1476 		 * write.
1477 		 */
1478 		if (mask & AT_SIZE) {
1479 			nfs_invalidate_pages(vp,
1480 			    (vap->va_size & PAGEMASK), cr);
1481 		}
1482 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
1483 		/*
1484 		 * Some servers will change the mode to clear the setuid
1485 		 * and setgid bits when changing the uid or gid.  The
1486 		 * client needs to compensate appropriately.
1487 		 */
1488 		if (mask & (AT_UID | AT_GID)) {
1489 			int terror;
1490 
1491 			va.va_mask = AT_MODE;
1492 			terror = nfs3getattr(vp, &va, cr);
1493 			if (!terror &&
1494 			    (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
1495 			    (!(mask & AT_MODE) && va.va_mode != omode))) {
1496 				va.va_mask = AT_MODE;
1497 				if (mask & AT_MODE)
1498 					va.va_mode = vap->va_mode;
1499 				else
1500 					va.va_mode = omode;
1501 				(void) nfs3setattr(vp, &va, 0, cr);
1502 			}
1503 		}
1504 	} else {
1505 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
1506 		/*
1507 		 * If we got back a "not synchronized" error, then
1508 		 * we need to retry with a new guard value.  The
1509 		 * guard value used is the change time.  If the
1510 		 * server returned post_op_attr, then we can just
1511 		 * retry because we have the latest attributes.
1512 		 * Otherwise, we issue a GETATTR to get the latest
1513 		 * attributes and then retry.  If we couldn't get
1514 		 * the attributes this way either, then we give
1515 		 * up because we can't complete the operation as
1516 		 * required.
1517 		 */
1518 		if (res.status == NFS3ERR_NOT_SYNC) {
1519 			va.va_mask = AT_CTIME;
1520 			if (nfs3getattr(vp, &va, cr) == 0)
1521 				goto tryagain;
1522 		}
1523 		PURGE_STALE_FH(error, vp, cr);
1524 	}
1525 
1526 	return (error);
1527 }
1528 
1529 static int
nfs3_accessx(void * vp,int mode,cred_t * cr)1530 nfs3_accessx(void *vp, int mode, cred_t *cr)
1531 {
1532 	ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1533 	return (nfs3_access(vp, mode, 0, cr, NULL));
1534 }
1535 
1536 /* ARGSUSED */
1537 static int
nfs3_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)1538 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1539 {
1540 	int error;
1541 	ACCESS3args args;
1542 	ACCESS3res res;
1543 	int douprintf;
1544 	uint32 acc;
1545 	rnode_t *rp;
1546 	cred_t *cred, *ncr, *ncrfree = NULL;
1547 	failinfo_t fi;
1548 	nfs_access_type_t cacc;
1549 	hrtime_t t;
1550 
1551 	acc = 0;
1552 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1553 		return (EIO);
1554 	if (mode & VREAD)
1555 		acc |= ACCESS3_READ;
1556 	if (mode & VWRITE) {
1557 		if (vn_is_readonly(vp) && !IS_DEVVP(vp))
1558 			return (EROFS);
1559 		if (vp->v_type == VDIR)
1560 			acc |= ACCESS3_DELETE;
1561 		acc |= ACCESS3_MODIFY | ACCESS3_EXTEND;
1562 	}
1563 	if (mode & VEXEC) {
1564 		if (vp->v_type == VDIR)
1565 			acc |= ACCESS3_LOOKUP;
1566 		else
1567 			acc |= ACCESS3_EXECUTE;
1568 	}
1569 
1570 	rp = VTOR(vp);
1571 	args.object = *VTOFH3(vp);
1572 	if (vp->v_type == VDIR) {
1573 		args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY |
1574 		    ACCESS3_EXTEND | ACCESS3_LOOKUP;
1575 	} else {
1576 		args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND |
1577 		    ACCESS3_EXECUTE;
1578 	}
1579 	fi.vp = vp;
1580 	fi.fhp = (caddr_t)&args.object;
1581 	fi.copyproc = nfs3copyfh;
1582 	fi.lookupproc = nfs3lookup;
1583 	fi.xattrdirproc = acl_getxattrdir3;
1584 
1585 	cred = cr;
1586 	/*
1587 	 * ncr and ncrfree both initially
1588 	 * point to the memory area returned
1589 	 * by crnetadjust();
1590 	 * ncrfree not NULL when exiting means
1591 	 * that we need to release it
1592 	 */
1593 	ncr = crnetadjust(cred);
1594 	ncrfree = ncr;
1595 tryagain:
1596 	if (rp->r_acache != NULL) {
1597 		cacc = nfs_access_check(rp, acc, cred);
1598 		if (cacc == NFS_ACCESS_ALLOWED) {
1599 			if (ncrfree != NULL)
1600 				crfree(ncrfree);
1601 			return (0);
1602 		}
1603 		if (cacc == NFS_ACCESS_DENIED) {
1604 			/*
1605 			 * If the cred can be adjusted, try again
1606 			 * with the new cred.
1607 			 */
1608 			if (ncr != NULL) {
1609 				cred = ncr;
1610 				ncr = NULL;
1611 				goto tryagain;
1612 			}
1613 			if (ncrfree != NULL)
1614 				crfree(ncrfree);
1615 			return (EACCES);
1616 		}
1617 	}
1618 
1619 	douprintf = 1;
1620 
1621 	t = gethrtime();
1622 
1623 	error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS,
1624 	    xdr_ACCESS3args, (caddr_t)&args,
1625 	    xdr_ACCESS3res, (caddr_t)&res, cred,
1626 	    &douprintf, &res.status, 0, &fi);
1627 
1628 	if (error) {
1629 		if (ncrfree != NULL)
1630 			crfree(ncrfree);
1631 		return (error);
1632 	}
1633 
1634 	error = geterrno3(res.status);
1635 	if (!error) {
1636 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1637 		nfs_access_cache(rp, args.access, res.resok.access, cred);
1638 		/*
1639 		 * we just cached results with cred; if cred is the
1640 		 * adjusted credentials from crnetadjust, we do not want
1641 		 * to release them before exiting: hence setting ncrfree
1642 		 * to NULL
1643 		 */
1644 		if (cred != cr)
1645 			ncrfree = NULL;
1646 		if ((acc & res.resok.access) != acc) {
1647 			/*
1648 			 * If the cred can be adjusted, try again
1649 			 * with the new cred.
1650 			 */
1651 			if (ncr != NULL) {
1652 				cred = ncr;
1653 				ncr = NULL;
1654 				goto tryagain;
1655 			}
1656 			error = EACCES;
1657 		}
1658 	} else {
1659 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1660 		PURGE_STALE_FH(error, vp, cr);
1661 	}
1662 
1663 	if (ncrfree != NULL)
1664 		crfree(ncrfree);
1665 
1666 	return (error);
1667 }
1668 
1669 static int nfs3_do_symlink_cache = 1;
1670 
1671 /* ARGSUSED */
1672 static int
nfs3_readlink(vnode_t * vp,struct uio * uiop,cred_t * cr,caller_context_t * ct)1673 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1674 {
1675 	int error;
1676 	READLINK3args args;
1677 	READLINK3res res;
1678 	nfspath3 resdata_backup;
1679 	rnode_t *rp;
1680 	int douprintf;
1681 	int len;
1682 	failinfo_t fi;
1683 	hrtime_t t;
1684 
1685 	/*
1686 	 * Can't readlink anything other than a symbolic link.
1687 	 */
1688 	if (vp->v_type != VLNK)
1689 		return (EINVAL);
1690 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1691 		return (EIO);
1692 
1693 	rp = VTOR(vp);
1694 	if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) {
1695 		error = nfs3_validate_caches(vp, cr);
1696 		if (error)
1697 			return (error);
1698 		mutex_enter(&rp->r_statelock);
1699 		if (rp->r_symlink.contents != NULL) {
1700 			error = uiomove(rp->r_symlink.contents,
1701 			    rp->r_symlink.len, UIO_READ, uiop);
1702 			mutex_exit(&rp->r_statelock);
1703 			return (error);
1704 		}
1705 		mutex_exit(&rp->r_statelock);
1706 	}
1707 
1708 	args.symlink = *VTOFH3(vp);
1709 	fi.vp = vp;
1710 	fi.fhp = (caddr_t)&args.symlink;
1711 	fi.copyproc = nfs3copyfh;
1712 	fi.lookupproc = nfs3lookup;
1713 	fi.xattrdirproc = acl_getxattrdir3;
1714 
1715 	res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1716 
1717 	resdata_backup = res.resok.data;
1718 
1719 	douprintf = 1;
1720 
1721 	t = gethrtime();
1722 
1723 	error = rfs3call(VTOMI(vp), NFSPROC3_READLINK,
1724 	    xdr_READLINK3args, (caddr_t)&args,
1725 	    xdr_READLINK3res, (caddr_t)&res, cr,
1726 	    &douprintf, &res.status, 0, &fi);
1727 
1728 	if (res.resok.data == nfs3nametoolong)
1729 		error = EINVAL;
1730 
1731 	if (error) {
1732 		kmem_free(resdata_backup, MAXPATHLEN);
1733 		return (error);
1734 	}
1735 
1736 	error = geterrno3(res.status);
1737 	if (!error) {
1738 		nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t,
1739 		    cr);
1740 		len = strlen(res.resok.data);
1741 		error = uiomove(res.resok.data, len, UIO_READ, uiop);
1742 		if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) {
1743 			mutex_enter(&rp->r_statelock);
1744 				if (rp->r_symlink.contents == NULL) {
1745 				rp->r_symlink.contents = res.resok.data;
1746 				rp->r_symlink.len = len;
1747 				rp->r_symlink.size = MAXPATHLEN;
1748 				mutex_exit(&rp->r_statelock);
1749 			} else {
1750 				mutex_exit(&rp->r_statelock);
1751 
1752 				kmem_free((void *)res.resok.data, MAXPATHLEN);
1753 			}
1754 		} else {
1755 			kmem_free((void *)res.resok.data, MAXPATHLEN);
1756 		}
1757 	} else {
1758 		nfs3_cache_post_op_attr(vp,
1759 		    &res.resfail.symlink_attributes, t, cr);
1760 		PURGE_STALE_FH(error, vp, cr);
1761 
1762 		kmem_free((void *)res.resok.data, MAXPATHLEN);
1763 
1764 	}
1765 
1766 	/*
1767 	 * The over the wire error for attempting to readlink something
1768 	 * other than a symbolic link is ENXIO.  However, we need to
1769 	 * return EINVAL instead of ENXIO, so we map it here.
1770 	 */
1771 	return (error == ENXIO ? EINVAL : error);
1772 }
1773 
1774 /*
1775  * Flush local dirty pages to stable storage on the server.
1776  *
1777  * If FNODSYNC is specified, then there is nothing to do because
1778  * metadata changes are not cached on the client before being
1779  * sent to the server.
1780  */
1781 /* ARGSUSED */
1782 static int
nfs3_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)1783 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1784 {
1785 	int error;
1786 
1787 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1788 		return (0);
1789 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1790 		return (EIO);
1791 
1792 	error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
1793 	if (!error)
1794 		error = VTOR(vp)->r_error;
1795 	return (error);
1796 }
1797 
1798 /*
1799  * Weirdness: if the file was removed or the target of a rename
1800  * operation while it was open, it got renamed instead.  Here we
1801  * remove the renamed file.
1802  */
1803 /* ARGSUSED */
1804 static void
nfs3_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)1805 nfs3_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1806 {
1807 	rnode_t *rp;
1808 
1809 	ASSERT(vp != DNLC_NO_VNODE);
1810 
1811 	/*
1812 	 * If this is coming from the wrong zone, we let someone in the right
1813 	 * zone take care of it asynchronously.  We can get here due to
1814 	 * VN_RELE() being called from pageout() or fsflush().  This call may
1815 	 * potentially turn into an expensive no-op if, for instance, v_count
1816 	 * gets incremented in the meantime, but it's still correct.
1817 	 */
1818 	if (nfs_zone() != VTOMI(vp)->mi_zone) {
1819 		nfs_async_inactive(vp, cr, nfs3_inactive);
1820 		return;
1821 	}
1822 
1823 	rp = VTOR(vp);
1824 redo:
1825 	if (rp->r_unldvp != NULL) {
1826 		/*
1827 		 * Save the vnode pointer for the directory where the
1828 		 * unlinked-open file got renamed, then set it to NULL
1829 		 * to prevent another thread from getting here before
1830 		 * we're done with the remove.  While we have the
1831 		 * statelock, make local copies of the pertinent rnode
1832 		 * fields.  If we weren't to do this in an atomic way, the
1833 		 * the unl* fields could become inconsistent with respect
1834 		 * to each other due to a race condition between this
1835 		 * code and nfs_remove().  See bug report 1034328.
1836 		 */
1837 		mutex_enter(&rp->r_statelock);
1838 		if (rp->r_unldvp != NULL) {
1839 			vnode_t *unldvp;
1840 			char *unlname;
1841 			cred_t *unlcred;
1842 			REMOVE3args args;
1843 			REMOVE3res res;
1844 			int douprintf;
1845 			int error;
1846 			hrtime_t t;
1847 
1848 			unldvp = rp->r_unldvp;
1849 			rp->r_unldvp = NULL;
1850 			unlname = rp->r_unlname;
1851 			rp->r_unlname = NULL;
1852 			unlcred = rp->r_unlcred;
1853 			rp->r_unlcred = NULL;
1854 			mutex_exit(&rp->r_statelock);
1855 
1856 			/*
1857 			 * If there are any dirty pages left, then flush
1858 			 * them.  This is unfortunate because they just
1859 			 * may get thrown away during the remove operation,
1860 			 * but we have to do this for correctness.
1861 			 */
1862 			if (vn_has_cached_data(vp) &&
1863 			    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1864 				ASSERT(vp->v_type != VCHR);
1865 				error = nfs3_putpage(vp, (offset_t)0, 0, 0,
1866 				    cr, ct);
1867 				if (error) {
1868 					mutex_enter(&rp->r_statelock);
1869 					if (!rp->r_error)
1870 						rp->r_error = error;
1871 					mutex_exit(&rp->r_statelock);
1872 				}
1873 			}
1874 
1875 			/*
1876 			 * Do the remove operation on the renamed file
1877 			 */
1878 			setdiropargs3(&args.object, unlname, unldvp);
1879 
1880 			douprintf = 1;
1881 
1882 			t = gethrtime();
1883 
1884 			error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE,
1885 			    xdr_diropargs3, (caddr_t)&args,
1886 			    xdr_REMOVE3res, (caddr_t)&res, unlcred,
1887 			    &douprintf, &res.status, 0, NULL);
1888 
1889 			if (error) {
1890 				PURGE_ATTRCACHE(unldvp);
1891 			} else {
1892 				error = geterrno3(res.status);
1893 				if (!error) {
1894 					nfs3_cache_wcc_data(unldvp,
1895 					    &res.resok.dir_wcc, t, cr);
1896 					if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1897 						nfs_purge_rddir_cache(unldvp);
1898 				} else {
1899 					nfs3_cache_wcc_data(unldvp,
1900 					    &res.resfail.dir_wcc, t, cr);
1901 					PURGE_STALE_FH(error, unldvp, cr);
1902 				}
1903 			}
1904 
1905 			/*
1906 			 * Release stuff held for the remove
1907 			 */
1908 			VN_RELE(unldvp);
1909 			kmem_free(unlname, MAXNAMELEN);
1910 			crfree(unlcred);
1911 			goto redo;
1912 		}
1913 		mutex_exit(&rp->r_statelock);
1914 	}
1915 
1916 	rp_addfree(rp, cr);
1917 }
1918 
1919 /*
1920  * Remote file system operations having to do with directory manipulation.
1921  */
1922 
1923 /* ARGSUSED */
1924 static int
nfs3_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)1925 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1926     int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1927     int *direntflags, pathname_t *realpnp)
1928 {
1929 	int error;
1930 	vnode_t *vp;
1931 	vnode_t *avp = NULL;
1932 	rnode_t *drp;
1933 
1934 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
1935 		return (EPERM);
1936 
1937 	drp = VTOR(dvp);
1938 
1939 	/*
1940 	 * Are we looking up extended attributes?  If so, "dvp" is
1941 	 * the file or directory for which we want attributes, and
1942 	 * we need a lookup of the hidden attribute directory
1943 	 * before we lookup the rest of the path.
1944 	 */
1945 	if (flags & LOOKUP_XATTR) {
1946 		bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1947 		mntinfo_t *mi;
1948 
1949 		mi = VTOMI(dvp);
1950 		if (!(mi->mi_flags & MI_EXTATTR))
1951 			return (EINVAL);
1952 
1953 		if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1954 			return (EINTR);
1955 
1956 		(void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1957 		if (avp == NULL)
1958 			error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0);
1959 		else
1960 			error = 0;
1961 
1962 		nfs_rw_exit(&drp->r_rwlock);
1963 
1964 		if (error) {
1965 			if (mi->mi_flags & MI_EXTATTR)
1966 				return (error);
1967 			return (EINVAL);
1968 		}
1969 		dvp = avp;
1970 		drp = VTOR(dvp);
1971 	}
1972 
1973 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1974 		error = EINTR;
1975 		goto out;
1976 	}
1977 
1978 	error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1979 
1980 	nfs_rw_exit(&drp->r_rwlock);
1981 
1982 	/*
1983 	 * If vnode is a device, create special vnode.
1984 	 */
1985 	if (!error && IS_DEVVP(*vpp)) {
1986 		vp = *vpp;
1987 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1988 		VN_RELE(vp);
1989 	}
1990 
1991 out:
1992 	if (avp != NULL)
1993 		VN_RELE(avp);
1994 
1995 	return (error);
1996 }
1997 
1998 static int nfs3_lookup_neg_cache = 1;
1999 
2000 #ifdef DEBUG
2001 static int nfs3_lookup_dnlc_hits = 0;
2002 static int nfs3_lookup_dnlc_misses = 0;
2003 static int nfs3_lookup_dnlc_neg_hits = 0;
2004 static int nfs3_lookup_dnlc_disappears = 0;
2005 static int nfs3_lookup_dnlc_lookups = 0;
2006 #endif
2007 
2008 /* ARGSUSED */
2009 int
nfs3lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,int rfscall_flags)2010 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
2011     int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
2012 {
2013 	int error;
2014 	rnode_t *drp;
2015 
2016 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2017 	/*
2018 	 * If lookup is for "", just return dvp.  Don't need
2019 	 * to send it over the wire, look it up in the dnlc,
2020 	 * or perform any access checks.
2021 	 */
2022 	if (*nm == '\0') {
2023 		VN_HOLD(dvp);
2024 		*vpp = dvp;
2025 		return (0);
2026 	}
2027 
2028 	/*
2029 	 * Can't do lookups in non-directories.
2030 	 */
2031 	if (dvp->v_type != VDIR)
2032 		return (ENOTDIR);
2033 
2034 	/*
2035 	 * If we're called with RFSCALL_SOFT, it's important that
2036 	 * the only rfscall is one we make directly; if we permit
2037 	 * an access call because we're looking up "." or validating
2038 	 * a dnlc hit, we'll deadlock because that rfscall will not
2039 	 * have the RFSCALL_SOFT set.
2040 	 */
2041 	if (rfscall_flags & RFSCALL_SOFT)
2042 		goto callit;
2043 
2044 	/*
2045 	 * If lookup is for ".", just return dvp.  Don't need
2046 	 * to send it over the wire or look it up in the dnlc,
2047 	 * just need to check access.
2048 	 */
2049 	if (strcmp(nm, ".") == 0) {
2050 		error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2051 		if (error)
2052 			return (error);
2053 		VN_HOLD(dvp);
2054 		*vpp = dvp;
2055 		return (0);
2056 	}
2057 
2058 	drp = VTOR(dvp);
2059 	if (!(drp->r_flags & RLOOKUP)) {
2060 		mutex_enter(&drp->r_statelock);
2061 		drp->r_flags |= RLOOKUP;
2062 		mutex_exit(&drp->r_statelock);
2063 	}
2064 
2065 	/*
2066 	 * Lookup this name in the DNLC.  If there was a valid entry,
2067 	 * then return the results of the lookup.
2068 	 */
2069 	error = nfs3lookup_dnlc(dvp, nm, vpp, cr);
2070 	if (error || *vpp != NULL)
2071 		return (error);
2072 
2073 callit:
2074 	error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags);
2075 
2076 	return (error);
2077 }
2078 
2079 static int
nfs3lookup_dnlc(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)2080 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
2081 {
2082 	int error;
2083 	vnode_t *vp;
2084 
2085 	ASSERT(*nm != '\0');
2086 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2087 	/*
2088 	 * Lookup this name in the DNLC.  If successful, then validate
2089 	 * the caches and then recheck the DNLC.  The DNLC is rechecked
2090 	 * just in case this entry got invalidated during the call
2091 	 * to nfs3_validate_caches.
2092 	 *
2093 	 * An assumption is being made that it is safe to say that a
2094 	 * file exists which may not on the server.  Any operations to
2095 	 * the server will fail with ESTALE.
2096 	 */
2097 #ifdef DEBUG
2098 	nfs3_lookup_dnlc_lookups++;
2099 #endif
2100 	vp = dnlc_lookup(dvp, nm);
2101 	if (vp != NULL) {
2102 		VN_RELE(vp);
2103 		if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
2104 			PURGE_ATTRCACHE(dvp);
2105 		}
2106 		error = nfs3_validate_caches(dvp, cr);
2107 		if (error)
2108 			return (error);
2109 		vp = dnlc_lookup(dvp, nm);
2110 		if (vp != NULL) {
2111 			error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2112 			if (error) {
2113 				VN_RELE(vp);
2114 				return (error);
2115 			}
2116 			if (vp == DNLC_NO_VNODE) {
2117 				VN_RELE(vp);
2118 #ifdef DEBUG
2119 				nfs3_lookup_dnlc_neg_hits++;
2120 #endif
2121 				return (ENOENT);
2122 			}
2123 			*vpp = vp;
2124 #ifdef DEBUG
2125 			nfs3_lookup_dnlc_hits++;
2126 #endif
2127 			return (0);
2128 		}
2129 #ifdef DEBUG
2130 		nfs3_lookup_dnlc_disappears++;
2131 #endif
2132 	}
2133 #ifdef DEBUG
2134 	else
2135 		nfs3_lookup_dnlc_misses++;
2136 #endif
2137 
2138 	*vpp = NULL;
2139 
2140 	return (0);
2141 }
2142 
2143 static int
nfs3lookup_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr,int rfscall_flags)2144 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
2145     int rfscall_flags)
2146 {
2147 	int error;
2148 	LOOKUP3args args;
2149 	LOOKUP3vres res;
2150 	int douprintf;
2151 	struct vattr vattr;
2152 	struct vattr dvattr;
2153 	vnode_t *vp;
2154 	failinfo_t fi;
2155 	hrtime_t t;
2156 
2157 	ASSERT(*nm != '\0');
2158 	ASSERT(dvp->v_type == VDIR);
2159 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2160 
2161 	setdiropargs3(&args.what, nm, dvp);
2162 
2163 	fi.vp = dvp;
2164 	fi.fhp = (caddr_t)&args.what.dir;
2165 	fi.copyproc = nfs3copyfh;
2166 	fi.lookupproc = nfs3lookup;
2167 	fi.xattrdirproc = acl_getxattrdir3;
2168 	res.obj_attributes.fres.vp = dvp;
2169 	res.obj_attributes.fres.vap = &vattr;
2170 	res.dir_attributes.fres.vp = dvp;
2171 	res.dir_attributes.fres.vap = &dvattr;
2172 
2173 	douprintf = 1;
2174 
2175 	t = gethrtime();
2176 
2177 	error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP,
2178 	    xdr_diropargs3, (caddr_t)&args,
2179 	    xdr_LOOKUP3vres, (caddr_t)&res, cr,
2180 	    &douprintf, &res.status, rfscall_flags, &fi);
2181 
2182 	if (error)
2183 		return (error);
2184 
2185 	nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr);
2186 
2187 	error = geterrno3(res.status);
2188 	if (error) {
2189 		PURGE_STALE_FH(error, dvp, cr);
2190 		if (error == ENOENT && nfs3_lookup_neg_cache)
2191 			dnlc_enter(dvp, nm, DNLC_NO_VNODE);
2192 		return (error);
2193 	}
2194 
2195 	if (res.obj_attributes.attributes) {
2196 		vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap,
2197 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2198 	} else {
2199 		vp = makenfs3node_va(&res.object, NULL,
2200 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2201 		if (vp->v_type == VNON) {
2202 			vattr.va_mask = AT_TYPE;
2203 			error = nfs3getattr(vp, &vattr, cr);
2204 			if (error) {
2205 				VN_RELE(vp);
2206 				return (error);
2207 			}
2208 			vp->v_type = vattr.va_type;
2209 		}
2210 	}
2211 
2212 	if (!(rfscall_flags & RFSCALL_SOFT))
2213 		dnlc_update(dvp, nm, vp);
2214 
2215 	*vpp = vp;
2216 
2217 	return (error);
2218 }
2219 
2220 #ifdef DEBUG
2221 static int nfs3_create_misses = 0;
2222 #endif
2223 
2224 /* ARGSUSED */
2225 static int
nfs3_create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int lfaware,caller_context_t * ct,vsecattr_t * vsecp)2226 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2227     int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
2228     vsecattr_t *vsecp)
2229 {
2230 	int error;
2231 	vnode_t *vp;
2232 	rnode_t *rp;
2233 	struct vattr vattr;
2234 	rnode_t *drp;
2235 	vnode_t *tempvp;
2236 
2237 	drp = VTOR(dvp);
2238 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2239 		return (EPERM);
2240 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2241 		return (EINTR);
2242 
2243 top:
2244 	/*
2245 	 * We make a copy of the attributes because the caller does not
2246 	 * expect us to change what va points to.
2247 	 */
2248 	vattr = *va;
2249 
2250 	/*
2251 	 * If the pathname is "", just use dvp.  Don't need
2252 	 * to send it over the wire, look it up in the dnlc,
2253 	 * or perform any access checks.
2254 	 */
2255 	if (*nm == '\0') {
2256 		error = 0;
2257 		VN_HOLD(dvp);
2258 		vp = dvp;
2259 	/*
2260 	 * If the pathname is ".", just use dvp.  Don't need
2261 	 * to send it over the wire or look it up in the dnlc,
2262 	 * just need to check access.
2263 	 */
2264 	} else if (strcmp(nm, ".") == 0) {
2265 		error = nfs3_access(dvp, VEXEC, 0, cr, ct);
2266 		if (error) {
2267 			nfs_rw_exit(&drp->r_rwlock);
2268 			return (error);
2269 		}
2270 		VN_HOLD(dvp);
2271 		vp = dvp;
2272 	/*
2273 	 * We need to go over the wire, just to be sure whether the
2274 	 * file exists or not.  Using the DNLC can be dangerous in
2275 	 * this case when making a decision regarding existence.
2276 	 */
2277 	} else {
2278 		error = nfs3lookup_otw(dvp, nm, &vp, cr, 0);
2279 	}
2280 	if (!error) {
2281 		if (exclusive == EXCL)
2282 			error = EEXIST;
2283 		else if (vp->v_type == VDIR && (mode & VWRITE))
2284 			error = EISDIR;
2285 		else {
2286 			/*
2287 			 * If vnode is a device, create special vnode.
2288 			 */
2289 			if (IS_DEVVP(vp)) {
2290 				tempvp = vp;
2291 				vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2292 				VN_RELE(tempvp);
2293 			}
2294 			if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2295 				if ((vattr.va_mask & AT_SIZE) &&
2296 				    vp->v_type == VREG) {
2297 					rp = VTOR(vp);
2298 					/*
2299 					 * Check here for large file handled
2300 					 * by LF-unaware process (as
2301 					 * ufs_create() does)
2302 					 */
2303 					if (!(lfaware & FOFFMAX)) {
2304 						mutex_enter(&rp->r_statelock);
2305 						if (rp->r_size > MAXOFF32_T)
2306 							error = EOVERFLOW;
2307 						mutex_exit(&rp->r_statelock);
2308 					}
2309 					if (!error) {
2310 						vattr.va_mask = AT_SIZE;
2311 						error = nfs3setattr(vp,
2312 						    &vattr, 0, cr);
2313 
2314 						/*
2315 						 * Existing file was truncated;
2316 						 * emit a create event.
2317 						 */
2318 						vnevent_create(vp, ct);
2319 					}
2320 				}
2321 			}
2322 		}
2323 		nfs_rw_exit(&drp->r_rwlock);
2324 		if (error) {
2325 			VN_RELE(vp);
2326 		} else {
2327 			*vpp = vp;
2328 		}
2329 
2330 		return (error);
2331 	}
2332 
2333 	dnlc_remove(dvp, nm);
2334 
2335 	/*
2336 	 * Decide what the group-id of the created file should be.
2337 	 * Set it in attribute list as advisory...
2338 	 */
2339 	error = setdirgid(dvp, &vattr.va_gid, cr);
2340 	if (error) {
2341 		nfs_rw_exit(&drp->r_rwlock);
2342 		return (error);
2343 	}
2344 	vattr.va_mask |= AT_GID;
2345 
2346 	ASSERT(vattr.va_mask & AT_TYPE);
2347 	if (vattr.va_type == VREG) {
2348 		ASSERT(vattr.va_mask & AT_MODE);
2349 		if (MANDMODE(vattr.va_mode)) {
2350 			nfs_rw_exit(&drp->r_rwlock);
2351 			return (EACCES);
2352 		}
2353 		error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr,
2354 		    lfaware);
2355 		/*
2356 		 * If this is not an exclusive create, then the CREATE
2357 		 * request will be made with the GUARDED mode set.  This
2358 		 * means that the server will return EEXIST if the file
2359 		 * exists.  The file could exist because of a retransmitted
2360 		 * request.  In this case, we recover by starting over and
2361 		 * checking to see whether the file exists.  This second
2362 		 * time through it should and a CREATE request will not be
2363 		 * sent.
2364 		 *
2365 		 * This handles the problem of a dangling CREATE request
2366 		 * which contains attributes which indicate that the file
2367 		 * should be truncated.  This retransmitted request could
2368 		 * possibly truncate valid data in the file if not caught
2369 		 * by the duplicate request mechanism on the server or if
2370 		 * not caught by other means.  The scenario is:
2371 		 *
2372 		 * Client transmits CREATE request with size = 0
2373 		 * Client times out, retransmits request.
2374 		 * Response to the first request arrives from the server
2375 		 *  and the client proceeds on.
2376 		 * Client writes data to the file.
2377 		 * The server now processes retransmitted CREATE request
2378 		 *  and truncates file.
2379 		 *
2380 		 * The use of the GUARDED CREATE request prevents this from
2381 		 * happening because the retransmitted CREATE would fail
2382 		 * with EEXIST and would not truncate the file.
2383 		 */
2384 		if (error == EEXIST && exclusive == NONEXCL) {
2385 #ifdef DEBUG
2386 			nfs3_create_misses++;
2387 #endif
2388 			goto top;
2389 		}
2390 		nfs_rw_exit(&drp->r_rwlock);
2391 		return (error);
2392 	}
2393 	error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
2394 	nfs_rw_exit(&drp->r_rwlock);
2395 	return (error);
2396 }
2397 
2398 /* ARGSUSED */
2399 static int
nfs3create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int lfaware)2400 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2401     int mode, vnode_t **vpp, cred_t *cr, int lfaware)
2402 {
2403 	int error;
2404 	CREATE3args args;
2405 	CREATE3res res;
2406 	int douprintf;
2407 	vnode_t *vp;
2408 	struct vattr vattr;
2409 	nfstime3 *verfp;
2410 	rnode_t *rp;
2411 	timestruc_t now;
2412 	hrtime_t t;
2413 
2414 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2415 	setdiropargs3(&args.where, nm, dvp);
2416 	if (exclusive == EXCL) {
2417 		args.how.mode = EXCLUSIVE;
2418 		/*
2419 		 * Construct the create verifier.  This verifier needs
2420 		 * to be unique between different clients.  It also needs
2421 		 * to vary for each exclusive create request generated
2422 		 * from the client to the server.
2423 		 *
2424 		 * The first attempt is made to use the hostid and a
2425 		 * unique number on the client.  If the hostid has not
2426 		 * been set, the high resolution time that the exclusive
2427 		 * create request is being made is used.  This will work
2428 		 * unless two different clients, both with the hostid
2429 		 * not set, attempt an exclusive create request on the
2430 		 * same file, at exactly the same clock time.  The
2431 		 * chances of this happening seem small enough to be
2432 		 * reasonable.
2433 		 */
2434 		verfp = (nfstime3 *)&args.how.createhow3_u.verf;
2435 		verfp->seconds = zone_get_hostid(NULL);
2436 		if (verfp->seconds != 0)
2437 			verfp->nseconds = newnum();
2438 		else {
2439 			gethrestime(&now);
2440 			verfp->seconds = now.tv_sec;
2441 			verfp->nseconds = now.tv_nsec;
2442 		}
2443 		/*
2444 		 * Since the server will use this value for the mtime,
2445 		 * make sure that it can't overflow. Zero out the MSB.
2446 		 * The actual value does not matter here, only its uniqeness.
2447 		 */
2448 		verfp->seconds %= INT32_MAX;
2449 	} else {
2450 		/*
2451 		 * Issue the non-exclusive create in guarded mode.  This
2452 		 * may result in some false EEXIST responses for
2453 		 * retransmitted requests, but these will be handled at
2454 		 * a higher level.  By using GUARDED, duplicate requests
2455 		 * to do file truncation and possible access problems
2456 		 * can be avoided.
2457 		 */
2458 		args.how.mode = GUARDED;
2459 		error = vattr_to_sattr3(va,
2460 		    &args.how.createhow3_u.obj_attributes);
2461 		if (error) {
2462 			/* req time field(s) overflow - return immediately */
2463 			return (error);
2464 		}
2465 	}
2466 
2467 	douprintf = 1;
2468 
2469 	t = gethrtime();
2470 
2471 	error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE,
2472 	    xdr_CREATE3args, (caddr_t)&args,
2473 	    xdr_CREATE3res, (caddr_t)&res, cr,
2474 	    &douprintf, &res.status, 0, NULL);
2475 
2476 	if (error) {
2477 		PURGE_ATTRCACHE(dvp);
2478 		return (error);
2479 	}
2480 
2481 	error = geterrno3(res.status);
2482 	if (!error) {
2483 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2484 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2485 			nfs_purge_rddir_cache(dvp);
2486 
2487 		/*
2488 		 * On exclusive create the times need to be explicitly
2489 		 * set to clear any potential verifier that may be stored
2490 		 * in one of these fields (see comment below).  This
2491 		 * is done here to cover the case where no post op attrs
2492 		 * were returned or a 'invalid' time was returned in
2493 		 * the attributes.
2494 		 */
2495 		if (exclusive == EXCL)
2496 			va->va_mask |= (AT_MTIME | AT_ATIME);
2497 
2498 		if (!res.resok.obj.handle_follows) {
2499 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2500 			if (error)
2501 				return (error);
2502 		} else {
2503 			if (res.resok.obj_attributes.attributes) {
2504 				vp = makenfs3node(&res.resok.obj.handle,
2505 				    &res.resok.obj_attributes.attr,
2506 				    dvp->v_vfsp, t, cr, NULL, NULL);
2507 			} else {
2508 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2509 				    dvp->v_vfsp, t, cr, NULL, NULL);
2510 
2511 				/*
2512 				 * On an exclusive create, it is possible
2513 				 * that attributes were returned but those
2514 				 * postop attributes failed to decode
2515 				 * properly.  If this is the case,
2516 				 * then most likely the atime or mtime
2517 				 * were invalid for our client; this
2518 				 * is caused by the server storing the
2519 				 * create verifier in one of the time
2520 				 * fields(most likely mtime).
2521 				 * So... we are going to setattr just the
2522 				 * atime/mtime to clear things up.
2523 				 */
2524 				if (exclusive == EXCL) {
2525 					if (error =
2526 					    nfs3excl_create_settimes(vp,
2527 					    va, cr)) {
2528 						/*
2529 						 * Setting the times failed.
2530 						 * Remove the file and return
2531 						 * the error.
2532 						 */
2533 						VN_RELE(vp);
2534 						(void) nfs3_remove(dvp,
2535 						    nm, cr, NULL, 0);
2536 						return (error);
2537 					}
2538 				}
2539 
2540 				/*
2541 				 * This handles the non-exclusive case
2542 				 * and the exclusive case where no post op
2543 				 * attrs were returned.
2544 				 */
2545 				if (vp->v_type == VNON) {
2546 					vattr.va_mask = AT_TYPE;
2547 					error = nfs3getattr(vp, &vattr, cr);
2548 					if (error) {
2549 						VN_RELE(vp);
2550 						return (error);
2551 					}
2552 					vp->v_type = vattr.va_type;
2553 				}
2554 			}
2555 			dnlc_update(dvp, nm, vp);
2556 		}
2557 
2558 		rp = VTOR(vp);
2559 
2560 		/*
2561 		 * Check here for large file handled by
2562 		 * LF-unaware process (as ufs_create() does)
2563 		 */
2564 		if ((va->va_mask & AT_SIZE) && vp->v_type == VREG &&
2565 		    !(lfaware & FOFFMAX)) {
2566 			mutex_enter(&rp->r_statelock);
2567 			if (rp->r_size > MAXOFF32_T) {
2568 				mutex_exit(&rp->r_statelock);
2569 				VN_RELE(vp);
2570 				return (EOVERFLOW);
2571 			}
2572 			mutex_exit(&rp->r_statelock);
2573 		}
2574 
2575 		if (exclusive == EXCL &&
2576 		    (va->va_mask & ~(AT_GID | AT_SIZE))) {
2577 			/*
2578 			 * If doing an exclusive create, then generate
2579 			 * a SETATTR to set the initial attributes.
2580 			 * Try to set the mtime and the atime to the
2581 			 * server's current time.  It is somewhat
2582 			 * expected that these fields will be used to
2583 			 * store the exclusive create cookie.  If not,
2584 			 * server implementors will need to know that
2585 			 * a SETATTR will follow an exclusive create
2586 			 * and the cookie should be destroyed if
2587 			 * appropriate. This work may have been done
2588 			 * earlier in this function if post op attrs
2589 			 * were not available.
2590 			 *
2591 			 * The AT_GID and AT_SIZE bits are turned off
2592 			 * so that the SETATTR request will not attempt
2593 			 * to process these.  The gid will be set
2594 			 * separately if appropriate.  The size is turned
2595 			 * off because it is assumed that a new file will
2596 			 * be created empty and if the file wasn't empty,
2597 			 * then the exclusive create will have failed
2598 			 * because the file must have existed already.
2599 			 * Therefore, no truncate operation is needed.
2600 			 */
2601 			va->va_mask &= ~(AT_GID | AT_SIZE);
2602 			error = nfs3setattr(vp, va, 0, cr);
2603 			if (error) {
2604 				/*
2605 				 * Couldn't correct the attributes of
2606 				 * the newly created file and the
2607 				 * attributes are wrong.  Remove the
2608 				 * file and return an error to the
2609 				 * application.
2610 				 */
2611 				VN_RELE(vp);
2612 				(void) nfs3_remove(dvp, nm, cr, NULL, 0);
2613 				return (error);
2614 			}
2615 		}
2616 
2617 		if (va->va_gid != rp->r_attr.va_gid) {
2618 			/*
2619 			 * If the gid on the file isn't right, then
2620 			 * generate a SETATTR to attempt to change
2621 			 * it.  This may or may not work, depending
2622 			 * upon the server's semantics for allowing
2623 			 * file ownership changes.
2624 			 */
2625 			va->va_mask = AT_GID;
2626 			(void) nfs3setattr(vp, va, 0, cr);
2627 		}
2628 
2629 		/*
2630 		 * If vnode is a device create special vnode
2631 		 */
2632 		if (IS_DEVVP(vp)) {
2633 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2634 			VN_RELE(vp);
2635 		} else
2636 			*vpp = vp;
2637 	} else {
2638 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2639 		PURGE_STALE_FH(error, dvp, cr);
2640 	}
2641 
2642 	return (error);
2643 }
2644 
2645 /*
2646  * Special setattr function to take care of rest of atime/mtime
2647  * after successful exclusive create.  This function exists to avoid
2648  * handling attributes from the server; exclusive the atime/mtime fields
2649  * may be 'invalid' in client's view and therefore can not be trusted.
2650  */
2651 static int
nfs3excl_create_settimes(vnode_t * vp,struct vattr * vap,cred_t * cr)2652 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr)
2653 {
2654 	int error;
2655 	uint_t mask;
2656 	SETATTR3args args;
2657 	SETATTR3res res;
2658 	int douprintf;
2659 	rnode_t *rp;
2660 	hrtime_t t;
2661 
2662 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
2663 	/* save the caller's mask so that it can be reset later */
2664 	mask = vap->va_mask;
2665 
2666 	rp = VTOR(vp);
2667 
2668 	args.object = *RTOFH3(rp);
2669 	args.guard.check = FALSE;
2670 
2671 	/* Use the mask to initialize the arguments */
2672 	vap->va_mask = 0;
2673 	error = vattr_to_sattr3(vap, &args.new_attributes);
2674 
2675 	/* We want to set just atime/mtime on this request */
2676 	args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2677 	args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2678 
2679 	douprintf = 1;
2680 
2681 	t = gethrtime();
2682 
2683 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
2684 	    xdr_SETATTR3args, (caddr_t)&args,
2685 	    xdr_SETATTR3res, (caddr_t)&res, cr,
2686 	    &douprintf, &res.status, 0, NULL);
2687 
2688 	if (error) {
2689 		vap->va_mask = mask;
2690 		return (error);
2691 	}
2692 
2693 	error = geterrno3(res.status);
2694 	if (!error) {
2695 		/*
2696 		 * It is important to pick up the attributes.
2697 		 * Since this is the exclusive create path, the
2698 		 * attributes on the initial create were ignored
2699 		 * and we need these to have the correct info.
2700 		 */
2701 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
2702 		/*
2703 		 * No need to do the atime/mtime work again so clear
2704 		 * the bits.
2705 		 */
2706 		mask &= ~(AT_ATIME | AT_MTIME);
2707 	} else {
2708 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
2709 	}
2710 
2711 	vap->va_mask = mask;
2712 
2713 	return (error);
2714 }
2715 
2716 /* ARGSUSED */
2717 static int
nfs3mknod(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr)2718 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2719     int mode, vnode_t **vpp, cred_t *cr)
2720 {
2721 	int error;
2722 	MKNOD3args args;
2723 	MKNOD3res res;
2724 	int douprintf;
2725 	vnode_t *vp;
2726 	struct vattr vattr;
2727 	hrtime_t t;
2728 
2729 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2730 	switch (va->va_type) {
2731 	case VCHR:
2732 	case VBLK:
2733 		setdiropargs3(&args.where, nm, dvp);
2734 		args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK;
2735 		error = vattr_to_sattr3(va,
2736 		    &args.what.mknoddata3_u.device.dev_attributes);
2737 		if (error) {
2738 			/* req time field(s) overflow - return immediately */
2739 			return (error);
2740 		}
2741 		args.what.mknoddata3_u.device.spec.specdata1 =
2742 		    getmajor(va->va_rdev);
2743 		args.what.mknoddata3_u.device.spec.specdata2 =
2744 		    getminor(va->va_rdev);
2745 		break;
2746 
2747 	case VFIFO:
2748 	case VSOCK:
2749 		setdiropargs3(&args.where, nm, dvp);
2750 		args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK;
2751 		error = vattr_to_sattr3(va,
2752 		    &args.what.mknoddata3_u.pipe_attributes);
2753 		if (error) {
2754 			/* req time field(s) overflow - return immediately */
2755 			return (error);
2756 		}
2757 		break;
2758 
2759 	default:
2760 		return (EINVAL);
2761 	}
2762 
2763 	douprintf = 1;
2764 
2765 	t = gethrtime();
2766 
2767 	error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD,
2768 	    xdr_MKNOD3args, (caddr_t)&args,
2769 	    xdr_MKNOD3res, (caddr_t)&res, cr,
2770 	    &douprintf, &res.status, 0, NULL);
2771 
2772 	if (error) {
2773 		PURGE_ATTRCACHE(dvp);
2774 		return (error);
2775 	}
2776 
2777 	error = geterrno3(res.status);
2778 	if (!error) {
2779 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2780 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2781 			nfs_purge_rddir_cache(dvp);
2782 
2783 		if (!res.resok.obj.handle_follows) {
2784 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2785 			if (error)
2786 				return (error);
2787 		} else {
2788 			if (res.resok.obj_attributes.attributes) {
2789 				vp = makenfs3node(&res.resok.obj.handle,
2790 				    &res.resok.obj_attributes.attr,
2791 				    dvp->v_vfsp, t, cr, NULL, NULL);
2792 			} else {
2793 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2794 				    dvp->v_vfsp, t, cr, NULL, NULL);
2795 				if (vp->v_type == VNON) {
2796 					vattr.va_mask = AT_TYPE;
2797 					error = nfs3getattr(vp, &vattr, cr);
2798 					if (error) {
2799 						VN_RELE(vp);
2800 						return (error);
2801 					}
2802 					vp->v_type = vattr.va_type;
2803 				}
2804 
2805 			}
2806 			dnlc_update(dvp, nm, vp);
2807 		}
2808 
2809 		if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
2810 			va->va_mask = AT_GID;
2811 			(void) nfs3setattr(vp, va, 0, cr);
2812 		}
2813 
2814 		/*
2815 		 * If vnode is a device create special vnode
2816 		 */
2817 		if (IS_DEVVP(vp)) {
2818 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2819 			VN_RELE(vp);
2820 		} else
2821 			*vpp = vp;
2822 	} else {
2823 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2824 		PURGE_STALE_FH(error, dvp, cr);
2825 	}
2826 	return (error);
2827 }
2828 
2829 /*
2830  * Weirdness: if the vnode to be removed is open
2831  * we rename it instead of removing it and nfs_inactive
2832  * will remove the new name.
2833  */
2834 /* ARGSUSED */
2835 static int
nfs3_remove(vnode_t * dvp,char * nm,cred_t * cr,caller_context_t * ct,int flags)2836 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2837 {
2838 	int error;
2839 	REMOVE3args args;
2840 	REMOVE3res res;
2841 	vnode_t *vp;
2842 	char *tmpname;
2843 	int douprintf;
2844 	rnode_t *rp;
2845 	rnode_t *drp;
2846 	hrtime_t t;
2847 
2848 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2849 		return (EPERM);
2850 	drp = VTOR(dvp);
2851 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2852 		return (EINTR);
2853 
2854 	error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2855 	if (error) {
2856 		nfs_rw_exit(&drp->r_rwlock);
2857 		return (error);
2858 	}
2859 
2860 	if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2861 		VN_RELE(vp);
2862 		nfs_rw_exit(&drp->r_rwlock);
2863 		return (EPERM);
2864 	}
2865 
2866 	/*
2867 	 * First just remove the entry from the name cache, as it
2868 	 * is most likely the only entry for this vp.
2869 	 */
2870 	dnlc_remove(dvp, nm);
2871 
2872 	/*
2873 	 * If the file has a v_count > 1 then there may be more than one
2874 	 * entry in the name cache due multiple links or an open file,
2875 	 * but we don't have the real reference count so flush all
2876 	 * possible entries.
2877 	 */
2878 	if (vp->v_count > 1)
2879 		dnlc_purge_vp(vp);
2880 
2881 	/*
2882 	 * Now we have the real reference count on the vnode
2883 	 */
2884 	rp = VTOR(vp);
2885 	mutex_enter(&rp->r_statelock);
2886 	if (vp->v_count > 1 &&
2887 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2888 		mutex_exit(&rp->r_statelock);
2889 		tmpname = newname();
2890 		error = nfs3rename(dvp, nm, dvp, tmpname, cr, ct);
2891 		if (error)
2892 			kmem_free(tmpname, MAXNAMELEN);
2893 		else {
2894 			mutex_enter(&rp->r_statelock);
2895 			if (rp->r_unldvp == NULL) {
2896 				VN_HOLD(dvp);
2897 				rp->r_unldvp = dvp;
2898 				if (rp->r_unlcred != NULL)
2899 					crfree(rp->r_unlcred);
2900 				crhold(cr);
2901 				rp->r_unlcred = cr;
2902 				rp->r_unlname = tmpname;
2903 			} else {
2904 				kmem_free(rp->r_unlname, MAXNAMELEN);
2905 				rp->r_unlname = tmpname;
2906 			}
2907 			mutex_exit(&rp->r_statelock);
2908 		}
2909 	} else {
2910 		mutex_exit(&rp->r_statelock);
2911 		/*
2912 		 * We need to flush any dirty pages which happen to
2913 		 * be hanging around before removing the file.  This
2914 		 * shouldn't happen very often and mostly on file
2915 		 * systems mounted "nocto".
2916 		 */
2917 		if (vn_has_cached_data(vp) &&
2918 		    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2919 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2920 			if (error && (error == ENOSPC || error == EDQUOT)) {
2921 				mutex_enter(&rp->r_statelock);
2922 				if (!rp->r_error)
2923 					rp->r_error = error;
2924 				mutex_exit(&rp->r_statelock);
2925 			}
2926 		}
2927 
2928 		setdiropargs3(&args.object, nm, dvp);
2929 
2930 		douprintf = 1;
2931 
2932 		t = gethrtime();
2933 
2934 		error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE,
2935 		    xdr_diropargs3, (caddr_t)&args,
2936 		    xdr_REMOVE3res, (caddr_t)&res, cr,
2937 		    &douprintf, &res.status, 0, NULL);
2938 
2939 		/*
2940 		 * The xattr dir may be gone after last attr is removed,
2941 		 * so flush it from dnlc.
2942 		 */
2943 		if (dvp->v_flag & V_XATTRDIR)
2944 			dnlc_purge_vp(dvp);
2945 
2946 		PURGE_ATTRCACHE(vp);
2947 
2948 		if (error) {
2949 			PURGE_ATTRCACHE(dvp);
2950 		} else {
2951 			error = geterrno3(res.status);
2952 			if (!error) {
2953 				nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t,
2954 				    cr);
2955 				if (HAVE_RDDIR_CACHE(drp))
2956 					nfs_purge_rddir_cache(dvp);
2957 			} else {
2958 				nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc,
2959 				    t, cr);
2960 				PURGE_STALE_FH(error, dvp, cr);
2961 			}
2962 		}
2963 	}
2964 
2965 	if (error == 0) {
2966 		vnevent_remove(vp, dvp, nm, ct);
2967 	}
2968 	VN_RELE(vp);
2969 
2970 	nfs_rw_exit(&drp->r_rwlock);
2971 
2972 	return (error);
2973 }
2974 
2975 /* ARGSUSED */
2976 static int
nfs3_link(vnode_t * tdvp,vnode_t * svp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)2977 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2978     caller_context_t *ct, int flags)
2979 {
2980 	int error;
2981 	LINK3args args;
2982 	LINK3res res;
2983 	vnode_t *realvp;
2984 	int douprintf;
2985 	mntinfo_t *mi;
2986 	rnode_t *tdrp;
2987 	hrtime_t t;
2988 
2989 	if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2990 		return (EPERM);
2991 	if (VOP_REALVP(svp, &realvp, ct) == 0)
2992 		svp = realvp;
2993 
2994 	mi = VTOMI(svp);
2995 
2996 	if (!(mi->mi_flags & MI_LINK))
2997 		return (EOPNOTSUPP);
2998 
2999 	args.file = *VTOFH3(svp);
3000 	setdiropargs3(&args.link, tnm, tdvp);
3001 
3002 	tdrp = VTOR(tdvp);
3003 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
3004 		return (EINTR);
3005 
3006 	dnlc_remove(tdvp, tnm);
3007 
3008 	douprintf = 1;
3009 
3010 	t = gethrtime();
3011 
3012 	error = rfs3call(mi, NFSPROC3_LINK,
3013 	    xdr_LINK3args, (caddr_t)&args,
3014 	    xdr_LINK3res, (caddr_t)&res, cr,
3015 	    &douprintf, &res.status, 0, NULL);
3016 
3017 	if (error) {
3018 		PURGE_ATTRCACHE(tdvp);
3019 		PURGE_ATTRCACHE(svp);
3020 		nfs_rw_exit(&tdrp->r_rwlock);
3021 		return (error);
3022 	}
3023 
3024 	error = geterrno3(res.status);
3025 
3026 	if (!error) {
3027 		nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr);
3028 		nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr);
3029 		if (HAVE_RDDIR_CACHE(tdrp))
3030 			nfs_purge_rddir_cache(tdvp);
3031 		dnlc_update(tdvp, tnm, svp);
3032 	} else {
3033 		nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t,
3034 		    cr);
3035 		nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr);
3036 		if (error == EOPNOTSUPP) {
3037 			mutex_enter(&mi->mi_lock);
3038 			mi->mi_flags &= ~MI_LINK;
3039 			mutex_exit(&mi->mi_lock);
3040 		}
3041 	}
3042 
3043 	nfs_rw_exit(&tdrp->r_rwlock);
3044 
3045 	if (!error) {
3046 		/*
3047 		 * Notify the source file of this link operation.
3048 		 */
3049 		vnevent_link(svp, ct);
3050 	}
3051 	return (error);
3052 }
3053 
3054 /* ARGSUSED */
3055 static int
nfs3_rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct,int flags)3056 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3057     caller_context_t *ct, int flags)
3058 {
3059 	vnode_t *realvp;
3060 
3061 	if (nfs_zone() != VTOMI(odvp)->mi_zone)
3062 		return (EPERM);
3063 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
3064 		ndvp = realvp;
3065 
3066 	return (nfs3rename(odvp, onm, ndvp, nnm, cr, ct));
3067 }
3068 
3069 /*
3070  * nfs3rename does the real work of renaming in NFS Version 3.
3071  */
3072 static int
nfs3rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct)3073 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3074     caller_context_t *ct)
3075 {
3076 	int error;
3077 	RENAME3args args;
3078 	RENAME3res res;
3079 	int douprintf;
3080 	vnode_t *nvp = NULL;
3081 	vnode_t *ovp = NULL;
3082 	char *tmpname;
3083 	rnode_t *rp;
3084 	rnode_t *odrp;
3085 	rnode_t *ndrp;
3086 	hrtime_t t;
3087 
3088 	ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
3089 
3090 	if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
3091 	    strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
3092 		return (EINVAL);
3093 
3094 	odrp = VTOR(odvp);
3095 	ndrp = VTOR(ndvp);
3096 	if ((intptr_t)odrp < (intptr_t)ndrp) {
3097 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
3098 			return (EINTR);
3099 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
3100 			nfs_rw_exit(&odrp->r_rwlock);
3101 			return (EINTR);
3102 		}
3103 	} else {
3104 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
3105 			return (EINTR);
3106 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
3107 			nfs_rw_exit(&ndrp->r_rwlock);
3108 			return (EINTR);
3109 		}
3110 	}
3111 
3112 	/*
3113 	 * Lookup the target file.  If it exists, it needs to be
3114 	 * checked to see whether it is a mount point and whether
3115 	 * it is active (open).
3116 	 */
3117 	error = nfs3lookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
3118 	if (!error) {
3119 		/*
3120 		 * If this file has been mounted on, then just
3121 		 * return busy because renaming to it would remove
3122 		 * the mounted file system from the name space.
3123 		 */
3124 		if (vn_mountedvfs(nvp) != NULL) {
3125 			VN_RELE(nvp);
3126 			nfs_rw_exit(&odrp->r_rwlock);
3127 			nfs_rw_exit(&ndrp->r_rwlock);
3128 			return (EBUSY);
3129 		}
3130 
3131 		/*
3132 		 * Purge the name cache of all references to this vnode
3133 		 * so that we can check the reference count to infer
3134 		 * whether it is active or not.
3135 		 */
3136 		/*
3137 		 * First just remove the entry from the name cache, as it
3138 		 * is most likely the only entry for this vp.
3139 		 */
3140 		dnlc_remove(ndvp, nnm);
3141 		/*
3142 		 * If the file has a v_count > 1 then there may be more
3143 		 * than one entry in the name cache due multiple links
3144 		 * or an open file, but we don't have the real reference
3145 		 * count so flush all possible entries.
3146 		 */
3147 		if (nvp->v_count > 1)
3148 			dnlc_purge_vp(nvp);
3149 
3150 		/*
3151 		 * If the vnode is active and is not a directory,
3152 		 * arrange to rename it to a
3153 		 * temporary file so that it will continue to be
3154 		 * accessible.  This implements the "unlink-open-file"
3155 		 * semantics for the target of a rename operation.
3156 		 * Before doing this though, make sure that the
3157 		 * source and target files are not already the same.
3158 		 */
3159 		if (nvp->v_count > 1 && nvp->v_type != VDIR) {
3160 			/*
3161 			 * Lookup the source name.
3162 			 */
3163 			error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL,
3164 			    cr, 0);
3165 
3166 			/*
3167 			 * The source name *should* already exist.
3168 			 */
3169 			if (error) {
3170 				VN_RELE(nvp);
3171 				nfs_rw_exit(&odrp->r_rwlock);
3172 				nfs_rw_exit(&ndrp->r_rwlock);
3173 				return (error);
3174 			}
3175 
3176 			/*
3177 			 * Compare the two vnodes.  If they are the same,
3178 			 * just release all held vnodes and return success.
3179 			 */
3180 			if (ovp == nvp) {
3181 				VN_RELE(ovp);
3182 				VN_RELE(nvp);
3183 				nfs_rw_exit(&odrp->r_rwlock);
3184 				nfs_rw_exit(&ndrp->r_rwlock);
3185 				return (0);
3186 			}
3187 
3188 			/*
3189 			 * Can't mix and match directories and non-
3190 			 * directories in rename operations.  We already
3191 			 * know that the target is not a directory.  If
3192 			 * the source is a directory, return an error.
3193 			 */
3194 			if (ovp->v_type == VDIR) {
3195 				VN_RELE(ovp);
3196 				VN_RELE(nvp);
3197 				nfs_rw_exit(&odrp->r_rwlock);
3198 				nfs_rw_exit(&ndrp->r_rwlock);
3199 				return (ENOTDIR);
3200 			}
3201 
3202 			/*
3203 			 * The target file exists, is not the same as
3204 			 * the source file, and is active.  Link it
3205 			 * to a temporary filename to avoid having
3206 			 * the server removing the file completely.
3207 			 */
3208 			tmpname = newname();
3209 			error = nfs3_link(ndvp, nvp, tmpname, cr, NULL, 0);
3210 			if (error == EOPNOTSUPP) {
3211 				error = nfs3_rename(ndvp, nnm, ndvp, tmpname,
3212 				    cr, NULL, 0);
3213 			}
3214 			if (error) {
3215 				kmem_free(tmpname, MAXNAMELEN);
3216 				VN_RELE(ovp);
3217 				VN_RELE(nvp);
3218 				nfs_rw_exit(&odrp->r_rwlock);
3219 				nfs_rw_exit(&ndrp->r_rwlock);
3220 				return (error);
3221 			}
3222 			rp = VTOR(nvp);
3223 			mutex_enter(&rp->r_statelock);
3224 			if (rp->r_unldvp == NULL) {
3225 				VN_HOLD(ndvp);
3226 				rp->r_unldvp = ndvp;
3227 				if (rp->r_unlcred != NULL)
3228 					crfree(rp->r_unlcred);
3229 				crhold(cr);
3230 				rp->r_unlcred = cr;
3231 				rp->r_unlname = tmpname;
3232 			} else {
3233 				kmem_free(rp->r_unlname, MAXNAMELEN);
3234 				rp->r_unlname = tmpname;
3235 			}
3236 			mutex_exit(&rp->r_statelock);
3237 		}
3238 	}
3239 
3240 	if (ovp == NULL) {
3241 		/*
3242 		 * When renaming directories to be a subdirectory of a
3243 		 * different parent, the dnlc entry for ".." will no
3244 		 * longer be valid, so it must be removed.
3245 		 *
3246 		 * We do a lookup here to determine whether we are renaming
3247 		 * a directory and we need to check if we are renaming
3248 		 * an unlinked file.  This might have already been done
3249 		 * in previous code, so we check ovp == NULL to avoid
3250 		 * doing it twice.
3251 		 */
3252 
3253 		error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
3254 		/*
3255 		 * The source name *should* already exist.
3256 		 */
3257 		if (error) {
3258 			nfs_rw_exit(&odrp->r_rwlock);
3259 			nfs_rw_exit(&ndrp->r_rwlock);
3260 			if (nvp) {
3261 				VN_RELE(nvp);
3262 			}
3263 			return (error);
3264 		}
3265 		ASSERT(ovp != NULL);
3266 	}
3267 
3268 	dnlc_remove(odvp, onm);
3269 	dnlc_remove(ndvp, nnm);
3270 
3271 	setdiropargs3(&args.from, onm, odvp);
3272 	setdiropargs3(&args.to, nnm, ndvp);
3273 
3274 	douprintf = 1;
3275 
3276 	t = gethrtime();
3277 
3278 	error = rfs3call(VTOMI(odvp), NFSPROC3_RENAME,
3279 	    xdr_RENAME3args, (caddr_t)&args,
3280 	    xdr_RENAME3res, (caddr_t)&res, cr,
3281 	    &douprintf, &res.status, 0, NULL);
3282 
3283 	if (error) {
3284 		PURGE_ATTRCACHE(odvp);
3285 		PURGE_ATTRCACHE(ndvp);
3286 		VN_RELE(ovp);
3287 		nfs_rw_exit(&odrp->r_rwlock);
3288 		nfs_rw_exit(&ndrp->r_rwlock);
3289 		if (nvp) {
3290 			VN_RELE(nvp);
3291 		}
3292 		return (error);
3293 	}
3294 
3295 	error = geterrno3(res.status);
3296 
3297 	if (!error) {
3298 		nfs3_cache_wcc_data(odvp, &res.resok.fromdir_wcc, t, cr);
3299 		if (HAVE_RDDIR_CACHE(odrp))
3300 			nfs_purge_rddir_cache(odvp);
3301 		if (ndvp != odvp) {
3302 			nfs3_cache_wcc_data(ndvp, &res.resok.todir_wcc, t, cr);
3303 			if (HAVE_RDDIR_CACHE(ndrp))
3304 				nfs_purge_rddir_cache(ndvp);
3305 		}
3306 		/*
3307 		 * when renaming directories to be a subdirectory of a
3308 		 * different parent, the dnlc entry for ".." will no
3309 		 * longer be valid, so it must be removed
3310 		 */
3311 		rp = VTOR(ovp);
3312 		if (ndvp != odvp) {
3313 			if (ovp->v_type == VDIR) {
3314 				dnlc_remove(ovp, "..");
3315 				if (HAVE_RDDIR_CACHE(rp))
3316 					nfs_purge_rddir_cache(ovp);
3317 			}
3318 		}
3319 
3320 		/*
3321 		 * If we are renaming the unlinked file, update the
3322 		 * r_unldvp and r_unlname as needed.
3323 		 */
3324 		mutex_enter(&rp->r_statelock);
3325 		if (rp->r_unldvp != NULL) {
3326 			if (strcmp(rp->r_unlname, onm) == 0) {
3327 				(void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
3328 				rp->r_unlname[MAXNAMELEN - 1] = '\0';
3329 
3330 				if (ndvp != rp->r_unldvp) {
3331 					VN_RELE(rp->r_unldvp);
3332 					rp->r_unldvp = ndvp;
3333 					VN_HOLD(ndvp);
3334 				}
3335 			}
3336 		}
3337 		mutex_exit(&rp->r_statelock);
3338 	} else {
3339 		nfs3_cache_wcc_data(odvp, &res.resfail.fromdir_wcc, t, cr);
3340 		if (ndvp != odvp) {
3341 			nfs3_cache_wcc_data(ndvp, &res.resfail.todir_wcc, t,
3342 			    cr);
3343 		}
3344 		/*
3345 		 * System V defines rename to return EEXIST, not
3346 		 * ENOTEMPTY if the target directory is not empty.
3347 		 * Over the wire, the error is NFSERR_ENOTEMPTY
3348 		 * which geterrno maps to ENOTEMPTY.
3349 		 */
3350 		if (error == ENOTEMPTY)
3351 			error = EEXIST;
3352 	}
3353 
3354 	if (error == 0) {
3355 		if (nvp)
3356 			vnevent_rename_dest(nvp, ndvp, nnm, ct);
3357 
3358 		if (odvp != ndvp)
3359 			vnevent_rename_dest_dir(ndvp, ct);
3360 		ASSERT(ovp != NULL);
3361 		vnevent_rename_src(ovp, odvp, onm, ct);
3362 	}
3363 
3364 	if (nvp) {
3365 		VN_RELE(nvp);
3366 	}
3367 	VN_RELE(ovp);
3368 
3369 	nfs_rw_exit(&odrp->r_rwlock);
3370 	nfs_rw_exit(&ndrp->r_rwlock);
3371 
3372 	return (error);
3373 }
3374 
3375 /* ARGSUSED */
3376 static int
nfs3_mkdir(vnode_t * dvp,char * nm,struct vattr * va,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)3377 nfs3_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
3378     caller_context_t *ct, int flags, vsecattr_t *vsecp)
3379 {
3380 	int error;
3381 	MKDIR3args args;
3382 	MKDIR3res res;
3383 	int douprintf;
3384 	struct vattr vattr;
3385 	vnode_t *vp;
3386 	rnode_t *drp;
3387 	hrtime_t t;
3388 
3389 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
3390 		return (EPERM);
3391 	setdiropargs3(&args.where, nm, dvp);
3392 
3393 	/*
3394 	 * Decide what the group-id and set-gid bit of the created directory
3395 	 * should be.  May have to do a setattr to get the gid right.
3396 	 */
3397 	error = setdirgid(dvp, &va->va_gid, cr);
3398 	if (error)
3399 		return (error);
3400 	error = setdirmode(dvp, &va->va_mode, cr);
3401 	if (error)
3402 		return (error);
3403 	va->va_mask |= AT_MODE|AT_GID;
3404 
3405 	error = vattr_to_sattr3(va, &args.attributes);
3406 	if (error) {
3407 		/* req time field(s) overflow - return immediately */
3408 		return (error);
3409 	}
3410 
3411 	drp = VTOR(dvp);
3412 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3413 		return (EINTR);
3414 
3415 	dnlc_remove(dvp, nm);
3416 
3417 	douprintf = 1;
3418 
3419 	t = gethrtime();
3420 
3421 	error = rfs3call(VTOMI(dvp), NFSPROC3_MKDIR,
3422 	    xdr_MKDIR3args, (caddr_t)&args,
3423 	    xdr_MKDIR3res, (caddr_t)&res, cr,
3424 	    &douprintf, &res.status, 0, NULL);
3425 
3426 	if (error) {
3427 		PURGE_ATTRCACHE(dvp);
3428 		nfs_rw_exit(&drp->r_rwlock);
3429 		return (error);
3430 	}
3431 
3432 	error = geterrno3(res.status);
3433 	if (!error) {
3434 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3435 		if (HAVE_RDDIR_CACHE(drp))
3436 			nfs_purge_rddir_cache(dvp);
3437 
3438 		if (!res.resok.obj.handle_follows) {
3439 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3440 			if (error) {
3441 				nfs_rw_exit(&drp->r_rwlock);
3442 				return (error);
3443 			}
3444 		} else {
3445 			if (res.resok.obj_attributes.attributes) {
3446 				vp = makenfs3node(&res.resok.obj.handle,
3447 				    &res.resok.obj_attributes.attr,
3448 				    dvp->v_vfsp, t, cr, NULL, NULL);
3449 			} else {
3450 				vp = makenfs3node(&res.resok.obj.handle, NULL,
3451 				    dvp->v_vfsp, t, cr, NULL, NULL);
3452 				if (vp->v_type == VNON) {
3453 					vattr.va_mask = AT_TYPE;
3454 					error = nfs3getattr(vp, &vattr, cr);
3455 					if (error) {
3456 						VN_RELE(vp);
3457 						nfs_rw_exit(&drp->r_rwlock);
3458 						return (error);
3459 					}
3460 					vp->v_type = vattr.va_type;
3461 				}
3462 			}
3463 			dnlc_update(dvp, nm, vp);
3464 		}
3465 		if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
3466 			va->va_mask = AT_GID;
3467 			(void) nfs3setattr(vp, va, 0, cr);
3468 		}
3469 		*vpp = vp;
3470 	} else {
3471 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3472 		PURGE_STALE_FH(error, dvp, cr);
3473 	}
3474 
3475 	nfs_rw_exit(&drp->r_rwlock);
3476 
3477 	return (error);
3478 }
3479 
3480 /* ARGSUSED */
3481 static int
nfs3_rmdir(vnode_t * dvp,char * nm,vnode_t * cdir,cred_t * cr,caller_context_t * ct,int flags)3482 nfs3_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
3483     caller_context_t *ct, int flags)
3484 {
3485 	int error;
3486 	RMDIR3args args;
3487 	RMDIR3res res;
3488 	vnode_t *vp;
3489 	int douprintf;
3490 	rnode_t *drp;
3491 	hrtime_t t;
3492 
3493 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
3494 		return (EPERM);
3495 	drp = VTOR(dvp);
3496 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3497 		return (EINTR);
3498 
3499 	/*
3500 	 * Attempt to prevent a rmdir(".") from succeeding.
3501 	 */
3502 	error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3503 	if (error) {
3504 		nfs_rw_exit(&drp->r_rwlock);
3505 		return (error);
3506 	}
3507 
3508 	if (vp == cdir) {
3509 		VN_RELE(vp);
3510 		nfs_rw_exit(&drp->r_rwlock);
3511 		return (EINVAL);
3512 	}
3513 
3514 	setdiropargs3(&args.object, nm, dvp);
3515 
3516 	/*
3517 	 * First just remove the entry from the name cache, as it
3518 	 * is most likely an entry for this vp.
3519 	 */
3520 	dnlc_remove(dvp, nm);
3521 
3522 	/*
3523 	 * If there vnode reference count is greater than one, then
3524 	 * there may be additional references in the DNLC which will
3525 	 * need to be purged.  First, trying removing the entry for
3526 	 * the parent directory and see if that removes the additional
3527 	 * reference(s).  If that doesn't do it, then use dnlc_purge_vp
3528 	 * to completely remove any references to the directory which
3529 	 * might still exist in the DNLC.
3530 	 */
3531 	if (vp->v_count > 1) {
3532 		dnlc_remove(vp, "..");
3533 		if (vp->v_count > 1)
3534 			dnlc_purge_vp(vp);
3535 	}
3536 
3537 	douprintf = 1;
3538 
3539 	t = gethrtime();
3540 
3541 	error = rfs3call(VTOMI(dvp), NFSPROC3_RMDIR,
3542 	    xdr_diropargs3, (caddr_t)&args,
3543 	    xdr_RMDIR3res, (caddr_t)&res, cr,
3544 	    &douprintf, &res.status, 0, NULL);
3545 
3546 	PURGE_ATTRCACHE(vp);
3547 
3548 	if (error) {
3549 		PURGE_ATTRCACHE(dvp);
3550 		VN_RELE(vp);
3551 		nfs_rw_exit(&drp->r_rwlock);
3552 		return (error);
3553 	}
3554 
3555 	error = geterrno3(res.status);
3556 	if (!error) {
3557 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3558 		if (HAVE_RDDIR_CACHE(drp))
3559 			nfs_purge_rddir_cache(dvp);
3560 		if (HAVE_RDDIR_CACHE(VTOR(vp)))
3561 			nfs_purge_rddir_cache(vp);
3562 	} else {
3563 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3564 		PURGE_STALE_FH(error, dvp, cr);
3565 		/*
3566 		 * System V defines rmdir to return EEXIST, not
3567 		 * ENOTEMPTY if the directory is not empty.  Over
3568 		 * the wire, the error is NFSERR_ENOTEMPTY which
3569 		 * geterrno maps to ENOTEMPTY.
3570 		 */
3571 		if (error == ENOTEMPTY)
3572 			error = EEXIST;
3573 	}
3574 
3575 	if (error == 0) {
3576 		vnevent_rmdir(vp, dvp, nm, ct);
3577 	}
3578 	VN_RELE(vp);
3579 
3580 	nfs_rw_exit(&drp->r_rwlock);
3581 
3582 	return (error);
3583 }
3584 
3585 /* ARGSUSED */
3586 static int
nfs3_symlink(vnode_t * dvp,char * lnm,struct vattr * tva,char * tnm,cred_t * cr,caller_context_t * ct,int flags)3587 nfs3_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
3588     caller_context_t *ct, int flags)
3589 {
3590 	int error;
3591 	SYMLINK3args args;
3592 	SYMLINK3res res;
3593 	int douprintf;
3594 	mntinfo_t *mi;
3595 	vnode_t *vp;
3596 	rnode_t *rp;
3597 	char *contents;
3598 	rnode_t *drp;
3599 	hrtime_t t;
3600 
3601 	mi = VTOMI(dvp);
3602 
3603 	if (nfs_zone() != mi->mi_zone)
3604 		return (EPERM);
3605 	if (!(mi->mi_flags & MI_SYMLINK))
3606 		return (EOPNOTSUPP);
3607 
3608 	setdiropargs3(&args.where, lnm, dvp);
3609 	error = vattr_to_sattr3(tva, &args.symlink.symlink_attributes);
3610 	if (error) {
3611 		/* req time field(s) overflow - return immediately */
3612 		return (error);
3613 	}
3614 	args.symlink.symlink_data = tnm;
3615 
3616 	drp = VTOR(dvp);
3617 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3618 		return (EINTR);
3619 
3620 	dnlc_remove(dvp, lnm);
3621 
3622 	douprintf = 1;
3623 
3624 	t = gethrtime();
3625 
3626 	error = rfs3call(mi, NFSPROC3_SYMLINK,
3627 	    xdr_SYMLINK3args, (caddr_t)&args,
3628 	    xdr_SYMLINK3res, (caddr_t)&res, cr,
3629 	    &douprintf, &res.status, 0, NULL);
3630 
3631 	if (error) {
3632 		PURGE_ATTRCACHE(dvp);
3633 		nfs_rw_exit(&drp->r_rwlock);
3634 		return (error);
3635 	}
3636 
3637 	error = geterrno3(res.status);
3638 	if (!error) {
3639 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3640 		if (HAVE_RDDIR_CACHE(drp))
3641 			nfs_purge_rddir_cache(dvp);
3642 
3643 		if (res.resok.obj.handle_follows) {
3644 			if (res.resok.obj_attributes.attributes) {
3645 				vp = makenfs3node(&res.resok.obj.handle,
3646 				    &res.resok.obj_attributes.attr,
3647 				    dvp->v_vfsp, t, cr, NULL, NULL);
3648 			} else {
3649 				vp = makenfs3node(&res.resok.obj.handle, NULL,
3650 				    dvp->v_vfsp, t, cr, NULL, NULL);
3651 				vp->v_type = VLNK;
3652 				vp->v_rdev = 0;
3653 			}
3654 			dnlc_update(dvp, lnm, vp);
3655 			rp = VTOR(vp);
3656 			if (nfs3_do_symlink_cache &&
3657 			    rp->r_symlink.contents == NULL) {
3658 
3659 				contents = kmem_alloc(MAXPATHLEN,
3660 				    KM_NOSLEEP);
3661 
3662 				if (contents != NULL) {
3663 					mutex_enter(&rp->r_statelock);
3664 					if (rp->r_symlink.contents == NULL) {
3665 						rp->r_symlink.len = strlen(tnm);
3666 						bcopy(tnm, contents,
3667 						    rp->r_symlink.len);
3668 						rp->r_symlink.contents =
3669 						    contents;
3670 						rp->r_symlink.size = MAXPATHLEN;
3671 						mutex_exit(&rp->r_statelock);
3672 					} else {
3673 						mutex_exit(&rp->r_statelock);
3674 						kmem_free((void *)contents,
3675 						    MAXPATHLEN);
3676 					}
3677 				}
3678 			}
3679 			VN_RELE(vp);
3680 		}
3681 	} else {
3682 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3683 		PURGE_STALE_FH(error, dvp, cr);
3684 		if (error == EOPNOTSUPP) {
3685 			mutex_enter(&mi->mi_lock);
3686 			mi->mi_flags &= ~MI_SYMLINK;
3687 			mutex_exit(&mi->mi_lock);
3688 		}
3689 	}
3690 
3691 	nfs_rw_exit(&drp->r_rwlock);
3692 
3693 	return (error);
3694 }
3695 
3696 #ifdef DEBUG
3697 static int nfs3_readdir_cache_hits = 0;
3698 static int nfs3_readdir_cache_shorts = 0;
3699 static int nfs3_readdir_cache_waits = 0;
3700 static int nfs3_readdir_cache_misses = 0;
3701 static int nfs3_readdir_readahead = 0;
3702 #endif
3703 
3704 static int nfs3_shrinkreaddir = 0;
3705 
3706 /*
3707  * Read directory entries.
3708  * There are some weird things to look out for here.  The uio_loffset
3709  * field is either 0 or it is the offset returned from a previous
3710  * readdir.  It is an opaque value used by the server to find the
3711  * correct directory block to read. The count field is the number
3712  * of blocks to read on the server.  This is advisory only, the server
3713  * may return only one block's worth of entries.  Entries may be compressed
3714  * on the server.
3715  */
3716 /* ARGSUSED */
3717 static int
nfs3_readdir(vnode_t * vp,struct uio * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)3718 nfs3_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
3719     caller_context_t *ct, int flags)
3720 {
3721 	int error;
3722 	size_t count;
3723 	rnode_t *rp;
3724 	rddir_cache *rdc;
3725 	rddir_cache *nrdc;
3726 	rddir_cache *rrdc;
3727 #ifdef DEBUG
3728 	int missed;
3729 #endif
3730 	int doreadahead;
3731 	rddir_cache srdc;
3732 	avl_index_t where;
3733 
3734 	if (nfs_zone() != VTOMI(vp)->mi_zone)
3735 		return (EIO);
3736 	rp = VTOR(vp);
3737 
3738 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
3739 
3740 	/*
3741 	 * Make sure that the directory cache is valid.
3742 	 */
3743 	if (HAVE_RDDIR_CACHE(rp)) {
3744 		if (nfs_disable_rddir_cache) {
3745 			/*
3746 			 * Setting nfs_disable_rddir_cache in /etc/system
3747 			 * allows interoperability with servers that do not
3748 			 * properly update the attributes of directories.
3749 			 * Any cached information gets purged before an
3750 			 * access is made to it.
3751 			 */
3752 			nfs_purge_rddir_cache(vp);
3753 		} else {
3754 			error = nfs3_validate_caches(vp, cr);
3755 			if (error)
3756 				return (error);
3757 		}
3758 	}
3759 
3760 	/*
3761 	 * It is possible that some servers may not be able to correctly
3762 	 * handle a large READDIR or READDIRPLUS request due to bugs in
3763 	 * their implementation.  In order to continue to interoperate
3764 	 * with them, this workaround is provided to limit the maximum
3765 	 * size of a READDIRPLUS request to 1024.  In any case, the request
3766 	 * size is limited to MAXBSIZE.
3767 	 */
3768 	count = MIN(uiop->uio_iov->iov_len,
3769 	    nfs3_shrinkreaddir ? 1024 : MAXBSIZE);
3770 
3771 	nrdc = NULL;
3772 #ifdef DEBUG
3773 	missed = 0;
3774 #endif
3775 top:
3776 	/*
3777 	 * Short circuit last readdir which always returns 0 bytes.
3778 	 * This can be done after the directory has been read through
3779 	 * completely at least once.  This will set r_direof which
3780 	 * can be used to find the value of the last cookie.
3781 	 */
3782 	mutex_enter(&rp->r_statelock);
3783 	if (rp->r_direof != NULL &&
3784 	    uiop->uio_loffset == rp->r_direof->nfs3_ncookie) {
3785 		mutex_exit(&rp->r_statelock);
3786 #ifdef DEBUG
3787 		nfs3_readdir_cache_shorts++;
3788 #endif
3789 		if (eofp)
3790 			*eofp = 1;
3791 		if (nrdc != NULL)
3792 			rddir_cache_rele(nrdc);
3793 		return (0);
3794 	}
3795 	/*
3796 	 * Look for a cache entry.  Cache entries are identified
3797 	 * by the NFS cookie value and the byte count requested.
3798 	 */
3799 	srdc.nfs3_cookie = uiop->uio_loffset;
3800 	srdc.buflen = count;
3801 	rdc = avl_find(&rp->r_dir, &srdc, &where);
3802 	if (rdc != NULL) {
3803 		rddir_cache_hold(rdc);
3804 		/*
3805 		 * If the cache entry is in the process of being
3806 		 * filled in, wait until this completes.  The
3807 		 * RDDIRWAIT bit is set to indicate that someone
3808 		 * is waiting and then the thread currently
3809 		 * filling the entry is done, it should do a
3810 		 * cv_broadcast to wakeup all of the threads
3811 		 * waiting for it to finish.
3812 		 */
3813 		if (rdc->flags & RDDIR) {
3814 			nfs_rw_exit(&rp->r_rwlock);
3815 			rdc->flags |= RDDIRWAIT;
3816 #ifdef DEBUG
3817 			nfs3_readdir_cache_waits++;
3818 #endif
3819 			if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3820 				/*
3821 				 * We got interrupted, probably
3822 				 * the user typed ^C or an alarm
3823 				 * fired.  We free the new entry
3824 				 * if we allocated one.
3825 				 */
3826 				mutex_exit(&rp->r_statelock);
3827 				(void) nfs_rw_enter_sig(&rp->r_rwlock,
3828 				    RW_READER, FALSE);
3829 				rddir_cache_rele(rdc);
3830 				if (nrdc != NULL)
3831 					rddir_cache_rele(nrdc);
3832 				return (EINTR);
3833 			}
3834 			mutex_exit(&rp->r_statelock);
3835 			(void) nfs_rw_enter_sig(&rp->r_rwlock,
3836 			    RW_READER, FALSE);
3837 			rddir_cache_rele(rdc);
3838 			goto top;
3839 		}
3840 		/*
3841 		 * Check to see if a readdir is required to
3842 		 * fill the entry.  If so, mark this entry
3843 		 * as being filled, remove our reference,
3844 		 * and branch to the code to fill the entry.
3845 		 */
3846 		if (rdc->flags & RDDIRREQ) {
3847 			rdc->flags &= ~RDDIRREQ;
3848 			rdc->flags |= RDDIR;
3849 			if (nrdc != NULL)
3850 				rddir_cache_rele(nrdc);
3851 			nrdc = rdc;
3852 			mutex_exit(&rp->r_statelock);
3853 			goto bottom;
3854 		}
3855 #ifdef DEBUG
3856 		if (!missed)
3857 			nfs3_readdir_cache_hits++;
3858 #endif
3859 		/*
3860 		 * If an error occurred while attempting
3861 		 * to fill the cache entry, just return it.
3862 		 */
3863 		if (rdc->error) {
3864 			error = rdc->error;
3865 			mutex_exit(&rp->r_statelock);
3866 			rddir_cache_rele(rdc);
3867 			if (nrdc != NULL)
3868 				rddir_cache_rele(nrdc);
3869 			return (error);
3870 		}
3871 
3872 		/*
3873 		 * The cache entry is complete and good,
3874 		 * copyout the dirent structs to the calling
3875 		 * thread.
3876 		 */
3877 		error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3878 
3879 		/*
3880 		 * If no error occurred during the copyout,
3881 		 * update the offset in the uio struct to
3882 		 * contain the value of the next cookie
3883 		 * and set the eof value appropriately.
3884 		 */
3885 		if (!error) {
3886 			uiop->uio_loffset = rdc->nfs3_ncookie;
3887 			if (eofp)
3888 				*eofp = rdc->eof;
3889 		}
3890 
3891 		/*
3892 		 * Decide whether to do readahead.
3893 		 *
3894 		 * Don't if have already read to the end of
3895 		 * directory.  There is nothing more to read.
3896 		 *
3897 		 * Don't if the application is not doing
3898 		 * lookups in the directory.  The readahead
3899 		 * is only effective if the application can
3900 		 * be doing work while an async thread is
3901 		 * handling the over the wire request.
3902 		 */
3903 		if (rdc->eof) {
3904 			rp->r_direof = rdc;
3905 			doreadahead = FALSE;
3906 		} else if (!(rp->r_flags & RLOOKUP))
3907 			doreadahead = FALSE;
3908 		else
3909 			doreadahead = TRUE;
3910 
3911 		if (!doreadahead) {
3912 			mutex_exit(&rp->r_statelock);
3913 			rddir_cache_rele(rdc);
3914 			if (nrdc != NULL)
3915 				rddir_cache_rele(nrdc);
3916 			return (error);
3917 		}
3918 
3919 		/*
3920 		 * Check to see whether we found an entry
3921 		 * for the readahead.  If so, we don't need
3922 		 * to do anything further, so free the new
3923 		 * entry if one was allocated.  Otherwise,
3924 		 * allocate a new entry, add it to the cache,
3925 		 * and then initiate an asynchronous readdir
3926 		 * operation to fill it.
3927 		 */
3928 		srdc.nfs3_cookie = rdc->nfs3_ncookie;
3929 		srdc.buflen = count;
3930 		rrdc = avl_find(&rp->r_dir, &srdc, &where);
3931 		if (rrdc != NULL) {
3932 			if (nrdc != NULL)
3933 				rddir_cache_rele(nrdc);
3934 		} else {
3935 			if (nrdc != NULL)
3936 				rrdc = nrdc;
3937 			else {
3938 				rrdc = rddir_cache_alloc(KM_NOSLEEP);
3939 			}
3940 			if (rrdc != NULL) {
3941 				rrdc->nfs3_cookie = rdc->nfs3_ncookie;
3942 				rrdc->buflen = count;
3943 				avl_insert(&rp->r_dir, rrdc, where);
3944 				rddir_cache_hold(rrdc);
3945 				mutex_exit(&rp->r_statelock);
3946 				rddir_cache_rele(rdc);
3947 #ifdef DEBUG
3948 				nfs3_readdir_readahead++;
3949 #endif
3950 				nfs_async_readdir(vp, rrdc, cr, do_nfs3readdir);
3951 				return (error);
3952 			}
3953 		}
3954 
3955 		mutex_exit(&rp->r_statelock);
3956 		rddir_cache_rele(rdc);
3957 		return (error);
3958 	}
3959 
3960 	/*
3961 	 * Didn't find an entry in the cache.  Construct a new empty
3962 	 * entry and link it into the cache.  Other processes attempting
3963 	 * to access this entry will need to wait until it is filled in.
3964 	 *
3965 	 * Since kmem_alloc may block, another pass through the cache
3966 	 * will need to be taken to make sure that another process
3967 	 * hasn't already added an entry to the cache for this request.
3968 	 */
3969 	if (nrdc == NULL) {
3970 		mutex_exit(&rp->r_statelock);
3971 		nrdc = rddir_cache_alloc(KM_SLEEP);
3972 		nrdc->nfs3_cookie = uiop->uio_loffset;
3973 		nrdc->buflen = count;
3974 		goto top;
3975 	}
3976 
3977 	/*
3978 	 * Add this entry to the cache.
3979 	 */
3980 	avl_insert(&rp->r_dir, nrdc, where);
3981 	rddir_cache_hold(nrdc);
3982 	mutex_exit(&rp->r_statelock);
3983 
3984 bottom:
3985 #ifdef DEBUG
3986 	missed = 1;
3987 	nfs3_readdir_cache_misses++;
3988 #endif
3989 	/*
3990 	 * Do the readdir.  This routine decides whether to use
3991 	 * READDIR or READDIRPLUS.
3992 	 */
3993 	error = do_nfs3readdir(vp, nrdc, cr);
3994 
3995 	/*
3996 	 * If this operation failed, just return the error which occurred.
3997 	 */
3998 	if (error != 0)
3999 		return (error);
4000 
4001 	/*
4002 	 * Since the RPC operation will have taken sometime and blocked
4003 	 * this process, another pass through the cache will need to be
4004 	 * taken to find the correct cache entry.  It is possible that
4005 	 * the correct cache entry will not be there (although one was
4006 	 * added) because the directory changed during the RPC operation
4007 	 * and the readdir cache was flushed.  In this case, just start
4008 	 * over.  It is hoped that this will not happen too often... :-)
4009 	 */
4010 	nrdc = NULL;
4011 	goto top;
4012 	/* NOTREACHED */
4013 }
4014 
4015 static int
do_nfs3readdir(vnode_t * vp,rddir_cache * rdc,cred_t * cr)4016 do_nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4017 {
4018 	int error;
4019 	rnode_t *rp;
4020 	mntinfo_t *mi;
4021 
4022 	rp = VTOR(vp);
4023 	mi = VTOMI(vp);
4024 	ASSERT(nfs_zone() == mi->mi_zone);
4025 	/*
4026 	 * Issue the proper request.
4027 	 *
4028 	 * If the server does not support READDIRPLUS, then use READDIR.
4029 	 *
4030 	 * Otherwise --
4031 	 * Issue a READDIRPLUS if reading to fill an empty cache or if
4032 	 * an application has performed a lookup in the directory which
4033 	 * required an over the wire lookup.  The use of READDIRPLUS
4034 	 * will help to (re)populate the DNLC.
4035 	 */
4036 	if (!(mi->mi_flags & MI_READDIRONLY) &&
4037 	    (rp->r_flags & (RLOOKUP | RREADDIRPLUS))) {
4038 		if (rp->r_flags & RREADDIRPLUS) {
4039 			mutex_enter(&rp->r_statelock);
4040 			rp->r_flags &= ~RREADDIRPLUS;
4041 			mutex_exit(&rp->r_statelock);
4042 		}
4043 		nfs3readdirplus(vp, rdc, cr);
4044 		if (rdc->error == EOPNOTSUPP)
4045 			nfs3readdir(vp, rdc, cr);
4046 	} else
4047 		nfs3readdir(vp, rdc, cr);
4048 
4049 	mutex_enter(&rp->r_statelock);
4050 	rdc->flags &= ~RDDIR;
4051 	if (rdc->flags & RDDIRWAIT) {
4052 		rdc->flags &= ~RDDIRWAIT;
4053 		cv_broadcast(&rdc->cv);
4054 	}
4055 	error = rdc->error;
4056 	if (error)
4057 		rdc->flags |= RDDIRREQ;
4058 	mutex_exit(&rp->r_statelock);
4059 
4060 	rddir_cache_rele(rdc);
4061 
4062 	return (error);
4063 }
4064 
4065 static void
nfs3readdir(vnode_t * vp,rddir_cache * rdc,cred_t * cr)4066 nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4067 {
4068 	int error;
4069 	READDIR3args args;
4070 	READDIR3vres res;
4071 	vattr_t dva;
4072 	rnode_t *rp;
4073 	int douprintf;
4074 	failinfo_t fi, *fip = NULL;
4075 	mntinfo_t *mi;
4076 	hrtime_t t;
4077 
4078 	rp = VTOR(vp);
4079 	mi = VTOMI(vp);
4080 	ASSERT(nfs_zone() == mi->mi_zone);
4081 
4082 	args.dir = *RTOFH3(rp);
4083 	args.cookie = (cookie3)rdc->nfs3_cookie;
4084 	args.cookieverf = (args.cookie == 0) ? 0 : rp->r_cookieverf;
4085 	args.count = rdc->buflen;
4086 
4087 	/*
4088 	 * NFS client failover support
4089 	 * suppress failover unless we have a zero cookie
4090 	 */
4091 	if (args.cookie == (cookie3) 0) {
4092 		fi.vp = vp;
4093 		fi.fhp = (caddr_t)&args.dir;
4094 		fi.copyproc = nfs3copyfh;
4095 		fi.lookupproc = nfs3lookup;
4096 		fi.xattrdirproc = acl_getxattrdir3;
4097 		fip = &fi;
4098 	}
4099 
4100 #ifdef DEBUG
4101 	rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4102 #else
4103 	rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4104 #endif
4105 
4106 	res.entries = (dirent64_t *)rdc->entries;
4107 	res.entries_size = rdc->buflen;
4108 	res.dir_attributes.fres.vap = &dva;
4109 	res.dir_attributes.fres.vp = vp;
4110 	res.loff = rdc->nfs3_cookie;
4111 
4112 	douprintf = 1;
4113 
4114 	if (mi->mi_io_kstats) {
4115 		mutex_enter(&mi->mi_lock);
4116 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4117 		mutex_exit(&mi->mi_lock);
4118 	}
4119 
4120 	t = gethrtime();
4121 
4122 	error = rfs3call(VTOMI(vp), NFSPROC3_READDIR,
4123 	    xdr_READDIR3args, (caddr_t)&args,
4124 	    xdr_READDIR3vres, (caddr_t)&res, cr,
4125 	    &douprintf, &res.status, 0, fip);
4126 
4127 	if (mi->mi_io_kstats) {
4128 		mutex_enter(&mi->mi_lock);
4129 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4130 		mutex_exit(&mi->mi_lock);
4131 	}
4132 
4133 	if (error)
4134 		goto err;
4135 
4136 	nfs3_cache_post_op_vattr(vp, &res.dir_attributes, t, cr);
4137 
4138 	error = geterrno3(res.status);
4139 	if (error) {
4140 		PURGE_STALE_FH(error, vp, cr);
4141 		goto err;
4142 	}
4143 
4144 	if (mi->mi_io_kstats) {
4145 		mutex_enter(&mi->mi_lock);
4146 		KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4147 		KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4148 		mutex_exit(&mi->mi_lock);
4149 	}
4150 
4151 	rdc->nfs3_ncookie = res.loff;
4152 	rp->r_cookieverf = res.cookieverf;
4153 	rdc->eof = res.eof ? 1 : 0;
4154 	rdc->entlen = res.size;
4155 	ASSERT(rdc->entlen <= rdc->buflen);
4156 	rdc->error = 0;
4157 	return;
4158 
4159 err:
4160 	kmem_free(rdc->entries, rdc->buflen);
4161 	rdc->entries = NULL;
4162 	rdc->error = error;
4163 }
4164 
4165 /*
4166  * Read directory entries.
4167  * There are some weird things to look out for here.  The uio_loffset
4168  * field is either 0 or it is the offset returned from a previous
4169  * readdir.  It is an opaque value used by the server to find the
4170  * correct directory block to read. The count field is the number
4171  * of blocks to read on the server.  This is advisory only, the server
4172  * may return only one block's worth of entries.  Entries may be compressed
4173  * on the server.
4174  */
4175 static void
nfs3readdirplus(vnode_t * vp,rddir_cache * rdc,cred_t * cr)4176 nfs3readdirplus(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4177 {
4178 	int error;
4179 	READDIRPLUS3args args;
4180 	READDIRPLUS3vres res;
4181 	vattr_t dva;
4182 	rnode_t *rp;
4183 	mntinfo_t *mi;
4184 	int douprintf;
4185 	failinfo_t fi, *fip = NULL;
4186 
4187 	rp = VTOR(vp);
4188 	mi = VTOMI(vp);
4189 	ASSERT(nfs_zone() == mi->mi_zone);
4190 
4191 	args.dir = *RTOFH3(rp);
4192 	args.cookie = (cookie3)rdc->nfs3_cookie;
4193 	args.cookieverf = (args.cookie == 0) ? 0 : rp->r_cookieverf;
4194 	args.dircount = rdc->buflen;
4195 	args.maxcount = mi->mi_tsize;
4196 
4197 	/*
4198 	 * NFS client failover support
4199 	 * suppress failover unless we have a zero cookie
4200 	 */
4201 	if (args.cookie == (cookie3)0) {
4202 		fi.vp = vp;
4203 		fi.fhp = (caddr_t)&args.dir;
4204 		fi.copyproc = nfs3copyfh;
4205 		fi.lookupproc = nfs3lookup;
4206 		fi.xattrdirproc = acl_getxattrdir3;
4207 		fip = &fi;
4208 	}
4209 
4210 #ifdef DEBUG
4211 	rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4212 #else
4213 	rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4214 #endif
4215 
4216 	res.entries = (dirent64_t *)rdc->entries;
4217 	res.entries_size = rdc->buflen;
4218 	res.dir_attributes.fres.vap = &dva;
4219 	res.dir_attributes.fres.vp = vp;
4220 	res.loff = rdc->nfs3_cookie;
4221 	res.credentials = cr;
4222 
4223 	douprintf = 1;
4224 
4225 	if (mi->mi_io_kstats) {
4226 		mutex_enter(&mi->mi_lock);
4227 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4228 		mutex_exit(&mi->mi_lock);
4229 	}
4230 
4231 	res.time = gethrtime();
4232 
4233 	error = rfs3call(mi, NFSPROC3_READDIRPLUS,
4234 	    xdr_READDIRPLUS3args, (caddr_t)&args,
4235 	    xdr_READDIRPLUS3vres, (caddr_t)&res, cr,
4236 	    &douprintf, &res.status, 0, fip);
4237 
4238 	if (mi->mi_io_kstats) {
4239 		mutex_enter(&mi->mi_lock);
4240 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4241 		mutex_exit(&mi->mi_lock);
4242 	}
4243 
4244 	if (error) {
4245 		goto err;
4246 	}
4247 
4248 	nfs3_cache_post_op_vattr(vp, &res.dir_attributes, res.time, cr);
4249 
4250 	error = geterrno3(res.status);
4251 	if (error) {
4252 		PURGE_STALE_FH(error, vp, cr);
4253 		if (error == EOPNOTSUPP) {
4254 			mutex_enter(&mi->mi_lock);
4255 			mi->mi_flags |= MI_READDIRONLY;
4256 			mutex_exit(&mi->mi_lock);
4257 		}
4258 		goto err;
4259 	}
4260 
4261 	if (mi->mi_io_kstats) {
4262 		mutex_enter(&mi->mi_lock);
4263 		KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4264 		KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4265 		mutex_exit(&mi->mi_lock);
4266 	}
4267 
4268 	rdc->nfs3_ncookie = res.loff;
4269 	rp->r_cookieverf = res.cookieverf;
4270 	rdc->eof = res.eof ? 1 : 0;
4271 	rdc->entlen = res.size;
4272 	ASSERT(rdc->entlen <= rdc->buflen);
4273 	rdc->error = 0;
4274 
4275 	return;
4276 
4277 err:
4278 	kmem_free(rdc->entries, rdc->buflen);
4279 	rdc->entries = NULL;
4280 	rdc->error = error;
4281 }
4282 
4283 #ifdef DEBUG
4284 static int nfs3_bio_do_stop = 0;
4285 #endif
4286 
4287 static int
nfs3_bio(struct buf * bp,stable_how * stab_comm,cred_t * cr)4288 nfs3_bio(struct buf *bp, stable_how *stab_comm, cred_t *cr)
4289 {
4290 	rnode_t *rp = VTOR(bp->b_vp);
4291 	int count;
4292 	int error;
4293 	cred_t *cred;
4294 	offset_t offset;
4295 
4296 	ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
4297 	offset = ldbtob(bp->b_lblkno);
4298 
4299 	DTRACE_IO1(start, struct buf *, bp);
4300 
4301 	if (bp->b_flags & B_READ) {
4302 		mutex_enter(&rp->r_statelock);
4303 		if (rp->r_cred != NULL) {
4304 			cred = rp->r_cred;
4305 			crhold(cred);
4306 		} else {
4307 			rp->r_cred = cr;
4308 			crhold(cr);
4309 			cred = cr;
4310 			crhold(cred);
4311 		}
4312 		mutex_exit(&rp->r_statelock);
4313 	read_again:
4314 		error = bp->b_error = nfs3read(bp->b_vp, bp->b_un.b_addr,
4315 		    offset, bp->b_bcount, &bp->b_resid, cred);
4316 		crfree(cred);
4317 		if (!error) {
4318 			if (bp->b_resid) {
4319 				/*
4320 				 * Didn't get it all because we hit EOF,
4321 				 * zero all the memory beyond the EOF.
4322 				 */
4323 				/* bzero(rdaddr + */
4324 				bzero(bp->b_un.b_addr +
4325 				    bp->b_bcount - bp->b_resid, bp->b_resid);
4326 			}
4327 			mutex_enter(&rp->r_statelock);
4328 			if (bp->b_resid == bp->b_bcount &&
4329 			    offset >= rp->r_size) {
4330 				/*
4331 				 * We didn't read anything at all as we are
4332 				 * past EOF.  Return an error indicator back
4333 				 * but don't destroy the pages (yet).
4334 				 */
4335 				error = NFS_EOF;
4336 			}
4337 			mutex_exit(&rp->r_statelock);
4338 		} else if (error == EACCES) {
4339 			mutex_enter(&rp->r_statelock);
4340 			if (cred != cr) {
4341 				if (rp->r_cred != NULL)
4342 					crfree(rp->r_cred);
4343 				rp->r_cred = cr;
4344 				crhold(cr);
4345 				cred = cr;
4346 				crhold(cred);
4347 				mutex_exit(&rp->r_statelock);
4348 				goto read_again;
4349 			}
4350 			mutex_exit(&rp->r_statelock);
4351 		}
4352 	} else {
4353 		if (!(rp->r_flags & RSTALE)) {
4354 			mutex_enter(&rp->r_statelock);
4355 			if (rp->r_cred != NULL) {
4356 				cred = rp->r_cred;
4357 				crhold(cred);
4358 			} else {
4359 				rp->r_cred = cr;
4360 				crhold(cr);
4361 				cred = cr;
4362 				crhold(cred);
4363 			}
4364 			mutex_exit(&rp->r_statelock);
4365 		write_again:
4366 			mutex_enter(&rp->r_statelock);
4367 			count = MIN(bp->b_bcount, rp->r_size - offset);
4368 			mutex_exit(&rp->r_statelock);
4369 			if (count < 0)
4370 				cmn_err(CE_PANIC, "nfs3_bio: write count < 0");
4371 #ifdef DEBUG
4372 			if (count == 0) {
4373 				zcmn_err(getzoneid(), CE_WARN,
4374 				    "nfs3_bio: zero length write at %lld",
4375 				    offset);
4376 				nfs_printfhandle(&rp->r_fh);
4377 				if (nfs3_bio_do_stop)
4378 					debug_enter("nfs3_bio");
4379 			}
4380 #endif
4381 			error = nfs3write(bp->b_vp, bp->b_un.b_addr, offset,
4382 			    count, cred, stab_comm);
4383 			if (error == EACCES) {
4384 				mutex_enter(&rp->r_statelock);
4385 				if (cred != cr) {
4386 					if (rp->r_cred != NULL)
4387 						crfree(rp->r_cred);
4388 					rp->r_cred = cr;
4389 					crhold(cr);
4390 					crfree(cred);
4391 					cred = cr;
4392 					crhold(cred);
4393 					mutex_exit(&rp->r_statelock);
4394 					goto write_again;
4395 				}
4396 				mutex_exit(&rp->r_statelock);
4397 			}
4398 			bp->b_error = error;
4399 			if (error && error != EINTR) {
4400 				/*
4401 				 * Don't print EDQUOT errors on the console.
4402 				 * Don't print asynchronous EACCES errors.
4403 				 * Don't print EFBIG errors.
4404 				 * Print all other write errors.
4405 				 */
4406 				if (error != EDQUOT && error != EFBIG &&
4407 				    (error != EACCES ||
4408 				    !(bp->b_flags & B_ASYNC)))
4409 					nfs_write_error(bp->b_vp, error, cred);
4410 				/*
4411 				 * Update r_error and r_flags as appropriate.
4412 				 * If the error was ESTALE, then mark the
4413 				 * rnode as not being writeable and save
4414 				 * the error status.  Otherwise, save any
4415 				 * errors which occur from asynchronous
4416 				 * page invalidations.  Any errors occurring
4417 				 * from other operations should be saved
4418 				 * by the caller.
4419 				 */
4420 				mutex_enter(&rp->r_statelock);
4421 				if (error == ESTALE) {
4422 					rp->r_flags |= RSTALE;
4423 					if (!rp->r_error)
4424 						rp->r_error = error;
4425 				} else if (!rp->r_error &&
4426 				    (bp->b_flags &
4427 				    (B_INVAL|B_FORCE|B_ASYNC)) ==
4428 				    (B_INVAL|B_FORCE|B_ASYNC)) {
4429 					rp->r_error = error;
4430 				}
4431 				mutex_exit(&rp->r_statelock);
4432 			}
4433 			crfree(cred);
4434 		} else {
4435 			error = rp->r_error;
4436 			/*
4437 			 * A close may have cleared r_error, if so,
4438 			 * propagate ESTALE error return properly
4439 			 */
4440 			if (error == 0)
4441 				error = ESTALE;
4442 		}
4443 	}
4444 
4445 	if (error != 0 && error != NFS_EOF)
4446 		bp->b_flags |= B_ERROR;
4447 
4448 	DTRACE_IO1(done, struct buf *, bp);
4449 
4450 	return (error);
4451 }
4452 
4453 /* ARGSUSED */
4454 static int
nfs3_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)4455 nfs3_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4456 {
4457 	rnode_t *rp;
4458 
4459 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4460 		return (EIO);
4461 	rp = VTOR(vp);
4462 
4463 	if (fidp->fid_len < (ushort_t)rp->r_fh.fh_len) {
4464 		fidp->fid_len = rp->r_fh.fh_len;
4465 		return (ENOSPC);
4466 	}
4467 	fidp->fid_len = rp->r_fh.fh_len;
4468 	bcopy(rp->r_fh.fh_buf, fidp->fid_data, fidp->fid_len);
4469 	return (0);
4470 }
4471 
4472 /* ARGSUSED2 */
4473 static int
nfs3_rwlock(vnode_t * vp,int write_lock,caller_context_t * ctp)4474 nfs3_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4475 {
4476 	rnode_t *rp = VTOR(vp);
4477 
4478 	if (!write_lock) {
4479 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4480 		return (V_WRITELOCK_FALSE);
4481 	}
4482 
4483 	if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
4484 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4485 		if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
4486 			return (V_WRITELOCK_FALSE);
4487 		nfs_rw_exit(&rp->r_rwlock);
4488 	}
4489 
4490 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
4491 	return (V_WRITELOCK_TRUE);
4492 }
4493 
4494 /* ARGSUSED */
4495 static void
nfs3_rwunlock(vnode_t * vp,int write_lock,caller_context_t * ctp)4496 nfs3_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4497 {
4498 	rnode_t *rp = VTOR(vp);
4499 
4500 	nfs_rw_exit(&rp->r_rwlock);
4501 }
4502 
4503 /* ARGSUSED */
4504 static int
nfs3_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)4505 nfs3_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
4506 {
4507 
4508 	/*
4509 	 * Because we stuff the readdir cookie into the offset field
4510 	 * someone may attempt to do an lseek with the cookie which
4511 	 * we want to succeed.
4512 	 */
4513 	if (vp->v_type == VDIR)
4514 		return (0);
4515 	if (*noffp < 0)
4516 		return (EINVAL);
4517 	return (0);
4518 }
4519 
4520 /*
4521  * number of nfs3_bsize blocks to read ahead.
4522  */
4523 static int nfs3_nra = 4;
4524 
4525 #ifdef DEBUG
4526 static int nfs3_lostpage = 0;	/* number of times we lost original page */
4527 #endif
4528 
4529 /*
4530  * Return all the pages from [off..off+len) in file
4531  */
4532 /* ARGSUSED */
4533 static int
nfs3_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)4534 nfs3_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4535     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4536     enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4537 {
4538 	rnode_t *rp;
4539 	int error;
4540 	mntinfo_t *mi;
4541 
4542 	if (vp->v_flag & VNOMAP)
4543 		return (ENOSYS);
4544 
4545 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4546 		return (EIO);
4547 	if (protp != NULL)
4548 		*protp = PROT_ALL;
4549 
4550 	/*
4551 	 * Now valididate that the caches are up to date.
4552 	 */
4553 	error = nfs3_validate_caches(vp, cr);
4554 	if (error)
4555 		return (error);
4556 
4557 	rp = VTOR(vp);
4558 	mi = VTOMI(vp);
4559 retry:
4560 	mutex_enter(&rp->r_statelock);
4561 
4562 	/*
4563 	 * Don't create dirty pages faster than they
4564 	 * can be cleaned so that the system doesn't
4565 	 * get imbalanced.  If the async queue is
4566 	 * maxed out, then wait for it to drain before
4567 	 * creating more dirty pages.  Also, wait for
4568 	 * any threads doing pagewalks in the vop_getattr
4569 	 * entry points so that they don't block for
4570 	 * long periods.
4571 	 */
4572 	if (rw == S_CREATE) {
4573 		while ((mi->mi_max_threads != 0 &&
4574 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
4575 		    rp->r_gcount > 0)
4576 			cv_wait(&rp->r_cv, &rp->r_statelock);
4577 	}
4578 
4579 	/*
4580 	 * If we are getting called as a side effect of an nfs_write()
4581 	 * operation the local file size might not be extended yet.
4582 	 * In this case we want to be able to return pages of zeroes.
4583 	 */
4584 	if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
4585 		mutex_exit(&rp->r_statelock);
4586 		return (EFAULT);		/* beyond EOF */
4587 	}
4588 
4589 	mutex_exit(&rp->r_statelock);
4590 
4591 	error = pvn_getpages(nfs3_getapage, vp, off, len, protp,
4592 	    pl, plsz, seg, addr, rw, cr);
4593 
4594 	switch (error) {
4595 	case NFS_EOF:
4596 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
4597 		goto retry;
4598 	case ESTALE:
4599 		PURGE_STALE_FH(error, vp, cr);
4600 	}
4601 
4602 	return (error);
4603 }
4604 
4605 /*
4606  * Called from pvn_getpages to get a particular page.
4607  */
4608 /* ARGSUSED */
4609 static int
nfs3_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)4610 nfs3_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
4611     page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4612     enum seg_rw rw, cred_t *cr)
4613 {
4614 	rnode_t *rp;
4615 	uint_t bsize;
4616 	struct buf *bp;
4617 	page_t *pp;
4618 	u_offset_t lbn;
4619 	u_offset_t io_off;
4620 	u_offset_t blkoff;
4621 	u_offset_t rablkoff;
4622 	size_t io_len;
4623 	uint_t blksize;
4624 	int error;
4625 	int readahead;
4626 	int readahead_issued = 0;
4627 	int ra_window; /* readahead window */
4628 	page_t *pagefound;
4629 	page_t *savepp;
4630 
4631 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4632 		return (EIO);
4633 	rp = VTOR(vp);
4634 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4635 
4636 reread:
4637 	bp = NULL;
4638 	pp = NULL;
4639 	pagefound = NULL;
4640 
4641 	if (pl != NULL)
4642 		pl[0] = NULL;
4643 
4644 	error = 0;
4645 	lbn = off / bsize;
4646 	blkoff = lbn * bsize;
4647 
4648 	/*
4649 	 * Queueing up the readahead before doing the synchronous read
4650 	 * results in a significant increase in read throughput because
4651 	 * of the increased parallelism between the async threads and
4652 	 * the process context.
4653 	 */
4654 	if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
4655 	    rw != S_CREATE &&
4656 	    !(vp->v_flag & VNOCACHE)) {
4657 		mutex_enter(&rp->r_statelock);
4658 
4659 		/*
4660 		 * Calculate the number of readaheads to do.
4661 		 * a) No readaheads at offset = 0.
4662 		 * b) Do maximum(nfs3_nra) readaheads when the readahead
4663 		 *    window is closed.
4664 		 * c) Do readaheads between 1 to (nfs3_nra - 1) depending
4665 		 *    upon how far the readahead window is open or close.
4666 		 * d) No readaheads if rp->r_nextr is not within the scope
4667 		 *    of the readahead window (random i/o).
4668 		 */
4669 
4670 		if (off == 0)
4671 			readahead = 0;
4672 		else if (blkoff == rp->r_nextr)
4673 			readahead = nfs3_nra;
4674 		else if (rp->r_nextr > blkoff &&
4675 		    ((ra_window = (rp->r_nextr - blkoff) / bsize)
4676 		    <= (nfs3_nra - 1)))
4677 			readahead = nfs3_nra - ra_window;
4678 		else
4679 			readahead = 0;
4680 
4681 		rablkoff = rp->r_nextr;
4682 		while (readahead > 0 && rablkoff + bsize < rp->r_size) {
4683 			mutex_exit(&rp->r_statelock);
4684 			if (nfs_async_readahead(vp, rablkoff + bsize,
4685 			    addr + (rablkoff + bsize - off), seg, cr,
4686 			    nfs3_readahead) < 0) {
4687 				mutex_enter(&rp->r_statelock);
4688 				break;
4689 			}
4690 			readahead--;
4691 			rablkoff += bsize;
4692 			/*
4693 			 * Indicate that we did a readahead so
4694 			 * readahead offset is not updated
4695 			 * by the synchronous read below.
4696 			 */
4697 			readahead_issued = 1;
4698 			mutex_enter(&rp->r_statelock);
4699 			/*
4700 			 * set readahead offset to
4701 			 * offset of last async readahead
4702 			 * request.
4703 			 */
4704 			rp->r_nextr = rablkoff;
4705 		}
4706 		mutex_exit(&rp->r_statelock);
4707 	}
4708 
4709 again:
4710 	if ((pagefound = page_exists(vp, off)) == NULL) {
4711 		if (pl == NULL) {
4712 			(void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
4713 			    nfs3_readahead);
4714 		} else if (rw == S_CREATE) {
4715 			/*
4716 			 * Block for this page is not allocated, or the offset
4717 			 * is beyond the current allocation size, or we're
4718 			 * allocating a swap slot and the page was not found,
4719 			 * so allocate it and return a zero page.
4720 			 */
4721 			if ((pp = page_create_va(vp, off,
4722 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL)
4723 				cmn_err(CE_PANIC, "nfs3_getapage: page_create");
4724 			io_len = PAGESIZE;
4725 			mutex_enter(&rp->r_statelock);
4726 			rp->r_nextr = off + PAGESIZE;
4727 			mutex_exit(&rp->r_statelock);
4728 		} else {
4729 			/*
4730 			 * Need to go to server to get a BLOCK, exception to
4731 			 * that being while reading at offset = 0 or doing
4732 			 * random i/o, in that case read only a PAGE.
4733 			 */
4734 			mutex_enter(&rp->r_statelock);
4735 			if (blkoff < rp->r_size &&
4736 			    blkoff + bsize >= rp->r_size) {
4737 				/*
4738 				 * If only a block or less is left in
4739 				 * the file, read all that is remaining.
4740 				 */
4741 				if (rp->r_size <= off) {
4742 					/*
4743 					 * Trying to access beyond EOF,
4744 					 * set up to get at least one page.
4745 					 */
4746 					blksize = off + PAGESIZE - blkoff;
4747 				} else
4748 					blksize = rp->r_size - blkoff;
4749 			} else if ((off == 0) ||
4750 			    (off != rp->r_nextr && !readahead_issued)) {
4751 				blksize = PAGESIZE;
4752 				blkoff = off; /* block = page here */
4753 			} else
4754 				blksize = bsize;
4755 			mutex_exit(&rp->r_statelock);
4756 
4757 			pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4758 			    &io_len, blkoff, blksize, 0);
4759 
4760 			/*
4761 			 * Some other thread has entered the page,
4762 			 * so just use it.
4763 			 */
4764 			if (pp == NULL)
4765 				goto again;
4766 
4767 			/*
4768 			 * Now round the request size up to page boundaries.
4769 			 * This ensures that the entire page will be
4770 			 * initialized to zeroes if EOF is encountered.
4771 			 */
4772 			io_len = ptob(btopr(io_len));
4773 
4774 			bp = pageio_setup(pp, io_len, vp, B_READ);
4775 			ASSERT(bp != NULL);
4776 
4777 			/*
4778 			 * pageio_setup should have set b_addr to 0.  This
4779 			 * is correct since we want to do I/O on a page
4780 			 * boundary.  bp_mapin will use this addr to calculate
4781 			 * an offset, and then set b_addr to the kernel virtual
4782 			 * address it allocated for us.
4783 			 */
4784 			ASSERT(bp->b_un.b_addr == 0);
4785 
4786 			bp->b_edev = 0;
4787 			bp->b_dev = 0;
4788 			bp->b_lblkno = lbtodb(io_off);
4789 			bp->b_file = vp;
4790 			bp->b_offset = (offset_t)off;
4791 			bp_mapin(bp);
4792 
4793 			/*
4794 			 * If doing a write beyond what we believe is EOF,
4795 			 * don't bother trying to read the pages from the
4796 			 * server, we'll just zero the pages here.  We
4797 			 * don't check that the rw flag is S_WRITE here
4798 			 * because some implementations may attempt a
4799 			 * read access to the buffer before copying data.
4800 			 */
4801 			mutex_enter(&rp->r_statelock);
4802 			if (io_off >= rp->r_size && seg == segkmap) {
4803 				mutex_exit(&rp->r_statelock);
4804 				bzero(bp->b_un.b_addr, io_len);
4805 			} else {
4806 				mutex_exit(&rp->r_statelock);
4807 				error = nfs3_bio(bp, NULL, cr);
4808 			}
4809 
4810 			/*
4811 			 * Unmap the buffer before freeing it.
4812 			 */
4813 			bp_mapout(bp);
4814 			pageio_done(bp);
4815 
4816 			savepp = pp;
4817 			do {
4818 				pp->p_fsdata = C_NOCOMMIT;
4819 			} while ((pp = pp->p_next) != savepp);
4820 
4821 			if (error == NFS_EOF) {
4822 				/*
4823 				 * If doing a write system call just return
4824 				 * zeroed pages, else user tried to get pages
4825 				 * beyond EOF, return error.  We don't check
4826 				 * that the rw flag is S_WRITE here because
4827 				 * some implementations may attempt a read
4828 				 * access to the buffer before copying data.
4829 				 */
4830 				if (seg == segkmap)
4831 					error = 0;
4832 				else
4833 					error = EFAULT;
4834 			}
4835 
4836 			if (!readahead_issued && !error) {
4837 				mutex_enter(&rp->r_statelock);
4838 				rp->r_nextr = io_off + io_len;
4839 				mutex_exit(&rp->r_statelock);
4840 			}
4841 		}
4842 	}
4843 
4844 	if (pl == NULL)
4845 		return (error);
4846 
4847 	if (error) {
4848 		if (pp != NULL)
4849 			pvn_read_done(pp, B_ERROR);
4850 		return (error);
4851 	}
4852 
4853 	if (pagefound) {
4854 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
4855 
4856 		/*
4857 		 * Page exists in the cache, acquire the appropriate lock.
4858 		 * If this fails, start all over again.
4859 		 */
4860 		if ((pp = page_lookup(vp, off, se)) == NULL) {
4861 #ifdef DEBUG
4862 			nfs3_lostpage++;
4863 #endif
4864 			goto reread;
4865 		}
4866 		pl[0] = pp;
4867 		pl[1] = NULL;
4868 		return (0);
4869 	}
4870 
4871 	if (pp != NULL)
4872 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4873 
4874 	return (error);
4875 }
4876 
4877 static void
nfs3_readahead(vnode_t * vp,u_offset_t blkoff,caddr_t addr,struct seg * seg,cred_t * cr)4878 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
4879     cred_t *cr)
4880 {
4881 	int error;
4882 	page_t *pp;
4883 	u_offset_t io_off;
4884 	size_t io_len;
4885 	struct buf *bp;
4886 	uint_t bsize, blksize;
4887 	rnode_t *rp = VTOR(vp);
4888 	page_t *savepp;
4889 
4890 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4891 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4892 
4893 	mutex_enter(&rp->r_statelock);
4894 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
4895 		/*
4896 		 * If less than a block left in file read less
4897 		 * than a block.
4898 		 */
4899 		blksize = rp->r_size - blkoff;
4900 	} else
4901 		blksize = bsize;
4902 	mutex_exit(&rp->r_statelock);
4903 
4904 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
4905 	    &io_off, &io_len, blkoff, blksize, 1);
4906 	/*
4907 	 * The isra flag passed to the kluster function is 1, we may have
4908 	 * gotten a return value of NULL for a variety of reasons (# of free
4909 	 * pages < minfree, someone entered the page on the vnode etc). In all
4910 	 * cases, we want to punt on the readahead.
4911 	 */
4912 	if (pp == NULL)
4913 		return;
4914 
4915 	/*
4916 	 * Now round the request size up to page boundaries.
4917 	 * This ensures that the entire page will be
4918 	 * initialized to zeroes if EOF is encountered.
4919 	 */
4920 	io_len = ptob(btopr(io_len));
4921 
4922 	bp = pageio_setup(pp, io_len, vp, B_READ);
4923 	ASSERT(bp != NULL);
4924 
4925 	/*
4926 	 * pageio_setup should have set b_addr to 0.  This is correct since
4927 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4928 	 * to calculate an offset, and then set b_addr to the kernel virtual
4929 	 * address it allocated for us.
4930 	 */
4931 	ASSERT(bp->b_un.b_addr == 0);
4932 
4933 	bp->b_edev = 0;
4934 	bp->b_dev = 0;
4935 	bp->b_lblkno = lbtodb(io_off);
4936 	bp->b_file = vp;
4937 	bp->b_offset = (offset_t)blkoff;
4938 	bp_mapin(bp);
4939 
4940 	/*
4941 	 * If doing a write beyond what we believe is EOF, don't bother trying
4942 	 * to read the pages from the server, we'll just zero the pages here.
4943 	 * We don't check that the rw flag is S_WRITE here because some
4944 	 * implementations may attempt a read access to the buffer before
4945 	 * copying data.
4946 	 */
4947 	mutex_enter(&rp->r_statelock);
4948 	if (io_off >= rp->r_size && seg == segkmap) {
4949 		mutex_exit(&rp->r_statelock);
4950 		bzero(bp->b_un.b_addr, io_len);
4951 		error = 0;
4952 	} else {
4953 		mutex_exit(&rp->r_statelock);
4954 		error = nfs3_bio(bp, NULL, cr);
4955 		if (error == NFS_EOF)
4956 			error = 0;
4957 	}
4958 
4959 	/*
4960 	 * Unmap the buffer before freeing it.
4961 	 */
4962 	bp_mapout(bp);
4963 	pageio_done(bp);
4964 
4965 	savepp = pp;
4966 	do {
4967 		pp->p_fsdata = C_NOCOMMIT;
4968 	} while ((pp = pp->p_next) != savepp);
4969 
4970 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4971 
4972 	/*
4973 	 * In case of error set readahead offset
4974 	 * to the lowest offset.
4975 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4976 	 */
4977 	if (error && rp->r_nextr > io_off) {
4978 		mutex_enter(&rp->r_statelock);
4979 		if (rp->r_nextr > io_off)
4980 			rp->r_nextr = io_off;
4981 		mutex_exit(&rp->r_statelock);
4982 	}
4983 }
4984 
4985 /*
4986  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4987  * If len == 0, do from off to EOF.
4988  *
4989  * The normal cases should be len == 0 && off == 0 (entire vp list),
4990  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4991  * (from pageout).
4992  */
4993 /* ARGSUSED */
4994 static int
nfs3_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)4995 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4996     caller_context_t *ct)
4997 {
4998 	int error;
4999 	rnode_t *rp;
5000 
5001 	ASSERT(cr != NULL);
5002 
5003 	/*
5004 	 * XXX - Why should this check be made here?
5005 	 */
5006 	if (vp->v_flag & VNOMAP)
5007 		return (ENOSYS);
5008 	if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
5009 		return (0);
5010 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5011 		return (EIO);
5012 
5013 	rp = VTOR(vp);
5014 	mutex_enter(&rp->r_statelock);
5015 	rp->r_count++;
5016 	mutex_exit(&rp->r_statelock);
5017 	error = nfs_putpages(vp, off, len, flags, cr);
5018 	mutex_enter(&rp->r_statelock);
5019 	rp->r_count--;
5020 	cv_broadcast(&rp->r_cv);
5021 	mutex_exit(&rp->r_statelock);
5022 
5023 	return (error);
5024 }
5025 
5026 /*
5027  * Write out a single page, possibly klustering adjacent dirty pages.
5028  */
5029 int
nfs3_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)5030 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
5031     int flags, cred_t *cr)
5032 {
5033 	u_offset_t io_off;
5034 	u_offset_t lbn_off;
5035 	u_offset_t lbn;
5036 	size_t io_len;
5037 	uint_t bsize;
5038 	int error;
5039 	rnode_t *rp;
5040 
5041 	ASSERT(!vn_is_readonly(vp));
5042 	ASSERT(pp != NULL);
5043 	ASSERT(cr != NULL);
5044 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
5045 
5046 	rp = VTOR(vp);
5047 	ASSERT(rp->r_count > 0);
5048 
5049 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
5050 	lbn = pp->p_offset / bsize;
5051 	lbn_off = lbn * bsize;
5052 
5053 	/*
5054 	 * Find a kluster that fits in one block, or in
5055 	 * one page if pages are bigger than blocks.  If
5056 	 * there is less file space allocated than a whole
5057 	 * page, we'll shorten the i/o request below.
5058 	 */
5059 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
5060 	    roundup(bsize, PAGESIZE), flags);
5061 
5062 	/*
5063 	 * pvn_write_kluster shouldn't have returned a page with offset
5064 	 * behind the original page we were given.  Verify that.
5065 	 */
5066 	ASSERT((pp->p_offset / bsize) >= lbn);
5067 
5068 	/*
5069 	 * Now pp will have the list of kept dirty pages marked for
5070 	 * write back.  It will also handle invalidation and freeing
5071 	 * of pages that are not dirty.  Check for page length rounding
5072 	 * problems.
5073 	 */
5074 	if (io_off + io_len > lbn_off + bsize) {
5075 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
5076 		io_len = lbn_off + bsize - io_off;
5077 	}
5078 	/*
5079 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5080 	 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5081 	 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5082 	 * progress and the r_size has not been made consistent with the
5083 	 * new size of the file. When the uiomove() completes the r_size is
5084 	 * updated and the RMODINPROGRESS flag is cleared.
5085 	 *
5086 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5087 	 * consistent value of r_size. Without this handshaking, it is
5088 	 * possible that nfs(3)_bio() picks  up the old value of r_size
5089 	 * before the uiomove() in writerp() completes. This will result
5090 	 * in the write through nfs(3)_bio() being dropped.
5091 	 *
5092 	 * More precisely, there is a window between the time the uiomove()
5093 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
5094 	 * operation intervenes in this window, the page will be picked up,
5095 	 * because it is dirty (it will be unlocked, unless it was
5096 	 * pagecreate'd). When the page is picked up as dirty, the dirty
5097 	 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5098 	 * checked. This will still be the old size. Therefore the page will
5099 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
5100 	 * the page will be found to be clean and the write will be dropped.
5101 	 */
5102 	if (rp->r_flags & RMODINPROGRESS) {
5103 		mutex_enter(&rp->r_statelock);
5104 		if ((rp->r_flags & RMODINPROGRESS) &&
5105 		    rp->r_modaddr + MAXBSIZE > io_off &&
5106 		    rp->r_modaddr < io_off + io_len) {
5107 			page_t *plist;
5108 			/*
5109 			 * A write is in progress for this region of the file.
5110 			 * If we did not detect RMODINPROGRESS here then this
5111 			 * path through nfs_putapage() would eventually go to
5112 			 * nfs(3)_bio() and may not write out all of the data
5113 			 * in the pages. We end up losing data. So we decide
5114 			 * to set the modified bit on each page in the page
5115 			 * list and mark the rnode with RDIRTY. This write
5116 			 * will be restarted at some later time.
5117 			 */
5118 			plist = pp;
5119 			while (plist != NULL) {
5120 				pp = plist;
5121 				page_sub(&plist, pp);
5122 				hat_setmod(pp);
5123 				page_io_unlock(pp);
5124 				page_unlock(pp);
5125 			}
5126 			rp->r_flags |= RDIRTY;
5127 			mutex_exit(&rp->r_statelock);
5128 			if (offp)
5129 				*offp = io_off;
5130 			if (lenp)
5131 				*lenp = io_len;
5132 			return (0);
5133 		}
5134 		mutex_exit(&rp->r_statelock);
5135 	}
5136 
5137 	if (flags & B_ASYNC) {
5138 		error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
5139 		    nfs3_sync_putapage);
5140 	} else
5141 		error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr);
5142 
5143 	if (offp)
5144 		*offp = io_off;
5145 	if (lenp)
5146 		*lenp = io_len;
5147 	return (error);
5148 }
5149 
5150 static int
nfs3_sync_putapage(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)5151 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5152     int flags, cred_t *cr)
5153 {
5154 	int error;
5155 	rnode_t *rp;
5156 
5157 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5158 
5159 	flags |= B_WRITE;
5160 
5161 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5162 
5163 	rp = VTOR(vp);
5164 
5165 	if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
5166 	    error == EACCES) &&
5167 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
5168 		if (!(rp->r_flags & ROUTOFSPACE)) {
5169 			mutex_enter(&rp->r_statelock);
5170 			rp->r_flags |= ROUTOFSPACE;
5171 			mutex_exit(&rp->r_statelock);
5172 		}
5173 		flags |= B_ERROR;
5174 		pvn_write_done(pp, flags);
5175 		/*
5176 		 * If this was not an async thread, then try again to
5177 		 * write out the pages, but this time, also destroy
5178 		 * them whether or not the write is successful.  This
5179 		 * will prevent memory from filling up with these
5180 		 * pages and destroying them is the only alternative
5181 		 * if they can't be written out.
5182 		 *
5183 		 * Don't do this if this is an async thread because
5184 		 * when the pages are unlocked in pvn_write_done,
5185 		 * some other thread could have come along, locked
5186 		 * them, and queued for an async thread.  It would be
5187 		 * possible for all of the async threads to be tied
5188 		 * up waiting to lock the pages again and they would
5189 		 * all already be locked and waiting for an async
5190 		 * thread to handle them.  Deadlock.
5191 		 */
5192 		if (!(flags & B_ASYNC)) {
5193 			error = nfs3_putpage(vp, io_off, io_len,
5194 			    B_INVAL | B_FORCE, cr, NULL);
5195 		}
5196 	} else {
5197 		if (error)
5198 			flags |= B_ERROR;
5199 		else if (rp->r_flags & ROUTOFSPACE) {
5200 			mutex_enter(&rp->r_statelock);
5201 			rp->r_flags &= ~ROUTOFSPACE;
5202 			mutex_exit(&rp->r_statelock);
5203 		}
5204 		pvn_write_done(pp, flags);
5205 		if (freemem < desfree)
5206 			(void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr);
5207 	}
5208 
5209 	return (error);
5210 }
5211 
5212 /* ARGSUSED */
5213 static int
nfs3_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)5214 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5215     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5216     cred_t *cr, caller_context_t *ct)
5217 {
5218 	struct segvn_crargs vn_a;
5219 	int error;
5220 	rnode_t *rp;
5221 	struct vattr va;
5222 
5223 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5224 		return (EIO);
5225 
5226 	if (vp->v_flag & VNOMAP)
5227 		return (ENOSYS);
5228 
5229 	if (off < 0 || off + len < 0)
5230 		return (ENXIO);
5231 
5232 	if (vp->v_type != VREG)
5233 		return (ENODEV);
5234 
5235 	/*
5236 	 * If there is cached data and if close-to-open consistency
5237 	 * checking is not turned off and if the file system is not
5238 	 * mounted readonly, then force an over the wire getattr.
5239 	 * Otherwise, just invoke nfs3getattr to get a copy of the
5240 	 * attributes.  The attribute cache will be used unless it
5241 	 * is timed out and if it is, then an over the wire getattr
5242 	 * will be issued.
5243 	 */
5244 	va.va_mask = AT_ALL;
5245 	if (vn_has_cached_data(vp) &&
5246 	    !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
5247 		error = nfs3_getattr_otw(vp, &va, cr);
5248 	else
5249 		error = nfs3getattr(vp, &va, cr);
5250 	if (error)
5251 		return (error);
5252 
5253 	/*
5254 	 * Check to see if the vnode is currently marked as not cachable.
5255 	 * This means portions of the file are locked (through VOP_FRLOCK).
5256 	 * In this case the map request must be refused.  We use
5257 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5258 	 */
5259 	rp = VTOR(vp);
5260 
5261 	/*
5262 	 * Atomically increment r_inmap after acquiring r_rwlock. The
5263 	 * idea here is to acquire r_rwlock to block read/write and
5264 	 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5265 	 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
5266 	 * and we can prevent the deadlock that would have occurred
5267 	 * when nfs3_addmap() would have acquired it out of order.
5268 	 *
5269 	 * Since we are not protecting r_inmap by any lock, we do not
5270 	 * hold any lock when we decrement it. We atomically decrement
5271 	 * r_inmap after we release r_lkserlock.
5272 	 */
5273 
5274 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
5275 		return (EINTR);
5276 	atomic_inc_uint(&rp->r_inmap);
5277 	nfs_rw_exit(&rp->r_rwlock);
5278 
5279 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
5280 		atomic_dec_uint(&rp->r_inmap);
5281 		return (EINTR);
5282 	}
5283 
5284 	if (vp->v_flag & VNOCACHE) {
5285 		error = EAGAIN;
5286 		goto done;
5287 	}
5288 
5289 	/*
5290 	 * Don't allow concurrent locks and mapping if mandatory locking is
5291 	 * enabled.
5292 	 */
5293 	if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
5294 	    MANDLOCK(vp, va.va_mode)) {
5295 		error = EAGAIN;
5296 		goto done;
5297 	}
5298 
5299 	as_rangelock(as);
5300 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5301 	if (error != 0) {
5302 		as_rangeunlock(as);
5303 		goto done;
5304 	}
5305 
5306 	vn_a.vp = vp;
5307 	vn_a.offset = off;
5308 	vn_a.type = (flags & MAP_TYPE);
5309 	vn_a.prot = (uchar_t)prot;
5310 	vn_a.maxprot = (uchar_t)maxprot;
5311 	vn_a.flags = (flags & ~MAP_TYPE);
5312 	vn_a.cred = cr;
5313 	vn_a.amp = NULL;
5314 	vn_a.szc = 0;
5315 	vn_a.lgrp_mem_policy_flags = 0;
5316 
5317 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
5318 	as_rangeunlock(as);
5319 
5320 done:
5321 	nfs_rw_exit(&rp->r_lkserlock);
5322 	atomic_dec_uint(&rp->r_inmap);
5323 	return (error);
5324 }
5325 
5326 /* ARGSUSED */
5327 static int
nfs3_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)5328 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5329     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5330     cred_t *cr, caller_context_t *ct)
5331 {
5332 	rnode_t *rp;
5333 
5334 	if (vp->v_flag & VNOMAP)
5335 		return (ENOSYS);
5336 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5337 		return (EIO);
5338 
5339 	rp = VTOR(vp);
5340 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
5341 
5342 	return (0);
5343 }
5344 
5345 /* ARGSUSED */
5346 static int
nfs3_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)5347 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5348     offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
5349     caller_context_t *ct)
5350 {
5351 	netobj lm_fh3;
5352 	int rc;
5353 	u_offset_t start, end;
5354 	rnode_t *rp;
5355 	int error = 0, intr = INTR(vp);
5356 
5357 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5358 		return (EIO);
5359 
5360 	/* check for valid cmd parameter */
5361 	switch (cmd) {
5362 	case F_FLOCK:
5363 	case F_FLOCKW:
5364 	case F_OFD_GETLK:
5365 	case F_OFD_SETLK:
5366 	case F_OFD_SETLKW:
5367 		return (EOPNOTSUPP);
5368 	case F_GETLK:
5369 	case F_SETLK:
5370 	case F_SETLKW:
5371 		break;
5372 	default:
5373 		return (EINVAL);
5374 	}
5375 
5376 	/* Verify l_type. */
5377 	switch (bfp->l_type) {
5378 	case F_RDLCK:
5379 		if (cmd != F_GETLK && !(flag & FREAD))
5380 			return (EBADF);
5381 		break;
5382 	case F_WRLCK:
5383 		if (cmd != F_GETLK && !(flag & FWRITE))
5384 			return (EBADF);
5385 		break;
5386 	case F_UNLCK:
5387 		intr = 0;
5388 		break;
5389 
5390 	default:
5391 		return (EINVAL);
5392 	}
5393 
5394 	/* check the validity of the lock range */
5395 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
5396 		return (rc);
5397 	if (rc = flk_check_lock_data(start, end, MAXEND))
5398 		return (rc);
5399 
5400 	/*
5401 	 * If the filesystem is mounted using local locking, pass the
5402 	 * request off to the local locking code.
5403 	 */
5404 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
5405 		if (cmd == F_SETLK || cmd == F_SETLKW) {
5406 			/*
5407 			 * For complete safety, we should be holding
5408 			 * r_lkserlock.  However, we can't call
5409 			 * lm_safelock and then fs_frlock while
5410 			 * holding r_lkserlock, so just invoke
5411 			 * lm_safelock and expect that this will
5412 			 * catch enough of the cases.
5413 			 */
5414 			if (!lm_safelock(vp, bfp, cr))
5415 				return (EAGAIN);
5416 		}
5417 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
5418 	}
5419 
5420 	rp = VTOR(vp);
5421 
5422 	/*
5423 	 * Check whether the given lock request can proceed, given the
5424 	 * current file mappings.
5425 	 */
5426 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
5427 		return (EINTR);
5428 	if (cmd == F_SETLK || cmd == F_SETLKW) {
5429 		if (!lm_safelock(vp, bfp, cr)) {
5430 			rc = EAGAIN;
5431 			goto done;
5432 		}
5433 	}
5434 
5435 	/*
5436 	 * Flush the cache after waiting for async I/O to finish.  For new
5437 	 * locks, this is so that the process gets the latest bits from the
5438 	 * server.  For unlocks, this is so that other clients see the
5439 	 * latest bits once the file has been unlocked.  If currently dirty
5440 	 * pages can't be flushed, then don't allow a lock to be set.  But
5441 	 * allow unlocks to succeed, to avoid having orphan locks on the
5442 	 * server.
5443 	 */
5444 	if (cmd != F_GETLK) {
5445 		mutex_enter(&rp->r_statelock);
5446 		while (rp->r_count > 0) {
5447 			if (intr) {
5448 				klwp_t *lwp = ttolwp(curthread);
5449 
5450 				if (lwp != NULL)
5451 					lwp->lwp_nostop++;
5452 				if (cv_wait_sig(&rp->r_cv,
5453 				    &rp->r_statelock) == 0) {
5454 					if (lwp != NULL)
5455 						lwp->lwp_nostop--;
5456 					rc = EINTR;
5457 					break;
5458 				}
5459 				if (lwp != NULL)
5460 					lwp->lwp_nostop--;
5461 			} else
5462 				cv_wait(&rp->r_cv, &rp->r_statelock);
5463 		}
5464 		mutex_exit(&rp->r_statelock);
5465 		if (rc != 0)
5466 			goto done;
5467 		error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
5468 		if (error) {
5469 			if (error == ENOSPC || error == EDQUOT) {
5470 				mutex_enter(&rp->r_statelock);
5471 				if (!rp->r_error)
5472 					rp->r_error = error;
5473 				mutex_exit(&rp->r_statelock);
5474 			}
5475 			if (bfp->l_type != F_UNLCK) {
5476 				rc = ENOLCK;
5477 				goto done;
5478 			}
5479 		}
5480 	}
5481 
5482 	lm_fh3.n_len = VTOFH3(vp)->fh3_length;
5483 	lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
5484 
5485 	/*
5486 	 * Call the lock manager to do the real work of contacting
5487 	 * the server and obtaining the lock.
5488 	 */
5489 	rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp);
5490 
5491 	if (rc == 0)
5492 		nfs_lockcompletion(vp, cmd);
5493 
5494 done:
5495 	nfs_rw_exit(&rp->r_lkserlock);
5496 	return (rc);
5497 }
5498 
5499 /*
5500  * Free storage space associated with the specified vnode.  The portion
5501  * to be freed is specified by bfp->l_start and bfp->l_len (already
5502  * normalized to a "whence" of 0).
5503  *
5504  * This is an experimental facility whose continued existence is not
5505  * guaranteed.  Currently, we only support the special case
5506  * of l_len == 0, meaning free to end of file.
5507  */
5508 /* ARGSUSED */
5509 static int
nfs3_space(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)5510 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5511     offset_t offset, cred_t *cr, caller_context_t *ct)
5512 {
5513 	int error;
5514 
5515 	ASSERT(vp->v_type == VREG);
5516 	if (cmd != F_FREESP)
5517 		return (EINVAL);
5518 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5519 		return (EIO);
5520 
5521 	error = convoff(vp, bfp, 0, offset);
5522 	if (!error) {
5523 		ASSERT(bfp->l_start >= 0);
5524 		if (bfp->l_len == 0) {
5525 			struct vattr va;
5526 
5527 			/*
5528 			 * ftruncate should not change the ctime and
5529 			 * mtime if we truncate the file to its
5530 			 * previous size.
5531 			 */
5532 			va.va_mask = AT_SIZE;
5533 			error = nfs3getattr(vp, &va, cr);
5534 			if (error || va.va_size == bfp->l_start)
5535 				return (error);
5536 			va.va_mask = AT_SIZE;
5537 			va.va_size = bfp->l_start;
5538 			error = nfs3setattr(vp, &va, 0, cr);
5539 
5540 			if (error == 0 && bfp->l_start == 0)
5541 				vnevent_truncate(vp, ct);
5542 		} else
5543 			error = EINVAL;
5544 	}
5545 
5546 	return (error);
5547 }
5548 
5549 /* ARGSUSED */
5550 static int
nfs3_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)5551 nfs3_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
5552 {
5553 
5554 	return (EINVAL);
5555 }
5556 
5557 /*
5558  * Setup and add an address space callback to do the work of the delmap call.
5559  * The callback will (and must be) deleted in the actual callback function.
5560  *
5561  * This is done in order to take care of the problem that we have with holding
5562  * the address space's a_lock for a long period of time (e.g. if the NFS server
5563  * is down).  Callbacks will be executed in the address space code while the
5564  * a_lock is not held.	Holding the address space's a_lock causes things such
5565  * as ps and fork to hang because they are trying to acquire this lock as well.
5566  */
5567 /* ARGSUSED */
5568 static int
nfs3_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)5569 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5570     size_t len, uint_t prot, uint_t maxprot, uint_t flags,
5571     cred_t *cr, caller_context_t *ct)
5572 {
5573 	int			caller_found;
5574 	int			error;
5575 	rnode_t			*rp;
5576 	nfs_delmap_args_t	*dmapp;
5577 	nfs_delmapcall_t	*delmap_call;
5578 
5579 	if (vp->v_flag & VNOMAP)
5580 		return (ENOSYS);
5581 	/*
5582 	 * A process may not change zones if it has NFS pages mmap'ed
5583 	 * in, so we can't legitimately get here from the wrong zone.
5584 	 */
5585 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5586 
5587 	rp = VTOR(vp);
5588 
5589 	/*
5590 	 * The way that the address space of this process deletes its mapping
5591 	 * of this file is via the following call chains:
5592 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5593 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5594 	 *
5595 	 * With the use of address space callbacks we are allowed to drop the
5596 	 * address space lock, a_lock, while executing the NFS operations that
5597 	 * need to go over the wire.  Returning EAGAIN to the caller of this
5598 	 * function is what drives the execution of the callback that we add
5599 	 * below.  The callback will be executed by the address space code
5600 	 * after dropping the a_lock.  When the callback is finished, since
5601 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5602 	 * is called again on the same segment to finish the rest of the work
5603 	 * that needs to happen during unmapping.
5604 	 *
5605 	 * This action of calling back into the segment driver causes
5606 	 * nfs3_delmap() to get called again, but since the callback was
5607 	 * already executed at this point, it already did the work and there
5608 	 * is nothing left for us to do.
5609 	 *
5610 	 * To Summarize:
5611 	 * - The first time nfs3_delmap is called by the current thread is when
5612 	 * we add the caller associated with this delmap to the delmap caller
5613 	 * list, add the callback, and return EAGAIN.
5614 	 * - The second time in this call chain when nfs3_delmap is called we
5615 	 * will find this caller in the delmap caller list and realize there
5616 	 * is no more work to do thus removing this caller from the list and
5617 	 * returning the error that was set in the callback execution.
5618 	 */
5619 	caller_found = nfs_find_and_delete_delmapcall(rp, &error);
5620 	if (caller_found) {
5621 		/*
5622 		 * 'error' is from the actual delmap operations.  To avoid
5623 		 * hangs, we need to handle the return of EAGAIN differently
5624 		 * since this is what drives the callback execution.
5625 		 * In this case, we don't want to return EAGAIN and do the
5626 		 * callback execution because there are none to execute.
5627 		 */
5628 		if (error == EAGAIN)
5629 			return (0);
5630 		else
5631 			return (error);
5632 	}
5633 
5634 	/* current caller was not in the list */
5635 	delmap_call = nfs_init_delmapcall();
5636 
5637 	mutex_enter(&rp->r_statelock);
5638 	list_insert_tail(&rp->r_indelmap, delmap_call);
5639 	mutex_exit(&rp->r_statelock);
5640 
5641 	dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
5642 
5643 	dmapp->vp = vp;
5644 	dmapp->off = off;
5645 	dmapp->addr = addr;
5646 	dmapp->len = len;
5647 	dmapp->prot = prot;
5648 	dmapp->maxprot = maxprot;
5649 	dmapp->flags = flags;
5650 	dmapp->cr = cr;
5651 	dmapp->caller = delmap_call;
5652 
5653 	error = as_add_callback(as, nfs3_delmap_callback, dmapp,
5654 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
5655 
5656 	return (error ? error : EAGAIN);
5657 }
5658 
5659 /*
5660  * Remove some pages from an mmap'd vnode.  Just update the
5661  * count of pages.  If doing close-to-open, then flush and
5662  * commit all of the pages associated with this file.
5663  * Otherwise, start an asynchronous page flush to write out
5664  * any dirty pages.  This will also associate a credential
5665  * with the rnode which can be used to write the pages.
5666  */
5667 /* ARGSUSED */
5668 static void
nfs3_delmap_callback(struct as * as,void * arg,uint_t event)5669 nfs3_delmap_callback(struct as *as, void *arg, uint_t event)
5670 {
5671 	int			error;
5672 	rnode_t			*rp;
5673 	mntinfo_t		*mi;
5674 	nfs_delmap_args_t	*dmapp = (nfs_delmap_args_t *)arg;
5675 
5676 	rp = VTOR(dmapp->vp);
5677 	mi = VTOMI(dmapp->vp);
5678 
5679 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
5680 	ASSERT(rp->r_mapcnt >= 0);
5681 
5682 	/*
5683 	 * Initiate a page flush and potential commit if there are
5684 	 * pages, the file system was not mounted readonly, the segment
5685 	 * was mapped shared, and the pages themselves were writeable.
5686 	 */
5687 	if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
5688 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
5689 		mutex_enter(&rp->r_statelock);
5690 		rp->r_flags |= RDIRTY;
5691 		mutex_exit(&rp->r_statelock);
5692 		/*
5693 		 * If this is a cross-zone access a sync putpage won't work, so
5694 		 * the best we can do is try an async putpage.  That seems
5695 		 * better than something more draconian such as discarding the
5696 		 * dirty pages.
5697 		 */
5698 		if ((mi->mi_flags & MI_NOCTO) ||
5699 		    nfs_zone() != mi->mi_zone)
5700 			error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5701 			    B_ASYNC, dmapp->cr, NULL);
5702 		else
5703 			error = nfs3_putpage_commit(dmapp->vp, dmapp->off,
5704 			    dmapp->len, dmapp->cr);
5705 		if (!error) {
5706 			mutex_enter(&rp->r_statelock);
5707 			error = rp->r_error;
5708 			rp->r_error = 0;
5709 			mutex_exit(&rp->r_statelock);
5710 		}
5711 	} else
5712 		error = 0;
5713 
5714 	if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
5715 		(void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5716 		    B_INVAL, dmapp->cr, NULL);
5717 
5718 	dmapp->caller->error = error;
5719 	(void) as_delete_callback(as, arg);
5720 	kmem_free(dmapp, sizeof (nfs_delmap_args_t));
5721 }
5722 
5723 static int nfs3_pathconf_disable_cache = 0;
5724 
5725 #ifdef DEBUG
5726 static int nfs3_pathconf_cache_hits = 0;
5727 static int nfs3_pathconf_cache_misses = 0;
5728 #endif
5729 
5730 /* ARGSUSED */
5731 static int
nfs3_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)5732 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5733     caller_context_t *ct)
5734 {
5735 	int error;
5736 	PATHCONF3args args;
5737 	PATHCONF3res res;
5738 	int douprintf;
5739 	failinfo_t fi;
5740 	rnode_t *rp;
5741 	hrtime_t t;
5742 
5743 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5744 		return (EIO);
5745 	/*
5746 	 * Large file spec - need to base answer on info stored
5747 	 * on original FSINFO response.
5748 	 */
5749 	if (cmd == _PC_FILESIZEBITS) {
5750 		unsigned long long ll;
5751 		long l = 1;
5752 
5753 		ll = VTOMI(vp)->mi_maxfilesize;
5754 
5755 		if (ll == 0) {
5756 			*valp = 0;
5757 			return (0);
5758 		}
5759 
5760 		if (ll & 0xffffffff00000000) {
5761 			l += 32; ll >>= 32;
5762 		}
5763 		if (ll & 0xffff0000) {
5764 			l += 16; ll >>= 16;
5765 		}
5766 		if (ll & 0xff00) {
5767 			l += 8; ll >>= 8;
5768 		}
5769 		if (ll & 0xf0) {
5770 			l += 4; ll >>= 4;
5771 		}
5772 		if (ll & 0xc) {
5773 			l += 2; ll >>= 2;
5774 		}
5775 		if (ll & 0x2)
5776 			l += 2;
5777 		else if (ll & 0x1)
5778 			l += 1;
5779 		*valp = l;
5780 		return (0);
5781 	}
5782 
5783 	if (cmd == _PC_ACL_ENABLED) {
5784 		*valp = _ACL_ACLENT_ENABLED;
5785 		return (0);
5786 	}
5787 
5788 	if (cmd == _PC_XATTR_EXISTS) {
5789 		error = 0;
5790 		*valp = 0;
5791 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5792 			vnode_t *avp;
5793 			rnode_t *rp;
5794 			int error = 0;
5795 			mntinfo_t *mi = VTOMI(vp);
5796 
5797 			if (!(mi->mi_flags & MI_EXTATTR))
5798 				return (0);
5799 
5800 			rp = VTOR(vp);
5801 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
5802 			    INTR(vp)))
5803 				return (EINTR);
5804 
5805 			error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
5806 			if (error || avp == NULL)
5807 				error = acl_getxattrdir3(vp, &avp, 0, cr, 0);
5808 
5809 			nfs_rw_exit(&rp->r_rwlock);
5810 
5811 			if (error == 0 && avp != NULL) {
5812 				error = do_xattr_exists_check(avp, valp, cr);
5813 				VN_RELE(avp);
5814 			} else if (error == ENOENT) {
5815 				error = 0;
5816 				*valp = 0;
5817 			}
5818 		}
5819 		return (error);
5820 	}
5821 
5822 	rp = VTOR(vp);
5823 	if (rp->r_pathconf != NULL) {
5824 		mutex_enter(&rp->r_statelock);
5825 		if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) {
5826 			kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf));
5827 			rp->r_pathconf = NULL;
5828 		}
5829 		if (rp->r_pathconf != NULL) {
5830 			error = 0;
5831 			switch (cmd) {
5832 			case _PC_LINK_MAX:
5833 				*valp = rp->r_pathconf->link_max;
5834 				break;
5835 			case _PC_NAME_MAX:
5836 				*valp = rp->r_pathconf->name_max;
5837 				break;
5838 			case _PC_PATH_MAX:
5839 			case _PC_SYMLINK_MAX:
5840 				*valp = MAXPATHLEN;
5841 				break;
5842 			case _PC_CHOWN_RESTRICTED:
5843 				*valp = rp->r_pathconf->chown_restricted;
5844 				break;
5845 			case _PC_NO_TRUNC:
5846 				*valp = rp->r_pathconf->no_trunc;
5847 				break;
5848 			default:
5849 				error = EINVAL;
5850 				break;
5851 			}
5852 			mutex_exit(&rp->r_statelock);
5853 #ifdef DEBUG
5854 			nfs3_pathconf_cache_hits++;
5855 #endif
5856 			return (error);
5857 		}
5858 		mutex_exit(&rp->r_statelock);
5859 	}
5860 #ifdef DEBUG
5861 	nfs3_pathconf_cache_misses++;
5862 #endif
5863 
5864 	args.object = *VTOFH3(vp);
5865 	fi.vp = vp;
5866 	fi.fhp = (caddr_t)&args.object;
5867 	fi.copyproc = nfs3copyfh;
5868 	fi.lookupproc = nfs3lookup;
5869 	fi.xattrdirproc = acl_getxattrdir3;
5870 
5871 	douprintf = 1;
5872 
5873 	t = gethrtime();
5874 
5875 	error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF,
5876 	    xdr_nfs_fh3, (caddr_t)&args,
5877 	    xdr_PATHCONF3res, (caddr_t)&res, cr,
5878 	    &douprintf, &res.status, 0, &fi);
5879 
5880 	if (error)
5881 		return (error);
5882 
5883 	error = geterrno3(res.status);
5884 
5885 	if (!error) {
5886 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
5887 		if (!nfs3_pathconf_disable_cache) {
5888 			mutex_enter(&rp->r_statelock);
5889 			if (rp->r_pathconf == NULL) {
5890 				rp->r_pathconf = kmem_alloc(
5891 				    sizeof (*rp->r_pathconf), KM_NOSLEEP);
5892 				if (rp->r_pathconf != NULL)
5893 					*rp->r_pathconf = res.resok.info;
5894 			}
5895 			mutex_exit(&rp->r_statelock);
5896 		}
5897 		switch (cmd) {
5898 		case _PC_LINK_MAX:
5899 			*valp = res.resok.info.link_max;
5900 			break;
5901 		case _PC_NAME_MAX:
5902 			*valp = res.resok.info.name_max;
5903 			break;
5904 		case _PC_PATH_MAX:
5905 		case _PC_SYMLINK_MAX:
5906 			*valp = MAXPATHLEN;
5907 			break;
5908 		case _PC_CHOWN_RESTRICTED:
5909 			*valp = res.resok.info.chown_restricted;
5910 			break;
5911 		case _PC_NO_TRUNC:
5912 			*valp = res.resok.info.no_trunc;
5913 			break;
5914 		default:
5915 			return (EINVAL);
5916 		}
5917 	} else {
5918 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
5919 		PURGE_STALE_FH(error, vp, cr);
5920 	}
5921 
5922 	return (error);
5923 }
5924 
5925 /*
5926  * Called by async thread to do synchronous pageio. Do the i/o, wait
5927  * for it to complete, and cleanup the page list when done.
5928  */
5929 static int
nfs3_sync_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)5930 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5931     int flags, cred_t *cr)
5932 {
5933 	int error;
5934 
5935 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5936 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5937 	if (flags & B_READ)
5938 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
5939 	else
5940 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
5941 	return (error);
5942 }
5943 
5944 /* ARGSUSED */
5945 static int
nfs3_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)5946 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5947     int flags, cred_t *cr, caller_context_t *ct)
5948 {
5949 	int error;
5950 	rnode_t *rp;
5951 
5952 	if (pp == NULL)
5953 		return (EINVAL);
5954 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5955 		return (EIO);
5956 
5957 	rp = VTOR(vp);
5958 	mutex_enter(&rp->r_statelock);
5959 	rp->r_count++;
5960 	mutex_exit(&rp->r_statelock);
5961 
5962 	if (flags & B_ASYNC) {
5963 		error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
5964 		    nfs3_sync_pageio);
5965 	} else
5966 		error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5967 	mutex_enter(&rp->r_statelock);
5968 	rp->r_count--;
5969 	cv_broadcast(&rp->r_cv);
5970 	mutex_exit(&rp->r_statelock);
5971 	return (error);
5972 }
5973 
5974 /* ARGSUSED */
5975 static void
nfs3_dispose(vnode_t * vp,page_t * pp,int fl,int dn,cred_t * cr,caller_context_t * ct)5976 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
5977     caller_context_t *ct)
5978 {
5979 	int error;
5980 	rnode_t *rp;
5981 	page_t *plist;
5982 	page_t *pptr;
5983 	offset3 offset;
5984 	count3 len;
5985 	k_sigset_t smask;
5986 
5987 	/*
5988 	 * We should get called with fl equal to either B_FREE or
5989 	 * B_INVAL.  Any other value is illegal.
5990 	 *
5991 	 * The page that we are either supposed to free or destroy
5992 	 * should be exclusive locked and its io lock should not
5993 	 * be held.
5994 	 */
5995 	ASSERT(fl == B_FREE || fl == B_INVAL);
5996 	ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
5997 	rp = VTOR(vp);
5998 
5999 	/*
6000 	 * If the page doesn't need to be committed or we shouldn't
6001 	 * even bother attempting to commit it, then just make sure
6002 	 * that the p_fsdata byte is clear and then either free or
6003 	 * destroy the page as appropriate.
6004 	 */
6005 	if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) {
6006 		pp->p_fsdata = C_NOCOMMIT;
6007 		if (fl == B_FREE)
6008 			page_free(pp, dn);
6009 		else
6010 			page_destroy(pp, dn);
6011 		return;
6012 	}
6013 
6014 	/*
6015 	 * If there is a page invalidation operation going on, then
6016 	 * if this is one of the pages being destroyed, then just
6017 	 * clear the p_fsdata byte and then either free or destroy
6018 	 * the page as appropriate.
6019 	 */
6020 	mutex_enter(&rp->r_statelock);
6021 	if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
6022 		mutex_exit(&rp->r_statelock);
6023 		pp->p_fsdata = C_NOCOMMIT;
6024 		if (fl == B_FREE)
6025 			page_free(pp, dn);
6026 		else
6027 			page_destroy(pp, dn);
6028 		return;
6029 	}
6030 
6031 	/*
6032 	 * If we are freeing this page and someone else is already
6033 	 * waiting to do a commit, then just unlock the page and
6034 	 * return.  That other thread will take care of commiting
6035 	 * this page.  The page can be freed sometime after the
6036 	 * commit has finished.  Otherwise, if the page is marked
6037 	 * as delay commit, then we may be getting called from
6038 	 * pvn_write_done, one page at a time.   This could result
6039 	 * in one commit per page, so we end up doing lots of small
6040 	 * commits instead of fewer larger commits.  This is bad,
6041 	 * we want do as few commits as possible.
6042 	 */
6043 	if (fl == B_FREE) {
6044 		if (rp->r_flags & RCOMMITWAIT) {
6045 			page_unlock(pp);
6046 			mutex_exit(&rp->r_statelock);
6047 			return;
6048 		}
6049 		if (pp->p_fsdata == C_DELAYCOMMIT) {
6050 			pp->p_fsdata = C_COMMIT;
6051 			page_unlock(pp);
6052 			mutex_exit(&rp->r_statelock);
6053 			return;
6054 		}
6055 	}
6056 
6057 	/*
6058 	 * Check to see if there is a signal which would prevent an
6059 	 * attempt to commit the pages from being successful.  If so,
6060 	 * then don't bother with all of the work to gather pages and
6061 	 * generate the unsuccessful RPC.  Just return from here and
6062 	 * let the page be committed at some later time.
6063 	 */
6064 	sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
6065 	if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
6066 		sigunintr(&smask);
6067 		page_unlock(pp);
6068 		mutex_exit(&rp->r_statelock);
6069 		return;
6070 	}
6071 	sigunintr(&smask);
6072 
6073 	/*
6074 	 * We are starting to need to commit pages, so let's try
6075 	 * to commit as many as possible at once to reduce the
6076 	 * overhead.
6077 	 *
6078 	 * Set the `commit inprogress' state bit.  We must
6079 	 * first wait until any current one finishes.  Then
6080 	 * we initialize the c_pages list with this page.
6081 	 */
6082 	while (rp->r_flags & RCOMMIT) {
6083 		rp->r_flags |= RCOMMITWAIT;
6084 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6085 		rp->r_flags &= ~RCOMMITWAIT;
6086 	}
6087 	rp->r_flags |= RCOMMIT;
6088 	mutex_exit(&rp->r_statelock);
6089 	ASSERT(rp->r_commit.c_pages == NULL);
6090 	rp->r_commit.c_pages = pp;
6091 	rp->r_commit.c_commbase = (offset3)pp->p_offset;
6092 	rp->r_commit.c_commlen = PAGESIZE;
6093 
6094 	/*
6095 	 * Gather together all other pages which can be committed.
6096 	 * They will all be chained off r_commit.c_pages.
6097 	 */
6098 	nfs3_get_commit(vp);
6099 
6100 	/*
6101 	 * Clear the `commit inprogress' status and disconnect
6102 	 * the list of pages to be committed from the rnode.
6103 	 * At this same time, we also save the starting offset
6104 	 * and length of data to be committed on the server.
6105 	 */
6106 	plist = rp->r_commit.c_pages;
6107 	rp->r_commit.c_pages = NULL;
6108 	offset = rp->r_commit.c_commbase;
6109 	len = rp->r_commit.c_commlen;
6110 	mutex_enter(&rp->r_statelock);
6111 	rp->r_flags &= ~RCOMMIT;
6112 	cv_broadcast(&rp->r_commit.c_cv);
6113 	mutex_exit(&rp->r_statelock);
6114 
6115 	if (curproc == proc_pageout || curproc == proc_fsflush ||
6116 	    nfs_zone() != VTOMI(vp)->mi_zone) {
6117 		nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit);
6118 		return;
6119 	}
6120 
6121 	/*
6122 	 * Actually generate the COMMIT3 over the wire operation.
6123 	 */
6124 	error = nfs3_commit(vp, offset, len, cr);
6125 
6126 	/*
6127 	 * If we got an error during the commit, just unlock all
6128 	 * of the pages.  The pages will get retransmitted to the
6129 	 * server during a putpage operation.
6130 	 */
6131 	if (error) {
6132 		while (plist != NULL) {
6133 			pptr = plist;
6134 			page_sub(&plist, pptr);
6135 			page_unlock(pptr);
6136 		}
6137 		return;
6138 	}
6139 
6140 	/*
6141 	 * We've tried as hard as we can to commit the data to stable
6142 	 * storage on the server.  We release the rest of the pages
6143 	 * and clear the commit required state.  They will be put
6144 	 * onto the tail of the cachelist if they are nolonger
6145 	 * mapped.
6146 	 */
6147 	while (plist != pp) {
6148 		pptr = plist;
6149 		page_sub(&plist, pptr);
6150 		pptr->p_fsdata = C_NOCOMMIT;
6151 		(void) page_release(pptr, 1);
6152 	}
6153 
6154 	/*
6155 	 * It is possible that nfs3_commit didn't return error but
6156 	 * some other thread has modified the page we are going
6157 	 * to free/destroy.
6158 	 *    In this case we need to rewrite the page. Do an explicit check
6159 	 * before attempting to free/destroy the page. If modified, needs to
6160 	 * be rewritten so unlock the page and return.
6161 	 */
6162 	if (hat_ismod(pp)) {
6163 		pp->p_fsdata = C_NOCOMMIT;
6164 		page_unlock(pp);
6165 		return;
6166 	}
6167 
6168 	/*
6169 	 * Now, as appropriate, either free or destroy the page
6170 	 * that we were called with.
6171 	 */
6172 	pp->p_fsdata = C_NOCOMMIT;
6173 	if (fl == B_FREE)
6174 		page_free(pp, dn);
6175 	else
6176 		page_destroy(pp, dn);
6177 }
6178 
6179 static int
nfs3_commit(vnode_t * vp,offset3 offset,count3 count,cred_t * cr)6180 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr)
6181 {
6182 	int error;
6183 	rnode_t *rp;
6184 	COMMIT3args args;
6185 	COMMIT3res res;
6186 	int douprintf;
6187 	cred_t *cred;
6188 
6189 	rp = VTOR(vp);
6190 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6191 
6192 	mutex_enter(&rp->r_statelock);
6193 	if (rp->r_cred != NULL) {
6194 		cred = rp->r_cred;
6195 		crhold(cred);
6196 	} else {
6197 		rp->r_cred = cr;
6198 		crhold(cr);
6199 		cred = cr;
6200 		crhold(cred);
6201 	}
6202 	mutex_exit(&rp->r_statelock);
6203 
6204 	args.file = *VTOFH3(vp);
6205 	args.offset = offset;
6206 	args.count = count;
6207 
6208 doitagain:
6209 	douprintf = 1;
6210 	error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT,
6211 	    xdr_COMMIT3args, (caddr_t)&args,
6212 	    xdr_COMMIT3res, (caddr_t)&res, cred,
6213 	    &douprintf, &res.status, 0, NULL);
6214 
6215 	crfree(cred);
6216 
6217 	if (error)
6218 		return (error);
6219 
6220 	error = geterrno3(res.status);
6221 	if (!error) {
6222 		ASSERT(rp->r_flags & RHAVEVERF);
6223 		mutex_enter(&rp->r_statelock);
6224 		if (rp->r_verf == res.resok.verf) {
6225 			mutex_exit(&rp->r_statelock);
6226 			return (0);
6227 		}
6228 		nfs3_set_mod(vp);
6229 		rp->r_verf = res.resok.verf;
6230 		mutex_exit(&rp->r_statelock);
6231 		error = NFS_VERF_MISMATCH;
6232 	} else {
6233 		if (error == EACCES) {
6234 			mutex_enter(&rp->r_statelock);
6235 			if (cred != cr) {
6236 				if (rp->r_cred != NULL)
6237 					crfree(rp->r_cred);
6238 				rp->r_cred = cr;
6239 				crhold(cr);
6240 				cred = cr;
6241 				crhold(cred);
6242 				mutex_exit(&rp->r_statelock);
6243 				goto doitagain;
6244 			}
6245 			mutex_exit(&rp->r_statelock);
6246 		}
6247 		/*
6248 		 * Can't do a PURGE_STALE_FH here because this
6249 		 * can cause a deadlock.  nfs3_commit can
6250 		 * be called from nfs3_dispose which can be called
6251 		 * indirectly via pvn_vplist_dirty.  PURGE_STALE_FH
6252 		 * can call back to pvn_vplist_dirty.
6253 		 */
6254 		if (error == ESTALE) {
6255 			mutex_enter(&rp->r_statelock);
6256 			rp->r_flags |= RSTALE;
6257 			if (!rp->r_error)
6258 				rp->r_error = error;
6259 			mutex_exit(&rp->r_statelock);
6260 			PURGE_ATTRCACHE(vp);
6261 		} else {
6262 			mutex_enter(&rp->r_statelock);
6263 			if (!rp->r_error)
6264 				rp->r_error = error;
6265 			mutex_exit(&rp->r_statelock);
6266 		}
6267 	}
6268 
6269 	return (error);
6270 }
6271 
6272 static void
nfs3_set_mod(vnode_t * vp)6273 nfs3_set_mod(vnode_t *vp)
6274 {
6275 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6276 
6277 	pvn_vplist_setdirty(vp, nfs_setmod_check);
6278 }
6279 
6280 /*
6281  * This routine is used to gather together a page list of the pages
6282  * which are to be committed on the server.  This routine must not
6283  * be called if the calling thread holds any locked pages.
6284  *
6285  * The calling thread must have set RCOMMIT.  This bit is used to
6286  * serialize access to the commit structure in the rnode.  As long
6287  * as the thread has set RCOMMIT, then it can manipulate the commit
6288  * structure without requiring any other locks.
6289  */
6290 static void
nfs3_get_commit(vnode_t * vp)6291 nfs3_get_commit(vnode_t *vp)
6292 {
6293 	rnode_t *rp;
6294 	page_t *pp;
6295 	kmutex_t *vphm;
6296 
6297 	rp = VTOR(vp);
6298 
6299 	ASSERT(rp->r_flags & RCOMMIT);
6300 
6301 	vphm = page_vnode_mutex(vp);
6302 	mutex_enter(vphm);
6303 
6304 	/*
6305 	 * If there are no pages associated with this vnode, then
6306 	 * just return.
6307 	 */
6308 	if ((pp = vp->v_pages) == NULL) {
6309 		mutex_exit(vphm);
6310 		return;
6311 	}
6312 
6313 	/*
6314 	 * Step through all of the pages associated with this vnode
6315 	 * looking for pages which need to be committed.
6316 	 */
6317 	do {
6318 		/* Skip marker pages. */
6319 		if (pp->p_hash == PVN_VPLIST_HASH_TAG)
6320 			continue;
6321 
6322 		/*
6323 		 * If this page does not need to be committed or is
6324 		 * modified, then just skip it.
6325 		 */
6326 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
6327 			continue;
6328 
6329 		/*
6330 		 * Attempt to lock the page.  If we can't, then
6331 		 * someone else is messing with it and we will
6332 		 * just skip it.
6333 		 */
6334 		if (!page_trylock(pp, SE_EXCL))
6335 			continue;
6336 
6337 		/*
6338 		 * If this page does not need to be committed or is
6339 		 * modified, then just skip it.  Recheck now that
6340 		 * the page is locked.
6341 		 */
6342 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6343 			page_unlock(pp);
6344 			continue;
6345 		}
6346 
6347 		if (PP_ISFREE(pp)) {
6348 			cmn_err(CE_PANIC, "nfs3_get_commit: %p is free",
6349 			    (void *)pp);
6350 		}
6351 
6352 		/*
6353 		 * The page needs to be committed and we locked it.
6354 		 * Update the base and length parameters and add it
6355 		 * to r_pages.
6356 		 */
6357 		if (rp->r_commit.c_pages == NULL) {
6358 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6359 			rp->r_commit.c_commlen = PAGESIZE;
6360 		} else if (pp->p_offset < rp->r_commit.c_commbase) {
6361 			rp->r_commit.c_commlen = rp->r_commit.c_commbase -
6362 			    (offset3)pp->p_offset + rp->r_commit.c_commlen;
6363 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6364 		} else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
6365 		    <= pp->p_offset) {
6366 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6367 			    rp->r_commit.c_commbase + PAGESIZE;
6368 		}
6369 		page_add(&rp->r_commit.c_pages, pp);
6370 	} while ((pp = pp->p_vpnext) != vp->v_pages);
6371 
6372 	mutex_exit(vphm);
6373 }
6374 
6375 /*
6376  * This routine is used to gather together a page list of the pages
6377  * which are to be committed on the server.  This routine must not
6378  * be called if the calling thread holds any locked pages.
6379  *
6380  * The calling thread must have set RCOMMIT.  This bit is used to
6381  * serialize access to the commit structure in the rnode.  As long
6382  * as the thread has set RCOMMIT, then it can manipulate the commit
6383  * structure without requiring any other locks.
6384  */
6385 static void
nfs3_get_commit_range(vnode_t * vp,u_offset_t soff,size_t len)6386 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
6387 {
6388 
6389 	rnode_t *rp;
6390 	page_t *pp;
6391 	u_offset_t end;
6392 	u_offset_t off;
6393 
6394 	ASSERT(len != 0);
6395 
6396 	rp = VTOR(vp);
6397 
6398 	ASSERT(rp->r_flags & RCOMMIT);
6399 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6400 
6401 	/*
6402 	 * If there are no pages associated with this vnode, then
6403 	 * just return.
6404 	 */
6405 	if ((pp = vp->v_pages) == NULL)
6406 		return;
6407 
6408 	/*
6409 	 * Calculate the ending offset.
6410 	 */
6411 	end = soff + len;
6412 
6413 	for (off = soff; off < end; off += PAGESIZE) {
6414 		/*
6415 		 * Lookup each page by vp, offset.
6416 		 */
6417 		if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
6418 			continue;
6419 
6420 		/*
6421 		 * If this page does not need to be committed or is
6422 		 * modified, then just skip it.
6423 		 */
6424 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6425 			page_unlock(pp);
6426 			continue;
6427 		}
6428 
6429 		ASSERT(PP_ISFREE(pp) == 0);
6430 
6431 		/*
6432 		 * The page needs to be committed and we locked it.
6433 		 * Update the base and length parameters and add it
6434 		 * to r_pages.
6435 		 */
6436 		if (rp->r_commit.c_pages == NULL) {
6437 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6438 			rp->r_commit.c_commlen = PAGESIZE;
6439 		} else {
6440 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6441 			    rp->r_commit.c_commbase + PAGESIZE;
6442 		}
6443 		page_add(&rp->r_commit.c_pages, pp);
6444 	}
6445 }
6446 
6447 static int
nfs3_putpage_commit(vnode_t * vp,offset_t poff,size_t plen,cred_t * cr)6448 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
6449 {
6450 	int error;
6451 	writeverf3 write_verf;
6452 	rnode_t *rp = VTOR(vp);
6453 
6454 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6455 	/*
6456 	 * Flush the data portion of the file and then commit any
6457 	 * portions which need to be committed.  This may need to
6458 	 * be done twice if the server has changed state since
6459 	 * data was last written.  The data will need to be
6460 	 * rewritten to the server and then a new commit done.
6461 	 *
6462 	 * In fact, this may need to be done several times if the
6463 	 * server is having problems and crashing while we are
6464 	 * attempting to do this.
6465 	 */
6466 
6467 top:
6468 	/*
6469 	 * Do a flush based on the poff and plen arguments.  This
6470 	 * will asynchronously write out any modified pages in the
6471 	 * range specified by (poff, plen).  This starts all of the
6472 	 * i/o operations which will be waited for in the next
6473 	 * call to nfs3_putpage
6474 	 */
6475 
6476 	mutex_enter(&rp->r_statelock);
6477 	write_verf = rp->r_verf;
6478 	mutex_exit(&rp->r_statelock);
6479 
6480 	error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
6481 	if (error == EAGAIN)
6482 		error = 0;
6483 
6484 	/*
6485 	 * Do a flush based on the poff and plen arguments.  This
6486 	 * will synchronously write out any modified pages in the
6487 	 * range specified by (poff, plen) and wait until all of
6488 	 * the asynchronous i/o's in that range are done as well.
6489 	 */
6490 	if (!error)
6491 		error = nfs3_putpage(vp, poff, plen, 0, cr, NULL);
6492 
6493 	if (error)
6494 		return (error);
6495 
6496 	mutex_enter(&rp->r_statelock);
6497 	if (rp->r_verf != write_verf) {
6498 		mutex_exit(&rp->r_statelock);
6499 		goto top;
6500 	}
6501 	mutex_exit(&rp->r_statelock);
6502 
6503 	/*
6504 	 * Now commit any pages which might need to be committed.
6505 	 * If the error, NFS_VERF_MISMATCH, is returned, then
6506 	 * start over with the flush operation.
6507 	 */
6508 
6509 	error = nfs3_commit_vp(vp, poff, plen, cr);
6510 
6511 	if (error == NFS_VERF_MISMATCH)
6512 		goto top;
6513 
6514 	return (error);
6515 }
6516 
6517 static int
nfs3_commit_vp(vnode_t * vp,u_offset_t poff,size_t plen,cred_t * cr)6518 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr)
6519 {
6520 	rnode_t *rp;
6521 	page_t *plist;
6522 	offset3 offset;
6523 	count3 len;
6524 
6525 
6526 	rp = VTOR(vp);
6527 
6528 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6529 		return (EIO);
6530 	/*
6531 	 * Set the `commit inprogress' state bit.  We must
6532 	 * first wait until any current one finishes.
6533 	 */
6534 	mutex_enter(&rp->r_statelock);
6535 	while (rp->r_flags & RCOMMIT) {
6536 		rp->r_flags |= RCOMMITWAIT;
6537 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6538 		rp->r_flags &= ~RCOMMITWAIT;
6539 	}
6540 	rp->r_flags |= RCOMMIT;
6541 	mutex_exit(&rp->r_statelock);
6542 
6543 	/*
6544 	 * Gather together all of the pages which need to be
6545 	 * committed.
6546 	 */
6547 	if (plen == 0)
6548 		nfs3_get_commit(vp);
6549 	else
6550 		nfs3_get_commit_range(vp, poff, plen);
6551 
6552 	/*
6553 	 * Clear the `commit inprogress' bit and disconnect the
6554 	 * page list which was gathered together in nfs3_get_commit.
6555 	 */
6556 	plist = rp->r_commit.c_pages;
6557 	rp->r_commit.c_pages = NULL;
6558 	offset = rp->r_commit.c_commbase;
6559 	len = rp->r_commit.c_commlen;
6560 	mutex_enter(&rp->r_statelock);
6561 	rp->r_flags &= ~RCOMMIT;
6562 	cv_broadcast(&rp->r_commit.c_cv);
6563 	mutex_exit(&rp->r_statelock);
6564 
6565 	/*
6566 	 * If any pages need to be committed, commit them and
6567 	 * then unlock them so that they can be freed some
6568 	 * time later.
6569 	 */
6570 	if (plist != NULL) {
6571 		/*
6572 		 * No error occurred during the flush portion
6573 		 * of this operation, so now attempt to commit
6574 		 * the data to stable storage on the server.
6575 		 *
6576 		 * This will unlock all of the pages on the list.
6577 		 */
6578 		return (nfs3_sync_commit(vp, plist, offset, len, cr));
6579 	}
6580 	return (0);
6581 }
6582 
6583 static int
nfs3_sync_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)6584 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6585     cred_t *cr)
6586 {
6587 	int error;
6588 	page_t *pp;
6589 
6590 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6591 	error = nfs3_commit(vp, offset, count, cr);
6592 
6593 	/*
6594 	 * If we got an error, then just unlock all of the pages
6595 	 * on the list.
6596 	 */
6597 	if (error) {
6598 		while (plist != NULL) {
6599 			pp = plist;
6600 			page_sub(&plist, pp);
6601 			page_unlock(pp);
6602 		}
6603 		return (error);
6604 	}
6605 	/*
6606 	 * We've tried as hard as we can to commit the data to stable
6607 	 * storage on the server.  We just unlock the pages and clear
6608 	 * the commit required state.  They will get freed later.
6609 	 */
6610 	while (plist != NULL) {
6611 		pp = plist;
6612 		page_sub(&plist, pp);
6613 		pp->p_fsdata = C_NOCOMMIT;
6614 		page_unlock(pp);
6615 	}
6616 
6617 	return (error);
6618 }
6619 
6620 static void
nfs3_async_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)6621 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6622     cred_t *cr)
6623 {
6624 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6625 	(void) nfs3_sync_commit(vp, plist, offset, count, cr);
6626 }
6627 
6628 /* ARGSUSED */
6629 static int
nfs3_setsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)6630 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6631     caller_context_t *ct)
6632 {
6633 	int error;
6634 	mntinfo_t *mi;
6635 
6636 	mi = VTOMI(vp);
6637 
6638 	if (nfs_zone() != mi->mi_zone)
6639 		return (EIO);
6640 
6641 	if (mi->mi_flags & MI_ACL) {
6642 		error = acl_setacl3(vp, vsecattr, flag, cr);
6643 		if (mi->mi_flags & MI_ACL)
6644 			return (error);
6645 	}
6646 
6647 	return (ENOSYS);
6648 }
6649 
6650 /* ARGSUSED */
6651 static int
nfs3_getsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)6652 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6653     caller_context_t *ct)
6654 {
6655 	int error;
6656 	mntinfo_t *mi;
6657 
6658 	mi = VTOMI(vp);
6659 
6660 	if (nfs_zone() != mi->mi_zone)
6661 		return (EIO);
6662 
6663 	if (mi->mi_flags & MI_ACL) {
6664 		error = acl_getacl3(vp, vsecattr, flag, cr);
6665 		if (mi->mi_flags & MI_ACL)
6666 			return (error);
6667 	}
6668 
6669 	return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
6670 }
6671 
6672 /* ARGSUSED */
6673 static int
nfs3_shrlock(vnode_t * vp,int cmd,struct shrlock * shr,int flag,cred_t * cr,caller_context_t * ct)6674 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
6675     caller_context_t *ct)
6676 {
6677 	int error;
6678 	struct shrlock nshr;
6679 	struct nfs_owner nfs_owner;
6680 	netobj lm_fh3;
6681 
6682 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6683 		return (EIO);
6684 
6685 	/*
6686 	 * check for valid cmd parameter
6687 	 */
6688 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
6689 		return (EINVAL);
6690 
6691 	/*
6692 	 * Check access permissions
6693 	 */
6694 	if (cmd == F_SHARE &&
6695 	    (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
6696 	    ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
6697 		return (EBADF);
6698 
6699 	/*
6700 	 * If the filesystem is mounted using local locking, pass the
6701 	 * request off to the local share code.
6702 	 */
6703 	if (VTOMI(vp)->mi_flags & MI_LLOCK)
6704 		return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
6705 
6706 	switch (cmd) {
6707 	case F_SHARE:
6708 	case F_UNSHARE:
6709 		lm_fh3.n_len = VTOFH3(vp)->fh3_length;
6710 		lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
6711 
6712 		/*
6713 		 * If passed an owner that is too large to fit in an
6714 		 * nfs_owner it is likely a recursive call from the
6715 		 * lock manager client and pass it straight through.  If
6716 		 * it is not a nfs_owner then simply return an error.
6717 		 */
6718 		if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
6719 			if (((struct nfs_owner *)shr->s_owner)->magic !=
6720 			    NFS_OWNER_MAGIC)
6721 				return (EINVAL);
6722 
6723 			if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) {
6724 				error = set_errno(error);
6725 			}
6726 			return (error);
6727 		}
6728 		/*
6729 		 * Remote share reservations owner is a combination of
6730 		 * a magic number, hostname, and the local owner
6731 		 */
6732 		bzero(&nfs_owner, sizeof (nfs_owner));
6733 		nfs_owner.magic = NFS_OWNER_MAGIC;
6734 		(void) strncpy(nfs_owner.hname, uts_nodename(),
6735 		    sizeof (nfs_owner.hname));
6736 		bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
6737 		nshr.s_access = shr->s_access;
6738 		nshr.s_deny = shr->s_deny;
6739 		nshr.s_sysid = 0;
6740 		nshr.s_pid = ttoproc(curthread)->p_pid;
6741 		nshr.s_own_len = sizeof (nfs_owner);
6742 		nshr.s_owner = (caddr_t)&nfs_owner;
6743 
6744 		if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) {
6745 			error = set_errno(error);
6746 		}
6747 
6748 		break;
6749 
6750 	case F_HASREMOTELOCKS:
6751 		/*
6752 		 * NFS client can't store remote locks itself
6753 		 */
6754 		shr->s_access = 0;
6755 		error = 0;
6756 		break;
6757 
6758 	default:
6759 		error = EINVAL;
6760 		break;
6761 	}
6762 
6763 	return (error);
6764 }
6765