nfs_nfsiod.c revision 122698
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 122698 2003-11-14 20:54:10Z alfred $"); 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 70#include <rpc/rpcclnt.h> 71 72#include <nfs/xdr_subs.h> 73#include <nfs/rpcv2.h> 74#include <nfs/nfsproto.h> 75#include <nfsclient/nfs.h> 76#include <nfsclient/nfsm_subs.h> 77#include <nfsclient/nfsmount.h> 78#include <nfsclient/nfsnode.h> 79#include <nfsclient/nfs_lock.h> 80 81static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure"); 82 83static void nfssvc_iod(void *); 84 85static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 86 87SYSCTL_DECL(_vfs_nfs); 88 89/* Maximum number of seconds a nfsiod kthread will sleep before exiting */ 90static unsigned int nfs_iodmaxidle = 120; 91SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, ""); 92 93/* Maximum number of nfsiod kthreads */ 94static unsigned int nfs_iodmax = 20; 95 96/* Minimum number of nfsiod kthreads to keep as spares */ 97static unsigned int nfs_iodmin = 4; 98 99static int 100sysctl_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 if (newmin > nfs_iodmax) 110 return (EINVAL); 111 nfs_iodmin = newmin; 112 if (nfs_numasync >= nfs_iodmin) 113 return (0); 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(); 120 return (0); 121} 122SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0, 123 sizeof (nfs_iodmin), sysctl_iodmin, "IU", ""); 124 125 126static int 127sysctl_iodmax(SYSCTL_HANDLER_ARGS) 128{ 129 int error, i; 130 int iod, newmax; 131 132 newmax = nfs_iodmax; 133 error = sysctl_handle_int(oidp, &newmax, 0, req); 134 if (error || (req->newptr == NULL)) 135 return (error); 136 if (newmax > NFS_MAXASYNCDAEMON) 137 return (EINVAL); 138 nfs_iodmax = newmax; 139 if (nfs_numasync <= nfs_iodmax) 140 return (0); 141 /* 142 * If there are some asleep nfsiods that should 143 * exit, wakeup() them so that they check nfs_iodmax 144 * and exit. Those who are active will exit as 145 * soon as they finish I/O. 146 */ 147 iod = nfs_numasync - 1; 148 for (i = 0; i < nfs_numasync - nfs_iodmax; i++) { 149 if (nfs_iodwant[iod]) 150 wakeup(&nfs_iodwant[iod]); 151 iod--; 152 } 153 return (0); 154} 155SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0, 156 sizeof (nfs_iodmax), sysctl_iodmax, "IU", ""); 157 158int 159nfs_nfsiodnew(void) 160{ 161 int error, i; 162 int newiod; 163 164 if (nfs_numasync >= nfs_iodmax) 165 return (-1); 166 newiod = -1; 167 for (i = 0; i < nfs_iodmax; i++) 168 if (nfs_asyncdaemon[i] == 0) { 169 nfs_asyncdaemon[i]++; 170 newiod = i; 171 break; 172 } 173 if (newiod == -1) 174 return (-1); 175 error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID, 176 0, "nfsiod %d", newiod); 177 if (error) 178 return (-1); 179 nfs_numasync++; 180 return (newiod); 181} 182 183static void 184nfsiod_setup(void *dummy) 185{ 186 int i; 187 int error; 188 189 TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin); 190 /* Silently limit the start number of nfsiod's */ 191 if (nfs_iodmin > NFS_MAXASYNCDAEMON) 192 nfs_iodmin = NFS_MAXASYNCDAEMON; 193 194 for (i = 0; i < nfs_iodmin; i++) { 195 error = nfs_nfsiodnew(); 196 if (error == -1) 197 panic("nfsiod_setup: nfs_nfsiodnew failed"); 198 } 199} 200SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); 201 202static int nfs_defect = 0; 203SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, ""); 204 205int 206nfsclnt(struct thread *td, struct nfsclnt_args *uap) 207{ 208 struct lockd_ans la; 209 int error; 210 211 if ((uap->flag & NFSCLNT_LOCKDANS) != 0) { 212 error = copyin(uap->argp, &la, sizeof(la)); 213 return (error != 0 ? error : nfslockdans(td, &la)); 214 } 215 return EINVAL; 216} 217 218/* 219 * Asynchronous I/O daemons for client nfs. 220 * They do read-ahead and write-behind operations on the block I/O cache. 221 * Returns if we hit the timeout defined by the iodmaxidle sysctl. 222 */ 223static void 224nfssvc_iod(void *instance) 225{ 226 struct buf *bp; 227 struct nfsmount *nmp; 228 int myiod, timo; 229 int error = 0; 230 231 mtx_lock(&Giant); 232 myiod = (int *)instance - nfs_asyncdaemon; 233 /* 234 * Main loop 235 */ 236 for (;;) { 237 while (((nmp = nfs_iodmount[myiod]) == NULL 238 || !TAILQ_FIRST(&nmp->nm_bufq)) 239 && error == 0) { 240 if (myiod >= nfs_iodmax) 241 goto finish; 242 if (nmp) 243 nmp->nm_bufqiods--; 244 nfs_iodwant[myiod] = curthread->td_proc; 245 nfs_iodmount[myiod] = NULL; 246 /* 247 * Always keep at least nfs_iodmin kthreads. 248 */ 249 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz; 250 error = tsleep(&nfs_iodwant[myiod], PWAIT | PCATCH, 251 "-", timo); 252 } 253 if (error) 254 break; 255 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { 256 /* Take one off the front of the list */ 257 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 258 nmp->nm_bufqlen--; 259 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) { 260 nmp->nm_bufqwant = 0; 261 wakeup(&nmp->nm_bufq); 262 } 263 if (bp->b_iocmd == BIO_READ) 264 (void) nfs_doio(bp, bp->b_rcred, NULL); 265 else 266 (void) nfs_doio(bp, bp->b_wcred, NULL); 267 /* 268 * If there are more than one iod on this mount, then defect 269 * so that the iods can be shared out fairly between the mounts 270 */ 271 if (nfs_defect && nmp->nm_bufqiods > 1) { 272 NFS_DPF(ASYNCIO, 273 ("nfssvc_iod: iod %d defecting from mount %p\n", 274 myiod, nmp)); 275 nfs_iodmount[myiod] = NULL; 276 nmp->nm_bufqiods--; 277 break; 278 } 279 } 280 } 281finish: 282 nfs_asyncdaemon[myiod] = 0; 283 if (nmp) 284 nmp->nm_bufqiods--; 285 nfs_iodwant[myiod] = NULL; 286 nfs_iodmount[myiod] = NULL; 287 nfs_numasync--; 288 if ((error == 0) || (error == EWOULDBLOCK)) 289 kthread_exit(0); 290 /* Abnormal termination */ 291 kthread_exit(1); 292} 293