nfs_vnops.c revision 153786
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 * 4. 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 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_vnops.c 153786 2005-12-28 10:03:48Z delphij $"); 37 38/* 39 * vnode op calls for Sun NFS version 2 and 3 40 */ 41 42#include "opt_inet.h" 43 44#include <sys/param.h> 45#include <sys/kernel.h> 46#include <sys/systm.h> 47#include <sys/resourcevar.h> 48#include <sys/proc.h> 49#include <sys/mount.h> 50#include <sys/bio.h> 51#include <sys/buf.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/namei.h> 55#include <sys/socket.h> 56#include <sys/vnode.h> 57#include <sys/dirent.h> 58#include <sys/fcntl.h> 59#include <sys/lockf.h> 60#include <sys/stat.h> 61#include <sys/sysctl.h> 62#include <sys/signalvar.h> 63 64#include <vm/vm.h> 65#include <vm/vm_object.h> 66#include <vm/vm_extern.h> 67#include <vm/vm_object.h> 68 69#include <fs/fifofs/fifo.h> 70 71#include <rpc/rpcclnt.h> 72 73#include <nfs/rpcv2.h> 74#include <nfs/nfsproto.h> 75#include <nfsclient/nfs.h> 76#include <nfsclient/nfsnode.h> 77#include <nfsclient/nfsmount.h> 78#include <nfsclient/nfs_lock.h> 79#include <nfs/xdr_subs.h> 80#include <nfsclient/nfsm_subs.h> 81 82#include <net/if.h> 83#include <netinet/in.h> 84#include <netinet/in_var.h> 85 86/* Defs */ 87#define TRUE 1 88#define FALSE 0 89 90/* 91 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these 92 * calls are not in getblk() and brelse() so that they would not be necessary 93 * here. 94 */ 95#ifndef B_VMIO 96#define vfs_busy_pages(bp, f) 97#endif 98 99static vop_read_t nfsfifo_read; 100static vop_write_t nfsfifo_write; 101static vop_close_t nfsfifo_close; 102static int nfs_flush(struct vnode *, int, struct thread *, 103 int); 104static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *, 105 struct thread *); 106static vop_lookup_t nfs_lookup; 107static vop_create_t nfs_create; 108static vop_mknod_t nfs_mknod; 109static vop_open_t nfs_open; 110static vop_close_t nfs_close; 111static vop_access_t nfs_access; 112static vop_getattr_t nfs_getattr; 113static vop_setattr_t nfs_setattr; 114static vop_read_t nfs_read; 115static vop_fsync_t nfs_fsync; 116static vop_remove_t nfs_remove; 117static vop_link_t nfs_link; 118static vop_rename_t nfs_rename; 119static vop_mkdir_t nfs_mkdir; 120static vop_rmdir_t nfs_rmdir; 121static vop_symlink_t nfs_symlink; 122static vop_readdir_t nfs_readdir; 123static vop_strategy_t nfs_strategy; 124static int nfs_lookitup(struct vnode *, const char *, int, 125 struct ucred *, struct thread *, struct nfsnode **); 126static int nfs_sillyrename(struct vnode *, struct vnode *, 127 struct componentname *); 128static vop_access_t nfsspec_access; 129static vop_readlink_t nfs_readlink; 130static vop_print_t nfs_print; 131static vop_advlock_t nfs_advlock; 132 133/* 134 * Global vfs data structures for nfs 135 */ 136struct vop_vector nfs_vnodeops = { 137 .vop_default = &default_vnodeops, 138 .vop_access = nfs_access, 139 .vop_advlock = nfs_advlock, 140 .vop_close = nfs_close, 141 .vop_create = nfs_create, 142 .vop_fsync = nfs_fsync, 143 .vop_getattr = nfs_getattr, 144 .vop_getpages = nfs_getpages, 145 .vop_putpages = nfs_putpages, 146 .vop_inactive = nfs_inactive, 147 .vop_lease = VOP_NULL, 148 .vop_link = nfs_link, 149 .vop_lookup = nfs_lookup, 150 .vop_mkdir = nfs_mkdir, 151 .vop_mknod = nfs_mknod, 152 .vop_open = nfs_open, 153 .vop_print = nfs_print, 154 .vop_read = nfs_read, 155 .vop_readdir = nfs_readdir, 156 .vop_readlink = nfs_readlink, 157 .vop_reclaim = nfs_reclaim, 158 .vop_remove = nfs_remove, 159 .vop_rename = nfs_rename, 160 .vop_rmdir = nfs_rmdir, 161 .vop_setattr = nfs_setattr, 162 .vop_strategy = nfs_strategy, 163 .vop_symlink = nfs_symlink, 164 .vop_write = nfs_write, 165}; 166 167struct vop_vector nfs_fifoops = { 168 .vop_default = &fifo_specops, 169 .vop_access = nfsspec_access, 170 .vop_close = nfsfifo_close, 171 .vop_fsync = nfs_fsync, 172 .vop_getattr = nfs_getattr, 173 .vop_inactive = nfs_inactive, 174 .vop_print = nfs_print, 175 .vop_read = nfsfifo_read, 176 .vop_reclaim = nfs_reclaim, 177 .vop_setattr = nfs_setattr, 178 .vop_write = nfsfifo_write, 179}; 180 181static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, 182 struct componentname *cnp, struct vattr *vap); 183static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen, 184 struct ucred *cred, struct thread *td); 185static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, 186 int fnamelen, struct vnode *tdvp, 187 const char *tnameptr, int tnamelen, 188 struct ucred *cred, struct thread *td); 189static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp, 190 struct sillyrename *sp); 191 192/* 193 * Global variables 194 */ 195struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 196struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON]; 197int nfs_numasync = 0; 198#define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1)) 199 200SYSCTL_DECL(_vfs_nfs); 201 202static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO; 203SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW, 204 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout"); 205 206static int nfsv3_commit_on_close = 0; 207SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW, 208 &nfsv3_commit_on_close, 0, "write+commit on close, else only write"); 209 210static int nfs_clean_pages_on_close = 1; 211SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW, 212 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close"); 213 214int nfs_directio_enable = 0; 215SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW, 216 &nfs_directio_enable, 0, "Enable NFS directio"); 217 218/* 219 * This sysctl allows other processes to mmap a file that has been opened 220 * O_DIRECT by a process. In general, having processes mmap the file while 221 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow 222 * this by default to prevent DoS attacks - to prevent a malicious user from 223 * opening up files O_DIRECT preventing other users from mmap'ing these 224 * files. "Protected" environments where stricter consistency guarantees are 225 * required can disable this knob. The process that opened the file O_DIRECT 226 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not 227 * meaningful. 228 */ 229int nfs_directio_allow_mmap = 1; 230SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW, 231 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens"); 232 233#if 0 234SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD, 235 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count"); 236 237SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD, 238 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count"); 239#endif 240 241#define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \ 242 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \ 243 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP) 244static int 245nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td, 246 struct ucred *cred) 247{ 248 const int v3 = 1; 249 u_int32_t *tl; 250 int error = 0, attrflag; 251 252 struct mbuf *mreq, *mrep, *md, *mb; 253 caddr_t bpos, dpos; 254 u_int32_t rmode; 255 struct nfsnode *np = VTONFS(vp); 256 257 nfsstats.rpccnt[NFSPROC_ACCESS]++; 258 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED); 259 mb = mreq; 260 bpos = mtod(mb, caddr_t); 261 nfsm_fhtom(vp, v3); 262 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED); 263 *tl = txdr_unsigned(wmode); 264 nfsm_request(vp, NFSPROC_ACCESS, td, cred); 265 nfsm_postop_attr(vp, attrflag); 266 if (!error) { 267 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 268 rmode = fxdr_unsigned(u_int32_t, *tl); 269 np->n_mode = rmode; 270 np->n_modeuid = cred->cr_uid; 271 np->n_modestamp = time_second; 272 } 273 m_freem(mrep); 274nfsmout: 275 return (error); 276} 277 278/* 279 * nfs access vnode op. 280 * For nfs version 2, just return ok. File accesses may fail later. 281 * For nfs version 3, use the access rpc to check accessibility. If file modes 282 * are changed on the server, accesses might still fail later. 283 */ 284static int 285nfs_access(struct vop_access_args *ap) 286{ 287 struct vnode *vp = ap->a_vp; 288 int error = 0; 289 u_int32_t mode, wmode; 290 int v3 = NFS_ISV3(vp); 291 struct nfsnode *np = VTONFS(vp); 292 293 /* 294 * Disallow write attempts on filesystems mounted read-only; 295 * unless the file is a socket, fifo, or a block or character 296 * device resident on the filesystem. 297 */ 298 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 299 switch (vp->v_type) { 300 case VREG: 301 case VDIR: 302 case VLNK: 303 return (EROFS); 304 default: 305 break; 306 } 307 } 308 /* 309 * For nfs v3, check to see if we have done this recently, and if 310 * so return our cached result instead of making an ACCESS call. 311 * If not, do an access rpc, otherwise you are stuck emulating 312 * ufs_access() locally using the vattr. This may not be correct, 313 * since the server may apply other access criteria such as 314 * client uid-->server uid mapping that we do not know about. 315 */ 316 if (v3) { 317 if (ap->a_mode & VREAD) 318 mode = NFSV3ACCESS_READ; 319 else 320 mode = 0; 321 if (vp->v_type != VDIR) { 322 if (ap->a_mode & VWRITE) 323 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND); 324 if (ap->a_mode & VEXEC) 325 mode |= NFSV3ACCESS_EXECUTE; 326 } else { 327 if (ap->a_mode & VWRITE) 328 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND | 329 NFSV3ACCESS_DELETE); 330 if (ap->a_mode & VEXEC) 331 mode |= NFSV3ACCESS_LOOKUP; 332 } 333 /* XXX safety belt, only make blanket request if caching */ 334 if (nfsaccess_cache_timeout > 0) { 335 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY | 336 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE | 337 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP; 338 } else { 339 wmode = mode; 340 } 341 342 /* 343 * Does our cached result allow us to give a definite yes to 344 * this request? 345 */ 346 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) && 347 (ap->a_cred->cr_uid == np->n_modeuid) && 348 ((np->n_mode & mode) == mode)) { 349 nfsstats.accesscache_hits++; 350 } else { 351 /* 352 * Either a no, or a don't know. Go to the wire. 353 */ 354 nfsstats.accesscache_misses++; 355 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred); 356 if (!error) { 357 if ((np->n_mode & mode) != mode) { 358 error = EACCES; 359 } 360 } 361 } 362 return (error); 363 } else { 364 if ((error = nfsspec_access(ap)) != 0) 365 return (error); 366 367 /* 368 * Attempt to prevent a mapped root from accessing a file 369 * which it shouldn't. We try to read a byte from the file 370 * if the user is root and the file is not zero length. 371 * After calling nfsspec_access, we should have the correct 372 * file size cached. 373 */ 374 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD) 375 && VTONFS(vp)->n_size > 0) { 376 struct iovec aiov; 377 struct uio auio; 378 char buf[1]; 379 380 aiov.iov_base = buf; 381 aiov.iov_len = 1; 382 auio.uio_iov = &aiov; 383 auio.uio_iovcnt = 1; 384 auio.uio_offset = 0; 385 auio.uio_resid = 1; 386 auio.uio_segflg = UIO_SYSSPACE; 387 auio.uio_rw = UIO_READ; 388 auio.uio_td = ap->a_td; 389 390 if (vp->v_type == VREG) 391 error = nfs_readrpc(vp, &auio, ap->a_cred); 392 else if (vp->v_type == VDIR) { 393 char* bp; 394 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK); 395 aiov.iov_base = bp; 396 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ; 397 error = nfs_readdirrpc(vp, &auio, ap->a_cred); 398 free(bp, M_TEMP); 399 } else if (vp->v_type == VLNK) 400 error = nfs_readlinkrpc(vp, &auio, ap->a_cred); 401 else 402 error = EACCES; 403 } 404 return (error); 405 } 406} 407 408/* 409 * nfs open vnode op 410 * Check to see if the type is ok 411 * and that deletion is not in progress. 412 * For paged in text files, you will need to flush the page cache 413 * if consistency is lost. 414 */ 415/* ARGSUSED */ 416static int 417nfs_open(struct vop_open_args *ap) 418{ 419 struct vnode *vp = ap->a_vp; 420 struct nfsnode *np = VTONFS(vp); 421 struct vattr vattr; 422 int error; 423 int fmode = ap->a_mode; 424 425 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) 426 return (EOPNOTSUPP); 427 428 /* 429 * Get a valid lease. If cached data is stale, flush it. 430 */ 431 if (np->n_flag & NMODIFIED) { 432 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1); 433 if (error == EINTR || error == EIO) 434 return (error); 435 np->n_attrstamp = 0; 436 if (vp->v_type == VDIR) 437 np->n_direofoffset = 0; 438 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td); 439 if (error) 440 return (error); 441 np->n_mtime = vattr.va_mtime; 442 } else { 443 np->n_attrstamp = 0; 444 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td); 445 if (error) 446 return (error); 447 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) { 448 if (vp->v_type == VDIR) 449 np->n_direofoffset = 0; 450 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1); 451 if (error == EINTR || error == EIO) 452 return (error); 453 np->n_mtime = vattr.va_mtime; 454 } 455 } 456 /* 457 * If the object has >= 1 O_DIRECT active opens, we disable caching. 458 */ 459 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) { 460 if (np->n_directio_opens == 0) { 461 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1); 462 if (error) 463 return (error); 464 np->n_flag |= NNONCACHE; 465 } 466 np->n_directio_opens++; 467 } 468 np->ra_expect_lbn = 0; 469 vnode_create_vobject(vp, vattr.va_size, ap->a_td); 470 return (0); 471} 472 473/* 474 * nfs close vnode op 475 * What an NFS client should do upon close after writing is a debatable issue. 476 * Most NFS clients push delayed writes to the server upon close, basically for 477 * two reasons: 478 * 1 - So that any write errors may be reported back to the client process 479 * doing the close system call. By far the two most likely errors are 480 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. 481 * 2 - To put a worst case upper bound on cache inconsistency between 482 * multiple clients for the file. 483 * There is also a consistency problem for Version 2 of the protocol w.r.t. 484 * not being able to tell if other clients are writing a file concurrently, 485 * since there is no way of knowing if the changed modify time in the reply 486 * is only due to the write for this client. 487 * (NFS Version 3 provides weak cache consistency data in the reply that 488 * should be sufficient to detect and handle this case.) 489 * 490 * The current code does the following: 491 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers 492 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate 493 * or commit them (this satisfies 1 and 2 except for the 494 * case where the server crashes after this close but 495 * before the commit RPC, which is felt to be "good 496 * enough". Changing the last argument to nfs_flush() to 497 * a 1 would force a commit operation, if it is felt a 498 * commit is necessary now. 499 */ 500/* ARGSUSED */ 501static int 502nfs_close(struct vop_close_args *ap) 503{ 504 struct vnode *vp = ap->a_vp; 505 struct nfsnode *np = VTONFS(vp); 506 int error = 0; 507 int fmode = ap->a_fflag; 508 509 if (vp->v_type == VREG) { 510 /* 511 * Examine and clean dirty pages, regardless of NMODIFIED. 512 * This closes a major hole in close-to-open consistency. 513 * We want to push out all dirty pages (and buffers) on 514 * close, regardless of whether they were dirtied by 515 * mmap'ed writes or via write(). 516 */ 517 if (nfs_clean_pages_on_close && vp->v_object) { 518 VM_OBJECT_LOCK(vp->v_object); 519 vm_object_page_clean(vp->v_object, 0, 0, 0); 520 VM_OBJECT_UNLOCK(vp->v_object); 521 } 522 if (np->n_flag & NMODIFIED) { 523 if (NFS_ISV3(vp)) { 524 /* 525 * Under NFSv3 we have dirty buffers to dispose of. We 526 * must flush them to the NFS server. We have the option 527 * of waiting all the way through the commit rpc or just 528 * waiting for the initial write. The default is to only 529 * wait through the initial write so the data is in the 530 * server's cache, which is roughly similar to the state 531 * a standard disk subsystem leaves the file in on close(). 532 * 533 * We cannot clear the NMODIFIED bit in np->n_flag due to 534 * potential races with other processes, and certainly 535 * cannot clear it if we don't commit. 536 */ 537 int cm = nfsv3_commit_on_close ? 1 : 0; 538 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm); 539 /* np->n_flag &= ~NMODIFIED; */ 540 } else 541 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1); 542 } 543 /* 544 * Invalidate the attribute cache in all cases. 545 * An open is going to fetch fresh attrs any way, other procs 546 * on this node that have file open will be forced to do an 547 * otw attr fetch, but this is safe. 548 */ 549 np->n_attrstamp = 0; 550 if (np->n_flag & NWRITEERR) { 551 np->n_flag &= ~NWRITEERR; 552 error = np->n_error; 553 } 554 } 555 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) { 556 KASSERT((np->n_directio_opens > 0), 557 ("nfs_close: unexpectedly value (0) of n_directio_opens\n")); 558 np->n_directio_opens--; 559 if (np->n_directio_opens == 0) 560 np->n_flag &= ~NNONCACHE; 561 } 562 return (error); 563} 564 565/* 566 * nfs getattr call from vfs. 567 */ 568static int 569nfs_getattr(struct vop_getattr_args *ap) 570{ 571 struct vnode *vp = ap->a_vp; 572 struct nfsnode *np = VTONFS(vp); 573 caddr_t bpos, dpos; 574 int error = 0; 575 struct mbuf *mreq, *mrep, *md, *mb; 576 int v3 = NFS_ISV3(vp); 577 578 /* 579 * Update local times for special files. 580 */ 581 if (np->n_flag & (NACC | NUPD)) 582 np->n_flag |= NCHG; 583 /* 584 * First look in the cache. 585 */ 586 if (nfs_getattrcache(vp, ap->a_vap) == 0) 587 return (0); 588 589 if (v3 && nfsaccess_cache_timeout > 0) { 590 nfsstats.accesscache_misses++; 591 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred); 592 if (nfs_getattrcache(vp, ap->a_vap) == 0) 593 return (0); 594 } 595 596 nfsstats.rpccnt[NFSPROC_GETATTR]++; 597 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3)); 598 mb = mreq; 599 bpos = mtod(mb, caddr_t); 600 nfsm_fhtom(vp, v3); 601 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred); 602 if (!error) { 603 nfsm_loadattr(vp, ap->a_vap); 604 } 605 m_freem(mrep); 606nfsmout: 607 return (error); 608} 609 610/* 611 * nfs setattr call. 612 */ 613static int 614nfs_setattr(struct vop_setattr_args *ap) 615{ 616 struct vnode *vp = ap->a_vp; 617 struct nfsnode *np = VTONFS(vp); 618 struct vattr *vap = ap->a_vap; 619 int error = 0; 620 u_quad_t tsize; 621 622#ifndef nolint 623 tsize = (u_quad_t)0; 624#endif 625 626 /* 627 * Setting of flags is not supported. 628 */ 629 if (vap->va_flags != VNOVAL) 630 return (EOPNOTSUPP); 631 632 /* 633 * Disallow write attempts if the filesystem is mounted read-only. 634 */ 635 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 636 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 637 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 638 (vp->v_mount->mnt_flag & MNT_RDONLY)) 639 return (EROFS); 640 if (vap->va_size != VNOVAL) { 641 switch (vp->v_type) { 642 case VDIR: 643 return (EISDIR); 644 case VCHR: 645 case VBLK: 646 case VSOCK: 647 case VFIFO: 648 if (vap->va_mtime.tv_sec == VNOVAL && 649 vap->va_atime.tv_sec == VNOVAL && 650 vap->va_mode == (mode_t)VNOVAL && 651 vap->va_uid == (uid_t)VNOVAL && 652 vap->va_gid == (gid_t)VNOVAL) 653 return (0); 654 vap->va_size = VNOVAL; 655 break; 656 default: 657 /* 658 * Disallow write attempts if the filesystem is 659 * mounted read-only. 660 */ 661 if (vp->v_mount->mnt_flag & MNT_RDONLY) 662 return (EROFS); 663 664 /* 665 * We run vnode_pager_setsize() early (why?), 666 * we must set np->n_size now to avoid vinvalbuf 667 * V_SAVE races that might setsize a lower 668 * value. 669 */ 670 671 tsize = np->n_size; 672 error = nfs_meta_setsize(vp, ap->a_cred, 673 ap->a_td, vap->va_size); 674 675 if (np->n_flag & NMODIFIED) { 676 if (vap->va_size == 0) 677 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1); 678 else 679 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1); 680 if (error) { 681 vnode_pager_setsize(vp, np->n_size); 682 return (error); 683 } 684 } 685 /* 686 * np->n_size has already been set to vap->va_size 687 * in nfs_meta_setsize(). We must set it again since 688 * nfs_loadattrcache() could be called through 689 * nfs_meta_setsize() and could modify np->n_size. 690 */ 691 np->n_vattr.va_size = np->n_size = vap->va_size; 692 }; 693 } else if ((vap->va_mtime.tv_sec != VNOVAL || 694 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) && 695 vp->v_type == VREG && 696 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 && 697 (error == EINTR || error == EIO)) 698 return (error); 699 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td); 700 if (error && vap->va_size != VNOVAL) { 701 np->n_size = np->n_vattr.va_size = tsize; 702 vnode_pager_setsize(vp, np->n_size); 703 } 704 return (error); 705} 706 707/* 708 * Do an nfs setattr rpc. 709 */ 710static int 711nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred, 712 struct thread *td) 713{ 714 struct nfsv2_sattr *sp; 715 struct nfsnode *np = VTONFS(vp); 716 caddr_t bpos, dpos; 717 u_int32_t *tl; 718 int error = 0, wccflag = NFSV3_WCCRATTR; 719 struct mbuf *mreq, *mrep, *md, *mb; 720 int v3 = NFS_ISV3(vp); 721 722 nfsstats.rpccnt[NFSPROC_SETATTR]++; 723 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3)); 724 mb = mreq; 725 bpos = mtod(mb, caddr_t); 726 nfsm_fhtom(vp, v3); 727 if (v3) { 728 nfsm_v3attrbuild(vap, TRUE); 729 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED); 730 *tl = nfs_false; 731 } else { 732 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR); 733 if (vap->va_mode == (mode_t)VNOVAL) 734 sp->sa_mode = nfs_xdrneg1; 735 else 736 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode); 737 if (vap->va_uid == (uid_t)VNOVAL) 738 sp->sa_uid = nfs_xdrneg1; 739 else 740 sp->sa_uid = txdr_unsigned(vap->va_uid); 741 if (vap->va_gid == (gid_t)VNOVAL) 742 sp->sa_gid = nfs_xdrneg1; 743 else 744 sp->sa_gid = txdr_unsigned(vap->va_gid); 745 sp->sa_size = txdr_unsigned(vap->va_size); 746 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 747 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 748 } 749 nfsm_request(vp, NFSPROC_SETATTR, td, cred); 750 if (v3) { 751 np->n_modestamp = 0; 752 nfsm_wcc_data(vp, wccflag); 753 } else 754 nfsm_loadattr(vp, NULL); 755 m_freem(mrep); 756nfsmout: 757 return (error); 758} 759 760/* 761 * nfs lookup call, one step at a time... 762 * First look in cache 763 * If not found, unlock the directory nfsnode and do the rpc 764 */ 765static int 766nfs_lookup(struct vop_lookup_args *ap) 767{ 768 struct componentname *cnp = ap->a_cnp; 769 struct vnode *dvp = ap->a_dvp; 770 struct vnode **vpp = ap->a_vpp; 771 int flags = cnp->cn_flags; 772 struct vnode *newvp; 773 struct nfsmount *nmp; 774 caddr_t bpos, dpos; 775 struct mbuf *mreq, *mrep, *md, *mb; 776 long len; 777 nfsfh_t *fhp; 778 struct nfsnode *np; 779 int error = 0, attrflag, fhsize; 780 int v3 = NFS_ISV3(dvp); 781 struct thread *td = cnp->cn_thread; 782 783 *vpp = NULLVP; 784 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 785 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 786 return (EROFS); 787 if (dvp->v_type != VDIR) 788 return (ENOTDIR); 789 nmp = VFSTONFS(dvp->v_mount); 790 np = VTONFS(dvp); 791 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) { 792 *vpp = NULLVP; 793 return (error); 794 } 795 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) { 796 struct vattr vattr; 797 798 newvp = *vpp; 799 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td) 800 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) { 801 nfsstats.lookupcache_hits++; 802 if (cnp->cn_nameiop != LOOKUP && 803 (flags & ISLASTCN)) 804 cnp->cn_flags |= SAVENAME; 805 return (0); 806 } 807 cache_purge(newvp); 808 if (dvp != newvp) 809 vput(newvp); 810 else 811 vrele(newvp); 812 *vpp = NULLVP; 813 } 814 error = 0; 815 newvp = NULLVP; 816 nfsstats.lookupcache_misses++; 817 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 818 len = cnp->cn_namelen; 819 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP, 820 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 821 mb = mreq; 822 bpos = mtod(mb, caddr_t); 823 nfsm_fhtom(dvp, v3); 824 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 825 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred); 826 if (error) { 827 if (v3) { 828 nfsm_postop_attr(dvp, attrflag); 829 m_freem(mrep); 830 } 831 goto nfsmout; 832 } 833 nfsm_getfh(fhp, fhsize, v3); 834 835 /* 836 * Handle RENAME case... 837 */ 838 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) { 839 if (NFS_CMPFH(np, fhp, fhsize)) { 840 m_freem(mrep); 841 return (EISDIR); 842 } 843 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 844 if (error) { 845 m_freem(mrep); 846 return (error); 847 } 848 newvp = NFSTOV(np); 849 if (v3) { 850 nfsm_postop_attr(newvp, attrflag); 851 nfsm_postop_attr(dvp, attrflag); 852 } else 853 nfsm_loadattr(newvp, NULL); 854 *vpp = newvp; 855 m_freem(mrep); 856 cnp->cn_flags |= SAVENAME; 857 return (0); 858 } 859 860 if (flags & ISDOTDOT) { 861 VOP_UNLOCK(dvp, 0, td); 862 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 863 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td); 864 if (error) 865 return (error); 866 newvp = NFSTOV(np); 867 } else if (NFS_CMPFH(np, fhp, fhsize)) { 868 VREF(dvp); 869 newvp = dvp; 870 } else { 871 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 872 if (error) { 873 m_freem(mrep); 874 return (error); 875 } 876 newvp = NFSTOV(np); 877 } 878 if (v3) { 879 nfsm_postop_attr(newvp, attrflag); 880 nfsm_postop_attr(dvp, attrflag); 881 } else 882 nfsm_loadattr(newvp, NULL); 883 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 884 cnp->cn_flags |= SAVENAME; 885 if ((cnp->cn_flags & MAKEENTRY) && 886 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) { 887 np->n_ctime = np->n_vattr.va_ctime.tv_sec; 888 cache_enter(dvp, newvp, cnp); 889 } 890 *vpp = newvp; 891 m_freem(mrep); 892nfsmout: 893 if (error) { 894 if (newvp != NULLVP) { 895 vput(newvp); 896 *vpp = NULLVP; 897 } 898 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) && 899 (flags & ISLASTCN) && error == ENOENT) { 900 if (dvp->v_mount->mnt_flag & MNT_RDONLY) 901 error = EROFS; 902 else 903 error = EJUSTRETURN; 904 } 905 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 906 cnp->cn_flags |= SAVENAME; 907 } 908 return (error); 909} 910 911/* 912 * nfs read call. 913 * Just call nfs_bioread() to do the work. 914 */ 915static int 916nfs_read(struct vop_read_args *ap) 917{ 918 struct vnode *vp = ap->a_vp; 919 920 switch (vp->v_type) { 921 case VREG: 922 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred)); 923 case VDIR: 924 return (EISDIR); 925 default: 926 return (EOPNOTSUPP); 927 } 928} 929 930/* 931 * nfs readlink call 932 */ 933static int 934nfs_readlink(struct vop_readlink_args *ap) 935{ 936 struct vnode *vp = ap->a_vp; 937 938 if (vp->v_type != VLNK) 939 return (EINVAL); 940 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred)); 941} 942 943/* 944 * Do a readlink rpc. 945 * Called by nfs_doio() from below the buffer cache. 946 */ 947int 948nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 949{ 950 caddr_t bpos, dpos; 951 int error = 0, len, attrflag; 952 struct mbuf *mreq, *mrep, *md, *mb; 953 int v3 = NFS_ISV3(vp); 954 955 nfsstats.rpccnt[NFSPROC_READLINK]++; 956 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3)); 957 mb = mreq; 958 bpos = mtod(mb, caddr_t); 959 nfsm_fhtom(vp, v3); 960 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred); 961 if (v3) 962 nfsm_postop_attr(vp, attrflag); 963 if (!error) { 964 nfsm_strsiz(len, NFS_MAXPATHLEN); 965 if (len == NFS_MAXPATHLEN) { 966 struct nfsnode *np = VTONFS(vp); 967 if (np->n_size && np->n_size < NFS_MAXPATHLEN) 968 len = np->n_size; 969 } 970 nfsm_mtouio(uiop, len); 971 } 972 m_freem(mrep); 973nfsmout: 974 return (error); 975} 976 977/* 978 * nfs read rpc call 979 * Ditto above 980 */ 981int 982nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 983{ 984 u_int32_t *tl; 985 caddr_t bpos, dpos; 986 struct mbuf *mreq, *mrep, *md, *mb; 987 struct nfsmount *nmp; 988 int error = 0, len, retlen, tsiz, eof, attrflag; 989 int v3 = NFS_ISV3(vp); 990 991#ifndef nolint 992 eof = 0; 993#endif 994 nmp = VFSTONFS(vp->v_mount); 995 tsiz = uiop->uio_resid; 996 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 997 return (EFBIG); 998 while (tsiz > 0) { 999 nfsstats.rpccnt[NFSPROC_READ]++; 1000 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 1001 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3); 1002 mb = mreq; 1003 bpos = mtod(mb, caddr_t); 1004 nfsm_fhtom(vp, v3); 1005 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3); 1006 if (v3) { 1007 txdr_hyper(uiop->uio_offset, tl); 1008 *(tl + 2) = txdr_unsigned(len); 1009 } else { 1010 *tl++ = txdr_unsigned(uiop->uio_offset); 1011 *tl++ = txdr_unsigned(len); 1012 *tl = 0; 1013 } 1014 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred); 1015 if (v3) { 1016 nfsm_postop_attr(vp, attrflag); 1017 if (error) { 1018 m_freem(mrep); 1019 goto nfsmout; 1020 } 1021 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED); 1022 eof = fxdr_unsigned(int, *(tl + 1)); 1023 } else 1024 nfsm_loadattr(vp, NULL); 1025 nfsm_strsiz(retlen, nmp->nm_rsize); 1026 nfsm_mtouio(uiop, retlen); 1027 m_freem(mrep); 1028 tsiz -= retlen; 1029 if (v3) { 1030 if (eof || retlen == 0) { 1031 tsiz = 0; 1032 } 1033 } else if (retlen < len) { 1034 tsiz = 0; 1035 } 1036 } 1037nfsmout: 1038 return (error); 1039} 1040 1041/* 1042 * nfs write call 1043 */ 1044int 1045nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred, 1046 int *iomode, int *must_commit) 1047{ 1048 u_int32_t *tl; 1049 int32_t backup; 1050 caddr_t bpos, dpos; 1051 struct mbuf *mreq, *mrep, *md, *mb; 1052 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1053 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit; 1054 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC; 1055 1056#ifndef DIAGNOSTIC 1057 if (uiop->uio_iovcnt != 1) 1058 panic("nfs: writerpc iovcnt > 1"); 1059#endif 1060 *must_commit = 0; 1061 tsiz = uiop->uio_resid; 1062 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1063 return (EFBIG); 1064 while (tsiz > 0) { 1065 nfsstats.rpccnt[NFSPROC_WRITE]++; 1066 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz; 1067 mreq = nfsm_reqhead(vp, NFSPROC_WRITE, 1068 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); 1069 mb = mreq; 1070 bpos = mtod(mb, caddr_t); 1071 nfsm_fhtom(vp, v3); 1072 if (v3) { 1073 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED); 1074 txdr_hyper(uiop->uio_offset, tl); 1075 tl += 2; 1076 *tl++ = txdr_unsigned(len); 1077 *tl++ = txdr_unsigned(*iomode); 1078 *tl = txdr_unsigned(len); 1079 } else { 1080 u_int32_t x; 1081 1082 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED); 1083 /* Set both "begin" and "current" to non-garbage. */ 1084 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1085 *tl++ = x; /* "begin offset" */ 1086 *tl++ = x; /* "current offset" */ 1087 x = txdr_unsigned(len); 1088 *tl++ = x; /* total to this offset */ 1089 *tl = x; /* size of this write */ 1090 } 1091 nfsm_uiotom(uiop, len); 1092 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred); 1093 if (v3) { 1094 wccflag = NFSV3_WCCCHK; 1095 nfsm_wcc_data(vp, wccflag); 1096 if (!error) { 1097 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED 1098 + NFSX_V3WRITEVERF); 1099 rlen = fxdr_unsigned(int, *tl++); 1100 if (rlen == 0) { 1101 error = NFSERR_IO; 1102 m_freem(mrep); 1103 break; 1104 } else if (rlen < len) { 1105 backup = len - rlen; 1106 uiop->uio_iov->iov_base = 1107 (char *)uiop->uio_iov->iov_base - 1108 backup; 1109 uiop->uio_iov->iov_len += backup; 1110 uiop->uio_offset -= backup; 1111 uiop->uio_resid += backup; 1112 len = rlen; 1113 } 1114 commit = fxdr_unsigned(int, *tl++); 1115 1116 /* 1117 * Return the lowest committment level 1118 * obtained by any of the RPCs. 1119 */ 1120 if (committed == NFSV3WRITE_FILESYNC) 1121 committed = commit; 1122 else if (committed == NFSV3WRITE_DATASYNC && 1123 commit == NFSV3WRITE_UNSTABLE) 1124 committed = commit; 1125 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){ 1126 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1127 NFSX_V3WRITEVERF); 1128 nmp->nm_state |= NFSSTA_HASWRITEVERF; 1129 } else if (bcmp((caddr_t)tl, 1130 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) { 1131 *must_commit = 1; 1132 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1133 NFSX_V3WRITEVERF); 1134 } 1135 } 1136 } else 1137 nfsm_loadattr(vp, NULL); 1138 if (wccflag) 1139 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime; 1140 m_freem(mrep); 1141 if (error) 1142 break; 1143 tsiz -= len; 1144 } 1145nfsmout: 1146 if (vp->v_mount->mnt_flag & MNT_ASYNC) 1147 committed = NFSV3WRITE_FILESYNC; 1148 *iomode = committed; 1149 if (error) 1150 uiop->uio_resid = tsiz; 1151 return (error); 1152} 1153 1154/* 1155 * nfs mknod rpc 1156 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1157 * mode set to specify the file type and the size field for rdev. 1158 */ 1159static int 1160nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1161 struct vattr *vap) 1162{ 1163 struct nfsv2_sattr *sp; 1164 u_int32_t *tl; 1165 struct vnode *newvp = NULL; 1166 struct nfsnode *np = NULL; 1167 struct vattr vattr; 1168 caddr_t bpos, dpos; 1169 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0; 1170 struct mbuf *mreq, *mrep, *md, *mb; 1171 u_int32_t rdev; 1172 int v3 = NFS_ISV3(dvp); 1173 1174 if (vap->va_type == VCHR || vap->va_type == VBLK) 1175 rdev = txdr_unsigned(vap->va_rdev); 1176 else if (vap->va_type == VFIFO || vap->va_type == VSOCK) 1177 rdev = nfs_xdrneg1; 1178 else { 1179 return (EOPNOTSUPP); 1180 } 1181 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) { 1182 return (error); 1183 } 1184 nfsstats.rpccnt[NFSPROC_MKNOD]++; 1185 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED + 1186 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1187 mb = mreq; 1188 bpos = mtod(mb, caddr_t); 1189 nfsm_fhtom(dvp, v3); 1190 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1191 if (v3) { 1192 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED); 1193 *tl++ = vtonfsv3_type(vap->va_type); 1194 nfsm_v3attrbuild(vap, FALSE); 1195 if (vap->va_type == VCHR || vap->va_type == VBLK) { 1196 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED); 1197 *tl++ = txdr_unsigned(umajor(vap->va_rdev)); 1198 *tl = txdr_unsigned(uminor(vap->va_rdev)); 1199 } 1200 } else { 1201 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR); 1202 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1203 sp->sa_uid = nfs_xdrneg1; 1204 sp->sa_gid = nfs_xdrneg1; 1205 sp->sa_size = rdev; 1206 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1207 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1208 } 1209 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred); 1210 if (!error) { 1211 nfsm_mtofh(dvp, newvp, v3, gotvp); 1212 if (!gotvp) { 1213 if (newvp) { 1214 vput(newvp); 1215 newvp = NULL; 1216 } 1217 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1218 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np); 1219 if (!error) 1220 newvp = NFSTOV(np); 1221 } 1222 } 1223 if (v3) 1224 nfsm_wcc_data(dvp, wccflag); 1225 m_freem(mrep); 1226nfsmout: 1227 if (error) { 1228 if (newvp) 1229 vput(newvp); 1230 } else { 1231 if (cnp->cn_flags & MAKEENTRY) 1232 cache_enter(dvp, newvp, cnp); 1233 *vpp = newvp; 1234 } 1235 VTONFS(dvp)->n_flag |= NMODIFIED; 1236 if (!wccflag) 1237 VTONFS(dvp)->n_attrstamp = 0; 1238 return (error); 1239} 1240 1241/* 1242 * nfs mknod vop 1243 * just call nfs_mknodrpc() to do the work. 1244 */ 1245/* ARGSUSED */ 1246static int 1247nfs_mknod(struct vop_mknod_args *ap) 1248{ 1249 1250 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap)); 1251} 1252 1253static u_long create_verf; 1254/* 1255 * nfs file create call 1256 */ 1257static int 1258nfs_create(struct vop_create_args *ap) 1259{ 1260 struct vnode *dvp = ap->a_dvp; 1261 struct vattr *vap = ap->a_vap; 1262 struct componentname *cnp = ap->a_cnp; 1263 struct nfsv2_sattr *sp; 1264 u_int32_t *tl; 1265 struct nfsnode *np = NULL; 1266 struct vnode *newvp = NULL; 1267 caddr_t bpos, dpos; 1268 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0; 1269 struct mbuf *mreq, *mrep, *md, *mb; 1270 struct vattr vattr; 1271 int v3 = NFS_ISV3(dvp); 1272 1273 /* 1274 * Oops, not for me.. 1275 */ 1276 if (vap->va_type == VSOCK) 1277 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap)); 1278 1279 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) { 1280 return (error); 1281 } 1282 if (vap->va_vaflags & VA_EXCLUSIVE) 1283 fmode |= O_EXCL; 1284again: 1285 nfsstats.rpccnt[NFSPROC_CREATE]++; 1286 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED + 1287 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1288 mb = mreq; 1289 bpos = mtod(mb, caddr_t); 1290 nfsm_fhtom(dvp, v3); 1291 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1292 if (v3) { 1293 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED); 1294 if (fmode & O_EXCL) { 1295 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE); 1296 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF); 1297#ifdef INET 1298 if (!TAILQ_EMPTY(&in_ifaddrhead)) 1299 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr; 1300 else 1301#endif 1302 *tl++ = create_verf; 1303 *tl = ++create_verf; 1304 } else { 1305 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED); 1306 nfsm_v3attrbuild(vap, FALSE); 1307 } 1308 } else { 1309 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR); 1310 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1311 sp->sa_uid = nfs_xdrneg1; 1312 sp->sa_gid = nfs_xdrneg1; 1313 sp->sa_size = 0; 1314 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1315 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1316 } 1317 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred); 1318 if (!error) { 1319 nfsm_mtofh(dvp, newvp, v3, gotvp); 1320 if (!gotvp) { 1321 if (newvp) { 1322 vput(newvp); 1323 newvp = NULL; 1324 } 1325 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1326 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np); 1327 if (!error) 1328 newvp = NFSTOV(np); 1329 } 1330 } 1331 if (v3) 1332 nfsm_wcc_data(dvp, wccflag); 1333 m_freem(mrep); 1334nfsmout: 1335 if (error) { 1336 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) { 1337 fmode &= ~O_EXCL; 1338 goto again; 1339 } 1340 if (newvp) 1341 vput(newvp); 1342 } else if (v3 && (fmode & O_EXCL)) { 1343 /* 1344 * We are normally called with only a partially initialized 1345 * VAP. Since the NFSv3 spec says that server may use the 1346 * file attributes to store the verifier, the spec requires 1347 * us to do a SETATTR RPC. FreeBSD servers store the verifier 1348 * in atime, but we can't really assume that all servers will 1349 * so we ensure that our SETATTR sets both atime and mtime. 1350 */ 1351 if (vap->va_mtime.tv_sec == VNOVAL) 1352 vfs_timestamp(&vap->va_mtime); 1353 if (vap->va_atime.tv_sec == VNOVAL) 1354 vap->va_atime = vap->va_mtime; 1355 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread); 1356 } 1357 if (!error) { 1358 if (cnp->cn_flags & MAKEENTRY) 1359 cache_enter(dvp, newvp, cnp); 1360 *ap->a_vpp = newvp; 1361 } 1362 VTONFS(dvp)->n_flag |= NMODIFIED; 1363 if (!wccflag) 1364 VTONFS(dvp)->n_attrstamp = 0; 1365 return (error); 1366} 1367 1368/* 1369 * nfs file remove call 1370 * To try and make nfs semantics closer to ufs semantics, a file that has 1371 * other processes using the vnode is renamed instead of removed and then 1372 * removed later on the last close. 1373 * - If v_usecount > 1 1374 * If a rename is not already in the works 1375 * call nfs_sillyrename() to set it up 1376 * else 1377 * do the remove rpc 1378 */ 1379static int 1380nfs_remove(struct vop_remove_args *ap) 1381{ 1382 struct vnode *vp = ap->a_vp; 1383 struct vnode *dvp = ap->a_dvp; 1384 struct componentname *cnp = ap->a_cnp; 1385 struct nfsnode *np = VTONFS(vp); 1386 int error = 0; 1387 struct vattr vattr; 1388 1389#ifndef DIAGNOSTIC 1390 if ((cnp->cn_flags & HASBUF) == 0) 1391 panic("nfs_remove: no name"); 1392 if (vrefcnt(vp) < 1) 1393 panic("nfs_remove: bad v_usecount"); 1394#endif 1395 if (vp->v_type == VDIR) 1396 error = EPERM; 1397 else if (vrefcnt(vp) == 1 || (np->n_sillyrename && 1398 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 && 1399 vattr.va_nlink > 1)) { 1400 /* 1401 * Purge the name cache so that the chance of a lookup for 1402 * the name succeeding while the remove is in progress is 1403 * minimized. Without node locking it can still happen, such 1404 * that an I/O op returns ESTALE, but since you get this if 1405 * another host removes the file.. 1406 */ 1407 cache_purge(vp); 1408 /* 1409 * throw away biocache buffers, mainly to avoid 1410 * unnecessary delayed writes later. 1411 */ 1412 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1); 1413 /* Do the rpc */ 1414 if (error != EINTR && error != EIO) 1415 error = nfs_removerpc(dvp, cnp->cn_nameptr, 1416 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread); 1417 /* 1418 * Kludge City: If the first reply to the remove rpc is lost.. 1419 * the reply to the retransmitted request will be ENOENT 1420 * since the file was in fact removed 1421 * Therefore, we cheat and return success. 1422 */ 1423 if (error == ENOENT) 1424 error = 0; 1425 } else if (!np->n_sillyrename) 1426 error = nfs_sillyrename(dvp, vp, cnp); 1427 np->n_attrstamp = 0; 1428 return (error); 1429} 1430 1431/* 1432 * nfs file remove rpc called from nfs_inactive 1433 */ 1434int 1435nfs_removeit(struct sillyrename *sp) 1436{ 1437 1438 /* 1439 * Make sure that the directory vnode is still valid. 1440 * XXX we should lock sp->s_dvp here. 1441 */ 1442 if (sp->s_dvp->v_type == VBAD) 1443 return (0); 1444 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, 1445 NULL)); 1446} 1447 1448/* 1449 * Nfs remove rpc, called from nfs_remove() and nfs_removeit(). 1450 */ 1451static int 1452nfs_removerpc(struct vnode *dvp, const char *name, int namelen, 1453 struct ucred *cred, struct thread *td) 1454{ 1455 caddr_t bpos, dpos; 1456 int error = 0, wccflag = NFSV3_WCCRATTR; 1457 struct mbuf *mreq, *mrep, *md, *mb; 1458 int v3 = NFS_ISV3(dvp); 1459 1460 nfsstats.rpccnt[NFSPROC_REMOVE]++; 1461 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE, 1462 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen)); 1463 mb = mreq; 1464 bpos = mtod(mb, caddr_t); 1465 nfsm_fhtom(dvp, v3); 1466 nfsm_strtom(name, namelen, NFS_MAXNAMLEN); 1467 nfsm_request(dvp, NFSPROC_REMOVE, td, cred); 1468 if (v3) 1469 nfsm_wcc_data(dvp, wccflag); 1470 m_freem(mrep); 1471nfsmout: 1472 VTONFS(dvp)->n_flag |= NMODIFIED; 1473 if (!wccflag) 1474 VTONFS(dvp)->n_attrstamp = 0; 1475 return (error); 1476} 1477 1478/* 1479 * nfs file rename call 1480 */ 1481static int 1482nfs_rename(struct vop_rename_args *ap) 1483{ 1484 struct vnode *fvp = ap->a_fvp; 1485 struct vnode *tvp = ap->a_tvp; 1486 struct vnode *fdvp = ap->a_fdvp; 1487 struct vnode *tdvp = ap->a_tdvp; 1488 struct componentname *tcnp = ap->a_tcnp; 1489 struct componentname *fcnp = ap->a_fcnp; 1490 int error; 1491 1492#ifndef DIAGNOSTIC 1493 if ((tcnp->cn_flags & HASBUF) == 0 || 1494 (fcnp->cn_flags & HASBUF) == 0) 1495 panic("nfs_rename: no name"); 1496#endif 1497 /* Check for cross-device rename */ 1498 if ((fvp->v_mount != tdvp->v_mount) || 1499 (tvp && (fvp->v_mount != tvp->v_mount))) { 1500 error = EXDEV; 1501 goto out; 1502 } 1503 1504 if (fvp == tvp) { 1505 printf("nfs_rename: fvp == tvp (can't happen)\n"); 1506 error = 0; 1507 goto out; 1508 } 1509 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0) 1510 goto out; 1511 1512 /* 1513 * We have to flush B_DELWRI data prior to renaming 1514 * the file. If we don't, the delayed-write buffers 1515 * can be flushed out later after the file has gone stale 1516 * under NFSV3. NFSV2 does not have this problem because 1517 * ( as far as I can tell ) it flushes dirty buffers more 1518 * often. 1519 * 1520 * Skip the rename operation if the fsync fails, this can happen 1521 * due to the server's volume being full, when we pushed out data 1522 * that was written back to our cache earlier. Not checking for 1523 * this condition can result in potential (silent) data loss. 1524 */ 1525 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread); 1526 VOP_UNLOCK(fvp, 0, fcnp->cn_thread); 1527 if (!error && tvp) 1528 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread); 1529 if (error) 1530 goto out; 1531 1532 /* 1533 * If the tvp exists and is in use, sillyrename it before doing the 1534 * rename of the new file over it. 1535 * XXX Can't sillyrename a directory. 1536 */ 1537 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename && 1538 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { 1539 vput(tvp); 1540 tvp = NULL; 1541 } 1542 1543 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen, 1544 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, 1545 tcnp->cn_thread); 1546 1547 if (fvp->v_type == VDIR) { 1548 if (tvp != NULL && tvp->v_type == VDIR) 1549 cache_purge(tdvp); 1550 cache_purge(fdvp); 1551 } 1552 1553out: 1554 if (tdvp == tvp) 1555 vrele(tdvp); 1556 else 1557 vput(tdvp); 1558 if (tvp) 1559 vput(tvp); 1560 vrele(fdvp); 1561 vrele(fvp); 1562 /* 1563 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. 1564 */ 1565 if (error == ENOENT) 1566 error = 0; 1567 return (error); 1568} 1569 1570/* 1571 * nfs file rename rpc called from nfs_remove() above 1572 */ 1573static int 1574nfs_renameit(struct vnode *sdvp, struct componentname *scnp, 1575 struct sillyrename *sp) 1576{ 1577 1578 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp, 1579 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread)); 1580} 1581 1582/* 1583 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit(). 1584 */ 1585static int 1586nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen, 1587 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred, 1588 struct thread *td) 1589{ 1590 caddr_t bpos, dpos; 1591 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR; 1592 struct mbuf *mreq, *mrep, *md, *mb; 1593 int v3 = NFS_ISV3(fdvp); 1594 1595 nfsstats.rpccnt[NFSPROC_RENAME]++; 1596 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME, 1597 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) + 1598 nfsm_rndup(tnamelen)); 1599 mb = mreq; 1600 bpos = mtod(mb, caddr_t); 1601 nfsm_fhtom(fdvp, v3); 1602 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN); 1603 nfsm_fhtom(tdvp, v3); 1604 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN); 1605 nfsm_request(fdvp, NFSPROC_RENAME, td, cred); 1606 if (v3) { 1607 nfsm_wcc_data(fdvp, fwccflag); 1608 nfsm_wcc_data(tdvp, twccflag); 1609 } 1610 m_freem(mrep); 1611nfsmout: 1612 VTONFS(fdvp)->n_flag |= NMODIFIED; 1613 VTONFS(tdvp)->n_flag |= NMODIFIED; 1614 if (!fwccflag) 1615 VTONFS(fdvp)->n_attrstamp = 0; 1616 if (!twccflag) 1617 VTONFS(tdvp)->n_attrstamp = 0; 1618 return (error); 1619} 1620 1621/* 1622 * nfs hard link create call 1623 */ 1624static int 1625nfs_link(struct vop_link_args *ap) 1626{ 1627 struct vnode *vp = ap->a_vp; 1628 struct vnode *tdvp = ap->a_tdvp; 1629 struct componentname *cnp = ap->a_cnp; 1630 caddr_t bpos, dpos; 1631 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0; 1632 struct mbuf *mreq, *mrep, *md, *mb; 1633 int v3; 1634 1635 if (vp->v_mount != tdvp->v_mount) { 1636 return (EXDEV); 1637 } 1638 1639 /* 1640 * Push all writes to the server, so that the attribute cache 1641 * doesn't get "out of sync" with the server. 1642 * XXX There should be a better way! 1643 */ 1644 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread); 1645 1646 v3 = NFS_ISV3(vp); 1647 nfsstats.rpccnt[NFSPROC_LINK]++; 1648 mreq = nfsm_reqhead(vp, NFSPROC_LINK, 1649 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 1650 mb = mreq; 1651 bpos = mtod(mb, caddr_t); 1652 nfsm_fhtom(vp, v3); 1653 nfsm_fhtom(tdvp, v3); 1654 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1655 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred); 1656 if (v3) { 1657 nfsm_postop_attr(vp, attrflag); 1658 nfsm_wcc_data(tdvp, wccflag); 1659 } 1660 m_freem(mrep); 1661nfsmout: 1662 VTONFS(tdvp)->n_flag |= NMODIFIED; 1663 if (!attrflag) 1664 VTONFS(vp)->n_attrstamp = 0; 1665 if (!wccflag) 1666 VTONFS(tdvp)->n_attrstamp = 0; 1667 /* 1668 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 1669 */ 1670 if (error == EEXIST) 1671 error = 0; 1672 return (error); 1673} 1674 1675/* 1676 * nfs symbolic link create call 1677 */ 1678static int 1679nfs_symlink(struct vop_symlink_args *ap) 1680{ 1681 struct vnode *dvp = ap->a_dvp; 1682 struct vattr *vap = ap->a_vap; 1683 struct componentname *cnp = ap->a_cnp; 1684 struct nfsv2_sattr *sp; 1685 caddr_t bpos, dpos; 1686 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp; 1687 struct mbuf *mreq, *mrep, *md, *mb; 1688 struct vnode *newvp = NULL; 1689 int v3 = NFS_ISV3(dvp); 1690 1691 nfsstats.rpccnt[NFSPROC_SYMLINK]++; 1692 slen = strlen(ap->a_target); 1693 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED + 1694 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3)); 1695 mb = mreq; 1696 bpos = mtod(mb, caddr_t); 1697 nfsm_fhtom(dvp, v3); 1698 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1699 if (v3) { 1700 nfsm_v3attrbuild(vap, FALSE); 1701 } 1702 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN); 1703 if (!v3) { 1704 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR); 1705 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode); 1706 sp->sa_uid = nfs_xdrneg1; 1707 sp->sa_gid = nfs_xdrneg1; 1708 sp->sa_size = nfs_xdrneg1; 1709 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1710 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1711 } 1712 1713 /* 1714 * Issue the NFS request and get the rpc response. 1715 * 1716 * Only NFSv3 responses returning an error of 0 actually return 1717 * a file handle that can be converted into newvp without having 1718 * to do an extra lookup rpc. 1719 */ 1720 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred); 1721 if (v3) { 1722 if (error == 0) 1723 nfsm_mtofh(dvp, newvp, v3, gotvp); 1724 nfsm_wcc_data(dvp, wccflag); 1725 } 1726 1727 /* 1728 * out code jumps -> here, mrep is also freed. 1729 */ 1730 1731 m_freem(mrep); 1732nfsmout: 1733 1734 /* 1735 * If we get an EEXIST error, silently convert it to no-error 1736 * in case of an NFS retry. 1737 */ 1738 if (error == EEXIST) 1739 error = 0; 1740 1741 /* 1742 * If we do not have (or no longer have) an error, and we could 1743 * not extract the newvp from the response due to the request being 1744 * NFSv2 or the error being EEXIST. We have to do a lookup in order 1745 * to obtain a newvp to return. 1746 */ 1747 if (error == 0 && newvp == NULL) { 1748 struct nfsnode *np = NULL; 1749 1750 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 1751 cnp->cn_cred, cnp->cn_thread, &np); 1752 if (!error) 1753 newvp = NFSTOV(np); 1754 } 1755 if (error) { 1756 if (newvp) 1757 vput(newvp); 1758 } else { 1759 *ap->a_vpp = newvp; 1760 } 1761 VTONFS(dvp)->n_flag |= NMODIFIED; 1762 if (!wccflag) 1763 VTONFS(dvp)->n_attrstamp = 0; 1764 return (error); 1765} 1766 1767/* 1768 * nfs make dir call 1769 */ 1770static int 1771nfs_mkdir(struct vop_mkdir_args *ap) 1772{ 1773 struct vnode *dvp = ap->a_dvp; 1774 struct vattr *vap = ap->a_vap; 1775 struct componentname *cnp = ap->a_cnp; 1776 struct nfsv2_sattr *sp; 1777 int len; 1778 struct nfsnode *np = NULL; 1779 struct vnode *newvp = NULL; 1780 caddr_t bpos, dpos; 1781 int error = 0, wccflag = NFSV3_WCCRATTR; 1782 int gotvp = 0; 1783 struct mbuf *mreq, *mrep, *md, *mb; 1784 struct vattr vattr; 1785 int v3 = NFS_ISV3(dvp); 1786 1787 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) { 1788 return (error); 1789 } 1790 len = cnp->cn_namelen; 1791 nfsstats.rpccnt[NFSPROC_MKDIR]++; 1792 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR, 1793 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3)); 1794 mb = mreq; 1795 bpos = mtod(mb, caddr_t); 1796 nfsm_fhtom(dvp, v3); 1797 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 1798 if (v3) { 1799 nfsm_v3attrbuild(vap, FALSE); 1800 } else { 1801 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR); 1802 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode); 1803 sp->sa_uid = nfs_xdrneg1; 1804 sp->sa_gid = nfs_xdrneg1; 1805 sp->sa_size = nfs_xdrneg1; 1806 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1807 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1808 } 1809 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred); 1810 if (!error) 1811 nfsm_mtofh(dvp, newvp, v3, gotvp); 1812 if (v3) 1813 nfsm_wcc_data(dvp, wccflag); 1814 m_freem(mrep); 1815nfsmout: 1816 VTONFS(dvp)->n_flag |= NMODIFIED; 1817 if (!wccflag) 1818 VTONFS(dvp)->n_attrstamp = 0; 1819 /* 1820 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry 1821 * if we can succeed in looking up the directory. 1822 */ 1823 if (error == EEXIST || (!error && !gotvp)) { 1824 if (newvp) { 1825 vput(newvp); 1826 newvp = NULL; 1827 } 1828 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred, 1829 cnp->cn_thread, &np); 1830 if (!error) { 1831 newvp = NFSTOV(np); 1832 if (newvp->v_type != VDIR) 1833 error = EEXIST; 1834 } 1835 } 1836 if (error) { 1837 if (newvp) 1838 vput(newvp); 1839 } else 1840 *ap->a_vpp = newvp; 1841 return (error); 1842} 1843 1844/* 1845 * nfs remove directory call 1846 */ 1847static int 1848nfs_rmdir(struct vop_rmdir_args *ap) 1849{ 1850 struct vnode *vp = ap->a_vp; 1851 struct vnode *dvp = ap->a_dvp; 1852 struct componentname *cnp = ap->a_cnp; 1853 caddr_t bpos, dpos; 1854 int error = 0, wccflag = NFSV3_WCCRATTR; 1855 struct mbuf *mreq, *mrep, *md, *mb; 1856 int v3 = NFS_ISV3(dvp); 1857 1858 if (dvp == vp) 1859 return (EINVAL); 1860 nfsstats.rpccnt[NFSPROC_RMDIR]++; 1861 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR, 1862 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 1863 mb = mreq; 1864 bpos = mtod(mb, caddr_t); 1865 nfsm_fhtom(dvp, v3); 1866 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1867 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred); 1868 if (v3) 1869 nfsm_wcc_data(dvp, wccflag); 1870 m_freem(mrep); 1871nfsmout: 1872 VTONFS(dvp)->n_flag |= NMODIFIED; 1873 if (!wccflag) 1874 VTONFS(dvp)->n_attrstamp = 0; 1875 cache_purge(dvp); 1876 cache_purge(vp); 1877 /* 1878 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 1879 */ 1880 if (error == ENOENT) 1881 error = 0; 1882 return (error); 1883} 1884 1885/* 1886 * nfs readdir call 1887 */ 1888static int 1889nfs_readdir(struct vop_readdir_args *ap) 1890{ 1891 struct vnode *vp = ap->a_vp; 1892 struct nfsnode *np = VTONFS(vp); 1893 struct uio *uio = ap->a_uio; 1894 int tresid, error; 1895 struct vattr vattr; 1896 1897 if (vp->v_type != VDIR) 1898 return (EPERM); 1899 /* 1900 * First, check for hit on the EOF offset cache 1901 */ 1902 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset && 1903 (np->n_flag & NMODIFIED) == 0) { 1904 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 && 1905 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) { 1906 nfsstats.direofcache_hits++; 1907 return (0); 1908 } 1909 } 1910 1911 /* 1912 * Call nfs_bioread() to do the real work. 1913 */ 1914 tresid = uio->uio_resid; 1915 error = nfs_bioread(vp, uio, 0, ap->a_cred); 1916 1917 if (!error && uio->uio_resid == tresid) 1918 nfsstats.direofcache_misses++; 1919 return (error); 1920} 1921 1922/* 1923 * Readdir rpc call. 1924 * Called from below the buffer cache by nfs_doio(). 1925 */ 1926int 1927nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 1928{ 1929 int len, left; 1930 struct dirent *dp = NULL; 1931 u_int32_t *tl; 1932 caddr_t cp; 1933 nfsuint64 *cookiep; 1934 caddr_t bpos, dpos; 1935 struct mbuf *mreq, *mrep, *md, *mb; 1936 nfsuint64 cookie; 1937 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1938 struct nfsnode *dnp = VTONFS(vp); 1939 u_quad_t fileno; 1940 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 1941 int attrflag; 1942 int v3 = NFS_ISV3(vp); 1943 1944#ifndef DIAGNOSTIC 1945 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) || 1946 (uiop->uio_resid & (DIRBLKSIZ - 1))) 1947 panic("nfs readdirrpc bad uio"); 1948#endif 1949 1950 /* 1951 * If there is no cookie, assume directory was stale. 1952 */ 1953 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 1954 if (cookiep) 1955 cookie = *cookiep; 1956 else 1957 return (NFSERR_BAD_COOKIE); 1958 /* 1959 * Loop around doing readdir rpc's of size nm_readdirsize 1960 * truncated to a multiple of DIRBLKSIZ. 1961 * The stopping criteria is EOF or buffer full. 1962 */ 1963 while (more_dirs && bigenough) { 1964 nfsstats.rpccnt[NFSPROC_READDIR]++; 1965 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) + 1966 NFSX_READDIR(v3)); 1967 mb = mreq; 1968 bpos = mtod(mb, caddr_t); 1969 nfsm_fhtom(vp, v3); 1970 if (v3) { 1971 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED); 1972 *tl++ = cookie.nfsuquad[0]; 1973 *tl++ = cookie.nfsuquad[1]; 1974 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 1975 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 1976 } else { 1977 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED); 1978 *tl++ = cookie.nfsuquad[0]; 1979 } 1980 *tl = txdr_unsigned(nmp->nm_readdirsize); 1981 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred); 1982 if (v3) { 1983 nfsm_postop_attr(vp, attrflag); 1984 if (!error) { 1985 tl = nfsm_dissect(u_int32_t *, 1986 2 * NFSX_UNSIGNED); 1987 dnp->n_cookieverf.nfsuquad[0] = *tl++; 1988 dnp->n_cookieverf.nfsuquad[1] = *tl; 1989 } else { 1990 m_freem(mrep); 1991 goto nfsmout; 1992 } 1993 } 1994 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 1995 more_dirs = fxdr_unsigned(int, *tl); 1996 1997 /* loop thru the dir entries, doctoring them to 4bsd form */ 1998 while (more_dirs && bigenough) { 1999 if (v3) { 2000 tl = nfsm_dissect(u_int32_t *, 2001 3 * NFSX_UNSIGNED); 2002 fileno = fxdr_hyper(tl); 2003 len = fxdr_unsigned(int, *(tl + 2)); 2004 } else { 2005 tl = nfsm_dissect(u_int32_t *, 2006 2 * NFSX_UNSIGNED); 2007 fileno = fxdr_unsigned(u_quad_t, *tl++); 2008 len = fxdr_unsigned(int, *tl); 2009 } 2010 if (len <= 0 || len > NFS_MAXNAMLEN) { 2011 error = EBADRPC; 2012 m_freem(mrep); 2013 goto nfsmout; 2014 } 2015 tlen = nfsm_rndup(len); 2016 if (tlen == len) 2017 tlen += 4; /* To ensure null termination */ 2018 left = DIRBLKSIZ - blksiz; 2019 if ((tlen + DIRHDSIZ) > left) { 2020 dp->d_reclen += left; 2021 uiop->uio_iov->iov_base = 2022 (char *)uiop->uio_iov->iov_base + left; 2023 uiop->uio_iov->iov_len -= left; 2024 uiop->uio_offset += left; 2025 uiop->uio_resid -= left; 2026 blksiz = 0; 2027 } 2028 if ((tlen + DIRHDSIZ) > uiop->uio_resid) 2029 bigenough = 0; 2030 if (bigenough) { 2031 dp = (struct dirent *)uiop->uio_iov->iov_base; 2032 dp->d_fileno = (int)fileno; 2033 dp->d_namlen = len; 2034 dp->d_reclen = tlen + DIRHDSIZ; 2035 dp->d_type = DT_UNKNOWN; 2036 blksiz += dp->d_reclen; 2037 if (blksiz == DIRBLKSIZ) 2038 blksiz = 0; 2039 uiop->uio_offset += DIRHDSIZ; 2040 uiop->uio_resid -= DIRHDSIZ; 2041 uiop->uio_iov->iov_base = 2042 (char *)uiop->uio_iov->iov_base + DIRHDSIZ; 2043 uiop->uio_iov->iov_len -= DIRHDSIZ; 2044 nfsm_mtouio(uiop, len); 2045 cp = uiop->uio_iov->iov_base; 2046 tlen -= len; 2047 *cp = '\0'; /* null terminate */ 2048 uiop->uio_iov->iov_base = 2049 (char *)uiop->uio_iov->iov_base + tlen; 2050 uiop->uio_iov->iov_len -= tlen; 2051 uiop->uio_offset += tlen; 2052 uiop->uio_resid -= tlen; 2053 } else 2054 nfsm_adv(nfsm_rndup(len)); 2055 if (v3) { 2056 tl = nfsm_dissect(u_int32_t *, 2057 3 * NFSX_UNSIGNED); 2058 } else { 2059 tl = nfsm_dissect(u_int32_t *, 2060 2 * NFSX_UNSIGNED); 2061 } 2062 if (bigenough) { 2063 cookie.nfsuquad[0] = *tl++; 2064 if (v3) 2065 cookie.nfsuquad[1] = *tl++; 2066 } else if (v3) 2067 tl += 2; 2068 else 2069 tl++; 2070 more_dirs = fxdr_unsigned(int, *tl); 2071 } 2072 /* 2073 * If at end of rpc data, get the eof boolean 2074 */ 2075 if (!more_dirs) { 2076 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2077 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2078 } 2079 m_freem(mrep); 2080 } 2081 /* 2082 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2083 * by increasing d_reclen for the last record. 2084 */ 2085 if (blksiz > 0) { 2086 left = DIRBLKSIZ - blksiz; 2087 dp->d_reclen += left; 2088 uiop->uio_iov->iov_base = 2089 (char *)uiop->uio_iov->iov_base + left; 2090 uiop->uio_iov->iov_len -= left; 2091 uiop->uio_offset += left; 2092 uiop->uio_resid -= left; 2093 } 2094 2095 /* 2096 * We are now either at the end of the directory or have filled the 2097 * block. 2098 */ 2099 if (bigenough) 2100 dnp->n_direofoffset = uiop->uio_offset; 2101 else { 2102 if (uiop->uio_resid > 0) 2103 printf("EEK! readdirrpc resid > 0\n"); 2104 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2105 *cookiep = cookie; 2106 } 2107nfsmout: 2108 return (error); 2109} 2110 2111/* 2112 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc(). 2113 */ 2114int 2115nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred) 2116{ 2117 int len, left; 2118 struct dirent *dp; 2119 u_int32_t *tl; 2120 caddr_t cp; 2121 struct vnode *newvp; 2122 nfsuint64 *cookiep; 2123 caddr_t bpos, dpos, dpossav1, dpossav2; 2124 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2; 2125 struct nameidata nami, *ndp = &nami; 2126 struct componentname *cnp = &ndp->ni_cnd; 2127 nfsuint64 cookie; 2128 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2129 struct nfsnode *dnp = VTONFS(vp), *np; 2130 nfsfh_t *fhp; 2131 u_quad_t fileno; 2132 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i; 2133 int attrflag, fhsize; 2134 2135#ifndef nolint 2136 dp = NULL; 2137#endif 2138#ifndef DIAGNOSTIC 2139 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) || 2140 (uiop->uio_resid & (DIRBLKSIZ - 1))) 2141 panic("nfs readdirplusrpc bad uio"); 2142#endif 2143 ndp->ni_dvp = vp; 2144 newvp = NULLVP; 2145 2146 /* 2147 * If there is no cookie, assume directory was stale. 2148 */ 2149 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 2150 if (cookiep) 2151 cookie = *cookiep; 2152 else 2153 return (NFSERR_BAD_COOKIE); 2154 /* 2155 * Loop around doing readdir rpc's of size nm_readdirsize 2156 * truncated to a multiple of DIRBLKSIZ. 2157 * The stopping criteria is EOF or buffer full. 2158 */ 2159 while (more_dirs && bigenough) { 2160 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++; 2161 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS, 2162 NFSX_FH(1) + 6 * NFSX_UNSIGNED); 2163 mb = mreq; 2164 bpos = mtod(mb, caddr_t); 2165 nfsm_fhtom(vp, 1); 2166 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED); 2167 *tl++ = cookie.nfsuquad[0]; 2168 *tl++ = cookie.nfsuquad[1]; 2169 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2170 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2171 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 2172 *tl = txdr_unsigned(nmp->nm_rsize); 2173 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred); 2174 nfsm_postop_attr(vp, attrflag); 2175 if (error) { 2176 m_freem(mrep); 2177 goto nfsmout; 2178 } 2179 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED); 2180 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2181 dnp->n_cookieverf.nfsuquad[1] = *tl++; 2182 more_dirs = fxdr_unsigned(int, *tl); 2183 2184 /* loop thru the dir entries, doctoring them to 4bsd form */ 2185 while (more_dirs && bigenough) { 2186 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED); 2187 fileno = fxdr_hyper(tl); 2188 len = fxdr_unsigned(int, *(tl + 2)); 2189 if (len <= 0 || len > NFS_MAXNAMLEN) { 2190 error = EBADRPC; 2191 m_freem(mrep); 2192 goto nfsmout; 2193 } 2194 tlen = nfsm_rndup(len); 2195 if (tlen == len) 2196 tlen += 4; /* To ensure null termination*/ 2197 left = DIRBLKSIZ - blksiz; 2198 if ((tlen + DIRHDSIZ) > left) { 2199 dp->d_reclen += left; 2200 uiop->uio_iov->iov_base = 2201 (char *)uiop->uio_iov->iov_base + left; 2202 uiop->uio_iov->iov_len -= left; 2203 uiop->uio_offset += left; 2204 uiop->uio_resid -= left; 2205 blksiz = 0; 2206 } 2207 if ((tlen + DIRHDSIZ) > uiop->uio_resid) 2208 bigenough = 0; 2209 if (bigenough) { 2210 dp = (struct dirent *)uiop->uio_iov->iov_base; 2211 dp->d_fileno = (int)fileno; 2212 dp->d_namlen = len; 2213 dp->d_reclen = tlen + DIRHDSIZ; 2214 dp->d_type = DT_UNKNOWN; 2215 blksiz += dp->d_reclen; 2216 if (blksiz == DIRBLKSIZ) 2217 blksiz = 0; 2218 uiop->uio_offset += DIRHDSIZ; 2219 uiop->uio_resid -= DIRHDSIZ; 2220 uiop->uio_iov->iov_base = 2221 (char *)uiop->uio_iov->iov_base + DIRHDSIZ; 2222 uiop->uio_iov->iov_len -= DIRHDSIZ; 2223 cnp->cn_nameptr = uiop->uio_iov->iov_base; 2224 cnp->cn_namelen = len; 2225 nfsm_mtouio(uiop, len); 2226 cp = uiop->uio_iov->iov_base; 2227 tlen -= len; 2228 *cp = '\0'; 2229 uiop->uio_iov->iov_base = 2230 (char *)uiop->uio_iov->iov_base + tlen; 2231 uiop->uio_iov->iov_len -= tlen; 2232 uiop->uio_offset += tlen; 2233 uiop->uio_resid -= tlen; 2234 } else 2235 nfsm_adv(nfsm_rndup(len)); 2236 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED); 2237 if (bigenough) { 2238 cookie.nfsuquad[0] = *tl++; 2239 cookie.nfsuquad[1] = *tl++; 2240 } else 2241 tl += 2; 2242 2243 /* 2244 * Since the attributes are before the file handle 2245 * (sigh), we must skip over the attributes and then 2246 * come back and get them. 2247 */ 2248 attrflag = fxdr_unsigned(int, *tl); 2249 if (attrflag) { 2250 dpossav1 = dpos; 2251 mdsav1 = md; 2252 nfsm_adv(NFSX_V3FATTR); 2253 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2254 doit = fxdr_unsigned(int, *tl); 2255 /* 2256 * Skip loading the attrs for "..". There's a 2257 * race between loading the attrs here and 2258 * lookups that look for the directory currently 2259 * being read (in the parent). We try to acquire 2260 * the exclusive lock on ".." here, owning the 2261 * lock on the directory being read. Lookup will 2262 * hold the lock on ".." and try to acquire the 2263 * lock on the directory being read. 2264 * 2265 * There are other ways of fixing this, one would 2266 * be to do a trylock on the ".." vnode and skip 2267 * loading the attrs on ".." if it happens to be 2268 * locked by another process. But skipping the 2269 * attrload on ".." seems the easiest option. 2270 */ 2271 if (strcmp(dp->d_name, "..") == 0) { 2272 doit = 0; 2273 /* 2274 * We've already skipped over the attrs, 2275 * skip over the filehandle. And store d_type 2276 * as VDIR. 2277 */ 2278 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2279 i = fxdr_unsigned(int, *tl); 2280 nfsm_adv(nfsm_rndup(i)); 2281 dp->d_type = IFTODT(VTTOIF(VDIR)); 2282 } 2283 if (doit) { 2284 nfsm_getfh(fhp, fhsize, 1); 2285 if (NFS_CMPFH(dnp, fhp, fhsize)) { 2286 VREF(vp); 2287 newvp = vp; 2288 np = dnp; 2289 } else { 2290 error = nfs_nget(vp->v_mount, fhp, 2291 fhsize, &np); 2292 if (error) 2293 doit = 0; 2294 else 2295 newvp = NFSTOV(np); 2296 } 2297 } 2298 if (doit && bigenough) { 2299 dpossav2 = dpos; 2300 dpos = dpossav1; 2301 mdsav2 = md; 2302 md = mdsav1; 2303 nfsm_loadattr(newvp, NULL); 2304 dpos = dpossav2; 2305 md = mdsav2; 2306 dp->d_type = 2307 IFTODT(VTTOIF(np->n_vattr.va_type)); 2308 ndp->ni_vp = newvp; 2309 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp); 2310 } 2311 } else { 2312 /* Just skip over the file handle */ 2313 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2314 i = fxdr_unsigned(int, *tl); 2315 nfsm_adv(nfsm_rndup(i)); 2316 } 2317 if (newvp != NULLVP) { 2318 if (newvp == vp) 2319 vrele(newvp); 2320 else 2321 vput(newvp); 2322 newvp = NULLVP; 2323 } 2324 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2325 more_dirs = fxdr_unsigned(int, *tl); 2326 } 2327 /* 2328 * If at end of rpc data, get the eof boolean 2329 */ 2330 if (!more_dirs) { 2331 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED); 2332 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2333 } 2334 m_freem(mrep); 2335 } 2336 /* 2337 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2338 * by increasing d_reclen for the last record. 2339 */ 2340 if (blksiz > 0) { 2341 left = DIRBLKSIZ - blksiz; 2342 dp->d_reclen += left; 2343 uiop->uio_iov->iov_base = 2344 (char *)uiop->uio_iov->iov_base + left; 2345 uiop->uio_iov->iov_len -= left; 2346 uiop->uio_offset += left; 2347 uiop->uio_resid -= left; 2348 } 2349 2350 /* 2351 * We are now either at the end of the directory or have filled the 2352 * block. 2353 */ 2354 if (bigenough) 2355 dnp->n_direofoffset = uiop->uio_offset; 2356 else { 2357 if (uiop->uio_resid > 0) 2358 printf("EEK! readdirplusrpc resid > 0\n"); 2359 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2360 *cookiep = cookie; 2361 } 2362nfsmout: 2363 if (newvp != NULLVP) { 2364 if (newvp == vp) 2365 vrele(newvp); 2366 else 2367 vput(newvp); 2368 newvp = NULLVP; 2369 } 2370 return (error); 2371} 2372 2373/* 2374 * Silly rename. To make the NFS filesystem that is stateless look a little 2375 * more like the "ufs" a remove of an active vnode is translated to a rename 2376 * to a funny looking filename that is removed by nfs_inactive on the 2377 * nfsnode. There is the potential for another process on a different client 2378 * to create the same funny name between the nfs_lookitup() fails and the 2379 * nfs_rename() completes, but... 2380 */ 2381static int 2382nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 2383{ 2384 struct sillyrename *sp; 2385 struct nfsnode *np; 2386 int error; 2387 short pid; 2388 unsigned int lticks; 2389 2390 cache_purge(dvp); 2391 np = VTONFS(vp); 2392#ifndef DIAGNOSTIC 2393 if (vp->v_type == VDIR) 2394 panic("nfs: sillyrename dir"); 2395#endif 2396 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), 2397 M_NFSREQ, M_WAITOK); 2398 sp->s_cred = crhold(cnp->cn_cred); 2399 sp->s_dvp = dvp; 2400 sp->s_removeit = nfs_removeit; 2401 VREF(dvp); 2402 2403 /* 2404 * Fudge together a funny name. 2405 * Changing the format of the funny name to accomodate more 2406 * sillynames per directory. 2407 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is 2408 * CPU ticks since boot. 2409 */ 2410 pid = cnp->cn_thread->td_proc->p_pid; 2411 lticks = (unsigned int)ticks; 2412 for ( ; ; ) { 2413 sp->s_namlen = sprintf(sp->s_name, 2414 ".nfs.%08x.%04x4.4", lticks, 2415 pid); 2416 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2417 cnp->cn_thread, NULL)) 2418 break; 2419 lticks++; 2420 } 2421 error = nfs_renameit(dvp, cnp, sp); 2422 if (error) 2423 goto bad; 2424 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2425 cnp->cn_thread, &np); 2426 np->n_sillyrename = sp; 2427 return (0); 2428bad: 2429 vrele(sp->s_dvp); 2430 crfree(sp->s_cred); 2431 free((caddr_t)sp, M_NFSREQ); 2432 return (error); 2433} 2434 2435/* 2436 * Look up a file name and optionally either update the file handle or 2437 * allocate an nfsnode, depending on the value of npp. 2438 * npp == NULL --> just do the lookup 2439 * *npp == NULL --> allocate a new nfsnode and make sure attributes are 2440 * handled too 2441 * *npp != NULL --> update the file handle in the vnode 2442 */ 2443static int 2444nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred, 2445 struct thread *td, struct nfsnode **npp) 2446{ 2447 struct vnode *newvp = NULL; 2448 struct nfsnode *np, *dnp = VTONFS(dvp); 2449 caddr_t bpos, dpos; 2450 int error = 0, fhlen, attrflag; 2451 struct mbuf *mreq, *mrep, *md, *mb; 2452 nfsfh_t *nfhp; 2453 int v3 = NFS_ISV3(dvp); 2454 2455 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 2456 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP, 2457 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 2458 mb = mreq; 2459 bpos = mtod(mb, caddr_t); 2460 nfsm_fhtom(dvp, v3); 2461 nfsm_strtom(name, len, NFS_MAXNAMLEN); 2462 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred); 2463 if (npp && !error) { 2464 nfsm_getfh(nfhp, fhlen, v3); 2465 if (*npp) { 2466 np = *npp; 2467 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) { 2468 free((caddr_t)np->n_fhp, M_NFSBIGFH); 2469 np->n_fhp = &np->n_fh; 2470 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH) 2471 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK); 2472 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen); 2473 np->n_fhsize = fhlen; 2474 newvp = NFSTOV(np); 2475 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) { 2476 VREF(dvp); 2477 newvp = dvp; 2478 } else { 2479 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np); 2480 if (error) { 2481 m_freem(mrep); 2482 return (error); 2483 } 2484 newvp = NFSTOV(np); 2485 } 2486 if (v3) { 2487 nfsm_postop_attr(newvp, attrflag); 2488 if (!attrflag && *npp == NULL) { 2489 m_freem(mrep); 2490 if (newvp == dvp) 2491 vrele(newvp); 2492 else 2493 vput(newvp); 2494 return (ENOENT); 2495 } 2496 } else 2497 nfsm_loadattr(newvp, NULL); 2498 } 2499 m_freem(mrep); 2500nfsmout: 2501 if (npp && *npp == NULL) { 2502 if (error) { 2503 if (newvp) { 2504 if (newvp == dvp) 2505 vrele(newvp); 2506 else 2507 vput(newvp); 2508 } 2509 } else 2510 *npp = np; 2511 } 2512 return (error); 2513} 2514 2515/* 2516 * Nfs Version 3 commit rpc 2517 */ 2518int 2519nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred, 2520 struct thread *td) 2521{ 2522 u_int32_t *tl; 2523 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2524 caddr_t bpos, dpos; 2525 int error = 0, wccflag = NFSV3_WCCRATTR; 2526 struct mbuf *mreq, *mrep, *md, *mb; 2527 2528 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) 2529 return (0); 2530 nfsstats.rpccnt[NFSPROC_COMMIT]++; 2531 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1)); 2532 mb = mreq; 2533 bpos = mtod(mb, caddr_t); 2534 nfsm_fhtom(vp, 1); 2535 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED); 2536 txdr_hyper(offset, tl); 2537 tl += 2; 2538 *tl = txdr_unsigned(cnt); 2539 nfsm_request(vp, NFSPROC_COMMIT, td, cred); 2540 nfsm_wcc_data(vp, wccflag); 2541 if (!error) { 2542 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF); 2543 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl, 2544 NFSX_V3WRITEVERF)) { 2545 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 2546 NFSX_V3WRITEVERF); 2547 error = NFSERR_STALEWRITEVERF; 2548 } 2549 } 2550 m_freem(mrep); 2551nfsmout: 2552 return (error); 2553} 2554 2555/* 2556 * Strategy routine. 2557 * For async requests when nfsiod(s) are running, queue the request by 2558 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the 2559 * request. 2560 */ 2561static int 2562nfs_strategy(struct vop_strategy_args *ap) 2563{ 2564 struct buf *bp = ap->a_bp; 2565 struct ucred *cr; 2566 struct thread *td; 2567 2568 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp)); 2569 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp)); 2570 2571 if (bp->b_flags & B_ASYNC) 2572 td = NULL; 2573 else 2574 td = curthread; /* XXX */ 2575 2576 if (bp->b_iocmd == BIO_READ) 2577 cr = bp->b_rcred; 2578 else 2579 cr = bp->b_wcred; 2580 2581 /* 2582 * If the op is asynchronous and an i/o daemon is waiting 2583 * queue the request, wake it up and wait for completion 2584 * otherwise just do it ourselves. 2585 */ 2586 if ((bp->b_flags & B_ASYNC) == 0 || 2587 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, td)) 2588 (void)nfs_doio(ap->a_vp, bp, cr, td); 2589 return (0); 2590} 2591 2592/* 2593 * fsync vnode op. Just call nfs_flush() with commit == 1. 2594 */ 2595/* ARGSUSED */ 2596static int 2597nfs_fsync(struct vop_fsync_args *ap) 2598{ 2599 2600 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1)); 2601} 2602 2603/* 2604 * Flush all the blocks associated with a vnode. 2605 * Walk through the buffer pool and push any dirty pages 2606 * associated with the vnode. 2607 */ 2608static int 2609nfs_flush(struct vnode *vp, int waitfor, struct thread *td, 2610 int commit) 2611{ 2612 struct nfsnode *np = VTONFS(vp); 2613 struct buf *bp; 2614 int i; 2615 struct buf *nbp; 2616 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2617 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos; 2618 int passone = 1; 2619 u_quad_t off, endoff, toff; 2620 struct ucred* wcred = NULL; 2621 struct buf **bvec = NULL; 2622#ifndef NFS_COMMITBVECSIZ 2623#define NFS_COMMITBVECSIZ 20 2624#endif 2625 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ]; 2626 int bvecsize = 0, bveccount; 2627 2628 if (nmp->nm_flag & NFSMNT_INT) 2629 slpflag = PCATCH; 2630 if (!commit) 2631 passone = 0; 2632 /* 2633 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the 2634 * server, but has not been committed to stable storage on the server 2635 * yet. On the first pass, the byte range is worked out and the commit 2636 * rpc is done. On the second pass, nfs_writebp() is called to do the 2637 * job. 2638 */ 2639again: 2640 off = (u_quad_t)-1; 2641 endoff = 0; 2642 bvecpos = 0; 2643 if (NFS_ISV3(vp) && commit) { 2644 s = splbio(); 2645 if (bvec != NULL && bvec != bvec_on_stack) 2646 free(bvec, M_TEMP); 2647 /* 2648 * Count up how many buffers waiting for a commit. 2649 */ 2650 bveccount = 0; 2651 VI_LOCK(vp); 2652 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) { 2653 if (BUF_REFCNT(bp) == 0 && 2654 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2655 == (B_DELWRI | B_NEEDCOMMIT)) 2656 bveccount++; 2657 } 2658 /* 2659 * Allocate space to remember the list of bufs to commit. It is 2660 * important to use M_NOWAIT here to avoid a race with nfs_write. 2661 * If we can't get memory (for whatever reason), we will end up 2662 * committing the buffers one-by-one in the loop below. 2663 */ 2664 if (bveccount > NFS_COMMITBVECSIZ) { 2665 /* 2666 * Release the vnode interlock to avoid a lock 2667 * order reversal. 2668 */ 2669 VI_UNLOCK(vp); 2670 bvec = (struct buf **) 2671 malloc(bveccount * sizeof(struct buf *), 2672 M_TEMP, M_NOWAIT); 2673 VI_LOCK(vp); 2674 if (bvec == NULL) { 2675 bvec = bvec_on_stack; 2676 bvecsize = NFS_COMMITBVECSIZ; 2677 } else 2678 bvecsize = bveccount; 2679 } else { 2680 bvec = bvec_on_stack; 2681 bvecsize = NFS_COMMITBVECSIZ; 2682 } 2683 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) { 2684 if (bvecpos >= bvecsize) 2685 break; 2686 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2687 nbp = TAILQ_NEXT(bp, b_bobufs); 2688 continue; 2689 } 2690 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) != 2691 (B_DELWRI | B_NEEDCOMMIT)) { 2692 BUF_UNLOCK(bp); 2693 nbp = TAILQ_NEXT(bp, b_bobufs); 2694 continue; 2695 } 2696 VI_UNLOCK(vp); 2697 bremfree(bp); 2698 /* 2699 * Work out if all buffers are using the same cred 2700 * so we can deal with them all with one commit. 2701 * 2702 * NOTE: we are not clearing B_DONE here, so we have 2703 * to do it later on in this routine if we intend to 2704 * initiate I/O on the bp. 2705 * 2706 * Note: to avoid loopback deadlocks, we do not 2707 * assign b_runningbufspace. 2708 */ 2709 if (wcred == NULL) 2710 wcred = bp->b_wcred; 2711 else if (wcred != bp->b_wcred) 2712 wcred = NOCRED; 2713 vfs_busy_pages(bp, 1); 2714 2715 VI_LOCK(vp); 2716 /* 2717 * bp is protected by being locked, but nbp is not 2718 * and vfs_busy_pages() may sleep. We have to 2719 * recalculate nbp. 2720 */ 2721 nbp = TAILQ_NEXT(bp, b_bobufs); 2722 2723 /* 2724 * A list of these buffers is kept so that the 2725 * second loop knows which buffers have actually 2726 * been committed. This is necessary, since there 2727 * may be a race between the commit rpc and new 2728 * uncommitted writes on the file. 2729 */ 2730 bvec[bvecpos++] = bp; 2731 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + 2732 bp->b_dirtyoff; 2733 if (toff < off) 2734 off = toff; 2735 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff); 2736 if (toff > endoff) 2737 endoff = toff; 2738 } 2739 splx(s); 2740 VI_UNLOCK(vp); 2741 } 2742 if (bvecpos > 0) { 2743 /* 2744 * Commit data on the server, as required. 2745 * If all bufs are using the same wcred, then use that with 2746 * one call for all of them, otherwise commit each one 2747 * separately. 2748 */ 2749 if (wcred != NOCRED) 2750 retv = nfs_commit(vp, off, (int)(endoff - off), 2751 wcred, td); 2752 else { 2753 retv = 0; 2754 for (i = 0; i < bvecpos; i++) { 2755 off_t off, size; 2756 bp = bvec[i]; 2757 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + 2758 bp->b_dirtyoff; 2759 size = (u_quad_t)(bp->b_dirtyend 2760 - bp->b_dirtyoff); 2761 retv = nfs_commit(vp, off, (int)size, 2762 bp->b_wcred, td); 2763 if (retv) break; 2764 } 2765 } 2766 2767 if (retv == NFSERR_STALEWRITEVERF) 2768 nfs_clearcommit(vp->v_mount); 2769 2770 /* 2771 * Now, either mark the blocks I/O done or mark the 2772 * blocks dirty, depending on whether the commit 2773 * succeeded. 2774 */ 2775 for (i = 0; i < bvecpos; i++) { 2776 bp = bvec[i]; 2777 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 2778 if (retv) { 2779 /* 2780 * Error, leave B_DELWRI intact 2781 */ 2782 vfs_unbusy_pages(bp); 2783 brelse(bp); 2784 } else { 2785 /* 2786 * Success, remove B_DELWRI ( bundirty() ). 2787 * 2788 * b_dirtyoff/b_dirtyend seem to be NFS 2789 * specific. We should probably move that 2790 * into bundirty(). XXX 2791 */ 2792 s = splbio(); 2793 bufobj_wref(&vp->v_bufobj); 2794 bp->b_flags |= B_ASYNC; 2795 bundirty(bp); 2796 bp->b_flags &= ~B_DONE; 2797 bp->b_ioflags &= ~BIO_ERROR; 2798 bp->b_dirtyoff = bp->b_dirtyend = 0; 2799 splx(s); 2800 bufdone(bp); 2801 } 2802 } 2803 } 2804 2805 /* 2806 * Start/do any write(s) that are required. 2807 */ 2808loop: 2809 s = splbio(); 2810 VI_LOCK(vp); 2811 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) { 2812 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2813 if (waitfor != MNT_WAIT || passone) 2814 continue; 2815 2816 error = BUF_TIMELOCK(bp, 2817 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 2818 VI_MTX(vp), "nfsfsync", slpflag, slptimeo); 2819 splx(s); 2820 if (error == 0) 2821 panic("nfs_fsync: inconsistent lock"); 2822 if (error == ENOLCK) 2823 goto loop; 2824 if (nfs_sigintr(nmp, NULL, td)) { 2825 error = EINTR; 2826 goto done; 2827 } 2828 if (slpflag == PCATCH) { 2829 slpflag = 0; 2830 slptimeo = 2 * hz; 2831 } 2832 goto loop; 2833 } 2834 if ((bp->b_flags & B_DELWRI) == 0) 2835 panic("nfs_fsync: not dirty"); 2836 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) { 2837 BUF_UNLOCK(bp); 2838 continue; 2839 } 2840 VI_UNLOCK(vp); 2841 bremfree(bp); 2842 if (passone || !commit) 2843 bp->b_flags |= B_ASYNC; 2844 else 2845 bp->b_flags |= B_ASYNC; 2846 splx(s); 2847 bwrite(bp); 2848 if (nfs_sigintr(nmp, NULL, td)) { 2849 error = EINTR; 2850 goto done; 2851 } 2852 goto loop; 2853 } 2854 splx(s); 2855 if (passone) { 2856 passone = 0; 2857 VI_UNLOCK(vp); 2858 goto again; 2859 } 2860 if (waitfor == MNT_WAIT) { 2861 while (vp->v_bufobj.bo_numoutput) { 2862 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo); 2863 if (error) { 2864 VI_UNLOCK(vp); 2865 error = nfs_sigintr(nmp, NULL, td); 2866 if (error) 2867 goto done; 2868 if (slpflag == PCATCH) { 2869 slpflag = 0; 2870 slptimeo = 2 * hz; 2871 } 2872 VI_LOCK(vp); 2873 } 2874 } 2875 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) { 2876 VI_UNLOCK(vp); 2877 goto loop; 2878 } 2879 } 2880 VI_UNLOCK(vp); 2881 if (np->n_flag & NWRITEERR) { 2882 error = np->n_error; 2883 np->n_flag &= ~NWRITEERR; 2884 } 2885 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0 && 2886 vp->v_bufobj.bo_numoutput == 0) 2887 np->n_flag &= ~NMODIFIED; 2888done: 2889 if (bvec != NULL && bvec != bvec_on_stack) 2890 free(bvec, M_TEMP); 2891 return (error); 2892} 2893 2894/* 2895 * NFS advisory byte-level locks. 2896 */ 2897static int 2898nfs_advlock(struct vop_advlock_args *ap) 2899{ 2900 2901 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) { 2902 struct nfsnode *np = VTONFS(ap->a_vp); 2903 2904 return (lf_advlock(ap, &(np->n_lockf), np->n_size)); 2905 } 2906 return (nfs_dolock(ap)); 2907} 2908 2909/* 2910 * Print out the contents of an nfsnode. 2911 */ 2912static int 2913nfs_print(struct vop_print_args *ap) 2914{ 2915 struct vnode *vp = ap->a_vp; 2916 struct nfsnode *np = VTONFS(vp); 2917 2918 printf("\tfileid %ld fsid 0x%x", 2919 np->n_vattr.va_fileid, np->n_vattr.va_fsid); 2920 if (vp->v_type == VFIFO) 2921 fifo_printinfo(vp); 2922 printf("\n"); 2923 return (0); 2924} 2925 2926/* 2927 * This is the "real" nfs::bwrite(struct buf*). 2928 * We set B_CACHE if this is a VMIO buffer. 2929 */ 2930int 2931nfs_writebp(struct buf *bp, int force __unused, struct thread *td) 2932{ 2933 int s; 2934 int oldflags = bp->b_flags; 2935#if 0 2936 int retv = 1; 2937 off_t off; 2938#endif 2939 2940 if (BUF_REFCNT(bp) == 0) 2941 panic("bwrite: buffer is not locked???"); 2942 2943 if (bp->b_flags & B_INVAL) { 2944 brelse(bp); 2945 return(0); 2946 } 2947 2948 bp->b_flags |= B_CACHE; 2949 2950 /* 2951 * Undirty the bp. We will redirty it later if the I/O fails. 2952 */ 2953 2954 s = splbio(); 2955 bundirty(bp); 2956 bp->b_flags &= ~B_DONE; 2957 bp->b_ioflags &= ~BIO_ERROR; 2958 bp->b_iocmd = BIO_WRITE; 2959 2960 bufobj_wref(bp->b_bufobj); 2961 curthread->td_proc->p_stats->p_ru.ru_oublock++; 2962 splx(s); 2963 2964 /* 2965 * Note: to avoid loopback deadlocks, we do not 2966 * assign b_runningbufspace. 2967 */ 2968 vfs_busy_pages(bp, 1); 2969 2970 BUF_KERNPROC(bp); 2971 bp->b_iooffset = dbtob(bp->b_blkno); 2972 bstrategy(bp); 2973 2974 if( (oldflags & B_ASYNC) == 0) { 2975 int rtval = bufwait(bp); 2976 2977 if (oldflags & B_DELWRI) { 2978 s = splbio(); 2979 reassignbuf(bp); 2980 splx(s); 2981 } 2982 2983 brelse(bp); 2984 return (rtval); 2985 } 2986 2987 return (0); 2988} 2989 2990/* 2991 * nfs special file access vnode op. 2992 * Essentially just get vattr and then imitate iaccess() since the device is 2993 * local to the client. 2994 */ 2995static int 2996nfsspec_access(struct vop_access_args *ap) 2997{ 2998 struct vattr *vap; 2999 struct ucred *cred = ap->a_cred; 3000 struct vnode *vp = ap->a_vp; 3001 mode_t mode = ap->a_mode; 3002 struct vattr vattr; 3003 int error; 3004 3005 /* 3006 * Disallow write attempts on filesystems mounted read-only; 3007 * unless the file is a socket, fifo, or a block or character 3008 * device resident on the filesystem. 3009 */ 3010 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 3011 switch (vp->v_type) { 3012 case VREG: 3013 case VDIR: 3014 case VLNK: 3015 return (EROFS); 3016 default: 3017 break; 3018 } 3019 } 3020 vap = &vattr; 3021 error = VOP_GETATTR(vp, vap, cred, ap->a_td); 3022 if (error) 3023 return (error); 3024 return (vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid, 3025 mode, cred, NULL)); 3026} 3027 3028/* 3029 * Read wrapper for fifos. 3030 */ 3031static int 3032nfsfifo_read(struct vop_read_args *ap) 3033{ 3034 struct nfsnode *np = VTONFS(ap->a_vp); 3035 3036 /* 3037 * Set access flag. 3038 */ 3039 np->n_flag |= NACC; 3040 getnanotime(&np->n_atim); 3041 return (fifo_specops.vop_read(ap)); 3042} 3043 3044/* 3045 * Write wrapper for fifos. 3046 */ 3047static int 3048nfsfifo_write(struct vop_write_args *ap) 3049{ 3050 struct nfsnode *np = VTONFS(ap->a_vp); 3051 3052 /* 3053 * Set update flag. 3054 */ 3055 np->n_flag |= NUPD; 3056 getnanotime(&np->n_mtim); 3057 return (fifo_specops.vop_write(ap)); 3058} 3059 3060/* 3061 * Close wrapper for fifos. 3062 * 3063 * Update the times on the nfsnode then do fifo close. 3064 */ 3065static int 3066nfsfifo_close(struct vop_close_args *ap) 3067{ 3068 struct vnode *vp = ap->a_vp; 3069 struct nfsnode *np = VTONFS(vp); 3070 struct vattr vattr; 3071 struct timespec ts; 3072 3073 if (np->n_flag & (NACC | NUPD)) { 3074 getnanotime(&ts); 3075 if (np->n_flag & NACC) 3076 np->n_atim = ts; 3077 if (np->n_flag & NUPD) 3078 np->n_mtim = ts; 3079 np->n_flag |= NCHG; 3080 if (vrefcnt(vp) == 1 && 3081 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 3082 VATTR_NULL(&vattr); 3083 if (np->n_flag & NACC) 3084 vattr.va_atime = np->n_atim; 3085 if (np->n_flag & NUPD) 3086 vattr.va_mtime = np->n_mtim; 3087 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td); 3088 } 3089 } 3090 return (fifo_specops.vop_close(ap)); 3091} 3092 3093/* 3094 * Just call nfs_writebp() with the force argument set to 1. 3095 * 3096 * NOTE: B_DONE may or may not be set in a_bp on call. 3097 */ 3098static int 3099nfs_bwrite(struct buf *bp) 3100{ 3101 3102 return (nfs_writebp(bp, 1, curthread)); 3103} 3104 3105struct buf_ops buf_ops_nfs = { 3106 .bop_name = "buf_ops_nfs", 3107 .bop_write = nfs_bwrite, 3108 .bop_strategy = bufstrategy, 3109 .bop_sync = bufsync, 3110}; 3111