NFS - implement async state machine for nfs_readrpc_bio()
[dragonfly.git] / sys / vfs / nfs / nfs_iod.c
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
74 void
75 nfssvc_iod_reader(void *arg)
76 {
77         struct nfsmount *nmp = arg;
78         struct nfsm_info *info;
79         struct nfsreq *req;
80         int error;
81
82         if (nmp->nm_rxstate == NFSSVC_INIT)
83                 nmp->nm_rxstate = NFSSVC_PENDING;
84         for (;;) {
85                 if (nmp->nm_rxstate == NFSSVC_WAITING) {
86                         if (TAILQ_FIRST(&nmp->nm_reqq) == NULL &&
87                             TAILQ_FIRST(&nmp->nm_reqrxq) == NULL) {
88                                 tsleep(&nmp->nm_rxstate, 0, "nfsidl", 0);
89                         } else {
90                                 /*
91                                  * This can happen during shutdown, we don't
92                                  * want to hardloop.
93                                  */
94                                 error = nfs_reply(nmp, NULL);
95                                 if (error && error != EWOULDBLOCK) {
96                                         tsleep(&nmp->nm_rxstate, 0,
97                                                 "nfsxxx", hz / 10);
98                                 }
99                         }
100                         continue;
101                 }
102                 if (nmp->nm_rxstate != NFSSVC_PENDING)
103                         break;
104                 nmp->nm_rxstate = NFSSVC_WAITING;
105
106                 /*
107                  * Process requests which have received replies.  Only
108                  * process the post-reply states.  If we get EINPROGRESS
109                  * it means the request went back to an auth or retransmit
110                  * state and we let the iod_writer thread deal with it.
111                  *
112                  * If the request completes we run the info->done call
113                  * to finish up the I/O.
114                  */
115                 while ((req = TAILQ_FIRST(&nmp->nm_reqrxq)) != NULL) {
116                         TAILQ_REMOVE(&nmp->nm_reqrxq, req, r_chain);
117                         info = req->r_info;
118                         KKASSERT(info);
119                         info->error = nfs_request(info,
120                                                   NFSM_STATE_PROCESSREPLY,
121                                                   NFSM_STATE_DONE);
122                         if (info->error == EINPROGRESS) {
123                                 kprintf("rxq: move info %p back to txq\n", info);
124                                 TAILQ_INSERT_TAIL(&nmp->nm_reqtxq, req, r_chain);
125                                 nfssvc_iod_writer_wakeup(nmp);
126                         } else {
127                                 info->done(info);
128                         }
129                 }
130         }
131         nmp->nm_rxthread = NULL;
132         nmp->nm_rxstate = NFSSVC_DONE;
133         wakeup(&nmp->nm_rxthread);
134 }
135
136 /*
137  * The writer sits on the send side of the client's socket and
138  * does both the initial processing of BIOs and also transmission
139  * and retransmission of nfsreq's.
140  *
141  * The writer processes both new BIOs from nm_bioq and retransmit
142  * or state machine jumpbacks from nm_reqtxq
143  */
144 void
145 nfssvc_iod_writer(void *arg)
146 {
147         struct nfsmount *nmp = arg;
148         struct bio *bio;
149         struct nfsreq *req;
150         struct vnode *vp;
151         nfsm_info_t info;
152
153         if (nmp->nm_txstate == NFSSVC_INIT)
154                 nmp->nm_txstate = NFSSVC_PENDING;
155         for (;;) {
156                 if (nmp->nm_txstate == NFSSVC_WAITING) {
157                         tsleep(&nmp->nm_txstate, 0, "nfsidl", 0);
158                         continue;
159                 }
160                 if (nmp->nm_txstate != NFSSVC_PENDING)
161                         break;
162                 nmp->nm_txstate = NFSSVC_WAITING;
163
164                 while (nmp->nm_bioqlen && nmp->nm_reqqlen < 32) {
165                         bio = TAILQ_FIRST(&nmp->nm_bioq);
166                         KKASSERT(bio);
167                         TAILQ_REMOVE(&nmp->nm_bioq, bio, bio_act);
168                         nmp->nm_bioqlen--;
169                         vp = bio->bio_driver_info;
170                         nfs_startio(vp, bio, NULL);
171                 }
172
173                 /*
174                  * Process reauths & retransmits.  If we get an EINPROGRESS
175                  * it means the state transitioned to WAITREPLY or later.
176                  * Otherwise the request completed (probably with an error
177                  * since we didn't get to a replied state).
178                  */
179                 while ((req = TAILQ_FIRST(&nmp->nm_reqtxq)) != NULL) {
180                         TAILQ_REMOVE(&nmp->nm_reqtxq, req, r_chain);
181                         info = req->r_info;
182                         KKASSERT(info);
183                         info->error = nfs_request(info,
184                                                   NFSM_STATE_AUTH,
185                                                   NFSM_STATE_WAITREPLY);
186                         if (info->error == EINPROGRESS) {
187                                 /*
188                                 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, req, r_chain);
189                                 */
190                         } else {
191                                 info->done(info);
192                         }
193                 }
194         }
195         nmp->nm_txthread = NULL;
196         nmp->nm_txstate = NFSSVC_DONE;
197         wakeup(&nmp->nm_txthread);
198 }
199
200 void
201 nfssvc_iod_stop(struct nfsmount *nmp)
202 {
203         nmp->nm_txstate = NFSSVC_STOPPING;
204         wakeup(&nmp->nm_txstate);
205         while (nmp->nm_txthread)
206                 tsleep(&nmp->nm_txthread, 0, "nfssttx", 0);
207
208         nmp->nm_rxstate = NFSSVC_STOPPING;
209         wakeup(&nmp->nm_rxstate);
210         while (nmp->nm_rxthread)
211                 tsleep(&nmp->nm_rxthread, 0, "nfsstrx", 0);
212 }
213
214 void
215 nfssvc_iod_writer_wakeup(struct nfsmount *nmp)
216 {
217         if (nmp->nm_txstate == NFSSVC_WAITING) {
218                 nmp->nm_txstate = NFSSVC_PENDING;
219                 wakeup(&nmp->nm_txstate);
220         }
221 }
222
223 void
224 nfssvc_iod_reader_wakeup(struct nfsmount *nmp)
225 {
226         if (nmp->nm_rxstate == NFSSVC_WAITING) {
227                 nmp->nm_rxstate = NFSSVC_PENDING;
228                 wakeup(&nmp->nm_rxstate);
229         }
230 }