Whamcloud - gitweb
- more of the locking infrastructure.
[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 EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 ptl_handle_eq_t sent_pkt_eq, rcvd_rep_eq, bulk_source_eq, bulk_sink_eq;
36 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL;
37
38 /*
39  *  Free the packet when it has gone out
40  */
41 static int sent_packet_callback(ptl_event_t *ev, void *data)
42 {
43         ptl_event_t junk_ev;
44
45         ENTRY;
46
47         PtlEQGet(sent_pkt_eq, &junk_ev);
48
49         if (ev->type == PTL_EVENT_SENT) {
50                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
51         } else { 
52                 // XXX make sure we understand all events, including ACK's
53                 CERROR("Unknown event %d\n", ev->type); 
54                 LBUG();
55         }
56
57         EXIT;
58         return 1;
59 }
60
61 /*
62  * Wake up the thread waiting for the reply once it comes in.
63  */
64 static int rcvd_reply_callback(ptl_event_t *ev, void *data)
65 {
66         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
67         ENTRY;
68
69         if (ev->type == PTL_EVENT_PUT) {
70                 rpc->rq_repbuf = ev->mem_desc.start + ev->offset;
71                 barrier();
72                 wake_up_interruptible(&rpc->rq_wait_for_rep);
73         } else { 
74                 // XXX make sure we understand all events, including ACK's
75                 CERROR("Unknown event %d\n", ev->type); 
76                 LBUG();
77         }
78
79         EXIT;
80         return 1;
81 }
82
83 int server_request_callback(ptl_event_t *ev, void *data)
84 {
85         struct ptlrpc_service *service = data;
86         int rc;
87
88         if (ev->rlength != ev->mlength)
89                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
90                        ev->mlength, ev->rlength);
91
92         /* The ME is unlinked when there is less than 1024 bytes free
93          * on its MD.  This ensures we are always able to handle the rpc, 
94          * although the 1024 value is a guess as to the size of a
95          * large rpc (the known safe margin should be determined).
96          *
97          * NOTE: The portals API by default unlinks all MD's associated
98          *       with an ME when it's unlinked.  For now, this behavior
99          *       has been commented out of the portals library so the
100          *       MD can be unlinked when its ref count drops to zero.
101          *       A new MD and ME will then be created that use the same
102          *       kmalloc()'ed memory and inserted at the ring tail.
103          */
104
105         spin_lock(&service->srv_lock); 
106         if ( ev->mem_desc.start != 
107              service->srv_md[service->srv_md_active].start ) {
108                 LBUG();
109         }
110
111         service->srv_ref_count[service->srv_md_active]++;
112         CDEBUG(D_INODE, "event offset %d buf size %d\n", 
113                ev->offset, service->srv_buf_size);
114         if (ev->offset >= (service->srv_buf_size - 1024)) {
115                 CDEBUG(D_INODE, "Unlinking ME %d\n", service->srv_md_active);
116
117                 rc = PtlMEUnlink(service->srv_me_h[service->srv_md_active]);
118                 service->srv_me_h[service->srv_md_active] = 0;
119
120                 if (rc != PTL_OK) {
121                         CERROR("PtlMEUnlink failed - DROPPING soon: %d\n", rc);
122                         LBUG();
123                         spin_unlock(&service->srv_lock); 
124                         return rc;
125                 }
126
127                 service->srv_md_active = (service->srv_md_active + 1) % 
128                         service->srv_ring_length;
129
130                 if (service->srv_me_h[service->srv_md_active] == 0) { 
131                         CERROR("All %d ring ME's are unlinked!\n",
132                                service->srv_ring_length);
133                         LBUG();
134                 }
135         }
136
137         spin_unlock(&service->srv_lock); 
138         if (ev->type == PTL_EVENT_PUT) {
139                 wake_up(&service->srv_waitq);
140         } else {
141                 CERROR("Unexpected event type: %d\n", ev->type);
142         }
143
144         return 0;
145 }
146
147 static int bulk_source_callback(ptl_event_t *ev, void *data)
148 {
149         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
150
151         ENTRY;
152
153         if (ev->type == PTL_EVENT_SENT) {
154                 CDEBUG(D_NET, "got SENT event\n");
155         } else if (ev->type == PTL_EVENT_ACK) {
156                 CDEBUG(D_NET, "got ACK event\n");
157                 bulk->b_flags = PTL_BULK_SENT;
158                 wake_up_interruptible(&bulk->b_waitq);
159         } else {
160                 CERROR("Unexpected event type!\n");
161                 LBUG();
162         }
163
164         EXIT;
165         return 1;
166 }
167
168 static int bulk_sink_callback(ptl_event_t *ev, void *data)
169 {
170         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
171
172         ENTRY;
173
174         if (ev->type == PTL_EVENT_PUT) {
175                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
176                         CERROR("bulkbuf != mem_desc -- why?\n");
177                 bulk->b_flags = PTL_BULK_RCVD;
178                 if (bulk->b_cb != NULL)
179                         bulk->b_cb(bulk, data);
180                 wake_up_interruptible(&bulk->b_waitq);
181         } else {
182                 CERROR("Unexpected event type!\n");
183                 LBUG();
184         }
185
186         /* FIXME: This should happen unconditionally */
187         if (bulk->b_cb != NULL) {
188                 OBD_FREE(bulk, sizeof(*bulk));
189         }
190
191         EXIT;
192         return 1;
193 }
194
195 int ptlrpc_init_portals(void)
196 {
197         int rc;
198         ptl_handle_ni_t ni;
199
200         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
201         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
202         if (socknal_nip == NULL && qswnal_nip == NULL) {
203                 CERROR("get_ni failed: is a NAL module loaded?\n");
204                 return -EIO;
205         }
206
207         /* Use the qswnal if it's there */
208         if (qswnal_nip != NULL)
209                 ni = *qswnal_nip;
210         else
211                 ni = *socknal_nip;
212
213         rc = PtlEQAlloc(ni, 128, sent_packet_callback, NULL, &sent_pkt_eq);
214         if (rc != PTL_OK)
215                 CERROR("PtlEQAlloc failed: %d\n", rc);
216
217         rc = PtlEQAlloc(ni, 128, rcvd_reply_callback, NULL, &rcvd_rep_eq);
218         if (rc != PTL_OK)
219                 CERROR("PtlEQAlloc failed: %d\n", rc);
220
221         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
222         if (rc != PTL_OK)
223                 CERROR("PtlEQAlloc failed: %d\n", rc);
224
225         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
226         if (rc != PTL_OK)
227                 CERROR("PtlEQAlloc failed: %d\n", rc);
228
229         return rc;
230 }
231
232 void ptlrpc_exit_portals(void)
233 {
234         PtlEQFree(sent_pkt_eq);
235         PtlEQFree(rcvd_rep_eq);
236         PtlEQFree(bulk_source_eq);
237         PtlEQFree(bulk_sink_eq);
238
239         if (qswnal_nip != NULL)
240                 inter_module_put("kqswnal_ni");
241         if (socknal_nip != NULL)
242                 inter_module_put("ksocknal_ni");
243 }