2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * NFSIOD operations - now built into the kernel.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/kernel.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>
57 #include <sys/signal2.h>
59 #include <netinet/in.h>
60 #include <netinet/tcp.h>
66 #include "nfsm_subs.h"
72 * nfs service connection reader thread
75 nfssvc_iod_reader(void *arg
)
77 struct nfsmount
*nmp
= arg
;
78 struct nfsm_info
*info
;
82 lwkt_gettoken(&nmp
->nm_token
);
84 if (nmp
->nm_rxstate
== NFSSVC_INIT
)
85 nmp
->nm_rxstate
= NFSSVC_PENDING
;
87 if (nmp
->nm_rxstate
== NFSSVC_WAITING
) {
88 if (TAILQ_FIRST(&nmp
->nm_reqq
) == NULL
&&
89 TAILQ_FIRST(&nmp
->nm_reqrxq
) == NULL
) {
90 tsleep(&nmp
->nm_rxstate
, 0, "nfsidl", 0);
93 * This can happen during shutdown, we don't
96 error
= nfs_reply(nmp
, NULL
);
97 if (error
&& error
!= EWOULDBLOCK
) {
98 tsleep(&nmp
->nm_rxstate
, 0,
104 if (nmp
->nm_rxstate
!= NFSSVC_PENDING
)
106 nmp
->nm_rxstate
= NFSSVC_WAITING
;
109 * Process requests which have received replies. Only
110 * process the post-reply states. If we get EINPROGRESS
111 * it means the request went back to an auth or retransmit
112 * state and we let the iod_writer thread deal with it.
114 * Any lock on the request is strictly temporary due to
117 * If the request completes we run the info->done call
118 * to finish up the I/O.
120 while ((req
= TAILQ_FIRST(&nmp
->nm_reqrxq
)) != NULL
) {
121 if (req
->r_flags
& R_LOCKED
) {
122 while (req
->r_flags
& R_LOCKED
) {
123 req
->r_flags
|= R_WANTED
;
124 tsleep(req
, 0, "nfstrac", 0);
128 TAILQ_REMOVE(&nmp
->nm_reqrxq
, req
, r_chain
);
131 info
->error
= nfs_request(info
,
132 NFSM_STATE_PROCESSREPLY
,
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
);
139 atomic_subtract_int(&nmp
->nm_bioqlen
, 1);
144 nmp
->nm_rxthread
= NULL
;
145 nmp
->nm_rxstate
= NFSSVC_DONE
;
147 lwkt_reltoken(&nmp
->nm_token
);
148 wakeup(&nmp
->nm_rxthread
);
152 * nfs service connection writer thread
154 * The writer sits on the send side of the client's socket and
155 * does both the initial processing of BIOs and also transmission
156 * and retransmission of nfsreq's.
158 * The writer processes both new BIOs from nm_bioq and retransmit
159 * or state machine jumpbacks from nm_reqtxq
162 nfssvc_iod_writer(void *arg
)
164 struct nfsmount
*nmp
= arg
;
170 lwkt_gettoken(&nmp
->nm_token
);
172 if (nmp
->nm_txstate
== NFSSVC_INIT
)
173 nmp
->nm_txstate
= NFSSVC_PENDING
;
176 if (nmp
->nm_txstate
== NFSSVC_WAITING
) {
177 tsleep(&nmp
->nm_txstate
, 0, "nfsidl", 0);
180 if (nmp
->nm_txstate
!= NFSSVC_PENDING
)
182 nmp
->nm_txstate
= NFSSVC_WAITING
;
185 * Eep, we could blow out the mbuf allocator if we just
186 * did everything the kernel wanted us to do.
188 while ((bio
= TAILQ_FIRST(&nmp
->nm_bioq
)) != NULL
) {
189 if (nmp
->nm_reqqlen
> nfs_maxasyncbio
)
191 TAILQ_REMOVE(&nmp
->nm_bioq
, bio
, bio_act
);
192 vp
= bio
->bio_driver_info
;
193 nfs_startio(vp
, bio
, NULL
);
197 * Process reauths & retransmits. If we get an EINPROGRESS
198 * it means the state transitioned to WAITREPLY or later.
199 * Otherwise the request completed (probably with an error
200 * since we didn't get to a replied state).
202 while ((req
= TAILQ_FIRST(&nmp
->nm_reqtxq
)) != NULL
) {
203 TAILQ_REMOVE(&nmp
->nm_reqtxq
, req
, r_chain
);
206 info
->error
= nfs_request(info
,
208 NFSM_STATE_WAITREPLY
);
209 if (info
->error
== EINPROGRESS
) {
212 atomic_subtract_int(&nmp
->nm_bioqlen
, 1);
217 nmp
->nm_txthread
= NULL
;
218 nmp
->nm_txstate
= NFSSVC_DONE
;
219 lwkt_reltoken(&nmp
->nm_token
);
220 wakeup(&nmp
->nm_txthread
);
224 nfssvc_iod_stop1(struct nfsmount
*nmp
)
226 nmp
->nm_txstate
= NFSSVC_STOPPING
;
227 nmp
->nm_rxstate
= NFSSVC_STOPPING
;
231 nfssvc_iod_stop2(struct nfsmount
*nmp
)
233 wakeup(&nmp
->nm_txstate
);
234 while (nmp
->nm_txthread
)
235 tsleep(&nmp
->nm_txthread
, 0, "nfssttx", hz
*2);
236 wakeup(&nmp
->nm_rxstate
);
237 while (nmp
->nm_rxthread
)
238 tsleep(&nmp
->nm_rxthread
, 0, "nfsstrx", hz
*2);
242 nfssvc_iod_writer_wakeup(struct nfsmount
*nmp
)
244 if (nmp
->nm_txstate
== NFSSVC_WAITING
) {
245 nmp
->nm_txstate
= NFSSVC_PENDING
;
246 wakeup(&nmp
->nm_txstate
);
251 nfssvc_iod_reader_wakeup(struct nfsmount
*nmp
)
253 if (nmp
->nm_rxstate
== NFSSVC_WAITING
) {
254 nmp
->nm_rxstate
= NFSSVC_PENDING
;
255 wakeup(&nmp
->nm_rxstate
);