Whamcloud - gitweb
* Landed b_cray_portals_merge.
[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, 2003 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 #ifndef __KERNEL__
25 #include <liblustre.h>
26 #endif
27 #include <linux/obd_support.h>
28 #include <linux/lustre_net.h>
29 #include <linux/lustre_lib.h>
30 #include <linux/obd.h>
31 #include "ptlrpc_internal.h"
32
33 static int ptl_send_buf (ptl_handle_md_t *mdh, void *base, int len, 
34                          ptl_ack_req_t ack, struct ptlrpc_cb_id *cbid,
35                          struct ptlrpc_connection *conn, int portal, __u64 xid)
36 {
37         ptl_process_id_t remote_id;
38         int              rc;
39         int              rc2;
40         ptl_md_t         md;
41         char str[PTL_NALFMT_SIZE];
42         ENTRY;
43
44         LASSERT (portal != 0);
45         LASSERT (conn != NULL);
46         CDEBUG (D_INFO, "conn=%p ni %s nid "LPX64" (%s) on %s\n",
47                 conn, conn->c_peer.peer_ni->pni_name,
48                 conn->c_peer.peer_nid,
49                 portals_nid2str(conn->c_peer.peer_ni->pni_number,
50                                 conn->c_peer.peer_nid, str),
51                 conn->c_peer.peer_ni->pni_name);
52
53         remote_id.nid = conn->c_peer.peer_nid,
54         remote_id.pid = 0;
55
56         md.start     = base;
57         md.length    = len;
58         md.threshold = (ack == PTL_ACK_REQ) ? 2 : 1;
59         md.options   = PTLRPC_MD_OPTIONS;
60         md.user_ptr  = cbid;
61         md.eventq    = conn->c_peer.peer_ni->pni_eq_h;
62
63         if (ack == PTL_ACK_REQ &&
64             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_ACK | OBD_FAIL_ONCE)) {
65                 /* don't ask for the ack to simulate failing client */
66                 ack = PTL_NOACK_REQ;
67                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
68         }
69
70         rc = PtlMDBind (conn->c_peer.peer_ni->pni_ni_h, md, 
71                         PTL_UNLINK, mdh);
72         if (rc != PTL_OK) {
73                 CERROR ("PtlMDBind failed: %d\n", rc);
74                 LASSERT (rc == PTL_NO_SPACE);
75                 RETURN (-ENOMEM);
76         }
77
78         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64"\n",
79                len, portal, xid);
80
81         rc2 = PtlPut (*mdh, ack, remote_id, portal, 0, xid, 0, 0);
82         if (rc != PTL_OK) {
83                 /* We're going to get an UNLINK event when I unlink below,
84                  * which will complete just like any other failed send, so
85                  * I fall through and return success here! */
86                 CERROR("PtlPut("LPU64", %d, "LPD64") failed: %d\n",
87                        remote_id.nid, portal, xid, rc);
88                 rc2 = PtlMDUnlink(*mdh);
89                 LASSERT (rc2 == PTL_OK);
90         }
91
92         RETURN (0);
93 }
94
95 int ptlrpc_start_bulk_transfer (struct ptlrpc_bulk_desc *desc)
96 {
97         int                 rc;
98         int                 rc2;
99         struct ptlrpc_peer *peer;
100         ptl_process_id_t    remote_id;
101         ptl_md_t            md;
102         __u64               xid;
103         ENTRY;
104
105         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_PUT_NET)) 
106                 RETURN(0);
107
108         /* NB no locking required until desc is on the network */
109         LASSERT (!desc->bd_network_rw);
110         LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
111                  desc->bd_type == BULK_GET_SINK);
112         desc->bd_success = 0;
113         peer = &desc->bd_export->exp_connection->c_peer;
114
115         md.user_ptr = &desc->bd_cbid;
116         md.eventq = peer->peer_ni->pni_eq_h;
117         md.threshold = 2; /* SENT and ACK/REPLY */
118         md.options = PTLRPC_MD_OPTIONS;
119         ptlrpc_fill_bulk_md(&md, desc);
120
121         LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
122         LASSERT (desc->bd_cbid.cbid_arg == desc);
123
124         /* NB total length may be 0 for a read past EOF, so we send a 0
125          * length bulk, since the client expects a bulk event. */
126
127         rc = PtlMDBind(peer->peer_ni->pni_ni_h, md,
128                        PTL_UNLINK, &desc->bd_md_h);
129         if (rc != PTL_OK) {
130                 CERROR("PtlMDBind failed: %d\n", rc);
131                 LASSERT (rc == PTL_NO_SPACE);
132                 RETURN(-ENOMEM);
133         }
134
135         /* Client's bulk and reply matchbits are the same */
136         xid = desc->bd_req->rq_xid;
137         remote_id.nid = peer->peer_nid;
138         remote_id.pid = 0;
139
140         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d on %s "
141                "nid "LPX64" pid %d xid "LPX64"\n", desc->bd_iov_count,
142                desc->bd_nob, desc->bd_portal, peer->peer_ni->pni_name,
143                remote_id.nid, remote_id.pid, xid);
144
145         /* Network is about to get at the memory */
146         desc->bd_network_rw = 1;
147
148         if (desc->bd_type == BULK_PUT_SOURCE)
149                 rc = PtlPut (desc->bd_md_h, PTL_ACK_REQ, remote_id,
150                              desc->bd_portal, 0, xid, 0, 0);
151         else
152                 rc = PtlGet (desc->bd_md_h, remote_id,
153                              desc->bd_portal, 0, xid, 0);
154         
155         if (rc != PTL_OK) {
156                 /* Can't send, so we unlink the MD bound above.  The UNLINK
157                  * event this creates will signal completion with failure,
158                  * so we return SUCCESS here! */
159                 CERROR("Transfer("LPU64", %d, "LPX64") failed: %d\n",
160                        remote_id.nid, desc->bd_portal, xid, rc);
161                 rc2 = PtlMDUnlink(desc->bd_md_h);
162                 LASSERT (rc2 == PTL_OK);
163         }
164
165         RETURN(0);
166 }
167
168 void ptlrpc_abort_bulk (struct ptlrpc_bulk_desc *desc)
169 {
170         /* Server side bulk abort. Idempotent. Not thread-safe (i.e. only
171          * serialises with completion callback) */
172         struct l_wait_info lwi;
173         int                rc;
174
175         LASSERT (!in_interrupt ());             /* might sleep */
176
177         if (!ptlrpc_bulk_active(desc))          /* completed or */
178                 return;                         /* never started */
179         
180         /* The unlink ensures the callback happens ASAP and is the last
181          * one.  If it fails, it must be because completion just happened,
182          * but we must still l_wait_event() in this case, to give liblustre
183          * a chance to run server_bulk_callback()*/
184
185         PtlMDUnlink (desc->bd_md_h);
186
187         for (;;) {
188                 /* Network access will complete in finite time but the HUGE
189                  * timeout lets us CWARN for visibility of sluggish NALs */
190                 lwi = LWI_TIMEOUT (300 * HZ, NULL, NULL);
191                 rc = l_wait_event(desc->bd_waitq, 
192                                   !ptlrpc_bulk_active(desc), &lwi);
193                 if (rc == 0)
194                         return;
195
196                 LASSERT(rc == -ETIMEDOUT);
197                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
198         }
199 }
200
201 int ptlrpc_register_bulk (struct ptlrpc_request *req)
202 {
203         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
204         struct ptlrpc_peer *peer;
205         int rc;
206         int rc2;
207         ptl_process_id_t source_id;
208         ptl_handle_me_t  me_h;
209         ptl_md_t         md;
210         ENTRY;
211
212         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_GET_NET)) 
213                 RETURN(0);
214
215         /* NB no locking required until desc is on the network */
216         LASSERT (desc->bd_nob > 0);
217         LASSERT (!desc->bd_network_rw);
218         LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
219         LASSERT (desc->bd_req != NULL);
220         LASSERT (desc->bd_type == BULK_PUT_SINK ||
221                  desc->bd_type == BULK_GET_SOURCE);
222
223         desc->bd_success = 0;
224
225         peer = &desc->bd_import->imp_connection->c_peer;
226
227         md.user_ptr = &desc->bd_cbid;
228         md.eventq = peer->peer_ni->pni_eq_h;
229         md.threshold = 1;                       /* PUT or GET */
230         md.options = PTLRPC_MD_OPTIONS | 
231                      ((desc->bd_type == BULK_GET_SOURCE) ? 
232                       PTL_MD_OP_GET : PTL_MD_OP_PUT);
233         ptlrpc_fill_bulk_md(&md, desc);
234
235         LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
236         LASSERT (desc->bd_cbid.cbid_arg == desc);
237
238         /* XXX Registering the same xid on retried bulk makes my head
239          * explode trying to understand how the original request's bulk
240          * might interfere with the retried request -eeb */
241         LASSERT (!desc->bd_registered || req->rq_xid != desc->bd_last_xid);
242         desc->bd_registered = 1;
243         desc->bd_last_xid = req->rq_xid;
244
245         source_id.nid = desc->bd_import->imp_connection->c_peer.peer_nid;
246         source_id.pid = PTL_PID_ANY;
247
248         rc = PtlMEAttach(peer->peer_ni->pni_ni_h,
249                          desc->bd_portal, source_id, req->rq_xid, 0,
250                          PTL_UNLINK, PTL_INS_AFTER, &me_h);
251         if (rc != PTL_OK) {
252                 CERROR("PtlMEAttach failed: %d\n", rc);
253                 LASSERT (rc == PTL_NO_SPACE);
254                 RETURN (-ENOMEM);
255         }
256
257         /* About to let the network at it... */
258         desc->bd_network_rw = 1;
259         rc = PtlMDAttach(me_h, md, PTL_UNLINK, &desc->bd_md_h);
260         if (rc != PTL_OK) {
261                 CERROR("PtlMDAttach failed: %d\n", rc);
262                 LASSERT (rc == PTL_NO_SPACE);
263                 desc->bd_network_rw = 0;
264                 rc2 = PtlMEUnlink (me_h);
265                 LASSERT (rc2 == PTL_OK);
266                 RETURN (-ENOMEM);
267         }
268
269         CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPX64", "
270                "portal %u on %s\n",
271                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
272                desc->bd_iov_count, desc->bd_nob,
273                req->rq_xid, desc->bd_portal, peer->peer_ni->pni_name);
274         RETURN(0);
275 }
276
277 void ptlrpc_unregister_bulk (struct ptlrpc_request *req)
278 {
279         /* Disconnect a bulk desc from the network. Idempotent. Not
280          * thread-safe (i.e. only interlocks with completion callback). */
281         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
282         wait_queue_head_t       *wq;
283         struct l_wait_info       lwi;
284         int                      rc;
285
286         LASSERT (!in_interrupt ());             /* might sleep */
287
288         if (!ptlrpc_bulk_active(desc))          /* completed or */
289                 return;                         /* never registered */
290         
291         LASSERT (desc->bd_req == req);          /* bd_req NULL until registered */
292
293         /* the unlink ensures the callback happens ASAP and is the last
294          * one.  If it fails, it must be because completion just happened,
295          * but we must still l_wait_event() in this case to give liblustre
296          * a chance to run client_bulk_callback() */
297
298         PtlMDUnlink (desc->bd_md_h);
299         
300         if (desc->bd_req->rq_set != NULL)
301                 wq = &req->rq_set->set_waitq;
302         else
303                 wq = &req->rq_reply_waitq;
304
305         for (;;) {
306                 /* Network access will complete in finite time but the HUGE
307                  * timeout lets us CWARN for visibility of sluggish NALs */
308                 lwi = LWI_TIMEOUT (300 * HZ, NULL, NULL);
309                 rc = l_wait_event(*wq, !ptlrpc_bulk_active(desc), &lwi);
310                 if (rc == 0)
311                         return;
312                 
313                 LASSERT (rc == -ETIMEDOUT);
314                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
315         }
316 }
317
318 int ptlrpc_send_reply (struct ptlrpc_request *req, int may_be_difficult)
319 {
320         struct ptlrpc_service     *svc = req->rq_rqbd->rqbd_srv_ni->sni_service;
321         struct ptlrpc_reply_state *rs = req->rq_reply_state;
322         struct ptlrpc_connection  *conn;
323         int                        rc;
324
325         /* We must already have a reply buffer (only ptlrpc_error() may be
326          * called without one).  We must also have a request buffer which
327          * is either the actual (swabbed) incoming request, or a saved copy
328          * if this is a req saved in target_queue_final_reply(). */
329         LASSERT (req->rq_reqmsg != NULL);
330         LASSERT (rs != NULL);
331         LASSERT (req->rq_repmsg != NULL);
332         LASSERT (may_be_difficult || !rs->rs_difficult);
333         LASSERT (req->rq_repmsg == &rs->rs_msg);
334         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
335         LASSERT (rs->rs_cb_id.cbid_arg == rs);
336
337         LASSERT (req->rq_repmsg != NULL);
338         if (req->rq_type != PTL_RPC_MSG_ERR)
339                 req->rq_type = PTL_RPC_MSG_REPLY;
340
341         req->rq_repmsg->type   = req->rq_type;
342         req->rq_repmsg->status = req->rq_status;
343         req->rq_repmsg->opc    = req->rq_reqmsg->opc;
344
345         if (req->rq_export == NULL) 
346                 conn = ptlrpc_get_connection(&req->rq_peer, NULL);
347         else
348                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
349
350         atomic_inc (&svc->srv_outstanding_replies);
351
352         rc = ptl_send_buf (&rs->rs_md_h, req->rq_repmsg, req->rq_replen,
353                            rs->rs_difficult ? PTL_ACK_REQ : PTL_NOACK_REQ,
354                            &rs->rs_cb_id, conn,
355                            svc->srv_rep_portal, req->rq_xid);
356         if (rc != 0) {
357                 atomic_dec (&svc->srv_outstanding_replies);
358
359                 if (!rs->rs_difficult) {
360                         /* Callers other than target_send_reply() expect me
361                          * to clean up on a comms error */
362                         lustre_free_reply_state (rs);
363                         req->rq_reply_state = NULL;
364                         req->rq_repmsg = NULL;
365                 }
366         }
367         ptlrpc_put_connection(conn);
368         return rc;
369 }
370
371 int ptlrpc_reply (struct ptlrpc_request *req)
372 {
373         return (ptlrpc_send_reply (req, 0));
374 }
375
376 int ptlrpc_error(struct ptlrpc_request *req)
377 {
378         int rc;
379         ENTRY;
380
381         if (!req->rq_repmsg) {
382                 rc = lustre_pack_reply(req, 0, NULL, NULL);
383                 if (rc)
384                         RETURN(rc);
385         }
386
387         req->rq_type = PTL_RPC_MSG_ERR;
388
389         rc = ptlrpc_send_reply (req, 0);
390         RETURN(rc);
391 }
392
393 int ptl_send_rpc(struct ptlrpc_request *request)
394 {
395         int rc;
396         int rc2;
397         struct ptlrpc_connection *connection;
398         unsigned long flags;
399         ptl_process_id_t source_id;
400         ptl_handle_me_t  reply_me_h;
401         ptl_md_t         reply_md;
402         ENTRY;
403
404         LASSERT (request->rq_type == PTL_RPC_MSG_REQUEST);
405
406         /* If this is a re-transmit, we're required to have disengaged
407          * cleanly from the previous attempt */
408         LASSERT (!request->rq_receiving_reply);
409
410         connection = request->rq_import->imp_connection;
411
412         if (request->rq_bulk != NULL) {
413                 rc = ptlrpc_register_bulk (request);
414                 if (rc != 0)
415                         RETURN(rc);
416         }
417
418         request->rq_reqmsg->handle = request->rq_import->imp_remote_handle;
419         request->rq_reqmsg->type = PTL_RPC_MSG_REQUEST;
420         request->rq_reqmsg->conn_cnt = request->rq_import->imp_conn_cnt;
421
422         source_id.nid = connection->c_peer.peer_nid;
423         source_id.pid = PTL_PID_ANY;
424
425         LASSERT (request->rq_replen != 0);
426         if (request->rq_repmsg == NULL)
427                 OBD_ALLOC(request->rq_repmsg, request->rq_replen);
428         if (request->rq_repmsg == NULL)
429                 GOTO(cleanup_bulk, rc = -ENOMEM);
430
431         rc = PtlMEAttach(connection->c_peer.peer_ni->pni_ni_h,
432                          request->rq_reply_portal, /* XXX FIXME bug 249 */
433                          source_id, request->rq_xid, 0, PTL_UNLINK,
434                          PTL_INS_AFTER, &reply_me_h);
435         if (rc != PTL_OK) {
436                 CERROR("PtlMEAttach failed: %d\n", rc);
437                 LASSERT (rc == PTL_NO_SPACE);
438                 GOTO(cleanup_repmsg, rc = -ENOMEM);
439         }
440
441         spin_lock_irqsave (&request->rq_lock, flags);
442         /* If the MD attach succeeds, there _will_ be a reply_in callback */
443         request->rq_receiving_reply = 1;
444         /* Clear any flags that may be present from previous sends. */
445         request->rq_replied = 0;
446         request->rq_err = 0;
447         request->rq_timedout = 0;
448         request->rq_resend = 0;
449         request->rq_restart = 0;
450         spin_unlock_irqrestore (&request->rq_lock, flags);
451
452         reply_md.start     = request->rq_repmsg;
453         reply_md.length    = request->rq_replen;
454         reply_md.threshold = 1;
455         reply_md.options   = PTLRPC_MD_OPTIONS | PTL_MD_OP_PUT;
456         reply_md.user_ptr  = &request->rq_reply_cbid;
457         reply_md.eventq    = connection->c_peer.peer_ni->pni_eq_h;
458
459         rc = PtlMDAttach(reply_me_h, reply_md, PTL_UNLINK, 
460                          &request->rq_reply_md_h);
461         if (rc != PTL_OK) {
462                 CERROR("PtlMDAttach failed: %d\n", rc);
463                 LASSERT (rc == PTL_NO_SPACE);
464                 GOTO(cleanup_me, rc -ENOMEM);
465         }
466
467         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
468                ", portal %u on %s\n",
469                request->rq_replen, request->rq_xid,
470                request->rq_reply_portal,
471                connection->c_peer.peer_ni->pni_name);
472
473         ptlrpc_request_addref(request);        /* +1 ref for the SENT callback */
474
475         request->rq_sent = LTIME_S(CURRENT_TIME);
476         ptlrpc_pinger_sending_on_import(request->rq_import);
477         rc = ptl_send_buf(&request->rq_req_md_h, 
478                           request->rq_reqmsg, request->rq_reqlen,
479                           PTL_NOACK_REQ, &request->rq_req_cbid, 
480                           connection,
481                           request->rq_request_portal,
482                           request->rq_xid);
483         if (rc == 0) {
484                 ptlrpc_lprocfs_rpc_sent(request);
485                 RETURN(rc);
486         }
487
488         ptlrpc_req_finished (request);          /* drop callback ref */
489
490  cleanup_me:
491         /* MEUnlink is safe; the PUT didn't even get off the ground, and
492          * nobody apart from the PUT's target has the right nid+XID to
493          * access the reply buffer. */
494         rc2 = PtlMEUnlink(reply_me_h);
495         LASSERT (rc2 == PTL_OK);
496         /* UNLINKED callback called synchronously */
497         LASSERT (!request->rq_receiving_reply);
498
499  cleanup_repmsg:
500         OBD_FREE(request->rq_repmsg, request->rq_replen);
501         request->rq_repmsg = NULL;
502
503  cleanup_bulk:
504         if (request->rq_bulk != NULL)
505                 ptlrpc_unregister_bulk(request);
506
507         return rc;
508 }
509
510 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
511 {
512         struct ptlrpc_srv_ni    *srv_ni = rqbd->rqbd_srv_ni;
513         struct ptlrpc_service   *service = srv_ni->sni_service;
514         static ptl_process_id_t  match_id = {PTL_NID_ANY, PTL_PID_ANY};
515         int                      rc;
516         ptl_md_t                 md;
517         ptl_handle_me_t          me_h;
518
519         CDEBUG(D_NET, "PtlMEAttach: portal %d on %s\n",
520                service->srv_req_portal, srv_ni->sni_ni->pni_name);
521
522         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_RQBD))
523                 return (-ENOMEM);
524
525         rc = PtlMEAttach(srv_ni->sni_ni->pni_ni_h, service->srv_req_portal,
526                          match_id, 0, ~0, PTL_UNLINK, PTL_INS_AFTER, &me_h);
527         if (rc != PTL_OK) {
528                 CERROR("PtlMEAttach failed: %d\n", rc);
529                 return (-ENOMEM);
530         }
531
532         LASSERT(rqbd->rqbd_refcount == 0);
533         rqbd->rqbd_refcount = 1;
534
535         md.start     = rqbd->rqbd_buffer;
536         md.length    = service->srv_buf_size;
537         md.max_size  = service->srv_max_req_size;
538         md.threshold = PTL_MD_THRESH_INF;
539         md.options   = PTLRPC_MD_OPTIONS | PTL_MD_OP_PUT | PTL_MD_MAX_SIZE;
540         md.user_ptr  = &rqbd->rqbd_cbid;
541         md.eventq    = srv_ni->sni_ni->pni_eq_h;
542         
543         rc = PtlMDAttach(me_h, md, PTL_UNLINK, &rqbd->rqbd_md_h);
544         if (rc == PTL_OK)
545                 return (0);
546
547         CERROR("PtlMDAttach failed: %d; \n", rc);
548         LASSERT (rc == PTL_NO_SPACE);
549         rc = PtlMEUnlink (me_h);
550         LASSERT (rc == PTL_OK);
551         rqbd->rqbd_refcount = 0;
552         
553         return (-ENOMEM);
554 }