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