xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs3_vnops.c (revision 044718b30e6a998dddd440a2d90357cc7eb053ad)
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 out:
4845 	if (pl == NULL)
4846 		return (error);
4847 
4848 	if (error) {
4849 		if (pp != NULL)
4850 			pvn_read_done(pp, B_ERROR);
4851 		return (error);
4852 	}
4853 
4854 	if (pagefound) {
4855 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
4856 
4857 		/*
4858 		 * Page exists in the cache, acquire the appropriate lock.
4859 		 * If this fails, start all over again.
4860 		 */
4861 		if ((pp = page_lookup(vp, off, se)) == NULL) {
4862 #ifdef DEBUG
4863 			nfs3_lostpage++;
4864 #endif
4865 			goto reread;
4866 		}
4867 		pl[0] = pp;
4868 		pl[1] = NULL;
4869 		return (0);
4870 	}
4871 
4872 	if (pp != NULL)
4873 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4874 
4875 	return (error);
4876 }
4877 
4878 static void
nfs3_readahead(vnode_t * vp,u_offset_t blkoff,caddr_t addr,struct seg * seg,cred_t * cr)4879 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
4880     cred_t *cr)
4881 {
4882 	int error;
4883 	page_t *pp;
4884 	u_offset_t io_off;
4885 	size_t io_len;
4886 	struct buf *bp;
4887 	uint_t bsize, blksize;
4888 	rnode_t *rp = VTOR(vp);
4889 	page_t *savepp;
4890 
4891 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4892 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4893 
4894 	mutex_enter(&rp->r_statelock);
4895 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
4896 		/*
4897 		 * If less than a block left in file read less
4898 		 * than a block.
4899 		 */
4900 		blksize = rp->r_size - blkoff;
4901 	} else
4902 		blksize = bsize;
4903 	mutex_exit(&rp->r_statelock);
4904 
4905 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
4906 	    &io_off, &io_len, blkoff, blksize, 1);
4907 	/*
4908 	 * The isra flag passed to the kluster function is 1, we may have
4909 	 * gotten a return value of NULL for a variety of reasons (# of free
4910 	 * pages < minfree, someone entered the page on the vnode etc). In all
4911 	 * cases, we want to punt on the readahead.
4912 	 */
4913 	if (pp == NULL)
4914 		return;
4915 
4916 	/*
4917 	 * Now round the request size up to page boundaries.
4918 	 * This ensures that the entire page will be
4919 	 * initialized to zeroes if EOF is encountered.
4920 	 */
4921 	io_len = ptob(btopr(io_len));
4922 
4923 	bp = pageio_setup(pp, io_len, vp, B_READ);
4924 	ASSERT(bp != NULL);
4925 
4926 	/*
4927 	 * pageio_setup should have set b_addr to 0.  This is correct since
4928 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4929 	 * to calculate an offset, and then set b_addr to the kernel virtual
4930 	 * address it allocated for us.
4931 	 */
4932 	ASSERT(bp->b_un.b_addr == 0);
4933 
4934 	bp->b_edev = 0;
4935 	bp->b_dev = 0;
4936 	bp->b_lblkno = lbtodb(io_off);
4937 	bp->b_file = vp;
4938 	bp->b_offset = (offset_t)blkoff;
4939 	bp_mapin(bp);
4940 
4941 	/*
4942 	 * If doing a write beyond what we believe is EOF, don't bother trying
4943 	 * to read the pages from the server, we'll just zero the pages here.
4944 	 * We don't check that the rw flag is S_WRITE here because some
4945 	 * implementations may attempt a read access to the buffer before
4946 	 * copying data.
4947 	 */
4948 	mutex_enter(&rp->r_statelock);
4949 	if (io_off >= rp->r_size && seg == segkmap) {
4950 		mutex_exit(&rp->r_statelock);
4951 		bzero(bp->b_un.b_addr, io_len);
4952 		error = 0;
4953 	} else {
4954 		mutex_exit(&rp->r_statelock);
4955 		error = nfs3_bio(bp, NULL, cr);
4956 		if (error == NFS_EOF)
4957 			error = 0;
4958 	}
4959 
4960 	/*
4961 	 * Unmap the buffer before freeing it.
4962 	 */
4963 	bp_mapout(bp);
4964 	pageio_done(bp);
4965 
4966 	savepp = pp;
4967 	do {
4968 		pp->p_fsdata = C_NOCOMMIT;
4969 	} while ((pp = pp->p_next) != savepp);
4970 
4971 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4972 
4973 	/*
4974 	 * In case of error set readahead offset
4975 	 * to the lowest offset.
4976 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4977 	 */
4978 	if (error && rp->r_nextr > io_off) {
4979 		mutex_enter(&rp->r_statelock);
4980 		if (rp->r_nextr > io_off)
4981 			rp->r_nextr = io_off;
4982 		mutex_exit(&rp->r_statelock);
4983 	}
4984 }
4985 
4986 /*
4987  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4988  * If len == 0, do from off to EOF.
4989  *
4990  * The normal cases should be len == 0 && off == 0 (entire vp list),
4991  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4992  * (from pageout).
4993  */
4994 /* ARGSUSED */
4995 static int
nfs3_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)4996 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4997     caller_context_t *ct)
4998 {
4999 	int error;
5000 	rnode_t *rp;
5001 
5002 	ASSERT(cr != NULL);
5003 
5004 	/*
5005 	 * XXX - Why should this check be made here?
5006 	 */
5007 	if (vp->v_flag & VNOMAP)
5008 		return (ENOSYS);
5009 	if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
5010 		return (0);
5011 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5012 		return (EIO);
5013 
5014 	rp = VTOR(vp);
5015 	mutex_enter(&rp->r_statelock);
5016 	rp->r_count++;
5017 	mutex_exit(&rp->r_statelock);
5018 	error = nfs_putpages(vp, off, len, flags, cr);
5019 	mutex_enter(&rp->r_statelock);
5020 	rp->r_count--;
5021 	cv_broadcast(&rp->r_cv);
5022 	mutex_exit(&rp->r_statelock);
5023 
5024 	return (error);
5025 }
5026 
5027 /*
5028  * Write out a single page, possibly klustering adjacent dirty pages.
5029  */
5030 int
nfs3_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)5031 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
5032     int flags, cred_t *cr)
5033 {
5034 	u_offset_t io_off;
5035 	u_offset_t lbn_off;
5036 	u_offset_t lbn;
5037 	size_t io_len;
5038 	uint_t bsize;
5039 	int error;
5040 	rnode_t *rp;
5041 
5042 	ASSERT(!vn_is_readonly(vp));
5043 	ASSERT(pp != NULL);
5044 	ASSERT(cr != NULL);
5045 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
5046 
5047 	rp = VTOR(vp);
5048 	ASSERT(rp->r_count > 0);
5049 
5050 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
5051 	lbn = pp->p_offset / bsize;
5052 	lbn_off = lbn * bsize;
5053 
5054 	/*
5055 	 * Find a kluster that fits in one block, or in
5056 	 * one page if pages are bigger than blocks.  If
5057 	 * there is less file space allocated than a whole
5058 	 * page, we'll shorten the i/o request below.
5059 	 */
5060 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
5061 	    roundup(bsize, PAGESIZE), flags);
5062 
5063 	/*
5064 	 * pvn_write_kluster shouldn't have returned a page with offset
5065 	 * behind the original page we were given.  Verify that.
5066 	 */
5067 	ASSERT((pp->p_offset / bsize) >= lbn);
5068 
5069 	/*
5070 	 * Now pp will have the list of kept dirty pages marked for
5071 	 * write back.  It will also handle invalidation and freeing
5072 	 * of pages that are not dirty.  Check for page length rounding
5073 	 * problems.
5074 	 */
5075 	if (io_off + io_len > lbn_off + bsize) {
5076 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
5077 		io_len = lbn_off + bsize - io_off;
5078 	}
5079 	/*
5080 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5081 	 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5082 	 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5083 	 * progress and the r_size has not been made consistent with the
5084 	 * new size of the file. When the uiomove() completes the r_size is
5085 	 * updated and the RMODINPROGRESS flag is cleared.
5086 	 *
5087 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5088 	 * consistent value of r_size. Without this handshaking, it is
5089 	 * possible that nfs(3)_bio() picks  up the old value of r_size
5090 	 * before the uiomove() in writerp() completes. This will result
5091 	 * in the write through nfs(3)_bio() being dropped.
5092 	 *
5093 	 * More precisely, there is a window between the time the uiomove()
5094 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
5095 	 * operation intervenes in this window, the page will be picked up,
5096 	 * because it is dirty (it will be unlocked, unless it was
5097 	 * pagecreate'd). When the page is picked up as dirty, the dirty
5098 	 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5099 	 * checked. This will still be the old size. Therefore the page will
5100 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
5101 	 * the page will be found to be clean and the write will be dropped.
5102 	 */
5103 	if (rp->r_flags & RMODINPROGRESS) {
5104 		mutex_enter(&rp->r_statelock);
5105 		if ((rp->r_flags & RMODINPROGRESS) &&
5106 		    rp->r_modaddr + MAXBSIZE > io_off &&
5107 		    rp->r_modaddr < io_off + io_len) {
5108 			page_t *plist;
5109 			/*
5110 			 * A write is in progress for this region of the file.
5111 			 * If we did not detect RMODINPROGRESS here then this
5112 			 * path through nfs_putapage() would eventually go to
5113 			 * nfs(3)_bio() and may not write out all of the data
5114 			 * in the pages. We end up losing data. So we decide
5115 			 * to set the modified bit on each page in the page
5116 			 * list and mark the rnode with RDIRTY. This write
5117 			 * will be restarted at some later time.
5118 			 */
5119 			plist = pp;
5120 			while (plist != NULL) {
5121 				pp = plist;
5122 				page_sub(&plist, pp);
5123 				hat_setmod(pp);
5124 				page_io_unlock(pp);
5125 				page_unlock(pp);
5126 			}
5127 			rp->r_flags |= RDIRTY;
5128 			mutex_exit(&rp->r_statelock);
5129 			if (offp)
5130 				*offp = io_off;
5131 			if (lenp)
5132 				*lenp = io_len;
5133 			return (0);
5134 		}
5135 		mutex_exit(&rp->r_statelock);
5136 	}
5137 
5138 	if (flags & B_ASYNC) {
5139 		error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
5140 		    nfs3_sync_putapage);
5141 	} else
5142 		error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr);
5143 
5144 	if (offp)
5145 		*offp = io_off;
5146 	if (lenp)
5147 		*lenp = io_len;
5148 	return (error);
5149 }
5150 
5151 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)5152 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5153     int flags, cred_t *cr)
5154 {
5155 	int error;
5156 	rnode_t *rp;
5157 
5158 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5159 
5160 	flags |= B_WRITE;
5161 
5162 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5163 
5164 	rp = VTOR(vp);
5165 
5166 	if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
5167 	    error == EACCES) &&
5168 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
5169 		if (!(rp->r_flags & ROUTOFSPACE)) {
5170 			mutex_enter(&rp->r_statelock);
5171 			rp->r_flags |= ROUTOFSPACE;
5172 			mutex_exit(&rp->r_statelock);
5173 		}
5174 		flags |= B_ERROR;
5175 		pvn_write_done(pp, flags);
5176 		/*
5177 		 * If this was not an async thread, then try again to
5178 		 * write out the pages, but this time, also destroy
5179 		 * them whether or not the write is successful.  This
5180 		 * will prevent memory from filling up with these
5181 		 * pages and destroying them is the only alternative
5182 		 * if they can't be written out.
5183 		 *
5184 		 * Don't do this if this is an async thread because
5185 		 * when the pages are unlocked in pvn_write_done,
5186 		 * some other thread could have come along, locked
5187 		 * them, and queued for an async thread.  It would be
5188 		 * possible for all of the async threads to be tied
5189 		 * up waiting to lock the pages again and they would
5190 		 * all already be locked and waiting for an async
5191 		 * thread to handle them.  Deadlock.
5192 		 */
5193 		if (!(flags & B_ASYNC)) {
5194 			error = nfs3_putpage(vp, io_off, io_len,
5195 			    B_INVAL | B_FORCE, cr, NULL);
5196 		}
5197 	} else {
5198 		if (error)
5199 			flags |= B_ERROR;
5200 		else if (rp->r_flags & ROUTOFSPACE) {
5201 			mutex_enter(&rp->r_statelock);
5202 			rp->r_flags &= ~ROUTOFSPACE;
5203 			mutex_exit(&rp->r_statelock);
5204 		}
5205 		pvn_write_done(pp, flags);
5206 		if (freemem < desfree)
5207 			(void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr);
5208 	}
5209 
5210 	return (error);
5211 }
5212 
5213 /* ARGSUSED */
5214 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)5215 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5216     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5217     cred_t *cr, caller_context_t *ct)
5218 {
5219 	struct segvn_crargs vn_a;
5220 	int error;
5221 	rnode_t *rp;
5222 	struct vattr va;
5223 
5224 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5225 		return (EIO);
5226 
5227 	if (vp->v_flag & VNOMAP)
5228 		return (ENOSYS);
5229 
5230 	if (off < 0 || off + len < 0)
5231 		return (ENXIO);
5232 
5233 	if (vp->v_type != VREG)
5234 		return (ENODEV);
5235 
5236 	/*
5237 	 * If there is cached data and if close-to-open consistency
5238 	 * checking is not turned off and if the file system is not
5239 	 * mounted readonly, then force an over the wire getattr.
5240 	 * Otherwise, just invoke nfs3getattr to get a copy of the
5241 	 * attributes.  The attribute cache will be used unless it
5242 	 * is timed out and if it is, then an over the wire getattr
5243 	 * will be issued.
5244 	 */
5245 	va.va_mask = AT_ALL;
5246 	if (vn_has_cached_data(vp) &&
5247 	    !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
5248 		error = nfs3_getattr_otw(vp, &va, cr);
5249 	else
5250 		error = nfs3getattr(vp, &va, cr);
5251 	if (error)
5252 		return (error);
5253 
5254 	/*
5255 	 * Check to see if the vnode is currently marked as not cachable.
5256 	 * This means portions of the file are locked (through VOP_FRLOCK).
5257 	 * In this case the map request must be refused.  We use
5258 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5259 	 */
5260 	rp = VTOR(vp);
5261 
5262 	/*
5263 	 * Atomically increment r_inmap after acquiring r_rwlock. The
5264 	 * idea here is to acquire r_rwlock to block read/write and
5265 	 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5266 	 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
5267 	 * and we can prevent the deadlock that would have occurred
5268 	 * when nfs3_addmap() would have acquired it out of order.
5269 	 *
5270 	 * Since we are not protecting r_inmap by any lock, we do not
5271 	 * hold any lock when we decrement it. We atomically decrement
5272 	 * r_inmap after we release r_lkserlock.
5273 	 */
5274 
5275 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
5276 		return (EINTR);
5277 	atomic_inc_uint(&rp->r_inmap);
5278 	nfs_rw_exit(&rp->r_rwlock);
5279 
5280 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
5281 		atomic_dec_uint(&rp->r_inmap);
5282 		return (EINTR);
5283 	}
5284 
5285 	if (vp->v_flag & VNOCACHE) {
5286 		error = EAGAIN;
5287 		goto done;
5288 	}
5289 
5290 	/*
5291 	 * Don't allow concurrent locks and mapping if mandatory locking is
5292 	 * enabled.
5293 	 */
5294 	if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
5295 	    MANDLOCK(vp, va.va_mode)) {
5296 		error = EAGAIN;
5297 		goto done;
5298 	}
5299 
5300 	as_rangelock(as);
5301 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5302 	if (error != 0) {
5303 		as_rangeunlock(as);
5304 		goto done;
5305 	}
5306 
5307 	vn_a.vp = vp;
5308 	vn_a.offset = off;
5309 	vn_a.type = (flags & MAP_TYPE);
5310 	vn_a.prot = (uchar_t)prot;
5311 	vn_a.maxprot = (uchar_t)maxprot;
5312 	vn_a.flags = (flags & ~MAP_TYPE);
5313 	vn_a.cred = cr;
5314 	vn_a.amp = NULL;
5315 	vn_a.szc = 0;
5316 	vn_a.lgrp_mem_policy_flags = 0;
5317 
5318 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
5319 	as_rangeunlock(as);
5320 
5321 done:
5322 	nfs_rw_exit(&rp->r_lkserlock);
5323 	atomic_dec_uint(&rp->r_inmap);
5324 	return (error);
5325 }
5326 
5327 /* ARGSUSED */
5328 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)5329 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5330     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5331     cred_t *cr, caller_context_t *ct)
5332 {
5333 	rnode_t *rp;
5334 
5335 	if (vp->v_flag & VNOMAP)
5336 		return (ENOSYS);
5337 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5338 		return (EIO);
5339 
5340 	rp = VTOR(vp);
5341 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
5342 
5343 	return (0);
5344 }
5345 
5346 /* ARGSUSED */
5347 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)5348 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5349     offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
5350     caller_context_t *ct)
5351 {
5352 	netobj lm_fh3;
5353 	int rc;
5354 	u_offset_t start, end;
5355 	rnode_t *rp;
5356 	int error = 0, intr = INTR(vp);
5357 
5358 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5359 		return (EIO);
5360 
5361 	/* check for valid cmd parameter */
5362 	switch (cmd) {
5363 	case F_FLOCK:
5364 	case F_FLOCKW:
5365 	case F_OFD_GETLK:
5366 	case F_OFD_SETLK:
5367 	case F_OFD_SETLKW:
5368 		return (EOPNOTSUPP);
5369 	case F_GETLK:
5370 	case F_SETLK:
5371 	case F_SETLKW:
5372 		break;
5373 	default:
5374 		return (EINVAL);
5375 	}
5376 
5377 	/* Verify l_type. */
5378 	switch (bfp->l_type) {
5379 	case F_RDLCK:
5380 		if (cmd != F_GETLK && !(flag & FREAD))
5381 			return (EBADF);
5382 		break;
5383 	case F_WRLCK:
5384 		if (cmd != F_GETLK && !(flag & FWRITE))
5385 			return (EBADF);
5386 		break;
5387 	case F_UNLCK:
5388 		intr = 0;
5389 		break;
5390 
5391 	default:
5392 		return (EINVAL);
5393 	}
5394 
5395 	/* check the validity of the lock range */
5396 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
5397 		return (rc);
5398 	if (rc = flk_check_lock_data(start, end, MAXEND))
5399 		return (rc);
5400 
5401 	/*
5402 	 * If the filesystem is mounted using local locking, pass the
5403 	 * request off to the local locking code.
5404 	 */
5405 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
5406 		if (cmd == F_SETLK || cmd == F_SETLKW) {
5407 			/*
5408 			 * For complete safety, we should be holding
5409 			 * r_lkserlock.  However, we can't call
5410 			 * lm_safelock and then fs_frlock while
5411 			 * holding r_lkserlock, so just invoke
5412 			 * lm_safelock and expect that this will
5413 			 * catch enough of the cases.
5414 			 */
5415 			if (!lm_safelock(vp, bfp, cr))
5416 				return (EAGAIN);
5417 		}
5418 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
5419 	}
5420 
5421 	rp = VTOR(vp);
5422 
5423 	/*
5424 	 * Check whether the given lock request can proceed, given the
5425 	 * current file mappings.
5426 	 */
5427 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
5428 		return (EINTR);
5429 	if (cmd == F_SETLK || cmd == F_SETLKW) {
5430 		if (!lm_safelock(vp, bfp, cr)) {
5431 			rc = EAGAIN;
5432 			goto done;
5433 		}
5434 	}
5435 
5436 	/*
5437 	 * Flush the cache after waiting for async I/O to finish.  For new
5438 	 * locks, this is so that the process gets the latest bits from the
5439 	 * server.  For unlocks, this is so that other clients see the
5440 	 * latest bits once the file has been unlocked.  If currently dirty
5441 	 * pages can't be flushed, then don't allow a lock to be set.  But
5442 	 * allow unlocks to succeed, to avoid having orphan locks on the
5443 	 * server.
5444 	 */
5445 	if (cmd != F_GETLK) {
5446 		mutex_enter(&rp->r_statelock);
5447 		while (rp->r_count > 0) {
5448 			if (intr) {
5449 				klwp_t *lwp = ttolwp(curthread);
5450 
5451 				if (lwp != NULL)
5452 					lwp->lwp_nostop++;
5453 				if (cv_wait_sig(&rp->r_cv,
5454 				    &rp->r_statelock) == 0) {
5455 					if (lwp != NULL)
5456 						lwp->lwp_nostop--;
5457 					rc = EINTR;
5458 					break;
5459 				}
5460 				if (lwp != NULL)
5461 					lwp->lwp_nostop--;
5462 			} else
5463 				cv_wait(&rp->r_cv, &rp->r_statelock);
5464 		}
5465 		mutex_exit(&rp->r_statelock);
5466 		if (rc != 0)
5467 			goto done;
5468 		error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
5469 		if (error) {
5470 			if (error == ENOSPC || error == EDQUOT) {
5471 				mutex_enter(&rp->r_statelock);
5472 				if (!rp->r_error)
5473 					rp->r_error = error;
5474 				mutex_exit(&rp->r_statelock);
5475 			}
5476 			if (bfp->l_type != F_UNLCK) {
5477 				rc = ENOLCK;
5478 				goto done;
5479 			}
5480 		}
5481 	}
5482 
5483 	lm_fh3.n_len = VTOFH3(vp)->fh3_length;
5484 	lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
5485 
5486 	/*
5487 	 * Call the lock manager to do the real work of contacting
5488 	 * the server and obtaining the lock.
5489 	 */
5490 	rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp);
5491 
5492 	if (rc == 0)
5493 		nfs_lockcompletion(vp, cmd);
5494 
5495 done:
5496 	nfs_rw_exit(&rp->r_lkserlock);
5497 	return (rc);
5498 }
5499 
5500 /*
5501  * Free storage space associated with the specified vnode.  The portion
5502  * to be freed is specified by bfp->l_start and bfp->l_len (already
5503  * normalized to a "whence" of 0).
5504  *
5505  * This is an experimental facility whose continued existence is not
5506  * guaranteed.  Currently, we only support the special case
5507  * of l_len == 0, meaning free to end of file.
5508  */
5509 /* ARGSUSED */
5510 static int
nfs3_space(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)5511 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5512     offset_t offset, cred_t *cr, caller_context_t *ct)
5513 {
5514 	int error;
5515 
5516 	ASSERT(vp->v_type == VREG);
5517 	if (cmd != F_FREESP)
5518 		return (EINVAL);
5519 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5520 		return (EIO);
5521 
5522 	error = convoff(vp, bfp, 0, offset);
5523 	if (!error) {
5524 		ASSERT(bfp->l_start >= 0);
5525 		if (bfp->l_len == 0) {
5526 			struct vattr va;
5527 
5528 			/*
5529 			 * ftruncate should not change the ctime and
5530 			 * mtime if we truncate the file to its
5531 			 * previous size.
5532 			 */
5533 			va.va_mask = AT_SIZE;
5534 			error = nfs3getattr(vp, &va, cr);
5535 			if (error || va.va_size == bfp->l_start)
5536 				return (error);
5537 			va.va_mask = AT_SIZE;
5538 			va.va_size = bfp->l_start;
5539 			error = nfs3setattr(vp, &va, 0, cr);
5540 
5541 			if (error == 0 && bfp->l_start == 0)
5542 				vnevent_truncate(vp, ct);
5543 		} else
5544 			error = EINVAL;
5545 	}
5546 
5547 	return (error);
5548 }
5549 
5550 /* ARGSUSED */
5551 static int
nfs3_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)5552 nfs3_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
5553 {
5554 
5555 	return (EINVAL);
5556 }
5557 
5558 /*
5559  * Setup and add an address space callback to do the work of the delmap call.
5560  * The callback will (and must be) deleted in the actual callback function.
5561  *
5562  * This is done in order to take care of the problem that we have with holding
5563  * the address space's a_lock for a long period of time (e.g. if the NFS server
5564  * is down).  Callbacks will be executed in the address space code while the
5565  * a_lock is not held.	Holding the address space's a_lock causes things such
5566  * as ps and fork to hang because they are trying to acquire this lock as well.
5567  */
5568 /* ARGSUSED */
5569 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)5570 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5571     size_t len, uint_t prot, uint_t maxprot, uint_t flags,
5572     cred_t *cr, caller_context_t *ct)
5573 {
5574 	int			caller_found;
5575 	int			error;
5576 	rnode_t			*rp;
5577 	nfs_delmap_args_t	*dmapp;
5578 	nfs_delmapcall_t	*delmap_call;
5579 
5580 	if (vp->v_flag & VNOMAP)
5581 		return (ENOSYS);
5582 	/*
5583 	 * A process may not change zones if it has NFS pages mmap'ed
5584 	 * in, so we can't legitimately get here from the wrong zone.
5585 	 */
5586 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5587 
5588 	rp = VTOR(vp);
5589 
5590 	/*
5591 	 * The way that the address space of this process deletes its mapping
5592 	 * of this file is via the following call chains:
5593 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5594 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5595 	 *
5596 	 * With the use of address space callbacks we are allowed to drop the
5597 	 * address space lock, a_lock, while executing the NFS operations that
5598 	 * need to go over the wire.  Returning EAGAIN to the caller of this
5599 	 * function is what drives the execution of the callback that we add
5600 	 * below.  The callback will be executed by the address space code
5601 	 * after dropping the a_lock.  When the callback is finished, since
5602 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5603 	 * is called again on the same segment to finish the rest of the work
5604 	 * that needs to happen during unmapping.
5605 	 *
5606 	 * This action of calling back into the segment driver causes
5607 	 * nfs3_delmap() to get called again, but since the callback was
5608 	 * already executed at this point, it already did the work and there
5609 	 * is nothing left for us to do.
5610 	 *
5611 	 * To Summarize:
5612 	 * - The first time nfs3_delmap is called by the current thread is when
5613 	 * we add the caller associated with this delmap to the delmap caller
5614 	 * list, add the callback, and return EAGAIN.
5615 	 * - The second time in this call chain when nfs3_delmap is called we
5616 	 * will find this caller in the delmap caller list and realize there
5617 	 * is no more work to do thus removing this caller from the list and
5618 	 * returning the error that was set in the callback execution.
5619 	 */
5620 	caller_found = nfs_find_and_delete_delmapcall(rp, &error);
5621 	if (caller_found) {
5622 		/*
5623 		 * 'error' is from the actual delmap operations.  To avoid
5624 		 * hangs, we need to handle the return of EAGAIN differently
5625 		 * since this is what drives the callback execution.
5626 		 * In this case, we don't want to return EAGAIN and do the
5627 		 * callback execution because there are none to execute.
5628 		 */
5629 		if (error == EAGAIN)
5630 			return (0);
5631 		else
5632 			return (error);
5633 	}
5634 
5635 	/* current caller was not in the list */
5636 	delmap_call = nfs_init_delmapcall();
5637 
5638 	mutex_enter(&rp->r_statelock);
5639 	list_insert_tail(&rp->r_indelmap, delmap_call);
5640 	mutex_exit(&rp->r_statelock);
5641 
5642 	dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
5643 
5644 	dmapp->vp = vp;
5645 	dmapp->off = off;
5646 	dmapp->addr = addr;
5647 	dmapp->len = len;
5648 	dmapp->prot = prot;
5649 	dmapp->maxprot = maxprot;
5650 	dmapp->flags = flags;
5651 	dmapp->cr = cr;
5652 	dmapp->caller = delmap_call;
5653 
5654 	error = as_add_callback(as, nfs3_delmap_callback, dmapp,
5655 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
5656 
5657 	return (error ? error : EAGAIN);
5658 }
5659 
5660 /*
5661  * Remove some pages from an mmap'd vnode.  Just update the
5662  * count of pages.  If doing close-to-open, then flush and
5663  * commit all of the pages associated with this file.
5664  * Otherwise, start an asynchronous page flush to write out
5665  * any dirty pages.  This will also associate a credential
5666  * with the rnode which can be used to write the pages.
5667  */
5668 /* ARGSUSED */
5669 static void
nfs3_delmap_callback(struct as * as,void * arg,uint_t event)5670 nfs3_delmap_callback(struct as *as, void *arg, uint_t event)
5671 {
5672 	int			error;
5673 	rnode_t			*rp;
5674 	mntinfo_t		*mi;
5675 	nfs_delmap_args_t	*dmapp = (nfs_delmap_args_t *)arg;
5676 
5677 	rp = VTOR(dmapp->vp);
5678 	mi = VTOMI(dmapp->vp);
5679 
5680 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
5681 	ASSERT(rp->r_mapcnt >= 0);
5682 
5683 	/*
5684 	 * Initiate a page flush and potential commit if there are
5685 	 * pages, the file system was not mounted readonly, the segment
5686 	 * was mapped shared, and the pages themselves were writeable.
5687 	 */
5688 	if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
5689 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
5690 		mutex_enter(&rp->r_statelock);
5691 		rp->r_flags |= RDIRTY;
5692 		mutex_exit(&rp->r_statelock);
5693 		/*
5694 		 * If this is a cross-zone access a sync putpage won't work, so
5695 		 * the best we can do is try an async putpage.  That seems
5696 		 * better than something more draconian such as discarding the
5697 		 * dirty pages.
5698 		 */
5699 		if ((mi->mi_flags & MI_NOCTO) ||
5700 		    nfs_zone() != mi->mi_zone)
5701 			error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5702 			    B_ASYNC, dmapp->cr, NULL);
5703 		else
5704 			error = nfs3_putpage_commit(dmapp->vp, dmapp->off,
5705 			    dmapp->len, dmapp->cr);
5706 		if (!error) {
5707 			mutex_enter(&rp->r_statelock);
5708 			error = rp->r_error;
5709 			rp->r_error = 0;
5710 			mutex_exit(&rp->r_statelock);
5711 		}
5712 	} else
5713 		error = 0;
5714 
5715 	if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
5716 		(void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5717 		    B_INVAL, dmapp->cr, NULL);
5718 
5719 	dmapp->caller->error = error;
5720 	(void) as_delete_callback(as, arg);
5721 	kmem_free(dmapp, sizeof (nfs_delmap_args_t));
5722 }
5723 
5724 static int nfs3_pathconf_disable_cache = 0;
5725 
5726 #ifdef DEBUG
5727 static int nfs3_pathconf_cache_hits = 0;
5728 static int nfs3_pathconf_cache_misses = 0;
5729 #endif
5730 
5731 /* ARGSUSED */
5732 static int
nfs3_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)5733 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5734     caller_context_t *ct)
5735 {
5736 	int error;
5737 	PATHCONF3args args;
5738 	PATHCONF3res res;
5739 	int douprintf;
5740 	failinfo_t fi;
5741 	rnode_t *rp;
5742 	hrtime_t t;
5743 
5744 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5745 		return (EIO);
5746 	/*
5747 	 * Large file spec - need to base answer on info stored
5748 	 * on original FSINFO response.
5749 	 */
5750 	if (cmd == _PC_FILESIZEBITS) {
5751 		unsigned long long ll;
5752 		long l = 1;
5753 
5754 		ll = VTOMI(vp)->mi_maxfilesize;
5755 
5756 		if (ll == 0) {
5757 			*valp = 0;
5758 			return (0);
5759 		}
5760 
5761 		if (ll & 0xffffffff00000000) {
5762 			l += 32; ll >>= 32;
5763 		}
5764 		if (ll & 0xffff0000) {
5765 			l += 16; ll >>= 16;
5766 		}
5767 		if (ll & 0xff00) {
5768 			l += 8; ll >>= 8;
5769 		}
5770 		if (ll & 0xf0) {
5771 			l += 4; ll >>= 4;
5772 		}
5773 		if (ll & 0xc) {
5774 			l += 2; ll >>= 2;
5775 		}
5776 		if (ll & 0x2)
5777 			l += 2;
5778 		else if (ll & 0x1)
5779 			l += 1;
5780 		*valp = l;
5781 		return (0);
5782 	}
5783 
5784 	if (cmd == _PC_ACL_ENABLED) {
5785 		*valp = _ACL_ACLENT_ENABLED;
5786 		return (0);
5787 	}
5788 
5789 	if (cmd == _PC_XATTR_EXISTS) {
5790 		error = 0;
5791 		*valp = 0;
5792 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5793 			vnode_t *avp;
5794 			rnode_t *rp;
5795 			int error = 0;
5796 			mntinfo_t *mi = VTOMI(vp);
5797 
5798 			if (!(mi->mi_flags & MI_EXTATTR))
5799 				return (0);
5800 
5801 			rp = VTOR(vp);
5802 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
5803 			    INTR(vp)))
5804 				return (EINTR);
5805 
5806 			error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
5807 			if (error || avp == NULL)
5808 				error = acl_getxattrdir3(vp, &avp, 0, cr, 0);
5809 
5810 			nfs_rw_exit(&rp->r_rwlock);
5811 
5812 			if (error == 0 && avp != NULL) {
5813 				error = do_xattr_exists_check(avp, valp, cr);
5814 				VN_RELE(avp);
5815 			} else if (error == ENOENT) {
5816 				error = 0;
5817 				*valp = 0;
5818 			}
5819 		}
5820 		return (error);
5821 	}
5822 
5823 	rp = VTOR(vp);
5824 	if (rp->r_pathconf != NULL) {
5825 		mutex_enter(&rp->r_statelock);
5826 		if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) {
5827 			kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf));
5828 			rp->r_pathconf = NULL;
5829 		}
5830 		if (rp->r_pathconf != NULL) {
5831 			error = 0;
5832 			switch (cmd) {
5833 			case _PC_LINK_MAX:
5834 				*valp = rp->r_pathconf->link_max;
5835 				break;
5836 			case _PC_NAME_MAX:
5837 				*valp = rp->r_pathconf->name_max;
5838 				break;
5839 			case _PC_PATH_MAX:
5840 			case _PC_SYMLINK_MAX:
5841 				*valp = MAXPATHLEN;
5842 				break;
5843 			case _PC_CHOWN_RESTRICTED:
5844 				*valp = rp->r_pathconf->chown_restricted;
5845 				break;
5846 			case _PC_NO_TRUNC:
5847 				*valp = rp->r_pathconf->no_trunc;
5848 				break;
5849 			default:
5850 				error = EINVAL;
5851 				break;
5852 			}
5853 			mutex_exit(&rp->r_statelock);
5854 #ifdef DEBUG
5855 			nfs3_pathconf_cache_hits++;
5856 #endif
5857 			return (error);
5858 		}
5859 		mutex_exit(&rp->r_statelock);
5860 	}
5861 #ifdef DEBUG
5862 	nfs3_pathconf_cache_misses++;
5863 #endif
5864 
5865 	args.object = *VTOFH3(vp);
5866 	fi.vp = vp;
5867 	fi.fhp = (caddr_t)&args.object;
5868 	fi.copyproc = nfs3copyfh;
5869 	fi.lookupproc = nfs3lookup;
5870 	fi.xattrdirproc = acl_getxattrdir3;
5871 
5872 	douprintf = 1;
5873 
5874 	t = gethrtime();
5875 
5876 	error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF,
5877 	    xdr_nfs_fh3, (caddr_t)&args,
5878 	    xdr_PATHCONF3res, (caddr_t)&res, cr,
5879 	    &douprintf, &res.status, 0, &fi);
5880 
5881 	if (error)
5882 		return (error);
5883 
5884 	error = geterrno3(res.status);
5885 
5886 	if (!error) {
5887 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
5888 		if (!nfs3_pathconf_disable_cache) {
5889 			mutex_enter(&rp->r_statelock);
5890 			if (rp->r_pathconf == NULL) {
5891 				rp->r_pathconf = kmem_alloc(
5892 				    sizeof (*rp->r_pathconf), KM_NOSLEEP);
5893 				if (rp->r_pathconf != NULL)
5894 					*rp->r_pathconf = res.resok.info;
5895 			}
5896 			mutex_exit(&rp->r_statelock);
5897 		}
5898 		switch (cmd) {
5899 		case _PC_LINK_MAX:
5900 			*valp = res.resok.info.link_max;
5901 			break;
5902 		case _PC_NAME_MAX:
5903 			*valp = res.resok.info.name_max;
5904 			break;
5905 		case _PC_PATH_MAX:
5906 		case _PC_SYMLINK_MAX:
5907 			*valp = MAXPATHLEN;
5908 			break;
5909 		case _PC_CHOWN_RESTRICTED:
5910 			*valp = res.resok.info.chown_restricted;
5911 			break;
5912 		case _PC_NO_TRUNC:
5913 			*valp = res.resok.info.no_trunc;
5914 			break;
5915 		default:
5916 			return (EINVAL);
5917 		}
5918 	} else {
5919 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
5920 		PURGE_STALE_FH(error, vp, cr);
5921 	}
5922 
5923 	return (error);
5924 }
5925 
5926 /*
5927  * Called by async thread to do synchronous pageio. Do the i/o, wait
5928  * for it to complete, and cleanup the page list when done.
5929  */
5930 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)5931 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5932     int flags, cred_t *cr)
5933 {
5934 	int error;
5935 
5936 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5937 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5938 	if (flags & B_READ)
5939 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
5940 	else
5941 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
5942 	return (error);
5943 }
5944 
5945 /* ARGSUSED */
5946 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)5947 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5948     int flags, cred_t *cr, caller_context_t *ct)
5949 {
5950 	int error;
5951 	rnode_t *rp;
5952 
5953 	if (pp == NULL)
5954 		return (EINVAL);
5955 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5956 		return (EIO);
5957 
5958 	rp = VTOR(vp);
5959 	mutex_enter(&rp->r_statelock);
5960 	rp->r_count++;
5961 	mutex_exit(&rp->r_statelock);
5962 
5963 	if (flags & B_ASYNC) {
5964 		error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
5965 		    nfs3_sync_pageio);
5966 	} else
5967 		error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5968 	mutex_enter(&rp->r_statelock);
5969 	rp->r_count--;
5970 	cv_broadcast(&rp->r_cv);
5971 	mutex_exit(&rp->r_statelock);
5972 	return (error);
5973 }
5974 
5975 /* ARGSUSED */
5976 static void
nfs3_dispose(vnode_t * vp,page_t * pp,int fl,int dn,cred_t * cr,caller_context_t * ct)5977 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
5978     caller_context_t *ct)
5979 {
5980 	int error;
5981 	rnode_t *rp;
5982 	page_t *plist;
5983 	page_t *pptr;
5984 	offset3 offset;
5985 	count3 len;
5986 	k_sigset_t smask;
5987 
5988 	/*
5989 	 * We should get called with fl equal to either B_FREE or
5990 	 * B_INVAL.  Any other value is illegal.
5991 	 *
5992 	 * The page that we are either supposed to free or destroy
5993 	 * should be exclusive locked and its io lock should not
5994 	 * be held.
5995 	 */
5996 	ASSERT(fl == B_FREE || fl == B_INVAL);
5997 	ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
5998 	rp = VTOR(vp);
5999 
6000 	/*
6001 	 * If the page doesn't need to be committed or we shouldn't
6002 	 * even bother attempting to commit it, then just make sure
6003 	 * that the p_fsdata byte is clear and then either free or
6004 	 * destroy the page as appropriate.
6005 	 */
6006 	if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) {
6007 		pp->p_fsdata = C_NOCOMMIT;
6008 		if (fl == B_FREE)
6009 			page_free(pp, dn);
6010 		else
6011 			page_destroy(pp, dn);
6012 		return;
6013 	}
6014 
6015 	/*
6016 	 * If there is a page invalidation operation going on, then
6017 	 * if this is one of the pages being destroyed, then just
6018 	 * clear the p_fsdata byte and then either free or destroy
6019 	 * the page as appropriate.
6020 	 */
6021 	mutex_enter(&rp->r_statelock);
6022 	if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
6023 		mutex_exit(&rp->r_statelock);
6024 		pp->p_fsdata = C_NOCOMMIT;
6025 		if (fl == B_FREE)
6026 			page_free(pp, dn);
6027 		else
6028 			page_destroy(pp, dn);
6029 		return;
6030 	}
6031 
6032 	/*
6033 	 * If we are freeing this page and someone else is already
6034 	 * waiting to do a commit, then just unlock the page and
6035 	 * return.  That other thread will take care of commiting
6036 	 * this page.  The page can be freed sometime after the
6037 	 * commit has finished.  Otherwise, if the page is marked
6038 	 * as delay commit, then we may be getting called from
6039 	 * pvn_write_done, one page at a time.   This could result
6040 	 * in one commit per page, so we end up doing lots of small
6041 	 * commits instead of fewer larger commits.  This is bad,
6042 	 * we want do as few commits as possible.
6043 	 */
6044 	if (fl == B_FREE) {
6045 		if (rp->r_flags & RCOMMITWAIT) {
6046 			page_unlock(pp);
6047 			mutex_exit(&rp->r_statelock);
6048 			return;
6049 		}
6050 		if (pp->p_fsdata == C_DELAYCOMMIT) {
6051 			pp->p_fsdata = C_COMMIT;
6052 			page_unlock(pp);
6053 			mutex_exit(&rp->r_statelock);
6054 			return;
6055 		}
6056 	}
6057 
6058 	/*
6059 	 * Check to see if there is a signal which would prevent an
6060 	 * attempt to commit the pages from being successful.  If so,
6061 	 * then don't bother with all of the work to gather pages and
6062 	 * generate the unsuccessful RPC.  Just return from here and
6063 	 * let the page be committed at some later time.
6064 	 */
6065 	sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
6066 	if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
6067 		sigunintr(&smask);
6068 		page_unlock(pp);
6069 		mutex_exit(&rp->r_statelock);
6070 		return;
6071 	}
6072 	sigunintr(&smask);
6073 
6074 	/*
6075 	 * We are starting to need to commit pages, so let's try
6076 	 * to commit as many as possible at once to reduce the
6077 	 * overhead.
6078 	 *
6079 	 * Set the `commit inprogress' state bit.  We must
6080 	 * first wait until any current one finishes.  Then
6081 	 * we initialize the c_pages list with this page.
6082 	 */
6083 	while (rp->r_flags & RCOMMIT) {
6084 		rp->r_flags |= RCOMMITWAIT;
6085 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6086 		rp->r_flags &= ~RCOMMITWAIT;
6087 	}
6088 	rp->r_flags |= RCOMMIT;
6089 	mutex_exit(&rp->r_statelock);
6090 	ASSERT(rp->r_commit.c_pages == NULL);
6091 	rp->r_commit.c_pages = pp;
6092 	rp->r_commit.c_commbase = (offset3)pp->p_offset;
6093 	rp->r_commit.c_commlen = PAGESIZE;
6094 
6095 	/*
6096 	 * Gather together all other pages which can be committed.
6097 	 * They will all be chained off r_commit.c_pages.
6098 	 */
6099 	nfs3_get_commit(vp);
6100 
6101 	/*
6102 	 * Clear the `commit inprogress' status and disconnect
6103 	 * the list of pages to be committed from the rnode.
6104 	 * At this same time, we also save the starting offset
6105 	 * and length of data to be committed on the server.
6106 	 */
6107 	plist = rp->r_commit.c_pages;
6108 	rp->r_commit.c_pages = NULL;
6109 	offset = rp->r_commit.c_commbase;
6110 	len = rp->r_commit.c_commlen;
6111 	mutex_enter(&rp->r_statelock);
6112 	rp->r_flags &= ~RCOMMIT;
6113 	cv_broadcast(&rp->r_commit.c_cv);
6114 	mutex_exit(&rp->r_statelock);
6115 
6116 	if (curproc == proc_pageout || curproc == proc_fsflush ||
6117 	    nfs_zone() != VTOMI(vp)->mi_zone) {
6118 		nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit);
6119 		return;
6120 	}
6121 
6122 	/*
6123 	 * Actually generate the COMMIT3 over the wire operation.
6124 	 */
6125 	error = nfs3_commit(vp, offset, len, cr);
6126 
6127 	/*
6128 	 * If we got an error during the commit, just unlock all
6129 	 * of the pages.  The pages will get retransmitted to the
6130 	 * server during a putpage operation.
6131 	 */
6132 	if (error) {
6133 		while (plist != NULL) {
6134 			pptr = plist;
6135 			page_sub(&plist, pptr);
6136 			page_unlock(pptr);
6137 		}
6138 		return;
6139 	}
6140 
6141 	/*
6142 	 * We've tried as hard as we can to commit the data to stable
6143 	 * storage on the server.  We release the rest of the pages
6144 	 * and clear the commit required state.  They will be put
6145 	 * onto the tail of the cachelist if they are nolonger
6146 	 * mapped.
6147 	 */
6148 	while (plist != pp) {
6149 		pptr = plist;
6150 		page_sub(&plist, pptr);
6151 		pptr->p_fsdata = C_NOCOMMIT;
6152 		(void) page_release(pptr, 1);
6153 	}
6154 
6155 	/*
6156 	 * It is possible that nfs3_commit didn't return error but
6157 	 * some other thread has modified the page we are going
6158 	 * to free/destroy.
6159 	 *    In this case we need to rewrite the page. Do an explicit check
6160 	 * before attempting to free/destroy the page. If modified, needs to
6161 	 * be rewritten so unlock the page and return.
6162 	 */
6163 	if (hat_ismod(pp)) {
6164 		pp->p_fsdata = C_NOCOMMIT;
6165 		page_unlock(pp);
6166 		return;
6167 	}
6168 
6169 	/*
6170 	 * Now, as appropriate, either free or destroy the page
6171 	 * that we were called with.
6172 	 */
6173 	pp->p_fsdata = C_NOCOMMIT;
6174 	if (fl == B_FREE)
6175 		page_free(pp, dn);
6176 	else
6177 		page_destroy(pp, dn);
6178 }
6179 
6180 static int
nfs3_commit(vnode_t * vp,offset3 offset,count3 count,cred_t * cr)6181 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr)
6182 {
6183 	int error;
6184 	rnode_t *rp;
6185 	COMMIT3args args;
6186 	COMMIT3res res;
6187 	int douprintf;
6188 	cred_t *cred;
6189 
6190 	rp = VTOR(vp);
6191 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6192 
6193 	mutex_enter(&rp->r_statelock);
6194 	if (rp->r_cred != NULL) {
6195 		cred = rp->r_cred;
6196 		crhold(cred);
6197 	} else {
6198 		rp->r_cred = cr;
6199 		crhold(cr);
6200 		cred = cr;
6201 		crhold(cred);
6202 	}
6203 	mutex_exit(&rp->r_statelock);
6204 
6205 	args.file = *VTOFH3(vp);
6206 	args.offset = offset;
6207 	args.count = count;
6208 
6209 doitagain:
6210 	douprintf = 1;
6211 	error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT,
6212 	    xdr_COMMIT3args, (caddr_t)&args,
6213 	    xdr_COMMIT3res, (caddr_t)&res, cred,
6214 	    &douprintf, &res.status, 0, NULL);
6215 
6216 	crfree(cred);
6217 
6218 	if (error)
6219 		return (error);
6220 
6221 	error = geterrno3(res.status);
6222 	if (!error) {
6223 		ASSERT(rp->r_flags & RHAVEVERF);
6224 		mutex_enter(&rp->r_statelock);
6225 		if (rp->r_verf == res.resok.verf) {
6226 			mutex_exit(&rp->r_statelock);
6227 			return (0);
6228 		}
6229 		nfs3_set_mod(vp);
6230 		rp->r_verf = res.resok.verf;
6231 		mutex_exit(&rp->r_statelock);
6232 		error = NFS_VERF_MISMATCH;
6233 	} else {
6234 		if (error == EACCES) {
6235 			mutex_enter(&rp->r_statelock);
6236 			if (cred != cr) {
6237 				if (rp->r_cred != NULL)
6238 					crfree(rp->r_cred);
6239 				rp->r_cred = cr;
6240 				crhold(cr);
6241 				cred = cr;
6242 				crhold(cred);
6243 				mutex_exit(&rp->r_statelock);
6244 				goto doitagain;
6245 			}
6246 			mutex_exit(&rp->r_statelock);
6247 		}
6248 		/*
6249 		 * Can't do a PURGE_STALE_FH here because this
6250 		 * can cause a deadlock.  nfs3_commit can
6251 		 * be called from nfs3_dispose which can be called
6252 		 * indirectly via pvn_vplist_dirty.  PURGE_STALE_FH
6253 		 * can call back to pvn_vplist_dirty.
6254 		 */
6255 		if (error == ESTALE) {
6256 			mutex_enter(&rp->r_statelock);
6257 			rp->r_flags |= RSTALE;
6258 			if (!rp->r_error)
6259 				rp->r_error = error;
6260 			mutex_exit(&rp->r_statelock);
6261 			PURGE_ATTRCACHE(vp);
6262 		} else {
6263 			mutex_enter(&rp->r_statelock);
6264 			if (!rp->r_error)
6265 				rp->r_error = error;
6266 			mutex_exit(&rp->r_statelock);
6267 		}
6268 	}
6269 
6270 	return (error);
6271 }
6272 
6273 static void
nfs3_set_mod(vnode_t * vp)6274 nfs3_set_mod(vnode_t *vp)
6275 {
6276 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6277 
6278 	pvn_vplist_setdirty(vp, nfs_setmod_check);
6279 }
6280 
6281 /*
6282  * This routine is used to gather together a page list of the pages
6283  * which are to be committed on the server.  This routine must not
6284  * be called if the calling thread holds any locked pages.
6285  *
6286  * The calling thread must have set RCOMMIT.  This bit is used to
6287  * serialize access to the commit structure in the rnode.  As long
6288  * as the thread has set RCOMMIT, then it can manipulate the commit
6289  * structure without requiring any other locks.
6290  */
6291 static void
nfs3_get_commit(vnode_t * vp)6292 nfs3_get_commit(vnode_t *vp)
6293 {
6294 	rnode_t *rp;
6295 	page_t *pp;
6296 	kmutex_t *vphm;
6297 
6298 	rp = VTOR(vp);
6299 
6300 	ASSERT(rp->r_flags & RCOMMIT);
6301 
6302 	vphm = page_vnode_mutex(vp);
6303 	mutex_enter(vphm);
6304 
6305 	/*
6306 	 * If there are no pages associated with this vnode, then
6307 	 * just return.
6308 	 */
6309 	if ((pp = vp->v_pages) == NULL) {
6310 		mutex_exit(vphm);
6311 		return;
6312 	}
6313 
6314 	/*
6315 	 * Step through all of the pages associated with this vnode
6316 	 * looking for pages which need to be committed.
6317 	 */
6318 	do {
6319 		/* Skip marker pages. */
6320 		if (pp->p_hash == PVN_VPLIST_HASH_TAG)
6321 			continue;
6322 
6323 		/*
6324 		 * If this page does not need to be committed or is
6325 		 * modified, then just skip it.
6326 		 */
6327 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
6328 			continue;
6329 
6330 		/*
6331 		 * Attempt to lock the page.  If we can't, then
6332 		 * someone else is messing with it and we will
6333 		 * just skip it.
6334 		 */
6335 		if (!page_trylock(pp, SE_EXCL))
6336 			continue;
6337 
6338 		/*
6339 		 * If this page does not need to be committed or is
6340 		 * modified, then just skip it.  Recheck now that
6341 		 * the page is locked.
6342 		 */
6343 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6344 			page_unlock(pp);
6345 			continue;
6346 		}
6347 
6348 		if (PP_ISFREE(pp)) {
6349 			cmn_err(CE_PANIC, "nfs3_get_commit: %p is free",
6350 			    (void *)pp);
6351 		}
6352 
6353 		/*
6354 		 * The page needs to be committed and we locked it.
6355 		 * Update the base and length parameters and add it
6356 		 * to r_pages.
6357 		 */
6358 		if (rp->r_commit.c_pages == NULL) {
6359 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6360 			rp->r_commit.c_commlen = PAGESIZE;
6361 		} else if (pp->p_offset < rp->r_commit.c_commbase) {
6362 			rp->r_commit.c_commlen = rp->r_commit.c_commbase -
6363 			    (offset3)pp->p_offset + rp->r_commit.c_commlen;
6364 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6365 		} else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
6366 		    <= pp->p_offset) {
6367 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6368 			    rp->r_commit.c_commbase + PAGESIZE;
6369 		}
6370 		page_add(&rp->r_commit.c_pages, pp);
6371 	} while ((pp = pp->p_vpnext) != vp->v_pages);
6372 
6373 	mutex_exit(vphm);
6374 }
6375 
6376 /*
6377  * This routine is used to gather together a page list of the pages
6378  * which are to be committed on the server.  This routine must not
6379  * be called if the calling thread holds any locked pages.
6380  *
6381  * The calling thread must have set RCOMMIT.  This bit is used to
6382  * serialize access to the commit structure in the rnode.  As long
6383  * as the thread has set RCOMMIT, then it can manipulate the commit
6384  * structure without requiring any other locks.
6385  */
6386 static void
nfs3_get_commit_range(vnode_t * vp,u_offset_t soff,size_t len)6387 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
6388 {
6389 
6390 	rnode_t *rp;
6391 	page_t *pp;
6392 	u_offset_t end;
6393 	u_offset_t off;
6394 
6395 	ASSERT(len != 0);
6396 
6397 	rp = VTOR(vp);
6398 
6399 	ASSERT(rp->r_flags & RCOMMIT);
6400 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6401 
6402 	/*
6403 	 * If there are no pages associated with this vnode, then
6404 	 * just return.
6405 	 */
6406 	if ((pp = vp->v_pages) == NULL)
6407 		return;
6408 
6409 	/*
6410 	 * Calculate the ending offset.
6411 	 */
6412 	end = soff + len;
6413 
6414 	for (off = soff; off < end; off += PAGESIZE) {
6415 		/*
6416 		 * Lookup each page by vp, offset.
6417 		 */
6418 		if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
6419 			continue;
6420 
6421 		/*
6422 		 * If this page does not need to be committed or is
6423 		 * modified, then just skip it.
6424 		 */
6425 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6426 			page_unlock(pp);
6427 			continue;
6428 		}
6429 
6430 		ASSERT(PP_ISFREE(pp) == 0);
6431 
6432 		/*
6433 		 * The page needs to be committed and we locked it.
6434 		 * Update the base and length parameters and add it
6435 		 * to r_pages.
6436 		 */
6437 		if (rp->r_commit.c_pages == NULL) {
6438 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6439 			rp->r_commit.c_commlen = PAGESIZE;
6440 		} else {
6441 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6442 			    rp->r_commit.c_commbase + PAGESIZE;
6443 		}
6444 		page_add(&rp->r_commit.c_pages, pp);
6445 	}
6446 }
6447 
6448 static int
nfs3_putpage_commit(vnode_t * vp,offset_t poff,size_t plen,cred_t * cr)6449 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
6450 {
6451 	int error;
6452 	writeverf3 write_verf;
6453 	rnode_t *rp = VTOR(vp);
6454 
6455 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6456 	/*
6457 	 * Flush the data portion of the file and then commit any
6458 	 * portions which need to be committed.  This may need to
6459 	 * be done twice if the server has changed state since
6460 	 * data was last written.  The data will need to be
6461 	 * rewritten to the server and then a new commit done.
6462 	 *
6463 	 * In fact, this may need to be done several times if the
6464 	 * server is having problems and crashing while we are
6465 	 * attempting to do this.
6466 	 */
6467 
6468 top:
6469 	/*
6470 	 * Do a flush based on the poff and plen arguments.  This
6471 	 * will asynchronously write out any modified pages in the
6472 	 * range specified by (poff, plen).  This starts all of the
6473 	 * i/o operations which will be waited for in the next
6474 	 * call to nfs3_putpage
6475 	 */
6476 
6477 	mutex_enter(&rp->r_statelock);
6478 	write_verf = rp->r_verf;
6479 	mutex_exit(&rp->r_statelock);
6480 
6481 	error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
6482 	if (error == EAGAIN)
6483 		error = 0;
6484 
6485 	/*
6486 	 * Do a flush based on the poff and plen arguments.  This
6487 	 * will synchronously write out any modified pages in the
6488 	 * range specified by (poff, plen) and wait until all of
6489 	 * the asynchronous i/o's in that range are done as well.
6490 	 */
6491 	if (!error)
6492 		error = nfs3_putpage(vp, poff, plen, 0, cr, NULL);
6493 
6494 	if (error)
6495 		return (error);
6496 
6497 	mutex_enter(&rp->r_statelock);
6498 	if (rp->r_verf != write_verf) {
6499 		mutex_exit(&rp->r_statelock);
6500 		goto top;
6501 	}
6502 	mutex_exit(&rp->r_statelock);
6503 
6504 	/*
6505 	 * Now commit any pages which might need to be committed.
6506 	 * If the error, NFS_VERF_MISMATCH, is returned, then
6507 	 * start over with the flush operation.
6508 	 */
6509 
6510 	error = nfs3_commit_vp(vp, poff, plen, cr);
6511 
6512 	if (error == NFS_VERF_MISMATCH)
6513 		goto top;
6514 
6515 	return (error);
6516 }
6517 
6518 static int
nfs3_commit_vp(vnode_t * vp,u_offset_t poff,size_t plen,cred_t * cr)6519 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr)
6520 {
6521 	rnode_t *rp;
6522 	page_t *plist;
6523 	offset3 offset;
6524 	count3 len;
6525 
6526 
6527 	rp = VTOR(vp);
6528 
6529 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6530 		return (EIO);
6531 	/*
6532 	 * Set the `commit inprogress' state bit.  We must
6533 	 * first wait until any current one finishes.
6534 	 */
6535 	mutex_enter(&rp->r_statelock);
6536 	while (rp->r_flags & RCOMMIT) {
6537 		rp->r_flags |= RCOMMITWAIT;
6538 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6539 		rp->r_flags &= ~RCOMMITWAIT;
6540 	}
6541 	rp->r_flags |= RCOMMIT;
6542 	mutex_exit(&rp->r_statelock);
6543 
6544 	/*
6545 	 * Gather together all of the pages which need to be
6546 	 * committed.
6547 	 */
6548 	if (plen == 0)
6549 		nfs3_get_commit(vp);
6550 	else
6551 		nfs3_get_commit_range(vp, poff, plen);
6552 
6553 	/*
6554 	 * Clear the `commit inprogress' bit and disconnect the
6555 	 * page list which was gathered together in nfs3_get_commit.
6556 	 */
6557 	plist = rp->r_commit.c_pages;
6558 	rp->r_commit.c_pages = NULL;
6559 	offset = rp->r_commit.c_commbase;
6560 	len = rp->r_commit.c_commlen;
6561 	mutex_enter(&rp->r_statelock);
6562 	rp->r_flags &= ~RCOMMIT;
6563 	cv_broadcast(&rp->r_commit.c_cv);
6564 	mutex_exit(&rp->r_statelock);
6565 
6566 	/*
6567 	 * If any pages need to be committed, commit them and
6568 	 * then unlock them so that they can be freed some
6569 	 * time later.
6570 	 */
6571 	if (plist != NULL) {
6572 		/*
6573 		 * No error occurred during the flush portion
6574 		 * of this operation, so now attempt to commit
6575 		 * the data to stable storage on the server.
6576 		 *
6577 		 * This will unlock all of the pages on the list.
6578 		 */
6579 		return (nfs3_sync_commit(vp, plist, offset, len, cr));
6580 	}
6581 	return (0);
6582 }
6583 
6584 static int
nfs3_sync_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)6585 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6586     cred_t *cr)
6587 {
6588 	int error;
6589 	page_t *pp;
6590 
6591 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6592 	error = nfs3_commit(vp, offset, count, cr);
6593 
6594 	/*
6595 	 * If we got an error, then just unlock all of the pages
6596 	 * on the list.
6597 	 */
6598 	if (error) {
6599 		while (plist != NULL) {
6600 			pp = plist;
6601 			page_sub(&plist, pp);
6602 			page_unlock(pp);
6603 		}
6604 		return (error);
6605 	}
6606 	/*
6607 	 * We've tried as hard as we can to commit the data to stable
6608 	 * storage on the server.  We just unlock the pages and clear
6609 	 * the commit required state.  They will get freed later.
6610 	 */
6611 	while (plist != NULL) {
6612 		pp = plist;
6613 		page_sub(&plist, pp);
6614 		pp->p_fsdata = C_NOCOMMIT;
6615 		page_unlock(pp);
6616 	}
6617 
6618 	return (error);
6619 }
6620 
6621 static void
nfs3_async_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)6622 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6623     cred_t *cr)
6624 {
6625 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6626 	(void) nfs3_sync_commit(vp, plist, offset, count, cr);
6627 }
6628 
6629 /* ARGSUSED */
6630 static int
nfs3_setsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)6631 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6632     caller_context_t *ct)
6633 {
6634 	int error;
6635 	mntinfo_t *mi;
6636 
6637 	mi = VTOMI(vp);
6638 
6639 	if (nfs_zone() != mi->mi_zone)
6640 		return (EIO);
6641 
6642 	if (mi->mi_flags & MI_ACL) {
6643 		error = acl_setacl3(vp, vsecattr, flag, cr);
6644 		if (mi->mi_flags & MI_ACL)
6645 			return (error);
6646 	}
6647 
6648 	return (ENOSYS);
6649 }
6650 
6651 /* ARGSUSED */
6652 static int
nfs3_getsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)6653 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6654     caller_context_t *ct)
6655 {
6656 	int error;
6657 	mntinfo_t *mi;
6658 
6659 	mi = VTOMI(vp);
6660 
6661 	if (nfs_zone() != mi->mi_zone)
6662 		return (EIO);
6663 
6664 	if (mi->mi_flags & MI_ACL) {
6665 		error = acl_getacl3(vp, vsecattr, flag, cr);
6666 		if (mi->mi_flags & MI_ACL)
6667 			return (error);
6668 	}
6669 
6670 	return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
6671 }
6672 
6673 /* ARGSUSED */
6674 static int
nfs3_shrlock(vnode_t * vp,int cmd,struct shrlock * shr,int flag,cred_t * cr,caller_context_t * ct)6675 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
6676     caller_context_t *ct)
6677 {
6678 	int error;
6679 	struct shrlock nshr;
6680 	struct nfs_owner nfs_owner;
6681 	netobj lm_fh3;
6682 
6683 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6684 		return (EIO);
6685 
6686 	/*
6687 	 * check for valid cmd parameter
6688 	 */
6689 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
6690 		return (EINVAL);
6691 
6692 	/*
6693 	 * Check access permissions
6694 	 */
6695 	if (cmd == F_SHARE &&
6696 	    (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
6697 	    ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
6698 		return (EBADF);
6699 
6700 	/*
6701 	 * If the filesystem is mounted using local locking, pass the
6702 	 * request off to the local share code.
6703 	 */
6704 	if (VTOMI(vp)->mi_flags & MI_LLOCK)
6705 		return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
6706 
6707 	switch (cmd) {
6708 	case F_SHARE:
6709 	case F_UNSHARE:
6710 		lm_fh3.n_len = VTOFH3(vp)->fh3_length;
6711 		lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
6712 
6713 		/*
6714 		 * If passed an owner that is too large to fit in an
6715 		 * nfs_owner it is likely a recursive call from the
6716 		 * lock manager client and pass it straight through.  If
6717 		 * it is not a nfs_owner then simply return an error.
6718 		 */
6719 		if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
6720 			if (((struct nfs_owner *)shr->s_owner)->magic !=
6721 			    NFS_OWNER_MAGIC)
6722 				return (EINVAL);
6723 
6724 			if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) {
6725 				error = set_errno(error);
6726 			}
6727 			return (error);
6728 		}
6729 		/*
6730 		 * Remote share reservations owner is a combination of
6731 		 * a magic number, hostname, and the local owner
6732 		 */
6733 		bzero(&nfs_owner, sizeof (nfs_owner));
6734 		nfs_owner.magic = NFS_OWNER_MAGIC;
6735 		(void) strncpy(nfs_owner.hname, uts_nodename(),
6736 		    sizeof (nfs_owner.hname));
6737 		bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
6738 		nshr.s_access = shr->s_access;
6739 		nshr.s_deny = shr->s_deny;
6740 		nshr.s_sysid = 0;
6741 		nshr.s_pid = ttoproc(curthread)->p_pid;
6742 		nshr.s_own_len = sizeof (nfs_owner);
6743 		nshr.s_owner = (caddr_t)&nfs_owner;
6744 
6745 		if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) {
6746 			error = set_errno(error);
6747 		}
6748 
6749 		break;
6750 
6751 	case F_HASREMOTELOCKS:
6752 		/*
6753 		 * NFS client can't store remote locks itself
6754 		 */
6755 		shr->s_access = 0;
6756 		error = 0;
6757 		break;
6758 
6759 	default:
6760 		error = EINVAL;
6761 		break;
6762 	}
6763 
6764 	return (error);
6765 }
6766