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