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