1 /* $NetBSD: nfs_node.c,v 1.19 1997/02/22 02:45:48 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95 39 */ 40 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/mount.h> 46 #include <sys/namei.h> 47 #include <sys/vnode.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 51 #include <nfs/rpcv2.h> 52 #include <nfs/nfsproto.h> 53 #include <nfs/nfs.h> 54 #include <nfs/nfsnode.h> 55 #include <nfs/nfsmount.h> 56 #include <nfs/nqnfs.h> 57 #include <nfs/nfs_var.h> 58 59 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl; 60 u_long nfsnodehash; 61 62 #define TRUE 1 63 #define FALSE 0 64 65 /* 66 * Initialize hash links for nfsnodes 67 * and build nfsnode free list. 68 */ 69 void 70 nfs_nhinit() 71 { 72 73 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash); 74 } 75 76 /* 77 * Compute an entry in the NFS hash table structure 78 */ 79 u_long 80 nfs_hash(fhp, fhsize) 81 register nfsfh_t *fhp; 82 int fhsize; 83 { 84 register u_char *fhpp; 85 register u_long fhsum; 86 register int i; 87 88 fhpp = &fhp->fh_bytes[0]; 89 fhsum = 0; 90 for (i = 0; i < fhsize; i++) 91 fhsum += *fhpp++; 92 return (fhsum); 93 } 94 95 /* 96 * Look up a vnode/nfsnode by file handle. 97 * Callers must check for mount points!! 98 * In all cases, a pointer to a 99 * nfsnode structure is returned. 100 */ 101 int 102 nfs_nget(mntp, fhp, fhsize, npp) 103 struct mount *mntp; 104 register nfsfh_t *fhp; 105 int fhsize; 106 struct nfsnode **npp; 107 { 108 #ifdef Lite2_integrated 109 struct proc *p = curproc; /* XXX */ 110 #endif 111 register struct nfsnode *np; 112 struct nfsnodehashhead *nhpp; 113 register struct vnode *vp; 114 extern int (**nfsv2_vnodeop_p)__P((void *)); 115 struct vnode *nvp; 116 int error; 117 118 nhpp = NFSNOHASH(nfs_hash(fhp, fhsize)); 119 loop: 120 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) { 121 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize || 122 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) 123 continue; 124 vp = NFSTOV(np); 125 #ifdef Lite2_integrated 126 if (vget(vp, LK_EXCLUSIVE, p)) 127 #else 128 if (vget(vp, 1)) 129 #endif 130 goto loop; 131 *npp = np; 132 return(0); 133 } 134 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp); 135 if (error) { 136 *npp = 0; 137 return (error); 138 } 139 vp = nvp; 140 MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK); 141 bzero((caddr_t)np, sizeof *np); 142 vp->v_data = np; 143 np->n_vnode = vp; 144 /* 145 * Insert the nfsnode in the hash queue for its new file handle 146 */ 147 LIST_INSERT_HEAD(nhpp, np, n_hash); 148 if (fhsize > NFS_SMALLFH) { 149 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK); 150 } else 151 np->n_fhp = &np->n_fh; 152 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize); 153 np->n_fhsize = fhsize; 154 *npp = np; 155 return (0); 156 } 157 158 int 159 nfs_inactive(v) 160 void *v; 161 { 162 struct vop_inactive_args /* { 163 struct vnode *a_vp; 164 #ifdef Lite2_integrated 165 struct proc *a_p; 166 #endif 167 } */ *ap = v; 168 register struct nfsnode *np; 169 register struct sillyrename *sp; 170 struct proc *p = curproc; /* XXX */ 171 extern int prtactive; 172 173 np = VTONFS(ap->a_vp); 174 if (prtactive && ap->a_vp->v_usecount != 0) 175 vprint("nfs_inactive: pushing active", ap->a_vp); 176 if (ap->a_vp->v_type != VDIR) { 177 sp = np->n_sillyrename; 178 np->n_sillyrename = (struct sillyrename *)0; 179 } else 180 sp = (struct sillyrename *)0; 181 #ifdef Lite2_integrated 182 VOP_UNLOCK(ap->a_vp, 0, ap->a_p); 183 #else 184 VOP_UNLOCK(ap->a_vp); 185 #endif 186 if (sp) { 187 /* 188 * If the usecount is greater than zero, then we are 189 * being inactivated by a forcible unmount and do not 190 * have to get our own reference. In the normal case, 191 * we need a reference to keep the vnode from being 192 * recycled by getnewvnode while we do the I/O 193 * associated with discarding the buffers. 194 */ 195 if (ap->a_vp->v_usecount > 0) 196 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1); 197 #ifdef Lite2_integrated 198 else if (vget(ap->a_vp, 0, p)) 199 #else 200 else if (vget(ap->a_vp, 0)) 201 #endif 202 panic("nfs_inactive: lost vnode"); 203 else { 204 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1); 205 vrele(ap->a_vp); 206 } 207 208 209 /* 210 * Remove the silly file that was rename'd earlier 211 */ 212 nfs_removeit(sp); 213 crfree(sp->s_cred); 214 vrele(sp->s_dvp); 215 FREE((caddr_t)sp, M_NFSREQ); 216 } 217 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED | 218 NQNFSNONCACHE | NQNFSWRITE); 219 return (0); 220 } 221 222 /* 223 * Reclaim an nfsnode so that it can be used for other purposes. 224 */ 225 int 226 nfs_reclaim(v) 227 void *v; 228 { 229 struct vop_reclaim_args /* { 230 struct vnode *a_vp; 231 } */ *ap = v; 232 register struct vnode *vp = ap->a_vp; 233 register struct nfsnode *np = VTONFS(vp); 234 register struct nfsmount *nmp = VFSTONFS(vp->v_mount); 235 register struct nfsdmap *dp, *dp2; 236 extern int prtactive; 237 238 if (prtactive && vp->v_usecount != 0) 239 vprint("nfs_reclaim: pushing active", vp); 240 241 LIST_REMOVE(np, n_hash); 242 243 /* 244 * For nqnfs, take it off the timer queue as required. 245 */ 246 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) { 247 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer); 248 } 249 250 /* 251 * Free up any directory cookie structures and 252 * large file handle structures that might be associated with 253 * this nfs node. 254 */ 255 if (vp->v_type == VDIR) { 256 dp = np->n_cookies.lh_first; 257 while (dp) { 258 dp2 = dp; 259 dp = dp->ndm_list.le_next; 260 FREE((caddr_t)dp2, M_NFSDIROFF); 261 } 262 } 263 if (np->n_fhsize > NFS_SMALLFH) { 264 FREE((caddr_t)np->n_fhp, M_NFSBIGFH); 265 } 266 267 cache_purge(vp); 268 FREE(vp->v_data, M_NFSNODE); 269 vp->v_data = (void *)0; 270 return (0); 271 } 272 273 #ifndef Lite2_integrated 274 /* 275 * Lock an nfsnode 276 */ 277 int 278 nfs_lock(v) 279 void *v; 280 { 281 struct vop_lock_args /* { 282 struct vnode *a_vp; 283 } */ *ap = v; 284 register struct vnode *vp = ap->a_vp; 285 286 /* 287 * Ugh, another place where interruptible mounts will get hung. 288 * If you make this sleep interruptible, then you have to fix all 289 * the VOP_LOCK() calls to expect interruptibility. 290 */ 291 while (vp->v_flag & VXLOCK) { 292 vp->v_flag |= VXWANT; 293 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0); 294 } 295 if (vp->v_tag == VT_NON) 296 return (ENOENT); 297 return (0); 298 } 299 300 /* 301 * Unlock an nfsnode 302 */ 303 int 304 nfs_unlock(v) 305 void *v; 306 { 307 #if 0 308 struct vop_unlock_args /* { 309 struct vnode *a_vp; 310 } */ *ap = v; 311 #endif 312 return (0); 313 } 314 315 /* 316 * Check for a locked nfsnode 317 */ 318 int 319 nfs_islocked(v) 320 void *v; 321 { 322 #if 0 323 struct vop_islocked_args /* { 324 struct vnode *a_vp; 325 } */ *ap = v; 326 #endif 327 return (0); 328 } 329 #endif /* Lite2_integrated */ 330