nfs_subs.c revision 63788
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95 37 * $FreeBSD: head/sys/nfsclient/nfs_subs.c 63788 2000-07-24 05:28:33Z mckusick $ 38 */ 39 40/* 41 * These functions support the macros and help fiddle mbuf chains for 42 * the nfs op functions. They do things like create the rpc header and 43 * copy data between mbuf chains and uio lists. 44 */ 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/bio.h> 49#include <sys/buf.h> 50#include <sys/proc.h> 51#include <sys/mount.h> 52#include <sys/vnode.h> 53#include <sys/namei.h> 54#include <sys/mbuf.h> 55#include <sys/socket.h> 56#include <sys/stat.h> 57#include <sys/malloc.h> 58#include <sys/sysent.h> 59#include <sys/syscall.h> 60 61#include <vm/vm.h> 62#include <vm/vm_object.h> 63#include <vm/vm_extern.h> 64#include <vm/vm_zone.h> 65 66#include <nfs/rpcv2.h> 67#include <nfs/nfsproto.h> 68#include <nfs/nfs.h> 69#include <nfs/nfsnode.h> 70#include <nfs/xdr_subs.h> 71#include <nfs/nfsm_subs.h> 72#include <nfs/nfsmount.h> 73#include <nfs/nqnfs.h> 74#include <nfs/nfsrtt.h> 75 76#include <netinet/in.h> 77 78/* 79 * Data items converted to xdr at startup, since they are constant 80 * This is kinda hokey, but may save a little time doing byte swaps 81 */ 82u_int32_t nfs_xdrneg1; 83u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 84 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, 85 rpc_auth_kerb; 86u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false; 87 88/* And other global data */ 89static u_int32_t nfs_xid = 0; 90static enum vtype nv2tov_type[8]= { 91 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 92}; 93enum vtype nv3tov_type[8]= { 94 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO 95}; 96 97int nfs_ticks; 98int nfs_pbuf_freecnt = -1; /* start out unlimited */ 99 100struct nfs_reqq nfs_reqq; 101struct nfssvc_sockhead nfssvc_sockhead; 102int nfssvc_sockhead_flag; 103struct nfsd_head nfsd_head; 104int nfsd_head_flag; 105struct nfs_bufq nfs_bufq; 106struct nqtimerhead nqtimerhead; 107struct nqfhhashhead *nqfhhashtbl; 108u_long nqfhhash; 109 110static void (*nfs_prev_lease_updatetime) __P((int)); 111static int nfs_prev_nfssvc_sy_narg; 112static sy_call_t *nfs_prev_nfssvc_sy_call; 113 114#ifndef NFS_NOSERVER 115 116static vop_t *nfs_prev_vop_lease_check; 117 118/* 119 * Mapping of old NFS Version 2 RPC numbers to generic numbers. 120 */ 121int nfsv3_procid[NFS_NPROCS] = { 122 NFSPROC_NULL, 123 NFSPROC_GETATTR, 124 NFSPROC_SETATTR, 125 NFSPROC_NOOP, 126 NFSPROC_LOOKUP, 127 NFSPROC_READLINK, 128 NFSPROC_READ, 129 NFSPROC_NOOP, 130 NFSPROC_WRITE, 131 NFSPROC_CREATE, 132 NFSPROC_REMOVE, 133 NFSPROC_RENAME, 134 NFSPROC_LINK, 135 NFSPROC_SYMLINK, 136 NFSPROC_MKDIR, 137 NFSPROC_RMDIR, 138 NFSPROC_READDIR, 139 NFSPROC_FSSTAT, 140 NFSPROC_NOOP, 141 NFSPROC_NOOP, 142 NFSPROC_NOOP, 143 NFSPROC_NOOP, 144 NFSPROC_NOOP, 145 NFSPROC_NOOP, 146 NFSPROC_NOOP, 147 NFSPROC_NOOP 148}; 149 150#endif /* NFS_NOSERVER */ 151/* 152 * and the reverse mapping from generic to Version 2 procedure numbers 153 */ 154int nfsv2_procid[NFS_NPROCS] = { 155 NFSV2PROC_NULL, 156 NFSV2PROC_GETATTR, 157 NFSV2PROC_SETATTR, 158 NFSV2PROC_LOOKUP, 159 NFSV2PROC_NOOP, 160 NFSV2PROC_READLINK, 161 NFSV2PROC_READ, 162 NFSV2PROC_WRITE, 163 NFSV2PROC_CREATE, 164 NFSV2PROC_MKDIR, 165 NFSV2PROC_SYMLINK, 166 NFSV2PROC_CREATE, 167 NFSV2PROC_REMOVE, 168 NFSV2PROC_RMDIR, 169 NFSV2PROC_RENAME, 170 NFSV2PROC_LINK, 171 NFSV2PROC_READDIR, 172 NFSV2PROC_NOOP, 173 NFSV2PROC_STATFS, 174 NFSV2PROC_NOOP, 175 NFSV2PROC_NOOP, 176 NFSV2PROC_NOOP, 177 NFSV2PROC_NOOP, 178 NFSV2PROC_NOOP, 179 NFSV2PROC_NOOP, 180 NFSV2PROC_NOOP, 181}; 182 183#ifndef NFS_NOSERVER 184/* 185 * Maps errno values to nfs error numbers. 186 * Use NFSERR_IO as the catch all for ones not specifically defined in 187 * RFC 1094. 188 */ 189static u_char nfsrv_v2errmap[ELAST] = { 190 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 191 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 192 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 193 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 194 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 195 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 196 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 197 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 198 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 199 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 200 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 202 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 203 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 206 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 207 NFSERR_IO /* << Last is 86 */ 208}; 209 210/* 211 * Maps errno values to nfs error numbers. 212 * Although it is not obvious whether or not NFS clients really care if 213 * a returned error value is in the specified list for the procedure, the 214 * safest thing to do is filter them appropriately. For Version 2, the 215 * X/Open XNFS document is the only specification that defines error values 216 * for each RPC (The RFC simply lists all possible error values for all RPCs), 217 * so I have decided to not do this for Version 2. 218 * The first entry is the default error return and the rest are the valid 219 * errors for that RPC in increasing numeric order. 220 */ 221static short nfsv3err_null[] = { 222 0, 223 0, 224}; 225 226static short nfsv3err_getattr[] = { 227 NFSERR_IO, 228 NFSERR_IO, 229 NFSERR_STALE, 230 NFSERR_BADHANDLE, 231 NFSERR_SERVERFAULT, 232 0, 233}; 234 235static short nfsv3err_setattr[] = { 236 NFSERR_IO, 237 NFSERR_PERM, 238 NFSERR_IO, 239 NFSERR_ACCES, 240 NFSERR_INVAL, 241 NFSERR_NOSPC, 242 NFSERR_ROFS, 243 NFSERR_DQUOT, 244 NFSERR_STALE, 245 NFSERR_BADHANDLE, 246 NFSERR_NOT_SYNC, 247 NFSERR_SERVERFAULT, 248 0, 249}; 250 251static short nfsv3err_lookup[] = { 252 NFSERR_IO, 253 NFSERR_NOENT, 254 NFSERR_IO, 255 NFSERR_ACCES, 256 NFSERR_NOTDIR, 257 NFSERR_NAMETOL, 258 NFSERR_STALE, 259 NFSERR_BADHANDLE, 260 NFSERR_SERVERFAULT, 261 0, 262}; 263 264static short nfsv3err_access[] = { 265 NFSERR_IO, 266 NFSERR_IO, 267 NFSERR_STALE, 268 NFSERR_BADHANDLE, 269 NFSERR_SERVERFAULT, 270 0, 271}; 272 273static short nfsv3err_readlink[] = { 274 NFSERR_IO, 275 NFSERR_IO, 276 NFSERR_ACCES, 277 NFSERR_INVAL, 278 NFSERR_STALE, 279 NFSERR_BADHANDLE, 280 NFSERR_NOTSUPP, 281 NFSERR_SERVERFAULT, 282 0, 283}; 284 285static short nfsv3err_read[] = { 286 NFSERR_IO, 287 NFSERR_IO, 288 NFSERR_NXIO, 289 NFSERR_ACCES, 290 NFSERR_INVAL, 291 NFSERR_STALE, 292 NFSERR_BADHANDLE, 293 NFSERR_SERVERFAULT, 294 0, 295}; 296 297static short nfsv3err_write[] = { 298 NFSERR_IO, 299 NFSERR_IO, 300 NFSERR_ACCES, 301 NFSERR_INVAL, 302 NFSERR_FBIG, 303 NFSERR_NOSPC, 304 NFSERR_ROFS, 305 NFSERR_DQUOT, 306 NFSERR_STALE, 307 NFSERR_BADHANDLE, 308 NFSERR_SERVERFAULT, 309 0, 310}; 311 312static short nfsv3err_create[] = { 313 NFSERR_IO, 314 NFSERR_IO, 315 NFSERR_ACCES, 316 NFSERR_EXIST, 317 NFSERR_NOTDIR, 318 NFSERR_NOSPC, 319 NFSERR_ROFS, 320 NFSERR_NAMETOL, 321 NFSERR_DQUOT, 322 NFSERR_STALE, 323 NFSERR_BADHANDLE, 324 NFSERR_NOTSUPP, 325 NFSERR_SERVERFAULT, 326 0, 327}; 328 329static short nfsv3err_mkdir[] = { 330 NFSERR_IO, 331 NFSERR_IO, 332 NFSERR_ACCES, 333 NFSERR_EXIST, 334 NFSERR_NOTDIR, 335 NFSERR_NOSPC, 336 NFSERR_ROFS, 337 NFSERR_NAMETOL, 338 NFSERR_DQUOT, 339 NFSERR_STALE, 340 NFSERR_BADHANDLE, 341 NFSERR_NOTSUPP, 342 NFSERR_SERVERFAULT, 343 0, 344}; 345 346static short nfsv3err_symlink[] = { 347 NFSERR_IO, 348 NFSERR_IO, 349 NFSERR_ACCES, 350 NFSERR_EXIST, 351 NFSERR_NOTDIR, 352 NFSERR_NOSPC, 353 NFSERR_ROFS, 354 NFSERR_NAMETOL, 355 NFSERR_DQUOT, 356 NFSERR_STALE, 357 NFSERR_BADHANDLE, 358 NFSERR_NOTSUPP, 359 NFSERR_SERVERFAULT, 360 0, 361}; 362 363static short nfsv3err_mknod[] = { 364 NFSERR_IO, 365 NFSERR_IO, 366 NFSERR_ACCES, 367 NFSERR_EXIST, 368 NFSERR_NOTDIR, 369 NFSERR_NOSPC, 370 NFSERR_ROFS, 371 NFSERR_NAMETOL, 372 NFSERR_DQUOT, 373 NFSERR_STALE, 374 NFSERR_BADHANDLE, 375 NFSERR_NOTSUPP, 376 NFSERR_SERVERFAULT, 377 NFSERR_BADTYPE, 378 0, 379}; 380 381static short nfsv3err_remove[] = { 382 NFSERR_IO, 383 NFSERR_NOENT, 384 NFSERR_IO, 385 NFSERR_ACCES, 386 NFSERR_NOTDIR, 387 NFSERR_ROFS, 388 NFSERR_NAMETOL, 389 NFSERR_STALE, 390 NFSERR_BADHANDLE, 391 NFSERR_SERVERFAULT, 392 0, 393}; 394 395static short nfsv3err_rmdir[] = { 396 NFSERR_IO, 397 NFSERR_NOENT, 398 NFSERR_IO, 399 NFSERR_ACCES, 400 NFSERR_EXIST, 401 NFSERR_NOTDIR, 402 NFSERR_INVAL, 403 NFSERR_ROFS, 404 NFSERR_NAMETOL, 405 NFSERR_NOTEMPTY, 406 NFSERR_STALE, 407 NFSERR_BADHANDLE, 408 NFSERR_NOTSUPP, 409 NFSERR_SERVERFAULT, 410 0, 411}; 412 413static short nfsv3err_rename[] = { 414 NFSERR_IO, 415 NFSERR_NOENT, 416 NFSERR_IO, 417 NFSERR_ACCES, 418 NFSERR_EXIST, 419 NFSERR_XDEV, 420 NFSERR_NOTDIR, 421 NFSERR_ISDIR, 422 NFSERR_INVAL, 423 NFSERR_NOSPC, 424 NFSERR_ROFS, 425 NFSERR_MLINK, 426 NFSERR_NAMETOL, 427 NFSERR_NOTEMPTY, 428 NFSERR_DQUOT, 429 NFSERR_STALE, 430 NFSERR_BADHANDLE, 431 NFSERR_NOTSUPP, 432 NFSERR_SERVERFAULT, 433 0, 434}; 435 436static short nfsv3err_link[] = { 437 NFSERR_IO, 438 NFSERR_IO, 439 NFSERR_ACCES, 440 NFSERR_EXIST, 441 NFSERR_XDEV, 442 NFSERR_NOTDIR, 443 NFSERR_INVAL, 444 NFSERR_NOSPC, 445 NFSERR_ROFS, 446 NFSERR_MLINK, 447 NFSERR_NAMETOL, 448 NFSERR_DQUOT, 449 NFSERR_STALE, 450 NFSERR_BADHANDLE, 451 NFSERR_NOTSUPP, 452 NFSERR_SERVERFAULT, 453 0, 454}; 455 456static short nfsv3err_readdir[] = { 457 NFSERR_IO, 458 NFSERR_IO, 459 NFSERR_ACCES, 460 NFSERR_NOTDIR, 461 NFSERR_STALE, 462 NFSERR_BADHANDLE, 463 NFSERR_BAD_COOKIE, 464 NFSERR_TOOSMALL, 465 NFSERR_SERVERFAULT, 466 0, 467}; 468 469static short nfsv3err_readdirplus[] = { 470 NFSERR_IO, 471 NFSERR_IO, 472 NFSERR_ACCES, 473 NFSERR_NOTDIR, 474 NFSERR_STALE, 475 NFSERR_BADHANDLE, 476 NFSERR_BAD_COOKIE, 477 NFSERR_NOTSUPP, 478 NFSERR_TOOSMALL, 479 NFSERR_SERVERFAULT, 480 0, 481}; 482 483static short nfsv3err_fsstat[] = { 484 NFSERR_IO, 485 NFSERR_IO, 486 NFSERR_STALE, 487 NFSERR_BADHANDLE, 488 NFSERR_SERVERFAULT, 489 0, 490}; 491 492static short nfsv3err_fsinfo[] = { 493 NFSERR_STALE, 494 NFSERR_STALE, 495 NFSERR_BADHANDLE, 496 NFSERR_SERVERFAULT, 497 0, 498}; 499 500static short nfsv3err_pathconf[] = { 501 NFSERR_STALE, 502 NFSERR_STALE, 503 NFSERR_BADHANDLE, 504 NFSERR_SERVERFAULT, 505 0, 506}; 507 508static short nfsv3err_commit[] = { 509 NFSERR_IO, 510 NFSERR_IO, 511 NFSERR_STALE, 512 NFSERR_BADHANDLE, 513 NFSERR_SERVERFAULT, 514 0, 515}; 516 517static short *nfsrv_v3errmap[] = { 518 nfsv3err_null, 519 nfsv3err_getattr, 520 nfsv3err_setattr, 521 nfsv3err_lookup, 522 nfsv3err_access, 523 nfsv3err_readlink, 524 nfsv3err_read, 525 nfsv3err_write, 526 nfsv3err_create, 527 nfsv3err_mkdir, 528 nfsv3err_symlink, 529 nfsv3err_mknod, 530 nfsv3err_remove, 531 nfsv3err_rmdir, 532 nfsv3err_rename, 533 nfsv3err_link, 534 nfsv3err_readdir, 535 nfsv3err_readdirplus, 536 nfsv3err_fsstat, 537 nfsv3err_fsinfo, 538 nfsv3err_pathconf, 539 nfsv3err_commit, 540}; 541 542#endif /* NFS_NOSERVER */ 543 544extern struct nfsrtt nfsrtt; 545extern time_t nqnfsstarttime; 546extern int nqsrv_clockskew; 547extern int nqsrv_writeslack; 548extern int nqsrv_maxlease; 549extern struct nfsstats nfsstats; 550extern int nqnfs_piggy[NFS_NPROCS]; 551extern nfstype nfsv2_type[9]; 552extern nfstype nfsv3_type[9]; 553extern struct nfsnodehashhead *nfsnodehashtbl; 554extern u_long nfsnodehash; 555 556struct nfssvc_args; 557extern int nfssvc(struct proc *, struct nfssvc_args *, int *); 558 559LIST_HEAD(nfsnodehashhead, nfsnode); 560 561int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *)); 562 563u_quad_t 564nfs_curusec() 565{ 566 struct timeval tv; 567 568 getmicrotime(&tv); 569 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec); 570} 571 572/* 573 * Create the header for an rpc request packet 574 * The hsiz is the size of the rest of the nfs request header. 575 * (just used to decide if a cluster is a good idea) 576 */ 577struct mbuf * 578nfsm_reqh(vp, procid, hsiz, bposp) 579 struct vnode *vp; 580 u_long procid; 581 int hsiz; 582 caddr_t *bposp; 583{ 584 register struct mbuf *mb; 585 register u_int32_t *tl; 586 register caddr_t bpos; 587 struct mbuf *mb2; 588 struct nfsmount *nmp; 589 int nqflag; 590 591 MGET(mb, M_WAIT, MT_DATA); 592 if (hsiz >= MINCLSIZE) 593 MCLGET(mb, M_WAIT); 594 mb->m_len = 0; 595 bpos = mtod(mb, caddr_t); 596 597 /* 598 * For NQNFS, add lease request. 599 */ 600 if (vp) { 601 nmp = VFSTONFS(vp->v_mount); 602 if (nmp->nm_flag & NFSMNT_NQNFS) { 603 nqflag = NQNFS_NEEDLEASE(vp, procid); 604 if (nqflag) { 605 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED); 606 *tl++ = txdr_unsigned(nqflag); 607 *tl = txdr_unsigned(nmp->nm_leaseterm); 608 } else { 609 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 610 *tl = 0; 611 } 612 } 613 } 614 /* Finally, return values */ 615 *bposp = bpos; 616 return (mb); 617} 618 619/* 620 * Build the RPC header and fill in the authorization info. 621 * The authorization string argument is only used when the credentials 622 * come from outside of the kernel. 623 * Returns the head of the mbuf list. 624 */ 625struct mbuf * 626nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len, 627 verf_str, mrest, mrest_len, mbp, xidp) 628 register struct ucred *cr; 629 int nmflag; 630 int procid; 631 int auth_type; 632 int auth_len; 633 char *auth_str; 634 int verf_len; 635 char *verf_str; 636 struct mbuf *mrest; 637 int mrest_len; 638 struct mbuf **mbp; 639 u_int32_t *xidp; 640{ 641 register struct mbuf *mb; 642 register u_int32_t *tl; 643 register caddr_t bpos; 644 register int i; 645 struct mbuf *mreq, *mb2; 646 int siz, grpsiz, authsiz; 647 648 authsiz = nfsm_rndup(auth_len); 649 MGETHDR(mb, M_WAIT, MT_DATA); 650 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) { 651 MCLGET(mb, M_WAIT); 652 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) { 653 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED); 654 } else { 655 MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 656 } 657 mb->m_len = 0; 658 mreq = mb; 659 bpos = mtod(mb, caddr_t); 660 661 /* 662 * First the RPC header. 663 */ 664 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 665 666 /* Get a pretty random xid to start with */ 667 if (!nfs_xid) 668 nfs_xid = random(); 669 /* 670 * Skip zero xid if it should ever happen. 671 */ 672 if (++nfs_xid == 0) 673 nfs_xid++; 674 675 *tl++ = *xidp = txdr_unsigned(nfs_xid); 676 *tl++ = rpc_call; 677 *tl++ = rpc_vers; 678 if (nmflag & NFSMNT_NQNFS) { 679 *tl++ = txdr_unsigned(NQNFS_PROG); 680 *tl++ = txdr_unsigned(NQNFS_VER3); 681 } else { 682 *tl++ = txdr_unsigned(NFS_PROG); 683 if (nmflag & NFSMNT_NFSV3) 684 *tl++ = txdr_unsigned(NFS_VER3); 685 else 686 *tl++ = txdr_unsigned(NFS_VER2); 687 } 688 if (nmflag & NFSMNT_NFSV3) 689 *tl++ = txdr_unsigned(procid); 690 else 691 *tl++ = txdr_unsigned(nfsv2_procid[procid]); 692 693 /* 694 * And then the authorization cred. 695 */ 696 *tl++ = txdr_unsigned(auth_type); 697 *tl = txdr_unsigned(authsiz); 698 switch (auth_type) { 699 case RPCAUTH_UNIX: 700 nfsm_build(tl, u_int32_t *, auth_len); 701 *tl++ = 0; /* stamp ?? */ 702 *tl++ = 0; /* NULL hostname */ 703 *tl++ = txdr_unsigned(cr->cr_uid); 704 *tl++ = txdr_unsigned(cr->cr_groups[0]); 705 grpsiz = (auth_len >> 2) - 5; 706 *tl++ = txdr_unsigned(grpsiz); 707 for (i = 1; i <= grpsiz; i++) 708 *tl++ = txdr_unsigned(cr->cr_groups[i]); 709 break; 710 case RPCAUTH_KERB4: 711 siz = auth_len; 712 while (siz > 0) { 713 if (M_TRAILINGSPACE(mb) == 0) { 714 MGET(mb2, M_WAIT, MT_DATA); 715 if (siz >= MINCLSIZE) 716 MCLGET(mb2, M_WAIT); 717 mb->m_next = mb2; 718 mb = mb2; 719 mb->m_len = 0; 720 bpos = mtod(mb, caddr_t); 721 } 722 i = min(siz, M_TRAILINGSPACE(mb)); 723 bcopy(auth_str, bpos, i); 724 mb->m_len += i; 725 auth_str += i; 726 bpos += i; 727 siz -= i; 728 } 729 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 730 for (i = 0; i < siz; i++) 731 *bpos++ = '\0'; 732 mb->m_len += siz; 733 } 734 break; 735 }; 736 737 /* 738 * And the verifier... 739 */ 740 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 741 if (verf_str) { 742 *tl++ = txdr_unsigned(RPCAUTH_KERB4); 743 *tl = txdr_unsigned(verf_len); 744 siz = verf_len; 745 while (siz > 0) { 746 if (M_TRAILINGSPACE(mb) == 0) { 747 MGET(mb2, M_WAIT, MT_DATA); 748 if (siz >= MINCLSIZE) 749 MCLGET(mb2, M_WAIT); 750 mb->m_next = mb2; 751 mb = mb2; 752 mb->m_len = 0; 753 bpos = mtod(mb, caddr_t); 754 } 755 i = min(siz, M_TRAILINGSPACE(mb)); 756 bcopy(verf_str, bpos, i); 757 mb->m_len += i; 758 verf_str += i; 759 bpos += i; 760 siz -= i; 761 } 762 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) { 763 for (i = 0; i < siz; i++) 764 *bpos++ = '\0'; 765 mb->m_len += siz; 766 } 767 } else { 768 *tl++ = txdr_unsigned(RPCAUTH_NULL); 769 *tl = 0; 770 } 771 mb->m_next = mrest; 772 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 773 mreq->m_pkthdr.rcvif = (struct ifnet *)0; 774 *mbp = mb; 775 return (mreq); 776} 777 778/* 779 * copies mbuf chain to the uio scatter/gather list 780 */ 781int 782nfsm_mbuftouio(mrep, uiop, siz, dpos) 783 struct mbuf **mrep; 784 register struct uio *uiop; 785 int siz; 786 caddr_t *dpos; 787{ 788 register char *mbufcp, *uiocp; 789 register int xfer, left, len; 790 register struct mbuf *mp; 791 long uiosiz, rem; 792 int error = 0; 793 794 mp = *mrep; 795 mbufcp = *dpos; 796 len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 797 rem = nfsm_rndup(siz)-siz; 798 while (siz > 0) { 799 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 800 return (EFBIG); 801 left = uiop->uio_iov->iov_len; 802 uiocp = uiop->uio_iov->iov_base; 803 if (left > siz) 804 left = siz; 805 uiosiz = left; 806 while (left > 0) { 807 while (len == 0) { 808 mp = mp->m_next; 809 if (mp == NULL) 810 return (EBADRPC); 811 mbufcp = mtod(mp, caddr_t); 812 len = mp->m_len; 813 } 814 xfer = (left > len) ? len : left; 815#ifdef notdef 816 /* Not Yet.. */ 817 if (uiop->uio_iov->iov_op != NULL) 818 (*(uiop->uio_iov->iov_op)) 819 (mbufcp, uiocp, xfer); 820 else 821#endif 822 if (uiop->uio_segflg == UIO_SYSSPACE) 823 bcopy(mbufcp, uiocp, xfer); 824 else 825 copyout(mbufcp, uiocp, xfer); 826 left -= xfer; 827 len -= xfer; 828 mbufcp += xfer; 829 uiocp += xfer; 830 uiop->uio_offset += xfer; 831 uiop->uio_resid -= xfer; 832 } 833 if (uiop->uio_iov->iov_len <= siz) { 834 uiop->uio_iovcnt--; 835 uiop->uio_iov++; 836 } else { 837 uiop->uio_iov->iov_base += uiosiz; 838 uiop->uio_iov->iov_len -= uiosiz; 839 } 840 siz -= uiosiz; 841 } 842 *dpos = mbufcp; 843 *mrep = mp; 844 if (rem > 0) { 845 if (len < rem) 846 error = nfs_adv(mrep, dpos, rem, len); 847 else 848 *dpos += rem; 849 } 850 return (error); 851} 852 853/* 854 * copies a uio scatter/gather list to an mbuf chain. 855 * NOTE: can ony handle iovcnt == 1 856 */ 857int 858nfsm_uiotombuf(uiop, mq, siz, bpos) 859 register struct uio *uiop; 860 struct mbuf **mq; 861 int siz; 862 caddr_t *bpos; 863{ 864 register char *uiocp; 865 register struct mbuf *mp, *mp2; 866 register int xfer, left, mlen; 867 int uiosiz, clflg, rem; 868 char *cp; 869 870#ifdef DIAGNOSTIC 871 if (uiop->uio_iovcnt != 1) 872 panic("nfsm_uiotombuf: iovcnt != 1"); 873#endif 874 875 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 876 clflg = 1; 877 else 878 clflg = 0; 879 rem = nfsm_rndup(siz)-siz; 880 mp = mp2 = *mq; 881 while (siz > 0) { 882 left = uiop->uio_iov->iov_len; 883 uiocp = uiop->uio_iov->iov_base; 884 if (left > siz) 885 left = siz; 886 uiosiz = left; 887 while (left > 0) { 888 mlen = M_TRAILINGSPACE(mp); 889 if (mlen == 0) { 890 MGET(mp, M_WAIT, MT_DATA); 891 if (clflg) 892 MCLGET(mp, M_WAIT); 893 mp->m_len = 0; 894 mp2->m_next = mp; 895 mp2 = mp; 896 mlen = M_TRAILINGSPACE(mp); 897 } 898 xfer = (left > mlen) ? mlen : left; 899#ifdef notdef 900 /* Not Yet.. */ 901 if (uiop->uio_iov->iov_op != NULL) 902 (*(uiop->uio_iov->iov_op)) 903 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 904 else 905#endif 906 if (uiop->uio_segflg == UIO_SYSSPACE) 907 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 908 else 909 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 910 mp->m_len += xfer; 911 left -= xfer; 912 uiocp += xfer; 913 uiop->uio_offset += xfer; 914 uiop->uio_resid -= xfer; 915 } 916 uiop->uio_iov->iov_base += uiosiz; 917 uiop->uio_iov->iov_len -= uiosiz; 918 siz -= uiosiz; 919 } 920 if (rem > 0) { 921 if (rem > M_TRAILINGSPACE(mp)) { 922 MGET(mp, M_WAIT, MT_DATA); 923 mp->m_len = 0; 924 mp2->m_next = mp; 925 } 926 cp = mtod(mp, caddr_t)+mp->m_len; 927 for (left = 0; left < rem; left++) 928 *cp++ = '\0'; 929 mp->m_len += rem; 930 *bpos = cp; 931 } else 932 *bpos = mtod(mp, caddr_t)+mp->m_len; 933 *mq = mp; 934 return (0); 935} 936 937/* 938 * Help break down an mbuf chain by setting the first siz bytes contiguous 939 * pointed to by returned val. 940 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 941 * cases. (The macros use the vars. dpos and dpos2) 942 */ 943int 944nfsm_disct(mdp, dposp, siz, left, cp2) 945 struct mbuf **mdp; 946 caddr_t *dposp; 947 int siz; 948 int left; 949 caddr_t *cp2; 950{ 951 register struct mbuf *mp, *mp2; 952 register int siz2, xfer; 953 register caddr_t p; 954 955 mp = *mdp; 956 while (left == 0) { 957 *mdp = mp = mp->m_next; 958 if (mp == NULL) 959 return (EBADRPC); 960 left = mp->m_len; 961 *dposp = mtod(mp, caddr_t); 962 } 963 if (left >= siz) { 964 *cp2 = *dposp; 965 *dposp += siz; 966 } else if (mp->m_next == NULL) { 967 return (EBADRPC); 968 } else if (siz > MHLEN) { 969 panic("nfs S too big"); 970 } else { 971 MGET(mp2, M_WAIT, MT_DATA); 972 mp2->m_next = mp->m_next; 973 mp->m_next = mp2; 974 mp->m_len -= left; 975 mp = mp2; 976 *cp2 = p = mtod(mp, caddr_t); 977 bcopy(*dposp, p, left); /* Copy what was left */ 978 siz2 = siz-left; 979 p += left; 980 mp2 = mp->m_next; 981 /* Loop around copying up the siz2 bytes */ 982 while (siz2 > 0) { 983 if (mp2 == NULL) 984 return (EBADRPC); 985 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 986 if (xfer > 0) { 987 bcopy(mtod(mp2, caddr_t), p, xfer); 988 NFSMADV(mp2, xfer); 989 mp2->m_len -= xfer; 990 p += xfer; 991 siz2 -= xfer; 992 } 993 if (siz2 > 0) 994 mp2 = mp2->m_next; 995 } 996 mp->m_len = siz; 997 *mdp = mp2; 998 *dposp = mtod(mp2, caddr_t); 999 } 1000 return (0); 1001} 1002 1003/* 1004 * Advance the position in the mbuf chain. 1005 */ 1006int 1007nfs_adv(mdp, dposp, offs, left) 1008 struct mbuf **mdp; 1009 caddr_t *dposp; 1010 int offs; 1011 int left; 1012{ 1013 register struct mbuf *m; 1014 register int s; 1015 1016 m = *mdp; 1017 s = left; 1018 while (s < offs) { 1019 offs -= s; 1020 m = m->m_next; 1021 if (m == NULL) 1022 return (EBADRPC); 1023 s = m->m_len; 1024 } 1025 *mdp = m; 1026 *dposp = mtod(m, caddr_t)+offs; 1027 return (0); 1028} 1029 1030/* 1031 * Copy a string into mbufs for the hard cases... 1032 */ 1033int 1034nfsm_strtmbuf(mb, bpos, cp, siz) 1035 struct mbuf **mb; 1036 char **bpos; 1037 const char *cp; 1038 long siz; 1039{ 1040 register struct mbuf *m1 = NULL, *m2; 1041 long left, xfer, len, tlen; 1042 u_int32_t *tl; 1043 int putsize; 1044 1045 putsize = 1; 1046 m2 = *mb; 1047 left = M_TRAILINGSPACE(m2); 1048 if (left > 0) { 1049 tl = ((u_int32_t *)(*bpos)); 1050 *tl++ = txdr_unsigned(siz); 1051 putsize = 0; 1052 left -= NFSX_UNSIGNED; 1053 m2->m_len += NFSX_UNSIGNED; 1054 if (left > 0) { 1055 bcopy(cp, (caddr_t) tl, left); 1056 siz -= left; 1057 cp += left; 1058 m2->m_len += left; 1059 left = 0; 1060 } 1061 } 1062 /* Loop around adding mbufs */ 1063 while (siz > 0) { 1064 MGET(m1, M_WAIT, MT_DATA); 1065 if (siz > MLEN) 1066 MCLGET(m1, M_WAIT); 1067 m1->m_len = NFSMSIZ(m1); 1068 m2->m_next = m1; 1069 m2 = m1; 1070 tl = mtod(m1, u_int32_t *); 1071 tlen = 0; 1072 if (putsize) { 1073 *tl++ = txdr_unsigned(siz); 1074 m1->m_len -= NFSX_UNSIGNED; 1075 tlen = NFSX_UNSIGNED; 1076 putsize = 0; 1077 } 1078 if (siz < m1->m_len) { 1079 len = nfsm_rndup(siz); 1080 xfer = siz; 1081 if (xfer < len) 1082 *(tl+(xfer>>2)) = 0; 1083 } else { 1084 xfer = len = m1->m_len; 1085 } 1086 bcopy(cp, (caddr_t) tl, xfer); 1087 m1->m_len = len+tlen; 1088 siz -= xfer; 1089 cp += xfer; 1090 } 1091 *mb = m1; 1092 *bpos = mtod(m1, caddr_t)+m1->m_len; 1093 return (0); 1094} 1095 1096/* 1097 * Called once to initialize data structures... 1098 */ 1099int 1100nfs_init(vfsp) 1101 struct vfsconf *vfsp; 1102{ 1103 register int i; 1104 1105 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1); 1106 1107 nfs_mount_type = vfsp->vfc_typenum; 1108 nfsrtt.pos = 0; 1109 rpc_vers = txdr_unsigned(RPC_VER2); 1110 rpc_call = txdr_unsigned(RPC_CALL); 1111 rpc_reply = txdr_unsigned(RPC_REPLY); 1112 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1113 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1114 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1115 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1116 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1117 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1118 nfs_prog = txdr_unsigned(NFS_PROG); 1119 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 1120 nfs_true = txdr_unsigned(TRUE); 1121 nfs_false = txdr_unsigned(FALSE); 1122 nfs_xdrneg1 = txdr_unsigned(-1); 1123 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1124 if (nfs_ticks < 1) 1125 nfs_ticks = 1; 1126 /* Ensure async daemons disabled */ 1127 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 1128 nfs_iodwant[i] = (struct proc *)0; 1129 nfs_iodmount[i] = (struct nfsmount *)0; 1130 } 1131 nfs_nhinit(); /* Init the nfsnode table */ 1132#ifndef NFS_NOSERVER 1133 nfsrv_init(0); /* Init server data structures */ 1134 nfsrv_initcache(); /* Init the server request cache */ 1135#endif 1136 1137 /* 1138 * Initialize the nqnfs server stuff. 1139 */ 1140 if (nqnfsstarttime == 0) { 1141 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 1142 + nqsrv_clockskew + nqsrv_writeslack; 1143 NQLOADNOVRAM(nqnfsstarttime); 1144 CIRCLEQ_INIT(&nqtimerhead); 1145 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash); 1146 } 1147 1148 /* 1149 * Initialize reply list and start timer 1150 */ 1151 TAILQ_INIT(&nfs_reqq); 1152 1153 nfs_timer(0); 1154 1155 /* 1156 * Set up lease_check and lease_updatetime so that other parts 1157 * of the system can call us, if we are loadable. 1158 */ 1159#ifndef NFS_NOSERVER 1160 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)]; 1161 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check; 1162#endif 1163 nfs_prev_lease_updatetime = lease_updatetime; 1164 lease_updatetime = nfs_lease_updatetime; 1165 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg; 1166 sysent[SYS_nfssvc].sy_narg = 2; 1167 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call; 1168 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc; 1169 1170 nfs_pbuf_freecnt = nswbuf / 2 + 1; 1171 1172 return (0); 1173} 1174 1175int 1176nfs_uninit(vfsp) 1177 struct vfsconf *vfsp; 1178{ 1179 1180 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle); 1181 nfs_mount_type = -1; 1182#ifndef NFS_NOSERVER 1183 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check; 1184#endif 1185 lease_updatetime = nfs_prev_lease_updatetime; 1186 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 1187 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 1188 return (0); 1189} 1190 1191/* 1192 * Attribute cache routines. 1193 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1194 * that are on the mbuf list 1195 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1196 * error otherwise 1197 */ 1198 1199/* 1200 * Load the attribute cache (that lives in the nfsnode entry) with 1201 * the values on the mbuf list and 1202 * Iff vap not NULL 1203 * copy the attributes to *vaper 1204 */ 1205int 1206nfs_loadattrcache(vpp, mdp, dposp, vaper) 1207 struct vnode **vpp; 1208 struct mbuf **mdp; 1209 caddr_t *dposp; 1210 struct vattr *vaper; 1211{ 1212 register struct vnode *vp = *vpp; 1213 register struct vattr *vap; 1214 register struct nfs_fattr *fp; 1215 register struct nfsnode *np; 1216 register int32_t t1; 1217 caddr_t cp2; 1218 int error = 0, rdev; 1219 struct mbuf *md; 1220 enum vtype vtyp; 1221 u_short vmode; 1222 struct timespec mtime; 1223 int v3 = NFS_ISV3(vp); 1224 1225 md = *mdp; 1226 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1227 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0) 1228 return (error); 1229 fp = (struct nfs_fattr *)cp2; 1230 if (v3) { 1231 vtyp = nfsv3tov_type(fp->fa_type); 1232 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1233 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 1234 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 1235 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1236 } else { 1237 vtyp = nfsv2tov_type(fp->fa_type); 1238 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1239 /* 1240 * XXX 1241 * 1242 * The duplicate information returned in fa_type and fa_mode 1243 * is an ambiguity in the NFS version 2 protocol. 1244 * 1245 * VREG should be taken literally as a regular file. If a 1246 * server intents to return some type information differently 1247 * in the upper bits of the mode field (e.g. for sockets, or 1248 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 1249 * leave the examination of the mode bits even in the VREG 1250 * case to avoid breakage for bogus servers, but we make sure 1251 * that there are actually type bits set in the upper part of 1252 * fa_mode (and failing that, trust the va_type field). 1253 * 1254 * NFSv3 cleared the issue, and requires fa_mode to not 1255 * contain any type information (while also introduing sockets 1256 * and FIFOs for fa_type). 1257 */ 1258 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 1259 vtyp = IFTOVT(vmode); 1260 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1261 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1262 1263 /* 1264 * Really ugly NFSv2 kludge. 1265 */ 1266 if (vtyp == VCHR && rdev == 0xffffffff) 1267 vtyp = VFIFO; 1268 } 1269 1270 /* 1271 * If v_type == VNON it is a new node, so fill in the v_type, 1272 * n_mtime fields. Check to see if it represents a special 1273 * device, and if so, check for a possible alias. Once the 1274 * correct vnode has been obtained, fill in the rest of the 1275 * information. 1276 */ 1277 np = VTONFS(vp); 1278 if (vp->v_type != vtyp) { 1279 vp->v_type = vtyp; 1280 if (vp->v_type == VFIFO) { 1281 vp->v_op = fifo_nfsv2nodeop_p; 1282 } 1283 if (vp->v_type == VCHR || vp->v_type == VBLK) { 1284 vp->v_op = spec_nfsv2nodeop_p; 1285 vp = addaliasu(vp, rdev); 1286 np->n_vnode = vp; 1287 } 1288 np->n_mtime = mtime.tv_sec; 1289 } 1290 vap = &np->n_vattr; 1291 vap->va_type = vtyp; 1292 vap->va_mode = (vmode & 07777); 1293 vap->va_rdev = rdev; 1294 vap->va_mtime = mtime; 1295 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1296 if (v3) { 1297 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1298 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1299 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1300 vap->va_size = fxdr_hyper(&fp->fa3_size); 1301 vap->va_blocksize = NFS_FABLKSIZE; 1302 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 1303 vap->va_fileid = fxdr_unsigned(int32_t, 1304 fp->fa3_fileid.nfsuquad[1]); 1305 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1306 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 1307 vap->va_flags = 0; 1308 vap->va_filerev = 0; 1309 } else { 1310 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1311 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1312 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1313 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1314 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 1315 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 1316 * NFS_FABLKSIZE; 1317 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1318 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1319 vap->va_flags = 0; 1320 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 1321 fp->fa2_ctime.nfsv2_sec); 1322 vap->va_ctime.tv_nsec = 0; 1323 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1324 vap->va_filerev = 0; 1325 } 1326 if (vap->va_size != np->n_size) { 1327 if (vap->va_type == VREG) { 1328 if (np->n_flag & NMODIFIED) { 1329 if (vap->va_size < np->n_size) 1330 vap->va_size = np->n_size; 1331 else 1332 np->n_size = vap->va_size; 1333 } else { 1334 np->n_size = vap->va_size; 1335 } 1336 vnode_pager_setsize(vp, np->n_size); 1337 } else { 1338 np->n_size = vap->va_size; 1339 } 1340 } 1341 np->n_attrstamp = time_second; 1342 if (vaper != NULL) { 1343 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 1344 if (np->n_flag & NCHG) { 1345 if (np->n_flag & NACC) 1346 vaper->va_atime = np->n_atim; 1347 if (np->n_flag & NUPD) 1348 vaper->va_mtime = np->n_mtim; 1349 } 1350 } 1351 return (0); 1352} 1353 1354#ifdef NFS_ACDEBUG 1355#include <sys/sysctl.h> 1356SYSCTL_DECL(_vfs_nfs); 1357static int nfs_acdebug; 1358SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 1359#endif 1360 1361/* 1362 * Check the time stamp 1363 * If the cache is valid, copy contents to *vap and return 0 1364 * otherwise return an error 1365 */ 1366int 1367nfs_getattrcache(vp, vaper) 1368 register struct vnode *vp; 1369 struct vattr *vaper; 1370{ 1371 register struct nfsnode *np; 1372 register struct vattr *vap; 1373 struct nfsmount *nmp; 1374 int timeo; 1375 1376 np = VTONFS(vp); 1377 vap = &np->n_vattr; 1378 nmp = VFSTONFS(vp->v_mount); 1379 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */ 1380 timeo = (time_second - np->n_mtime) / 10; 1381 1382#ifdef NFS_ACDEBUG 1383 if (nfs_acdebug>1) 1384 printf("nfs_getattrcache: initial timeo = %d\n", timeo); 1385#endif 1386 1387 if (vap->va_type == VDIR) { 1388 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin) 1389 timeo = nmp->nm_acdirmin; 1390 else if (timeo > nmp->nm_acdirmax) 1391 timeo = nmp->nm_acdirmax; 1392 } else { 1393 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin) 1394 timeo = nmp->nm_acregmin; 1395 else if (timeo > nmp->nm_acregmax) 1396 timeo = nmp->nm_acregmax; 1397 } 1398 1399#ifdef NFS_ACDEBUG 1400 if (nfs_acdebug > 2) 1401 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 1402 nmp->nm_acregmin, nmp->nm_acregmax, 1403 nmp->nm_acdirmin, nmp->nm_acdirmax); 1404 1405 if (nfs_acdebug) 1406 printf("nfs_getattrcache: age = %d; final timeo = %d\n", 1407 (time_second - np->n_attrstamp), timeo); 1408#endif 1409 1410 if ((time_second - np->n_attrstamp) >= timeo) { 1411 nfsstats.attrcache_misses++; 1412 return (ENOENT); 1413 } 1414 nfsstats.attrcache_hits++; 1415 if (vap->va_size != np->n_size) { 1416 if (vap->va_type == VREG) { 1417 if (np->n_flag & NMODIFIED) { 1418 if (vap->va_size < np->n_size) 1419 vap->va_size = np->n_size; 1420 else 1421 np->n_size = vap->va_size; 1422 } else { 1423 np->n_size = vap->va_size; 1424 } 1425 vnode_pager_setsize(vp, np->n_size); 1426 } else { 1427 np->n_size = vap->va_size; 1428 } 1429 } 1430 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 1431 if (np->n_flag & NCHG) { 1432 if (np->n_flag & NACC) 1433 vaper->va_atime = np->n_atim; 1434 if (np->n_flag & NUPD) 1435 vaper->va_mtime = np->n_mtim; 1436 } 1437 return (0); 1438} 1439 1440#ifndef NFS_NOSERVER 1441/* 1442 * Set up nameidata for a lookup() call and do it. 1443 * 1444 * If pubflag is set, this call is done for a lookup operation on the 1445 * public filehandle. In that case we allow crossing mountpoints and 1446 * absolute pathnames. However, the caller is expected to check that 1447 * the lookup result is within the public fs, and deny access if 1448 * it is not. 1449 * 1450 * nfs_namei() clears out garbage fields that namei() might leave garbage. 1451 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no 1452 * error occurs but the parent was not requested. 1453 * 1454 * dirp may be set whether an error is returned or not, and must be 1455 * released by the caller. 1456 */ 1457int 1458nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag) 1459 register struct nameidata *ndp; 1460 fhandle_t *fhp; 1461 int len; 1462 struct nfssvc_sock *slp; 1463 struct sockaddr *nam; 1464 struct mbuf **mdp; 1465 caddr_t *dposp; 1466 struct vnode **retdirp; 1467 struct proc *p; 1468 int kerbflag, pubflag; 1469{ 1470 register int i, rem; 1471 register struct mbuf *md; 1472 register char *fromcp, *tocp, *cp; 1473 struct iovec aiov; 1474 struct uio auio; 1475 struct vnode *dp; 1476 int error, rdonly, linklen; 1477 struct componentname *cnp = &ndp->ni_cnd; 1478 1479 *retdirp = (struct vnode *)0; 1480 cnp->cn_pnbuf = zalloc(namei_zone); 1481 1482 /* 1483 * Copy the name from the mbuf list to ndp->ni_pnbuf 1484 * and set the various ndp fields appropriately. 1485 */ 1486 fromcp = *dposp; 1487 tocp = cnp->cn_pnbuf; 1488 md = *mdp; 1489 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1490 for (i = 0; i < len; i++) { 1491 while (rem == 0) { 1492 md = md->m_next; 1493 if (md == NULL) { 1494 error = EBADRPC; 1495 goto out; 1496 } 1497 fromcp = mtod(md, caddr_t); 1498 rem = md->m_len; 1499 } 1500 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1501 error = EACCES; 1502 goto out; 1503 } 1504 *tocp++ = *fromcp++; 1505 rem--; 1506 } 1507 *tocp = '\0'; 1508 *mdp = md; 1509 *dposp = fromcp; 1510 len = nfsm_rndup(len)-len; 1511 if (len > 0) { 1512 if (rem >= len) 1513 *dposp += len; 1514 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1515 goto out; 1516 } 1517 1518 /* 1519 * Extract and set starting directory. 1520 */ 1521 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 1522 nam, &rdonly, kerbflag, pubflag); 1523 if (error) 1524 goto out; 1525 if (dp->v_type != VDIR) { 1526 vrele(dp); 1527 error = ENOTDIR; 1528 goto out; 1529 } 1530 1531 if (rdonly) 1532 cnp->cn_flags |= RDONLY; 1533 1534 /* 1535 * Set return directory. Reference to dp is implicitly transfered 1536 * to the returned pointer 1537 */ 1538 *retdirp = dp; 1539 1540 if (pubflag) { 1541 /* 1542 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1543 * and the 'native path' indicator. 1544 */ 1545 cp = zalloc(namei_zone); 1546 fromcp = cnp->cn_pnbuf; 1547 tocp = cp; 1548 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1549 switch ((unsigned char)*fromcp) { 1550 case WEBNFS_NATIVE_CHAR: 1551 /* 1552 * 'Native' path for us is the same 1553 * as a path according to the NFS spec, 1554 * just skip the escape char. 1555 */ 1556 fromcp++; 1557 break; 1558 /* 1559 * More may be added in the future, range 0x80-0xff 1560 */ 1561 default: 1562 error = EIO; 1563 zfree(namei_zone, cp); 1564 goto out; 1565 } 1566 } 1567 /* 1568 * Translate the '%' escapes, URL-style. 1569 */ 1570 while (*fromcp != '\0') { 1571 if (*fromcp == WEBNFS_ESC_CHAR) { 1572 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1573 fromcp++; 1574 *tocp++ = HEXSTRTOI(fromcp); 1575 fromcp += 2; 1576 continue; 1577 } else { 1578 error = ENOENT; 1579 zfree(namei_zone, cp); 1580 goto out; 1581 } 1582 } else 1583 *tocp++ = *fromcp++; 1584 } 1585 *tocp = '\0'; 1586 zfree(namei_zone, cnp->cn_pnbuf); 1587 cnp->cn_pnbuf = cp; 1588 } 1589 1590 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1; 1591 ndp->ni_segflg = UIO_SYSSPACE; 1592 1593 if (pubflag) { 1594 ndp->ni_rootdir = rootvnode; 1595 ndp->ni_loopcnt = 0; 1596 if (cnp->cn_pnbuf[0] == '/') 1597 dp = rootvnode; 1598 } else { 1599 cnp->cn_flags |= NOCROSSMOUNT; 1600 } 1601 1602 /* 1603 * Initialize for scan, set ni_startdir and bump ref on dp again 1604 * becuase lookup() will dereference ni_startdir. 1605 */ 1606 1607 cnp->cn_proc = p; 1608 VREF(dp); 1609 ndp->ni_startdir = dp; 1610 1611 for (;;) { 1612 cnp->cn_nameptr = cnp->cn_pnbuf; 1613 /* 1614 * Call lookup() to do the real work. If an error occurs, 1615 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 1616 * we do not have to dereference anything before returning. 1617 * In either case ni_startdir will be dereferenced and NULLed 1618 * out. 1619 */ 1620 error = lookup(ndp); 1621 if (error) 1622 break; 1623 1624 /* 1625 * Check for encountering a symbolic link. Trivial 1626 * termination occurs if no symlink encountered. 1627 * Note: zfree is safe because error is 0, so we will 1628 * not zfree it again when we break. 1629 */ 1630 if ((cnp->cn_flags & ISSYMLINK) == 0) { 1631 nfsrv_object_create(ndp->ni_vp); 1632 if (cnp->cn_flags & (SAVENAME | SAVESTART)) 1633 cnp->cn_flags |= HASBUF; 1634 else 1635 zfree(namei_zone, cnp->cn_pnbuf); 1636 break; 1637 } 1638 1639 /* 1640 * Validate symlink 1641 */ 1642 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 1643 VOP_UNLOCK(ndp->ni_dvp, 0, p); 1644 if (!pubflag) { 1645 error = EINVAL; 1646 goto badlink2; 1647 } 1648 1649 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 1650 error = ELOOP; 1651 goto badlink2; 1652 } 1653 if (ndp->ni_pathlen > 1) 1654 cp = zalloc(namei_zone); 1655 else 1656 cp = cnp->cn_pnbuf; 1657 aiov.iov_base = cp; 1658 aiov.iov_len = MAXPATHLEN; 1659 auio.uio_iov = &aiov; 1660 auio.uio_iovcnt = 1; 1661 auio.uio_offset = 0; 1662 auio.uio_rw = UIO_READ; 1663 auio.uio_segflg = UIO_SYSSPACE; 1664 auio.uio_procp = (struct proc *)0; 1665 auio.uio_resid = MAXPATHLEN; 1666 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 1667 if (error) { 1668 badlink1: 1669 if (ndp->ni_pathlen > 1) 1670 zfree(namei_zone, cp); 1671 badlink2: 1672 vrele(ndp->ni_dvp); 1673 vput(ndp->ni_vp); 1674 break; 1675 } 1676 linklen = MAXPATHLEN - auio.uio_resid; 1677 if (linklen == 0) { 1678 error = ENOENT; 1679 goto badlink1; 1680 } 1681 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 1682 error = ENAMETOOLONG; 1683 goto badlink1; 1684 } 1685 1686 /* 1687 * Adjust or replace path 1688 */ 1689 if (ndp->ni_pathlen > 1) { 1690 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 1691 zfree(namei_zone, cnp->cn_pnbuf); 1692 cnp->cn_pnbuf = cp; 1693 } else 1694 cnp->cn_pnbuf[linklen] = '\0'; 1695 ndp->ni_pathlen += linklen; 1696 1697 /* 1698 * Cleanup refs for next loop and check if root directory 1699 * should replace current directory. Normally ni_dvp 1700 * becomes the new base directory and is cleaned up when 1701 * we loop. Explicitly null pointers after invalidation 1702 * to clarify operation. 1703 */ 1704 vput(ndp->ni_vp); 1705 ndp->ni_vp = NULL; 1706 1707 if (cnp->cn_pnbuf[0] == '/') { 1708 vrele(ndp->ni_dvp); 1709 ndp->ni_dvp = ndp->ni_rootdir; 1710 VREF(ndp->ni_dvp); 1711 } 1712 ndp->ni_startdir = ndp->ni_dvp; 1713 ndp->ni_dvp = NULL; 1714 } 1715 1716 /* 1717 * nfs_namei() guarentees that fields will not contain garbage 1718 * whether an error occurs or not. This allows the caller to track 1719 * cleanup state trivially. 1720 */ 1721out: 1722 if (error) { 1723 zfree(namei_zone, cnp->cn_pnbuf); 1724 ndp->ni_vp = NULL; 1725 ndp->ni_dvp = NULL; 1726 ndp->ni_startdir = NULL; 1727 cnp->cn_flags &= ~HASBUF; 1728 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 1729 ndp->ni_dvp = NULL; 1730 } 1731 return (error); 1732} 1733 1734/* 1735 * A fiddled version of m_adj() that ensures null fill to a long 1736 * boundary and only trims off the back end 1737 */ 1738void 1739nfsm_adj(mp, len, nul) 1740 struct mbuf *mp; 1741 register int len; 1742 int nul; 1743{ 1744 register struct mbuf *m; 1745 register int count, i; 1746 register char *cp; 1747 1748 /* 1749 * Trim from tail. Scan the mbuf chain, 1750 * calculating its length and finding the last mbuf. 1751 * If the adjustment only affects this mbuf, then just 1752 * adjust and return. Otherwise, rescan and truncate 1753 * after the remaining size. 1754 */ 1755 count = 0; 1756 m = mp; 1757 for (;;) { 1758 count += m->m_len; 1759 if (m->m_next == (struct mbuf *)0) 1760 break; 1761 m = m->m_next; 1762 } 1763 if (m->m_len > len) { 1764 m->m_len -= len; 1765 if (nul > 0) { 1766 cp = mtod(m, caddr_t)+m->m_len-nul; 1767 for (i = 0; i < nul; i++) 1768 *cp++ = '\0'; 1769 } 1770 return; 1771 } 1772 count -= len; 1773 if (count < 0) 1774 count = 0; 1775 /* 1776 * Correct length for chain is "count". 1777 * Find the mbuf with last data, adjust its length, 1778 * and toss data from remaining mbufs on chain. 1779 */ 1780 for (m = mp; m; m = m->m_next) { 1781 if (m->m_len >= count) { 1782 m->m_len = count; 1783 if (nul > 0) { 1784 cp = mtod(m, caddr_t)+m->m_len-nul; 1785 for (i = 0; i < nul; i++) 1786 *cp++ = '\0'; 1787 } 1788 break; 1789 } 1790 count -= m->m_len; 1791 } 1792 for (m = m->m_next;m;m = m->m_next) 1793 m->m_len = 0; 1794} 1795 1796/* 1797 * Make these functions instead of macros, so that the kernel text size 1798 * doesn't get too big... 1799 */ 1800void 1801nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp) 1802 struct nfsrv_descript *nfsd; 1803 int before_ret; 1804 register struct vattr *before_vap; 1805 int after_ret; 1806 struct vattr *after_vap; 1807 struct mbuf **mbp; 1808 char **bposp; 1809{ 1810 register struct mbuf *mb = *mbp, *mb2; 1811 register char *bpos = *bposp; 1812 register u_int32_t *tl; 1813 1814 if (before_ret) { 1815 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1816 *tl = nfs_false; 1817 } else { 1818 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1819 *tl++ = nfs_true; 1820 txdr_hyper(before_vap->va_size, tl); 1821 tl += 2; 1822 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1823 tl += 2; 1824 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1825 } 1826 *bposp = bpos; 1827 *mbp = mb; 1828 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1829} 1830 1831void 1832nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp) 1833 struct nfsrv_descript *nfsd; 1834 int after_ret; 1835 struct vattr *after_vap; 1836 struct mbuf **mbp; 1837 char **bposp; 1838{ 1839 register struct mbuf *mb = *mbp, *mb2; 1840 register char *bpos = *bposp; 1841 register u_int32_t *tl; 1842 register struct nfs_fattr *fp; 1843 1844 if (after_ret) { 1845 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1846 *tl = nfs_false; 1847 } else { 1848 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1849 *tl++ = nfs_true; 1850 fp = (struct nfs_fattr *)tl; 1851 nfsm_srvfattr(nfsd, after_vap, fp); 1852 } 1853 *mbp = mb; 1854 *bposp = bpos; 1855} 1856 1857void 1858nfsm_srvfattr(nfsd, vap, fp) 1859 register struct nfsrv_descript *nfsd; 1860 register struct vattr *vap; 1861 register struct nfs_fattr *fp; 1862{ 1863 1864 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1865 fp->fa_uid = txdr_unsigned(vap->va_uid); 1866 fp->fa_gid = txdr_unsigned(vap->va_gid); 1867 if (nfsd->nd_flag & ND_NFSV3) { 1868 fp->fa_type = vtonfsv3_type(vap->va_type); 1869 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1870 txdr_hyper(vap->va_size, &fp->fa3_size); 1871 txdr_hyper(vap->va_bytes, &fp->fa3_used); 1872 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev)); 1873 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev)); 1874 fp->fa3_fsid.nfsuquad[0] = 0; 1875 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1876 fp->fa3_fileid.nfsuquad[0] = 0; 1877 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid); 1878 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1879 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1880 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1881 } else { 1882 fp->fa_type = vtonfsv2_type(vap->va_type); 1883 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1884 fp->fa2_size = txdr_unsigned(vap->va_size); 1885 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1886 if (vap->va_type == VFIFO) 1887 fp->fa2_rdev = 0xffffffff; 1888 else 1889 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1890 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1891 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1892 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1893 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1894 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1895 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1896 } 1897} 1898 1899/* 1900 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1901 * - look up fsid in mount list (if not found ret error) 1902 * - get vp and export rights by calling VFS_FHTOVP() 1903 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1904 * - if not lockflag unlock it with VOP_UNLOCK() 1905 */ 1906int 1907nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag) 1908 fhandle_t *fhp; 1909 int lockflag; 1910 struct vnode **vpp; 1911 struct ucred *cred; 1912 struct nfssvc_sock *slp; 1913 struct sockaddr *nam; 1914 int *rdonlyp; 1915 int kerbflag; 1916 int pubflag; 1917{ 1918 struct proc *p = curproc; /* XXX */ 1919 register struct mount *mp; 1920 register int i; 1921 struct ucred *credanon; 1922 int error, exflags; 1923#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1924 struct sockaddr_int *saddr; 1925#endif 1926 1927 *vpp = (struct vnode *)0; 1928 1929 if (nfs_ispublicfh(fhp)) { 1930 if (!pubflag || !nfs_pub.np_valid) 1931 return (ESTALE); 1932 fhp = &nfs_pub.np_handle; 1933 } 1934 1935 mp = vfs_getvfs(&fhp->fh_fsid); 1936 if (!mp) 1937 return (ESTALE); 1938 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon); 1939 if (error) 1940 return (error); 1941 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 1942 if (error) 1943 return (error); 1944#ifdef MNT_EXNORESPORT 1945 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1946 saddr = (struct sockaddr_in *)nam; 1947 if (saddr->sin_family == AF_INET && 1948 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1949 vput(*vpp); 1950 *vpp = NULL; 1951 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1952 } 1953 } 1954#endif 1955 /* 1956 * Check/setup credentials. 1957 */ 1958 if (exflags & MNT_EXKERB) { 1959 if (!kerbflag) { 1960 vput(*vpp); 1961 *vpp = NULL; 1962 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1963 } 1964 } else if (kerbflag) { 1965 vput(*vpp); 1966 *vpp = NULL; 1967 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1968 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1969 cred->cr_uid = credanon->cr_uid; 1970 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1971 cred->cr_groups[i] = credanon->cr_groups[i]; 1972 cred->cr_ngroups = i; 1973 } 1974 if (exflags & MNT_EXRDONLY) 1975 *rdonlyp = 1; 1976 else 1977 *rdonlyp = 0; 1978 1979 nfsrv_object_create(*vpp); 1980 1981 if (!lockflag) 1982 VOP_UNLOCK(*vpp, 0, p); 1983 return (0); 1984} 1985 1986 1987/* 1988 * WebNFS: check if a filehandle is a public filehandle. For v3, this 1989 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 1990 * transformed this to all zeroes in both cases, so check for it. 1991 */ 1992int 1993nfs_ispublicfh(fhp) 1994 fhandle_t *fhp; 1995{ 1996 char *cp = (char *)fhp; 1997 int i; 1998 1999 for (i = 0; i < NFSX_V3FH; i++) 2000 if (*cp++ != 0) 2001 return (FALSE); 2002 return (TRUE); 2003} 2004 2005#endif /* NFS_NOSERVER */ 2006/* 2007 * This function compares two net addresses by family and returns TRUE 2008 * if they are the same host. 2009 * If there is any doubt, return FALSE. 2010 * The AF_INET family is handled as a special case so that address mbufs 2011 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 2012 */ 2013int 2014netaddr_match(family, haddr, nam) 2015 int family; 2016 union nethostaddr *haddr; 2017 struct sockaddr *nam; 2018{ 2019 register struct sockaddr_in *inetaddr; 2020 2021 switch (family) { 2022 case AF_INET: 2023 inetaddr = (struct sockaddr_in *)nam; 2024 if (inetaddr->sin_family == AF_INET && 2025 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 2026 return (1); 2027 break; 2028 default: 2029 break; 2030 }; 2031 return (0); 2032} 2033 2034static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 2035/* 2036 * This function finds the directory cookie that corresponds to the 2037 * logical byte offset given. 2038 */ 2039nfsuint64 * 2040nfs_getcookie(np, off, add) 2041 register struct nfsnode *np; 2042 off_t off; 2043 int add; 2044{ 2045 register struct nfsdmap *dp, *dp2; 2046 register int pos; 2047 2048 pos = (uoff_t)off / NFS_DIRBLKSIZ; 2049 if (pos == 0 || off < 0) { 2050#ifdef DIAGNOSTIC 2051 if (add) 2052 panic("nfs getcookie add at <= 0"); 2053#endif 2054 return (&nfs_nullcookie); 2055 } 2056 pos--; 2057 dp = np->n_cookies.lh_first; 2058 if (!dp) { 2059 if (add) { 2060 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 2061 M_NFSDIROFF, M_WAITOK); 2062 dp->ndm_eocookie = 0; 2063 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 2064 } else 2065 return ((nfsuint64 *)0); 2066 } 2067 while (pos >= NFSNUMCOOKIES) { 2068 pos -= NFSNUMCOOKIES; 2069 if (dp->ndm_list.le_next) { 2070 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 2071 pos >= dp->ndm_eocookie) 2072 return ((nfsuint64 *)0); 2073 dp = dp->ndm_list.le_next; 2074 } else if (add) { 2075 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 2076 M_NFSDIROFF, M_WAITOK); 2077 dp2->ndm_eocookie = 0; 2078 LIST_INSERT_AFTER(dp, dp2, ndm_list); 2079 dp = dp2; 2080 } else 2081 return ((nfsuint64 *)0); 2082 } 2083 if (pos >= dp->ndm_eocookie) { 2084 if (add) 2085 dp->ndm_eocookie = pos + 1; 2086 else 2087 return ((nfsuint64 *)0); 2088 } 2089 return (&dp->ndm_cookies[pos]); 2090} 2091 2092/* 2093 * Invalidate cached directory information, except for the actual directory 2094 * blocks (which are invalidated separately). 2095 * Done mainly to avoid the use of stale offset cookies. 2096 */ 2097void 2098nfs_invaldir(vp) 2099 register struct vnode *vp; 2100{ 2101 register struct nfsnode *np = VTONFS(vp); 2102 2103#ifdef DIAGNOSTIC 2104 if (vp->v_type != VDIR) 2105 panic("nfs: invaldir not dir"); 2106#endif 2107 np->n_direofoffset = 0; 2108 np->n_cookieverf.nfsuquad[0] = 0; 2109 np->n_cookieverf.nfsuquad[1] = 0; 2110 if (np->n_cookies.lh_first) 2111 np->n_cookies.lh_first->ndm_eocookie = 0; 2112} 2113 2114/* 2115 * The write verifier has changed (probably due to a server reboot), so all 2116 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 2117 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 2118 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 2119 * mount point. 2120 * 2121 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 2122 * writes are not clusterable. 2123 */ 2124void 2125nfs_clearcommit(mp) 2126 struct mount *mp; 2127{ 2128 register struct vnode *vp, *nvp; 2129 register struct buf *bp, *nbp; 2130 int s; 2131 2132 s = splbio(); 2133loop: 2134 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 2135 if (vp->v_mount != mp) /* Paranoia */ 2136 goto loop; 2137 nvp = vp->v_mntvnodes.le_next; 2138 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 2139 nbp = TAILQ_NEXT(bp, b_vnbufs); 2140 if (BUF_REFCNT(bp) == 0 && 2141 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2142 == (B_DELWRI | B_NEEDCOMMIT)) 2143 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 2144 } 2145 } 2146 splx(s); 2147} 2148 2149#ifndef NFS_NOSERVER 2150/* 2151 * Map errnos to NFS error numbers. For Version 3 also filter out error 2152 * numbers not specified for the associated procedure. 2153 */ 2154int 2155nfsrv_errmap(nd, err) 2156 struct nfsrv_descript *nd; 2157 register int err; 2158{ 2159 register short *defaulterrp, *errp; 2160 2161 if (nd->nd_flag & ND_NFSV3) { 2162 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2163 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2164 while (*++errp) { 2165 if (*errp == err) 2166 return (err); 2167 else if (*errp > err) 2168 break; 2169 } 2170 return ((int)*defaulterrp); 2171 } else 2172 return (err & 0xffff); 2173 } 2174 if (err <= ELAST) 2175 return ((int)nfsrv_v2errmap[err - 1]); 2176 return (NFSERR_IO); 2177} 2178 2179int 2180nfsrv_object_create(vp) 2181 struct vnode *vp; 2182{ 2183 2184 if (vp == NULL || vp->v_type != VREG) 2185 return (1); 2186 return (vfs_object_create(vp, curproc, 2187 curproc ? curproc->p_ucred : NULL)); 2188} 2189 2190/* 2191 * Sort the group list in increasing numerical order. 2192 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2193 * that used to be here.) 2194 */ 2195void 2196nfsrvw_sort(list, num) 2197 register gid_t *list; 2198 register int num; 2199{ 2200 register int i, j; 2201 gid_t v; 2202 2203 /* Insertion sort. */ 2204 for (i = 1; i < num; i++) { 2205 v = list[i]; 2206 /* find correct slot for value v, moving others up */ 2207 for (j = i; --j >= 0 && v < list[j];) 2208 list[j + 1] = list[j]; 2209 list[j + 1] = v; 2210 } 2211} 2212 2213/* 2214 * copy credentials making sure that the result can be compared with bcmp(). 2215 */ 2216void 2217nfsrv_setcred(incred, outcred) 2218 register struct ucred *incred, *outcred; 2219{ 2220 register int i; 2221 2222 bzero((caddr_t)outcred, sizeof (struct ucred)); 2223 outcred->cr_ref = 1; 2224 outcred->cr_uid = incred->cr_uid; 2225 outcred->cr_ngroups = incred->cr_ngroups; 2226 for (i = 0; i < incred->cr_ngroups; i++) 2227 outcred->cr_groups[i] = incred->cr_groups[i]; 2228 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2229} 2230#endif /* NFS_NOSERVER */ 2231