1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * 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 are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the University of California, Berkeley. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * @(#)nfs_vnops.c 7.3 (Berkeley) 07/06/89 21 */ 22 23 /* 24 * vnode op calls for sun nfs version 2 25 */ 26 27 #include "machine/pte.h" 28 #include "machine/mtpr.h" 29 #include "strings.h" 30 #include "param.h" 31 #include "user.h" 32 #include "proc.h" 33 #include "mount.h" 34 #include "buf.h" 35 #include "vm.h" 36 #include "../ufs/dir.h" 37 #include "malloc.h" 38 #include "mbuf.h" 39 #include "uio.h" 40 #include "ucred.h" 41 #include "namei.h" 42 #include "errno.h" 43 #include "file.h" 44 #include "conf.h" 45 #include "vnode.h" 46 #include "../ufs/inode.h" 47 #include "nfsv2.h" 48 #include "nfs.h" 49 #include "nfsnode.h" 50 #include "nfsmount.h" 51 #include "xdr_subs.h" 52 #include "nfsm_subs.h" 53 54 /* Defs */ 55 #define TRUE 1 56 #define FALSE 0 57 58 /* Global vars */ 59 int nfs_lookup(), 60 nfs_create(), 61 nfs_open(), 62 nfs_close(), 63 nfs_access(), 64 nfs_getattr(), 65 nfs_setattr(), 66 nfs_read(), 67 nfs_write(), 68 vfs_noop(), 69 vfs_nullop(), 70 nfs_remove(), 71 nfs_link(), 72 nfs_rename(), 73 nfs_mkdir(), 74 nfs_rmdir(), 75 nfs_symlink(), 76 nfs_readdir(), 77 nfs_readlink(), 78 nfs_abortop(), 79 nfs_lock(), 80 nfs_unlock(), 81 nfs_bmap(), 82 nfs_strategy(), 83 nfs_inactive(); 84 85 struct vnodeops nfsv2_vnodeops = { 86 nfs_lookup, 87 nfs_create, 88 vfs_noop, 89 nfs_open, 90 nfs_close, 91 nfs_access, 92 nfs_getattr, 93 nfs_setattr, 94 nfs_read, 95 nfs_write, 96 vfs_noop, 97 vfs_noop, 98 vfs_noop, 99 vfs_nullop, 100 vfs_noop, 101 nfs_remove, 102 nfs_link, 103 nfs_rename, 104 nfs_mkdir, 105 nfs_rmdir, 106 nfs_symlink, 107 nfs_readdir, 108 nfs_readlink, 109 nfs_abortop, 110 nfs_inactive, 111 nfs_lock, 112 nfs_unlock, 113 nfs_bmap, 114 nfs_strategy, 115 }; 116 117 /* Special device vnode ops */ 118 int blk_open(), 119 blk_read(), 120 blk_write(), 121 blk_ioctl(), 122 blk_select(), 123 ufs_bmap(), 124 blk_strategy(); 125 126 struct vnodeops nfsv2chr_vnodeops = { 127 vfs_noop, 128 vfs_noop, 129 vfs_noop, 130 blk_open, 131 nfs_close, 132 nfs_access, 133 nfs_getattr, 134 nfs_setattr, 135 blk_read, 136 blk_write, 137 blk_ioctl, 138 blk_select, 139 vfs_noop, 140 vfs_nullop, 141 vfs_noop, 142 nfs_remove, 143 nfs_link, 144 nfs_rename, 145 vfs_noop, 146 vfs_noop, 147 nfs_symlink, 148 vfs_noop, 149 vfs_noop, 150 nfs_abortop, 151 nfs_inactive, 152 nfs_lock, 153 nfs_unlock, 154 ufs_bmap, 155 blk_strategy, 156 }; 157 158 extern u_long nfs_procids[NFS_NPROCS]; 159 extern u_long nfs_prog, nfs_vers; 160 extern struct vnode *cache_lookup(); 161 extern char nfsiobuf[MAXPHYS+NBPG]; 162 enum vtype v_type[NFLNK+1]; 163 164 /* 165 * nfs null call from vfs. 166 */ 167 nfs_null(vp, cred) 168 struct vnode *vp; 169 struct ucred *cred; 170 { 171 nfsm_vars; 172 173 nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0); 174 nfsm_request(vp); 175 nfsm_reqdone; 176 return (error); 177 } 178 179 /* 180 * nfs access vnode op. 181 * Essentially just get vattr and then imitate iaccess() 182 */ 183 nfs_access(vp, mode, cred) 184 struct vnode *vp; 185 int mode; 186 register struct ucred *cred; 187 { 188 register struct vattr *vap; 189 register gid_t *gp; 190 struct vattr vattr; 191 register int i; 192 int error; 193 194 /* 195 * If you're the super-user, 196 * you always get access. 197 */ 198 if (cred->cr_uid == 0) 199 return (0); 200 vap = &vattr; 201 if (nfs_getattrcache(vp, vap)) { 202 if (error = nfs_getattr(vp, vap, cred)) 203 return (error); 204 } 205 /* 206 * Access check is based on only one of owner, group, public. 207 * If not owner, then check group. If not a member of the 208 * group, then check public access. 209 */ 210 if (cred->cr_uid != vap->va_uid) { 211 mode >>= 3; 212 gp = cred->cr_groups; 213 for (i = 0; i < cred->cr_ngroups; i++, gp++) 214 if (vap->va_gid == *gp) 215 goto found; 216 mode >>= 3; 217 found: 218 ; 219 } 220 if ((vap->va_mode & mode) != 0) 221 return (0); 222 return (EACCES); 223 } 224 225 /* 226 * nfs open vnode op 227 * Just check to see if the type is ok 228 */ 229 nfs_open(vp, mode, cred) 230 struct vnode *vp; 231 int mode; 232 struct ucred *cred; 233 { 234 register enum vtype vtyp; 235 236 vtyp = vp->v_type; 237 if (vtyp == VREG || vtyp == VDIR || vtyp == VLNK) 238 return (0); 239 else 240 return (EACCES); 241 } 242 243 /* 244 * nfs close vnode op 245 * Nothin to do unless its a VCHR 246 */ 247 nfs_close(vp, fflags, cred) 248 register struct vnode *vp; 249 int fflags; 250 struct ucred *cred; 251 { 252 dev_t dev; 253 int error; 254 255 if (vp->v_type != VCHR || vp->v_count > 1) 256 return (0); 257 dev = vp->v_rdev; 258 /* XXX what is this doing below the vnode op call */ 259 if (setjmp(&u.u_qsave)) { 260 /* 261 * If device close routine is interrupted, 262 * must return so closef can clean up. 263 */ 264 error = EINTR; 265 } else 266 error = (*cdevsw[major(dev)].d_close)(dev, fflags, IFCHR); 267 /* 268 * Most device close routines don't return errors, 269 * and dup2() doesn't work right on error. 270 */ 271 error = 0; /* XXX */ 272 return (error); 273 } 274 275 /* 276 * nfs getattr call from vfs. 277 */ 278 nfs_getattr(vp, vap, cred) 279 struct vnode *vp; 280 register struct vattr *vap; 281 struct ucred *cred; 282 { 283 nfsm_vars; 284 285 /* First look in the cache.. */ 286 if (nfs_getattrcache(vp, vap) == 0) 287 return (0); 288 nfsstats.rpccnt[NFSPROC_GETATTR]++; 289 nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH); 290 nfsm_fhtom(vp); 291 nfsm_request(vp); 292 nfsm_loadattr(vp, vap); 293 nfsm_reqdone; 294 return (error); 295 } 296 297 /* 298 * nfs setattr call. 299 */ 300 nfs_setattr(vp, vap, cred) 301 struct vnode *vp; 302 register struct vattr *vap; 303 struct ucred *cred; 304 { 305 nfsm_vars; 306 307 nfsstats.rpccnt[NFSPROC_SETATTR]++; 308 nfsm_reqhead(nfs_procids[NFSPROC_SETATTR], cred, NFSX_FH+NFSX_SATTR); 309 nfsm_fhtom(vp); 310 nfsm_build(p, u_long *, NFSX_SATTR); 311 if (vap->va_mode == 0xffff) 312 *p++ = VNOVAL; 313 else 314 *p++ = vtonfs_mode(vp->v_type, vap->va_mode); 315 if (vap->va_uid == 0xffff) 316 *p++ = VNOVAL; 317 else 318 *p++ = txdr_unsigned(vap->va_uid); 319 if (vap->va_gid == 0xffff) 320 *p++ = VNOVAL; 321 else 322 *p++ = txdr_unsigned(vap->va_gid); 323 *p++ = txdr_unsigned(vap->va_size); 324 txdr_time(&(vap->va_atime), p); 325 p += 2; 326 txdr_time(&(vap->va_mtime), p); 327 nfsm_request(vp); 328 nfsm_loadattr(vp, (struct vattr *)0); 329 /* should we fill in any vap fields ?? */ 330 nfsm_reqdone; 331 return (error); 332 } 333 334 /* 335 * nfs lookup call, one step at a time... 336 * First look in cache 337 * If not found, unlock the directory nfsnode and do the rpc 338 */ 339 nfs_lookup(vp, ndp) 340 register struct vnode *vp; 341 register struct nameidata *ndp; 342 { 343 register struct vnode *vdp; 344 nfsm_vars; 345 struct vnode *newvp; 346 long len; 347 nfsv2fh_t *fhp; 348 struct nfsnode *np; 349 int lockparent, wantparent, flag; 350 dev_t rdev; 351 352 ndp->ni_dvp = vp; 353 ndp->ni_vp = NULL; 354 if (vp->v_type != VDIR) 355 return (ENOTDIR); 356 lockparent = ndp->ni_nameiop & LOCKPARENT; 357 flag = ndp->ni_nameiop & OPFLAG; 358 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); 359 #ifdef notyet 360 if (vdp = cache_lookup(ndp)) { 361 nfsstats.lookupcache_hits++; 362 /* 363 * Get the next vnode in the path. 364 * See comment above `IUNLOCK' code for 365 * an explaination of the locking protocol. 366 */ 367 if (vp == vdp) { 368 VREF(vdp); 369 } else if (ndp->ni_isdotdot) { 370 nfs_unlock(vp); 371 nfs_ngrab(VTONFS(vdp)); 372 } else { 373 nfs_ngrab(VTONFS(vdp)); 374 nfs_unlock(vp); 375 } 376 ndp->ni_vp = vdp; 377 return (0); 378 } 379 nfsstats.lookupcache_misses++; 380 #endif 381 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 382 len = ndp->ni_namelen; 383 nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)); 384 nfsm_fhtom(vp); 385 nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN); 386 nfsm_request(vp); 387 nfsmout: 388 if (error) { 389 if ((flag == CREATE || flag == RENAME) && 390 *ndp->ni_next == 0) { 391 if (!lockparent) 392 nfs_unlock(vp); 393 } 394 return (ENOENT); 395 } 396 nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); 397 398 /* 399 * Handle DELETE and RENAME cases... 400 */ 401 if (flag == DELETE && *ndp->ni_next == 0) { 402 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { 403 VREF(vp); 404 newvp = vp; 405 np = VTONFS(vp); 406 } else { 407 if (error = nfs_nget(vp->v_mount, fhp, &np)) { 408 m_freem(mrep); 409 return (error); 410 } 411 newvp = NFSTOV(np); 412 } 413 if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) { 414 if (newvp != vp) 415 nfs_nput(newvp); 416 else 417 vrele(vp); 418 m_freem(mrep); 419 return (error); 420 } 421 newvp->v_type = np->n_vattr.va_type; 422 ndp->ni_vp = newvp; 423 if (!lockparent) 424 nfs_unlock(vp); 425 m_freem(mrep); 426 return (0); 427 } 428 429 if (flag == RENAME && wantparent && *ndp->ni_next == 0) { 430 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { 431 m_freem(mrep); 432 return (EISDIR); 433 } 434 if (error = nfs_nget(vp->v_mount, fhp, &np)) { 435 m_freem(mrep); 436 return (error); 437 } 438 newvp = NFSTOV(np); 439 if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) { 440 nfs_nput(newvp); 441 m_freem(mrep); 442 return (error); 443 } 444 ndp->ni_vp = newvp; 445 if (!lockparent) 446 nfs_unlock(vp); 447 return (0); 448 } 449 450 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { 451 VREF(vp); 452 newvp = vp; 453 np = VTONFS(vp); 454 } else if (ndp->ni_isdotdot) { 455 nfs_unlock(vp); 456 if (error = nfs_nget(vp->v_mount, fhp, &np)) { 457 nfs_lock(vp); 458 m_freem(mrep); 459 return (error); 460 } 461 nfs_lock(vp); 462 newvp = NFSTOV(np); 463 } else { 464 if (error = nfs_nget(vp->v_mount, fhp, &np)) { 465 m_freem(mrep); 466 return (error); 467 } 468 newvp = NFSTOV(np); 469 } 470 if (error = nfs_loadattrcache(newvp, &md, &dpos, (struct vattr *)0)) { 471 if (newvp != vp) 472 nfs_nput(newvp); 473 else 474 vrele(vp); 475 m_freem(mrep); 476 return (error); 477 } 478 m_freem(mrep); 479 newvp->v_type = np->n_vattr.va_type; 480 481 /* 482 * Handling special files... 483 * For VCHR, use the nfs_node, but with the nfsv2chr_vnodeops 484 * that are a mix of nfs and blk vnode ops. 485 * For VBLK, get a block dev. inode using bdevvp() and release 486 * the nfs_node. This means that ufs_inactive() had better know 487 * how to release inodes that do not have an underlying ufs. 488 * (i_fs == 0) 489 * Also, returns right away to avoid loading the name cache 490 */ 491 if (newvp->v_type == VCHR) { 492 newvp->v_rdev = np->n_vattr.va_rdev; 493 newvp->v_op = &nfsv2chr_vnodeops; 494 } else if (newvp->v_type == VBLK) { 495 rdev = np->n_vattr.va_rdev; 496 nfs_nput(newvp); 497 if (error = bdevvp(rdev, &newvp)) 498 return (error); 499 } 500 if (vp != newvp && (!lockparent || *ndp->ni_next != '\0')) 501 nfs_unlock(vp); 502 ndp->ni_vp = newvp; 503 #ifdef notyet 504 if (error == 0 && ndp->ni_makeentry) 505 cache_enter(ndp); 506 #endif 507 return (error); 508 } 509 510 /* 511 * nfs readlink call 512 */ 513 nfs_readlink(vp, uiop, cred) 514 struct vnode *vp; 515 struct uio *uiop; 516 struct ucred *cred; 517 { 518 nfsm_vars; 519 long len; 520 521 nfsstats.rpccnt[NFSPROC_READLINK]++; 522 nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH); 523 nfsm_fhtom(vp); 524 nfsm_request(vp); 525 nfsm_strsiz(len, NFS_MAXPATHLEN); 526 nfsm_mtouio(uiop, len); 527 nfsm_reqdone; 528 return (error); 529 } 530 531 /* 532 * nfs read call 533 */ 534 nfs_read(vp, uiop, offp, ioflag, cred) 535 struct vnode *vp; 536 struct uio *uiop; 537 off_t *offp; 538 int ioflag; 539 struct ucred *cred; 540 { 541 nfsm_vars; 542 struct nfsmount *nmp; 543 long len, retlen, tsiz; 544 545 nmp = vfs_to_nfs(vp->v_mount); 546 tsiz = uiop->uio_resid; 547 if (!(ioflag & IO_NODELOCKED)) 548 nfs_lock(vp); 549 while (tsiz > 0) { 550 nfsstats.rpccnt[NFSPROC_READ]++; 551 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 552 nfsm_reqhead(nfs_procids[NFSPROC_READ], cred, NFSX_FH+NFSX_UNSIGNED*3); 553 nfsm_fhtom(vp); 554 nfsm_build(p, u_long *, NFSX_UNSIGNED*3); 555 *p++ = txdr_unsigned(*offp); 556 *p++ = txdr_unsigned(len); 557 *p = 0; 558 nfsm_request(vp); 559 nfsm_loadattr(vp, (struct vattr *)0); 560 nfsm_strsiz(retlen, nmp->nm_rsize); 561 nfsm_mtouio(uiop, retlen); 562 m_freem(mrep); 563 *offp += retlen; 564 if (retlen < len) 565 tsiz = 0; 566 else 567 tsiz -= len; 568 } 569 nfsmout: 570 if (!(ioflag & IO_NODELOCKED)) 571 nfs_unlock(vp); 572 return (error); 573 } 574 575 /* 576 * nfs write call 577 */ 578 nfs_write(vp, uiop, offp, ioflag, cred) 579 struct vnode *vp; 580 struct uio *uiop; 581 off_t *offp; 582 int ioflag; 583 struct ucred *cred; 584 { 585 nfsm_vars; 586 struct nfsmount *nmp; 587 long len, tsiz; 588 u_long osize; 589 off_t ooff; 590 struct vattr va; 591 592 nmp = vfs_to_nfs(vp->v_mount); 593 tsiz = uiop->uio_resid; 594 if (!(ioflag & IO_NODELOCKED)) 595 nfs_lock(vp); 596 if ((ioflag&IO_UNIT) || (vp->v_type == VREG && (ioflag&IO_APPEND))) { 597 if (error = nfs_getattr(vp, &va, cred)) 598 goto nfsmout; 599 osize = va.va_size; 600 if (vp->v_type == VREG && (ioflag & IO_APPEND)) 601 *offp = osize; 602 ooff = *offp; 603 } 604 while (tsiz > 0) { 605 nfsstats.rpccnt[NFSPROC_WRITE]++; 606 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz; 607 nfsm_reqhead(nfs_procids[NFSPROC_WRITE], cred, 608 NFSX_FH+NFSX_UNSIGNED*4); 609 nfsm_fhtom(vp); 610 nfsm_build(p, u_long *, NFSX_UNSIGNED*4); 611 *(p+1) = txdr_unsigned(*offp); 612 *(p+3) = txdr_unsigned(len); 613 nfsm_uiotom(uiop, len); 614 nfsm_request(vp); 615 nfsm_loadattr(vp, (struct vattr *)0); 616 m_freem(mrep); 617 tsiz -= len; 618 *offp += len; 619 } 620 nfsmout: 621 if (error && (ioflag & IO_UNIT)) { 622 vattr_null(&va); 623 va.va_size = osize; 624 nfs_setattr(vp, &va, cred); 625 *offp = ooff; 626 } 627 if (!(ioflag & IO_NODELOCKED)) 628 nfs_unlock(vp); 629 return (error); 630 } 631 632 /* 633 * nfs file create call 634 */ 635 nfs_create(ndp, vap) 636 register struct nameidata *ndp; 637 register struct vattr *vap; 638 { 639 nfsm_vars; 640 641 nfsstats.rpccnt[NFSPROC_CREATE]++; 642 nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred, 643 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR); 644 nfsm_fhtom(ndp->ni_dvp); 645 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 646 nfsm_build(p, u_long *, NFSX_UNSIGNED*8); 647 *p++ = vtonfs_mode(VREG, vap->va_mode); 648 *p++ = txdr_unsigned(ndp->ni_cred->cr_uid); 649 *p++ = txdr_unsigned(ndp->ni_cred->cr_gid); 650 *p++ = txdr_unsigned(0); 651 /* or should these be VNOVAL ?? */ 652 txdr_time(&(vap->va_atime), p); 653 txdr_time(&(vap->va_mtime), p+2); 654 nfsm_request(ndp->ni_dvp); 655 nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp); 656 nfsm_reqdone; 657 nfs_nput(ndp->ni_dvp); 658 return (error); 659 } 660 661 /* 662 * nfs file remove call 663 */ 664 nfs_remove(ndp) 665 register struct nameidata *ndp; 666 { 667 nfsm_vars; 668 669 if (ndp->ni_vp->v_count > 1) 670 error = nfs_sillyrename(ndp, REMOVE); 671 else { 672 nfsstats.rpccnt[NFSPROC_REMOVE]++; 673 nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred, 674 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); 675 nfsm_fhtom(ndp->ni_dvp); 676 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 677 nfsm_request(ndp->ni_dvp); 678 nfsm_reqdone; 679 } 680 if (ndp->ni_dvp == ndp->ni_vp) 681 vrele(ndp->ni_vp); 682 else 683 nfs_nput(ndp->ni_vp); 684 nfs_nput(ndp->ni_dvp); 685 return (error); 686 } 687 688 /* 689 * nfs file remove rpc called from nfs_inactive 690 */ 691 nfs_removeit(ndp) 692 register struct nameidata *ndp; 693 { 694 nfsm_vars; 695 696 nfsstats.rpccnt[NFSPROC_REMOVE]++; 697 nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred, 698 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); 699 nfsm_fhtom(ndp->ni_dvp); 700 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 701 nfsm_request(ndp->ni_dvp); 702 nfsm_reqdone; 703 return (error); 704 } 705 706 /* 707 * nfs file rename call 708 */ 709 nfs_rename(sndp, tndp) 710 register struct nameidata *sndp, *tndp; 711 { 712 nfsm_vars; 713 714 nfsstats.rpccnt[NFSPROC_RENAME]++; 715 nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred, 716 (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+ 717 nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/ 718 nfsm_fhtom(sndp->ni_dvp); 719 nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN); 720 nfsm_fhtom(tndp->ni_dvp); 721 nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN); 722 nfsm_request(sndp->ni_dvp); 723 nfsm_reqdone; 724 #ifdef notyet 725 if (sndp->ni_vp->v_type == VDIR) { 726 if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR) 727 cache_purge(tndp->ni_dvp); 728 cache_purge(sndp->ni_dvp); 729 } 730 #endif 731 nfs_abortop(sndp); 732 nfs_abortop(tndp); 733 return (error); 734 } 735 736 /* 737 * nfs file rename rpc called from above 738 */ 739 nfs_renameit(sndp, tndp) 740 register struct nameidata *sndp, *tndp; 741 { 742 nfsm_vars; 743 744 nfsstats.rpccnt[NFSPROC_RENAME]++; 745 nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred, 746 (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+ 747 nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/ 748 nfsm_fhtom(sndp->ni_dvp); 749 nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN); 750 nfsm_fhtom(tndp->ni_dvp); 751 nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN); 752 nfsm_request(sndp->ni_dvp); 753 nfsm_reqdone; 754 return (error); 755 } 756 757 /* 758 * nfs hard link create call 759 */ 760 nfs_link(vp, ndp) 761 struct vnode *vp; 762 register struct nameidata *ndp; 763 { 764 nfsm_vars; 765 766 if (ndp->ni_dvp != vp) 767 nfs_lock(vp); 768 nfsstats.rpccnt[NFSPROC_LINK]++; 769 nfsm_reqhead(nfs_procids[NFSPROC_LINK], ndp->ni_cred, 770 NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); 771 nfsm_fhtom(vp); 772 nfsm_fhtom(ndp->ni_dvp); 773 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 774 nfsm_request(vp); 775 nfsm_reqdone; 776 if (ndp->ni_dvp != vp) 777 nfs_unlock(vp); 778 nfs_nput(ndp->ni_dvp); 779 return (error); 780 } 781 782 /* 783 * nfs symbolic link create call 784 */ 785 nfs_symlink(ndp, vap, nm) 786 struct nameidata *ndp; 787 struct vattr *vap; 788 char *nm; /* is this the path ?? */ 789 { 790 nfsm_vars; 791 792 nfsstats.rpccnt[NFSPROC_SYMLINK]++; 793 nfsm_reqhead(nfs_procids[NFSPROC_SYMLINK], ndp->ni_cred, 794 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_UNSIGNED); 795 nfsm_fhtom(ndp->ni_dvp); 796 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 797 nfsm_strtom(nm, strlen(nm), NFS_MAXPATHLEN); 798 nfsm_build(p, u_long *, NFSX_SATTR); 799 *p++ = vtonfs_mode(VLNK, vap->va_mode); 800 *p++ = txdr_unsigned(ndp->ni_cred->cr_uid); 801 *p++ = txdr_unsigned(ndp->ni_cred->cr_gid); 802 *p++ = txdr_unsigned(VNOVAL); 803 txdr_time(&(vap->va_atime), p); /* or VNOVAL ?? */ 804 txdr_time(&(vap->va_mtime), p+2); /* or VNOVAL ?? */ 805 nfsm_request(ndp->ni_dvp); 806 nfsm_reqdone; 807 nfs_nput(ndp->ni_dvp); 808 return (error); 809 } 810 811 /* 812 * nfs make dir call 813 */ 814 nfs_mkdir(ndp, vap) 815 struct nameidata *ndp; 816 struct vattr *vap; 817 { 818 nfsm_vars; 819 820 nfsstats.rpccnt[NFSPROC_MKDIR]++; 821 nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred, 822 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR); 823 nfsm_fhtom(ndp->ni_dvp); 824 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 825 nfsm_build(p, u_long *, NFSX_SATTR); 826 *p++ = vtonfs_mode(VDIR, vap->va_mode); 827 *p++ = txdr_unsigned(ndp->ni_cred->cr_uid); 828 *p++ = txdr_unsigned(ndp->ni_cred->cr_gid); 829 *p++ = txdr_unsigned(VNOVAL); 830 txdr_time(&(vap->va_atime), p); /* or VNOVAL ?? */ 831 txdr_time(&(vap->va_mtime), p+2); /* or VNOVAL ?? */ 832 nfsm_request(ndp->ni_dvp); 833 nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp); 834 nfsm_reqdone; 835 nfs_nput(ndp->ni_dvp); 836 return (error); 837 } 838 839 /* 840 * nfs remove directory call 841 */ 842 nfs_rmdir(ndp) 843 register struct nameidata *ndp; 844 { 845 nfsm_vars; 846 847 if (ndp->ni_dvp == ndp->ni_vp) { 848 vrele(ndp->ni_dvp); 849 nfs_nput(ndp->ni_dvp); 850 return (EINVAL); 851 } 852 if (ndp->ni_vp->v_count > 1) 853 error = nfs_sillyrename(ndp, RMDIR); 854 else { 855 nfsstats.rpccnt[NFSPROC_RMDIR]++; 856 nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred, 857 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); 858 nfsm_fhtom(ndp->ni_dvp); 859 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 860 nfsm_request(ndp->ni_dvp); 861 nfsm_reqdone; 862 } 863 #ifdef notyet 864 cache_purge(ndp->ni_dvp); 865 cache_purge(ndp->ni_vp); 866 #endif 867 nfs_nput(ndp->ni_vp); 868 nfs_nput(ndp->ni_dvp); 869 return (error); 870 } 871 872 /* 873 * nfs remove dir rpc called from above 874 */ 875 nfs_rmdirit(ndp) 876 register struct nameidata *ndp; 877 { 878 nfsm_vars; 879 880 nfsstats.rpccnt[NFSPROC_RMDIR]++; 881 nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred, 882 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); 883 nfsm_fhtom(ndp->ni_dvp); 884 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); 885 nfsm_request(ndp->ni_dvp); 886 nfsm_reqdone; 887 return (error); 888 } 889 890 /* 891 * nfs readdir call 892 * Although cookie is defined as opaque, I translate it to/from net byte 893 * order so that it looks more sensible. This appears consistent with the 894 * Ultrix implementation of NFS. 895 */ 896 nfs_readdir(vp, uiop, offp, cred) 897 struct vnode *vp; 898 struct uio *uiop; 899 off_t *offp; 900 struct ucred *cred; 901 { 902 register long len; 903 register struct direct *dp; 904 nfsm_vars; 905 struct mbuf *md2; 906 caddr_t dpos2; 907 int siz; 908 int more_dirs, eofflg; 909 off_t off, savoff; 910 struct direct *savdp; 911 912 nfs_lock(vp); 913 nfsstats.rpccnt[NFSPROC_READDIR]++; 914 nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid); 915 nfsm_fhtom(vp); 916 nfsm_build(p, u_long *, 2*NFSX_UNSIGNED); 917 off = *offp; 918 *p++ = txdr_unsigned(off); 919 *p = txdr_unsigned(uiop->uio_resid); 920 nfsm_request(vp); 921 siz = 0; 922 nfsm_disect(p, u_long *, NFSX_UNSIGNED); 923 more_dirs = fxdr_unsigned(int, *p); 924 925 /* Save the position so that we can do nfsm_mtouio() later */ 926 dpos2 = dpos; 927 md2 = md; 928 929 /* loop thru the dir entries, doctoring them to 4bsd form */ 930 while (more_dirs && siz < uiop->uio_resid) { 931 savoff = off; /* Hold onto offset and dp */ 932 savdp = dp; 933 nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED); 934 dp = (struct direct *)p; 935 dp->d_ino = fxdr_unsigned(u_long, *p++); 936 len = fxdr_unsigned(int, *p); 937 if (len <= 0 || len > NFS_MAXNAMLEN) { 938 error = EBADRPC; 939 m_freem(mrep); 940 goto nfsmout; 941 } 942 dp->d_namlen = (u_short)len; 943 len = nfsm_rndup(len); 944 nfsm_adv(len); 945 nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED); 946 off = fxdr_unsigned(off_t, *p); 947 *p++ = 0; /* Ensures null termination of name */ 948 more_dirs = fxdr_unsigned(int, *p); 949 dp->d_reclen = len+4*NFSX_UNSIGNED; 950 siz += dp->d_reclen; 951 } 952 /* 953 * If at end of rpc data, get the eof boolean 954 */ 955 if (!more_dirs) { 956 nfsm_disecton(p, u_long *, NFSX_UNSIGNED); 957 eofflg = fxdr_unsigned(long, *p); 958 } 959 /* 960 * If there is too much to fit in the data buffer, use savoff and 961 * savdp to trim off the last record. 962 * --> we are not at eof 963 */ 964 if (siz > uiop->uio_resid) { 965 eofflg = FALSE; 966 off = savoff; 967 siz -= dp->d_reclen; 968 dp = savdp; 969 } 970 if (siz > 0) { 971 #ifdef notdef 972 if (!eofflg) 973 dp->d_reclen += (uiop->uio_resid-siz); 974 #endif 975 md = md2; 976 dpos = dpos2; 977 nfsm_mtouio(uiop, siz); 978 #ifdef notdef 979 if (!eofflg) 980 uiop->uio_resid = 0; 981 #endif 982 *offp = off; 983 } 984 nfsm_reqdone; 985 nfs_unlock(vp); 986 return (error); 987 } 988 989 /* 990 * nfs statfs call 991 * (Actually a vfsop, not a vnode op) 992 */ 993 nfs_statfs(mp, sbp) 994 struct mount *mp; 995 register struct statfs *sbp; 996 { 997 register struct nfsmount *nmp; 998 nfsm_vars; 999 struct ucred *cred; 1000 struct nfsnode *np; 1001 struct vnode *vp; 1002 1003 nmp = vfs_to_nfs(mp); 1004 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 1005 return (error); 1006 vp = NFSTOV(np); 1007 nfsstats.rpccnt[NFSPROC_STATFS]++; 1008 cred = crget(); 1009 cred->cr_ngroups = 1; 1010 nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH); 1011 nfsm_fhtom(vp); 1012 nfsm_request(vp); 1013 nfsm_disect(p, u_long *, 5*NFSX_UNSIGNED); 1014 sbp->f_type = MOUNT_NFS; 1015 sbp->f_flags = nmp->nm_flag; 1016 sbp->f_bsize = fxdr_unsigned(long, *p++); 1017 sbp->f_fsize = fxdr_unsigned(long, *p++); 1018 sbp->f_blocks = fxdr_unsigned(long, *p++); 1019 sbp->f_bfree = fxdr_unsigned(long, *p++); 1020 sbp->f_bavail = fxdr_unsigned(long, *p); 1021 sbp->f_files = 0x7fffffff; 1022 sbp->f_ffree = 0x7fffffff; 1023 sbp->f_fsid.val[0] = mp->m_fsid.val[0]; 1024 sbp->f_fsid.val[1] = mp->m_fsid.val[1]; 1025 bcopy(nmp->nm_path, sbp->f_mntonname, MNAMELEN); 1026 bcopy(nmp->nm_host, sbp->f_mntfromname, MNAMELEN); 1027 nfsm_reqdone; 1028 nfs_nput(vp); 1029 crfree(cred); 1030 return (error); 1031 } 1032 1033 #define HEXTOASC(x) "0123456789abcdef"[x] 1034 1035 /* 1036 * Silly rename. To make the NFS filesystem that is stateless look a little 1037 * more like the "ufs" a remove of an active vnode is translated to a rename 1038 * to a funny looking filename that is removed by nfs_inactive on the 1039 * nfsnode. There is the potential for another process on a different client 1040 * to create the same funny name between the nfs_lookitup() fails and the 1041 * nfs_rename() completes, but... 1042 */ 1043 nfs_sillyrename(ndp, flag) 1044 struct nameidata *ndp; 1045 int flag; 1046 { 1047 register struct nfsnode *np; 1048 register struct sillyrename *sp; 1049 register struct nameidata *tndp; 1050 int error; 1051 short pid; 1052 1053 np = VTONFS(ndp->ni_dvp); 1054 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), 1055 M_WAITOK, M_TEMP); 1056 sp->s_flag = flag; 1057 bcopy((caddr_t)&np->n_fh, (caddr_t)&sp->s_fh, NFSX_FH); 1058 np = VTONFS(ndp->ni_vp); 1059 tndp = &sp->s_namei; 1060 tndp->ni_cred = crdup(ndp->ni_cred); 1061 1062 /* Fudge together a funny name */ 1063 pid = u.u_procp->p_pid; 1064 bcopy(".nfsAxxxx4.4", tndp->ni_dent.d_name, 13); 1065 tndp->ni_dent.d_namlen = 12; 1066 tndp->ni_dent.d_name[8] = HEXTOASC(pid & 0xf); 1067 tndp->ni_dent.d_name[7] = HEXTOASC((pid >> 4) & 0xf); 1068 tndp->ni_dent.d_name[6] = HEXTOASC((pid >> 8) & 0xf); 1069 tndp->ni_dent.d_name[5] = HEXTOASC((pid >> 12) & 0xf); 1070 1071 /* Try lookitups until we get one that isn't there */ 1072 while (nfs_lookitup(ndp->ni_dvp, tndp, (nfsv2fh_t *)0) == 0) { 1073 tndp->ni_dent.d_name[4]++; 1074 if (tndp->ni_dent.d_name[4] > 'z') { 1075 error = EINVAL; 1076 goto bad; 1077 } 1078 } 1079 if (error = nfs_renameit(ndp, tndp)) 1080 goto bad; 1081 nfs_lookitup(ndp->ni_dvp, tndp, &np->n_fh); 1082 np->n_sillyrename = sp; 1083 return (0); 1084 bad: 1085 crfree(ndp->ni_cred); 1086 free((caddr_t)sp, M_TEMP); 1087 return (error); 1088 } 1089 1090 /* 1091 * Look up a file name for silly rename stuff. 1092 * Just like nfs_lookup() except that it doesn't load returned values 1093 * into the nfsnode table. 1094 * If fhp != NULL it copies the returned file handle out 1095 */ 1096 nfs_lookitup(vp, ndp, fhp) 1097 register struct vnode *vp; 1098 register struct nameidata *ndp; 1099 nfsv2fh_t *fhp; 1100 { 1101 nfsm_vars; 1102 long len; 1103 1104 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 1105 ndp->ni_dvp = vp; 1106 ndp->ni_vp = NULL; 1107 len = ndp->ni_dent.d_namlen; 1108 nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)); 1109 nfsm_fhtom(vp); 1110 nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN); 1111 nfsm_request(vp); 1112 if (fhp != NULL) { 1113 nfsm_disect(cp, caddr_t, NFSX_FH); 1114 bcopy(cp, (caddr_t)fhp, NFSX_FH); 1115 } 1116 nfsm_reqdone; 1117 return (error); 1118 } 1119 1120 /* 1121 * Kludge City.. 1122 * - make nfs_bmap() essentially a no-op that does no translation 1123 * - do nfs_strategy() by faking physical I/O with nfs_readit/nfs_writeit 1124 * after mapping the physical addresses into Kernel Virtual space in the 1125 * nfsiobuf area. 1126 * (Maybe I could use the process's page mapping, but I was concerned that 1127 * Kernel Write might not be enabled and also figured copyout() would do 1128 * a lot more work than bcopy() and also it currently happens in the 1129 * context of the swapper process (2). 1130 */ 1131 nfs_bmap(vp, bn, vpp, bnp) 1132 struct vnode *vp; 1133 daddr_t bn; 1134 struct vnode **vpp; 1135 daddr_t *bnp; 1136 { 1137 if (vpp != NULL) 1138 *vpp = vp; 1139 if (bnp != NULL) 1140 *bnp = bn * btodb(vp->v_mount->m_bsize); 1141 return (0); 1142 } 1143 1144 /* 1145 * Fun and games with phys i/o 1146 */ 1147 nfs_strategy(bp) 1148 register struct buf *bp; 1149 { 1150 register struct pte *pte, *ppte; 1151 register caddr_t vaddr; 1152 register struct uio *uiop; 1153 register struct ucred *cr; 1154 register struct vnode *vp; 1155 int npf; 1156 unsigned v; 1157 struct proc *rp; 1158 int o, error; 1159 off_t off; 1160 struct uio uio; 1161 struct iovec io; 1162 1163 vp = bp->b_vp; 1164 cr = crget(); 1165 cr->cr_gid = 10; /* Pick anything ?? */ 1166 cr->cr_ngroups = 1; 1167 uiop = &uio; 1168 uiop->uio_iov = &io; 1169 uiop->uio_iovcnt = 1; 1170 io.iov_len = uiop->uio_resid = bp->b_bcount; 1171 uiop->uio_segflg = UIO_SYSSPACE; 1172 uiop->uio_offset = off = bp->b_blkno*DEV_BSIZE; 1173 o = (int)bp->b_un.b_addr & PGOFSET; 1174 npf = btoc(bp->b_bcount + o); 1175 rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc; 1176 cr->cr_uid = rp->p_uid; 1177 if ((bp->b_flags & B_PHYS) == 0) 1178 panic("nfs strategy Not PHYS IO"); 1179 if (bp->b_flags & B_PAGET) 1180 pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)]; 1181 else { 1182 v = btop(bp->b_un.b_addr); 1183 if (bp->b_flags & B_UAREA) 1184 pte = &rp->p_addr[v]; 1185 else 1186 pte = vtopte(rp, v); 1187 } 1188 /* 1189 * Play vmaccess() but with the Nfsiomap page table 1190 */ 1191 ppte = &Nfsiomap[0]; 1192 vaddr = nfsiobuf; 1193 while (npf != 0) { 1194 mapin(ppte, (u_int)vaddr, pte->pg_pfnum, (int)(PG_V|PG_KW)); 1195 #if defined(tahoe) 1196 mtpr(P1DC, vaddr); 1197 #endif 1198 ppte++; 1199 pte++; 1200 vaddr += NBPG; 1201 --npf; 1202 } 1203 io.iov_base = nfsiobuf+o; 1204 if (bp->b_flags & B_READ) { 1205 uiop->uio_rw = UIO_READ; 1206 bp->b_error = error = nfs_read(vp, uiop, &off, 0, cr); 1207 } else { 1208 uiop->uio_rw = UIO_WRITE; 1209 bp->b_error = error = nfs_write(vp, uiop, &off, 0, cr); 1210 } 1211 bp->b_resid = uiop->uio_resid; 1212 biodone(bp); 1213 crfree(cr); 1214 return (error); 1215 } 1216