NFS - Fix numerous issues with NFS root mounts, primarily for vkernels
[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>
59#include <sys/mutex2.h>
60
61#include <netinet/in.h>
62#include <netinet/tcp.h>
63#include <sys/thread2.h>
64
65#include "rpcv2.h"
66#include "nfsproto.h"
67#include "nfs.h"
68#include "xdr_subs.h"
69#include "nfsm_subs.h"
70#include "nfsmount.h"
71#include "nfsnode.h"
72#include "nfsrtt.h"
73
74void
75nfssvc_iod_reader(void *arg)
76{
77 struct nfsmount *nmp = arg;
edb90c22
MD
78 struct nfsm_info *info;
79 struct nfsreq *req;
80 int error;
52e1cf57
MD
81
82 if (nmp->nm_rxstate == NFSSVC_INIT)
83 nmp->nm_rxstate = NFSSVC_PENDING;
f8565b0f 84 crit_enter();
52e1cf57
MD
85 for (;;) {
86 if (nmp->nm_rxstate == NFSSVC_WAITING) {
edb90c22
MD
87 if (TAILQ_FIRST(&nmp->nm_reqq) == NULL &&
88 TAILQ_FIRST(&nmp->nm_reqrxq) == NULL) {
89 tsleep(&nmp->nm_rxstate, 0, "nfsidl", 0);
90 } else {
91 /*
92 * This can happen during shutdown, we don't
93 * want to hardloop.
94 */
95 error = nfs_reply(nmp, NULL);
96 if (error && error != EWOULDBLOCK) {
97 tsleep(&nmp->nm_rxstate, 0,
98 "nfsxxx", hz / 10);
99 }
100 }
52e1cf57
MD
101 continue;
102 }
103 if (nmp->nm_rxstate != NFSSVC_PENDING)
104 break;
105 nmp->nm_rxstate = NFSSVC_WAITING;
106
edb90c22
MD
107 /*
108 * Process requests which have received replies. Only
109 * process the post-reply states. If we get EINPROGRESS
110 * it means the request went back to an auth or retransmit
111 * state and we let the iod_writer thread deal with it.
112 *
f8565b0f
MD
113 * Any lock on the request is strictly temporary due to
114 * MP races (XXX).
115 *
edb90c22
MD
116 * If the request completes we run the info->done call
117 * to finish up the I/O.
118 */
119 while ((req = TAILQ_FIRST(&nmp->nm_reqrxq)) != NULL) {
f8565b0f
MD
120 if (req->r_flags & R_LOCKED) {
121 while (req->r_flags & R_LOCKED) {
122 req->r_flags |= R_WANTED;
123 tsleep(req, 0, "nfstrac", 0);
124 }
125 continue;
126 }
edb90c22 127 TAILQ_REMOVE(&nmp->nm_reqrxq, req, r_chain);
f8565b0f 128 crit_exit();
edb90c22
MD
129 info = req->r_info;
130 KKASSERT(info);
131 info->error = nfs_request(info,
132 NFSM_STATE_PROCESSREPLY,
133 NFSM_STATE_DONE);
134 if (info->error == EINPROGRESS) {
135 kprintf("rxq: move info %p back to txq\n", info);
136 TAILQ_INSERT_TAIL(&nmp->nm_reqtxq, req, r_chain);
137 nfssvc_iod_writer_wakeup(nmp);
138 } else {
f8565b0f 139 atomic_subtract_int(&nmp->nm_bioqlen, 1);
edb90c22
MD
140 info->done(info);
141 }
f8565b0f 142 crit_enter();
edb90c22 143 }
52e1cf57 144 }
f8565b0f 145 crit_exit();
52e1cf57
MD
146 nmp->nm_rxthread = NULL;
147 nmp->nm_rxstate = NFSSVC_DONE;
148 wakeup(&nmp->nm_rxthread);
149}
150
151/*
152 * The writer sits on the send side of the client's socket and
153 * does both the initial processing of BIOs and also transmission
154 * and retransmission of nfsreq's.
edb90c22
MD
155 *
156 * The writer processes both new BIOs from nm_bioq and retransmit
157 * or state machine jumpbacks from nm_reqtxq
52e1cf57
MD
158 */
159void
160nfssvc_iod_writer(void *arg)
161{
162 struct nfsmount *nmp = arg;
163 struct bio *bio;
edb90c22 164 struct nfsreq *req;
52e1cf57 165 struct vnode *vp;
edb90c22 166 nfsm_info_t info;
52e1cf57
MD
167
168 if (nmp->nm_txstate == NFSSVC_INIT)
169 nmp->nm_txstate = NFSSVC_PENDING;
f8565b0f 170 crit_enter();
52e1cf57
MD
171 for (;;) {
172 if (nmp->nm_txstate == NFSSVC_WAITING) {
173 tsleep(&nmp->nm_txstate, 0, "nfsidl", 0);
174 continue;
175 }
176 if (nmp->nm_txstate != NFSSVC_PENDING)
177 break;
178 nmp->nm_txstate = NFSSVC_WAITING;
179
cc7d050e
MD
180 /*
181 * Eep, we could blow out the mbuf allocator if we just
182 * did everything the kernel wanted us to do.
183 */
f8565b0f 184 while ((bio = TAILQ_FIRST(&nmp->nm_bioq)) != NULL) {
b9a7a2bd 185 if (nmp->nm_reqqlen > nfs_maxasyncbio)
cc7d050e 186 break;
52e1cf57 187 TAILQ_REMOVE(&nmp->nm_bioq, bio, bio_act);
52e1cf57 188 vp = bio->bio_driver_info;
f8565b0f 189 crit_exit();
edb90c22 190 nfs_startio(vp, bio, NULL);
f8565b0f 191 crit_enter();
edb90c22
MD
192 }
193
194 /*
195 * Process reauths & retransmits. If we get an EINPROGRESS
196 * it means the state transitioned to WAITREPLY or later.
197 * Otherwise the request completed (probably with an error
198 * since we didn't get to a replied state).
199 */
200 while ((req = TAILQ_FIRST(&nmp->nm_reqtxq)) != NULL) {
201 TAILQ_REMOVE(&nmp->nm_reqtxq, req, r_chain);
202 info = req->r_info;
203 KKASSERT(info);
f8565b0f 204 crit_exit();
edb90c22
MD
205 info->error = nfs_request(info,
206 NFSM_STATE_AUTH,
207 NFSM_STATE_WAITREPLY);
f8565b0f 208 crit_enter();
edb90c22 209 if (info->error == EINPROGRESS) {
f8565b0f 210 ;
edb90c22 211 } else {
f8565b0f 212 atomic_subtract_int(&nmp->nm_bioqlen, 1);
edb90c22
MD
213 info->done(info);
214 }
52e1cf57
MD
215 }
216 }
f8565b0f 217 crit_exit();
52e1cf57
MD
218 nmp->nm_txthread = NULL;
219 nmp->nm_txstate = NFSSVC_DONE;
220 wakeup(&nmp->nm_txthread);
221}
222
223void
13ddc895 224nfssvc_iod_stop1(struct nfsmount *nmp)
52e1cf57 225{
13ddc895 226 crit_enter();
52e1cf57 227 nmp->nm_txstate = NFSSVC_STOPPING;
13ddc895
MD
228 nmp->nm_rxstate = NFSSVC_STOPPING;
229 crit_exit();
230}
231
232void
233nfssvc_iod_stop2(struct nfsmount *nmp)
234{
52e1cf57
MD
235 wakeup(&nmp->nm_txstate);
236 while (nmp->nm_txthread)
a63246d1 237 tsleep(&nmp->nm_txthread, 0, "nfssttx", hz*2);
52e1cf57
MD
238 wakeup(&nmp->nm_rxstate);
239 while (nmp->nm_rxthread)
a63246d1 240 tsleep(&nmp->nm_rxthread, 0, "nfsstrx", hz*2);
52e1cf57
MD
241}
242
243void
244nfssvc_iod_writer_wakeup(struct nfsmount *nmp)
245{
246 if (nmp->nm_txstate == NFSSVC_WAITING) {
247 nmp->nm_txstate = NFSSVC_PENDING;
248 wakeup(&nmp->nm_txstate);
249 }
250}
edb90c22
MD
251
252void
253nfssvc_iod_reader_wakeup(struct nfsmount *nmp)
254{
255 if (nmp->nm_rxstate == NFSSVC_WAITING) {
256 nmp->nm_rxstate = NFSSVC_PENDING;
257 wakeup(&nmp->nm_rxstate);
258 }
259}