The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfs/nfs_subs.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: a10682607efc65772dd1cad41764ffae


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.