nfs_nfsiod.c revision 83651
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_nfsiod.c 83651 2001-09-18 23:32:09Z peter $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/sysproto.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/file.h>
48#include <sys/filedesc.h>
49#include <sys/vnode.h>
50#include <sys/malloc.h>
51#include <sys/mount.h>
52#include <sys/proc.h>
53#include <sys/bio.h>
54#include <sys/buf.h>
55#include <sys/mbuf.h>
56#include <sys/socket.h>
57#include <sys/socketvar.h>
58#include <sys/domain.h>
59#include <sys/protosw.h>
60#include <sys/namei.h>
61#include <sys/unistd.h>
62#include <sys/kthread.h>
63#include <sys/fcntl.h>
64#include <sys/lockf.h>
65#include <sys/mutex.h>
66
67#include <netinet/in.h>
68#include <netinet/tcp.h>
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
78static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
79
80static void	nfssvc_iod(void *);
81
82#define	TRUE	1
83#define	FALSE	0
84
85static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
86
87SYSCTL_DECL(_vfs_nfs);
88
89static void
90nfsiod_setup(void *dummy)
91{
92	int i;
93	int error;
94	struct proc *p;
95
96	for (i = 0; i < 4; i++) {
97		error = kthread_create(nfssvc_iod, NULL, &p, RFHIGHPID,
98		    "nfsiod %d", i);
99		if (error)
100			panic("nfsiod_setup: kthread_create error %d", error);
101	}
102}
103SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
104
105static int nfs_defect = 0;
106SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
107
108int
109nfsclnt(struct thread *td, struct nfsclnt_args *uap)
110{
111	struct lockd_ans la;
112	int error;
113
114	if ((uap->flag & NFSCLNT_LOCKDANS) != 0) {
115		error = copyin(uap->argp, &la, sizeof(la));
116		return (error != 0 ? error : nfslockdans(td->td_proc, &la));
117	}
118	return EINVAL;
119}
120
121/*
122 * Asynchronous I/O daemons for client nfs.
123 * They do read-ahead and write-behind operations on the block I/O cache.
124 * Never returns unless it fails or gets killed.
125 */
126static void
127nfssvc_iod(void *dummy)
128{
129	struct buf *bp;
130	int i, myiod;
131	struct nfsmount *nmp;
132	int error = 0;
133
134	mtx_lock(&Giant);
135	/*
136	 * Assign my position or return error if too many already running
137	 */
138	myiod = -1;
139	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
140		if (nfs_asyncdaemon[i] == 0) {
141			nfs_asyncdaemon[i]++;
142			myiod = i;
143			break;
144		}
145	if (myiod == -1)
146		return /* XXX (EBUSY) */;
147	nfs_numasync++;
148	/*
149	 * Just loop around doin our stuff until SIGKILL
150	 */
151	for (;;) {
152	    while (((nmp = nfs_iodmount[myiod]) == NULL
153		    || !TAILQ_FIRST(&nmp->nm_bufq))
154		   && error == 0) {
155		if (nmp)
156		    nmp->nm_bufqiods--;
157		nfs_iodwant[myiod] = curthread->td_proc;
158		nfs_iodmount[myiod] = NULL;
159		error = tsleep((caddr_t)&nfs_iodwant[myiod],
160			PWAIT | PCATCH, "nfsidl", 0);
161	    }
162	    if (error) {
163		nfs_asyncdaemon[myiod] = 0;
164		if (nmp)
165		    nmp->nm_bufqiods--;
166		nfs_iodwant[myiod] = NULL;
167		nfs_iodmount[myiod] = NULL;
168		nfs_numasync--;
169		return /* XXX (error) */;
170	    }
171	    while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
172		/* Take one off the front of the list */
173		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
174		nmp->nm_bufqlen--;
175		if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
176		    nmp->nm_bufqwant = FALSE;
177		    wakeup(&nmp->nm_bufq);
178		}
179		if (bp->b_iocmd == BIO_READ)
180		    (void) nfs_doio(bp, bp->b_rcred, (struct thread *)0);
181		else
182		    (void) nfs_doio(bp, bp->b_wcred, (struct thread *)0);
183		/*
184		 * If there are more than one iod on this mount, then defect
185		 * so that the iods can be shared out fairly between the mounts
186		 */
187		if (nfs_defect && nmp->nm_bufqiods > 1) {
188		    NFS_DPF(ASYNCIO,
189			    ("nfssvc_iod: iod %d defecting from mount %p\n",
190			     myiod, nmp));
191		    nfs_iodmount[myiod] = NULL;
192		    nmp->nm_bufqiods--;
193		    break;
194		}
195	    }
196	}
197}
198