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: releng/10.2/sys/fs/nfsclient/nfs_clnode.c 248084 2013-03-09 02:32:23Z attilio $"); 37191783Srmacklem 38223280Srmacklem#include "opt_kdtrace.h" 39223280Srmacklem 40191783Srmacklem#include <sys/param.h> 41191783Srmacklem#include <sys/systm.h> 42214048Srmacklem#include <sys/fcntl.h> 43191783Srmacklem#include <sys/lock.h> 44191783Srmacklem#include <sys/malloc.h> 45191783Srmacklem#include <sys/mount.h> 46191783Srmacklem#include <sys/namei.h> 47191783Srmacklem#include <sys/proc.h> 48191783Srmacklem#include <sys/socket.h> 49191783Srmacklem#include <sys/sysctl.h> 50224606Srmacklem#include <sys/taskqueue.h> 51191783Srmacklem#include <sys/vnode.h> 52191783Srmacklem 53191783Srmacklem#include <vm/uma.h> 54191783Srmacklem 55191783Srmacklem#include <fs/nfs/nfsport.h> 56191783Srmacklem#include <fs/nfsclient/nfsnode.h> 57191783Srmacklem#include <fs/nfsclient/nfsmount.h> 58191783Srmacklem#include <fs/nfsclient/nfs.h> 59223280Srmacklem#include <fs/nfsclient/nfs_kdtrace.h> 60191783Srmacklem 61214048Srmacklem#include <nfs/nfs_lock.h> 62214048Srmacklem 63191783Srmacklemextern struct vop_vector newnfs_vnodeops; 64191783Srmacklemextern struct buf_ops buf_ops_newnfs; 65191783SrmacklemMALLOC_DECLARE(M_NEWNFSREQ); 66191783Srmacklem 67191783Srmacklemuma_zone_t newnfsnode_zone; 68191783Srmacklem 69224606Srmacklemstatic void nfs_freesillyrename(void *arg, __unused int pending); 70224606Srmacklem 71191783Srmacklemvoid 72191783Srmacklemncl_nhinit(void) 73191783Srmacklem{ 74191783Srmacklem 75191783Srmacklem newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL, 76191783Srmacklem NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 77191783Srmacklem} 78191783Srmacklem 79191783Srmacklemvoid 80191783Srmacklemncl_nhuninit(void) 81191783Srmacklem{ 82191783Srmacklem uma_zdestroy(newnfsnode_zone); 83191783Srmacklem} 84191783Srmacklem 85191783Srmacklem/* 86191783Srmacklem * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this 87191783Srmacklem * function is going to be used to get Regular Files, code must be added 88191783Srmacklem * to fill in the "struct nfsv4node". 89191783Srmacklem * Look up a vnode/nfsnode by file handle. 90191783Srmacklem * Callers must check for mount points!! 91191783Srmacklem * In all cases, a pointer to a 92191783Srmacklem * nfsnode structure is returned. 93191783Srmacklem */ 94191783Srmacklemint 95220732Srmacklemncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp, 96220732Srmacklem int lkflags) 97191783Srmacklem{ 98191783Srmacklem struct thread *td = curthread; /* XXX */ 99191783Srmacklem struct nfsnode *np; 100191783Srmacklem struct vnode *vp; 101191783Srmacklem struct vnode *nvp; 102191783Srmacklem int error; 103191783Srmacklem u_int hash; 104191783Srmacklem struct nfsmount *nmp; 105191783Srmacklem struct nfsfh *nfhp; 106191783Srmacklem 107191783Srmacklem nmp = VFSTONFS(mntp); 108191783Srmacklem *npp = NULL; 109191783Srmacklem 110191783Srmacklem hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT); 111191783Srmacklem 112191783Srmacklem MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize, 113191783Srmacklem M_NFSFH, M_WAITOK); 114191783Srmacklem bcopy(fhp, &nfhp->nfh_fh[0], fhsize); 115191783Srmacklem nfhp->nfh_len = fhsize; 116220732Srmacklem error = vfs_hash_get(mntp, hash, lkflags, 117191783Srmacklem td, &nvp, newnfs_vncmpf, nfhp); 118191783Srmacklem FREE(nfhp, M_NFSFH); 119191783Srmacklem if (error) 120191783Srmacklem return (error); 121191783Srmacklem if (nvp != NULL) { 122191783Srmacklem *npp = VTONFS(nvp); 123191783Srmacklem return (0); 124191783Srmacklem } 125191783Srmacklem np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO); 126191783Srmacklem 127191783Srmacklem error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp); 128191783Srmacklem if (error) { 129191783Srmacklem uma_zfree(newnfsnode_zone, np); 130191783Srmacklem return (error); 131191783Srmacklem } 132191783Srmacklem vp = nvp; 133230605Srmacklem KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0")); 134191783Srmacklem vp->v_bufobj.bo_ops = &buf_ops_newnfs; 135191783Srmacklem vp->v_data = np; 136191783Srmacklem np->n_vnode = vp; 137191783Srmacklem /* 138191783Srmacklem * Initialize the mutex even if the vnode is going to be a loser. 139191783Srmacklem * This simplifies the logic in reclaim, which can then unconditionally 140191783Srmacklem * destroy the mutex (in the case of the loser, or if hash_insert 141191783Srmacklem * happened to return an error no special casing is needed). 142191783Srmacklem */ 143191783Srmacklem mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK); 144191783Srmacklem /* 145191783Srmacklem * NFS supports recursive and shared locking. 146191783Srmacklem */ 147211531Sjhb lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL); 148191783Srmacklem VN_LOCK_AREC(vp); 149191783Srmacklem VN_LOCK_ASHARE(vp); 150191783Srmacklem /* 151191783Srmacklem * Are we getting the root? If so, make sure the vnode flags 152191783Srmacklem * are correct 153191783Srmacklem */ 154191783Srmacklem if ((fhsize == nmp->nm_fhsize) && 155191783Srmacklem !bcmp(fhp, nmp->nm_fh, fhsize)) { 156191783Srmacklem if (vp->v_type == VNON) 157191783Srmacklem vp->v_type = VDIR; 158191783Srmacklem vp->v_vflag |= VV_ROOT; 159191783Srmacklem } 160191783Srmacklem 161191783Srmacklem MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize, 162191783Srmacklem M_NFSFH, M_WAITOK); 163191783Srmacklem bcopy(fhp, np->n_fhp->nfh_fh, fhsize); 164191783Srmacklem np->n_fhp->nfh_len = fhsize; 165191783Srmacklem error = insmntque(vp, mntp); 166191783Srmacklem if (error != 0) { 167191783Srmacklem *npp = NULL; 168191783Srmacklem FREE((caddr_t)np->n_fhp, M_NFSFH); 169191783Srmacklem mtx_destroy(&np->n_mtx); 170191783Srmacklem uma_zfree(newnfsnode_zone, np); 171191783Srmacklem return (error); 172191783Srmacklem } 173220732Srmacklem error = vfs_hash_insert(vp, hash, lkflags, 174191783Srmacklem td, &nvp, newnfs_vncmpf, np->n_fhp); 175191783Srmacklem if (error) 176191783Srmacklem return (error); 177191783Srmacklem if (nvp != NULL) { 178191783Srmacklem *npp = VTONFS(nvp); 179191783Srmacklem /* vfs_hash_insert() vput()'s the losing vnode */ 180191783Srmacklem return (0); 181191783Srmacklem } 182191783Srmacklem *npp = np; 183191783Srmacklem 184191783Srmacklem return (0); 185191783Srmacklem} 186191783Srmacklem 187224606Srmacklem/* 188224606Srmacklem * Do the vrele(sp->s_dvp) as a separate task in order to avoid a 189224606Srmacklem * deadlock because of a LOR when vrele() locks the directory vnode. 190224606Srmacklem */ 191224606Srmacklemstatic void 192224606Srmacklemnfs_freesillyrename(void *arg, __unused int pending) 193224606Srmacklem{ 194224606Srmacklem struct sillyrename *sp; 195224606Srmacklem 196224606Srmacklem sp = arg; 197224606Srmacklem vrele(sp->s_dvp); 198224606Srmacklem free(sp, M_NEWNFSREQ); 199224606Srmacklem} 200224606Srmacklem 201191783Srmacklemint 202191783Srmacklemncl_inactive(struct vop_inactive_args *ap) 203191783Srmacklem{ 204191783Srmacklem struct nfsnode *np; 205191783Srmacklem struct sillyrename *sp; 206193125Srmacklem struct vnode *vp = ap->a_vp; 207237244Srmacklem boolean_t retv; 208191783Srmacklem 209193125Srmacklem np = VTONFS(vp); 210192337Srmacklem 211193125Srmacklem if (NFS_ISV4(vp) && vp->v_type == VREG) { 212192928Srmacklem /* 213192928Srmacklem * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4 214237244Srmacklem * Close operations are delayed until now. Any dirty 215237244Srmacklem * buffers/pages must be flushed before the close, so that the 216237244Srmacklem * stateid is available for the writes. 217192928Srmacklem */ 218237244Srmacklem if (vp->v_object != NULL) { 219248084Sattilio VM_OBJECT_WLOCK(vp->v_object); 220237244Srmacklem retv = vm_object_page_clean(vp->v_object, 0, 0, 221237244Srmacklem OBJPC_SYNC); 222248084Sattilio VM_OBJECT_WUNLOCK(vp->v_object); 223237244Srmacklem } else 224237244Srmacklem retv = TRUE; 225237244Srmacklem if (retv == TRUE) { 226237244Srmacklem (void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0); 227237244Srmacklem (void)nfsrpc_close(vp, 1, ap->a_td); 228237244Srmacklem } 229192928Srmacklem } 230192337Srmacklem 231220731Srmacklem mtx_lock(&np->n_mtx); 232193125Srmacklem if (vp->v_type != VDIR) { 233191783Srmacklem sp = np->n_sillyrename; 234191783Srmacklem np->n_sillyrename = NULL; 235191783Srmacklem } else 236191783Srmacklem sp = NULL; 237191783Srmacklem if (sp) { 238220731Srmacklem mtx_unlock(&np->n_mtx); 239193125Srmacklem (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1); 240191783Srmacklem /* 241191783Srmacklem * Remove the silly file that was rename'd earlier 242191783Srmacklem */ 243193125Srmacklem ncl_removeit(sp, vp); 244191783Srmacklem crfree(sp->s_cred); 245224606Srmacklem TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp); 246224606Srmacklem taskqueue_enqueue(taskqueue_thread, &sp->s_task); 247220731Srmacklem mtx_lock(&np->n_mtx); 248191783Srmacklem } 249191783Srmacklem np->n_flag &= NMODIFIED; 250220731Srmacklem mtx_unlock(&np->n_mtx); 251191783Srmacklem return (0); 252191783Srmacklem} 253191783Srmacklem 254191783Srmacklem/* 255191783Srmacklem * Reclaim an nfsnode so that it can be used for other purposes. 256191783Srmacklem */ 257191783Srmacklemint 258191783Srmacklemncl_reclaim(struct vop_reclaim_args *ap) 259191783Srmacklem{ 260191783Srmacklem struct vnode *vp = ap->a_vp; 261191783Srmacklem struct nfsnode *np = VTONFS(vp); 262191783Srmacklem struct nfsdmap *dp, *dp2; 263191783Srmacklem 264191783Srmacklem /* 265191783Srmacklem * If the NLM is running, give it a chance to abort pending 266191783Srmacklem * locks. 267191783Srmacklem */ 268214048Srmacklem if (nfs_reclaim_p != NULL) 269214048Srmacklem nfs_reclaim_p(ap); 270191783Srmacklem 271191783Srmacklem /* 272191783Srmacklem * Destroy the vm object and flush associated pages. 273191783Srmacklem */ 274191783Srmacklem vnode_destroy_vobject(vp); 275191783Srmacklem 276237200Srmacklem if (NFS_ISV4(vp) && vp->v_type == VREG) 277237200Srmacklem /* 278237200Srmacklem * We can now safely close any remaining NFSv4 Opens for 279237200Srmacklem * this file. Most opens will have already been closed by 280237200Srmacklem * ncl_inactive(), but there are cases where it is not 281237200Srmacklem * called, so we need to do it again here. 282237200Srmacklem */ 283237200Srmacklem (void) nfsrpc_close(vp, 1, ap->a_td); 284237200Srmacklem 285191783Srmacklem vfs_hash_remove(vp); 286191783Srmacklem 287191783Srmacklem /* 288191783Srmacklem * Call nfscl_reclaimnode() to save attributes in the delegation, 289191783Srmacklem * as required. 290191783Srmacklem */ 291191783Srmacklem if (vp->v_type == VREG) 292191783Srmacklem nfscl_reclaimnode(vp); 293191783Srmacklem 294191783Srmacklem /* 295191783Srmacklem * Free up any directory cookie structures and 296191783Srmacklem * large file handle structures that might be associated with 297191783Srmacklem * this nfs node. 298191783Srmacklem */ 299191783Srmacklem if (vp->v_type == VDIR) { 300191783Srmacklem dp = LIST_FIRST(&np->n_cookies); 301191783Srmacklem while (dp) { 302191783Srmacklem dp2 = dp; 303191783Srmacklem dp = LIST_NEXT(dp, ndm_list); 304191783Srmacklem FREE((caddr_t)dp2, M_NFSDIROFF); 305191783Srmacklem } 306191783Srmacklem } 307235332Srmacklem if (np->n_writecred != NULL) 308235332Srmacklem crfree(np->n_writecred); 309191783Srmacklem FREE((caddr_t)np->n_fhp, M_NFSFH); 310191783Srmacklem if (np->n_v4 != NULL) 311191783Srmacklem FREE((caddr_t)np->n_v4, M_NFSV4NODE); 312191783Srmacklem mtx_destroy(&np->n_mtx); 313191783Srmacklem uma_zfree(newnfsnode_zone, vp->v_data); 314191783Srmacklem vp->v_data = NULL; 315191783Srmacklem return (0); 316191783Srmacklem} 317191783Srmacklem 318191783Srmacklem/* 319191783Srmacklem * Invalidate both the access and attribute caches for this vnode. 320191783Srmacklem */ 321191783Srmacklemvoid 322191783Srmacklemncl_invalcaches(struct vnode *vp) 323191783Srmacklem{ 324191783Srmacklem struct nfsnode *np = VTONFS(vp); 325191783Srmacklem int i; 326191783Srmacklem 327191783Srmacklem mtx_lock(&np->n_mtx); 328191783Srmacklem for (i = 0; i < NFS_ACCESSCACHESIZE; i++) 329191783Srmacklem np->n_accesscache[i].stamp = 0; 330223280Srmacklem KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp); 331191783Srmacklem np->n_attrstamp = 0; 332223280Srmacklem KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 333191783Srmacklem mtx_unlock(&np->n_mtx); 334191783Srmacklem} 335191783Srmacklem 336