1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from nfs_subs.c 8.8 (Berkeley) 5/22/95 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 /* 39 * These functions support the macros and help fiddle mbuf chains for 40 * the nfs op functions. They do things like create the rpc header and 41 * copy data between mbuf chains and uio lists. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/bio.h> 48 #include <sys/buf.h> 49 #include <sys/proc.h> 50 #include <sys/mount.h> 51 #include <sys/vnode.h> 52 #include <sys/namei.h> 53 #include <sys/mbuf.h> 54 #include <sys/socket.h> 55 #include <sys/stat.h> 56 #include <sys/malloc.h> 57 #include <sys/sysent.h> 58 #include <sys/syscall.h> 59 #include <sys/sysproto.h> 60 #include <sys/taskqueue.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_object.h> 64 #include <vm/vm_extern.h> 65 #include <vm/uma.h> 66 67 #include <fs/nfs/nfsport.h> 68 #include <fs/nfsclient/nfsnode.h> 69 #include <fs/nfsclient/nfsmount.h> 70 #include <fs/nfsclient/nfs.h> 71 #include <fs/nfsclient/nfs_kdtrace.h> 72 73 #include <netinet/in.h> 74 75 /* 76 * Note that stdarg.h and the ANSI style va_start macro is used for both 77 * ANSI and traditional C compilers. 78 */ 79 #include <machine/stdarg.h> 80 81 extern struct mtx ncl_iod_mutex; 82 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON]; 83 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON]; 84 extern int ncl_numasync; 85 extern unsigned int ncl_iodmax; 86 extern struct nfsstatsv1 nfsstatsv1; 87 88 struct task ncl_nfsiodnew_task; 89 90 int 91 ncl_uninit(struct vfsconf *vfsp) 92 { 93 /* 94 * XXX: Unloading of nfscl module is unsupported. 95 */ 96 #if 0 97 int i; 98 99 /* 100 * Tell all nfsiod processes to exit. Clear ncl_iodmax, and wakeup 101 * any sleeping nfsiods so they check ncl_iodmax and exit. 102 */ 103 mtx_lock(&ncl_iod_mutex); 104 ncl_iodmax = 0; 105 for (i = 0; i < ncl_numasync; i++) 106 if (ncl_iodwant[i] == NFSIOD_AVAILABLE) 107 wakeup(&ncl_iodwant[i]); 108 /* The last nfsiod to exit will wake us up when ncl_numasync hits 0 */ 109 while (ncl_numasync) 110 msleep(&ncl_numasync, &ncl_iod_mutex, PWAIT, "ioddie", 0); 111 mtx_unlock(&ncl_iod_mutex); 112 ncl_nhuninit(); 113 return (0); 114 #else 115 return (EOPNOTSUPP); 116 #endif 117 } 118 119 void 120 ncl_dircookie_lock(struct nfsnode *np) 121 { 122 mtx_lock(&np->n_mtx); 123 while (np->n_flag & NDIRCOOKIELK) 124 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0); 125 np->n_flag |= NDIRCOOKIELK; 126 mtx_unlock(&np->n_mtx); 127 } 128 129 void 130 ncl_dircookie_unlock(struct nfsnode *np) 131 { 132 mtx_lock(&np->n_mtx); 133 np->n_flag &= ~NDIRCOOKIELK; 134 wakeup(&np->n_flag); 135 mtx_unlock(&np->n_mtx); 136 } 137 138 bool 139 ncl_excl_start(struct vnode *vp) 140 { 141 struct nfsnode *np; 142 int vn_lk; 143 144 ASSERT_VOP_LOCKED(vp, "ncl_excl_start"); 145 vn_lk = NFSVOPISLOCKED(vp); 146 if (vn_lk == LK_EXCLUSIVE) 147 return (false); 148 KASSERT(vn_lk == LK_SHARED, 149 ("ncl_excl_start: wrong vnode lock %d", vn_lk)); 150 /* Ensure exclusive access, this might block */ 151 np = VTONFS(vp); 152 lockmgr(&np->n_excl, LK_EXCLUSIVE, NULL); 153 return (true); 154 } 155 156 void 157 ncl_excl_finish(struct vnode *vp, bool old_lock) 158 { 159 struct nfsnode *np; 160 161 if (!old_lock) 162 return; 163 np = VTONFS(vp); 164 lockmgr(&np->n_excl, LK_RELEASE, NULL); 165 } 166 167 #ifdef NFS_ACDEBUG 168 #include <sys/sysctl.h> 169 SYSCTL_DECL(_vfs_nfs); 170 static int nfs_acdebug; 171 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 172 #endif 173 174 /* 175 * Check the time stamp 176 * If the cache is valid, copy contents to *vap and return 0 177 * otherwise return an error 178 */ 179 int 180 ncl_getattrcache(struct vnode *vp, struct vattr *vaper) 181 { 182 struct nfsnode *np; 183 struct vattr *vap; 184 struct nfsmount *nmp; 185 int timeo, mustflush; 186 187 np = VTONFS(vp); 188 vap = &np->n_vattr.na_vattr; 189 nmp = VFSTONFS(vp->v_mount); 190 mustflush = nfscl_mustflush(vp); /* must be before mtx_lock() */ 191 mtx_lock(&np->n_mtx); 192 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 193 timeo = (time_second - np->n_mtime.tv_sec) / 10; 194 195 #ifdef NFS_ACDEBUG 196 if (nfs_acdebug>1) 197 printf("ncl_getattrcache: initial timeo = %d\n", timeo); 198 #endif 199 200 if (vap->va_type == VDIR) { 201 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 202 timeo = nmp->nm_acdirmin; 203 else if (timeo > nmp->nm_acdirmax) 204 timeo = nmp->nm_acdirmax; 205 } else { 206 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 207 timeo = nmp->nm_acregmin; 208 else if (timeo > nmp->nm_acregmax) 209 timeo = nmp->nm_acregmax; 210 } 211 212 #ifdef NFS_ACDEBUG 213 if (nfs_acdebug > 2) 214 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 215 nmp->nm_acregmin, nmp->nm_acregmax, 216 nmp->nm_acdirmin, nmp->nm_acdirmax); 217 218 if (nfs_acdebug) 219 printf("ncl_getattrcache: age = %d; final timeo = %d\n", 220 (time_second - np->n_attrstamp), timeo); 221 #endif 222 223 if ((time_second - np->n_attrstamp) >= timeo && 224 (mustflush != 0 || np->n_attrstamp == 0)) { 225 nfsstatsv1.attrcache_misses++; 226 mtx_unlock(&np->n_mtx); 227 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp); 228 return( ENOENT); 229 } 230 nfsstatsv1.attrcache_hits++; 231 if (vap->va_size != np->n_size) { 232 if (vap->va_type == VREG) { 233 if (np->n_flag & NMODIFIED) { 234 if (vap->va_size < np->n_size) 235 vap->va_size = np->n_size; 236 else 237 np->n_size = vap->va_size; 238 } else { 239 np->n_size = vap->va_size; 240 } 241 vnode_pager_setsize(vp, np->n_size); 242 } else { 243 np->n_size = vap->va_size; 244 } 245 } 246 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 247 if (np->n_flag & NCHG) { 248 if (np->n_flag & NACC) 249 vaper->va_atime = np->n_atim; 250 if (np->n_flag & NUPD) 251 vaper->va_mtime = np->n_mtim; 252 } 253 mtx_unlock(&np->n_mtx); 254 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap); 255 return (0); 256 } 257 258 static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 259 /* 260 * This function finds the directory cookie that corresponds to the 261 * logical byte offset given. 262 */ 263 nfsuint64 * 264 ncl_getcookie(struct nfsnode *np, off_t off, int add) 265 { 266 struct nfsdmap *dp, *dp2; 267 int pos; 268 nfsuint64 *retval = NULL; 269 270 pos = (uoff_t)off / NFS_DIRBLKSIZ; 271 if (pos == 0 || off < 0) { 272 KASSERT(!add, ("nfs getcookie add at <= 0")); 273 return (&nfs_nullcookie); 274 } 275 pos--; 276 dp = LIST_FIRST(&np->n_cookies); 277 if (!dp) { 278 if (add) { 279 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 280 M_NFSDIROFF, M_WAITOK); 281 dp->ndm_eocookie = 0; 282 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 283 } else 284 goto out; 285 } 286 while (pos >= NFSNUMCOOKIES) { 287 pos -= NFSNUMCOOKIES; 288 if (LIST_NEXT(dp, ndm_list)) { 289 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 290 pos >= dp->ndm_eocookie) 291 goto out; 292 dp = LIST_NEXT(dp, ndm_list); 293 } else if (add) { 294 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 295 M_NFSDIROFF, M_WAITOK); 296 dp2->ndm_eocookie = 0; 297 LIST_INSERT_AFTER(dp, dp2, ndm_list); 298 dp = dp2; 299 } else 300 goto out; 301 } 302 if (pos >= dp->ndm_eocookie) { 303 if (add) 304 dp->ndm_eocookie = pos + 1; 305 else 306 goto out; 307 } 308 retval = &dp->ndm_cookies[pos]; 309 out: 310 return (retval); 311 } 312 313 /* 314 * Invalidate cached directory information, except for the actual directory 315 * blocks (which are invalidated separately). 316 * Done mainly to avoid the use of stale offset cookies. 317 */ 318 void 319 ncl_invaldir(struct vnode *vp) 320 { 321 struct nfsnode *np = VTONFS(vp); 322 323 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir")); 324 ncl_dircookie_lock(np); 325 np->n_direofoffset = 0; 326 np->n_cookieverf.nfsuquad[0] = 0; 327 np->n_cookieverf.nfsuquad[1] = 0; 328 if (LIST_FIRST(&np->n_cookies)) 329 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0; 330 ncl_dircookie_unlock(np); 331 } 332 333 /* 334 * The write verifier has changed (probably due to a server reboot), so all 335 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 336 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 337 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 338 * mount point. 339 * 340 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 341 * writes are not clusterable. 342 */ 343 void 344 ncl_clearcommit(struct mount *mp) 345 { 346 struct vnode *vp, *nvp; 347 struct buf *bp, *nbp; 348 struct bufobj *bo; 349 350 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) { 351 bo = &vp->v_bufobj; 352 vholdl(vp); 353 VI_UNLOCK(vp); 354 BO_LOCK(bo); 355 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 356 if (!BUF_ISLOCKED(bp) && 357 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 358 == (B_DELWRI | B_NEEDCOMMIT)) 359 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 360 } 361 BO_UNLOCK(bo); 362 vdrop(vp); 363 } 364 } 365 366 /* 367 * Called once to initialize data structures... 368 */ 369 int 370 ncl_init(struct vfsconf *vfsp) 371 { 372 int i; 373 374 /* Ensure async daemons disabled */ 375 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 376 ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE; 377 ncl_iodmount[i] = NULL; 378 } 379 TASK_INIT(&ncl_nfsiodnew_task, 0, ncl_nfsiodnew_tq, NULL); 380 ncl_nhinit(); /* Init the nfsnode table */ 381 382 return (0); 383 } 384 385