1 /* $OpenBSD: nfs_node.c,v 1.64 2016/03/19 12:04:16 natano Exp $ */ 2 /* $NetBSD: nfs_node.c,v 1.16 1996/02/18 11:53:42 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95 36 */ 37 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/timeout.h> 42 #include <sys/mount.h> 43 #include <sys/namei.h> 44 #include <sys/vnode.h> 45 #include <sys/lock.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/pool.h> 49 #include <sys/rwlock.h> 50 #include <sys/queue.h> 51 52 #include <nfs/rpcv2.h> 53 #include <nfs/nfsproto.h> 54 #include <nfs/nfsnode.h> 55 #include <nfs/nfsmount.h> 56 #include <nfs/nfs_var.h> 57 58 struct pool nfs_node_pool; 59 extern int prtactive; 60 61 struct rwlock nfs_hashlock = RWLOCK_INITIALIZER("nfshshlk"); 62 63 /* XXX */ 64 extern struct vops nfs_vops; 65 66 /* filehandle to node lookup. */ 67 static __inline int 68 nfsnode_cmp(const struct nfsnode *a, const struct nfsnode *b) 69 { 70 if (a->n_fhsize != b->n_fhsize) 71 return (a->n_fhsize - b->n_fhsize); 72 return (memcmp(a->n_fhp, b->n_fhp, a->n_fhsize)); 73 } 74 75 RB_PROTOTYPE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp); 76 RB_GENERATE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp); 77 78 /* 79 * Look up a vnode/nfsnode by file handle and store the pointer in *npp. 80 * Callers must check for mount points!! 81 * An error number is returned. 82 */ 83 int 84 nfs_nget(struct mount *mnt, nfsfh_t *fh, int fhsize, struct nfsnode **npp) 85 { 86 struct nfsmount *nmp; 87 struct nfsnode *np, find, *np2; 88 struct vnode *vp, *nvp; 89 struct proc *p = curproc; /* XXX */ 90 int error; 91 92 nmp = VFSTONFS(mnt); 93 94 loop: 95 rw_enter_write(&nfs_hashlock); 96 find.n_fhp = fh; 97 find.n_fhsize = fhsize; 98 np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find); 99 if (np != NULL) { 100 rw_exit_write(&nfs_hashlock); 101 vp = NFSTOV(np); 102 error = vget(vp, LK_EXCLUSIVE, p); 103 if (error) 104 goto loop; 105 *npp = np; 106 return (0); 107 } 108 109 /* 110 * getnewvnode() could recycle a vnode, potentially formerly 111 * owned by NFS. This will cause a VOP_RECLAIM() to happen, 112 * which will cause recursive locking, so we unlock before 113 * calling getnewvnode() lock again afterwards, but must check 114 * to see if this nfsnode has been added while we did not hold 115 * the lock. 116 */ 117 rw_exit_write(&nfs_hashlock); 118 error = getnewvnode(VT_NFS, mnt, &nfs_vops, &nvp); 119 /* note that we don't have this vnode set up completely yet */ 120 rw_enter_write(&nfs_hashlock); 121 if (error) { 122 *npp = NULL; 123 rw_exit_write(&nfs_hashlock); 124 return (error); 125 } 126 nvp->v_flag |= VLARVAL; 127 np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find); 128 if (np != NULL) { 129 vgone(nvp); 130 rw_exit_write(&nfs_hashlock); 131 goto loop; 132 } 133 134 vp = nvp; 135 np = pool_get(&nfs_node_pool, PR_WAITOK | PR_ZERO); 136 vp->v_data = np; 137 /* we now have an nfsnode on this vnode */ 138 vp->v_flag &= ~VLARVAL; 139 np->n_vnode = vp; 140 141 rw_init(&np->n_commitlock, "nfs_commitlk"); 142 143 /* 144 * Are we getting the root? If so, make sure the vnode flags 145 * are correct 146 */ 147 if ((fhsize == nmp->nm_fhsize) && !bcmp(fh, nmp->nm_fh, fhsize)) { 148 if (vp->v_type == VNON) 149 vp->v_type = VDIR; 150 vp->v_flag |= VROOT; 151 } 152 153 np->n_fhp = &np->n_fh; 154 bcopy(fh, np->n_fhp, fhsize); 155 np->n_fhsize = fhsize; 156 np2 = RB_INSERT(nfs_nodetree, &nmp->nm_ntree, np); 157 KASSERT(np2 == NULL); 158 np->n_accstamp = -1; 159 rw_exit(&nfs_hashlock); 160 *npp = np; 161 162 return (0); 163 } 164 165 int 166 nfs_inactive(void *v) 167 { 168 struct vop_inactive_args *ap = v; 169 struct nfsnode *np; 170 struct sillyrename *sp; 171 172 #ifdef DIAGNOSTIC 173 if (prtactive && ap->a_vp->v_usecount != 0) 174 vprint("nfs_inactive: pushing active", ap->a_vp); 175 #endif 176 if (ap->a_vp->v_flag & VLARVAL) 177 /* 178 * vnode was incompletely set up, just return 179 * as we are throwing it away. 180 */ 181 return(0); 182 #ifdef DIAGNOSTIC 183 if (ap->a_vp->v_data == NULL) 184 panic("NULL v_data (no nfsnode set up?) in vnode %p", 185 ap->a_vp); 186 #endif 187 np = VTONFS(ap->a_vp); 188 if (ap->a_vp->v_type != VDIR) { 189 sp = np->n_sillyrename; 190 np->n_sillyrename = NULL; 191 } else 192 sp = NULL; 193 if (sp) { 194 /* 195 * Remove the silly file that was rename'd earlier 196 */ 197 nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, curproc); 198 nfs_removeit(sp); 199 crfree(sp->s_cred); 200 vrele(sp->s_dvp); 201 free(sp, M_NFSREQ, sizeof(*sp)); 202 } 203 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT); 204 205 VOP_UNLOCK(ap->a_vp, ap->a_p); 206 return (0); 207 } 208 209 /* 210 * Reclaim an nfsnode so that it can be used for other purposes. 211 */ 212 int 213 nfs_reclaim(void *v) 214 { 215 struct vop_reclaim_args *ap = v; 216 struct vnode *vp = ap->a_vp; 217 struct nfsmount *nmp; 218 struct nfsnode *np = VTONFS(vp); 219 220 #ifdef DIAGNOSTIC 221 if (prtactive && vp->v_usecount != 0) 222 vprint("nfs_reclaim: pushing active", vp); 223 #endif 224 if (ap->a_vp->v_flag & VLARVAL) 225 /* 226 * vnode was incompletely set up, just return 227 * as we are throwing it away. 228 */ 229 return(0); 230 #ifdef DIAGNOSTIC 231 if (ap->a_vp->v_data == NULL) 232 panic("NULL v_data (no nfsnode set up?) in vnode %p", 233 ap->a_vp); 234 #endif 235 nmp = VFSTONFS(vp->v_mount); 236 rw_enter_write(&nfs_hashlock); 237 RB_REMOVE(nfs_nodetree, &nmp->nm_ntree, np); 238 rw_exit_write(&nfs_hashlock); 239 240 if (np->n_rcred) 241 crfree(np->n_rcred); 242 if (np->n_wcred) 243 crfree(np->n_wcred); 244 245 cache_purge(vp); 246 pool_put(&nfs_node_pool, vp->v_data); 247 vp->v_data = NULL; 248 249 return (0); 250 } 251