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