1 1.2 pgoyette /* $NetBSD: nfs_clnfsiod.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $ */ 2 1.1 dholland /*- 3 1.1 dholland * Copyright (c) 1989, 1993 4 1.1 dholland * The Regents of the University of California. All rights reserved. 5 1.1 dholland * 6 1.1 dholland * This code is derived from software contributed to Berkeley by 7 1.1 dholland * Rick Macklem at The University of Guelph. 8 1.1 dholland * 9 1.1 dholland * Redistribution and use in source and binary forms, with or without 10 1.1 dholland * modification, are permitted provided that the following conditions 11 1.1 dholland * are met: 12 1.1 dholland * 1. Redistributions of source code must retain the above copyright 13 1.1 dholland * notice, this list of conditions and the following disclaimer. 14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 dholland * notice, this list of conditions and the following disclaimer in the 16 1.1 dholland * documentation and/or other materials provided with the distribution. 17 1.1 dholland * 4. Neither the name of the University nor the names of its contributors 18 1.1 dholland * may be used to endorse or promote products derived from this software 19 1.1 dholland * without specific prior written permission. 20 1.1 dholland * 21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 1.1 dholland * SUCH DAMAGE. 32 1.1 dholland * 33 1.1 dholland * from nfs_syscalls.c 8.5 (Berkeley) 3/30/95 34 1.1 dholland */ 35 1.1 dholland 36 1.1 dholland #include <sys/cdefs.h> 37 1.1 dholland /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clnfsiod.c 249630 2013-04-18 23:20:16Z rmacklem "); */ 38 1.2 pgoyette __RCSID("$NetBSD: nfs_clnfsiod.c,v 1.2 2016/12/13 22:17:33 pgoyette Exp $"); 39 1.1 dholland 40 1.1 dholland #include <sys/param.h> 41 1.1 dholland #include <sys/systm.h> 42 1.1 dholland #include <sys/sysproto.h> 43 1.1 dholland #include <sys/kernel.h> 44 1.1 dholland #include <sys/sysctl.h> 45 1.1 dholland #include <sys/file.h> 46 1.1 dholland #include <sys/filedesc.h> 47 1.1 dholland #include <sys/vnode.h> 48 1.1 dholland #include <sys/malloc.h> 49 1.1 dholland #include <sys/mount.h> 50 1.1 dholland #include <sys/proc.h> 51 1.1 dholland #include <sys/bio.h> 52 1.1 dholland #include <sys/buf.h> 53 1.1 dholland #include <sys/mbuf.h> 54 1.1 dholland #include <sys/socket.h> 55 1.1 dholland #include <sys/socketvar.h> 56 1.1 dholland #include <sys/domain.h> 57 1.1 dholland #include <sys/protosw.h> 58 1.1 dholland #include <sys/namei.h> 59 1.1 dholland #include <sys/unistd.h> 60 1.1 dholland #include <sys/kthread.h> 61 1.1 dholland #include <sys/fcntl.h> 62 1.1 dholland #include <sys/lockf.h> 63 1.1 dholland #include <sys/mutex.h> 64 1.1 dholland #include <sys/taskqueue.h> 65 1.1 dholland 66 1.1 dholland #include <netinet/in.h> 67 1.1 dholland #include <netinet/tcp.h> 68 1.1 dholland 69 1.2 pgoyette #include <fs/nfs/common/nfsport.h> 70 1.2 pgoyette #include <fs/nfs/client/nfsmount.h> 71 1.2 pgoyette #include <fs/nfs/client/nfs.h> 72 1.2 pgoyette #include <fs/nfs/client/nfsnode.h> 73 1.1 dholland 74 1.1 dholland extern struct mtx ncl_iod_mutex; 75 1.1 dholland extern struct task ncl_nfsiodnew_task; 76 1.1 dholland 77 1.1 dholland int ncl_numasync; 78 1.1 dholland enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON]; 79 1.1 dholland struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON]; 80 1.1 dholland 81 1.1 dholland static void nfssvc_iod(void *); 82 1.1 dholland 83 1.1 dholland static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 84 1.1 dholland 85 1.1 dholland SYSCTL_DECL(_vfs_nfs); 86 1.1 dholland 87 1.1 dholland /* Maximum number of seconds a nfsiod kthread will sleep before exiting */ 88 1.1 dholland static unsigned int nfs_iodmaxidle = 120; 89 1.1 dholland SYSCTL_UINT(_vfs_nfs, OID_AUTO, iodmaxidle, CTLFLAG_RW, &nfs_iodmaxidle, 0, 90 1.1 dholland "Max number of seconds an nfsiod kthread will sleep before exiting"); 91 1.1 dholland 92 1.1 dholland /* Maximum number of nfsiod kthreads */ 93 1.1 dholland unsigned int ncl_iodmax = 20; 94 1.1 dholland 95 1.1 dholland /* Minimum number of nfsiod kthreads to keep as spares */ 96 1.1 dholland static unsigned int nfs_iodmin = 0; 97 1.1 dholland 98 1.1 dholland static int nfs_nfsiodnew_sync(void); 99 1.1 dholland 100 1.1 dholland static int 101 1.1 dholland sysctl_iodmin(SYSCTL_HANDLER_ARGS) 102 1.1 dholland { 103 1.1 dholland int error, i; 104 1.1 dholland int newmin; 105 1.1 dholland 106 1.1 dholland newmin = nfs_iodmin; 107 1.1 dholland error = sysctl_handle_int(oidp, &newmin, 0, req); 108 1.1 dholland if (error || (req->newptr == NULL)) 109 1.1 dholland return (error); 110 1.1 dholland mtx_lock(&ncl_iod_mutex); 111 1.1 dholland if (newmin > ncl_iodmax) { 112 1.1 dholland error = EINVAL; 113 1.1 dholland goto out; 114 1.1 dholland } 115 1.1 dholland nfs_iodmin = newmin; 116 1.1 dholland if (ncl_numasync >= nfs_iodmin) 117 1.1 dholland goto out; 118 1.1 dholland /* 119 1.1 dholland * If the current number of nfsiod is lower 120 1.1 dholland * than the new minimum, create some more. 121 1.1 dholland */ 122 1.1 dholland for (i = nfs_iodmin - ncl_numasync; i > 0; i--) 123 1.1 dholland nfs_nfsiodnew_sync(); 124 1.1 dholland out: 125 1.1 dholland mtx_unlock(&ncl_iod_mutex); 126 1.1 dholland return (0); 127 1.1 dholland } 128 1.1 dholland SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmin, CTLTYPE_UINT | CTLFLAG_RW, 0, 129 1.1 dholland sizeof (nfs_iodmin), sysctl_iodmin, "IU", 130 1.1 dholland "Min number of nfsiod kthreads to keep as spares"); 131 1.1 dholland 132 1.1 dholland static int 133 1.1 dholland sysctl_iodmax(SYSCTL_HANDLER_ARGS) 134 1.1 dholland { 135 1.1 dholland int error, i; 136 1.1 dholland int iod, newmax; 137 1.1 dholland 138 1.1 dholland newmax = ncl_iodmax; 139 1.1 dholland error = sysctl_handle_int(oidp, &newmax, 0, req); 140 1.1 dholland if (error || (req->newptr == NULL)) 141 1.1 dholland return (error); 142 1.1 dholland if (newmax > NFS_MAXASYNCDAEMON) 143 1.1 dholland return (EINVAL); 144 1.1 dholland mtx_lock(&ncl_iod_mutex); 145 1.1 dholland ncl_iodmax = newmax; 146 1.1 dholland if (ncl_numasync <= ncl_iodmax) 147 1.1 dholland goto out; 148 1.1 dholland /* 149 1.1 dholland * If there are some asleep nfsiods that should 150 1.1 dholland * exit, wakeup() them so that they check ncl_iodmax 151 1.1 dholland * and exit. Those who are active will exit as 152 1.1 dholland * soon as they finish I/O. 153 1.1 dholland */ 154 1.1 dholland iod = ncl_numasync - 1; 155 1.1 dholland for (i = 0; i < ncl_numasync - ncl_iodmax; i++) { 156 1.1 dholland if (ncl_iodwant[iod] == NFSIOD_AVAILABLE) 157 1.1 dholland wakeup(&ncl_iodwant[iod]); 158 1.1 dholland iod--; 159 1.1 dholland } 160 1.1 dholland out: 161 1.1 dholland mtx_unlock(&ncl_iod_mutex); 162 1.1 dholland return (0); 163 1.1 dholland } 164 1.1 dholland SYSCTL_PROC(_vfs_nfs, OID_AUTO, iodmax, CTLTYPE_UINT | CTLFLAG_RW, 0, 165 1.1 dholland sizeof (ncl_iodmax), sysctl_iodmax, "IU", 166 1.1 dholland "Max number of nfsiod kthreads"); 167 1.1 dholland 168 1.1 dholland static int 169 1.1 dholland nfs_nfsiodnew_sync(void) 170 1.1 dholland { 171 1.1 dholland int error, i; 172 1.1 dholland 173 1.1 dholland mtx_assert(&ncl_iod_mutex, MA_OWNED); 174 1.1 dholland for (i = 0; i < ncl_iodmax; i++) { 175 1.1 dholland if (nfs_asyncdaemon[i] == 0) { 176 1.1 dholland nfs_asyncdaemon[i] = 1; 177 1.1 dholland break; 178 1.1 dholland } 179 1.1 dholland } 180 1.1 dholland if (i == ncl_iodmax) 181 1.1 dholland return (0); 182 1.1 dholland mtx_unlock(&ncl_iod_mutex); 183 1.1 dholland error = kproc_create(nfssvc_iod, nfs_asyncdaemon + i, NULL, 184 1.1 dholland RFHIGHPID, 0, "newnfs %d", i); 185 1.1 dholland mtx_lock(&ncl_iod_mutex); 186 1.1 dholland if (error == 0) { 187 1.1 dholland ncl_numasync++; 188 1.1 dholland ncl_iodwant[i] = NFSIOD_AVAILABLE; 189 1.1 dholland } else 190 1.1 dholland nfs_asyncdaemon[i] = 0; 191 1.1 dholland return (error); 192 1.1 dholland } 193 1.1 dholland 194 1.1 dholland void 195 1.1 dholland ncl_nfsiodnew_tq(__unused void *arg, int pending) 196 1.1 dholland { 197 1.1 dholland 198 1.1 dholland mtx_lock(&ncl_iod_mutex); 199 1.1 dholland while (pending > 0) { 200 1.1 dholland pending--; 201 1.1 dholland nfs_nfsiodnew_sync(); 202 1.1 dholland } 203 1.1 dholland mtx_unlock(&ncl_iod_mutex); 204 1.1 dholland } 205 1.1 dholland 206 1.1 dholland void 207 1.1 dholland ncl_nfsiodnew(void) 208 1.1 dholland { 209 1.1 dholland 210 1.1 dholland mtx_assert(&ncl_iod_mutex, MA_OWNED); 211 1.1 dholland taskqueue_enqueue(taskqueue_thread, &ncl_nfsiodnew_task); 212 1.1 dholland } 213 1.1 dholland 214 1.1 dholland static void 215 1.1 dholland nfsiod_setup(void *dummy) 216 1.1 dholland { 217 1.1 dholland int error; 218 1.1 dholland 219 1.1 dholland TUNABLE_INT_FETCH("vfs.nfs.iodmin", &nfs_iodmin); 220 1.1 dholland nfscl_init(); 221 1.1 dholland mtx_lock(&ncl_iod_mutex); 222 1.1 dholland /* Silently limit the start number of nfsiod's */ 223 1.1 dholland if (nfs_iodmin > NFS_MAXASYNCDAEMON) 224 1.1 dholland nfs_iodmin = NFS_MAXASYNCDAEMON; 225 1.1 dholland 226 1.1 dholland while (ncl_numasync < nfs_iodmin) { 227 1.1 dholland error = nfs_nfsiodnew_sync(); 228 1.1 dholland if (error == -1) 229 1.1 dholland panic("nfsiod_setup: nfs_nfsiodnew failed"); 230 1.1 dholland } 231 1.1 dholland mtx_unlock(&ncl_iod_mutex); 232 1.1 dholland } 233 1.1 dholland SYSINIT(newnfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); 234 1.1 dholland 235 1.1 dholland static int nfs_defect = 0; 236 1.1 dholland SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, 237 1.1 dholland "Allow nfsiods to migrate serving different mounts"); 238 1.1 dholland 239 1.1 dholland /* 240 1.1 dholland * Asynchronous I/O daemons for client nfs. 241 1.1 dholland * They do read-ahead and write-behind operations on the block I/O cache. 242 1.1 dholland * Returns if we hit the timeout defined by the iodmaxidle sysctl. 243 1.1 dholland */ 244 1.1 dholland static void 245 1.1 dholland nfssvc_iod(void *instance) 246 1.1 dholland { 247 1.1 dholland struct buf *bp; 248 1.1 dholland struct nfsmount *nmp; 249 1.1 dholland int myiod, timo; 250 1.1 dholland int error = 0; 251 1.1 dholland 252 1.1 dholland mtx_lock(&ncl_iod_mutex); 253 1.1 dholland myiod = (int *)instance - nfs_asyncdaemon; 254 1.1 dholland /* 255 1.1 dholland * Main loop 256 1.1 dholland */ 257 1.1 dholland for (;;) { 258 1.1 dholland while (((nmp = ncl_iodmount[myiod]) == NULL) 259 1.1 dholland || !TAILQ_FIRST(&nmp->nm_bufq)) { 260 1.1 dholland if (myiod >= ncl_iodmax) 261 1.1 dholland goto finish; 262 1.1 dholland if (nmp) 263 1.1 dholland nmp->nm_bufqiods--; 264 1.1 dholland if (ncl_iodwant[myiod] == NFSIOD_NOT_AVAILABLE) 265 1.1 dholland ncl_iodwant[myiod] = NFSIOD_AVAILABLE; 266 1.1 dholland ncl_iodmount[myiod] = NULL; 267 1.1 dholland /* 268 1.1 dholland * Always keep at least nfs_iodmin kthreads. 269 1.1 dholland */ 270 1.1 dholland timo = (myiod < nfs_iodmin) ? 0 : nfs_iodmaxidle * hz; 271 1.1 dholland error = msleep(&ncl_iodwant[myiod], &ncl_iod_mutex, PWAIT | PCATCH, 272 1.1 dholland "-", timo); 273 1.1 dholland if (error) { 274 1.1 dholland nmp = ncl_iodmount[myiod]; 275 1.1 dholland /* 276 1.1 dholland * Rechecking the nm_bufq closes a rare race where the 277 1.1 dholland * nfsiod is woken up at the exact time the idle timeout 278 1.1 dholland * fires 279 1.1 dholland */ 280 1.1 dholland if (nmp && TAILQ_FIRST(&nmp->nm_bufq)) 281 1.1 dholland error = 0; 282 1.1 dholland break; 283 1.1 dholland } 284 1.1 dholland } 285 1.1 dholland if (error) 286 1.1 dholland break; 287 1.1 dholland while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { 288 1.1 dholland /* Take one off the front of the list */ 289 1.1 dholland TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 290 1.1 dholland nmp->nm_bufqlen--; 291 1.1 dholland if (nmp->nm_bufqwant && nmp->nm_bufqlen <= ncl_numasync) { 292 1.1 dholland nmp->nm_bufqwant = 0; 293 1.1 dholland wakeup(&nmp->nm_bufq); 294 1.1 dholland } 295 1.1 dholland mtx_unlock(&ncl_iod_mutex); 296 1.1 dholland if (bp->b_flags & B_DIRECT) { 297 1.1 dholland KASSERT((bp->b_iocmd == BIO_WRITE), ("nfscvs_iod: BIO_WRITE not set")); 298 1.1 dholland (void)ncl_doio_directwrite(bp); 299 1.1 dholland } else { 300 1.1 dholland if (bp->b_iocmd == BIO_READ) 301 1.1 dholland (void) ncl_doio(bp->b_vp, bp, bp->b_rcred, 302 1.1 dholland NULL, 0); 303 1.1 dholland else 304 1.1 dholland (void) ncl_doio(bp->b_vp, bp, bp->b_wcred, 305 1.1 dholland NULL, 0); 306 1.1 dholland } 307 1.1 dholland mtx_lock(&ncl_iod_mutex); 308 1.1 dholland /* 309 1.1 dholland * Make sure the nmp hasn't been dismounted as soon as 310 1.1 dholland * ncl_doio() completes for the last buffer. 311 1.1 dholland */ 312 1.1 dholland nmp = ncl_iodmount[myiod]; 313 1.1 dholland if (nmp == NULL) 314 1.1 dholland break; 315 1.1 dholland 316 1.1 dholland /* 317 1.1 dholland * If there are more than one iod on this mount, then defect 318 1.1 dholland * so that the iods can be shared out fairly between the mounts 319 1.1 dholland */ 320 1.1 dholland if (nfs_defect && nmp->nm_bufqiods > 1) { 321 1.1 dholland NFS_DPF(ASYNCIO, 322 1.1 dholland ("nfssvc_iod: iod %d defecting from mount %p\n", 323 1.1 dholland myiod, nmp)); 324 1.1 dholland ncl_iodmount[myiod] = NULL; 325 1.1 dholland nmp->nm_bufqiods--; 326 1.1 dholland break; 327 1.1 dholland } 328 1.1 dholland } 329 1.1 dholland } 330 1.1 dholland finish: 331 1.1 dholland nfs_asyncdaemon[myiod] = 0; 332 1.1 dholland if (nmp) 333 1.1 dholland nmp->nm_bufqiods--; 334 1.1 dholland ncl_iodwant[myiod] = NFSIOD_NOT_AVAILABLE; 335 1.1 dholland ncl_iodmount[myiod] = NULL; 336 1.1 dholland /* Someone may be waiting for the last nfsiod to terminate. */ 337 1.1 dholland if (--ncl_numasync == 0) 338 1.1 dholland wakeup(&ncl_numasync); 339 1.1 dholland mtx_unlock(&ncl_iod_mutex); 340 1.1 dholland if ((error == 0) || (error == EWOULDBLOCK)) 341 1.1 dholland kproc_exit(0); 342 1.1 dholland /* Abnormal termination */ 343 1.1 dholland kproc_exit(1); 344 1.1 dholland } 345