Whamcloud - gitweb
Prevent C-c and C-z from locking us up, and make most of our waits
[fs/lustre-release.git] / lustre / ptlrpc / events.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/module.h>
26 #include <linux/lustre_net.h>
27
28 ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq, bulk_source_eq,
29         bulk_sink_eq;
30 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL;
31
32 /*
33  *  Free the packet when it has gone out
34  */
35 static int request_out_callback(ptl_event_t *ev)
36 {
37         ENTRY;
38
39         if (ev->type != PTL_EVENT_SENT) {
40                 // XXX make sure we understand all events, including ACK's
41                 CERROR("Unknown event %d\n", ev->type);
42                 LBUG();
43         }
44
45         RETURN(1);
46 }
47
48
49 /*
50  *  Free the packet when it has gone out
51  */
52 static int reply_out_callback(ptl_event_t *ev)
53 {
54         ENTRY;
55
56         if (ev->type == PTL_EVENT_SENT) {
57                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
58         } else {
59                 // XXX make sure we understand all events, including ACK's
60                 CERROR("Unknown event %d\n", ev->type);
61                 LBUG();
62         }
63
64         RETURN(1);
65 }
66
67 /*
68  * Wake up the thread waiting for the reply once it comes in.
69  */
70 static int reply_in_callback(ptl_event_t *ev)
71 {
72         struct ptlrpc_request *req = ev->mem_desc.user_ptr;
73         ENTRY;
74
75         if (req->rq_xid == 0x5a5a5a5a) {
76                 CERROR("Reply received for freed request!  Probably a missing "
77                        "ptlrpc_abort()\n");
78                 LBUG();
79         }
80
81         if (req->rq_xid != ev->match_bits) {
82                 CERROR("Reply packet for wrong request\n");
83                 LBUG(); 
84         }
85
86         if (ev->type == PTL_EVENT_PUT) {
87                 req->rq_repmsg = ev->mem_desc.start + ev->offset;
88                 barrier();
89                 wake_up(&req->rq_wait_for_rep);
90         } else {
91                 // XXX make sure we understand all events, including ACK's
92                 CERROR("Unknown event %d\n", ev->type);
93                 LBUG();
94         }
95
96         RETURN(1);
97 }
98
99 int request_in_callback(ptl_event_t *ev)
100 {
101         struct ptlrpc_service *service = ev->mem_desc.user_ptr;
102
103         if (ev->rlength != ev->mlength)
104                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
105                        ev->mlength, ev->rlength);
106
107         if (ev->type == PTL_EVENT_PUT)
108                 wake_up(&service->srv_waitq);
109         else
110                 CERROR("Unexpected event type: %d\n", ev->type);
111
112         return 0;
113 }
114
115 static int bulk_source_callback(ptl_event_t *ev)
116 {
117         struct ptlrpc_bulk_page *bulk = ev->mem_desc.user_ptr;
118         struct ptlrpc_bulk_desc *desc = bulk->b_desc;
119         ENTRY;
120
121         if (ev->type == PTL_EVENT_SENT) {
122                 CDEBUG(D_NET, "got SENT event\n");
123         } else if (ev->type == PTL_EVENT_ACK) {
124                 CDEBUG(D_NET, "got ACK event\n");
125                 if (bulk->b_cb != NULL)
126                         bulk->b_cb(bulk);
127                 if (atomic_dec_and_test(&desc->b_pages_remaining)) {
128                         desc->b_flags |= PTL_BULK_FL_SENT;
129                         wake_up(&desc->b_waitq);
130                         if (desc->b_cb != NULL)
131                                 desc->b_cb(desc, desc->b_cb_data);
132                 }
133         } else {
134                 CERROR("Unexpected event type!\n");
135                 LBUG();
136         }
137
138         RETURN(1);
139 }
140
141 static int bulk_sink_callback(ptl_event_t *ev)
142 {
143         struct ptlrpc_bulk_page *bulk = ev->mem_desc.user_ptr;
144         struct ptlrpc_bulk_desc *desc = bulk->b_desc;
145         ENTRY;
146
147         if (ev->type == PTL_EVENT_PUT) {
148                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
149                         CERROR("bulkbuf != mem_desc -- why?\n");
150                 if (bulk->b_cb != NULL)
151                         bulk->b_cb(bulk);
152                 if (atomic_dec_and_test(&desc->b_pages_remaining)) {
153                         desc->b_flags |= PTL_BULK_FL_RCVD;
154                         wake_up(&desc->b_waitq);
155                         if (desc->b_cb != NULL)
156                                 desc->b_cb(desc, desc->b_cb_data);
157                 }
158         } else {
159                 CERROR("Unexpected event type!\n");
160                 LBUG();
161         }
162
163         RETURN(1);
164 }
165
166 int ptlrpc_init_portals(void)
167 {
168         int rc;
169         ptl_handle_ni_t ni;
170
171         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
172         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
173         if (socknal_nip == NULL && qswnal_nip == NULL) {
174                 CERROR("get_ni failed: is a NAL module loaded?\n");
175                 return -EIO;
176         }
177
178         /* Use the qswnal if it's there */
179         if (qswnal_nip != NULL)
180                 ni = *qswnal_nip;
181         else
182                 ni = *socknal_nip;
183
184         rc = PtlEQAlloc(ni, 128, request_out_callback, &request_out_eq);
185         if (rc != PTL_OK)
186                 CERROR("PtlEQAlloc failed: %d\n", rc);
187
188         rc = PtlEQAlloc(ni, 128, reply_out_callback, &reply_out_eq);
189         if (rc != PTL_OK)
190                 CERROR("PtlEQAlloc failed: %d\n", rc);
191
192         rc = PtlEQAlloc(ni, 128, reply_in_callback, &reply_in_eq);
193         if (rc != PTL_OK)
194                 CERROR("PtlEQAlloc failed: %d\n", rc);
195
196         rc = PtlEQAlloc(ni, 128, bulk_source_callback, &bulk_source_eq);
197         if (rc != PTL_OK)
198                 CERROR("PtlEQAlloc failed: %d\n", rc);
199
200         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, &bulk_sink_eq);
201         if (rc != PTL_OK)
202                 CERROR("PtlEQAlloc failed: %d\n", rc);
203
204         return rc;
205 }
206
207 void ptlrpc_exit_portals(void)
208 {
209         PtlEQFree(request_out_eq);
210         PtlEQFree(reply_out_eq);
211         PtlEQFree(reply_in_eq);
212         PtlEQFree(bulk_source_eq);
213         PtlEQFree(bulk_sink_eq);
214
215         if (qswnal_nip != NULL)
216                 inter_module_put("kqswnal_ni");
217         if (socknal_nip != NULL)
218                 inter_module_put("ksocknal_ni");
219 }