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