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