Whamcloud - gitweb
Include headers explicitly that were previously included from lustre_net.h.
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.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/obd_support.h>
26 #include <linux/lustre_net.h>
27
28 extern ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq,
29         bulk_source_eq, bulk_sink_eq;
30 static ptl_process_id_t local_id = {PTL_NID_ANY, PTL_PID_ANY};
31
32 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
33 {
34         ENTRY;
35
36         if (bulk->b_flags & PTL_BULK_FL_SENT)
37                 RETURN(1);
38
39         if (sigismember(&(current->pending.signal), SIGKILL) ||
40             sigismember(&(current->pending.signal), SIGINT)) {
41                 bulk->b_flags |= PTL_RPC_FL_INTR;
42                 RETURN(1);
43         }
44
45         CDEBUG(D_NET, "no event yet\n");
46         RETURN(0);
47 }
48
49 static int ptl_send_buf(struct ptlrpc_request *request,
50                         struct ptlrpc_connection *conn, int portal)
51 {
52         int rc;
53         ptl_process_id_t remote_id;
54         ptl_handle_md_t md_h;
55         ptl_ack_req_t ack;
56
57         request->rq_req_md.user_ptr = request;
58
59         switch (request->rq_type) {
60         case PTL_RPC_TYPE_REQUEST:
61                 request->rq_req_md.start = request->rq_reqmsg;
62                 request->rq_req_md.length = request->rq_reqlen;
63                 request->rq_req_md.eventq = request_out_eq;
64                 request->rq_req_md.threshold = 1;
65                 ack = PTL_NOACK_REQ;
66                 break;
67         case PTL_RPC_TYPE_REPLY:
68                 request->rq_req_md.start = request->rq_repmsg;
69                 request->rq_req_md.length = request->rq_replen;
70                 request->rq_req_md.eventq = reply_out_eq;
71                 request->rq_req_md.threshold = 1;
72                 ack = PTL_NOACK_REQ;
73                 break;
74         default:
75                 LBUG();
76                 return -1; /* notreached */
77         }
78         request->rq_req_md.options = PTL_MD_OP_PUT;
79         request->rq_req_md.user_ptr = request;
80
81         rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
82         //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
83         if (rc != 0) {
84                 CERROR("PtlMDBind failed: %d\n", rc);
85                 LBUG();
86                 return rc;
87         }
88
89         remote_id.nid = conn->c_peer.peer_nid;
90         remote_id.pid = 0;
91
92         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %Ld\n",
93                request->rq_req_md.length, portal, request->rq_xid);
94
95         rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_xid,
96                     0, 0);
97         if (rc != PTL_OK) {
98                 CERROR("PtlPut(%Lu, %d, %Ld) failed: %d\n", remote_id.nid,
99                        portal, request->rq_xid, rc);
100                 PtlMDUnlink(md_h);
101         }
102
103         return rc;
104 }
105
106 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *desc)
107 {
108         int rc;
109         struct list_head *tmp, *next;
110         ptl_process_id_t remote_id;
111         ENTRY;
112
113         list_for_each_safe(tmp, next, &desc->b_page_list) {
114                 struct ptlrpc_bulk_page *bulk;
115                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
116
117                 bulk->b_md.start = bulk->b_buf;
118                 bulk->b_md.length = bulk->b_buflen;
119                 bulk->b_md.eventq = bulk_source_eq;
120                 bulk->b_md.threshold = 2; /* SENT and ACK */
121                 bulk->b_md.options = PTL_MD_OP_PUT;
122                 bulk->b_md.user_ptr = bulk;
123
124                 rc = PtlMDBind(desc->b_connection->c_peer.peer_ni, bulk->b_md,
125                                &bulk->b_md_h);
126                 if (rc != 0) {
127                         CERROR("PtlMDBind failed: %d\n", rc);
128                         LBUG();
129                         RETURN(rc);
130                 }
131
132                 remote_id.nid = desc->b_connection->c_peer.peer_nid;
133                 remote_id.pid = 0;
134
135                 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
136                        bulk->b_md.length, desc->b_portal, bulk->b_xid);
137
138                 rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id,
139                             desc->b_portal, 0, bulk->b_xid, 0, 0);
140                 if (rc != PTL_OK) {
141                         CERROR("PtlPut(%Lu, %d, %d) failed: %d\n",
142                                remote_id.nid, desc->b_portal, bulk->b_xid, rc);
143                         PtlMDUnlink(bulk->b_md_h);
144                         LBUG();
145                         RETURN(rc);
146                 }
147         }
148
149         RETURN(0);
150 }
151
152 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *desc)
153 {
154         struct list_head *tmp, *next;
155         int rc;
156         ENTRY;
157
158         list_for_each_safe(tmp, next, &desc->b_page_list) {
159                 struct ptlrpc_bulk_page *bulk;
160                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
161
162                 rc = PtlMEAttach(desc->b_connection->c_peer.peer_ni,
163                                  desc->b_portal, local_id, bulk->b_xid, 0,
164                                  PTL_UNLINK, PTL_INS_AFTER, &bulk->b_me_h);
165                 if (rc != PTL_OK) {
166                         CERROR("PtlMEAttach failed: %d\n", rc);
167                         LBUG();
168                         GOTO(cleanup, rc);
169                 }
170
171                 bulk->b_md.start = bulk->b_buf;
172                 bulk->b_md.length = bulk->b_buflen;
173                 bulk->b_md.threshold = 1;
174                 bulk->b_md.options = PTL_MD_OP_PUT;
175                 bulk->b_md.user_ptr = bulk;
176                 bulk->b_md.eventq = bulk_sink_eq;
177
178                 rc = PtlMDAttach(bulk->b_me_h, bulk->b_md, PTL_UNLINK,
179                                  &bulk->b_md_h);
180                 if (rc != PTL_OK) {
181                         CERROR("PtlMDAttach failed: %d\n", rc);
182                         LBUG();
183                         GOTO(cleanup, rc);
184                 }
185
186                 CDEBUG(D_NET, "Setup bulk sink buffer: %u bytes, xid %u, "
187                        "portal %u\n", bulk->b_buflen, bulk->b_xid,
188                        desc->b_portal);
189         }
190
191         RETURN(0);
192
193  cleanup:
194         ptlrpc_abort_bulk(desc);
195
196         return rc;
197 }
198
199 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
200 {
201         struct list_head *tmp, *next;
202
203         list_for_each_safe(tmp, next, &desc->b_page_list) {
204                 struct ptlrpc_bulk_page *bulk;
205                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
206
207                 /* This should be safe: these handles are initialized to be
208                  * invalid in ptlrpc_prep_bulk_page() */
209                 PtlMDUnlink(bulk->b_md_h);
210                 PtlMEUnlink(bulk->b_me_h);
211         }
212
213         return 0;
214 }
215
216 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
217 {
218         /* FIXME: we need to increment the count of handled events */
219         req->rq_type = PTL_RPC_TYPE_REPLY;
220         //req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
221         //req->rq_repmsg->token = req->rq_connection->c_remote_token;
222         req->rq_repmsg->status = HTON__u32(req->rq_status);
223         req->rq_reqmsg->type = HTON__u32(req->rq_type);
224         return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
225 }
226
227 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
228 {
229         int rc;
230         ENTRY;
231
232         if (req->rq_repmsg) {
233                 CERROR("req already has repmsg\n");
234                 LBUG();
235         }
236
237         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
238         if (rc)
239                 RETURN(rc);
240
241         req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
242
243         rc = ptlrpc_reply(svc, req);
244         RETURN(rc);
245 }
246
247
248 int ptl_send_rpc(struct ptlrpc_request *request)
249 {
250         int rc;
251         char *repbuf;
252
253         ENTRY;
254
255         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
256                 CERROR("wrong packet type sent %d\n",
257                        NTOH__u32(request->rq_reqmsg->type));
258                 LBUG();
259                 RETURN(EINVAL);
260         }
261         if (request->rq_replen == 0) {
262                 CERROR("request->rq_replen is 0!\n");
263                 RETURN(EINVAL);
264         }
265
266         /* request->rq_repmsg is set only when the reply comes in, in
267          * client_packet_callback() */
268         if (request->rq_reply_md.start)
269                 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
270
271         OBD_ALLOC(repbuf, request->rq_replen);
272         if (!repbuf) {
273                 LBUG();
274                 RETURN(ENOMEM);
275         }
276
277         down(&request->rq_client->cli_rpc_sem);
278
279         rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
280                          request->rq_client->cli_reply_portal,
281                          local_id, request->rq_xid, 0, PTL_UNLINK,
282                          PTL_INS_AFTER, &request->rq_reply_me_h);
283         if (rc != PTL_OK) {
284                 CERROR("PtlMEAttach failed: %d\n", rc);
285                 LBUG();
286                 GOTO(cleanup, rc);
287         }
288
289         request->rq_type = PTL_RPC_TYPE_REQUEST;
290         request->rq_reply_md.start = repbuf;
291         request->rq_reply_md.length = request->rq_replen;
292         request->rq_reply_md.threshold = 1;
293         request->rq_reply_md.options = PTL_MD_OP_PUT;
294         request->rq_reply_md.user_ptr = request;
295         request->rq_reply_md.eventq = reply_in_eq;
296
297         rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
298                          PTL_UNLINK, &request->rq_reply_md_h);
299         if (rc != PTL_OK) {
300                 CERROR("PtlMDAttach failed: %d\n", rc);
301                 LBUG();
302                 GOTO(cleanup2, rc);
303         }
304
305         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %Lu, portal %u\n",
306                request->rq_replen, request->rq_xid,
307                request->rq_client->cli_reply_portal);
308
309         rc = ptl_send_buf(request, request->rq_connection,
310                           request->rq_client->cli_request_portal);
311         RETURN(rc);
312
313  cleanup2:
314         PtlMEUnlink(request->rq_reply_me_h);
315  cleanup:
316         OBD_FREE(repbuf, request->rq_replen);
317         up(&request->rq_client->cli_rpc_sem);
318
319         return rc;
320 }
321
322 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
323 {
324         int rc;
325         ptl_md_t dummy;
326         ptl_handle_md_t md_h;
327
328         /* Attach the leading ME on which we build the ring */
329         rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
330                          local_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
331                          &(service->srv_me_h[i]));
332         if (rc != PTL_OK) {
333                 CERROR("PtlMEAttach failed: %d\n", rc);
334                 LBUG();
335         }
336         
337         if (service->srv_ref_count[i])
338                 LBUG();
339
340         dummy.start         = service->srv_buf[i];
341         dummy.length        = service->srv_buf_size;
342         dummy.max_offset    = service->srv_buf_size;
343         dummy.threshold     = PTL_MD_THRESH_INF;
344         dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
345         dummy.user_ptr      = service;
346         dummy.eventq        = service->srv_eq_h;
347         dummy.max_offset    = service->srv_buf_size;
348         
349         rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
350         if (rc != PTL_OK) {
351                 /* cleanup */
352                 CERROR("PtlMDAttach failed: %d\n", rc);
353                 LBUG();
354         }
355 }        
356
357 /* ptl_handled_rpc() should be called by the sleeping process once
358  * it finishes processing an event.  This ensures the ref count is
359  * decremented and that the rpc ring buffer cycles properly.
360  */ 
361 int ptl_handled_rpc(struct ptlrpc_service *service, void *start) 
362 {
363         int index;
364
365         spin_lock(&service->srv_lock);
366         for (index = 0; index < service->srv_ring_length; index++)
367                 if (service->srv_buf[index] == start) 
368                         break;
369
370         if (index == service->srv_ring_length)
371                 LBUG();
372
373         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
374                service->srv_ref_count[index]);
375         service->srv_ref_count[index]--;
376
377         if (service->srv_ref_count[index] < 0)
378                 LBUG();
379
380         if (service->srv_ref_count[index] == 0 &&
381             !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
382                 CDEBUG(D_NET, "relinking %d\n", index); 
383                 ptlrpc_link_svc_me(service, index); 
384         }
385         
386         spin_unlock(&service->srv_lock);
387         return 0;
388 }