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