1 /* $NetBSD: nfs_subs.c,v 1.232 2018/05/08 16:47:58 maxv 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.232 2018/05/08 16:47:58 maxv Exp $"); 74 75 #ifdef _KERNEL_OPT 76 #include "opt_nfs.h" 77 #endif 78 79 /* 80 * These functions support the macros and help fiddle mbuf chains for 81 * the nfs op functions. They do things like create the rpc header and 82 * copy data between mbuf chains and uio lists. 83 */ 84 #include <sys/param.h> 85 #include <sys/proc.h> 86 #include <sys/systm.h> 87 #include <sys/kernel.h> 88 #include <sys/kmem.h> 89 #include <sys/mount.h> 90 #include <sys/vnode.h> 91 #include <sys/namei.h> 92 #include <sys/mbuf.h> 93 #include <sys/socket.h> 94 #include <sys/stat.h> 95 #include <sys/filedesc.h> 96 #include <sys/time.h> 97 #include <sys/dirent.h> 98 #include <sys/once.h> 99 #include <sys/kauth.h> 100 #include <sys/atomic.h> 101 #include <sys/cprng.h> 102 103 #include <uvm/uvm.h> 104 105 #include <nfs/rpcv2.h> 106 #include <nfs/nfsproto.h> 107 #include <nfs/nfsnode.h> 108 #include <nfs/nfs.h> 109 #include <nfs/xdr_subs.h> 110 #include <nfs/nfsm_subs.h> 111 #include <nfs/nfsmount.h> 112 #include <nfs/nfsrtt.h> 113 #include <nfs/nfs_var.h> 114 115 #include <miscfs/specfs/specdev.h> 116 117 #include <netinet/in.h> 118 119 static u_int32_t nfs_xid; 120 121 int nuidhash_max = NFS_MAXUIDHASH; 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, 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 143 /* NFS client/server stats. */ 144 struct nfsstats nfsstats; 145 146 /* 147 * Mapping of old NFS Version 2 RPC numbers to generic numbers. 148 */ 149 const int nfsv3_procid[NFS_NPROCS] = { 150 NFSPROC_NULL, 151 NFSPROC_GETATTR, 152 NFSPROC_SETATTR, 153 NFSPROC_NOOP, 154 NFSPROC_LOOKUP, 155 NFSPROC_READLINK, 156 NFSPROC_READ, 157 NFSPROC_NOOP, 158 NFSPROC_WRITE, 159 NFSPROC_CREATE, 160 NFSPROC_REMOVE, 161 NFSPROC_RENAME, 162 NFSPROC_LINK, 163 NFSPROC_SYMLINK, 164 NFSPROC_MKDIR, 165 NFSPROC_RMDIR, 166 NFSPROC_READDIR, 167 NFSPROC_FSSTAT, 168 NFSPROC_NOOP, 169 NFSPROC_NOOP, 170 NFSPROC_NOOP, 171 NFSPROC_NOOP, 172 NFSPROC_NOOP 173 }; 174 175 /* 176 * and the reverse mapping from generic to Version 2 procedure numbers 177 */ 178 const int nfsv2_procid[NFS_NPROCS] = { 179 NFSV2PROC_NULL, 180 NFSV2PROC_GETATTR, 181 NFSV2PROC_SETATTR, 182 NFSV2PROC_LOOKUP, 183 NFSV2PROC_NOOP, 184 NFSV2PROC_READLINK, 185 NFSV2PROC_READ, 186 NFSV2PROC_WRITE, 187 NFSV2PROC_CREATE, 188 NFSV2PROC_MKDIR, 189 NFSV2PROC_SYMLINK, 190 NFSV2PROC_CREATE, 191 NFSV2PROC_REMOVE, 192 NFSV2PROC_RMDIR, 193 NFSV2PROC_RENAME, 194 NFSV2PROC_LINK, 195 NFSV2PROC_READDIR, 196 NFSV2PROC_NOOP, 197 NFSV2PROC_STATFS, 198 NFSV2PROC_NOOP, 199 NFSV2PROC_NOOP, 200 NFSV2PROC_NOOP, 201 NFSV2PROC_NOOP, 202 }; 203 204 /* 205 * Maps errno values to nfs error numbers. 206 * Use NFSERR_IO as the catch all for ones not specifically defined in 207 * RFC 1094. 208 */ 209 static const u_char nfsrv_v2errmap[ELAST] = { 210 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 211 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 212 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 213 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 214 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 215 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 216 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 217 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 218 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 219 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 220 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 221 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 222 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 223 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 224 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 225 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 226 NFSERR_IO, NFSERR_IO, 227 }; 228 229 /* 230 * Maps errno values to nfs error numbers. 231 * Although it is not obvious whether or not NFS clients really care if 232 * a returned error value is in the specified list for the procedure, the 233 * safest thing to do is filter them appropriately. For Version 2, the 234 * X/Open XNFS document is the only specification that defines error values 235 * for each RPC (The RFC simply lists all possible error values for all RPCs), 236 * so I have decided to not do this for Version 2. 237 * The first entry is the default error return and the rest are the valid 238 * errors for that RPC in increasing numeric order. 239 */ 240 static const short nfsv3err_null[] = { 241 0, 242 0, 243 }; 244 245 static const short nfsv3err_getattr[] = { 246 NFSERR_IO, 247 NFSERR_IO, 248 NFSERR_STALE, 249 NFSERR_BADHANDLE, 250 NFSERR_SERVERFAULT, 251 0, 252 }; 253 254 static const short nfsv3err_setattr[] = { 255 NFSERR_IO, 256 NFSERR_PERM, 257 NFSERR_IO, 258 NFSERR_ACCES, 259 NFSERR_INVAL, 260 NFSERR_NOSPC, 261 NFSERR_ROFS, 262 NFSERR_DQUOT, 263 NFSERR_STALE, 264 NFSERR_BADHANDLE, 265 NFSERR_NOT_SYNC, 266 NFSERR_SERVERFAULT, 267 0, 268 }; 269 270 static const short nfsv3err_lookup[] = { 271 NFSERR_IO, 272 NFSERR_NOENT, 273 NFSERR_IO, 274 NFSERR_ACCES, 275 NFSERR_NOTDIR, 276 NFSERR_NAMETOL, 277 NFSERR_STALE, 278 NFSERR_BADHANDLE, 279 NFSERR_SERVERFAULT, 280 0, 281 }; 282 283 static const short nfsv3err_access[] = { 284 NFSERR_IO, 285 NFSERR_IO, 286 NFSERR_STALE, 287 NFSERR_BADHANDLE, 288 NFSERR_SERVERFAULT, 289 0, 290 }; 291 292 static const short nfsv3err_readlink[] = { 293 NFSERR_IO, 294 NFSERR_IO, 295 NFSERR_ACCES, 296 NFSERR_INVAL, 297 NFSERR_STALE, 298 NFSERR_BADHANDLE, 299 NFSERR_NOTSUPP, 300 NFSERR_SERVERFAULT, 301 0, 302 }; 303 304 static const short nfsv3err_read[] = { 305 NFSERR_IO, 306 NFSERR_IO, 307 NFSERR_NXIO, 308 NFSERR_ACCES, 309 NFSERR_INVAL, 310 NFSERR_STALE, 311 NFSERR_BADHANDLE, 312 NFSERR_SERVERFAULT, 313 NFSERR_JUKEBOX, 314 0, 315 }; 316 317 static const short nfsv3err_write[] = { 318 NFSERR_IO, 319 NFSERR_IO, 320 NFSERR_ACCES, 321 NFSERR_INVAL, 322 NFSERR_FBIG, 323 NFSERR_NOSPC, 324 NFSERR_ROFS, 325 NFSERR_DQUOT, 326 NFSERR_STALE, 327 NFSERR_BADHANDLE, 328 NFSERR_SERVERFAULT, 329 NFSERR_JUKEBOX, 330 0, 331 }; 332 333 static const short nfsv3err_create[] = { 334 NFSERR_IO, 335 NFSERR_IO, 336 NFSERR_ACCES, 337 NFSERR_EXIST, 338 NFSERR_NOTDIR, 339 NFSERR_NOSPC, 340 NFSERR_ROFS, 341 NFSERR_NAMETOL, 342 NFSERR_DQUOT, 343 NFSERR_STALE, 344 NFSERR_BADHANDLE, 345 NFSERR_NOTSUPP, 346 NFSERR_SERVERFAULT, 347 0, 348 }; 349 350 static const short nfsv3err_mkdir[] = { 351 NFSERR_IO, 352 NFSERR_IO, 353 NFSERR_ACCES, 354 NFSERR_EXIST, 355 NFSERR_NOTDIR, 356 NFSERR_NOSPC, 357 NFSERR_ROFS, 358 NFSERR_NAMETOL, 359 NFSERR_DQUOT, 360 NFSERR_STALE, 361 NFSERR_BADHANDLE, 362 NFSERR_NOTSUPP, 363 NFSERR_SERVERFAULT, 364 0, 365 }; 366 367 static const short nfsv3err_symlink[] = { 368 NFSERR_IO, 369 NFSERR_IO, 370 NFSERR_ACCES, 371 NFSERR_EXIST, 372 NFSERR_NOTDIR, 373 NFSERR_NOSPC, 374 NFSERR_ROFS, 375 NFSERR_NAMETOL, 376 NFSERR_DQUOT, 377 NFSERR_STALE, 378 NFSERR_BADHANDLE, 379 NFSERR_NOTSUPP, 380 NFSERR_SERVERFAULT, 381 0, 382 }; 383 384 static const short nfsv3err_mknod[] = { 385 NFSERR_IO, 386 NFSERR_IO, 387 NFSERR_ACCES, 388 NFSERR_EXIST, 389 NFSERR_NOTDIR, 390 NFSERR_NOSPC, 391 NFSERR_ROFS, 392 NFSERR_NAMETOL, 393 NFSERR_DQUOT, 394 NFSERR_STALE, 395 NFSERR_BADHANDLE, 396 NFSERR_NOTSUPP, 397 NFSERR_SERVERFAULT, 398 NFSERR_BADTYPE, 399 0, 400 }; 401 402 static const short nfsv3err_remove[] = { 403 NFSERR_IO, 404 NFSERR_NOENT, 405 NFSERR_IO, 406 NFSERR_ACCES, 407 NFSERR_NOTDIR, 408 NFSERR_ROFS, 409 NFSERR_NAMETOL, 410 NFSERR_STALE, 411 NFSERR_BADHANDLE, 412 NFSERR_SERVERFAULT, 413 0, 414 }; 415 416 static const short nfsv3err_rmdir[] = { 417 NFSERR_IO, 418 NFSERR_NOENT, 419 NFSERR_IO, 420 NFSERR_ACCES, 421 NFSERR_EXIST, 422 NFSERR_NOTDIR, 423 NFSERR_INVAL, 424 NFSERR_ROFS, 425 NFSERR_NAMETOL, 426 NFSERR_NOTEMPTY, 427 NFSERR_STALE, 428 NFSERR_BADHANDLE, 429 NFSERR_NOTSUPP, 430 NFSERR_SERVERFAULT, 431 0, 432 }; 433 434 static const short nfsv3err_rename[] = { 435 NFSERR_IO, 436 NFSERR_NOENT, 437 NFSERR_IO, 438 NFSERR_ACCES, 439 NFSERR_EXIST, 440 NFSERR_XDEV, 441 NFSERR_NOTDIR, 442 NFSERR_ISDIR, 443 NFSERR_INVAL, 444 NFSERR_NOSPC, 445 NFSERR_ROFS, 446 NFSERR_MLINK, 447 NFSERR_NAMETOL, 448 NFSERR_NOTEMPTY, 449 NFSERR_DQUOT, 450 NFSERR_STALE, 451 NFSERR_BADHANDLE, 452 NFSERR_NOTSUPP, 453 NFSERR_SERVERFAULT, 454 0, 455 }; 456 457 static const short nfsv3err_link[] = { 458 NFSERR_IO, 459 NFSERR_IO, 460 NFSERR_ACCES, 461 NFSERR_EXIST, 462 NFSERR_XDEV, 463 NFSERR_NOTDIR, 464 NFSERR_INVAL, 465 NFSERR_NOSPC, 466 NFSERR_ROFS, 467 NFSERR_MLINK, 468 NFSERR_NAMETOL, 469 NFSERR_DQUOT, 470 NFSERR_STALE, 471 NFSERR_BADHANDLE, 472 NFSERR_NOTSUPP, 473 NFSERR_SERVERFAULT, 474 0, 475 }; 476 477 static const short nfsv3err_readdir[] = { 478 NFSERR_IO, 479 NFSERR_IO, 480 NFSERR_ACCES, 481 NFSERR_NOTDIR, 482 NFSERR_STALE, 483 NFSERR_BADHANDLE, 484 NFSERR_BAD_COOKIE, 485 NFSERR_TOOSMALL, 486 NFSERR_SERVERFAULT, 487 0, 488 }; 489 490 static const short nfsv3err_readdirplus[] = { 491 NFSERR_IO, 492 NFSERR_IO, 493 NFSERR_ACCES, 494 NFSERR_NOTDIR, 495 NFSERR_STALE, 496 NFSERR_BADHANDLE, 497 NFSERR_BAD_COOKIE, 498 NFSERR_NOTSUPP, 499 NFSERR_TOOSMALL, 500 NFSERR_SERVERFAULT, 501 0, 502 }; 503 504 static const short nfsv3err_fsstat[] = { 505 NFSERR_IO, 506 NFSERR_IO, 507 NFSERR_STALE, 508 NFSERR_BADHANDLE, 509 NFSERR_SERVERFAULT, 510 0, 511 }; 512 513 static const short nfsv3err_fsinfo[] = { 514 NFSERR_STALE, 515 NFSERR_STALE, 516 NFSERR_BADHANDLE, 517 NFSERR_SERVERFAULT, 518 0, 519 }; 520 521 static const short nfsv3err_pathconf[] = { 522 NFSERR_STALE, 523 NFSERR_STALE, 524 NFSERR_BADHANDLE, 525 NFSERR_SERVERFAULT, 526 0, 527 }; 528 529 static const short nfsv3err_commit[] = { 530 NFSERR_IO, 531 NFSERR_IO, 532 NFSERR_STALE, 533 NFSERR_BADHANDLE, 534 NFSERR_SERVERFAULT, 535 0, 536 }; 537 538 static const short * const nfsrv_v3errmap[] = { 539 nfsv3err_null, 540 nfsv3err_getattr, 541 nfsv3err_setattr, 542 nfsv3err_lookup, 543 nfsv3err_access, 544 nfsv3err_readlink, 545 nfsv3err_read, 546 nfsv3err_write, 547 nfsv3err_create, 548 nfsv3err_mkdir, 549 nfsv3err_symlink, 550 nfsv3err_mknod, 551 nfsv3err_remove, 552 nfsv3err_rmdir, 553 nfsv3err_rename, 554 nfsv3err_link, 555 nfsv3err_readdir, 556 nfsv3err_readdirplus, 557 nfsv3err_fsstat, 558 nfsv3err_fsinfo, 559 nfsv3err_pathconf, 560 nfsv3err_commit, 561 }; 562 563 extern struct nfsrtt nfsrtt; 564 565 u_long nfsdirhashmask; 566 567 int nfs_webnamei(struct nameidata *, struct vnode *, struct proc *); 568 569 /* 570 * Create the header for an rpc request packet 571 * The hsiz is the size of the rest of the nfs request header. 572 * (just used to decide if a cluster is a good idea) 573 */ 574 struct mbuf * 575 nfsm_reqh(struct nfsnode *np, u_long procid, int hsiz, char **bposp) 576 { 577 struct mbuf *mb; 578 char *bpos; 579 580 mb = m_get(M_WAIT, MT_DATA); 581 MCLAIM(mb, &nfs_mowner); 582 if (hsiz >= MINCLSIZE) 583 m_clget(mb, M_WAIT); 584 mb->m_len = 0; 585 bpos = mtod(mb, void *); 586 587 /* Finally, return values */ 588 *bposp = bpos; 589 return (mb); 590 } 591 592 /* 593 * Build the RPC header and fill in the authorization info. 594 * The authorization string argument is only used when the credentials 595 * come from outside of the kernel. 596 * Returns the head of the mbuf list. 597 */ 598 struct mbuf * 599 nfsm_rpchead(kauth_cred_t cr, int nmflag, int procid, 600 int auth_type, int auth_len, char *auth_str, int verf_len, 601 char *verf_str, struct mbuf *mrest, int mrest_len, 602 struct mbuf **mbp, uint32_t *xidp) 603 { 604 struct mbuf *mb; 605 u_int32_t *tl; 606 char *bpos; 607 int i; 608 struct mbuf *mreq; 609 int siz, grpsiz, authsiz; 610 611 authsiz = nfsm_rndup(auth_len); 612 mb = m_gethdr(M_WAIT, MT_DATA); 613 MCLAIM(mb, &nfs_mowner); 614 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) { 615 m_clget(mb, M_WAIT); 616 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) { 617 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED); 618 } else { 619 MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 620 } 621 mb->m_len = 0; 622 mreq = mb; 623 bpos = mtod(mb, void *); 624 625 /* 626 * First the RPC header. 627 */ 628 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 629 630 *tl++ = *xidp = nfs_getxid(); 631 *tl++ = rpc_call; 632 *tl++ = rpc_vers; 633 *tl++ = txdr_unsigned(NFS_PROG); 634 if (nmflag & NFSMNT_NFSV3) 635 *tl++ = txdr_unsigned(NFS_VER3); 636 else 637 *tl++ = txdr_unsigned(NFS_VER2); 638 if (nmflag & NFSMNT_NFSV3) 639 *tl++ = txdr_unsigned(procid); 640 else 641 *tl++ = txdr_unsigned(nfsv2_procid[procid]); 642 643 /* 644 * And then the authorization cred. 645 */ 646 *tl++ = txdr_unsigned(auth_type); 647 *tl = txdr_unsigned(authsiz); 648 switch (auth_type) { 649 case RPCAUTH_UNIX: 650 nfsm_build(tl, u_int32_t *, auth_len); 651 *tl++ = 0; /* stamp ?? */ 652 *tl++ = 0; /* NULL hostname */ 653 *tl++ = txdr_unsigned(kauth_cred_geteuid(cr)); 654 *tl++ = txdr_unsigned(kauth_cred_getegid(cr)); 655 grpsiz = (auth_len >> 2) - 5; 656 *tl++ = txdr_unsigned(grpsiz); 657 for (i = 0; i < grpsiz; i++) 658 *tl++ = txdr_unsigned(kauth_cred_group(cr, i)); /* XXX elad review */ 659 break; 660 case RPCAUTH_KERB4: 661 siz = auth_len; 662 while (siz > 0) { 663 if (M_TRAILINGSPACE(mb) == 0) { 664 struct mbuf *mb2; 665 mb2 = m_get(M_WAIT, MT_DATA); 666 MCLAIM(mb2, &nfs_mowner); 667 if (siz >= MINCLSIZE) 668 m_clget(mb2, M_WAIT); 669 mb->m_next = mb2; 670 mb = mb2; 671 mb->m_len = 0; 672 bpos = mtod(mb, void *); 673 } 674 i = min(siz, M_TRAILINGSPACE(mb)); 675 memcpy(bpos, auth_str, i); 676 mb->m_len += i; 677 auth_str += i; 678 bpos += i; 679 siz -= i; 680 } 681 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 682 for (i = 0; i < siz; i++) 683 *bpos++ = '\0'; 684 mb->m_len += siz; 685 } 686 break; 687 }; 688 689 /* 690 * And the verifier... 691 */ 692 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 693 if (verf_str) { 694 *tl++ = txdr_unsigned(RPCAUTH_KERB4); 695 *tl = txdr_unsigned(verf_len); 696 siz = verf_len; 697 while (siz > 0) { 698 if (M_TRAILINGSPACE(mb) == 0) { 699 struct mbuf *mb2; 700 mb2 = m_get(M_WAIT, MT_DATA); 701 MCLAIM(mb2, &nfs_mowner); 702 if (siz >= MINCLSIZE) 703 m_clget(mb2, M_WAIT); 704 mb->m_next = mb2; 705 mb = mb2; 706 mb->m_len = 0; 707 bpos = mtod(mb, void *); 708 } 709 i = min(siz, M_TRAILINGSPACE(mb)); 710 memcpy(bpos, verf_str, i); 711 mb->m_len += i; 712 verf_str += i; 713 bpos += i; 714 siz -= i; 715 } 716 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) { 717 for (i = 0; i < siz; i++) 718 *bpos++ = '\0'; 719 mb->m_len += siz; 720 } 721 } else { 722 *tl++ = txdr_unsigned(RPCAUTH_NULL); 723 *tl = 0; 724 } 725 mb->m_next = mrest; 726 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 727 m_reset_rcvif(mreq); 728 *mbp = mb; 729 return (mreq); 730 } 731 732 /* 733 * copies mbuf chain to the uio scatter/gather list 734 */ 735 int 736 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, char **dpos) 737 { 738 char *mbufcp, *uiocp; 739 int xfer, left, len; 740 struct mbuf *mp; 741 long uiosiz, rem; 742 int error = 0; 743 744 mp = *mrep; 745 mbufcp = *dpos; 746 len = mtod(mp, char *) + mp->m_len - mbufcp; 747 rem = nfsm_rndup(siz)-siz; 748 while (siz > 0) { 749 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 750 return (EFBIG); 751 left = uiop->uio_iov->iov_len; 752 uiocp = uiop->uio_iov->iov_base; 753 if (left > siz) 754 left = siz; 755 uiosiz = left; 756 while (left > 0) { 757 while (len == 0) { 758 mp = mp->m_next; 759 if (mp == NULL) 760 return (EBADRPC); 761 mbufcp = mtod(mp, void *); 762 len = mp->m_len; 763 } 764 xfer = (left > len) ? len : left; 765 error = copyout_vmspace(uiop->uio_vmspace, mbufcp, 766 uiocp, xfer); 767 if (error) { 768 return error; 769 } 770 left -= xfer; 771 len -= xfer; 772 mbufcp += xfer; 773 uiocp += xfer; 774 uiop->uio_offset += xfer; 775 uiop->uio_resid -= xfer; 776 } 777 if (uiop->uio_iov->iov_len <= siz) { 778 uiop->uio_iovcnt--; 779 uiop->uio_iov++; 780 } else { 781 uiop->uio_iov->iov_base = 782 (char *)uiop->uio_iov->iov_base + uiosiz; 783 uiop->uio_iov->iov_len -= uiosiz; 784 } 785 siz -= uiosiz; 786 } 787 *dpos = mbufcp; 788 *mrep = mp; 789 if (rem > 0) { 790 if (len < rem) 791 error = nfs_adv(mrep, dpos, rem, len); 792 else 793 *dpos += rem; 794 } 795 return (error); 796 } 797 798 /* 799 * copies a uio scatter/gather list to an mbuf chain. 800 * NOTE: can ony handle iovcnt == 1 801 */ 802 int 803 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, char **bpos) 804 { 805 char *uiocp; 806 struct mbuf *mp, *mp2; 807 int xfer, left, mlen; 808 int uiosiz, clflg, rem; 809 char *cp; 810 int error; 811 812 #ifdef DIAGNOSTIC 813 if (uiop->uio_iovcnt != 1) 814 panic("nfsm_uiotombuf: iovcnt != 1"); 815 #endif 816 817 if (siz > MLEN) /* or should it >= MCLBYTES ?? */ 818 clflg = 1; 819 else 820 clflg = 0; 821 rem = nfsm_rndup(siz)-siz; 822 mp = mp2 = *mq; 823 while (siz > 0) { 824 left = uiop->uio_iov->iov_len; 825 uiocp = uiop->uio_iov->iov_base; 826 if (left > siz) 827 left = siz; 828 uiosiz = left; 829 while (left > 0) { 830 mlen = M_TRAILINGSPACE(mp); 831 if (mlen == 0) { 832 mp = m_get(M_WAIT, MT_DATA); 833 MCLAIM(mp, &nfs_mowner); 834 if (clflg) 835 m_clget(mp, M_WAIT); 836 mp->m_len = 0; 837 mp2->m_next = mp; 838 mp2 = mp; 839 mlen = M_TRAILINGSPACE(mp); 840 } 841 xfer = (left > mlen) ? mlen : left; 842 cp = mtod(mp, char *) + mp->m_len; 843 error = copyin_vmspace(uiop->uio_vmspace, uiocp, cp, 844 xfer); 845 if (error) { 846 /* XXX */ 847 } 848 mp->m_len += xfer; 849 left -= xfer; 850 uiocp += xfer; 851 uiop->uio_offset += xfer; 852 uiop->uio_resid -= xfer; 853 } 854 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 855 uiosiz; 856 uiop->uio_iov->iov_len -= uiosiz; 857 siz -= uiosiz; 858 } 859 if (rem > 0) { 860 if (rem > M_TRAILINGSPACE(mp)) { 861 mp = m_get(M_WAIT, MT_DATA); 862 MCLAIM(mp, &nfs_mowner); 863 mp->m_len = 0; 864 mp2->m_next = mp; 865 } 866 cp = mtod(mp, char *) + mp->m_len; 867 for (left = 0; left < rem; left++) 868 *cp++ = '\0'; 869 mp->m_len += rem; 870 *bpos = cp; 871 } else 872 *bpos = mtod(mp, char *) + mp->m_len; 873 *mq = mp; 874 return (0); 875 } 876 877 /* 878 * Get at least "siz" bytes of correctly aligned data. 879 * When called the mbuf pointers are not necessarily correct, 880 * dsosp points to what ought to be in m_data and left contains 881 * what ought to be in m_len. 882 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 883 * cases. (The macros use the vars. dpos and dpos2) 884 */ 885 int 886 nfsm_disct(struct mbuf **mdp, char **dposp, int siz, int left, char **cp2) 887 { 888 struct mbuf *m1, *m2; 889 struct mbuf *havebuf = NULL; 890 char *src = *dposp; 891 char *dst; 892 int len; 893 894 #ifdef DEBUG 895 if (left < 0) 896 panic("nfsm_disct: left < 0"); 897 #endif 898 m1 = *mdp; 899 /* 900 * Skip through the mbuf chain looking for an mbuf with 901 * some data. If the first mbuf found has enough data 902 * and it is correctly aligned return it. 903 */ 904 while (left == 0) { 905 havebuf = m1; 906 *mdp = m1 = m1->m_next; 907 if (m1 == NULL) 908 return (EBADRPC); 909 src = mtod(m1, void *); 910 left = m1->m_len; 911 /* 912 * If we start a new mbuf and it is big enough 913 * and correctly aligned just return it, don't 914 * do any pull up. 915 */ 916 if (left >= siz && nfsm_aligned(src)) { 917 *cp2 = src; 918 *dposp = src + siz; 919 return (0); 920 } 921 } 922 if ((m1->m_flags & M_EXT) != 0) { 923 if (havebuf && M_TRAILINGSPACE(havebuf) >= siz && 924 nfsm_aligned(mtod(havebuf, char *) + havebuf->m_len)) { 925 /* 926 * If the first mbuf with data has external data 927 * and there is a previous mbuf with some trailing 928 * space, use it to move the data into. 929 */ 930 m2 = m1; 931 *mdp = m1 = havebuf; 932 *cp2 = mtod(m1, char *) + m1->m_len; 933 } else if (havebuf) { 934 /* 935 * If the first mbuf has a external data 936 * and there is no previous empty mbuf 937 * allocate a new mbuf and move the external 938 * data to the new mbuf. Also make the first 939 * mbuf look empty. 940 */ 941 m2 = m1; 942 *mdp = m1 = m_get(M_WAIT, MT_DATA); 943 MCLAIM(m1, m2->m_owner); 944 if ((m2->m_flags & M_PKTHDR) != 0) { 945 M_MOVE_PKTHDR(m1, m2); 946 } 947 if (havebuf) { 948 havebuf->m_next = m1; 949 } 950 m1->m_next = m2; 951 MRESETDATA(m1); 952 m1->m_len = 0; 953 m2->m_data = src; 954 m2->m_len = left; 955 *cp2 = mtod(m1, char *); 956 } else { 957 struct mbuf **nextp = &m1->m_next; 958 959 m1->m_len -= left; 960 do { 961 m2 = m_get(M_WAIT, MT_DATA); 962 MCLAIM(m2, m1->m_owner); 963 if (left >= MINCLSIZE) { 964 MCLGET(m2, M_WAIT); 965 } 966 m2->m_next = *nextp; 967 *nextp = m2; 968 nextp = &m2->m_next; 969 len = (m2->m_flags & M_EXT) != 0 ? 970 MCLBYTES : MLEN; 971 if (len > left) { 972 len = left; 973 } 974 memcpy(mtod(m2, char *), src, len); 975 m2->m_len = len; 976 src += len; 977 left -= len; 978 } while (left > 0); 979 *mdp = m1 = m1->m_next; 980 m2 = m1->m_next; 981 *cp2 = mtod(m1, char *); 982 } 983 } else { 984 /* 985 * If the first mbuf has no external data 986 * move the data to the front of the mbuf. 987 */ 988 MRESETDATA(m1); 989 dst = mtod(m1, char *); 990 if (dst != src) { 991 memmove(dst, src, left); 992 } 993 m1->m_len = left; 994 m2 = m1->m_next; 995 *cp2 = m1->m_data; 996 } 997 *dposp = *cp2 + siz; 998 /* 999 * Loop through mbufs pulling data up into first mbuf until 1000 * the first mbuf is full or there is no more data to 1001 * pullup. 1002 */ 1003 dst = mtod(m1, char *) + m1->m_len; 1004 while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) { 1005 if ((len = min(len, m2->m_len)) != 0) { 1006 memcpy(dst, mtod(m2, char *), len); 1007 } 1008 m1->m_len += len; 1009 dst += len; 1010 m2->m_data += len; 1011 m2->m_len -= len; 1012 m2 = m2->m_next; 1013 } 1014 if (m1->m_len < siz) 1015 return (EBADRPC); 1016 return (0); 1017 } 1018 1019 /* 1020 * Advance the position in the mbuf chain. 1021 */ 1022 int 1023 nfs_adv(struct mbuf **mdp, char **dposp, int offs, int left) 1024 { 1025 struct mbuf *m; 1026 int s; 1027 1028 m = *mdp; 1029 s = left; 1030 while (s < offs) { 1031 offs -= s; 1032 m = m->m_next; 1033 if (m == NULL) 1034 return (EBADRPC); 1035 s = m->m_len; 1036 } 1037 *mdp = m; 1038 *dposp = mtod(m, char *) + offs; 1039 return (0); 1040 } 1041 1042 /* 1043 * Copy a string into mbufs for the hard cases... 1044 */ 1045 int 1046 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz) 1047 { 1048 struct mbuf *m1 = NULL, *m2; 1049 long left, xfer, len, tlen; 1050 u_int32_t *tl; 1051 int putsize; 1052 1053 putsize = 1; 1054 m2 = *mb; 1055 left = M_TRAILINGSPACE(m2); 1056 if (left > 0) { 1057 tl = ((u_int32_t *)(*bpos)); 1058 *tl++ = txdr_unsigned(siz); 1059 putsize = 0; 1060 left -= NFSX_UNSIGNED; 1061 m2->m_len += NFSX_UNSIGNED; 1062 if (left > 0) { 1063 memcpy((void *) tl, cp, left); 1064 siz -= left; 1065 cp += left; 1066 m2->m_len += left; 1067 left = 0; 1068 } 1069 } 1070 /* Loop around adding mbufs */ 1071 while (siz > 0) { 1072 m1 = m_get(M_WAIT, MT_DATA); 1073 MCLAIM(m1, &nfs_mowner); 1074 if (siz > MLEN) 1075 m_clget(m1, M_WAIT); 1076 m1->m_len = NFSMSIZ(m1); 1077 m2->m_next = m1; 1078 m2 = m1; 1079 tl = mtod(m1, u_int32_t *); 1080 tlen = 0; 1081 if (putsize) { 1082 *tl++ = txdr_unsigned(siz); 1083 m1->m_len -= NFSX_UNSIGNED; 1084 tlen = NFSX_UNSIGNED; 1085 putsize = 0; 1086 } 1087 if (siz < m1->m_len) { 1088 len = nfsm_rndup(siz); 1089 xfer = siz; 1090 if (xfer < len) 1091 *(tl+(xfer>>2)) = 0; 1092 } else { 1093 xfer = len = m1->m_len; 1094 } 1095 memcpy((void *) tl, cp, xfer); 1096 m1->m_len = len+tlen; 1097 siz -= xfer; 1098 cp += xfer; 1099 } 1100 *mb = m1; 1101 *bpos = mtod(m1, char *) + m1->m_len; 1102 return (0); 1103 } 1104 1105 /* 1106 * Directory caching routines. They work as follows: 1107 * - a cache is maintained per VDIR nfsnode. 1108 * - for each offset cookie that is exported to userspace, and can 1109 * thus be thrown back at us as an offset to VOP_READDIR, store 1110 * information in the cache. 1111 * - cached are: 1112 * - cookie itself 1113 * - blocknumber (essentially just a search key in the buffer cache) 1114 * - entry number in block. 1115 * - offset cookie of block in which this entry is stored 1116 * - 32 bit cookie if NFSMNT_XLATECOOKIE is used. 1117 * - entries are looked up in a hash table 1118 * - also maintained is an LRU list of entries, used to determine 1119 * which ones to delete if the cache grows too large. 1120 * - if 32 <-> 64 translation mode is requested for a filesystem, 1121 * the cache also functions as a translation table 1122 * - in the translation case, invalidating the cache does not mean 1123 * flushing it, but just marking entries as invalid, except for 1124 * the <64bit cookie, 32bitcookie> pair which is still valid, to 1125 * still be able to use the cache as a translation table. 1126 * - 32 bit cookies are uniquely created by combining the hash table 1127 * entry value, and one generation count per hash table entry, 1128 * incremented each time an entry is appended to the chain. 1129 * - the cache is invalidated each time a direcory is modified 1130 * - sanity checks are also done; if an entry in a block turns 1131 * out not to have a matching cookie, the cache is invalidated 1132 * and a new block starting from the wanted offset is fetched from 1133 * the server. 1134 * - directory entries as read from the server are extended to contain 1135 * the 64bit and, optionally, the 32bit cookies, for sanity checking 1136 * the cache and exporting them to userspace through the cookie 1137 * argument to VOP_READDIR. 1138 */ 1139 1140 u_long 1141 nfs_dirhash(off_t off) 1142 { 1143 int i; 1144 char *cp = (char *)&off; 1145 u_long sum = 0L; 1146 1147 for (i = 0 ; i < sizeof (off); i++) 1148 sum += *cp++; 1149 1150 return sum; 1151 } 1152 1153 #define _NFSDC_MTX(np) (NFSTOV(np)->v_interlock) 1154 #define NFSDC_LOCK(np) mutex_enter(_NFSDC_MTX(np)) 1155 #define NFSDC_UNLOCK(np) mutex_exit(_NFSDC_MTX(np)) 1156 #define NFSDC_ASSERT_LOCKED(np) KASSERT(mutex_owned(_NFSDC_MTX(np))) 1157 1158 void 1159 nfs_initdircache(struct vnode *vp) 1160 { 1161 struct nfsnode *np = VTONFS(vp); 1162 struct nfsdirhashhead *dircache; 1163 1164 dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, true, 1165 &nfsdirhashmask); 1166 1167 NFSDC_LOCK(np); 1168 if (np->n_dircache == NULL) { 1169 np->n_dircachesize = 0; 1170 np->n_dircache = dircache; 1171 dircache = NULL; 1172 TAILQ_INIT(&np->n_dirchain); 1173 } 1174 NFSDC_UNLOCK(np); 1175 if (dircache) 1176 hashdone(dircache, HASH_LIST, nfsdirhashmask); 1177 } 1178 1179 void 1180 nfs_initdirxlatecookie(struct vnode *vp) 1181 { 1182 struct nfsnode *np = VTONFS(vp); 1183 unsigned *dirgens; 1184 1185 KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE); 1186 1187 dirgens = kmem_zalloc(NFS_DIRHASHSIZ * sizeof(unsigned), KM_SLEEP); 1188 NFSDC_LOCK(np); 1189 if (np->n_dirgens == NULL) { 1190 np->n_dirgens = dirgens; 1191 dirgens = NULL; 1192 } 1193 NFSDC_UNLOCK(np); 1194 if (dirgens) 1195 kmem_free(dirgens, NFS_DIRHASHSIZ * sizeof(unsigned)); 1196 } 1197 1198 static const struct nfsdircache dzero; 1199 1200 static void nfs_unlinkdircache(struct nfsnode *np, struct nfsdircache *); 1201 static void nfs_putdircache_unlocked(struct nfsnode *, 1202 struct nfsdircache *); 1203 1204 static void 1205 nfs_unlinkdircache(struct nfsnode *np, struct nfsdircache *ndp) 1206 { 1207 1208 NFSDC_ASSERT_LOCKED(np); 1209 KASSERT(ndp != &dzero); 1210 1211 if (LIST_NEXT(ndp, dc_hash) == (void *)-1) 1212 return; 1213 1214 TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain); 1215 LIST_REMOVE(ndp, dc_hash); 1216 LIST_NEXT(ndp, dc_hash) = (void *)-1; /* mark as unlinked */ 1217 1218 nfs_putdircache_unlocked(np, ndp); 1219 } 1220 1221 void 1222 nfs_putdircache(struct nfsnode *np, struct nfsdircache *ndp) 1223 { 1224 int ref; 1225 1226 if (ndp == &dzero) 1227 return; 1228 1229 KASSERT(ndp->dc_refcnt > 0); 1230 NFSDC_LOCK(np); 1231 ref = --ndp->dc_refcnt; 1232 NFSDC_UNLOCK(np); 1233 1234 if (ref == 0) 1235 kmem_free(ndp, sizeof(*ndp)); 1236 } 1237 1238 static void 1239 nfs_putdircache_unlocked(struct nfsnode *np, struct nfsdircache *ndp) 1240 { 1241 int ref; 1242 1243 NFSDC_ASSERT_LOCKED(np); 1244 1245 if (ndp == &dzero) 1246 return; 1247 1248 KASSERT(ndp->dc_refcnt > 0); 1249 ref = --ndp->dc_refcnt; 1250 if (ref == 0) 1251 kmem_free(ndp, sizeof(*ndp)); 1252 } 1253 1254 struct nfsdircache * 1255 nfs_searchdircache(struct vnode *vp, off_t off, int do32, int *hashent) 1256 { 1257 struct nfsdirhashhead *ndhp; 1258 struct nfsdircache *ndp = NULL; 1259 struct nfsnode *np = VTONFS(vp); 1260 unsigned ent; 1261 1262 /* 1263 * Zero is always a valid cookie. 1264 */ 1265 if (off == 0) 1266 /* XXXUNCONST */ 1267 return (struct nfsdircache *)__UNCONST(&dzero); 1268 1269 if (!np->n_dircache) 1270 return NULL; 1271 1272 /* 1273 * We use a 32bit cookie as search key, directly reconstruct 1274 * the hashentry. Else use the hashfunction. 1275 */ 1276 if (do32) { 1277 ent = (u_int32_t)off >> 24; 1278 if (ent >= NFS_DIRHASHSIZ) 1279 return NULL; 1280 ndhp = &np->n_dircache[ent]; 1281 } else { 1282 ndhp = NFSDIRHASH(np, off); 1283 } 1284 1285 if (hashent) 1286 *hashent = (int)(ndhp - np->n_dircache); 1287 1288 NFSDC_LOCK(np); 1289 if (do32) { 1290 LIST_FOREACH(ndp, ndhp, dc_hash) { 1291 if (ndp->dc_cookie32 == (u_int32_t)off) { 1292 /* 1293 * An invalidated entry will become the 1294 * start of a new block fetched from 1295 * the server. 1296 */ 1297 if (ndp->dc_flags & NFSDC_INVALID) { 1298 ndp->dc_blkcookie = ndp->dc_cookie; 1299 ndp->dc_entry = 0; 1300 ndp->dc_flags &= ~NFSDC_INVALID; 1301 } 1302 break; 1303 } 1304 } 1305 } else { 1306 LIST_FOREACH(ndp, ndhp, dc_hash) { 1307 if (ndp->dc_cookie == off) 1308 break; 1309 } 1310 } 1311 if (ndp != NULL) 1312 ndp->dc_refcnt++; 1313 NFSDC_UNLOCK(np); 1314 return ndp; 1315 } 1316 1317 1318 struct nfsdircache * 1319 nfs_enterdircache(struct vnode *vp, off_t off, off_t blkoff, int en, 1320 daddr_t blkno) 1321 { 1322 struct nfsnode *np = VTONFS(vp); 1323 struct nfsdirhashhead *ndhp; 1324 struct nfsdircache *ndp = NULL; 1325 struct nfsdircache *newndp = NULL; 1326 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1327 int hashent = 0, gen, overwrite; /* XXX: GCC */ 1328 1329 /* 1330 * XXX refuse entries for offset 0. amd(8) erroneously sets 1331 * cookie 0 for the '.' entry, making this necessary. This 1332 * isn't so bad, as 0 is a special case anyway. 1333 */ 1334 if (off == 0) 1335 /* XXXUNCONST */ 1336 return (struct nfsdircache *)__UNCONST(&dzero); 1337 1338 if (!np->n_dircache) 1339 /* 1340 * XXX would like to do this in nfs_nget but vtype 1341 * isn't known at that time. 1342 */ 1343 nfs_initdircache(vp); 1344 1345 if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens) 1346 nfs_initdirxlatecookie(vp); 1347 1348 retry: 1349 ndp = nfs_searchdircache(vp, off, 0, &hashent); 1350 1351 NFSDC_LOCK(np); 1352 if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) { 1353 /* 1354 * Overwriting an old entry. Check if it's the same. 1355 * If so, just return. If not, remove the old entry. 1356 */ 1357 if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en) 1358 goto done; 1359 nfs_unlinkdircache(np, ndp); 1360 nfs_putdircache_unlocked(np, ndp); 1361 ndp = NULL; 1362 } 1363 1364 ndhp = &np->n_dircache[hashent]; 1365 1366 if (!ndp) { 1367 if (newndp == NULL) { 1368 NFSDC_UNLOCK(np); 1369 newndp = kmem_alloc(sizeof(*newndp), KM_SLEEP); 1370 newndp->dc_refcnt = 1; 1371 LIST_NEXT(newndp, dc_hash) = (void *)-1; 1372 goto retry; 1373 } 1374 ndp = newndp; 1375 newndp = NULL; 1376 overwrite = 0; 1377 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) { 1378 /* 1379 * We're allocating a new entry, so bump the 1380 * generation number. 1381 */ 1382 KASSERT(np->n_dirgens); 1383 gen = ++np->n_dirgens[hashent]; 1384 if (gen == 0) { 1385 np->n_dirgens[hashent]++; 1386 gen++; 1387 } 1388 ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff); 1389 } 1390 } else 1391 overwrite = 1; 1392 1393 ndp->dc_cookie = off; 1394 ndp->dc_blkcookie = blkoff; 1395 ndp->dc_entry = en; 1396 ndp->dc_flags = 0; 1397 1398 if (overwrite) 1399 goto done; 1400 1401 /* 1402 * If the maximum directory cookie cache size has been reached 1403 * for this node, take one off the front. The idea is that 1404 * directories are typically read front-to-back once, so that 1405 * the oldest entries can be thrown away without much performance 1406 * loss. 1407 */ 1408 if (np->n_dircachesize == NFS_MAXDIRCACHE) { 1409 nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain)); 1410 } else 1411 np->n_dircachesize++; 1412 1413 KASSERT(ndp->dc_refcnt == 1); 1414 LIST_INSERT_HEAD(ndhp, ndp, dc_hash); 1415 TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain); 1416 ndp->dc_refcnt++; 1417 done: 1418 KASSERT(ndp->dc_refcnt > 0); 1419 NFSDC_UNLOCK(np); 1420 if (newndp) 1421 nfs_putdircache(np, newndp); 1422 return ndp; 1423 } 1424 1425 void 1426 nfs_invaldircache(struct vnode *vp, int flags) 1427 { 1428 struct nfsnode *np = VTONFS(vp); 1429 struct nfsdircache *ndp = NULL; 1430 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1431 const bool forcefree = flags & NFS_INVALDIRCACHE_FORCE; 1432 1433 #ifdef DIAGNOSTIC 1434 if (vp->v_type != VDIR) 1435 panic("nfs: invaldircache: not dir"); 1436 #endif 1437 1438 if ((flags & NFS_INVALDIRCACHE_KEEPEOF) == 0) 1439 np->n_flag &= ~NEOFVALID; 1440 1441 if (!np->n_dircache) 1442 return; 1443 1444 NFSDC_LOCK(np); 1445 if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) { 1446 while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != NULL) { 1447 KASSERT(!forcefree || ndp->dc_refcnt == 1); 1448 nfs_unlinkdircache(np, ndp); 1449 } 1450 np->n_dircachesize = 0; 1451 if (forcefree && np->n_dirgens) { 1452 kmem_free(np->n_dirgens, 1453 NFS_DIRHASHSIZ * sizeof(unsigned)); 1454 np->n_dirgens = NULL; 1455 } 1456 } else { 1457 TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain) 1458 ndp->dc_flags |= NFSDC_INVALID; 1459 } 1460 1461 NFSDC_UNLOCK(np); 1462 } 1463 1464 /* 1465 * Called once before VFS init to initialize shared and 1466 * server-specific data structures. 1467 */ 1468 static int 1469 nfs_init0(void) 1470 { 1471 1472 nfsrtt.pos = 0; 1473 rpc_vers = txdr_unsigned(RPC_VER2); 1474 rpc_call = txdr_unsigned(RPC_CALL); 1475 rpc_reply = txdr_unsigned(RPC_REPLY); 1476 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1477 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1478 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1479 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1480 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1481 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1482 nfs_prog = txdr_unsigned(NFS_PROG); 1483 nfs_true = txdr_unsigned(true); 1484 nfs_false = txdr_unsigned(false); 1485 nfs_xdrneg1 = txdr_unsigned(-1); 1486 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1487 if (nfs_ticks < 1) 1488 nfs_ticks = 1; 1489 nfsdreq_init(); 1490 1491 /* 1492 * Initialize reply list and start timer 1493 */ 1494 TAILQ_INIT(&nfs_reqq); 1495 mutex_init(&nfs_reqq_lock, MUTEX_DEFAULT, IPL_NONE); 1496 nfs_timer_init(); 1497 MOWNER_ATTACH(&nfs_mowner); 1498 1499 return 0; 1500 } 1501 1502 static volatile uint32_t nfs_mutex; 1503 static uint32_t nfs_refcount; 1504 1505 #define nfs_p() while (atomic_cas_32(&nfs_mutex, 0, 1) == 0) continue; 1506 #define nfs_v() while (atomic_cas_32(&nfs_mutex, 1, 0) == 1) continue; 1507 1508 /* 1509 * This is disgusting, but it must support both modular and monolothic 1510 * configurations, plus the code is shared between server and client. 1511 * For monolithic builds NFSSERVER may not imply NFS. Unfortunately we 1512 * can't use regular mutexes here that would require static initialization 1513 * and we can get initialized from multiple places, so we improvise. 1514 * 1515 * Yuck. 1516 */ 1517 void 1518 nfs_init(void) 1519 { 1520 1521 nfs_p(); 1522 if (nfs_refcount++ == 0) 1523 nfs_init0(); 1524 nfs_v(); 1525 } 1526 1527 void 1528 nfs_fini(void) 1529 { 1530 1531 nfs_p(); 1532 if (--nfs_refcount == 0) { 1533 MOWNER_DETACH(&nfs_mowner); 1534 nfs_timer_fini(); 1535 mutex_destroy(&nfs_reqq_lock); 1536 nfsdreq_fini(); 1537 } 1538 nfs_v(); 1539 } 1540 1541 /* 1542 * A fiddled version of m_adj() that ensures null fill to a 32-bit 1543 * boundary and only trims off the back end 1544 * 1545 * 1. trim off 'len' bytes as m_adj(mp, -len). 1546 * 2. add zero-padding 'nul' bytes at the end of the mbuf chain. 1547 */ 1548 void 1549 nfs_zeropad(struct mbuf *mp, int len, int nul) 1550 { 1551 struct mbuf *m; 1552 int count; 1553 1554 /* 1555 * Trim from tail. Scan the mbuf chain, 1556 * calculating its length and finding the last mbuf. 1557 * If the adjustment only affects this mbuf, then just 1558 * adjust and return. Otherwise, rescan and truncate 1559 * after the remaining size. 1560 */ 1561 count = 0; 1562 m = mp; 1563 for (;;) { 1564 count += m->m_len; 1565 if (m->m_next == NULL) 1566 break; 1567 m = m->m_next; 1568 } 1569 1570 KDASSERT(count >= len); 1571 1572 if (m->m_len >= len) { 1573 m->m_len -= len; 1574 } else { 1575 count -= len; 1576 /* 1577 * Correct length for chain is "count". 1578 * Find the mbuf with last data, adjust its length, 1579 * and toss data from remaining mbufs on chain. 1580 */ 1581 for (m = mp; m; m = m->m_next) { 1582 if (m->m_len >= count) { 1583 m->m_len = count; 1584 break; 1585 } 1586 count -= m->m_len; 1587 } 1588 KASSERT(m && m->m_next); 1589 m_freem(m->m_next); 1590 m->m_next = NULL; 1591 } 1592 1593 KDASSERT(m->m_next == NULL); 1594 1595 /* 1596 * zero-padding. 1597 */ 1598 if (nul > 0) { 1599 char *cp; 1600 int i; 1601 1602 if (M_READONLY(m) || M_TRAILINGSPACE(m) < nul) { 1603 struct mbuf *n; 1604 1605 KDASSERT(MLEN >= nul); 1606 n = m_get(M_WAIT, MT_DATA); 1607 MCLAIM(n, &nfs_mowner); 1608 n->m_len = nul; 1609 n->m_next = NULL; 1610 m->m_next = n; 1611 cp = mtod(n, void *); 1612 } else { 1613 cp = mtod(m, char *) + m->m_len; 1614 m->m_len += nul; 1615 } 1616 for (i = 0; i < nul; i++) 1617 *cp++ = '\0'; 1618 } 1619 return; 1620 } 1621 1622 /* 1623 * Make these functions instead of macros, so that the kernel text size 1624 * doesn't get too big... 1625 */ 1626 void 1627 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret, struct vattr *before_vap, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp) 1628 { 1629 struct mbuf *mb = *mbp; 1630 char *bpos = *bposp; 1631 u_int32_t *tl; 1632 1633 if (before_ret) { 1634 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1635 *tl = nfs_false; 1636 } else { 1637 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1638 *tl++ = nfs_true; 1639 txdr_hyper(before_vap->va_size, tl); 1640 tl += 2; 1641 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1642 tl += 2; 1643 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1644 } 1645 *bposp = bpos; 1646 *mbp = mb; 1647 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1648 } 1649 1650 void 1651 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp) 1652 { 1653 struct mbuf *mb = *mbp; 1654 char *bpos = *bposp; 1655 u_int32_t *tl; 1656 struct nfs_fattr *fp; 1657 1658 if (after_ret) { 1659 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1660 *tl = nfs_false; 1661 } else { 1662 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1663 *tl++ = nfs_true; 1664 fp = (struct nfs_fattr *)tl; 1665 nfsm_srvfattr(nfsd, after_vap, fp); 1666 } 1667 *mbp = mb; 1668 *bposp = bpos; 1669 } 1670 1671 void 1672 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap, struct nfs_fattr *fp) 1673 { 1674 1675 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1676 fp->fa_uid = txdr_unsigned(vap->va_uid); 1677 fp->fa_gid = txdr_unsigned(vap->va_gid); 1678 if (nfsd->nd_flag & ND_NFSV3) { 1679 fp->fa_type = vtonfsv3_type(vap->va_type); 1680 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1681 txdr_hyper(vap->va_size, &fp->fa3_size); 1682 txdr_hyper(vap->va_bytes, &fp->fa3_used); 1683 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev)); 1684 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev)); 1685 fp->fa3_fsid.nfsuquad[0] = 0; 1686 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1687 txdr_hyper(vap->va_fileid, &fp->fa3_fileid); 1688 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1689 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1690 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1691 } else { 1692 fp->fa_type = vtonfsv2_type(vap->va_type); 1693 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1694 fp->fa2_size = txdr_unsigned(vap->va_size); 1695 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1696 if (vap->va_type == VFIFO) 1697 fp->fa2_rdev = 0xffffffff; 1698 else 1699 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1700 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1701 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1702 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1703 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1704 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1705 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1706 } 1707 } 1708 1709 /* 1710 * This function compares two net addresses by family and returns true 1711 * if they are the same host. 1712 * If there is any doubt, return false. 1713 * The AF_INET family is handled as a special case so that address mbufs 1714 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1715 */ 1716 int 1717 netaddr_match(int family, union nethostaddr *haddr, struct mbuf *nam) 1718 { 1719 struct sockaddr_in *inetaddr; 1720 1721 switch (family) { 1722 case AF_INET: 1723 inetaddr = mtod(nam, struct sockaddr_in *); 1724 if (inetaddr->sin_family == AF_INET && 1725 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1726 return (1); 1727 break; 1728 case AF_INET6: 1729 { 1730 struct sockaddr_in6 *sin6_1, *sin6_2; 1731 1732 sin6_1 = mtod(nam, struct sockaddr_in6 *); 1733 sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *); 1734 if (sin6_1->sin6_family == AF_INET6 && 1735 IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr)) 1736 return 1; 1737 } 1738 default: 1739 break; 1740 }; 1741 return (0); 1742 } 1743 1744 struct nfs_clearcommit_ctx { 1745 struct mount *mp; 1746 }; 1747 1748 static bool 1749 nfs_clearcommit_selector(void *cl, struct vnode *vp) 1750 { 1751 struct nfs_clearcommit_ctx *c = cl; 1752 struct nfsnode *np; 1753 struct vm_page *pg; 1754 1755 KASSERT(mutex_owned(vp->v_interlock)); 1756 1757 np = VTONFS(vp); 1758 if (vp->v_type != VREG || vp->v_mount != c->mp || np == NULL) 1759 return false; 1760 np->n_pushlo = np->n_pushhi = np->n_pushedlo = 1761 np->n_pushedhi = 0; 1762 np->n_commitflags &= 1763 ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID); 1764 TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq.queue) { 1765 pg->flags &= ~PG_NEEDCOMMIT; 1766 } 1767 return false; 1768 } 1769 1770 /* 1771 * The write verifier has changed (probably due to a server reboot), so all 1772 * PG_NEEDCOMMIT pages will have to be written again. Since they are marked 1773 * as dirty or are being written out just now, all this takes is clearing 1774 * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for 1775 * the mount point. 1776 */ 1777 void 1778 nfs_clearcommit(struct mount *mp) 1779 { 1780 struct vnode *vp __diagused; 1781 struct vnode_iterator *marker; 1782 struct nfsmount *nmp = VFSTONFS(mp); 1783 struct nfs_clearcommit_ctx ctx; 1784 1785 rw_enter(&nmp->nm_writeverflock, RW_WRITER); 1786 vfs_vnode_iterator_init(mp, &marker); 1787 ctx.mp = mp; 1788 vp = vfs_vnode_iterator_next(marker, nfs_clearcommit_selector, &ctx); 1789 KASSERT(vp == NULL); 1790 vfs_vnode_iterator_destroy(marker); 1791 mutex_enter(&nmp->nm_lock); 1792 nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF; 1793 mutex_exit(&nmp->nm_lock); 1794 rw_exit(&nmp->nm_writeverflock); 1795 } 1796 1797 void 1798 nfs_merge_commit_ranges(struct vnode *vp) 1799 { 1800 struct nfsnode *np = VTONFS(vp); 1801 1802 KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID); 1803 1804 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) { 1805 np->n_pushedlo = np->n_pushlo; 1806 np->n_pushedhi = np->n_pushhi; 1807 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID; 1808 } else { 1809 if (np->n_pushlo < np->n_pushedlo) 1810 np->n_pushedlo = np->n_pushlo; 1811 if (np->n_pushhi > np->n_pushedhi) 1812 np->n_pushedhi = np->n_pushhi; 1813 } 1814 1815 np->n_pushlo = np->n_pushhi = 0; 1816 np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID; 1817 1818 #ifdef NFS_DEBUG_COMMIT 1819 printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo, 1820 (unsigned)np->n_pushedhi); 1821 #endif 1822 } 1823 1824 int 1825 nfs_in_committed_range(struct vnode *vp, off_t off, off_t len) 1826 { 1827 struct nfsnode *np = VTONFS(vp); 1828 off_t lo, hi; 1829 1830 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) 1831 return 0; 1832 lo = off; 1833 hi = lo + len; 1834 1835 return (lo >= np->n_pushedlo && hi <= np->n_pushedhi); 1836 } 1837 1838 int 1839 nfs_in_tobecommitted_range(struct vnode *vp, off_t off, off_t len) 1840 { 1841 struct nfsnode *np = VTONFS(vp); 1842 off_t lo, hi; 1843 1844 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) 1845 return 0; 1846 lo = off; 1847 hi = lo + len; 1848 1849 return (lo >= np->n_pushlo && hi <= np->n_pushhi); 1850 } 1851 1852 void 1853 nfs_add_committed_range(struct vnode *vp, off_t off, off_t len) 1854 { 1855 struct nfsnode *np = VTONFS(vp); 1856 off_t lo, hi; 1857 1858 lo = off; 1859 hi = lo + len; 1860 1861 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) { 1862 np->n_pushedlo = lo; 1863 np->n_pushedhi = hi; 1864 np->n_commitflags |= NFS_COMMIT_PUSHED_VALID; 1865 } else { 1866 if (hi > np->n_pushedhi) 1867 np->n_pushedhi = hi; 1868 if (lo < np->n_pushedlo) 1869 np->n_pushedlo = lo; 1870 } 1871 #ifdef NFS_DEBUG_COMMIT 1872 printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo, 1873 (unsigned)np->n_pushedhi); 1874 #endif 1875 } 1876 1877 void 1878 nfs_del_committed_range(struct vnode *vp, off_t off, off_t len) 1879 { 1880 struct nfsnode *np = VTONFS(vp); 1881 off_t lo, hi; 1882 1883 if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) 1884 return; 1885 1886 lo = off; 1887 hi = lo + len; 1888 1889 if (lo > np->n_pushedhi || hi < np->n_pushedlo) 1890 return; 1891 if (lo <= np->n_pushedlo) 1892 np->n_pushedlo = hi; 1893 else if (hi >= np->n_pushedhi) 1894 np->n_pushedhi = lo; 1895 else { 1896 /* 1897 * XXX There's only one range. If the deleted range 1898 * is in the middle, pick the largest of the 1899 * contiguous ranges that it leaves. 1900 */ 1901 if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi)) 1902 np->n_pushedhi = lo; 1903 else 1904 np->n_pushedlo = hi; 1905 } 1906 #ifdef NFS_DEBUG_COMMIT 1907 printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo, 1908 (unsigned)np->n_pushedhi); 1909 #endif 1910 } 1911 1912 void 1913 nfs_add_tobecommitted_range(struct vnode *vp, off_t off, off_t len) 1914 { 1915 struct nfsnode *np = VTONFS(vp); 1916 off_t lo, hi; 1917 1918 lo = off; 1919 hi = lo + len; 1920 1921 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) { 1922 np->n_pushlo = lo; 1923 np->n_pushhi = hi; 1924 np->n_commitflags |= NFS_COMMIT_PUSH_VALID; 1925 } else { 1926 if (lo < np->n_pushlo) 1927 np->n_pushlo = lo; 1928 if (hi > np->n_pushhi) 1929 np->n_pushhi = hi; 1930 } 1931 #ifdef NFS_DEBUG_COMMIT 1932 printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo, 1933 (unsigned)np->n_pushhi); 1934 #endif 1935 } 1936 1937 void 1938 nfs_del_tobecommitted_range(struct vnode *vp, off_t off, off_t len) 1939 { 1940 struct nfsnode *np = VTONFS(vp); 1941 off_t lo, hi; 1942 1943 if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) 1944 return; 1945 1946 lo = off; 1947 hi = lo + len; 1948 1949 if (lo > np->n_pushhi || hi < np->n_pushlo) 1950 return; 1951 1952 if (lo <= np->n_pushlo) 1953 np->n_pushlo = hi; 1954 else if (hi >= np->n_pushhi) 1955 np->n_pushhi = lo; 1956 else { 1957 /* 1958 * XXX There's only one range. If the deleted range 1959 * is in the middle, pick the largest of the 1960 * contiguous ranges that it leaves. 1961 */ 1962 if ((np->n_pushlo - lo) > (hi - np->n_pushhi)) 1963 np->n_pushhi = lo; 1964 else 1965 np->n_pushlo = hi; 1966 } 1967 #ifdef NFS_DEBUG_COMMIT 1968 printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo, 1969 (unsigned)np->n_pushhi); 1970 #endif 1971 } 1972 1973 /* 1974 * Map errnos to NFS error numbers. For Version 3 also filter out error 1975 * numbers not specified for the associated procedure. 1976 */ 1977 int 1978 nfsrv_errmap(struct nfsrv_descript *nd, int err) 1979 { 1980 const short *defaulterrp, *errp; 1981 1982 if (nd->nd_flag & ND_NFSV3) { 1983 if (nd->nd_procnum <= NFSPROC_COMMIT) { 1984 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 1985 while (*++errp) { 1986 if (*errp == err) 1987 return (err); 1988 else if (*errp > err) 1989 break; 1990 } 1991 return ((int)*defaulterrp); 1992 } else 1993 return (err & 0xffff); 1994 } 1995 if (err <= ELAST) 1996 return ((int)nfsrv_v2errmap[err - 1]); 1997 return (NFSERR_IO); 1998 } 1999 2000 u_int32_t 2001 nfs_getxid(void) 2002 { 2003 u_int32_t newxid; 2004 2005 if (__predict_false(nfs_xid == 0)) { 2006 nfs_xid = cprng_fast32(); 2007 } 2008 2009 /* get next xid. skip 0 */ 2010 do { 2011 newxid = atomic_inc_32_nv(&nfs_xid); 2012 } while (__predict_false(newxid == 0)); 2013 2014 return txdr_unsigned(newxid); 2015 } 2016 2017 /* 2018 * assign a new xid for existing request. 2019 * used for NFSERR_JUKEBOX handling. 2020 */ 2021 void 2022 nfs_renewxid(struct nfsreq *req) 2023 { 2024 u_int32_t xid; 2025 int off; 2026 2027 xid = nfs_getxid(); 2028 if (req->r_nmp->nm_sotype == SOCK_STREAM) 2029 off = sizeof(u_int32_t); /* RPC record mark */ 2030 else 2031 off = 0; 2032 2033 m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid); 2034 req->r_xid = xid; 2035 } 2036