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