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: releng/8.0/sys/nfsclient/nfs_nfsiod.c 195202 2009-06-30 19:03:27Z dfr $");
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 <nfs/xdr_subs.h>
67 #include <nfs/nfsproto.h>
68 #include <nfsclient/nfs.h>
69 #include <nfsclient/nfsm_subs.h>
70 #include <nfsclient/nfsmount.h>
71 #include <nfsclient/nfsnode.h>
72 #include <nfsclient/nfs_lock.h>
73
74 static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
75
76 static void nfssvc_iod(void *);
77
78 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
79
80 SYSCTL_DECL(_vfs_nfs);
81
82 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
83 static unsigned int nfs_iodmaxidle = 120;
84 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
85 "Max number of seconds an nfsiod kthread will sleep before exiting");
86
87 /* Maximum number of nfsiod kthreads */
88 unsigned int nfs_iodmax = 20;
89
90 /* Minimum number of nfsiod kthreads to keep as spares */
91 static unsigned int nfs_iodmin = 0;
92
93 static int
94 sysctl_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(&nfs_iod_mtx);
104 if (newmin > nfs_iodmax) {
105 error = EINVAL;
106 goto out;
107 }
108 nfs_iodmin = newmin;
109 if (nfs_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 - nfs_numasync; i > 0; i--)
116 nfs_nfsiodnew();
117 out:
118 mtx_unlock(&nfs_iod_mtx);
119 return (0);
120 }
121 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
122 sizeof (nfs_iodmin), sysctl_iodmin, "IU",
123 "Min number of nfsiod kthreads to keep as spares");
124
125
126 static int
127 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
128 {
129 int error, i;
130 int iod, newmax;
131
132 newmax = nfs_iodmax;
133 error = sysctl_handle_int(oidp, &newmax, 0, req);
134 if (error || (req->newptr == NULL))
135 return (error);
136 if (newmax > NFS_MAXASYNCDAEMON)
137 return (EINVAL);
138 mtx_lock(&nfs_iod_mtx);
139 nfs_iodmax = newmax;
140 if (nfs_numasync <= nfs_iodmax)
141 goto out;
142 /*
143 * If there are some asleep nfsiods that should
144 * exit, wakeup() them so that they check nfs_iodmax
145 * and exit. Those who are active will exit as
146 * soon as they finish I/O.
147 */
148 iod = nfs_numasync - 1;
149 for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
150 if (nfs_iodwant[iod])
151 wakeup(&nfs_iodwant[iod]);
152 iod--;
153 }
154 out:
155 mtx_unlock(&nfs_iod_mtx);
156 return (0);
157 }
158 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
159 sizeof (nfs_iodmax), sysctl_iodmax, "IU",
160 "Max number of nfsiod kthreads");
161
162 int
163 nfs_nfsiodnew(void)
164 {
165 int error, i;
166 int newiod;
167
168 if (nfs_numasync >= nfs_iodmax)
169 return (-1);
170 newiod = -1;
171 for (i = 0; i < nfs_iodmax; i++)
172 if (nfs_asyncdaemon[i] == 0) {
173 nfs_asyncdaemon[i]++;
174 newiod = i;
175 break;
176 }
177 if (newiod == -1)
178 return (-1);
179 mtx_unlock(&nfs_iod_mtx);
180 error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID,
181 0, "nfsiod %d", newiod);
182 mtx_lock(&nfs_iod_mtx);
183 if (error)
184 return (-1);
185 nfs_numasync++;
186 return (newiod);
187 }
188
189 static void
190 nfsiod_setup(void *dummy)
191 {
192 int i;
193 int error;
194
195 TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
196 mtx_lock(&nfs_iod_mtx);
197 /* Silently limit the start number of nfsiod's */
198 if (nfs_iodmin > NFS_MAXASYNCDAEMON)
199 nfs_iodmin = NFS_MAXASYNCDAEMON;
200
201 for (i = 0; i < nfs_iodmin; i++) {
202 error = nfs_nfsiodnew();
203 if (error == -1)
204 panic("nfsiod_setup: nfs_nfsiodnew failed");
205 }
206 mtx_unlock(&nfs_iod_mtx);
207 }
208 SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
209
210 static int nfs_defect = 0;
211 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
212 "Allow nfsiods to migrate serving different mounts");
213
214 /*
215 * Asynchronous I/O daemons for client nfs.
216 * They do read-ahead and write-behind operations on the block I/O cache.
217 * Returns if we hit the timeout defined by the iodmaxidle sysctl.
218 */
219 static void
220 nfssvc_iod(void *instance)
221 {
222 struct buf *bp;
223 struct nfsmount *nmp;
224 int myiod, timo;
225 int error = 0;
226
227 mtx_lock(&nfs_iod_mtx);
228 myiod = (int *)instance - nfs_asyncdaemon;
229 /*
230 * Main loop
231 */
232 for (;;) {
233 while (((nmp = nfs_iodmount[myiod]) == NULL)
234 || !TAILQ_FIRST(&nmp->nm_bufq)) {
235 if (myiod >= nfs_iodmax)
236 goto finish;
237 if (nmp)
238 nmp->nm_bufqiods--;
239 nfs_iodwant[myiod] = curthread->td_proc;
240 nfs_iodmount[myiod] = NULL;
241 /*
242 * Always keep at least nfs_iodmin kthreads.
243 */
244 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
245 error = msleep(&nfs_iodwant[myiod], &nfs_iod_mtx, PWAIT | PCATCH,
246 "-", timo);
247 if (error) {
248 nmp = nfs_iodmount[myiod];
249 /*
250 * Rechecking the nm_bufq closes a rare race where the
251 * nfsiod is woken up at the exact time the idle timeout
252 * fires
253 */
254 if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
255 error = 0;
256 break;
257 }
258 }
259 if (error)
260 break;
261 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
262 int giant_locked = 0;
263
264 /* Take one off the front of the list */
265 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
266 nmp->nm_bufqlen--;
267 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
268 nmp->nm_bufqwant = 0;
269 wakeup(&nmp->nm_bufq);
270 }
271 mtx_unlock(&nfs_iod_mtx);
272 if (NFS_ISV4(bp->b_vp)) {
273 giant_locked = 1;
274 mtx_lock(&Giant);
275 }
276 if (bp->b_flags & B_DIRECT) {
277 KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
278 (void)nfs_doio_directwrite(bp);
279 } else {
280 if (bp->b_iocmd == BIO_READ)
281 (void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
282 else
283 (void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
284 }
285 if (giant_locked)
286 mtx_unlock(&Giant);
287 mtx_lock(&nfs_iod_mtx);
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 nfs_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 nfs_iodwant[myiod] = NULL;
307 nfs_iodmount[myiod] = NULL;
308 /* Someone may be waiting for the last nfsiod to terminate. */
309 if (--nfs_numasync == 0)
310 wakeup(&nfs_numasync);
311 mtx_unlock(&nfs_iod_mtx);
312 if ((error == 0) || (error == EWOULDBLOCK))
313 kproc_exit(0);
314 /* Abnormal termination */
315 kproc_exit(1);
316 }
Cache object: 4be3ec282b95994e660235c5b501991e
|