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