nfs_clsubs.c revision 303909
1191783Srmacklem/*- 2191783Srmacklem * Copyright (c) 1989, 1993 3191783Srmacklem * The Regents of the University of California. All rights reserved. 4191783Srmacklem * 5191783Srmacklem * This code is derived from software contributed to Berkeley by 6191783Srmacklem * Rick Macklem at The University of Guelph. 7191783Srmacklem * 8191783Srmacklem * Redistribution and use in source and binary forms, with or without 9191783Srmacklem * modification, are permitted provided that the following conditions 10191783Srmacklem * are met: 11191783Srmacklem * 1. Redistributions of source code must retain the above copyright 12191783Srmacklem * notice, this list of conditions and the following disclaimer. 13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright 14191783Srmacklem * notice, this list of conditions and the following disclaimer in the 15191783Srmacklem * documentation and/or other materials provided with the distribution. 16191783Srmacklem * 4. Neither the name of the University nor the names of its contributors 17191783Srmacklem * may be used to endorse or promote products derived from this software 18191783Srmacklem * without specific prior written permission. 19191783Srmacklem * 20191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23191783Srmacklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30191783Srmacklem * SUCH DAMAGE. 31191783Srmacklem * 32191783Srmacklem * from nfs_subs.c 8.8 (Berkeley) 5/22/95 33191783Srmacklem */ 34191783Srmacklem 35191783Srmacklem#include <sys/cdefs.h> 36191783Srmacklem__FBSDID("$FreeBSD: stable/11/sys/fs/nfsclient/nfs_clsubs.c 303909 2016-08-10 12:53:30Z kib $"); 37191783Srmacklem 38191783Srmacklem/* 39191783Srmacklem * These functions support the macros and help fiddle mbuf chains for 40191783Srmacklem * the nfs op functions. They do things like create the rpc header and 41191783Srmacklem * copy data between mbuf chains and uio lists. 42191783Srmacklem */ 43191783Srmacklem 44191783Srmacklem#include <sys/param.h> 45191783Srmacklem#include <sys/systm.h> 46191783Srmacklem#include <sys/kernel.h> 47191783Srmacklem#include <sys/bio.h> 48191783Srmacklem#include <sys/buf.h> 49191783Srmacklem#include <sys/proc.h> 50191783Srmacklem#include <sys/mount.h> 51191783Srmacklem#include <sys/vnode.h> 52191783Srmacklem#include <sys/namei.h> 53191783Srmacklem#include <sys/mbuf.h> 54191783Srmacklem#include <sys/socket.h> 55191783Srmacklem#include <sys/stat.h> 56191783Srmacklem#include <sys/malloc.h> 57191783Srmacklem#include <sys/sysent.h> 58191783Srmacklem#include <sys/syscall.h> 59191783Srmacklem#include <sys/sysproto.h> 60220683Srmacklem#include <sys/taskqueue.h> 61191783Srmacklem 62191783Srmacklem#include <vm/vm.h> 63191783Srmacklem#include <vm/vm_object.h> 64191783Srmacklem#include <vm/vm_extern.h> 65191783Srmacklem#include <vm/uma.h> 66191783Srmacklem 67191783Srmacklem#include <fs/nfs/nfsport.h> 68191783Srmacklem#include <fs/nfsclient/nfsnode.h> 69191783Srmacklem#include <fs/nfsclient/nfsmount.h> 70191783Srmacklem#include <fs/nfsclient/nfs.h> 71223280Srmacklem#include <fs/nfsclient/nfs_kdtrace.h> 72191783Srmacklem 73191783Srmacklem#include <netinet/in.h> 74191783Srmacklem 75191783Srmacklem/* 76191783Srmacklem * Note that stdarg.h and the ANSI style va_start macro is used for both 77191783Srmacklem * ANSI and traditional C compilers. 78191783Srmacklem */ 79191783Srmacklem#include <machine/stdarg.h> 80191783Srmacklem 81191783Srmacklemextern struct mtx ncl_iod_mutex; 82220683Srmacklemextern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON]; 83220683Srmacklemextern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON]; 84191783Srmacklemextern int ncl_numasync; 85191783Srmacklemextern unsigned int ncl_iodmax; 86191783Srmacklemextern struct nfsstats newnfsstats; 87191783Srmacklem 88220683Srmacklemstruct task ncl_nfsiodnew_task; 89220683Srmacklem 90191783Srmacklemint 91191783Srmacklemncl_uninit(struct vfsconf *vfsp) 92191783Srmacklem{ 93198291Sjh /* 94198291Sjh * XXX: Unloading of nfscl module is unsupported. 95198291Sjh */ 96198291Sjh#if 0 97191783Srmacklem int i; 98191783Srmacklem 99191783Srmacklem /* 100191783Srmacklem * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup 101191783Srmacklem * any sleeping nfsiods so they check ncl_iodmax and exit. 102191783Srmacklem */ 103191783Srmacklem mtx_lock(&ncl_iod_mutex); 104191783Srmacklem ncl_iodmax = 0; 105191783Srmacklem for (i = 0; i < ncl_numasync; i++) 106203119Srmacklem if (ncl_iodwant[i] == NFSIOD_AVAILABLE) 107191783Srmacklem wakeup(&ncl_iodwant[i]); 108191783Srmacklem /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */ 109191783Srmacklem while (ncl_numasync) 110191783Srmacklem msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0); 111191783Srmacklem mtx_unlock(&ncl_iod_mutex); 112191783Srmacklem ncl_nhuninit(); 113191783Srmacklem return (0); 114198291Sjh#else 115198291Sjh return (EOPNOTSUPP); 116198291Sjh#endif 117191783Srmacklem} 118191783Srmacklem 119191783Srmacklemvoid 120191783Srmacklemncl_dircookie_lock(struct nfsnode *np) 121191783Srmacklem{ 122191783Srmacklem mtx_lock(&np->n_mtx); 123191783Srmacklem while (np->n_flag & NDIRCOOKIELK) 124191783Srmacklem (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0); 125191783Srmacklem np->n_flag |= NDIRCOOKIELK; 126191783Srmacklem mtx_unlock(&np->n_mtx); 127191783Srmacklem} 128191783Srmacklem 129191783Srmacklemvoid 130191783Srmacklemncl_dircookie_unlock(struct nfsnode *np) 131191783Srmacklem{ 132191783Srmacklem mtx_lock(&np->n_mtx); 133191783Srmacklem np->n_flag &= ~NDIRCOOKIELK; 134191783Srmacklem wakeup(&np->n_flag); 135191783Srmacklem mtx_unlock(&np->n_mtx); 136191783Srmacklem} 137191783Srmacklem 138191783Srmacklemint 139191783Srmacklemncl_upgrade_vnlock(struct vnode *vp) 140191783Srmacklem{ 141191783Srmacklem int old_lock; 142196332Srmacklem 143196332Srmacklem ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock"); 144224083Szack old_lock = NFSVOPISLOCKED(vp); 145196332Srmacklem if (old_lock != LK_EXCLUSIVE) { 146196332Srmacklem KASSERT(old_lock == LK_SHARED, 147196332Srmacklem ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock)); 148196332Srmacklem /* Upgrade to exclusive lock, this might block */ 149224081Szack NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 150191783Srmacklem } 151196332Srmacklem return (old_lock); 152191783Srmacklem} 153191783Srmacklem 154191783Srmacklemvoid 155191783Srmacklemncl_downgrade_vnlock(struct vnode *vp, int old_lock) 156191783Srmacklem{ 157191783Srmacklem if (old_lock != LK_EXCLUSIVE) { 158196332Srmacklem KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock)); 159196332Srmacklem /* Downgrade from exclusive lock. */ 160224081Szack NFSVOPLOCK(vp, LK_DOWNGRADE | LK_RETRY); 161191783Srmacklem } 162191783Srmacklem} 163191783Srmacklem 164191783Srmacklem#ifdef NFS_ACDEBUG 165191783Srmacklem#include <sys/sysctl.h> 166221973SrmacklemSYSCTL_DECL(_vfs_nfs); 167191783Srmacklemstatic int nfs_acdebug; 168221973SrmacklemSYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 169191783Srmacklem#endif 170191783Srmacklem 171191783Srmacklem/* 172191783Srmacklem * Check the time stamp 173191783Srmacklem * If the cache is valid, copy contents to *vap and return 0 174191783Srmacklem * otherwise return an error 175191783Srmacklem */ 176191783Srmacklemint 177191783Srmacklemncl_getattrcache(struct vnode *vp, struct vattr *vaper) 178191783Srmacklem{ 179191783Srmacklem struct nfsnode *np; 180191783Srmacklem struct vattr *vap; 181191783Srmacklem struct nfsmount *nmp; 182210786Srmacklem int timeo, mustflush; 183191783Srmacklem 184191783Srmacklem np = VTONFS(vp); 185191783Srmacklem vap = &np->n_vattr.na_vattr; 186191783Srmacklem nmp = VFSTONFS(vp->v_mount); 187208254Srmacklem mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */ 188191783Srmacklem mtx_lock(&np->n_mtx); 189191783Srmacklem /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 190191783Srmacklem timeo = (time_second - np->n_mtime.tv_sec) / 10; 191191783Srmacklem 192191783Srmacklem#ifdef NFS_ACDEBUG 193191783Srmacklem if (nfs_acdebug>1) 194303909Skib printf("ncl_getattrcache: initial timeo = %d\n", timeo); 195191783Srmacklem#endif 196191783Srmacklem 197191783Srmacklem if (vap->va_type == VDIR) { 198191783Srmacklem if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 199191783Srmacklem timeo = nmp->nm_acdirmin; 200191783Srmacklem else if (timeo > nmp->nm_acdirmax) 201191783Srmacklem timeo = nmp->nm_acdirmax; 202191783Srmacklem } else { 203191783Srmacklem if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 204191783Srmacklem timeo = nmp->nm_acregmin; 205191783Srmacklem else if (timeo > nmp->nm_acregmax) 206191783Srmacklem timeo = nmp->nm_acregmax; 207191783Srmacklem } 208191783Srmacklem 209191783Srmacklem#ifdef NFS_ACDEBUG 210191783Srmacklem if (nfs_acdebug > 2) 211303909Skib printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 212303909Skib nmp->nm_acregmin, nmp->nm_acregmax, 213303909Skib nmp->nm_acdirmin, nmp->nm_acdirmax); 214191783Srmacklem 215191783Srmacklem if (nfs_acdebug) 216303909Skib printf("ncl_getattrcache: age = %d; final timeo = %d\n", 217303909Skib (time_second - np->n_attrstamp), timeo); 218191783Srmacklem#endif 219191783Srmacklem 220208254Srmacklem if ((time_second - np->n_attrstamp) >= timeo && 221210786Srmacklem (mustflush != 0 || np->n_attrstamp == 0)) { 222191783Srmacklem newnfsstats.attrcache_misses++; 223191783Srmacklem mtx_unlock(&np->n_mtx); 224223280Srmacklem KDTRACE_NFS_ATTRCACHE_GET_MISS(vp); 225191783Srmacklem return( ENOENT); 226191783Srmacklem } 227191783Srmacklem newnfsstats.attrcache_hits++; 228191783Srmacklem if (vap->va_size != np->n_size) { 229191783Srmacklem if (vap->va_type == VREG) { 230191783Srmacklem if (np->n_flag & NMODIFIED) { 231191783Srmacklem if (vap->va_size < np->n_size) 232191783Srmacklem vap->va_size = np->n_size; 233191783Srmacklem else 234191783Srmacklem np->n_size = vap->va_size; 235191783Srmacklem } else { 236191783Srmacklem np->n_size = vap->va_size; 237191783Srmacklem } 238191783Srmacklem vnode_pager_setsize(vp, np->n_size); 239191783Srmacklem } else { 240191783Srmacklem np->n_size = vap->va_size; 241191783Srmacklem } 242191783Srmacklem } 243191783Srmacklem bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 244191783Srmacklem if (np->n_flag & NCHG) { 245191783Srmacklem if (np->n_flag & NACC) 246191783Srmacklem vaper->va_atime = np->n_atim; 247191783Srmacklem if (np->n_flag & NUPD) 248191783Srmacklem vaper->va_mtime = np->n_mtim; 249191783Srmacklem } 250191783Srmacklem mtx_unlock(&np->n_mtx); 251223280Srmacklem KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap); 252191783Srmacklem return (0); 253191783Srmacklem} 254191783Srmacklem 255191783Srmacklemstatic nfsuint64 nfs_nullcookie = { { 0, 0 } }; 256191783Srmacklem/* 257191783Srmacklem * This function finds the directory cookie that corresponds to the 258191783Srmacklem * logical byte offset given. 259191783Srmacklem */ 260191783Srmacklemnfsuint64 * 261191783Srmacklemncl_getcookie(struct nfsnode *np, off_t off, int add) 262191783Srmacklem{ 263191783Srmacklem struct nfsdmap *dp, *dp2; 264191783Srmacklem int pos; 265191783Srmacklem nfsuint64 *retval = NULL; 266191783Srmacklem 267191783Srmacklem pos = (uoff_t)off / NFS_DIRBLKSIZ; 268191783Srmacklem if (pos == 0 || off < 0) { 269209120Skib KASSERT(!add, ("nfs getcookie add at <= 0")); 270191783Srmacklem return (&nfs_nullcookie); 271191783Srmacklem } 272191783Srmacklem pos--; 273191783Srmacklem dp = LIST_FIRST(&np->n_cookies); 274191783Srmacklem if (!dp) { 275191783Srmacklem if (add) { 276191783Srmacklem MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 277191783Srmacklem M_NFSDIROFF, M_WAITOK); 278191783Srmacklem dp->ndm_eocookie = 0; 279191783Srmacklem LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 280191783Srmacklem } else 281191783Srmacklem goto out; 282191783Srmacklem } 283191783Srmacklem while (pos >= NFSNUMCOOKIES) { 284191783Srmacklem pos -= NFSNUMCOOKIES; 285191783Srmacklem if (LIST_NEXT(dp, ndm_list)) { 286191783Srmacklem if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 287191783Srmacklem pos >= dp->ndm_eocookie) 288191783Srmacklem goto out; 289191783Srmacklem dp = LIST_NEXT(dp, ndm_list); 290191783Srmacklem } else if (add) { 291191783Srmacklem MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 292191783Srmacklem M_NFSDIROFF, M_WAITOK); 293191783Srmacklem dp2->ndm_eocookie = 0; 294191783Srmacklem LIST_INSERT_AFTER(dp, dp2, ndm_list); 295191783Srmacklem dp = dp2; 296191783Srmacklem } else 297191783Srmacklem goto out; 298191783Srmacklem } 299191783Srmacklem if (pos >= dp->ndm_eocookie) { 300191783Srmacklem if (add) 301191783Srmacklem dp->ndm_eocookie = pos + 1; 302191783Srmacklem else 303191783Srmacklem goto out; 304191783Srmacklem } 305191783Srmacklem retval = &dp->ndm_cookies[pos]; 306191783Srmacklemout: 307191783Srmacklem return (retval); 308191783Srmacklem} 309191783Srmacklem 310191783Srmacklem/* 311191783Srmacklem * Invalidate cached directory information, except for the actual directory 312191783Srmacklem * blocks (which are invalidated separately). 313191783Srmacklem * Done mainly to avoid the use of stale offset cookies. 314191783Srmacklem */ 315191783Srmacklemvoid 316191783Srmacklemncl_invaldir(struct vnode *vp) 317191783Srmacklem{ 318191783Srmacklem struct nfsnode *np = VTONFS(vp); 319191783Srmacklem 320209120Skib KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir")); 321191783Srmacklem ncl_dircookie_lock(np); 322191783Srmacklem np->n_direofoffset = 0; 323191783Srmacklem np->n_cookieverf.nfsuquad[0] = 0; 324191783Srmacklem np->n_cookieverf.nfsuquad[1] = 0; 325191783Srmacklem if (LIST_FIRST(&np->n_cookies)) 326191783Srmacklem LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0; 327191783Srmacklem ncl_dircookie_unlock(np); 328191783Srmacklem} 329191783Srmacklem 330191783Srmacklem/* 331191783Srmacklem * The write verifier has changed (probably due to a server reboot), so all 332191783Srmacklem * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 333191783Srmacklem * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 334191783Srmacklem * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 335191783Srmacklem * mount point. 336191783Srmacklem * 337191783Srmacklem * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 338191783Srmacklem * writes are not clusterable. 339191783Srmacklem */ 340191783Srmacklemvoid 341191783Srmacklemncl_clearcommit(struct mount *mp) 342191783Srmacklem{ 343191783Srmacklem struct vnode *vp, *nvp; 344191783Srmacklem struct buf *bp, *nbp; 345191783Srmacklem struct bufobj *bo; 346191783Srmacklem 347234386Smckusick MNT_VNODE_FOREACH_ALL(vp, mp, nvp) { 348191783Srmacklem bo = &vp->v_bufobj; 349191783Srmacklem vholdl(vp); 350191783Srmacklem VI_UNLOCK(vp); 351191783Srmacklem BO_LOCK(bo); 352191783Srmacklem TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 353191783Srmacklem if (!BUF_ISLOCKED(bp) && 354191783Srmacklem (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 355191783Srmacklem == (B_DELWRI | B_NEEDCOMMIT)) 356191783Srmacklem bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 357191783Srmacklem } 358191783Srmacklem BO_UNLOCK(bo); 359191783Srmacklem vdrop(vp); 360191783Srmacklem } 361191783Srmacklem} 362191783Srmacklem 363191783Srmacklem/* 364191783Srmacklem * Called once to initialize data structures... 365191783Srmacklem */ 366191783Srmacklemint 367191783Srmacklemncl_init(struct vfsconf *vfsp) 368191783Srmacklem{ 369191783Srmacklem int i; 370191783Srmacklem 371191783Srmacklem /* Ensure async daemons disabled */ 372220683Srmacklem for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 373203119Srmacklem ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE; 374191783Srmacklem ncl_iodmount[i] = NULL; 375191783Srmacklem } 376220683Srmacklem TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL); 377191783Srmacklem ncl_nhinit(); /* Init the nfsnode table */ 378191783Srmacklem 379191783Srmacklem return (0); 380191783Srmacklem} 381191783Srmacklem 382