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