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