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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95 37 * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $ 38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.80 2008/10/18 01:13:54 dillon Exp $ 39 */ 40 41 42 /* 43 * vnode op calls for Sun NFS version 2 and 3 44 */ 45 46 #include "opt_inet.h" 47 48 #include <sys/param.h> 49 #include <sys/kernel.h> 50 #include <sys/systm.h> 51 #include <sys/resourcevar.h> 52 #include <sys/proc.h> 53 #include <sys/mount.h> 54 #include <sys/buf.h> 55 #include <sys/malloc.h> 56 #include <sys/mbuf.h> 57 #include <sys/namei.h> 58 #include <sys/nlookup.h> 59 #include <sys/socket.h> 60 #include <sys/vnode.h> 61 #include <sys/dirent.h> 62 #include <sys/fcntl.h> 63 #include <sys/lockf.h> 64 #include <sys/stat.h> 65 #include <sys/sysctl.h> 66 #include <sys/conf.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_extern.h> 70 71 #include <sys/buf2.h> 72 73 #include <vfs/fifofs/fifo.h> 74 #include <vfs/ufs/dir.h> 75 76 #undef DIRBLKSIZ 77 78 #include "rpcv2.h" 79 #include "nfsproto.h" 80 #include "nfs.h" 81 #include "nfsmount.h" 82 #include "nfsnode.h" 83 #include "xdr_subs.h" 84 #include "nfsm_subs.h" 85 86 #include <net/if.h> 87 #include <netinet/in.h> 88 #include <netinet/in_var.h> 89 90 #include <sys/thread2.h> 91 92 /* Defs */ 93 #define TRUE 1 94 #define FALSE 0 95 96 static int nfsfifo_read (struct vop_read_args *); 97 static int nfsfifo_write (struct vop_write_args *); 98 static int nfsfifo_close (struct vop_close_args *); 99 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *); 100 static int nfs_lookup (struct vop_old_lookup_args *); 101 static int nfs_create (struct vop_old_create_args *); 102 static int nfs_mknod (struct vop_old_mknod_args *); 103 static int nfs_open (struct vop_open_args *); 104 static int nfs_close (struct vop_close_args *); 105 static int nfs_access (struct vop_access_args *); 106 static int nfs_getattr (struct vop_getattr_args *); 107 static int nfs_setattr (struct vop_setattr_args *); 108 static int nfs_read (struct vop_read_args *); 109 static int nfs_mmap (struct vop_mmap_args *); 110 static int nfs_fsync (struct vop_fsync_args *); 111 static int nfs_remove (struct vop_old_remove_args *); 112 static int nfs_link (struct vop_old_link_args *); 113 static int nfs_rename (struct vop_old_rename_args *); 114 static int nfs_mkdir (struct vop_old_mkdir_args *); 115 static int nfs_rmdir (struct vop_old_rmdir_args *); 116 static int nfs_symlink (struct vop_old_symlink_args *); 117 static int nfs_readdir (struct vop_readdir_args *); 118 static int nfs_bmap (struct vop_bmap_args *); 119 static int nfs_strategy (struct vop_strategy_args *); 120 static int nfs_lookitup (struct vnode *, const char *, int, 121 struct ucred *, struct thread *, struct nfsnode **); 122 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *); 123 static int nfs_laccess (struct vop_access_args *); 124 static int nfs_readlink (struct vop_readlink_args *); 125 static int nfs_print (struct vop_print_args *); 126 static int nfs_advlock (struct vop_advlock_args *); 127 128 static int nfs_nresolve (struct vop_nresolve_args *); 129 /* 130 * Global vfs data structures for nfs 131 */ 132 struct vop_ops nfsv2_vnode_vops = { 133 .vop_default = vop_defaultop, 134 .vop_access = nfs_access, 135 .vop_advlock = nfs_advlock, 136 .vop_bmap = nfs_bmap, 137 .vop_close = nfs_close, 138 .vop_old_create = nfs_create, 139 .vop_fsync = nfs_fsync, 140 .vop_getattr = nfs_getattr, 141 .vop_getpages = vop_stdgetpages, 142 .vop_putpages = vop_stdputpages, 143 .vop_inactive = nfs_inactive, 144 .vop_old_link = nfs_link, 145 .vop_old_lookup = nfs_lookup, 146 .vop_old_mkdir = nfs_mkdir, 147 .vop_old_mknod = nfs_mknod, 148 .vop_mmap = nfs_mmap, 149 .vop_open = nfs_open, 150 .vop_print = nfs_print, 151 .vop_read = nfs_read, 152 .vop_readdir = nfs_readdir, 153 .vop_readlink = nfs_readlink, 154 .vop_reclaim = nfs_reclaim, 155 .vop_old_remove = nfs_remove, 156 .vop_old_rename = nfs_rename, 157 .vop_old_rmdir = nfs_rmdir, 158 .vop_setattr = nfs_setattr, 159 .vop_strategy = nfs_strategy, 160 .vop_old_symlink = nfs_symlink, 161 .vop_write = nfs_write, 162 .vop_nresolve = nfs_nresolve 163 }; 164 165 /* 166 * Special device vnode ops 167 */ 168 struct vop_ops nfsv2_spec_vops = { 169 .vop_default = vop_defaultop, 170 .vop_access = nfs_laccess, 171 .vop_close = nfs_close, 172 .vop_fsync = nfs_fsync, 173 .vop_getattr = nfs_getattr, 174 .vop_inactive = nfs_inactive, 175 .vop_print = nfs_print, 176 .vop_read = vop_stdnoread, 177 .vop_reclaim = nfs_reclaim, 178 .vop_setattr = nfs_setattr, 179 .vop_write = vop_stdnowrite 180 }; 181 182 struct vop_ops nfsv2_fifo_vops = { 183 .vop_default = fifo_vnoperate, 184 .vop_access = nfs_laccess, 185 .vop_close = nfsfifo_close, 186 .vop_fsync = nfs_fsync, 187 .vop_getattr = nfs_getattr, 188 .vop_inactive = nfs_inactive, 189 .vop_print = nfs_print, 190 .vop_read = nfsfifo_read, 191 .vop_reclaim = nfs_reclaim, 192 .vop_setattr = nfs_setattr, 193 .vop_write = nfsfifo_write 194 }; 195 196 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp, 197 struct componentname *cnp, 198 struct vattr *vap); 199 static int nfs_removerpc (struct vnode *dvp, const char *name, 200 int namelen, 201 struct ucred *cred, struct thread *td); 202 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr, 203 int fnamelen, struct vnode *tdvp, 204 const char *tnameptr, int tnamelen, 205 struct ucred *cred, struct thread *td); 206 static int nfs_renameit (struct vnode *sdvp, 207 struct componentname *scnp, 208 struct sillyrename *sp); 209 210 SYSCTL_DECL(_vfs_nfs); 211 212 static int nfs_flush_on_rename = 1; 213 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW, 214 &nfs_flush_on_rename, 0, "flush fvp prior to rename"); 215 static int nfs_flush_on_hlink = 0; 216 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW, 217 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link"); 218 219 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO; 220 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW, 221 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout"); 222 223 static int nfsneg_cache_timeout = NFS_MINATTRTIMO; 224 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW, 225 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout"); 226 227 static int nfspos_cache_timeout = NFS_MINATTRTIMO; 228 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW, 229 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout"); 230 231 static int nfsv3_commit_on_close = 0; 232 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW, 233 &nfsv3_commit_on_close, 0, "write+commit on close, else only write"); 234 #if 0 235 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD, 236 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count"); 237 238 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD, 239 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count"); 240 #endif 241 242 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \ 243 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \ 244 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP) 245 246 /* 247 * Returns whether a name component is a degenerate '.' or '..'. 248 */ 249 static __inline 250 int 251 nlcdegenerate(struct nlcomponent *nlc) 252 { 253 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.') 254 return(1); 255 if (nlc->nlc_namelen == 2 && 256 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.') 257 return(1); 258 return(0); 259 } 260 261 static int 262 nfs3_access_otw(struct vnode *vp, int wmode, 263 struct thread *td, struct ucred *cred) 264 { 265 struct nfsnode *np = VTONFS(vp); 266 int attrflag; 267 int error = 0; 268 u_int32_t *tl; 269 u_int32_t rmode; 270 struct nfsm_info info; 271 272 info.mrep = NULL; 273 info.v3 = 1; 274 275 nfsstats.rpccnt[NFSPROC_ACCESS]++; 276 nfsm_reqhead(&info, vp, NFSPROC_ACCESS, 277 NFSX_FH(info.v3) + NFSX_UNSIGNED); 278 ERROROUT(nfsm_fhtom(&info, vp)); 279 tl = nfsm_build(&info, NFSX_UNSIGNED); 280 *tl = txdr_unsigned(wmode); 281 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error)); 282 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK)); 283 if (error == 0) { 284 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 285 rmode = fxdr_unsigned(u_int32_t, *tl); 286 np->n_mode = rmode; 287 np->n_modeuid = cred->cr_uid; 288 np->n_modestamp = mycpu->gd_time_seconds; 289 } 290 m_freem(info.mrep); 291 info.mrep = NULL; 292 nfsmout: 293 return error; 294 } 295 296 /* 297 * nfs access vnode op. 298 * For nfs version 2, just return ok. File accesses may fail later. 299 * For nfs version 3, use the access rpc to check accessibility. If file modes 300 * are changed on the server, accesses might still fail later. 301 * 302 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred) 303 */ 304 static int 305 nfs_access(struct vop_access_args *ap) 306 { 307 struct ucred *cred; 308 struct vnode *vp = ap->a_vp; 309 thread_t td = curthread; 310 int error = 0; 311 u_int32_t mode, wmode; 312 struct nfsnode *np = VTONFS(vp); 313 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 314 int v3 = NFS_ISV3(vp); 315 316 lwkt_gettoken(&nmp->nm_token); 317 318 /* 319 * Disallow write attempts on filesystems mounted read-only; 320 * unless the file is a socket, fifo, or a block or character 321 * device resident on the filesystem. 322 */ 323 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 324 switch (vp->v_type) { 325 case VREG: 326 case VDIR: 327 case VLNK: 328 lwkt_reltoken(&nmp->nm_token); 329 return (EROFS); 330 default: 331 break; 332 } 333 } 334 335 /* 336 * The NFS protocol passes only the effective uid/gid over the wire but 337 * we need to check access against real ids if AT_EACCESS not set. 338 * Handle this case by cloning the credentials and setting the 339 * effective ids to the real ones. 340 */ 341 if (ap->a_flags & AT_EACCESS) { 342 cred = crhold(ap->a_cred); 343 } else { 344 cred = crdup(ap->a_cred); 345 cred->cr_uid = cred->cr_ruid; 346 cred->cr_gid = cred->cr_rgid; 347 } 348 349 /* 350 * For nfs v3, check to see if we have done this recently, and if 351 * so return our cached result instead of making an ACCESS call. 352 * If not, do an access rpc, otherwise you are stuck emulating 353 * ufs_access() locally using the vattr. This may not be correct, 354 * since the server may apply other access criteria such as 355 * client uid-->server uid mapping that we do not know about. 356 */ 357 if (v3) { 358 if (ap->a_mode & VREAD) 359 mode = NFSV3ACCESS_READ; 360 else 361 mode = 0; 362 if (vp->v_type != VDIR) { 363 if (ap->a_mode & VWRITE) 364 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND); 365 if (ap->a_mode & VEXEC) 366 mode |= NFSV3ACCESS_EXECUTE; 367 } else { 368 if (ap->a_mode & VWRITE) 369 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND | 370 NFSV3ACCESS_DELETE); 371 if (ap->a_mode & VEXEC) 372 mode |= NFSV3ACCESS_LOOKUP; 373 } 374 /* XXX safety belt, only make blanket request if caching */ 375 if (nfsaccess_cache_timeout > 0) { 376 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY | 377 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE | 378 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP; 379 } else { 380 wmode = mode; 381 } 382 383 /* 384 * Does our cached result allow us to give a definite yes to 385 * this request? 386 */ 387 if (np->n_modestamp && 388 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) && 389 (cred->cr_uid == np->n_modeuid) && 390 ((np->n_mode & mode) == mode)) { 391 nfsstats.accesscache_hits++; 392 } else { 393 /* 394 * Either a no, or a don't know. Go to the wire. 395 */ 396 nfsstats.accesscache_misses++; 397 error = nfs3_access_otw(vp, wmode, td, cred); 398 if (!error) { 399 if ((np->n_mode & mode) != mode) { 400 error = EACCES; 401 } 402 } 403 } 404 } else { 405 if ((error = nfs_laccess(ap)) != 0) { 406 crfree(cred); 407 lwkt_reltoken(&nmp->nm_token); 408 return (error); 409 } 410 411 /* 412 * Attempt to prevent a mapped root from accessing a file 413 * which it shouldn't. We try to read a byte from the file 414 * if the user is root and the file is not zero length. 415 * After calling nfs_laccess, we should have the correct 416 * file size cached. 417 */ 418 if (cred->cr_uid == 0 && (ap->a_mode & VREAD) 419 && VTONFS(vp)->n_size > 0) { 420 struct iovec aiov; 421 struct uio auio; 422 char buf[1]; 423 424 aiov.iov_base = buf; 425 aiov.iov_len = 1; 426 auio.uio_iov = &aiov; 427 auio.uio_iovcnt = 1; 428 auio.uio_offset = 0; 429 auio.uio_resid = 1; 430 auio.uio_segflg = UIO_SYSSPACE; 431 auio.uio_rw = UIO_READ; 432 auio.uio_td = td; 433 434 if (vp->v_type == VREG) { 435 error = nfs_readrpc_uio(vp, &auio); 436 } else if (vp->v_type == VDIR) { 437 char* bp; 438 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK); 439 aiov.iov_base = bp; 440 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ; 441 error = nfs_readdirrpc_uio(vp, &auio); 442 kfree(bp, M_TEMP); 443 } else if (vp->v_type == VLNK) { 444 error = nfs_readlinkrpc_uio(vp, &auio); 445 } else { 446 error = EACCES; 447 } 448 } 449 } 450 /* 451 * [re]record creds for reading and/or writing if access 452 * was granted. Assume the NFS server will grant read access 453 * for execute requests. 454 */ 455 if (error == 0) { 456 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) { 457 crhold(cred); 458 if (np->n_rucred) 459 crfree(np->n_rucred); 460 np->n_rucred = cred; 461 } 462 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) { 463 crhold(cred); 464 if (np->n_wucred) 465 crfree(np->n_wucred); 466 np->n_wucred = cred; 467 } 468 } 469 lwkt_reltoken(&nmp->nm_token); 470 crfree(cred); 471 return(error); 472 } 473 474 /* 475 * nfs open vnode op 476 * Check to see if the type is ok 477 * and that deletion is not in progress. 478 * For paged in text files, you will need to flush the page cache 479 * if consistency is lost. 480 * 481 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, 482 * struct file *a_fp) 483 */ 484 /* ARGSUSED */ 485 static int 486 nfs_open(struct vop_open_args *ap) 487 { 488 struct vnode *vp = ap->a_vp; 489 struct nfsnode *np = VTONFS(vp); 490 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 491 struct vattr vattr; 492 int error; 493 494 lwkt_gettoken(&nmp->nm_token); 495 496 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) { 497 #ifdef DIAGNOSTIC 498 kprintf("open eacces vtyp=%d\n",vp->v_type); 499 #endif 500 lwkt_reltoken(&nmp->nm_token); 501 return (EOPNOTSUPP); 502 } 503 504 /* 505 * Save valid creds for reading and writing for later RPCs. 506 */ 507 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) { 508 crhold(ap->a_cred); 509 if (np->n_rucred) 510 crfree(np->n_rucred); 511 np->n_rucred = ap->a_cred; 512 } 513 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) { 514 crhold(ap->a_cred); 515 if (np->n_wucred) 516 crfree(np->n_wucred); 517 np->n_wucred = ap->a_cred; 518 } 519 520 /* 521 * Clear the attribute cache only if opening with write access. It 522 * is unclear if we should do this at all here, but we certainly 523 * should not clear the cache unconditionally simply because a file 524 * is being opened. 525 */ 526 if (ap->a_mode & FWRITE) 527 np->n_attrstamp = 0; 528 529 /* 530 * For normal NFS, reconcile changes made locally verses 531 * changes made remotely. Note that VOP_GETATTR only goes 532 * to the wire if the cached attribute has timed out or been 533 * cleared. 534 * 535 * If local modifications have been made clear the attribute 536 * cache to force an attribute and modified time check. If 537 * GETATTR detects that the file has been changed by someone 538 * other then us it will set NRMODIFIED. 539 * 540 * If we are opening a directory and local changes have been 541 * made we have to invalidate the cache in order to ensure 542 * that we get the most up-to-date information from the 543 * server. XXX 544 */ 545 if (np->n_flag & NLMODIFIED) { 546 np->n_attrstamp = 0; 547 if (vp->v_type == VDIR) { 548 error = nfs_vinvalbuf(vp, V_SAVE, 1); 549 if (error == EINTR) 550 return (error); 551 nfs_invaldir(vp); 552 } 553 } 554 error = VOP_GETATTR(vp, &vattr); 555 if (error) { 556 lwkt_reltoken(&nmp->nm_token); 557 return (error); 558 } 559 if (np->n_flag & NRMODIFIED) { 560 if (vp->v_type == VDIR) 561 nfs_invaldir(vp); 562 error = nfs_vinvalbuf(vp, V_SAVE, 1); 563 if (error == EINTR) { 564 lwkt_reltoken(&nmp->nm_token); 565 return (error); 566 } 567 np->n_flag &= ~NRMODIFIED; 568 } 569 error = vop_stdopen(ap); 570 lwkt_reltoken(&nmp->nm_token); 571 572 return error; 573 } 574 575 /* 576 * nfs close vnode op 577 * What an NFS client should do upon close after writing is a debatable issue. 578 * Most NFS clients push delayed writes to the server upon close, basically for 579 * two reasons: 580 * 1 - So that any write errors may be reported back to the client process 581 * doing the close system call. By far the two most likely errors are 582 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. 583 * 2 - To put a worst case upper bound on cache inconsistency between 584 * multiple clients for the file. 585 * There is also a consistency problem for Version 2 of the protocol w.r.t. 586 * not being able to tell if other clients are writing a file concurrently, 587 * since there is no way of knowing if the changed modify time in the reply 588 * is only due to the write for this client. 589 * (NFS Version 3 provides weak cache consistency data in the reply that 590 * should be sufficient to detect and handle this case.) 591 * 592 * The current code does the following: 593 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers 594 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate 595 * or commit them (this satisfies 1 and 2 except for the 596 * case where the server crashes after this close but 597 * before the commit RPC, which is felt to be "good 598 * enough". Changing the last argument to nfs_flush() to 599 * a 1 would force a commit operation, if it is felt a 600 * commit is necessary now. 601 * for NQNFS - do nothing now, since 2 is dealt with via leases and 602 * 1 should be dealt with via an fsync() system call for 603 * cases where write errors are important. 604 * 605 * nfs_close(struct vnode *a_vp, int a_fflag) 606 */ 607 /* ARGSUSED */ 608 static int 609 nfs_close(struct vop_close_args *ap) 610 { 611 struct vnode *vp = ap->a_vp; 612 struct nfsnode *np = VTONFS(vp); 613 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 614 int error = 0; 615 thread_t td = curthread; 616 617 lwkt_gettoken(&nmp->nm_token); 618 619 if (vp->v_type == VREG) { 620 if (np->n_flag & NLMODIFIED) { 621 if (NFS_ISV3(vp)) { 622 /* 623 * Under NFSv3 we have dirty buffers to dispose of. We 624 * must flush them to the NFS server. We have the option 625 * of waiting all the way through the commit rpc or just 626 * waiting for the initial write. The default is to only 627 * wait through the initial write so the data is in the 628 * server's cache, which is roughly similar to the state 629 * a standard disk subsystem leaves the file in on close(). 630 * 631 * We cannot clear the NLMODIFIED bit in np->n_flag due to 632 * potential races with other processes, and certainly 633 * cannot clear it if we don't commit. 634 */ 635 int cm = nfsv3_commit_on_close ? 1 : 0; 636 error = nfs_flush(vp, MNT_WAIT, td, cm); 637 /* np->n_flag &= ~NLMODIFIED; */ 638 } else { 639 error = nfs_vinvalbuf(vp, V_SAVE, 1); 640 } 641 np->n_attrstamp = 0; 642 } 643 if (np->n_flag & NWRITEERR) { 644 np->n_flag &= ~NWRITEERR; 645 error = np->n_error; 646 } 647 } 648 vop_stdclose(ap); 649 lwkt_reltoken(&nmp->nm_token); 650 651 return (error); 652 } 653 654 /* 655 * nfs getattr call from vfs. 656 * 657 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap) 658 */ 659 static int 660 nfs_getattr(struct vop_getattr_args *ap) 661 { 662 struct vnode *vp = ap->a_vp; 663 struct nfsnode *np = VTONFS(vp); 664 struct nfsmount *nmp; 665 int error = 0; 666 thread_t td = curthread; 667 struct nfsm_info info; 668 669 info.mrep = NULL; 670 info.v3 = NFS_ISV3(vp); 671 nmp = VFSTONFS(vp->v_mount); 672 673 lwkt_gettoken(&nmp->nm_token); 674 675 /* 676 * Update local times for special files. 677 */ 678 if (np->n_flag & (NACC | NUPD)) 679 np->n_flag |= NCHG; 680 /* 681 * First look in the cache. 682 */ 683 if (nfs_getattrcache(vp, ap->a_vap) == 0) 684 goto done; 685 686 if (info.v3 && nfsaccess_cache_timeout > 0) { 687 nfsstats.accesscache_misses++; 688 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK)); 689 if (nfs_getattrcache(vp, ap->a_vap) == 0) 690 goto done; 691 } 692 693 nfsstats.rpccnt[NFSPROC_GETATTR]++; 694 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3)); 695 ERROROUT(nfsm_fhtom(&info, vp)); 696 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td, 697 nfs_vpcred(vp, ND_CHECK), &error)); 698 if (error == 0) { 699 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap)); 700 } 701 m_freem(info.mrep); 702 info.mrep = NULL; 703 done: 704 /* 705 * NFS doesn't support chflags flags. If the nfs mount was 706 * made -o cache set the UF_CACHE bit for swapcache. 707 */ 708 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT)) 709 ap->a_vap->va_flags |= UF_CACHE; 710 nfsmout: 711 lwkt_reltoken(&nmp->nm_token); 712 return (error); 713 } 714 715 /* 716 * nfs setattr call. 717 * 718 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred) 719 */ 720 static int 721 nfs_setattr(struct vop_setattr_args *ap) 722 { 723 struct vnode *vp = ap->a_vp; 724 struct nfsnode *np = VTONFS(vp); 725 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 726 struct vattr *vap = ap->a_vap; 727 int biosize = vp->v_mount->mnt_stat.f_iosize; 728 int error = 0; 729 int boff; 730 off_t tsize; 731 thread_t td = curthread; 732 733 #ifndef nolint 734 tsize = (off_t)0; 735 #endif 736 /* 737 * Setting of flags is not supported. 738 */ 739 if (vap->va_flags != VNOVAL) 740 return (EOPNOTSUPP); 741 742 /* 743 * Disallow write attempts if the filesystem is mounted read-only. 744 */ 745 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 746 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 747 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 748 (vp->v_mount->mnt_flag & MNT_RDONLY)) 749 return (EROFS); 750 751 lwkt_gettoken(&nmp->nm_token); 752 753 if (vap->va_size != VNOVAL) { 754 /* 755 * truncation requested 756 */ 757 switch (vp->v_type) { 758 case VDIR: 759 lwkt_reltoken(&nmp->nm_token); 760 return (EISDIR); 761 case VCHR: 762 case VBLK: 763 case VSOCK: 764 case VFIFO: 765 if (vap->va_mtime.tv_sec == VNOVAL && 766 vap->va_atime.tv_sec == VNOVAL && 767 vap->va_mode == (mode_t)VNOVAL && 768 vap->va_uid == (uid_t)VNOVAL && 769 vap->va_gid == (gid_t)VNOVAL) { 770 lwkt_reltoken(&nmp->nm_token); 771 return (0); 772 } 773 vap->va_size = VNOVAL; 774 break; 775 default: 776 /* 777 * Disallow write attempts if the filesystem is 778 * mounted read-only. 779 */ 780 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 781 lwkt_reltoken(&nmp->nm_token); 782 return (EROFS); 783 } 784 785 tsize = np->n_size; 786 again: 787 boff = (int)vap->va_size & (biosize - 1); 788 error = nfs_meta_setsize(vp, td, vap->va_size, 0); 789 790 #if 0 791 if (np->n_flag & NLMODIFIED) { 792 if (vap->va_size == 0) 793 error = nfs_vinvalbuf(vp, 0, 1); 794 else 795 error = nfs_vinvalbuf(vp, V_SAVE, 1); 796 } 797 #endif 798 /* 799 * note: this loop case almost always happens at 800 * least once per truncation. 801 */ 802 if (error == 0 && np->n_size != vap->va_size) 803 goto again; 804 np->n_vattr.va_size = vap->va_size; 805 break; 806 } 807 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) { 808 /* 809 * What to do. If we are modifying the mtime we lose 810 * mtime detection of changes made by the server or other 811 * clients. But programs like rsync/rdist/cpdup are going 812 * to call utimes a lot. We don't want to piecemeal sync. 813 * 814 * For now sync if any prior remote changes were detected, 815 * but allow us to lose track of remote changes made during 816 * the utimes operation. 817 */ 818 if (np->n_flag & NRMODIFIED) 819 error = nfs_vinvalbuf(vp, V_SAVE, 1); 820 if (error == EINTR) 821 return (error); 822 if (error == 0) { 823 if (vap->va_mtime.tv_sec != VNOVAL) { 824 np->n_mtime = vap->va_mtime.tv_sec; 825 } 826 } 827 } 828 error = nfs_setattrrpc(vp, vap, ap->a_cred, td); 829 830 /* 831 * Sanity check if a truncation was issued. This should only occur 832 * if multiple processes are racing on the same file. 833 */ 834 if (error == 0 && vap->va_size != VNOVAL && 835 np->n_size != vap->va_size) { 836 kprintf("NFS ftruncate: server disagrees on the file size: " 837 "%jd/%jd/%jd\n", 838 (intmax_t)tsize, 839 (intmax_t)vap->va_size, 840 (intmax_t)np->n_size); 841 goto again; 842 } 843 if (error && vap->va_size != VNOVAL) { 844 np->n_size = np->n_vattr.va_size = tsize; 845 nfs_meta_setsize(vp, td, np->n_size, 0); 846 } 847 lwkt_reltoken(&nmp->nm_token); 848 849 return (error); 850 } 851 852 /* 853 * Do an nfs setattr rpc. 854 */ 855 static int 856 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, 857 struct ucred *cred, struct thread *td) 858 { 859 struct nfsv2_sattr *sp; 860 struct nfsnode *np = VTONFS(vp); 861 u_int32_t *tl; 862 int error = 0, wccflag = NFSV3_WCCRATTR; 863 struct nfsm_info info; 864 865 info.mrep = NULL; 866 info.v3 = NFS_ISV3(vp); 867 868 nfsstats.rpccnt[NFSPROC_SETATTR]++; 869 nfsm_reqhead(&info, vp, NFSPROC_SETATTR, 870 NFSX_FH(info.v3) + NFSX_SATTR(info.v3)); 871 ERROROUT(nfsm_fhtom(&info, vp)); 872 if (info.v3) { 873 nfsm_v3attrbuild(&info, vap, TRUE); 874 tl = nfsm_build(&info, NFSX_UNSIGNED); 875 *tl = nfs_false; 876 } else { 877 sp = nfsm_build(&info, NFSX_V2SATTR); 878 if (vap->va_mode == (mode_t)VNOVAL) 879 sp->sa_mode = nfs_xdrneg1; 880 else 881 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode); 882 if (vap->va_uid == (uid_t)VNOVAL) 883 sp->sa_uid = nfs_xdrneg1; 884 else 885 sp->sa_uid = txdr_unsigned(vap->va_uid); 886 if (vap->va_gid == (gid_t)VNOVAL) 887 sp->sa_gid = nfs_xdrneg1; 888 else 889 sp->sa_gid = txdr_unsigned(vap->va_gid); 890 sp->sa_size = txdr_unsigned(vap->va_size); 891 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 892 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 893 } 894 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error)); 895 if (info.v3) { 896 np->n_modestamp = 0; 897 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag)); 898 } else { 899 ERROROUT(nfsm_loadattr(&info, vp, NULL)); 900 } 901 m_freem(info.mrep); 902 info.mrep = NULL; 903 nfsmout: 904 return (error); 905 } 906 907 static 908 void 909 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout) 910 { 911 if (nctimeout == 0) 912 nctimeout = 1; 913 else 914 nctimeout *= hz; 915 cache_setvp(nch, vp); 916 cache_settimeout(nch, nctimeout); 917 } 918 919 /* 920 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove 921 * nfs_lookup() until all remaining new api calls are implemented. 922 * 923 * Resolve a namecache entry. This function is passed a locked ncp and 924 * must call nfs_cache_setvp() on it as appropriate to resolve the entry. 925 */ 926 static int 927 nfs_nresolve(struct vop_nresolve_args *ap) 928 { 929 struct thread *td = curthread; 930 struct namecache *ncp; 931 struct nfsmount *nmp; 932 struct ucred *cred; 933 struct nfsnode *np; 934 struct vnode *dvp; 935 struct vnode *nvp; 936 nfsfh_t *fhp; 937 int attrflag; 938 int fhsize; 939 int error; 940 int tmp_error; 941 int len; 942 struct nfsm_info info; 943 944 cred = ap->a_cred; 945 dvp = ap->a_dvp; 946 nmp = VFSTONFS(dvp->v_mount); 947 948 lwkt_gettoken(&nmp->nm_token); 949 950 if ((error = vget(dvp, LK_SHARED)) != 0) { 951 lwkt_reltoken(&nmp->nm_token); 952 return (error); 953 } 954 955 info.mrep = NULL; 956 info.v3 = NFS_ISV3(dvp); 957 958 nvp = NULL; 959 nfsstats.lookupcache_misses++; 960 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 961 ncp = ap->a_nch->ncp; 962 len = ncp->nc_nlen; 963 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP, 964 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 965 ERROROUT(nfsm_fhtom(&info, dvp)); 966 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN)); 967 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, 968 ap->a_cred, &error)); 969 if (error) { 970 /* 971 * Cache negatve lookups to reduce NFS traffic, but use 972 * a fast timeout. Otherwise use a timeout of 1 tick. 973 * XXX we should add a namecache flag for no-caching 974 * to uncache the negative hit as soon as possible, but 975 * we cannot simply destroy the entry because it is used 976 * as a placeholder by the caller. 977 * 978 * The refactored nfs code will overwrite a non-zero error 979 * with 0 when we use ERROROUT(), so don't here. 980 */ 981 if (error == ENOENT) 982 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout); 983 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag, 984 NFS_LATTR_NOSHRINK); 985 if (tmp_error) { 986 error = tmp_error; 987 goto nfsmout; 988 } 989 m_freem(info.mrep); 990 info.mrep = NULL; 991 goto nfsmout; 992 } 993 994 /* 995 * Success, get the file handle, do various checks, and load 996 * post-operation data from the reply packet. Theoretically 997 * we should never be looking up "." so, theoretically, we 998 * should never get the same file handle as our directory. But 999 * we check anyway. XXX 1000 * 1001 * Note that no timeout is set for the positive cache hit. We 1002 * assume, theoretically, that ESTALE returns will be dealt with 1003 * properly to handle NFS races and in anycase we cannot depend 1004 * on a timeout to deal with NFS open/create/excl issues so instead 1005 * of a bad hack here the rest of the NFS client code needs to do 1006 * the right thing. 1007 */ 1008 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp)); 1009 1010 np = VTONFS(dvp); 1011 if (NFS_CMPFH(np, fhp, fhsize)) { 1012 vref(dvp); 1013 nvp = dvp; 1014 } else { 1015 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 1016 if (error) { 1017 m_freem(info.mrep); 1018 info.mrep = NULL; 1019 vput(dvp); 1020 lwkt_reltoken(&nmp->nm_token); 1021 return (error); 1022 } 1023 nvp = NFSTOV(np); 1024 } 1025 if (info.v3) { 1026 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag, 1027 NFS_LATTR_NOSHRINK)); 1028 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag, 1029 NFS_LATTR_NOSHRINK)); 1030 } else { 1031 ERROROUT(nfsm_loadattr(&info, nvp, NULL)); 1032 } 1033 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout); 1034 m_freem(info.mrep); 1035 info.mrep = NULL; 1036 nfsmout: 1037 lwkt_reltoken(&nmp->nm_token); 1038 vput(dvp); 1039 if (nvp) { 1040 if (nvp == dvp) 1041 vrele(nvp); 1042 else 1043 vput(nvp); 1044 } 1045 return (error); 1046 } 1047 1048 /* 1049 * 'cached' nfs directory lookup 1050 * 1051 * NOTE: cannot be removed until NFS implements all the new n*() API calls. 1052 * 1053 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp, 1054 * struct componentname *a_cnp) 1055 */ 1056 static int 1057 nfs_lookup(struct vop_old_lookup_args *ap) 1058 { 1059 struct componentname *cnp = ap->a_cnp; 1060 struct vnode *dvp = ap->a_dvp; 1061 struct vnode **vpp = ap->a_vpp; 1062 int flags = cnp->cn_flags; 1063 struct vnode *newvp; 1064 struct nfsmount *nmp; 1065 long len; 1066 nfsfh_t *fhp; 1067 struct nfsnode *np; 1068 int lockparent, wantparent, attrflag, fhsize; 1069 int error; 1070 int tmp_error; 1071 struct nfsm_info info; 1072 1073 info.mrep = NULL; 1074 info.v3 = NFS_ISV3(dvp); 1075 error = 0; 1076 1077 /* 1078 * Read-only mount check and directory check. 1079 */ 1080 *vpp = NULLVP; 1081 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) && 1082 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) 1083 return (EROFS); 1084 1085 if (dvp->v_type != VDIR) 1086 return (ENOTDIR); 1087 1088 /* 1089 * Look it up in the cache. Note that ENOENT is only returned if we 1090 * previously entered a negative hit (see later on). The additional 1091 * nfsneg_cache_timeout check causes previously cached results to 1092 * be instantly ignored if the negative caching is turned off. 1093 */ 1094 lockparent = flags & CNP_LOCKPARENT; 1095 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT); 1096 nmp = VFSTONFS(dvp->v_mount); 1097 np = VTONFS(dvp); 1098 1099 lwkt_gettoken(&nmp->nm_token); 1100 1101 /* 1102 * Go to the wire. 1103 */ 1104 error = 0; 1105 newvp = NULLVP; 1106 nfsstats.lookupcache_misses++; 1107 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 1108 len = cnp->cn_namelen; 1109 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP, 1110 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 1111 ERROROUT(nfsm_fhtom(&info, dvp)); 1112 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN)); 1113 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td, 1114 cnp->cn_cred, &error)); 1115 if (error) { 1116 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag, 1117 NFS_LATTR_NOSHRINK); 1118 if (tmp_error) { 1119 error = tmp_error; 1120 goto nfsmout; 1121 } 1122 1123 m_freem(info.mrep); 1124 info.mrep = NULL; 1125 goto nfsmout; 1126 } 1127 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp)); 1128 1129 /* 1130 * Handle RENAME case... 1131 */ 1132 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) { 1133 if (NFS_CMPFH(np, fhp, fhsize)) { 1134 m_freem(info.mrep); 1135 info.mrep = NULL; 1136 lwkt_reltoken(&nmp->nm_token); 1137 return (EISDIR); 1138 } 1139 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 1140 if (error) { 1141 m_freem(info.mrep); 1142 info.mrep = NULL; 1143 lwkt_reltoken(&nmp->nm_token); 1144 return (error); 1145 } 1146 newvp = NFSTOV(np); 1147 if (info.v3) { 1148 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag, 1149 NFS_LATTR_NOSHRINK)); 1150 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag, 1151 NFS_LATTR_NOSHRINK)); 1152 } else { 1153 ERROROUT(nfsm_loadattr(&info, newvp, NULL)); 1154 } 1155 *vpp = newvp; 1156 m_freem(info.mrep); 1157 info.mrep = NULL; 1158 if (!lockparent) { 1159 vn_unlock(dvp); 1160 cnp->cn_flags |= CNP_PDIRUNLOCK; 1161 } 1162 lwkt_reltoken(&nmp->nm_token); 1163 return (0); 1164 } 1165 1166 if (flags & CNP_ISDOTDOT) { 1167 vn_unlock(dvp); 1168 cnp->cn_flags |= CNP_PDIRUNLOCK; 1169 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 1170 if (error) { 1171 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1172 cnp->cn_flags &= ~CNP_PDIRUNLOCK; 1173 lwkt_reltoken(&nmp->nm_token); 1174 return (error); /* NOTE: return error from nget */ 1175 } 1176 newvp = NFSTOV(np); 1177 if (lockparent) { 1178 error = vn_lock(dvp, LK_EXCLUSIVE); 1179 if (error) { 1180 vput(newvp); 1181 lwkt_reltoken(&nmp->nm_token); 1182 return (error); 1183 } 1184 cnp->cn_flags |= CNP_PDIRUNLOCK; 1185 } 1186 } else if (NFS_CMPFH(np, fhp, fhsize)) { 1187 vref(dvp); 1188 newvp = dvp; 1189 } else { 1190 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 1191 if (error) { 1192 m_freem(info.mrep); 1193 info.mrep = NULL; 1194 lwkt_reltoken(&nmp->nm_token); 1195 return (error); 1196 } 1197 if (!lockparent) { 1198 vn_unlock(dvp); 1199 cnp->cn_flags |= CNP_PDIRUNLOCK; 1200 } 1201 newvp = NFSTOV(np); 1202 } 1203 if (info.v3) { 1204 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag, 1205 NFS_LATTR_NOSHRINK)); 1206 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag, 1207 NFS_LATTR_NOSHRINK)); 1208 } else { 1209 ERROROUT(nfsm_loadattr(&info, newvp, NULL)); 1210 } 1211 #if 0 1212 /* XXX MOVE TO nfs_nremove() */ 1213 if ((cnp->cn_flags & CNP_MAKEENTRY) && 1214 cnp->cn_nameiop != NAMEI_DELETE) { 1215 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */ 1216 } 1217 #endif 1218 *vpp = newvp; 1219 m_freem(info.mrep); 1220 info.mrep = NULL; 1221 nfsmout: 1222 if (error) { 1223 if (newvp != NULLVP) { 1224 vrele(newvp); 1225 *vpp = NULLVP; 1226 } 1227 if ((cnp->cn_nameiop == NAMEI_CREATE || 1228 cnp->cn_nameiop == NAMEI_RENAME) && 1229 error == ENOENT) { 1230 if (!lockparent) { 1231 vn_unlock(dvp); 1232 cnp->cn_flags |= CNP_PDIRUNLOCK; 1233 } 1234 if (dvp->v_mount->mnt_flag & MNT_RDONLY) 1235 error = EROFS; 1236 else 1237 error = EJUSTRETURN; 1238 } 1239 } 1240 lwkt_reltoken(&nmp->nm_token); 1241 return (error); 1242 } 1243 1244 /* 1245 * nfs read call. 1246 * Just call nfs_bioread() to do the work. 1247 * 1248 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 1249 * struct ucred *a_cred) 1250 */ 1251 static int 1252 nfs_read(struct vop_read_args *ap) 1253 { 1254 struct vnode *vp = ap->a_vp; 1255 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1256 int error; 1257 1258 lwkt_gettoken(&nmp->nm_token); 1259 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag); 1260 lwkt_reltoken(&nmp->nm_token); 1261 1262 return error; 1263 } 1264 1265 /* 1266 * nfs readlink call 1267 * 1268 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred) 1269 */ 1270 static int 1271 nfs_readlink(struct vop_readlink_args *ap) 1272 { 1273 struct vnode *vp = ap->a_vp; 1274 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1275 int error; 1276 1277 if (vp->v_type != VLNK) 1278 return (EINVAL); 1279 1280 lwkt_gettoken(&nmp->nm_token); 1281 error = nfs_bioread(vp, ap->a_uio, 0); 1282 lwkt_reltoken(&nmp->nm_token); 1283 1284 return error; 1285 } 1286 1287 /* 1288 * Do a readlink rpc. 1289 * Called by nfs_doio() from below the buffer cache. 1290 */ 1291 int 1292 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop) 1293 { 1294 int error = 0, len, attrflag; 1295 struct nfsm_info info; 1296 1297 info.mrep = NULL; 1298 info.v3 = NFS_ISV3(vp); 1299 1300 nfsstats.rpccnt[NFSPROC_READLINK]++; 1301 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3)); 1302 ERROROUT(nfsm_fhtom(&info, vp)); 1303 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td, 1304 nfs_vpcred(vp, ND_CHECK), &error)); 1305 if (info.v3) { 1306 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, 1307 NFS_LATTR_NOSHRINK)); 1308 } 1309 if (!error) { 1310 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN)); 1311 if (len == NFS_MAXPATHLEN) { 1312 struct nfsnode *np = VTONFS(vp); 1313 if (np->n_size && np->n_size < NFS_MAXPATHLEN) 1314 len = np->n_size; 1315 } 1316 ERROROUT(nfsm_mtouio(&info, uiop, len)); 1317 } 1318 m_freem(info.mrep); 1319 info.mrep = NULL; 1320 nfsmout: 1321 return (error); 1322 } 1323 1324 /* 1325 * nfs synchronous read rpc using UIO 1326 */ 1327 int 1328 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop) 1329 { 1330 u_int32_t *tl; 1331 struct nfsmount *nmp; 1332 int error = 0, len, retlen, tsiz, eof, attrflag; 1333 struct nfsm_info info; 1334 off_t tmp_off; 1335 1336 info.mrep = NULL; 1337 info.v3 = NFS_ISV3(vp); 1338 1339 #ifndef nolint 1340 eof = 0; 1341 #endif 1342 nmp = VFSTONFS(vp->v_mount); 1343 1344 tsiz = uiop->uio_resid; 1345 tmp_off = uiop->uio_offset + tsiz; 1346 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) 1347 return (EFBIG); 1348 tmp_off = uiop->uio_offset; 1349 while (tsiz > 0) { 1350 nfsstats.rpccnt[NFSPROC_READ]++; 1351 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 1352 nfsm_reqhead(&info, vp, NFSPROC_READ, 1353 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3); 1354 ERROROUT(nfsm_fhtom(&info, vp)); 1355 tl = nfsm_build(&info, NFSX_UNSIGNED * 3); 1356 if (info.v3) { 1357 txdr_hyper(uiop->uio_offset, tl); 1358 *(tl + 2) = txdr_unsigned(len); 1359 } else { 1360 *tl++ = txdr_unsigned(uiop->uio_offset); 1361 *tl++ = txdr_unsigned(len); 1362 *tl = 0; 1363 } 1364 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td, 1365 nfs_vpcred(vp, ND_READ), &error)); 1366 if (info.v3) { 1367 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, 1368 NFS_LATTR_NOSHRINK)); 1369 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 1370 eof = fxdr_unsigned(int, *(tl + 1)); 1371 } else { 1372 ERROROUT(nfsm_loadattr(&info, vp, NULL)); 1373 } 1374 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len)); 1375 ERROROUT(nfsm_mtouio(&info, uiop, retlen)); 1376 m_freem(info.mrep); 1377 info.mrep = NULL; 1378 1379 /* 1380 * Handle short-read from server (NFSv3). If EOF is not 1381 * flagged (and no error occurred), but retlen is less 1382 * then the request size, we must zero-fill the remainder. 1383 */ 1384 if (retlen < len && info.v3 && eof == 0) { 1385 ERROROUT(uiomovez(len - retlen, uiop)); 1386 retlen = len; 1387 } 1388 tsiz -= retlen; 1389 1390 /* 1391 * Terminate loop on EOF or zero-length read. 1392 * 1393 * For NFSv2 a short-read indicates EOF, not zero-fill, 1394 * and also terminates the loop. 1395 */ 1396 if (info.v3) { 1397 if (eof || retlen == 0) 1398 tsiz = 0; 1399 } else if (retlen < len) { 1400 tsiz = 0; 1401 } 1402 } 1403 nfsmout: 1404 return (error); 1405 } 1406 1407 /* 1408 * nfs write call 1409 */ 1410 int 1411 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop, 1412 int *iomode, int *must_commit) 1413 { 1414 u_int32_t *tl; 1415 int32_t backup; 1416 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1417 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit; 1418 int committed = NFSV3WRITE_FILESYNC; 1419 struct nfsm_info info; 1420 1421 info.mrep = NULL; 1422 info.v3 = NFS_ISV3(vp); 1423 1424 #ifndef DIAGNOSTIC 1425 if (uiop->uio_iovcnt != 1) 1426 panic("nfs: writerpc iovcnt > 1"); 1427 #endif 1428 *must_commit = 0; 1429 tsiz = uiop->uio_resid; 1430 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1431 return (EFBIG); 1432 while (tsiz > 0) { 1433 nfsstats.rpccnt[NFSPROC_WRITE]++; 1434 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz; 1435 nfsm_reqhead(&info, vp, NFSPROC_WRITE, 1436 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED + 1437 nfsm_rndup(len)); 1438 ERROROUT(nfsm_fhtom(&info, vp)); 1439 if (info.v3) { 1440 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED); 1441 txdr_hyper(uiop->uio_offset, tl); 1442 tl += 2; 1443 *tl++ = txdr_unsigned(len); 1444 *tl++ = txdr_unsigned(*iomode); 1445 *tl = txdr_unsigned(len); 1446 } else { 1447 u_int32_t x; 1448 1449 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED); 1450 /* Set both "begin" and "current" to non-garbage. */ 1451 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1452 *tl++ = x; /* "begin offset" */ 1453 *tl++ = x; /* "current offset" */ 1454 x = txdr_unsigned(len); 1455 *tl++ = x; /* total to this offset */ 1456 *tl = x; /* size of this write */ 1457 } 1458 ERROROUT(nfsm_uiotom(&info, uiop, len)); 1459 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td, 1460 nfs_vpcred(vp, ND_WRITE), &error)); 1461 if (info.v3) { 1462 /* 1463 * The write RPC returns a before and after mtime. The 1464 * nfsm_wcc_data() macro checks the before n_mtime 1465 * against the before time and stores the after time 1466 * in the nfsnode's cached vattr and n_mtime field. 1467 * The NRMODIFIED bit will be set if the before 1468 * time did not match the original mtime. 1469 */ 1470 wccflag = NFSV3_WCCCHK; 1471 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag)); 1472 if (error == 0) { 1473 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF)); 1474 rlen = fxdr_unsigned(int, *tl++); 1475 if (rlen == 0) { 1476 error = NFSERR_IO; 1477 m_freem(info.mrep); 1478 info.mrep = NULL; 1479 break; 1480 } else if (rlen < len) { 1481 backup = len - rlen; 1482 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup; 1483 uiop->uio_iov->iov_len += backup; 1484 uiop->uio_offset -= backup; 1485 uiop->uio_resid += backup; 1486 len = rlen; 1487 } 1488 commit = fxdr_unsigned(int, *tl++); 1489 1490 /* 1491 * Return the lowest committment level 1492 * obtained by any of the RPCs. 1493 */ 1494 if (committed == NFSV3WRITE_FILESYNC) 1495 committed = commit; 1496 else if (committed == NFSV3WRITE_DATASYNC && 1497 commit == NFSV3WRITE_UNSTABLE) 1498 committed = commit; 1499 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){ 1500 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1501 NFSX_V3WRITEVERF); 1502 nmp->nm_state |= NFSSTA_HASWRITEVERF; 1503 } else if (bcmp((caddr_t)tl, 1504 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) { 1505 *must_commit = 1; 1506 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1507 NFSX_V3WRITEVERF); 1508 } 1509 } 1510 } else { 1511 ERROROUT(nfsm_loadattr(&info, vp, NULL)); 1512 } 1513 m_freem(info.mrep); 1514 info.mrep = NULL; 1515 if (error) 1516 break; 1517 tsiz -= len; 1518 } 1519 nfsmout: 1520 if (vp->v_mount->mnt_flag & MNT_ASYNC) 1521 committed = NFSV3WRITE_FILESYNC; 1522 *iomode = committed; 1523 if (error) 1524 uiop->uio_resid = tsiz; 1525 return (error); 1526 } 1527 1528 /* 1529 * nfs mknod rpc 1530 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1531 * mode set to specify the file type and the size field for rdev. 1532 */ 1533 static int 1534 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1535 struct vattr *vap) 1536 { 1537 struct nfsv2_sattr *sp; 1538 u_int32_t *tl; 1539 struct vnode *newvp = NULL; 1540 struct nfsnode *np = NULL; 1541 struct vattr vattr; 1542 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0; 1543 int rmajor, rminor; 1544 struct nfsm_info info; 1545 1546 info.mrep = NULL; 1547 info.v3 = NFS_ISV3(dvp); 1548 1549 if (vap->va_type == VCHR || vap->va_type == VBLK) { 1550 rmajor = txdr_unsigned(vap->va_rmajor); 1551 rminor = txdr_unsigned(vap->va_rminor); 1552 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) { 1553 rmajor = nfs_xdrneg1; 1554 rminor = nfs_xdrneg1; 1555 } else { 1556 return (EOPNOTSUPP); 1557 } 1558 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) { 1559 return (error); 1560 } 1561 nfsstats.rpccnt[NFSPROC_MKNOD]++; 1562 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD, 1563 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED + 1564 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3)); 1565 ERROROUT(nfsm_fhtom(&info, dvp)); 1566 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen, 1567 NFS_MAXNAMLEN)); 1568 if (info.v3) { 1569 tl = nfsm_build(&info, NFSX_UNSIGNED); 1570 *tl++ = vtonfsv3_type(vap->va_type); 1571 nfsm_v3attrbuild(&info, vap, FALSE); 1572 if (vap->va_type == VCHR || vap->va_type == VBLK) { 1573 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED); 1574 *tl++ = txdr_unsigned(vap->va_rmajor); 1575 *tl = txdr_unsigned(vap->va_rminor); 1576 } 1577 } else { 1578 sp = nfsm_build(&info, NFSX_V2SATTR); 1579 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1580 sp->sa_uid = nfs_xdrneg1; 1581 sp->sa_gid = nfs_xdrneg1; 1582 sp->sa_size = makeudev(rmajor, rminor); 1583 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1584 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1585 } 1586 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td, 1587 cnp->cn_cred, &error)); 1588 if (!error) { 1589 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp)); 1590 if (!gotvp) { 1591 if (newvp) { 1592 vput(newvp); 1593 newvp = NULL; 1594 } 1595 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1596 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np); 1597 if (!error) 1598 newvp = NFSTOV(np); 1599 } 1600 } 1601 if (info.v3) { 1602 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag)); 1603 } 1604 m_freem(info.mrep); 1605 info.mrep = NULL; 1606 nfsmout: 1607 if (error) { 1608 if (newvp) 1609 vput(newvp); 1610 } else { 1611 *vpp = newvp; 1612 } 1613 VTONFS(dvp)->n_flag |= NLMODIFIED; 1614 if (!wccflag) 1615 VTONFS(dvp)->n_attrstamp = 0; 1616 return (error); 1617 } 1618 1619 /* 1620 * nfs mknod vop 1621 * just call nfs_mknodrpc() to do the work. 1622 * 1623 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp, 1624 * struct componentname *a_cnp, struct vattr *a_vap) 1625 */ 1626 /* ARGSUSED */ 1627 static int 1628 nfs_mknod(struct vop_old_mknod_args *ap) 1629 { 1630 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount); 1631 int error; 1632 1633 lwkt_gettoken(&nmp->nm_token); 1634 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap); 1635 lwkt_reltoken(&nmp->nm_token); 1636 1637 return error; 1638 } 1639 1640 static u_long create_verf; 1641 /* 1642 * nfs file create call 1643 * 1644 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp, 1645 * struct componentname *a_cnp, struct vattr *a_vap) 1646 */ 1647 static int 1648 nfs_create(struct vop_old_create_args *ap) 1649 { 1650 struct vnode *dvp = ap->a_dvp; 1651 struct vattr *vap = ap->a_vap; 1652 struct nfsmount *nmp = VFSTONFS(dvp->v_mount); 1653 struct componentname *cnp = ap->a_cnp; 1654 struct nfsv2_sattr *sp; 1655 u_int32_t *tl; 1656 struct nfsnode *np = NULL; 1657 struct vnode *newvp = NULL; 1658 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0; 1659 struct vattr vattr; 1660 struct nfsm_info info; 1661 1662 info.mrep = NULL; 1663 info.v3 = NFS_ISV3(dvp); 1664 lwkt_gettoken(&nmp->nm_token); 1665 1666 /* 1667 * Oops, not for me.. 1668 */ 1669 if (vap->va_type == VSOCK) { 1670 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap); 1671 lwkt_reltoken(&nmp->nm_token); 1672 return error; 1673 } 1674 1675 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) { 1676 lwkt_reltoken(&nmp->nm_token); 1677 return (error); 1678 } 1679 if (vap->va_vaflags & VA_EXCLUSIVE) 1680 fmode |= O_EXCL; 1681 again: 1682 nfsstats.rpccnt[NFSPROC_CREATE]++; 1683 nfsm_reqhead(&info, dvp, NFSPROC_CREATE, 1684 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED + 1685 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3)); 1686 ERROROUT(nfsm_fhtom(&info, dvp)); 1687 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen, 1688 NFS_MAXNAMLEN)); 1689 if (info.v3) { 1690 tl = nfsm_build(&info, NFSX_UNSIGNED); 1691 if (fmode & O_EXCL) { 1692 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE); 1693 tl = nfsm_build(&info, NFSX_V3CREATEVERF); 1694 #ifdef INET 1695 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) 1696 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr; 1697 else 1698 #endif 1699 *tl++ = create_verf; 1700 *tl = ++create_verf; 1701 } else { 1702 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED); 1703 nfsm_v3attrbuild(&info, vap, FALSE); 1704 } 1705 } else { 1706 sp = nfsm_build(&info, NFSX_V2SATTR); 1707 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1708 sp->sa_uid = nfs_xdrneg1; 1709 sp->sa_gid = nfs_xdrneg1; 1710 sp->sa_size = 0; 1711 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1712 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1713 } 1714 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td, 1715 cnp->cn_cred, &error)); 1716 if (error == 0) { 1717 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp)); 1718 if (!gotvp) { 1719 if (newvp) { 1720 vput(newvp); 1721 newvp = NULL; 1722 } 1723 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1724 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np); 1725 if (!error) 1726 newvp = NFSTOV(np); 1727 } 1728 } 1729 if (info.v3) { 1730 if (error == 0) 1731 error = nfsm_wcc_data(&info, dvp, &wccflag); 1732 else 1733 (void)nfsm_wcc_data(&info, dvp, &wccflag); 1734 } 1735 m_freem(info.mrep); 1736 info.mrep = NULL; 1737 nfsmout: 1738 if (error) { 1739 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) { 1740 KKASSERT(newvp == NULL); 1741 fmode &= ~O_EXCL; 1742 goto again; 1743 } 1744 } else if (info.v3 && (fmode & O_EXCL)) { 1745 /* 1746 * We are normally called with only a partially initialized 1747 * VAP. Since the NFSv3 spec says that server may use the 1748 * file attributes to store the verifier, the spec requires 1749 * us to do a SETATTR RPC. FreeBSD servers store the verifier 1750 * in atime, but we can't really assume that all servers will 1751 * so we ensure that our SETATTR sets both atime and mtime. 1752 */ 1753 if (vap->va_mtime.tv_sec == VNOVAL) 1754 vfs_timestamp(&vap->va_mtime); 1755 if (vap->va_atime.tv_sec == VNOVAL) 1756 vap->va_atime = vap->va_mtime; 1757 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td); 1758 } 1759 if (error == 0) { 1760 /* 1761 * The new np may have enough info for access 1762 * checks, make sure rucred and wucred are 1763 * initialized for read and write rpc's. 1764 */ 1765 np = VTONFS(newvp); 1766 if (np->n_rucred == NULL) 1767 np->n_rucred = crhold(cnp->cn_cred); 1768 if (np->n_wucred == NULL) 1769 np->n_wucred = crhold(cnp->cn_cred); 1770 *ap->a_vpp = newvp; 1771 } else if (newvp) { 1772 vput(newvp); 1773 } 1774 VTONFS(dvp)->n_flag |= NLMODIFIED; 1775 if (!wccflag) 1776 VTONFS(dvp)->n_attrstamp = 0; 1777 lwkt_reltoken(&nmp->nm_token); 1778 return (error); 1779 } 1780 1781 /* 1782 * nfs file remove call 1783 * To try and make nfs semantics closer to ufs semantics, a file that has 1784 * other processes using the vnode is renamed instead of removed and then 1785 * removed later on the last close. 1786 * - If v_sysref.refcnt > 1 1787 * If a rename is not already in the works 1788 * call nfs_sillyrename() to set it up 1789 * else 1790 * do the remove rpc 1791 * 1792 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp, 1793 * struct componentname *a_cnp) 1794 */ 1795 static int 1796 nfs_remove(struct vop_old_remove_args *ap) 1797 { 1798 struct vnode *vp = ap->a_vp; 1799 struct vnode *dvp = ap->a_dvp; 1800 struct nfsmount *nmp = VFSTONFS(dvp->v_mount); 1801 struct componentname *cnp = ap->a_cnp; 1802 struct nfsnode *np = VTONFS(vp); 1803 int error = 0; 1804 struct vattr vattr; 1805 1806 lwkt_gettoken(&nmp->nm_token); 1807 #ifndef DIAGNOSTIC 1808 if (vp->v_sysref.refcnt < 1) 1809 panic("nfs_remove: bad v_sysref.refcnt"); 1810 #endif 1811 if (vp->v_type == VDIR) { 1812 error = EPERM; 1813 } else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename && 1814 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) { 1815 /* 1816 * throw away biocache buffers, mainly to avoid 1817 * unnecessary delayed writes later. 1818 */ 1819 error = nfs_vinvalbuf(vp, 0, 1); 1820 /* Do the rpc */ 1821 if (error != EINTR) 1822 error = nfs_removerpc(dvp, cnp->cn_nameptr, 1823 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td); 1824 /* 1825 * Kludge City: If the first reply to the remove rpc is lost.. 1826 * the reply to the retransmitted request will be ENOENT 1827 * since the file was in fact removed 1828 * Therefore, we cheat and return success. 1829 */ 1830 if (error == ENOENT) 1831 error = 0; 1832 } else if (!np->n_sillyrename) { 1833 error = nfs_sillyrename(dvp, vp, cnp); 1834 } 1835 np->n_attrstamp = 0; 1836 lwkt_reltoken(&nmp->nm_token); 1837 1838 return (error); 1839 } 1840 1841 /* 1842 * nfs file remove rpc called from nfs_inactive 1843 */ 1844 int 1845 nfs_removeit(struct sillyrename *sp) 1846 { 1847 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, 1848 sp->s_cred, NULL)); 1849 } 1850 1851 /* 1852 * Nfs remove rpc, called from nfs_remove() and nfs_removeit(). 1853 */ 1854 static int 1855 nfs_removerpc(struct vnode *dvp, const char *name, int namelen, 1856 struct ucred *cred, struct thread *td) 1857 { 1858 int error = 0, wccflag = NFSV3_WCCRATTR; 1859 struct nfsm_info info; 1860 1861 info.mrep = NULL; 1862 info.v3 = NFS_ISV3(dvp); 1863 1864 nfsstats.rpccnt[NFSPROC_REMOVE]++; 1865 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE, 1866 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen)); 1867 ERROROUT(nfsm_fhtom(&info, dvp)); 1868 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN)); 1869 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error)); 1870 if (info.v3) { 1871 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag)); 1872 } 1873 m_freem(info.mrep); 1874 info.mrep = NULL; 1875 nfsmout: 1876 VTONFS(dvp)->n_flag |= NLMODIFIED; 1877 if (!wccflag) 1878 VTONFS(dvp)->n_attrstamp = 0; 1879 return (error); 1880 } 1881 1882 /* 1883 * nfs file rename call 1884 * 1885 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp, 1886 * struct componentname *a_fcnp, struct vnode *a_tdvp, 1887 * struct vnode *a_tvp, struct componentname *a_tcnp) 1888 */ 1889 static int 1890 nfs_rename(struct vop_old_rename_args *ap) 1891 { 1892 struct vnode *fvp = ap->a_fvp; 1893 struct vnode *tvp = ap->a_tvp; 1894 struct vnode *fdvp = ap->a_fdvp; 1895 struct vnode *tdvp = ap->a_tdvp; 1896 struct componentname *tcnp = ap->a_tcnp; 1897 struct componentname *fcnp = ap->a_fcnp; 1898 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount); 1899 int error; 1900 1901 lwkt_gettoken(&nmp->nm_token); 1902 1903 /* Check for cross-device rename */ 1904 if ((fvp->v_mount != tdvp->v_mount) || 1905 (tvp && (fvp->v_mount != tvp->v_mount))) { 1906 error = EXDEV; 1907 goto out; 1908 } 1909 1910 /* 1911 * We shouldn't have to flush fvp on rename for most server-side 1912 * filesystems as the file handle should not change. Unfortunately 1913 * the inode for some filesystems (msdosfs) might be tied to the 1914 * file name or directory position so to be completely safe 1915 * vfs.nfs.flush_on_rename is set by default. Clear to improve 1916 * performance. 1917 * 1918 * We must flush tvp on rename because it might become stale on the 1919 * server after the rename. 1920 */ 1921 if (nfs_flush_on_rename) 1922 VOP_FSYNC(fvp, MNT_WAIT, 0); 1923 if (tvp) 1924 VOP_FSYNC(tvp, MNT_WAIT, 0); 1925 1926 /* 1927 * If the tvp exists and is in use, sillyrename it before doing the 1928 * rename of the new file over it. 1929 * 1930 * XXX Can't sillyrename a directory. 1931 * 1932 * We do not attempt to do any namecache purges in this old API 1933 * routine. The new API compat functions have access to the actual 1934 * namecache structures and will do it for us. 1935 */ 1936 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename && 1937 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { 1938 vput(tvp); 1939 tvp = NULL; 1940 } else if (tvp) { 1941 ; 1942 } 1943 1944 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen, 1945 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, 1946 tcnp->cn_td); 1947 1948 out: 1949 lwkt_reltoken(&nmp->nm_token); 1950 if (tdvp == tvp) 1951 vrele(tdvp); 1952 else 1953 vput(tdvp); 1954 if (tvp) 1955 vput(tvp); 1956 vrele(fdvp); 1957 vrele(fvp); 1958 /* 1959 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. 1960 */ 1961 if (error == ENOENT) 1962 error = 0; 1963 return (error); 1964 } 1965 1966 /* 1967 * nfs file rename rpc called from nfs_remove() above 1968 */ 1969 static int 1970 nfs_renameit(struct vnode *sdvp, struct componentname *scnp, 1971 struct sillyrename *sp) 1972 { 1973 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, 1974 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td)); 1975 } 1976 1977 /* 1978 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit(). 1979 */ 1980 static int 1981 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen, 1982 struct vnode *tdvp, const char *tnameptr, int tnamelen, 1983 struct ucred *cred, struct thread *td) 1984 { 1985 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR; 1986 struct nfsm_info info; 1987 1988 info.mrep = NULL; 1989 info.v3 = NFS_ISV3(fdvp); 1990 1991 nfsstats.rpccnt[NFSPROC_RENAME]++; 1992 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME, 1993 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 + 1994 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen)); 1995 ERROROUT(nfsm_fhtom(&info, fdvp)); 1996 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN)); 1997 ERROROUT(nfsm_fhtom(&info, tdvp)); 1998 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN)); 1999 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error)); 2000 if (info.v3) { 2001 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag)); 2002 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag)); 2003 } 2004 m_freem(info.mrep); 2005 info.mrep = NULL; 2006 nfsmout: 2007 VTONFS(fdvp)->n_flag |= NLMODIFIED; 2008 VTONFS(tdvp)->n_flag |= NLMODIFIED; 2009 if (!fwccflag) 2010 VTONFS(fdvp)->n_attrstamp = 0; 2011 if (!twccflag) 2012 VTONFS(tdvp)->n_attrstamp = 0; 2013 return (error); 2014 } 2015 2016 /* 2017 * nfs hard link create call 2018 * 2019 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp, 2020 * struct componentname *a_cnp) 2021 */ 2022 static int 2023 nfs_link(struct vop_old_link_args *ap) 2024 { 2025 struct vnode *vp = ap->a_vp; 2026 struct vnode *tdvp = ap->a_tdvp; 2027 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount); 2028 struct componentname *cnp = ap->a_cnp; 2029 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0; 2030 struct nfsm_info info; 2031 2032 if (vp->v_mount != tdvp->v_mount) { 2033 return (EXDEV); 2034 } 2035 lwkt_gettoken(&nmp->nm_token); 2036 2037 /* 2038 * The attribute cache may get out of sync with the server on link. 2039 * Pushing writes to the server before handle was inherited from 2040 * long long ago and it is unclear if we still need to do this. 2041 * Defaults to off. 2042 */ 2043 if (nfs_flush_on_hlink) 2044 VOP_FSYNC(vp, MNT_WAIT, 0); 2045 2046 info.mrep = NULL; 2047 info.v3 = NFS_ISV3(vp); 2048 2049 nfsstats.rpccnt[NFSPROC_LINK]++; 2050 nfsm_reqhead(&info, vp, NFSPROC_LINK, 2051 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED + 2052 nfsm_rndup(cnp->cn_namelen)); 2053 ERROROUT(nfsm_fhtom(&info, vp)); 2054 ERROROUT(nfsm_fhtom(&info, tdvp)); 2055 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen, 2056 NFS_MAXNAMLEN)); 2057 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td, 2058 cnp->cn_cred, &error)); 2059 if (info.v3) { 2060 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, 2061 NFS_LATTR_NOSHRINK)); 2062 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag)); 2063 } 2064 m_freem(info.mrep); 2065 info.mrep = NULL; 2066 nfsmout: 2067 VTONFS(tdvp)->n_flag |= NLMODIFIED; 2068 if (!attrflag) 2069 VTONFS(vp)->n_attrstamp = 0; 2070 if (!wccflag) 2071 VTONFS(tdvp)->n_attrstamp = 0; 2072 /* 2073 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2074 */ 2075 if (error == EEXIST) 2076 error = 0; 2077 lwkt_reltoken(&nmp->nm_token); 2078 return (error); 2079 } 2080 2081 /* 2082 * nfs symbolic link create call 2083 * 2084 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp, 2085 * struct componentname *a_cnp, struct vattr *a_vap, 2086 * char *a_target) 2087 */ 2088 static int 2089 nfs_symlink(struct vop_old_symlink_args *ap) 2090 { 2091 struct vnode *dvp = ap->a_dvp; 2092 struct vattr *vap = ap->a_vap; 2093 struct nfsmount *nmp = VFSTONFS(dvp->v_mount); 2094 struct componentname *cnp = ap->a_cnp; 2095 struct nfsv2_sattr *sp; 2096 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp; 2097 struct vnode *newvp = NULL; 2098 struct nfsm_info info; 2099 2100 info.mrep = NULL; 2101 info.v3 = NFS_ISV3(dvp); 2102 lwkt_gettoken(&nmp->nm_token); 2103 2104 nfsstats.rpccnt[NFSPROC_SYMLINK]++; 2105 slen = strlen(ap->a_target); 2106 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK, 2107 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED + 2108 nfsm_rndup(cnp->cn_namelen) + 2109 nfsm_rndup(slen) + NFSX_SATTR(info.v3)); 2110 ERROROUT(nfsm_fhtom(&info, dvp)); 2111 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen, 2112 NFS_MAXNAMLEN)); 2113 if (info.v3) { 2114 nfsm_v3attrbuild(&info, vap, FALSE); 2115 } 2116 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN)); 2117 if (info.v3 == 0) { 2118 sp = nfsm_build(&info, NFSX_V2SATTR); 2119 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode); 2120 sp->sa_uid = nfs_xdrneg1; 2121 sp->sa_gid = nfs_xdrneg1; 2122 sp->sa_size = nfs_xdrneg1; 2123 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 2124 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 2125 } 2126 2127 /* 2128 * Issue the NFS request and get the rpc response. 2129 * 2130 * Only NFSv3 responses returning an error of 0 actually return 2131 * a file handle that can be converted into newvp without having 2132 * to do an extra lookup rpc. 2133 */ 2134 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td, 2135 cnp->cn_cred, &error)); 2136 if (info.v3) { 2137 if (error == 0) { 2138 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp)); 2139 } 2140 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag)); 2141 } 2142 2143 /* 2144 * out code jumps -> here, mrep is also freed. 2145 */ 2146 2147 m_freem(info.mrep); 2148 info.mrep = NULL; 2149 nfsmout: 2150 2151 /* 2152 * If we get an EEXIST error, silently convert it to no-error 2153 * in case of an NFS retry. 2154 */ 2155 if (error == EEXIST) 2156 error = 0; 2157 2158 /* 2159 * If we do not have (or no longer have) an error, and we could 2160 * not extract the newvp from the response due to the request being 2161 * NFSv2 or the error being EEXIST. We have to do a lookup in order 2162 * to obtain a newvp to return. 2163 */ 2164 if (error == 0 && newvp == NULL) { 2165 struct nfsnode *np = NULL; 2166 2167 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 2168 cnp->cn_cred, cnp->cn_td, &np); 2169 if (!error) 2170 newvp = NFSTOV(np); 2171 } 2172 if (error) { 2173 if (newvp) 2174 vput(newvp); 2175 } else { 2176 *ap->a_vpp = newvp; 2177 } 2178 VTONFS(dvp)->n_flag |= NLMODIFIED; 2179 if (!wccflag) 2180 VTONFS(dvp)->n_attrstamp = 0; 2181 lwkt_reltoken(&nmp->nm_token); 2182 2183 return (error); 2184 } 2185 2186 /* 2187 * nfs make dir call 2188 * 2189 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp, 2190 * struct componentname *a_cnp, struct vattr *a_vap) 2191 */ 2192 static int 2193 nfs_mkdir(struct vop_old_mkdir_args *ap) 2194 { 2195 struct vnode *dvp = ap->a_dvp; 2196 struct vattr *vap = ap->a_vap; 2197 struct nfsmount *nmp = VFSTONFS(dvp->v_mount); 2198 struct componentname *cnp = ap->a_cnp; 2199 struct nfsv2_sattr *sp; 2200 struct nfsnode *np = NULL; 2201 struct vnode *newvp = NULL; 2202 struct vattr vattr; 2203 int error = 0, wccflag = NFSV3_WCCRATTR; 2204 int gotvp = 0; 2205 int len; 2206 struct nfsm_info info; 2207 2208 info.mrep = NULL; 2209 info.v3 = NFS_ISV3(dvp); 2210 lwkt_gettoken(&nmp->nm_token); 2211 2212 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) { 2213 lwkt_reltoken(&nmp->nm_token); 2214 return (error); 2215 } 2216 len = cnp->cn_namelen; 2217 nfsstats.rpccnt[NFSPROC_MKDIR]++; 2218 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR, 2219 NFSX_FH(info.v3) + NFSX_UNSIGNED + 2220 nfsm_rndup(len) + NFSX_SATTR(info.v3)); 2221 ERROROUT(nfsm_fhtom(&info, dvp)); 2222 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN)); 2223 if (info.v3) { 2224 nfsm_v3attrbuild(&info, vap, FALSE); 2225 } else { 2226 sp = nfsm_build(&info, NFSX_V2SATTR); 2227 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode); 2228 sp->sa_uid = nfs_xdrneg1; 2229 sp->sa_gid = nfs_xdrneg1; 2230 sp->sa_size = nfs_xdrneg1; 2231 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 2232 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 2233 } 2234 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td, 2235 cnp->cn_cred, &error)); 2236 if (error == 0) { 2237 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp)); 2238 } 2239 if (info.v3) { 2240 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag)); 2241 } 2242 m_freem(info.mrep); 2243 info.mrep = NULL; 2244 nfsmout: 2245 VTONFS(dvp)->n_flag |= NLMODIFIED; 2246 if (!wccflag) 2247 VTONFS(dvp)->n_attrstamp = 0; 2248 /* 2249 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry 2250 * if we can succeed in looking up the directory. 2251 */ 2252 if (error == EEXIST || (!error && !gotvp)) { 2253 if (newvp) { 2254 vrele(newvp); 2255 newvp = NULL; 2256 } 2257 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred, 2258 cnp->cn_td, &np); 2259 if (!error) { 2260 newvp = NFSTOV(np); 2261 if (newvp->v_type != VDIR) 2262 error = EEXIST; 2263 } 2264 } 2265 if (error) { 2266 if (newvp) 2267 vrele(newvp); 2268 } else { 2269 *ap->a_vpp = newvp; 2270 } 2271 lwkt_reltoken(&nmp->nm_token); 2272 return (error); 2273 } 2274 2275 /* 2276 * nfs remove directory call 2277 * 2278 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp, 2279 * struct componentname *a_cnp) 2280 */ 2281 static int 2282 nfs_rmdir(struct vop_old_rmdir_args *ap) 2283 { 2284 struct vnode *vp = ap->a_vp; 2285 struct vnode *dvp = ap->a_dvp; 2286 struct nfsmount *nmp = VFSTONFS(dvp->v_mount); 2287 struct componentname *cnp = ap->a_cnp; 2288 int error = 0, wccflag = NFSV3_WCCRATTR; 2289 struct nfsm_info info; 2290 2291 info.mrep = NULL; 2292 info.v3 = NFS_ISV3(dvp); 2293 2294 if (dvp == vp) 2295 return (EINVAL); 2296 2297 lwkt_gettoken(&nmp->nm_token); 2298 2299 nfsstats.rpccnt[NFSPROC_RMDIR]++; 2300 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR, 2301 NFSX_FH(info.v3) + NFSX_UNSIGNED + 2302 nfsm_rndup(cnp->cn_namelen)); 2303 ERROROUT(nfsm_fhtom(&info, dvp)); 2304 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen, 2305 NFS_MAXNAMLEN)); 2306 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td, 2307 cnp->cn_cred, &error)); 2308 if (info.v3) { 2309 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag)); 2310 } 2311 m_freem(info.mrep); 2312 info.mrep = NULL; 2313 nfsmout: 2314 VTONFS(dvp)->n_flag |= NLMODIFIED; 2315 if (!wccflag) 2316 VTONFS(dvp)->n_attrstamp = 0; 2317 /* 2318 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2319 */ 2320 if (error == ENOENT) 2321 error = 0; 2322 lwkt_reltoken(&nmp->nm_token); 2323 2324 return (error); 2325 } 2326 2327 /* 2328 * nfs readdir call 2329 * 2330 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred) 2331 */ 2332 static int 2333 nfs_readdir(struct vop_readdir_args *ap) 2334 { 2335 struct vnode *vp = ap->a_vp; 2336 struct nfsnode *np = VTONFS(vp); 2337 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2338 struct uio *uio = ap->a_uio; 2339 int tresid, error; 2340 struct vattr vattr; 2341 2342 if (vp->v_type != VDIR) 2343 return (EPERM); 2344 2345 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) 2346 return (error); 2347 2348 lwkt_gettoken(&nmp->nm_token); 2349 2350 /* 2351 * If we have a valid EOF offset cache we must call VOP_GETATTR() 2352 * and then check that is still valid, or if this is an NQNFS mount 2353 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that 2354 * VOP_GETATTR() does not necessarily go to the wire. 2355 */ 2356 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset && 2357 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) { 2358 if (VOP_GETATTR(vp, &vattr) == 0 && 2359 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0 2360 ) { 2361 nfsstats.direofcache_hits++; 2362 goto done; 2363 } 2364 } 2365 2366 /* 2367 * Call nfs_bioread() to do the real work. nfs_bioread() does its 2368 * own cache coherency checks so we do not have to. 2369 */ 2370 tresid = uio->uio_resid; 2371 error = nfs_bioread(vp, uio, 0); 2372 2373 if (!error && uio->uio_resid == tresid) 2374 nfsstats.direofcache_misses++; 2375 done: 2376 lwkt_reltoken(&nmp->nm_token); 2377 vn_unlock(vp); 2378 2379 return (error); 2380 } 2381 2382 /* 2383 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc. 2384 * 2385 * Note that for directories, nfs_bioread maintains the underlying nfs-centric 2386 * offset/block and converts the nfs formatted directory entries for userland 2387 * consumption as well as deals with offsets into the middle of blocks. 2388 * nfs_doio only deals with logical blocks. In particular, uio_offset will 2389 * be block-bounded. It must convert to cookies for the actual RPC. 2390 */ 2391 int 2392 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop) 2393 { 2394 int len, left; 2395 struct nfs_dirent *dp = NULL; 2396 u_int32_t *tl; 2397 nfsuint64 *cookiep; 2398 caddr_t cp; 2399 nfsuint64 cookie; 2400 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2401 struct nfsnode *dnp = VTONFS(vp); 2402 u_quad_t fileno; 2403 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2404 int attrflag; 2405 struct nfsm_info info; 2406 2407 info.mrep = NULL; 2408 info.v3 = NFS_ISV3(vp); 2409 2410 #ifndef DIAGNOSTIC 2411 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) || 2412 (uiop->uio_resid & (DIRBLKSIZ - 1))) 2413 panic("nfs readdirrpc bad uio"); 2414 #endif 2415 2416 /* 2417 * If there is no cookie, assume directory was stale. 2418 */ 2419 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 2420 if (cookiep) 2421 cookie = *cookiep; 2422 else 2423 return (NFSERR_BAD_COOKIE); 2424 /* 2425 * Loop around doing readdir rpc's of size nm_readdirsize 2426 * truncated to a multiple of DIRBLKSIZ. 2427 * The stopping criteria is EOF or buffer full. 2428 */ 2429 while (more_dirs && bigenough) { 2430 nfsstats.rpccnt[NFSPROC_READDIR]++; 2431 nfsm_reqhead(&info, vp, NFSPROC_READDIR, 2432 NFSX_FH(info.v3) + NFSX_READDIR(info.v3)); 2433 ERROROUT(nfsm_fhtom(&info, vp)); 2434 if (info.v3) { 2435 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED); 2436 *tl++ = cookie.nfsuquad[0]; 2437 *tl++ = cookie.nfsuquad[1]; 2438 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2439 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2440 } else { 2441 /* 2442 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL 2443 * WITH NFSv2!!! There's nothing I can really do 2444 * about it other than to hope the server supports 2445 * rdirplus w/NFSv2. 2446 */ 2447 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED); 2448 *tl++ = cookie.nfsuquad[0]; 2449 } 2450 *tl = txdr_unsigned(nmp->nm_readdirsize); 2451 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR, 2452 uiop->uio_td, 2453 nfs_vpcred(vp, ND_READ), &error)); 2454 if (info.v3) { 2455 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, 2456 NFS_LATTR_NOSHRINK)); 2457 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2458 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2459 dnp->n_cookieverf.nfsuquad[1] = *tl; 2460 } 2461 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2462 more_dirs = fxdr_unsigned(int, *tl); 2463 2464 /* loop thru the dir entries, converting them to std form */ 2465 while (more_dirs && bigenough) { 2466 if (info.v3) { 2467 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2468 fileno = fxdr_hyper(tl); 2469 len = fxdr_unsigned(int, *(tl + 2)); 2470 } else { 2471 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2472 fileno = fxdr_unsigned(u_quad_t, *tl++); 2473 len = fxdr_unsigned(int, *tl); 2474 } 2475 if (len <= 0 || len > NFS_MAXNAMLEN) { 2476 error = EBADRPC; 2477 m_freem(info.mrep); 2478 info.mrep = NULL; 2479 goto nfsmout; 2480 } 2481 2482 /* 2483 * len is the number of bytes in the path element 2484 * name, not including the \0 termination. 2485 * 2486 * tlen is the number of bytes w have to reserve for 2487 * the path element name. 2488 */ 2489 tlen = nfsm_rndup(len); 2490 if (tlen == len) 2491 tlen += 4; /* To ensure null termination */ 2492 2493 /* 2494 * If the entry would cross a DIRBLKSIZ boundary, 2495 * extend the previous nfs_dirent to cover the 2496 * remaining space. 2497 */ 2498 left = DIRBLKSIZ - blksiz; 2499 if ((tlen + sizeof(struct nfs_dirent)) > left) { 2500 dp->nfs_reclen += left; 2501 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left; 2502 uiop->uio_iov->iov_len -= left; 2503 uiop->uio_offset += left; 2504 uiop->uio_resid -= left; 2505 blksiz = 0; 2506 } 2507 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid) 2508 bigenough = 0; 2509 if (bigenough) { 2510 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base; 2511 dp->nfs_ino = fileno; 2512 dp->nfs_namlen = len; 2513 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent); 2514 dp->nfs_type = DT_UNKNOWN; 2515 blksiz += dp->nfs_reclen; 2516 if (blksiz == DIRBLKSIZ) 2517 blksiz = 0; 2518 uiop->uio_offset += sizeof(struct nfs_dirent); 2519 uiop->uio_resid -= sizeof(struct nfs_dirent); 2520 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent); 2521 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent); 2522 ERROROUT(nfsm_mtouio(&info, uiop, len)); 2523 2524 /* 2525 * The uiop has advanced by nfs_dirent + len 2526 * but really needs to advance by 2527 * nfs_dirent + tlen 2528 */ 2529 cp = uiop->uio_iov->iov_base; 2530 tlen -= len; 2531 *cp = '\0'; /* null terminate */ 2532 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen; 2533 uiop->uio_iov->iov_len -= tlen; 2534 uiop->uio_offset += tlen; 2535 uiop->uio_resid -= tlen; 2536 } else { 2537 /* 2538 * NFS strings must be rounded up (nfsm_myouio 2539 * handled that in the bigenough case). 2540 */ 2541 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2542 } 2543 if (info.v3) { 2544 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2545 } else { 2546 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2547 } 2548 2549 /* 2550 * If we were able to accomodate the last entry, 2551 * get the cookie for the next one. Otherwise 2552 * hold-over the cookie for the one we were not 2553 * able to accomodate. 2554 */ 2555 if (bigenough) { 2556 cookie.nfsuquad[0] = *tl++; 2557 if (info.v3) 2558 cookie.nfsuquad[1] = *tl++; 2559 } else if (info.v3) { 2560 tl += 2; 2561 } else { 2562 tl++; 2563 } 2564 more_dirs = fxdr_unsigned(int, *tl); 2565 } 2566 /* 2567 * If at end of rpc data, get the eof boolean 2568 */ 2569 if (!more_dirs) { 2570 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2571 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2572 } 2573 m_freem(info.mrep); 2574 info.mrep = NULL; 2575 } 2576 /* 2577 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2578 * by increasing d_reclen for the last record. 2579 */ 2580 if (blksiz > 0) { 2581 left = DIRBLKSIZ - blksiz; 2582 dp->nfs_reclen += left; 2583 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left; 2584 uiop->uio_iov->iov_len -= left; 2585 uiop->uio_offset += left; 2586 uiop->uio_resid -= left; 2587 } 2588 2589 if (bigenough) { 2590 /* 2591 * We hit the end of the directory, update direofoffset. 2592 */ 2593 dnp->n_direofoffset = uiop->uio_offset; 2594 } else { 2595 /* 2596 * There is more to go, insert the link cookie so the 2597 * next block can be read. 2598 */ 2599 if (uiop->uio_resid > 0) 2600 kprintf("EEK! readdirrpc resid > 0\n"); 2601 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2602 *cookiep = cookie; 2603 } 2604 nfsmout: 2605 return (error); 2606 } 2607 2608 /* 2609 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc(). 2610 */ 2611 int 2612 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop) 2613 { 2614 int len, left; 2615 struct nfs_dirent *dp; 2616 u_int32_t *tl; 2617 struct vnode *newvp; 2618 nfsuint64 *cookiep; 2619 caddr_t dpossav1, dpossav2; 2620 caddr_t cp; 2621 struct mbuf *mdsav1, *mdsav2; 2622 nfsuint64 cookie; 2623 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2624 struct nfsnode *dnp = VTONFS(vp), *np; 2625 nfsfh_t *fhp; 2626 u_quad_t fileno; 2627 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i; 2628 int attrflag, fhsize; 2629 struct nchandle nch; 2630 struct nchandle dnch; 2631 struct nlcomponent nlc; 2632 struct nfsm_info info; 2633 2634 info.mrep = NULL; 2635 info.v3 = 1; 2636 2637 #ifndef nolint 2638 dp = NULL; 2639 #endif 2640 #ifndef DIAGNOSTIC 2641 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) || 2642 (uiop->uio_resid & (DIRBLKSIZ - 1))) 2643 panic("nfs readdirplusrpc bad uio"); 2644 #endif 2645 /* 2646 * Obtain the namecache record for the directory so we have something 2647 * to use as a basis for creating the entries. This function will 2648 * return a held (but not locked) ncp. The ncp may be disconnected 2649 * from the tree and cannot be used for upward traversals, and the 2650 * ncp may be unnamed. Note that other unrelated operations may 2651 * cause the ncp to be named at any time. 2652 * 2653 * We have to lock the ncp to prevent a lock order reversal when 2654 * rdirplus does nlookups of the children, because the vnode is 2655 * locked and has to stay that way. 2656 */ 2657 cache_fromdvp(vp, NULL, 0, &dnch); 2658 bzero(&nlc, sizeof(nlc)); 2659 newvp = NULLVP; 2660 2661 /* 2662 * If there is no cookie, assume directory was stale. 2663 */ 2664 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 2665 if (cookiep) { 2666 cookie = *cookiep; 2667 } else { 2668 if (dnch.ncp) 2669 cache_drop(&dnch); 2670 return (NFSERR_BAD_COOKIE); 2671 } 2672 2673 /* 2674 * Loop around doing readdir rpc's of size nm_readdirsize 2675 * truncated to a multiple of DIRBLKSIZ. 2676 * The stopping criteria is EOF or buffer full. 2677 */ 2678 while (more_dirs && bigenough) { 2679 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++; 2680 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS, 2681 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED); 2682 ERROROUT(nfsm_fhtom(&info, vp)); 2683 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED); 2684 *tl++ = cookie.nfsuquad[0]; 2685 *tl++ = cookie.nfsuquad[1]; 2686 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2687 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2688 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 2689 *tl = txdr_unsigned(nmp->nm_rsize); 2690 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS, 2691 uiop->uio_td, 2692 nfs_vpcred(vp, ND_READ), &error)); 2693 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, 2694 NFS_LATTR_NOSHRINK)); 2695 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2696 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2697 dnp->n_cookieverf.nfsuquad[1] = *tl++; 2698 more_dirs = fxdr_unsigned(int, *tl); 2699 2700 /* loop thru the dir entries, doctoring them to 4bsd form */ 2701 while (more_dirs && bigenough) { 2702 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2703 fileno = fxdr_hyper(tl); 2704 len = fxdr_unsigned(int, *(tl + 2)); 2705 if (len <= 0 || len > NFS_MAXNAMLEN) { 2706 error = EBADRPC; 2707 m_freem(info.mrep); 2708 info.mrep = NULL; 2709 goto nfsmout; 2710 } 2711 tlen = nfsm_rndup(len); 2712 if (tlen == len) 2713 tlen += 4; /* To ensure null termination*/ 2714 left = DIRBLKSIZ - blksiz; 2715 if ((tlen + sizeof(struct nfs_dirent)) > left) { 2716 dp->nfs_reclen += left; 2717 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left; 2718 uiop->uio_iov->iov_len -= left; 2719 uiop->uio_offset += left; 2720 uiop->uio_resid -= left; 2721 blksiz = 0; 2722 } 2723 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid) 2724 bigenough = 0; 2725 if (bigenough) { 2726 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base; 2727 dp->nfs_ino = fileno; 2728 dp->nfs_namlen = len; 2729 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent); 2730 dp->nfs_type = DT_UNKNOWN; 2731 blksiz += dp->nfs_reclen; 2732 if (blksiz == DIRBLKSIZ) 2733 blksiz = 0; 2734 uiop->uio_offset += sizeof(struct nfs_dirent); 2735 uiop->uio_resid -= sizeof(struct nfs_dirent); 2736 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent); 2737 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent); 2738 nlc.nlc_nameptr = uiop->uio_iov->iov_base; 2739 nlc.nlc_namelen = len; 2740 ERROROUT(nfsm_mtouio(&info, uiop, len)); 2741 cp = uiop->uio_iov->iov_base; 2742 tlen -= len; 2743 *cp = '\0'; 2744 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen; 2745 uiop->uio_iov->iov_len -= tlen; 2746 uiop->uio_offset += tlen; 2747 uiop->uio_resid -= tlen; 2748 } else { 2749 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2750 } 2751 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2752 if (bigenough) { 2753 cookie.nfsuquad[0] = *tl++; 2754 cookie.nfsuquad[1] = *tl++; 2755 } else { 2756 tl += 2; 2757 } 2758 2759 /* 2760 * Since the attributes are before the file handle 2761 * (sigh), we must skip over the attributes and then 2762 * come back and get them. 2763 */ 2764 attrflag = fxdr_unsigned(int, *tl); 2765 if (attrflag) { 2766 dpossav1 = info.dpos; 2767 mdsav1 = info.md; 2768 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR)); 2769 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2770 doit = fxdr_unsigned(int, *tl); 2771 if (doit) { 2772 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp)); 2773 } 2774 if (doit && bigenough && !nlcdegenerate(&nlc) && 2775 !NFS_CMPFH(dnp, fhp, fhsize) 2776 ) { 2777 if (dnch.ncp) { 2778 #if 0 2779 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n", 2780 nlc.nlc_namelen, nlc.nlc_namelen, 2781 nlc.nlc_nameptr); 2782 #endif 2783 /* 2784 * This is a bit hokey but there isn't 2785 * much we can do about it. We can't 2786 * hold the directory vp locked while 2787 * doing lookups and gets. 2788 */ 2789 nch = cache_nlookup_nonblock(&dnch, &nlc); 2790 if (nch.ncp == NULL) 2791 goto rdfail; 2792 cache_setunresolved(&nch); 2793 error = nfs_nget_nonblock(vp->v_mount, fhp, 2794 fhsize, &np); 2795 if (error) { 2796 cache_put(&nch); 2797 goto rdfail; 2798 } 2799 newvp = NFSTOV(np); 2800 dpossav2 = info.dpos; 2801 info.dpos = dpossav1; 2802 mdsav2 = info.md; 2803 info.md = mdsav1; 2804 ERROROUT(nfsm_loadattr(&info, newvp, NULL)); 2805 info.dpos = dpossav2; 2806 info.md = mdsav2; 2807 dp->nfs_type = 2808 IFTODT(VTTOIF(np->n_vattr.va_type)); 2809 nfs_cache_setvp(&nch, newvp, 2810 nfspos_cache_timeout); 2811 vput(newvp); 2812 newvp = NULLVP; 2813 cache_put(&nch); 2814 } else { 2815 rdfail: 2816 ; 2817 #if 0 2818 kprintf("Warning: NFS/rddirplus, " 2819 "UNABLE TO ENTER %*.*s\n", 2820 nlc.nlc_namelen, nlc.nlc_namelen, 2821 nlc.nlc_nameptr); 2822 #endif 2823 } 2824 } 2825 } else { 2826 /* Just skip over the file handle */ 2827 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2828 i = fxdr_unsigned(int, *tl); 2829 ERROROUT(nfsm_adv(&info, nfsm_rndup(i))); 2830 } 2831 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2832 more_dirs = fxdr_unsigned(int, *tl); 2833 } 2834 /* 2835 * If at end of rpc data, get the eof boolean 2836 */ 2837 if (!more_dirs) { 2838 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED)); 2839 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2840 } 2841 m_freem(info.mrep); 2842 info.mrep = NULL; 2843 } 2844 /* 2845 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2846 * by increasing d_reclen for the last record. 2847 */ 2848 if (blksiz > 0) { 2849 left = DIRBLKSIZ - blksiz; 2850 dp->nfs_reclen += left; 2851 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left; 2852 uiop->uio_iov->iov_len -= left; 2853 uiop->uio_offset += left; 2854 uiop->uio_resid -= left; 2855 } 2856 2857 /* 2858 * We are now either at the end of the directory or have filled the 2859 * block. 2860 */ 2861 if (bigenough) { 2862 dnp->n_direofoffset = uiop->uio_offset; 2863 } else { 2864 if (uiop->uio_resid > 0) 2865 kprintf("EEK! readdirplusrpc resid > 0\n"); 2866 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2867 *cookiep = cookie; 2868 } 2869 nfsmout: 2870 if (newvp != NULLVP) { 2871 if (newvp == vp) 2872 vrele(newvp); 2873 else 2874 vput(newvp); 2875 newvp = NULLVP; 2876 } 2877 if (dnch.ncp) 2878 cache_drop(&dnch); 2879 return (error); 2880 } 2881 2882 /* 2883 * Silly rename. To make the NFS filesystem that is stateless look a little 2884 * more like the "ufs" a remove of an active vnode is translated to a rename 2885 * to a funny looking filename that is removed by nfs_inactive on the 2886 * nfsnode. There is the potential for another process on a different client 2887 * to create the same funny name between the nfs_lookitup() fails and the 2888 * nfs_rename() completes, but... 2889 */ 2890 static int 2891 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2892 { 2893 struct sillyrename *sp; 2894 struct nfsnode *np; 2895 int error; 2896 2897 /* 2898 * We previously purged dvp instead of vp. I don't know why, it 2899 * completely destroys performance. We can't do it anyway with the 2900 * new VFS API since we would be breaking the namecache topology. 2901 */ 2902 cache_purge(vp); /* XXX */ 2903 np = VTONFS(vp); 2904 #ifndef DIAGNOSTIC 2905 if (vp->v_type == VDIR) 2906 panic("nfs: sillyrename dir"); 2907 #endif 2908 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), 2909 M_NFSREQ, M_WAITOK); 2910 sp->s_cred = crdup(cnp->cn_cred); 2911 sp->s_dvp = dvp; 2912 vref(dvp); 2913 2914 /* Fudge together a funny name */ 2915 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4", 2916 (int)(intptr_t)cnp->cn_td); 2917 2918 /* Try lookitups until we get one that isn't there */ 2919 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2920 cnp->cn_td, NULL) == 0) { 2921 sp->s_name[4]++; 2922 if (sp->s_name[4] > 'z') { 2923 error = EINVAL; 2924 goto bad; 2925 } 2926 } 2927 error = nfs_renameit(dvp, cnp, sp); 2928 if (error) 2929 goto bad; 2930 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2931 cnp->cn_td, &np); 2932 np->n_sillyrename = sp; 2933 return (0); 2934 bad: 2935 vrele(sp->s_dvp); 2936 crfree(sp->s_cred); 2937 kfree((caddr_t)sp, M_NFSREQ); 2938 return (error); 2939 } 2940 2941 /* 2942 * Look up a file name and optionally either update the file handle or 2943 * allocate an nfsnode, depending on the value of npp. 2944 * npp == NULL --> just do the lookup 2945 * *npp == NULL --> allocate a new nfsnode and make sure attributes are 2946 * handled too 2947 * *npp != NULL --> update the file handle in the vnode 2948 */ 2949 static int 2950 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred, 2951 struct thread *td, struct nfsnode **npp) 2952 { 2953 struct vnode *newvp = NULL; 2954 struct nfsnode *np, *dnp = VTONFS(dvp); 2955 int error = 0, fhlen, attrflag; 2956 nfsfh_t *nfhp; 2957 struct nfsm_info info; 2958 2959 info.mrep = NULL; 2960 info.v3 = NFS_ISV3(dvp); 2961 2962 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 2963 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP, 2964 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 2965 ERROROUT(nfsm_fhtom(&info, dvp)); 2966 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN)); 2967 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error)); 2968 if (npp && !error) { 2969 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp)); 2970 if (*npp) { 2971 np = *npp; 2972 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) { 2973 kfree((caddr_t)np->n_fhp, M_NFSBIGFH); 2974 np->n_fhp = &np->n_fh; 2975 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH) 2976 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK); 2977 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen); 2978 np->n_fhsize = fhlen; 2979 newvp = NFSTOV(np); 2980 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) { 2981 vref(dvp); 2982 newvp = dvp; 2983 } else { 2984 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np); 2985 if (error) { 2986 m_freem(info.mrep); 2987 info.mrep = NULL; 2988 return (error); 2989 } 2990 newvp = NFSTOV(np); 2991 } 2992 if (info.v3) { 2993 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag, 2994 NFS_LATTR_NOSHRINK)); 2995 if (!attrflag && *npp == NULL) { 2996 m_freem(info.mrep); 2997 info.mrep = NULL; 2998 if (newvp == dvp) 2999 vrele(newvp); 3000 else 3001 vput(newvp); 3002 return (ENOENT); 3003 } 3004 } else { 3005 ERROROUT(nfsm_loadattr(&info, newvp, NULL)); 3006 } 3007 } 3008 m_freem(info.mrep); 3009 info.mrep = NULL; 3010 nfsmout: 3011 if (npp && *npp == NULL) { 3012 if (error) { 3013 if (newvp) { 3014 if (newvp == dvp) 3015 vrele(newvp); 3016 else 3017 vput(newvp); 3018 } 3019 } else 3020 *npp = np; 3021 } 3022 return (error); 3023 } 3024 3025 /* 3026 * Nfs Version 3 commit rpc 3027 * 3028 * We call it 'uio' to distinguish it from 'bio' but there is no real uio 3029 * involved. 3030 */ 3031 int 3032 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td) 3033 { 3034 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 3035 int error = 0, wccflag = NFSV3_WCCRATTR; 3036 struct nfsm_info info; 3037 u_int32_t *tl; 3038 3039 info.mrep = NULL; 3040 info.v3 = 1; 3041 3042 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) 3043 return (0); 3044 nfsstats.rpccnt[NFSPROC_COMMIT]++; 3045 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1)); 3046 ERROROUT(nfsm_fhtom(&info, vp)); 3047 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED); 3048 txdr_hyper(offset, tl); 3049 tl += 2; 3050 *tl = txdr_unsigned(cnt); 3051 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td, 3052 nfs_vpcred(vp, ND_WRITE), &error)); 3053 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag)); 3054 if (!error) { 3055 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF)); 3056 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl, 3057 NFSX_V3WRITEVERF)) { 3058 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 3059 NFSX_V3WRITEVERF); 3060 error = NFSERR_STALEWRITEVERF; 3061 } 3062 } 3063 m_freem(info.mrep); 3064 info.mrep = NULL; 3065 nfsmout: 3066 return (error); 3067 } 3068 3069 /* 3070 * Kludge City.. 3071 * - make nfs_bmap() essentially a no-op that does no translation 3072 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc 3073 * (Maybe I could use the process's page mapping, but I was concerned that 3074 * Kernel Write might not be enabled and also figured copyout() would do 3075 * a lot more work than bcopy() and also it currently happens in the 3076 * context of the swapper process (2). 3077 * 3078 * nfs_bmap(struct vnode *a_vp, off_t a_loffset, 3079 * off_t *a_doffsetp, int *a_runp, int *a_runb) 3080 */ 3081 static int 3082 nfs_bmap(struct vop_bmap_args *ap) 3083 { 3084 /* no token lock required */ 3085 if (ap->a_doffsetp != NULL) 3086 *ap->a_doffsetp = ap->a_loffset; 3087 if (ap->a_runp != NULL) 3088 *ap->a_runp = 0; 3089 if (ap->a_runb != NULL) 3090 *ap->a_runb = 0; 3091 return (0); 3092 } 3093 3094 /* 3095 * Strategy routine. 3096 */ 3097 static int 3098 nfs_strategy(struct vop_strategy_args *ap) 3099 { 3100 struct bio *bio = ap->a_bio; 3101 struct bio *nbio; 3102 struct buf *bp __debugvar = bio->bio_buf; 3103 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount); 3104 struct thread *td; 3105 int error; 3106 3107 KASSERT(bp->b_cmd != BUF_CMD_DONE, 3108 ("nfs_strategy: buffer %p unexpectedly marked done", bp)); 3109 KASSERT(BUF_REFCNT(bp) > 0, 3110 ("nfs_strategy: buffer %p not locked", bp)); 3111 3112 if (bio->bio_flags & BIO_SYNC) 3113 td = curthread; /* XXX */ 3114 else 3115 td = NULL; 3116 3117 lwkt_gettoken(&nmp->nm_token); 3118 3119 /* 3120 * We probably don't need to push an nbio any more since no 3121 * block conversion is required due to the use of 64 bit byte 3122 * offsets, but do it anyway. 3123 * 3124 * NOTE: When NFS callers itself via this strategy routines and 3125 * sets up a synchronous I/O, it expects the I/O to run 3126 * synchronously (its bio_done routine just assumes it), 3127 * so for now we have to honor the bit. 3128 */ 3129 nbio = push_bio(bio); 3130 nbio->bio_offset = bio->bio_offset; 3131 nbio->bio_flags = bio->bio_flags & BIO_SYNC; 3132 3133 /* 3134 * If the op is asynchronous and an i/o daemon is waiting 3135 * queue the request, wake it up and wait for completion 3136 * otherwise just do it ourselves. 3137 */ 3138 if (bio->bio_flags & BIO_SYNC) { 3139 error = nfs_doio(ap->a_vp, nbio, td); 3140 } else { 3141 nfs_asyncio(ap->a_vp, nbio); 3142 error = 0; 3143 } 3144 lwkt_reltoken(&nmp->nm_token); 3145 3146 return (error); 3147 } 3148 3149 /* 3150 * Mmap a file 3151 * 3152 * NB Currently unsupported. 3153 * 3154 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred) 3155 */ 3156 /* ARGSUSED */ 3157 static int 3158 nfs_mmap(struct vop_mmap_args *ap) 3159 { 3160 /* no token lock required */ 3161 return (EINVAL); 3162 } 3163 3164 /* 3165 * fsync vnode op. Just call nfs_flush() with commit == 1. 3166 * 3167 * nfs_fsync(struct vnode *a_vp, int a_waitfor) 3168 */ 3169 /* ARGSUSED */ 3170 static int 3171 nfs_fsync(struct vop_fsync_args *ap) 3172 { 3173 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount); 3174 int error; 3175 3176 lwkt_gettoken(&nmp->nm_token); 3177 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1); 3178 lwkt_reltoken(&nmp->nm_token); 3179 3180 return error; 3181 } 3182 3183 /* 3184 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be 3185 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains 3186 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is 3187 * set the buffer contains data that has already been written to the server 3188 * and which now needs a commit RPC. 3189 * 3190 * If commit is 0 we only take one pass and only flush buffers containing new 3191 * dirty data. 3192 * 3193 * If commit is 1 we take two passes, issuing a commit RPC in the second 3194 * pass. 3195 * 3196 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required 3197 * to completely flush all pending data. 3198 * 3199 * Note that the RB_SCAN code properly handles the case where the 3200 * callback might block and directly or indirectly (another thread) cause 3201 * the RB tree to change. 3202 */ 3203 3204 #ifndef NFS_COMMITBVECSIZ 3205 #define NFS_COMMITBVECSIZ 16 3206 #endif 3207 3208 struct nfs_flush_info { 3209 enum { NFI_FLUSHNEW, NFI_COMMIT } mode; 3210 struct thread *td; 3211 struct vnode *vp; 3212 int waitfor; 3213 int slpflag; 3214 int slptimeo; 3215 int loops; 3216 struct buf *bvary[NFS_COMMITBVECSIZ]; 3217 int bvsize; 3218 off_t beg_off; 3219 off_t end_off; 3220 }; 3221 3222 static int nfs_flush_bp(struct buf *bp, void *data); 3223 static int nfs_flush_docommit(struct nfs_flush_info *info, int error); 3224 3225 int 3226 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit) 3227 { 3228 struct nfsnode *np = VTONFS(vp); 3229 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 3230 struct nfs_flush_info info; 3231 int error; 3232 3233 bzero(&info, sizeof(info)); 3234 info.td = td; 3235 info.vp = vp; 3236 info.waitfor = waitfor; 3237 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0; 3238 info.loops = 0; 3239 lwkt_gettoken(&vp->v_token); 3240 3241 do { 3242 /* 3243 * Flush mode 3244 */ 3245 info.mode = NFI_FLUSHNEW; 3246 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 3247 nfs_flush_bp, &info); 3248 3249 /* 3250 * Take a second pass if committing and no error occured. 3251 * Clean up any left over collection (whether an error 3252 * occurs or not). 3253 */ 3254 if (commit && error == 0) { 3255 info.mode = NFI_COMMIT; 3256 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 3257 nfs_flush_bp, &info); 3258 if (info.bvsize) 3259 error = nfs_flush_docommit(&info, error); 3260 } 3261 3262 /* 3263 * Wait for pending I/O to complete before checking whether 3264 * any further dirty buffers exist. 3265 */ 3266 while (waitfor == MNT_WAIT && 3267 bio_track_active(&vp->v_track_write)) { 3268 error = bio_track_wait(&vp->v_track_write, 3269 info.slpflag, info.slptimeo); 3270 if (error) { 3271 /* 3272 * We have to be able to break out if this 3273 * is an 'intr' mount. 3274 */ 3275 if (nfs_sigintr(nmp, NULL, td)) { 3276 error = -EINTR; 3277 break; 3278 } 3279 3280 /* 3281 * Since we do not process pending signals, 3282 * once we get a PCATCH our tsleep() will no 3283 * longer sleep, switch to a fixed timeout 3284 * instead. 3285 */ 3286 if (info.slpflag == PCATCH) { 3287 info.slpflag = 0; 3288 info.slptimeo = 2 * hz; 3289 } 3290 error = 0; 3291 } 3292 } 3293 ++info.loops; 3294 /* 3295 * Loop if we are flushing synchronous as well as committing, 3296 * and dirty buffers are still present. Otherwise we might livelock. 3297 */ 3298 } while (waitfor == MNT_WAIT && commit && 3299 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree)); 3300 3301 /* 3302 * The callbacks have to return a negative error to terminate the 3303 * RB scan. 3304 */ 3305 if (error < 0) 3306 error = -error; 3307 3308 /* 3309 * Deal with any error collection 3310 */ 3311 if (np->n_flag & NWRITEERR) { 3312 error = np->n_error; 3313 np->n_flag &= ~NWRITEERR; 3314 } 3315 lwkt_reltoken(&vp->v_token); 3316 return (error); 3317 } 3318 3319 static 3320 int 3321 nfs_flush_bp(struct buf *bp, void *data) 3322 { 3323 struct nfs_flush_info *info = data; 3324 int lkflags; 3325 int error; 3326 off_t toff; 3327 3328 error = 0; 3329 switch(info->mode) { 3330 case NFI_FLUSHNEW: 3331 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT); 3332 if (error && info->loops && info->waitfor == MNT_WAIT) { 3333 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT); 3334 if (error) { 3335 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; 3336 if (info->slpflag & PCATCH) 3337 lkflags |= LK_PCATCH; 3338 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync", 3339 info->slptimeo); 3340 } 3341 } 3342 3343 /* 3344 * Ignore locking errors 3345 */ 3346 if (error) { 3347 error = 0; 3348 break; 3349 } 3350 3351 /* 3352 * The buffer may have changed out from under us, even if 3353 * we did not block (MPSAFE). Check again now that it is 3354 * locked. 3355 */ 3356 if (bp->b_vp == info->vp && 3357 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) { 3358 bremfree(bp); 3359 bawrite(bp); 3360 } else { 3361 BUF_UNLOCK(bp); 3362 } 3363 break; 3364 case NFI_COMMIT: 3365 /* 3366 * Only process buffers in need of a commit which we can 3367 * immediately lock. This may prevent a buffer from being 3368 * committed, but the normal flush loop will block on the 3369 * same buffer so we shouldn't get into an endless loop. 3370 */ 3371 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) != 3372 (B_DELWRI | B_NEEDCOMMIT)) { 3373 break; 3374 } 3375 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) 3376 break; 3377 3378 /* 3379 * We must recheck after successfully locking the buffer. 3380 */ 3381 if (bp->b_vp != info->vp || 3382 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) != 3383 (B_DELWRI | B_NEEDCOMMIT)) { 3384 BUF_UNLOCK(bp); 3385 break; 3386 } 3387 3388 /* 3389 * NOTE: storing the bp in the bvary[] basically sets 3390 * it up for a commit operation. 3391 * 3392 * We must call vfs_busy_pages() now so the commit operation 3393 * is interlocked with user modifications to memory mapped 3394 * pages. The b_dirtyoff/b_dirtyend range is not correct 3395 * until after the pages have been busied. 3396 * 3397 * Note: to avoid loopback deadlocks, we do not 3398 * assign b_runningbufspace. 3399 */ 3400 bremfree(bp); 3401 bp->b_cmd = BUF_CMD_WRITE; 3402 vfs_busy_pages(bp->b_vp, bp); 3403 info->bvary[info->bvsize] = bp; 3404 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff; 3405 if (info->bvsize == 0 || toff < info->beg_off) 3406 info->beg_off = toff; 3407 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff); 3408 if (info->bvsize == 0 || toff > info->end_off) 3409 info->end_off = toff; 3410 ++info->bvsize; 3411 if (info->bvsize == NFS_COMMITBVECSIZ) { 3412 error = nfs_flush_docommit(info, 0); 3413 KKASSERT(info->bvsize == 0); 3414 } 3415 } 3416 return (error); 3417 } 3418 3419 static 3420 int 3421 nfs_flush_docommit(struct nfs_flush_info *info, int error) 3422 { 3423 struct vnode *vp; 3424 struct buf *bp; 3425 off_t bytes; 3426 int retv; 3427 int i; 3428 3429 vp = info->vp; 3430 3431 if (info->bvsize > 0) { 3432 /* 3433 * Commit data on the server, as required. Note that 3434 * nfs_commit will use the vnode's cred for the commit. 3435 * The NFSv3 commit RPC is limited to a 32 bit byte count. 3436 */ 3437 bytes = info->end_off - info->beg_off; 3438 if (bytes > 0x40000000) 3439 bytes = 0x40000000; 3440 if (error) { 3441 retv = -error; 3442 } else { 3443 retv = nfs_commitrpc_uio(vp, info->beg_off, 3444 (int)bytes, info->td); 3445 if (retv == NFSERR_STALEWRITEVERF) 3446 nfs_clearcommit(vp->v_mount); 3447 } 3448 3449 /* 3450 * Now, either mark the blocks I/O done or mark the 3451 * blocks dirty, depending on whether the commit 3452 * succeeded. 3453 */ 3454 for (i = 0; i < info->bvsize; ++i) { 3455 bp = info->bvary[i]; 3456 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) { 3457 /* 3458 * Either an error or the original 3459 * vfs_busy_pages() cleared B_NEEDCOMMIT 3460 * due to finding new dirty VM pages in 3461 * the buffer. 3462 * 3463 * Leave B_DELWRI intact. 3464 */ 3465 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 3466 vfs_unbusy_pages(bp); 3467 bp->b_cmd = BUF_CMD_DONE; 3468 bqrelse(bp); 3469 } else { 3470 /* 3471 * Success, remove B_DELWRI ( bundirty() ). 3472 * 3473 * b_dirtyoff/b_dirtyend seem to be NFS 3474 * specific. We should probably move that 3475 * into bundirty(). XXX 3476 * 3477 * We are faking an I/O write, we have to 3478 * start the transaction in order to 3479 * immediately biodone() it. 3480 */ 3481 bundirty(bp); 3482 bp->b_flags &= ~B_ERROR; 3483 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 3484 bp->b_dirtyoff = bp->b_dirtyend = 0; 3485 biodone(&bp->b_bio1); 3486 } 3487 } 3488 info->bvsize = 0; 3489 } 3490 return (error); 3491 } 3492 3493 /* 3494 * NFS advisory byte-level locks. 3495 * Currently unsupported. 3496 * 3497 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl, 3498 * int a_flags) 3499 */ 3500 static int 3501 nfs_advlock(struct vop_advlock_args *ap) 3502 { 3503 struct nfsnode *np = VTONFS(ap->a_vp); 3504 3505 /* no token lock currently required */ 3506 /* 3507 * The following kludge is to allow diskless support to work 3508 * until a real NFS lockd is implemented. Basically, just pretend 3509 * that this is a local lock. 3510 */ 3511 return (lf_advlock(ap, &(np->n_lockf), np->n_size)); 3512 } 3513 3514 /* 3515 * Print out the contents of an nfsnode. 3516 * 3517 * nfs_print(struct vnode *a_vp) 3518 */ 3519 static int 3520 nfs_print(struct vop_print_args *ap) 3521 { 3522 struct vnode *vp = ap->a_vp; 3523 struct nfsnode *np = VTONFS(vp); 3524 3525 kprintf("tag VT_NFS, fileid %lld fsid 0x%x", 3526 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid); 3527 if (vp->v_type == VFIFO) 3528 fifo_printinfo(vp); 3529 kprintf("\n"); 3530 return (0); 3531 } 3532 3533 /* 3534 * nfs special file access vnode op. 3535 * 3536 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred) 3537 */ 3538 static int 3539 nfs_laccess(struct vop_access_args *ap) 3540 { 3541 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount); 3542 struct vattr vattr; 3543 int error; 3544 3545 lwkt_gettoken(&nmp->nm_token); 3546 error = VOP_GETATTR(ap->a_vp, &vattr); 3547 if (error == 0) { 3548 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid, 3549 vattr.va_mode, 0); 3550 } 3551 lwkt_reltoken(&nmp->nm_token); 3552 3553 return (error); 3554 } 3555 3556 /* 3557 * Read wrapper for fifos. 3558 * 3559 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 3560 * struct ucred *a_cred) 3561 */ 3562 static int 3563 nfsfifo_read(struct vop_read_args *ap) 3564 { 3565 struct nfsnode *np = VTONFS(ap->a_vp); 3566 3567 /* no token access required */ 3568 /* 3569 * Set access flag. 3570 */ 3571 np->n_flag |= NACC; 3572 getnanotime(&np->n_atim); 3573 return (VOCALL(&fifo_vnode_vops, &ap->a_head)); 3574 } 3575 3576 /* 3577 * Write wrapper for fifos. 3578 * 3579 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 3580 * struct ucred *a_cred) 3581 */ 3582 static int 3583 nfsfifo_write(struct vop_write_args *ap) 3584 { 3585 struct nfsnode *np = VTONFS(ap->a_vp); 3586 3587 /* no token access required */ 3588 /* 3589 * Set update flag. 3590 */ 3591 np->n_flag |= NUPD; 3592 getnanotime(&np->n_mtim); 3593 return (VOCALL(&fifo_vnode_vops, &ap->a_head)); 3594 } 3595 3596 /* 3597 * Close wrapper for fifos. 3598 * 3599 * Update the times on the nfsnode then do fifo close. 3600 * 3601 * nfsfifo_close(struct vnode *a_vp, int a_fflag) 3602 */ 3603 static int 3604 nfsfifo_close(struct vop_close_args *ap) 3605 { 3606 struct vnode *vp = ap->a_vp; 3607 struct nfsnode *np = VTONFS(vp); 3608 struct vattr vattr; 3609 struct timespec ts; 3610 3611 /* no token access required */ 3612 3613 if (np->n_flag & (NACC | NUPD)) { 3614 getnanotime(&ts); 3615 if (np->n_flag & NACC) 3616 np->n_atim = ts; 3617 if (np->n_flag & NUPD) 3618 np->n_mtim = ts; 3619 np->n_flag |= NCHG; 3620 if (vp->v_sysref.refcnt == 1 && 3621 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 3622 VATTR_NULL(&vattr); 3623 if (np->n_flag & NACC) 3624 vattr.va_atime = np->n_atim; 3625 if (np->n_flag & NUPD) 3626 vattr.va_mtime = np->n_mtim; 3627 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE)); 3628 } 3629 } 3630 return (VOCALL(&fifo_vnode_vops, &ap->a_head)); 3631 } 3632 3633