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/nlm/nlm_prot_impl.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) 2008 Isilon Inc http://www.isilon.com/
    3  * Authors: Doug Rabson <dfr@rabson.org>
    4  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include "opt_inet6.h"
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/8.0/sys/nlm/nlm_prot_impl.c 197836 2009-10-07 14:14:05Z nyan $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/fcntl.h>
   35 #include <sys/kernel.h>
   36 #include <sys/kthread.h>
   37 #include <sys/lockf.h>
   38 #include <sys/malloc.h>
   39 #include <sys/mount.h>
   40 #if __FreeBSD_version >= 700000
   41 #include <sys/priv.h>
   42 #endif
   43 #include <sys/proc.h>
   44 #include <sys/socket.h>
   45 #include <sys/socketvar.h>
   46 #include <sys/syscall.h>
   47 #include <sys/sysctl.h>
   48 #include <sys/sysent.h>
   49 #include <sys/syslog.h>
   50 #include <sys/sysproto.h>
   51 #include <sys/systm.h>
   52 #include <sys/taskqueue.h>
   53 #include <sys/unistd.h>
   54 #include <sys/vnode.h>
   55 
   56 #include <nfs/nfsproto.h>
   57 #include <nfsclient/nfs.h>
   58 #include <nfsclient/nfsnode.h>
   59 
   60 #include <nlm/nlm_prot.h>
   61 #include <nlm/sm_inter.h>
   62 #include <nlm/nlm.h>
   63 #include <rpc/rpc_com.h>
   64 #include <rpc/rpcb_prot.h>
   65 
   66 MALLOC_DEFINE(M_NLM, "NLM", "Network Lock Manager");
   67 
   68 /*
   69  * If a host is inactive (and holds no locks) for this amount of
   70  * seconds, we consider it idle and stop tracking it.
   71  */
   72 #define NLM_IDLE_TIMEOUT        30
   73 
   74 /*
   75  * We check the host list for idle every few seconds.
   76  */
   77 #define NLM_IDLE_PERIOD         5
   78 
   79 /*
   80  * Support for sysctl vfs.nlm.sysid
   81  */
   82 SYSCTL_NODE(_vfs, OID_AUTO, nlm, CTLFLAG_RW, NULL, "Network Lock Manager");
   83 SYSCTL_NODE(_vfs_nlm, OID_AUTO, sysid, CTLFLAG_RW, NULL, "");
   84 
   85 /*
   86  * Syscall hooks
   87  */
   88 static int nlm_syscall_offset = SYS_nlm_syscall;
   89 static struct sysent nlm_syscall_prev_sysent;
   90 #if __FreeBSD_version < 700000
   91 static struct sysent nlm_syscall_sysent = {
   92         (sizeof(struct nlm_syscall_args) / sizeof(register_t)) | SYF_MPSAFE,
   93         (sy_call_t *) nlm_syscall
   94 };
   95 #else
   96 MAKE_SYSENT(nlm_syscall);
   97 #endif
   98 static bool_t nlm_syscall_registered = FALSE;
   99 
  100 /*
  101  * Debug level passed in from userland. We also support a sysctl hook
  102  * so that it can be changed on a live system.
  103  */
  104 static int nlm_debug_level;
  105 SYSCTL_INT(_debug, OID_AUTO, nlm_debug, CTLFLAG_RW, &nlm_debug_level, 0, "");
  106 
  107 #define NLM_DEBUG(_level, args...)                      \
  108         do {                                            \
  109                 if (nlm_debug_level >= (_level))        \
  110                         log(LOG_DEBUG, args);           \
  111         } while(0)
  112 #define NLM_ERR(args...)                        \
  113         do {                                    \
  114                 log(LOG_ERR, args);             \
  115         } while(0)
  116 
  117 /*
  118  * Grace period handling. The value of nlm_grace_threshold is the
  119  * value of time_uptime after which we are serving requests normally.
  120  */
  121 static time_t nlm_grace_threshold;
  122 
  123 /*
  124  * We check for idle hosts if time_uptime is greater than
  125  * nlm_next_idle_check,
  126  */
  127 static time_t nlm_next_idle_check;
  128 
  129 /*
  130  * A socket to use for RPC - shared by all IPv4 RPC clients.
  131  */
  132 static struct socket *nlm_socket;
  133 
  134 #ifdef INET6
  135 
  136 /*
  137  * A socket to use for RPC - shared by all IPv6 RPC clients.
  138  */
  139 static struct socket *nlm_socket6;
  140 
  141 #endif
  142 
  143 /*
  144  * An RPC client handle that can be used to communicate with the local
  145  * NSM.
  146  */
  147 static CLIENT *nlm_nsm;
  148 
  149 /*
  150  * An AUTH handle for the server's creds.
  151  */
  152 static AUTH *nlm_auth;
  153 
  154 /*
  155  * A zero timeval for sending async RPC messages.
  156  */
  157 struct timeval nlm_zero_tv = { 0, 0 };
  158 
  159 /*
  160  * The local NSM state number
  161  */
  162 int nlm_nsm_state;
  163 
  164 
  165 /*
  166  * A lock to protect the host list and waiting lock list.
  167  */
  168 static struct mtx nlm_global_lock;
  169 
  170 /*
  171  * Locks:
  172  * (l)          locked by nh_lock
  173  * (s)          only accessed via server RPC which is single threaded
  174  * (g)          locked by nlm_global_lock
  175  * (c)          const until freeing
  176  * (a)          modified using atomic ops
  177  */
  178 
  179 /*
  180  * A pending client-side lock request, stored on the nlm_waiting_locks
  181  * list.
  182  */
  183 struct nlm_waiting_lock {
  184         TAILQ_ENTRY(nlm_waiting_lock) nw_link; /* (g) */
  185         bool_t          nw_waiting;            /* (g) */
  186         nlm4_lock       nw_lock;               /* (c) */
  187         union nfsfh     nw_fh;                 /* (c) */
  188         struct vnode    *nw_vp;                /* (c) */
  189 };
  190 TAILQ_HEAD(nlm_waiting_lock_list, nlm_waiting_lock);
  191 
  192 struct nlm_waiting_lock_list nlm_waiting_locks; /* (g) */
  193 
  194 /*
  195  * A pending server-side asynchronous lock request, stored on the
  196  * nh_pending list of the NLM host.
  197  */
  198 struct nlm_async_lock {
  199         TAILQ_ENTRY(nlm_async_lock) af_link; /* (l) host's list of locks */
  200         struct task     af_task;        /* (c) async callback details */
  201         void            *af_cookie;     /* (l) lock manager cancel token */
  202         struct vnode    *af_vp;         /* (l) vnode to lock */
  203         struct flock    af_fl;          /* (c) lock details */
  204         struct nlm_host *af_host;       /* (c) host which is locking */
  205         CLIENT          *af_rpc;        /* (c) rpc client to send message */
  206         nlm4_testargs   af_granted;     /* (c) notification details */
  207 };
  208 TAILQ_HEAD(nlm_async_lock_list, nlm_async_lock);
  209 
  210 /*
  211  * NLM host.
  212  */
  213 enum nlm_host_state {
  214         NLM_UNMONITORED,
  215         NLM_MONITORED,
  216         NLM_MONITOR_FAILED,
  217         NLM_RECOVERING
  218 };
  219 
  220 struct nlm_rpc {
  221         CLIENT          *nr_client;    /* (l) RPC client handle */
  222         time_t          nr_create_time; /* (l) when client was created */
  223 };
  224 
  225 struct nlm_host {
  226         struct mtx      nh_lock;
  227         volatile u_int  nh_refs;       /* (a) reference count */
  228         TAILQ_ENTRY(nlm_host) nh_link; /* (g) global list of hosts */
  229         char            nh_caller_name[MAXNAMELEN]; /* (c) printable name of host */
  230         uint32_t        nh_sysid;        /* (c) our allocaed system ID */
  231         char            nh_sysid_string[10]; /* (c) string rep. of sysid */
  232         struct sockaddr_storage nh_addr; /* (s) remote address of host */
  233         struct nlm_rpc  nh_srvrpc;       /* (l) RPC for server replies */
  234         struct nlm_rpc  nh_clntrpc;      /* (l) RPC for client requests */
  235         rpcvers_t       nh_vers;         /* (s) NLM version of host */
  236         int             nh_state;        /* (s) last seen NSM state of host */
  237         enum nlm_host_state nh_monstate; /* (l) local NSM monitoring state */
  238         time_t          nh_idle_timeout; /* (s) Time at which host is idle */
  239         struct sysctl_ctx_list nh_sysctl; /* (c) vfs.nlm.sysid nodes */
  240         struct nlm_async_lock_list nh_pending; /* (l) pending async locks */
  241         struct nlm_async_lock_list nh_finished; /* (l) finished async locks */
  242 };
  243 TAILQ_HEAD(nlm_host_list, nlm_host);
  244 
  245 static struct nlm_host_list nlm_hosts; /* (g) */
  246 static uint32_t nlm_next_sysid = 1;    /* (g) */
  247 
  248 static void     nlm_host_unmonitor(struct nlm_host *);
  249 
  250 /**********************************************************************/
  251 
  252 /*
  253  * Initialise NLM globals.
  254  */
  255 static void
  256 nlm_init(void *dummy)
  257 {
  258         int error;
  259 
  260         mtx_init(&nlm_global_lock, "nlm_global_lock", NULL, MTX_DEF);
  261         TAILQ_INIT(&nlm_waiting_locks);
  262         TAILQ_INIT(&nlm_hosts);
  263 
  264         error = syscall_register(&nlm_syscall_offset, &nlm_syscall_sysent,
  265             &nlm_syscall_prev_sysent);
  266         if (error)
  267                 NLM_ERR("Can't register NLM syscall\n");
  268         else
  269                 nlm_syscall_registered = TRUE;
  270 }
  271 SYSINIT(nlm_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_init, NULL);
  272 
  273 static void
  274 nlm_uninit(void *dummy)
  275 {
  276 
  277         if (nlm_syscall_registered)
  278                 syscall_deregister(&nlm_syscall_offset,
  279                     &nlm_syscall_prev_sysent);
  280 }
  281 SYSUNINIT(nlm_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_uninit, NULL);
  282 
  283 /*
  284  * Copy a struct netobj.
  285  */ 
  286 void
  287 nlm_copy_netobj(struct netobj *dst, struct netobj *src,
  288     struct malloc_type *type)
  289 {
  290 
  291         dst->n_len = src->n_len;
  292         dst->n_bytes = malloc(src->n_len, type, M_WAITOK);
  293         memcpy(dst->n_bytes, src->n_bytes, src->n_len);
  294 }
  295 
  296 /*
  297  * Create an RPC client handle for the given (address,prog,vers)
  298  * triple using UDP.
  299  */
  300 static CLIENT *
  301 nlm_get_rpc(struct sockaddr *sa, rpcprog_t prog, rpcvers_t vers)
  302 {
  303         char *wchan = "nlmrcv";
  304         const char* protofmly;
  305         struct sockaddr_storage ss;
  306         struct socket *so;
  307         CLIENT *rpcb;
  308         struct timeval timo;
  309         RPCB parms;
  310         char *uaddr;
  311         enum clnt_stat stat = RPC_SUCCESS;
  312         int rpcvers = RPCBVERS4;
  313         bool_t do_tcp = FALSE;
  314         bool_t tryagain = FALSE;
  315         struct portmap mapping;
  316         u_short port = 0;
  317 
  318         /*
  319          * First we need to contact the remote RPCBIND service to find
  320          * the right port.
  321          */
  322         memcpy(&ss, sa, sa->sa_len);
  323         switch (ss.ss_family) {
  324         case AF_INET:
  325                 ((struct sockaddr_in *)&ss)->sin_port = htons(111);
  326                 protofmly = "inet";
  327                 so = nlm_socket;
  328                 break;
  329                 
  330 #ifdef INET6
  331         case AF_INET6:
  332                 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(111);
  333                 protofmly = "inet6";
  334                 so = nlm_socket6;
  335                 break;
  336 #endif
  337 
  338         default:
  339                 /*
  340                  * Unsupported address family - fail.
  341                  */
  342                 return (NULL);
  343         }
  344 
  345         rpcb = clnt_dg_create(so, (struct sockaddr *)&ss,
  346             RPCBPROG, rpcvers, 0, 0);
  347         if (!rpcb)
  348                 return (NULL);
  349 
  350 try_tcp:
  351         parms.r_prog = prog;
  352         parms.r_vers = vers;
  353         if (do_tcp)
  354                 parms.r_netid = "tcp";
  355         else
  356                 parms.r_netid = "udp";
  357         parms.r_addr = "";
  358         parms.r_owner = "";
  359 
  360         /*
  361          * Use the default timeout.
  362          */
  363         timo.tv_sec = 25;
  364         timo.tv_usec = 0;
  365 again:
  366         switch (rpcvers) {
  367         case RPCBVERS4:
  368         case RPCBVERS:
  369                 /*
  370                  * Try RPCBIND 4 then 3.
  371                  */
  372                 uaddr = NULL;
  373                 stat = CLNT_CALL(rpcb, (rpcprog_t) RPCBPROC_GETADDR,
  374                     (xdrproc_t) xdr_rpcb, &parms,
  375                     (xdrproc_t) xdr_wrapstring, &uaddr, timo);
  376                 if (stat == RPC_SUCCESS) {
  377                         /*
  378                          * We have a reply from the remote RPCBIND - turn it
  379                          * into an appropriate address and make a new client
  380                          * that can talk to the remote NLM.
  381                          *
  382                          * XXX fixup IPv6 scope ID.
  383                          */
  384                         struct netbuf *a;
  385                         a = __rpc_uaddr2taddr_af(ss.ss_family, uaddr);
  386                         if (!a) {
  387                                 tryagain = TRUE;
  388                         } else {
  389                                 tryagain = FALSE;
  390                                 memcpy(&ss, a->buf, a->len);
  391                                 free(a->buf, M_RPC);
  392                                 free(a, M_RPC);
  393                                 xdr_free((xdrproc_t) xdr_wrapstring, &uaddr);
  394                         }
  395                 }
  396                 if (tryagain || stat == RPC_PROGVERSMISMATCH) {
  397                         if (rpcvers == RPCBVERS4)
  398                                 rpcvers = RPCBVERS;
  399                         else if (rpcvers == RPCBVERS)
  400                                 rpcvers = PMAPVERS;
  401                         CLNT_CONTROL(rpcb, CLSET_VERS, &rpcvers);
  402                         goto again;
  403                 }
  404                 break;
  405         case PMAPVERS:
  406                 /*
  407                  * Try portmap.
  408                  */
  409                 mapping.pm_prog = parms.r_prog;
  410                 mapping.pm_vers = parms.r_vers;
  411                 mapping.pm_prot = do_tcp ? IPPROTO_TCP : IPPROTO_UDP;
  412                 mapping.pm_port = 0;
  413 
  414                 stat = CLNT_CALL(rpcb, (rpcprog_t) PMAPPROC_GETPORT,
  415                     (xdrproc_t) xdr_portmap, &mapping,
  416                     (xdrproc_t) xdr_u_short, &port, timo);
  417 
  418                 if (stat == RPC_SUCCESS) {
  419                         switch (ss.ss_family) {
  420                         case AF_INET:
  421                                 ((struct sockaddr_in *)&ss)->sin_port =
  422                                         htons(port);
  423                                 break;
  424                 
  425 #ifdef INET6
  426                         case AF_INET6:
  427                                 ((struct sockaddr_in6 *)&ss)->sin6_port =
  428                                         htons(port);
  429                                 break;
  430 #endif
  431                         }
  432                 }
  433                 break;
  434         default:
  435                 panic("invalid rpcvers %d", rpcvers);
  436         }
  437         /*
  438          * We may have a positive response from the portmapper, but the NLM
  439          * service was not found. Make sure we received a valid port.
  440          */
  441         switch (ss.ss_family) {
  442         case AF_INET:
  443                 port = ((struct sockaddr_in *)&ss)->sin_port;
  444                 break;
  445 #ifdef INET6
  446         case AF_INET6:
  447                 port = ((struct sockaddr_in6 *)&ss)->sin6_port;
  448                 break;
  449 #endif
  450         }
  451         if (stat != RPC_SUCCESS || !port) {
  452                 /*
  453                  * If we were able to talk to rpcbind or portmap, but the udp
  454                  * variant wasn't available, ask about tcp.
  455                  *
  456                  * XXX - We could also check for a TCP portmapper, but
  457                  * if the host is running a portmapper at all, we should be able
  458                  * to hail it over UDP.
  459                  */
  460                 if (stat == RPC_SUCCESS && !do_tcp) {
  461                         do_tcp = TRUE;
  462                         goto try_tcp;
  463                 }
  464 
  465                 /* Otherwise, bad news. */
  466                 NLM_ERR("NLM: failed to contact remote rpcbind, "
  467                     "stat = %d, port = %d\n", (int) stat, port);
  468                 CLNT_DESTROY(rpcb);
  469                 return (NULL);
  470         }
  471 
  472         if (do_tcp) {
  473                 /*
  474                  * Destroy the UDP client we used to speak to rpcbind and
  475                  * recreate as a TCP client.
  476                  */
  477                 struct netconfig *nconf = NULL;
  478 
  479                 CLNT_DESTROY(rpcb);
  480 
  481                 switch (ss.ss_family) {
  482                 case AF_INET:
  483                         nconf = getnetconfigent("tcp");
  484                         break;
  485 #ifdef INET6
  486                 case AF_INET6:
  487                         nconf = getnetconfigent("tcp6");
  488                         break;
  489 #endif
  490                 }
  491 
  492                 rpcb = clnt_reconnect_create(nconf, (struct sockaddr *)&ss,
  493                     prog, vers, 0, 0);
  494                 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
  495                 rpcb->cl_auth = nlm_auth;
  496                 
  497         } else {
  498                 /*
  499                  * Re-use the client we used to speak to rpcbind.
  500                  */
  501                 CLNT_CONTROL(rpcb, CLSET_SVC_ADDR, &ss);
  502                 CLNT_CONTROL(rpcb, CLSET_PROG, &prog);
  503                 CLNT_CONTROL(rpcb, CLSET_VERS, &vers);
  504                 CLNT_CONTROL(rpcb, CLSET_WAITCHAN, wchan);
  505                 rpcb->cl_auth = nlm_auth;
  506         }
  507 
  508         return (rpcb);
  509 }
  510 
  511 /*
  512  * This async callback after when an async lock request has been
  513  * granted. We notify the host which initiated the request.
  514  */
  515 static void
  516 nlm_lock_callback(void *arg, int pending)
  517 {
  518         struct nlm_async_lock *af = (struct nlm_async_lock *) arg;
  519         struct rpc_callextra ext;
  520 
  521         NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) granted\n",
  522             af, af->af_host->nh_caller_name, af->af_host->nh_sysid);
  523 
  524         /*
  525          * Send the results back to the host.
  526          *
  527          * Note: there is a possible race here with nlm_host_notify
  528          * destroying the RPC client. To avoid problems, the first
  529          * thing nlm_host_notify does is to cancel pending async lock
  530          * requests.
  531          */
  532         memset(&ext, 0, sizeof(ext));
  533         ext.rc_auth = nlm_auth;
  534         if (af->af_host->nh_vers == NLM_VERS4) {
  535                 nlm4_granted_msg_4(&af->af_granted,
  536                     NULL, af->af_rpc, &ext, nlm_zero_tv);
  537         } else {
  538                 /*
  539                  * Back-convert to legacy protocol
  540                  */
  541                 nlm_testargs granted;
  542                 granted.cookie = af->af_granted.cookie;
  543                 granted.exclusive = af->af_granted.exclusive;
  544                 granted.alock.caller_name =
  545                         af->af_granted.alock.caller_name;
  546                 granted.alock.fh = af->af_granted.alock.fh;
  547                 granted.alock.oh = af->af_granted.alock.oh;
  548                 granted.alock.svid = af->af_granted.alock.svid;
  549                 granted.alock.l_offset =
  550                         af->af_granted.alock.l_offset;
  551                 granted.alock.l_len =
  552                         af->af_granted.alock.l_len;
  553 
  554                 nlm_granted_msg_1(&granted,
  555                     NULL, af->af_rpc, &ext, nlm_zero_tv);
  556         }
  557 
  558         /*
  559          * Move this entry to the nh_finished list. Someone else will
  560          * free it later - its too hard to do it here safely without
  561          * racing with cancel.
  562          *
  563          * XXX possibly we should have a third "granted sent but not
  564          * ack'ed" list so that we can re-send the granted message.
  565          */
  566         mtx_lock(&af->af_host->nh_lock);
  567         TAILQ_REMOVE(&af->af_host->nh_pending, af, af_link);
  568         TAILQ_INSERT_TAIL(&af->af_host->nh_finished, af, af_link);
  569         mtx_unlock(&af->af_host->nh_lock);
  570 }
  571 
  572 /*
  573  * Free an async lock request. The request must have been removed from
  574  * any list.
  575  */
  576 static void
  577 nlm_free_async_lock(struct nlm_async_lock *af)
  578 {
  579         /*
  580          * Free an async lock.
  581          */
  582         if (af->af_rpc)
  583                 CLNT_RELEASE(af->af_rpc);
  584         xdr_free((xdrproc_t) xdr_nlm4_testargs, &af->af_granted);
  585         if (af->af_vp)
  586                 vrele(af->af_vp);
  587         free(af, M_NLM);
  588 }
  589 
  590 /*
  591  * Cancel our async request - this must be called with
  592  * af->nh_host->nh_lock held. This is slightly complicated by a
  593  * potential race with our own callback. If we fail to cancel the
  594  * lock, it must already have been granted - we make sure our async
  595  * task has completed by calling taskqueue_drain in this case.
  596  */
  597 static int
  598 nlm_cancel_async_lock(struct nlm_async_lock *af)
  599 {
  600         struct nlm_host *host = af->af_host;
  601         int error;
  602 
  603         mtx_assert(&host->nh_lock, MA_OWNED);
  604 
  605         mtx_unlock(&host->nh_lock);
  606 
  607         error = VOP_ADVLOCKASYNC(af->af_vp, NULL, F_CANCEL, &af->af_fl,
  608             F_REMOTE, NULL, &af->af_cookie);
  609 
  610         if (error) {
  611                 /*
  612                  * We failed to cancel - make sure our callback has
  613                  * completed before we continue.
  614                  */
  615                 taskqueue_drain(taskqueue_thread, &af->af_task);
  616         }
  617 
  618         mtx_lock(&host->nh_lock);
  619         
  620         if (!error) {
  621                 NLM_DEBUG(2, "NLM: async lock %p for %s (sysid %d) "
  622                     "cancelled\n", af, host->nh_caller_name, host->nh_sysid);
  623 
  624                 /*
  625                  * Remove from the nh_pending list and free now that
  626                  * we are safe from the callback.
  627                  */
  628                 TAILQ_REMOVE(&host->nh_pending, af, af_link);
  629                 mtx_unlock(&host->nh_lock);
  630                 nlm_free_async_lock(af);
  631                 mtx_lock(&host->nh_lock);
  632         }
  633 
  634         return (error);
  635 }
  636 
  637 static void
  638 nlm_free_finished_locks(struct nlm_host *host)
  639 {
  640         struct nlm_async_lock *af;
  641 
  642         mtx_lock(&host->nh_lock);
  643         while ((af = TAILQ_FIRST(&host->nh_finished)) != NULL) {
  644                 TAILQ_REMOVE(&host->nh_finished, af, af_link);
  645                 mtx_unlock(&host->nh_lock);
  646                 nlm_free_async_lock(af);
  647                 mtx_lock(&host->nh_lock);
  648         }
  649         mtx_unlock(&host->nh_lock);
  650 }
  651 
  652 /*
  653  * Free resources used by a host. This is called after the reference
  654  * count has reached zero so it doesn't need to worry about locks.
  655  */
  656 static void
  657 nlm_host_destroy(struct nlm_host *host)
  658 {
  659 
  660         mtx_lock(&nlm_global_lock);
  661         TAILQ_REMOVE(&nlm_hosts, host, nh_link);
  662         mtx_unlock(&nlm_global_lock);
  663 
  664         if (host->nh_srvrpc.nr_client)
  665                 CLNT_RELEASE(host->nh_srvrpc.nr_client);
  666         if (host->nh_clntrpc.nr_client)
  667                 CLNT_RELEASE(host->nh_clntrpc.nr_client);
  668         mtx_destroy(&host->nh_lock);
  669         sysctl_ctx_free(&host->nh_sysctl);
  670         free(host, M_NLM);
  671 }
  672 
  673 /*
  674  * Thread start callback for client lock recovery
  675  */
  676 static void
  677 nlm_client_recovery_start(void *arg)
  678 {
  679         struct nlm_host *host = (struct nlm_host *) arg;
  680 
  681         NLM_DEBUG(1, "NLM: client lock recovery for %s started\n",
  682             host->nh_caller_name);
  683 
  684         nlm_client_recovery(host);
  685 
  686         NLM_DEBUG(1, "NLM: client lock recovery for %s completed\n",
  687             host->nh_caller_name);
  688 
  689         host->nh_monstate = NLM_MONITORED;
  690         nlm_host_release(host);
  691 
  692         kthread_exit();
  693 }
  694 
  695 /*
  696  * This is called when we receive a host state change notification. We
  697  * unlock any active locks owned by the host. When rpc.lockd is
  698  * shutting down, this function is called with newstate set to zero
  699  * which allows us to cancel any pending async locks and clear the
  700  * locking state.
  701  */
  702 static void
  703 nlm_host_notify(struct nlm_host *host, int newstate)
  704 {
  705         struct nlm_async_lock *af;
  706 
  707         if (newstate) {
  708                 NLM_DEBUG(1, "NLM: host %s (sysid %d) rebooted, new "
  709                     "state is %d\n", host->nh_caller_name,
  710                     host->nh_sysid, newstate);
  711         }
  712 
  713         /*
  714          * Cancel any pending async locks for this host.
  715          */
  716         mtx_lock(&host->nh_lock);
  717         while ((af = TAILQ_FIRST(&host->nh_pending)) != NULL) {
  718                 /*
  719                  * nlm_cancel_async_lock will remove the entry from
  720                  * nh_pending and free it.
  721                  */
  722                 nlm_cancel_async_lock(af);
  723         }
  724         mtx_unlock(&host->nh_lock);
  725         nlm_free_finished_locks(host);
  726 
  727         /*
  728          * The host just rebooted - trash its locks.
  729          */
  730         lf_clearremotesys(host->nh_sysid);
  731         host->nh_state = newstate;
  732 
  733         /*
  734          * If we have any remote locks for this host (i.e. it
  735          * represents a remote NFS server that our local NFS client
  736          * has locks for), start a recovery thread.
  737          */
  738         if (newstate != 0
  739             && host->nh_monstate != NLM_RECOVERING
  740             && lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid) > 0) {
  741                 struct thread *td;
  742                 host->nh_monstate = NLM_RECOVERING;
  743                 refcount_acquire(&host->nh_refs);
  744                 kthread_add(nlm_client_recovery_start, host, curproc, &td, 0, 0,
  745                     "NFS lock recovery for %s", host->nh_caller_name);
  746         }
  747 }
  748 
  749 /*
  750  * Sysctl handler to count the number of locks for a sysid.
  751  */
  752 static int
  753 nlm_host_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
  754 {
  755         struct nlm_host *host;
  756         int count;
  757 
  758         host = oidp->oid_arg1;
  759         count = lf_countlocks(host->nh_sysid);
  760         return sysctl_handle_int(oidp, &count, 0, req);
  761 }
  762 
  763 /*
  764  * Sysctl handler to count the number of client locks for a sysid.
  765  */
  766 static int
  767 nlm_host_client_lock_count_sysctl(SYSCTL_HANDLER_ARGS)
  768 {
  769         struct nlm_host *host;
  770         int count;
  771 
  772         host = oidp->oid_arg1;
  773         count = lf_countlocks(NLM_SYSID_CLIENT | host->nh_sysid);
  774         return sysctl_handle_int(oidp, &count, 0, req);
  775 }
  776 
  777 /*
  778  * Create a new NLM host.
  779  */
  780 static struct nlm_host *
  781 nlm_create_host(const char* caller_name)
  782 {
  783         struct nlm_host *host;
  784         struct sysctl_oid *oid;
  785 
  786         mtx_assert(&nlm_global_lock, MA_OWNED);
  787 
  788         NLM_DEBUG(1, "NLM: new host %s (sysid %d)\n",
  789             caller_name, nlm_next_sysid);
  790         host = malloc(sizeof(struct nlm_host), M_NLM, M_NOWAIT|M_ZERO);
  791         if (!host)
  792                 return (NULL);
  793         mtx_init(&host->nh_lock, "nh_lock", NULL, MTX_DEF);
  794         host->nh_refs = 1;
  795         strlcpy(host->nh_caller_name, caller_name, MAXNAMELEN);
  796         host->nh_sysid = nlm_next_sysid++;
  797         snprintf(host->nh_sysid_string, sizeof(host->nh_sysid_string),
  798                 "%d", host->nh_sysid);
  799         host->nh_vers = 0;
  800         host->nh_state = 0;
  801         host->nh_monstate = NLM_UNMONITORED;
  802         TAILQ_INIT(&host->nh_pending);
  803         TAILQ_INIT(&host->nh_finished);
  804         TAILQ_INSERT_TAIL(&nlm_hosts, host, nh_link);
  805 
  806         mtx_unlock(&nlm_global_lock);
  807 
  808         sysctl_ctx_init(&host->nh_sysctl);
  809         oid = SYSCTL_ADD_NODE(&host->nh_sysctl,
  810             SYSCTL_STATIC_CHILDREN(_vfs_nlm_sysid),
  811             OID_AUTO, host->nh_sysid_string, CTLFLAG_RD, NULL, "");
  812         SYSCTL_ADD_STRING(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
  813             "hostname", CTLFLAG_RD, host->nh_caller_name, 0, "");
  814         SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
  815             "version", CTLFLAG_RD, &host->nh_vers, 0, "");
  816         SYSCTL_ADD_INT(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
  817             "monitored", CTLFLAG_RD, &host->nh_monstate, 0, "");
  818         SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
  819             "lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
  820             nlm_host_lock_count_sysctl, "I", "");
  821         SYSCTL_ADD_PROC(&host->nh_sysctl, SYSCTL_CHILDREN(oid), OID_AUTO,
  822             "client_lock_count", CTLTYPE_INT | CTLFLAG_RD, host, 0,
  823             nlm_host_client_lock_count_sysctl, "I", "");
  824 
  825         mtx_lock(&nlm_global_lock);
  826 
  827         return (host);
  828 }
  829 
  830 /*
  831  * Acquire the next sysid for remote locks not handled by the NLM.
  832  */
  833 uint32_t
  834 nlm_acquire_next_sysid(void)
  835 {
  836         uint32_t next_sysid;
  837 
  838         mtx_lock(&nlm_global_lock);
  839         next_sysid = nlm_next_sysid++;
  840         mtx_unlock(&nlm_global_lock);
  841         return (next_sysid);
  842 }
  843 
  844 /*
  845  * Return non-zero if the address parts of the two sockaddrs are the
  846  * same.
  847  */
  848 static int
  849 nlm_compare_addr(const struct sockaddr *a, const struct sockaddr *b)
  850 {
  851         const struct sockaddr_in *a4, *b4;
  852 #ifdef INET6
  853         const struct sockaddr_in6 *a6, *b6;
  854 #endif
  855 
  856         if (a->sa_family != b->sa_family)
  857                 return (FALSE);
  858 
  859         switch (a->sa_family) {
  860         case AF_INET:
  861                 a4 = (const struct sockaddr_in *) a;
  862                 b4 = (const struct sockaddr_in *) b;
  863                 return !memcmp(&a4->sin_addr, &b4->sin_addr,
  864                     sizeof(a4->sin_addr));
  865 #ifdef INET6
  866         case AF_INET6:
  867                 a6 = (const struct sockaddr_in6 *) a;
  868                 b6 = (const struct sockaddr_in6 *) b;
  869                 return !memcmp(&a6->sin6_addr, &b6->sin6_addr,
  870                     sizeof(a6->sin6_addr));
  871 #endif
  872         }
  873 
  874         return (0);
  875 }
  876 
  877 /*
  878  * Check for idle hosts and stop monitoring them. We could also free
  879  * the host structure here, possibly after a larger timeout but that
  880  * would require some care to avoid races with
  881  * e.g. nlm_host_lock_count_sysctl.
  882  */
  883 static void
  884 nlm_check_idle(void)
  885 {
  886         struct nlm_host *host;
  887 
  888         mtx_assert(&nlm_global_lock, MA_OWNED);
  889 
  890         if (time_uptime <= nlm_next_idle_check)
  891                 return;
  892 
  893         nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
  894 
  895         TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
  896                 if (host->nh_monstate == NLM_MONITORED
  897                     && time_uptime > host->nh_idle_timeout) {
  898                         mtx_unlock(&nlm_global_lock);
  899                         if (lf_countlocks(host->nh_sysid) > 0
  900                             || lf_countlocks(NLM_SYSID_CLIENT
  901                                 + host->nh_sysid)) {
  902                                 host->nh_idle_timeout =
  903                                         time_uptime + NLM_IDLE_TIMEOUT;
  904                                 mtx_lock(&nlm_global_lock);
  905                                 continue;
  906                         }
  907                         nlm_host_unmonitor(host);
  908                         mtx_lock(&nlm_global_lock);
  909                 } 
  910         }
  911 }
  912 
  913 /*
  914  * Search for an existing NLM host that matches the given name
  915  * (typically the caller_name element of an nlm4_lock).  If none is
  916  * found, create a new host. If 'addr' is non-NULL, record the remote
  917  * address of the host so that we can call it back for async
  918  * responses. If 'vers' is greater than zero then record the NLM
  919  * program version to use to communicate with this client.
  920  */
  921 struct nlm_host *
  922 nlm_find_host_by_name(const char *name, const struct sockaddr *addr,
  923     rpcvers_t vers)
  924 {
  925         struct nlm_host *host;
  926 
  927         mtx_lock(&nlm_global_lock);
  928 
  929         /*
  930          * The remote host is determined by caller_name.
  931          */
  932         TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
  933                 if (!strcmp(host->nh_caller_name, name))
  934                         break;
  935         }
  936 
  937         if (!host) {
  938                 host = nlm_create_host(name);
  939                 if (!host) {
  940                         mtx_unlock(&nlm_global_lock);
  941                         return (NULL);
  942                 }
  943         }
  944         refcount_acquire(&host->nh_refs);
  945 
  946         host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
  947 
  948         /*
  949          * If we have an address for the host, record it so that we
  950          * can send async replies etc.
  951          */
  952         if (addr) {
  953                 
  954                 KASSERT(addr->sa_len < sizeof(struct sockaddr_storage),
  955                     ("Strange remote transport address length"));
  956 
  957                 /*
  958                  * If we have seen an address before and we currently
  959                  * have an RPC client handle, make sure the address is
  960                  * the same, otherwise discard the client handle.
  961                  */
  962                 if (host->nh_addr.ss_len && host->nh_srvrpc.nr_client) {
  963                         if (!nlm_compare_addr(
  964                                     (struct sockaddr *) &host->nh_addr,
  965                                     addr)
  966                             || host->nh_vers != vers) {
  967                                 CLIENT *client;
  968                                 mtx_lock(&host->nh_lock);
  969                                 client = host->nh_srvrpc.nr_client;
  970                                 host->nh_srvrpc.nr_client = NULL;
  971                                 mtx_unlock(&host->nh_lock);
  972                                 if (client) {
  973                                         CLNT_RELEASE(client);
  974                                 }
  975                         }
  976                 }
  977                 memcpy(&host->nh_addr, addr, addr->sa_len);
  978                 host->nh_vers = vers;
  979         }
  980 
  981         nlm_check_idle();
  982 
  983         mtx_unlock(&nlm_global_lock);
  984 
  985         return (host);
  986 }
  987 
  988 /*
  989  * Search for an existing NLM host that matches the given remote
  990  * address. If none is found, create a new host with the requested
  991  * address and remember 'vers' as the NLM protocol version to use for
  992  * that host.
  993  */
  994 struct nlm_host *
  995 nlm_find_host_by_addr(const struct sockaddr *addr, int vers)
  996 {
  997         /*
  998          * Fake up a name using inet_ntop. This buffer is
  999          * large enough for an IPv6 address.
 1000          */
 1001         char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
 1002         struct nlm_host *host;
 1003 
 1004         switch (addr->sa_family) {
 1005         case AF_INET:
 1006                 __rpc_inet_ntop(AF_INET,
 1007                     &((const struct sockaddr_in *) addr)->sin_addr,
 1008                     tmp, sizeof tmp);
 1009                 break;
 1010 #ifdef INET6
 1011         case AF_INET6:
 1012                 __rpc_inet_ntop(AF_INET6,
 1013                     &((const struct sockaddr_in6 *) addr)->sin6_addr,
 1014                     tmp, sizeof tmp);
 1015                 break;
 1016 #endif
 1017         default:
 1018                 strcmp(tmp, "<unknown>");
 1019         }
 1020 
 1021 
 1022         mtx_lock(&nlm_global_lock);
 1023 
 1024         /*
 1025          * The remote host is determined by caller_name.
 1026          */
 1027         TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
 1028                 if (nlm_compare_addr(addr,
 1029                         (const struct sockaddr *) &host->nh_addr))
 1030                         break;
 1031         }
 1032 
 1033         if (!host) {
 1034                 host = nlm_create_host(tmp);
 1035                 if (!host) {
 1036                         mtx_unlock(&nlm_global_lock);
 1037                         return (NULL);
 1038                 }
 1039                 memcpy(&host->nh_addr, addr, addr->sa_len);
 1040                 host->nh_vers = vers;
 1041         }
 1042         refcount_acquire(&host->nh_refs);
 1043 
 1044         host->nh_idle_timeout = time_uptime + NLM_IDLE_TIMEOUT;
 1045 
 1046         nlm_check_idle();
 1047 
 1048         mtx_unlock(&nlm_global_lock);
 1049 
 1050         return (host);
 1051 }
 1052 
 1053 /*
 1054  * Find the NLM host that matches the value of 'sysid'. If none
 1055  * exists, return NULL.
 1056  */
 1057 static struct nlm_host *
 1058 nlm_find_host_by_sysid(int sysid)
 1059 {
 1060         struct nlm_host *host;
 1061 
 1062         TAILQ_FOREACH(host, &nlm_hosts, nh_link) {
 1063                 if (host->nh_sysid == sysid) {
 1064                         refcount_acquire(&host->nh_refs);
 1065                         return (host);
 1066                 }
 1067         }
 1068 
 1069         return (NULL);
 1070 }
 1071 
 1072 void nlm_host_release(struct nlm_host *host)
 1073 {
 1074         if (refcount_release(&host->nh_refs)) {
 1075                 /*
 1076                  * Free the host
 1077                  */
 1078                 nlm_host_destroy(host);
 1079         }
 1080 }
 1081 
 1082 /*
 1083  * Unregister this NLM host with the local NSM due to idleness.
 1084  */
 1085 static void
 1086 nlm_host_unmonitor(struct nlm_host *host)
 1087 {
 1088         mon_id smmonid;
 1089         sm_stat_res smstat;
 1090         struct timeval timo;
 1091         enum clnt_stat stat;
 1092 
 1093         NLM_DEBUG(1, "NLM: unmonitoring %s (sysid %d)\n",
 1094             host->nh_caller_name, host->nh_sysid);
 1095 
 1096         /*
 1097          * We put our assigned system ID value in the priv field to
 1098          * make it simpler to find the host if we are notified of a
 1099          * host restart.
 1100          */
 1101         smmonid.mon_name = host->nh_caller_name;
 1102         smmonid.my_id.my_name = "localhost";
 1103         smmonid.my_id.my_prog = NLM_PROG;
 1104         smmonid.my_id.my_vers = NLM_SM;
 1105         smmonid.my_id.my_proc = NLM_SM_NOTIFY;
 1106 
 1107         timo.tv_sec = 25;
 1108         timo.tv_usec = 0;
 1109         stat = CLNT_CALL(nlm_nsm, SM_UNMON,
 1110             (xdrproc_t) xdr_mon, &smmonid,
 1111             (xdrproc_t) xdr_sm_stat, &smstat, timo);
 1112 
 1113         if (stat != RPC_SUCCESS) {
 1114                 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
 1115                 return;
 1116         }
 1117         if (smstat.res_stat == stat_fail) {
 1118                 NLM_ERR("Local NSM refuses to unmonitor %s\n",
 1119                     host->nh_caller_name);
 1120                 return;
 1121         }
 1122 
 1123         host->nh_monstate = NLM_UNMONITORED;
 1124 }
 1125 
 1126 /*
 1127  * Register this NLM host with the local NSM so that we can be
 1128  * notified if it reboots.
 1129  */
 1130 void
 1131 nlm_host_monitor(struct nlm_host *host, int state)
 1132 {
 1133         mon smmon;
 1134         sm_stat_res smstat;
 1135         struct timeval timo;
 1136         enum clnt_stat stat;
 1137 
 1138         if (state && !host->nh_state) {
 1139                 /*
 1140                  * This is the first time we have seen an NSM state
 1141                  * value for this host. We record it here to help
 1142                  * detect host reboots.
 1143                  */
 1144                 host->nh_state = state;
 1145                 NLM_DEBUG(1, "NLM: host %s (sysid %d) has NSM state %d\n",
 1146                     host->nh_caller_name, host->nh_sysid, state);
 1147         }
 1148 
 1149         mtx_lock(&host->nh_lock);
 1150         if (host->nh_monstate != NLM_UNMONITORED) {
 1151                 mtx_unlock(&host->nh_lock);
 1152                 return;
 1153         }
 1154         host->nh_monstate = NLM_MONITORED;
 1155         mtx_unlock(&host->nh_lock);
 1156 
 1157         NLM_DEBUG(1, "NLM: monitoring %s (sysid %d)\n",
 1158             host->nh_caller_name, host->nh_sysid);
 1159 
 1160         /*
 1161          * We put our assigned system ID value in the priv field to
 1162          * make it simpler to find the host if we are notified of a
 1163          * host restart.
 1164          */
 1165         smmon.mon_id.mon_name = host->nh_caller_name;
 1166         smmon.mon_id.my_id.my_name = "localhost";
 1167         smmon.mon_id.my_id.my_prog = NLM_PROG;
 1168         smmon.mon_id.my_id.my_vers = NLM_SM;
 1169         smmon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
 1170         memcpy(smmon.priv, &host->nh_sysid, sizeof(host->nh_sysid));
 1171 
 1172         timo.tv_sec = 25;
 1173         timo.tv_usec = 0;
 1174         stat = CLNT_CALL(nlm_nsm, SM_MON,
 1175             (xdrproc_t) xdr_mon, &smmon,
 1176             (xdrproc_t) xdr_sm_stat, &smstat, timo);
 1177 
 1178         if (stat != RPC_SUCCESS) {
 1179                 NLM_ERR("Failed to contact local NSM - rpc error %d\n", stat);
 1180                 return;
 1181         }
 1182         if (smstat.res_stat == stat_fail) {
 1183                 NLM_ERR("Local NSM refuses to monitor %s\n",
 1184                     host->nh_caller_name);
 1185                 mtx_lock(&host->nh_lock);
 1186                 host->nh_monstate = NLM_MONITOR_FAILED;
 1187                 mtx_unlock(&host->nh_lock);
 1188                 return;
 1189         }
 1190 
 1191         host->nh_monstate = NLM_MONITORED;
 1192 }
 1193 
 1194 /*
 1195  * Return an RPC client handle that can be used to talk to the NLM
 1196  * running on the given host.
 1197  */
 1198 CLIENT *
 1199 nlm_host_get_rpc(struct nlm_host *host, bool_t isserver)
 1200 {
 1201         struct nlm_rpc *rpc;
 1202         CLIENT *client;
 1203 
 1204         mtx_lock(&host->nh_lock);
 1205 
 1206         if (isserver)
 1207                 rpc = &host->nh_srvrpc;
 1208         else
 1209                 rpc = &host->nh_clntrpc;
 1210 
 1211         /*
 1212          * We can't hold onto RPC handles for too long - the async
 1213          * call/reply protocol used by some NLM clients makes it hard
 1214          * to tell when they change port numbers (e.g. after a
 1215          * reboot). Note that if a client reboots while it isn't
 1216          * holding any locks, it won't bother to notify us. We
 1217          * expire the RPC handles after two minutes.
 1218          */
 1219         if (rpc->nr_client && time_uptime > rpc->nr_create_time + 2*60) {
 1220                 client = rpc->nr_client;
 1221                 rpc->nr_client = NULL;
 1222                 mtx_unlock(&host->nh_lock);
 1223                 CLNT_RELEASE(client);
 1224                 mtx_lock(&host->nh_lock);
 1225         }
 1226 
 1227         if (!rpc->nr_client) {
 1228                 mtx_unlock(&host->nh_lock);
 1229                 client = nlm_get_rpc((struct sockaddr *)&host->nh_addr,
 1230                     NLM_PROG, host->nh_vers);
 1231                 mtx_lock(&host->nh_lock);
 1232 
 1233                 if (client) {
 1234                         if (rpc->nr_client) {
 1235                                 mtx_unlock(&host->nh_lock);
 1236                                 CLNT_DESTROY(client);
 1237                                 mtx_lock(&host->nh_lock);
 1238                         } else {
 1239                                 rpc->nr_client = client;
 1240                                 rpc->nr_create_time = time_uptime;
 1241                         }
 1242                 }
 1243         }
 1244 
 1245         client = rpc->nr_client;
 1246         if (client)
 1247                 CLNT_ACQUIRE(client);
 1248         mtx_unlock(&host->nh_lock);
 1249 
 1250         return (client);
 1251 
 1252 }
 1253 
 1254 int nlm_host_get_sysid(struct nlm_host *host)
 1255 {
 1256 
 1257         return (host->nh_sysid);
 1258 }
 1259 
 1260 int
 1261 nlm_host_get_state(struct nlm_host *host)
 1262 {
 1263 
 1264         return (host->nh_state);
 1265 }
 1266 
 1267 void *
 1268 nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp)
 1269 {
 1270         struct nlm_waiting_lock *nw;
 1271 
 1272         nw = malloc(sizeof(struct nlm_waiting_lock), M_NLM, M_WAITOK);
 1273         nw->nw_lock = *lock;
 1274         memcpy(&nw->nw_fh.fh_bytes, nw->nw_lock.fh.n_bytes,
 1275             nw->nw_lock.fh.n_len);
 1276         nw->nw_lock.fh.n_bytes = nw->nw_fh.fh_bytes;
 1277         nw->nw_waiting = TRUE;
 1278         nw->nw_vp = vp;
 1279         mtx_lock(&nlm_global_lock);
 1280         TAILQ_INSERT_TAIL(&nlm_waiting_locks, nw, nw_link);
 1281         mtx_unlock(&nlm_global_lock);
 1282 
 1283         return nw;
 1284 }
 1285 
 1286 void
 1287 nlm_deregister_wait_lock(void *handle)
 1288 {
 1289         struct nlm_waiting_lock *nw = handle;
 1290 
 1291         mtx_lock(&nlm_global_lock);
 1292         TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
 1293         mtx_unlock(&nlm_global_lock);
 1294         
 1295         free(nw, M_NLM);
 1296 }
 1297 
 1298 int
 1299 nlm_wait_lock(void *handle, int timo)
 1300 {
 1301         struct nlm_waiting_lock *nw = handle;
 1302         int error;
 1303 
 1304         /*
 1305          * If the granted message arrived before we got here,
 1306          * nw->nw_waiting will be FALSE - in that case, don't sleep.
 1307          */
 1308         mtx_lock(&nlm_global_lock);
 1309         error = 0;
 1310         if (nw->nw_waiting)
 1311                 error = msleep(nw, &nlm_global_lock, PCATCH, "nlmlock", timo);
 1312         TAILQ_REMOVE(&nlm_waiting_locks, nw, nw_link);
 1313         if (error) {
 1314                 /*
 1315                  * The granted message may arrive after the
 1316                  * interrupt/timeout but before we manage to lock the
 1317                  * mutex. Detect this by examining nw_lock.
 1318                  */
 1319                 if (!nw->nw_waiting)
 1320                         error = 0;
 1321         } else {
 1322                 /*
 1323                  * If nlm_cancel_wait is called, then error will be
 1324                  * zero but nw_waiting will still be TRUE. We
 1325                  * translate this into EINTR.
 1326                  */
 1327                 if (nw->nw_waiting)
 1328                         error = EINTR;
 1329         }
 1330         mtx_unlock(&nlm_global_lock);
 1331 
 1332         free(nw, M_NLM);
 1333 
 1334         return (error);
 1335 }
 1336 
 1337 void
 1338 nlm_cancel_wait(struct vnode *vp)
 1339 {
 1340         struct nlm_waiting_lock *nw;
 1341 
 1342         mtx_lock(&nlm_global_lock);
 1343         TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
 1344                 if (nw->nw_vp == vp) {
 1345                         wakeup(nw);
 1346                 }
 1347         }
 1348         mtx_unlock(&nlm_global_lock);
 1349 }
 1350 
 1351 
 1352 /**********************************************************************/
 1353 
 1354 /*
 1355  * Syscall interface with userland.
 1356  */
 1357 
 1358 extern void nlm_prog_0(struct svc_req *rqstp, SVCXPRT *transp);
 1359 extern void nlm_prog_1(struct svc_req *rqstp, SVCXPRT *transp);
 1360 extern void nlm_prog_3(struct svc_req *rqstp, SVCXPRT *transp);
 1361 extern void nlm_prog_4(struct svc_req *rqstp, SVCXPRT *transp);
 1362 
 1363 static int
 1364 nlm_register_services(SVCPOOL *pool, int addr_count, char **addrs)
 1365 {
 1366         static rpcvers_t versions[] = {
 1367                 NLM_SM, NLM_VERS, NLM_VERSX, NLM_VERS4
 1368         };
 1369         static void (*dispatchers[])(struct svc_req *, SVCXPRT *) = {
 1370                 nlm_prog_0, nlm_prog_1, nlm_prog_3, nlm_prog_4
 1371         };
 1372         static const int version_count = sizeof(versions) / sizeof(versions[0]);
 1373 
 1374         SVCXPRT **xprts;
 1375         char netid[16];
 1376         char uaddr[128];
 1377         struct netconfig *nconf;
 1378         int i, j, error;
 1379 
 1380         if (!addr_count) {
 1381                 NLM_ERR("NLM: no service addresses given - can't start server");
 1382                 return (EINVAL);
 1383         }
 1384 
 1385         xprts = malloc(addr_count * sizeof(SVCXPRT *), M_NLM, M_WAITOK|M_ZERO);
 1386         for (i = 0; i < version_count; i++) {
 1387                 for (j = 0; j < addr_count; j++) {
 1388                         /*
 1389                          * Create transports for the first version and
 1390                          * then just register everything else to the
 1391                          * same transports.
 1392                          */
 1393                         if (i == 0) {
 1394                                 char *up;
 1395 
 1396                                 error = copyin(&addrs[2*j], &up,
 1397                                     sizeof(char*));
 1398                                 if (error)
 1399                                         goto out;
 1400                                 error = copyinstr(up, netid, sizeof(netid),
 1401                                     NULL);
 1402                                 if (error)
 1403                                         goto out;
 1404                                 error = copyin(&addrs[2*j+1], &up,
 1405                                     sizeof(char*));
 1406                                 if (error)
 1407                                         goto out;
 1408                                 error = copyinstr(up, uaddr, sizeof(uaddr),
 1409                                     NULL);
 1410                                 if (error)
 1411                                         goto out;
 1412                                 nconf = getnetconfigent(netid);
 1413                                 if (!nconf) {
 1414                                         NLM_ERR("Can't lookup netid %s\n",
 1415                                             netid);
 1416                                         error = EINVAL;
 1417                                         goto out;
 1418                                 }
 1419                                 xprts[j] = svc_tp_create(pool, dispatchers[i],
 1420                                     NLM_PROG, versions[i], uaddr, nconf);
 1421                                 if (!xprts[j]) {
 1422                                         NLM_ERR("NLM: unable to create "
 1423                                             "(NLM_PROG, %d).\n", versions[i]);
 1424                                         error = EINVAL;
 1425                                         goto out;
 1426                                 }
 1427                                 freenetconfigent(nconf);
 1428                         } else {
 1429                                 nconf = getnetconfigent(xprts[j]->xp_netid);
 1430                                 rpcb_unset(NLM_PROG, versions[i], nconf);
 1431                                 if (!svc_reg(xprts[j], NLM_PROG, versions[i],
 1432                                         dispatchers[i], nconf)) {
 1433                                         NLM_ERR("NLM: can't register "
 1434                                             "(NLM_PROG, %d)\n", versions[i]);
 1435                                         error = EINVAL;
 1436                                         goto out;
 1437                                 }
 1438                         }
 1439                 }
 1440         }
 1441         error = 0;
 1442 out:
 1443         for (j = 0; j < addr_count; j++) {
 1444                 if (xprts[j])
 1445                         SVC_RELEASE(xprts[j]);
 1446         }
 1447         free(xprts, M_NLM);
 1448         return (error);
 1449 }
 1450 
 1451 /*
 1452  * Main server entry point. Contacts the local NSM to get its current
 1453  * state and send SM_UNMON_ALL. Registers the NLM services and then
 1454  * services requests. Does not return until the server is interrupted
 1455  * by a signal.
 1456  */
 1457 static int
 1458 nlm_server_main(int addr_count, char **addrs)
 1459 {
 1460         struct thread *td = curthread;
 1461         int error;
 1462         SVCPOOL *pool = NULL;
 1463         struct sockopt opt;
 1464         int portlow;
 1465 #ifdef INET6
 1466         struct sockaddr_in6 sin6;
 1467 #endif
 1468         struct sockaddr_in sin;
 1469         my_id id;
 1470         sm_stat smstat;
 1471         struct timeval timo;
 1472         enum clnt_stat stat;
 1473         struct nlm_host *host, *nhost;
 1474         struct nlm_waiting_lock *nw;
 1475         vop_advlock_t *old_nfs_advlock;
 1476         vop_reclaim_t *old_nfs_reclaim;
 1477         int v4_used;
 1478 #ifdef INET6
 1479         int v6_used;
 1480 #endif
 1481 
 1482         if (nlm_socket) {
 1483                 NLM_ERR("NLM: can't start server - "
 1484                     "it appears to be running already\n");
 1485                 return (EPERM);
 1486         }
 1487 
 1488         memset(&opt, 0, sizeof(opt));
 1489 
 1490         nlm_socket = NULL;
 1491         error = socreate(AF_INET, &nlm_socket, SOCK_DGRAM, 0,
 1492             td->td_ucred, td);
 1493         if (error) {
 1494                 NLM_ERR("NLM: can't create IPv4 socket - error %d\n", error);
 1495                 return (error);
 1496         }
 1497         opt.sopt_dir = SOPT_SET;
 1498         opt.sopt_level = IPPROTO_IP;
 1499         opt.sopt_name = IP_PORTRANGE;
 1500         portlow = IP_PORTRANGE_LOW;
 1501         opt.sopt_val = &portlow;
 1502         opt.sopt_valsize = sizeof(portlow);
 1503         sosetopt(nlm_socket, &opt);
 1504 
 1505 #ifdef INET6
 1506         nlm_socket6 = NULL;
 1507         error = socreate(AF_INET6, &nlm_socket6, SOCK_DGRAM, 0,
 1508             td->td_ucred, td);
 1509         if (error) {
 1510                 NLM_ERR("NLM: can't create IPv6 socket - error %d\n", error);
 1511                 goto out;
 1512                 return (error);
 1513         }
 1514         opt.sopt_dir = SOPT_SET;
 1515         opt.sopt_level = IPPROTO_IPV6;
 1516         opt.sopt_name = IPV6_PORTRANGE;
 1517         portlow = IPV6_PORTRANGE_LOW;
 1518         opt.sopt_val = &portlow;
 1519         opt.sopt_valsize = sizeof(portlow);
 1520         sosetopt(nlm_socket6, &opt);
 1521 #endif
 1522 
 1523         nlm_auth = authunix_create(curthread->td_ucred);
 1524 
 1525 #ifdef INET6
 1526         memset(&sin6, 0, sizeof(sin6));
 1527         sin6.sin6_len = sizeof(sin6);
 1528         sin6.sin6_family = AF_INET6;
 1529         sin6.sin6_addr = in6addr_loopback;
 1530         nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin6, SM_PROG, SM_VERS);
 1531         if (!nlm_nsm) {
 1532 #endif
 1533                 memset(&sin, 0, sizeof(sin));
 1534                 sin.sin_len = sizeof(sin);
 1535                 sin.sin_family = AF_INET;
 1536                 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 1537                 nlm_nsm = nlm_get_rpc((struct sockaddr *) &sin, SM_PROG,
 1538                     SM_VERS);
 1539 #ifdef INET6
 1540         }
 1541 #endif
 1542 
 1543         if (!nlm_nsm) {
 1544                 NLM_ERR("Can't start NLM - unable to contact NSM\n");
 1545                 error = EINVAL;
 1546                 goto out;
 1547         }
 1548 
 1549         pool = svcpool_create("NLM", NULL);
 1550 
 1551         error = nlm_register_services(pool, addr_count, addrs);
 1552         if (error)
 1553                 goto out;
 1554 
 1555         memset(&id, 0, sizeof(id));
 1556         id.my_name = "NFS NLM";
 1557 
 1558         timo.tv_sec = 25;
 1559         timo.tv_usec = 0;
 1560         stat = CLNT_CALL(nlm_nsm, SM_UNMON_ALL,
 1561             (xdrproc_t) xdr_my_id, &id,
 1562             (xdrproc_t) xdr_sm_stat, &smstat, timo);
 1563 
 1564         if (stat != RPC_SUCCESS) {
 1565                 struct rpc_err err;
 1566 
 1567                 CLNT_GETERR(nlm_nsm, &err);
 1568                 NLM_ERR("NLM: unexpected error contacting NSM, "
 1569                     "stat=%d, errno=%d\n", stat, err.re_errno);
 1570                 error = EINVAL;
 1571                 goto out;
 1572         }
 1573 
 1574         NLM_DEBUG(1, "NLM: local NSM state is %d\n", smstat.state);
 1575         nlm_nsm_state = smstat.state;
 1576 
 1577         old_nfs_advlock = nfs_advlock_p;
 1578         nfs_advlock_p = nlm_advlock;
 1579         old_nfs_reclaim = nfs_reclaim_p;
 1580         nfs_reclaim_p = nlm_reclaim;
 1581 
 1582         svc_run(pool);
 1583         error = 0;
 1584 
 1585         nfs_advlock_p = old_nfs_advlock;
 1586         nfs_reclaim_p = old_nfs_reclaim;
 1587 
 1588 out:
 1589         if (pool)
 1590                 svcpool_destroy(pool);
 1591 
 1592         /*
 1593          * We are finished communicating with the NSM.
 1594          */
 1595         if (nlm_nsm) {
 1596                 CLNT_RELEASE(nlm_nsm);
 1597                 nlm_nsm = NULL;
 1598         }
 1599 
 1600         /*
 1601          * Trash all the existing state so that if the server
 1602          * restarts, it gets a clean slate. This is complicated by the
 1603          * possibility that there may be other threads trying to make
 1604          * client locking requests.
 1605          *
 1606          * First we fake a client reboot notification which will
 1607          * cancel any pending async locks and purge remote lock state
 1608          * from the local lock manager. We release the reference from
 1609          * nlm_hosts to the host (which may remove it from the list
 1610          * and free it). After this phase, the only entries in the
 1611          * nlm_host list should be from other threads performing
 1612          * client lock requests. We arrange to defer closing the
 1613          * sockets until the last RPC client handle is released.
 1614          */
 1615         v4_used = 0;
 1616 #ifdef INET6
 1617         v6_used = 0;
 1618 #endif
 1619         mtx_lock(&nlm_global_lock);
 1620         TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
 1621                 wakeup(nw);
 1622         }
 1623         TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
 1624                 mtx_unlock(&nlm_global_lock);
 1625                 nlm_host_notify(host, 0);
 1626                 nlm_host_release(host);
 1627                 mtx_lock(&nlm_global_lock);
 1628         }
 1629         TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, nhost) {
 1630                 mtx_lock(&host->nh_lock);
 1631                 if (host->nh_srvrpc.nr_client
 1632                     || host->nh_clntrpc.nr_client) {
 1633                         if (host->nh_addr.ss_family == AF_INET)
 1634                                 v4_used++;
 1635 #ifdef INET6
 1636                         if (host->nh_addr.ss_family == AF_INET6)
 1637                                 v6_used++;
 1638 #endif
 1639                         /*
 1640                          * Note that the rpc over udp code copes
 1641                          * correctly with the fact that a socket may
 1642                          * be used by many rpc handles.
 1643                          */
 1644                         if (host->nh_srvrpc.nr_client)
 1645                                 CLNT_CONTROL(host->nh_srvrpc.nr_client,
 1646                                     CLSET_FD_CLOSE, 0);
 1647                         if (host->nh_clntrpc.nr_client)
 1648                                 CLNT_CONTROL(host->nh_clntrpc.nr_client,
 1649                                     CLSET_FD_CLOSE, 0);
 1650                 }
 1651                 mtx_unlock(&host->nh_lock);
 1652         }
 1653         mtx_unlock(&nlm_global_lock);
 1654 
 1655         AUTH_DESTROY(nlm_auth);
 1656 
 1657         if (!v4_used)
 1658                 soclose(nlm_socket);
 1659         nlm_socket = NULL;
 1660 #ifdef INET6
 1661         if (!v6_used)
 1662                 soclose(nlm_socket6);
 1663         nlm_socket6 = NULL;
 1664 #endif
 1665 
 1666         return (error);
 1667 }
 1668 
 1669 int
 1670 nlm_syscall(struct thread *td, struct nlm_syscall_args *uap)
 1671 {
 1672         int error;
 1673 
 1674 #if __FreeBSD_version >= 700000
 1675         error = priv_check(td, PRIV_NFS_LOCKD);
 1676 #else
 1677         error = suser(td);
 1678 #endif
 1679         if (error)
 1680                 return (error);
 1681 
 1682         nlm_debug_level = uap->debug_level;
 1683         nlm_grace_threshold = time_uptime + uap->grace_period;
 1684         nlm_next_idle_check = time_uptime + NLM_IDLE_PERIOD;
 1685 
 1686         return nlm_server_main(uap->addr_count, uap->addrs);
 1687 }
 1688 
 1689 /**********************************************************************/
 1690 
 1691 /*
 1692  * NLM implementation details, called from the RPC stubs.
 1693  */
 1694 
 1695 
 1696 void
 1697 nlm_sm_notify(struct nlm_sm_status *argp)
 1698 {
 1699         uint32_t sysid;
 1700         struct nlm_host *host;
 1701 
 1702         NLM_DEBUG(3, "nlm_sm_notify(): mon_name = %s\n", argp->mon_name);
 1703         memcpy(&sysid, &argp->priv, sizeof(sysid));
 1704         host = nlm_find_host_by_sysid(sysid);
 1705         if (host) {
 1706                 nlm_host_notify(host, argp->state);
 1707                 nlm_host_release(host);
 1708         }
 1709 }
 1710 
 1711 static void
 1712 nlm_convert_to_fhandle_t(fhandle_t *fhp, struct netobj *p)
 1713 {
 1714         memcpy(fhp, p->n_bytes, sizeof(fhandle_t));
 1715 }
 1716 
 1717 struct vfs_state {
 1718         struct mount    *vs_mp;
 1719         struct vnode    *vs_vp;
 1720         int             vs_vfslocked;
 1721         int             vs_vnlocked;
 1722 };
 1723 
 1724 static int
 1725 nlm_get_vfs_state(struct nlm_host *host, struct svc_req *rqstp,
 1726     fhandle_t *fhp, struct vfs_state *vs)
 1727 {
 1728         int error, exflags;
 1729         struct ucred *cred = NULL, *credanon;
 1730         
 1731         memset(vs, 0, sizeof(*vs));
 1732 
 1733         vs->vs_mp = vfs_getvfs(&fhp->fh_fsid);
 1734         if (!vs->vs_mp) {
 1735                 return (ESTALE);
 1736         }
 1737         vs->vs_vfslocked = VFS_LOCK_GIANT(vs->vs_mp);
 1738 
 1739         error = VFS_CHECKEXP(vs->vs_mp, (struct sockaddr *)&host->nh_addr,
 1740             &exflags, &credanon, NULL, NULL);
 1741         if (error)
 1742                 goto out;
 1743 
 1744         if (exflags & MNT_EXRDONLY || (vs->vs_mp->mnt_flag & MNT_RDONLY)) {
 1745                 error = EROFS;
 1746                 goto out;
 1747         }
 1748 
 1749         error = VFS_FHTOVP(vs->vs_mp, &fhp->fh_fid, &vs->vs_vp);
 1750         if (error)
 1751                 goto out;
 1752         vs->vs_vnlocked = TRUE;
 1753 
 1754         if (!svc_getcred(rqstp, &cred, NULL)) {
 1755                 error = EINVAL;
 1756                 goto out;
 1757         }
 1758         if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
 1759                 crfree(cred);
 1760                 cred = credanon;
 1761                 credanon = NULL;
 1762         }
 1763 
 1764         /*
 1765          * Check cred.
 1766          */
 1767         error = VOP_ACCESS(vs->vs_vp, VWRITE, cred, curthread);
 1768         if (error)
 1769                 goto out;
 1770 
 1771 #if __FreeBSD_version < 800011
 1772         VOP_UNLOCK(vs->vs_vp, 0, curthread);
 1773 #else
 1774         VOP_UNLOCK(vs->vs_vp, 0);
 1775 #endif
 1776         vs->vs_vnlocked = FALSE;
 1777 
 1778 out:
 1779         if (cred)
 1780                 crfree(cred);
 1781         if (credanon)
 1782                 crfree(credanon);
 1783 
 1784         return (error);
 1785 }
 1786 
 1787 static void
 1788 nlm_release_vfs_state(struct vfs_state *vs)
 1789 {
 1790 
 1791         if (vs->vs_vp) {
 1792                 if (vs->vs_vnlocked)
 1793                         vput(vs->vs_vp);
 1794                 else
 1795                         vrele(vs->vs_vp);
 1796         }
 1797         if (vs->vs_mp)
 1798                 vfs_rel(vs->vs_mp);
 1799         VFS_UNLOCK_GIANT(vs->vs_vfslocked);
 1800 }
 1801 
 1802 static nlm4_stats
 1803 nlm_convert_error(int error)
 1804 {
 1805 
 1806         if (error == ESTALE)
 1807                 return nlm4_stale_fh;
 1808         else if (error == EROFS)
 1809                 return nlm4_rofs;
 1810         else
 1811                 return nlm4_failed;
 1812 }
 1813 
 1814 int
 1815 nlm_do_test(nlm4_testargs *argp, nlm4_testres *result, struct svc_req *rqstp,
 1816         CLIENT **rpcp)
 1817 {
 1818         fhandle_t fh;
 1819         struct vfs_state vs;
 1820         struct nlm_host *host, *bhost;
 1821         int error, sysid;
 1822         struct flock fl;
 1823         
 1824         memset(result, 0, sizeof(*result));
 1825         memset(&vs, 0, sizeof(vs));
 1826 
 1827         host = nlm_find_host_by_name(argp->alock.caller_name,
 1828             svc_getrpccaller(rqstp), rqstp->rq_vers);
 1829         if (!host) {
 1830                 result->stat.stat = nlm4_denied_nolocks;
 1831                 return (ENOMEM);
 1832         }
 1833 
 1834         NLM_DEBUG(3, "nlm_do_test(): caller_name = %s (sysid = %d)\n",
 1835             host->nh_caller_name, host->nh_sysid);
 1836 
 1837         nlm_free_finished_locks(host);
 1838         sysid = host->nh_sysid;
 1839 
 1840         nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
 1841         nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
 1842 
 1843         if (time_uptime < nlm_grace_threshold) {
 1844                 result->stat.stat = nlm4_denied_grace_period;
 1845                 goto out;
 1846         }
 1847 
 1848         error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
 1849         if (error) {
 1850                 result->stat.stat = nlm_convert_error(error);
 1851                 goto out;
 1852         }
 1853 
 1854         fl.l_start = argp->alock.l_offset;
 1855         fl.l_len = argp->alock.l_len;
 1856         fl.l_pid = argp->alock.svid;
 1857         fl.l_sysid = sysid;
 1858         fl.l_whence = SEEK_SET;
 1859         if (argp->exclusive)
 1860                 fl.l_type = F_WRLCK;
 1861         else
 1862                 fl.l_type = F_RDLCK;
 1863         error = VOP_ADVLOCK(vs.vs_vp, NULL, F_GETLK, &fl, F_REMOTE);
 1864         if (error) {
 1865                 result->stat.stat = nlm4_failed;
 1866                 goto out;
 1867         }
 1868 
 1869         if (fl.l_type == F_UNLCK) {
 1870                 result->stat.stat = nlm4_granted;
 1871         } else {
 1872                 result->stat.stat = nlm4_denied;
 1873                 result->stat.nlm4_testrply_u.holder.exclusive =
 1874                         (fl.l_type == F_WRLCK);
 1875                 result->stat.nlm4_testrply_u.holder.svid = fl.l_pid;
 1876                 bhost = nlm_find_host_by_sysid(fl.l_sysid);
 1877                 if (bhost) {
 1878                         /*
 1879                          * We don't have any useful way of recording
 1880                          * the value of oh used in the original lock
 1881                          * request. Ideally, the test reply would have
 1882                          * a space for the owning host's name allowing
 1883                          * our caller's NLM to keep track.
 1884                          *
 1885                          * As far as I can see, Solaris uses an eight
 1886                          * byte structure for oh which contains a four
 1887                          * byte pid encoded in local byte order and
 1888                          * the first four bytes of the host
 1889                          * name. Linux uses a variable length string
 1890                          * 'pid@hostname' in ascii but doesn't even
 1891                          * return that in test replies.
 1892                          *
 1893                          * For the moment, return nothing in oh
 1894                          * (already zero'ed above).
 1895                          */
 1896                         nlm_host_release(bhost);
 1897                 }
 1898                 result->stat.nlm4_testrply_u.holder.l_offset = fl.l_start;
 1899                 result->stat.nlm4_testrply_u.holder.l_len = fl.l_len;
 1900         }
 1901 
 1902 out:
 1903         nlm_release_vfs_state(&vs);
 1904         if (rpcp)
 1905                 *rpcp = nlm_host_get_rpc(host, TRUE);
 1906         nlm_host_release(host);
 1907         return (0);
 1908 }
 1909 
 1910 int
 1911 nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result, struct svc_req *rqstp,
 1912     bool_t monitor, CLIENT **rpcp)
 1913 {
 1914         fhandle_t fh;
 1915         struct vfs_state vs;
 1916         struct nlm_host *host;
 1917         int error, sysid;
 1918         struct flock fl;
 1919         
 1920         memset(result, 0, sizeof(*result));
 1921         memset(&vs, 0, sizeof(vs));
 1922 
 1923         host = nlm_find_host_by_name(argp->alock.caller_name,
 1924             svc_getrpccaller(rqstp), rqstp->rq_vers);
 1925         if (!host) {
 1926                 result->stat.stat = nlm4_denied_nolocks;
 1927                 return (ENOMEM);
 1928         }
 1929 
 1930         NLM_DEBUG(3, "nlm_do_lock(): caller_name = %s (sysid = %d)\n",
 1931             host->nh_caller_name, host->nh_sysid);
 1932 
 1933         if (monitor && host->nh_state && argp->state
 1934             && host->nh_state != argp->state) {
 1935                 /*
 1936                  * The host rebooted without telling us. Trash its
 1937                  * locks.
 1938                  */
 1939                 nlm_host_notify(host, argp->state);
 1940         }
 1941 
 1942         nlm_free_finished_locks(host);
 1943         sysid = host->nh_sysid;
 1944 
 1945         nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
 1946         nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
 1947 
 1948         if (time_uptime < nlm_grace_threshold && !argp->reclaim) {
 1949                 result->stat.stat = nlm4_denied_grace_period;
 1950                 goto out;
 1951         }
 1952 
 1953         error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
 1954         if (error) {
 1955                 result->stat.stat = nlm_convert_error(error);
 1956                 goto out;
 1957         }
 1958 
 1959         fl.l_start = argp->alock.l_offset;
 1960         fl.l_len = argp->alock.l_len;
 1961         fl.l_pid = argp->alock.svid;
 1962         fl.l_sysid = sysid;
 1963         fl.l_whence = SEEK_SET;
 1964         if (argp->exclusive)
 1965                 fl.l_type = F_WRLCK;
 1966         else
 1967                 fl.l_type = F_RDLCK;
 1968         if (argp->block) {
 1969                 struct nlm_async_lock *af;
 1970                 CLIENT *client;
 1971 
 1972                 /*
 1973                  * First, make sure we can contact the host's NLM.
 1974                  */
 1975                 client = nlm_host_get_rpc(host, TRUE);
 1976                 if (!client) {
 1977                         result->stat.stat = nlm4_failed;
 1978                         goto out;
 1979                 }
 1980 
 1981                 /*
 1982                  * First we need to check and see if there is an
 1983                  * existing blocked lock that matches. This could be a
 1984                  * badly behaved client or an RPC re-send. If we find
 1985                  * one, just return nlm4_blocked.
 1986                  */
 1987                 mtx_lock(&host->nh_lock);
 1988                 TAILQ_FOREACH(af, &host->nh_pending, af_link) {
 1989                         if (af->af_fl.l_start == fl.l_start
 1990                             && af->af_fl.l_len == fl.l_len
 1991                             && af->af_fl.l_pid == fl.l_pid
 1992                             && af->af_fl.l_type == fl.l_type) {
 1993                                 break;
 1994                         }
 1995                 }
 1996                 mtx_unlock(&host->nh_lock);
 1997                 if (af) {
 1998                         CLNT_RELEASE(client);
 1999                         result->stat.stat = nlm4_blocked;
 2000                         goto out;
 2001                 }
 2002 
 2003                 af = malloc(sizeof(struct nlm_async_lock), M_NLM,
 2004                     M_WAITOK|M_ZERO);
 2005                 TASK_INIT(&af->af_task, 0, nlm_lock_callback, af);
 2006                 af->af_vp = vs.vs_vp;
 2007                 af->af_fl = fl;
 2008                 af->af_host = host;
 2009                 af->af_rpc = client;
 2010                 /*
 2011                  * We use M_RPC here so that we can xdr_free the thing
 2012                  * later.
 2013                  */
 2014                 af->af_granted.exclusive = argp->exclusive;
 2015                 af->af_granted.alock.caller_name =
 2016                         strdup(argp->alock.caller_name, M_RPC);
 2017                 nlm_copy_netobj(&af->af_granted.alock.fh,
 2018                     &argp->alock.fh, M_RPC);
 2019                 nlm_copy_netobj(&af->af_granted.alock.oh,
 2020                     &argp->alock.oh, M_RPC);
 2021                 af->af_granted.alock.svid = argp->alock.svid;
 2022                 af->af_granted.alock.l_offset = argp->alock.l_offset;
 2023                 af->af_granted.alock.l_len = argp->alock.l_len;
 2024 
 2025                 /*
 2026                  * Put the entry on the pending list before calling
 2027                  * VOP_ADVLOCKASYNC. We do this in case the lock
 2028                  * request was blocked (returning EINPROGRESS) but
 2029                  * then granted before we manage to run again. The
 2030                  * client may receive the granted message before we
 2031                  * send our blocked reply but thats their problem.
 2032                  */
 2033                 mtx_lock(&host->nh_lock);
 2034                 TAILQ_INSERT_TAIL(&host->nh_pending, af, af_link);
 2035                 mtx_unlock(&host->nh_lock);
 2036 
 2037                 error = VOP_ADVLOCKASYNC(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE,
 2038                     &af->af_task, &af->af_cookie);
 2039 
 2040                 /*
 2041                  * If the lock completed synchronously, just free the
 2042                  * tracking structure now.
 2043                  */
 2044                 if (error != EINPROGRESS) {
 2045                         CLNT_RELEASE(af->af_rpc);
 2046                         mtx_lock(&host->nh_lock);
 2047                         TAILQ_REMOVE(&host->nh_pending, af, af_link);
 2048                         mtx_unlock(&host->nh_lock);
 2049                         xdr_free((xdrproc_t) xdr_nlm4_testargs,
 2050                             &af->af_granted);
 2051                         free(af, M_NLM);
 2052                 } else {
 2053                         NLM_DEBUG(2, "NLM: pending async lock %p for %s "
 2054                             "(sysid %d)\n", af, host->nh_caller_name, sysid);
 2055                         /*
 2056                          * Don't vrele the vnode just yet - this must
 2057                          * wait until either the async callback
 2058                          * happens or the lock is cancelled.
 2059                          */
 2060                         vs.vs_vp = NULL;
 2061                 }
 2062         } else {
 2063                 error = VOP_ADVLOCK(vs.vs_vp, NULL, F_SETLK, &fl, F_REMOTE);
 2064         }
 2065 
 2066         if (error) {
 2067                 if (error == EINPROGRESS) {
 2068                         result->stat.stat = nlm4_blocked;
 2069                 } else if (error == EDEADLK) {
 2070                         result->stat.stat = nlm4_deadlck;
 2071                 } else if (error == EAGAIN) {
 2072                         result->stat.stat = nlm4_denied;
 2073                 } else {
 2074                         result->stat.stat = nlm4_failed;
 2075                 }
 2076         } else {
 2077                 if (monitor)
 2078                         nlm_host_monitor(host, argp->state);
 2079                 result->stat.stat = nlm4_granted;
 2080         }       
 2081 
 2082 out:
 2083         nlm_release_vfs_state(&vs);
 2084         if (rpcp)
 2085                 *rpcp = nlm_host_get_rpc(host, TRUE);
 2086         nlm_host_release(host);
 2087         return (0);
 2088 }
 2089 
 2090 int
 2091 nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result, struct svc_req *rqstp,
 2092     CLIENT **rpcp)
 2093 {
 2094         fhandle_t fh;
 2095         struct vfs_state vs;
 2096         struct nlm_host *host;
 2097         int error, sysid;
 2098         struct flock fl;
 2099         struct nlm_async_lock *af;
 2100         
 2101         memset(result, 0, sizeof(*result));
 2102         memset(&vs, 0, sizeof(vs));
 2103 
 2104         host = nlm_find_host_by_name(argp->alock.caller_name,
 2105             svc_getrpccaller(rqstp), rqstp->rq_vers);
 2106         if (!host) {
 2107                 result->stat.stat = nlm4_denied_nolocks;
 2108                 return (ENOMEM);
 2109         }
 2110 
 2111         NLM_DEBUG(3, "nlm_do_cancel(): caller_name = %s (sysid = %d)\n",
 2112             host->nh_caller_name, host->nh_sysid);
 2113 
 2114         nlm_free_finished_locks(host);
 2115         sysid = host->nh_sysid;
 2116 
 2117         nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
 2118         nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
 2119 
 2120         if (time_uptime < nlm_grace_threshold) {
 2121                 result->stat.stat = nlm4_denied_grace_period;
 2122                 goto out;
 2123         }
 2124 
 2125         error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
 2126         if (error) {
 2127                 result->stat.stat = nlm_convert_error(error);
 2128                 goto out;
 2129         }
 2130 
 2131         fl.l_start = argp->alock.l_offset;
 2132         fl.l_len = argp->alock.l_len;
 2133         fl.l_pid = argp->alock.svid;
 2134         fl.l_sysid = sysid;
 2135         fl.l_whence = SEEK_SET;
 2136         if (argp->exclusive)
 2137                 fl.l_type = F_WRLCK;
 2138         else
 2139                 fl.l_type = F_RDLCK;
 2140 
 2141         /*
 2142          * First we need to try and find the async lock request - if
 2143          * there isn't one, we give up and return nlm4_denied.
 2144          */
 2145         mtx_lock(&host->nh_lock);
 2146 
 2147         TAILQ_FOREACH(af, &host->nh_pending, af_link) {
 2148                 if (af->af_fl.l_start == fl.l_start
 2149                     && af->af_fl.l_len == fl.l_len
 2150                     && af->af_fl.l_pid == fl.l_pid
 2151                     && af->af_fl.l_type == fl.l_type) {
 2152                         break;
 2153                 }
 2154         }
 2155 
 2156         if (!af) {
 2157                 mtx_unlock(&host->nh_lock);
 2158                 result->stat.stat = nlm4_denied;
 2159                 goto out;
 2160         }
 2161 
 2162         error = nlm_cancel_async_lock(af);
 2163 
 2164         if (error) {
 2165                 result->stat.stat = nlm4_denied;
 2166         } else {
 2167                 result->stat.stat = nlm4_granted;
 2168         }
 2169 
 2170         mtx_unlock(&host->nh_lock);
 2171 
 2172 out:
 2173         nlm_release_vfs_state(&vs);
 2174         if (rpcp)
 2175                 *rpcp = nlm_host_get_rpc(host, TRUE);
 2176         nlm_host_release(host);
 2177         return (0);
 2178 }
 2179 
 2180 int
 2181 nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result, struct svc_req *rqstp,
 2182     CLIENT **rpcp)
 2183 {
 2184         fhandle_t fh;
 2185         struct vfs_state vs;
 2186         struct nlm_host *host;
 2187         int error, sysid;
 2188         struct flock fl;
 2189         
 2190         memset(result, 0, sizeof(*result));
 2191         memset(&vs, 0, sizeof(vs));
 2192 
 2193         host = nlm_find_host_by_name(argp->alock.caller_name,
 2194             svc_getrpccaller(rqstp), rqstp->rq_vers);
 2195         if (!host) {
 2196                 result->stat.stat = nlm4_denied_nolocks;
 2197                 return (ENOMEM);
 2198         }
 2199 
 2200         NLM_DEBUG(3, "nlm_do_unlock(): caller_name = %s (sysid = %d)\n",
 2201             host->nh_caller_name, host->nh_sysid);
 2202 
 2203         nlm_free_finished_locks(host);
 2204         sysid = host->nh_sysid;
 2205 
 2206         nlm_convert_to_fhandle_t(&fh, &argp->alock.fh);
 2207         nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
 2208 
 2209         if (time_uptime < nlm_grace_threshold) {
 2210                 result->stat.stat = nlm4_denied_grace_period;
 2211                 goto out;
 2212         }
 2213 
 2214         error = nlm_get_vfs_state(host, rqstp, &fh, &vs);
 2215         if (error) {
 2216                 result->stat.stat = nlm_convert_error(error);
 2217                 goto out;
 2218         }
 2219 
 2220         fl.l_start = argp->alock.l_offset;
 2221         fl.l_len = argp->alock.l_len;
 2222         fl.l_pid = argp->alock.svid;
 2223         fl.l_sysid = sysid;
 2224         fl.l_whence = SEEK_SET;
 2225         fl.l_type = F_UNLCK;
 2226         error = VOP_ADVLOCK(vs.vs_vp, NULL, F_UNLCK, &fl, F_REMOTE);
 2227 
 2228         /*
 2229          * Ignore the error - there is no result code for failure,
 2230          * only for grace period.
 2231          */
 2232         result->stat.stat = nlm4_granted;
 2233 
 2234 out:
 2235         nlm_release_vfs_state(&vs);
 2236         if (rpcp)
 2237                 *rpcp = nlm_host_get_rpc(host, TRUE);
 2238         nlm_host_release(host);
 2239         return (0);
 2240 }
 2241 
 2242 int
 2243 nlm_do_granted(nlm4_testargs *argp, nlm4_res *result, struct svc_req *rqstp,
 2244 
 2245     CLIENT **rpcp)
 2246 {
 2247         struct nlm_host *host;
 2248         struct nlm_waiting_lock *nw;
 2249         
 2250         memset(result, 0, sizeof(*result));
 2251 
 2252         host = nlm_find_host_by_addr(svc_getrpccaller(rqstp), rqstp->rq_vers);
 2253         if (!host) {
 2254                 result->stat.stat = nlm4_denied_nolocks;
 2255                 return (ENOMEM);
 2256         }
 2257 
 2258         nlm_copy_netobj(&result->cookie, &argp->cookie, M_RPC);
 2259         result->stat.stat = nlm4_denied;
 2260 
 2261         mtx_lock(&nlm_global_lock);
 2262         TAILQ_FOREACH(nw, &nlm_waiting_locks, nw_link) {
 2263                 if (!nw->nw_waiting)
 2264                         continue;
 2265                 if (argp->alock.svid == nw->nw_lock.svid
 2266                     && argp->alock.l_offset == nw->nw_lock.l_offset
 2267                     && argp->alock.l_len == nw->nw_lock.l_len
 2268                     && argp->alock.fh.n_len == nw->nw_lock.fh.n_len
 2269                     && !memcmp(argp->alock.fh.n_bytes, nw->nw_lock.fh.n_bytes,
 2270                         nw->nw_lock.fh.n_len)) {
 2271                         nw->nw_waiting = FALSE;
 2272                         wakeup(nw);
 2273                         result->stat.stat = nlm4_granted;
 2274                         break;
 2275                 }
 2276         }
 2277         mtx_unlock(&nlm_global_lock);
 2278         if (rpcp)
 2279                 *rpcp = nlm_host_get_rpc(host, TRUE);
 2280         nlm_host_release(host);
 2281         return (0);
 2282 }
 2283 
 2284 void
 2285 nlm_do_free_all(nlm4_notify *argp)
 2286 {
 2287         struct nlm_host *host, *thost;
 2288 
 2289         TAILQ_FOREACH_SAFE(host, &nlm_hosts, nh_link, thost) {
 2290                 if (!strcmp(host->nh_caller_name, argp->name))
 2291                         nlm_host_notify(host, argp->state);
 2292         }
 2293 }
 2294 
 2295 /*
 2296  * Kernel module glue
 2297  */
 2298 static int
 2299 nfslockd_modevent(module_t mod, int type, void *data)
 2300 {
 2301 
 2302         return (0);
 2303 }
 2304 static moduledata_t nfslockd_mod = {
 2305         "nfslockd",
 2306         nfslockd_modevent,
 2307         NULL,
 2308 };
 2309 DECLARE_MODULE(nfslockd, nfslockd_mod, SI_SUB_VFS, SI_ORDER_ANY);
 2310 
 2311 /* So that loader and kldload(2) can find us, wherever we are.. */
 2312 MODULE_DEPEND(nfslockd, krpc, 1, 1, 1);
 2313 MODULE_DEPEND(nfslockd, nfs, 1, 1, 1);
 2314 MODULE_VERSION(nfslockd, 1);

Cache object: f164aa6427ca6063697a8407707d4714


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