1 /* $NetBSD: nfs_subs.c,v 1.156 2005/12/11 12:25:16 christos 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.156 2005/12/11 12:25:16 christos 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 #ifdef notdef 829 /* Not Yet.. */ 830 if (uiop->uio_iov->iov_op != NULL) 831 (*(uiop->uio_iov->iov_op)) 832 (mbufcp, uiocp, xfer); 833 else 834 #endif 835 if (uiop->uio_segflg == UIO_SYSSPACE) 836 memcpy(uiocp, mbufcp, xfer); 837 else 838 if ((error = copyout_proc(uiop->uio_lwp->l_proc, 839 mbufcp, uiocp, xfer)) != 0) 840 return error; 841 left -= xfer; 842 len -= xfer; 843 mbufcp += xfer; 844 uiocp += xfer; 845 uiop->uio_offset += xfer; 846 uiop->uio_resid -= xfer; 847 } 848 if (uiop->uio_iov->iov_len <= siz) { 849 uiop->uio_iovcnt--; 850 uiop->uio_iov++; 851 } else { 852 uiop->uio_iov->iov_base = 853 (caddr_t)uiop->uio_iov->iov_base + uiosiz; 854 uiop->uio_iov->iov_len -= uiosiz; 855 } 856 siz -= uiosiz; 857 } 858 *dpos = mbufcp; 859 *mrep = mp; 860 if (rem > 0) { 861 if (len < rem) 862 error = nfs_adv(mrep, dpos, rem, len); 863 else 864 *dpos += rem; 865 } 866 return (error); 867 } 868 869 /* 870 * copies a uio scatter/gather list to an mbuf chain. 871 * NOTE: can ony handle iovcnt == 1 872 */ 873 int 874 nfsm_uiotombuf(uiop, mq, siz, bpos) 875 struct uio *uiop; 876 struct mbuf **mq; 877 int siz; 878 caddr_t *bpos; 879 { 880 char *uiocp; 881 struct mbuf *mp, *mp2; 882 int xfer, left, mlen; 883 int uiosiz, clflg, rem; 884 char *cp; 885 886 #ifdef DIAGNOSTIC 887 if (uiop->uio_iovcnt != 1) 888 panic("nfsm_uiotombuf: iovcnt != 1"); 889 #endif 890 891 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 892 clflg = 1; 893 else 894 clflg = 0; 895 rem = nfsm_rndup(siz)-siz; 896 mp = mp2 = *mq; 897 while (siz > 0) { 898 left = uiop->uio_iov->iov_len; 899 uiocp = uiop->uio_iov->iov_base; 900 if (left > siz) 901 left = siz; 902 uiosiz = left; 903 while (left > 0) { 904 mlen = M_TRAILINGSPACE(mp); 905 if (mlen == 0) { 906 mp = m_get(M_WAIT, MT_DATA); 907 MCLAIM(mp, &nfs_mowner); 908 if (clflg) 909 m_clget(mp, M_WAIT); 910 mp->m_len = 0; 911 mp2->m_next = mp; 912 mp2 = mp; 913 mlen = M_TRAILINGSPACE(mp); 914 } 915 xfer = (left > mlen) ? mlen : left; 916 cp = mtod(mp, caddr_t) + mp->m_len; 917 #ifdef notdef 918 /* Not Yet.. */ 919 if (uiop->uio_iov->iov_op != NULL) 920 (*(uiop->uio_iov->iov_op))(uiocp, cp, xfer); 921 else 922 #endif 923 if (uiop->uio_segflg == UIO_SYSSPACE) 924 (void)memcpy(cp, uiocp, xfer); 925 else 926 /*XXX: Check error */ 927 (void)copyin_proc(uiop->uio_lwp->l_proc, uiocp, 928 cp, xfer); 929 mp->m_len += xfer; 930 left -= xfer; 931 uiocp += xfer; 932 uiop->uio_offset += xfer; 933 uiop->uio_resid -= xfer; 934 } 935 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base + 936 uiosiz; 937 uiop->uio_iov->iov_len -= uiosiz; 938 siz -= uiosiz; 939 } 940 if (rem > 0) { 941 if (rem > M_TRAILINGSPACE(mp)) { 942 mp = m_get(M_WAIT, MT_DATA); 943 MCLAIM(mp, &nfs_mowner); 944 mp->m_len = 0; 945 mp2->m_next = mp; 946 } 947 cp = mtod(mp, caddr_t) + mp->m_len; 948 for (left = 0; left < rem; left++) 949 *cp++ = '\0'; 950 mp->m_len += rem; 951 *bpos = cp; 952 } else 953 *bpos = mtod(mp, caddr_t)+mp->m_len; 954 *mq = mp; 955 return (0); 956 } 957 958 /* 959 * Get at least "siz" bytes of correctly aligned data. 960 * When called the mbuf pointers are not necessarily correct, 961 * dsosp points to what ought to be in m_data and left contains 962 * what ought to be in m_len. 963 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 964 * cases. (The macros use the vars. dpos and dpos2) 965 */ 966 int 967 nfsm_disct(mdp, dposp, siz, left, cp2) 968 struct mbuf **mdp; 969 caddr_t *dposp; 970 int siz; 971 int left; 972 caddr_t *cp2; 973 { 974 struct mbuf *m1, *m2; 975 struct mbuf *havebuf = NULL; 976 caddr_t src = *dposp; 977 caddr_t dst; 978 int len; 979 980 #ifdef DEBUG 981 if (left < 0) 982 panic("nfsm_disct: left < 0"); 983 #endif 984 m1 = *mdp; 985 /* 986 * Skip through the mbuf chain looking for an mbuf with 987 * some data. If the first mbuf found has enough data 988 * and it is correctly aligned return it. 989 */ 990 while (left == 0) { 991 havebuf = m1; 992 *mdp = m1 = m1->m_next; 993 if (m1 == NULL) 994 return (EBADRPC); 995 src = mtod(m1, caddr_t); 996 left = m1->m_len; 997 /* 998 * If we start a new mbuf and it is big enough 999 * and correctly aligned just return it, don't 1000 * do any pull up. 1001 */ 1002 if (left >= siz && nfsm_aligned(src)) { 1003 *cp2 = src; 1004 *dposp = src + siz; 1005 return (0); 1006 } 1007 } 1008 if (m1->m_flags & M_EXT) { 1009 if (havebuf) { 1010 /* If the first mbuf with data has external data 1011 * and there is a previous empty mbuf use it 1012 * to move the data into. 1013 */ 1014 m2 = m1; 1015 *mdp = m1 = havebuf; 1016 if (m1->m_flags & M_EXT) { 1017 MEXTREMOVE(m1); 1018 } 1019 } else { 1020 /* 1021 * If the first mbuf has a external data 1022 * and there is no previous empty mbuf 1023 * allocate a new mbuf and move the external 1024 * data to the new mbuf. Also make the first 1025 * mbuf look empty. 1026 */ 1027 m2 = m_get(M_WAIT, MT_DATA); 1028 m2->m_ext = m1->m_ext; 1029 m2->m_data = src; 1030 m2->m_len = left; 1031 MCLADDREFERENCE(m1, m2); 1032 MEXTREMOVE(m1); 1033 m2->m_next = m1->m_next; 1034 m1->m_next = m2; 1035 } 1036 m1->m_len = 0; 1037 if (m1->m_flags & M_PKTHDR) 1038 dst = m1->m_pktdat; 1039 else 1040 dst = m1->m_dat; 1041 m1->m_data = dst; 1042 } else { 1043 /* 1044 * If the first mbuf has no external data 1045 * move the data to the front of the mbuf. 1046 */ 1047 if (m1->m_flags & M_PKTHDR) 1048 dst = m1->m_pktdat; 1049 else 1050 dst = m1->m_dat; 1051 m1->m_data = dst; 1052 if (dst != src) 1053 memmove(dst, src, left); 1054 dst += left; 1055 m1->m_len = left; 1056 m2 = m1->m_next; 1057 } 1058 *cp2 = m1->m_data; 1059 *dposp = mtod(m1, caddr_t) + siz; 1060 /* 1061 * Loop through mbufs pulling data up into first mbuf until 1062 * the first mbuf is full or there is no more data to 1063 * pullup. 1064 */ 1065 while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) { 1066 if ((len = min(len, m2->m_len)) != 0) 1067 memcpy(dst, m2->m_data, len); 1068 m1->m_len += len; 1069 dst += len; 1070 m2->m_data += len; 1071 m2->m_len -= len; 1072 m2 = m2->m_next; 1073 } 1074 if (m1->m_len < siz) 1075 return (EBADRPC); 1076 return (0); 1077 } 1078 1079 /* 1080 * Advance the position in the mbuf chain. 1081 */ 1082 int 1083 nfs_adv(mdp, dposp, offs, left) 1084 struct mbuf **mdp; 1085 caddr_t *dposp; 1086 int offs; 1087 int left; 1088 { 1089 struct mbuf *m; 1090 int s; 1091 1092 m = *mdp; 1093 s = left; 1094 while (s < offs) { 1095 offs -= s; 1096 m = m->m_next; 1097 if (m == NULL) 1098 return (EBADRPC); 1099 s = m->m_len; 1100 } 1101 *mdp = m; 1102 *dposp = mtod(m, caddr_t)+offs; 1103 return (0); 1104 } 1105 1106 /* 1107 * Copy a string into mbufs for the hard cases... 1108 */ 1109 int 1110 nfsm_strtmbuf(mb, bpos, cp, siz) 1111 struct mbuf **mb; 1112 char **bpos; 1113 const char *cp; 1114 long siz; 1115 { 1116 struct mbuf *m1 = NULL, *m2; 1117 long left, xfer, len, tlen; 1118 u_int32_t *tl; 1119 int putsize; 1120 1121 putsize = 1; 1122 m2 = *mb; 1123 left = M_TRAILINGSPACE(m2); 1124 if (left > 0) { 1125 tl = ((u_int32_t *)(*bpos)); 1126 *tl++ = txdr_unsigned(siz); 1127 putsize = 0; 1128 left -= NFSX_UNSIGNED; 1129 m2->m_len += NFSX_UNSIGNED; 1130 if (left > 0) { 1131 memcpy((caddr_t) tl, cp, left); 1132 siz -= left; 1133 cp += left; 1134 m2->m_len += left; 1135 left = 0; 1136 } 1137 } 1138 /* Loop around adding mbufs */ 1139 while (siz > 0) { 1140 m1 = m_get(M_WAIT, MT_DATA); 1141 MCLAIM(m1, &nfs_mowner); 1142 if (siz > MLEN) 1143 m_clget(m1, M_WAIT); 1144 m1->m_len = NFSMSIZ(m1); 1145 m2->m_next = m1; 1146 m2 = m1; 1147 tl = mtod(m1, u_int32_t *); 1148 tlen = 0; 1149 if (putsize) { 1150 *tl++ = txdr_unsigned(siz); 1151 m1->m_len -= NFSX_UNSIGNED; 1152 tlen = NFSX_UNSIGNED; 1153 putsize = 0; 1154 } 1155 if (siz < m1->m_len) { 1156 len = nfsm_rndup(siz); 1157 xfer = siz; 1158 if (xfer < len) 1159 *(tl+(xfer>>2)) = 0; 1160 } else { 1161 xfer = len = m1->m_len; 1162 } 1163 memcpy((caddr_t) tl, cp, xfer); 1164 m1->m_len = len+tlen; 1165 siz -= xfer; 1166 cp += xfer; 1167 } 1168 *mb = m1; 1169 *bpos = mtod(m1, caddr_t)+m1->m_len; 1170 return (0); 1171 } 1172 1173 /* 1174 * Directory caching routines. They work as follows: 1175 * - a cache is maintained per VDIR nfsnode. 1176 * - for each offset cookie that is exported to userspace, and can 1177 * thus be thrown back at us as an offset to VOP_READDIR, store 1178 * information in the cache. 1179 * - cached are: 1180 * - cookie itself 1181 * - blocknumber (essentially just a search key in the buffer cache) 1182 * - entry number in block. 1183 * - offset cookie of block in which this entry is stored 1184 * - 32 bit cookie if NFSMNT_XLATECOOKIE is used. 1185 * - entries are looked up in a hash table 1186 * - also maintained is an LRU list of entries, used to determine 1187 * which ones to delete if the cache grows too large. 1188 * - if 32 <-> 64 translation mode is requested for a filesystem, 1189 * the cache also functions as a translation table 1190 * - in the translation case, invalidating the cache does not mean 1191 * flushing it, but just marking entries as invalid, except for 1192 * the <64bit cookie, 32bitcookie> pair which is still valid, to 1193 * still be able to use the cache as a translation table. 1194 * - 32 bit cookies are uniquely created by combining the hash table 1195 * entry value, and one generation count per hash table entry, 1196 * incremented each time an entry is appended to the chain. 1197 * - the cache is invalidated each time a direcory is modified 1198 * - sanity checks are also done; if an entry in a block turns 1199 * out not to have a matching cookie, the cache is invalidated 1200 * and a new block starting from the wanted offset is fetched from 1201 * the server. 1202 * - directory entries as read from the server are extended to contain 1203 * the 64bit and, optionally, the 32bit cookies, for sanity checking 1204 * the cache and exporting them to userspace through the cookie 1205 * argument to VOP_READDIR. 1206 */ 1207 1208 u_long 1209 nfs_dirhash(off) 1210 off_t off; 1211 { 1212 int i; 1213 char *cp = (char *)&off; 1214 u_long sum = 0L; 1215 1216 for (i = 0 ; i < sizeof (off); i++) 1217 sum += *cp++; 1218 1219 return sum; 1220 } 1221 1222 #define _NFSDC_MTX(np) (&NFSTOV(np)->v_interlock) 1223 #define NFSDC_LOCK(np) simple_lock(_NFSDC_MTX(np)) 1224 #define NFSDC_UNLOCK(np) simple_unlock(_NFSDC_MTX(np)) 1225 #define NFSDC_ASSERT_LOCKED(np) LOCK_ASSERT(simple_lock_held(_NFSDC_MTX(np))) 1226 1227 void 1228 nfs_initdircache(vp) 1229 struct vnode *vp; 1230 { 1231 struct nfsnode *np = VTONFS(vp); 1232 struct nfsdirhashhead *dircache; 1233 1234 dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF, 1235 M_WAITOK, &nfsdirhashmask); 1236 1237 NFSDC_LOCK(np); 1238 if (np->n_dircache == NULL) { 1239 np->n_dircachesize = 0; 1240 np->n_dircache = dircache; 1241 dircache = NULL; 1242 TAILQ_INIT(&np->n_dirchain); 1243 } 1244 NFSDC_UNLOCK(np); 1245 if (dircache) 1246 hashdone(dircache, M_NFSDIROFF); 1247 } 1248 1249 void 1250 nfs_initdirxlatecookie(vp) 1251 struct vnode *vp; 1252 { 1253 struct nfsnode *np = VTONFS(vp); 1254 unsigned *dirgens; 1255 1256 KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE); 1257 1258 dirgens = malloc(NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF, 1259 M_WAITOK|M_ZERO); 1260 NFSDC_LOCK(np); 1261 if (np->n_dirgens == NULL) { 1262 np->n_dirgens = dirgens; 1263 dirgens = NULL; 1264 } 1265 NFSDC_UNLOCK(np); 1266 if (dirgens) 1267 free(dirgens, M_NFSDIROFF); 1268 } 1269 1270 static const struct nfsdircache dzero; 1271 1272 static void nfs_unlinkdircache __P((struct nfsnode *np, struct nfsdircache *)); 1273 static void nfs_putdircache_unlocked __P((struct nfsnode *, 1274 struct nfsdircache *)); 1275 1276 static void 1277 nfs_unlinkdircache(np, ndp) 1278 struct nfsnode *np; 1279 struct nfsdircache *ndp; 1280 { 1281 1282 NFSDC_ASSERT_LOCKED(np); 1283 KASSERT(ndp != &dzero); 1284 1285 if (LIST_NEXT(ndp, dc_hash) == (void *)-1) 1286 return; 1287 1288 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain); 1289 LIST_REMOVE(ndp, dc_hash); 1290 LIST_NEXT(ndp, dc_hash) = (void *)-1; /* mark as unlinked */ 1291 1292 nfs_putdircache_unlocked(np, ndp); 1293 } 1294 1295 void 1296 nfs_putdircache(np, ndp) 1297 struct nfsnode *np; 1298 struct nfsdircache *ndp; 1299 { 1300 int ref; 1301 1302 if (ndp == &dzero) 1303 return; 1304 1305 KASSERT(ndp->dc_refcnt > 0); 1306 NFSDC_LOCK(np); 1307 ref = --ndp->dc_refcnt; 1308 NFSDC_UNLOCK(np); 1309 1310 if (ref == 0) 1311 free(ndp, M_NFSDIROFF); 1312 } 1313 1314 static void 1315 nfs_putdircache_unlocked(np, ndp) 1316 struct nfsnode *np; 1317 struct nfsdircache *ndp; 1318 { 1319 int ref; 1320 1321 NFSDC_ASSERT_LOCKED(np); 1322 1323 if (ndp == &dzero) 1324 return; 1325 1326 KASSERT(ndp->dc_refcnt > 0); 1327 ref = --ndp->dc_refcnt; 1328 if (ref == 0) 1329 free(ndp, M_NFSDIROFF); 1330 } 1331 1332 struct nfsdircache * 1333 nfs_searchdircache(vp, off, do32, hashent) 1334 struct vnode *vp; 1335 off_t off; 1336 int do32; 1337 int *hashent; 1338 { 1339 struct nfsdirhashhead *ndhp; 1340 struct nfsdircache *ndp = NULL; 1341 struct nfsnode *np = VTONFS(vp); 1342 unsigned ent; 1343 1344 /* 1345 * Zero is always a valid cookie. 1346 */ 1347 if (off == 0) 1348 /* XXXUNCONST */ 1349 return (struct nfsdircache *)__UNCONST(&dzero); 1350 1351 if (!np->n_dircache) 1352 return NULL; 1353 1354 /* 1355 * We use a 32bit cookie as search key, directly reconstruct 1356 * the hashentry. Else use the hashfunction. 1357 */ 1358 if (do32) { 1359 ent = (u_int32_t)off >> 24; 1360 if (ent >= NFS_DIRHASHSIZ) 1361 return NULL; 1362 ndhp = &np->n_dircache[ent]; 1363 } else { 1364 ndhp = NFSDIRHASH(np, off); 1365 } 1366 1367 if (hashent) 1368 *hashent = (int)(ndhp - np->n_dircache); 1369 1370 NFSDC_LOCK(np); 1371 if (do32) { 1372 LIST_FOREACH(ndp, ndhp, dc_hash) { 1373 if (ndp->dc_cookie32 == (u_int32_t)off) { 1374 /* 1375 * An invalidated entry will become the 1376 * start of a new block fetched from 1377 * the server. 1378 */ 1379 if (ndp->dc_flags & NFSDC_INVALID) { 1380 ndp->dc_blkcookie = ndp->dc_cookie; 1381 ndp->dc_entry = 0; 1382 ndp->dc_flags &= ~NFSDC_INVALID; 1383 } 1384 break; 1385 } 1386 } 1387 } else { 1388 LIST_FOREACH(ndp, ndhp, dc_hash) { 1389 if (ndp->dc_cookie == off) 1390 break; 1391 } 1392 } 1393 if (ndp != NULL) 1394 ndp->dc_refcnt++; 1395 NFSDC_UNLOCK(np); 1396 return ndp; 1397 } 1398 1399 1400 struct nfsdircache * 1401 nfs_enterdircache(vp, off, blkoff, en, blkno) 1402 struct vnode *vp; 1403 off_t off, blkoff; 1404 int en; 1405 daddr_t blkno; 1406 { 1407 struct nfsnode *np = VTONFS(vp); 1408 struct nfsdirhashhead *ndhp; 1409 struct nfsdircache *ndp = NULL; 1410 struct nfsdircache *newndp = NULL; 1411 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1412 int hashent, gen, overwrite; 1413 1414 /* 1415 * XXX refuse entries for offset 0. amd(8) erroneously sets 1416 * cookie 0 for the '.' entry, making this necessary. This 1417 * isn't so bad, as 0 is a special case anyway. 1418 */ 1419 if (off == 0) 1420 /* XXXUNCONST */ 1421 return (struct nfsdircache *)__UNCONST(&dzero); 1422 1423 if (!np->n_dircache) 1424 /* 1425 * XXX would like to do this in nfs_nget but vtype 1426 * isn't known at that time. 1427 */ 1428 nfs_initdircache(vp); 1429 1430 if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens) 1431 nfs_initdirxlatecookie(vp); 1432 1433 retry: 1434 ndp = nfs_searchdircache(vp, off, 0, &hashent); 1435 1436 NFSDC_LOCK(np); 1437 if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) { 1438 /* 1439 * Overwriting an old entry. Check if it's the same. 1440 * If so, just return. If not, remove the old entry. 1441 */ 1442 if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en) 1443 goto done; 1444 nfs_unlinkdircache(np, ndp); 1445 nfs_putdircache_unlocked(np, ndp); 1446 ndp = NULL; 1447 } 1448 1449 ndhp = &np->n_dircache[hashent]; 1450 1451 if (!ndp) { 1452 if (newndp == NULL) { 1453 NFSDC_UNLOCK(np); 1454 newndp = malloc(sizeof(*ndp), M_NFSDIROFF, M_WAITOK); 1455 newndp->dc_refcnt = 1; 1456 LIST_NEXT(newndp, dc_hash) = (void *)-1; 1457 goto retry; 1458 } 1459 ndp = newndp; 1460 newndp = NULL; 1461 overwrite = 0; 1462 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) { 1463 /* 1464 * We're allocating a new entry, so bump the 1465 * generation number. 1466 */ 1467 gen = ++np->n_dirgens[hashent]; 1468 if (gen == 0) { 1469 np->n_dirgens[hashent]++; 1470 gen++; 1471 } 1472 ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff); 1473 } 1474 } else 1475 overwrite = 1; 1476 1477 ndp->dc_cookie = off; 1478 ndp->dc_blkcookie = blkoff; 1479 ndp->dc_entry = en; 1480 ndp->dc_flags = 0; 1481 1482 if (overwrite) 1483 goto done; 1484 1485 /* 1486 * If the maximum directory cookie cache size has been reached 1487 * for this node, take one off the front. The idea is that 1488 * directories are typically read front-to-back once, so that 1489 * the oldest entries can be thrown away without much performance 1490 * loss. 1491 */ 1492 if (np->n_dircachesize == NFS_MAXDIRCACHE) { 1493 nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain)); 1494 } else 1495 np->n_dircachesize++; 1496 1497 KASSERT(ndp->dc_refcnt == 1); 1498 LIST_INSERT_HEAD(ndhp, ndp, dc_hash); 1499 TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain); 1500 ndp->dc_refcnt++; 1501 done: 1502 KASSERT(ndp->dc_refcnt > 0); 1503 NFSDC_UNLOCK(np); 1504 if (newndp) 1505 nfs_putdircache(np, newndp); 1506 return ndp; 1507 } 1508 1509 void 1510 nfs_invaldircache(vp, flags) 1511 struct vnode *vp; 1512 int flags; 1513 { 1514 struct nfsnode *np = VTONFS(vp); 1515 struct nfsdircache *ndp = NULL; 1516 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1517 const boolean_t forcefree = flags & NFS_INVALDIRCACHE_FORCE; 1518 1519 #ifdef DIAGNOSTIC 1520 if (vp->v_type != VDIR) 1521 panic("nfs: invaldircache: not dir"); 1522 #endif 1523 1524 if ((flags & NFS_INVALDIRCACHE_KEEPEOF) == 0) 1525 np->n_flag &= ~NEOFVALID; 1526 1527 if (!np->n_dircache) 1528 return; 1529 1530 NFSDC_LOCK(np); 1531 if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) { 1532 while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != NULL) { 1533 KASSERT(!forcefree || ndp->dc_refcnt == 1); 1534 nfs_unlinkdircache(np, ndp); 1535 } 1536 np->n_dircachesize = 0; 1537 if (forcefree && np->n_dirgens) { 1538 FREE(np->n_dirgens, M_NFSDIROFF); 1539 np->n_dirgens = NULL; 1540 } 1541 } else { 1542 TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain) 1543 ndp->dc_flags |= NFSDC_INVALID; 1544 } 1545 1546 NFSDC_UNLOCK(np); 1547 } 1548 1549 /* 1550 * Called once before VFS init to initialize shared and 1551 * server-specific data structures. 1552 */ 1553 static void 1554 nfs_init0(void) 1555 { 1556 nfsrtt.pos = 0; 1557 rpc_vers = txdr_unsigned(RPC_VER2); 1558 rpc_call = txdr_unsigned(RPC_CALL); 1559 rpc_reply = txdr_unsigned(RPC_REPLY); 1560 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1561 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1562 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1563 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1564 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1565 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1566 nfs_prog = txdr_unsigned(NFS_PROG); 1567 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 1568 nfs_true = txdr_unsigned(TRUE); 1569 nfs_false = txdr_unsigned(FALSE); 1570 nfs_xdrneg1 = txdr_unsigned(-1); 1571 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1572 if (nfs_ticks < 1) 1573 nfs_ticks = 1; 1574 #ifdef NFSSERVER 1575 nfsrv_init(0); /* Init server data structures */ 1576 nfsrv_initcache(); /* Init the server request cache */ 1577 pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript), 1578 0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr); 1579 #endif /* NFSSERVER */ 1580 1581 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY) 1582 /* 1583 * Initialize the nqnfs data structures. 1584 */ 1585 if (nqnfsstarttime == 0) { 1586 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 1587 + nqsrv_clockskew + nqsrv_writeslack; 1588 NQLOADNOVRAM(nqnfsstarttime); 1589 CIRCLEQ_INIT(&nqtimerhead); 1590 nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE, 1591 M_WAITOK, &nqfhhash); 1592 } 1593 #endif 1594 1595 exithook_establish(nfs_exit, NULL); 1596 1597 /* 1598 * Initialize reply list and start timer 1599 */ 1600 TAILQ_INIT(&nfs_reqq); 1601 nfs_timer(NULL); 1602 MOWNER_ATTACH(&nfs_mowner); 1603 1604 #ifdef NFS 1605 /* Initialize the kqueue structures */ 1606 nfs_kqinit(); 1607 /* Initialize the iod structures */ 1608 nfs_iodinit(); 1609 #endif 1610 } 1611 1612 void 1613 nfs_init(void) 1614 { 1615 static ONCE_DECL(nfs_init_once); 1616 1617 RUN_ONCE(&nfs_init_once, nfs_init0); 1618 } 1619 1620 #ifdef NFS 1621 /* 1622 * Called once at VFS init to initialize client-specific data structures. 1623 */ 1624 void 1625 nfs_vfs_init() 1626 { 1627 /* Initialize NFS server / client shared data. */ 1628 nfs_init(); 1629 1630 nfs_nhinit(); /* Init the nfsnode table */ 1631 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4); 1632 } 1633 1634 void 1635 nfs_vfs_reinit() 1636 { 1637 nfs_nhreinit(); 1638 } 1639 1640 void 1641 nfs_vfs_done() 1642 { 1643 nfs_nhdone(); 1644 } 1645 1646 /* 1647 * Attribute cache routines. 1648 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1649 * that are on the mbuf list 1650 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1651 * error otherwise 1652 */ 1653 1654 /* 1655 * Load the attribute cache (that lives in the nfsnode entry) with 1656 * the values on the mbuf list and 1657 * Iff vap not NULL 1658 * copy the attributes to *vaper 1659 */ 1660 int 1661 nfsm_loadattrcache(vpp, mdp, dposp, vaper, flags) 1662 struct vnode **vpp; 1663 struct mbuf **mdp; 1664 caddr_t *dposp; 1665 struct vattr *vaper; 1666 int flags; 1667 { 1668 int32_t t1; 1669 caddr_t cp2; 1670 int error = 0; 1671 struct mbuf *md; 1672 int v3 = NFS_ISV3(*vpp); 1673 1674 md = *mdp; 1675 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1676 error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2); 1677 if (error) 1678 return (error); 1679 return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper, flags); 1680 } 1681 1682 int 1683 nfs_loadattrcache(vpp, fp, vaper, flags) 1684 struct vnode **vpp; 1685 struct nfs_fattr *fp; 1686 struct vattr *vaper; 1687 int flags; 1688 { 1689 struct vnode *vp = *vpp; 1690 struct vattr *vap; 1691 int v3 = NFS_ISV3(vp); 1692 enum vtype vtyp; 1693 u_short vmode; 1694 struct timespec mtime; 1695 struct timespec ctime; 1696 struct vnode *nvp; 1697 int32_t rdev; 1698 struct nfsnode *np; 1699 extern int (**spec_nfsv2nodeop_p) __P((void *)); 1700 uid_t uid; 1701 gid_t gid; 1702 1703 if (v3) { 1704 vtyp = nfsv3tov_type(fp->fa_type); 1705 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1706 rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1), 1707 fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2)); 1708 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1709 fxdr_nfsv3time(&fp->fa3_ctime, &ctime); 1710 } else { 1711 vtyp = nfsv2tov_type(fp->fa_type); 1712 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1713 if (vtyp == VNON || vtyp == VREG) 1714 vtyp = IFTOVT(vmode); 1715 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1716 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1717 ctime.tv_sec = fxdr_unsigned(u_int32_t, 1718 fp->fa2_ctime.nfsv2_sec); 1719 ctime.tv_nsec = 0; 1720 1721 /* 1722 * Really ugly NFSv2 kludge. 1723 */ 1724 if (vtyp == VCHR && rdev == 0xffffffff) 1725 vtyp = VFIFO; 1726 } 1727 1728 vmode &= ALLPERMS; 1729 1730 /* 1731 * If v_type == VNON it is a new node, so fill in the v_type, 1732 * n_mtime fields. Check to see if it represents a special 1733 * device, and if so, check for a possible alias. Once the 1734 * correct vnode has been obtained, fill in the rest of the 1735 * information. 1736 */ 1737 np = VTONFS(vp); 1738 if (vp->v_type == VNON) { 1739 vp->v_type = vtyp; 1740 if (vp->v_type == VFIFO) { 1741 extern int (**fifo_nfsv2nodeop_p) __P((void *)); 1742 vp->v_op = fifo_nfsv2nodeop_p; 1743 } else if (vp->v_type == VREG) { 1744 lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0); 1745 } else if (vp->v_type == VCHR || vp->v_type == VBLK) { 1746 vp->v_op = spec_nfsv2nodeop_p; 1747 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount); 1748 if (nvp) { 1749 /* 1750 * Discard unneeded vnode, but save its nfsnode. 1751 * Since the nfsnode does not have a lock, its 1752 * vnode lock has to be carried over. 1753 */ 1754 /* 1755 * XXX is the old node sure to be locked here? 1756 */ 1757 KASSERT(lockstatus(&vp->v_lock) == 1758 LK_EXCLUSIVE); 1759 nvp->v_data = vp->v_data; 1760 vp->v_data = NULL; 1761 VOP_UNLOCK(vp, 0); 1762 vp->v_op = spec_vnodeop_p; 1763 vrele(vp); 1764 vgone(vp); 1765 lockmgr(&nvp->v_lock, LK_EXCLUSIVE, 1766 &nvp->v_interlock); 1767 /* 1768 * Reinitialize aliased node. 1769 */ 1770 np->n_vnode = nvp; 1771 *vpp = vp = nvp; 1772 } 1773 } 1774 np->n_mtime = mtime; 1775 } 1776 uid = fxdr_unsigned(uid_t, fp->fa_uid); 1777 gid = fxdr_unsigned(gid_t, fp->fa_gid); 1778 vap = np->n_vattr; 1779 1780 /* 1781 * Invalidate access cache if uid, gid, mode or ctime changed. 1782 */ 1783 if (np->n_accstamp != -1 && 1784 (gid != vap->va_gid || uid != vap->va_uid || vmode != vap->va_mode 1785 || timespeccmp(&ctime, &vap->va_ctime, !=))) 1786 np->n_accstamp = -1; 1787 1788 vap->va_type = vtyp; 1789 vap->va_mode = vmode; 1790 vap->va_rdev = (dev_t)rdev; 1791 vap->va_mtime = mtime; 1792 vap->va_ctime = ctime; 1793 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 1794 switch (vtyp) { 1795 case VDIR: 1796 vap->va_blocksize = NFS_DIRFRAGSIZ; 1797 break; 1798 case VBLK: 1799 vap->va_blocksize = BLKDEV_IOSIZE; 1800 break; 1801 case VCHR: 1802 vap->va_blocksize = MAXBSIZE; 1803 break; 1804 default: 1805 vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize : 1806 fxdr_unsigned(int32_t, fp->fa2_blocksize); 1807 break; 1808 } 1809 if (v3) { 1810 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1811 vap->va_uid = uid; 1812 vap->va_gid = gid; 1813 vap->va_size = fxdr_hyper(&fp->fa3_size); 1814 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 1815 vap->va_fileid = fxdr_hyper(&fp->fa3_fileid); 1816 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1817 vap->va_flags = 0; 1818 vap->va_filerev = 0; 1819 } else { 1820 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1821 vap->va_uid = uid; 1822 vap->va_gid = gid; 1823 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1824 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks) 1825 * NFS_FABLKSIZE; 1826 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1827 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1828 vap->va_flags = 0; 1829 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1830 vap->va_filerev = 0; 1831 } 1832 if (vap->va_size != np->n_size) { 1833 if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) { 1834 vap->va_size = np->n_size; 1835 } else { 1836 np->n_size = vap->va_size; 1837 if (vap->va_type == VREG) { 1838 /* 1839 * we can't free pages if NAC_NOTRUNC because 1840 * the pages can be owned by ourselves. 1841 */ 1842 if (flags & NAC_NOTRUNC) { 1843 np->n_flag |= NTRUNCDELAYED; 1844 } else { 1845 simple_lock(&vp->v_interlock); 1846 (void)VOP_PUTPAGES(vp, 0, 1847 0, PGO_SYNCIO | PGO_CLEANIT | 1848 PGO_FREE | PGO_ALLPAGES); 1849 uvm_vnp_setsize(vp, np->n_size); 1850 } 1851 } 1852 } 1853 } 1854 np->n_attrstamp = mono_time.tv_sec; 1855 if (vaper != NULL) { 1856 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap)); 1857 if (np->n_flag & NCHG) { 1858 if (np->n_flag & NACC) 1859 vaper->va_atime = np->n_atim; 1860 if (np->n_flag & NUPD) 1861 vaper->va_mtime = np->n_mtim; 1862 } 1863 } 1864 return (0); 1865 } 1866 1867 /* 1868 * Check the time stamp 1869 * If the cache is valid, copy contents to *vap and return 0 1870 * otherwise return an error 1871 */ 1872 int 1873 nfs_getattrcache(vp, vaper) 1874 struct vnode *vp; 1875 struct vattr *vaper; 1876 { 1877 struct nfsnode *np = VTONFS(vp); 1878 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1879 struct vattr *vap; 1880 1881 if (np->n_attrstamp == 0 || 1882 (mono_time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(nmp, np)) { 1883 nfsstats.attrcache_misses++; 1884 return (ENOENT); 1885 } 1886 nfsstats.attrcache_hits++; 1887 vap = np->n_vattr; 1888 if (vap->va_size != np->n_size) { 1889 if (vap->va_type == VREG) { 1890 if (np->n_flag & NMODIFIED) { 1891 if (vap->va_size < np->n_size) 1892 vap->va_size = np->n_size; 1893 else 1894 np->n_size = vap->va_size; 1895 } else 1896 np->n_size = vap->va_size; 1897 uvm_vnp_setsize(vp, np->n_size); 1898 } else 1899 np->n_size = vap->va_size; 1900 } 1901 memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr)); 1902 if (np->n_flag & NCHG) { 1903 if (np->n_flag & NACC) 1904 vaper->va_atime = np->n_atim; 1905 if (np->n_flag & NUPD) 1906 vaper->va_mtime = np->n_mtim; 1907 } 1908 return (0); 1909 } 1910 1911 void 1912 nfs_delayedtruncate(vp) 1913 struct vnode *vp; 1914 { 1915 struct nfsnode *np = VTONFS(vp); 1916 1917 if (np->n_flag & NTRUNCDELAYED) { 1918 np->n_flag &= ~NTRUNCDELAYED; 1919 simple_lock(&vp->v_interlock); 1920 (void)VOP_PUTPAGES(vp, 0, 1921 0, PGO_SYNCIO | PGO_CLEANIT | PGO_FREE | PGO_ALLPAGES); 1922 uvm_vnp_setsize(vp, np->n_size); 1923 } 1924 } 1925 1926 #define NFS_WCCKLUDGE_TIMEOUT (24 * 60 * 60) /* 1 day */ 1927 #define NFS_WCCKLUDGE(nmp, now) \ 1928 (((nmp)->nm_iflag & NFSMNT_WCCKLUDGE) && \ 1929 ((now) - (nmp)->nm_wcckludgetime - NFS_WCCKLUDGE_TIMEOUT) < 0) 1930 1931 /* 1932 * nfs_check_wccdata: check inaccurate wcc_data 1933 * 1934 * => return non-zero if we shouldn't trust the wcc_data. 1935 * => NFS_WCCKLUDGE_TIMEOUT is for the case that the server is "fixed". 1936 */ 1937 1938 int 1939 nfs_check_wccdata(struct nfsnode *np, const struct timespec *ctime, 1940 struct timespec *mtime, boolean_t docheck) 1941 { 1942 int error = 0; 1943 1944 #if !defined(NFS_V2_ONLY) 1945 1946 if (docheck) { 1947 struct vnode *vp = NFSTOV(np); 1948 struct nfsmount *nmp; 1949 long now = mono_time.tv_sec; 1950 #if defined(DEBUG) 1951 const char *reason = NULL; /* XXX: gcc */ 1952 #endif 1953 1954 if (timespeccmp(&np->n_vattr->va_mtime, mtime, <=)) { 1955 #if defined(DEBUG) 1956 reason = "mtime"; 1957 #endif 1958 error = EINVAL; 1959 } 1960 1961 if (vp->v_type == VDIR && 1962 timespeccmp(&np->n_vattr->va_ctime, ctime, <=)) { 1963 #if defined(DEBUG) 1964 reason = "ctime"; 1965 #endif 1966 error = EINVAL; 1967 } 1968 1969 nmp = VFSTONFS(vp->v_mount); 1970 if (error) { 1971 1972 /* 1973 * despite of the fact that we've updated the file, 1974 * timestamps of the file were not updated as we 1975 * expected. 1976 * it means that the server has incompatible 1977 * semantics of timestamps or (more likely) 1978 * the server time is not precise enough to 1979 * track each modifications. 1980 * in that case, we disable wcc processing. 1981 * 1982 * yes, strictly speaking, we should disable all 1983 * caching. it's a compromise. 1984 */ 1985 1986 simple_lock(&nmp->nm_slock); 1987 #if defined(DEBUG) 1988 if (!NFS_WCCKLUDGE(nmp, now)) { 1989 printf("%s: inaccurate wcc data (%s) detected," 1990 " disabling wcc\n", 1991 vp->v_mount->mnt_stat.f_mntfromname, 1992 reason); 1993 } 1994 #endif 1995 nmp->nm_iflag |= NFSMNT_WCCKLUDGE; 1996 nmp->nm_wcckludgetime = now; 1997 simple_unlock(&nmp->nm_slock); 1998 } else if (NFS_WCCKLUDGE(nmp, now)) { 1999 error = EPERM; /* XXX */ 2000 } else if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) { 2001 simple_lock(&nmp->nm_slock); 2002 if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) { 2003 #if defined(DEBUG) 2004 printf("%s: re-enabling wcc\n", 2005 vp->v_mount->mnt_stat.f_mntfromname); 2006 #endif 2007 nmp->nm_iflag &= ~NFSMNT_WCCKLUDGE; 2008 } 2009 simple_unlock(&nmp->nm_slock); 2010 } 2011 } 2012 2013 #endif /* !defined(NFS_V2_ONLY) */ 2014 2015 return error; 2016 } 2017 2018 /* 2019 * Heuristic to see if the server XDR encodes directory cookies or not. 2020 * it is not supposed to, but a lot of servers may do this. Also, since 2021 * most/all servers will implement V2 as well, it is expected that they 2022 * may return just 32 bits worth of cookie information, so we need to 2023 * find out in which 32 bits this information is available. We do this 2024 * to avoid trouble with emulated binaries that can't handle 64 bit 2025 * directory offsets. 2026 */ 2027 2028 void 2029 nfs_cookieheuristic(vp, flagp, l, cred) 2030 struct vnode *vp; 2031 int *flagp; 2032 struct lwp *l; 2033 struct ucred *cred; 2034 { 2035 struct uio auio; 2036 struct iovec aiov; 2037 caddr_t tbuf, cp; 2038 struct dirent *dp; 2039 off_t *cookies = NULL, *cop; 2040 int error, eof, nc, len; 2041 2042 MALLOC(tbuf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK); 2043 2044 aiov.iov_base = tbuf; 2045 aiov.iov_len = NFS_DIRFRAGSIZ; 2046 auio.uio_iov = &aiov; 2047 auio.uio_iovcnt = 1; 2048 auio.uio_rw = UIO_READ; 2049 auio.uio_segflg = UIO_SYSSPACE; 2050 auio.uio_lwp = NULL; 2051 auio.uio_resid = NFS_DIRFRAGSIZ; 2052 auio.uio_offset = 0; 2053 2054 error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc); 2055 2056 len = NFS_DIRFRAGSIZ - auio.uio_resid; 2057 if (error || len == 0) { 2058 FREE(tbuf, M_TEMP); 2059 if (cookies) 2060 free(cookies, M_TEMP); 2061 return; 2062 } 2063 2064 /* 2065 * Find the first valid entry and look at its offset cookie. 2066 */ 2067 2068 cp = tbuf; 2069 for (cop = cookies; len > 0; len -= dp->d_reclen) { 2070 dp = (struct dirent *)cp; 2071 if (dp->d_fileno != 0 && len >= dp->d_reclen) { 2072 if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) { 2073 *flagp |= NFSMNT_SWAPCOOKIE; 2074 nfs_invaldircache(vp, 0); 2075 nfs_vinvalbuf(vp, 0, cred, l, 1); 2076 } 2077 break; 2078 } 2079 cop++; 2080 cp += dp->d_reclen; 2081 } 2082 2083 FREE(tbuf, M_TEMP); 2084 free(cookies, M_TEMP); 2085 } 2086 #endif /* NFS */ 2087 2088 #ifdef NFSSERVER 2089 /* 2090 * Set up nameidata for a lookup() call and do it. 2091 * 2092 * If pubflag is set, this call is done for a lookup operation on the 2093 * public filehandle. In that case we allow crossing mountpoints and 2094 * absolute pathnames. However, the caller is expected to check that 2095 * the lookup result is within the public fs, and deny access if 2096 * it is not. 2097 */ 2098 int 2099 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, l, kerbflag, pubflag) 2100 struct nameidata *ndp; 2101 fhandle_t *fhp; 2102 uint32_t len; 2103 struct nfssvc_sock *slp; 2104 struct mbuf *nam; 2105 struct mbuf **mdp; 2106 caddr_t *dposp; 2107 struct vnode **retdirp; 2108 struct lwp *l; 2109 int kerbflag, pubflag; 2110 { 2111 int i, rem; 2112 struct mbuf *md; 2113 char *fromcp, *tocp, *cp; 2114 struct iovec aiov; 2115 struct uio auio; 2116 struct vnode *dp; 2117 int error, rdonly, linklen; 2118 struct componentname *cnp = &ndp->ni_cnd; 2119 2120 *retdirp = (struct vnode *)0; 2121 2122 if ((len + 1) > MAXPATHLEN) 2123 return (ENAMETOOLONG); 2124 if (len == 0) 2125 return (EACCES); 2126 cnp->cn_pnbuf = PNBUF_GET(); 2127 2128 /* 2129 * Copy the name from the mbuf list to ndp->ni_pnbuf 2130 * and set the various ndp fields appropriately. 2131 */ 2132 fromcp = *dposp; 2133 tocp = cnp->cn_pnbuf; 2134 md = *mdp; 2135 rem = mtod(md, caddr_t) + md->m_len - fromcp; 2136 for (i = 0; i < len; i++) { 2137 while (rem == 0) { 2138 md = md->m_next; 2139 if (md == NULL) { 2140 error = EBADRPC; 2141 goto out; 2142 } 2143 fromcp = mtod(md, caddr_t); 2144 rem = md->m_len; 2145 } 2146 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 2147 error = EACCES; 2148 goto out; 2149 } 2150 *tocp++ = *fromcp++; 2151 rem--; 2152 } 2153 *tocp = '\0'; 2154 *mdp = md; 2155 *dposp = fromcp; 2156 len = nfsm_rndup(len)-len; 2157 if (len > 0) { 2158 if (rem >= len) 2159 *dposp += len; 2160 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 2161 goto out; 2162 } 2163 2164 /* 2165 * Extract and set starting directory. 2166 */ 2167 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp, 2168 nam, &rdonly, kerbflag, pubflag); 2169 if (error) 2170 goto out; 2171 if (dp->v_type != VDIR) { 2172 vrele(dp); 2173 error = ENOTDIR; 2174 goto out; 2175 } 2176 2177 if (rdonly) 2178 cnp->cn_flags |= RDONLY; 2179 2180 *retdirp = dp; 2181 2182 if (pubflag) { 2183 /* 2184 * Oh joy. For WebNFS, handle those pesky '%' escapes, 2185 * and the 'native path' indicator. 2186 */ 2187 cp = PNBUF_GET(); 2188 fromcp = cnp->cn_pnbuf; 2189 tocp = cp; 2190 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 2191 switch ((unsigned char)*fromcp) { 2192 case WEBNFS_NATIVE_CHAR: 2193 /* 2194 * 'Native' path for us is the same 2195 * as a path according to the NFS spec, 2196 * just skip the escape char. 2197 */ 2198 fromcp++; 2199 break; 2200 /* 2201 * More may be added in the future, range 0x80-0xff 2202 */ 2203 default: 2204 error = EIO; 2205 PNBUF_PUT(cp); 2206 goto out; 2207 } 2208 } 2209 /* 2210 * Translate the '%' escapes, URL-style. 2211 */ 2212 while (*fromcp != '\0') { 2213 if (*fromcp == WEBNFS_ESC_CHAR) { 2214 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 2215 fromcp++; 2216 *tocp++ = HEXSTRTOI(fromcp); 2217 fromcp += 2; 2218 continue; 2219 } else { 2220 error = ENOENT; 2221 PNBUF_PUT(cp); 2222 goto out; 2223 } 2224 } else 2225 *tocp++ = *fromcp++; 2226 } 2227 *tocp = '\0'; 2228 PNBUF_PUT(cnp->cn_pnbuf); 2229 cnp->cn_pnbuf = cp; 2230 } 2231 2232 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1; 2233 ndp->ni_segflg = UIO_SYSSPACE; 2234 ndp->ni_rootdir = rootvnode; 2235 2236 if (pubflag) { 2237 ndp->ni_loopcnt = 0; 2238 if (cnp->cn_pnbuf[0] == '/') 2239 dp = rootvnode; 2240 } else { 2241 cnp->cn_flags |= NOCROSSMOUNT; 2242 } 2243 2244 cnp->cn_lwp = l; 2245 VREF(dp); 2246 2247 for (;;) { 2248 cnp->cn_nameptr = cnp->cn_pnbuf; 2249 ndp->ni_startdir = dp; 2250 /* 2251 * And call lookup() to do the real work 2252 */ 2253 error = lookup(ndp); 2254 if (error) { 2255 PNBUF_PUT(cnp->cn_pnbuf); 2256 return (error); 2257 } 2258 /* 2259 * Check for encountering a symbolic link 2260 */ 2261 if ((cnp->cn_flags & ISSYMLINK) == 0) { 2262 if (cnp->cn_flags & (SAVENAME | SAVESTART)) 2263 cnp->cn_flags |= HASBUF; 2264 else 2265 PNBUF_PUT(cnp->cn_pnbuf); 2266 return (0); 2267 } else { 2268 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) 2269 VOP_UNLOCK(ndp->ni_dvp, 0); 2270 if (!pubflag) { 2271 error = EINVAL; 2272 break; 2273 } 2274 2275 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 2276 error = ELOOP; 2277 break; 2278 } 2279 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) { 2280 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred, 2281 cnp->cn_lwp); 2282 if (error != 0) 2283 break; 2284 } 2285 if (ndp->ni_pathlen > 1) 2286 cp = PNBUF_GET(); 2287 else 2288 cp = cnp->cn_pnbuf; 2289 aiov.iov_base = cp; 2290 aiov.iov_len = MAXPATHLEN; 2291 auio.uio_iov = &aiov; 2292 auio.uio_iovcnt = 1; 2293 auio.uio_offset = 0; 2294 auio.uio_rw = UIO_READ; 2295 auio.uio_segflg = UIO_SYSSPACE; 2296 auio.uio_lwp = NULL; 2297 auio.uio_resid = MAXPATHLEN; 2298 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 2299 if (error) { 2300 badlink: 2301 if (ndp->ni_pathlen > 1) 2302 PNBUF_PUT(cp); 2303 break; 2304 } 2305 linklen = MAXPATHLEN - auio.uio_resid; 2306 if (linklen == 0) { 2307 error = ENOENT; 2308 goto badlink; 2309 } 2310 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 2311 error = ENAMETOOLONG; 2312 goto badlink; 2313 } 2314 if (ndp->ni_pathlen > 1) { 2315 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen); 2316 PNBUF_PUT(cnp->cn_pnbuf); 2317 cnp->cn_pnbuf = cp; 2318 } else 2319 cnp->cn_pnbuf[linklen] = '\0'; 2320 ndp->ni_pathlen += linklen; 2321 vput(ndp->ni_vp); 2322 dp = ndp->ni_dvp; 2323 /* 2324 * Check if root directory should replace current directory. 2325 */ 2326 if (cnp->cn_pnbuf[0] == '/') { 2327 vrele(dp); 2328 dp = ndp->ni_rootdir; 2329 VREF(dp); 2330 } 2331 } 2332 } 2333 vrele(ndp->ni_dvp); 2334 vput(ndp->ni_vp); 2335 ndp->ni_vp = NULL; 2336 out: 2337 PNBUF_PUT(cnp->cn_pnbuf); 2338 return (error); 2339 } 2340 #endif /* NFSSERVER */ 2341 2342 /* 2343 * A fiddled version of m_adj() that ensures null fill to a 32-bit 2344 * boundary and only trims off the back end 2345 * 2346 * 1. trim off 'len' bytes as m_adj(mp, -len). 2347 * 2. add zero-padding 'nul' bytes at the end of the mbuf chain. 2348 */ 2349 void 2350 nfs_zeropad(mp, len, nul) 2351 struct mbuf *mp; 2352 int len; 2353 int nul; 2354 { 2355 struct mbuf *m; 2356 int count; 2357 2358 /* 2359 * Trim from tail. Scan the mbuf chain, 2360 * calculating its length and finding the last mbuf. 2361 * If the adjustment only affects this mbuf, then just 2362 * adjust and return. Otherwise, rescan and truncate 2363 * after the remaining size. 2364 */ 2365 count = 0; 2366 m = mp; 2367 for (;;) { 2368 count += m->m_len; 2369 if (m->m_next == NULL) 2370 break; 2371 m = m->m_next; 2372 } 2373 2374 KDASSERT(count >= len); 2375 2376 if (m->m_len >= len) { 2377 m->m_len -= len; 2378 } else { 2379 count -= len; 2380 /* 2381 * Correct length for chain is "count". 2382 * Find the mbuf with last data, adjust its length, 2383 * and toss data from remaining mbufs on chain. 2384 */ 2385 for (m = mp; m; m = m->m_next) { 2386 if (m->m_len >= count) { 2387 m->m_len = count; 2388 break; 2389 } 2390 count -= m->m_len; 2391 } 2392 m_freem(m->m_next); 2393 m->m_next = NULL; 2394 } 2395 2396 KDASSERT(m->m_next == NULL); 2397 2398 /* 2399 * zero-padding. 2400 */ 2401 if (nul > 0) { 2402 char *cp; 2403 int i; 2404 2405 if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) { 2406 struct mbuf *n; 2407 2408 KDASSERT(MLEN >= nul); 2409 n = m_get(M_WAIT, MT_DATA); 2410 MCLAIM(n, &nfs_mowner); 2411 n->m_len = nul; 2412 n->m_next = NULL; 2413 m->m_next = n; 2414 cp = mtod(n, caddr_t); 2415 } else { 2416 cp = mtod(m, caddr_t) + m->m_len; 2417 m->m_len += nul; 2418 } 2419 for (i = 0; i < nul; i++) 2420 *cp++ = '\0'; 2421 } 2422 return; 2423 } 2424 2425 /* 2426 * Make these functions instead of macros, so that the kernel text size 2427 * doesn't get too big... 2428 */ 2429 void 2430 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp) 2431 struct nfsrv_descript *nfsd; 2432 int before_ret; 2433 struct vattr *before_vap; 2434 int after_ret; 2435 struct vattr *after_vap; 2436 struct mbuf **mbp; 2437 char **bposp; 2438 { 2439 struct mbuf *mb = *mbp; 2440 char *bpos = *bposp; 2441 u_int32_t *tl; 2442 2443 if (before_ret) { 2444 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 2445 *tl = nfs_false; 2446 } else { 2447 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 2448 *tl++ = nfs_true; 2449 txdr_hyper(before_vap->va_size, tl); 2450 tl += 2; 2451 txdr_nfsv3time(&(before_vap->va_mtime), tl); 2452 tl += 2; 2453 txdr_nfsv3time(&(before_vap->va_ctime), tl); 2454 } 2455 *bposp = bpos; 2456 *mbp = mb; 2457 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 2458 } 2459 2460 void 2461 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp) 2462 struct nfsrv_descript *nfsd; 2463 int after_ret; 2464 struct vattr *after_vap; 2465 struct mbuf **mbp; 2466 char **bposp; 2467 { 2468 struct mbuf *mb = *mbp; 2469 char *bpos = *bposp; 2470 u_int32_t *tl; 2471 struct nfs_fattr *fp; 2472 2473 if (after_ret) { 2474 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 2475 *tl = nfs_false; 2476 } else { 2477 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 2478 *tl++ = nfs_true; 2479 fp = (struct nfs_fattr *)tl; 2480 nfsm_srvfattr(nfsd, after_vap, fp); 2481 } 2482 *mbp = mb; 2483 *bposp = bpos; 2484 } 2485 2486 void 2487 nfsm_srvfattr(nfsd, vap, fp) 2488 struct nfsrv_descript *nfsd; 2489 struct vattr *vap; 2490 struct nfs_fattr *fp; 2491 { 2492 2493 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 2494 fp->fa_uid = txdr_unsigned(vap->va_uid); 2495 fp->fa_gid = txdr_unsigned(vap->va_gid); 2496 if (nfsd->nd_flag & ND_NFSV3) { 2497 fp->fa_type = vtonfsv3_type(vap->va_type); 2498 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 2499 txdr_hyper(vap->va_size, &fp->fa3_size); 2500 txdr_hyper(vap->va_bytes, &fp->fa3_used); 2501 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev)); 2502 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev)); 2503 fp->fa3_fsid.nfsuquad[0] = 0; 2504 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 2505 txdr_hyper(vap->va_fileid, &fp->fa3_fileid); 2506 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 2507 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 2508 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 2509 } else { 2510 fp->fa_type = vtonfsv2_type(vap->va_type); 2511 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 2512 fp->fa2_size = txdr_unsigned(vap->va_size); 2513 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 2514 if (vap->va_type == VFIFO) 2515 fp->fa2_rdev = 0xffffffff; 2516 else 2517 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 2518 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 2519 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 2520 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 2521 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 2522 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 2523 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 2524 } 2525 } 2526 2527 #ifdef NFSSERVER 2528 /* 2529 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 2530 * - look up fsid in mount list (if not found ret error) 2531 * - get vp and export rights by calling VFS_FHTOVP() 2532 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2533 * - if not lockflag unlock it with VOP_UNLOCK() 2534 */ 2535 int 2536 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag) 2537 fhandle_t *fhp; 2538 int lockflag; 2539 struct vnode **vpp; 2540 struct ucred *cred; 2541 struct nfssvc_sock *slp; 2542 struct mbuf *nam; 2543 int *rdonlyp; 2544 int kerbflag; 2545 { 2546 struct mount *mp; 2547 int i; 2548 struct ucred *credanon; 2549 int error, exflags; 2550 struct sockaddr_in *saddr; 2551 2552 *vpp = (struct vnode *)0; 2553 2554 if (nfs_ispublicfh(fhp)) { 2555 if (!pubflag || !nfs_pub.np_valid) 2556 return (ESTALE); 2557 fhp = &nfs_pub.np_handle; 2558 } 2559 2560 error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon); 2561 if (error) { 2562 return error; 2563 } 2564 2565 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 2566 if (error) 2567 return (error); 2568 2569 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 2570 saddr = mtod(nam, struct sockaddr_in *); 2571 if ((saddr->sin_family == AF_INET) && 2572 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 2573 vput(*vpp); 2574 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 2575 } 2576 #ifdef INET6 2577 if ((saddr->sin_family == AF_INET6) && 2578 ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) { 2579 vput(*vpp); 2580 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 2581 } 2582 #endif 2583 } 2584 /* 2585 * Check/setup credentials. 2586 */ 2587 if (exflags & MNT_EXKERB) { 2588 if (!kerbflag) { 2589 vput(*vpp); 2590 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 2591 } 2592 } else if (kerbflag) { 2593 vput(*vpp); 2594 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 2595 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 2596 cred->cr_uid = credanon->cr_uid; 2597 cred->cr_gid = credanon->cr_gid; 2598 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 2599 cred->cr_groups[i] = credanon->cr_groups[i]; 2600 cred->cr_ngroups = i; 2601 } 2602 if (exflags & MNT_EXRDONLY) 2603 *rdonlyp = 1; 2604 else 2605 *rdonlyp = 0; 2606 if (!lockflag) 2607 VOP_UNLOCK(*vpp, 0); 2608 return (0); 2609 } 2610 2611 /* 2612 * WebNFS: check if a filehandle is a public filehandle. For v3, this 2613 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 2614 * transformed this to all zeroes in both cases, so check for it. 2615 */ 2616 int 2617 nfs_ispublicfh(fhp) 2618 fhandle_t *fhp; 2619 { 2620 char *cp = (char *)fhp; 2621 int i; 2622 2623 for (i = 0; i < NFSX_V3FH; i++) 2624 if (*cp++ != 0) 2625 return (FALSE); 2626 return (TRUE); 2627 } 2628 #endif /* NFSSERVER */ 2629 2630 /* 2631 * This function compares two net addresses by family and returns TRUE 2632 * if they are the same host. 2633 * If there is any doubt, return FALSE. 2634 * The AF_INET family is handled as a special case so that address mbufs 2635 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 2636 */ 2637 int 2638 netaddr_match(family, haddr, nam) 2639 int family; 2640 union nethostaddr *haddr; 2641 struct mbuf *nam; 2642 { 2643 struct sockaddr_in *inetaddr; 2644 2645 switch (family) { 2646 case AF_INET: 2647 inetaddr = mtod(nam, struct sockaddr_in *); 2648 if (inetaddr->sin_family == AF_INET && 2649 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 2650 return (1); 2651 break; 2652 #ifdef INET6 2653 case AF_INET6: 2654 { 2655 struct sockaddr_in6 *sin6_1, *sin6_2; 2656 2657 sin6_1 = mtod(nam, struct sockaddr_in6 *); 2658 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *); 2659 if (sin6_1->sin6_family == AF_INET6 && 2660 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr)) 2661 return 1; 2662 } 2663 #endif 2664 #ifdef ISO 2665 case AF_ISO: 2666 { 2667 struct sockaddr_iso *isoaddr1, *isoaddr2; 2668 2669 isoaddr1 = mtod(nam, struct sockaddr_iso *); 2670 isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *); 2671 if (isoaddr1->siso_family == AF_ISO && 2672 isoaddr1->siso_nlen > 0 && 2673 isoaddr1->siso_nlen == isoaddr2->siso_nlen && 2674 SAME_ISOADDR(isoaddr1, isoaddr2)) 2675 return (1); 2676 break; 2677 } 2678 #endif /* ISO */ 2679 default: 2680 break; 2681 }; 2682 return (0); 2683 } 2684 2685 /* 2686 * The write verifier has changed (probably due to a server reboot), so all 2687 * PG_NEEDCOMMIT pages will have to be written again. Since they are marked 2688 * as dirty or are being written out just now, all this takes is clearing 2689 * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for 2690 * the mount point. 2691 */ 2692 void 2693 nfs_clearcommit(mp) 2694 struct mount *mp; 2695 { 2696 struct vnode *vp; 2697 struct nfsnode *np; 2698 struct vm_page *pg; 2699 struct nfsmount *nmp = VFSTONFS(mp); 2700 2701 lockmgr(&nmp->nm_writeverflock, LK_EXCLUSIVE, NULL); 2702 2703 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2704 KASSERT(vp->v_mount == mp); 2705 if (vp->v_type != VREG) 2706 continue; 2707 np = VTONFS(vp); 2708 np->n_pushlo = np->n_pushhi = np->n_pushedlo = 2709 np->n_pushedhi = 0; 2710 np->n_commitflags &= 2711 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID); 2712 simple_lock(&vp->v_uobj.vmobjlock); 2713 TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq) { 2714 pg->flags &= ~PG_NEEDCOMMIT; 2715 } 2716 simple_unlock(&vp->v_uobj.vmobjlock); 2717 } 2718 simple_lock(&nmp->nm_slock); 2719 nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF; 2720 simple_unlock(&nmp->nm_slock); 2721 lockmgr(&nmp->nm_writeverflock, LK_RELEASE, NULL); 2722 } 2723 2724 void 2725 nfs_merge_commit_ranges(vp) 2726 struct vnode *vp; 2727 { 2728 struct nfsnode *np = VTONFS(vp); 2729 2730 KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID); 2731 2732 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) { 2733 np->n_pushedlo = np->n_pushlo; 2734 np->n_pushedhi = np->n_pushhi; 2735 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID; 2736 } else { 2737 if (np->n_pushlo < np->n_pushedlo) 2738 np->n_pushedlo = np->n_pushlo; 2739 if (np->n_pushhi > np->n_pushedhi) 2740 np->n_pushedhi = np->n_pushhi; 2741 } 2742 2743 np->n_pushlo = np->n_pushhi = 0; 2744 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID; 2745 2746 #ifdef NFS_DEBUG_COMMIT 2747 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo, 2748 (unsigned)np->n_pushedhi); 2749 #endif 2750 } 2751 2752 int 2753 nfs_in_committed_range(vp, off, len) 2754 struct vnode *vp; 2755 off_t off, len; 2756 { 2757 struct nfsnode *np = VTONFS(vp); 2758 off_t lo, hi; 2759 2760 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) 2761 return 0; 2762 lo = off; 2763 hi = lo + len; 2764 2765 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi); 2766 } 2767 2768 int 2769 nfs_in_tobecommitted_range(vp, off, len) 2770 struct vnode *vp; 2771 off_t off, len; 2772 { 2773 struct nfsnode *np = VTONFS(vp); 2774 off_t lo, hi; 2775 2776 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) 2777 return 0; 2778 lo = off; 2779 hi = lo + len; 2780 2781 return (lo >= np->n_pushlo && hi <= np->n_pushhi); 2782 } 2783 2784 void 2785 nfs_add_committed_range(vp, off, len) 2786 struct vnode *vp; 2787 off_t off, len; 2788 { 2789 struct nfsnode *np = VTONFS(vp); 2790 off_t lo, hi; 2791 2792 lo = off; 2793 hi = lo + len; 2794 2795 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) { 2796 np->n_pushedlo = lo; 2797 np->n_pushedhi = hi; 2798 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID; 2799 } else { 2800 if (hi > np->n_pushedhi) 2801 np->n_pushedhi = hi; 2802 if (lo < np->n_pushedlo) 2803 np->n_pushedlo = lo; 2804 } 2805 #ifdef NFS_DEBUG_COMMIT 2806 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo, 2807 (unsigned)np->n_pushedhi); 2808 #endif 2809 } 2810 2811 void 2812 nfs_del_committed_range(vp, off, len) 2813 struct vnode *vp; 2814 off_t off, len; 2815 { 2816 struct nfsnode *np = VTONFS(vp); 2817 off_t lo, hi; 2818 2819 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) 2820 return; 2821 2822 lo = off; 2823 hi = lo + len; 2824 2825 if (lo > np->n_pushedhi || hi < np->n_pushedlo) 2826 return; 2827 if (lo <= np->n_pushedlo) 2828 np->n_pushedlo = hi; 2829 else if (hi >= np->n_pushedhi) 2830 np->n_pushedhi = lo; 2831 else { 2832 /* 2833 * XXX There's only one range. If the deleted range 2834 * is in the middle, pick the largest of the 2835 * contiguous ranges that it leaves. 2836 */ 2837 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi)) 2838 np->n_pushedhi = lo; 2839 else 2840 np->n_pushedlo = hi; 2841 } 2842 #ifdef NFS_DEBUG_COMMIT 2843 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo, 2844 (unsigned)np->n_pushedhi); 2845 #endif 2846 } 2847 2848 void 2849 nfs_add_tobecommitted_range(vp, off, len) 2850 struct vnode *vp; 2851 off_t off, len; 2852 { 2853 struct nfsnode *np = VTONFS(vp); 2854 off_t lo, hi; 2855 2856 lo = off; 2857 hi = lo + len; 2858 2859 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) { 2860 np->n_pushlo = lo; 2861 np->n_pushhi = hi; 2862 np->n_commitflags |= NFS_COMMIT_PUSH_VALID; 2863 } else { 2864 if (lo < np->n_pushlo) 2865 np->n_pushlo = lo; 2866 if (hi > np->n_pushhi) 2867 np->n_pushhi = hi; 2868 } 2869 #ifdef NFS_DEBUG_COMMIT 2870 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo, 2871 (unsigned)np->n_pushhi); 2872 #endif 2873 } 2874 2875 void 2876 nfs_del_tobecommitted_range(vp, off, len) 2877 struct vnode *vp; 2878 off_t off, len; 2879 { 2880 struct nfsnode *np = VTONFS(vp); 2881 off_t lo, hi; 2882 2883 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) 2884 return; 2885 2886 lo = off; 2887 hi = lo + len; 2888 2889 if (lo > np->n_pushhi || hi < np->n_pushlo) 2890 return; 2891 2892 if (lo <= np->n_pushlo) 2893 np->n_pushlo = hi; 2894 else if (hi >= np->n_pushhi) 2895 np->n_pushhi = lo; 2896 else { 2897 /* 2898 * XXX There's only one range. If the deleted range 2899 * is in the middle, pick the largest of the 2900 * contiguous ranges that it leaves. 2901 */ 2902 if ((np->n_pushlo - lo) > (hi - np->n_pushhi)) 2903 np->n_pushhi = lo; 2904 else 2905 np->n_pushlo = hi; 2906 } 2907 #ifdef NFS_DEBUG_COMMIT 2908 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo, 2909 (unsigned)np->n_pushhi); 2910 #endif 2911 } 2912 2913 /* 2914 * Map errnos to NFS error numbers. For Version 3 also filter out error 2915 * numbers not specified for the associated procedure. 2916 */ 2917 int 2918 nfsrv_errmap(nd, err) 2919 struct nfsrv_descript *nd; 2920 int err; 2921 { 2922 const short *defaulterrp, *errp; 2923 2924 if (nd->nd_flag & ND_NFSV3) { 2925 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2926 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2927 while (*++errp) { 2928 if (*errp == err) 2929 return (err); 2930 else if (*errp > err) 2931 break; 2932 } 2933 return ((int)*defaulterrp); 2934 } else 2935 return (err & 0xffff); 2936 } 2937 if (err <= ELAST) 2938 return ((int)nfsrv_v2errmap[err - 1]); 2939 return (NFSERR_IO); 2940 } 2941 2942 /* 2943 * Sort the group list in increasing numerical order. 2944 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2945 * that used to be here.) 2946 */ 2947 void 2948 nfsrvw_sort(list, num) 2949 gid_t *list; 2950 int num; 2951 { 2952 int i, j; 2953 gid_t v; 2954 2955 /* Insertion sort. */ 2956 for (i = 1; i < num; i++) { 2957 v = list[i]; 2958 /* find correct slot for value v, moving others up */ 2959 for (j = i; --j >= 0 && v < list[j];) 2960 list[j + 1] = list[j]; 2961 list[j + 1] = v; 2962 } 2963 } 2964 2965 /* 2966 * copy credentials making sure that the result can be compared with memcmp(). 2967 */ 2968 void 2969 nfsrv_setcred(incred, outcred) 2970 struct ucred *incred, *outcred; 2971 { 2972 int i; 2973 2974 memset((caddr_t)outcred, 0, sizeof (struct ucred)); 2975 outcred->cr_ref = 1; 2976 outcred->cr_uid = incred->cr_uid; 2977 outcred->cr_gid = incred->cr_gid; 2978 outcred->cr_ngroups = incred->cr_ngroups; 2979 for (i = 0; i < incred->cr_ngroups; i++) 2980 outcred->cr_groups[i] = incred->cr_groups[i]; 2981 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2982 } 2983 2984 u_int32_t 2985 nfs_getxid() 2986 { 2987 static u_int32_t base; 2988 static u_int32_t nfs_xid = 0; 2989 static struct simplelock nfs_xidlock = SIMPLELOCK_INITIALIZER; 2990 u_int32_t newxid; 2991 2992 simple_lock(&nfs_xidlock); 2993 /* 2994 * derive initial xid from system time 2995 * XXX time is invalid if root not yet mounted 2996 */ 2997 if (__predict_false(!base && (rootvp))) { 2998 struct timeval tv; 2999 3000 microtime(&tv); 3001 base = tv.tv_sec << 12; 3002 nfs_xid = base; 3003 } 3004 3005 /* 3006 * Skip zero xid if it should ever happen. 3007 */ 3008 if (__predict_false(++nfs_xid == 0)) 3009 nfs_xid++; 3010 newxid = nfs_xid; 3011 simple_unlock(&nfs_xidlock); 3012 3013 return txdr_unsigned(newxid); 3014 } 3015 3016 /* 3017 * assign a new xid for existing request. 3018 * used for NFSERR_JUKEBOX handling. 3019 */ 3020 void 3021 nfs_renewxid(struct nfsreq *req) 3022 { 3023 u_int32_t xid; 3024 int off; 3025 3026 xid = nfs_getxid(); 3027 if (req->r_nmp->nm_sotype == SOCK_STREAM) 3028 off = sizeof(u_int32_t); /* RPC record mark */ 3029 else 3030 off = 0; 3031 3032 m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid); 3033 req->r_xid = xid; 3034 } 3035