1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 * from nfs_node.c 8.6 (Berkeley) 5/22/95 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/fcntl.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mount.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/socket.h> 46 #include <sys/sysctl.h> 47 #include <sys/taskqueue.h> 48 #include <sys/vnode.h> 49 50 #include <vm/uma.h> 51 52 #include <fs/nfs/nfsport.h> 53 #include <fs/nfsclient/nfsnode.h> 54 #include <fs/nfsclient/nfsmount.h> 55 #include <fs/nfsclient/nfs.h> 56 #include <fs/nfsclient/nfs_kdtrace.h> 57 58 #include <nfs/nfs_lock.h> 59 60 extern struct vop_vector newnfs_vnodeops; 61 MALLOC_DECLARE(M_NEWNFSREQ); 62 63 uma_zone_t newnfsnode_zone; 64 65 const char nfs_vnode_tag[] = "nfs"; 66 67 static void nfs_freesillyrename(void *arg, __unused int pending); 68 69 void 70 ncl_nhinit(void) 71 { 72 73 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL, 74 NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 75 } 76 77 void 78 ncl_nhuninit(void) 79 { 80 uma_zdestroy(newnfsnode_zone); 81 } 82 83 /* 84 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this 85 * function is going to be used to get Regular Files, code must be added 86 * to fill in the "struct nfsv4node". 87 * Look up a vnode/nfsnode by file handle. 88 * Callers must check for mount points!! 89 * In all cases, a pointer to a 90 * nfsnode structure is returned. 91 */ 92 int 93 ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp, 94 int lkflags) 95 { 96 struct thread *td = curthread; /* XXX */ 97 struct nfsnode *np; 98 struct vnode *vp; 99 struct vnode *nvp; 100 int error; 101 u_int hash; 102 struct nfsmount *nmp; 103 struct nfsfh *nfhp; 104 105 nmp = VFSTONFS(mntp); 106 *npp = NULL; 107 108 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT); 109 110 nfhp = malloc(sizeof (struct nfsfh) + fhsize, 111 M_NFSFH, M_WAITOK); 112 bcopy(fhp, &nfhp->nfh_fh[0], fhsize); 113 nfhp->nfh_len = fhsize; 114 error = vfs_hash_get(mntp, hash, lkflags, 115 td, &nvp, newnfs_vncmpf, nfhp); 116 free(nfhp, M_NFSFH); 117 if (error) 118 return (error); 119 if (nvp != NULL) { 120 *npp = VTONFS(nvp); 121 return (0); 122 } 123 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO); 124 125 error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp); 126 if (error) { 127 uma_zfree(newnfsnode_zone, np); 128 return (error); 129 } 130 vp = nvp; 131 KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0")); 132 vp->v_data = np; 133 np->n_vnode = vp; 134 /* 135 * Initialize the mutex even if the vnode is going to be a loser. 136 * This simplifies the logic in reclaim, which can then unconditionally 137 * destroy the mutex (in the case of the loser, or if hash_insert 138 * happened to return an error no special casing is needed). 139 */ 140 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK); 141 lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE | 142 LK_CANRECURSE); 143 144 /* 145 * NFS supports recursive and shared locking. 146 */ 147 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL); 148 VN_LOCK_AREC(vp); 149 VN_LOCK_ASHARE(vp); 150 /* 151 * Are we getting the root? If so, make sure the vnode flags 152 * are correct 153 */ 154 if (fhsize == NFSX_FHMAX + 1 || (fhsize == nmp->nm_fhsize && 155 !bcmp(fhp, nmp->nm_fh, fhsize))) { 156 if (vp->v_type == VNON) 157 vp->v_type = VDIR; 158 vp->v_vflag |= VV_ROOT; 159 } 160 161 vp->v_vflag |= VV_VMSIZEVNLOCK; 162 163 np->n_fhp = malloc(sizeof (struct nfsfh) + fhsize, 164 M_NFSFH, M_WAITOK); 165 bcopy(fhp, np->n_fhp->nfh_fh, fhsize); 166 np->n_fhp->nfh_len = fhsize; 167 error = insmntque(vp, mntp); 168 if (error != 0) { 169 *npp = NULL; 170 free(np->n_fhp, M_NFSFH); 171 mtx_destroy(&np->n_mtx); 172 lockdestroy(&np->n_excl); 173 uma_zfree(newnfsnode_zone, np); 174 return (error); 175 } 176 vn_set_state(vp, VSTATE_CONSTRUCTED); 177 error = vfs_hash_insert(vp, hash, lkflags, 178 td, &nvp, newnfs_vncmpf, np->n_fhp); 179 if (error) 180 return (error); 181 if (nvp != NULL) { 182 *npp = VTONFS(nvp); 183 /* vfs_hash_insert() vput()'s the losing vnode */ 184 return (0); 185 } 186 *npp = np; 187 188 return (0); 189 } 190 191 /* 192 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a 193 * deadlock because of a LOR when vrele() locks the directory vnode. 194 */ 195 static void 196 nfs_freesillyrename(void *arg, __unused int pending) 197 { 198 struct sillyrename *sp; 199 200 sp = arg; 201 vrele(sp->s_dvp); 202 free(sp, M_NEWNFSREQ); 203 } 204 205 static void 206 ncl_releasesillyrename(struct vnode *vp, struct thread *td) 207 { 208 struct nfsnode *np; 209 struct sillyrename *sp; 210 211 ASSERT_VOP_ELOCKED(vp, "releasesillyrename"); 212 np = VTONFS(vp); 213 NFSASSERTNODE(np); 214 if (vp->v_type != VDIR) { 215 sp = np->n_sillyrename; 216 np->n_sillyrename = NULL; 217 } else 218 sp = NULL; 219 if (sp != NULL) { 220 NFSUNLOCKNODE(np); 221 (void) ncl_vinvalbuf(vp, 0, td, 1); 222 /* 223 * Remove the silly file that was rename'd earlier 224 */ 225 ncl_removeit(sp, vp); 226 crfree(sp->s_cred); 227 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp); 228 taskqueue_enqueue(taskqueue_thread, &sp->s_task); 229 NFSLOCKNODE(np); 230 } 231 } 232 233 int 234 ncl_inactive(struct vop_inactive_args *ap) 235 { 236 struct vnode *vp = ap->a_vp; 237 struct nfsnode *np; 238 struct thread *td; 239 boolean_t retv; 240 241 td = curthread; 242 np = VTONFS(vp); 243 if (NFS_ISV4(vp) && vp->v_type == VREG) { 244 NFSLOCKNODE(np); 245 np->n_openstateid = NULL; 246 NFSUNLOCKNODE(np); 247 /* 248 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4 249 * Close operations are delayed until now. Any dirty 250 * buffers/pages must be flushed before the close, so that the 251 * stateid is available for the writes. 252 */ 253 if (vp->v_object != NULL) { 254 VM_OBJECT_WLOCK(vp->v_object); 255 retv = vm_object_page_clean(vp->v_object, 0, 0, 256 OBJPC_SYNC); 257 VM_OBJECT_WUNLOCK(vp->v_object); 258 } else 259 retv = TRUE; 260 if (retv == TRUE) { 261 (void)ncl_flush(vp, MNT_WAIT, td, 1, 0); 262 (void)nfsrpc_close(vp, 1, td); 263 } 264 } 265 266 NFSLOCKNODE(np); 267 ncl_releasesillyrename(vp, td); 268 269 /* 270 * NMODIFIED means that there might be dirty/stale buffers 271 * associated with the NFS vnode. 272 * NDSCOMMIT means that the file is on a pNFS server and commits 273 * should be done to the DS. 274 * None of the other flags are meaningful after the vnode is unused. 275 */ 276 np->n_flag &= (NMODIFIED | NDSCOMMIT); 277 NFSUNLOCKNODE(np); 278 return (0); 279 } 280 281 /* 282 * Reclaim an nfsnode so that it can be used for other purposes. 283 */ 284 int 285 ncl_reclaim(struct vop_reclaim_args *ap) 286 { 287 struct vnode *vp = ap->a_vp; 288 struct nfsnode *np = VTONFS(vp); 289 struct nfsdmap *dp, *dp2; 290 struct thread *td; 291 struct mount *mp; 292 293 td = curthread; 294 mp = vp->v_mount; 295 296 /* 297 * If the NLM is running, give it a chance to abort pending 298 * locks. 299 */ 300 if (nfs_reclaim_p != NULL) 301 nfs_reclaim_p(ap); 302 303 NFSLOCKNODE(np); 304 ncl_releasesillyrename(vp, td); 305 306 if (NFS_ISV4(vp) && vp->v_type == VREG) { 307 np->n_openstateid = NULL; 308 NFSUNLOCKNODE(np); 309 /* 310 * We can now safely close any remaining NFSv4 Opens for 311 * this file. Most opens will have already been closed by 312 * ncl_inactive(), but there are cases where it is not 313 * called, so we need to do it again here. 314 */ 315 (void) nfsrpc_close(vp, 1, td); 316 /* 317 * It it unlikely a delegation will still exist, but 318 * if one does, it must be returned before calling 319 * vfs_hash_remove(), since it cannot be recalled once the 320 * nfs node is no longer available. 321 */ 322 MNT_ILOCK(mp); 323 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0) { 324 MNT_IUNLOCK(mp); 325 nfscl_delegreturnvp(vp, td); 326 } else 327 MNT_IUNLOCK(mp); 328 } else 329 NFSUNLOCKNODE(np); 330 331 vfs_hash_remove(vp); 332 333 /* 334 * Call nfscl_reclaimnode() to save attributes in the delegation, 335 * as required. 336 */ 337 if (vp->v_type == VREG) 338 nfscl_reclaimnode(vp); 339 340 /* 341 * Free up any directory cookie structures and 342 * large file handle structures that might be associated with 343 * this nfs node. 344 */ 345 if (vp->v_type == VDIR) { 346 dp = LIST_FIRST(&np->n_cookies); 347 while (dp) { 348 dp2 = dp; 349 dp = LIST_NEXT(dp, ndm_list); 350 free(dp2, M_NFSDIROFF); 351 } 352 } 353 if (np->n_writecred != NULL) 354 crfree(np->n_writecred); 355 free(np->n_fhp, M_NFSFH); 356 if (np->n_v4 != NULL) 357 free(np->n_v4, M_NFSV4NODE); 358 mtx_destroy(&np->n_mtx); 359 lockdestroy(&np->n_excl); 360 uma_zfree(newnfsnode_zone, vp->v_data); 361 vp->v_data = NULL; 362 return (0); 363 } 364 365 /* 366 * Invalidate both the access and attribute caches for this vnode. 367 */ 368 void 369 ncl_invalcaches(struct vnode *vp) 370 { 371 struct nfsnode *np = VTONFS(vp); 372 int i; 373 374 NFSLOCKNODE(np); 375 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) 376 np->n_accesscache[i].stamp = 0; 377 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp); 378 np->n_attrstamp = 0; 379 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 380 NFSUNLOCKNODE(np); 381 } 382