]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/nfsclient/nfs_nfsiod.c
MFC r212851:
[FreeBSD/stable/8.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 #include <sys/taskqueue.h>
63
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
66
67 #include <nfs/xdr_subs.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfsclient/nfsm_subs.h>
71 #include <nfsclient/nfsmount.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfs/nfs_lock.h>
74
75 static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure");
76
77 static void     nfssvc_iod(void *);
78
79 struct nfsiod_str {
80         STAILQ_ENTRY(nfsiod_str) ni_links;
81         int *ni_inst;
82         int ni_iod;
83         int ni_error;
84         int ni_done;
85 };
86 static STAILQ_HEAD(, nfsiod_str) nfsiodhead =
87     STAILQ_HEAD_INITIALIZER(nfsiodhead);
88
89 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
90
91 SYSCTL_DECL(_vfs_nfs);
92
93 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
94 static unsigned int nfs_iodmaxidle = 120;
95 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
96     "Max number of seconds an nfsiod kthread will sleep before exiting");
97
98 /* Maximum number of nfsiod kthreads */
99 unsigned int nfs_iodmax = 20;
100
101 /* Minimum number of nfsiod kthreads to keep as spares */
102 static unsigned int nfs_iodmin = 0;
103
104 static int
105 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
106 {
107         int error, i;
108         int newmin;
109
110         newmin = nfs_iodmin;
111         error = sysctl_handle_int(oidp, &newmin, 0, req);
112         if (error || (req->newptr == NULL))
113                 return (error);
114         mtx_lock(&nfs_iod_mtx);
115         if (newmin > nfs_iodmax) {
116                 error = EINVAL;
117                 goto out;
118         }
119         nfs_iodmin = newmin;
120         if (nfs_numasync >= nfs_iodmin)
121                 goto out;
122         /*
123          * If the current number of nfsiod is lower
124          * than the new minimum, create some more.
125          */
126         for (i = nfs_iodmin - nfs_numasync; i > 0; i--)
127                 nfs_nfsiodnew(0);
128 out:
129         mtx_unlock(&nfs_iod_mtx);       
130         return (0);
131 }
132 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0,
133     sizeof (nfs_iodmin), sysctl_iodmin, "IU",
134     "Min number of nfsiod kthreads to keep as spares");
135
136
137 static int
138 sysctl_iodmax(SYSCTL_HANDLER_ARGS)
139 {
140         int error, i;
141         int iod, newmax;
142
143         newmax = nfs_iodmax;
144         error = sysctl_handle_int(oidp, &newmax, 0, req);
145         if (error || (req->newptr == NULL))
146                 return (error);
147         if (newmax > NFS_MAXASYNCDAEMON)
148                 return (EINVAL);
149         mtx_lock(&nfs_iod_mtx);
150         nfs_iodmax = newmax;
151         if (nfs_numasync <= nfs_iodmax)
152                 goto out;
153         /*
154          * If there are some asleep nfsiods that should
155          * exit, wakeup() them so that they check nfs_iodmax
156          * and exit.  Those who are active will exit as
157          * soon as they finish I/O.
158          */
159         iod = nfs_numasync - 1;
160         for (i = 0; i < nfs_numasync - nfs_iodmax; i++) {
161                 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE)
162                         wakeup(&nfs_iodwant[iod]);
163                 iod--;
164         }
165 out:
166         mtx_unlock(&nfs_iod_mtx);
167         return (0);
168 }
169 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0,
170     sizeof (nfs_iodmax), sysctl_iodmax, "IU",
171     "Max number of nfsiod kthreads");
172
173 void
174 nfs_nfsiodnew_tq(__unused void *arg, int pending)
175 {
176         struct nfsiod_str *nip;
177
178         mtx_lock(&nfs_iod_mtx);
179         while ((nip = STAILQ_FIRST(&nfsiodhead)) != NULL) {
180                 STAILQ_REMOVE_HEAD(&nfsiodhead, ni_links);
181                 mtx_unlock(&nfs_iod_mtx);
182                 nip->ni_error = kproc_create(nfssvc_iod, nip->ni_inst, NULL,
183                     RFHIGHPID, 0, "nfsiod %d", nip->ni_iod);
184                 nip->ni_done = 1;
185                 mtx_lock(&nfs_iod_mtx);
186                 wakeup(nip);
187         }
188         mtx_unlock(&nfs_iod_mtx);
189 }
190
191 int
192 nfs_nfsiodnew(int set_iodwant)
193 {
194         int error, i;
195         int newiod;
196         struct nfsiod_str *nip;
197
198         if (nfs_numasync >= nfs_iodmax)
199                 return (-1);
200         newiod = -1;
201         for (i = 0; i < nfs_iodmax; i++)
202                 if (nfs_asyncdaemon[i] == 0) {
203                         nfs_asyncdaemon[i]++;
204                         newiod = i;
205                         break;
206                 }
207         if (newiod == -1)
208                 return (-1);
209         if (set_iodwant > 0)
210                 nfs_iodwant[i] = NFSIOD_CREATED_FOR_NFS_ASYNCIO;
211         mtx_unlock(&nfs_iod_mtx);
212         nip = malloc(sizeof(*nip), M_TEMP, M_WAITOK | M_ZERO);
213         nip->ni_inst = nfs_asyncdaemon + i;
214         nip->ni_iod = newiod;
215         mtx_lock(&nfs_iod_mtx);
216         STAILQ_INSERT_TAIL(&nfsiodhead, nip, ni_links);
217         taskqueue_enqueue(taskqueue_thread, &nfs_nfsiodnew_task);
218         while (!nip->ni_done)
219                 mtx_sleep(nip, &nfs_iod_mtx, 0, "niwt", 0);
220         error = nip->ni_error;
221         free(nip, M_TEMP);
222         if (error) {
223                 if (set_iodwant > 0)
224                         nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
225                 return (-1);
226         }
227         nfs_numasync++;
228         return (newiod);
229 }
230
231 static void
232 nfsiod_setup(void *dummy)
233 {
234         int i;
235         int error;
236
237         TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
238         mtx_lock(&nfs_iod_mtx);
239         /* Silently limit the start number of nfsiod's */
240         if (nfs_iodmin > NFS_MAXASYNCDAEMON)
241                 nfs_iodmin = NFS_MAXASYNCDAEMON;
242
243         for (i = 0; i < nfs_iodmin; i++) {
244                 error = nfs_nfsiodnew(0);
245                 if (error == -1)
246                         panic("nfsiod_setup: nfs_nfsiodnew failed");
247         }
248         mtx_unlock(&nfs_iod_mtx);
249 }
250 SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
251
252 static int nfs_defect = 0;
253 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
254     "Allow nfsiods to migrate serving different mounts");
255
256 /*
257  * Asynchronous I/O daemons for client nfs.
258  * They do read-ahead and write-behind operations on the block I/O cache.
259  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
260  */
261 static void
262 nfssvc_iod(void *instance)
263 {
264         struct buf *bp;
265         struct nfsmount *nmp;
266         int myiod, timo;
267         int error = 0;
268
269         mtx_lock(&nfs_iod_mtx);
270         myiod = (int *)instance - nfs_asyncdaemon;
271         /*
272          * Main loop
273          */
274         for (;;) {
275             while (((nmp = nfs_iodmount[myiod]) == NULL)
276                    || !TAILQ_FIRST(&nmp->nm_bufq)) {
277                 if (myiod >= nfs_iodmax)
278                         goto finish;
279                 if (nmp)
280                         nmp->nm_bufqiods--;
281                 if (nfs_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
282                         nfs_iodwant[myiod] = NFSIOD_AVAILABLE;
283                 nfs_iodmount[myiod] = NULL;
284                 /*
285                  * Always keep at least nfs_iodmin kthreads.
286                  */
287                 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
288                 error = msleep(&nfs_iodwant[myiod], &nfs_iod_mtx, PWAIT | PCATCH,
289                     "-", timo);
290                 if (error) {
291                         nmp = nfs_iodmount[myiod];
292                         /*
293                          * Rechecking the nm_bufq closes a rare race where the 
294                          * nfsiod is woken up at the exact time the idle timeout
295                          * fires
296                          */
297                         if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
298                                 error = 0;
299                         break;
300                 }
301             }
302             if (error)
303                     break;
304             while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
305                 int giant_locked = 0;
306                     
307                 /* Take one off the front of the list */
308                 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
309                 nmp->nm_bufqlen--;
310                 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
311                     nmp->nm_bufqwant = 0;
312                     wakeup(&nmp->nm_bufq);
313                 }
314                 mtx_unlock(&nfs_iod_mtx);
315                 if (NFS_ISV4(bp->b_vp)) {
316                         giant_locked = 1;
317                         mtx_lock(&Giant);
318                 }
319                 if (bp->b_flags & B_DIRECT) {
320                         KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
321                         (void)nfs_doio_directwrite(bp);
322                 } else {
323                         if (bp->b_iocmd == BIO_READ)
324                                 (void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL);
325                         else
326                                 (void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL);
327                 }
328                 if (giant_locked)
329                         mtx_unlock(&Giant);
330                 mtx_lock(&nfs_iod_mtx);
331                 /*
332                  * If there are more than one iod on this mount, then defect
333                  * so that the iods can be shared out fairly between the mounts
334                  */
335                 if (nfs_defect && nmp->nm_bufqiods > 1) {
336                     NFS_DPF(ASYNCIO,
337                             ("nfssvc_iod: iod %d defecting from mount %p\n",
338                              myiod, nmp));
339                     nfs_iodmount[myiod] = NULL;
340                     nmp->nm_bufqiods--;
341                     break;
342                 }
343             }
344         }
345 finish:
346         nfs_asyncdaemon[myiod] = 0;
347         if (nmp)
348             nmp->nm_bufqiods--;
349         nfs_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
350         nfs_iodmount[myiod] = NULL;
351         /* Someone may be waiting for the last nfsiod to terminate. */
352         if (--nfs_numasync == 0)
353                 wakeup(&nfs_numasync);
354         mtx_unlock(&nfs_iod_mtx);
355         if ((error == 0) || (error == EWOULDBLOCK))
356                 kproc_exit(0);
357         /* Abnormal termination */
358         kproc_exit(1);
359 }