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 /*
    2  * Copyright (c) 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Rick Macklem at The University of Guelph.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. All advertising materials mentioning features or use of this software
   17  *    must display the following acknowledgement:
   18  *      This product includes software developed by the University of
   19  *      California, Berkeley and its contributors.
   20  * 4. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)nfs_subs.c  8.8 (Berkeley) 5/22/95
   37  * $FreeBSD$
   38  */
   39 
   40 /*
   41  * These functions support the macros and help fiddle mbuf chains for
   42  * the nfs op functions. They do things like create the rpc header and
   43  * copy data between mbuf chains and uio lists.
   44  */
   45 #include <sys/param.h>
   46 #include <sys/buf.h>
   47 #include <sys/proc.h>
   48 #include <sys/systm.h>
   49 #include <sys/kernel.h>
   50 #include <sys/mount.h>
   51 #include <sys/vnode.h>
   52 #include <sys/namei.h>
   53 #include <sys/mbuf.h>
   54 #include <sys/socket.h>
   55 #include <sys/stat.h>
   56 #include <sys/malloc.h>
   57 #include <sys/sysent.h>
   58 #include <sys/syscall.h>
   59 
   60 #include <vm/vm.h>
   61 #include <vm/vm_object.h>
   62 #include <vm/vm_extern.h>
   63 #include <vm/vm_zone.h>
   64 
   65 #include <nfs/rpcv2.h>
   66 #include <nfs/nfsproto.h>
   67 #include <nfs/nfs.h>
   68 #include <nfs/nfsnode.h>
   69 #include <nfs/xdr_subs.h>
   70 #include <nfs/nfsm_subs.h>
   71 #include <nfs/nfsmount.h>
   72 #include <nfs/nqnfs.h>
   73 #include <nfs/nfsrtt.h>
   74 
   75 #include <miscfs/specfs/specdev.h>
   76 
   77 #include <netinet/in.h>
   78 #ifdef ISO
   79 #include <netiso/iso.h>
   80 #endif
   81 
   82 /*
   83  * Data items converted to xdr at startup, since they are constant
   84  * This is kinda hokey, but may save a little time doing byte swaps
   85  */
   86 u_int32_t nfs_xdrneg1;
   87 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
   88         rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
   89         rpc_auth_kerb;
   90 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
   91 
   92 /* And other global data */
   93 static u_int32_t nfs_xid = 0;
   94 static enum vtype nv2tov_type[8]= {
   95         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON 
   96 };
   97 enum vtype nv3tov_type[8]= {
   98         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
   99 };
  100 
  101 int nfs_ticks;
  102 
  103 struct nfs_reqq nfs_reqq;
  104 struct nfssvc_sockhead nfssvc_sockhead;
  105 int nfssvc_sockhead_flag;
  106 struct nfsd_head nfsd_head;
  107 int nfsd_head_flag;
  108 struct nfs_bufq nfs_bufq;
  109 struct nqtimerhead nqtimerhead;
  110 struct nqfhhashhead *nqfhhashtbl;
  111 u_long nqfhhash;
  112 
  113 static void (*nfs_prev_lease_updatetime) __P((int));
  114 static int nfs_prev_nfssvc_sy_narg;
  115 static sy_call_t *nfs_prev_nfssvc_sy_call;
  116 
  117 #ifndef NFS_NOSERVER
  118 
  119 static vop_t *nfs_prev_vop_lease_check;
  120 static int nfs_prev_getfh_sy_narg;
  121 static sy_call_t *nfs_prev_getfh_sy_call;
  122 
  123 /*
  124  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
  125  */
  126 int nfsv3_procid[NFS_NPROCS] = {
  127         NFSPROC_NULL,
  128         NFSPROC_GETATTR,
  129         NFSPROC_SETATTR,
  130         NFSPROC_NOOP,
  131         NFSPROC_LOOKUP,
  132         NFSPROC_READLINK,
  133         NFSPROC_READ,
  134         NFSPROC_NOOP,
  135         NFSPROC_WRITE,
  136         NFSPROC_CREATE,
  137         NFSPROC_REMOVE,
  138         NFSPROC_RENAME,
  139         NFSPROC_LINK,
  140         NFSPROC_SYMLINK,
  141         NFSPROC_MKDIR,
  142         NFSPROC_RMDIR,
  143         NFSPROC_READDIR,
  144         NFSPROC_FSSTAT,
  145         NFSPROC_NOOP,
  146         NFSPROC_NOOP,
  147         NFSPROC_NOOP,
  148         NFSPROC_NOOP,
  149         NFSPROC_NOOP,
  150         NFSPROC_NOOP,
  151         NFSPROC_NOOP,
  152         NFSPROC_NOOP
  153 };
  154 
  155 #endif /* NFS_NOSERVER */
  156 /*
  157  * and the reverse mapping from generic to Version 2 procedure numbers
  158  */
  159 int nfsv2_procid[NFS_NPROCS] = {
  160         NFSV2PROC_NULL,
  161         NFSV2PROC_GETATTR,
  162         NFSV2PROC_SETATTR,
  163         NFSV2PROC_LOOKUP,
  164         NFSV2PROC_NOOP,
  165         NFSV2PROC_READLINK,
  166         NFSV2PROC_READ,
  167         NFSV2PROC_WRITE,
  168         NFSV2PROC_CREATE,
  169         NFSV2PROC_MKDIR,
  170         NFSV2PROC_SYMLINK,
  171         NFSV2PROC_CREATE,
  172         NFSV2PROC_REMOVE,
  173         NFSV2PROC_RMDIR,
  174         NFSV2PROC_RENAME,
  175         NFSV2PROC_LINK,
  176         NFSV2PROC_READDIR,
  177         NFSV2PROC_NOOP,
  178         NFSV2PROC_STATFS,
  179         NFSV2PROC_NOOP,
  180         NFSV2PROC_NOOP,
  181         NFSV2PROC_NOOP,
  182         NFSV2PROC_NOOP,
  183         NFSV2PROC_NOOP,
  184         NFSV2PROC_NOOP,
  185         NFSV2PROC_NOOP,
  186 };
  187 
  188 #ifndef NFS_NOSERVER
  189 /*
  190  * Maps errno values to nfs error numbers.
  191  * Use NFSERR_IO as the catch all for ones not specifically defined in
  192  * RFC 1094.
  193  */
  194 static u_char nfsrv_v2errmap[ELAST] = {
  195   NFSERR_PERM,  NFSERR_NOENT,   NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  196   NFSERR_NXIO,  NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  197   NFSERR_IO,    NFSERR_IO,      NFSERR_ACCES,   NFSERR_IO,      NFSERR_IO,
  198   NFSERR_IO,    NFSERR_EXIST,   NFSERR_IO,      NFSERR_NODEV,   NFSERR_NOTDIR,
  199   NFSERR_ISDIR, NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  200   NFSERR_IO,    NFSERR_FBIG,    NFSERR_NOSPC,   NFSERR_IO,      NFSERR_ROFS,
  201   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  202   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  203   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  204   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  205   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  206   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  207   NFSERR_IO,    NFSERR_IO,      NFSERR_NAMETOL, NFSERR_IO,      NFSERR_IO,
  208   NFSERR_NOTEMPTY, NFSERR_IO,   NFSERR_IO,      NFSERR_DQUOT,   NFSERR_STALE,
  209   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  210   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  211   NFSERR_IO,    NFSERR_IO,      NFSERR_IO,      NFSERR_IO,      NFSERR_IO,
  212   NFSERR_IO /* << Last is 86 */
  213 };
  214 
  215 /*
  216  * Maps errno values to nfs error numbers.
  217  * Although it is not obvious whether or not NFS clients really care if
  218  * a returned error value is in the specified list for the procedure, the
  219  * safest thing to do is filter them appropriately. For Version 2, the
  220  * X/Open XNFS document is the only specification that defines error values
  221  * for each RPC (The RFC simply lists all possible error values for all RPCs),
  222  * so I have decided to not do this for Version 2.
  223  * The first entry is the default error return and the rest are the valid
  224  * errors for that RPC in increasing numeric order.
  225  */
  226 static short nfsv3err_null[] = {
  227         0,
  228         0,
  229 };
  230 
  231 static short nfsv3err_getattr[] = {
  232         NFSERR_IO,
  233         NFSERR_IO,
  234         NFSERR_STALE,
  235         NFSERR_BADHANDLE,
  236         NFSERR_SERVERFAULT,
  237         0,
  238 };
  239 
  240 static short nfsv3err_setattr[] = {
  241         NFSERR_IO,
  242         NFSERR_PERM,
  243         NFSERR_IO,
  244         NFSERR_ACCES,
  245         NFSERR_INVAL,
  246         NFSERR_NOSPC,
  247         NFSERR_ROFS,
  248         NFSERR_DQUOT,
  249         NFSERR_STALE,
  250         NFSERR_BADHANDLE,
  251         NFSERR_NOT_SYNC,
  252         NFSERR_SERVERFAULT,
  253         0,
  254 };
  255 
  256 static short nfsv3err_lookup[] = {
  257         NFSERR_IO,
  258         NFSERR_NOENT,
  259         NFSERR_IO,
  260         NFSERR_ACCES,
  261         NFSERR_NOTDIR,
  262         NFSERR_NAMETOL,
  263         NFSERR_STALE,
  264         NFSERR_BADHANDLE,
  265         NFSERR_SERVERFAULT,
  266         0,
  267 };
  268 
  269 static short nfsv3err_access[] = {
  270         NFSERR_IO,
  271         NFSERR_IO,
  272         NFSERR_STALE,
  273         NFSERR_BADHANDLE,
  274         NFSERR_SERVERFAULT,
  275         0,
  276 };
  277 
  278 static short nfsv3err_readlink[] = {
  279         NFSERR_IO,
  280         NFSERR_IO,
  281         NFSERR_ACCES,
  282         NFSERR_INVAL,
  283         NFSERR_STALE,
  284         NFSERR_BADHANDLE,
  285         NFSERR_NOTSUPP,
  286         NFSERR_SERVERFAULT,
  287         0,
  288 };
  289 
  290 static short nfsv3err_read[] = {
  291         NFSERR_IO,
  292         NFSERR_IO,
  293         NFSERR_NXIO,
  294         NFSERR_ACCES,
  295         NFSERR_INVAL,
  296         NFSERR_STALE,
  297         NFSERR_BADHANDLE,
  298         NFSERR_SERVERFAULT,
  299         0,
  300 };
  301 
  302 static short nfsv3err_write[] = {
  303         NFSERR_IO,
  304         NFSERR_IO,
  305         NFSERR_ACCES,
  306         NFSERR_INVAL,
  307         NFSERR_FBIG,
  308         NFSERR_NOSPC,
  309         NFSERR_ROFS,
  310         NFSERR_DQUOT,
  311         NFSERR_STALE,
  312         NFSERR_BADHANDLE,
  313         NFSERR_SERVERFAULT,
  314         0,
  315 };
  316 
  317 static short nfsv3err_create[] = {
  318         NFSERR_IO,
  319         NFSERR_IO,
  320         NFSERR_ACCES,
  321         NFSERR_EXIST,
  322         NFSERR_NOTDIR,
  323         NFSERR_NOSPC,
  324         NFSERR_ROFS,
  325         NFSERR_NAMETOL,
  326         NFSERR_DQUOT,
  327         NFSERR_STALE,
  328         NFSERR_BADHANDLE,
  329         NFSERR_NOTSUPP,
  330         NFSERR_SERVERFAULT,
  331         0,
  332 };
  333 
  334 static short nfsv3err_mkdir[] = {
  335         NFSERR_IO,
  336         NFSERR_IO,
  337         NFSERR_ACCES,
  338         NFSERR_EXIST,
  339         NFSERR_NOTDIR,
  340         NFSERR_NOSPC,
  341         NFSERR_ROFS,
  342         NFSERR_NAMETOL,
  343         NFSERR_DQUOT,
  344         NFSERR_STALE,
  345         NFSERR_BADHANDLE,
  346         NFSERR_NOTSUPP,
  347         NFSERR_SERVERFAULT,
  348         0,
  349 };
  350 
  351 static short nfsv3err_symlink[] = {
  352         NFSERR_IO,
  353         NFSERR_IO,
  354         NFSERR_ACCES,
  355         NFSERR_EXIST,
  356         NFSERR_NOTDIR,
  357         NFSERR_NOSPC,
  358         NFSERR_ROFS,
  359         NFSERR_NAMETOL,
  360         NFSERR_DQUOT,
  361         NFSERR_STALE,
  362         NFSERR_BADHANDLE,
  363         NFSERR_NOTSUPP,
  364         NFSERR_SERVERFAULT,
  365         0,
  366 };
  367 
  368 static short nfsv3err_mknod[] = {
  369         NFSERR_IO,
  370         NFSERR_IO,
  371         NFSERR_ACCES,
  372         NFSERR_EXIST,
  373         NFSERR_NOTDIR,
  374         NFSERR_NOSPC,
  375         NFSERR_ROFS,
  376         NFSERR_NAMETOL,
  377         NFSERR_DQUOT,
  378         NFSERR_STALE,
  379         NFSERR_BADHANDLE,
  380         NFSERR_NOTSUPP,
  381         NFSERR_SERVERFAULT,
  382         NFSERR_BADTYPE,
  383         0,
  384 };
  385 
  386 static short nfsv3err_remove[] = {
  387         NFSERR_IO,
  388         NFSERR_NOENT,
  389         NFSERR_IO,
  390         NFSERR_ACCES,
  391         NFSERR_NOTDIR,
  392         NFSERR_ROFS,
  393         NFSERR_NAMETOL,
  394         NFSERR_STALE,
  395         NFSERR_BADHANDLE,
  396         NFSERR_SERVERFAULT,
  397         0,
  398 };
  399 
  400 static short nfsv3err_rmdir[] = {
  401         NFSERR_IO,
  402         NFSERR_NOENT,
  403         NFSERR_IO,
  404         NFSERR_ACCES,
  405         NFSERR_EXIST,
  406         NFSERR_NOTDIR,
  407         NFSERR_INVAL,
  408         NFSERR_ROFS,
  409         NFSERR_NAMETOL,
  410         NFSERR_NOTEMPTY,
  411         NFSERR_STALE,
  412         NFSERR_BADHANDLE,
  413         NFSERR_NOTSUPP,
  414         NFSERR_SERVERFAULT,
  415         0,
  416 };
  417 
  418 static short nfsv3err_rename[] = {
  419         NFSERR_IO,
  420         NFSERR_NOENT,
  421         NFSERR_IO,
  422         NFSERR_ACCES,
  423         NFSERR_EXIST,
  424         NFSERR_XDEV,
  425         NFSERR_NOTDIR,
  426         NFSERR_ISDIR,
  427         NFSERR_INVAL,
  428         NFSERR_NOSPC,
  429         NFSERR_ROFS,
  430         NFSERR_MLINK,
  431         NFSERR_NAMETOL,
  432         NFSERR_NOTEMPTY,
  433         NFSERR_DQUOT,
  434         NFSERR_STALE,
  435         NFSERR_BADHANDLE,
  436         NFSERR_NOTSUPP,
  437         NFSERR_SERVERFAULT,
  438         0,
  439 };
  440 
  441 static short nfsv3err_link[] = {
  442         NFSERR_IO,
  443         NFSERR_IO,
  444         NFSERR_ACCES,
  445         NFSERR_EXIST,
  446         NFSERR_XDEV,
  447         NFSERR_NOTDIR,
  448         NFSERR_INVAL,
  449         NFSERR_NOSPC,
  450         NFSERR_ROFS,
  451         NFSERR_MLINK,
  452         NFSERR_NAMETOL,
  453         NFSERR_DQUOT,
  454         NFSERR_STALE,
  455         NFSERR_BADHANDLE,
  456         NFSERR_NOTSUPP,
  457         NFSERR_SERVERFAULT,
  458         0,
  459 };
  460 
  461 static short nfsv3err_readdir[] = {
  462         NFSERR_IO,
  463         NFSERR_IO,
  464         NFSERR_ACCES,
  465         NFSERR_NOTDIR,
  466         NFSERR_STALE,
  467         NFSERR_BADHANDLE,
  468         NFSERR_BAD_COOKIE,
  469         NFSERR_TOOSMALL,
  470         NFSERR_SERVERFAULT,
  471         0,
  472 };
  473 
  474 static short nfsv3err_readdirplus[] = {
  475         NFSERR_IO,
  476         NFSERR_IO,
  477         NFSERR_ACCES,
  478         NFSERR_NOTDIR,
  479         NFSERR_STALE,
  480         NFSERR_BADHANDLE,
  481         NFSERR_BAD_COOKIE,
  482         NFSERR_NOTSUPP,
  483         NFSERR_TOOSMALL,
  484         NFSERR_SERVERFAULT,
  485         0,
  486 };
  487 
  488 static short nfsv3err_fsstat[] = {
  489         NFSERR_IO,
  490         NFSERR_IO,
  491         NFSERR_STALE,
  492         NFSERR_BADHANDLE,
  493         NFSERR_SERVERFAULT,
  494         0,
  495 };
  496 
  497 static short nfsv3err_fsinfo[] = {
  498         NFSERR_STALE,
  499         NFSERR_STALE,
  500         NFSERR_BADHANDLE,
  501         NFSERR_SERVERFAULT,
  502         0,
  503 };
  504 
  505 static short nfsv3err_pathconf[] = {
  506         NFSERR_STALE,
  507         NFSERR_STALE,
  508         NFSERR_BADHANDLE,
  509         NFSERR_SERVERFAULT,
  510         0,
  511 };
  512 
  513 static short nfsv3err_commit[] = {
  514         NFSERR_IO,
  515         NFSERR_IO,
  516         NFSERR_STALE,
  517         NFSERR_BADHANDLE,
  518         NFSERR_SERVERFAULT,
  519         0,
  520 };
  521 
  522 static short *nfsrv_v3errmap[] = {
  523         nfsv3err_null,
  524         nfsv3err_getattr,
  525         nfsv3err_setattr,
  526         nfsv3err_lookup,
  527         nfsv3err_access,
  528         nfsv3err_readlink,
  529         nfsv3err_read,
  530         nfsv3err_write,
  531         nfsv3err_create,
  532         nfsv3err_mkdir,
  533         nfsv3err_symlink,
  534         nfsv3err_mknod,
  535         nfsv3err_remove,
  536         nfsv3err_rmdir,
  537         nfsv3err_rename,
  538         nfsv3err_link,
  539         nfsv3err_readdir,
  540         nfsv3err_readdirplus,
  541         nfsv3err_fsstat,
  542         nfsv3err_fsinfo,
  543         nfsv3err_pathconf,
  544         nfsv3err_commit,
  545 };
  546 
  547 #endif /* NFS_NOSERVER */
  548 
  549 extern struct nfsrtt nfsrtt;
  550 extern time_t nqnfsstarttime;
  551 extern int nqsrv_clockskew;
  552 extern int nqsrv_writeslack;
  553 extern int nqsrv_maxlease;
  554 extern struct nfsstats nfsstats;
  555 extern int nqnfs_piggy[NFS_NPROCS];
  556 extern nfstype nfsv2_type[9];
  557 extern nfstype nfsv3_type[9];
  558 extern struct nfsnodehashhead *nfsnodehashtbl;
  559 extern u_long nfsnodehash;
  560 
  561 struct getfh_args;
  562 extern int getfh(struct proc *, struct getfh_args *, int *);
  563 struct nfssvc_args;
  564 extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
  565 
  566 LIST_HEAD(nfsnodehashhead, nfsnode);
  567 
  568 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
  569 
  570 u_quad_t
  571 nfs_curusec() 
  572 {
  573         struct timeval tv;
  574         
  575         getmicrotime(&tv);
  576         return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
  577 }
  578 
  579 /*
  580  * Create the header for an rpc request packet
  581  * The hsiz is the size of the rest of the nfs request header.
  582  * (just used to decide if a cluster is a good idea)
  583  */
  584 struct mbuf *
  585 nfsm_reqh(vp, procid, hsiz, bposp)
  586         struct vnode *vp;
  587         u_long procid;
  588         int hsiz;
  589         caddr_t *bposp;
  590 {
  591         register struct mbuf *mb;
  592         register u_int32_t *tl;
  593         register caddr_t bpos;
  594         struct mbuf *mb2;
  595         struct nfsmount *nmp;
  596         int nqflag;
  597 
  598         MGET(mb, M_WAIT, MT_DATA);
  599         if (hsiz >= MINCLSIZE)
  600                 MCLGET(mb, M_WAIT);
  601         mb->m_len = 0;
  602         bpos = mtod(mb, caddr_t);
  603 
  604         /*
  605          * For NQNFS, add lease request.
  606          */
  607         if (vp) {
  608                 nmp = VFSTONFS(vp->v_mount);
  609                 if (nmp->nm_flag & NFSMNT_NQNFS) {
  610                         nqflag = NQNFS_NEEDLEASE(vp, procid);
  611                         if (nqflag) {
  612                                 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
  613                                 *tl++ = txdr_unsigned(nqflag);
  614                                 *tl = txdr_unsigned(nmp->nm_leaseterm);
  615                         } else {
  616                                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
  617                                 *tl = 0;
  618                         }
  619                 }
  620         }
  621         /* Finally, return values */
  622         *bposp = bpos;
  623         return (mb);
  624 }
  625 
  626 /*
  627  * Build the RPC header and fill in the authorization info.
  628  * The authorization string argument is only used when the credentials
  629  * come from outside of the kernel.
  630  * Returns the head of the mbuf list.
  631  */
  632 struct mbuf *
  633 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
  634         verf_str, mrest, mrest_len, mbp, xidp)
  635         register struct ucred *cr;
  636         int nmflag;
  637         int procid;
  638         int auth_type;
  639         int auth_len;
  640         char *auth_str;
  641         int verf_len;
  642         char *verf_str;
  643         struct mbuf *mrest;
  644         int mrest_len;
  645         struct mbuf **mbp;
  646         u_int32_t *xidp;
  647 {
  648         register struct mbuf *mb;
  649         register u_int32_t *tl;
  650         register caddr_t bpos;
  651         register int i;
  652         struct mbuf *mreq, *mb2;
  653         int siz, grpsiz, authsiz;
  654 
  655         authsiz = nfsm_rndup(auth_len);
  656         MGETHDR(mb, M_WAIT, MT_DATA);
  657         if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
  658                 MCLGET(mb, M_WAIT);
  659         } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
  660                 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
  661         } else {
  662                 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
  663         }
  664         mb->m_len = 0;
  665         mreq = mb;
  666         bpos = mtod(mb, caddr_t);
  667 
  668         /*
  669          * First the RPC header.
  670          */
  671         nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
  672 
  673         /* Get a pretty random xid to start with */
  674         if (!nfs_xid) 
  675                 nfs_xid = random();
  676         /*
  677          * Skip zero xid if it should ever happen.
  678          */
  679         if (++nfs_xid == 0)
  680                 nfs_xid++;
  681 
  682         *tl++ = *xidp = txdr_unsigned(nfs_xid);
  683         *tl++ = rpc_call;
  684         *tl++ = rpc_vers;
  685         if (nmflag & NFSMNT_NQNFS) {
  686                 *tl++ = txdr_unsigned(NQNFS_PROG);
  687                 *tl++ = txdr_unsigned(NQNFS_VER3);
  688         } else {
  689                 *tl++ = txdr_unsigned(NFS_PROG);
  690                 if (nmflag & NFSMNT_NFSV3)
  691                         *tl++ = txdr_unsigned(NFS_VER3);
  692                 else
  693                         *tl++ = txdr_unsigned(NFS_VER2);
  694         }
  695         if (nmflag & NFSMNT_NFSV3)
  696                 *tl++ = txdr_unsigned(procid);
  697         else
  698                 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
  699 
  700         /*
  701          * And then the authorization cred.
  702          */
  703         *tl++ = txdr_unsigned(auth_type);
  704         *tl = txdr_unsigned(authsiz);
  705         switch (auth_type) {
  706         case RPCAUTH_UNIX:
  707                 nfsm_build(tl, u_int32_t *, auth_len);
  708                 *tl++ = 0;              /* stamp ?? */
  709                 *tl++ = 0;              /* NULL hostname */
  710                 *tl++ = txdr_unsigned(cr->cr_uid);
  711                 *tl++ = txdr_unsigned(cr->cr_groups[0]);
  712                 grpsiz = (auth_len >> 2) - 5;
  713                 *tl++ = txdr_unsigned(grpsiz);
  714                 for (i = 1; i <= grpsiz; i++)
  715                         *tl++ = txdr_unsigned(cr->cr_groups[i]);
  716                 break;
  717         case RPCAUTH_KERB4:
  718                 siz = auth_len;
  719                 while (siz > 0) {
  720                         if (M_TRAILINGSPACE(mb) == 0) {
  721                                 MGET(mb2, M_WAIT, MT_DATA);
  722                                 if (siz >= MINCLSIZE)
  723                                         MCLGET(mb2, M_WAIT);
  724                                 mb->m_next = mb2;
  725                                 mb = mb2;
  726                                 mb->m_len = 0;
  727                                 bpos = mtod(mb, caddr_t);
  728                         }
  729                         i = min(siz, M_TRAILINGSPACE(mb));
  730                         bcopy(auth_str, bpos, i);
  731                         mb->m_len += i;
  732                         auth_str += i;
  733                         bpos += i;
  734                         siz -= i;
  735                 }
  736                 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
  737                         for (i = 0; i < siz; i++)
  738                                 *bpos++ = '\0';
  739                         mb->m_len += siz;
  740                 }
  741                 break;
  742         };
  743 
  744         /*
  745          * And the verifier...
  746          */
  747         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  748         if (verf_str) {
  749                 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
  750                 *tl = txdr_unsigned(verf_len);
  751                 siz = verf_len;
  752                 while (siz > 0) {
  753                         if (M_TRAILINGSPACE(mb) == 0) {
  754                                 MGET(mb2, M_WAIT, MT_DATA);
  755                                 if (siz >= MINCLSIZE)
  756                                         MCLGET(mb2, M_WAIT);
  757                                 mb->m_next = mb2;
  758                                 mb = mb2;
  759                                 mb->m_len = 0;
  760                                 bpos = mtod(mb, caddr_t);
  761                         }
  762                         i = min(siz, M_TRAILINGSPACE(mb));
  763                         bcopy(verf_str, bpos, i);
  764                         mb->m_len += i;
  765                         verf_str += i;
  766                         bpos += i;
  767                         siz -= i;
  768                 }
  769                 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
  770                         for (i = 0; i < siz; i++)
  771                                 *bpos++ = '\0';
  772                         mb->m_len += siz;
  773                 }
  774         } else {
  775                 *tl++ = txdr_unsigned(RPCAUTH_NULL);
  776                 *tl = 0;
  777         }
  778         mb->m_next = mrest;
  779         mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
  780         mreq->m_pkthdr.rcvif = (struct ifnet *)0;
  781         *mbp = mb;
  782         return (mreq);
  783 }
  784 
  785 /*
  786  * copies mbuf chain to the uio scatter/gather list
  787  */
  788 int
  789 nfsm_mbuftouio(mrep, uiop, siz, dpos)
  790         struct mbuf **mrep;
  791         register struct uio *uiop;
  792         int siz;
  793         caddr_t *dpos;
  794 {
  795         register char *mbufcp, *uiocp;
  796         register int xfer, left, len;
  797         register struct mbuf *mp;
  798         long uiosiz, rem;
  799         int error = 0;
  800 
  801         mp = *mrep;
  802         mbufcp = *dpos;
  803         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
  804         rem = nfsm_rndup(siz)-siz;
  805         while (siz > 0) {
  806                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
  807                         return (EFBIG);
  808                 left = uiop->uio_iov->iov_len;
  809                 uiocp = uiop->uio_iov->iov_base;
  810                 if (left > siz)
  811                         left = siz;
  812                 uiosiz = left;
  813                 while (left > 0) {
  814                         while (len == 0) {
  815                                 mp = mp->m_next;
  816                                 if (mp == NULL)
  817                                         return (EBADRPC);
  818                                 mbufcp = mtod(mp, caddr_t);
  819                                 len = mp->m_len;
  820                         }
  821                         xfer = (left > len) ? len : left;
  822 #ifdef notdef
  823                         /* Not Yet.. */
  824                         if (uiop->uio_iov->iov_op != NULL)
  825                                 (*(uiop->uio_iov->iov_op))
  826                                 (mbufcp, uiocp, xfer);
  827                         else
  828 #endif
  829                         if (uiop->uio_segflg == UIO_SYSSPACE)
  830                                 bcopy(mbufcp, uiocp, xfer);
  831                         else
  832                                 copyout(mbufcp, uiocp, xfer);
  833                         left -= xfer;
  834                         len -= xfer;
  835                         mbufcp += xfer;
  836                         uiocp += xfer;
  837                         uiop->uio_offset += xfer;
  838                         uiop->uio_resid -= xfer;
  839                 }
  840                 if (uiop->uio_iov->iov_len <= siz) {
  841                         uiop->uio_iovcnt--;
  842                         uiop->uio_iov++;
  843                 } else {
  844                         uiop->uio_iov->iov_base += uiosiz;
  845                         uiop->uio_iov->iov_len -= uiosiz;
  846                 }
  847                 siz -= uiosiz;
  848         }
  849         *dpos = mbufcp;
  850         *mrep = mp;
  851         if (rem > 0) {
  852                 if (len < rem)
  853                         error = nfs_adv(mrep, dpos, rem, len);
  854                 else
  855                         *dpos += rem;
  856         }
  857         return (error);
  858 }
  859 
  860 /*
  861  * copies a uio scatter/gather list to an mbuf chain.
  862  * NOTE: can ony handle iovcnt == 1
  863  */
  864 int
  865 nfsm_uiotombuf(uiop, mq, siz, bpos)
  866         register struct uio *uiop;
  867         struct mbuf **mq;
  868         int siz;
  869         caddr_t *bpos;
  870 {
  871         register char *uiocp;
  872         register struct mbuf *mp, *mp2;
  873         register int xfer, left, mlen;
  874         int uiosiz, clflg, rem;
  875         char *cp;
  876 
  877 #ifdef DIAGNOSTIC
  878         if (uiop->uio_iovcnt != 1)
  879                 panic("nfsm_uiotombuf: iovcnt != 1");
  880 #endif
  881 
  882         if (siz > MLEN)         /* or should it >= MCLBYTES ?? */
  883                 clflg = 1;
  884         else
  885                 clflg = 0;
  886         rem = nfsm_rndup(siz)-siz;
  887         mp = mp2 = *mq;
  888         while (siz > 0) {
  889                 left = uiop->uio_iov->iov_len;
  890                 uiocp = uiop->uio_iov->iov_base;
  891                 if (left > siz)
  892                         left = siz;
  893                 uiosiz = left;
  894                 while (left > 0) {
  895                         mlen = M_TRAILINGSPACE(mp);
  896                         if (mlen == 0) {
  897                                 MGET(mp, M_WAIT, MT_DATA);
  898                                 if (clflg)
  899                                         MCLGET(mp, M_WAIT);
  900                                 mp->m_len = 0;
  901                                 mp2->m_next = mp;
  902                                 mp2 = mp;
  903                                 mlen = M_TRAILINGSPACE(mp);
  904                         }
  905                         xfer = (left > mlen) ? mlen : left;
  906 #ifdef notdef
  907                         /* Not Yet.. */
  908                         if (uiop->uio_iov->iov_op != NULL)
  909                                 (*(uiop->uio_iov->iov_op))
  910                                 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
  911                         else
  912 #endif
  913                         if (uiop->uio_segflg == UIO_SYSSPACE)
  914                                 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
  915                         else
  916                                 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
  917                         mp->m_len += xfer;
  918                         left -= xfer;
  919                         uiocp += xfer;
  920                         uiop->uio_offset += xfer;
  921                         uiop->uio_resid -= xfer;
  922                 }
  923                 uiop->uio_iov->iov_base += uiosiz;
  924                 uiop->uio_iov->iov_len -= uiosiz;
  925                 siz -= uiosiz;
  926         }
  927         if (rem > 0) {
  928                 if (rem > M_TRAILINGSPACE(mp)) {
  929                         MGET(mp, M_WAIT, MT_DATA);
  930                         mp->m_len = 0;
  931                         mp2->m_next = mp;
  932                 }
  933                 cp = mtod(mp, caddr_t)+mp->m_len;
  934                 for (left = 0; left < rem; left++)
  935                         *cp++ = '\0';
  936                 mp->m_len += rem;
  937                 *bpos = cp;
  938         } else
  939                 *bpos = mtod(mp, caddr_t)+mp->m_len;
  940         *mq = mp;
  941         return (0);
  942 }
  943 
  944 /*
  945  * Help break down an mbuf chain by setting the first siz bytes contiguous
  946  * pointed to by returned val.
  947  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
  948  * cases. (The macros use the vars. dpos and dpos2)
  949  */
  950 int
  951 nfsm_disct(mdp, dposp, siz, left, cp2)
  952         struct mbuf **mdp;
  953         caddr_t *dposp;
  954         int siz;
  955         int left;
  956         caddr_t *cp2;
  957 {
  958         register struct mbuf *mp, *mp2;
  959         register int siz2, xfer;
  960         register caddr_t p;
  961 
  962         mp = *mdp;
  963         while (left == 0) {
  964                 *mdp = mp = mp->m_next;
  965                 if (mp == NULL)
  966                         return (EBADRPC);
  967                 left = mp->m_len;
  968                 *dposp = mtod(mp, caddr_t);
  969         }
  970         if (left >= siz) {
  971                 *cp2 = *dposp;
  972                 *dposp += siz;
  973         } else if (mp->m_next == NULL) {
  974                 return (EBADRPC);
  975         } else if (siz > MHLEN) {
  976                 panic("nfs S too big");
  977         } else {
  978                 MGET(mp2, M_WAIT, MT_DATA);
  979                 mp2->m_next = mp->m_next;
  980                 mp->m_next = mp2;
  981                 mp->m_len -= left;
  982                 mp = mp2;
  983                 *cp2 = p = mtod(mp, caddr_t);
  984                 bcopy(*dposp, p, left);         /* Copy what was left */
  985                 siz2 = siz-left;
  986                 p += left;
  987                 mp2 = mp->m_next;
  988                 /* Loop around copying up the siz2 bytes */
  989                 while (siz2 > 0) {
  990                         if (mp2 == NULL)
  991                                 return (EBADRPC);
  992                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
  993                         if (xfer > 0) {
  994                                 bcopy(mtod(mp2, caddr_t), p, xfer);
  995                                 NFSMADV(mp2, xfer);
  996                                 mp2->m_len -= xfer;
  997                                 p += xfer;
  998                                 siz2 -= xfer;
  999                         }
 1000                         if (siz2 > 0)
 1001                                 mp2 = mp2->m_next;
 1002                 }
 1003                 mp->m_len = siz;
 1004                 *mdp = mp2;
 1005                 *dposp = mtod(mp2, caddr_t);
 1006         }
 1007         return (0);
 1008 }
 1009 
 1010 /*
 1011  * Advance the position in the mbuf chain.
 1012  */
 1013 int
 1014 nfs_adv(mdp, dposp, offs, left)
 1015         struct mbuf **mdp;
 1016         caddr_t *dposp;
 1017         int offs;
 1018         int left;
 1019 {
 1020         register struct mbuf *m;
 1021         register int s;
 1022 
 1023         m = *mdp;
 1024         s = left;
 1025         while (s < offs) {
 1026                 offs -= s;
 1027                 m = m->m_next;
 1028                 if (m == NULL)
 1029                         return (EBADRPC);
 1030                 s = m->m_len;
 1031         }
 1032         *mdp = m;
 1033         *dposp = mtod(m, caddr_t)+offs;
 1034         return (0);
 1035 }
 1036 
 1037 /*
 1038  * Copy a string into mbufs for the hard cases...
 1039  */
 1040 int
 1041 nfsm_strtmbuf(mb, bpos, cp, siz)
 1042         struct mbuf **mb;
 1043         char **bpos;
 1044         const char *cp;
 1045         long siz;
 1046 {
 1047         register struct mbuf *m1 = NULL, *m2;
 1048         long left, xfer, len, tlen;
 1049         u_int32_t *tl;
 1050         int putsize;
 1051 
 1052         putsize = 1;
 1053         m2 = *mb;
 1054         left = M_TRAILINGSPACE(m2);
 1055         if (left > 0) {
 1056                 tl = ((u_int32_t *)(*bpos));
 1057                 *tl++ = txdr_unsigned(siz);
 1058                 putsize = 0;
 1059                 left -= NFSX_UNSIGNED;
 1060                 m2->m_len += NFSX_UNSIGNED;
 1061                 if (left > 0) {
 1062                         bcopy(cp, (caddr_t) tl, left);
 1063                         siz -= left;
 1064                         cp += left;
 1065                         m2->m_len += left;
 1066                         left = 0;
 1067                 }
 1068         }
 1069         /* Loop around adding mbufs */
 1070         while (siz > 0) {
 1071                 MGET(m1, M_WAIT, MT_DATA);
 1072                 if (siz > MLEN)
 1073                         MCLGET(m1, M_WAIT);
 1074                 m1->m_len = NFSMSIZ(m1);
 1075                 m2->m_next = m1;
 1076                 m2 = m1;
 1077                 tl = mtod(m1, u_int32_t *);
 1078                 tlen = 0;
 1079                 if (putsize) {
 1080                         *tl++ = txdr_unsigned(siz);
 1081                         m1->m_len -= NFSX_UNSIGNED;
 1082                         tlen = NFSX_UNSIGNED;
 1083                         putsize = 0;
 1084                 }
 1085                 if (siz < m1->m_len) {
 1086                         len = nfsm_rndup(siz);
 1087                         xfer = siz;
 1088                         if (xfer < len)
 1089                                 *(tl+(xfer>>2)) = 0;
 1090                 } else {
 1091                         xfer = len = m1->m_len;
 1092                 }
 1093                 bcopy(cp, (caddr_t) tl, xfer);
 1094                 m1->m_len = len+tlen;
 1095                 siz -= xfer;
 1096                 cp += xfer;
 1097         }
 1098         *mb = m1;
 1099         *bpos = mtod(m1, caddr_t)+m1->m_len;
 1100         return (0);
 1101 }
 1102 
 1103 /*
 1104  * Called once to initialize data structures...
 1105  */
 1106 int
 1107 nfs_init(vfsp)
 1108         struct vfsconf *vfsp;
 1109 {
 1110         register int i;
 1111 
 1112         nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
 1113 
 1114         /*
 1115          * Check to see if major data structures haven't bloated.
 1116          */
 1117         if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
 1118                 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
 1119                 printf("Try reducing NFS_UIDHASHSIZ\n");
 1120         }
 1121         if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
 1122                 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
 1123                 printf("Try unionizing the nu_nickname and nu_flag fields\n");
 1124         }
 1125         nfs_mount_type = vfsp->vfc_typenum;
 1126         nfsrtt.pos = 0;
 1127         rpc_vers = txdr_unsigned(RPC_VER2);
 1128         rpc_call = txdr_unsigned(RPC_CALL);
 1129         rpc_reply = txdr_unsigned(RPC_REPLY);
 1130         rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
 1131         rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
 1132         rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
 1133         rpc_autherr = txdr_unsigned(RPC_AUTHERR);
 1134         rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
 1135         rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
 1136         nfs_prog = txdr_unsigned(NFS_PROG);
 1137         nqnfs_prog = txdr_unsigned(NQNFS_PROG);
 1138         nfs_true = txdr_unsigned(TRUE);
 1139         nfs_false = txdr_unsigned(FALSE);
 1140         nfs_xdrneg1 = txdr_unsigned(-1);
 1141         nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
 1142         if (nfs_ticks < 1)
 1143                 nfs_ticks = 1;
 1144         /* Ensure async daemons disabled */
 1145         for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
 1146                 nfs_iodwant[i] = (struct proc *)0;
 1147                 nfs_iodmount[i] = (struct nfsmount *)0;
 1148         }
 1149         nfs_nhinit();                   /* Init the nfsnode table */
 1150 #ifndef NFS_NOSERVER
 1151         nfsrv_init(0);                  /* Init server data structures */
 1152         nfsrv_initcache();              /* Init the server request cache */
 1153 #endif
 1154 
 1155         /*
 1156          * Initialize the nqnfs server stuff.
 1157          */
 1158         if (nqnfsstarttime == 0) {
 1159                 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
 1160                         + nqsrv_clockskew + nqsrv_writeslack;
 1161                 NQLOADNOVRAM(nqnfsstarttime);
 1162                 CIRCLEQ_INIT(&nqtimerhead);
 1163                 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
 1164         }
 1165 
 1166         /*
 1167          * Initialize reply list and start timer
 1168          */
 1169         TAILQ_INIT(&nfs_reqq);
 1170 
 1171         nfs_timer(0);
 1172 
 1173         /*
 1174          * Set up lease_check and lease_updatetime so that other parts
 1175          * of the system can call us, if we are loadable.
 1176          */
 1177 #ifndef NFS_NOSERVER
 1178         nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
 1179         default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
 1180 #endif
 1181         nfs_prev_lease_updatetime = lease_updatetime;
 1182         lease_updatetime = nfs_lease_updatetime;
 1183         nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
 1184         sysent[SYS_nfssvc].sy_narg = 2;
 1185         nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
 1186         sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
 1187 #ifndef NFS_NOSERVER
 1188         nfs_prev_getfh_sy_narg = sysent[SYS_getfh].sy_narg;
 1189         sysent[SYS_getfh].sy_narg = 2;
 1190         nfs_prev_getfh_sy_call = sysent[SYS_getfh].sy_call;
 1191         sysent[SYS_getfh].sy_call = (sy_call_t *)getfh;
 1192 #endif
 1193 
 1194         return (0);
 1195 }
 1196 
 1197 int
 1198 nfs_uninit(vfsp)
 1199         struct vfsconf *vfsp;
 1200 {
 1201 
 1202         untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
 1203         nfs_mount_type = -1;
 1204 #ifndef NFS_NOSERVER
 1205         default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
 1206 #endif
 1207         lease_updatetime = nfs_prev_lease_updatetime;
 1208         sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
 1209         sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
 1210 #ifndef NFS_NOSERVER
 1211         sysent[SYS_getfh].sy_narg = nfs_prev_getfh_sy_narg;
 1212         sysent[SYS_getfh].sy_call = nfs_prev_getfh_sy_call;
 1213 #endif
 1214         return (0);
 1215 }
 1216 
 1217 /*
 1218  * Attribute cache routines.
 1219  * nfs_loadattrcache() - loads or updates the cache contents from attributes
 1220  *      that are on the mbuf list
 1221  * nfs_getattrcache() - returns valid attributes if found in cache, returns
 1222  *      error otherwise
 1223  */
 1224 
 1225 /*
 1226  * Load the attribute cache (that lives in the nfsnode entry) with
 1227  * the values on the mbuf list and
 1228  * Iff vap not NULL
 1229  *    copy the attributes to *vaper
 1230  */
 1231 int
 1232 nfs_loadattrcache(vpp, mdp, dposp, vaper)
 1233         struct vnode **vpp;
 1234         struct mbuf **mdp;
 1235         caddr_t *dposp;
 1236         struct vattr *vaper;
 1237 {
 1238         register struct vnode *vp = *vpp;
 1239         register struct vattr *vap;
 1240         register struct nfs_fattr *fp;
 1241         register struct nfsnode *np;
 1242         register int32_t t1;
 1243         caddr_t cp2;
 1244         int error = 0, rdev;
 1245         struct mbuf *md;
 1246         enum vtype vtyp;
 1247         u_short vmode;
 1248         struct timespec mtime;
 1249         struct vnode *nvp;
 1250         int v3 = NFS_ISV3(vp);
 1251 
 1252         md = *mdp;
 1253         t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
 1254         if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
 1255                 return (error);
 1256         fp = (struct nfs_fattr *)cp2;
 1257         if (v3) {
 1258                 vtyp = nfsv3tov_type(fp->fa_type);
 1259                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
 1260                 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
 1261                         fxdr_unsigned(int, fp->fa3_rdev.specdata2));
 1262                 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
 1263         } else {
 1264                 vtyp = nfsv2tov_type(fp->fa_type);
 1265                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
 1266                 /*
 1267                  * XXX
 1268                  *
 1269                  * The duplicate information returned in fa_type and fa_mode
 1270                  * is an ambiguity in the NFS version 2 protocol.
 1271                  *
 1272                  * VREG should be taken literally as a regular file.  If a
 1273                  * server intents to return some type information differently
 1274                  * in the upper bits of the mode field (e.g. for sockets, or
 1275                  * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
 1276                  * leave the examination of the mode bits even in the VREG
 1277                  * case to avoid breakage for bogus servers, but we make sure
 1278                  * that there are actually type bits set in the upper part of
 1279                  * fa_mode (and failing that, trust the va_type field).
 1280                  *
 1281                  * NFSv3 cleared the issue, and requires fa_mode to not
 1282                  * contain any type information (while also introduing sockets
 1283                  * and FIFOs for fa_type).
 1284                  */
 1285                 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
 1286                         vtyp = IFTOVT(vmode);
 1287                 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
 1288                 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
 1289 
 1290                 /*
 1291                  * Really ugly NFSv2 kludge.
 1292                  */
 1293                 if (vtyp == VCHR && rdev == 0xffffffff)
 1294                         vtyp = VFIFO;
 1295         }
 1296 
 1297         /*
 1298          * If v_type == VNON it is a new node, so fill in the v_type,
 1299          * n_mtime fields. Check to see if it represents a special
 1300          * device, and if so, check for a possible alias. Once the
 1301          * correct vnode has been obtained, fill in the rest of the
 1302          * information.
 1303          */
 1304         np = VTONFS(vp);
 1305         if (vp->v_type != vtyp) {
 1306                 vp->v_type = vtyp;
 1307                 if (vp->v_type == VFIFO) {
 1308                         vp->v_op = fifo_nfsv2nodeop_p;
 1309                 }
 1310                 if (vp->v_type == VCHR || vp->v_type == VBLK) {
 1311                         vp->v_op = spec_nfsv2nodeop_p;
 1312                         nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
 1313                         if (nvp) {
 1314                                 /*
 1315                                  * Discard unneeded vnode, but save its nfsnode.
 1316                                  * Since the nfsnode does not have a lock, its
 1317                                  * vnode lock has to be carried over.
 1318                                  */
 1319                                 nvp->v_vnlock = vp->v_vnlock;
 1320                                 vp->v_vnlock = NULL;
 1321                                 nvp->v_data = vp->v_data;
 1322                                 vp->v_data = NULL;
 1323                                 vp->v_op = spec_vnodeop_p;
 1324                                 vrele(vp);
 1325                                 vgone(vp);
 1326                                 /*
 1327                                  * Reinitialize aliased node.
 1328                                  */
 1329                                 np->n_vnode = nvp;
 1330                                 *vpp = vp = nvp;
 1331                         }
 1332                 }
 1333                 np->n_mtime = mtime.tv_sec;
 1334         }
 1335         vap = &np->n_vattr;
 1336         vap->va_type = vtyp;
 1337         vap->va_mode = (vmode & 07777);
 1338         vap->va_rdev = (dev_t)rdev;
 1339         vap->va_mtime = mtime;
 1340         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
 1341         if (v3) {
 1342                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
 1343                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
 1344                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
 1345                 fxdr_hyper(&fp->fa3_size, &vap->va_size);
 1346                 vap->va_blocksize = NFS_FABLKSIZE;
 1347                 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
 1348                 vap->va_fileid = fxdr_unsigned(int32_t,
 1349                     fp->fa3_fileid.nfsuquad[1]);
 1350                 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
 1351                 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
 1352                 vap->va_flags = 0;
 1353                 vap->va_filerev = 0;
 1354         } else {
 1355                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
 1356                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
 1357                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
 1358                 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
 1359                 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
 1360                 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
 1361                     * NFS_FABLKSIZE;
 1362                 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
 1363                 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
 1364                 vap->va_flags = 0;
 1365                 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
 1366                     fp->fa2_ctime.nfsv2_sec);
 1367                 vap->va_ctime.tv_nsec = 0;
 1368                 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
 1369                 vap->va_filerev = 0;
 1370         }
 1371         if (vap->va_size != np->n_size) {
 1372                 if (vap->va_type == VREG) {
 1373                         if (np->n_flag & NMODIFIED) {
 1374                                 if (vap->va_size < np->n_size)
 1375                                         vap->va_size = np->n_size;
 1376                                 else
 1377                                         np->n_size = vap->va_size;
 1378                         } else
 1379                                 np->n_size = vap->va_size;
 1380                         vnode_pager_setsize(vp, np->n_size);
 1381                 } else
 1382                         np->n_size = vap->va_size;
 1383         }
 1384         np->n_attrstamp = time_second;
 1385         if (vaper != NULL) {
 1386                 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
 1387                 if (np->n_flag & NCHG) {
 1388                         if (np->n_flag & NACC)
 1389                                 vaper->va_atime = np->n_atim;
 1390                         if (np->n_flag & NUPD)
 1391                                 vaper->va_mtime = np->n_mtim;
 1392                 }
 1393         }
 1394         return (0);
 1395 }
 1396 
 1397 #ifdef NFS_ACDEBUG
 1398 #include <sys/sysctl.h>
 1399 static int nfs_acdebug;
 1400 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
 1401 #endif
 1402 
 1403 /*
 1404  * Check the time stamp
 1405  * If the cache is valid, copy contents to *vap and return 0
 1406  * otherwise return an error
 1407  */
 1408 int
 1409 nfs_getattrcache(vp, vaper)
 1410         register struct vnode *vp;
 1411         struct vattr *vaper;
 1412 {
 1413         register struct nfsnode *np;
 1414         register struct vattr *vap;
 1415         struct nfsmount *nmp;
 1416         int timeo;
 1417 
 1418         np = VTONFS(vp);
 1419         vap = &np->n_vattr;
 1420         nmp = VFSTONFS(vp->v_mount);
 1421         /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
 1422         timeo = (time_second - np->n_mtime) / 10;
 1423 
 1424 #ifdef NFS_ACDEBUG
 1425         if (nfs_acdebug>1)
 1426                 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
 1427 #endif
 1428 
 1429         if (vap->va_type == VDIR) {
 1430                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
 1431                         timeo = nmp->nm_acdirmin;
 1432                 else if (timeo > nmp->nm_acdirmax)
 1433                         timeo = nmp->nm_acdirmax;
 1434         } else {
 1435                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
 1436                         timeo = nmp->nm_acregmin;
 1437                 else if (timeo > nmp->nm_acregmax)
 1438                         timeo = nmp->nm_acregmax;
 1439         }
 1440 
 1441 #ifdef NFS_ACDEBUG
 1442         if (nfs_acdebug > 2)
 1443                 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
 1444                         nmp->nm_acregmin, nmp->nm_acregmax,
 1445                         nmp->nm_acdirmin, nmp->nm_acdirmax);
 1446 
 1447         if (nfs_acdebug)
 1448                 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
 1449                         (time_second - np->n_attrstamp), timeo);
 1450 #endif
 1451 
 1452         if ((time_second - np->n_attrstamp) >= timeo) {
 1453                 nfsstats.attrcache_misses++;
 1454                 return (ENOENT);
 1455         }
 1456         nfsstats.attrcache_hits++;
 1457         if (vap->va_size != np->n_size) {
 1458                 if (vap->va_type == VREG) {
 1459                         if (np->n_flag & NMODIFIED) {
 1460                                 if (vap->va_size < np->n_size)
 1461                                         vap->va_size = np->n_size;
 1462                                 else
 1463                                         np->n_size = vap->va_size;
 1464                         } else
 1465                                 np->n_size = vap->va_size;
 1466                         vnode_pager_setsize(vp, np->n_size);
 1467                 } else
 1468                         np->n_size = vap->va_size;
 1469         }
 1470         bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
 1471         if (np->n_flag & NCHG) {
 1472                 if (np->n_flag & NACC)
 1473                         vaper->va_atime = np->n_atim;
 1474                 if (np->n_flag & NUPD)
 1475                         vaper->va_mtime = np->n_mtim;
 1476         }
 1477         return (0);
 1478 }
 1479 
 1480 #ifndef NFS_NOSERVER
 1481 /*
 1482  * Set up nameidata for a lookup() call and do it.
 1483  *
 1484  * If pubflag is set, this call is done for a lookup operation on the
 1485  * public filehandle. In that case we allow crossing mountpoints and
 1486  * absolute pathnames. However, the caller is expected to check that
 1487  * the lookup result is within the public fs, and deny access if
 1488  * it is not.
 1489  *
 1490  * nfs_namei() clears out garbage fields that namei() might leave garbage.
 1491  * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
 1492  * error occurs but the parent was not requested.
 1493  *
 1494  * dirp may be set whether an error is returned or not, and must be 
 1495  * released by the caller.
 1496  */
 1497 int
 1498 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
 1499         register struct nameidata *ndp;
 1500         fhandle_t *fhp;
 1501         int len;
 1502         struct nfssvc_sock *slp;
 1503         struct sockaddr *nam;
 1504         struct mbuf **mdp;
 1505         caddr_t *dposp;
 1506         struct vnode **retdirp;
 1507         struct proc *p;
 1508         int kerbflag, pubflag;
 1509 {
 1510         register int i, rem;
 1511         register struct mbuf *md;
 1512         register char *fromcp, *tocp, *cp;
 1513         struct iovec aiov;
 1514         struct uio auio;
 1515         struct vnode *dp;
 1516         int error, rdonly, linklen;
 1517         struct componentname *cnp = &ndp->ni_cnd;
 1518 
 1519         *retdirp = (struct vnode *)0;
 1520         cnp->cn_pnbuf = zalloc(namei_zone);
 1521 
 1522         /*
 1523          * Copy the name from the mbuf list to ndp->ni_pnbuf
 1524          * and set the various ndp fields appropriately.
 1525          */
 1526         fromcp = *dposp;
 1527         tocp = cnp->cn_pnbuf;
 1528         md = *mdp;
 1529         rem = mtod(md, caddr_t) + md->m_len - fromcp;
 1530         cnp->cn_hash = 0;
 1531         for (i = 0; i < len; i++) {
 1532                 while (rem == 0) {
 1533                         md = md->m_next;
 1534                         if (md == NULL) {
 1535                                 error = EBADRPC;
 1536                                 goto out;
 1537                         }
 1538                         fromcp = mtod(md, caddr_t);
 1539                         rem = md->m_len;
 1540                 }
 1541                 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
 1542                         error = EACCES;
 1543                         goto out;
 1544                 }
 1545                 cnp->cn_hash += (unsigned char)*fromcp;
 1546                 *tocp++ = *fromcp++;
 1547                 rem--;
 1548         }
 1549         *tocp = '\0';
 1550         *mdp = md;
 1551         *dposp = fromcp;
 1552         len = nfsm_rndup(len)-len;
 1553         if (len > 0) {
 1554                 if (rem >= len)
 1555                         *dposp += len;
 1556                 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
 1557                         goto out;
 1558         }
 1559 
 1560         /*
 1561          * Extract and set starting directory.
 1562          */
 1563         error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
 1564             nam, &rdonly, kerbflag, pubflag);
 1565         if (error)
 1566                 goto out;
 1567         if (dp->v_type != VDIR) {
 1568                 vrele(dp);
 1569                 error = ENOTDIR;
 1570                 goto out;
 1571         }
 1572 
 1573         if (rdonly)
 1574                 cnp->cn_flags |= RDONLY;
 1575 
 1576         /*
 1577          * Set return directory.  Reference to dp is implicitly transfered 
 1578          * to the returned pointer
 1579          */
 1580         *retdirp = dp;
 1581 
 1582         if (pubflag) {
 1583                 /*
 1584                  * Oh joy. For WebNFS, handle those pesky '%' escapes,
 1585                  * and the 'native path' indicator.
 1586                  */
 1587                 cp = zalloc(namei_zone);
 1588                 fromcp = cnp->cn_pnbuf;
 1589                 tocp = cp;
 1590                 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
 1591                         switch ((unsigned char)*fromcp) {
 1592                         case WEBNFS_NATIVE_CHAR:
 1593                                 /*
 1594                                  * 'Native' path for us is the same
 1595                                  * as a path according to the NFS spec,
 1596                                  * just skip the escape char.
 1597                                  */
 1598                                 fromcp++;
 1599                                 break;
 1600                         /*
 1601                          * More may be added in the future, range 0x80-0xff
 1602                          */
 1603                         default:
 1604                                 error = EIO;
 1605                                 zfree(namei_zone, cp);
 1606                                 goto out;
 1607                         }
 1608                 }
 1609                 /*
 1610                  * Translate the '%' escapes, URL-style.
 1611                  */
 1612                 while (*fromcp != '\0') {
 1613                         if (*fromcp == WEBNFS_ESC_CHAR) {
 1614                                 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
 1615                                         fromcp++;
 1616                                         *tocp++ = HEXSTRTOI(fromcp);
 1617                                         fromcp += 2;
 1618                                         continue;
 1619                                 } else {
 1620                                         error = ENOENT;
 1621                                         zfree(namei_zone, cp);
 1622                                         goto out;
 1623                                 }
 1624                         } else
 1625                                 *tocp++ = *fromcp++;
 1626                 }
 1627                 *tocp = '\0';
 1628                 zfree(namei_zone, cnp->cn_pnbuf);
 1629                 cnp->cn_pnbuf = cp;
 1630         }
 1631 
 1632         ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
 1633         ndp->ni_segflg = UIO_SYSSPACE;
 1634 
 1635         if (pubflag) {
 1636                 ndp->ni_rootdir = rootvnode;
 1637                 ndp->ni_loopcnt = 0;
 1638                 if (cnp->cn_pnbuf[0] == '/')
 1639                         dp = rootvnode;
 1640         } else {
 1641                 cnp->cn_flags |= NOCROSSMOUNT;
 1642         }
 1643 
 1644         /*
 1645          * Initialize for scan, set ni_startdir and bump ref on dp again
 1646          * becuase lookup() will dereference ni_startdir.
 1647          */
 1648 
 1649         cnp->cn_proc = p;
 1650         VREF(dp);
 1651         ndp->ni_startdir = dp;
 1652 
 1653         for (;;) {
 1654                 cnp->cn_nameptr = cnp->cn_pnbuf;
 1655                 /*
 1656                  * Call lookup() to do the real work.  If an error occurs,
 1657                  * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
 1658                  * we do not have to dereference anything before returning.
 1659                  * In either case ni_startdir will be dereferenced and NULLed
 1660                  * out.
 1661                  */
 1662                 error = lookup(ndp);
 1663                 if (error)
 1664                         break;
 1665 
 1666                 /*
 1667                  * Check for encountering a symbolic link.  Trivial 
 1668                  * termination occurs if no symlink encountered.
 1669                  * Note: zfree is safe because error is 0, so we will
 1670                  * not zfree it again when we break.
 1671                  */
 1672                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
 1673                         nfsrv_object_create(ndp->ni_vp);
 1674                         if (cnp->cn_flags & (SAVENAME | SAVESTART))
 1675                                 cnp->cn_flags |= HASBUF;
 1676                         else
 1677                                 zfree(namei_zone, cnp->cn_pnbuf);
 1678                         break;
 1679                 }
 1680 
 1681                 /*
 1682                  * Validate symlink
 1683                  */
 1684                 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
 1685                         VOP_UNLOCK(ndp->ni_dvp, 0, p);
 1686                 if (!pubflag) {
 1687                         error = EINVAL;
 1688                         goto badlink2;
 1689                 }
 1690 
 1691                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
 1692                         error = ELOOP;
 1693                         goto badlink2;
 1694                 }
 1695                 if (ndp->ni_pathlen > 1)
 1696                         cp = zalloc(namei_zone);
 1697                 else
 1698                         cp = cnp->cn_pnbuf;
 1699                 aiov.iov_base = cp;
 1700                 aiov.iov_len = MAXPATHLEN;
 1701                 auio.uio_iov = &aiov;
 1702                 auio.uio_iovcnt = 1;
 1703                 auio.uio_offset = 0;
 1704                 auio.uio_rw = UIO_READ;
 1705                 auio.uio_segflg = UIO_SYSSPACE;
 1706                 auio.uio_procp = (struct proc *)0;
 1707                 auio.uio_resid = MAXPATHLEN;
 1708                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
 1709                 if (error) {
 1710                 badlink1:
 1711                         if (ndp->ni_pathlen > 1)
 1712                                 zfree(namei_zone, cp);
 1713                 badlink2:
 1714                         vrele(ndp->ni_dvp);
 1715                         vput(ndp->ni_vp);
 1716                         break;
 1717                 }
 1718                 linklen = MAXPATHLEN - auio.uio_resid;
 1719                 if (linklen == 0) {
 1720                         error = ENOENT;
 1721                         goto badlink1;
 1722                 }
 1723                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
 1724                         error = ENAMETOOLONG;
 1725                         goto badlink1;
 1726                 }
 1727 
 1728                 /*
 1729                  * Adjust or replace path
 1730                  */
 1731                 if (ndp->ni_pathlen > 1) {
 1732                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
 1733                         zfree(namei_zone, cnp->cn_pnbuf);
 1734                         cnp->cn_pnbuf = cp;
 1735                 } else
 1736                         cnp->cn_pnbuf[linklen] = '\0';
 1737                 ndp->ni_pathlen += linklen;
 1738 
 1739                 /*
 1740                  * Cleanup refs for next loop and check if root directory 
 1741                  * should replace current directory.  Normally ni_dvp 
 1742                  * becomes the new base directory and is cleaned up when
 1743                  * we loop.  Explicitly null pointers after invalidation
 1744                  * to clarify operation.
 1745                  */
 1746                 vput(ndp->ni_vp);
 1747                 ndp->ni_vp = NULL;
 1748 
 1749                 if (cnp->cn_pnbuf[0] == '/') {
 1750                         vrele(ndp->ni_dvp);
 1751                         ndp->ni_dvp = ndp->ni_rootdir;
 1752                         VREF(ndp->ni_dvp);
 1753                 }
 1754                 ndp->ni_startdir = ndp->ni_dvp;
 1755                 ndp->ni_dvp = NULL;
 1756         }
 1757 
 1758         /*
 1759          * nfs_namei() guarentees that fields will not contain garbage
 1760          * whether an error occurs or not.  This allows the caller to track
 1761          * cleanup state trivially.
 1762          */
 1763 out:
 1764         if (error) {
 1765                 zfree(namei_zone, cnp->cn_pnbuf);
 1766                 ndp->ni_vp = NULL;
 1767                 ndp->ni_dvp = NULL;
 1768                 ndp->ni_startdir = NULL;
 1769                 cnp->cn_flags &= ~HASBUF;
 1770         } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
 1771                 ndp->ni_dvp = NULL;
 1772         }
 1773         return (error);
 1774 }
 1775 
 1776 /*
 1777  * A fiddled version of m_adj() that ensures null fill to a long
 1778  * boundary and only trims off the back end
 1779  */
 1780 void
 1781 nfsm_adj(mp, len, nul)
 1782         struct mbuf *mp;
 1783         register int len;
 1784         int nul;
 1785 {
 1786         register struct mbuf *m;
 1787         register int count, i;
 1788         register char *cp;
 1789 
 1790         /*
 1791          * Trim from tail.  Scan the mbuf chain,
 1792          * calculating its length and finding the last mbuf.
 1793          * If the adjustment only affects this mbuf, then just
 1794          * adjust and return.  Otherwise, rescan and truncate
 1795          * after the remaining size.
 1796          */
 1797         count = 0;
 1798         m = mp;
 1799         for (;;) {
 1800                 count += m->m_len;
 1801                 if (m->m_next == (struct mbuf *)0)
 1802                         break;
 1803                 m = m->m_next;
 1804         }
 1805         if (m->m_len > len) {
 1806                 m->m_len -= len;
 1807                 if (nul > 0) {
 1808                         cp = mtod(m, caddr_t)+m->m_len-nul;
 1809                         for (i = 0; i < nul; i++)
 1810                                 *cp++ = '\0';
 1811                 }
 1812                 return;
 1813         }
 1814         count -= len;
 1815         if (count < 0)
 1816                 count = 0;
 1817         /*
 1818          * Correct length for chain is "count".
 1819          * Find the mbuf with last data, adjust its length,
 1820          * and toss data from remaining mbufs on chain.
 1821          */
 1822         for (m = mp; m; m = m->m_next) {
 1823                 if (m->m_len >= count) {
 1824                         m->m_len = count;
 1825                         if (nul > 0) {
 1826                                 cp = mtod(m, caddr_t)+m->m_len-nul;
 1827                                 for (i = 0; i < nul; i++)
 1828                                         *cp++ = '\0';
 1829                         }
 1830                         break;
 1831                 }
 1832                 count -= m->m_len;
 1833         }
 1834         for (m = m->m_next;m;m = m->m_next)
 1835                 m->m_len = 0;
 1836 }
 1837 
 1838 /*
 1839  * Make these functions instead of macros, so that the kernel text size
 1840  * doesn't get too big...
 1841  */
 1842 void
 1843 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
 1844         struct nfsrv_descript *nfsd;
 1845         int before_ret;
 1846         register struct vattr *before_vap;
 1847         int after_ret;
 1848         struct vattr *after_vap;
 1849         struct mbuf **mbp;
 1850         char **bposp;
 1851 {
 1852         register struct mbuf *mb = *mbp, *mb2;
 1853         register char *bpos = *bposp;
 1854         register u_int32_t *tl;
 1855 
 1856         if (before_ret) {
 1857                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
 1858                 *tl = nfs_false;
 1859         } else {
 1860                 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
 1861                 *tl++ = nfs_true;
 1862                 txdr_hyper(&(before_vap->va_size), tl);
 1863                 tl += 2;
 1864                 txdr_nfsv3time(&(before_vap->va_mtime), tl);
 1865                 tl += 2;
 1866                 txdr_nfsv3time(&(before_vap->va_ctime), tl);
 1867         }
 1868         *bposp = bpos;
 1869         *mbp = mb;
 1870         nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
 1871 }
 1872 
 1873 void
 1874 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
 1875         struct nfsrv_descript *nfsd;
 1876         int after_ret;
 1877         struct vattr *after_vap;
 1878         struct mbuf **mbp;
 1879         char **bposp;
 1880 {
 1881         register struct mbuf *mb = *mbp, *mb2;
 1882         register char *bpos = *bposp;
 1883         register u_int32_t *tl;
 1884         register struct nfs_fattr *fp;
 1885 
 1886         if (after_ret) {
 1887                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
 1888                 *tl = nfs_false;
 1889         } else {
 1890                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
 1891                 *tl++ = nfs_true;
 1892                 fp = (struct nfs_fattr *)tl;
 1893                 nfsm_srvfattr(nfsd, after_vap, fp);
 1894         }
 1895         *mbp = mb;
 1896         *bposp = bpos;
 1897 }
 1898 
 1899 void
 1900 nfsm_srvfattr(nfsd, vap, fp)
 1901         register struct nfsrv_descript *nfsd;
 1902         register struct vattr *vap;
 1903         register struct nfs_fattr *fp;
 1904 {
 1905 
 1906         fp->fa_nlink = txdr_unsigned(vap->va_nlink);
 1907         fp->fa_uid = txdr_unsigned(vap->va_uid);
 1908         fp->fa_gid = txdr_unsigned(vap->va_gid);
 1909         if (nfsd->nd_flag & ND_NFSV3) {
 1910                 fp->fa_type = vtonfsv3_type(vap->va_type);
 1911                 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
 1912                 txdr_hyper(&vap->va_size, &fp->fa3_size);
 1913                 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
 1914                 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
 1915                 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
 1916                 fp->fa3_fsid.nfsuquad[0] = 0;
 1917                 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
 1918                 fp->fa3_fileid.nfsuquad[0] = 0;
 1919                 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
 1920                 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
 1921                 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
 1922                 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
 1923         } else {
 1924                 fp->fa_type = vtonfsv2_type(vap->va_type);
 1925                 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
 1926                 fp->fa2_size = txdr_unsigned(vap->va_size);
 1927                 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
 1928                 if (vap->va_type == VFIFO)
 1929                         fp->fa2_rdev = 0xffffffff;
 1930                 else
 1931                         fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
 1932                 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
 1933                 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
 1934                 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
 1935                 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
 1936                 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
 1937                 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
 1938         }
 1939 }
 1940 
 1941 /*
 1942  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
 1943  *      - look up fsid in mount list (if not found ret error)
 1944  *      - get vp and export rights by calling VFS_FHTOVP()
 1945  *      - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
 1946  *      - if not lockflag unlock it with VOP_UNLOCK()
 1947  */
 1948 int
 1949 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
 1950         fhandle_t *fhp;
 1951         int lockflag;
 1952         struct vnode **vpp;
 1953         struct ucred *cred;
 1954         struct nfssvc_sock *slp;
 1955         struct sockaddr *nam;
 1956         int *rdonlyp;
 1957         int kerbflag;
 1958         int pubflag;
 1959 {
 1960         struct proc *p = curproc; /* XXX */
 1961         register struct mount *mp;
 1962         register int i;
 1963         struct ucred *credanon;
 1964         int error, exflags;
 1965 #ifdef MNT_EXNORESPORT          /* XXX needs mountd and /etc/exports help yet */
 1966         struct sockaddr_int *saddr;
 1967 #endif
 1968 
 1969         *vpp = (struct vnode *)0;
 1970 
 1971         if (nfs_ispublicfh(fhp)) {
 1972                 if (!pubflag || !nfs_pub.np_valid)
 1973                         return (ESTALE);
 1974                 fhp = &nfs_pub.np_handle;
 1975         }
 1976 
 1977         mp = vfs_getvfs(&fhp->fh_fsid);
 1978         if (!mp)
 1979                 return (ESTALE);
 1980         error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
 1981         if (error)
 1982                 return (error);
 1983 #ifdef MNT_EXNORESPORT
 1984         if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
 1985                 saddr = (struct sockaddr_in *)nam;
 1986                 if (saddr->sin_family == AF_INET &&
 1987                     ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
 1988                         vput(*vpp);
 1989                         *vpp = NULL;
 1990                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
 1991                 }
 1992         }
 1993 #endif
 1994         /*
 1995          * Check/setup credentials.
 1996          */
 1997         if (exflags & MNT_EXKERB) {
 1998                 if (!kerbflag) {
 1999                         vput(*vpp);
 2000                         *vpp = NULL;
 2001                         return (NFSERR_AUTHERR | AUTH_TOOWEAK);
 2002                 }
 2003         } else if (kerbflag) {
 2004                 vput(*vpp);
 2005                 *vpp = NULL;
 2006                 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
 2007         } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
 2008                 cred->cr_uid = credanon->cr_uid;
 2009                 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
 2010                         cred->cr_groups[i] = credanon->cr_groups[i];
 2011                 cred->cr_ngroups = i;
 2012         }
 2013         if (exflags & MNT_EXRDONLY)
 2014                 *rdonlyp = 1;
 2015         else
 2016                 *rdonlyp = 0;
 2017 
 2018         nfsrv_object_create(*vpp);
 2019 
 2020         if (!lockflag)
 2021                 VOP_UNLOCK(*vpp, 0, p);
 2022         return (0);
 2023 }
 2024 
 2025 
 2026 /*
 2027  * WebNFS: check if a filehandle is a public filehandle. For v3, this
 2028  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
 2029  * transformed this to all zeroes in both cases, so check for it.
 2030  */
 2031 int
 2032 nfs_ispublicfh(fhp)
 2033         fhandle_t *fhp;
 2034 {
 2035         char *cp = (char *)fhp;
 2036         int i;
 2037 
 2038         for (i = 0; i < NFSX_V3FH; i++)
 2039                 if (*cp++ != 0)
 2040                         return (FALSE);
 2041         return (TRUE);
 2042 }
 2043   
 2044 #endif /* NFS_NOSERVER */
 2045 /*
 2046  * This function compares two net addresses by family and returns TRUE
 2047  * if they are the same host.
 2048  * If there is any doubt, return FALSE.
 2049  * The AF_INET family is handled as a special case so that address mbufs
 2050  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
 2051  */
 2052 int
 2053 netaddr_match(family, haddr, nam)
 2054         int family;
 2055         union nethostaddr *haddr;
 2056         struct sockaddr *nam;
 2057 {
 2058         register struct sockaddr_in *inetaddr;
 2059 
 2060         switch (family) {
 2061         case AF_INET:
 2062                 inetaddr = (struct sockaddr_in *)nam;
 2063                 if (inetaddr->sin_family == AF_INET &&
 2064                     inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
 2065                         return (1);
 2066                 break;
 2067 #ifdef ISO
 2068         case AF_ISO:
 2069             {
 2070                 register struct sockaddr_iso *isoaddr1, *isoaddr2;
 2071 
 2072                 isoaddr1 = (struct sockaddr_iso *)nam;
 2073                 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
 2074                 if (isoaddr1->siso_family == AF_ISO &&
 2075                     isoaddr1->siso_nlen > 0 &&
 2076                     isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
 2077                     SAME_ISOADDR(isoaddr1, isoaddr2))
 2078                         return (1);
 2079                 break;
 2080             }
 2081 #endif  /* ISO */
 2082         default:
 2083                 break;
 2084         };
 2085         return (0);
 2086 }
 2087 
 2088 static nfsuint64 nfs_nullcookie = { 0, 0 };
 2089 /*
 2090  * This function finds the directory cookie that corresponds to the
 2091  * logical byte offset given.
 2092  */
 2093 nfsuint64 *
 2094 nfs_getcookie(np, off, add)
 2095         register struct nfsnode *np;
 2096         off_t off;
 2097         int add;
 2098 {
 2099         register struct nfsdmap *dp, *dp2;
 2100         register int pos;
 2101 
 2102         pos = (uoff_t)off / NFS_DIRBLKSIZ;
 2103         if (pos == 0 || off < 0) {
 2104 #ifdef DIAGNOSTIC
 2105                 if (add)
 2106                         panic("nfs getcookie add at <= 0");
 2107 #endif
 2108                 return (&nfs_nullcookie);
 2109         }
 2110         pos--;
 2111         dp = np->n_cookies.lh_first;
 2112         if (!dp) {
 2113                 if (add) {
 2114                         MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
 2115                                 M_NFSDIROFF, M_WAITOK);
 2116                         dp->ndm_eocookie = 0;
 2117                         LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
 2118                 } else
 2119                         return ((nfsuint64 *)0);
 2120         }
 2121         while (pos >= NFSNUMCOOKIES) {
 2122                 pos -= NFSNUMCOOKIES;
 2123                 if (dp->ndm_list.le_next) {
 2124                         if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
 2125                                 pos >= dp->ndm_eocookie)
 2126                                 return ((nfsuint64 *)0);
 2127                         dp = dp->ndm_list.le_next;
 2128                 } else if (add) {
 2129                         MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
 2130                                 M_NFSDIROFF, M_WAITOK);
 2131                         dp2->ndm_eocookie = 0;
 2132                         LIST_INSERT_AFTER(dp, dp2, ndm_list);
 2133                         dp = dp2;
 2134                 } else
 2135                         return ((nfsuint64 *)0);
 2136         }
 2137         if (pos >= dp->ndm_eocookie) {
 2138                 if (add)
 2139                         dp->ndm_eocookie = pos + 1;
 2140                 else
 2141                         return ((nfsuint64 *)0);
 2142         }
 2143         return (&dp->ndm_cookies[pos]);
 2144 }
 2145 
 2146 /*
 2147  * Invalidate cached directory information, except for the actual directory
 2148  * blocks (which are invalidated separately).
 2149  * Done mainly to avoid the use of stale offset cookies.
 2150  */
 2151 void
 2152 nfs_invaldir(vp)
 2153         register struct vnode *vp;
 2154 {
 2155         register struct nfsnode *np = VTONFS(vp);
 2156 
 2157 #ifdef DIAGNOSTIC
 2158         if (vp->v_type != VDIR)
 2159                 panic("nfs: invaldir not dir");
 2160 #endif
 2161         np->n_direofoffset = 0;
 2162         np->n_cookieverf.nfsuquad[0] = 0;
 2163         np->n_cookieverf.nfsuquad[1] = 0;
 2164         if (np->n_cookies.lh_first)
 2165                 np->n_cookies.lh_first->ndm_eocookie = 0;
 2166 }
 2167 
 2168 /*
 2169  * The write verifier has changed (probably due to a server reboot), so all
 2170  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
 2171  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
 2172  * flag. Once done the new write verifier can be set for the mount point.
 2173  */
 2174 void
 2175 nfs_clearcommit(mp)
 2176         struct mount *mp;
 2177 {
 2178         register struct vnode *vp, *nvp;
 2179         register struct buf *bp, *nbp;
 2180         int s;
 2181 
 2182         s = splbio();
 2183 loop:
 2184         for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
 2185                 if (vp->v_mount != mp)  /* Paranoia */
 2186                         goto loop;
 2187                 nvp = vp->v_mntvnodes.le_next;
 2188                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
 2189                         nbp = TAILQ_NEXT(bp, b_vnbufs);
 2190                         if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
 2191                                 == (B_DELWRI | B_NEEDCOMMIT))
 2192                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 2193                 }
 2194         }
 2195         splx(s);
 2196 }
 2197 
 2198 #ifndef NFS_NOSERVER
 2199 /*
 2200  * Map errnos to NFS error numbers. For Version 3 also filter out error
 2201  * numbers not specified for the associated procedure.
 2202  */
 2203 int
 2204 nfsrv_errmap(nd, err)
 2205         struct nfsrv_descript *nd;
 2206         register int err;
 2207 {
 2208         register short *defaulterrp, *errp;
 2209 
 2210         if (nd->nd_flag & ND_NFSV3) {
 2211             if (nd->nd_procnum <= NFSPROC_COMMIT) {
 2212                 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
 2213                 while (*++errp) {
 2214                         if (*errp == err)
 2215                                 return (err);
 2216                         else if (*errp > err)
 2217                                 break;
 2218                 }
 2219                 return ((int)*defaulterrp);
 2220             } else
 2221                 return (err & 0xffff);
 2222         }
 2223         if (err <= ELAST)
 2224                 return ((int)nfsrv_v2errmap[err - 1]);
 2225         return (NFSERR_IO);
 2226 }
 2227 
 2228 int
 2229 nfsrv_object_create(vp)
 2230         struct vnode *vp;
 2231 {
 2232 
 2233         if (vp == NULL || vp->v_type != VREG)
 2234                 return (1);
 2235         return (vfs_object_create(vp, curproc,
 2236                                   curproc ? curproc->p_ucred : NULL));
 2237 }
 2238 
 2239 /*
 2240  * Sort the group list in increasing numerical order.
 2241  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
 2242  *  that used to be here.)
 2243  */
 2244 void
 2245 nfsrvw_sort(list, num)
 2246         register gid_t *list;
 2247         register int num;
 2248 {
 2249         register int i, j;
 2250         gid_t v;
 2251 
 2252         /* Insertion sort. */
 2253         for (i = 1; i < num; i++) {
 2254                 v = list[i];
 2255                 /* find correct slot for value v, moving others up */
 2256                 for (j = i; --j >= 0 && v < list[j];)
 2257                         list[j + 1] = list[j];
 2258                 list[j + 1] = v;
 2259         }
 2260 }
 2261 
 2262 /*
 2263  * copy credentials making sure that the result can be compared with bcmp().
 2264  */
 2265 void
 2266 nfsrv_setcred(incred, outcred)
 2267         register struct ucred *incred, *outcred;
 2268 {
 2269         register int i;
 2270 
 2271         bzero((caddr_t)outcred, sizeof (struct ucred));
 2272         outcred->cr_ref = 1;
 2273         outcred->cr_uid = incred->cr_uid;
 2274         outcred->cr_ngroups = incred->cr_ngroups;
 2275         for (i = 0; i < incred->cr_ngroups; i++)
 2276                 outcred->cr_groups[i] = incred->cr_groups[i];
 2277         nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
 2278 }
 2279 #endif /* NFS_NOSERVER */

Cache object: b5497dfd1b6382a2cb148226e6fd1405


[ 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.