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