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