sys/vfs/nfs/nfs_iod.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | /* * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * NFSIOD operations - now built into the kernel. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/proc.h> #include <sys/malloc.h> #include <sys/mount.h> #include <sys/kernel.h> #include <sys/mbuf.h> #include <sys/vnode.h> #include <sys/fcntl.h> #include <sys/protosw.h> #include <sys/resourcevar.h> #include <sys/socket.h> #include <sys/socketvar.h> #include <sys/socketops.h> #include <sys/syslog.h> #include <sys/thread.h> #include <sys/tprintf.h> #include <sys/sysctl.h> #include <sys/signalvar.h> #include <sys/signal2.h> #include <netinet/in.h> #include <netinet/tcp.h> #include "rpcv2.h" #include "nfsproto.h" #include "nfs.h" #include "xdr_subs.h" #include "nfsm_subs.h" #include "nfsmount.h" #include "nfsnode.h" #include "nfsrtt.h" /* * nfs service connection reader thread */ void nfssvc_iod_reader(void *arg) { struct nfsmount *nmp = arg; struct nfsm_info *info; struct nfsreq *req; int error; lwkt_gettoken(&nmp->nm_token); if (nmp->nm_rxstate == NFSSVC_INIT) nmp->nm_rxstate = NFSSVC_PENDING; for (;;) { if (nmp->nm_rxstate == NFSSVC_WAITING) { if (TAILQ_FIRST(&nmp->nm_reqq) == NULL && TAILQ_FIRST(&nmp->nm_reqrxq) == NULL) { tsleep(&nmp->nm_rxstate, 0, "nfsidl", 0); } else { /* * This can happen during shutdown, we don't * want to hardloop. */ error = nfs_reply(nmp, NULL); if (error && error != EWOULDBLOCK) { tsleep(&nmp->nm_rxstate, 0, "nfsxxx", hz / 10); } } continue; } if (nmp->nm_rxstate != NFSSVC_PENDING) break; nmp->nm_rxstate = NFSSVC_WAITING; /* * Process requests which have received replies. Only * process the post-reply states. If we get EINPROGRESS * it means the request went back to an auth or retransmit * state and we let the iod_writer thread deal with it. * * Any lock on the request is strictly temporary due to * MP races (XXX). * * If the request completes we run the info->done call * to finish up the I/O. */ while ((req = TAILQ_FIRST(&nmp->nm_reqrxq)) != NULL) { if (req->r_flags & R_LOCKED) { while (req->r_flags & R_LOCKED) { req->r_flags |= R_WANTED; tsleep(req, 0, "nfstrac", 0); } continue; } TAILQ_REMOVE(&nmp->nm_reqrxq, req, r_chain); info = req->r_info; KKASSERT(info); info->error = nfs_request(info, NFSM_STATE_PROCESSREPLY, NFSM_STATE_DONE); if (info->error == EINPROGRESS) { kprintf("rxq: move info %p back to txq\n", info); TAILQ_INSERT_TAIL(&nmp->nm_reqtxq, req, r_chain); nfssvc_iod_writer_wakeup(nmp); } else { atomic_subtract_int(&nmp->nm_bioqlen, 1); info->done(info); } } } nmp->nm_rxthread = NULL; nmp->nm_rxstate = NFSSVC_DONE; lwkt_reltoken(&nmp->nm_token); wakeup(&nmp->nm_rxthread); } /* * nfs service connection writer thread * * The writer sits on the send side of the client's socket and * does both the initial processing of BIOs and also transmission * and retransmission of nfsreq's. * * The writer processes both new BIOs from nm_bioq and retransmit * or state machine jumpbacks from nm_reqtxq */ void nfssvc_iod_writer(void *arg) { struct nfsmount *nmp = arg; struct bio *bio; struct nfsreq *req; struct vnode *vp; nfsm_info_t info; lwkt_gettoken(&nmp->nm_token); if (nmp->nm_txstate == NFSSVC_INIT) nmp->nm_txstate = NFSSVC_PENDING; for (;;) { if (nmp->nm_txstate == NFSSVC_WAITING) { tsleep(&nmp->nm_txstate, 0, "nfsidl", 0); continue; } if (nmp->nm_txstate != NFSSVC_PENDING) break; nmp->nm_txstate = NFSSVC_WAITING; /* * Eep, we could blow out the mbuf allocator if we just * did everything the kernel wanted us to do. */ while ((bio = TAILQ_FIRST(&nmp->nm_bioq)) != NULL) { if (nmp->nm_reqqlen > nfs_maxasyncbio) break; TAILQ_REMOVE(&nmp->nm_bioq, bio, bio_act); vp = bio->bio_driver_info; nfs_startio(vp, bio, NULL); } /* * Process reauths & retransmits. If we get an EINPROGRESS * it means the state transitioned to WAITREPLY or later. * Otherwise the request completed (probably with an error * since we didn't get to a replied state). */ while ((req = TAILQ_FIRST(&nmp->nm_reqtxq)) != NULL) { TAILQ_REMOVE(&nmp->nm_reqtxq, req, r_chain); info = req->r_info; KKASSERT(info); info->error = nfs_request(info, NFSM_STATE_AUTH, NFSM_STATE_WAITREPLY); if (info->error == EINPROGRESS) { ; } else { atomic_subtract_int(&nmp->nm_bioqlen, 1); info->done(info); } } } nmp->nm_txthread = NULL; nmp->nm_txstate = NFSSVC_DONE; lwkt_reltoken(&nmp->nm_token); wakeup(&nmp->nm_txthread); } void nfssvc_iod_stop1(struct nfsmount *nmp) { nmp->nm_txstate = NFSSVC_STOPPING; nmp->nm_rxstate = NFSSVC_STOPPING; } void nfssvc_iod_stop2(struct nfsmount *nmp) { wakeup(&nmp->nm_txstate); while (nmp->nm_txthread) tsleep(&nmp->nm_txthread, 0, "nfssttx", hz*2); wakeup(&nmp->nm_rxstate); while (nmp->nm_rxthread) tsleep(&nmp->nm_rxthread, 0, "nfsstrx", hz*2); } void nfssvc_iod_writer_wakeup(struct nfsmount *nmp) { if (nmp->nm_txstate == NFSSVC_WAITING) { nmp->nm_txstate = NFSSVC_PENDING; wakeup(&nmp->nm_txstate); } } void nfssvc_iod_reader_wakeup(struct nfsmount *nmp) { if (nmp->nm_rxstate == NFSSVC_WAITING) { nmp->nm_rxstate = NFSSVC_PENDING; wakeup(&nmp->nm_rxstate); } } |