Home | History | Annotate | Line # | Download | only in client
nfs_clsubs.c revision 1.1.1.1.6.2
      1 /*	$NetBSD: nfs_clsubs.c,v 1.1.1.1.6.2 2014/05/22 11:41:00 yamt 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 234386 2012-04-17 16:28:22Z mckusick "); */
     38 __RCSID("$NetBSD: nfs_clsubs.c,v 1.1.1.1.6.2 2014/05/22 11:41:00 yamt Exp $");
     39 
     40 #include "opt_kdtrace.h"
     41 
     42 /*
     43  * These functions support the macros and help fiddle mbuf chains for
     44  * the nfs op functions. They do things like create the rpc header and
     45  * copy data between mbuf chains and uio lists.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/bio.h>
     52 #include <sys/buf.h>
     53 #include <sys/proc.h>
     54 #include <sys/mount.h>
     55 #include <sys/vnode.h>
     56 #include <sys/namei.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/socket.h>
     59 #include <sys/stat.h>
     60 #include <sys/malloc.h>
     61 #include <sys/sysent.h>
     62 #include <sys/syscall.h>
     63 #include <sys/sysproto.h>
     64 #include <sys/taskqueue.h>
     65 
     66 #include <vm/vm.h>
     67 #include <vm/vm_object.h>
     68 #include <vm/vm_extern.h>
     69 #include <vm/uma.h>
     70 
     71 #include <fs/nfs/nfsport.h>
     72 #include <fs/nfsclient/nfsnode.h>
     73 #include <fs/nfsclient/nfsmount.h>
     74 #include <fs/nfsclient/nfs.h>
     75 #include <fs/nfsclient/nfs_kdtrace.h>
     76 
     77 #include <netinet/in.h>
     78 
     79 /*
     80  * Note that stdarg.h and the ANSI style va_start macro is used for both
     81  * ANSI and traditional C compilers.
     82  */
     83 #include <machine/stdarg.h>
     84 
     85 extern struct mtx ncl_iod_mutex;
     86 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
     87 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
     88 extern int ncl_numasync;
     89 extern unsigned int ncl_iodmax;
     90 extern struct nfsstats newnfsstats;
     91 
     92 struct task	ncl_nfsiodnew_task;
     93 
     94 int
     95 ncl_uninit(struct vfsconf *vfsp)
     96 {
     97 	/*
     98 	 * XXX: Unloading of nfscl module is unsupported.
     99 	 */
    100 #if 0
    101 	int i;
    102 
    103 	/*
    104 	 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup
    105 	 * any sleeping nfsiods so they check ncl_iodmax and exit.
    106 	 */
    107 	mtx_lock(&ncl_iod_mutex);
    108 	ncl_iodmax = 0;
    109 	for (i = 0; i < ncl_numasync; i++)
    110 		if (ncl_iodwant[i] == NFSIOD_AVAILABLE)
    111 			wakeup(&ncl_iodwant[i]);
    112 	/* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */
    113 	while (ncl_numasync)
    114 		msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0);
    115 	mtx_unlock(&ncl_iod_mutex);
    116 	ncl_nhuninit();
    117 	return (0);
    118 #else
    119 	return (EOPNOTSUPP);
    120 #endif
    121 }
    122 
    123 void
    124 ncl_dircookie_lock(struct nfsnode *np)
    125 {
    126 	mtx_lock(&np->n_mtx);
    127 	while (np->n_flag & NDIRCOOKIELK)
    128 		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
    129 	np->n_flag |= NDIRCOOKIELK;
    130 	mtx_unlock(&np->n_mtx);
    131 }
    132 
    133 void
    134 ncl_dircookie_unlock(struct nfsnode *np)
    135 {
    136 	mtx_lock(&np->n_mtx);
    137 	np->n_flag &= ~NDIRCOOKIELK;
    138 	wakeup(&np->n_flag);
    139 	mtx_unlock(&np->n_mtx);
    140 }
    141 
    142 int
    143 ncl_upgrade_vnlock(struct vnode *vp)
    144 {
    145 	int old_lock;
    146 
    147 	ASSERT_VOP_LOCKED(vp, "ncl_upgrade_vnlock");
    148 	old_lock = NFSVOPISLOCKED(vp);
    149 	if (old_lock != LK_EXCLUSIVE) {
    150 		KASSERT(old_lock == LK_SHARED,
    151 		    ("ncl_upgrade_vnlock: wrong old_lock %d", old_lock));
    152 		/* Upgrade to exclusive lock, this might block */
    153 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
    154   	}
    155 	return (old_lock);
    156 }
    157 
    158 void
    159 ncl_downgrade_vnlock(struct vnode *vp, int old_lock)
    160 {
    161 	if (old_lock != LK_EXCLUSIVE) {
    162 		KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
    163 		/* Downgrade from exclusive lock. */
    164 		NFSVOPLOCK(vp, LK_DOWNGRADE | LK_RETRY);
    165   	}
    166 }
    167 
    168 void
    169 ncl_printf(const char *fmt, ...)
    170 {
    171 	va_list ap;
    172 
    173 	mtx_lock(&Giant);
    174 	va_start(ap, fmt);
    175 	vprintf(fmt, ap);
    176 	va_end(ap);
    177 	mtx_unlock(&Giant);
    178 }
    179 
    180 #ifdef NFS_ACDEBUG
    181 #include <sys/sysctl.h>
    182 SYSCTL_DECL(_vfs_nfs);
    183 static int nfs_acdebug;
    184 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
    185 #endif
    186 
    187 /*
    188  * Check the time stamp
    189  * If the cache is valid, copy contents to *vap and return 0
    190  * otherwise return an error
    191  */
    192 int
    193 ncl_getattrcache(struct vnode *vp, struct vattr *vaper)
    194 {
    195 	struct nfsnode *np;
    196 	struct vattr *vap;
    197 	struct nfsmount *nmp;
    198 	int timeo, mustflush;
    199 
    200 	np = VTONFS(vp);
    201 	vap = &np->n_vattr.na_vattr;
    202 	nmp = VFSTONFS(vp->v_mount);
    203 	mustflush = nfscl_mustflush(vp);	/* must be before mtx_lock() */
    204 #ifdef NFS_ACDEBUG
    205 	mtx_lock(&Giant);	/* ncl_printf() */
    206 #endif
    207 	mtx_lock(&np->n_mtx);
    208 	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
    209 	timeo = (time_second - np->n_mtime.tv_sec) / 10;
    210 
    211 #ifdef NFS_ACDEBUG
    212 	if (nfs_acdebug>1)
    213 		ncl_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
    214 #endif
    215 
    216 	if (vap->va_type == VDIR) {
    217 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
    218 			timeo = nmp->nm_acdirmin;
    219 		else if (timeo > nmp->nm_acdirmax)
    220 			timeo = nmp->nm_acdirmax;
    221 	} else {
    222 		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
    223 			timeo = nmp->nm_acregmin;
    224 		else if (timeo > nmp->nm_acregmax)
    225 			timeo = nmp->nm_acregmax;
    226 	}
    227 
    228 #ifdef NFS_ACDEBUG
    229 	if (nfs_acdebug > 2)
    230 		ncl_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
    231 			   nmp->nm_acregmin, nmp->nm_acregmax,
    232 			   nmp->nm_acdirmin, nmp->nm_acdirmax);
    233 
    234 	if (nfs_acdebug)
    235 		ncl_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
    236 			   (time_second - np->n_attrstamp), timeo);
    237 #endif
    238 
    239 	if ((time_second - np->n_attrstamp) >= timeo &&
    240 	    (mustflush != 0 || np->n_attrstamp == 0)) {
    241 		newnfsstats.attrcache_misses++;
    242 		mtx_unlock(&np->n_mtx);
    243 #ifdef NFS_ACDEBUG
    244 		mtx_unlock(&Giant);	/* ncl_printf() */
    245 #endif
    246 		KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
    247 		return( ENOENT);
    248 	}
    249 	newnfsstats.attrcache_hits++;
    250 	if (vap->va_size != np->n_size) {
    251 		if (vap->va_type == VREG) {
    252 			if (np->n_flag & NMODIFIED) {
    253 				if (vap->va_size < np->n_size)
    254 					vap->va_size = np->n_size;
    255 				else
    256 					np->n_size = vap->va_size;
    257 			} else {
    258 				np->n_size = vap->va_size;
    259 			}
    260 			vnode_pager_setsize(vp, np->n_size);
    261 		} else {
    262 			np->n_size = vap->va_size;
    263 		}
    264 	}
    265 	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
    266 	if (np->n_flag & NCHG) {
    267 		if (np->n_flag & NACC)
    268 			vaper->va_atime = np->n_atim;
    269 		if (np->n_flag & NUPD)
    270 			vaper->va_mtime = np->n_mtim;
    271 	}
    272 	mtx_unlock(&np->n_mtx);
    273 #ifdef NFS_ACDEBUG
    274 	mtx_unlock(&Giant);	/* ncl_printf() */
    275 #endif
    276 	KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
    277 	return (0);
    278 }
    279 
    280 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
    281 /*
    282  * This function finds the directory cookie that corresponds to the
    283  * logical byte offset given.
    284  */
    285 nfsuint64 *
    286 ncl_getcookie(struct nfsnode *np, off_t off, int add)
    287 {
    288 	struct nfsdmap *dp, *dp2;
    289 	int pos;
    290 	nfsuint64 *retval = NULL;
    291 
    292 	pos = (uoff_t)off / NFS_DIRBLKSIZ;
    293 	if (pos == 0 || off < 0) {
    294 		KASSERT(!add, ("nfs getcookie add at <= 0"));
    295 		return (&nfs_nullcookie);
    296 	}
    297 	pos--;
    298 	dp = LIST_FIRST(&np->n_cookies);
    299 	if (!dp) {
    300 		if (add) {
    301 			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
    302 				M_NFSDIROFF, M_WAITOK);
    303 			dp->ndm_eocookie = 0;
    304 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
    305 		} else
    306 			goto out;
    307 	}
    308 	while (pos >= NFSNUMCOOKIES) {
    309 		pos -= NFSNUMCOOKIES;
    310 		if (LIST_NEXT(dp, ndm_list)) {
    311 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
    312 			    pos >= dp->ndm_eocookie)
    313 				goto out;
    314 			dp = LIST_NEXT(dp, ndm_list);
    315 		} else if (add) {
    316 			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
    317 				M_NFSDIROFF, M_WAITOK);
    318 			dp2->ndm_eocookie = 0;
    319 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
    320 			dp = dp2;
    321 		} else
    322 			goto out;
    323 	}
    324 	if (pos >= dp->ndm_eocookie) {
    325 		if (add)
    326 			dp->ndm_eocookie = pos + 1;
    327 		else
    328 			goto out;
    329 	}
    330 	retval = &dp->ndm_cookies[pos];
    331 out:
    332 	return (retval);
    333 }
    334 
    335 /*
    336  * Invalidate cached directory information, except for the actual directory
    337  * blocks (which are invalidated separately).
    338  * Done mainly to avoid the use of stale offset cookies.
    339  */
    340 void
    341 ncl_invaldir(struct vnode *vp)
    342 {
    343 	struct nfsnode *np = VTONFS(vp);
    344 
    345 	KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
    346 	ncl_dircookie_lock(np);
    347 	np->n_direofoffset = 0;
    348 	np->n_cookieverf.nfsuquad[0] = 0;
    349 	np->n_cookieverf.nfsuquad[1] = 0;
    350 	if (LIST_FIRST(&np->n_cookies))
    351 		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
    352 	ncl_dircookie_unlock(np);
    353 }
    354 
    355 /*
    356  * The write verifier has changed (probably due to a server reboot), so all
    357  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
    358  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
    359  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
    360  * mount point.
    361  *
    362  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
    363  * writes are not clusterable.
    364  */
    365 void
    366 ncl_clearcommit(struct mount *mp)
    367 {
    368 	struct vnode *vp, *nvp;
    369 	struct buf *bp, *nbp;
    370 	struct bufobj *bo;
    371 
    372 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
    373 		bo = &vp->v_bufobj;
    374 		vholdl(vp);
    375 		VI_UNLOCK(vp);
    376 		BO_LOCK(bo);
    377 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
    378 			if (!BUF_ISLOCKED(bp) &&
    379 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
    380 				== (B_DELWRI | B_NEEDCOMMIT))
    381 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
    382 		}
    383 		BO_UNLOCK(bo);
    384 		vdrop(vp);
    385 	}
    386 }
    387 
    388 /*
    389  * Called once to initialize data structures...
    390  */
    391 int
    392 ncl_init(struct vfsconf *vfsp)
    393 {
    394 	int i;
    395 
    396 	/* Ensure async daemons disabled */
    397 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
    398 		ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
    399 		ncl_iodmount[i] = NULL;
    400 	}
    401 	TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL);
    402 	ncl_nhinit();			/* Init the nfsnode table */
    403 
    404 	return (0);
    405 }
    406 
    407