1 /* $NetBSD: nfs_node.c,v 1.103 2008/05/24 14:29:18 tron 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.103 2008/05/24 14:29:18 tron 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/pool.h> 50 #include <sys/lock.h> 51 #include <sys/hash.h> 52 #include <sys/kauth.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/nfs_var.h> 60 61 struct nfsnodehashhead *nfsnodehashtbl; 62 u_long nfsnodehash; 63 static kmutex_t nfs_hashlock; 64 65 POOL_INIT(nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl", 66 &pool_allocator_nointr, IPL_NONE); 67 POOL_INIT(nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl", 68 &pool_allocator_nointr, IPL_NONE); 69 70 MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS vnode private part"); 71 72 extern int prtactive; 73 74 #define nfs_hash(x,y) hash32_buf((x), (y), HASH32_BUF_INIT) 75 76 void nfs_gop_size(struct vnode *, off_t, off_t *, int); 77 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t); 78 int nfs_gop_write(struct vnode *, struct vm_page **, int, int); 79 80 static const struct genfs_ops nfs_genfsops = { 81 .gop_size = nfs_gop_size, 82 .gop_alloc = nfs_gop_alloc, 83 .gop_write = nfs_gop_write, 84 }; 85 86 /* 87 * Initialize hash links for nfsnodes 88 * and build nfsnode free list. 89 */ 90 void 91 nfs_nhinit() 92 { 93 94 nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, true, 95 &nfsnodehash); 96 mutex_init(&nfs_hashlock, MUTEX_DEFAULT, IPL_NONE); 97 } 98 99 /* 100 * Reinitialize inode hash table. 101 */ 102 103 void 104 nfs_nhreinit() 105 { 106 struct nfsnode *np; 107 struct nfsnodehashhead *oldhash, *hash; 108 u_long oldmask, mask, val; 109 int i; 110 111 hash = hashinit(desiredvnodes, HASH_LIST, true, &mask); 112 113 mutex_enter(&nfs_hashlock); 114 oldhash = nfsnodehashtbl; 115 oldmask = nfsnodehash; 116 nfsnodehashtbl = hash; 117 nfsnodehash = mask; 118 for (i = 0; i <= oldmask; i++) { 119 while ((np = LIST_FIRST(&oldhash[i])) != NULL) { 120 LIST_REMOVE(np, n_hash); 121 val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize)); 122 LIST_INSERT_HEAD(&hash[val], np, n_hash); 123 } 124 } 125 mutex_exit(&nfs_hashlock); 126 hashdone(oldhash, HASH_LIST, oldmask); 127 } 128 129 /* 130 * Free resources previoslu allocated in nfs_nhinit(). 131 */ 132 void 133 nfs_nhdone() 134 { 135 hashdone(nfsnodehashtbl, HASH_LIST, nfsnodehash); 136 pool_destroy(&nfs_node_pool); 137 pool_destroy(&nfs_vattr_pool); 138 mutex_destroy(&nfs_hashlock); 139 } 140 141 /* 142 * Look up a vnode/nfsnode by file handle. 143 * Callers must check for mount points!! 144 * In all cases, a pointer to a 145 * nfsnode structure is returned. 146 */ 147 int 148 nfs_nget1(mntp, fhp, fhsize, npp, lkflags) 149 struct mount *mntp; 150 nfsfh_t *fhp; 151 int fhsize; 152 struct nfsnode **npp; 153 int lkflags; 154 { 155 struct nfsnode *np, *np2; 156 struct nfsnodehashhead *nhpp; 157 struct vnode *vp; 158 int error; 159 160 nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))]; 161 loop: 162 mutex_enter(&nfs_hashlock); 163 LIST_FOREACH(np, nhpp, n_hash) { 164 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize || 165 memcmp(fhp, np->n_fhp, fhsize)) 166 continue; 167 vp = NFSTOV(np); 168 mutex_enter(&vp->v_interlock); 169 mutex_exit(&nfs_hashlock); 170 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | lkflags); 171 if (error == EBUSY) 172 return error; 173 if (error) 174 goto loop; 175 *npp = np; 176 return(0); 177 } 178 mutex_exit(&nfs_hashlock); 179 180 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &vp); 181 if (error) { 182 *npp = 0; 183 return (error); 184 } 185 np = pool_get(&nfs_node_pool, PR_WAITOK); 186 memset(np, 0, sizeof *np); 187 np->n_vnode = vp; 188 189 /* 190 * Insert the nfsnode in the hash queue for its new file handle 191 */ 192 193 if (fhsize > NFS_SMALLFH) { 194 np->n_fhp = kmem_alloc(fhsize, KM_SLEEP); 195 } else 196 np->n_fhp = &np->n_fh; 197 memcpy(np->n_fhp, fhp, fhsize); 198 np->n_fhsize = fhsize; 199 np->n_accstamp = -1; 200 np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK); 201 202 mutex_enter(&nfs_hashlock); 203 LIST_FOREACH(np2, nhpp, n_hash) { 204 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize || 205 memcmp(fhp, np2->n_fhp, fhsize)) 206 continue; 207 mutex_exit(&nfs_hashlock); 208 if (fhsize > NFS_SMALLFH) { 209 kmem_free(np->n_fhp, fhsize); 210 } 211 pool_put(&nfs_vattr_pool, np->n_vattr); 212 pool_put(&nfs_node_pool, np); 213 ungetnewvnode(vp); 214 goto loop; 215 } 216 vp->v_data = np; 217 genfs_node_init(vp, &nfs_genfsops); 218 /* 219 * Initalize read/write creds to useful values. VOP_OPEN will 220 * overwrite these. 221 */ 222 np->n_rcred = curlwp->l_cred; 223 kauth_cred_hold(np->n_rcred); 224 np->n_wcred = curlwp->l_cred; 225 kauth_cred_hold(np->n_wcred); 226 vlockmgr(&vp->v_lock, LK_EXCLUSIVE); 227 NFS_INVALIDATE_ATTRCACHE(np); 228 uvm_vnp_setsize(vp, 0); 229 LIST_INSERT_HEAD(nhpp, np, n_hash); 230 mutex_exit(&nfs_hashlock); 231 232 *npp = np; 233 return (0); 234 } 235 236 int 237 nfs_inactive(v) 238 void *v; 239 { 240 struct vop_inactive_args /* { 241 struct vnode *a_vp; 242 bool *a_recycle; 243 } */ *ap = v; 244 struct nfsnode *np; 245 struct sillyrename *sp; 246 struct vnode *vp = ap->a_vp; 247 248 np = VTONFS(vp); 249 if (vp->v_type != VDIR) { 250 sp = np->n_sillyrename; 251 np->n_sillyrename = (struct sillyrename *)0; 252 } else 253 sp = NULL; 254 if (sp != NULL) 255 nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1); 256 *ap->a_recycle = (np->n_flag & NREMOVED) != 0; 257 np->n_flag &= 258 (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED); 259 260 if (vp->v_type == VDIR && np->n_dircache) 261 nfs_invaldircache(vp, 262 NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF); 263 264 VOP_UNLOCK(vp, 0); 265 266 if (sp != NULL) { 267 int error; 268 269 /* 270 * Remove the silly file that was rename'd earlier 271 * 272 * Just in case our thread also has the parent node locked, 273 * we use LK_CANRECURSE. 274 */ 275 276 error = vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_CANRECURSE); 277 if (error || sp->s_dvp->v_data == NULL) { 278 /* XXX should recover */ 279 printf("%s: vp=%p error=%d\n", 280 __func__, sp->s_dvp, error); 281 } else { 282 nfs_removeit(sp); 283 } 284 kauth_cred_free(sp->s_cred); 285 vput(sp->s_dvp); 286 kmem_free(sp, sizeof(*sp)); 287 } 288 289 return (0); 290 } 291 292 /* 293 * Reclaim an nfsnode so that it can be used for other purposes. 294 */ 295 int 296 nfs_reclaim(v) 297 void *v; 298 { 299 struct vop_reclaim_args /* { 300 struct vnode *a_vp; 301 } */ *ap = v; 302 struct vnode *vp = ap->a_vp; 303 struct nfsnode *np = VTONFS(vp); 304 305 if (prtactive && vp->v_usecount > 1) 306 vprint("nfs_reclaim: pushing active", vp); 307 308 mutex_enter(&nfs_hashlock); 309 LIST_REMOVE(np, n_hash); 310 mutex_exit(&nfs_hashlock); 311 312 /* 313 * Free up any directory cookie structures and 314 * large file handle structures that might be associated with 315 * this nfs node. 316 */ 317 if (vp->v_type == VDIR && np->n_dircache != NULL) { 318 nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE); 319 hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask); 320 } 321 KASSERT(np->n_dirgens == NULL); 322 323 if (np->n_fhsize > NFS_SMALLFH) 324 kmem_free(np->n_fhp, np->n_fhsize); 325 326 pool_put(&nfs_vattr_pool, np->n_vattr); 327 if (np->n_rcred) 328 kauth_cred_free(np->n_rcred); 329 330 if (np->n_wcred) 331 kauth_cred_free(np->n_wcred); 332 333 cache_purge(vp); 334 if (vp->v_type == VREG) { 335 mutex_destroy(&np->n_commitlock); 336 } 337 genfs_node_destroy(vp); 338 pool_put(&nfs_node_pool, np); 339 vp->v_data = NULL; 340 return (0); 341 } 342 343 void 344 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags) 345 { 346 347 *eobp = MAX(size, vp->v_size); 348 } 349 350 int 351 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags, 352 kauth_cred_t cred) 353 { 354 355 return 0; 356 } 357 358 int 359 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags) 360 { 361 int i; 362 363 for (i = 0; i < npages; i++) { 364 pmap_page_protect(pgs[i], VM_PROT_READ); 365 } 366 return genfs_gop_write(vp, pgs, npages, flags); 367 } 368