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