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