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