Home | History | Annotate | Line # | Download | only in client
      1 /*	$NetBSD: nfs_clsubs.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $	*/
      2 /*-
      3  * Copyright (c) 1989, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Rick Macklem at The University of Guelph.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from nfs_subs.c  8.8 (Berkeley) 5/22/95
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clsubs.c 304026 2016-08-12 22:44:59Z rmacklem "); */
     38 __RCSID("$NetBSD: nfs_clsubs.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $");
     39 
     40 /*
     41  * These functions support the macros and help fiddle mbuf chains for
     42  * the nfs op functions. They do things like create the rpc header and
     43  * copy data between mbuf chains and uio lists.
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/bio.h>
     50 #include <sys/buf.h>
     51 #include <sys/proc.h>
     52 #include <sys/mount.h>
     53 #include <sys/vnode.h>
     54 #include <sys/namei.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/socket.h>
     57 #include <sys/stat.h>
     58 #include <sys/malloc.h>
     59 #include <sys/sysent.h>
     60 #include <sys/syscall.h>
     61 #include <sys/sysproto.h>
     62 #include <sys/taskqueue.h>
     63 
     64 #include <vm/vm.h>
     65 #include <vm/vm_object.h>
     66 #include <vm/vm_extern.h>
     67 #include <vm/uma.h>
     68 
     69 #include <fs/nfs/common/nfsport.h>
     70 #include <fs/nfs/client/nfsnode.h>
     71 #include <fs/nfs/client/nfsmount.h>
     72 #include <fs/nfs/client/nfs.h>
     73 #include <fs/nfs/client/nfs_kdtrace.h>
     74 
     75 #include <netinet/in.h>
     76 
     77 /*
     78  * Note that stdarg.h and the ANSI style va_start macro is used for both
     79  * ANSI and traditional C compilers.
     80  */
     81 #include <machine/stdarg.h>
     82 
     83 extern struct mtx ncl_iod_mutex;
     84 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
     85 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
     86 extern int ncl_numasync;
     87 extern unsigned int ncl_iodmax;
     88 extern struct nfsstatsv1 nfsstatsv1;
     89 
     90 struct task	ncl_nfsiodnew_task;
     91 
     92 int
     93 ncl_uninit(struct vfsconf *vfsp)
     94 {
     95 	/*
     96 	 * XXX: Unloading of nfscl module is unsupported.
     97 	 */
     98 #if 0
     99 	int i;
    100 
    101 	/*
    102 	 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
    103 	 * any sleeping nfsiods so they check ncl_iodmax and exit.
    104 	 */
    105 	mtx_lock(&ncl_iod_mutex);
    106 	ncl_iodmax = 0;
    107 	for (i = 0; i < ncl_numasync; i++)
    108 		if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
    109 			wakeup(&ncl_iodwant[i]);
    110 	/* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
    111 	while (ncl_numasync)
    112 		msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
    113 	mtx_unlock(&ncl_iod_mutex);
    114 	ncl_nhuninit();
    115 	return (0);
    116 #else
    117 	return (EOPNOTSUPP);
    118 #endif
    119 }
    120 
    121 void
    122 ncl_dircookie_lock(struct nfsnode *np)
    123 {
    124 	mtx_lock(&np->n_mtx);
    125 	while (np->n_flag & NDIRCOOKIELK)
    126 		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
    127 	np->n_flag |= NDIRCOOKIELK;
    128 	mtx_unlock(&np->n_mtx);
    129 }
    130 
    131 void
    132 ncl_dircookie_unlock(struct nfsnode *np)
    133 {
    134 	mtx_lock(&np->n_mtx);
    135 	np->n_flag &= ~NDIRCOOKIELK;
    136 	wakeup(&np->n_flag);
    137 	mtx_unlock(&np->n_mtx);
    138 }
    139 
    140 int
    141 ncl_upgrade_vnlock(struct vnode *vp)
    142 {
    143 	int old_lock;
    144 
    145 	ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
    146 	old_lock = NFSVOPISLOCKED(vp);
    147 	if (old_lock != LK_EXCLUSIVE) {
    148 		KASSERT(old_lock == LK_SHARED,
    149 		    ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
    150 		/* Upgrade to exclusive lock, this might block */
    151 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
    152   	}
    153 	return (old_lock);
    154 }
    155 
    156 void
    157 ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
    158 {
    159 	if (old_lock != LK_EXCLUSIVE) {
    160 		KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
    161 		/* Downgrade from exclusive lock. */
    162 		NFSVOPLOCK(vp, LK_DOWNGRADE | LK_RETRY);
    163   	}
    164 }
    165 
    166 #ifdef NFS_ACDEBUG
    167 #include <sys/sysctl.h>
    168 SYSCTL_DECL(_vfs_nfs);
    169 static int nfs_acdebug;
    170 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
    171 #endif
    172 
    173 /*
    174  * Check the time stamp
    175  * If the cache is valid, copy contents to *vap and return 0
    176  * otherwise return an error
    177  */
    178 int
    179 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
    180 {
    181 	struct nfsnode *np;
    182 	struct vattr *vap;
    183 	struct nfsmount *nmp;
    184 	int timeo, mustflush;
    185 
    186 	np = VTONFS(vp);
    187 	vap = &np->n_vattr.na_vattr;
    188 	nmp = VFSTONFS(vp->v_mount);
    189 	mustflush = nfscl_mustflush(vp);	/* must be before mtx_lock() */
    190 	mtx_lock(&np->n_mtx);
    191 	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
    192 	timeo = (time_second - np->n_mtime.tv_sec) / 10;
    193 
    194 #ifdef NFS_ACDEBUG
    195 	if (nfs_acdebug>1)
    196 		printf("ncl_getattrcache: initial timeo = %d\n", timeo);
    197 #endif
    198 
    199 	if (vap->va_type == VDIR) {
    200 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
    201 			timeo = nmp->nm_acdirmin;
    202 		else if (timeo > nmp->nm_acdirmax)
    203 			timeo = nmp->nm_acdirmax;
    204 	} else {
    205 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
    206 			timeo = nmp->nm_acregmin;
    207 		else if (timeo > nmp->nm_acregmax)
    208 			timeo = nmp->nm_acregmax;
    209 	}
    210 
    211 #ifdef NFS_ACDEBUG
    212 	if (nfs_acdebug > 2)
    213 		printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
    214 		    nmp->nm_acregmin, nmp->nm_acregmax,
    215 		    nmp->nm_acdirmin, nmp->nm_acdirmax);
    216 
    217 	if (nfs_acdebug)
    218 		printf("ncl_getattrcache: age = %d; final timeo = %d\n",
    219 		    (time_second - np->n_attrstamp), timeo);
    220 #endif
    221 
    222 	if ((time_second - np->n_attrstamp) >= timeo &&
    223 	    (mustflush != 0 || np->n_attrstamp == 0)) {
    224 		nfsstatsv1.attrcache_misses++;
    225 		mtx_unlock(&np->n_mtx);
    226 		KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
    227 		return( ENOENT);
    228 	}
    229 	nfsstatsv1.attrcache_hits++;
    230 	if (vap->va_size != np->n_size) {
    231 		if (vap->va_type == VREG) {
    232 			if (np->n_flag & NMODIFIED) {
    233 				if (vap->va_size < np->n_size)
    234 					vap->va_size = np->n_size;
    235 				else
    236 					np->n_size = vap->va_size;
    237 			} else {
    238 				np->n_size = vap->va_size;
    239 			}
    240 			vnode_pager_setsize(vp, np->n_size);
    241 		} else {
    242 			np->n_size = vap->va_size;
    243 		}
    244 	}
    245 	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
    246 	if (np->n_flag & NCHG) {
    247 		if (np->n_flag & NACC)
    248 			vaper->va_atime = np->n_atim;
    249 		if (np->n_flag & NUPD)
    250 			vaper->va_mtime = np->n_mtim;
    251 	}
    252 	mtx_unlock(&np->n_mtx);
    253 	KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
    254 	return (0);
    255 }
    256 
    257 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
    258 /*
    259  * This function finds the directory cookie that corresponds to the
    260  * logical byte offset given.
    261  */
    262 nfsuint64 *
    263 ncl_getcookie(struct nfsnode *np, off_t off, int add)
    264 {
    265 	struct nfsdmap *dp, *dp2;
    266 	int pos;
    267 	nfsuint64 *retval = NULL;
    268 
    269 	pos = (uoff_t)off / NFS_DIRBLKSIZ;
    270 	if (pos == 0 || off < 0) {
    271 		KASSERT(!add, ("nfs getcookie add at <= 0"));
    272 		return (&nfs_nullcookie);
    273 	}
    274 	pos--;
    275 	dp = LIST_FIRST(&np->n_cookies);
    276 	if (!dp) {
    277 		if (add) {
    278 			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
    279 				M_NFSDIROFF, M_WAITOK);
    280 			dp->ndm_eocookie = 0;
    281 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
    282 		} else
    283 			goto out;
    284 	}
    285 	while (pos >= NFSNUMCOOKIES) {
    286 		pos -= NFSNUMCOOKIES;
    287 		if (LIST_NEXT(dp, ndm_list)) {
    288 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
    289 			    pos >= dp->ndm_eocookie)
    290 				goto out;
    291 			dp = LIST_NEXT(dp, ndm_list);
    292 		} else if (add) {
    293 			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
    294 				M_NFSDIROFF, M_WAITOK);
    295 			dp2->ndm_eocookie = 0;
    296 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
    297 			dp = dp2;
    298 		} else
    299 			goto out;
    300 	}
    301 	if (pos >= dp->ndm_eocookie) {
    302 		if (add)
    303 			dp->ndm_eocookie = pos + 1;
    304 		else
    305 			goto out;
    306 	}
    307 	retval = &dp->ndm_cookies[pos];
    308 out:
    309 	return (retval);
    310 }
    311 
    312 /*
    313  * Invalidate cached directory information, except for the actual directory
    314  * blocks (which are invalidated separately).
    315  * Done mainly to avoid the use of stale offset cookies.
    316  */
    317 void
    318 ncl_invaldir(struct vnode *vp)
    319 {
    320 	struct nfsnode *np = VTONFS(vp);
    321 
    322 	KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
    323 	ncl_dircookie_lock(np);
    324 	np->n_direofoffset = 0;
    325 	np->n_cookieverf.nfsuquad[0] = 0;
    326 	np->n_cookieverf.nfsuquad[1] = 0;
    327 	if (LIST_FIRST(&np->n_cookies))
    328 		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
    329 	ncl_dircookie_unlock(np);
    330 }
    331 
    332 /*
    333  * The write verifier has changed (probably due to a server reboot), so all
    334  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
    335  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
    336  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
    337  * mount point.
    338  *
    339  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
    340  * writes are not clusterable.
    341  */
    342 void
    343 ncl_clearcommit(struct mount *mp)
    344 {
    345 	struct vnode *vp, *nvp;
    346 	struct buf *bp, *nbp;
    347 	struct bufobj *bo;
    348 
    349 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
    350 		bo = &vp->v_bufobj;
    351 		vholdl(vp);
    352 		VI_UNLOCK(vp);
    353 		BO_LOCK(bo);
    354 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
    355 			if (!BUF_ISLOCKED(bp) &&
    356 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
    357 				== (B_DELWRI | B_NEEDCOMMIT))
    358 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
    359 		}
    360 		BO_UNLOCK(bo);
    361 		vdrop(vp);
    362 	}
    363 }
    364 
    365 /*
    366  * Called once to initialize data structures...
    367  */
    368 int
    369 ncl_init(struct vfsconf *vfsp)
    370 {
    371 	int i;
    372 
    373 	/* Ensure async daemons disabled */
    374 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
    375 		ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
    376 		ncl_iodmount[i] = NULL;
    377 	}
    378 	TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
    379 	ncl_nhinit();			/* Init the nfsnode table */
    380 
    381 	return (0);
    382 }
    383 
    384