Whamcloud - gitweb
240b226deb5e4ce61fb86df1511046c5cedea49f
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_RPC
38 #ifndef __KERNEL__
39 #include <liblustre.h>
40 #endif
41 #include <obd_support.h>
42 #include <lustre_net.h>
43 #include <lustre_lib.h>
44 #include <obd.h>
45 #include "ptlrpc_internal.h"
46
47 /**
48  * Helper function. Sends \a len bytes from \a base at offset \a offset
49  * over \a conn connection to portal \a portal.
50  * Returns 0 on success or error code.
51  */
52 static int ptl_send_buf (lnet_handle_md_t *mdh, void *base, int len,
53                          lnet_ack_req_t ack, struct ptlrpc_cb_id *cbid,
54                          struct ptlrpc_connection *conn, int portal, __u64 xid,
55                          unsigned int offset)
56 {
57         int              rc;
58         lnet_md_t         md;
59         ENTRY;
60
61         LASSERT (portal != 0);
62         LASSERT (conn != NULL);
63         CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
64         md.start     = base;
65         md.length    = len;
66         md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
67         md.options   = PTLRPC_MD_OPTIONS;
68         md.user_ptr  = cbid;
69         md.eq_handle = ptlrpc_eq_h;
70
71         if (unlikely(ack == LNET_ACK_REQ &&
72                      OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
73                 /* don't ask for the ack to simulate failing client */
74                 ack = LNET_NOACK_REQ;
75         }
76
77         rc = LNetMDBind (md, LNET_UNLINK, mdh);
78         if (unlikely(rc != 0)) {
79                 CERROR ("LNetMDBind failed: %d\n", rc);
80                 LASSERT (rc == -ENOMEM);
81                 RETURN (-ENOMEM);
82         }
83
84         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64", offset %u\n",
85                len, portal, xid, offset);
86
87         rc = LNetPut (conn->c_self, *mdh, ack,
88                       conn->c_peer, portal, xid, offset, 0);
89         if (unlikely(rc != 0)) {
90                 int rc2;
91                 /* We're going to get an UNLINK event when I unlink below,
92                  * which will complete just like any other failed send, so
93                  * I fall through and return success here! */
94                 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
95                        libcfs_id2str(conn->c_peer), portal, xid, rc);
96                 rc2 = LNetMDUnlink(*mdh);
97                 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
98         }
99
100         RETURN (0);
101 }
102
103 /**
104  * Starts bulk transfer for descriptor \a desc
105  * Returns 0 on success or error code.
106  */
107 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc)
108 {
109         struct ptlrpc_connection *conn = desc->bd_export->exp_connection;
110         int                       rc;
111         int                       rc2;
112         lnet_md_t                 md;
113         __u64                     xid;
114         ENTRY;
115
116         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
117                 RETURN(0);
118
119         /* NB no locking required until desc is on the network */
120         LASSERT (!desc->bd_network_rw);
121         LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
122                  desc->bd_type == BULK_GET_SINK);
123         desc->bd_success = 0;
124
125         md.user_ptr = &desc->bd_cbid;
126         md.eq_handle = ptlrpc_eq_h;
127         md.threshold = 2; /* SENT and ACK/REPLY */
128         md.options = PTLRPC_MD_OPTIONS;
129         ptlrpc_fill_bulk_md(&md, desc);
130
131         LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
132         LASSERT (desc->bd_cbid.cbid_arg == desc);
133
134         /* NB total length may be 0 for a read past EOF, so we send a 0
135          * length bulk, since the client expects a bulk event. */
136
137         rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_md_h);
138         if (rc != 0) {
139                 CERROR("LNetMDBind failed: %d\n", rc);
140                 LASSERT (rc == -ENOMEM);
141                 RETURN(-ENOMEM);
142         }
143
144         /* Client's bulk and reply matchbits are the same */
145         xid = desc->bd_req->rq_xid;
146         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
147                "id %s xid "LPX64"\n", desc->bd_iov_count,
148                desc->bd_nob, desc->bd_portal,
149                libcfs_id2str(conn->c_peer), xid);
150
151         /* Network is about to get at the memory */
152         desc->bd_network_rw = 1;
153
154         if (desc->bd_type == BULK_PUT_SOURCE)
155                 rc = LNetPut (conn->c_self, desc->bd_md_h, LNET_ACK_REQ,
156                               conn->c_peer, desc->bd_portal, xid, 0, 0);
157         else
158                 rc = LNetGet (conn->c_self, desc->bd_md_h,
159                               conn->c_peer, desc->bd_portal, xid, 0);
160
161         if (rc != 0) {
162                 /* Can't send, so we unlink the MD bound above.  The UNLINK
163                  * event this creates will signal completion with failure,
164                  * so we return SUCCESS here! */
165                 CERROR("Transfer(%s, %d, "LPX64") failed: %d\n",
166                        libcfs_id2str(conn->c_peer), desc->bd_portal, xid, rc);
167                 rc2 = LNetMDUnlink(desc->bd_md_h);
168                 LASSERT (rc2 == 0);
169         }
170
171         RETURN(0);
172 }
173
174 /**
175  * Server side bulk abort. Idempotent. Not thread-safe (i.e. only
176  * serialises with completion callback)
177  */
178 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
179 {
180         struct l_wait_info       lwi;
181         int                      rc;
182
183         LASSERT(!cfs_in_interrupt());           /* might sleep */
184
185         if (!ptlrpc_server_bulk_active(desc))   /* completed or */
186                 return;                         /* never started */
187
188         /* We used to poison the pages with 0xab here because we did not want to
189          * send any meaningful data over the wire for evicted clients (bug 9297)
190          * However, this is no longer safe now that we use the page cache on the
191          * OSS (bug 20560) */
192
193         /* The unlink ensures the callback happens ASAP and is the last
194          * one.  If it fails, it must be because completion just happened,
195          * but we must still l_wait_event() in this case, to give liblustre
196          * a chance to run server_bulk_callback()*/
197
198         LNetMDUnlink(desc->bd_md_h);
199
200         for (;;) {
201                 /* Network access will complete in finite time but the HUGE
202                  * timeout lets us CWARN for visibility of sluggish NALs */
203                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
204                                            cfs_time_seconds(1), NULL, NULL);
205                 rc = l_wait_event(desc->bd_waitq,
206                                   !ptlrpc_server_bulk_active(desc), &lwi);
207                 if (rc == 0)
208                         return;
209
210                 LASSERT(rc == -ETIMEDOUT);
211                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
212         }
213 }
214
215 /**
216  * Register bulk for later transfer
217  * Returns 0 on success or error code.
218  */
219 int ptlrpc_register_bulk(struct ptlrpc_request *req)
220 {
221         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
222         lnet_process_id_t peer;
223         int rc;
224         int rc2;
225         lnet_handle_me_t  me_h;
226         lnet_md_t         md;
227         ENTRY;
228
229         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
230                 RETURN(0);
231
232         /* NB no locking required until desc is on the network */
233         LASSERT (desc->bd_nob > 0);
234         LASSERT (!desc->bd_network_rw);
235         LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
236         LASSERT (desc->bd_req != NULL);
237         LASSERT (desc->bd_type == BULK_PUT_SINK ||
238                  desc->bd_type == BULK_GET_SOURCE);
239
240         desc->bd_success = 0;
241
242         peer = desc->bd_import->imp_connection->c_peer;
243
244         md.user_ptr = &desc->bd_cbid;
245         md.eq_handle = ptlrpc_eq_h;
246         md.threshold = 1;                       /* PUT or GET */
247         md.options = PTLRPC_MD_OPTIONS |
248                      ((desc->bd_type == BULK_GET_SOURCE) ?
249                       LNET_MD_OP_GET : LNET_MD_OP_PUT);
250         ptlrpc_fill_bulk_md(&md, desc);
251
252         LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
253         LASSERT (desc->bd_cbid.cbid_arg == desc);
254
255         /* XXX Registering the same xid on retried bulk makes my head
256          * explode trying to understand how the original request's bulk
257          * might interfere with the retried request -eeb
258          * On the other hand replaying with the same xid is fine, since
259          * we are guaranteed old request have completed. -green */
260         LASSERTF(!(desc->bd_registered &&
261                  req->rq_send_state != LUSTRE_IMP_REPLAY) ||
262                  req->rq_xid != desc->bd_last_xid,
263                  "registered: %d  rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
264                  desc->bd_registered, req->rq_xid, desc->bd_last_xid);
265         desc->bd_registered = 1;
266         desc->bd_last_xid = req->rq_xid;
267
268         rc = LNetMEAttach(desc->bd_portal, peer,
269                          req->rq_xid, 0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
270         if (rc != 0) {
271                 CERROR("LNetMEAttach failed: %d\n", rc);
272                 LASSERT (rc == -ENOMEM);
273                 RETURN (-ENOMEM);
274         }
275
276         /* About to let the network at it... */
277         desc->bd_network_rw = 1;
278         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &desc->bd_md_h);
279         if (rc != 0) {
280                 CERROR("LNetMDAttach failed: %d\n", rc);
281                 LASSERT (rc == -ENOMEM);
282                 desc->bd_network_rw = 0;
283                 rc2 = LNetMEUnlink (me_h);
284                 LASSERT (rc2 == 0);
285                 RETURN (-ENOMEM);
286         }
287
288         CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPU64", "
289                "portal %u\n",
290                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
291                desc->bd_iov_count, desc->bd_nob,
292                req->rq_xid, desc->bd_portal);
293         RETURN(0);
294 }
295
296 /**
297  * Disconnect a bulk desc from the network. Idempotent. Not
298  * thread-safe (i.e. only interlocks with completion callback).
299  * Returns 1 on success or 0 if network unregistration failed for whatever
300  * reason.
301  */
302 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async)
303 {
304         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
305         cfs_waitq_t             *wq;
306         struct l_wait_info       lwi;
307         int                      rc;
308         ENTRY;
309
310         LASSERT(!cfs_in_interrupt());     /* might sleep */
311
312         /* Let's setup deadline for reply unlink. */
313         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
314             async && req->rq_bulk_deadline == 0)
315                 req->rq_bulk_deadline = cfs_time_current_sec() + LONG_UNLINK;
316
317         if (!ptlrpc_client_bulk_active(req))  /* completed or */
318                 RETURN(1);                    /* never registered */
319
320         LASSERT(desc->bd_req == req);  /* bd_req NULL until registered */
321
322         /* the unlink ensures the callback happens ASAP and is the last
323          * one.  If it fails, it must be because completion just happened,
324          * but we must still l_wait_event() in this case to give liblustre
325          * a chance to run client_bulk_callback() */
326
327         LNetMDUnlink(desc->bd_md_h);
328
329         if (!ptlrpc_client_bulk_active(req))  /* completed or */
330                 RETURN(1);                    /* never registered */
331
332         /* Move to "Unregistering" phase as bulk was not unlinked yet. */
333         ptlrpc_rqphase_move(req, RQ_PHASE_UNREGISTERING);
334
335         /* Do not wait for unlink to finish. */
336         if (async)
337                 RETURN(0);
338
339         if (req->rq_set != NULL)
340                 wq = &req->rq_set->set_waitq;
341         else
342                 wq = &req->rq_reply_waitq;
343
344         for (;;) {
345                 /* Network access will complete in finite time but the HUGE
346                  * timeout lets us CWARN for visibility of sluggish NALs */
347                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
348                                            cfs_time_seconds(1), NULL, NULL);
349                 rc = l_wait_event(*wq, !ptlrpc_client_bulk_active(req), &lwi);
350                 if (rc == 0) {
351                         ptlrpc_rqphase_move(req, req->rq_next_phase);
352                         RETURN(1);
353                 }
354
355                 LASSERT(rc == -ETIMEDOUT);
356                 DEBUG_REQ(D_WARNING, req, "Unexpectedly long timeout: desc %p",
357                           desc);
358         }
359         RETURN(0);
360 }
361
362 static void ptlrpc_at_set_reply(struct ptlrpc_request *req, int flags)
363 {
364         struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
365         int service_time = max_t(int, cfs_time_current_sec() -
366                                  req->rq_arrival_time.tv_sec, 1);
367
368         if (!(flags & PTLRPC_REPLY_EARLY) &&
369             (req->rq_type != PTL_RPC_MSG_ERR) &&
370             (req->rq_reqmsg != NULL) &&
371             !(lustre_msg_get_flags(req->rq_reqmsg) &
372               (MSG_RESENT | MSG_REPLAY |
373                MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))) {
374                 /* early replies, errors and recovery requests don't count
375                  * toward our service time estimate */
376                 int oldse = at_measured(&svc->srv_at_estimate, service_time);
377                 if (oldse != 0)
378                         DEBUG_REQ(D_ADAPTTO, req,
379                                   "svc %s changed estimate from %d to %d",
380                                   svc->srv_name, oldse,
381                                   at_get(&svc->srv_at_estimate));
382         }
383         /* Report actual service time for client latency calc */
384         lustre_msg_set_service_time(req->rq_repmsg, service_time);
385         /* Report service time estimate for future client reqs, but report 0
386          * (to be ignored by client) if it's a error reply during recovery.
387          * (bz15815) */
388         if (req->rq_type == PTL_RPC_MSG_ERR &&
389             (req->rq_export == NULL || req->rq_export->exp_obd->obd_recovering))
390                 lustre_msg_set_timeout(req->rq_repmsg, 0);
391         else
392                 lustre_msg_set_timeout(req->rq_repmsg,
393                                        at_get(&svc->srv_at_estimate));
394
395         if (req->rq_reqmsg &&
396             !(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
397                 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
398                        "req_flags=%#x magic=%d:%x/%x len=%d\n",
399                        flags, lustre_msg_get_flags(req->rq_reqmsg),
400                        lustre_msg_is_v1(req->rq_reqmsg),
401                        lustre_msg_get_magic(req->rq_reqmsg),
402                        lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
403         }
404 }
405
406 /**
407  * Send request reply from request \a req reply buffer.
408  * \a flags defines reply types
409  * Returns 0 on sucess or error code
410  */
411 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags)
412 {
413         struct ptlrpc_service     *svc = req->rq_rqbd->rqbd_service;
414         struct ptlrpc_reply_state *rs = req->rq_reply_state;
415         struct ptlrpc_connection  *conn;
416         int                        rc;
417
418         /* We must already have a reply buffer (only ptlrpc_error() may be
419          * called without one). The reply generated by sptlrpc layer (e.g.
420          * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
421          * have a request buffer which is either the actual (swabbed) incoming
422          * request, or a saved copy if this is a req saved in
423          * target_queue_final_reply().
424          */
425         LASSERT (req->rq_no_reply == 0);
426         LASSERT (req->rq_reqbuf != NULL);
427         LASSERT (rs != NULL);
428         LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
429         LASSERT (req->rq_repmsg != NULL);
430         LASSERT (req->rq_repmsg == rs->rs_msg);
431         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
432         LASSERT (rs->rs_cb_id.cbid_arg == rs);
433
434         /* There may be no rq_export during failover */
435
436         if (unlikely(req->rq_export && req->rq_export->exp_obd &&
437                      req->rq_export->exp_obd->obd_fail)) {
438                 /* Failed obd's only send ENODEV */
439                 req->rq_type = PTL_RPC_MSG_ERR;
440                 req->rq_status = -ENODEV;
441                 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
442                        req->rq_export->exp_obd->obd_minor);
443         }
444
445         if (req->rq_type != PTL_RPC_MSG_ERR)
446                 req->rq_type = PTL_RPC_MSG_REPLY;
447
448         lustre_msg_set_type(req->rq_repmsg, req->rq_type);
449         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
450         lustre_msg_set_opc(req->rq_repmsg,
451                 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
452
453         target_pack_pool_reply(req);
454
455         ptlrpc_at_set_reply(req, flags);
456
457         if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
458                 conn = ptlrpc_connection_get(req->rq_peer, req->rq_self, NULL);
459         else
460                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
461
462         if (unlikely(conn == NULL)) {
463                 CERROR("not replying on NULL connection\n"); /* bug 9635 */
464                 return -ENOTCONN;
465         }
466         ptlrpc_rs_addref(rs);                   /* +1 ref for the network */
467
468         rc = sptlrpc_svc_wrap_reply(req);
469         if (unlikely(rc))
470                 goto out;
471
472         req->rq_sent = cfs_time_current_sec();
473
474         rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
475                            (rs->rs_difficult && !rs->rs_no_ack) ?
476                            LNET_ACK_REQ : LNET_NOACK_REQ,
477                            &rs->rs_cb_id, conn, svc->srv_rep_portal,
478                            req->rq_xid, req->rq_reply_off);
479 out:
480         if (unlikely(rc != 0))
481                 ptlrpc_req_drop_rs(req);
482         ptlrpc_connection_put(conn);
483         return rc;
484 }
485
486 int ptlrpc_reply (struct ptlrpc_request *req)
487 {
488         if (req->rq_no_reply)
489                 return 0;
490         else
491                 return (ptlrpc_send_reply(req, 0));
492 }
493
494 /**
495  * For request \a req send an error reply back. Create empty
496  * reply buffers if necessary.
497  */
498 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
499 {
500         int rc;
501         ENTRY;
502
503         if (req->rq_no_reply)
504                 RETURN(0);
505
506         if (!req->rq_repmsg) {
507                 rc = lustre_pack_reply(req, 1, NULL, NULL);
508                 if (rc)
509                         RETURN(rc);
510         }
511
512         req->rq_type = PTL_RPC_MSG_ERR;
513
514         rc = ptlrpc_send_reply(req, may_be_difficult);
515         RETURN(rc);
516 }
517
518 int ptlrpc_error(struct ptlrpc_request *req)
519 {
520         return ptlrpc_send_error(req, 0);
521 }
522
523 /**
524  * Send request \a request.
525  * if \a noreply is set, don't expect any reply back and don't set up
526  * reply buffers.
527  * Returns 0 on success or error code.
528  */
529 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
530 {
531         int rc;
532         int rc2;
533         int mpflag = 0;
534         struct ptlrpc_connection *connection;
535         lnet_handle_me_t  reply_me_h;
536         lnet_md_t         reply_md;
537         struct obd_device *obd = request->rq_import->imp_obd;
538         ENTRY;
539
540         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
541                 RETURN(0);
542
543         LASSERT(request->rq_type == PTL_RPC_MSG_REQUEST);
544         LASSERT(request->rq_wait_ctx == 0);
545
546         /* If this is a re-transmit, we're required to have disengaged
547          * cleanly from the previous attempt */
548         LASSERT(!request->rq_receiving_reply);
549
550         if (request->rq_import->imp_obd &&
551             request->rq_import->imp_obd->obd_fail) {
552                 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
553                        request->rq_import->imp_obd->obd_name);
554                 /* this prevents us from waiting in ptlrpc_queue_wait */
555                 request->rq_err = 1;
556                 request->rq_status = -ENODEV;
557                 RETURN(-ENODEV);
558         }
559
560         connection = request->rq_import->imp_connection;
561
562         lustre_msg_set_handle(request->rq_reqmsg,
563                               &request->rq_import->imp_remote_handle);
564         lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
565         lustre_msg_set_conn_cnt(request->rq_reqmsg,
566                                 request->rq_import->imp_conn_cnt);
567         lustre_msghdr_set_flags(request->rq_reqmsg,
568                                 request->rq_import->imp_msghdr_flags);
569
570         if (request->rq_resend)
571                 lustre_msg_add_flags(request->rq_reqmsg, MSG_RESENT);
572
573         if (request->rq_memalloc)
574                 mpflag = cfs_memory_pressure_get_and_set();
575
576         rc = sptlrpc_cli_wrap_request(request);
577         if (rc)
578                 GOTO(out, rc);
579
580         /* bulk register should be done after wrap_request() */
581         if (request->rq_bulk != NULL) {
582                 rc = ptlrpc_register_bulk (request);
583                 if (rc != 0)
584                         GOTO(out, rc);
585         }
586
587         if (!noreply) {
588                 LASSERT (request->rq_replen != 0);
589                 if (request->rq_repbuf == NULL) {
590                         LASSERT(request->rq_repdata == NULL);
591                         LASSERT(request->rq_repmsg == NULL);
592                         rc = sptlrpc_cli_alloc_repbuf(request,
593                                                       request->rq_replen);
594                         if (rc) {
595                                 /* this prevents us from looping in
596                                  * ptlrpc_queue_wait */
597                                 request->rq_err = 1;
598                                 request->rq_status = rc;
599                                 GOTO(cleanup_bulk, rc);
600                         }
601                 } else {
602                         request->rq_repdata = NULL;
603                         request->rq_repmsg = NULL;
604                 }
605
606                 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
607                                   connection->c_peer, request->rq_xid, 0,
608                                   LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
609                 if (rc != 0) {
610                         CERROR("LNetMEAttach failed: %d\n", rc);
611                         LASSERT (rc == -ENOMEM);
612                         GOTO(cleanup_bulk, rc = -ENOMEM);
613                 }
614         }
615
616         cfs_spin_lock(&request->rq_lock);
617         /* If the MD attach succeeds, there _will_ be a reply_in callback */
618         request->rq_receiving_reply = !noreply;
619         /* We are responsible for unlinking the reply buffer */
620         request->rq_must_unlink = !noreply;
621         /* Clear any flags that may be present from previous sends. */
622         request->rq_replied = 0;
623         request->rq_err = 0;
624         request->rq_timedout = 0;
625         request->rq_net_err = 0;
626         request->rq_resend = 0;
627         request->rq_restart = 0;
628         request->rq_reply_truncate = 0;
629         cfs_spin_unlock(&request->rq_lock);
630
631         if (!noreply) {
632                 reply_md.start     = request->rq_repbuf;
633                 reply_md.length    = request->rq_repbuf_len;
634                 /* Allow multiple early replies */
635                 reply_md.threshold = LNET_MD_THRESH_INF;
636                 /* Manage remote for early replies */
637                 reply_md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
638                         LNET_MD_MANAGE_REMOTE |
639                         LNET_MD_TRUNCATE; /* allow to make EOVERFLOW error */;
640                 reply_md.user_ptr  = &request->rq_reply_cbid;
641                 reply_md.eq_handle = ptlrpc_eq_h;
642
643                 /* We must see the unlink callback to unset rq_must_unlink,
644                    so we can't auto-unlink */
645                 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN,
646                                   &request->rq_reply_md_h);
647                 if (rc != 0) {
648                         CERROR("LNetMDAttach failed: %d\n", rc);
649                         LASSERT (rc == -ENOMEM);
650                         cfs_spin_lock(&request->rq_lock);
651                         /* ...but the MD attach didn't succeed... */
652                         request->rq_receiving_reply = 0;
653                         cfs_spin_unlock(&request->rq_lock);
654                         GOTO(cleanup_me, rc = -ENOMEM);
655                 }
656
657                 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
658                        ", portal %u\n",
659                        request->rq_repbuf_len, request->rq_xid,
660                        request->rq_reply_portal);
661         }
662
663         /* add references on request for request_out_callback */
664         ptlrpc_request_addref(request);
665         if (obd->obd_svc_stats != NULL)
666                 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
667                         cfs_atomic_read(&request->rq_import->imp_inflight));
668
669         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
670
671         cfs_gettimeofday(&request->rq_arrival_time);
672         request->rq_sent = cfs_time_current_sec();
673         /* We give the server rq_timeout secs to process the req, and
674            add the network latency for our local timeout. */
675         request->rq_deadline = request->rq_sent + request->rq_timeout +
676                 ptlrpc_at_get_net_latency(request);
677
678         ptlrpc_pinger_sending_on_import(request->rq_import);
679
680         DEBUG_REQ(D_INFO, request, "send flg=%x",
681                   lustre_msg_get_flags(request->rq_reqmsg));
682         rc = ptl_send_buf(&request->rq_req_md_h,
683                           request->rq_reqbuf, request->rq_reqdata_len,
684                           LNET_NOACK_REQ, &request->rq_req_cbid,
685                           connection,
686                           request->rq_request_portal,
687                           request->rq_xid, 0);
688         if (rc == 0)
689                 GOTO(out, rc);
690
691         ptlrpc_req_finished(request);
692         if (noreply)
693                 GOTO(out, rc);
694
695  cleanup_me:
696         /* MEUnlink is safe; the PUT didn't even get off the ground, and
697          * nobody apart from the PUT's target has the right nid+XID to
698          * access the reply buffer. */
699         rc2 = LNetMEUnlink(reply_me_h);
700         LASSERT (rc2 == 0);
701         /* UNLINKED callback called synchronously */
702         LASSERT(!request->rq_receiving_reply);
703
704  cleanup_bulk:
705         /* We do sync unlink here as there was no real transfer here so
706          * the chance to have long unlink to sluggish net is smaller here. */
707         ptlrpc_unregister_bulk(request, 0);
708  out:
709         if (request->rq_memalloc)
710                 cfs_memory_pressure_restore(mpflag);
711         return rc;
712 }
713
714 /**
715  * Register request buffer descriptor for request receiving.
716  */
717 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
718 {
719         struct ptlrpc_service   *service = rqbd->rqbd_service;
720         static lnet_process_id_t  match_id = {LNET_NID_ANY, LNET_PID_ANY};
721         int                      rc;
722         lnet_md_t                 md;
723         lnet_handle_me_t          me_h;
724
725         CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
726                service->srv_req_portal);
727
728         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
729                 return (-ENOMEM);
730
731         rc = LNetMEAttach(service->srv_req_portal,
732                           match_id, 0, ~0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
733         if (rc != 0) {
734                 CERROR("LNetMEAttach failed: %d\n", rc);
735                 return (-ENOMEM);
736         }
737
738         LASSERT(rqbd->rqbd_refcount == 0);
739         rqbd->rqbd_refcount = 1;
740
741         md.start     = rqbd->rqbd_buffer;
742         md.length    = service->srv_buf_size;
743         md.max_size  = service->srv_max_req_size;
744         md.threshold = LNET_MD_THRESH_INF;
745         md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
746         md.user_ptr  = &rqbd->rqbd_cbid;
747         md.eq_handle = ptlrpc_eq_h;
748
749         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
750         if (rc == 0)
751                 return (0);
752
753         CERROR("LNetMDAttach failed: %d; \n", rc);
754         LASSERT (rc == -ENOMEM);
755         rc = LNetMEUnlink (me_h);
756         LASSERT (rc == 0);
757         rqbd->rqbd_refcount = 0;
758
759         return (-ENOMEM);
760 }