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 75static MALLOC_DEFINE(M_NFSSVC, "nfsclient_srvsock", "Nfs server structure"); 76 77static void nfssvc_iod(void *); 78 79static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 80 81SYSCTL_DECL(_vfs_oldnfs); 82 83/* Maximum number of seconds a nfsiod kthread will sleep before exiting */ 84static unsigned int nfs_iodmaxidle = 120; 85SYSCTL_UINT(_vfs_oldnfs, 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 */ 89unsigned int nfs_iodmax = 20; 90 91/* Minimum number of nfsiod kthreads to keep as spares */ 92static unsigned int nfs_iodmin = 0; 93 94static int nfs_nfsiodnew_sync(void); 95 96static int 97sysctl_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 mtx_lock(&nfs_iod_mtx); 107 if (newmin > nfs_iodmax) { 108 error = EINVAL; 109 goto out; 110 } 111 nfs_iodmin = newmin; 112 if (nfs_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 - nfs_numasync; i > 0; i--) 119 nfs_nfsiodnew_sync(); 120out: 121 mtx_unlock(&nfs_iod_mtx); 122 return (0); 123} 124SYSCTL_PROC(_vfs_oldnfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0, 125 sizeof (nfs_iodmin), sysctl_iodmin, "IU", 126 "Min number of nfsiod kthreads to keep as spares"); 127 128 129static int 130sysctl_iodmax(SYSCTL_HANDLER_ARGS) 131{ 132 int error, i; 133 int iod, newmax; 134 135 newmax = nfs_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(&nfs_iod_mtx); 142 nfs_iodmax = newmax; 143 if (nfs_numasync <= nfs_iodmax) 144 goto out; 145 /* 146 * If there are some asleep nfsiods that should 147 * exit, wakeup() them so that they check nfs_iodmax 148 * and exit. Those who are active will exit as 149 * soon as they finish I/O. 150 */ 151 iod = nfs_numasync - 1; 152 for (i = 0; i < nfs_numasync - nfs_iodmax; i++) { 153 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE) 154 wakeup(&nfs_iodwant[iod]); 155 iod--; 156 } 157out: 158 mtx_unlock(&nfs_iod_mtx); 159 return (0); 160} 161SYSCTL_PROC(_vfs_oldnfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0, 162 sizeof (nfs_iodmax), sysctl_iodmax, "IU", 163 "Max number of nfsiod kthreads"); 164 165static int 166nfs_nfsiodnew_sync(void) 167{ 168 int error, i; 169 170 mtx_assert(&nfs_iod_mtx, MA_OWNED); 171 for (i = 0; i < nfs_iodmax; i++) { 172 if (nfs_asyncdaemon[i] == 0) { 173 nfs_asyncdaemon[i] = 1; 174 break; 175 } 176 } 177 if (i == nfs_iodmax) 178 return (0); 179 mtx_unlock(&nfs_iod_mtx); 180 error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, 181 RFHIGHPID, 0, "nfsiod %d", i); 182 mtx_lock(&nfs_iod_mtx); 183 if (error == 0) { 184 nfs_numasync++; 185 nfs_iodwant[i] = NFSIOD_AVAILABLE; 186 } else 187 nfs_asyncdaemon[i] = 0; 188 return (error); 189} 190 191void 192nfs_nfsiodnew_tq(__unused void *arg, int pending) 193{ 194 195 mtx_lock(&nfs_iod_mtx); 196 while (pending > 0) { 197 pending--; 198 nfs_nfsiodnew_sync(); 199 } 200 mtx_unlock(&nfs_iod_mtx); 201} 202 203void 204nfs_nfsiodnew(void) 205{ 206 207 mtx_assert(&nfs_iod_mtx, MA_OWNED); 208 taskqueue_enqueue(taskqueue_thread, &nfs_nfsiodnew_task); 209} 210 211static void 212nfsiod_setup(void *dummy) 213{ 214 int error; 215 216 TUNABLE_INT_FETCH("vfs.oldnfs.iodmin", &nfs_iodmin); 217 mtx_lock(&nfs_iod_mtx); 218 /* Silently limit the start number of nfsiod's */ 219 if (nfs_iodmin > NFS_MAXASYNCDAEMON) 220 nfs_iodmin = NFS_MAXASYNCDAEMON; 221 222 while (nfs_numasync < nfs_iodmin) { 223 error = nfs_nfsiodnew_sync(); 224 if (error == -1) 225 panic("nfsiod_setup: nfs_nfsiodnew failed"); 226 } 227 mtx_unlock(&nfs_iod_mtx); 228} 229SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); 230 231static int nfs_defect = 0; 232SYSCTL_INT(_vfs_oldnfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, 233 "Allow nfsiods to migrate serving different mounts"); 234 235/* 236 * Asynchronous I/O daemons for client nfs. 237 * They do read-ahead and write-behind operations on the block I/O cache. 238 * Returns if we hit the timeout defined by the iodmaxidle sysctl. 239 */ 240static void 241nfssvc_iod(void *instance) 242{ 243 struct buf *bp; 244 struct nfsmount *nmp; 245 int myiod, timo; 246 int error = 0; 247 248 mtx_lock(&nfs_iod_mtx); 249 myiod = (int *)instance - nfs_asyncdaemon; 250 /* 251 * Main loop 252 */ 253 for (;;) { 254 while (((nmp = nfs_iodmount[myiod]) == NULL) 255 || !TAILQ_FIRST(&nmp->nm_bufq)) { 256 if (myiod >= nfs_iodmax) 257 goto finish; 258 if (nmp) 259 nmp->nm_bufqiods--; 260 if (nfs_iodwant[myiod] == NFSIOD_NOT_AVAILABLE) 261 nfs_iodwant[myiod] = NFSIOD_AVAILABLE; 262 nfs_iodmount[myiod] = NULL; 263 /* 264 * Always keep at least nfs_iodmin kthreads. 265 */ 266 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz; 267 error = msleep(&nfs_iodwant[myiod], &nfs_iod_mtx, PWAIT | PCATCH, 268 "-", timo); 269 if (error) { 270 nmp = nfs_iodmount[myiod]; 271 /* 272 * Rechecking the nm_bufq closes a rare race where the 273 * nfsiod is woken up at the exact time the idle timeout 274 * fires 275 */ 276 if (nmp && TAILQ_FIRST(&nmp->nm_bufq)) 277 error = 0; 278 break; 279 } 280 } 281 if (error) 282 break; 283 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { 284 int giant_locked = 0; 285 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 <= nfs_numasync) { 290 nmp->nm_bufqwant = 0; 291 wakeup(&nmp->nm_bufq); 292 } 293 mtx_unlock(&nfs_iod_mtx); 294 if (NFS_ISV4(bp->b_vp)) { 295 giant_locked = 1; 296 mtx_lock(&Giant); 297 } 298 if (bp->b_flags & B_DIRECT) { 299 KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set")); 300 (void)nfs_doio_directwrite(bp); 301 } else { 302 if (bp->b_iocmd == BIO_READ) 303 (void) nfs_doio(bp->b_vp, bp, bp->b_rcred, NULL); 304 else 305 (void) nfs_doio(bp->b_vp, bp, bp->b_wcred, NULL); 306 } 307 if (giant_locked) 308 mtx_unlock(&Giant); 309 mtx_lock(&nfs_iod_mtx); 310 /* 311 * Make sure the nmp hasn't been dismounted as soon as 312 * nfs_doio() completes for the last buffer. 313 */ 314 nmp = nfs_iodmount[myiod]; 315 if (nmp == NULL) 316 break; 317 318 /* 319 * If there are more than one iod on this mount, then defect 320 * so that the iods can be shared out fairly between the mounts 321 */ 322 if (nfs_defect && nmp->nm_bufqiods > 1) { 323 NFS_DPF(ASYNCIO, 324 ("nfssvc_iod: iod %d defecting from mount %p\n", 325 myiod, nmp)); 326 nfs_iodmount[myiod] = NULL; 327 nmp->nm_bufqiods--; 328 break; 329 } 330 } 331 } 332finish: 333 nfs_asyncdaemon[myiod] = 0; 334 if (nmp) 335 nmp->nm_bufqiods--; 336 nfs_iodwant[myiod] = NFSIOD_NOT_AVAILABLE; 337 nfs_iodmount[myiod] = NULL; 338 /* Someone may be waiting for the last nfsiod to terminate. */ 339 if (--nfs_numasync == 0) 340 wakeup(&nfs_numasync); 341 mtx_unlock(&nfs_iod_mtx); 342 if ((error == 0) || (error == EWOULDBLOCK)) 343 kproc_exit(0); 344 /* Abnormal termination */ 345 kproc_exit(1); 346} 347