]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clnfsiod.c
Remove unused header.
[FreeBSD/FreeBSD.git] / sys / fs / nfsclient / nfs_clnfsiod.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  * 3. 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$");
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/vnode.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/namei.h>
56 #include <sys/unistd.h>
57 #include <sys/kthread.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
60 #include <sys/mutex.h>
61 #include <sys/taskqueue.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
71 extern struct mtx       ncl_iod_mutex;
72 extern struct task      ncl_nfsiodnew_task;
73
74 int ncl_numasync;
75 enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
76 struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
77
78 static void     nfssvc_iod(void *);
79
80 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
81
82 SYSCTL_DECL(_vfs_nfs);
83
84 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
85 static unsigned int nfs_iodmaxidle = 120;
86 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
87     "Max number of seconds an nfsiod kthread will sleep before exiting");
88
89 /* Maximum number of nfsiod kthreads */
90 unsigned int ncl_iodmax = 20;
91
92 /* Minimum number of nfsiod kthreads to keep as spares */
93 static unsigned int nfs_iodmin = 0;
94
95 static int nfs_nfsiodnew_sync(void);
96
97 static int
98 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
99 {
100         int error, i;
101         int newmin;
102
103         newmin = nfs_iodmin;
104         error = sysctl_handle_int(oidp, &newmin, 0, req);
105         if (error || (req->newptr == NULL))
106                 return (error);
107         mtx_lock(&ncl_iod_mutex);
108         if (newmin > ncl_iodmax) {
109                 error = EINVAL;
110                 goto out;
111         }
112         nfs_iodmin = newmin;
113         if (ncl_numasync >= nfs_iodmin)
114                 goto out;
115         /*
116          * If the current number of nfsiod is lower
117          * than the new minimum, create some more.
118          */
119         for (i = nfs_iodmin - ncl_numasync; i > 0; i--)
120                 nfs_nfsiodnew_sync();
121 out:
122         mtx_unlock(&ncl_iod_mutex);     
123         return (0);
124 }
125 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
126     sizeof (nfs_iodmin), sysctl_iodmin, "IU",
127     "Min number of nfsiod kthreads to keep as spares");
128
129 static int
130 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
131 {
132         int error, i;
133         int iod, newmax;
134
135         newmax = ncl_iodmax;
136         error = sysctl_handle_int(oidp, &newmax, 0, req);
137         if (error || (req->newptr == NULL))
138                 return (error);
139         if (newmax > NFS_MAXASYNCDAEMON)
140                 return (EINVAL);
141         mtx_lock(&ncl_iod_mutex);
142         ncl_iodmax = newmax;
143         if (ncl_numasync <= ncl_iodmax)
144                 goto out;
145         /*
146          * If there are some asleep nfsiods that should
147          * exit, wakeup() them so that they check ncl_iodmax
148          * and exit.  Those who are active will exit as
149          * soon as they finish I/O.
150          */
151         iod = ncl_numasync - 1;
152         for (i = 0; i < ncl_numasync - ncl_iodmax; i++) {
153                 if (ncl_iodwant[iod] == NFSIOD_AVAILABLE)
154                         wakeup(&ncl_iodwant[iod]);
155                 iod--;
156         }
157 out:
158         mtx_unlock(&ncl_iod_mutex);
159         return (0);
160 }
161 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
162     sizeof (ncl_iodmax), sysctl_iodmax, "IU",
163     "Max number of nfsiod kthreads");
164
165 static int
166 nfs_nfsiodnew_sync(void)
167 {
168         int error, i;
169
170         mtx_assert(&ncl_iod_mutex, MA_OWNED);
171         for (i = 0; i < ncl_iodmax; i++) {
172                 if (nfs_asyncdaemon[i] == 0) {
173                         nfs_asyncdaemon[i] = 1;
174                         break;
175                 }
176         }
177         if (i == ncl_iodmax)
178                 return (0);
179         mtx_unlock(&ncl_iod_mutex);
180         error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL,
181             RFHIGHPID, 0, "newnfs %d", i);
182         mtx_lock(&ncl_iod_mutex);
183         if (error == 0) {
184                 ncl_numasync++;
185                 ncl_iodwant[i] = NFSIOD_AVAILABLE;
186         } else
187                 nfs_asyncdaemon[i] = 0;
188         return (error);
189 }
190
191 void
192 ncl_nfsiodnew_tq(__unused void *arg, int pending)
193 {
194
195         mtx_lock(&ncl_iod_mutex);
196         while (pending > 0) {
197                 pending--;
198                 nfs_nfsiodnew_sync();
199         }
200         mtx_unlock(&ncl_iod_mutex);
201 }
202
203 void
204 ncl_nfsiodnew(void)
205 {
206
207         mtx_assert(&ncl_iod_mutex, MA_OWNED);
208         taskqueue_enqueue(taskqueue_thread, &ncl_nfsiodnew_task);
209 }
210
211 static void
212 nfsiod_setup(void *dummy)
213 {
214         int error;
215
216         TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
217         nfscl_init();
218         mtx_lock(&ncl_iod_mutex);
219         /* Silently limit the start number of nfsiod's */
220         if (nfs_iodmin > NFS_MAXASYNCDAEMON)
221                 nfs_iodmin = NFS_MAXASYNCDAEMON;
222
223         while (ncl_numasync < nfs_iodmin) {
224                 error = nfs_nfsiodnew_sync();
225                 if (error == -1)
226                         panic("nfsiod_setup: nfs_nfsiodnew failed");
227         }
228         mtx_unlock(&ncl_iod_mutex);
229 }
230 SYSINIT(newnfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
231
232 static int nfs_defect = 0;
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
234     "Allow nfsiods to migrate serving different mounts");
235
236 /*
237  * Asynchronous I/O daemons for client nfs.
238  * They do read-ahead and write-behind operations on the block I/O cache.
239  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
240  */
241 static void
242 nfssvc_iod(void *instance)
243 {
244         struct buf *bp;
245         struct nfsmount *nmp;
246         int myiod, timo;
247         int error = 0;
248
249         mtx_lock(&ncl_iod_mutex);
250         myiod = (int *)instance - nfs_asyncdaemon;
251         /*
252          * Main loop
253          */
254         for (;;) {
255             while (((nmp = ncl_iodmount[myiod]) == NULL)
256                    || !TAILQ_FIRST(&nmp->nm_bufq)) {
257                 if (myiod >= ncl_iodmax)
258                         goto finish;
259                 if (nmp)
260                         nmp->nm_bufqiods--;
261                 if (ncl_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
262                         ncl_iodwant[myiod] = NFSIOD_AVAILABLE;
263                 ncl_iodmount[myiod] = NULL;
264                 /*
265                  * Always keep at least nfs_iodmin kthreads.
266                  */
267                 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
268                 error = msleep(&ncl_iodwant[myiod], &ncl_iod_mutex, PWAIT | PCATCH,
269                     "-", timo);
270                 if (error) {
271                         nmp = ncl_iodmount[myiod];
272                         /*
273                          * Rechecking the nm_bufq closes a rare race where the 
274                          * nfsiod is woken up at the exact time the idle timeout
275                          * fires
276                          */
277                         if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
278                                 error = 0;
279                         break;
280                 }
281             }
282             if (error)
283                     break;
284             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
285                 /* Take one off the front of the list */
286                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
287                 nmp->nm_bufqlen--;
288                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= ncl_numasync) {
289                     nmp->nm_bufqwant = 0;
290                     wakeup(&nmp->nm_bufq);
291                 }
292                 mtx_unlock(&ncl_iod_mutex);
293                 if (bp->b_flags & B_DIRECT) {
294                         KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
295                         (void)ncl_doio_directwrite(bp);
296                 } else {
297                         if (bp->b_iocmd == BIO_READ)
298                                 (void) ncl_doio(bp->b_vp, bp, bp->b_rcred,
299                                     NULL, 0);
300                         else
301                                 (void) ncl_doio(bp->b_vp, bp, bp->b_wcred,
302                                     NULL, 0);
303                 }
304                 mtx_lock(&ncl_iod_mutex);
305                 /*
306                  * Make sure the nmp hasn't been dismounted as soon as
307                  * ncl_doio() completes for the last buffer.
308                  */
309                 nmp = ncl_iodmount[myiod];
310                 if (nmp == NULL)
311                         break;
312
313                 /*
314                  * If there are more than one iod on this mount, then defect
315                  * so that the iods can be shared out fairly between the mounts
316                  */
317                 if (nfs_defect && nmp->nm_bufqiods > 1) {
318                     NFS_DPF(ASYNCIO,
319                             ("nfssvc_iod: iod %d defecting from mount %p\n",
320                              myiod, nmp));
321                     ncl_iodmount[myiod] = NULL;
322                     nmp->nm_bufqiods--;
323                     break;
324                 }
325             }
326         }
327 finish:
328         nfs_asyncdaemon[myiod] = 0;
329         if (nmp)
330             nmp->nm_bufqiods--;
331         ncl_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
332         ncl_iodmount[myiod] = NULL;
333         /* Someone may be waiting for the last nfsiod to terminate. */
334         if (--ncl_numasync == 0)
335                 wakeup(&ncl_numasync);
336         mtx_unlock(&ncl_iod_mutex);
337         if ((error == 0) || (error == EWOULDBLOCK))
338                 kproc_exit(0);
339         /* Abnormal termination */
340         kproc_exit(1);
341 }