nfs_nfsiod.c revision 89324
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 89324 2002-01-14 02:13:46Z peter $"); 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/* Minimum number of nfsiod kthreads to keep as spares */ 87static unsigned int nfs_iodmin = 4; 88SYSCTL_INT(_vfs_nfs, OID_AUTO, iodmin, CTLFLAG_RW, &nfs_iodmin, 0, ""); 89 90/* Maximum number of seconds a nfsiod kthread will sleep before exiting */ 91static int nfs_iodmaxidle = 120; 92SYSCTL_INT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, ""); 93 94int 95nfs_nfsiodnew(void) 96{ 97 int error, i; 98 int newiod; 99 100 newiod = -1; 101 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 102 if (nfs_asyncdaemon[i] == 0) { 103 nfs_asyncdaemon[i]++; 104 newiod = i; 105 break; 106 } 107 if (newiod == -1) 108 return (-1); 109 error = kthread_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, RFHIGHPID, 110 "nfsiod %d", newiod); 111 if (error) 112 return (-1); 113 nfs_numasync++; 114 return (newiod); 115} 116 117static void 118nfsiod_setup(void *dummy) 119{ 120 int i; 121 int error; 122 123 TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin); 124 /* Silently limit the start number of nfsiod's */ 125 if (nfs_iodmin > NFS_MAXASYNCDAEMON) 126 nfs_iodmin = NFS_MAXASYNCDAEMON; 127 128 for (i = 0; i < nfs_iodmin; i++) { 129 error = nfs_nfsiodnew(); 130 if (error == -1) 131 panic("nfsiod_setup: nfs_nfsiodnew failed"); 132 } 133} 134SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); 135 136static int nfs_defect = 0; 137SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, ""); 138 139int 140nfsclnt(struct thread *td, struct nfsclnt_args *uap) 141{ 142 struct lockd_ans la; 143 int error; 144 145 if ((uap->flag & NFSCLNT_LOCKDANS) != 0) { 146 error = copyin(uap->argp, &la, sizeof(la)); 147 return (error != 0 ? error : nfslockdans(td, &la)); 148 } 149 return EINVAL; 150} 151 152/* 153 * Asynchronous I/O daemons for client nfs. 154 * They do read-ahead and write-behind operations on the block I/O cache. 155 * Returns if we hit the timeout defined by the iodmaxidle sysctl. 156 */ 157static void 158nfssvc_iod(void *instance) 159{ 160 struct buf *bp; 161 struct nfsmount *nmp; 162 int myiod, timo; 163 int error = 0; 164 165 mtx_lock(&Giant); 166 /* 167 * Assign my position or return error if too many already running 168 */ 169 myiod = (int *)instance - nfs_asyncdaemon; 170 /* 171 * Main loop 172 */ 173 for (;;) { 174 while (((nmp = nfs_iodmount[myiod]) == NULL 175 || !TAILQ_FIRST(&nmp->nm_bufq)) 176 && error == 0) { 177 if (nmp) 178 nmp->nm_bufqiods--; 179 nfs_iodwant[myiod] = curthread->td_proc; 180 nfs_iodmount[myiod] = NULL; 181 /* 182 * Always keep at least nfs_iodmin kthreads. 183 */ 184 timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz; 185 error = tsleep((caddr_t)&nfs_iodwant[myiod], PWAIT | PCATCH, 186 "nfsidl", timo); 187 } 188 if (error) 189 break; 190 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { 191 /* Take one off the front of the list */ 192 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 193 nmp->nm_bufqlen--; 194 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) { 195 nmp->nm_bufqwant = 0; 196 wakeup(&nmp->nm_bufq); 197 } 198 if (bp->b_iocmd == BIO_READ) 199 (void) nfs_doio(bp, bp->b_rcred, (struct thread *)0); 200 else 201 (void) nfs_doio(bp, bp->b_wcred, (struct thread *)0); 202 /* 203 * If there are more than one iod on this mount, then defect 204 * so that the iods can be shared out fairly between the mounts 205 */ 206 if (nfs_defect && nmp->nm_bufqiods > 1) { 207 NFS_DPF(ASYNCIO, 208 ("nfssvc_iod: iod %d defecting from mount %p\n", 209 myiod, nmp)); 210 nfs_iodmount[myiod] = NULL; 211 nmp->nm_bufqiods--; 212 break; 213 } 214 } 215 } 216 nfs_asyncdaemon[myiod] = 0; 217 if (nmp) 218 nmp->nm_bufqiods--; 219 nfs_iodwant[myiod] = NULL; 220 nfs_iodmount[myiod] = NULL; 221 nfs_numasync--; 222 if (error == EWOULDBLOCK) 223 kthread_exit(0); 224 /* Abnormal termination */ 225 kthread_exit(1); 226} 227