nfs_nfsiod.c revision 99797
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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95 37 */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_nfsiod.c 99797 2002-07-11 17:54:58Z dillon $"); 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/sysproto.h> 45#include <sys/kernel.h> 46#include <sys/sysctl.h> 47#include <sys/file.h> 48#include <sys/filedesc.h> 49#include <sys/vnode.h> 50#include <sys/malloc.h> 51#include <sys/mount.h> 52#include <sys/proc.h> 53#include <sys/bio.h> 54#include <sys/buf.h> 55#include <sys/mbuf.h> 56#include <sys/socket.h> 57#include <sys/socketvar.h> 58#include <sys/domain.h> 59#include <sys/protosw.h> 60#include <sys/namei.h> 61#include <sys/unistd.h> 62#include <sys/kthread.h> 63#include <sys/fcntl.h> 64#include <sys/lockf.h> 65#include <sys/mutex.h> 66 67#include <netinet/in.h> 68#include <netinet/tcp.h> 69#include <nfs/xdr_subs.h> 70#include <nfs/rpcv2.h> 71#include <nfs/nfsproto.h> 72#include <nfsclient/nfs.h> 73#include <nfsclient/nfsm_subs.h> 74#include <nfsclient/nfsmount.h> 75#include <nfsclient/nfsnode.h> 76#include <nfsclient/nfs_lock.h> 77 78static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure"); 79 80static void nfssvc_iod(void *); 81 82static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 83 84SYSCTL_DECL(_vfs_nfs); 85 86/* Maximum number of seconds a nfsiod kthread will sleep before exiting */ 87static unsigned int nfs_iodmaxidle = 120; 88SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, ""); 89 90/* Maximum number of nfsiod kthreads */ 91static unsigned int nfs_iodmax = 20; 92 93/* Minimum number of nfsiod kthreads to keep as spares */ 94static unsigned int nfs_iodmin = 4; 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 if (newmin > nfs_iodmax) 107 return (EINVAL); 108 nfs_iodmin = newmin; 109 if (nfs_numasync >= nfs_iodmin) 110 return (0); 111 /* 112 * If the current number of nfsiod is lower 113 * than the new minimum, create some more. 114 */ 115 for (i = nfs_iodmin - nfs_numasync; i > 0; i--) 116 nfs_nfsiodnew(); 117 return (0); 118} 119SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0, 120 sizeof (nfs_iodmin), sysctl_iodmin, "IU", ""); 121 122 123static int 124sysctl_iodmax(SYSCTL_HANDLER_ARGS) 125{ 126 int error, i; 127 int iod, newmax; 128 129 newmax = nfs_iodmax; 130 error = sysctl_handle_int(oidp, &newmax, 0, req); 131 if (error || (req->newptr == NULL)) 132 return (error); 133 if (newmax > NFS_MAXASYNCDAEMON) 134 return (EINVAL); 135 nfs_iodmax = newmax; 136 if (nfs_numasync <= nfs_iodmax) 137 return (0); 138 /* 139 * If there are some asleep nfsiods that should 140 * exit, wakeup() them so that they check nfs_iodmax 141 * and exit. Those who are active will exit as 142 * soon as they finish I/O. 143 */ 144 iod = nfs_numasync - 1; 145 for (i = 0; i < nfs_numasync - nfs_iodmax; i++) { 146 if (nfs_iodwant[iod]) 147 wakeup((caddr_t)&nfs_iodwant[iod]); 148 iod--; 149 } 150 return (0); 151} 152SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0, 153 sizeof (nfs_iodmax), sysctl_iodmax, "IU", ""); 154 155int 156nfs_nfsiodnew(void) 157{ 158 int error, i; 159 int newiod; 160 161 if (nfs_numasync >= nfs_iodmax) 162 return (-1); 163 newiod = -1; 164 for (i = 0; i < nfs_iodmax; i++) 165 if (nfs_asyncdaemon[i] == 0) { 166 nfs_asyncdaemon[i]++; 167 newiod = i; 168 break; 169 } 170 if (newiod == -1) 171 return (-1); 172 error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID, 173 "nfsiod %d", newiod); 174 if (error) 175 return (-1); 176 nfs_numasync++; 177 return (newiod); 178} 179 180static void 181nfsiod_setup(void *dummy) 182{ 183 int i; 184 int error; 185 186 TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin); 187 /* Silently limit the start number of nfsiod's */ 188 if (nfs_iodmin > NFS_MAXASYNCDAEMON) 189 nfs_iodmin = NFS_MAXASYNCDAEMON; 190 191 for (i = 0; i < nfs_iodmin; i++) { 192 error = nfs_nfsiodnew(); 193 if (error == -1) 194 panic("nfsiod_setup: nfs_nfsiodnew failed"); 195 } 196} 197SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); 198 199static int nfs_defect = 0; 200SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, ""); 201 202int 203nfsclnt(struct thread *td, struct nfsclnt_args *uap) 204{ 205 struct lockd_ans la; 206 int error; 207 208 if ((uap->flag & NFSCLNT_LOCKDANS) != 0) { 209 error = copyin(uap->argp, &la, sizeof(la)); 210 return (error != 0 ? error : nfslockdans(td, &la)); 211 } 212 return EINVAL; 213} 214 215/* 216 * Asynchronous I/O daemons for client nfs. 217 * They do read-ahead and write-behind operations on the block I/O cache. 218 * Returns if we hit the timeout defined by the iodmaxidle sysctl. 219 */ 220static void 221nfssvc_iod(void *instance) 222{ 223 struct buf *bp; 224 struct nfsmount *nmp; 225 int myiod, timo; 226 int error = 0; 227 228 mtx_lock(&Giant); 229 myiod = (int *)instance - nfs_asyncdaemon; 230 /* 231 * Main loop 232 */ 233 for (;;) { 234 while (((nmp = nfs_iodmount[myiod]) == NULL 235 || !TAILQ_FIRST(&nmp->nm_bufq)) 236 && error == 0) { 237 if (myiod >= nfs_iodmax) 238 goto finish; 239 if (nmp) 240 nmp->nm_bufqiods--; 241 nfs_iodwant[myiod] = curthread->td_proc; 242 nfs_iodmount[myiod] = NULL; 243 /* 244 * Always keep at least nfs_iodmin kthreads. 245 */ 246 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz; 247 error = tsleep((caddr_t)&nfs_iodwant[myiod], PWAIT | PCATCH, 248 "nfsidl", timo); 249 } 250 if (error) 251 break; 252 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { 253 /* Take one off the front of the list */ 254 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 255 nmp->nm_bufqlen--; 256 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) { 257 nmp->nm_bufqwant = 0; 258 wakeup(&nmp->nm_bufq); 259 } 260 if (bp->b_iocmd == BIO_READ) 261 (void) nfs_doio(bp, bp->b_rcred, NULL); 262 else 263 (void) nfs_doio(bp, bp->b_wcred, NULL); 264 /* 265 * If there are more than one iod on this mount, then defect 266 * so that the iods can be shared out fairly between the mounts 267 */ 268 if (nfs_defect && nmp->nm_bufqiods > 1) { 269 NFS_DPF(ASYNCIO, 270 ("nfssvc_iod: iod %d defecting from mount %p\n", 271 myiod, nmp)); 272 nfs_iodmount[myiod] = NULL; 273 nmp->nm_bufqiods--; 274 break; 275 } 276 } 277 } 278finish: 279 nfs_asyncdaemon[myiod] = 0; 280 if (nmp) 281 nmp->nm_bufqiods--; 282 nfs_iodwant[myiod] = NULL; 283 nfs_iodmount[myiod] = NULL; 284 nfs_numasync--; 285 if ((error == 0) || (error == EWOULDBLOCK)) 286 kthread_exit(0); 287 /* Abnormal termination */ 288 kthread_exit(1); 289} 290