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