xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_subr.c (revision e010bda9)
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 2011 Nexenta Systems, Inc. All rights reserved.
28  * Copyright (c) 2016, 2017 by Delphix. All rights reserved.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/cred.h>
35 #include <sys/proc.h>
36 #include <sys/user.h>
37 #include <sys/time.h>
38 #include <sys/buf.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/socket.h>
42 #include <sys/uio.h>
43 #include <sys/tiuser.h>
44 #include <sys/swap.h>
45 #include <sys/errno.h>
46 #include <sys/debug.h>
47 #include <sys/kmem.h>
48 #include <sys/kstat.h>
49 #include <sys/cmn_err.h>
50 #include <sys/vtrace.h>
51 #include <sys/session.h>
52 #include <sys/dnlc.h>
53 #include <sys/bitmap.h>
54 #include <sys/acl.h>
55 #include <sys/ddi.h>
56 #include <sys/pathname.h>
57 #include <sys/flock.h>
58 #include <sys/dirent.h>
59 #include <sys/flock.h>
60 #include <sys/callb.h>
61 #include <sys/atomic.h>
62 #include <sys/list.h>
63 #include <sys/tsol/tnet.h>
64 #include <sys/priv.h>
65 #include <sys/sdt.h>
66 #include <sys/attr.h>
67 
68 #include <inet/ip6.h>
69 
70 #include <rpc/types.h>
71 #include <rpc/xdr.h>
72 #include <rpc/auth.h>
73 #include <rpc/clnt.h>
74 
75 #include <nfs/nfs.h>
76 #include <nfs/nfs4.h>
77 #include <nfs/nfs_clnt.h>
78 #include <nfs/rnode.h>
79 #include <nfs/nfs_acl.h>
80 
81 #include <sys/tsol/label.h>
82 
83 /*
84  * The hash queues for the access to active and cached rnodes
85  * are organized as doubly linked lists.  A reader/writer lock
86  * for each hash bucket is used to control access and to synchronize
87  * lookups, additions, and deletions from the hash queue.
88  *
89  * The rnode freelist is organized as a doubly linked list with
90  * a head pointer.  Additions and deletions are synchronized via
91  * a single mutex.
92  *
93  * In order to add an rnode to the free list, it must be hashed into
94  * a hash queue and the exclusive lock to the hash queue be held.
95  * If an rnode is not hashed into a hash queue, then it is destroyed
96  * because it represents no valuable information that can be reused
97  * about the file.  The exclusive lock to the hash queue must be
98  * held in order to prevent a lookup in the hash queue from finding
99  * the rnode and using it and assuming that the rnode is not on the
100  * freelist.  The lookup in the hash queue will have the hash queue
101  * locked, either exclusive or shared.
102  *
103  * The vnode reference count for each rnode is not allowed to drop
104  * below 1.  This prevents external entities, such as the VM
105  * subsystem, from acquiring references to vnodes already on the
106  * freelist and then trying to place them back on the freelist
107  * when their reference is released.  This means that the when an
108  * rnode is looked up in the hash queues, then either the rnode
109  * is removed from the freelist and that reference is transferred to
110  * the new reference or the vnode reference count must be incremented
111  * accordingly.  The mutex for the freelist must be held in order to
112  * accurately test to see if the rnode is on the freelist or not.
113  * The hash queue lock might be held shared and it is possible that
114  * two different threads may race to remove the rnode from the
115  * freelist.  This race can be resolved by holding the mutex for the
116  * freelist.  Please note that the mutex for the freelist does not
117  * need to held if the rnode is not on the freelist.  It can not be
118  * placed on the freelist due to the requirement that the thread
119  * putting the rnode on the freelist must hold the exclusive lock
120  * to the hash queue and the thread doing the lookup in the hash
121  * queue is holding either a shared or exclusive lock to the hash
122  * queue.
123  *
124  * The lock ordering is:
125  *
126  *	hash bucket lock -> vnode lock
127  *	hash bucket lock -> freelist lock
128  */
129 static rhashq_t *rtable;
130 
131 static kmutex_t rpfreelist_lock;
132 static rnode_t *rpfreelist = NULL;
133 static long rnew = 0;
134 long nrnode = 0;
135 
136 static int rtablesize;
137 static int rtablemask;
138 
139 static int hashlen = 4;
140 
141 static struct kmem_cache *rnode_cache;
142 
143 /*
144  * Mutex to protect the following variables:
145  *	nfs_major
146  *	nfs_minor
147  */
148 kmutex_t nfs_minor_lock;
149 int nfs_major;
150 int nfs_minor;
151 
152 /* Do we allow preepoch (negative) time values otw? */
153 bool_t nfs_allow_preepoch_time = FALSE;	/* default: do not allow preepoch */
154 
155 /*
156  * Access cache
157  */
158 static acache_hash_t *acache;
159 static long nacache;	/* used strictly to size the number of hash queues */
160 
161 static int acachesize;
162 static int acachemask;
163 static struct kmem_cache *acache_cache;
164 
165 /*
166  * Client side utilities
167  */
168 
169 /*
170  * client side statistics
171  */
172 static const struct clstat clstat_tmpl = {
173 	{ "calls",	KSTAT_DATA_UINT64 },
174 	{ "badcalls",	KSTAT_DATA_UINT64 },
175 	{ "clgets",	KSTAT_DATA_UINT64 },
176 	{ "cltoomany",	KSTAT_DATA_UINT64 },
177 #ifdef DEBUG
178 	{ "clalloc",	KSTAT_DATA_UINT64 },
179 	{ "noresponse",	KSTAT_DATA_UINT64 },
180 	{ "failover",	KSTAT_DATA_UINT64 },
181 	{ "remap",	KSTAT_DATA_UINT64 },
182 #endif
183 };
184 
185 /*
186  * The following are statistics that describe behavior of the system as a whole
187  * and doesn't correspond to any one particular zone.
188  */
189 #ifdef DEBUG
190 static struct clstat_debug {
191 	kstat_named_t	nrnode;			/* number of allocated rnodes */
192 	kstat_named_t	access;			/* size of access cache */
193 	kstat_named_t	dirent;			/* size of readdir cache */
194 	kstat_named_t	dirents;		/* size of readdir buf cache */
195 	kstat_named_t	reclaim;		/* number of reclaims */
196 	kstat_named_t	clreclaim;		/* number of cl reclaims */
197 	kstat_named_t	f_reclaim;		/* number of free reclaims */
198 	kstat_named_t	a_reclaim;		/* number of active reclaims */
199 	kstat_named_t	r_reclaim;		/* number of rnode reclaims */
200 	kstat_named_t	rpath;			/* bytes used to store rpaths */
201 } clstat_debug = {
202 	{ "nrnode",	KSTAT_DATA_UINT64 },
203 	{ "access",	KSTAT_DATA_UINT64 },
204 	{ "dirent",	KSTAT_DATA_UINT64 },
205 	{ "dirents",	KSTAT_DATA_UINT64 },
206 	{ "reclaim",	KSTAT_DATA_UINT64 },
207 	{ "clreclaim",	KSTAT_DATA_UINT64 },
208 	{ "f_reclaim",	KSTAT_DATA_UINT64 },
209 	{ "a_reclaim",	KSTAT_DATA_UINT64 },
210 	{ "r_reclaim",	KSTAT_DATA_UINT64 },
211 	{ "r_path",	KSTAT_DATA_UINT64 },
212 };
213 #endif	/* DEBUG */
214 
215 /*
216  * We keep a global list of per-zone client data, so we can clean up all zones
217  * if we get low on memory.
218  */
219 static list_t nfs_clnt_list;
220 static kmutex_t nfs_clnt_list_lock;
221 static zone_key_t nfsclnt_zone_key;
222 
223 static struct kmem_cache *chtab_cache;
224 
225 /*
226  * Some servers do not properly update the attributes of the
227  * directory when changes are made.  To allow interoperability
228  * with these broken servers, the nfs_disable_rddir_cache
229  * parameter must be set in /etc/system
230  */
231 int nfs_disable_rddir_cache = 0;
232 
233 int		clget(clinfo_t *, servinfo_t *, cred_t *, CLIENT **,
234 		    struct chtab **);
235 void		clfree(CLIENT *, struct chtab *);
236 static int	acl_clget(mntinfo_t *, servinfo_t *, cred_t *, CLIENT **,
237 		    struct chtab **, struct nfs_clnt *);
238 static int	nfs_clget(mntinfo_t *, servinfo_t *, cred_t *, CLIENT **,
239 		    struct chtab **, struct nfs_clnt *);
240 static void	clreclaim(void *);
241 static int	nfs_feedback(int, int, mntinfo_t *);
242 static int	rfscall(mntinfo_t *, rpcproc_t, xdrproc_t, caddr_t, xdrproc_t,
243 		    caddr_t, cred_t *, int *, enum clnt_stat *, int,
244 		    failinfo_t *);
245 static int	aclcall(mntinfo_t *, rpcproc_t, xdrproc_t, caddr_t, xdrproc_t,
246 		    caddr_t, cred_t *, int *, int, failinfo_t *);
247 static void	rinactive(rnode_t *, cred_t *);
248 static int	rtablehash(nfs_fhandle *);
249 static vnode_t	*make_rnode(nfs_fhandle *, rhashq_t *, struct vfs *,
250 		    struct vnodeops *,
251 		    int (*)(vnode_t *, page_t *, u_offset_t *, size_t *, int,
252 			cred_t *),
253 		    int (*)(const void *, const void *), int *, cred_t *,
254 		    char *, char *);
255 static void	rp_rmfree(rnode_t *);
256 static void	rp_addhash(rnode_t *);
257 static void	rp_rmhash_locked(rnode_t *);
258 static rnode_t	*rfind(rhashq_t *, nfs_fhandle *, struct vfs *);
259 static void	destroy_rnode(rnode_t *);
260 static void	rddir_cache_free(rddir_cache *);
261 static int	nfs_free_data_reclaim(rnode_t *);
262 static int	nfs_active_data_reclaim(rnode_t *);
263 static int	nfs_free_reclaim(void);
264 static int	nfs_active_reclaim(void);
265 static int	nfs_rnode_reclaim(void);
266 static void	nfs_reclaim(void *);
267 static int	failover_safe(failinfo_t *);
268 static void	failover_newserver(mntinfo_t *mi);
269 static void	failover_thread(mntinfo_t *mi);
270 static int	failover_wait(mntinfo_t *);
271 static int	failover_remap(failinfo_t *);
272 static int	failover_lookup(char *, vnode_t *,
273 		    int (*)(vnode_t *, char *, vnode_t **,
274 			struct pathname *, int, vnode_t *, cred_t *, int),
275 		    int (*)(vnode_t *, vnode_t **, bool_t, cred_t *, int),
276 		    vnode_t **);
277 static void	nfs_free_r_path(rnode_t *);
278 static void	nfs_set_vroot(vnode_t *);
279 static char	*nfs_getsrvnames(mntinfo_t *, size_t *);
280 
281 /*
282  * from rpcsec module (common/rpcsec)
283  */
284 extern int sec_clnt_geth(CLIENT *, struct sec_data *, cred_t *, AUTH **);
285 extern void sec_clnt_freeh(AUTH *);
286 extern void sec_clnt_freeinfo(struct sec_data *);
287 
288 /*
289  * used in mount policy
290  */
291 extern ts_label_t *getflabel_cipso(vfs_t *);
292 
293 /*
294  * EIO or EINTR are not recoverable errors.
295  */
296 #define	IS_RECOVERABLE_ERROR(error)	!((error == EINTR) || (error == EIO))
297 
298 #ifdef DEBUG
299 #define	SRV_QFULL_MSG	"send queue to NFS%d server %s is full; still trying\n"
300 #define	SRV_NOTRESP_MSG	"NFS%d server %s not responding still trying\n"
301 #else
302 #define	SRV_QFULL_MSG	"send queue to NFS server %s is full still trying\n"
303 #define	SRV_NOTRESP_MSG	"NFS server %s not responding still trying\n"
304 #endif
305 /*
306  * Common handle get program for NFS, NFS ACL, and NFS AUTH client.
307  */
308 static int
clget_impl(clinfo_t * ci,servinfo_t * svp,cred_t * cr,CLIENT ** newcl,struct chtab ** chp,struct nfs_clnt * nfscl)309 clget_impl(clinfo_t *ci, servinfo_t *svp, cred_t *cr, CLIENT **newcl,
310     struct chtab **chp, struct nfs_clnt *nfscl)
311 {
312 	struct chhead *ch, *newch;
313 	struct chhead **plistp;
314 	struct chtab *cp;
315 	int error;
316 	k_sigset_t smask;
317 
318 	if (newcl == NULL || chp == NULL || ci == NULL)
319 		return (EINVAL);
320 
321 	*newcl = NULL;
322 	*chp = NULL;
323 
324 	/*
325 	 * Find an unused handle or create one
326 	 */
327 	newch = NULL;
328 	nfscl->nfscl_stat.clgets.value.ui64++;
329 top:
330 	/*
331 	 * Find the correct entry in the cache to check for free
332 	 * client handles.  The search is based on the RPC program
333 	 * number, program version number, dev_t for the transport
334 	 * device, and the protocol family.
335 	 */
336 	mutex_enter(&nfscl->nfscl_chtable_lock);
337 	plistp = &nfscl->nfscl_chtable;
338 	for (ch = nfscl->nfscl_chtable; ch != NULL; ch = ch->ch_next) {
339 		if (ch->ch_prog == ci->cl_prog &&
340 		    ch->ch_vers == ci->cl_vers &&
341 		    ch->ch_dev == svp->sv_knconf->knc_rdev &&
342 		    (strcmp(ch->ch_protofmly,
343 		    svp->sv_knconf->knc_protofmly) == 0))
344 			break;
345 		plistp = &ch->ch_next;
346 	}
347 
348 	/*
349 	 * If we didn't find a cache entry for this quadruple, then
350 	 * create one.  If we don't have one already preallocated,
351 	 * then drop the cache lock, create one, and then start over.
352 	 * If we did have a preallocated entry, then just add it to
353 	 * the front of the list.
354 	 */
355 	if (ch == NULL) {
356 		if (newch == NULL) {
357 			mutex_exit(&nfscl->nfscl_chtable_lock);
358 			newch = kmem_alloc(sizeof (*newch), KM_SLEEP);
359 			newch->ch_timesused = 0;
360 			newch->ch_prog = ci->cl_prog;
361 			newch->ch_vers = ci->cl_vers;
362 			newch->ch_dev = svp->sv_knconf->knc_rdev;
363 			newch->ch_protofmly = kmem_alloc(
364 			    strlen(svp->sv_knconf->knc_protofmly) + 1,
365 			    KM_SLEEP);
366 			(void) strcpy(newch->ch_protofmly,
367 			    svp->sv_knconf->knc_protofmly);
368 			newch->ch_list = NULL;
369 			goto top;
370 		}
371 		ch = newch;
372 		newch = NULL;
373 		ch->ch_next = nfscl->nfscl_chtable;
374 		nfscl->nfscl_chtable = ch;
375 	/*
376 	 * We found a cache entry, but if it isn't on the front of the
377 	 * list, then move it to the front of the list to try to take
378 	 * advantage of locality of operations.
379 	 */
380 	} else if (ch != nfscl->nfscl_chtable) {
381 		*plistp = ch->ch_next;
382 		ch->ch_next = nfscl->nfscl_chtable;
383 		nfscl->nfscl_chtable = ch;
384 	}
385 
386 	/*
387 	 * If there was a free client handle cached, then remove it
388 	 * from the list, init it, and use it.
389 	 */
390 	if (ch->ch_list != NULL) {
391 		cp = ch->ch_list;
392 		ch->ch_list = cp->ch_list;
393 		mutex_exit(&nfscl->nfscl_chtable_lock);
394 		if (newch != NULL) {
395 			kmem_free(newch->ch_protofmly,
396 			    strlen(newch->ch_protofmly) + 1);
397 			kmem_free(newch, sizeof (*newch));
398 		}
399 		(void) clnt_tli_kinit(cp->ch_client, svp->sv_knconf,
400 		    &svp->sv_addr, ci->cl_readsize, ci->cl_retrans, cr);
401 		error = sec_clnt_geth(cp->ch_client, svp->sv_secdata, cr,
402 		    &cp->ch_client->cl_auth);
403 		if (error || cp->ch_client->cl_auth == NULL) {
404 			CLNT_DESTROY(cp->ch_client);
405 			kmem_cache_free(chtab_cache, cp);
406 			return ((error != 0) ? error : EINTR);
407 		}
408 		ch->ch_timesused++;
409 		*newcl = cp->ch_client;
410 		*chp = cp;
411 		return (0);
412 	}
413 
414 	/*
415 	 * There weren't any free client handles which fit, so allocate
416 	 * a new one and use that.
417 	 */
418 #ifdef DEBUG
419 	atomic_inc_64(&nfscl->nfscl_stat.clalloc.value.ui64);
420 #endif
421 	mutex_exit(&nfscl->nfscl_chtable_lock);
422 
423 	nfscl->nfscl_stat.cltoomany.value.ui64++;
424 	if (newch != NULL) {
425 		kmem_free(newch->ch_protofmly, strlen(newch->ch_protofmly) + 1);
426 		kmem_free(newch, sizeof (*newch));
427 	}
428 
429 	cp = kmem_cache_alloc(chtab_cache, KM_SLEEP);
430 	cp->ch_head = ch;
431 
432 	sigintr(&smask, (int)ci->cl_flags & MI_INT);
433 	error = clnt_tli_kcreate(svp->sv_knconf, &svp->sv_addr, ci->cl_prog,
434 	    ci->cl_vers, ci->cl_readsize, ci->cl_retrans, cr, &cp->ch_client);
435 	sigunintr(&smask);
436 
437 	if (error != 0) {
438 		kmem_cache_free(chtab_cache, cp);
439 #ifdef DEBUG
440 		atomic_dec_64(&nfscl->nfscl_stat.clalloc.value.ui64);
441 #endif
442 		/*
443 		 * Warning is unnecessary if error is EINTR.
444 		 */
445 		if (error != EINTR) {
446 			nfs_cmn_err(error, CE_WARN,
447 			    "clget: couldn't create handle: %m\n");
448 		}
449 		return (error);
450 	}
451 	(void) CLNT_CONTROL(cp->ch_client, CLSET_PROGRESS, NULL);
452 	auth_destroy(cp->ch_client->cl_auth);
453 	error = sec_clnt_geth(cp->ch_client, svp->sv_secdata, cr,
454 	    &cp->ch_client->cl_auth);
455 	if (error || cp->ch_client->cl_auth == NULL) {
456 		CLNT_DESTROY(cp->ch_client);
457 		kmem_cache_free(chtab_cache, cp);
458 #ifdef DEBUG
459 		atomic_dec_64(&nfscl->nfscl_stat.clalloc.value.ui64);
460 #endif
461 		return ((error != 0) ? error : EINTR);
462 	}
463 	ch->ch_timesused++;
464 	*newcl = cp->ch_client;
465 	ASSERT(cp->ch_client->cl_nosignal == FALSE);
466 	*chp = cp;
467 	return (0);
468 }
469 
470 int
clget(clinfo_t * ci,servinfo_t * svp,cred_t * cr,CLIENT ** newcl,struct chtab ** chp)471 clget(clinfo_t *ci, servinfo_t *svp, cred_t *cr, CLIENT **newcl,
472     struct chtab **chp)
473 {
474 	struct nfs_clnt *nfscl;
475 
476 	nfscl = zone_getspecific(nfsclnt_zone_key, nfs_zone());
477 	ASSERT(nfscl != NULL);
478 
479 	return (clget_impl(ci, svp, cr, newcl, chp, nfscl));
480 }
481 
482 static int
acl_clget(mntinfo_t * mi,servinfo_t * svp,cred_t * cr,CLIENT ** newcl,struct chtab ** chp,struct nfs_clnt * nfscl)483 acl_clget(mntinfo_t *mi, servinfo_t *svp, cred_t *cr, CLIENT **newcl,
484     struct chtab **chp, struct nfs_clnt *nfscl)
485 {
486 	clinfo_t ci;
487 	int error;
488 
489 	/*
490 	 * Set read buffer size to rsize
491 	 * and add room for RPC headers.
492 	 */
493 	ci.cl_readsize = mi->mi_tsize;
494 	if (ci.cl_readsize != 0)
495 		ci.cl_readsize += (RPC_MAXDATASIZE - NFS_MAXDATA);
496 
497 	/*
498 	 * If soft mount and server is down just try once.
499 	 * meaning: do not retransmit.
500 	 */
501 	if (!(mi->mi_flags & MI_HARD) && (mi->mi_flags & MI_DOWN))
502 		ci.cl_retrans = 0;
503 	else
504 		ci.cl_retrans = mi->mi_retrans;
505 
506 	ci.cl_prog = NFS_ACL_PROGRAM;
507 	ci.cl_vers = mi->mi_vers;
508 	ci.cl_flags = mi->mi_flags;
509 
510 	/*
511 	 * clget calls sec_clnt_geth() to get an auth handle. For RPCSEC_GSS
512 	 * security flavor, the client tries to establish a security context
513 	 * by contacting the server. If the connection is timed out or reset,
514 	 * e.g. server reboot, we will try again.
515 	 */
516 	do {
517 		error = clget_impl(&ci, svp, cr, newcl, chp, nfscl);
518 
519 		if (error == 0)
520 			break;
521 
522 		/*
523 		 * For forced unmount or zone shutdown, bail out, no retry.
524 		 */
525 		if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
526 			error = EIO;
527 			break;
528 		}
529 
530 		/* do not retry for softmount */
531 		if (!(mi->mi_flags & MI_HARD))
532 			break;
533 
534 		/* let the caller deal with the failover case */
535 		if (FAILOVER_MOUNT(mi))
536 			break;
537 
538 	} while (error == ETIMEDOUT || error == ECONNRESET);
539 
540 	return (error);
541 }
542 
543 static int
nfs_clget(mntinfo_t * mi,servinfo_t * svp,cred_t * cr,CLIENT ** newcl,struct chtab ** chp,struct nfs_clnt * nfscl)544 nfs_clget(mntinfo_t *mi, servinfo_t *svp, cred_t *cr, CLIENT **newcl,
545     struct chtab **chp, struct nfs_clnt *nfscl)
546 {
547 	clinfo_t ci;
548 	int error;
549 
550 	/*
551 	 * Set read buffer size to rsize
552 	 * and add room for RPC headers.
553 	 */
554 	ci.cl_readsize = mi->mi_tsize;
555 	if (ci.cl_readsize != 0)
556 		ci.cl_readsize += (RPC_MAXDATASIZE - NFS_MAXDATA);
557 
558 	/*
559 	 * If soft mount and server is down just try once.
560 	 * meaning: do not retransmit.
561 	 */
562 	if (!(mi->mi_flags & MI_HARD) && (mi->mi_flags & MI_DOWN))
563 		ci.cl_retrans = 0;
564 	else
565 		ci.cl_retrans = mi->mi_retrans;
566 
567 	ci.cl_prog = mi->mi_prog;
568 	ci.cl_vers = mi->mi_vers;
569 	ci.cl_flags = mi->mi_flags;
570 
571 	/*
572 	 * clget calls sec_clnt_geth() to get an auth handle. For RPCSEC_GSS
573 	 * security flavor, the client tries to establish a security context
574 	 * by contacting the server. If the connection is timed out or reset,
575 	 * e.g. server reboot, we will try again.
576 	 */
577 	do {
578 		error = clget_impl(&ci, svp, cr, newcl, chp, nfscl);
579 
580 		if (error == 0)
581 			break;
582 
583 		/*
584 		 * For forced unmount or zone shutdown, bail out, no retry.
585 		 */
586 		if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
587 			error = EIO;
588 			break;
589 		}
590 
591 		/* do not retry for softmount */
592 		if (!(mi->mi_flags & MI_HARD))
593 			break;
594 
595 		/* let the caller deal with the failover case */
596 		if (FAILOVER_MOUNT(mi))
597 			break;
598 
599 	} while (error == ETIMEDOUT || error == ECONNRESET);
600 
601 	return (error);
602 }
603 
604 static void
clfree_impl(CLIENT * cl,struct chtab * cp,struct nfs_clnt * nfscl)605 clfree_impl(CLIENT *cl, struct chtab *cp, struct nfs_clnt *nfscl)
606 {
607 	if (cl->cl_auth != NULL) {
608 		sec_clnt_freeh(cl->cl_auth);
609 		cl->cl_auth = NULL;
610 	}
611 
612 	/*
613 	 * Timestamp this cache entry so that we know when it was last
614 	 * used.
615 	 */
616 	cp->ch_freed = gethrestime_sec();
617 
618 	/*
619 	 * Add the free client handle to the front of the list.
620 	 * This way, the list will be sorted in youngest to oldest
621 	 * order.
622 	 */
623 	mutex_enter(&nfscl->nfscl_chtable_lock);
624 	cp->ch_list = cp->ch_head->ch_list;
625 	cp->ch_head->ch_list = cp;
626 	mutex_exit(&nfscl->nfscl_chtable_lock);
627 }
628 
629 void
clfree(CLIENT * cl,struct chtab * cp)630 clfree(CLIENT *cl, struct chtab *cp)
631 {
632 	struct nfs_clnt *nfscl;
633 
634 	nfscl = zone_getspecific(nfsclnt_zone_key, nfs_zone());
635 	ASSERT(nfscl != NULL);
636 
637 	clfree_impl(cl, cp, nfscl);
638 }
639 
640 #define	CL_HOLDTIME	60	/* time to hold client handles */
641 
642 static void
clreclaim_zone(struct nfs_clnt * nfscl,uint_t cl_holdtime)643 clreclaim_zone(struct nfs_clnt *nfscl, uint_t cl_holdtime)
644 {
645 	struct chhead *ch;
646 	struct chtab *cp;	/* list of objects that can be reclaimed */
647 	struct chtab *cpe;
648 	struct chtab *cpl;
649 	struct chtab **cpp;
650 #ifdef DEBUG
651 	int n = 0;
652 #endif
653 
654 	/*
655 	 * Need to reclaim some memory, so step through the cache
656 	 * looking through the lists for entries which can be freed.
657 	 */
658 	cp = NULL;
659 
660 	mutex_enter(&nfscl->nfscl_chtable_lock);
661 
662 	/*
663 	 * Here we step through each non-NULL quadruple and start to
664 	 * construct the reclaim list pointed to by cp.  Note that
665 	 * cp will contain all eligible chtab entries.  When this traversal
666 	 * completes, chtab entries from the last quadruple will be at the
667 	 * front of cp and entries from previously inspected quadruples have
668 	 * been appended to the rear of cp.
669 	 */
670 	for (ch = nfscl->nfscl_chtable; ch != NULL; ch = ch->ch_next) {
671 		if (ch->ch_list == NULL)
672 			continue;
673 		/*
674 		 * Search each list for entries older then
675 		 * cl_holdtime seconds.  The lists are maintained
676 		 * in youngest to oldest order so that when the
677 		 * first entry is found which is old enough, then
678 		 * all of the rest of the entries on the list will
679 		 * be old enough as well.
680 		 */
681 		cpl = ch->ch_list;
682 		cpp = &ch->ch_list;
683 		while (cpl != NULL &&
684 		    cpl->ch_freed + cl_holdtime > gethrestime_sec()) {
685 			cpp = &cpl->ch_list;
686 			cpl = cpl->ch_list;
687 		}
688 		if (cpl != NULL) {
689 			*cpp = NULL;
690 			if (cp != NULL) {
691 				cpe = cpl;
692 				while (cpe->ch_list != NULL)
693 					cpe = cpe->ch_list;
694 				cpe->ch_list = cp;
695 			}
696 			cp = cpl;
697 		}
698 	}
699 
700 	mutex_exit(&nfscl->nfscl_chtable_lock);
701 
702 	/*
703 	 * If cp is empty, then there is nothing to reclaim here.
704 	 */
705 	if (cp == NULL)
706 		return;
707 
708 	/*
709 	 * Step through the list of entries to free, destroying each client
710 	 * handle and kmem_free'ing the memory for each entry.
711 	 */
712 	while (cp != NULL) {
713 #ifdef DEBUG
714 		n++;
715 #endif
716 		CLNT_DESTROY(cp->ch_client);
717 		cpl = cp->ch_list;
718 		kmem_cache_free(chtab_cache, cp);
719 		cp = cpl;
720 	}
721 
722 #ifdef DEBUG
723 	/*
724 	 * Update clalloc so that nfsstat shows the current number
725 	 * of allocated client handles.
726 	 */
727 	atomic_add_64(&nfscl->nfscl_stat.clalloc.value.ui64, -n);
728 #endif
729 }
730 
731 /* ARGSUSED */
732 static void
clreclaim(void * all)733 clreclaim(void *all)
734 {
735 	struct nfs_clnt *nfscl;
736 
737 #ifdef DEBUG
738 	clstat_debug.clreclaim.value.ui64++;
739 #endif
740 	/*
741 	 * The system is low on memory; go through and try to reclaim some from
742 	 * every zone on the system.
743 	 */
744 	mutex_enter(&nfs_clnt_list_lock);
745 	nfscl = list_head(&nfs_clnt_list);
746 	for (; nfscl != NULL; nfscl = list_next(&nfs_clnt_list, nfscl))
747 		clreclaim_zone(nfscl, CL_HOLDTIME);
748 	mutex_exit(&nfs_clnt_list_lock);
749 }
750 
751 /*
752  * Minimum time-out values indexed by call type
753  * These units are in "eights" of a second to avoid multiplies
754  */
755 static unsigned int minimum_timeo[] = {
756 	6, 7, 10
757 };
758 
759 /*
760  * Back off for retransmission timeout, MAXTIMO is in hz of a sec
761  */
762 #define	MAXTIMO	(20*hz)
763 #define	backoff(tim)	(((tim) < MAXTIMO) ? dobackoff(tim) : (tim))
764 #define	dobackoff(tim)	((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1))
765 
766 #define	MIN_NFS_TSIZE 512	/* minimum "chunk" of NFS IO */
767 #define	REDUCE_NFS_TIME (hz/2)	/* rtxcur we try to keep under */
768 #define	INCREASE_NFS_TIME (hz/3*8) /* srtt we try to keep under (scaled*8) */
769 
770 /*
771  * Function called when rfscall notices that we have been
772  * re-transmitting, or when we get a response without retransmissions.
773  * Return 1 if the transfer size was adjusted down - 0 if no change.
774  */
775 static int
nfs_feedback(int flag,int which,mntinfo_t * mi)776 nfs_feedback(int flag, int which, mntinfo_t *mi)
777 {
778 	int kind;
779 	int r = 0;
780 
781 	mutex_enter(&mi->mi_lock);
782 	if (flag == FEEDBACK_REXMIT1) {
783 		if (mi->mi_timers[NFS_CALLTYPES].rt_rtxcur != 0 &&
784 		    mi->mi_timers[NFS_CALLTYPES].rt_rtxcur < REDUCE_NFS_TIME)
785 			goto done;
786 		if (mi->mi_curread > MIN_NFS_TSIZE) {
787 			mi->mi_curread /= 2;
788 			if (mi->mi_curread < MIN_NFS_TSIZE)
789 				mi->mi_curread = MIN_NFS_TSIZE;
790 			r = 1;
791 		}
792 
793 		if (mi->mi_curwrite > MIN_NFS_TSIZE) {
794 			mi->mi_curwrite /= 2;
795 			if (mi->mi_curwrite < MIN_NFS_TSIZE)
796 				mi->mi_curwrite = MIN_NFS_TSIZE;
797 			r = 1;
798 		}
799 	} else if (flag == FEEDBACK_OK) {
800 		kind = mi->mi_timer_type[which];
801 		if (kind == 0 ||
802 		    mi->mi_timers[kind].rt_srtt >= INCREASE_NFS_TIME)
803 			goto done;
804 		if (kind == 1) {
805 			if (mi->mi_curread >= mi->mi_tsize)
806 				goto done;
807 			mi->mi_curread +=  MIN_NFS_TSIZE;
808 			if (mi->mi_curread > mi->mi_tsize/2)
809 				mi->mi_curread = mi->mi_tsize;
810 		} else if (kind == 2) {
811 			if (mi->mi_curwrite >= mi->mi_stsize)
812 				goto done;
813 			mi->mi_curwrite += MIN_NFS_TSIZE;
814 			if (mi->mi_curwrite > mi->mi_stsize/2)
815 				mi->mi_curwrite = mi->mi_stsize;
816 		}
817 	}
818 done:
819 	mutex_exit(&mi->mi_lock);
820 	return (r);
821 }
822 
823 #ifdef DEBUG
824 static int rfs2call_hits = 0;
825 static int rfs2call_misses = 0;
826 #endif
827 
828 int
rfs2call(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * cr,int * douprintf,enum nfsstat * statusp,int flags,failinfo_t * fi)829 rfs2call(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
830     xdrproc_t xdrres, caddr_t resp, cred_t *cr, int *douprintf,
831     enum nfsstat *statusp, int flags, failinfo_t *fi)
832 {
833 	int rpcerror;
834 	enum clnt_stat rpc_status;
835 
836 	ASSERT(statusp != NULL);
837 
838 	rpcerror = rfscall(mi, which, xdrargs, argsp, xdrres, resp,
839 	    cr, douprintf, &rpc_status, flags, fi);
840 	if (!rpcerror) {
841 		/*
842 		 * See crnetadjust() for comments.
843 		 */
844 		if (*statusp == NFSERR_ACCES &&
845 		    (cr = crnetadjust(cr)) != NULL) {
846 #ifdef DEBUG
847 			rfs2call_hits++;
848 #endif
849 			rpcerror = rfscall(mi, which, xdrargs, argsp, xdrres,
850 			    resp, cr, douprintf, NULL, flags, fi);
851 			crfree(cr);
852 #ifdef DEBUG
853 			if (*statusp == NFSERR_ACCES)
854 				rfs2call_misses++;
855 #endif
856 		}
857 	} else if (rpc_status == RPC_PROCUNAVAIL) {
858 		*statusp = NFSERR_OPNOTSUPP;
859 		rpcerror = 0;
860 	}
861 
862 	return (rpcerror);
863 }
864 
865 #define	NFS3_JUKEBOX_DELAY	10 * hz
866 
867 static clock_t nfs3_jukebox_delay = 0;
868 
869 #ifdef DEBUG
870 static int rfs3call_hits = 0;
871 static int rfs3call_misses = 0;
872 #endif
873 
874 int
rfs3call(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * cr,int * douprintf,nfsstat3 * statusp,int flags,failinfo_t * fi)875 rfs3call(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
876     xdrproc_t xdrres, caddr_t resp, cred_t *cr, int *douprintf,
877     nfsstat3 *statusp, int flags, failinfo_t *fi)
878 {
879 	int rpcerror;
880 	int user_informed;
881 
882 	user_informed = 0;
883 	do {
884 		rpcerror = rfscall(mi, which, xdrargs, argsp, xdrres, resp,
885 		    cr, douprintf, NULL, flags, fi);
886 		if (!rpcerror) {
887 			cred_t *crr;
888 			if (*statusp == NFS3ERR_JUKEBOX) {
889 				if (ttoproc(curthread) == &p0) {
890 					rpcerror = EAGAIN;
891 					break;
892 				}
893 				if (!user_informed) {
894 					user_informed = 1;
895 					uprintf(
896 		"file temporarily unavailable on the server, retrying...\n");
897 				}
898 				delay(nfs3_jukebox_delay);
899 			}
900 			/*
901 			 * See crnetadjust() for comments.
902 			 */
903 			else if (*statusp == NFS3ERR_ACCES &&
904 			    (crr = crnetadjust(cr)) != NULL) {
905 #ifdef DEBUG
906 				rfs3call_hits++;
907 #endif
908 				rpcerror = rfscall(mi, which, xdrargs, argsp,
909 				    xdrres, resp, crr, douprintf,
910 				    NULL, flags, fi);
911 
912 				crfree(crr);
913 #ifdef DEBUG
914 				if (*statusp == NFS3ERR_ACCES)
915 					rfs3call_misses++;
916 #endif
917 			}
918 		}
919 	} while (!rpcerror && *statusp == NFS3ERR_JUKEBOX);
920 
921 	return (rpcerror);
922 }
923 
924 #define	VALID_FH(fi)	(VTOR(fi->vp)->r_server == VTOMI(fi->vp)->mi_curr_serv)
925 #define	INC_READERS(mi)		{ \
926 	mi->mi_readers++; \
927 }
928 #define	DEC_READERS(mi)		{ \
929 	mi->mi_readers--; \
930 	if (mi->mi_readers == 0) \
931 		cv_broadcast(&mi->mi_failover_cv); \
932 }
933 
934 static int
rfscall(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * icr,int * douprintf,enum clnt_stat * rpc_status,int flags,failinfo_t * fi)935 rfscall(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
936     xdrproc_t xdrres, caddr_t resp, cred_t *icr, int *douprintf,
937     enum clnt_stat *rpc_status, int flags, failinfo_t *fi)
938 {
939 	CLIENT *client;
940 	struct chtab *ch;
941 	cred_t *cr = icr;
942 	enum clnt_stat status;
943 	struct rpc_err rpcerr, rpcerr_tmp;
944 	struct timeval wait;
945 	int timeo;		/* in units of hz */
946 	int my_rsize, my_wsize;
947 	bool_t tryagain;
948 	bool_t cred_cloned = FALSE;
949 	k_sigset_t smask;
950 	servinfo_t *svp;
951 	struct nfs_clnt *nfscl;
952 	zoneid_t zoneid = getzoneid();
953 	char *msg;
954 #ifdef DEBUG
955 	char *bufp;
956 #endif
957 
958 
959 	TRACE_2(TR_FAC_NFS, TR_RFSCALL_START,
960 	    "rfscall_start:which %d mi %p", which, mi);
961 
962 	nfscl = zone_getspecific(nfsclnt_zone_key, nfs_zone());
963 	ASSERT(nfscl != NULL);
964 
965 	nfscl->nfscl_stat.calls.value.ui64++;
966 	mi->mi_reqs[which].value.ui64++;
967 
968 	rpcerr.re_status = RPC_SUCCESS;
969 
970 	/*
971 	 * In case of forced unmount or zone shutdown, return EIO.
972 	 */
973 
974 	if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
975 		rpcerr.re_status = RPC_FAILED;
976 		rpcerr.re_errno = EIO;
977 		return (rpcerr.re_errno);
978 	}
979 
980 	/*
981 	 * Remember the transfer sizes in case
982 	 * nfs_feedback changes them underneath us.
983 	 */
984 	my_rsize = mi->mi_curread;
985 	my_wsize = mi->mi_curwrite;
986 
987 	/*
988 	 * NFS client failover support
989 	 *
990 	 * If this rnode is not in sync with the current server (VALID_FH),
991 	 * we'd like to do a remap to get in sync.  We can be interrupted
992 	 * in failover_remap(), and if so we'll bail.  Otherwise, we'll
993 	 * use the best info we have to try the RPC.  Part of that is
994 	 * unconditionally updating the filehandle copy kept for V3.
995 	 *
996 	 * Locking: INC_READERS/DEC_READERS is a poor man's interrruptible
997 	 * rw_enter(); we're trying to keep the current server from being
998 	 * changed on us until we're done with the remapping and have a
999 	 * matching client handle.  We don't want to sending a filehandle
1000 	 * to the wrong host.
1001 	 */
1002 failoverretry:
1003 	if (FAILOVER_MOUNT(mi)) {
1004 		mutex_enter(&mi->mi_lock);
1005 		if (!(flags & RFSCALL_SOFT) && failover_safe(fi)) {
1006 			if (failover_wait(mi)) {
1007 				mutex_exit(&mi->mi_lock);
1008 				return (EINTR);
1009 			}
1010 		}
1011 		INC_READERS(mi);
1012 		mutex_exit(&mi->mi_lock);
1013 		if (fi) {
1014 			if (!VALID_FH(fi) &&
1015 			    !(flags & RFSCALL_SOFT) && failover_safe(fi)) {
1016 				int remaperr;
1017 
1018 				svp = mi->mi_curr_serv;
1019 				remaperr = failover_remap(fi);
1020 				if (remaperr != 0) {
1021 #ifdef DEBUG
1022 					if (remaperr != EINTR)
1023 						nfs_cmn_err(remaperr, CE_WARN,
1024 					    "rfscall couldn't failover: %m");
1025 #endif
1026 					mutex_enter(&mi->mi_lock);
1027 					DEC_READERS(mi);
1028 					mutex_exit(&mi->mi_lock);
1029 					/*
1030 					 * If failover_remap returns ETIMEDOUT
1031 					 * and the filesystem is hard mounted
1032 					 * we have to retry the call with a new
1033 					 * server.
1034 					 */
1035 					if ((mi->mi_flags & MI_HARD) &&
1036 					    IS_RECOVERABLE_ERROR(remaperr)) {
1037 						if (svp == mi->mi_curr_serv)
1038 							failover_newserver(mi);
1039 						rpcerr.re_status = RPC_SUCCESS;
1040 						goto failoverretry;
1041 					}
1042 					rpcerr.re_errno = remaperr;
1043 					return (remaperr);
1044 				}
1045 			}
1046 			if (fi->fhp && fi->copyproc)
1047 				(*fi->copyproc)(fi->fhp, fi->vp);
1048 		}
1049 	}
1050 
1051 	/* For TSOL, use a new cred which has net_mac_aware flag */
1052 	if (!cred_cloned && is_system_labeled()) {
1053 		cred_cloned = TRUE;
1054 		cr = crdup(icr);
1055 		(void) setpflags(NET_MAC_AWARE, 1, cr);
1056 	}
1057 
1058 	/*
1059 	 * clget() calls clnt_tli_kinit() which clears the xid, so we
1060 	 * are guaranteed to reprocess the retry as a new request.
1061 	 */
1062 	svp = mi->mi_curr_serv;
1063 	rpcerr.re_errno = nfs_clget(mi, svp, cr, &client, &ch, nfscl);
1064 
1065 	if (FAILOVER_MOUNT(mi)) {
1066 		mutex_enter(&mi->mi_lock);
1067 		DEC_READERS(mi);
1068 		mutex_exit(&mi->mi_lock);
1069 
1070 		if ((rpcerr.re_errno == ETIMEDOUT ||
1071 		    rpcerr.re_errno == ECONNRESET) &&
1072 		    failover_safe(fi)) {
1073 			if (svp == mi->mi_curr_serv)
1074 				failover_newserver(mi);
1075 			goto failoverretry;
1076 		}
1077 	}
1078 	if (rpcerr.re_errno != 0)
1079 		return (rpcerr.re_errno);
1080 
1081 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
1082 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS) {
1083 		timeo = (mi->mi_timeo * hz) / 10;
1084 	} else {
1085 		mutex_enter(&mi->mi_lock);
1086 		timeo = CLNT_SETTIMERS(client,
1087 		    &(mi->mi_timers[mi->mi_timer_type[which]]),
1088 		    &(mi->mi_timers[NFS_CALLTYPES]),
1089 		    (minimum_timeo[mi->mi_call_type[which]]*hz)>>3,
1090 		    (void (*)())NULL, (caddr_t)mi, 0);
1091 		mutex_exit(&mi->mi_lock);
1092 	}
1093 
1094 	/*
1095 	 * If hard mounted fs, retry call forever unless hard error occurs.
1096 	 */
1097 	do {
1098 		tryagain = FALSE;
1099 
1100 		if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
1101 			status = RPC_FAILED;
1102 			rpcerr.re_status = RPC_FAILED;
1103 			rpcerr.re_errno = EIO;
1104 			break;
1105 		}
1106 
1107 		TICK_TO_TIMEVAL(timeo, &wait);
1108 
1109 		/*
1110 		 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
1111 		 * and SIGTERM. (Preserving the existing masks).
1112 		 * Mask out SIGINT if mount option nointr is specified.
1113 		 */
1114 		sigintr(&smask, (int)mi->mi_flags & MI_INT);
1115 		if (!(mi->mi_flags & MI_INT))
1116 			client->cl_nosignal = TRUE;
1117 
1118 		/*
1119 		 * If there is a current signal, then don't bother
1120 		 * even trying to send out the request because we
1121 		 * won't be able to block waiting for the response.
1122 		 * Simply assume RPC_INTR and get on with it.
1123 		 */
1124 		if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING))
1125 			status = RPC_INTR;
1126 		else {
1127 			status = CLNT_CALL(client, which, xdrargs, argsp,
1128 			    xdrres, resp, wait);
1129 		}
1130 
1131 		if (!(mi->mi_flags & MI_INT))
1132 			client->cl_nosignal = FALSE;
1133 		/*
1134 		 * restore original signal mask
1135 		 */
1136 		sigunintr(&smask);
1137 
1138 		switch (status) {
1139 		case RPC_SUCCESS:
1140 			if ((mi->mi_flags & MI_DYNAMIC) &&
1141 			    mi->mi_timer_type[which] != 0 &&
1142 			    (mi->mi_curread != my_rsize ||
1143 			    mi->mi_curwrite != my_wsize))
1144 				(void) nfs_feedback(FEEDBACK_OK, which, mi);
1145 			break;
1146 
1147 		case RPC_INTR:
1148 			/*
1149 			 * There is no way to recover from this error,
1150 			 * even if mount option nointr is specified.
1151 			 * SIGKILL, for example, cannot be blocked.
1152 			 */
1153 			rpcerr.re_status = RPC_INTR;
1154 			rpcerr.re_errno = EINTR;
1155 			break;
1156 
1157 		case RPC_UDERROR:
1158 			/*
1159 			 * If the NFS server is local (vold) and
1160 			 * it goes away then we get RPC_UDERROR.
1161 			 * This is a retryable error, so we would
1162 			 * loop, so check to see if the specific
1163 			 * error was ECONNRESET, indicating that
1164 			 * target did not exist at all.  If so,
1165 			 * return with RPC_PROGUNAVAIL and
1166 			 * ECONNRESET to indicate why.
1167 			 */
1168 			CLNT_GETERR(client, &rpcerr);
1169 			if (rpcerr.re_errno == ECONNRESET) {
1170 				rpcerr.re_status = RPC_PROGUNAVAIL;
1171 				rpcerr.re_errno = ECONNRESET;
1172 				break;
1173 			}
1174 			/*FALLTHROUGH*/
1175 
1176 		default:		/* probably RPC_TIMEDOUT */
1177 			if (IS_UNRECOVERABLE_RPC(status))
1178 				break;
1179 
1180 			/*
1181 			 * increment server not responding count
1182 			 */
1183 			mutex_enter(&mi->mi_lock);
1184 			mi->mi_noresponse++;
1185 			mutex_exit(&mi->mi_lock);
1186 #ifdef DEBUG
1187 			nfscl->nfscl_stat.noresponse.value.ui64++;
1188 #endif
1189 
1190 			if (!(mi->mi_flags & MI_HARD)) {
1191 				if (!(mi->mi_flags & MI_SEMISOFT) ||
1192 				    (mi->mi_ss_call_type[which] == 0))
1193 					break;
1194 			}
1195 
1196 			/*
1197 			 * The call is in progress (over COTS).
1198 			 * Try the CLNT_CALL again, but don't
1199 			 * print a noisy error message.
1200 			 */
1201 			if (status == RPC_INPROGRESS) {
1202 				tryagain = TRUE;
1203 				break;
1204 			}
1205 
1206 			if (flags & RFSCALL_SOFT)
1207 				break;
1208 
1209 			/*
1210 			 * On zone shutdown, just move on.
1211 			 */
1212 			if (zone_status_get(curproc->p_zone) >=
1213 			    ZONE_IS_SHUTTING_DOWN) {
1214 				rpcerr.re_status = RPC_FAILED;
1215 				rpcerr.re_errno = EIO;
1216 				break;
1217 			}
1218 
1219 			/*
1220 			 * NFS client failover support
1221 			 *
1222 			 * If the current server just failed us, we'll
1223 			 * start the process of finding a new server.
1224 			 * After that, we can just retry.
1225 			 */
1226 			if (FAILOVER_MOUNT(mi) && failover_safe(fi)) {
1227 				if (svp == mi->mi_curr_serv)
1228 					failover_newserver(mi);
1229 				clfree_impl(client, ch, nfscl);
1230 				goto failoverretry;
1231 			}
1232 
1233 			tryagain = TRUE;
1234 			timeo = backoff(timeo);
1235 
1236 			CLNT_GETERR(client, &rpcerr_tmp);
1237 			if ((status == RPC_CANTSEND) &&
1238 			    (rpcerr_tmp.re_errno == ENOBUFS))
1239 				msg = SRV_QFULL_MSG;
1240 			else
1241 				msg = SRV_NOTRESP_MSG;
1242 
1243 			mutex_enter(&mi->mi_lock);
1244 			if (!(mi->mi_flags & MI_PRINTED)) {
1245 				mi->mi_flags |= MI_PRINTED;
1246 				mutex_exit(&mi->mi_lock);
1247 #ifdef DEBUG
1248 				zprintf(zoneid, msg, mi->mi_vers,
1249 				    svp->sv_hostname);
1250 #else
1251 				zprintf(zoneid, msg, svp->sv_hostname);
1252 #endif
1253 			} else
1254 				mutex_exit(&mi->mi_lock);
1255 			if (*douprintf && nfs_has_ctty()) {
1256 				*douprintf = 0;
1257 				if (!(mi->mi_flags & MI_NOPRINT))
1258 #ifdef DEBUG
1259 					uprintf(msg, mi->mi_vers,
1260 					    svp->sv_hostname);
1261 #else
1262 					uprintf(msg, svp->sv_hostname);
1263 #endif
1264 			}
1265 
1266 			/*
1267 			 * If doing dynamic adjustment of transfer
1268 			 * size and if it's a read or write call
1269 			 * and if the transfer size changed while
1270 			 * retransmitting or if the feedback routine
1271 			 * changed the transfer size,
1272 			 * then exit rfscall so that the transfer
1273 			 * size can be adjusted at the vnops level.
1274 			 */
1275 			if ((mi->mi_flags & MI_DYNAMIC) &&
1276 			    mi->mi_timer_type[which] != 0 &&
1277 			    (mi->mi_curread != my_rsize ||
1278 			    mi->mi_curwrite != my_wsize ||
1279 			    nfs_feedback(FEEDBACK_REXMIT1, which, mi))) {
1280 				/*
1281 				 * On read or write calls, return
1282 				 * back to the vnode ops level if
1283 				 * the transfer size changed.
1284 				 */
1285 				clfree_impl(client, ch, nfscl);
1286 				if (cred_cloned)
1287 					crfree(cr);
1288 				return (ENFS_TRYAGAIN);
1289 			}
1290 		}
1291 	} while (tryagain);
1292 
1293 	if (status != RPC_SUCCESS) {
1294 		/*
1295 		 * Let soft mounts use the timed out message.
1296 		 */
1297 		if (status == RPC_INPROGRESS)
1298 			status = RPC_TIMEDOUT;
1299 		nfscl->nfscl_stat.badcalls.value.ui64++;
1300 		if (status != RPC_INTR) {
1301 			mutex_enter(&mi->mi_lock);
1302 			mi->mi_flags |= MI_DOWN;
1303 			mutex_exit(&mi->mi_lock);
1304 			CLNT_GETERR(client, &rpcerr);
1305 #ifdef DEBUG
1306 			bufp = clnt_sperror(client, svp->sv_hostname);
1307 			zprintf(zoneid, "NFS%d %s failed for %s\n",
1308 			    mi->mi_vers, mi->mi_rfsnames[which], bufp);
1309 			if (nfs_has_ctty()) {
1310 				if (!(mi->mi_flags & MI_NOPRINT)) {
1311 					uprintf("NFS%d %s failed for %s\n",
1312 					    mi->mi_vers, mi->mi_rfsnames[which],
1313 					    bufp);
1314 				}
1315 			}
1316 			kmem_free(bufp, MAXPATHLEN);
1317 #else
1318 			zprintf(zoneid,
1319 			    "NFS %s failed for server %s: error %d (%s)\n",
1320 			    mi->mi_rfsnames[which], svp->sv_hostname,
1321 			    status, clnt_sperrno(status));
1322 			if (nfs_has_ctty()) {
1323 				if (!(mi->mi_flags & MI_NOPRINT)) {
1324 					uprintf(
1325 				"NFS %s failed for server %s: error %d (%s)\n",
1326 					    mi->mi_rfsnames[which],
1327 					    svp->sv_hostname, status,
1328 					    clnt_sperrno(status));
1329 				}
1330 			}
1331 #endif
1332 			/*
1333 			 * when CLNT_CALL() fails with RPC_AUTHERROR,
1334 			 * re_errno is set appropriately depending on
1335 			 * the authentication error
1336 			 */
1337 			if (status == RPC_VERSMISMATCH ||
1338 			    status == RPC_PROGVERSMISMATCH)
1339 				rpcerr.re_errno = EIO;
1340 		}
1341 	} else {
1342 		/*
1343 		 * Test the value of mi_down and mi_printed without
1344 		 * holding the mi_lock mutex.  If they are both zero,
1345 		 * then it is okay to skip the down and printed
1346 		 * processing.  This saves on a mutex_enter and
1347 		 * mutex_exit pair for a normal, successful RPC.
1348 		 * This was just complete overhead.
1349 		 */
1350 		if (mi->mi_flags & (MI_DOWN | MI_PRINTED)) {
1351 			mutex_enter(&mi->mi_lock);
1352 			mi->mi_flags &= ~MI_DOWN;
1353 			if (mi->mi_flags & MI_PRINTED) {
1354 				mi->mi_flags &= ~MI_PRINTED;
1355 				mutex_exit(&mi->mi_lock);
1356 #ifdef DEBUG
1357 			if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1358 				zprintf(zoneid, "NFS%d server %s ok\n",
1359 				    mi->mi_vers, svp->sv_hostname);
1360 #else
1361 			if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1362 				zprintf(zoneid, "NFS server %s ok\n",
1363 				    svp->sv_hostname);
1364 #endif
1365 			} else
1366 				mutex_exit(&mi->mi_lock);
1367 		}
1368 
1369 		if (*douprintf == 0) {
1370 			if (!(mi->mi_flags & MI_NOPRINT))
1371 #ifdef DEBUG
1372 				if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1373 					uprintf("NFS%d server %s ok\n",
1374 					    mi->mi_vers, svp->sv_hostname);
1375 #else
1376 			if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
1377 				uprintf("NFS server %s ok\n", svp->sv_hostname);
1378 #endif
1379 			*douprintf = 1;
1380 		}
1381 	}
1382 
1383 	clfree_impl(client, ch, nfscl);
1384 	if (cred_cloned)
1385 		crfree(cr);
1386 
1387 	ASSERT(rpcerr.re_status == RPC_SUCCESS || rpcerr.re_errno != 0);
1388 
1389 	if (rpc_status != NULL)
1390 		*rpc_status = rpcerr.re_status;
1391 
1392 	TRACE_1(TR_FAC_NFS, TR_RFSCALL_END, "rfscall_end:errno %d",
1393 	    rpcerr.re_errno);
1394 
1395 	return (rpcerr.re_errno);
1396 }
1397 
1398 #ifdef DEBUG
1399 static int acl2call_hits = 0;
1400 static int acl2call_misses = 0;
1401 #endif
1402 
1403 int
acl2call(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * cr,int * douprintf,enum nfsstat * statusp,int flags,failinfo_t * fi)1404 acl2call(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
1405     xdrproc_t xdrres, caddr_t resp, cred_t *cr, int *douprintf,
1406     enum nfsstat *statusp, int flags, failinfo_t *fi)
1407 {
1408 	int rpcerror;
1409 
1410 	rpcerror = aclcall(mi, which, xdrargs, argsp, xdrres, resp,
1411 	    cr, douprintf, flags, fi);
1412 	if (!rpcerror) {
1413 		/*
1414 		 * See comments with crnetadjust().
1415 		 */
1416 		if (*statusp == NFSERR_ACCES &&
1417 		    (cr = crnetadjust(cr)) != NULL) {
1418 #ifdef DEBUG
1419 			acl2call_hits++;
1420 #endif
1421 			rpcerror = aclcall(mi, which, xdrargs, argsp, xdrres,
1422 			    resp, cr, douprintf, flags, fi);
1423 			crfree(cr);
1424 #ifdef DEBUG
1425 			if (*statusp == NFSERR_ACCES)
1426 				acl2call_misses++;
1427 #endif
1428 		}
1429 	}
1430 
1431 	return (rpcerror);
1432 }
1433 
1434 #ifdef DEBUG
1435 static int acl3call_hits = 0;
1436 static int acl3call_misses = 0;
1437 #endif
1438 
1439 int
acl3call(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * cr,int * douprintf,nfsstat3 * statusp,int flags,failinfo_t * fi)1440 acl3call(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
1441     xdrproc_t xdrres, caddr_t resp, cred_t *cr, int *douprintf,
1442     nfsstat3 *statusp, int flags, failinfo_t *fi)
1443 {
1444 	int rpcerror;
1445 	int user_informed;
1446 
1447 	user_informed = 0;
1448 
1449 	do {
1450 		rpcerror = aclcall(mi, which, xdrargs, argsp, xdrres, resp,
1451 		    cr, douprintf, flags, fi);
1452 		if (!rpcerror) {
1453 			cred_t *crr;
1454 			if (*statusp == NFS3ERR_JUKEBOX) {
1455 				if (!user_informed) {
1456 					user_informed = 1;
1457 					uprintf(
1458 		"file temporarily unavailable on the server, retrying...\n");
1459 				}
1460 				delay(nfs3_jukebox_delay);
1461 			}
1462 			/*
1463 			 * See crnetadjust() for comments.
1464 			 */
1465 			else if (*statusp == NFS3ERR_ACCES &&
1466 			    (crr = crnetadjust(cr)) != NULL) {
1467 #ifdef DEBUG
1468 				acl3call_hits++;
1469 #endif
1470 				rpcerror = aclcall(mi, which, xdrargs, argsp,
1471 				    xdrres, resp, crr, douprintf, flags, fi);
1472 
1473 				crfree(crr);
1474 #ifdef DEBUG
1475 				if (*statusp == NFS3ERR_ACCES)
1476 					acl3call_misses++;
1477 #endif
1478 			}
1479 		}
1480 	} while (!rpcerror && *statusp == NFS3ERR_JUKEBOX);
1481 
1482 	return (rpcerror);
1483 }
1484 
1485 static int
aclcall(mntinfo_t * mi,rpcproc_t which,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,cred_t * icr,int * douprintf,int flags,failinfo_t * fi)1486 aclcall(mntinfo_t *mi, rpcproc_t which, xdrproc_t xdrargs, caddr_t argsp,
1487     xdrproc_t xdrres, caddr_t resp, cred_t *icr, int *douprintf,
1488     int flags, failinfo_t *fi)
1489 {
1490 	CLIENT *client;
1491 	struct chtab *ch;
1492 	cred_t *cr = icr;
1493 	bool_t cred_cloned = FALSE;
1494 	enum clnt_stat status;
1495 	struct rpc_err rpcerr;
1496 	struct timeval wait;
1497 	int timeo;		/* in units of hz */
1498 #if 0 /* notyet */
1499 	int my_rsize, my_wsize;
1500 #endif
1501 	bool_t tryagain;
1502 	k_sigset_t smask;
1503 	servinfo_t *svp;
1504 	struct nfs_clnt *nfscl;
1505 	zoneid_t zoneid = getzoneid();
1506 #ifdef DEBUG
1507 	char *bufp;
1508 #endif
1509 
1510 #if 0 /* notyet */
1511 	TRACE_2(TR_FAC_NFS, TR_RFSCALL_START,
1512 	    "rfscall_start:which %d mi %p", which, mi);
1513 #endif
1514 
1515 	nfscl = zone_getspecific(nfsclnt_zone_key, nfs_zone());
1516 	ASSERT(nfscl != NULL);
1517 
1518 	nfscl->nfscl_stat.calls.value.ui64++;
1519 	mi->mi_aclreqs[which].value.ui64++;
1520 
1521 	rpcerr.re_status = RPC_SUCCESS;
1522 
1523 	if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
1524 		rpcerr.re_status = RPC_FAILED;
1525 		rpcerr.re_errno = EIO;
1526 		return (rpcerr.re_errno);
1527 	}
1528 
1529 #if 0 /* notyet */
1530 	/*
1531 	 * Remember the transfer sizes in case
1532 	 * nfs_feedback changes them underneath us.
1533 	 */
1534 	my_rsize = mi->mi_curread;
1535 	my_wsize = mi->mi_curwrite;
1536 #endif
1537 
1538 	/*
1539 	 * NFS client failover support
1540 	 *
1541 	 * If this rnode is not in sync with the current server (VALID_FH),
1542 	 * we'd like to do a remap to get in sync.  We can be interrupted
1543 	 * in failover_remap(), and if so we'll bail.  Otherwise, we'll
1544 	 * use the best info we have to try the RPC.  Part of that is
1545 	 * unconditionally updating the filehandle copy kept for V3.
1546 	 *
1547 	 * Locking: INC_READERS/DEC_READERS is a poor man's interrruptible
1548 	 * rw_enter(); we're trying to keep the current server from being
1549 	 * changed on us until we're done with the remapping and have a
1550 	 * matching client handle.  We don't want to sending a filehandle
1551 	 * to the wrong host.
1552 	 */
1553 failoverretry:
1554 	if (FAILOVER_MOUNT(mi)) {
1555 		mutex_enter(&mi->mi_lock);
1556 		if (!(flags & RFSCALL_SOFT) && failover_safe(fi)) {
1557 			if (failover_wait(mi)) {
1558 				mutex_exit(&mi->mi_lock);
1559 				return (EINTR);
1560 			}
1561 		}
1562 		INC_READERS(mi);
1563 		mutex_exit(&mi->mi_lock);
1564 		if (fi) {
1565 			if (!VALID_FH(fi) &&
1566 			    !(flags & RFSCALL_SOFT) && failover_safe(fi)) {
1567 				int remaperr;
1568 
1569 				svp = mi->mi_curr_serv;
1570 				remaperr = failover_remap(fi);
1571 				if (remaperr != 0) {
1572 #ifdef DEBUG
1573 					if (remaperr != EINTR)
1574 						nfs_cmn_err(remaperr, CE_WARN,
1575 					    "aclcall couldn't failover: %m");
1576 #endif
1577 					mutex_enter(&mi->mi_lock);
1578 					DEC_READERS(mi);
1579 					mutex_exit(&mi->mi_lock);
1580 
1581 					/*
1582 					 * If failover_remap returns ETIMEDOUT
1583 					 * and the filesystem is hard mounted
1584 					 * we have to retry the call with a new
1585 					 * server.
1586 					 */
1587 					if ((mi->mi_flags & MI_HARD) &&
1588 					    IS_RECOVERABLE_ERROR(remaperr)) {
1589 						if (svp == mi->mi_curr_serv)
1590 							failover_newserver(mi);
1591 						rpcerr.re_status = RPC_SUCCESS;
1592 						goto failoverretry;
1593 					}
1594 					return (remaperr);
1595 				}
1596 			}
1597 			if (fi->fhp && fi->copyproc)
1598 				(*fi->copyproc)(fi->fhp, fi->vp);
1599 		}
1600 	}
1601 
1602 	/* For TSOL, use a new cred which has net_mac_aware flag */
1603 	if (!cred_cloned && is_system_labeled()) {
1604 		cred_cloned = TRUE;
1605 		cr = crdup(icr);
1606 		(void) setpflags(NET_MAC_AWARE, 1, cr);
1607 	}
1608 
1609 	/*
1610 	 * acl_clget() calls clnt_tli_kinit() which clears the xid, so we
1611 	 * are guaranteed to reprocess the retry as a new request.
1612 	 */
1613 	svp = mi->mi_curr_serv;
1614 	rpcerr.re_errno = acl_clget(mi, svp, cr, &client, &ch, nfscl);
1615 	if (FAILOVER_MOUNT(mi)) {
1616 		mutex_enter(&mi->mi_lock);
1617 		DEC_READERS(mi);
1618 		mutex_exit(&mi->mi_lock);
1619 
1620 		if ((rpcerr.re_errno == ETIMEDOUT ||
1621 		    rpcerr.re_errno == ECONNRESET) &&
1622 		    failover_safe(fi)) {
1623 			if (svp == mi->mi_curr_serv)
1624 				failover_newserver(mi);
1625 			goto failoverretry;
1626 		}
1627 	}
1628 	if (rpcerr.re_errno != 0) {
1629 		if (cred_cloned)
1630 			crfree(cr);
1631 		return (rpcerr.re_errno);
1632 	}
1633 
1634 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
1635 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS) {
1636 		timeo = (mi->mi_timeo * hz) / 10;
1637 	} else {
1638 		mutex_enter(&mi->mi_lock);
1639 		timeo = CLNT_SETTIMERS(client,
1640 		    &(mi->mi_timers[mi->mi_acl_timer_type[which]]),
1641 		    &(mi->mi_timers[NFS_CALLTYPES]),
1642 		    (minimum_timeo[mi->mi_acl_call_type[which]]*hz)>>3,
1643 		    (void (*)()) 0, (caddr_t)mi, 0);
1644 		mutex_exit(&mi->mi_lock);
1645 	}
1646 
1647 	/*
1648 	 * If hard mounted fs, retry call forever unless hard error occurs.
1649 	 */
1650 	do {
1651 		tryagain = FALSE;
1652 
1653 		if (FS_OR_ZONE_GONE(mi->mi_vfsp)) {
1654 			status = RPC_FAILED;
1655 			rpcerr.re_status = RPC_FAILED;
1656 			rpcerr.re_errno = EIO;
1657 			break;
1658 		}
1659 
1660 		TICK_TO_TIMEVAL(timeo, &wait);
1661 
1662 		/*
1663 		 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
1664 		 * and SIGTERM. (Preserving the existing masks).
1665 		 * Mask out SIGINT if mount option nointr is specified.
1666 		 */
1667 		sigintr(&smask, (int)mi->mi_flags & MI_INT);
1668 		if (!(mi->mi_flags & MI_INT))
1669 			client->cl_nosignal = TRUE;
1670 
1671 		/*
1672 		 * If there is a current signal, then don't bother
1673 		 * even trying to send out the request because we
1674 		 * won't be able to block waiting for the response.
1675 		 * Simply assume RPC_INTR and get on with it.
1676 		 */
1677 		if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING))
1678 			status = RPC_INTR;
1679 		else {
1680 			status = CLNT_CALL(client, which, xdrargs, argsp,
1681 			    xdrres, resp, wait);
1682 		}
1683 
1684 		if (!(mi->mi_flags & MI_INT))
1685 			client->cl_nosignal = FALSE;
1686 		/*
1687 		 * restore original signal mask
1688 		 */
1689 		sigunintr(&smask);
1690 
1691 		switch (status) {
1692 		case RPC_SUCCESS:
1693 #if 0 /* notyet */
1694 			if ((mi->mi_flags & MI_DYNAMIC) &&
1695 			    mi->mi_timer_type[which] != 0 &&
1696 			    (mi->mi_curread != my_rsize ||
1697 			    mi->mi_curwrite != my_wsize))
1698 				(void) nfs_feedback(FEEDBACK_OK, which, mi);
1699 #endif
1700 			break;
1701 
1702 		/*
1703 		 * Unfortunately, there are servers in the world which
1704 		 * are not coded correctly.  They are not prepared to
1705 		 * handle RPC requests to the NFS port which are not
1706 		 * NFS requests.  Thus, they may try to process the
1707 		 * NFS_ACL request as if it were an NFS request.  This
1708 		 * does not work.  Generally, an error will be generated
1709 		 * on the client because it will not be able to decode
1710 		 * the response from the server.  However, it seems
1711 		 * possible that the server may not be able to decode
1712 		 * the arguments.  Thus, the criteria for deciding
1713 		 * whether the server supports NFS_ACL or not is whether
1714 		 * the following RPC errors are returned from CLNT_CALL.
1715 		 */
1716 		case RPC_CANTDECODERES:
1717 		case RPC_PROGUNAVAIL:
1718 		case RPC_CANTDECODEARGS:
1719 		case RPC_PROGVERSMISMATCH:
1720 			mutex_enter(&mi->mi_lock);
1721 			mi->mi_flags &= ~(MI_ACL | MI_EXTATTR);
1722 			mutex_exit(&mi->mi_lock);
1723 			break;
1724 
1725 		/*
1726 		 * If the server supports NFS_ACL but not the new ops
1727 		 * for extended attributes, make sure we don't retry.
1728 		 */
1729 		case RPC_PROCUNAVAIL:
1730 			mutex_enter(&mi->mi_lock);
1731 			mi->mi_flags &= ~MI_EXTATTR;
1732 			mutex_exit(&mi->mi_lock);
1733 			break;
1734 
1735 		case RPC_INTR:
1736 			/*
1737 			 * There is no way to recover from this error,
1738 			 * even if mount option nointr is specified.
1739 			 * SIGKILL, for example, cannot be blocked.
1740 			 */
1741 			rpcerr.re_status = RPC_INTR;
1742 			rpcerr.re_errno = EINTR;
1743 			break;
1744 
1745 		case RPC_UDERROR:
1746 			/*
1747 			 * If the NFS server is local (vold) and
1748 			 * it goes away then we get RPC_UDERROR.
1749 			 * This is a retryable error, so we would
1750 			 * loop, so check to see if the specific
1751 			 * error was ECONNRESET, indicating that
1752 			 * target did not exist at all.  If so,
1753 			 * return with RPC_PROGUNAVAIL and
1754 			 * ECONNRESET to indicate why.
1755 			 */
1756 			CLNT_GETERR(client, &rpcerr);
1757 			if (rpcerr.re_errno == ECONNRESET) {
1758 				rpcerr.re_status = RPC_PROGUNAVAIL;
1759 				rpcerr.re_errno = ECONNRESET;
1760 				break;
1761 			}
1762 			/*FALLTHROUGH*/
1763 
1764 		default:		/* probably RPC_TIMEDOUT */
1765 			if (IS_UNRECOVERABLE_RPC(status))
1766 				break;
1767 
1768 			/*
1769 			 * increment server not responding count
1770 			 */
1771 			mutex_enter(&mi->mi_lock);
1772 			mi->mi_noresponse++;
1773 			mutex_exit(&mi->mi_lock);
1774 #ifdef DEBUG
1775 			nfscl->nfscl_stat.noresponse.value.ui64++;
1776 #endif
1777 
1778 			if (!(mi->mi_flags & MI_HARD)) {
1779 				if (!(mi->mi_flags & MI_SEMISOFT) ||
1780 				    (mi->mi_acl_ss_call_type[which] == 0))
1781 					break;
1782 			}
1783 
1784 			/*
1785 			 * The call is in progress (over COTS).
1786 			 * Try the CLNT_CALL again, but don't
1787 			 * print a noisy error message.
1788 			 */
1789 			if (status == RPC_INPROGRESS) {
1790 				tryagain = TRUE;
1791 				break;
1792 			}
1793 
1794 			if (flags & RFSCALL_SOFT)
1795 				break;
1796 
1797 			/*
1798 			 * On zone shutdown, just move on.
1799 			 */
1800 			if (zone_status_get(curproc->p_zone) >=
1801 			    ZONE_IS_SHUTTING_DOWN) {
1802 				rpcerr.re_status = RPC_FAILED;
1803 				rpcerr.re_errno = EIO;
1804 				break;
1805 			}
1806 
1807 			/*
1808 			 * NFS client failover support
1809 			 *
1810 			 * If the current server just failed us, we'll
1811 			 * start the process of finding a new server.
1812 			 * After that, we can just retry.
1813 			 */
1814 			if (FAILOVER_MOUNT(mi) && failover_safe(fi)) {
1815 				if (svp == mi->mi_curr_serv)
1816 					failover_newserver(mi);
1817 				clfree_impl(client, ch, nfscl);
1818 				goto failoverretry;
1819 			}
1820 
1821 			tryagain = TRUE;
1822 			timeo = backoff(timeo);
1823 			mutex_enter(&mi->mi_lock);
1824 			if (!(mi->mi_flags & MI_PRINTED)) {
1825 				mi->mi_flags |= MI_PRINTED;
1826 				mutex_exit(&mi->mi_lock);
1827 #ifdef DEBUG
1828 				zprintf(zoneid,
1829 			"NFS_ACL%d server %s not responding still trying\n",
1830 				    mi->mi_vers, svp->sv_hostname);
1831 #else
1832 				zprintf(zoneid,
1833 			    "NFS server %s not responding still trying\n",
1834 				    svp->sv_hostname);
1835 #endif
1836 			} else
1837 				mutex_exit(&mi->mi_lock);
1838 			if (*douprintf && nfs_has_ctty()) {
1839 				*douprintf = 0;
1840 				if (!(mi->mi_flags & MI_NOPRINT))
1841 #ifdef DEBUG
1842 					uprintf(
1843 			"NFS_ACL%d server %s not responding still trying\n",
1844 					    mi->mi_vers, svp->sv_hostname);
1845 #else
1846 					uprintf(
1847 			    "NFS server %s not responding still trying\n",
1848 					    svp->sv_hostname);
1849 #endif
1850 			}
1851 
1852 #if 0 /* notyet */
1853 			/*
1854 			 * If doing dynamic adjustment of transfer
1855 			 * size and if it's a read or write call
1856 			 * and if the transfer size changed while
1857 			 * retransmitting or if the feedback routine
1858 			 * changed the transfer size,
1859 			 * then exit rfscall so that the transfer
1860 			 * size can be adjusted at the vnops level.
1861 			 */
1862 			if ((mi->mi_flags & MI_DYNAMIC) &&
1863 			    mi->mi_acl_timer_type[which] != 0 &&
1864 			    (mi->mi_curread != my_rsize ||
1865 			    mi->mi_curwrite != my_wsize ||
1866 			    nfs_feedback(FEEDBACK_REXMIT1, which, mi))) {
1867 				/*
1868 				 * On read or write calls, return
1869 				 * back to the vnode ops level if
1870 				 * the transfer size changed.
1871 				 */
1872 				clfree_impl(client, ch, nfscl);
1873 				if (cred_cloned)
1874 					crfree(cr);
1875 				return (ENFS_TRYAGAIN);
1876 			}
1877 #endif
1878 		}
1879 	} while (tryagain);
1880 
1881 	if (status != RPC_SUCCESS) {
1882 		/*
1883 		 * Let soft mounts use the timed out message.
1884 		 */
1885 		if (status == RPC_INPROGRESS)
1886 			status = RPC_TIMEDOUT;
1887 		nfscl->nfscl_stat.badcalls.value.ui64++;
1888 		if (status == RPC_CANTDECODERES ||
1889 		    status == RPC_PROGUNAVAIL ||
1890 		    status == RPC_PROCUNAVAIL ||
1891 		    status == RPC_CANTDECODEARGS ||
1892 		    status == RPC_PROGVERSMISMATCH)
1893 			CLNT_GETERR(client, &rpcerr);
1894 		else if (status != RPC_INTR) {
1895 			mutex_enter(&mi->mi_lock);
1896 			mi->mi_flags |= MI_DOWN;
1897 			mutex_exit(&mi->mi_lock);
1898 			CLNT_GETERR(client, &rpcerr);
1899 #ifdef DEBUG
1900 			bufp = clnt_sperror(client, svp->sv_hostname);
1901 			zprintf(zoneid, "NFS_ACL%d %s failed for %s\n",
1902 			    mi->mi_vers, mi->mi_aclnames[which], bufp);
1903 			if (nfs_has_ctty()) {
1904 				if (!(mi->mi_flags & MI_NOPRINT)) {
1905 					uprintf("NFS_ACL%d %s failed for %s\n",
1906 					    mi->mi_vers, mi->mi_aclnames[which],
1907 					    bufp);
1908 				}
1909 			}
1910 			kmem_free(bufp, MAXPATHLEN);
1911 #else
1912 			zprintf(zoneid,
1913 			    "NFS %s failed for server %s: error %d (%s)\n",
1914 			    mi->mi_aclnames[which], svp->sv_hostname,
1915 			    status, clnt_sperrno(status));
1916 			if (nfs_has_ctty()) {
1917 				if (!(mi->mi_flags & MI_NOPRINT))
1918 					uprintf(
1919 				"NFS %s failed for server %s: error %d (%s)\n",
1920 					    mi->mi_aclnames[which],
1921 					    svp->sv_hostname, status,
1922 					    clnt_sperrno(status));
1923 			}
1924 #endif
1925 			/*
1926 			 * when CLNT_CALL() fails with RPC_AUTHERROR,
1927 			 * re_errno is set appropriately depending on
1928 			 * the authentication error
1929 			 */
1930 			if (status == RPC_VERSMISMATCH ||
1931 			    status == RPC_PROGVERSMISMATCH)
1932 				rpcerr.re_errno = EIO;
1933 		}
1934 	} else {
1935 		/*
1936 		 * Test the value of mi_down and mi_printed without
1937 		 * holding the mi_lock mutex.  If they are both zero,
1938 		 * then it is okay to skip the down and printed
1939 		 * processing.  This saves on a mutex_enter and
1940 		 * mutex_exit pair for a normal, successful RPC.
1941 		 * This was just complete overhead.
1942 		 */
1943 		if (mi->mi_flags & (MI_DOWN | MI_PRINTED)) {
1944 			mutex_enter(&mi->mi_lock);
1945 			mi->mi_flags &= ~MI_DOWN;
1946 			if (mi->mi_flags & MI_PRINTED) {
1947 				mi->mi_flags &= ~MI_PRINTED;
1948 				mutex_exit(&mi->mi_lock);
1949 #ifdef DEBUG
1950 				zprintf(zoneid, "NFS_ACL%d server %s ok\n",
1951 				    mi->mi_vers, svp->sv_hostname);
1952 #else
1953 				zprintf(zoneid, "NFS server %s ok\n",
1954 				    svp->sv_hostname);
1955 #endif
1956 			} else
1957 				mutex_exit(&mi->mi_lock);
1958 		}
1959 
1960 		if (*douprintf == 0) {
1961 			if (!(mi->mi_flags & MI_NOPRINT))
1962 #ifdef DEBUG
1963 				uprintf("NFS_ACL%d server %s ok\n",
1964 				    mi->mi_vers, svp->sv_hostname);
1965 #else
1966 				uprintf("NFS server %s ok\n", svp->sv_hostname);
1967 #endif
1968 			*douprintf = 1;
1969 		}
1970 	}
1971 
1972 	clfree_impl(client, ch, nfscl);
1973 	if (cred_cloned)
1974 		crfree(cr);
1975 
1976 	ASSERT(rpcerr.re_status == RPC_SUCCESS || rpcerr.re_errno != 0);
1977 
1978 #if 0 /* notyet */
1979 	TRACE_1(TR_FAC_NFS, TR_RFSCALL_END, "rfscall_end:errno %d",
1980 	    rpcerr.re_errno);
1981 #endif
1982 
1983 	return (rpcerr.re_errno);
1984 }
1985 
1986 int
vattr_to_sattr(struct vattr * vap,struct nfssattr * sa)1987 vattr_to_sattr(struct vattr *vap, struct nfssattr *sa)
1988 {
1989 	uint_t mask = vap->va_mask;
1990 
1991 	if (!(mask & AT_MODE))
1992 		sa->sa_mode = (uint32_t)-1;
1993 	else
1994 		sa->sa_mode = vap->va_mode;
1995 	if (!(mask & AT_UID))
1996 		sa->sa_uid = (uint32_t)-1;
1997 	else
1998 		sa->sa_uid = (uint32_t)vap->va_uid;
1999 	if (!(mask & AT_GID))
2000 		sa->sa_gid = (uint32_t)-1;
2001 	else
2002 		sa->sa_gid = (uint32_t)vap->va_gid;
2003 	if (!(mask & AT_SIZE))
2004 		sa->sa_size = (uint32_t)-1;
2005 	else
2006 		sa->sa_size = (uint32_t)vap->va_size;
2007 	if (!(mask & AT_ATIME))
2008 		sa->sa_atime.tv_sec = sa->sa_atime.tv_usec = (int32_t)-1;
2009 	else {
2010 		/* check time validity */
2011 		if (! NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
2012 			return (EOVERFLOW);
2013 		}
2014 		sa->sa_atime.tv_sec = vap->va_atime.tv_sec;
2015 		sa->sa_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
2016 	}
2017 	if (!(mask & AT_MTIME))
2018 		sa->sa_mtime.tv_sec = sa->sa_mtime.tv_usec = (int32_t)-1;
2019 	else {
2020 		/* check time validity */
2021 		if (! NFS_TIME_T_OK(vap->va_mtime.tv_sec)) {
2022 			return (EOVERFLOW);
2023 		}
2024 		sa->sa_mtime.tv_sec = vap->va_mtime.tv_sec;
2025 		sa->sa_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
2026 	}
2027 	return (0);
2028 }
2029 
2030 int
vattr_to_sattr3(struct vattr * vap,sattr3 * sa)2031 vattr_to_sattr3(struct vattr *vap, sattr3 *sa)
2032 {
2033 	uint_t mask = vap->va_mask;
2034 
2035 	if (!(mask & AT_MODE))
2036 		sa->mode.set_it = FALSE;
2037 	else {
2038 		sa->mode.set_it = TRUE;
2039 		sa->mode.mode = (mode3)vap->va_mode;
2040 	}
2041 	if (!(mask & AT_UID))
2042 		sa->uid.set_it = FALSE;
2043 	else {
2044 		sa->uid.set_it = TRUE;
2045 		sa->uid.uid = (uid3)vap->va_uid;
2046 	}
2047 	if (!(mask & AT_GID))
2048 		sa->gid.set_it = FALSE;
2049 	else {
2050 		sa->gid.set_it = TRUE;
2051 		sa->gid.gid = (gid3)vap->va_gid;
2052 	}
2053 	if (!(mask & AT_SIZE))
2054 		sa->size.set_it = FALSE;
2055 	else {
2056 		sa->size.set_it = TRUE;
2057 		sa->size.size = (size3)vap->va_size;
2058 	}
2059 	if (!(mask & AT_ATIME))
2060 		sa->atime.set_it = DONT_CHANGE;
2061 	else {
2062 		/* check time validity */
2063 		if (! NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
2064 			return (EOVERFLOW);
2065 		}
2066 		sa->atime.set_it = SET_TO_CLIENT_TIME;
2067 		sa->atime.atime.seconds = (uint32)vap->va_atime.tv_sec;
2068 		sa->atime.atime.nseconds = (uint32)vap->va_atime.tv_nsec;
2069 	}
2070 	if (!(mask & AT_MTIME))
2071 		sa->mtime.set_it = DONT_CHANGE;
2072 	else {
2073 		/* check time validity */
2074 		if (! NFS_TIME_T_OK(vap->va_mtime.tv_sec)) {
2075 			return (EOVERFLOW);
2076 		}
2077 		sa->mtime.set_it = SET_TO_CLIENT_TIME;
2078 		sa->mtime.mtime.seconds = (uint32)vap->va_mtime.tv_sec;
2079 		sa->mtime.mtime.nseconds = (uint32)vap->va_mtime.tv_nsec;
2080 	}
2081 	return (0);
2082 }
2083 
2084 void
setdiropargs(struct nfsdiropargs * da,char * nm,vnode_t * dvp)2085 setdiropargs(struct nfsdiropargs *da, char *nm, vnode_t *dvp)
2086 {
2087 
2088 	da->da_fhandle = VTOFH(dvp);
2089 	da->da_name = nm;
2090 	da->da_flags = 0;
2091 }
2092 
2093 void
setdiropargs3(diropargs3 * da,char * nm,vnode_t * dvp)2094 setdiropargs3(diropargs3 *da, char *nm, vnode_t *dvp)
2095 {
2096 
2097 	da->dirp = VTOFH3(dvp);
2098 	da->name = nm;
2099 }
2100 
2101 int
setdirgid(vnode_t * dvp,gid_t * gidp,cred_t * cr)2102 setdirgid(vnode_t *dvp, gid_t *gidp, cred_t *cr)
2103 {
2104 	int error;
2105 	rnode_t *rp;
2106 	struct vattr va;
2107 
2108 	va.va_mask = AT_MODE | AT_GID;
2109 	error = VOP_GETATTR(dvp, &va, 0, cr, NULL);
2110 	if (error)
2111 		return (error);
2112 
2113 	/*
2114 	 * To determine the expected group-id of the created file:
2115 	 *  1)	If the filesystem was not mounted with the Old-BSD-compatible
2116 	 *	GRPID option, and the directory's set-gid bit is clear,
2117 	 *	then use the process's gid.
2118 	 *  2)	Otherwise, set the group-id to the gid of the parent directory.
2119 	 */
2120 	rp = VTOR(dvp);
2121 	mutex_enter(&rp->r_statelock);
2122 	if (!(VTOMI(dvp)->mi_flags & MI_GRPID) && !(va.va_mode & VSGID))
2123 		*gidp = crgetgid(cr);
2124 	else
2125 		*gidp = va.va_gid;
2126 	mutex_exit(&rp->r_statelock);
2127 	return (0);
2128 }
2129 
2130 int
setdirmode(vnode_t * dvp,mode_t * omp,cred_t * cr)2131 setdirmode(vnode_t *dvp, mode_t *omp, cred_t *cr)
2132 {
2133 	int error;
2134 	struct vattr va;
2135 
2136 	va.va_mask = AT_MODE;
2137 	error = VOP_GETATTR(dvp, &va, 0, cr, NULL);
2138 	if (error)
2139 		return (error);
2140 
2141 	/*
2142 	 * Modify the expected mode (om) so that the set-gid bit matches
2143 	 * that of the parent directory (dvp).
2144 	 */
2145 	if (va.va_mode & VSGID)
2146 		*omp |= VSGID;
2147 	else
2148 		*omp &= ~VSGID;
2149 	return (0);
2150 }
2151 
2152 void
nfs_setswaplike(vnode_t * vp,vattr_t * vap)2153 nfs_setswaplike(vnode_t *vp, vattr_t *vap)
2154 {
2155 
2156 	if (vp->v_type == VREG && (vap->va_mode & (VEXEC | VSVTX)) == VSVTX) {
2157 		if (!(vp->v_flag & VSWAPLIKE)) {
2158 			mutex_enter(&vp->v_lock);
2159 			vp->v_flag |= VSWAPLIKE;
2160 			mutex_exit(&vp->v_lock);
2161 		}
2162 	} else {
2163 		if (vp->v_flag & VSWAPLIKE) {
2164 			mutex_enter(&vp->v_lock);
2165 			vp->v_flag &= ~VSWAPLIKE;
2166 			mutex_exit(&vp->v_lock);
2167 		}
2168 	}
2169 }
2170 
2171 /*
2172  * Free the resources associated with an rnode.
2173  */
2174 static void
rinactive(rnode_t * rp,cred_t * cr)2175 rinactive(rnode_t *rp, cred_t *cr)
2176 {
2177 	vnode_t *vp;
2178 	cred_t *cred;
2179 	char *contents;
2180 	int size;
2181 	vsecattr_t *vsp;
2182 	int error;
2183 	nfs3_pathconf_info *info;
2184 
2185 	/*
2186 	 * Before freeing anything, wait until all asynchronous
2187 	 * activity is done on this rnode.  This will allow all
2188 	 * asynchronous read ahead and write behind i/o's to
2189 	 * finish.
2190 	 */
2191 	mutex_enter(&rp->r_statelock);
2192 	while (rp->r_count > 0)
2193 		cv_wait(&rp->r_cv, &rp->r_statelock);
2194 	mutex_exit(&rp->r_statelock);
2195 
2196 	/*
2197 	 * Flush and invalidate all pages associated with the vnode.
2198 	 */
2199 	vp = RTOV(rp);
2200 	if (vn_has_cached_data(vp)) {
2201 		ASSERT(vp->v_type != VCHR);
2202 		if ((rp->r_flags & RDIRTY) && !rp->r_error) {
2203 			error = VOP_PUTPAGE(vp, (u_offset_t)0, 0, 0, cr, NULL);
2204 			if (error && (error == ENOSPC || error == EDQUOT)) {
2205 				mutex_enter(&rp->r_statelock);
2206 				if (!rp->r_error)
2207 					rp->r_error = error;
2208 				mutex_exit(&rp->r_statelock);
2209 			}
2210 		}
2211 		nfs_invalidate_pages(vp, (u_offset_t)0, cr);
2212 	}
2213 
2214 	/*
2215 	 * Free any held credentials and caches which may be associated
2216 	 * with this rnode.
2217 	 */
2218 	mutex_enter(&rp->r_statelock);
2219 	cred = rp->r_cred;
2220 	rp->r_cred = NULL;
2221 	contents = rp->r_symlink.contents;
2222 	size = rp->r_symlink.size;
2223 	rp->r_symlink.contents = NULL;
2224 	vsp = rp->r_secattr;
2225 	rp->r_secattr = NULL;
2226 	info = rp->r_pathconf;
2227 	rp->r_pathconf = NULL;
2228 	mutex_exit(&rp->r_statelock);
2229 
2230 	/*
2231 	 * Free the held credential.
2232 	 */
2233 	if (cred != NULL)
2234 		crfree(cred);
2235 
2236 	/*
2237 	 * Free the access cache entries.
2238 	 */
2239 	(void) nfs_access_purge_rp(rp);
2240 
2241 	/*
2242 	 * Free the readdir cache entries.
2243 	 */
2244 	if (HAVE_RDDIR_CACHE(rp))
2245 		nfs_purge_rddir_cache(vp);
2246 
2247 	/*
2248 	 * Free the symbolic link cache.
2249 	 */
2250 	if (contents != NULL) {
2251 
2252 		kmem_free((void *)contents, size);
2253 	}
2254 
2255 	/*
2256 	 * Free any cached ACL.
2257 	 */
2258 	if (vsp != NULL)
2259 		nfs_acl_free(vsp);
2260 
2261 	/*
2262 	 * Free any cached pathconf information.
2263 	 */
2264 	if (info != NULL)
2265 		kmem_free(info, sizeof (*info));
2266 }
2267 
2268 /*
2269  * Return a vnode for the given NFS Version 2 file handle.
2270  * If no rnode exists for this fhandle, create one and put it
2271  * into the hash queues.  If the rnode for this fhandle
2272  * already exists, return it.
2273  *
2274  * Note: make_rnode() may upgrade the hash bucket lock to exclusive.
2275  */
2276 vnode_t *
makenfsnode(fhandle_t * fh,struct nfsfattr * attr,struct vfs * vfsp,hrtime_t t,cred_t * cr,char * dnm,char * nm)2277 makenfsnode(fhandle_t *fh, struct nfsfattr *attr, struct vfs *vfsp,
2278     hrtime_t t, cred_t *cr, char *dnm, char *nm)
2279 {
2280 	int newnode;
2281 	int index;
2282 	vnode_t *vp;
2283 	nfs_fhandle nfh;
2284 	vattr_t va;
2285 
2286 	nfh.fh_len = NFS_FHSIZE;
2287 	bcopy(fh, nfh.fh_buf, NFS_FHSIZE);
2288 
2289 	index = rtablehash(&nfh);
2290 	rw_enter(&rtable[index].r_lock, RW_READER);
2291 
2292 	vp = make_rnode(&nfh, &rtable[index], vfsp, nfs_vnodeops,
2293 	    nfs_putapage, nfs_rddir_compar, &newnode, cr, dnm, nm);
2294 
2295 	if (attr != NULL) {
2296 		if (!newnode) {
2297 			rw_exit(&rtable[index].r_lock);
2298 			(void) nfs_cache_fattr(vp, attr, &va, t, cr);
2299 		} else {
2300 			if (attr->na_type < NFNON || attr->na_type > NFSOC)
2301 				vp->v_type = VBAD;
2302 			else
2303 				vp->v_type = n2v_type(attr);
2304 			/*
2305 			 * A translation here seems to be necessary
2306 			 * because this function can be called
2307 			 * with `attr' that has come from the wire,
2308 			 * and been operated on by vattr_to_nattr().
2309 			 * See nfsrootvp()->VOP_GETTATTR()->nfsgetattr()
2310 			 * ->nfs_getattr_otw()->rfscall()->vattr_to_nattr()
2311 			 * ->makenfsnode().
2312 			 */
2313 			if ((attr->na_rdev & 0xffff0000) == 0)
2314 				vp->v_rdev = nfsv2_expdev(attr->na_rdev);
2315 			else
2316 				vp->v_rdev = expldev(n2v_rdev(attr));
2317 			nfs_attrcache(vp, attr, t);
2318 			rw_exit(&rtable[index].r_lock);
2319 		}
2320 	} else {
2321 		if (newnode) {
2322 			PURGE_ATTRCACHE(vp);
2323 		}
2324 		rw_exit(&rtable[index].r_lock);
2325 	}
2326 
2327 	return (vp);
2328 }
2329 
2330 /*
2331  * Return a vnode for the given NFS Version 3 file handle.
2332  * If no rnode exists for this fhandle, create one and put it
2333  * into the hash queues.  If the rnode for this fhandle
2334  * already exists, return it.
2335  *
2336  * Note: make_rnode() may upgrade the hash bucket lock to exclusive.
2337  */
2338 vnode_t *
makenfs3node_va(nfs_fh3 * fh,vattr_t * vap,struct vfs * vfsp,hrtime_t t,cred_t * cr,char * dnm,char * nm)2339 makenfs3node_va(nfs_fh3 *fh, vattr_t *vap, struct vfs *vfsp, hrtime_t t,
2340     cred_t *cr, char *dnm, char *nm)
2341 {
2342 	int newnode;
2343 	int index;
2344 	vnode_t *vp;
2345 
2346 	index = rtablehash((nfs_fhandle *)fh);
2347 	rw_enter(&rtable[index].r_lock, RW_READER);
2348 
2349 	vp = make_rnode((nfs_fhandle *)fh, &rtable[index], vfsp,
2350 	    nfs3_vnodeops, nfs3_putapage, nfs3_rddir_compar, &newnode, cr,
2351 	    dnm, nm);
2352 
2353 	if (vap == NULL) {
2354 		if (newnode) {
2355 			PURGE_ATTRCACHE(vp);
2356 		}
2357 		rw_exit(&rtable[index].r_lock);
2358 		return (vp);
2359 	}
2360 
2361 	if (!newnode) {
2362 		rw_exit(&rtable[index].r_lock);
2363 		nfs_attr_cache(vp, vap, t, cr);
2364 	} else {
2365 		rnode_t *rp = VTOR(vp);
2366 
2367 		vp->v_type = vap->va_type;
2368 		vp->v_rdev = vap->va_rdev;
2369 
2370 		mutex_enter(&rp->r_statelock);
2371 		if (rp->r_mtime <= t)
2372 			nfs_attrcache_va(vp, vap);
2373 		mutex_exit(&rp->r_statelock);
2374 		rw_exit(&rtable[index].r_lock);
2375 	}
2376 
2377 	return (vp);
2378 }
2379 
2380 vnode_t *
makenfs3node(nfs_fh3 * fh,fattr3 * attr,struct vfs * vfsp,hrtime_t t,cred_t * cr,char * dnm,char * nm)2381 makenfs3node(nfs_fh3 *fh, fattr3 *attr, struct vfs *vfsp, hrtime_t t,
2382     cred_t *cr, char *dnm, char *nm)
2383 {
2384 	int newnode;
2385 	int index;
2386 	vnode_t *vp;
2387 	vattr_t va;
2388 
2389 	index = rtablehash((nfs_fhandle *)fh);
2390 	rw_enter(&rtable[index].r_lock, RW_READER);
2391 
2392 	vp = make_rnode((nfs_fhandle *)fh, &rtable[index], vfsp,
2393 	    nfs3_vnodeops, nfs3_putapage, nfs3_rddir_compar, &newnode, cr,
2394 	    dnm, nm);
2395 
2396 	if (attr == NULL) {
2397 		if (newnode) {
2398 			PURGE_ATTRCACHE(vp);
2399 		}
2400 		rw_exit(&rtable[index].r_lock);
2401 		return (vp);
2402 	}
2403 
2404 	if (!newnode) {
2405 		rw_exit(&rtable[index].r_lock);
2406 		(void) nfs3_cache_fattr3(vp, attr, &va, t, cr);
2407 	} else {
2408 		if (attr->type < NF3REG || attr->type > NF3FIFO)
2409 			vp->v_type = VBAD;
2410 		else
2411 			vp->v_type = nf3_to_vt[attr->type];
2412 		vp->v_rdev = makedevice(attr->rdev.specdata1,
2413 		    attr->rdev.specdata2);
2414 		nfs3_attrcache(vp, attr, t);
2415 		rw_exit(&rtable[index].r_lock);
2416 	}
2417 
2418 	return (vp);
2419 }
2420 
2421 /*
2422  * Read this comment before making changes to rtablehash()!
2423  * This is a hash function in which seemingly obvious and harmless
2424  * changes can cause escalations costing million dollars!
2425  * Know what you are doing.
2426  *
2427  * rtablehash() implements Jenkins' one-at-a-time hash algorithm.  The
2428  * algorithm is currently detailed here:
2429  *
2430  *   http://burtleburtle.net/bob/hash/doobs.html
2431  *
2432  * Of course, the above link may not be valid by the time you are reading
2433  * this, but suffice it to say that the one-at-a-time algorithm works well in
2434  * almost all cases.  If you are changing the algorithm be sure to verify that
2435  * the hash algorithm still provides even distribution in all cases and with
2436  * any server returning filehandles in whatever order (sequential or random).
2437  */
2438 static int
rtablehash(nfs_fhandle * fh)2439 rtablehash(nfs_fhandle *fh)
2440 {
2441 	ulong_t hash, len, i;
2442 	char *key;
2443 
2444 	key = fh->fh_buf;
2445 	len = (ulong_t)fh->fh_len;
2446 	for (hash = 0, i = 0; i < len; i++) {
2447 		hash += key[i];
2448 		hash += (hash << 10);
2449 		hash ^= (hash >> 6);
2450 	}
2451 	hash += (hash << 3);
2452 	hash ^= (hash >> 11);
2453 	hash += (hash << 15);
2454 	return (hash & rtablemask);
2455 }
2456 
2457 static vnode_t *
make_rnode(nfs_fhandle * fh,rhashq_t * rhtp,struct vfs * vfsp,struct vnodeops * vops,int (* putapage)(vnode_t *,page_t *,u_offset_t *,size_t *,int,cred_t *),int (* compar)(const void *,const void *),int * newnode,cred_t * cr,char * dnm,char * nm)2458 make_rnode(nfs_fhandle *fh, rhashq_t *rhtp, struct vfs *vfsp,
2459     struct vnodeops *vops,
2460     int (*putapage)(vnode_t *, page_t *, u_offset_t *, size_t *, int, cred_t *),
2461     int (*compar)(const void *, const void *),
2462     int *newnode, cred_t *cr, char *dnm, char *nm)
2463 {
2464 	rnode_t *rp;
2465 	rnode_t *trp;
2466 	vnode_t *vp;
2467 	mntinfo_t *mi;
2468 
2469 	ASSERT(RW_READ_HELD(&rhtp->r_lock));
2470 
2471 	mi = VFTOMI(vfsp);
2472 start:
2473 	if ((rp = rfind(rhtp, fh, vfsp)) != NULL) {
2474 		vp = RTOV(rp);
2475 		nfs_set_vroot(vp);
2476 		*newnode = 0;
2477 		return (vp);
2478 	}
2479 	rw_exit(&rhtp->r_lock);
2480 
2481 	mutex_enter(&rpfreelist_lock);
2482 	if (rpfreelist != NULL && rnew >= nrnode) {
2483 		rp = rpfreelist;
2484 		rp_rmfree(rp);
2485 		mutex_exit(&rpfreelist_lock);
2486 
2487 		vp = RTOV(rp);
2488 
2489 		if (rp->r_flags & RHASHED) {
2490 			rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
2491 			mutex_enter(&vp->v_lock);
2492 			if (vp->v_count > 1) {
2493 				VN_RELE_LOCKED(vp);
2494 				mutex_exit(&vp->v_lock);
2495 				rw_exit(&rp->r_hashq->r_lock);
2496 				rw_enter(&rhtp->r_lock, RW_READER);
2497 				goto start;
2498 			}
2499 			mutex_exit(&vp->v_lock);
2500 			rp_rmhash_locked(rp);
2501 			rw_exit(&rp->r_hashq->r_lock);
2502 		}
2503 
2504 		rinactive(rp, cr);
2505 
2506 		mutex_enter(&vp->v_lock);
2507 		if (vp->v_count > 1) {
2508 			VN_RELE_LOCKED(vp);
2509 			mutex_exit(&vp->v_lock);
2510 			rw_enter(&rhtp->r_lock, RW_READER);
2511 			goto start;
2512 		}
2513 		mutex_exit(&vp->v_lock);
2514 		vn_invalid(vp);
2515 		/*
2516 		 * destroy old locks before bzero'ing and
2517 		 * recreating the locks below.
2518 		 */
2519 		nfs_rw_destroy(&rp->r_rwlock);
2520 		nfs_rw_destroy(&rp->r_lkserlock);
2521 		mutex_destroy(&rp->r_statelock);
2522 		cv_destroy(&rp->r_cv);
2523 		cv_destroy(&rp->r_commit.c_cv);
2524 		nfs_free_r_path(rp);
2525 		avl_destroy(&rp->r_dir);
2526 		/*
2527 		 * Make sure that if rnode is recycled then
2528 		 * VFS count is decremented properly before
2529 		 * reuse.
2530 		 */
2531 		VFS_RELE(vp->v_vfsp);
2532 		vn_reinit(vp);
2533 	} else {
2534 		vnode_t *new_vp;
2535 
2536 		mutex_exit(&rpfreelist_lock);
2537 
2538 		rp = kmem_cache_alloc(rnode_cache, KM_SLEEP);
2539 		new_vp = vn_alloc(KM_SLEEP);
2540 
2541 		atomic_inc_ulong((ulong_t *)&rnew);
2542 #ifdef DEBUG
2543 		clstat_debug.nrnode.value.ui64++;
2544 #endif
2545 		vp = new_vp;
2546 	}
2547 
2548 	bzero(rp, sizeof (*rp));
2549 	rp->r_vnode = vp;
2550 	nfs_rw_init(&rp->r_rwlock, NULL, RW_DEFAULT, NULL);
2551 	nfs_rw_init(&rp->r_lkserlock, NULL, RW_DEFAULT, NULL);
2552 	mutex_init(&rp->r_statelock, NULL, MUTEX_DEFAULT, NULL);
2553 	cv_init(&rp->r_cv, NULL, CV_DEFAULT, NULL);
2554 	cv_init(&rp->r_commit.c_cv, NULL, CV_DEFAULT, NULL);
2555 	rp->r_fh.fh_len = fh->fh_len;
2556 	bcopy(fh->fh_buf, rp->r_fh.fh_buf, fh->fh_len);
2557 	rp->r_server = mi->mi_curr_serv;
2558 	if (FAILOVER_MOUNT(mi)) {
2559 		/*
2560 		 * If replicated servers, stash pathnames
2561 		 */
2562 		if (dnm != NULL && nm != NULL) {
2563 			char *s, *p;
2564 			uint_t len;
2565 
2566 			len = (uint_t)(strlen(dnm) + strlen(nm) + 2);
2567 			rp->r_path = kmem_alloc(len, KM_SLEEP);
2568 #ifdef DEBUG
2569 			clstat_debug.rpath.value.ui64 += len;
2570 #endif
2571 			s = rp->r_path;
2572 			for (p = dnm; *p; p++)
2573 				*s++ = *p;
2574 			*s++ = '/';
2575 			for (p = nm; *p; p++)
2576 				*s++ = *p;
2577 			*s = '\0';
2578 		} else {
2579 			/* special case for root */
2580 			rp->r_path = kmem_alloc(2, KM_SLEEP);
2581 #ifdef DEBUG
2582 			clstat_debug.rpath.value.ui64 += 2;
2583 #endif
2584 			*rp->r_path = '.';
2585 			*(rp->r_path + 1) = '\0';
2586 		}
2587 	}
2588 	VFS_HOLD(vfsp);
2589 	rp->r_putapage = putapage;
2590 	rp->r_hashq = rhtp;
2591 	rp->r_flags = RREADDIRPLUS;
2592 	avl_create(&rp->r_dir, compar, sizeof (rddir_cache),
2593 	    offsetof(rddir_cache, tree));
2594 	vn_setops(vp, vops);
2595 	vp->v_data = (caddr_t)rp;
2596 	vp->v_vfsp = vfsp;
2597 	vp->v_type = VNON;
2598 	vp->v_flag |= VMODSORT;
2599 	nfs_set_vroot(vp);
2600 
2601 	/*
2602 	 * There is a race condition if someone else
2603 	 * alloc's the rnode while no locks are held, so we
2604 	 * check again and recover if found.
2605 	 */
2606 	rw_enter(&rhtp->r_lock, RW_WRITER);
2607 	if ((trp = rfind(rhtp, fh, vfsp)) != NULL) {
2608 		vp = RTOV(trp);
2609 		nfs_set_vroot(vp);
2610 		*newnode = 0;
2611 		rw_exit(&rhtp->r_lock);
2612 		rp_addfree(rp, cr);
2613 		rw_enter(&rhtp->r_lock, RW_READER);
2614 		return (vp);
2615 	}
2616 	rp_addhash(rp);
2617 	*newnode = 1;
2618 	return (vp);
2619 }
2620 
2621 /*
2622  * Callback function to check if the page should be marked as
2623  * modified. In the positive case, p_fsdata is set to C_NOCOMMIT.
2624  */
2625 int
nfs_setmod_check(page_t * pp)2626 nfs_setmod_check(page_t *pp)
2627 {
2628 	if (pp->p_fsdata != C_NOCOMMIT) {
2629 		pp->p_fsdata = C_NOCOMMIT;
2630 		return (1);
2631 	}
2632 	return (0);
2633 }
2634 
2635 static void
nfs_set_vroot(vnode_t * vp)2636 nfs_set_vroot(vnode_t *vp)
2637 {
2638 	rnode_t *rp;
2639 	nfs_fhandle *rootfh;
2640 
2641 	rp = VTOR(vp);
2642 	rootfh = &rp->r_server->sv_fhandle;
2643 	if (rootfh->fh_len == rp->r_fh.fh_len &&
2644 	    bcmp(rootfh->fh_buf, rp->r_fh.fh_buf, rp->r_fh.fh_len) == 0) {
2645 		if (!(vp->v_flag & VROOT)) {
2646 			mutex_enter(&vp->v_lock);
2647 			vp->v_flag |= VROOT;
2648 			mutex_exit(&vp->v_lock);
2649 		}
2650 	}
2651 }
2652 
2653 static void
nfs_free_r_path(rnode_t * rp)2654 nfs_free_r_path(rnode_t *rp)
2655 {
2656 	char *path;
2657 	size_t len;
2658 
2659 	path = rp->r_path;
2660 	if (path) {
2661 		rp->r_path = NULL;
2662 		len = strlen(path) + 1;
2663 		kmem_free(path, len);
2664 #ifdef DEBUG
2665 		clstat_debug.rpath.value.ui64 -= len;
2666 #endif
2667 	}
2668 }
2669 
2670 /*
2671  * Put an rnode on the free list.
2672  *
2673  * Rnodes which were allocated above and beyond the normal limit
2674  * are immediately freed.
2675  */
2676 void
rp_addfree(rnode_t * rp,cred_t * cr)2677 rp_addfree(rnode_t *rp, cred_t *cr)
2678 {
2679 	vnode_t *vp;
2680 	struct vfs *vfsp;
2681 
2682 	vp = RTOV(rp);
2683 	ASSERT(vp->v_count >= 1);
2684 	ASSERT(rp->r_freef == NULL && rp->r_freeb == NULL);
2685 
2686 	/*
2687 	 * If we have too many rnodes allocated and there are no
2688 	 * references to this rnode, or if the rnode is no longer
2689 	 * accessible by it does not reside in the hash queues,
2690 	 * or if an i/o error occurred while writing to the file,
2691 	 * then just free it instead of putting it on the rnode
2692 	 * freelist.
2693 	 */
2694 	vfsp = vp->v_vfsp;
2695 	if (((rnew > nrnode || !(rp->r_flags & RHASHED) || rp->r_error ||
2696 	    (vfsp->vfs_flag & VFS_UNMOUNTED)) && rp->r_count == 0)) {
2697 		if (rp->r_flags & RHASHED) {
2698 			rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
2699 			mutex_enter(&vp->v_lock);
2700 			if (vp->v_count > 1) {
2701 				VN_RELE_LOCKED(vp);
2702 				mutex_exit(&vp->v_lock);
2703 				rw_exit(&rp->r_hashq->r_lock);
2704 				return;
2705 			}
2706 			mutex_exit(&vp->v_lock);
2707 			rp_rmhash_locked(rp);
2708 			rw_exit(&rp->r_hashq->r_lock);
2709 		}
2710 
2711 		rinactive(rp, cr);
2712 
2713 		/*
2714 		 * Recheck the vnode reference count.  We need to
2715 		 * make sure that another reference has not been
2716 		 * acquired while we were not holding v_lock.  The
2717 		 * rnode is not in the rnode hash queues, so the
2718 		 * only way for a reference to have been acquired
2719 		 * is for a VOP_PUTPAGE because the rnode was marked
2720 		 * with RDIRTY or for a modified page.  This
2721 		 * reference may have been acquired before our call
2722 		 * to rinactive.  The i/o may have been completed,
2723 		 * thus allowing rinactive to complete, but the
2724 		 * reference to the vnode may not have been released
2725 		 * yet.  In any case, the rnode can not be destroyed
2726 		 * until the other references to this vnode have been
2727 		 * released.  The other references will take care of
2728 		 * either destroying the rnode or placing it on the
2729 		 * rnode freelist.  If there are no other references,
2730 		 * then the rnode may be safely destroyed.
2731 		 */
2732 		mutex_enter(&vp->v_lock);
2733 		if (vp->v_count > 1) {
2734 			VN_RELE_LOCKED(vp);
2735 			mutex_exit(&vp->v_lock);
2736 			return;
2737 		}
2738 		mutex_exit(&vp->v_lock);
2739 
2740 		destroy_rnode(rp);
2741 		return;
2742 	}
2743 
2744 	/*
2745 	 * Lock the hash queue and then recheck the reference count
2746 	 * to ensure that no other threads have acquired a reference
2747 	 * to indicate that the rnode should not be placed on the
2748 	 * freelist.  If another reference has been acquired, then
2749 	 * just release this one and let the other thread complete
2750 	 * the processing of adding this rnode to the freelist.
2751 	 */
2752 	rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
2753 
2754 	mutex_enter(&vp->v_lock);
2755 	if (vp->v_count > 1) {
2756 		VN_RELE_LOCKED(vp);
2757 		mutex_exit(&vp->v_lock);
2758 		rw_exit(&rp->r_hashq->r_lock);
2759 		return;
2760 	}
2761 	mutex_exit(&vp->v_lock);
2762 
2763 	/*
2764 	 * If there is no cached data or metadata for this file, then
2765 	 * put the rnode on the front of the freelist so that it will
2766 	 * be reused before other rnodes which may have cached data or
2767 	 * metadata associated with them.
2768 	 */
2769 	mutex_enter(&rpfreelist_lock);
2770 	if (rpfreelist == NULL) {
2771 		rp->r_freef = rp;
2772 		rp->r_freeb = rp;
2773 		rpfreelist = rp;
2774 	} else {
2775 		rp->r_freef = rpfreelist;
2776 		rp->r_freeb = rpfreelist->r_freeb;
2777 		rpfreelist->r_freeb->r_freef = rp;
2778 		rpfreelist->r_freeb = rp;
2779 		if (!vn_has_cached_data(vp) &&
2780 		    !HAVE_RDDIR_CACHE(rp) &&
2781 		    rp->r_symlink.contents == NULL &&
2782 		    rp->r_secattr == NULL &&
2783 		    rp->r_pathconf == NULL)
2784 			rpfreelist = rp;
2785 	}
2786 	mutex_exit(&rpfreelist_lock);
2787 
2788 	rw_exit(&rp->r_hashq->r_lock);
2789 }
2790 
2791 /*
2792  * Remove an rnode from the free list.
2793  *
2794  * The caller must be holding rpfreelist_lock and the rnode
2795  * must be on the freelist.
2796  */
2797 static void
rp_rmfree(rnode_t * rp)2798 rp_rmfree(rnode_t *rp)
2799 {
2800 
2801 	ASSERT(MUTEX_HELD(&rpfreelist_lock));
2802 	ASSERT(rp->r_freef != NULL && rp->r_freeb != NULL);
2803 
2804 	if (rp == rpfreelist) {
2805 		rpfreelist = rp->r_freef;
2806 		if (rp == rpfreelist)
2807 			rpfreelist = NULL;
2808 	}
2809 
2810 	rp->r_freeb->r_freef = rp->r_freef;
2811 	rp->r_freef->r_freeb = rp->r_freeb;
2812 
2813 	rp->r_freef = rp->r_freeb = NULL;
2814 }
2815 
2816 /*
2817  * Put a rnode in the hash table.
2818  *
2819  * The caller must be holding the exclusive hash queue lock.
2820  */
2821 static void
rp_addhash(rnode_t * rp)2822 rp_addhash(rnode_t *rp)
2823 {
2824 	mntinfo_t *mi;
2825 
2826 	ASSERT(RW_WRITE_HELD(&rp->r_hashq->r_lock));
2827 	ASSERT(!(rp->r_flags & RHASHED));
2828 
2829 	rp->r_hashf = rp->r_hashq->r_hashf;
2830 	rp->r_hashq->r_hashf = rp;
2831 	rp->r_hashb = (rnode_t *)rp->r_hashq;
2832 	rp->r_hashf->r_hashb = rp;
2833 
2834 	mutex_enter(&rp->r_statelock);
2835 	rp->r_flags |= RHASHED;
2836 	mutex_exit(&rp->r_statelock);
2837 
2838 	mi = VTOMI(RTOV(rp));
2839 	mutex_enter(&mi->mi_rnodes_lock);
2840 	list_insert_tail(&mi->mi_rnodes, rp);
2841 	mutex_exit(&mi->mi_rnodes_lock);
2842 }
2843 
2844 /*
2845  * Remove a rnode from the hash table.
2846  *
2847  * The caller must be holding the hash queue lock.
2848  */
2849 static void
rp_rmhash_locked(rnode_t * rp)2850 rp_rmhash_locked(rnode_t *rp)
2851 {
2852 	mntinfo_t *mi;
2853 
2854 	ASSERT(RW_WRITE_HELD(&rp->r_hashq->r_lock));
2855 	ASSERT(rp->r_flags & RHASHED);
2856 
2857 	rp->r_hashb->r_hashf = rp->r_hashf;
2858 	rp->r_hashf->r_hashb = rp->r_hashb;
2859 
2860 	mutex_enter(&rp->r_statelock);
2861 	rp->r_flags &= ~RHASHED;
2862 	mutex_exit(&rp->r_statelock);
2863 
2864 	mi = VTOMI(RTOV(rp));
2865 	mutex_enter(&mi->mi_rnodes_lock);
2866 	if (list_link_active(&rp->r_mi_link))
2867 		list_remove(&mi->mi_rnodes, rp);
2868 	mutex_exit(&mi->mi_rnodes_lock);
2869 }
2870 
2871 /*
2872  * Remove a rnode from the hash table.
2873  *
2874  * The caller must not be holding the hash queue lock.
2875  */
2876 void
rp_rmhash(rnode_t * rp)2877 rp_rmhash(rnode_t *rp)
2878 {
2879 
2880 	rw_enter(&rp->r_hashq->r_lock, RW_WRITER);
2881 	rp_rmhash_locked(rp);
2882 	rw_exit(&rp->r_hashq->r_lock);
2883 }
2884 
2885 /*
2886  * Lookup a rnode by fhandle.
2887  *
2888  * The caller must be holding the hash queue lock, either shared or exclusive.
2889  */
2890 static rnode_t *
rfind(rhashq_t * rhtp,nfs_fhandle * fh,struct vfs * vfsp)2891 rfind(rhashq_t *rhtp, nfs_fhandle *fh, struct vfs *vfsp)
2892 {
2893 	rnode_t *rp;
2894 	vnode_t *vp;
2895 
2896 	ASSERT(RW_LOCK_HELD(&rhtp->r_lock));
2897 
2898 	for (rp = rhtp->r_hashf; rp != (rnode_t *)rhtp; rp = rp->r_hashf) {
2899 		vp = RTOV(rp);
2900 		if (vp->v_vfsp == vfsp &&
2901 		    rp->r_fh.fh_len == fh->fh_len &&
2902 		    bcmp(rp->r_fh.fh_buf, fh->fh_buf, fh->fh_len) == 0) {
2903 			/*
2904 			 * remove rnode from free list, if necessary.
2905 			 */
2906 			if (rp->r_freef != NULL) {
2907 				mutex_enter(&rpfreelist_lock);
2908 				/*
2909 				 * If the rnode is on the freelist,
2910 				 * then remove it and use that reference
2911 				 * as the new reference.  Otherwise,
2912 				 * need to increment the reference count.
2913 				 */
2914 				if (rp->r_freef != NULL) {
2915 					rp_rmfree(rp);
2916 					mutex_exit(&rpfreelist_lock);
2917 				} else {
2918 					mutex_exit(&rpfreelist_lock);
2919 					VN_HOLD(vp);
2920 				}
2921 			} else
2922 				VN_HOLD(vp);
2923 			return (rp);
2924 		}
2925 	}
2926 	return (NULL);
2927 }
2928 
2929 /*
2930  * Return 1 if there is an active vnode belonging to this vfs in the
2931  * rtable cache.
2932  *
2933  * Several of these checks are done without holding the usual
2934  * locks.  This is safe because destroy_rtable(), rp_addfree(),
2935  * etc. will redo the necessary checks before actually destroying
2936  * any rnodes.
2937  */
2938 int
check_rtable(struct vfs * vfsp)2939 check_rtable(struct vfs *vfsp)
2940 {
2941 	rnode_t *rp;
2942 	vnode_t *vp;
2943 	mntinfo_t *mi;
2944 
2945 	ASSERT(vfsp != NULL);
2946 	mi = VFTOMI(vfsp);
2947 
2948 	mutex_enter(&mi->mi_rnodes_lock);
2949 	for (rp = list_head(&mi->mi_rnodes); rp != NULL;
2950 	    rp = list_next(&mi->mi_rnodes, rp)) {
2951 		vp = RTOV(rp);
2952 
2953 		if (rp->r_freef == NULL ||
2954 		    (vn_has_cached_data(vp) && (rp->r_flags & RDIRTY)) ||
2955 		    rp->r_count > 0) {
2956 			mutex_exit(&mi->mi_rnodes_lock);
2957 			return (1);
2958 		}
2959 	}
2960 	mutex_exit(&mi->mi_rnodes_lock);
2961 
2962 	return (0);
2963 }
2964 
2965 /*
2966  * Destroy inactive vnodes from the hash queues which belong to this
2967  * vfs.  It is essential that we destroy all inactive vnodes during a
2968  * forced unmount as well as during a normal unmount.
2969  */
2970 void
destroy_rtable(struct vfs * vfsp,cred_t * cr)2971 destroy_rtable(struct vfs *vfsp, cred_t *cr)
2972 {
2973 	rnode_t *rp;
2974 	mntinfo_t *mi;
2975 
2976 	ASSERT(vfsp != NULL);
2977 
2978 	mi = VFTOMI(vfsp);
2979 
2980 	mutex_enter(&rpfreelist_lock);
2981 	mutex_enter(&mi->mi_rnodes_lock);
2982 	while ((rp = list_remove_head(&mi->mi_rnodes)) != NULL) {
2983 		/*
2984 		 * If the rnode is no longer on the freelist it is not
2985 		 * ours and it will be handled by some other thread, so
2986 		 * skip it.
2987 		 */
2988 		if (rp->r_freef == NULL)
2989 			continue;
2990 		mutex_exit(&mi->mi_rnodes_lock);
2991 
2992 		rp_rmfree(rp);
2993 		mutex_exit(&rpfreelist_lock);
2994 
2995 		rp_rmhash(rp);
2996 
2997 		/*
2998 		 * This call to rp_addfree will end up destroying the
2999 		 * rnode, but in a safe way with the appropriate set
3000 		 * of checks done.
3001 		 */
3002 		rp_addfree(rp, cr);
3003 
3004 		mutex_enter(&rpfreelist_lock);
3005 		mutex_enter(&mi->mi_rnodes_lock);
3006 	}
3007 	mutex_exit(&mi->mi_rnodes_lock);
3008 	mutex_exit(&rpfreelist_lock);
3009 }
3010 
3011 /*
3012  * This routine destroys all the resources associated with the rnode
3013  * and then the rnode itself.
3014  */
3015 static void
destroy_rnode(rnode_t * rp)3016 destroy_rnode(rnode_t *rp)
3017 {
3018 	vnode_t *vp;
3019 	vfs_t *vfsp;
3020 
3021 	vp = RTOV(rp);
3022 	vfsp = vp->v_vfsp;
3023 
3024 	ASSERT(vp->v_count == 1);
3025 	ASSERT(rp->r_count == 0);
3026 	ASSERT(rp->r_lmpl == NULL);
3027 	ASSERT(rp->r_mapcnt == 0);
3028 	ASSERT(!(rp->r_flags & RHASHED));
3029 	ASSERT(rp->r_freef == NULL && rp->r_freeb == NULL);
3030 	atomic_dec_ulong((ulong_t *)&rnew);
3031 #ifdef DEBUG
3032 	clstat_debug.nrnode.value.ui64--;
3033 #endif
3034 	nfs_rw_destroy(&rp->r_rwlock);
3035 	nfs_rw_destroy(&rp->r_lkserlock);
3036 	mutex_destroy(&rp->r_statelock);
3037 	cv_destroy(&rp->r_cv);
3038 	cv_destroy(&rp->r_commit.c_cv);
3039 	if (rp->r_flags & RDELMAPLIST)
3040 		list_destroy(&rp->r_indelmap);
3041 	nfs_free_r_path(rp);
3042 	avl_destroy(&rp->r_dir);
3043 	vn_invalid(vp);
3044 	vn_free(vp);
3045 	kmem_cache_free(rnode_cache, rp);
3046 	VFS_RELE(vfsp);
3047 }
3048 
3049 /*
3050  * Flush all vnodes in this (or every) vfs.
3051  * Used by nfs_sync and by nfs_unmount.
3052  */
3053 void
rflush(struct vfs * vfsp,cred_t * cr)3054 rflush(struct vfs *vfsp, cred_t *cr)
3055 {
3056 	int index;
3057 	rnode_t *rp;
3058 	vnode_t *vp, **vplist;
3059 	long num, cnt;
3060 
3061 	/*
3062 	 * Check to see whether there is anything to do.
3063 	 */
3064 	num = rnew;
3065 	if (num == 0)
3066 		return;
3067 
3068 	/*
3069 	 * Allocate a slot for all currently active rnodes on the
3070 	 * supposition that they all may need flushing.
3071 	 */
3072 	vplist = kmem_alloc(num * sizeof (*vplist), KM_SLEEP);
3073 	cnt = 0;
3074 
3075 	/*
3076 	 * If the vfs is known we can do fast path by iterating all rnodes that
3077 	 * belongs to this vfs.  This is much faster than the traditional way
3078 	 * of iterating rtable (below) in a case there is a lot of rnodes that
3079 	 * does not belong to our vfs.
3080 	 */
3081 	if (vfsp != NULL) {
3082 		mntinfo_t *mi = VFTOMI(vfsp);
3083 
3084 		mutex_enter(&mi->mi_rnodes_lock);
3085 		for (rp = list_head(&mi->mi_rnodes); rp != NULL;
3086 		    rp = list_next(&mi->mi_rnodes, rp)) {
3087 			vp = RTOV(rp);
3088 			/*
3089 			 * Don't bother sync'ing a vp if it
3090 			 * is part of virtual swap device or
3091 			 * if VFS is read-only
3092 			 */
3093 			if (IS_SWAPVP(vp) || vn_is_readonly(vp))
3094 				continue;
3095 			/*
3096 			 * If the vnode has pages and is marked as either dirty
3097 			 * or mmap'd, hold and add this vnode to the list of
3098 			 * vnodes to flush.
3099 			 */
3100 			ASSERT(vp->v_vfsp == vfsp);
3101 			if (vn_has_cached_data(vp) &&
3102 			    ((rp->r_flags & RDIRTY) || rp->r_mapcnt > 0)) {
3103 				VN_HOLD(vp);
3104 				vplist[cnt++] = vp;
3105 				if (cnt == num) {
3106 					/*
3107 					 * The vplist is full because there is
3108 					 * too many rnodes.  We are done for
3109 					 * now.
3110 					 */
3111 					break;
3112 				}
3113 			}
3114 		}
3115 		mutex_exit(&mi->mi_rnodes_lock);
3116 
3117 		goto done;
3118 	}
3119 
3120 	ASSERT(vfsp == NULL);
3121 
3122 	/*
3123 	 * Walk the hash queues looking for rnodes with page
3124 	 * lists associated with them.  Make a list of these
3125 	 * files.
3126 	 */
3127 	for (index = 0; index < rtablesize; index++) {
3128 		rw_enter(&rtable[index].r_lock, RW_READER);
3129 		for (rp = rtable[index].r_hashf;
3130 		    rp != (rnode_t *)(&rtable[index]);
3131 		    rp = rp->r_hashf) {
3132 			vp = RTOV(rp);
3133 			/*
3134 			 * Don't bother sync'ing a vp if it
3135 			 * is part of virtual swap device or
3136 			 * if VFS is read-only
3137 			 */
3138 			if (IS_SWAPVP(vp) || vn_is_readonly(vp))
3139 				continue;
3140 			/*
3141 			 * If the vnode has pages and is marked as either dirty
3142 			 * or mmap'd, hold and add this vnode to the list of
3143 			 * vnodes to flush.
3144 			 */
3145 			if (vn_has_cached_data(vp) &&
3146 			    ((rp->r_flags & RDIRTY) || rp->r_mapcnt > 0)) {
3147 				VN_HOLD(vp);
3148 				vplist[cnt++] = vp;
3149 				if (cnt == num) {
3150 					rw_exit(&rtable[index].r_lock);
3151 					/*
3152 					 * The vplist is full because there is
3153 					 * too many rnodes.  We are done for
3154 					 * now.
3155 					 */
3156 					goto done;
3157 				}
3158 			}
3159 		}
3160 		rw_exit(&rtable[index].r_lock);
3161 	}
3162 
3163 done:
3164 
3165 	/*
3166 	 * Flush and release all of the files on the list.
3167 	 */
3168 	while (cnt-- > 0) {
3169 		vp = vplist[cnt];
3170 		(void) VOP_PUTPAGE(vp, (u_offset_t)0, 0, B_ASYNC, cr, NULL);
3171 		VN_RELE(vp);
3172 	}
3173 
3174 	/*
3175 	 * Free the space allocated to hold the list.
3176 	 */
3177 	kmem_free(vplist, num * sizeof (*vplist));
3178 }
3179 
3180 /*
3181  * This probably needs to be larger than or equal to
3182  * log2(sizeof (struct rnode)) due to the way that rnodes are
3183  * allocated.
3184  */
3185 #define	ACACHE_SHIFT_BITS	9
3186 
3187 static int
acachehash(rnode_t * rp,cred_t * cr)3188 acachehash(rnode_t *rp, cred_t *cr)
3189 {
3190 
3191 	return ((((intptr_t)rp >> ACACHE_SHIFT_BITS) + crgetuid(cr)) &
3192 	    acachemask);
3193 }
3194 
3195 #ifdef DEBUG
3196 static long nfs_access_cache_hits = 0;
3197 static long nfs_access_cache_misses = 0;
3198 #endif
3199 
3200 nfs_access_type_t
nfs_access_check(rnode_t * rp,uint32_t acc,cred_t * cr)3201 nfs_access_check(rnode_t *rp, uint32_t acc, cred_t *cr)
3202 {
3203 	vnode_t *vp;
3204 	acache_t *ap;
3205 	acache_hash_t *hp;
3206 	nfs_access_type_t all;
3207 
3208 	vp = RTOV(rp);
3209 	if (!ATTRCACHE_VALID(vp) || nfs_waitfor_purge_complete(vp))
3210 		return (NFS_ACCESS_UNKNOWN);
3211 
3212 	if (rp->r_acache != NULL) {
3213 		hp = &acache[acachehash(rp, cr)];
3214 		rw_enter(&hp->lock, RW_READER);
3215 		ap = hp->next;
3216 		while (ap != (acache_t *)hp) {
3217 			if (crcmp(ap->cred, cr) == 0 && ap->rnode == rp) {
3218 				if ((ap->known & acc) == acc) {
3219 #ifdef DEBUG
3220 					nfs_access_cache_hits++;
3221 #endif
3222 					if ((ap->allowed & acc) == acc)
3223 						all = NFS_ACCESS_ALLOWED;
3224 					else
3225 						all = NFS_ACCESS_DENIED;
3226 				} else {
3227 #ifdef DEBUG
3228 					nfs_access_cache_misses++;
3229 #endif
3230 					all = NFS_ACCESS_UNKNOWN;
3231 				}
3232 				rw_exit(&hp->lock);
3233 				return (all);
3234 			}
3235 			ap = ap->next;
3236 		}
3237 		rw_exit(&hp->lock);
3238 	}
3239 
3240 #ifdef DEBUG
3241 	nfs_access_cache_misses++;
3242 #endif
3243 	return (NFS_ACCESS_UNKNOWN);
3244 }
3245 
3246 void
nfs_access_cache(rnode_t * rp,uint32_t acc,uint32_t resacc,cred_t * cr)3247 nfs_access_cache(rnode_t *rp, uint32_t acc, uint32_t resacc, cred_t *cr)
3248 {
3249 	acache_t *ap;
3250 	acache_t *nap;
3251 	acache_hash_t *hp;
3252 
3253 	hp = &acache[acachehash(rp, cr)];
3254 
3255 	/*
3256 	 * Allocate now assuming that mostly an allocation will be
3257 	 * required.  This allows the allocation to happen without
3258 	 * holding the hash bucket locked.
3259 	 */
3260 	nap = kmem_cache_alloc(acache_cache, KM_NOSLEEP);
3261 	if (nap != NULL) {
3262 		nap->known = acc;
3263 		nap->allowed = resacc;
3264 		nap->rnode = rp;
3265 		crhold(cr);
3266 		nap->cred = cr;
3267 		nap->hashq = hp;
3268 	}
3269 
3270 	rw_enter(&hp->lock, RW_WRITER);
3271 
3272 	if (rp->r_acache != NULL) {
3273 		ap = hp->next;
3274 		while (ap != (acache_t *)hp) {
3275 			if (crcmp(ap->cred, cr) == 0 && ap->rnode == rp) {
3276 				ap->known |= acc;
3277 				ap->allowed &= ~acc;
3278 				ap->allowed |= resacc;
3279 				rw_exit(&hp->lock);
3280 				if (nap != NULL) {
3281 					crfree(nap->cred);
3282 					kmem_cache_free(acache_cache, nap);
3283 				}
3284 				return;
3285 			}
3286 			ap = ap->next;
3287 		}
3288 	}
3289 
3290 	if (nap != NULL) {
3291 #ifdef DEBUG
3292 		clstat_debug.access.value.ui64++;
3293 #endif
3294 		nap->next = hp->next;
3295 		hp->next = nap;
3296 		nap->next->prev = nap;
3297 		nap->prev = (acache_t *)hp;
3298 
3299 		mutex_enter(&rp->r_statelock);
3300 		nap->list = rp->r_acache;
3301 		rp->r_acache = nap;
3302 		mutex_exit(&