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