]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_nfsiod.c
This commit was generated by cvs2svn to compensate for changes in r155506,
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_nfsiod.c
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$");
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 <rpc/rpcclnt.h>
67
68 #include <nfs/xdr_subs.h>
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsm_subs.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfsclient/nfsnode.h>
75 #include <nfsclient/nfs_lock.h>
76
77 static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
78
79 static void     nfssvc_iod(void *);
80
81 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
82
83 SYSCTL_DECL(_vfs_nfs);
84
85 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
86 static unsigned int nfs_iodmaxidle = 120;
87 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, "");
88
89 /* Maximum number of nfsiod kthreads */
90 unsigned int nfs_iodmax = 20;
91
92 /* Minimum number of nfsiod kthreads to keep as spares */
93 static unsigned int nfs_iodmin = 4;
94
95 static int
96 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
97 {
98         int error, i;
99         int newmin;
100
101         newmin = nfs_iodmin;
102         error = sysctl_handle_int(oidp, &newmin, 0, req);
103         if (error || (req->newptr == NULL))
104                 return (error);
105         if (newmin > nfs_iodmax)
106                 return (EINVAL);
107         nfs_iodmin = newmin;
108         if (nfs_numasync >= nfs_iodmin)
109                 return (0);
110         /*
111          * If the current number of nfsiod is lower
112          * than the new minimum, create some more.
113          */
114         for (i = nfs_iodmin - nfs_numasync; i > 0; i--)
115                 nfs_nfsiodnew();
116         return (0);
117 }
118 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
119     sizeof (nfs_iodmin), sysctl_iodmin, "IU", "");
120
121
122 static int
123 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
124 {
125         int error, i;
126         int iod, newmax;
127
128         newmax = nfs_iodmax;
129         error = sysctl_handle_int(oidp, &newmax, 0, req);
130         if (error || (req->newptr == NULL))
131                 return (error);
132         if (newmax > NFS_MAXASYNCDAEMON)
133                 return (EINVAL);
134         nfs_iodmax = newmax;
135         if (nfs_numasync <= nfs_iodmax)
136                 return (0);
137         /*
138          * If there are some asleep nfsiods that should
139          * exit, wakeup() them so that they check nfs_iodmax
140          * and exit.  Those who are active will exit as
141          * soon as they finish I/O.
142          */
143         iod = nfs_numasync - 1;
144         for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
145                 if (nfs_iodwant[iod])
146                         wakeup(&nfs_iodwant[iod]);
147                 iod--;
148         }
149         return (0);
150 }
151 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
152     sizeof (nfs_iodmax), sysctl_iodmax, "IU", "");
153
154 int
155 nfs_nfsiodnew(void)
156 {
157         int error, i;
158         int newiod;
159
160         if (nfs_numasync >= nfs_iodmax)
161                 return (-1);
162         newiod = -1;
163         for (i = 0; i < nfs_iodmax; i++)
164                 if (nfs_asyncdaemon[i] == 0) {
165                         nfs_asyncdaemon[i]++;
166                         newiod = i;
167                         break;
168                 }
169         if (newiod == -1)
170                 return (-1);
171         error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID,
172             0, "nfsiod %d", newiod);
173         if (error)
174                 return (-1);
175         nfs_numasync++;
176         return (newiod);
177 }
178
179 static void
180 nfsiod_setup(void *dummy)
181 {
182         int i;
183         int error;
184
185         TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
186         /* Silently limit the start number of nfsiod's */
187         if (nfs_iodmin > NFS_MAXASYNCDAEMON)
188                 nfs_iodmin = NFS_MAXASYNCDAEMON;
189
190         for (i = 0; i < nfs_iodmin; i++) {
191                 error = nfs_nfsiodnew();
192                 if (error == -1)
193                         panic("nfsiod_setup: nfs_nfsiodnew failed");
194         }
195 }
196 SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
197
198 static int nfs_defect = 0;
199 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
200
201 /*
202  * Asynchronous I/O daemons for client nfs.
203  * They do read-ahead and write-behind operations on the block I/O cache.
204  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
205  */
206 static void
207 nfssvc_iod(void *instance)
208 {
209         struct buf *bp;
210         struct nfsmount *nmp;
211         int myiod, timo;
212         int error = 0;
213
214         mtx_lock(&Giant);
215         myiod = (int *)instance - nfs_asyncdaemon;
216         /*
217          * Main loop
218          */
219         for (;;) {
220             while (((nmp = nfs_iodmount[myiod]) == NULL
221                    || !TAILQ_FIRST(&nmp->nm_bufq))
222                    && error == 0) {
223                 if (myiod >= nfs_iodmax)
224                         goto finish;
225                 if (nmp)
226                         nmp->nm_bufqiods--;
227                 nfs_iodwant[myiod] = curthread->td_proc;
228                 nfs_iodmount[myiod] = NULL;
229                 /*
230                  * Always keep at least nfs_iodmin kthreads.
231                  */
232                 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
233                 error = tsleep(&nfs_iodwant[myiod], PWAIT | PCATCH,
234                     "-", timo);
235             }
236             if (error)
237                     break;
238             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
239                 /* Take one off the front of the list */
240                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
241                 nmp->nm_bufqlen--;
242                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
243                     nmp->nm_bufqwant = 0;
244                     wakeup(&nmp->nm_bufq);
245                 }
246                 if (bp->b_flags & B_DIRECT) {
247                         KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
248                         (void)nfs_doio_directwrite(bp);
249                 } else {
250                         if (bp->b_iocmd == BIO_READ)
251                                 (void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
252                         else
253                                 (void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
254                 }
255
256                 /*
257                  * If there are more than one iod on this mount, then defect
258                  * so that the iods can be shared out fairly between the mounts
259                  */
260                 if (nfs_defect && nmp->nm_bufqiods > 1) {
261                     NFS_DPF(ASYNCIO,
262                             ("nfssvc_iod: iod %d defecting from mount %p\n",
263                              myiod, nmp));
264                     nfs_iodmount[myiod] = NULL;
265                     nmp->nm_bufqiods--;
266                     break;
267                 }
268             }
269         }
270 finish:
271         nfs_asyncdaemon[myiod] = 0;
272         if (nmp)
273             nmp->nm_bufqiods--;
274         nfs_iodwant[myiod] = NULL;
275         nfs_iodmount[myiod] = NULL;
276         /* Someone may be waiting for the last nfsiod to terminate. */
277         if (--nfs_numasync == 0)
278                 wakeup(&nfs_numasync);
279         mtx_unlock(&Giant);
280         if ((error == 0) || (error == EWOULDBLOCK))
281                 kthread_exit(0);
282         /* Abnormal termination */
283         kthread_exit(1);
284 }