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