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