nfs_clnode.c revision 220731
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_node.c 8.6 (Berkeley) 5/22/95 33191783Srmacklem */ 34191783Srmacklem 35191783Srmacklem#include <sys/cdefs.h> 36191783Srmacklem__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clnode.c 220731 2011-04-16 22:15:59Z rmacklem $"); 37191783Srmacklem 38191783Srmacklem#include <sys/param.h> 39191783Srmacklem#include <sys/systm.h> 40214048Srmacklem#include <sys/fcntl.h> 41191783Srmacklem#include <sys/lock.h> 42191783Srmacklem#include <sys/malloc.h> 43191783Srmacklem#include <sys/mount.h> 44191783Srmacklem#include <sys/namei.h> 45191783Srmacklem#include <sys/proc.h> 46191783Srmacklem#include <sys/socket.h> 47191783Srmacklem#include <sys/sysctl.h> 48191783Srmacklem#include <sys/vnode.h> 49191783Srmacklem 50191783Srmacklem#include <vm/uma.h> 51191783Srmacklem 52191783Srmacklem#include <fs/nfs/nfsport.h> 53191783Srmacklem#include <fs/nfsclient/nfsnode.h> 54191783Srmacklem#include <fs/nfsclient/nfsmount.h> 55191783Srmacklem#include <fs/nfsclient/nfs.h> 56191783Srmacklem 57214048Srmacklem#include <nfs/nfs_lock.h> 58214048Srmacklem 59191783Srmacklemextern struct vop_vector newnfs_vnodeops; 60191783Srmacklemextern struct buf_ops buf_ops_newnfs; 61191783SrmacklemMALLOC_DECLARE(M_NEWNFSREQ); 62191783Srmacklem 63191783Srmacklemuma_zone_t newnfsnode_zone; 64191783Srmacklem 65191783Srmacklemvoid 66191783Srmacklemncl_nhinit(void) 67191783Srmacklem{ 68191783Srmacklem 69191783Srmacklem newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL, 70191783Srmacklem NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 71191783Srmacklem} 72191783Srmacklem 73191783Srmacklemvoid 74191783Srmacklemncl_nhuninit(void) 75191783Srmacklem{ 76191783Srmacklem uma_zdestroy(newnfsnode_zone); 77191783Srmacklem} 78191783Srmacklem 79191783Srmacklem/* 80191783Srmacklem * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this 81191783Srmacklem * function is going to be used to get Regular Files, code must be added 82191783Srmacklem * to fill in the "struct nfsv4node". 83191783Srmacklem * Look up a vnode/nfsnode by file handle. 84191783Srmacklem * Callers must check for mount points!! 85191783Srmacklem * In all cases, a pointer to a 86191783Srmacklem * nfsnode structure is returned. 87191783Srmacklem */ 88191783Srmacklemint 89191783Srmacklemncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp) 90191783Srmacklem{ 91191783Srmacklem struct thread *td = curthread; /* XXX */ 92191783Srmacklem struct nfsnode *np; 93191783Srmacklem struct vnode *vp; 94191783Srmacklem struct vnode *nvp; 95191783Srmacklem int error; 96191783Srmacklem u_int hash; 97191783Srmacklem struct nfsmount *nmp; 98191783Srmacklem struct nfsfh *nfhp; 99191783Srmacklem 100191783Srmacklem nmp = VFSTONFS(mntp); 101191783Srmacklem *npp = NULL; 102191783Srmacklem 103191783Srmacklem hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT); 104191783Srmacklem 105191783Srmacklem MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize, 106191783Srmacklem M_NFSFH, M_WAITOK); 107191783Srmacklem bcopy(fhp, &nfhp->nfh_fh[0], fhsize); 108191783Srmacklem nfhp->nfh_len = fhsize; 109191783Srmacklem error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE, 110191783Srmacklem td, &nvp, newnfs_vncmpf, nfhp); 111191783Srmacklem FREE(nfhp, M_NFSFH); 112191783Srmacklem if (error) 113191783Srmacklem return (error); 114191783Srmacklem if (nvp != NULL) { 115191783Srmacklem *npp = VTONFS(nvp); 116191783Srmacklem return (0); 117191783Srmacklem } 118191783Srmacklem 119191783Srmacklem /* 120191783Srmacklem * Allocate before getnewvnode since doing so afterward 121191783Srmacklem * might cause a bogus v_data pointer to get dereferenced 122191783Srmacklem * elsewhere if zalloc should block. 123191783Srmacklem */ 124191783Srmacklem np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO); 125191783Srmacklem 126191783Srmacklem error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp); 127191783Srmacklem if (error) { 128191783Srmacklem uma_zfree(newnfsnode_zone, np); 129191783Srmacklem return (error); 130191783Srmacklem } 131191783Srmacklem vp = nvp; 132191783Srmacklem vp->v_bufobj.bo_ops = &buf_ops_newnfs; 133191783Srmacklem vp->v_data = np; 134191783Srmacklem np->n_vnode = vp; 135191783Srmacklem /* 136191783Srmacklem * Initialize the mutex even if the vnode is going to be a loser. 137191783Srmacklem * This simplifies the logic in reclaim, which can then unconditionally 138191783Srmacklem * destroy the mutex (in the case of the loser, or if hash_insert 139191783Srmacklem * happened to return an error no special casing is needed). 140191783Srmacklem */ 141191783Srmacklem mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK); 142191783Srmacklem /* 143191783Srmacklem * NFS supports recursive and shared locking. 144191783Srmacklem */ 145211531Sjhb lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL); 146191783Srmacklem VN_LOCK_AREC(vp); 147191783Srmacklem VN_LOCK_ASHARE(vp); 148191783Srmacklem /* 149191783Srmacklem * Are we getting the root? If so, make sure the vnode flags 150191783Srmacklem * are correct 151191783Srmacklem */ 152191783Srmacklem if ((fhsize == nmp->nm_fhsize) && 153191783Srmacklem !bcmp(fhp, nmp->nm_fh, fhsize)) { 154191783Srmacklem if (vp->v_type == VNON) 155191783Srmacklem vp->v_type = VDIR; 156191783Srmacklem vp->v_vflag |= VV_ROOT; 157191783Srmacklem } 158191783Srmacklem 159191783Srmacklem MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize, 160191783Srmacklem M_NFSFH, M_WAITOK); 161191783Srmacklem bcopy(fhp, np->n_fhp->nfh_fh, fhsize); 162191783Srmacklem np->n_fhp->nfh_len = fhsize; 163191783Srmacklem error = insmntque(vp, mntp); 164191783Srmacklem if (error != 0) { 165191783Srmacklem *npp = NULL; 166191783Srmacklem FREE((caddr_t)np->n_fhp, M_NFSFH); 167191783Srmacklem mtx_destroy(&np->n_mtx); 168191783Srmacklem uma_zfree(newnfsnode_zone, np); 169191783Srmacklem return (error); 170191783Srmacklem } 171191783Srmacklem error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE, 172191783Srmacklem td, &nvp, newnfs_vncmpf, np->n_fhp); 173191783Srmacklem if (error) 174191783Srmacklem return (error); 175191783Srmacklem if (nvp != NULL) { 176191783Srmacklem *npp = VTONFS(nvp); 177191783Srmacklem /* vfs_hash_insert() vput()'s the losing vnode */ 178191783Srmacklem return (0); 179191783Srmacklem } 180191783Srmacklem *npp = np; 181191783Srmacklem 182191783Srmacklem return (0); 183191783Srmacklem} 184191783Srmacklem 185191783Srmacklemint 186191783Srmacklemncl_inactive(struct vop_inactive_args *ap) 187191783Srmacklem{ 188191783Srmacklem struct nfsnode *np; 189191783Srmacklem struct sillyrename *sp; 190193125Srmacklem struct vnode *vp = ap->a_vp; 191191783Srmacklem 192193125Srmacklem np = VTONFS(vp); 193192337Srmacklem 194193125Srmacklem if (NFS_ISV4(vp) && vp->v_type == VREG) { 195192928Srmacklem /* 196192928Srmacklem * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4 197192928Srmacklem * Close operations are delayed until now. Any dirty buffers 198192928Srmacklem * must be flushed before the close, so that the stateid is 199192928Srmacklem * available for the writes. 200192928Srmacklem */ 201207350Srmacklem (void) ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0); 202193125Srmacklem (void) nfsrpc_close(vp, 1, ap->a_td); 203192928Srmacklem } 204192337Srmacklem 205220731Srmacklem mtx_lock(&np->n_mtx); 206193125Srmacklem if (vp->v_type != VDIR) { 207191783Srmacklem sp = np->n_sillyrename; 208191783Srmacklem np->n_sillyrename = NULL; 209191783Srmacklem } else 210191783Srmacklem sp = NULL; 211191783Srmacklem if (sp) { 212220731Srmacklem mtx_unlock(&np->n_mtx); 213193125Srmacklem (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1); 214191783Srmacklem /* 215191783Srmacklem * Remove the silly file that was rename'd earlier 216191783Srmacklem */ 217193125Srmacklem ncl_removeit(sp, vp); 218191783Srmacklem crfree(sp->s_cred); 219191783Srmacklem vrele(sp->s_dvp); 220191783Srmacklem FREE((caddr_t)sp, M_NEWNFSREQ); 221220731Srmacklem mtx_lock(&np->n_mtx); 222191783Srmacklem } 223191783Srmacklem np->n_flag &= NMODIFIED; 224220731Srmacklem mtx_unlock(&np->n_mtx); 225191783Srmacklem return (0); 226191783Srmacklem} 227191783Srmacklem 228191783Srmacklem/* 229191783Srmacklem * Reclaim an nfsnode so that it can be used for other purposes. 230191783Srmacklem */ 231191783Srmacklemint 232191783Srmacklemncl_reclaim(struct vop_reclaim_args *ap) 233191783Srmacklem{ 234191783Srmacklem struct vnode *vp = ap->a_vp; 235191783Srmacklem struct nfsnode *np = VTONFS(vp); 236191783Srmacklem struct nfsdmap *dp, *dp2; 237191783Srmacklem 238214511Srmacklem if (NFS_ISV4(vp) && vp->v_type == VREG) 239214511Srmacklem /* 240214511Srmacklem * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4 241214511Srmacklem * Close operations are delayed until ncl_inactive(). 242214511Srmacklem * However, since VOP_INACTIVE() is not guaranteed to be 243214511Srmacklem * called, we need to do it again here. 244214511Srmacklem */ 245214511Srmacklem (void) nfsrpc_close(vp, 1, ap->a_td); 246214511Srmacklem 247191783Srmacklem /* 248191783Srmacklem * If the NLM is running, give it a chance to abort pending 249191783Srmacklem * locks. 250191783Srmacklem */ 251214048Srmacklem if (nfs_reclaim_p != NULL) 252214048Srmacklem nfs_reclaim_p(ap); 253191783Srmacklem 254191783Srmacklem /* 255191783Srmacklem * Destroy the vm object and flush associated pages. 256191783Srmacklem */ 257191783Srmacklem vnode_destroy_vobject(vp); 258191783Srmacklem 259191783Srmacklem vfs_hash_remove(vp); 260191783Srmacklem 261191783Srmacklem /* 262191783Srmacklem * Call nfscl_reclaimnode() to save attributes in the delegation, 263191783Srmacklem * as required. 264191783Srmacklem */ 265191783Srmacklem if (vp->v_type == VREG) 266191783Srmacklem nfscl_reclaimnode(vp); 267191783Srmacklem 268191783Srmacklem /* 269191783Srmacklem * Free up any directory cookie structures and 270191783Srmacklem * large file handle structures that might be associated with 271191783Srmacklem * this nfs node. 272191783Srmacklem */ 273191783Srmacklem if (vp->v_type == VDIR) { 274191783Srmacklem dp = LIST_FIRST(&np->n_cookies); 275191783Srmacklem while (dp) { 276191783Srmacklem dp2 = dp; 277191783Srmacklem dp = LIST_NEXT(dp, ndm_list); 278191783Srmacklem FREE((caddr_t)dp2, M_NFSDIROFF); 279191783Srmacklem } 280191783Srmacklem } 281191783Srmacklem FREE((caddr_t)np->n_fhp, M_NFSFH); 282191783Srmacklem if (np->n_v4 != NULL) 283191783Srmacklem FREE((caddr_t)np->n_v4, M_NFSV4NODE); 284191783Srmacklem mtx_destroy(&np->n_mtx); 285191783Srmacklem uma_zfree(newnfsnode_zone, vp->v_data); 286191783Srmacklem vp->v_data = NULL; 287191783Srmacklem return (0); 288191783Srmacklem} 289191783Srmacklem 290191783Srmacklem/* 291191783Srmacklem * Invalidate both the access and attribute caches for this vnode. 292191783Srmacklem */ 293191783Srmacklemvoid 294191783Srmacklemncl_invalcaches(struct vnode *vp) 295191783Srmacklem{ 296191783Srmacklem struct nfsnode *np = VTONFS(vp); 297191783Srmacklem int i; 298191783Srmacklem 299191783Srmacklem mtx_lock(&np->n_mtx); 300191783Srmacklem for (i = 0; i < NFS_ACCESSCACHESIZE; i++) 301191783Srmacklem np->n_accesscache[i].stamp = 0; 302191783Srmacklem np->n_attrstamp = 0; 303191783Srmacklem mtx_unlock(&np->n_mtx); 304191783Srmacklem} 305191783Srmacklem 306