2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Rick Macklem at The University of Guelph.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1995-11-14 05:16:37 +00:00
|
|
|
#include <sys/sysproto.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/kernel.h>
|
1996-11-06 10:53:16 +00:00
|
|
|
#include <sys/sysctl.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/file.h>
|
1996-04-30 23:26:52 +00:00
|
|
|
#include <sys/filedesc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/vnode.h>
|
1997-10-12 20:26:33 +00:00
|
|
|
#include <sys/malloc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/proc.h>
|
2000-05-05 09:59:14 +00:00
|
|
|
#include <sys/bio.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/namei.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/kthread.h>
|
2001-04-17 20:45:23 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/lockf.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <sys/mutex.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2003-11-14 20:54:10 +00:00
|
|
|
|
|
|
|
#include <rpc/rpcclnt.h>
|
|
|
|
|
1995-06-27 11:07:30 +00:00
|
|
|
#include <nfs/xdr_subs.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <nfs/rpcv2.h>
|
1995-06-27 11:07:30 +00:00
|
|
|
#include <nfs/nfsproto.h>
|
2001-09-18 23:32:09 +00:00
|
|
|
#include <nfsclient/nfs.h>
|
|
|
|
#include <nfsclient/nfsm_subs.h>
|
|
|
|
#include <nfsclient/nfsmount.h>
|
|
|
|
#include <nfsclient/nfsnode.h>
|
|
|
|
#include <nfsclient/nfs_lock.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2005-10-31 15:41:29 +00:00
|
|
|
static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
|
1997-10-11 18:31:40 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static void nfssvc_iod(void *);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
|
1995-11-21 15:51:39 +00:00
|
|
|
|
1999-02-18 09:19:41 +00:00
|
|
|
SYSCTL_DECL(_vfs_nfs);
|
|
|
|
|
2002-01-15 20:57:21 +00:00
|
|
|
/* Maximum number of seconds a nfsiod kthread will sleep before exiting */
|
|
|
|
static unsigned int nfs_iodmaxidle = 120;
|
|
|
|
SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, "");
|
|
|
|
|
|
|
|
/* Maximum number of nfsiod kthreads */
|
2004-04-11 13:30:20 +00:00
|
|
|
unsigned int nfs_iodmax = 20;
|
2002-01-15 20:57:21 +00:00
|
|
|
|
2002-01-14 02:13:46 +00:00
|
|
|
/* Minimum number of nfsiod kthreads to keep as spares */
|
|
|
|
static unsigned int nfs_iodmin = 4;
|
|
|
|
|
2002-01-15 20:57:21 +00:00
|
|
|
static int
|
|
|
|
sysctl_iodmin(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
int newmin;
|
|
|
|
|
|
|
|
newmin = nfs_iodmin;
|
|
|
|
error = sysctl_handle_int(oidp, &newmin, 0, req);
|
|
|
|
if (error || (req->newptr == NULL))
|
|
|
|
return (error);
|
|
|
|
if (newmin > nfs_iodmax)
|
|
|
|
return (EINVAL);
|
|
|
|
nfs_iodmin = newmin;
|
|
|
|
if (nfs_numasync >= nfs_iodmin)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* If the current number of nfsiod is lower
|
|
|
|
* than the new minimum, create some more.
|
|
|
|
*/
|
|
|
|
for (i = nfs_iodmin - nfs_numasync; i > 0; i--)
|
|
|
|
nfs_nfsiodnew();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
|
|
|
|
sizeof (nfs_iodmin), sysctl_iodmin, "IU", "");
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_iodmax(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
int iod, newmax;
|
|
|
|
|
|
|
|
newmax = nfs_iodmax;
|
|
|
|
error = sysctl_handle_int(oidp, &newmax, 0, req);
|
|
|
|
if (error || (req->newptr == NULL))
|
|
|
|
return (error);
|
|
|
|
if (newmax > NFS_MAXASYNCDAEMON)
|
|
|
|
return (EINVAL);
|
|
|
|
nfs_iodmax = newmax;
|
|
|
|
if (nfs_numasync <= nfs_iodmax)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* If there are some asleep nfsiods that should
|
|
|
|
* exit, wakeup() them so that they check nfs_iodmax
|
|
|
|
* and exit. Those who are active will exit as
|
|
|
|
* soon as they finish I/O.
|
|
|
|
*/
|
|
|
|
iod = nfs_numasync - 1;
|
|
|
|
for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
|
|
|
|
if (nfs_iodwant[iod])
|
2003-03-02 16:54:40 +00:00
|
|
|
wakeup(&nfs_iodwant[iod]);
|
2002-01-15 20:57:21 +00:00
|
|
|
iod--;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
|
|
|
|
sizeof (nfs_iodmax), sysctl_iodmax, "IU", "");
|
2002-01-14 02:13:46 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
nfs_nfsiodnew(void)
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
int newiod;
|
|
|
|
|
2002-01-15 20:57:21 +00:00
|
|
|
if (nfs_numasync >= nfs_iodmax)
|
|
|
|
return (-1);
|
2002-01-14 02:13:46 +00:00
|
|
|
newiod = -1;
|
2002-01-15 20:57:21 +00:00
|
|
|
for (i = 0; i < nfs_iodmax; i++)
|
2002-01-14 02:13:46 +00:00
|
|
|
if (nfs_asyncdaemon[i] == 0) {
|
|
|
|
nfs_asyncdaemon[i]++;
|
|
|
|
newiod = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (newiod == -1)
|
|
|
|
return (-1);
|
|
|
|
error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID,
|
2002-10-02 07:44:29 +00:00
|
|
|
0, "nfsiod %d", newiod);
|
2002-01-14 02:13:46 +00:00
|
|
|
if (error)
|
|
|
|
return (-1);
|
|
|
|
nfs_numasync++;
|
|
|
|
return (newiod);
|
|
|
|
}
|
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static void
|
|
|
|
nfsiod_setup(void *dummy)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
int i;
|
1994-05-24 10:09:53 +00:00
|
|
|
int error;
|
|
|
|
|
2002-01-14 02:13:46 +00:00
|
|
|
TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
|
|
|
|
/* Silently limit the start number of nfsiod's */
|
|
|
|
if (nfs_iodmin > NFS_MAXASYNCDAEMON)
|
|
|
|
nfs_iodmin = NFS_MAXASYNCDAEMON;
|
|
|
|
|
|
|
|
for (i = 0; i < nfs_iodmin; i++) {
|
|
|
|
error = nfs_nfsiodnew();
|
|
|
|
if (error == -1)
|
|
|
|
panic("nfsiod_setup: nfs_nfsiodnew failed");
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-18 23:32:09 +00:00
|
|
|
SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
|
1998-05-31 17:27:58 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
static int nfs_defect = 0;
|
|
|
|
SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
|
1998-05-31 17:27:58 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Asynchronous I/O daemons for client nfs.
|
|
|
|
* They do read-ahead and write-behind operations on the block I/O cache.
|
2002-01-14 02:13:46 +00:00
|
|
|
* Returns if we hit the timeout defined by the iodmaxidle sysctl.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
2001-09-18 23:32:09 +00:00
|
|
|
static void
|
2002-01-14 02:13:46 +00:00
|
|
|
nfssvc_iod(void *instance)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2001-09-18 23:32:09 +00:00
|
|
|
struct buf *bp;
|
1996-11-06 10:53:16 +00:00
|
|
|
struct nfsmount *nmp;
|
2002-01-14 02:13:46 +00:00
|
|
|
int myiod, timo;
|
1997-11-07 08:53:44 +00:00
|
|
|
int error = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-09-18 23:32:09 +00:00
|
|
|
mtx_lock(&Giant);
|
2002-01-14 02:13:46 +00:00
|
|
|
myiod = (int *)instance - nfs_asyncdaemon;
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2002-01-14 02:13:46 +00:00
|
|
|
* Main loop
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
for (;;) {
|
1996-11-06 10:53:16 +00:00
|
|
|
while (((nmp = nfs_iodmount[myiod]) == NULL
|
2002-01-14 02:13:46 +00:00
|
|
|
|| !TAILQ_FIRST(&nmp->nm_bufq))
|
1996-11-06 10:53:16 +00:00
|
|
|
&& error == 0) {
|
2002-01-15 20:57:21 +00:00
|
|
|
if (myiod >= nfs_iodmax)
|
|
|
|
goto finish;
|
1996-11-06 10:53:16 +00:00
|
|
|
if (nmp)
|
2002-01-14 02:13:46 +00:00
|
|
|
nmp->nm_bufqiods--;
|
2001-09-18 23:32:09 +00:00
|
|
|
nfs_iodwant[myiod] = curthread->td_proc;
|
1996-11-06 10:53:16 +00:00
|
|
|
nfs_iodmount[myiod] = NULL;
|
2002-01-14 02:13:46 +00:00
|
|
|
/*
|
|
|
|
* Always keep at least nfs_iodmin kthreads.
|
|
|
|
*/
|
|
|
|
timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
|
2003-03-02 16:54:40 +00:00
|
|
|
error = tsleep(&nfs_iodwant[myiod], PWAIT | PCATCH,
|
2003-07-02 08:09:20 +00:00
|
|
|
"-", timo);
|
1995-06-27 11:07:30 +00:00
|
|
|
}
|
2002-01-14 02:13:46 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2001-09-18 23:32:09 +00:00
|
|
|
while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
|
1996-11-06 10:53:16 +00:00
|
|
|
/* Take one off the front of the list */
|
|
|
|
TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
|
|
|
|
nmp->nm_bufqlen--;
|
2000-01-05 05:11:37 +00:00
|
|
|
if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
|
2002-01-14 02:13:46 +00:00
|
|
|
nmp->nm_bufqwant = 0;
|
1996-11-06 10:53:16 +00:00
|
|
|
wakeup(&nmp->nm_bufq);
|
|
|
|
}
|
2004-12-15 22:20:22 +00:00
|
|
|
if (bp->b_flags & B_DIRECT) {
|
|
|
|
KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
|
|
|
|
(void)nfs_doio_directwrite(bp);
|
|
|
|
} else {
|
|
|
|
if (bp->b_iocmd == BIO_READ)
|
|
|
|
(void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
|
|
|
|
else
|
|
|
|
(void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
|
|
|
|
}
|
|
|
|
|
1996-11-06 10:53:16 +00:00
|
|
|
/*
|
|
|
|
* If there are more than one iod on this mount, then defect
|
|
|
|
* so that the iods can be shared out fairly between the mounts
|
|
|
|
*/
|
|
|
|
if (nfs_defect && nmp->nm_bufqiods > 1) {
|
|
|
|
NFS_DPF(ASYNCIO,
|
|
|
|
("nfssvc_iod: iod %d defecting from mount %p\n",
|
|
|
|
myiod, nmp));
|
|
|
|
nfs_iodmount[myiod] = NULL;
|
|
|
|
nmp->nm_bufqiods--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2002-01-15 20:57:21 +00:00
|
|
|
finish:
|
2002-01-14 02:13:46 +00:00
|
|
|
nfs_asyncdaemon[myiod] = 0;
|
|
|
|
if (nmp)
|
|
|
|
nmp->nm_bufqiods--;
|
|
|
|
nfs_iodwant[myiod] = NULL;
|
|
|
|
nfs_iodmount[myiod] = NULL;
|
2004-04-11 13:30:20 +00:00
|
|
|
/* Someone may be waiting for the last nfsiod to terminate. */
|
|
|
|
if (--nfs_numasync == 0)
|
|
|
|
wakeup(&nfs_numasync);
|
2005-02-07 18:21:50 +00:00
|
|
|
mtx_unlock(&Giant);
|
2002-01-15 20:57:21 +00:00
|
|
|
if ((error == 0) || (error == EWOULDBLOCK))
|
2002-01-14 02:13:46 +00:00
|
|
|
kthread_exit(0);
|
|
|
|
/* Abnormal termination */
|
|
|
|
kthread_exit(1);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|