xref: /freebsd/sys/fs/nfsclient/nfs_clnfsiod.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from nfs_syscalls.c	8.5 (Berkeley) 3/30/95
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/file.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/namei.h>
55 #include <sys/unistd.h>
56 #include <sys/kthread.h>
57 #include <sys/fcntl.h>
58 #include <sys/lockf.h>
59 #include <sys/mutex.h>
60 #include <sys/taskqueue.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
64 
65 #include <fs/nfs/nfsport.h>
66 #include <fs/nfsclient/nfsmount.h>
67 #include <fs/nfsclient/nfs.h>
68 #include <fs/nfsclient/nfsnode.h>
69 
70 extern struct mtx	ncl_iod_mutex;
71 extern struct task	ncl_nfsiodnew_task;
72 
73 int ncl_numasync;
74 enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
75 struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
76 
77 static void	nfssvc_iod(void *);
78 
79 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
80 
81 SYSCTL_DECL(_vfs_nfs);
82 
83 /* Maximum number of seconds a nfsiod kthread will sleep before exiting */
84 static unsigned int nfs_iodmaxidle = 120;
85 SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0,
86     "Max number of seconds an nfsiod kthread will sleep before exiting");
87 
88 /* Maximum number of nfsiod kthreads */
89 unsigned int ncl_iodmax = 20;
90 
91 /* Minimum number of nfsiod kthreads to keep as spares */
92 static unsigned int nfs_iodmin = 0;
93 
94 static int nfs_nfsiodnew_sync(void);
95 
96 static int
97 sysctl_iodmin(SYSCTL_HANDLER_ARGS)
98 {
99 	int error, i;
100 	int newmin;
101 
102 	newmin = nfs_iodmin;
103 	error = sysctl_handle_int(oidp, &newmin, 0, req);
104 	if (error || (req->newptr == NULL))
105 		return (error);
106 	NFSLOCKIOD();
107 	if (newmin > ncl_iodmax) {
108 		error = EINVAL;
109 		goto out;
110 	}
111 	nfs_iodmin = newmin;
112 	if (ncl_numasync >= nfs_iodmin)
113 		goto out;
114 	/*
115 	 * If the current number of nfsiod is lower
116 	 * than the new minimum, create some more.
117 	 */
118 	for (i = nfs_iodmin - ncl_numasync; i > 0; i--)
119 		nfs_nfsiodnew_sync();
120 out:
121 	NFSUNLOCKIOD();
122 	return (0);
123 }
124 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin,
125     CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
126     0, 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 	NFSLOCKIOD();
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 	NFSUNLOCKIOD();
159 	return (0);
160 }
161 SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax,
162     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof (ncl_iodmax),
163     sysctl_iodmax, "IU",
164     "Max number of nfsiod kthreads");
165 
166 static int
167 nfs_nfsiodnew_sync(void)
168 {
169 	int error, i;
170 
171 	NFSASSERTIOD();
172 	for (i = 0; i < ncl_iodmax; i++) {
173 		if (nfs_asyncdaemon[i] == 0) {
174 			nfs_asyncdaemon[i] = 1;
175 			break;
176 		}
177 	}
178 	if (i == ncl_iodmax)
179 		return (0);
180 	NFSUNLOCKIOD();
181 	error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL,
182 	    RFHIGHPID, 0, "newnfs %d", i);
183 	NFSLOCKIOD();
184 	if (error == 0) {
185 		ncl_numasync++;
186 		ncl_iodwant[i] = NFSIOD_AVAILABLE;
187 	} else
188 		nfs_asyncdaemon[i] = 0;
189 	return (error);
190 }
191 
192 void
193 ncl_nfsiodnew_tq(__unused void *arg, int pending)
194 {
195 
196 	NFSLOCKIOD();
197 	while (pending > 0) {
198 		pending--;
199 		nfs_nfsiodnew_sync();
200 	}
201 	NFSUNLOCKIOD();
202 }
203 
204 void
205 ncl_nfsiodnew(void)
206 {
207 
208 	NFSASSERTIOD();
209 	taskqueue_enqueue(taskqueue_thread, &ncl_nfsiodnew_task);
210 }
211 
212 static void
213 nfsiod_setup(void *dummy)
214 {
215 	int error;
216 
217 	TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin);
218 	nfscl_init();
219 	NFSLOCKIOD();
220 	/* Silently limit the start number of nfsiod's */
221 	if (nfs_iodmin > NFS_MAXASYNCDAEMON)
222 		nfs_iodmin = NFS_MAXASYNCDAEMON;
223 
224 	while (ncl_numasync < nfs_iodmin) {
225 		error = nfs_nfsiodnew_sync();
226 		if (error == -1)
227 			panic("nfsiod_setup: nfs_nfsiodnew failed");
228 	}
229 	NFSUNLOCKIOD();
230 }
231 SYSINIT(newnfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL);
232 
233 static int nfs_defect = 0;
234 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0,
235     "Allow nfsiods to migrate serving different mounts");
236 
237 /*
238  * Asynchronous I/O daemons for client nfs.
239  * They do read-ahead and write-behind operations on the block I/O cache.
240  * Returns if we hit the timeout defined by the iodmaxidle sysctl.
241  */
242 static void
243 nfssvc_iod(void *instance)
244 {
245 	struct buf *bp;
246 	struct nfsmount *nmp;
247 	int myiod, timo;
248 	int error = 0;
249 
250 	NFSLOCKIOD();
251 	myiod = (int *)instance - nfs_asyncdaemon;
252 	/*
253 	 * Main loop
254 	 */
255 	for (;;) {
256 	    while (((nmp = ncl_iodmount[myiod]) == NULL)
257 		   || !TAILQ_FIRST(&nmp->nm_bufq)) {
258 		if (myiod >= ncl_iodmax)
259 			goto finish;
260 		if (nmp)
261 			nmp->nm_bufqiods--;
262 		if (ncl_iodwant[myiod] == NFSIOD_NOT_AVAILABLE)
263 			ncl_iodwant[myiod] = NFSIOD_AVAILABLE;
264 		ncl_iodmount[myiod] = NULL;
265 		/*
266 		 * Always keep at least nfs_iodmin kthreads.
267 		 */
268 		timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz;
269 		error = msleep(&ncl_iodwant[myiod], &ncl_iod_mutex, PWAIT | PCATCH,
270 		    "-", timo);
271 		if (error) {
272 			nmp = ncl_iodmount[myiod];
273 			/*
274 			 * Rechecking the nm_bufq closes a rare race where the
275 			 * nfsiod is woken up at the exact time the idle timeout
276 			 * fires
277 			 */
278 			if (nmp && TAILQ_FIRST(&nmp->nm_bufq))
279 				error = 0;
280 			break;
281 		}
282 	    }
283 	    if (error)
284 		    break;
285 	    while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
286 		/* Take one off the front of the list */
287 		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
288 		nmp->nm_bufqlen--;
289 		if (nmp->nm_bufqwant && nmp->nm_bufqlen <= ncl_numasync) {
290 		    nmp->nm_bufqwant = 0;
291 		    wakeup(&nmp->nm_bufq);
292 		}
293 		NFSUNLOCKIOD();
294 		if (bp->b_flags & B_DIRECT) {
295 			KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set"));
296 			(void)ncl_doio_directwrite(bp);
297 		} else {
298 			if (bp->b_iocmd == BIO_READ)
299 				(void) ncl_doio(bp->b_vp, bp, bp->b_rcred,
300 				    NULL, 0);
301 			else
302 				(void) ncl_doio(bp->b_vp, bp, bp->b_wcred,
303 				    NULL, 0);
304 		}
305 		NFSLOCKIOD();
306 		/*
307 		 * Make sure the nmp hasn't been dismounted as soon as
308 		 * ncl_doio() completes for the last buffer.
309 		 */
310 		nmp = ncl_iodmount[myiod];
311 		if (nmp == NULL)
312 			break;
313 
314 		/*
315 		 * If there are more than one iod on this mount, then defect
316 		 * so that the iods can be shared out fairly between the mounts
317 		 */
318 		if (nfs_defect && nmp->nm_bufqiods > 1) {
319 		    NFS_DPF(ASYNCIO,
320 			    ("nfssvc_iod: iod %d defecting from mount %p\n",
321 			     myiod, nmp));
322 		    ncl_iodmount[myiod] = NULL;
323 		    nmp->nm_bufqiods--;
324 		    break;
325 		}
326 	    }
327 	}
328 finish:
329 	nfs_asyncdaemon[myiod] = 0;
330 	if (nmp)
331 	    nmp->nm_bufqiods--;
332 	ncl_iodwant[myiod] = NFSIOD_NOT_AVAILABLE;
333 	ncl_iodmount[myiod] = NULL;
334 	/* Someone may be waiting for the last nfsiod to terminate. */
335 	if (--ncl_numasync == 0)
336 		wakeup(&ncl_numasync);
337 	NFSUNLOCKIOD();
338 	if ((error == 0) || (error == EWOULDBLOCK))
339 		kproc_exit(0);
340 	/* Abnormal termination */
341 	kproc_exit(1);
342 }
343