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/nfsclient/nfs_nfsiod.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  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)nfs_syscalls.c      8.5 (Berkeley) 3/30/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/sysproto.h>
   41 #include <sys/kernel.h>
   42 #include <sys/sysctl.h>
   43 #include <sys/file.h>
   44 #include <sys/filedesc.h>
   45 #include <sys/vnode.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mount.h>
   48 #include <sys/proc.h>
   49 #include <sys/bio.h>
   50 #include <sys/buf.h>
   51 #include <sys/mbuf.h>
   52 #include <sys/socket.h>
   53 #include <sys/socketvar.h>
   54 #include <sys/domain.h>
   55 #include <sys/protosw.h>
   56 #include <sys/namei.h>
   57 #include <sys/unistd.h>
   58 #include <sys/kthread.h>
   59 #include <sys/fcntl.h>
   60 #include <sys/lockf.h>
   61 #include <sys/mutex.h>
   62 #include <sys/taskqueue.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/tcp.h>
   66 
   67 #include <rpc/rpcclnt.h>
   68 
   69 #include <nfs/xdr_subs.h>
   70 #include <nfs/rpcv2.h>
   71 #include <nfs/nfsproto.h>
   72 #include <nfsclient/nfs.h>
   73 #include <nfsclient/nfsm_subs.h>
   74 #include <nfsclient/nfsmount.h>
   75 #include <nfsclient/nfsnode.h>
   76 #include <nfsclient/nfs_lock.h>
   77 
   78 static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
   79 
   80 static void     nfssvc_iod(void *);
   81 
   82 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
   83 
   84 SYSCTL_DECL(_vfs_nfs);
   85 
   86 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
   87 static unsigned int nfs_iodmaxidle = 120;
   88 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, "");
   89 
   90 /* Maximum number of nfsiod kthreads */
   91 unsigned int nfs_iodmax = 20;
   92 
   93 /* Minimum number of nfsiod kthreads to keep as spares */
   94 static unsigned int nfs_iodmin = 0;
   95 
   96 static int nfs_nfsiodnew_sync(void);
   97 
   98 static int
   99 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
  100 {
  101         int error, i;
  102         int newmin;
  103 
  104         newmin = nfs_iodmin;
  105         error = sysctl_handle_int(oidp, &newmin, 0, req);
  106         if (error || (req->newptr == NULL))
  107                 return (error);
  108         mtx_lock(&nfs_iod_mtx);
  109         if (newmin > nfs_iodmax) {
  110                 error = EINVAL;
  111                 goto out;
  112         }
  113         nfs_iodmin = newmin;
  114         if (nfs_numasync >= nfs_iodmin)
  115                 goto out;
  116         /*
  117          * If the current number of nfsiod is lower
  118          * than the new minimum, create some more.
  119          */
  120         for (i = nfs_iodmin - nfs_numasync; i > 0; i--)
  121                 nfs_nfsiodnew_sync();
  122 out:
  123         mtx_unlock(&nfs_iod_mtx);       
  124         return (0);
  125 }
  126 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
  127     sizeof (nfs_iodmin), sysctl_iodmin, "IU", "");
  128 
  129 
  130 static int
  131 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
  132 {
  133         int error, i;
  134         int iod, newmax;
  135 
  136         newmax = nfs_iodmax;
  137         error = sysctl_handle_int(oidp, &newmax, 0, req);
  138         if (error || (req->newptr == NULL))
  139                 return (error);
  140         if (newmax > NFS_MAXASYNCDAEMON)
  141                 return (EINVAL);
  142         mtx_lock(&nfs_iod_mtx);
  143         nfs_iodmax = newmax;
  144         if (nfs_numasync <= nfs_iodmax)
  145                 goto out;
  146         /*
  147          * If there are some asleep nfsiods that should
  148          * exit, wakeup() them so that they check nfs_iodmax
  149          * and exit.  Those who are active will exit as
  150          * soon as they finish I/O.
  151          */
  152         iod = nfs_numasync - 1;
  153         for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
  154                 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE)
  155                         wakeup(&nfs_iodwant[iod]);
  156                 iod--;
  157         }
  158 out:
  159         mtx_unlock(&nfs_iod_mtx);
  160         return (0);
  161 }
  162 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
  163     sizeof (nfs_iodmax), sysctl_iodmax, "IU", "");
  164 
  165 static int
  166 nfs_nfsiodnew_sync(void)
  167 {
  168         int error, i;
  169 
  170         mtx_assert(&nfs_iod_mtx, MA_OWNED);
  171         for (i = 0; i < nfs_iodmax; i++) {
  172                 if (nfs_asyncdaemon[i] == 0) {
  173                         nfs_asyncdaemon[i] = 1;
  174                         break;
  175                 }
  176         }
  177         if (i == nfs_iodmax)
  178                 return (0);
  179         mtx_unlock(&nfs_iod_mtx);
  180         error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL,
  181             RFHIGHPID, 0, "nfsiod %d", i);
  182         mtx_lock(&nfs_iod_mtx);
  183         if (error == 0) {
  184                 nfs_numasync++;
  185                 nfs_iodwant[i] = NFSIOD_AVAILABLE;
  186         } else
  187                 nfs_asyncdaemon[i] = 0;
  188         return (error);
  189 }
  190 
  191 void
  192 nfs_nfsiodnew_tq(__unused void *arg, int pending)
  193 {
  194 
  195         mtx_lock(&nfs_iod_mtx);
  196         while (pending > 0) {
  197                 pending--;
  198                 nfs_nfsiodnew_sync();
  199         }
  200         mtx_unlock(&nfs_iod_mtx);
  201 }
  202 
  203 void
  204 nfs_nfsiodnew(void)
  205 {
  206 
  207         mtx_assert(&nfs_iod_mtx, MA_OWNED);
  208         taskqueue_enqueue(taskqueue_thread, &nfs_nfsiodnew_task);
  209 }
  210 
  211 static void
  212 nfsiod_setup(void *dummy)
  213 {
  214         int error;
  215 
  216         TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
  217         mtx_lock(&nfs_iod_mtx);
  218         /* Silently limit the start number of nfsiod's */
  219         if (nfs_iodmin > NFS_MAXASYNCDAEMON)
  220                 nfs_iodmin = NFS_MAXASYNCDAEMON;
  221 
  222         while (nfs_numasync < nfs_iodmin) {
  223                 error = nfs_nfsiodnew_sync();
  224                 if (error == -1)
  225                         panic("nfsiod_setup: nfs_nfsiodnew failed");
  226         }
  227         mtx_unlock(&nfs_iod_mtx);
  228 }
  229 SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
  230 
  231 static int nfs_defect = 0;
  232 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
  233 
  234 /*
  235  * Asynchronous I/O daemons for client nfs.
  236  * They do read-ahead and write-behind operations on the block I/O cache.
  237  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
  238  */
  239 static void
  240 nfssvc_iod(void *instance)
  241 {
  242         struct buf *bp;
  243         struct nfsmount *nmp;
  244         int myiod, timo;
  245         int error = 0;
  246 
  247         mtx_lock(&nfs_iod_mtx);
  248         myiod = (int *)instance - nfs_asyncdaemon;
  249         /*
  250          * Main loop
  251          */
  252         for (;;) {
  253             while (((nmp = nfs_iodmount[myiod]) == NULL)
  254                    || !TAILQ_FIRST(&nmp->nm_bufq)) {
  255                 if (myiod >= nfs_iodmax)
  256                         goto finish;
  257                 if (nmp)
  258                         nmp->nm_bufqiods--;
  259                 if (nfs_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
  260                         nfs_iodwant[myiod] = NFSIOD_AVAILABLE;
  261                 nfs_iodmount[myiod] = NULL;
  262                 /*
  263                  * Always keep at least nfs_iodmin kthreads.
  264                  */
  265                 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
  266                 error = msleep(&nfs_iodwant[myiod], &nfs_iod_mtx, PWAIT | PCATCH,
  267                     "-", timo);
  268                 if (error) {
  269                         nmp = nfs_iodmount[myiod];
  270                         /*
  271                          * Rechecking the nm_bufq closes a rare race where the 
  272                          * nfsiod is woken up at the exact time the idle timeout
  273                          * fires
  274                          */
  275                         if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
  276                                 error = 0;
  277                         break;
  278                 }
  279             }
  280             if (error)
  281                     break;
  282             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
  283                 int giant_locked = 0;
  284                     
  285                 /* Take one off the front of the list */
  286                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
  287                 nmp->nm_bufqlen--;
  288                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
  289                     nmp->nm_bufqwant = 0;
  290                     wakeup(&nmp->nm_bufq);
  291                 }
  292                 mtx_unlock(&nfs_iod_mtx);
  293                 if (NFS_ISV4(bp->b_vp)) {
  294                         giant_locked = 1;
  295                         mtx_lock(&Giant);
  296                 }
  297                 if (bp->b_flags & B_DIRECT) {
  298                         KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
  299                         (void)nfs_doio_directwrite(bp);
  300                 } else {
  301                         if (bp->b_iocmd == BIO_READ)
  302                                 (void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
  303                         else
  304                                 (void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
  305                 }
  306                 if (giant_locked)
  307                         mtx_unlock(&Giant);
  308                 mtx_lock(&nfs_iod_mtx);
  309                 /*
  310                  * If there are more than one iod on this mount, then defect
  311                  * so that the iods can be shared out fairly between the mounts
  312                  */
  313                 if (nfs_defect && nmp->nm_bufqiods > 1) {
  314                     NFS_DPF(ASYNCIO,
  315                             ("nfssvc_iod: iod %d defecting from mount %p\n",
  316                              myiod, nmp));
  317                     nfs_iodmount[myiod] = NULL;
  318                     nmp->nm_bufqiods--;
  319                     break;
  320                 }
  321             }
  322         }
  323 finish:
  324         nfs_asyncdaemon[myiod] = 0;
  325         if (nmp)
  326             nmp->nm_bufqiods--;
  327         nfs_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
  328         nfs_iodmount[myiod] = NULL;
  329         /* Someone may be waiting for the last nfsiod to terminate. */
  330         if (--nfs_numasync == 0)
  331                 wakeup(&nfs_numasync);
  332         mtx_unlock(&nfs_iod_mtx);
  333         if ((error == 0) || (error == EWOULDBLOCK))
  334                 kthread_exit(0);
  335         /* Abnormal termination */
  336         kthread_exit(1);
  337 }

Cache object: 4bdb10d3c7d393da40309a6e247eb145


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