1 /* $NetBSD: nfs_node.c,v 1.82 2006/01/02 21:43:24 yamt 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.82 2006/01/02 21:43:24 yamt Exp $"); 39 40 #include "opt_nfs.h" 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 #include <sys/pool.h> 51 #include <sys/lock.h> 52 #include <sys/hash.h> 53 54 #include <nfs/rpcv2.h> 55 #include <nfs/nfsproto.h> 56 #include <nfs/nfs.h> 57 #include <nfs/nfsnode.h> 58 #include <nfs/nfsmount.h> 59 #include <nfs/nqnfs.h> 60 #include <nfs/nfs_var.h> 61 62 struct nfsnodehashhead *nfsnodehashtbl; 63 u_long nfsnodehash; 64 struct lock nfs_hashlock; 65 66 POOL_INIT(nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl", 67 &pool_allocator_nointr); 68 POOL_INIT(nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl", 69 &pool_allocator_nointr); 70 71 MALLOC_DEFINE(M_NFSBIGFH, "NFS bigfh", "NFS big filehandle"); 72 MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS vnode private part"); 73 74 extern int prtactive; 75 76 #define nfs_hash(x,y) hash32_buf((x), (y), HASH32_BUF_INIT) 77 78 void nfs_gop_size(struct vnode *, off_t, off_t *, int); 79 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *); 80 int nfs_gop_write(struct vnode *, struct vm_page **, int, int); 81 82 static const struct genfs_ops nfs_genfsops = { 83 .gop_size = nfs_gop_size, 84 .gop_alloc = nfs_gop_alloc, 85 .gop_write = nfs_gop_write, 86 }; 87 88 /* 89 * Initialize hash links for nfsnodes 90 * and build nfsnode free list. 91 */ 92 void 93 nfs_nhinit() 94 { 95 96 nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, 97 M_WAITOK, &nfsnodehash); 98 lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0); 99 } 100 101 /* 102 * Reinitialize inode hash table. 103 */ 104 105 void 106 nfs_nhreinit() 107 { 108 struct nfsnode *np; 109 struct nfsnodehashhead *oldhash, *hash; 110 u_long oldmask, mask, val; 111 int i; 112 113 hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK, 114 &mask); 115 116 lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL); 117 oldhash = nfsnodehashtbl; 118 oldmask = nfsnodehash; 119 nfsnodehashtbl = hash; 120 nfsnodehash = mask; 121 for (i = 0; i <= oldmask; i++) { 122 while ((np = LIST_FIRST(&oldhash[i])) != NULL) { 123 LIST_REMOVE(np, n_hash); 124 val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize)); 125 LIST_INSERT_HEAD(&hash[val], np, n_hash); 126 } 127 } 128 lockmgr(&nfs_hashlock, LK_RELEASE, NULL); 129 hashdone(oldhash, M_NFSNODE); 130 } 131 132 /* 133 * Free resources previoslu allocated in nfs_nhinit(). 134 */ 135 void 136 nfs_nhdone() 137 { 138 hashdone(nfsnodehashtbl, M_NFSNODE); 139 pool_destroy(&nfs_node_pool); 140 pool_destroy(&nfs_vattr_pool); 141 } 142 143 /* 144 * Look up a vnode/nfsnode by file handle. 145 * Callers must check for mount points!! 146 * In all cases, a pointer to a 147 * nfsnode structure is returned. 148 */ 149 int 150 nfs_nget1(mntp, fhp, fhsize, npp, lkflags) 151 struct mount *mntp; 152 nfsfh_t *fhp; 153 int fhsize; 154 struct nfsnode **npp; 155 int lkflags; 156 { 157 struct nfsnode *np; 158 struct nfsnodehashhead *nhpp; 159 struct vnode *vp; 160 int error; 161 162 nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))]; 163 loop: 164 LIST_FOREACH(np, nhpp, n_hash) { 165 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize || 166 memcmp(fhp, np->n_fhp, fhsize)) 167 continue; 168 vp = NFSTOV(np); 169 error = vget(vp, LK_EXCLUSIVE | lkflags); 170 if (error == EBUSY) 171 return error; 172 if (error) 173 goto loop; 174 *npp = np; 175 return(0); 176 } 177 if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0)) 178 goto loop; 179 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &vp); 180 if (error) { 181 *npp = 0; 182 lockmgr(&nfs_hashlock, LK_RELEASE, 0); 183 return (error); 184 } 185 np = pool_get(&nfs_node_pool, PR_WAITOK); 186 memset(np, 0, sizeof *np); 187 vp->v_data = np; 188 np->n_vnode = vp; 189 genfs_node_init(vp, &nfs_genfsops); 190 191 /* 192 * Insert the nfsnode in the hash queue for its new file handle 193 */ 194 195 LIST_INSERT_HEAD(nhpp, np, n_hash); 196 if (fhsize > NFS_SMALLFH) { 197 np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK); 198 } else 199 np->n_fhp = &np->n_fh; 200 memcpy(np->n_fhp, fhp, fhsize); 201 np->n_fhsize = fhsize; 202 np->n_accstamp = -1; 203 np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK); 204 205 /* 206 * Initalize read/write creds to useful values. VOP_OPEN will 207 * overwrite these. 208 */ 209 np->n_rcred = curproc->p_ucred; 210 crhold(np->n_rcred); 211 np->n_wcred = curproc->p_ucred; 212 crhold(np->n_wcred); 213 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); 214 lockmgr(&nfs_hashlock, LK_RELEASE, NULL); 215 NFS_INVALIDATE_ATTRCACHE(np); 216 uvm_vnp_setsize(vp, 0); 217 *npp = np; 218 return (0); 219 } 220 221 int 222 nfs_inactive(v) 223 void *v; 224 { 225 struct vop_inactive_args /* { 226 struct vnode *a_vp; 227 struct lwp *a_l; 228 } */ *ap = v; 229 struct nfsnode *np; 230 struct sillyrename *sp; 231 struct lwp *l = ap->a_l; 232 struct vnode *vp = ap->a_vp; 233 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 234 boolean_t removed; 235 236 np = VTONFS(vp); 237 if (prtactive && vp->v_usecount != 0) 238 vprint("nfs_inactive: pushing active", vp); 239 if (vp->v_type != VDIR) { 240 sp = np->n_sillyrename; 241 np->n_sillyrename = (struct sillyrename *)0; 242 } else 243 sp = NULL; 244 if (sp != NULL) 245 nfs_vinvalbuf(vp, 0, sp->s_cred, l, 1); 246 removed = (np->n_flag & NREMOVED) != 0; 247 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED | 248 NQNFSNONCACHE | NQNFSWRITE | NEOFVALID); 249 250 if ((nmp->nm_flag & NFSMNT_NQNFS) && CIRCLEQ_NEXT(np, n_timer) != 0) { 251 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer); 252 } 253 254 if (vp->v_type == VDIR && np->n_dircache) 255 nfs_invaldircache(vp, 256 NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF); 257 258 VOP_UNLOCK(vp, 0); 259 260 /* XXXMP only kernel_lock protects vp */ 261 if (removed) 262 vrecycle(vp, NULL, l); 263 264 if (sp != NULL) { 265 int error; 266 267 /* 268 * Remove the silly file that was rename'd earlier 269 * 270 * Just in case our thread also has the parent node locked, 271 * we use LK_CANRECURSE. 272 */ 273 274 error = vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_CANRECURSE); 275 if (error || sp->s_dvp->v_data == NULL) { 276 /* XXX should recover */ 277 panic("%s: vp=%p error=%d", __func__, sp->s_dvp, error); 278 } 279 nfs_removeit(sp); 280 crfree(sp->s_cred); 281 vput(sp->s_dvp); 282 FREE(sp, M_NFSREQ); 283 } 284 285 return (0); 286 } 287 288 /* 289 * Reclaim an nfsnode so that it can be used for other purposes. 290 */ 291 int 292 nfs_reclaim(v) 293 void *v; 294 { 295 struct vop_reclaim_args /* { 296 struct vnode *a_vp; 297 } */ *ap = v; 298 struct vnode *vp = ap->a_vp; 299 struct nfsnode *np = VTONFS(vp); 300 301 if (prtactive && vp->v_usecount != 0) 302 vprint("nfs_reclaim: pushing active", vp); 303 304 LIST_REMOVE(np, n_hash); 305 306 /* 307 * Free up any directory cookie structures and 308 * large file handle structures that might be associated with 309 * this nfs node. 310 */ 311 if (vp->v_type == VDIR && np->n_dircache) 312 hashdone(np->n_dircache, M_NFSDIROFF); 313 KASSERT(np->n_dirgens == NULL); 314 315 if (np->n_fhsize > NFS_SMALLFH) 316 free(np->n_fhp, M_NFSBIGFH); 317 318 pool_put(&nfs_vattr_pool, np->n_vattr); 319 if (np->n_rcred) 320 crfree(np->n_rcred); 321 322 if (np->n_wcred) 323 crfree(np->n_wcred); 324 325 cache_purge(vp); 326 pool_put(&nfs_node_pool, vp->v_data); 327 vp->v_data = NULL; 328 return (0); 329 } 330 331 void 332 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags) 333 { 334 KASSERT(flags & (GOP_SIZE_READ | GOP_SIZE_WRITE)); 335 KASSERT((flags & (GOP_SIZE_READ | GOP_SIZE_WRITE)) 336 != (GOP_SIZE_READ | GOP_SIZE_WRITE)); 337 *eobp = MAX(size, vp->v_size); 338 } 339 340 int 341 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags, 342 struct ucred *cred) 343 { 344 return 0; 345 } 346 347 int 348 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags) 349 { 350 int i; 351 352 for (i = 0; i < npages; i++) { 353 pmap_page_protect(pgs[i], VM_PROT_READ); 354 } 355 return genfs_gop_write(vp, pgs, npages, flags); 356 } 357