1*3be2222bSpgoyette /* $NetBSD: nfs_clnode.c,v 1.3 2016/12/13 22:17:33 pgoyette Exp $ */
26ca35587Sdholland /*-
36ca35587Sdholland * Copyright (c) 1989, 1993
46ca35587Sdholland * The Regents of the University of California. All rights reserved.
56ca35587Sdholland *
66ca35587Sdholland * This code is derived from software contributed to Berkeley by
76ca35587Sdholland * Rick Macklem at The University of Guelph.
86ca35587Sdholland *
96ca35587Sdholland * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland * modification, are permitted provided that the following conditions
116ca35587Sdholland * are met:
126ca35587Sdholland * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland * notice, this list of conditions and the following disclaimer.
146ca35587Sdholland * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland * notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland * documentation and/or other materials provided with the distribution.
176ca35587Sdholland * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland * may be used to endorse or promote products derived from this software
196ca35587Sdholland * without specific prior written permission.
206ca35587Sdholland *
216ca35587Sdholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland * SUCH DAMAGE.
326ca35587Sdholland *
336ca35587Sdholland * from nfs_node.c 8.6 (Berkeley) 5/22/95
346ca35587Sdholland */
356ca35587Sdholland
366ca35587Sdholland #include <sys/cdefs.h>
37e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clnode.c 302210 2016-06-26 14:18:28Z kib "); */
38*3be2222bSpgoyette __RCSID("$NetBSD: nfs_clnode.c,v 1.3 2016/12/13 22:17:33 pgoyette Exp $");
396ca35587Sdholland
406ca35587Sdholland #include <sys/param.h>
416ca35587Sdholland #include <sys/systm.h>
426ca35587Sdholland #include <sys/fcntl.h>
436ca35587Sdholland #include <sys/lock.h>
446ca35587Sdholland #include <sys/malloc.h>
456ca35587Sdholland #include <sys/mount.h>
466ca35587Sdholland #include <sys/namei.h>
476ca35587Sdholland #include <sys/proc.h>
486ca35587Sdholland #include <sys/socket.h>
496ca35587Sdholland #include <sys/sysctl.h>
506ca35587Sdholland #include <sys/taskqueue.h>
516ca35587Sdholland #include <sys/vnode.h>
526ca35587Sdholland
536ca35587Sdholland #include <vm/uma.h>
546ca35587Sdholland
55*3be2222bSpgoyette #include <fs/nfs/common/nfsport.h>
56*3be2222bSpgoyette #include <fs/nfs/client/nfsnode.h>
57*3be2222bSpgoyette #include <fs/nfs/client/nfsmount.h>
58*3be2222bSpgoyette #include <fs/nfs/client/nfs.h>
59*3be2222bSpgoyette #include <fs/nfs/client/nfs_kdtrace.h>
606ca35587Sdholland
61*3be2222bSpgoyette #include <fs/nfs/common/nfs_lock.h>
626ca35587Sdholland
636ca35587Sdholland extern struct vop_vector newnfs_vnodeops;
646ca35587Sdholland extern struct buf_ops buf_ops_newnfs;
656ca35587Sdholland MALLOC_DECLARE(M_NEWNFSREQ);
666ca35587Sdholland
676ca35587Sdholland uma_zone_t newnfsnode_zone;
686ca35587Sdholland
69e81f0ea2Spgoyette const char nfs_vnode_tag[] = "nfs";
70e81f0ea2Spgoyette
7101ce88d0Spgoyette static void nfs_freesillyrename(void *arg);
726ca35587Sdholland
736ca35587Sdholland void
ncl_nhinit(void)746ca35587Sdholland ncl_nhinit(void)
756ca35587Sdholland {
766ca35587Sdholland
776ca35587Sdholland newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
786ca35587Sdholland NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
796ca35587Sdholland }
806ca35587Sdholland
816ca35587Sdholland void
ncl_nhuninit(void)826ca35587Sdholland ncl_nhuninit(void)
836ca35587Sdholland {
846ca35587Sdholland uma_zdestroy(newnfsnode_zone);
856ca35587Sdholland }
866ca35587Sdholland
876ca35587Sdholland /*
886ca35587Sdholland * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
896ca35587Sdholland * function is going to be used to get Regular Files, code must be added
906ca35587Sdholland * to fill in the "struct nfsv4node".
916ca35587Sdholland * Look up a vnode/nfsnode by file handle.
926ca35587Sdholland * Callers must check for mount points!!
936ca35587Sdholland * In all cases, a pointer to a
946ca35587Sdholland * nfsnode structure is returned.
956ca35587Sdholland */
966ca35587Sdholland int
ncl_nget(struct mount * mntp,u_int8_t * fhp,int fhsize,struct nfsnode ** npp,int lkflags)976ca35587Sdholland ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
986ca35587Sdholland int lkflags)
996ca35587Sdholland {
1006ca35587Sdholland struct thread *td = curthread; /* XXX */
1016ca35587Sdholland struct nfsnode *np;
1026ca35587Sdholland struct vnode *vp;
1036ca35587Sdholland struct vnode *nvp;
1046ca35587Sdholland int error;
1056ca35587Sdholland u_int hash;
1066ca35587Sdholland struct nfsmount *nmp;
1076ca35587Sdholland struct nfsfh *nfhp;
1086ca35587Sdholland
1096ca35587Sdholland nmp = VFSTONFS(mntp);
1106ca35587Sdholland *npp = NULL;
1116ca35587Sdholland
1126ca35587Sdholland hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
1136ca35587Sdholland
1146ca35587Sdholland MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
1156ca35587Sdholland M_NFSFH, M_WAITOK);
1166ca35587Sdholland bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
1176ca35587Sdholland nfhp->nfh_len = fhsize;
1186ca35587Sdholland error = vfs_hash_get(mntp, hash, lkflags,
1196ca35587Sdholland td, &nvp, newnfs_vncmpf, nfhp);
1206ca35587Sdholland FREE(nfhp, M_NFSFH);
1216ca35587Sdholland if (error)
1226ca35587Sdholland return (error);
1236ca35587Sdholland if (nvp != NULL) {
1246ca35587Sdholland *npp = VTONFS(nvp);
1256ca35587Sdholland return (0);
1266ca35587Sdholland }
1276ca35587Sdholland np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
1286ca35587Sdholland
129e81f0ea2Spgoyette error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
1306ca35587Sdholland if (error) {
1316ca35587Sdholland uma_zfree(newnfsnode_zone, np);
1326ca35587Sdholland return (error);
1336ca35587Sdholland }
1346ca35587Sdholland vp = nvp;
1356ca35587Sdholland KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
1366ca35587Sdholland vp->v_bufobj.bo_ops = &buf_ops_newnfs;
1376ca35587Sdholland vp->v_data = np;
1386ca35587Sdholland np->n_vnode = vp;
1396ca35587Sdholland /*
1406ca35587Sdholland * Initialize the mutex even if the vnode is going to be a loser.
1416ca35587Sdholland * This simplifies the logic in reclaim, which can then unconditionally
1426ca35587Sdholland * destroy the mutex (in the case of the loser, or if hash_insert
1436ca35587Sdholland * happened to return an error no special casing is needed).
1446ca35587Sdholland */
1456ca35587Sdholland mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
1466ca35587Sdholland /*
1476ca35587Sdholland * NFS supports recursive and shared locking.
1486ca35587Sdholland */
1496ca35587Sdholland lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
1506ca35587Sdholland VN_LOCK_AREC(vp);
1516ca35587Sdholland VN_LOCK_ASHARE(vp);
1526ca35587Sdholland /*
1536ca35587Sdholland * Are we getting the root? If so, make sure the vnode flags
1546ca35587Sdholland * are correct
1556ca35587Sdholland */
1566ca35587Sdholland if ((fhsize == nmp->nm_fhsize) &&
1576ca35587Sdholland !bcmp(fhp, nmp->nm_fh, fhsize)) {
1586ca35587Sdholland if (vp->v_type == VNON)
1596ca35587Sdholland vp->v_type = VDIR;
1606ca35587Sdholland vp->v_vflag |= VV_ROOT;
1616ca35587Sdholland }
1626ca35587Sdholland
1636ca35587Sdholland MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
1646ca35587Sdholland M_NFSFH, M_WAITOK);
1656ca35587Sdholland bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
1666ca35587Sdholland np->n_fhp->nfh_len = fhsize;
1676ca35587Sdholland error = insmntque(vp, mntp);
1686ca35587Sdholland if (error != 0) {
1696ca35587Sdholland *npp = NULL;
1706ca35587Sdholland FREE((caddr_t)np->n_fhp, M_NFSFH);
1716ca35587Sdholland mtx_destroy(&np->n_mtx);
1726ca35587Sdholland uma_zfree(newnfsnode_zone, np);
1736ca35587Sdholland return (error);
1746ca35587Sdholland }
1756ca35587Sdholland error = vfs_hash_insert(vp, hash, lkflags,
1766ca35587Sdholland td, &nvp, newnfs_vncmpf, np->n_fhp);
1776ca35587Sdholland if (error)
1786ca35587Sdholland return (error);
1796ca35587Sdholland if (nvp != NULL) {
1806ca35587Sdholland *npp = VTONFS(nvp);
1816ca35587Sdholland /* vfs_hash_insert() vput()'s the losing vnode */
1826ca35587Sdholland return (0);
1836ca35587Sdholland }
1846ca35587Sdholland *npp = np;
1856ca35587Sdholland
1866ca35587Sdholland return (0);
1876ca35587Sdholland }
1886ca35587Sdholland
1896ca35587Sdholland /*
1906ca35587Sdholland * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
1916ca35587Sdholland * deadlock because of a LOR when vrele() locks the directory vnode.
1926ca35587Sdholland */
1936ca35587Sdholland static void
nfs_freesillyrename(void * arg)19401ce88d0Spgoyette nfs_freesillyrename(void *arg)
1956ca35587Sdholland {
1966ca35587Sdholland struct sillyrename *sp;
1976ca35587Sdholland
1986ca35587Sdholland sp = arg;
1996ca35587Sdholland vrele(sp->s_dvp);
2006ca35587Sdholland free(sp, M_NEWNFSREQ);
2016ca35587Sdholland }
2026ca35587Sdholland
203e81f0ea2Spgoyette static void
ncl_releasesillyrename(struct vnode * vp,struct thread * td)204e81f0ea2Spgoyette ncl_releasesillyrename(struct vnode *vp, struct thread *td)
2056ca35587Sdholland {
2066ca35587Sdholland struct nfsnode *np;
2076ca35587Sdholland struct sillyrename *sp;
2086ca35587Sdholland
209e81f0ea2Spgoyette ASSERT_VOP_ELOCKED(vp, "releasesillyrename");
2106ca35587Sdholland np = VTONFS(vp);
211e81f0ea2Spgoyette mtx_assert(&np->n_mtx, MA_OWNED);
212e81f0ea2Spgoyette if (vp->v_type != VDIR) {
213e81f0ea2Spgoyette sp = np->n_sillyrename;
214e81f0ea2Spgoyette np->n_sillyrename = NULL;
215e81f0ea2Spgoyette } else
216e81f0ea2Spgoyette sp = NULL;
217e81f0ea2Spgoyette if (sp != NULL) {
218e81f0ea2Spgoyette mtx_unlock(&np->n_mtx);
219e81f0ea2Spgoyette (void) ncl_vinvalbuf(vp, 0, td, 1);
220e81f0ea2Spgoyette /*
221e81f0ea2Spgoyette * Remove the silly file that was rename'd earlier
222e81f0ea2Spgoyette */
223e81f0ea2Spgoyette ncl_removeit(sp, vp);
224e81f0ea2Spgoyette crfree(sp->s_cred);
22501ce88d0Spgoyette sysmon_task_queue_sched(0, nfs_freesillyrename, sp);
226e81f0ea2Spgoyette mtx_lock(&np->n_mtx);
227e81f0ea2Spgoyette }
228e81f0ea2Spgoyette }
229e81f0ea2Spgoyette
230e81f0ea2Spgoyette int
ncl_inactive(struct vop_inactive_args * ap)231e81f0ea2Spgoyette ncl_inactive(struct vop_inactive_args *ap)
232e81f0ea2Spgoyette {
233e81f0ea2Spgoyette struct vnode *vp = ap->a_vp;
234e81f0ea2Spgoyette struct nfsnode *np;
235e81f0ea2Spgoyette boolean_t retv;
2366ca35587Sdholland
2376ca35587Sdholland if (NFS_ISV4(vp) && vp->v_type == VREG) {
2386ca35587Sdholland /*
2396ca35587Sdholland * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
2406ca35587Sdholland * Close operations are delayed until now. Any dirty
2416ca35587Sdholland * buffers/pages must be flushed before the close, so that the
2426ca35587Sdholland * stateid is available for the writes.
2436ca35587Sdholland */
2446ca35587Sdholland if (vp->v_object != NULL) {
2456ca35587Sdholland VM_OBJECT_WLOCK(vp->v_object);
2466ca35587Sdholland retv = vm_object_page_clean(vp->v_object, 0, 0,
2476ca35587Sdholland OBJPC_SYNC);
2486ca35587Sdholland VM_OBJECT_WUNLOCK(vp->v_object);
2496ca35587Sdholland } else
2506ca35587Sdholland retv = TRUE;
2516ca35587Sdholland if (retv == TRUE) {
2526ca35587Sdholland (void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
2536ca35587Sdholland (void)nfsrpc_close(vp, 1, ap->a_td);
2546ca35587Sdholland }
2556ca35587Sdholland }
2566ca35587Sdholland
257e81f0ea2Spgoyette np = VTONFS(vp);
2586ca35587Sdholland mtx_lock(&np->n_mtx);
259e81f0ea2Spgoyette ncl_releasesillyrename(vp, ap->a_td);
260e81f0ea2Spgoyette
2616ca35587Sdholland /*
262e81f0ea2Spgoyette * NMODIFIED means that there might be dirty/stale buffers
263e81f0ea2Spgoyette * associated with the NFS vnode. None of the other flags are
264e81f0ea2Spgoyette * meaningful after the vnode is unused.
2656ca35587Sdholland */
2666ca35587Sdholland np->n_flag &= NMODIFIED;
2676ca35587Sdholland mtx_unlock(&np->n_mtx);
2686ca35587Sdholland return (0);
2696ca35587Sdholland }
2706ca35587Sdholland
2716ca35587Sdholland /*
2726ca35587Sdholland * Reclaim an nfsnode so that it can be used for other purposes.
2736ca35587Sdholland */
2746ca35587Sdholland int
ncl_reclaim(struct vop_reclaim_args * ap)2756ca35587Sdholland ncl_reclaim(struct vop_reclaim_args *ap)
2766ca35587Sdholland {
2776ca35587Sdholland struct vnode *vp = ap->a_vp;
2786ca35587Sdholland struct nfsnode *np = VTONFS(vp);
2796ca35587Sdholland struct nfsdmap *dp, *dp2;
2806ca35587Sdholland
2816ca35587Sdholland /*
2826ca35587Sdholland * If the NLM is running, give it a chance to abort pending
2836ca35587Sdholland * locks.
2846ca35587Sdholland */
2856ca35587Sdholland if (nfs_reclaim_p != NULL)
2866ca35587Sdholland nfs_reclaim_p(ap);
2876ca35587Sdholland
288e81f0ea2Spgoyette mtx_lock(&np->n_mtx);
289e81f0ea2Spgoyette ncl_releasesillyrename(vp, ap->a_td);
290e81f0ea2Spgoyette mtx_unlock(&np->n_mtx);
291e81f0ea2Spgoyette
2926ca35587Sdholland /*
2936ca35587Sdholland * Destroy the vm object and flush associated pages.
2946ca35587Sdholland */
2956ca35587Sdholland vnode_destroy_vobject(vp);
2966ca35587Sdholland
2976ca35587Sdholland if (NFS_ISV4(vp) && vp->v_type == VREG)
2986ca35587Sdholland /*
2996ca35587Sdholland * We can now safely close any remaining NFSv4 Opens for
3006ca35587Sdholland * this file. Most opens will have already been closed by
3016ca35587Sdholland * ncl_inactive(), but there are cases where it is not
3026ca35587Sdholland * called, so we need to do it again here.
3036ca35587Sdholland */
3046ca35587Sdholland (void) nfsrpc_close(vp, 1, ap->a_td);
3056ca35587Sdholland
3066ca35587Sdholland vfs_hash_remove(vp);
3076ca35587Sdholland
3086ca35587Sdholland /*
3096ca35587Sdholland * Call nfscl_reclaimnode() to save attributes in the delegation,
3106ca35587Sdholland * as required.
3116ca35587Sdholland */
3126ca35587Sdholland if (vp->v_type == VREG)
3136ca35587Sdholland nfscl_reclaimnode(vp);
3146ca35587Sdholland
3156ca35587Sdholland /*
3166ca35587Sdholland * Free up any directory cookie structures and
3176ca35587Sdholland * large file handle structures that might be associated with
3186ca35587Sdholland * this nfs node.
3196ca35587Sdholland */
3206ca35587Sdholland if (vp->v_type == VDIR) {
3216ca35587Sdholland dp = LIST_FIRST(&np->n_cookies);
3226ca35587Sdholland while (dp) {
3236ca35587Sdholland dp2 = dp;
3246ca35587Sdholland dp = LIST_NEXT(dp, ndm_list);
3256ca35587Sdholland FREE((caddr_t)dp2, M_NFSDIROFF);
3266ca35587Sdholland }
3276ca35587Sdholland }
3286ca35587Sdholland if (np->n_writecred != NULL)
3296ca35587Sdholland crfree(np->n_writecred);
3306ca35587Sdholland FREE((caddr_t)np->n_fhp, M_NFSFH);
3316ca35587Sdholland if (np->n_v4 != NULL)
3326ca35587Sdholland FREE((caddr_t)np->n_v4, M_NFSV4NODE);
3336ca35587Sdholland mtx_destroy(&np->n_mtx);
3346ca35587Sdholland uma_zfree(newnfsnode_zone, vp->v_data);
3356ca35587Sdholland vp->v_data = NULL;
3366ca35587Sdholland return (0);
3376ca35587Sdholland }
3386ca35587Sdholland
3396ca35587Sdholland /*
3406ca35587Sdholland * Invalidate both the access and attribute caches for this vnode.
3416ca35587Sdholland */
3426ca35587Sdholland void
ncl_invalcaches(struct vnode * vp)3436ca35587Sdholland ncl_invalcaches(struct vnode *vp)
3446ca35587Sdholland {
3456ca35587Sdholland struct nfsnode *np = VTONFS(vp);
3466ca35587Sdholland int i;
3476ca35587Sdholland
3486ca35587Sdholland mtx_lock(&np->n_mtx);
3496ca35587Sdholland for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
3506ca35587Sdholland np->n_accesscache[i].stamp = 0;
3516ca35587Sdholland KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
3526ca35587Sdholland np->n_attrstamp = 0;
3536ca35587Sdholland KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3546ca35587Sdholland mtx_unlock(&np->n_mtx);
3556ca35587Sdholland }
356