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/fs/nfsclient/nfs_clnfsiod.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  *      from nfs_syscalls.c     8.5 (Berkeley) 3/30/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/8.1/sys/fs/nfsclient/nfs_clnfsiod.c 207406 2010-04-29 23:48:09Z rmacklem $");
   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 
   63 #include <netinet/in.h>
   64 #include <netinet/tcp.h>
   65 
   66 #include <fs/nfs/nfsport.h>
   67 #include <fs/nfsclient/nfsmount.h>
   68 #include <fs/nfsclient/nfs.h>
   69 #include <fs/nfsclient/nfsnode.h>
   70 #include <fs/nfsclient/nfs_lock.h>
   71 
   72 extern struct mtx ncl_iod_mutex;
   73 
   74 int ncl_numasync;
   75 enum nfsiod_state ncl_iodwant[NFS_MAXRAHEAD];
   76 struct nfsmount *ncl_iodmount[NFS_MAXRAHEAD];
   77 
   78 static void     nfssvc_iod(void *);
   79 
   80 static int nfs_asyncdaemon[NFS_MAXRAHEAD];
   81 
   82 SYSCTL_DECL(_vfs_newnfs);
   83 
   84 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
   85 static unsigned int ncl_iodmaxidle = 120;
   86 SYSCTL_UINT(_vfs_newnfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &ncl_iodmaxidle, 0, "");
   87 
   88 /* Maximum number of nfsiod kthreads */
   89 unsigned int ncl_iodmax = NFS_MAXRAHEAD;
   90 
   91 /* Minimum number of nfsiod kthreads to keep as spares */
   92 static unsigned int nfs_iodmin = 0;
   93 
   94 static int
   95 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
   96 {
   97         int error, i;
   98         int newmin;
   99 
  100         newmin = nfs_iodmin;
  101         error = sysctl_handle_int(oidp, &newmin, 0, req);
  102         if (error || (req->newptr == NULL))
  103                 return (error);
  104         mtx_lock(&ncl_iod_mutex);
  105         if (newmin > ncl_iodmax) {
  106                 error = EINVAL;
  107                 goto out;
  108         }
  109         nfs_iodmin = newmin;
  110         if (ncl_numasync >= nfs_iodmin)
  111                 goto out;
  112         /*
  113          * If the current number of nfsiod is lower
  114          * than the new minimum, create some more.
  115          */
  116         for (i = nfs_iodmin - ncl_numasync; i > 0; i--)
  117                 ncl_nfsiodnew(0);
  118 out:
  119         mtx_unlock(&ncl_iod_mutex);     
  120         return (0);
  121 }
  122 SYSCTL_PROC(_vfs_newnfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
  123     sizeof (nfs_iodmin), sysctl_iodmin, "IU", "");
  124 
  125 
  126 static int
  127 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
  128 {
  129         int error, i;
  130         int iod, newmax;
  131 
  132         newmax = ncl_iodmax;
  133         error = sysctl_handle_int(oidp, &newmax, 0, req);
  134         if (error || (req->newptr == NULL))
  135                 return (error);
  136         if (newmax > NFS_MAXRAHEAD)
  137                 return (EINVAL);
  138         mtx_lock(&ncl_iod_mutex);
  139         ncl_iodmax = newmax;
  140         if (ncl_numasync <= ncl_iodmax)
  141                 goto out;
  142         /*
  143          * If there are some asleep nfsiods that should
  144          * exit, wakeup() them so that they check ncl_iodmax
  145          * and exit.  Those who are active will exit as
  146          * soon as they finish I/O.
  147          */
  148         iod = ncl_numasync - 1;
  149         for (i = 0; i < ncl_numasync - ncl_iodmax; i++) {
  150                 if (ncl_iodwant[iod] == NFSIOD_AVAILABLE)
  151                         wakeup(&ncl_iodwant[iod]);
  152                 iod--;
  153         }
  154 out:
  155         mtx_unlock(&ncl_iod_mutex);
  156         return (0);
  157 }
  158 SYSCTL_PROC(_vfs_newnfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
  159     sizeof (ncl_iodmax), sysctl_iodmax, "IU", "");
  160 
  161 int
  162 ncl_nfsiodnew(int set_iodwant)
  163 {
  164         int error, i;
  165         int newiod;
  166 
  167         if (ncl_numasync >= ncl_iodmax)
  168                 return (-1);
  169         newiod = -1;
  170         for (i = 0; i < ncl_iodmax; i++)
  171                 if (nfs_asyncdaemon[i] == 0) {
  172                         nfs_asyncdaemon[i]++;
  173                         newiod = i;
  174                         break;
  175                 }
  176         if (newiod == -1)
  177                 return (-1);
  178         if (set_iodwant > 0)
  179                 ncl_iodwant[i] = NFSIOD_CREATED_FOR_NFS_ASYNCIO;
  180         mtx_unlock(&ncl_iod_mutex);
  181         error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID,
  182             0, "nfsiod %d", newiod);
  183         mtx_lock(&ncl_iod_mutex);
  184         if (error) {
  185                 if (set_iodwant > 0)
  186                         ncl_iodwant[i] = NFSIOD_NOT_AVAILABLE;
  187                 return (-1);
  188         }
  189         ncl_numasync++;
  190         return (newiod);
  191 }
  192 
  193 static void
  194 nfsiod_setup(void *dummy)
  195 {
  196         int i;
  197         int error;
  198 
  199         TUNABLE_INT_FETCH("vfs.newnfs.iodmin", &nfs_iodmin);
  200         nfscl_init();
  201         mtx_lock(&ncl_iod_mutex);
  202         /* Silently limit the start number of nfsiod's */
  203         if (nfs_iodmin > NFS_MAXRAHEAD)
  204                 nfs_iodmin = NFS_MAXRAHEAD;
  205 
  206         for (i = 0; i < nfs_iodmin; i++) {
  207                 error = ncl_nfsiodnew(0);
  208                 if (error == -1)
  209                         panic("newnfsiod_setup: ncl_nfsiodnew failed");
  210         }
  211         mtx_unlock(&ncl_iod_mutex);
  212 }
  213 SYSINIT(newnfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
  214 
  215 static int nfs_defect = 0;
  216 SYSCTL_INT(_vfs_newnfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
  217 
  218 /*
  219  * Asynchronous I/O daemons for client nfs.
  220  * They do read-ahead and write-behind operations on the block I/O cache.
  221  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
  222  */
  223 static void
  224 nfssvc_iod(void *instance)
  225 {
  226         struct buf *bp;
  227         struct nfsmount *nmp;
  228         int myiod, timo;
  229         int error = 0;
  230 
  231         mtx_lock(&ncl_iod_mutex);
  232         myiod = (int *)instance - nfs_asyncdaemon;
  233         /*
  234          * Main loop
  235          */
  236         for (;;) {
  237             while (((nmp = ncl_iodmount[myiod]) == NULL)
  238                    || !TAILQ_FIRST(&nmp->nm_bufq)) {
  239                 if (myiod >= ncl_iodmax)
  240                         goto finish;
  241                 if (nmp)
  242                         nmp->nm_bufqiods--;
  243                 if (ncl_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
  244                         ncl_iodwant[myiod] = NFSIOD_AVAILABLE;
  245                 ncl_iodmount[myiod] = NULL;
  246                 /*
  247                  * Always keep at least nfs_iodmin kthreads.
  248                  */
  249                 timo = (myiod < nfs_iodmin) ? 0 : ncl_iodmaxidle * hz;
  250                 error = msleep(&ncl_iodwant[myiod], &ncl_iod_mutex, PWAIT | PCATCH,
  251                     "-", timo);
  252                 if (error) {
  253                         nmp = ncl_iodmount[myiod];
  254                         /*
  255                          * Rechecking the nm_bufq closes a rare race where the 
  256                          * nfsiod is woken up at the exact time the idle timeout
  257                          * fires
  258                          */
  259                         if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
  260                                 error = 0;
  261                         break;
  262                 }
  263             }
  264             if (error)
  265                     break;
  266             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
  267                     
  268                 /* Take one off the front of the list */
  269                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
  270                 nmp->nm_bufqlen--;
  271                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= ncl_numasync) {
  272                     nmp->nm_bufqwant = 0;
  273                     wakeup(&nmp->nm_bufq);
  274                 }
  275                 mtx_unlock(&ncl_iod_mutex);
  276                 if (bp->b_flags & B_DIRECT) {
  277                         KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
  278                         (void)ncl_doio_directwrite(bp);
  279                 } else {
  280                         if (bp->b_iocmd == BIO_READ)
  281                                 (void) ncl_doio(bp->b_vp, bp, bp->b_rcred,
  282                                     NULL, 0);
  283                         else
  284                                 (void) ncl_doio(bp->b_vp, bp, bp->b_wcred,
  285                                     NULL, 0);
  286                 }
  287                 mtx_lock(&ncl_iod_mutex);
  288                 /*
  289                  * If there are more than one iod on this mount, then defect
  290                  * so that the iods can be shared out fairly between the mounts
  291                  */
  292                 if (nfs_defect && nmp->nm_bufqiods > 1) {
  293                     NFS_DPF(ASYNCIO,
  294                             ("nfssvc_iod: iod %d defecting from mount %p\n",
  295                              myiod, nmp));
  296                     ncl_iodmount[myiod] = NULL;
  297                     nmp->nm_bufqiods--;
  298                     break;
  299                 }
  300             }
  301         }
  302 finish:
  303         nfs_asyncdaemon[myiod] = 0;
  304         if (nmp)
  305             nmp->nm_bufqiods--;
  306         ncl_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
  307         ncl_iodmount[myiod] = NULL;
  308         /* Someone may be waiting for the last nfsiod to terminate. */
  309         if (--ncl_numasync == 0)
  310                 wakeup(&ncl_numasync);
  311         mtx_unlock(&ncl_iod_mutex);
  312         if ((error == 0) || (error == EWOULDBLOCK))
  313                 kproc_exit(0);
  314         /* Abnormal termination */
  315         kproc_exit(1);
  316 }

Cache object: 498e5104ac029afdeed23ffdb1e625f6


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