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