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