38 */ 39 40 41/* 42 * vnode op calls for Sun NFS version 2 and 3 43 */ 44 45#include "opt_inet.h" 46 47#include <sys/param.h> 48#include <sys/kernel.h> 49#include <sys/systm.h> 50#include <sys/resourcevar.h> 51#include <sys/proc.h> 52#include <sys/mount.h> 53#include <sys/buf.h> 54#include <sys/malloc.h> 55#include <sys/mbuf.h> 56#include <sys/namei.h> 57#include <sys/socket.h> 58#include <sys/vnode.h> 59#include <sys/dirent.h> 60#include <sys/fcntl.h> 61#include <sys/lockf.h> 62#include <sys/stat.h> 63 64#include <vm/vm.h> 65#include <vm/vm_extern.h> 66#include <vm/vm_zone.h> 67 68#include <miscfs/fifofs/fifo.h> 69#include <miscfs/specfs/specdev.h> 70 71#include <nfs/rpcv2.h> 72#include <nfs/nfsproto.h> 73#include <nfs/nfs.h> 74#include <nfs/nfsnode.h> 75#include <nfs/nfsmount.h> 76#include <nfs/xdr_subs.h> 77#include <nfs/nfsm_subs.h> 78#include <nfs/nqnfs.h> 79 80#include <net/if.h> 81#include <netinet/in.h> 82#include <netinet/in_var.h> 83 84/* Defs */ 85#define TRUE 1 86#define FALSE 0 87 88/* 89 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these 90 * calls are not in getblk() and brelse() so that they would not be necessary 91 * here. 92 */ 93#ifndef B_VMIO 94#define vfs_busy_pages(bp, f) 95#endif 96 97static int nfsspec_read __P((struct vop_read_args *)); 98static int nfsspec_write __P((struct vop_write_args *)); 99static int nfsfifo_read __P((struct vop_read_args *)); 100static int nfsfifo_write __P((struct vop_write_args *)); 101static int nfsspec_close __P((struct vop_close_args *)); 102static int nfsfifo_close __P((struct vop_close_args *)); 103#define nfs_poll vop_nopoll 104static int nfs_flush __P((struct vnode *,struct ucred *,int,struct proc *,int)); 105static int nfs_setattrrpc __P((struct vnode *,struct vattr *,struct ucred *,struct proc *)); 106static int nfs_lookup __P((struct vop_lookup_args *)); 107static int nfs_create __P((struct vop_create_args *)); 108static int nfs_mknod __P((struct vop_mknod_args *)); 109static int nfs_open __P((struct vop_open_args *)); 110static int nfs_close __P((struct vop_close_args *)); 111static int nfs_access __P((struct vop_access_args *)); 112static int nfs_getattr __P((struct vop_getattr_args *)); 113static int nfs_setattr __P((struct vop_setattr_args *)); 114static int nfs_read __P((struct vop_read_args *)); 115static int nfs_mmap __P((struct vop_mmap_args *)); 116static int nfs_fsync __P((struct vop_fsync_args *)); 117static int nfs_remove __P((struct vop_remove_args *)); 118static int nfs_link __P((struct vop_link_args *)); 119static int nfs_rename __P((struct vop_rename_args *)); 120static int nfs_mkdir __P((struct vop_mkdir_args *)); 121static int nfs_rmdir __P((struct vop_rmdir_args *)); 122static int nfs_symlink __P((struct vop_symlink_args *)); 123static int nfs_readdir __P((struct vop_readdir_args *)); 124static int nfs_bmap __P((struct vop_bmap_args *)); 125static int nfs_strategy __P((struct vop_strategy_args *)); 126static int nfs_lookitup __P((struct vnode *, const char *, int, 127 struct ucred *, struct proc *, struct nfsnode **)); 128static int nfs_sillyrename __P((struct vnode *,struct vnode *,struct componentname *)); 129static int nfsspec_access __P((struct vop_access_args *)); 130static int nfs_readlink __P((struct vop_readlink_args *)); 131static int nfs_print __P((struct vop_print_args *)); 132static int nfs_advlock __P((struct vop_advlock_args *)); 133static int nfs_bwrite __P((struct vop_bwrite_args *)); 134/* 135 * Global vfs data structures for nfs 136 */ 137vop_t **nfsv2_vnodeop_p; 138static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { 139 { &vop_default_desc, (vop_t *) vop_defaultop }, 140 { &vop_abortop_desc, (vop_t *) nfs_abortop }, 141 { &vop_access_desc, (vop_t *) nfs_access }, 142 { &vop_advlock_desc, (vop_t *) nfs_advlock }, 143 { &vop_bmap_desc, (vop_t *) nfs_bmap }, 144 { &vop_bwrite_desc, (vop_t *) nfs_bwrite }, 145 { &vop_close_desc, (vop_t *) nfs_close }, 146 { &vop_create_desc, (vop_t *) nfs_create }, 147 { &vop_fsync_desc, (vop_t *) nfs_fsync }, 148 { &vop_getattr_desc, (vop_t *) nfs_getattr }, 149 { &vop_getpages_desc, (vop_t *) nfs_getpages }, 150 { &vop_putpages_desc, (vop_t *) nfs_putpages }, 151 { &vop_inactive_desc, (vop_t *) nfs_inactive }, 152 { &vop_lease_desc, (vop_t *) vop_null }, 153 { &vop_link_desc, (vop_t *) nfs_link }, 154 { &vop_lock_desc, (vop_t *) vop_sharedlock }, 155 { &vop_lookup_desc, (vop_t *) nfs_lookup }, 156 { &vop_mkdir_desc, (vop_t *) nfs_mkdir }, 157 { &vop_mknod_desc, (vop_t *) nfs_mknod }, 158 { &vop_mmap_desc, (vop_t *) nfs_mmap }, 159 { &vop_open_desc, (vop_t *) nfs_open }, 160 { &vop_poll_desc, (vop_t *) nfs_poll }, 161 { &vop_print_desc, (vop_t *) nfs_print }, 162 { &vop_read_desc, (vop_t *) nfs_read }, 163 { &vop_readdir_desc, (vop_t *) nfs_readdir }, 164 { &vop_readlink_desc, (vop_t *) nfs_readlink }, 165 { &vop_reclaim_desc, (vop_t *) nfs_reclaim }, 166 { &vop_remove_desc, (vop_t *) nfs_remove }, 167 { &vop_rename_desc, (vop_t *) nfs_rename }, 168 { &vop_rmdir_desc, (vop_t *) nfs_rmdir }, 169 { &vop_setattr_desc, (vop_t *) nfs_setattr }, 170 { &vop_strategy_desc, (vop_t *) nfs_strategy }, 171 { &vop_symlink_desc, (vop_t *) nfs_symlink }, 172 { &vop_write_desc, (vop_t *) nfs_write }, 173 { NULL, NULL } 174}; 175static struct vnodeopv_desc nfsv2_vnodeop_opv_desc = 176 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries }; 177VNODEOP_SET(nfsv2_vnodeop_opv_desc); 178 179/* 180 * Special device vnode ops 181 */ 182vop_t **spec_nfsv2nodeop_p; 183static struct vnodeopv_entry_desc nfsv2_specop_entries[] = { 184 { &vop_default_desc, (vop_t *) spec_vnoperate }, 185 { &vop_access_desc, (vop_t *) nfsspec_access }, 186 { &vop_close_desc, (vop_t *) nfsspec_close }, 187 { &vop_fsync_desc, (vop_t *) nfs_fsync }, 188 { &vop_getattr_desc, (vop_t *) nfs_getattr }, 189 { &vop_inactive_desc, (vop_t *) nfs_inactive }, 190 { &vop_lock_desc, (vop_t *) vop_sharedlock }, 191 { &vop_print_desc, (vop_t *) nfs_print }, 192 { &vop_read_desc, (vop_t *) nfsspec_read }, 193 { &vop_reclaim_desc, (vop_t *) nfs_reclaim }, 194 { &vop_setattr_desc, (vop_t *) nfs_setattr }, 195 { &vop_write_desc, (vop_t *) nfsspec_write }, 196 { NULL, NULL } 197}; 198static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc = 199 { &spec_nfsv2nodeop_p, nfsv2_specop_entries }; 200VNODEOP_SET(spec_nfsv2nodeop_opv_desc); 201 202vop_t **fifo_nfsv2nodeop_p; 203static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = { 204 { &vop_default_desc, (vop_t *) fifo_vnoperate }, 205 { &vop_access_desc, (vop_t *) nfsspec_access }, 206 { &vop_close_desc, (vop_t *) nfsfifo_close }, 207 { &vop_fsync_desc, (vop_t *) nfs_fsync }, 208 { &vop_getattr_desc, (vop_t *) nfs_getattr }, 209 { &vop_inactive_desc, (vop_t *) nfs_inactive }, 210 { &vop_lock_desc, (vop_t *) vop_sharedlock }, 211 { &vop_print_desc, (vop_t *) nfs_print }, 212 { &vop_read_desc, (vop_t *) nfsfifo_read }, 213 { &vop_reclaim_desc, (vop_t *) nfs_reclaim }, 214 { &vop_setattr_desc, (vop_t *) nfs_setattr }, 215 { &vop_write_desc, (vop_t *) nfsfifo_write }, 216 { NULL, NULL } 217}; 218static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc = 219 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries }; 220VNODEOP_SET(fifo_nfsv2nodeop_opv_desc); 221 222static int nfs_commit __P((struct vnode *vp, u_quad_t offset, int cnt, 223 struct ucred *cred, struct proc *procp)); 224static int nfs_mknodrpc __P((struct vnode *dvp, struct vnode **vpp, 225 struct componentname *cnp, 226 struct vattr *vap)); 227static int nfs_removerpc __P((struct vnode *dvp, const char *name, 228 int namelen, 229 struct ucred *cred, struct proc *proc)); 230static int nfs_renamerpc __P((struct vnode *fdvp, const char *fnameptr, 231 int fnamelen, struct vnode *tdvp, 232 const char *tnameptr, int tnamelen, 233 struct ucred *cred, struct proc *proc)); 234static int nfs_renameit __P((struct vnode *sdvp, 235 struct componentname *scnp, 236 struct sillyrename *sp)); 237 238/* 239 * Global variables 240 */ 241extern u_int32_t nfs_true, nfs_false; 242extern u_int32_t nfs_xdrneg1; 243extern struct nfsstats nfsstats; 244extern nfstype nfsv3_type[9]; 245struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 246struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON]; 247int nfs_numasync = 0; 248#define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1)) 249 250/* 251 * nfs access vnode op. 252 * For nfs version 2, just return ok. File accesses may fail later. 253 * For nfs version 3, use the access rpc to check accessibility. If file modes 254 * are changed on the server, accesses might still fail later. 255 */ 256static int 257nfs_access(ap) 258 struct vop_access_args /* { 259 struct vnode *a_vp; 260 int a_mode; 261 struct ucred *a_cred; 262 struct proc *a_p; 263 } */ *ap; 264{ 265 register struct vnode *vp = ap->a_vp; 266 register u_int32_t *tl; 267 register caddr_t cp; 268 register int32_t t1, t2; 269 caddr_t bpos, dpos, cp2; 270 int error = 0, attrflag; 271 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 272 u_int32_t mode, rmode; 273 int v3 = NFS_ISV3(vp); 274 275 /* 276 * Disallow write attempts on filesystems mounted read-only; 277 * unless the file is a socket, fifo, or a block or character 278 * device resident on the filesystem. 279 */ 280 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 281 switch (vp->v_type) { 282 case VREG: 283 case VDIR: 284 case VLNK: 285 return (EROFS); 286 default: 287 break; 288 } 289 } 290 /* 291 * For nfs v3, do an access rpc, otherwise you are stuck emulating 292 * ufs_access() locally using the vattr. This may not be correct, 293 * since the server may apply other access criteria such as 294 * client uid-->server uid mapping that we do not know about, but 295 * this is better than just returning anything that is lying about 296 * in the cache. 297 */ 298 if (v3) { 299 nfsstats.rpccnt[NFSPROC_ACCESS]++; 300 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED); 301 nfsm_fhtom(vp, v3); 302 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 303 if (ap->a_mode & VREAD) 304 mode = NFSV3ACCESS_READ; 305 else 306 mode = 0; 307 if (vp->v_type != VDIR) { 308 if (ap->a_mode & VWRITE) 309 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND); 310 if (ap->a_mode & VEXEC) 311 mode |= NFSV3ACCESS_EXECUTE; 312 } else { 313 if (ap->a_mode & VWRITE) 314 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND | 315 NFSV3ACCESS_DELETE); 316 if (ap->a_mode & VEXEC) 317 mode |= NFSV3ACCESS_LOOKUP; 318 } 319 *tl = txdr_unsigned(mode); 320 nfsm_request(vp, NFSPROC_ACCESS, ap->a_p, ap->a_cred); 321 nfsm_postop_attr(vp, attrflag); 322 if (!error) { 323 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 324 rmode = fxdr_unsigned(u_int32_t, *tl); 325 /* 326 * The NFS V3 spec does not clarify whether or not 327 * the returned access bits can be a superset of 328 * the ones requested, so... 329 */ 330 if ((rmode & mode) != mode) 331 error = EACCES; 332 } 333 nfsm_reqdone; 334 return (error); 335 } else { 336 if (error = nfsspec_access(ap)) 337 return (error); 338 339 /* 340 * Attempt to prevent a mapped root from accessing a file 341 * which it shouldn't. We try to read a byte from the file 342 * if the user is root and the file is not zero length. 343 * After calling nfsspec_access, we should have the correct 344 * file size cached. 345 */ 346 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD) 347 && VTONFS(vp)->n_size > 0) { 348 struct iovec aiov; 349 struct uio auio; 350 char buf[1]; 351 352 aiov.iov_base = buf; 353 aiov.iov_len = 1; 354 auio.uio_iov = &aiov; 355 auio.uio_iovcnt = 1; 356 auio.uio_offset = 0; 357 auio.uio_resid = 1; 358 auio.uio_segflg = UIO_SYSSPACE; 359 auio.uio_rw = UIO_READ; 360 auio.uio_procp = ap->a_p; 361 362 if (vp->v_type == VREG) 363 error = nfs_readrpc(vp, &auio, ap->a_cred); 364 else if (vp->v_type == VDIR) { 365 char* bp; 366 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK); 367 aiov.iov_base = bp; 368 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ; 369 error = nfs_readdirrpc(vp, &auio, ap->a_cred); 370 free(bp, M_TEMP); 371 } else if (vp->v_type = VLNK) 372 error = nfs_readlinkrpc(vp, &auio, ap->a_cred); 373 else 374 error = EACCES; 375 } 376 return (error); 377 } 378} 379 380/* 381 * nfs open vnode op 382 * Check to see if the type is ok 383 * and that deletion is not in progress. 384 * For paged in text files, you will need to flush the page cache 385 * if consistency is lost. 386 */ 387/* ARGSUSED */ 388static int 389nfs_open(ap) 390 struct vop_open_args /* { 391 struct vnode *a_vp; 392 int a_mode; 393 struct ucred *a_cred; 394 struct proc *a_p; 395 } */ *ap; 396{ 397 register struct vnode *vp = ap->a_vp; 398 struct nfsnode *np = VTONFS(vp); 399 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 400 struct vattr vattr; 401 int error; 402 403 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) { 404#ifdef DIAGNOSTIC 405 printf("open eacces vtyp=%d\n",vp->v_type); 406#endif 407 return (EACCES); 408 } 409 /* 410 * Get a valid lease. If cached data is stale, flush it. 411 */ 412 if (nmp->nm_flag & NFSMNT_NQNFS) { 413 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 414 do { 415 error = nqnfs_getlease(vp, ND_READ, ap->a_cred, 416 ap->a_p); 417 } while (error == NQNFS_EXPIRED); 418 if (error) 419 return (error); 420 if (np->n_lrev != np->n_brev || 421 (np->n_flag & NQNFSNONCACHE)) { 422 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 423 ap->a_p, 1)) == EINTR) 424 return (error); 425 np->n_brev = np->n_lrev; 426 } 427 } 428 } else { 429 if (np->n_flag & NMODIFIED) { 430 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 431 ap->a_p, 1)) == EINTR) 432 return (error); 433 np->n_attrstamp = 0; 434 if (vp->v_type == VDIR) 435 np->n_direofoffset = 0; 436 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p); 437 if (error) 438 return (error); 439 np->n_mtime = vattr.va_mtime.tv_sec; 440 } else { 441 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p); 442 if (error) 443 return (error); 444 if (np->n_mtime != vattr.va_mtime.tv_sec) { 445 if (vp->v_type == VDIR) 446 np->n_direofoffset = 0; 447 if ((error = nfs_vinvalbuf(vp, V_SAVE, 448 ap->a_cred, ap->a_p, 1)) == EINTR) 449 return (error); 450 np->n_mtime = vattr.va_mtime.tv_sec; 451 } 452 } 453 } 454 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) 455 np->n_attrstamp = 0; /* For Open/Close consistency */ 456 return (0); 457} 458 459/* 460 * nfs close vnode op 461 * What an NFS client should do upon close after writing is a debatable issue. 462 * Most NFS clients push delayed writes to the server upon close, basically for 463 * two reasons: 464 * 1 - So that any write errors may be reported back to the client process 465 * doing the close system call. By far the two most likely errors are 466 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. 467 * 2 - To put a worst case upper bound on cache inconsistency between 468 * multiple clients for the file. 469 * There is also a consistency problem for Version 2 of the protocol w.r.t. 470 * not being able to tell if other clients are writing a file concurrently, 471 * since there is no way of knowing if the changed modify time in the reply 472 * is only due to the write for this client. 473 * (NFS Version 3 provides weak cache consistency data in the reply that 474 * should be sufficient to detect and handle this case.) 475 * 476 * The current code does the following: 477 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers 478 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate 479 * or commit them (this satisfies 1 and 2 except for the 480 * case where the server crashes after this close but 481 * before the commit RPC, which is felt to be "good 482 * enough". Changing the last argument to nfs_flush() to 483 * a 1 would force a commit operation, if it is felt a 484 * commit is necessary now. 485 * for NQNFS - do nothing now, since 2 is dealt with via leases and 486 * 1 should be dealt with via an fsync() system call for 487 * cases where write errors are important. 488 */ 489/* ARGSUSED */ 490static int 491nfs_close(ap) 492 struct vop_close_args /* { 493 struct vnodeop_desc *a_desc; 494 struct vnode *a_vp; 495 int a_fflag; 496 struct ucred *a_cred; 497 struct proc *a_p; 498 } */ *ap; 499{ 500 register struct vnode *vp = ap->a_vp; 501 register struct nfsnode *np = VTONFS(vp); 502 int error = 0; 503 504 if (vp->v_type == VREG) { 505 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 && 506 (np->n_flag & NMODIFIED)) { 507 if (NFS_ISV3(vp)) { 508 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0); 509 np->n_flag &= ~NMODIFIED; 510 } else 511 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1); 512 np->n_attrstamp = 0; 513 } 514 if (np->n_flag & NWRITEERR) { 515 np->n_flag &= ~NWRITEERR; 516 error = np->n_error; 517 } 518 } 519 return (error); 520} 521 522/* 523 * nfs getattr call from vfs. 524 */ 525static int 526nfs_getattr(ap) 527 struct vop_getattr_args /* { 528 struct vnode *a_vp; 529 struct vattr *a_vap; 530 struct ucred *a_cred; 531 struct proc *a_p; 532 } */ *ap; 533{ 534 register struct vnode *vp = ap->a_vp; 535 register struct nfsnode *np = VTONFS(vp); 536 register caddr_t cp; 537 register u_int32_t *tl; 538 register int32_t t1, t2; 539 caddr_t bpos, dpos; 540 int error = 0; 541 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 542 int v3 = NFS_ISV3(vp); 543 544 /* 545 * Update local times for special files. 546 */ 547 if (np->n_flag & (NACC | NUPD)) 548 np->n_flag |= NCHG; 549 /* 550 * First look in the cache. 551 */ 552 if (nfs_getattrcache(vp, ap->a_vap) == 0) 553 return (0); 554 nfsstats.rpccnt[NFSPROC_GETATTR]++; 555 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3)); 556 nfsm_fhtom(vp, v3); 557 nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred); 558 if (!error) { 559 nfsm_loadattr(vp, ap->a_vap); 560 } 561 nfsm_reqdone; 562 return (error); 563} 564 565/* 566 * nfs setattr call. 567 */ 568static int 569nfs_setattr(ap) 570 struct vop_setattr_args /* { 571 struct vnodeop_desc *a_desc; 572 struct vnode *a_vp; 573 struct vattr *a_vap; 574 struct ucred *a_cred; 575 struct proc *a_p; 576 } */ *ap; 577{ 578 register struct vnode *vp = ap->a_vp; 579 register struct nfsnode *np = VTONFS(vp); 580 register struct vattr *vap = ap->a_vap; 581 int error = 0; 582 u_quad_t tsize; 583 584#ifndef nolint 585 tsize = (u_quad_t)0; 586#endif 587 588 /* 589 * Setting of flags is not supported. 590 */ 591 if (vap->va_flags != VNOVAL) 592 return (EOPNOTSUPP); 593 594 /* 595 * Disallow write attempts if the filesystem is mounted read-only. 596 */ 597 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 598 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 599 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 600 (vp->v_mount->mnt_flag & MNT_RDONLY)) 601 return (EROFS); 602 if (vap->va_size != VNOVAL) { 603 switch (vp->v_type) { 604 case VDIR: 605 return (EISDIR); 606 case VCHR: 607 case VBLK: 608 case VSOCK: 609 case VFIFO: 610 if (vap->va_mtime.tv_sec == VNOVAL && 611 vap->va_atime.tv_sec == VNOVAL && 612 vap->va_mode == (mode_t)VNOVAL && 613 vap->va_uid == (uid_t)VNOVAL && 614 vap->va_gid == (gid_t)VNOVAL) 615 return (0); 616 vap->va_size = VNOVAL; 617 break; 618 default: 619 /* 620 * Disallow write attempts if the filesystem is 621 * mounted read-only. 622 */ 623 if (vp->v_mount->mnt_flag & MNT_RDONLY) 624 return (EROFS); 625 vnode_pager_setsize(vp, (u_long)vap->va_size); 626 if (np->n_flag & NMODIFIED) { 627 if (vap->va_size == 0) 628 error = nfs_vinvalbuf(vp, 0, 629 ap->a_cred, ap->a_p, 1); 630 else 631 error = nfs_vinvalbuf(vp, V_SAVE, 632 ap->a_cred, ap->a_p, 1); 633 if (error) { 634 vnode_pager_setsize(vp, (u_long)np->n_size); 635 return (error); 636 } 637 } 638 tsize = np->n_size; 639 np->n_size = np->n_vattr.va_size = vap->va_size; 640 }; 641 } else if ((vap->va_mtime.tv_sec != VNOVAL || 642 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) && 643 vp->v_type == VREG && 644 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 645 ap->a_p, 1)) == EINTR) 646 return (error); 647 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p); 648 if (error && vap->va_size != VNOVAL) { 649 np->n_size = np->n_vattr.va_size = tsize; 650 vnode_pager_setsize(vp, (u_long)np->n_size); 651 } 652 return (error); 653} 654 655/* 656 * Do an nfs setattr rpc. 657 */ 658static int 659nfs_setattrrpc(vp, vap, cred, procp) 660 register struct vnode *vp; 661 register struct vattr *vap; 662 struct ucred *cred; 663 struct proc *procp; 664{ 665 register struct nfsv2_sattr *sp; 666 register caddr_t cp; 667 register int32_t t1, t2; 668 caddr_t bpos, dpos, cp2; 669 u_int32_t *tl; 670 int error = 0, wccflag = NFSV3_WCCRATTR; 671 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 672 int v3 = NFS_ISV3(vp); 673 674 nfsstats.rpccnt[NFSPROC_SETATTR]++; 675 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3)); 676 nfsm_fhtom(vp, v3); 677 if (v3) { 678 if (vap->va_mode != (mode_t)VNOVAL) { 679 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 680 *tl++ = nfs_true; 681 *tl = txdr_unsigned(vap->va_mode); 682 } else { 683 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 684 *tl = nfs_false; 685 } 686 if (vap->va_uid != (uid_t)VNOVAL) { 687 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 688 *tl++ = nfs_true; 689 *tl = txdr_unsigned(vap->va_uid); 690 } else { 691 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 692 *tl = nfs_false; 693 } 694 if (vap->va_gid != (gid_t)VNOVAL) { 695 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 696 *tl++ = nfs_true; 697 *tl = txdr_unsigned(vap->va_gid); 698 } else { 699 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 700 *tl = nfs_false; 701 } 702 if (vap->va_size != VNOVAL) { 703 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 704 *tl++ = nfs_true; 705 txdr_hyper(&vap->va_size, tl); 706 } else { 707 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 708 *tl = nfs_false; 709 } 710 if (vap->va_atime.tv_sec != VNOVAL) { 711 if (vap->va_atime.tv_sec != time_second) { 712 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 713 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 714 txdr_nfsv3time(&vap->va_atime, tl); 715 } else { 716 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 717 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 718 } 719 } else { 720 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 721 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 722 } 723 if (vap->va_mtime.tv_sec != VNOVAL) { 724 if (vap->va_mtime.tv_sec != time_second) { 725 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 726 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 727 txdr_nfsv3time(&vap->va_mtime, tl); 728 } else { 729 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 730 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 731 } 732 } else { 733 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 734 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 735 } 736 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 737 *tl = nfs_false; 738 } else { 739 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 740 if (vap->va_mode == (mode_t)VNOVAL) 741 sp->sa_mode = nfs_xdrneg1; 742 else 743 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode); 744 if (vap->va_uid == (uid_t)VNOVAL) 745 sp->sa_uid = nfs_xdrneg1; 746 else 747 sp->sa_uid = txdr_unsigned(vap->va_uid); 748 if (vap->va_gid == (gid_t)VNOVAL) 749 sp->sa_gid = nfs_xdrneg1; 750 else 751 sp->sa_gid = txdr_unsigned(vap->va_gid); 752 sp->sa_size = txdr_unsigned(vap->va_size); 753 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 754 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 755 } 756 nfsm_request(vp, NFSPROC_SETATTR, procp, cred); 757 if (v3) { 758 nfsm_wcc_data(vp, wccflag); 759 } else 760 nfsm_loadattr(vp, (struct vattr *)0); 761 nfsm_reqdone; 762 return (error); 763} 764 765/* 766 * nfs lookup call, one step at a time... 767 * First look in cache 768 * If not found, unlock the directory nfsnode and do the rpc 769 */ 770static int 771nfs_lookup(ap) 772 struct vop_lookup_args /* { 773 struct vnodeop_desc *a_desc; 774 struct vnode *a_dvp; 775 struct vnode **a_vpp; 776 struct componentname *a_cnp; 777 } */ *ap; 778{ 779 struct componentname *cnp = ap->a_cnp; 780 struct vnode *dvp = ap->a_dvp; 781 struct vnode **vpp = ap->a_vpp; 782 int flags = cnp->cn_flags; 783 struct vnode *newvp; 784 u_int32_t *tl; 785 caddr_t cp; 786 int32_t t1, t2; 787 struct nfsmount *nmp; 788 caddr_t bpos, dpos, cp2; 789 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 790 long len; 791 nfsfh_t *fhp; 792 struct nfsnode *np; 793 int lockparent, wantparent, error = 0, attrflag, fhsize; 794 int v3 = NFS_ISV3(dvp); 795 struct proc *p = cnp->cn_proc; 796 797 *vpp = NULLVP; 798 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 799 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 800 return (EROFS); 801 if (dvp->v_type != VDIR) 802 return (ENOTDIR); 803 lockparent = flags & LOCKPARENT; 804 wantparent = flags & (LOCKPARENT|WANTPARENT); 805 nmp = VFSTONFS(dvp->v_mount); 806 np = VTONFS(dvp); 807 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) { 808 struct vattr vattr; 809 int vpid; 810 811 if (error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, p)) { 812 *vpp = NULLVP; 813 return (error); 814 } 815 816 newvp = *vpp; 817 vpid = newvp->v_id; 818 /* 819 * See the comment starting `Step through' in ufs/ufs_lookup.c 820 * for an explanation of the locking protocol 821 */ 822 if (dvp == newvp) { 823 VREF(newvp); 824 error = 0; 825 } else if (flags & ISDOTDOT) { 826 VOP_UNLOCK(dvp, 0, p); 827 error = vget(newvp, LK_EXCLUSIVE, p); 828 if (!error && lockparent && (flags & ISLASTCN)) 829 error = vn_lock(dvp, LK_EXCLUSIVE, p); 830 } else { 831 error = vget(newvp, LK_EXCLUSIVE, p); 832 if (!lockparent || error || !(flags & ISLASTCN)) 833 VOP_UNLOCK(dvp, 0, p); 834 } 835 if (!error) { 836 if (vpid == newvp->v_id) { 837 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, p) 838 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) { 839 nfsstats.lookupcache_hits++; 840 if (cnp->cn_nameiop != LOOKUP && 841 (flags & ISLASTCN)) 842 cnp->cn_flags |= SAVENAME; 843 return (0); 844 } 845 cache_purge(newvp); 846 } 847 vput(newvp); 848 if (lockparent && dvp != newvp && (flags & ISLASTCN)) 849 VOP_UNLOCK(dvp, 0, p); 850 } 851 error = vn_lock(dvp, LK_EXCLUSIVE, p); 852 *vpp = NULLVP; 853 if (error) 854 return (error); 855 } 856 error = 0; 857 newvp = NULLVP; 858 nfsstats.lookupcache_misses++; 859 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 860 len = cnp->cn_namelen; 861 nfsm_reqhead(dvp, NFSPROC_LOOKUP, 862 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 863 nfsm_fhtom(dvp, v3); 864 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 865 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred); 866 if (error) { 867 nfsm_postop_attr(dvp, attrflag); 868 m_freem(mrep); 869 goto nfsmout; 870 } 871 nfsm_getfh(fhp, fhsize, v3); 872 873 /* 874 * Handle RENAME case... 875 */ 876 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) { 877 if (NFS_CMPFH(np, fhp, fhsize)) { 878 m_freem(mrep); 879 return (EISDIR); 880 } 881 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 882 if (error) { 883 m_freem(mrep); 884 return (error); 885 } 886 newvp = NFSTOV(np); 887 if (v3) { 888 nfsm_postop_attr(newvp, attrflag); 889 nfsm_postop_attr(dvp, attrflag); 890 } else 891 nfsm_loadattr(newvp, (struct vattr *)0); 892 *vpp = newvp; 893 m_freem(mrep); 894 cnp->cn_flags |= SAVENAME; 895 if (!lockparent) 896 VOP_UNLOCK(dvp, 0, p); 897 return (0); 898 } 899 900 if (flags & ISDOTDOT) { 901 VOP_UNLOCK(dvp, 0, p); 902 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 903 if (error) { 904 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p); 905 return (error); 906 } 907 newvp = NFSTOV(np); 908 if (lockparent && (flags & ISLASTCN) && 909 (error = vn_lock(dvp, LK_EXCLUSIVE, p))) { 910 vput(newvp); 911 return (error); 912 } 913 } else if (NFS_CMPFH(np, fhp, fhsize)) { 914 VREF(dvp); 915 newvp = dvp; 916 } else { 917 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 918 if (error) { 919 m_freem(mrep); 920 return (error); 921 } 922 if (!lockparent || !(flags & ISLASTCN)) 923 VOP_UNLOCK(dvp, 0, p); 924 newvp = NFSTOV(np); 925 } 926 if (v3) { 927 nfsm_postop_attr(newvp, attrflag); 928 nfsm_postop_attr(dvp, attrflag); 929 } else 930 nfsm_loadattr(newvp, (struct vattr *)0); 931 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 932 cnp->cn_flags |= SAVENAME; 933 if ((cnp->cn_flags & MAKEENTRY) && 934 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) { 935 np->n_ctime = np->n_vattr.va_ctime.tv_sec; 936 cache_enter(dvp, newvp, cnp); 937 } 938 *vpp = newvp; 939 nfsm_reqdone; 940 if (error) { 941 if (newvp != NULLVP) { 942 vrele(newvp); 943 *vpp = NULLVP; 944 } 945 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) && 946 (flags & ISLASTCN) && error == ENOENT) { 947 if (!lockparent) 948 VOP_UNLOCK(dvp, 0, p); 949 if (dvp->v_mount->mnt_flag & MNT_RDONLY) 950 error = EROFS; 951 else 952 error = EJUSTRETURN; 953 } 954 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 955 cnp->cn_flags |= SAVENAME; 956 } 957 return (error); 958} 959 960/* 961 * nfs read call. 962 * Just call nfs_bioread() to do the work. 963 */ 964static int 965nfs_read(ap) 966 struct vop_read_args /* { 967 struct vnode *a_vp; 968 struct uio *a_uio; 969 int a_ioflag; 970 struct ucred *a_cred; 971 } */ *ap; 972{ 973 register struct vnode *vp = ap->a_vp; 974 975 if (vp->v_type != VREG) 976 return (EPERM); 977 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0)); 978} 979 980/* 981 * nfs readlink call 982 */ 983static int 984nfs_readlink(ap) 985 struct vop_readlink_args /* { 986 struct vnode *a_vp; 987 struct uio *a_uio; 988 struct ucred *a_cred; 989 } */ *ap; 990{ 991 register struct vnode *vp = ap->a_vp; 992 993 if (vp->v_type != VLNK) 994 return (EINVAL); 995 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0)); 996} 997 998/* 999 * Do a readlink rpc. 1000 * Called by nfs_doio() from below the buffer cache. 1001 */ 1002int 1003nfs_readlinkrpc(vp, uiop, cred) 1004 register struct vnode *vp; 1005 struct uio *uiop; 1006 struct ucred *cred; 1007{ 1008 register u_int32_t *tl; 1009 register caddr_t cp; 1010 register int32_t t1, t2; 1011 caddr_t bpos, dpos, cp2; 1012 int error = 0, len, attrflag; 1013 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1014 int v3 = NFS_ISV3(vp); 1015 1016 nfsstats.rpccnt[NFSPROC_READLINK]++; 1017 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3)); 1018 nfsm_fhtom(vp, v3); 1019 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred); 1020 if (v3) 1021 nfsm_postop_attr(vp, attrflag); 1022 if (!error) { 1023 nfsm_strsiz(len, NFS_MAXPATHLEN); 1024 nfsm_mtouio(uiop, len); 1025 } 1026 nfsm_reqdone; 1027 return (error); 1028} 1029 1030/* 1031 * nfs read rpc call 1032 * Ditto above 1033 */ 1034int 1035nfs_readrpc(vp, uiop, cred) 1036 register struct vnode *vp; 1037 struct uio *uiop; 1038 struct ucred *cred; 1039{ 1040 register u_int32_t *tl; 1041 register caddr_t cp; 1042 register int32_t t1, t2; 1043 caddr_t bpos, dpos, cp2; 1044 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1045 struct nfsmount *nmp; 1046 int error = 0, len, retlen, tsiz, eof, attrflag; 1047 int v3 = NFS_ISV3(vp); 1048 1049#ifndef nolint 1050 eof = 0; 1051#endif 1052 nmp = VFSTONFS(vp->v_mount); 1053 tsiz = uiop->uio_resid; 1054 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1055 return (EFBIG); 1056 while (tsiz > 0) { 1057 nfsstats.rpccnt[NFSPROC_READ]++; 1058 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 1059 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3); 1060 nfsm_fhtom(vp, v3); 1061 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3); 1062 if (v3) { 1063 txdr_hyper(&uiop->uio_offset, tl); 1064 *(tl + 2) = txdr_unsigned(len); 1065 } else { 1066 *tl++ = txdr_unsigned(uiop->uio_offset); 1067 *tl++ = txdr_unsigned(len); 1068 *tl = 0; 1069 } 1070 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred); 1071 if (v3) { 1072 nfsm_postop_attr(vp, attrflag); 1073 if (error) { 1074 m_freem(mrep); 1075 goto nfsmout; 1076 } 1077 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1078 eof = fxdr_unsigned(int, *(tl + 1)); 1079 } else 1080 nfsm_loadattr(vp, (struct vattr *)0); 1081 nfsm_strsiz(retlen, nmp->nm_rsize); 1082 nfsm_mtouio(uiop, retlen); 1083 m_freem(mrep); 1084 tsiz -= retlen; 1085 if (v3) { 1086 if (eof || retlen == 0) 1087 tsiz = 0; 1088 } else if (retlen < len) 1089 tsiz = 0; 1090 } 1091nfsmout: 1092 return (error); 1093} 1094 1095/* 1096 * nfs write call 1097 */ 1098int 1099nfs_writerpc(vp, uiop, cred, iomode, must_commit) 1100 register struct vnode *vp; 1101 register struct uio *uiop; 1102 struct ucred *cred; 1103 int *iomode, *must_commit; 1104{ 1105 register u_int32_t *tl; 1106 register caddr_t cp; 1107 register int32_t t1, t2, backup; 1108 caddr_t bpos, dpos, cp2; 1109 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1110 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1111 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit; 1112 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC; 1113 1114#ifndef DIAGNOSTIC 1115 if (uiop->uio_iovcnt != 1) 1116 panic("nfs: writerpc iovcnt > 1"); 1117#endif 1118 *must_commit = 0; 1119 tsiz = uiop->uio_resid; 1120 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1121 return (EFBIG); 1122 while (tsiz > 0) { 1123 nfsstats.rpccnt[NFSPROC_WRITE]++; 1124 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz; 1125 nfsm_reqhead(vp, NFSPROC_WRITE, 1126 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); 1127 nfsm_fhtom(vp, v3); 1128 if (v3) { 1129 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1130 txdr_hyper(&uiop->uio_offset, tl); 1131 tl += 2; 1132 *tl++ = txdr_unsigned(len); 1133 *tl++ = txdr_unsigned(*iomode); 1134 *tl = txdr_unsigned(len); 1135 } else { 1136 register u_int32_t x; 1137 1138 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1139 /* Set both "begin" and "current" to non-garbage. */ 1140 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1141 *tl++ = x; /* "begin offset" */ 1142 *tl++ = x; /* "current offset" */ 1143 x = txdr_unsigned(len); 1144 *tl++ = x; /* total to this offset */ 1145 *tl = x; /* size of this write */ 1146 } 1147 nfsm_uiotom(uiop, len); 1148 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred); 1149 if (v3) { 1150 wccflag = NFSV3_WCCCHK; 1151 nfsm_wcc_data(vp, wccflag); 1152 if (!error) { 1153 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED 1154 + NFSX_V3WRITEVERF); 1155 rlen = fxdr_unsigned(int, *tl++); 1156 if (rlen == 0) { 1157 error = NFSERR_IO; 1158 m_freem(mrep); 1159 break; 1160 } else if (rlen < len) { 1161 backup = len - rlen; 1162 uiop->uio_iov->iov_base -= backup; 1163 uiop->uio_iov->iov_len += backup; 1164 uiop->uio_offset -= backup; 1165 uiop->uio_resid += backup; 1166 len = rlen; 1167 } 1168 commit = fxdr_unsigned(int, *tl++); 1169 1170 /* 1171 * Return the lowest committment level 1172 * obtained by any of the RPCs. 1173 */ 1174 if (committed == NFSV3WRITE_FILESYNC) 1175 committed = commit; 1176 else if (committed == NFSV3WRITE_DATASYNC && 1177 commit == NFSV3WRITE_UNSTABLE) 1178 committed = commit; 1179 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){ 1180 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1181 NFSX_V3WRITEVERF); 1182 nmp->nm_state |= NFSSTA_HASWRITEVERF; 1183 } else if (bcmp((caddr_t)tl, 1184 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) { 1185 *must_commit = 1; 1186 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 1187 NFSX_V3WRITEVERF); 1188 } 1189 } 1190 } else 1191 nfsm_loadattr(vp, (struct vattr *)0); 1192 if (wccflag) 1193 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec; 1194 m_freem(mrep); 1195 if (error) 1196 break; 1197 tsiz -= len; 1198 } 1199nfsmout: 1200 if (vp->v_mount->mnt_flag & MNT_ASYNC) 1201 committed = NFSV3WRITE_FILESYNC; 1202 *iomode = committed; 1203 if (error) 1204 uiop->uio_resid = tsiz; 1205 return (error); 1206} 1207 1208/* 1209 * nfs mknod rpc 1210 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1211 * mode set to specify the file type and the size field for rdev. 1212 */ 1213static int 1214nfs_mknodrpc(dvp, vpp, cnp, vap) 1215 register struct vnode *dvp; 1216 register struct vnode **vpp; 1217 register struct componentname *cnp; 1218 register struct vattr *vap; 1219{ 1220 register struct nfsv2_sattr *sp; 1221 register struct nfsv3_sattr *sp3; 1222 register u_int32_t *tl; 1223 register caddr_t cp; 1224 register int32_t t1, t2; 1225 struct vnode *newvp = (struct vnode *)0; 1226 struct nfsnode *np = (struct nfsnode *)0; 1227 struct vattr vattr; 1228 char *cp2; 1229 caddr_t bpos, dpos; 1230 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0; 1231 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1232 u_int32_t rdev; 1233 int v3 = NFS_ISV3(dvp); 1234 1235 if (vap->va_type == VCHR || vap->va_type == VBLK) 1236 rdev = txdr_unsigned(vap->va_rdev); 1237 else if (vap->va_type == VFIFO || vap->va_type == VSOCK) 1238 rdev = nfs_xdrneg1; 1239 else { 1240 VOP_ABORTOP(dvp, cnp); 1241 return (EOPNOTSUPP); 1242 } 1243 if (error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) { 1244 VOP_ABORTOP(dvp, cnp); 1245 return (error); 1246 } 1247 nfsstats.rpccnt[NFSPROC_MKNOD]++; 1248 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED + 1249 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1250 nfsm_fhtom(dvp, v3); 1251 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1252 if (v3) { 1253 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3SRVSATTR); 1254 *tl++ = vtonfsv3_type(vap->va_type); 1255 sp3 = (struct nfsv3_sattr *)tl; 1256 nfsm_v3sattr(sp3, vap); 1257 if (vap->va_type == VCHR || vap->va_type == VBLK) { 1258 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1259 *tl++ = txdr_unsigned(major(vap->va_rdev)); 1260 *tl = txdr_unsigned(minor(vap->va_rdev)); 1261 } 1262 } else { 1263 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1264 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1265 sp->sa_uid = nfs_xdrneg1; 1266 sp->sa_gid = nfs_xdrneg1; 1267 sp->sa_size = rdev; 1268 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1269 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1270 } 1271 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred); 1272 if (!error) { 1273 nfsm_mtofh(dvp, newvp, v3, gotvp); 1274 if (!gotvp) { 1275 if (newvp) { 1276 vput(newvp); 1277 newvp = (struct vnode *)0; 1278 } 1279 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1280 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np); 1281 if (!error) 1282 newvp = NFSTOV(np); 1283 } 1284 } 1285 if (v3) 1286 nfsm_wcc_data(dvp, wccflag); 1287 nfsm_reqdone; 1288 if (error) { 1289 if (newvp) 1290 vput(newvp); 1291 } else { 1292 if (cnp->cn_flags & MAKEENTRY) 1293 cache_enter(dvp, newvp, cnp); 1294 *vpp = newvp; 1295 } 1296 zfree(namei_zone, cnp->cn_pnbuf); 1297 VTONFS(dvp)->n_flag |= NMODIFIED; 1298 if (!wccflag) 1299 VTONFS(dvp)->n_attrstamp = 0; 1300 return (error); 1301} 1302 1303/* 1304 * nfs mknod vop 1305 * just call nfs_mknodrpc() to do the work. 1306 */ 1307/* ARGSUSED */ 1308static int 1309nfs_mknod(ap) 1310 struct vop_mknod_args /* { 1311 struct vnode *a_dvp; 1312 struct vnode **a_vpp; 1313 struct componentname *a_cnp; 1314 struct vattr *a_vap; 1315 } */ *ap; 1316{ 1317 struct vnode *newvp; 1318 int error; 1319 1320 error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap); 1321 if (!error) 1322 vput(newvp); 1323 return (error); 1324} 1325 1326static u_long create_verf; 1327/* 1328 * nfs file create call 1329 */ 1330static int 1331nfs_create(ap) 1332 struct vop_create_args /* { 1333 struct vnode *a_dvp; 1334 struct vnode **a_vpp; 1335 struct componentname *a_cnp; 1336 struct vattr *a_vap; 1337 } */ *ap; 1338{ 1339 register struct vnode *dvp = ap->a_dvp; 1340 register struct vattr *vap = ap->a_vap; 1341 register struct componentname *cnp = ap->a_cnp; 1342 register struct nfsv2_sattr *sp; 1343 register struct nfsv3_sattr *sp3; 1344 register u_int32_t *tl; 1345 register caddr_t cp; 1346 register int32_t t1, t2; 1347 struct nfsnode *np = (struct nfsnode *)0; 1348 struct vnode *newvp = (struct vnode *)0; 1349 caddr_t bpos, dpos, cp2; 1350 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0; 1351 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1352 struct vattr vattr; 1353 int v3 = NFS_ISV3(dvp); 1354 1355 /* 1356 * Oops, not for me.. 1357 */ 1358 if (vap->va_type == VSOCK) 1359 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap)); 1360 1361 if (error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) { 1362 VOP_ABORTOP(dvp, cnp); 1363 return (error); 1364 } 1365 if (vap->va_vaflags & VA_EXCLUSIVE) 1366 fmode |= O_EXCL; 1367again: 1368 nfsstats.rpccnt[NFSPROC_CREATE]++; 1369 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED + 1370 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1371 nfsm_fhtom(dvp, v3); 1372 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1373 if (v3) { 1374 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1375 if (fmode & O_EXCL) { 1376 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE); 1377 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF); 1378#ifdef INET 1379 if (!TAILQ_EMPTY(&in_ifaddrhead)) 1380 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr; 1381 else 1382#endif 1383 *tl++ = create_verf; 1384 *tl = ++create_verf; 1385 } else { 1386 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED); 1387 nfsm_build(tl, u_int32_t *, NFSX_V3SRVSATTR); 1388 sp3 = (struct nfsv3_sattr *)tl; 1389 nfsm_v3sattr(sp3, vap); 1390 } 1391 } else { 1392 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1393 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1394 sp->sa_uid = nfs_xdrneg1; 1395 sp->sa_gid = nfs_xdrneg1; 1396 sp->sa_size = 0; 1397 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1398 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1399 } 1400 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred); 1401 if (!error) { 1402 nfsm_mtofh(dvp, newvp, v3, gotvp); 1403 if (!gotvp) { 1404 if (newvp) { 1405 vput(newvp); 1406 newvp = (struct vnode *)0; 1407 } 1408 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1409 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np); 1410 if (!error) 1411 newvp = NFSTOV(np); 1412 } 1413 } 1414 if (v3) 1415 nfsm_wcc_data(dvp, wccflag); 1416 nfsm_reqdone; 1417 if (error) { 1418 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) { 1419 fmode &= ~O_EXCL; 1420 goto again; 1421 } 1422 if (newvp) 1423 vput(newvp); 1424 } else if (v3 && (fmode & O_EXCL)) 1425 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc); 1426 if (!error) { 1427 if (cnp->cn_flags & MAKEENTRY) 1428 cache_enter(dvp, newvp, cnp); 1429 *ap->a_vpp = newvp; 1430 } 1431 zfree(namei_zone, cnp->cn_pnbuf); 1432 VTONFS(dvp)->n_flag |= NMODIFIED; 1433 if (!wccflag) 1434 VTONFS(dvp)->n_attrstamp = 0; 1435 return (error); 1436} 1437 1438/* 1439 * nfs file remove call 1440 * To try and make nfs semantics closer to ufs semantics, a file that has 1441 * other processes using the vnode is renamed instead of removed and then 1442 * removed later on the last close. 1443 * - If v_usecount > 1 1444 * If a rename is not already in the works 1445 * call nfs_sillyrename() to set it up 1446 * else 1447 * do the remove rpc 1448 */ 1449static int 1450nfs_remove(ap) 1451 struct vop_remove_args /* { 1452 struct vnodeop_desc *a_desc; 1453 struct vnode * a_dvp; 1454 struct vnode * a_vp; 1455 struct componentname * a_cnp; 1456 } */ *ap; 1457{ 1458 register struct vnode *vp = ap->a_vp; 1459 register struct vnode *dvp = ap->a_dvp; 1460 register struct componentname *cnp = ap->a_cnp; 1461 register struct nfsnode *np = VTONFS(vp); 1462 int error = 0; 1463 struct vattr vattr; 1464 1465#ifndef DIAGNOSTIC 1466 if ((cnp->cn_flags & HASBUF) == 0) 1467 panic("nfs_remove: no name"); 1468 if (vp->v_usecount < 1) 1469 panic("nfs_remove: bad v_usecount"); 1470#endif 1471 if (vp->v_type == VDIR) 1472 error = EPERM; 1473 else if (vp->v_usecount == 1 || (np->n_sillyrename && 1474 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 && 1475 vattr.va_nlink > 1)) { 1476 /* 1477 * Purge the name cache so that the chance of a lookup for 1478 * the name succeeding while the remove is in progress is 1479 * minimized. Without node locking it can still happen, such 1480 * that an I/O op returns ESTALE, but since you get this if 1481 * another host removes the file.. 1482 */ 1483 cache_purge(vp); 1484 /* 1485 * throw away biocache buffers, mainly to avoid 1486 * unnecessary delayed writes later. 1487 */ 1488 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1); 1489 /* Do the rpc */ 1490 if (error != EINTR) 1491 error = nfs_removerpc(dvp, cnp->cn_nameptr, 1492 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc); 1493 /* 1494 * Kludge City: If the first reply to the remove rpc is lost.. 1495 * the reply to the retransmitted request will be ENOENT 1496 * since the file was in fact removed 1497 * Therefore, we cheat and return success. 1498 */ 1499 if (error == ENOENT) 1500 error = 0; 1501 } else if (!np->n_sillyrename) 1502 error = nfs_sillyrename(dvp, vp, cnp); 1503 zfree(namei_zone, cnp->cn_pnbuf); 1504 np->n_attrstamp = 0; 1505 return (error); 1506} 1507 1508/* 1509 * nfs file remove rpc called from nfs_inactive 1510 */ 1511int 1512nfs_removeit(sp) 1513 register struct sillyrename *sp; 1514{ 1515 1516 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, 1517 (struct proc *)0)); 1518} 1519 1520/* 1521 * Nfs remove rpc, called from nfs_remove() and nfs_removeit(). 1522 */ 1523static int 1524nfs_removerpc(dvp, name, namelen, cred, proc) 1525 register struct vnode *dvp; 1526 const char *name; 1527 int namelen; 1528 struct ucred *cred; 1529 struct proc *proc; 1530{ 1531 register u_int32_t *tl; 1532 register caddr_t cp; 1533 register int32_t t1, t2; 1534 caddr_t bpos, dpos, cp2; 1535 int error = 0, wccflag = NFSV3_WCCRATTR; 1536 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1537 int v3 = NFS_ISV3(dvp); 1538 1539 nfsstats.rpccnt[NFSPROC_REMOVE]++; 1540 nfsm_reqhead(dvp, NFSPROC_REMOVE, 1541 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen)); 1542 nfsm_fhtom(dvp, v3); 1543 nfsm_strtom(name, namelen, NFS_MAXNAMLEN); 1544 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred); 1545 if (v3) 1546 nfsm_wcc_data(dvp, wccflag); 1547 nfsm_reqdone; 1548 VTONFS(dvp)->n_flag |= NMODIFIED; 1549 if (!wccflag) 1550 VTONFS(dvp)->n_attrstamp = 0; 1551 return (error); 1552} 1553 1554/* 1555 * nfs file rename call 1556 */ 1557static int 1558nfs_rename(ap) 1559 struct vop_rename_args /* { 1560 struct vnode *a_fdvp; 1561 struct vnode *a_fvp; 1562 struct componentname *a_fcnp; 1563 struct vnode *a_tdvp; 1564 struct vnode *a_tvp; 1565 struct componentname *a_tcnp; 1566 } */ *ap; 1567{ 1568 register struct vnode *fvp = ap->a_fvp; 1569 register struct vnode *tvp = ap->a_tvp; 1570 register struct vnode *fdvp = ap->a_fdvp; 1571 register struct vnode *tdvp = ap->a_tdvp; 1572 register struct componentname *tcnp = ap->a_tcnp; 1573 register struct componentname *fcnp = ap->a_fcnp; 1574 int error; 1575 1576#ifndef DIAGNOSTIC 1577 if ((tcnp->cn_flags & HASBUF) == 0 || 1578 (fcnp->cn_flags & HASBUF) == 0) 1579 panic("nfs_rename: no name"); 1580#endif 1581 /* Check for cross-device rename */ 1582 if ((fvp->v_mount != tdvp->v_mount) || 1583 (tvp && (fvp->v_mount != tvp->v_mount))) { 1584 error = EXDEV; 1585 goto out; 1586 } 1587 1588 /* 1589 * If the tvp exists and is in use, sillyrename it before doing the 1590 * rename of the new file over it. 1591 * XXX Can't sillyrename a directory. 1592 */ 1593 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename && 1594 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { 1595 vput(tvp); 1596 tvp = NULL; 1597 } 1598 1599 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen, 1600 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, 1601 tcnp->cn_proc); 1602 1603 if (fvp->v_type == VDIR) { 1604 if (tvp != NULL && tvp->v_type == VDIR) 1605 cache_purge(tdvp); 1606 cache_purge(fdvp); 1607 } 1608out: 1609 if (tdvp == tvp) 1610 vrele(tdvp); 1611 else 1612 vput(tdvp); 1613 if (tvp) 1614 vput(tvp); 1615 vrele(fdvp); 1616 vrele(fvp); 1617 /* 1618 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. 1619 */ 1620 if (error == ENOENT) 1621 error = 0; 1622 return (error); 1623} 1624 1625/* 1626 * nfs file rename rpc called from nfs_remove() above 1627 */ 1628static int 1629nfs_renameit(sdvp, scnp, sp) 1630 struct vnode *sdvp; 1631 struct componentname *scnp; 1632 register struct sillyrename *sp; 1633{ 1634 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, 1635 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc)); 1636} 1637 1638/* 1639 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit(). 1640 */ 1641static int 1642nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc) 1643 register struct vnode *fdvp; 1644 const char *fnameptr; 1645 int fnamelen; 1646 register struct vnode *tdvp; 1647 const char *tnameptr; 1648 int tnamelen; 1649 struct ucred *cred; 1650 struct proc *proc; 1651{ 1652 register u_int32_t *tl; 1653 register caddr_t cp; 1654 register int32_t t1, t2; 1655 caddr_t bpos, dpos, cp2; 1656 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR; 1657 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1658 int v3 = NFS_ISV3(fdvp); 1659 1660 nfsstats.rpccnt[NFSPROC_RENAME]++; 1661 nfsm_reqhead(fdvp, NFSPROC_RENAME, 1662 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) + 1663 nfsm_rndup(tnamelen)); 1664 nfsm_fhtom(fdvp, v3); 1665 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN); 1666 nfsm_fhtom(tdvp, v3); 1667 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN); 1668 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred); 1669 if (v3) { 1670 nfsm_wcc_data(fdvp, fwccflag); 1671 nfsm_wcc_data(tdvp, twccflag); 1672 } 1673 nfsm_reqdone; 1674 VTONFS(fdvp)->n_flag |= NMODIFIED; 1675 VTONFS(tdvp)->n_flag |= NMODIFIED; 1676 if (!fwccflag) 1677 VTONFS(fdvp)->n_attrstamp = 0; 1678 if (!twccflag) 1679 VTONFS(tdvp)->n_attrstamp = 0; 1680 return (error); 1681} 1682 1683/* 1684 * nfs hard link create call 1685 */ 1686static int 1687nfs_link(ap) 1688 struct vop_link_args /* { 1689 struct vnode *a_tdvp; 1690 struct vnode *a_vp; 1691 struct componentname *a_cnp; 1692 } */ *ap; 1693{ 1694 register struct vnode *vp = ap->a_vp; 1695 register struct vnode *tdvp = ap->a_tdvp; 1696 register struct componentname *cnp = ap->a_cnp; 1697 register u_int32_t *tl; 1698 register caddr_t cp; 1699 register int32_t t1, t2; 1700 caddr_t bpos, dpos, cp2; 1701 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0; 1702 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1703 int v3; 1704 1705 if (vp->v_mount != tdvp->v_mount) { 1706 VOP_ABORTOP(tdvp, cnp); 1707 vput(tdvp); 1708 return (EXDEV); 1709 } 1710 1711 /* 1712 * Push all writes to the server, so that the attribute cache 1713 * doesn't get "out of sync" with the server. 1714 * XXX There should be a better way! 1715 */ 1716 VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_proc); 1717 1718 v3 = NFS_ISV3(vp); 1719 nfsstats.rpccnt[NFSPROC_LINK]++; 1720 nfsm_reqhead(vp, NFSPROC_LINK, 1721 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 1722 nfsm_fhtom(vp, v3); 1723 nfsm_fhtom(tdvp, v3); 1724 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1725 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred); 1726 if (v3) { 1727 nfsm_postop_attr(vp, attrflag); 1728 nfsm_wcc_data(tdvp, wccflag); 1729 } 1730 nfsm_reqdone; 1731 zfree(namei_zone, cnp->cn_pnbuf); 1732 VTONFS(tdvp)->n_flag |= NMODIFIED; 1733 if (!attrflag) 1734 VTONFS(vp)->n_attrstamp = 0; 1735 if (!wccflag) 1736 VTONFS(tdvp)->n_attrstamp = 0; 1737 /* 1738 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 1739 */ 1740 if (error == EEXIST) 1741 error = 0; 1742 return (error); 1743} 1744 1745/* 1746 * nfs symbolic link create call 1747 */ 1748static int 1749nfs_symlink(ap) 1750 struct vop_symlink_args /* { 1751 struct vnode *a_dvp; 1752 struct vnode **a_vpp; 1753 struct componentname *a_cnp; 1754 struct vattr *a_vap; 1755 char *a_target; 1756 } */ *ap; 1757{ 1758 register struct vnode *dvp = ap->a_dvp; 1759 register struct vattr *vap = ap->a_vap; 1760 register struct componentname *cnp = ap->a_cnp; 1761 register struct nfsv2_sattr *sp; 1762 register struct nfsv3_sattr *sp3; 1763 register u_int32_t *tl; 1764 register caddr_t cp; 1765 register int32_t t1, t2; 1766 caddr_t bpos, dpos, cp2; 1767 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp; 1768 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1769 struct vnode *newvp = (struct vnode *)0; 1770 int v3 = NFS_ISV3(dvp); 1771 1772 nfsstats.rpccnt[NFSPROC_SYMLINK]++; 1773 slen = strlen(ap->a_target); 1774 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED + 1775 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3)); 1776 nfsm_fhtom(dvp, v3); 1777 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1778 if (v3) { 1779 nfsm_build(sp3, struct nfsv3_sattr *, NFSX_V3SRVSATTR); 1780 nfsm_v3sattr(sp3, vap); 1781 } 1782 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN); 1783 if (!v3) { 1784 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1785 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode); 1786 sp->sa_uid = nfs_xdrneg1; 1787 sp->sa_gid = nfs_xdrneg1; 1788 sp->sa_size = nfs_xdrneg1; 1789 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1790 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1791 } 1792 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred); 1793 if (v3) { 1794 if (!error) 1795 nfsm_mtofh(dvp, newvp, v3, gotvp); 1796 nfsm_wcc_data(dvp, wccflag); 1797 } 1798 nfsm_reqdone; 1799 if (newvp) 1800 vput(newvp); 1801 zfree(namei_zone, cnp->cn_pnbuf); 1802 VTONFS(dvp)->n_flag |= NMODIFIED; 1803 if (!wccflag) 1804 VTONFS(dvp)->n_attrstamp = 0; 1805 /* 1806 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 1807 */ 1808 if (error == EEXIST) 1809 error = 0; 1810 return (error); 1811} 1812 1813/* 1814 * nfs make dir call 1815 */ 1816static int 1817nfs_mkdir(ap) 1818 struct vop_mkdir_args /* { 1819 struct vnode *a_dvp; 1820 struct vnode **a_vpp; 1821 struct componentname *a_cnp; 1822 struct vattr *a_vap; 1823 } */ *ap; 1824{ 1825 register struct vnode *dvp = ap->a_dvp; 1826 register struct vattr *vap = ap->a_vap; 1827 register struct componentname *cnp = ap->a_cnp; 1828 register struct nfsv2_sattr *sp; 1829 register struct nfsv3_sattr *sp3; 1830 register u_int32_t *tl; 1831 register caddr_t cp; 1832 register int32_t t1, t2; 1833 register int len; 1834 struct nfsnode *np = (struct nfsnode *)0; 1835 struct vnode *newvp = (struct vnode *)0; 1836 caddr_t bpos, dpos, cp2; 1837 int error = 0, wccflag = NFSV3_WCCRATTR; 1838 int gotvp = 0; 1839 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1840 struct vattr vattr; 1841 int v3 = NFS_ISV3(dvp); 1842 1843 if (error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) { 1844 VOP_ABORTOP(dvp, cnp); 1845 return (error); 1846 } 1847 len = cnp->cn_namelen; 1848 nfsstats.rpccnt[NFSPROC_MKDIR]++; 1849 nfsm_reqhead(dvp, NFSPROC_MKDIR, 1850 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3)); 1851 nfsm_fhtom(dvp, v3); 1852 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 1853 if (v3) { 1854 nfsm_build(sp3, struct nfsv3_sattr *, NFSX_V3SRVSATTR); 1855 nfsm_v3sattr(sp3, vap); 1856 } else { 1857 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1858 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode); 1859 sp->sa_uid = nfs_xdrneg1; 1860 sp->sa_gid = nfs_xdrneg1; 1861 sp->sa_size = nfs_xdrneg1; 1862 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1863 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1864 } 1865 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred); 1866 if (!error) 1867 nfsm_mtofh(dvp, newvp, v3, gotvp); 1868 if (v3) 1869 nfsm_wcc_data(dvp, wccflag); 1870 nfsm_reqdone; 1871 VTONFS(dvp)->n_flag |= NMODIFIED; 1872 if (!wccflag) 1873 VTONFS(dvp)->n_attrstamp = 0; 1874 /* 1875 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry 1876 * if we can succeed in looking up the directory. 1877 */ 1878 if (error == EEXIST || (!error && !gotvp)) { 1879 if (newvp) { 1880 vrele(newvp); 1881 newvp = (struct vnode *)0; 1882 } 1883 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred, 1884 cnp->cn_proc, &np); 1885 if (!error) { 1886 newvp = NFSTOV(np); 1887 if (newvp->v_type != VDIR) 1888 error = EEXIST; 1889 } 1890 } 1891 if (error) { 1892 if (newvp) 1893 vrele(newvp); 1894 } else 1895 *ap->a_vpp = newvp; 1896 zfree(namei_zone, cnp->cn_pnbuf); 1897 return (error); 1898} 1899 1900/* 1901 * nfs remove directory call 1902 */ 1903static int 1904nfs_rmdir(ap) 1905 struct vop_rmdir_args /* { 1906 struct vnode *a_dvp; 1907 struct vnode *a_vp; 1908 struct componentname *a_cnp; 1909 } */ *ap; 1910{ 1911 register struct vnode *vp = ap->a_vp; 1912 register struct vnode *dvp = ap->a_dvp; 1913 register struct componentname *cnp = ap->a_cnp; 1914 register u_int32_t *tl; 1915 register caddr_t cp; 1916 register int32_t t1, t2; 1917 caddr_t bpos, dpos, cp2; 1918 int error = 0, wccflag = NFSV3_WCCRATTR; 1919 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1920 int v3 = NFS_ISV3(dvp); 1921 1922 if (dvp == vp) 1923 return (EINVAL); 1924 nfsstats.rpccnt[NFSPROC_RMDIR]++; 1925 nfsm_reqhead(dvp, NFSPROC_RMDIR, 1926 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 1927 nfsm_fhtom(dvp, v3); 1928 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1929 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred); 1930 if (v3) 1931 nfsm_wcc_data(dvp, wccflag); 1932 nfsm_reqdone; 1933 zfree(namei_zone, cnp->cn_pnbuf); 1934 VTONFS(dvp)->n_flag |= NMODIFIED; 1935 if (!wccflag) 1936 VTONFS(dvp)->n_attrstamp = 0; 1937 cache_purge(dvp); 1938 cache_purge(vp); 1939 /* 1940 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 1941 */ 1942 if (error == ENOENT) 1943 error = 0; 1944 return (error); 1945} 1946 1947/* 1948 * nfs readdir call 1949 */ 1950static int 1951nfs_readdir(ap) 1952 struct vop_readdir_args /* { 1953 struct vnode *a_vp; 1954 struct uio *a_uio; 1955 struct ucred *a_cred; 1956 } */ *ap; 1957{ 1958 register struct vnode *vp = ap->a_vp; 1959 register struct nfsnode *np = VTONFS(vp); 1960 register struct uio *uio = ap->a_uio; 1961 int tresid, error; 1962 struct vattr vattr; 1963 1964 if (vp->v_type != VDIR) 1965 return (EPERM); 1966 /* 1967 * First, check for hit on the EOF offset cache 1968 */ 1969 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset && 1970 (np->n_flag & NMODIFIED) == 0) { 1971 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) { 1972 if (NQNFS_CKCACHABLE(vp, ND_READ)) { 1973 nfsstats.direofcache_hits++; 1974 return (0); 1975 } 1976 } else if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_procp) == 0 && 1977 np->n_mtime == vattr.va_mtime.tv_sec) { 1978 nfsstats.direofcache_hits++; 1979 return (0); 1980 } 1981 } 1982 1983 /* 1984 * Call nfs_bioread() to do the real work. 1985 */ 1986 tresid = uio->uio_resid; 1987 error = nfs_bioread(vp, uio, 0, ap->a_cred, 0); 1988 1989 if (!error && uio->uio_resid == tresid) 1990 nfsstats.direofcache_misses++; 1991 return (error); 1992} 1993 1994/* 1995 * Readdir rpc call. 1996 * Called from below the buffer cache by nfs_doio(). 1997 */ 1998int 1999nfs_readdirrpc(vp, uiop, cred) 2000 struct vnode *vp; 2001 register struct uio *uiop; 2002 struct ucred *cred; 2003 2004{ 2005 register int len, left; 2006 register struct dirent *dp; 2007 register u_int32_t *tl; 2008 register caddr_t cp; 2009 register int32_t t1, t2; 2010 register nfsuint64 *cookiep; 2011 caddr_t bpos, dpos, cp2; 2012 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2013 nfsuint64 cookie; 2014 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2015 struct nfsnode *dnp = VTONFS(vp); 2016 u_quad_t fileno; 2017 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2018 int attrflag; 2019 int v3 = NFS_ISV3(vp); 2020 2021#ifndef nolint 2022 dp = (struct dirent *)0; 2023#endif 2024#ifndef DIAGNOSTIC 2025 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (NFS_DIRBLKSIZ - 1)) || 2026 (uiop->uio_resid & (NFS_DIRBLKSIZ - 1))) 2027 panic("nfs readdirrpc bad uio"); 2028#endif 2029 2030 /* 2031 * If there is no cookie, assume directory was stale. 2032 */ 2033 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 2034 if (cookiep) 2035 cookie = *cookiep; 2036 else 2037 return (NFSERR_BAD_COOKIE); 2038 /* 2039 * Loop around doing readdir rpc's of size nm_readdirsize 2040 * truncated to a multiple of DIRBLKSIZ. 2041 * The stopping criteria is EOF or buffer full. 2042 */ 2043 while (more_dirs && bigenough) { 2044 nfsstats.rpccnt[NFSPROC_READDIR]++; 2045 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) + 2046 NFSX_READDIR(v3)); 2047 nfsm_fhtom(vp, v3); 2048 if (v3) { 2049 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2050 *tl++ = cookie.nfsuquad[0]; 2051 *tl++ = cookie.nfsuquad[1]; 2052 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2053 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2054 } else { 2055 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2056 *tl++ = cookie.nfsuquad[0]; 2057 } 2058 *tl = txdr_unsigned(nmp->nm_readdirsize); 2059 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred); 2060 if (v3) { 2061 nfsm_postop_attr(vp, attrflag); 2062 if (!error) { 2063 nfsm_dissect(tl, u_int32_t *, 2064 2 * NFSX_UNSIGNED); 2065 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2066 dnp->n_cookieverf.nfsuquad[1] = *tl; 2067 } else { 2068 m_freem(mrep); 2069 goto nfsmout; 2070 } 2071 } 2072 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2073 more_dirs = fxdr_unsigned(int, *tl); 2074 2075 /* loop thru the dir entries, doctoring them to 4bsd form */ 2076 while (more_dirs && bigenough) { 2077 if (v3) { 2078 nfsm_dissect(tl, u_int32_t *, 2079 3 * NFSX_UNSIGNED); 2080 fxdr_hyper(tl, &fileno); 2081 len = fxdr_unsigned(int, *(tl + 2)); 2082 } else { 2083 nfsm_dissect(tl, u_int32_t *, 2084 2 * NFSX_UNSIGNED); 2085 fileno = fxdr_unsigned(u_quad_t, *tl++); 2086 len = fxdr_unsigned(int, *tl); 2087 } 2088 if (len <= 0 || len > NFS_MAXNAMLEN) { 2089 error = EBADRPC; 2090 m_freem(mrep); 2091 goto nfsmout; 2092 } 2093 tlen = nfsm_rndup(len); 2094 if (tlen == len) 2095 tlen += 4; /* To ensure null termination */ 2096 left = DIRBLKSIZ - blksiz; 2097 if ((tlen + DIRHDSIZ) > left) { 2098 dp->d_reclen += left; 2099 uiop->uio_iov->iov_base += left; 2100 uiop->uio_iov->iov_len -= left; 2101 uiop->uio_offset += left; 2102 uiop->uio_resid -= left; 2103 blksiz = 0; 2104 } 2105 if ((tlen + DIRHDSIZ) > uiop->uio_resid) 2106 bigenough = 0; 2107 if (bigenough) { 2108 dp = (struct dirent *)uiop->uio_iov->iov_base; 2109 dp->d_fileno = (int)fileno; 2110 dp->d_namlen = len; 2111 dp->d_reclen = tlen + DIRHDSIZ; 2112 dp->d_type = DT_UNKNOWN; 2113 blksiz += dp->d_reclen; 2114 if (blksiz == DIRBLKSIZ) 2115 blksiz = 0; 2116 uiop->uio_offset += DIRHDSIZ; 2117 uiop->uio_resid -= DIRHDSIZ; 2118 uiop->uio_iov->iov_base += DIRHDSIZ; 2119 uiop->uio_iov->iov_len -= DIRHDSIZ; 2120 nfsm_mtouio(uiop, len); 2121 cp = uiop->uio_iov->iov_base; 2122 tlen -= len; 2123 *cp = '\0'; /* null terminate */ 2124 uiop->uio_iov->iov_base += tlen; 2125 uiop->uio_iov->iov_len -= tlen; 2126 uiop->uio_offset += tlen; 2127 uiop->uio_resid -= tlen; 2128 } else 2129 nfsm_adv(nfsm_rndup(len)); 2130 if (v3) { 2131 nfsm_dissect(tl, u_int32_t *, 2132 3 * NFSX_UNSIGNED); 2133 } else { 2134 nfsm_dissect(tl, u_int32_t *, 2135 2 * NFSX_UNSIGNED); 2136 } 2137 if (bigenough) { 2138 cookie.nfsuquad[0] = *tl++; 2139 if (v3) 2140 cookie.nfsuquad[1] = *tl++; 2141 } else if (v3) 2142 tl += 2; 2143 else 2144 tl++; 2145 more_dirs = fxdr_unsigned(int, *tl); 2146 } 2147 /* 2148 * If at end of rpc data, get the eof boolean 2149 */ 2150 if (!more_dirs) { 2151 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2152 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2153 } 2154 m_freem(mrep); 2155 } 2156 /* 2157 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2158 * by increasing d_reclen for the last record. 2159 */ 2160 if (blksiz > 0) { 2161 left = DIRBLKSIZ - blksiz; 2162 dp->d_reclen += left; 2163 uiop->uio_iov->iov_base += left; 2164 uiop->uio_iov->iov_len -= left; 2165 uiop->uio_offset += left; 2166 uiop->uio_resid -= left; 2167 } 2168 2169 /* 2170 * We are now either at the end of the directory or have filled the 2171 * block. 2172 */ 2173 if (bigenough) 2174 dnp->n_direofoffset = uiop->uio_offset; 2175 else { 2176 if (uiop->uio_resid > 0) 2177 printf("EEK! readdirrpc resid > 0\n"); 2178 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2179 *cookiep = cookie; 2180 } 2181nfsmout: 2182 return (error); 2183} 2184 2185/* 2186 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc(). 2187 */ 2188int 2189nfs_readdirplusrpc(vp, uiop, cred) 2190 struct vnode *vp; 2191 register struct uio *uiop; 2192 struct ucred *cred; 2193{ 2194 register int len, left; 2195 register struct dirent *dp; 2196 register u_int32_t *tl; 2197 register caddr_t cp; 2198 register int32_t t1, t2; 2199 register struct vnode *newvp; 2200 register nfsuint64 *cookiep; 2201 caddr_t bpos, dpos, cp2, dpossav1, dpossav2; 2202 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2; 2203 struct nameidata nami, *ndp = &nami; 2204 struct componentname *cnp = &ndp->ni_cnd; 2205 nfsuint64 cookie; 2206 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2207 struct nfsnode *dnp = VTONFS(vp), *np; 2208 nfsfh_t *fhp; 2209 u_quad_t fileno; 2210 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i; 2211 int attrflag, fhsize; 2212 2213#ifndef nolint 2214 dp = (struct dirent *)0; 2215#endif 2216#ifndef DIAGNOSTIC 2217 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) || 2218 (uiop->uio_resid & (DIRBLKSIZ - 1))) 2219 panic("nfs readdirplusrpc bad uio"); 2220#endif 2221 ndp->ni_dvp = vp; 2222 newvp = NULLVP; 2223 2224 /* 2225 * If there is no cookie, assume directory was stale. 2226 */ 2227 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0); 2228 if (cookiep) 2229 cookie = *cookiep; 2230 else 2231 return (NFSERR_BAD_COOKIE); 2232 /* 2233 * Loop around doing readdir rpc's of size nm_readdirsize 2234 * truncated to a multiple of DIRBLKSIZ. 2235 * The stopping criteria is EOF or buffer full. 2236 */ 2237 while (more_dirs && bigenough) { 2238 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++; 2239 nfsm_reqhead(vp, NFSPROC_READDIRPLUS, 2240 NFSX_FH(1) + 6 * NFSX_UNSIGNED); 2241 nfsm_fhtom(vp, 1); 2242 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2243 *tl++ = cookie.nfsuquad[0]; 2244 *tl++ = cookie.nfsuquad[1]; 2245 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2246 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2247 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 2248 *tl = txdr_unsigned(nmp->nm_rsize); 2249 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred); 2250 nfsm_postop_attr(vp, attrflag); 2251 if (error) { 2252 m_freem(mrep); 2253 goto nfsmout; 2254 } 2255 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2256 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2257 dnp->n_cookieverf.nfsuquad[1] = *tl++; 2258 more_dirs = fxdr_unsigned(int, *tl); 2259 2260 /* loop thru the dir entries, doctoring them to 4bsd form */ 2261 while (more_dirs && bigenough) { 2262 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2263 fxdr_hyper(tl, &fileno); 2264 len = fxdr_unsigned(int, *(tl + 2)); 2265 if (len <= 0 || len > NFS_MAXNAMLEN) { 2266 error = EBADRPC; 2267 m_freem(mrep); 2268 goto nfsmout; 2269 } 2270 tlen = nfsm_rndup(len); 2271 if (tlen == len) 2272 tlen += 4; /* To ensure null termination*/ 2273 left = DIRBLKSIZ - blksiz; 2274 if ((tlen + DIRHDSIZ) > left) { 2275 dp->d_reclen += left; 2276 uiop->uio_iov->iov_base += left; 2277 uiop->uio_iov->iov_len -= left; 2278 uiop->uio_offset += left; 2279 uiop->uio_resid -= left; 2280 blksiz = 0; 2281 } 2282 if ((tlen + DIRHDSIZ) > uiop->uio_resid) 2283 bigenough = 0; 2284 if (bigenough) { 2285 dp = (struct dirent *)uiop->uio_iov->iov_base; 2286 dp->d_fileno = (int)fileno; 2287 dp->d_namlen = len; 2288 dp->d_reclen = tlen + DIRHDSIZ; 2289 dp->d_type = DT_UNKNOWN; 2290 blksiz += dp->d_reclen; 2291 if (blksiz == DIRBLKSIZ) 2292 blksiz = 0; 2293 uiop->uio_offset += DIRHDSIZ; 2294 uiop->uio_resid -= DIRHDSIZ; 2295 uiop->uio_iov->iov_base += DIRHDSIZ; 2296 uiop->uio_iov->iov_len -= DIRHDSIZ; 2297 cnp->cn_nameptr = uiop->uio_iov->iov_base; 2298 cnp->cn_namelen = len; 2299 nfsm_mtouio(uiop, len); 2300 cp = uiop->uio_iov->iov_base; 2301 tlen -= len; 2302 *cp = '\0'; 2303 uiop->uio_iov->iov_base += tlen; 2304 uiop->uio_iov->iov_len -= tlen; 2305 uiop->uio_offset += tlen; 2306 uiop->uio_resid -= tlen; 2307 } else 2308 nfsm_adv(nfsm_rndup(len)); 2309 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2310 if (bigenough) { 2311 cookie.nfsuquad[0] = *tl++; 2312 cookie.nfsuquad[1] = *tl++; 2313 } else 2314 tl += 2; 2315 2316 /* 2317 * Since the attributes are before the file handle 2318 * (sigh), we must skip over the attributes and then 2319 * come back and get them. 2320 */ 2321 attrflag = fxdr_unsigned(int, *tl); 2322 if (attrflag) { 2323 dpossav1 = dpos; 2324 mdsav1 = md; 2325 nfsm_adv(NFSX_V3FATTR); 2326 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2327 doit = fxdr_unsigned(int, *tl); 2328 if (doit) { 2329 nfsm_getfh(fhp, fhsize, 1); 2330 if (NFS_CMPFH(dnp, fhp, fhsize)) { 2331 VREF(vp); 2332 newvp = vp; 2333 np = dnp; 2334 } else { 2335 error = nfs_nget(vp->v_mount, fhp, 2336 fhsize, &np); 2337 if (error) 2338 doit = 0; 2339 else 2340 newvp = NFSTOV(np); 2341 } 2342 } 2343 if (doit) { 2344 dpossav2 = dpos; 2345 dpos = dpossav1; 2346 mdsav2 = md; 2347 md = mdsav1; 2348 nfsm_loadattr(newvp, (struct vattr *)0); 2349 dpos = dpossav2; 2350 md = mdsav2; 2351 dp->d_type = 2352 IFTODT(VTTOIF(np->n_vattr.va_type)); 2353 ndp->ni_vp = newvp; 2354 cnp->cn_hash = 0; 2355 for (cp = cnp->cn_nameptr, i = 1; i <= len; 2356 i++, cp++) 2357 cnp->cn_hash += (unsigned char)*cp * i; 2358 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp); 2359 } 2360 } else { 2361 /* Just skip over the file handle */ 2362 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2363 i = fxdr_unsigned(int, *tl); 2364 nfsm_adv(nfsm_rndup(i)); 2365 } 2366 if (newvp != NULLVP) { 2367 vrele(newvp); 2368 newvp = NULLVP; 2369 } 2370 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2371 more_dirs = fxdr_unsigned(int, *tl); 2372 } 2373 /* 2374 * If at end of rpc data, get the eof boolean 2375 */ 2376 if (!more_dirs) { 2377 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2378 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2379 } 2380 m_freem(mrep); 2381 } 2382 /* 2383 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ 2384 * by increasing d_reclen for the last record. 2385 */ 2386 if (blksiz > 0) { 2387 left = DIRBLKSIZ - blksiz; 2388 dp->d_reclen += left; 2389 uiop->uio_iov->iov_base += left; 2390 uiop->uio_iov->iov_len -= left; 2391 uiop->uio_offset += left; 2392 uiop->uio_resid -= left; 2393 } 2394 2395 /* 2396 * We are now either at the end of the directory or have filled the 2397 * block. 2398 */ 2399 if (bigenough) 2400 dnp->n_direofoffset = uiop->uio_offset; 2401 else { 2402 if (uiop->uio_resid > 0) 2403 printf("EEK! readdirplusrpc resid > 0\n"); 2404 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1); 2405 *cookiep = cookie; 2406 } 2407nfsmout: 2408 if (newvp != NULLVP) { 2409 if (newvp == vp) 2410 vrele(newvp); 2411 else 2412 vput(newvp); 2413 newvp = NULLVP; 2414 } 2415 return (error); 2416} 2417 2418/* 2419 * Silly rename. To make the NFS filesystem that is stateless look a little 2420 * more like the "ufs" a remove of an active vnode is translated to a rename 2421 * to a funny looking filename that is removed by nfs_inactive on the 2422 * nfsnode. There is the potential for another process on a different client 2423 * to create the same funny name between the nfs_lookitup() fails and the 2424 * nfs_rename() completes, but... 2425 */ 2426static int 2427nfs_sillyrename(dvp, vp, cnp) 2428 struct vnode *dvp, *vp; 2429 struct componentname *cnp; 2430{ 2431 register struct sillyrename *sp; 2432 struct nfsnode *np; 2433 int error; 2434 short pid; 2435 2436 cache_purge(dvp); 2437 np = VTONFS(vp); 2438#ifndef DIAGNOSTIC 2439 if (vp->v_type == VDIR) 2440 panic("nfs: sillyrename dir"); 2441#endif 2442 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), 2443 M_NFSREQ, M_WAITOK); 2444 sp->s_cred = crdup(cnp->cn_cred); 2445 sp->s_dvp = dvp; 2446 VREF(dvp); 2447 2448 /* Fudge together a funny name */ 2449 pid = cnp->cn_proc->p_pid; 2450 sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid); 2451 2452 /* Try lookitups until we get one that isn't there */ 2453 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2454 cnp->cn_proc, (struct nfsnode **)0) == 0) { 2455 sp->s_name[4]++; 2456 if (sp->s_name[4] > 'z') { 2457 error = EINVAL; 2458 goto bad; 2459 } 2460 } 2461 error = nfs_renameit(dvp, cnp, sp); 2462 if (error) 2463 goto bad; 2464 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2465 cnp->cn_proc, &np); 2466 np->n_sillyrename = sp; 2467 return (0); 2468bad: 2469 vrele(sp->s_dvp); 2470 crfree(sp->s_cred); 2471 free((caddr_t)sp, M_NFSREQ); 2472 return (error); 2473} 2474 2475/* 2476 * Look up a file name and optionally either update the file handle or 2477 * allocate an nfsnode, depending on the value of npp. 2478 * npp == NULL --> just do the lookup 2479 * *npp == NULL --> allocate a new nfsnode and make sure attributes are 2480 * handled too 2481 * *npp != NULL --> update the file handle in the vnode 2482 */ 2483static int 2484nfs_lookitup(dvp, name, len, cred, procp, npp) 2485 register struct vnode *dvp; 2486 const char *name; 2487 int len; 2488 struct ucred *cred; 2489 struct proc *procp; 2490 struct nfsnode **npp; 2491{ 2492 register u_int32_t *tl; 2493 register caddr_t cp; 2494 register int32_t t1, t2; 2495 struct vnode *newvp = (struct vnode *)0; 2496 struct nfsnode *np, *dnp = VTONFS(dvp); 2497 caddr_t bpos, dpos, cp2; 2498 int error = 0, fhlen, attrflag; 2499 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2500 nfsfh_t *nfhp; 2501 int v3 = NFS_ISV3(dvp); 2502 2503 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 2504 nfsm_reqhead(dvp, NFSPROC_LOOKUP, 2505 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 2506 nfsm_fhtom(dvp, v3); 2507 nfsm_strtom(name, len, NFS_MAXNAMLEN); 2508 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred); 2509 if (npp && !error) { 2510 nfsm_getfh(nfhp, fhlen, v3); 2511 if (*npp) { 2512 np = *npp; 2513 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) { 2514 free((caddr_t)np->n_fhp, M_NFSBIGFH); 2515 np->n_fhp = &np->n_fh; 2516 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH) 2517 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK); 2518 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen); 2519 np->n_fhsize = fhlen; 2520 newvp = NFSTOV(np); 2521 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) { 2522 VREF(dvp); 2523 newvp = dvp; 2524 } else { 2525 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np); 2526 if (error) { 2527 m_freem(mrep); 2528 return (error); 2529 } 2530 newvp = NFSTOV(np); 2531 } 2532 if (v3) { 2533 nfsm_postop_attr(newvp, attrflag); 2534 if (!attrflag && *npp == NULL) { 2535 m_freem(mrep); 2536 if (newvp == dvp) 2537 vrele(newvp); 2538 else 2539 vput(newvp); 2540 return (ENOENT); 2541 } 2542 } else 2543 nfsm_loadattr(newvp, (struct vattr *)0); 2544 } 2545 nfsm_reqdone; 2546 if (npp && *npp == NULL) { 2547 if (error) { 2548 if (newvp) 2549 if (newvp == dvp) 2550 vrele(newvp); 2551 else 2552 vput(newvp); 2553 } else 2554 *npp = np; 2555 } 2556 return (error); 2557} 2558 2559/* 2560 * Nfs Version 3 commit rpc 2561 */ 2562static int 2563nfs_commit(vp, offset, cnt, cred, procp) 2564 register struct vnode *vp; 2565 u_quad_t offset; 2566 int cnt; 2567 struct ucred *cred; 2568 struct proc *procp; 2569{ 2570 register caddr_t cp; 2571 register u_int32_t *tl; 2572 register int32_t t1, t2; 2573 register struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2574 caddr_t bpos, dpos, cp2; 2575 int error = 0, wccflag = NFSV3_WCCRATTR; 2576 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2577 2578 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) 2579 return (0); 2580 nfsstats.rpccnt[NFSPROC_COMMIT]++; 2581 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1)); 2582 nfsm_fhtom(vp, 1); 2583 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2584 txdr_hyper(&offset, tl); 2585 tl += 2; 2586 *tl = txdr_unsigned(cnt); 2587 nfsm_request(vp, NFSPROC_COMMIT, procp, cred); 2588 nfsm_wcc_data(vp, wccflag); 2589 if (!error) { 2590 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF); 2591 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl, 2592 NFSX_V3WRITEVERF)) { 2593 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf, 2594 NFSX_V3WRITEVERF); 2595 error = NFSERR_STALEWRITEVERF; 2596 } 2597 } 2598 nfsm_reqdone; 2599 return (error); 2600} 2601 2602/* 2603 * Kludge City.. 2604 * - make nfs_bmap() essentially a no-op that does no translation 2605 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc 2606 * (Maybe I could use the process's page mapping, but I was concerned that 2607 * Kernel Write might not be enabled and also figured copyout() would do 2608 * a lot more work than bcopy() and also it currently happens in the 2609 * context of the swapper process (2). 2610 */ 2611static int 2612nfs_bmap(ap) 2613 struct vop_bmap_args /* { 2614 struct vnode *a_vp; 2615 daddr_t a_bn; 2616 struct vnode **a_vpp; 2617 daddr_t *a_bnp; 2618 int *a_runp; 2619 int *a_runb; 2620 } */ *ap; 2621{ 2622 register struct vnode *vp = ap->a_vp; 2623 2624 if (ap->a_vpp != NULL) 2625 *ap->a_vpp = vp; 2626 if (ap->a_bnp != NULL) 2627 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize); 2628 if (ap->a_runp != NULL) 2629 *ap->a_runp = 0; 2630 if (ap->a_runb != NULL) 2631 *ap->a_runb = 0; 2632 return (0); 2633} 2634 2635/* 2636 * Strategy routine. 2637 * For async requests when nfsiod(s) are running, queue the request by 2638 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the 2639 * request. 2640 */ 2641static int 2642nfs_strategy(ap) 2643 struct vop_strategy_args *ap; 2644{ 2645 register struct buf *bp = ap->a_bp; 2646 struct ucred *cr; 2647 struct proc *p; 2648 int error = 0; 2649 2650 if (bp->b_flags & B_PHYS) 2651 panic("nfs physio"); 2652 if (bp->b_flags & B_ASYNC) 2653 p = (struct proc *)0; 2654 else 2655 p = curproc; /* XXX */ 2656 if (bp->b_flags & B_READ) 2657 cr = bp->b_rcred; 2658 else 2659 cr = bp->b_wcred; 2660 /* 2661 * If the op is asynchronous and an i/o daemon is waiting 2662 * queue the request, wake it up and wait for completion 2663 * otherwise just do it ourselves. 2664 */ 2665 if ((bp->b_flags & B_ASYNC) == 0 || 2666 nfs_asyncio(bp, NOCRED)) 2667 error = nfs_doio(bp, cr, p); 2668 return (error); 2669} 2670 2671/* 2672 * Mmap a file 2673 * 2674 * NB Currently unsupported. 2675 */ 2676/* ARGSUSED */ 2677static int 2678nfs_mmap(ap) 2679 struct vop_mmap_args /* { 2680 struct vnode *a_vp; 2681 int a_fflags; 2682 struct ucred *a_cred; 2683 struct proc *a_p; 2684 } */ *ap; 2685{ 2686 2687 return (EINVAL); 2688} 2689 2690/* 2691 * fsync vnode op. Just call nfs_flush() with commit == 1. 2692 */ 2693/* ARGSUSED */ 2694static int 2695nfs_fsync(ap) 2696 struct vop_fsync_args /* { 2697 struct vnodeop_desc *a_desc; 2698 struct vnode * a_vp; 2699 struct ucred * a_cred; 2700 int a_waitfor; 2701 struct proc * a_p; 2702 } */ *ap; 2703{ 2704 2705 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_p, 1)); 2706} 2707 2708/* 2709 * Flush all the blocks associated with a vnode. 2710 * Walk through the buffer pool and push any dirty pages 2711 * associated with the vnode. 2712 */ 2713static int 2714nfs_flush(vp, cred, waitfor, p, commit) 2715 register struct vnode *vp; 2716 struct ucred *cred; 2717 int waitfor; 2718 struct proc *p; 2719 int commit; 2720{ 2721 register struct nfsnode *np = VTONFS(vp); 2722 register struct buf *bp; 2723 register int i; 2724 struct buf *nbp; 2725 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2726 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos; 2727 int passone = 1; 2728 u_quad_t off, endoff, toff; 2729 struct ucred* wcred = NULL; 2730 struct buf **bvec = NULL; 2731#ifndef NFS_COMMITBVECSIZ 2732#define NFS_COMMITBVECSIZ 20 2733#endif 2734 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ]; 2735 int bvecsize = 0, bveccount; 2736 2737 if (nmp->nm_flag & NFSMNT_INT) 2738 slpflag = PCATCH; 2739 if (!commit) 2740 passone = 0; 2741 /* 2742 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the 2743 * server, but nas not been committed to stable storage on the server 2744 * yet. On the first pass, the byte range is worked out and the commit 2745 * rpc is done. On the second pass, nfs_writebp() is called to do the 2746 * job. 2747 */ 2748again: 2749 off = (u_quad_t)-1; 2750 endoff = 0; 2751 bvecpos = 0; 2752 if (NFS_ISV3(vp) && commit) { 2753 s = splbio(); 2754 /* 2755 * Count up how many buffers waiting for a commit. 2756 */ 2757 bveccount = 0;
|