Whamcloud - gitweb
LU-6441 ptlrpc: ptlrpc_bulk_abort unlink all entries in bd_mds
[fs/lustre-release.git] / 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) 2012, 2014, 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 <obd_support.h>
39 #include <lustre_net.h>
40 #include <lustre_lib.h>
41 #include <obd.h>
42 #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         ENTRY;
58
59         LASSERT (portal != 0);
60         LASSERT (conn != NULL);
61         CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
62         md.start     = base;
63         md.length    = len;
64         md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
65         md.options   = PTLRPC_MD_OPTIONS;
66         md.user_ptr  = cbid;
67         md.eq_handle = ptlrpc_eq_h;
68
69         if (unlikely(ack == LNET_ACK_REQ &&
70                      OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
71                 /* don't ask for the ack to simulate failing client */
72                 ack = LNET_NOACK_REQ;
73         }
74
75         rc = LNetMDBind (md, LNET_UNLINK, mdh);
76         if (unlikely(rc != 0)) {
77                 CERROR ("LNetMDBind failed: %d\n", rc);
78                 LASSERT (rc == -ENOMEM);
79                 RETURN (-ENOMEM);
80         }
81
82         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64", offset %u\n",
83                len, portal, xid, offset);
84
85         rc = LNetPut (conn->c_self, *mdh, ack,
86                       conn->c_peer, portal, xid, offset, 0);
87         if (unlikely(rc != 0)) {
88                 int rc2;
89                 /* We're going to get an UNLINK event when I unlink below,
90                  * which will complete just like any other failed send, so
91                  * I fall through and return success here! */
92                 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
93                        libcfs_id2str(conn->c_peer), portal, xid, rc);
94                 rc2 = LNetMDUnlink(*mdh);
95                 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
96         }
97
98         RETURN (0);
99 }
100
101 static void mdunlink_iterate_helper(lnet_handle_md_t *bd_mds, int count)
102 {
103         int i;
104
105         for (i = 0; i < count; i++)
106                 LNetMDUnlink(bd_mds[i]);
107 }
108
109 #ifdef HAVE_SERVER_SUPPORT
110 /**
111  * Prepare bulk descriptor for specified incoming request \a req that
112  * can fit \a npages * pages. \a type is bulk type. \a portal is where
113  * the bulk to be sent. Used on server-side after request was already
114  * received.
115  * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
116  * error.
117  */
118 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
119                                               unsigned npages, unsigned max_brw,
120                                               unsigned type, unsigned portal)
121 {
122         struct obd_export *exp = req->rq_export;
123         struct ptlrpc_bulk_desc *desc;
124
125         ENTRY;
126         LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
127
128         desc = ptlrpc_new_bulk(npages, max_brw, type, portal);
129         if (desc == NULL)
130                 RETURN(NULL);
131
132         desc->bd_export = class_export_get(exp);
133         desc->bd_req = req;
134
135         desc->bd_cbid.cbid_fn  = server_bulk_callback;
136         desc->bd_cbid.cbid_arg = desc;
137
138         /* NB we don't assign rq_bulk here; server-side requests are
139          * re-used, and the handler frees the bulk desc explicitly. */
140
141         return desc;
142 }
143 EXPORT_SYMBOL(ptlrpc_prep_bulk_exp);
144
145 /**
146  * Starts bulk transfer for descriptor \a desc on the server.
147  * Returns 0 on success or error code.
148  */
149 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc)
150 {
151         struct obd_export        *exp = desc->bd_export;
152         struct ptlrpc_connection *conn = exp->exp_connection;
153         int                       rc = 0;
154         __u64                     xid;
155         int                       posted_md;
156         int                       total_md;
157         lnet_md_t                 md;
158         ENTRY;
159
160         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
161                 RETURN(0);
162
163         /* NB no locking required until desc is on the network */
164         LASSERT(desc->bd_md_count == 0);
165         LASSERT(desc->bd_type == BULK_PUT_SOURCE ||
166                 desc->bd_type == BULK_GET_SINK);
167
168         LASSERT(desc->bd_cbid.cbid_fn == server_bulk_callback);
169         LASSERT(desc->bd_cbid.cbid_arg == desc);
170
171         /* NB total length may be 0 for a read past EOF, so we send 0
172          * length bulks, since the client expects bulk events.
173          *
174          * The client may not need all of the bulk XIDs for the RPC.  The RPC
175          * used the XID of the highest bulk XID needed, and the server masks
176          * off high bits to get bulk count for this RPC. LU-1431 */
177         xid = desc->bd_req->rq_xid & ~((__u64)desc->bd_md_max_brw - 1);
178         total_md = desc->bd_req->rq_xid - xid + 1;
179
180         desc->bd_md_count = total_md;
181         desc->bd_failure = 0;
182
183         md.user_ptr = &desc->bd_cbid;
184         md.eq_handle = ptlrpc_eq_h;
185         md.threshold = 2; /* SENT and ACK/REPLY */
186
187         for (posted_md = 0; posted_md < total_md; xid++) {
188                 md.options = PTLRPC_MD_OPTIONS;
189
190                 /* NB it's assumed that source and sink buffer frags are
191                  * page-aligned. Otherwise we'd have to send client bulk
192                  * sizes over and split server buffer accordingly */
193                 ptlrpc_fill_bulk_md(&md, desc, posted_md);
194                 rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_mds[posted_md]);
195                 if (rc != 0) {
196                         CERROR("%s: LNetMDBind failed for MD %u: rc = %d\n",
197                                exp->exp_obd->obd_name, posted_md, rc);
198                         LASSERT(rc == -ENOMEM);
199                         if (posted_md == 0) {
200                                 desc->bd_md_count = 0;
201                                 RETURN(-ENOMEM);
202                         }
203                         break;
204                 }
205
206                 /* LU-6441: last md is not sent and desc->bd_md_count == 1 */
207                 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB3,
208                                          CFS_FAIL_ONCE) &&
209                     posted_md == desc->bd_md_max_brw - 1) {
210                         posted_md++;
211                         continue;
212                 }
213
214                 /* Network is about to get at the memory */
215                 if (desc->bd_type == BULK_PUT_SOURCE)
216                         rc = LNetPut(conn->c_self, desc->bd_mds[posted_md],
217                                      LNET_ACK_REQ, conn->c_peer,
218                                      desc->bd_portal, xid, 0, 0);
219                 else
220                         rc = LNetGet(conn->c_self, desc->bd_mds[posted_md],
221                                      conn->c_peer, desc->bd_portal, xid, 0);
222
223                 posted_md++;
224                 if (rc != 0) {
225                         CERROR("%s: failed bulk transfer with %s:%u x"LPU64": "
226                                "rc = %d\n", exp->exp_obd->obd_name,
227                                libcfs_id2str(conn->c_peer), desc->bd_portal,
228                                xid, rc);
229                         break;
230                 }
231         }
232
233         if (rc != 0) {
234                 /* Can't send, so we unlink the MD bound above.  The UNLINK
235                  * event this creates will signal completion with failure,
236                  * so we return SUCCESS here! */
237                 spin_lock(&desc->bd_lock);
238                 desc->bd_md_count -= total_md - posted_md;
239                 spin_unlock(&desc->bd_lock);
240                 LASSERT(desc->bd_md_count >= 0);
241
242                 mdunlink_iterate_helper(desc->bd_mds, posted_md);
243                 RETURN(0);
244         }
245
246         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
247                "id %s xid "LPX64"-"LPX64"\n", desc->bd_iov_count,
248                desc->bd_nob, desc->bd_portal, libcfs_id2str(conn->c_peer),
249                xid - posted_md, xid - 1);
250
251         RETURN(0);
252 }
253
254 /**
255  * Server side bulk abort. Idempotent. Not thread-safe (i.e. only
256  * serialises with completion callback)
257  */
258 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
259 {
260         struct l_wait_info       lwi;
261         int                      rc;
262
263         LASSERT(!in_interrupt());           /* might sleep */
264
265         if (!ptlrpc_server_bulk_active(desc))   /* completed or */
266                 return;                         /* never started */
267
268         /* We used to poison the pages with 0xab here because we did not want to
269          * send any meaningful data over the wire for evicted clients (bug 9297)
270          * However, this is no longer safe now that we use the page cache on the
271          * OSS (bug 20560) */
272
273         /* The unlink ensures the callback happens ASAP and is the last
274          * one.  If it fails, it must be because completion just happened,
275          * but we must still l_wait_event() in this case, to give liblustre
276          * a chance to run server_bulk_callback()*/
277         mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
278
279         for (;;) {
280                 /* Network access will complete in finite time but the HUGE
281                  * timeout lets us CWARN for visibility of sluggish NALs */
282                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
283                                            cfs_time_seconds(1), NULL, NULL);
284                 rc = l_wait_event(desc->bd_waitq,
285                                   !ptlrpc_server_bulk_active(desc), &lwi);
286                 if (rc == 0)
287                         return;
288
289                 LASSERT(rc == -ETIMEDOUT);
290                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
291         }
292 }
293 #endif /* HAVE_SERVER_SUPPORT */
294
295 /**
296  * Register bulk at the sender for later transfer.
297  * Returns 0 on success or error code.
298  */
299 int ptlrpc_register_bulk(struct ptlrpc_request *req)
300 {
301         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
302         lnet_process_id_t peer;
303         int rc = 0;
304         int rc2;
305         int posted_md;
306         int total_md;
307         __u64 xid;
308         lnet_handle_me_t  me_h;
309         lnet_md_t         md;
310         ENTRY;
311
312         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
313                 RETURN(0);
314
315         /* NB no locking required until desc is on the network */
316         LASSERT(desc->bd_nob > 0);
317         LASSERT(desc->bd_md_count == 0);
318         LASSERT(desc->bd_md_max_brw <= PTLRPC_BULK_OPS_COUNT);
319         LASSERT(desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
320         LASSERT(desc->bd_req != NULL);
321         LASSERT(desc->bd_type == BULK_PUT_SINK ||
322                 desc->bd_type == BULK_GET_SOURCE);
323
324         /* cleanup the state of the bulk for it will be reused */
325         if (req->rq_resend || req->rq_send_state == LUSTRE_IMP_REPLAY)
326                 desc->bd_nob_transferred = 0;
327         else
328                 LASSERT(desc->bd_nob_transferred == 0);
329
330         desc->bd_failure = 0;
331
332         peer = desc->bd_import->imp_connection->c_peer;
333
334         LASSERT(desc->bd_cbid.cbid_fn == client_bulk_callback);
335         LASSERT(desc->bd_cbid.cbid_arg == desc);
336
337         /* An XID is only used for a single request from the client.
338          * For retried bulk transfers, a new XID will be allocated in
339          * in ptlrpc_check_set() if it needs to be resent, so it is not
340          * using the same RDMA match bits after an error.
341          *
342          * For multi-bulk RPCs, rq_xid is the last XID needed for bulks. The
343          * first bulk XID is power-of-two aligned before rq_xid. LU-1431 */
344         xid = req->rq_xid & ~((__u64)desc->bd_md_max_brw - 1);
345         LASSERTF(!(desc->bd_registered &&
346                    req->rq_send_state != LUSTRE_IMP_REPLAY) ||
347                  xid != desc->bd_last_xid,
348                  "registered: %d  rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
349                  desc->bd_registered, xid, desc->bd_last_xid);
350
351         total_md = (desc->bd_iov_count + LNET_MAX_IOV - 1) / LNET_MAX_IOV;
352         desc->bd_registered = 1;
353         desc->bd_last_xid = xid;
354         desc->bd_md_count = total_md;
355         md.user_ptr = &desc->bd_cbid;
356         md.eq_handle = ptlrpc_eq_h;
357         md.threshold = 1;                       /* PUT or GET */
358
359         for (posted_md = 0; posted_md < total_md; posted_md++, xid++) {
360                 md.options = PTLRPC_MD_OPTIONS |
361                              ((desc->bd_type == BULK_GET_SOURCE) ?
362                               LNET_MD_OP_GET : LNET_MD_OP_PUT);
363                 ptlrpc_fill_bulk_md(&md, desc, posted_md);
364
365                 rc = LNetMEAttach(desc->bd_portal, peer, xid, 0,
366                                   LNET_UNLINK, LNET_INS_AFTER, &me_h);
367                 if (rc != 0) {
368                         CERROR("%s: LNetMEAttach failed x"LPU64"/%d: rc = %d\n",
369                                desc->bd_import->imp_obd->obd_name, xid,
370                                posted_md, rc);
371                         break;
372                 }
373
374                 /* About to let the network at it... */
375                 rc = LNetMDAttach(me_h, md, LNET_UNLINK,
376                                   &desc->bd_mds[posted_md]);
377                 if (rc != 0) {
378                         CERROR("%s: LNetMDAttach failed x"LPU64"/%d: rc = %d\n",
379                                desc->bd_import->imp_obd->obd_name, xid,
380                                posted_md, rc);
381                         rc2 = LNetMEUnlink(me_h);
382                         LASSERT(rc2 == 0);
383                         break;
384                 }
385         }
386
387         if (rc != 0) {
388                 LASSERT(rc == -ENOMEM);
389                 spin_lock(&desc->bd_lock);
390                 desc->bd_md_count -= total_md - posted_md;
391                 spin_unlock(&desc->bd_lock);
392                 LASSERT(desc->bd_md_count >= 0);
393                 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
394                 req->rq_status = -ENOMEM;
395                 RETURN(-ENOMEM);
396         }
397
398         /* Set rq_xid to matchbits of the final bulk so that server can
399          * infer the number of bulks that were prepared */
400         req->rq_xid = --xid;
401         LASSERTF(desc->bd_last_xid == (req->rq_xid & PTLRPC_BULK_OPS_MASK),
402                  "bd_last_xid = x"LPU64", rq_xid = x"LPU64"\n",
403                  desc->bd_last_xid, req->rq_xid);
404
405         spin_lock(&desc->bd_lock);
406         /* Holler if peer manages to touch buffers before he knows the xid */
407         if (desc->bd_md_count != total_md)
408                 CWARN("%s: Peer %s touched %d buffers while I registered\n",
409                       desc->bd_import->imp_obd->obd_name, libcfs_id2str(peer),
410                       total_md - desc->bd_md_count);
411         spin_unlock(&desc->bd_lock);
412
413         CDEBUG(D_NET, "Setup %u bulk %s buffers: %u pages %u bytes, "
414                "xid x"LPX64"-"LPX64", portal %u\n", desc->bd_md_count,
415                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
416                desc->bd_iov_count, desc->bd_nob,
417                desc->bd_last_xid, req->rq_xid, desc->bd_portal);
418
419         RETURN(0);
420 }
421
422 /**
423  * Disconnect a bulk desc from the network. Idempotent. Not
424  * thread-safe (i.e. only interlocks with completion callback).
425  * Returns 1 on success or 0 if network unregistration failed for whatever
426  * reason.
427  */
428 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async)
429 {
430         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
431         struct l_wait_info       lwi;
432         int                      rc;
433         ENTRY;
434
435         LASSERT(!in_interrupt());     /* might sleep */
436
437         /* Let's setup deadline for reply unlink. */
438         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
439             async && req->rq_bulk_deadline == 0)
440                 req->rq_bulk_deadline = cfs_time_current_sec() + LONG_UNLINK;
441
442         if (ptlrpc_client_bulk_active(req) == 0)        /* completed or */
443                 RETURN(1);                              /* never registered */
444
445         LASSERT(desc->bd_req == req);  /* bd_req NULL until registered */
446
447         /* the unlink ensures the callback happens ASAP and is the last
448          * one.  If it fails, it must be because completion just happened,
449          * but we must still l_wait_event() in this case to give liblustre
450          * a chance to run client_bulk_callback() */
451         mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
452
453         if (ptlrpc_client_bulk_active(req) == 0)        /* completed or */
454                 RETURN(1);                              /* never registered */
455
456         /* Move to "Unregistering" phase as bulk was not unlinked yet. */
457         ptlrpc_rqphase_move(req, RQ_PHASE_UNREGISTERING);
458
459         /* Do not wait for unlink to finish. */
460         if (async)
461                 RETURN(0);
462
463         for (;;) {
464                 /* The wq argument is ignored by user-space wait_event macros */
465                 wait_queue_head_t *wq = (req->rq_set != NULL) ?
466                                         &req->rq_set->set_waitq :
467                                         &req->rq_reply_waitq;
468                 /* Network access will complete in finite time but the HUGE
469                  * timeout lets us CWARN for visibility of sluggish NALs */
470                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
471                                            cfs_time_seconds(1), NULL, NULL);
472                 rc = l_wait_event(*wq, !ptlrpc_client_bulk_active(req), &lwi);
473                 if (rc == 0) {
474                         ptlrpc_rqphase_move(req, req->rq_next_phase);
475                         RETURN(1);
476                 }
477
478                 LASSERT(rc == -ETIMEDOUT);
479                 DEBUG_REQ(D_WARNING, req, "Unexpectedly long timeout: desc %p",
480                           desc);
481         }
482         RETURN(0);
483 }
484
485 static void ptlrpc_at_set_reply(struct ptlrpc_request *req, int flags)
486 {
487         struct ptlrpc_service_part      *svcpt = req->rq_rqbd->rqbd_svcpt;
488         struct ptlrpc_service           *svc = svcpt->scp_service;
489         int service_time = max_t(int, cfs_time_current_sec() -
490                                  req->rq_arrival_time.tv_sec, 1);
491
492         if (!(flags & PTLRPC_REPLY_EARLY) &&
493             (req->rq_type != PTL_RPC_MSG_ERR) &&
494             (req->rq_reqmsg != NULL) &&
495             !(lustre_msg_get_flags(req->rq_reqmsg) &
496               (MSG_RESENT | MSG_REPLAY |
497                MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))) {
498                 /* early replies, errors and recovery requests don't count
499                  * toward our service time estimate */
500                 int oldse = at_measured(&svcpt->scp_at_estimate, service_time);
501
502                 if (oldse != 0) {
503                         DEBUG_REQ(D_ADAPTTO, req,
504                                   "svc %s changed estimate from %d to %d",
505                                   svc->srv_name, oldse,
506                                   at_get(&svcpt->scp_at_estimate));
507                 }
508         }
509         /* Report actual service time for client latency calc */
510         lustre_msg_set_service_time(req->rq_repmsg, service_time);
511         /* Report service time estimate for future client reqs, but report 0
512          * (to be ignored by client) if it's an error reply during recovery.
513          * (bz15815) */
514         if (req->rq_type == PTL_RPC_MSG_ERR &&
515             (req->rq_export == NULL || req->rq_export->exp_obd->obd_recovering))
516                 lustre_msg_set_timeout(req->rq_repmsg, 0);
517         else
518                 lustre_msg_set_timeout(req->rq_repmsg,
519                                        at_get(&svcpt->scp_at_estimate));
520
521         if (req->rq_reqmsg &&
522             !(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
523                 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
524                        "req_flags=%#x magic=%x/%x len=%d\n",
525                        flags, lustre_msg_get_flags(req->rq_reqmsg),
526                        lustre_msg_get_magic(req->rq_reqmsg),
527                        lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
528         }
529 }
530
531 /**
532  * Send request reply from request \a req reply buffer.
533  * \a flags defines reply types
534  * Returns 0 on success or error code
535  */
536 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags)
537 {
538         struct ptlrpc_reply_state *rs = req->rq_reply_state;
539         struct ptlrpc_connection  *conn;
540         int                        rc;
541
542         /* We must already have a reply buffer (only ptlrpc_error() may be
543          * called without one). The reply generated by sptlrpc layer (e.g.
544          * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
545          * have a request buffer which is either the actual (swabbed) incoming
546          * request, or a saved copy if this is a req saved in
547          * target_queue_final_reply().
548          */
549         LASSERT (req->rq_no_reply == 0);
550         LASSERT (req->rq_reqbuf != NULL);
551         LASSERT (rs != NULL);
552         LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
553         LASSERT (req->rq_repmsg != NULL);
554         LASSERT (req->rq_repmsg == rs->rs_msg);
555         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
556         LASSERT (rs->rs_cb_id.cbid_arg == rs);
557
558         /* There may be no rq_export during failover */
559
560         if (unlikely(req->rq_export && req->rq_export->exp_obd &&
561                      req->rq_export->exp_obd->obd_fail)) {
562                 /* Failed obd's only send ENODEV */
563                 req->rq_type = PTL_RPC_MSG_ERR;
564                 req->rq_status = -ENODEV;
565                 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
566                        req->rq_export->exp_obd->obd_minor);
567         }
568
569         /* In order to keep interoprability with the client (< 2.3) which
570          * doesn't have pb_jobid in ptlrpc_body, We have to shrink the
571          * ptlrpc_body in reply buffer to ptlrpc_body_v2, otherwise, the
572          * reply buffer on client will be overflow.
573          *
574          * XXX Remove this whenver we drop the interoprability with such client.
575          */
576         req->rq_replen = lustre_shrink_msg(req->rq_repmsg, 0,
577                                            sizeof(struct ptlrpc_body_v2), 1);
578
579         if (req->rq_type != PTL_RPC_MSG_ERR)
580                 req->rq_type = PTL_RPC_MSG_REPLY;
581
582         lustre_msg_set_type(req->rq_repmsg, req->rq_type);
583         lustre_msg_set_status(req->rq_repmsg,
584                               ptlrpc_status_hton(req->rq_status));
585         lustre_msg_set_opc(req->rq_repmsg,
586                 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
587
588         target_pack_pool_reply(req);
589
590         ptlrpc_at_set_reply(req, flags);
591
592         if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
593                 conn = ptlrpc_connection_get(req->rq_peer, req->rq_self, NULL);
594         else
595                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
596
597         if (unlikely(conn == NULL)) {
598                 CERROR("not replying on NULL connection\n"); /* bug 9635 */
599                 return -ENOTCONN;
600         }
601         ptlrpc_rs_addref(rs);                   /* +1 ref for the network */
602
603         rc = sptlrpc_svc_wrap_reply(req);
604         if (unlikely(rc))
605                 goto out;
606
607         req->rq_sent = cfs_time_current_sec();
608
609         rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
610                            (rs->rs_difficult && !rs->rs_no_ack) ?
611                            LNET_ACK_REQ : LNET_NOACK_REQ,
612                            &rs->rs_cb_id, conn,
613                            ptlrpc_req2svc(req)->srv_rep_portal,
614                            req->rq_xid, req->rq_reply_off);
615 out:
616         if (unlikely(rc != 0))
617                 ptlrpc_req_drop_rs(req);
618         ptlrpc_connection_put(conn);
619         return rc;
620 }
621
622 int ptlrpc_reply (struct ptlrpc_request *req)
623 {
624         if (req->rq_no_reply)
625                 return 0;
626         else
627                 return (ptlrpc_send_reply(req, 0));
628 }
629
630 /**
631  * For request \a req send an error reply back. Create empty
632  * reply buffers if necessary.
633  */
634 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
635 {
636         int rc;
637         ENTRY;
638
639         if (req->rq_no_reply)
640                 RETURN(0);
641
642         if (!req->rq_repmsg) {
643                 rc = lustre_pack_reply(req, 1, NULL, NULL);
644                 if (rc)
645                         RETURN(rc);
646         }
647
648         if (req->rq_status != -ENOSPC && req->rq_status != -EACCES &&
649             req->rq_status != -EPERM && req->rq_status != -ENOENT &&
650             req->rq_status != -EINPROGRESS && req->rq_status != -EDQUOT)
651                 req->rq_type = PTL_RPC_MSG_ERR;
652
653         rc = ptlrpc_send_reply(req, may_be_difficult);
654         RETURN(rc);
655 }
656
657 int ptlrpc_error(struct ptlrpc_request *req)
658 {
659         return ptlrpc_send_error(req, 0);
660 }
661
662 /**
663  * Send request \a request.
664  * if \a noreply is set, don't expect any reply back and don't set up
665  * reply buffers.
666  * Returns 0 on success or error code.
667  */
668 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
669 {
670         int rc;
671         int rc2;
672         int mpflag = 0;
673         struct ptlrpc_connection *connection;
674         lnet_handle_me_t  reply_me_h;
675         lnet_md_t         reply_md;
676         struct obd_import *imp = request->rq_import;
677         struct obd_device *obd = imp->imp_obd;
678         ENTRY;
679
680         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
681                 RETURN(0);
682
683         LASSERT(request->rq_type == PTL_RPC_MSG_REQUEST);
684         LASSERT(request->rq_wait_ctx == 0);
685
686         /* If this is a re-transmit, we're required to have disengaged
687          * cleanly from the previous attempt */
688         LASSERT(!request->rq_receiving_reply);
689         LASSERT(!((lustre_msg_get_flags(request->rq_reqmsg) & MSG_REPLAY) &&
690                 (imp->imp_state == LUSTRE_IMP_FULL)));
691
692         if (unlikely(obd != NULL && obd->obd_fail)) {
693                 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
694                         obd->obd_name);
695                 /* this prevents us from waiting in ptlrpc_queue_wait */
696                 spin_lock(&request->rq_lock);
697                 request->rq_err = 1;
698                 spin_unlock(&request->rq_lock);
699                 request->rq_status = -ENODEV;
700                 RETURN(-ENODEV);
701         }
702
703         connection = imp->imp_connection;
704
705         lustre_msg_set_handle(request->rq_reqmsg,
706                               &imp->imp_remote_handle);
707         lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
708         lustre_msg_set_conn_cnt(request->rq_reqmsg,
709                                 imp->imp_conn_cnt);
710         lustre_msghdr_set_flags(request->rq_reqmsg,
711                                 imp->imp_msghdr_flags);
712
713         /** For enabled AT all request should have AT_SUPPORT in the
714          * FULL import state when OBD_CONNECT_AT is set */
715         LASSERT(AT_OFF || imp->imp_state != LUSTRE_IMP_FULL ||
716                 (imp->imp_msghdr_flags & MSGHDR_AT_SUPPORT) ||
717                 !(imp->imp_connect_data.ocd_connect_flags &
718                 OBD_CONNECT_AT));
719
720         if (request->rq_resend) {
721                 lustre_msg_add_flags(request->rq_reqmsg, MSG_RESENT);
722                 if (request->rq_resend_cb != NULL)
723                         request->rq_resend_cb(request, &request->rq_async_args);
724         }
725         if (request->rq_memalloc)
726                 mpflag = cfs_memory_pressure_get_and_set();
727
728         rc = sptlrpc_cli_wrap_request(request);
729         if (rc)
730                 GOTO(out, rc);
731
732         /* bulk register should be done after wrap_request() */
733         if (request->rq_bulk != NULL) {
734                 rc = ptlrpc_register_bulk (request);
735                 if (rc != 0)
736                         GOTO(out, rc);
737         }
738
739         if (!noreply) {
740                 LASSERT (request->rq_replen != 0);
741                 if (request->rq_repbuf == NULL) {
742                         LASSERT(request->rq_repdata == NULL);
743                         LASSERT(request->rq_repmsg == NULL);
744                         rc = sptlrpc_cli_alloc_repbuf(request,
745                                                       request->rq_replen);
746                         if (rc) {
747                                 /* this prevents us from looping in
748                                  * ptlrpc_queue_wait */
749                                 spin_lock(&request->rq_lock);
750                                 request->rq_err = 1;
751                                 spin_unlock(&request->rq_lock);
752                                 request->rq_status = rc;
753                                 GOTO(cleanup_bulk, rc);
754                         }
755                 } else {
756                         request->rq_repdata = NULL;
757                         request->rq_repmsg = NULL;
758                 }
759
760                 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
761                                   connection->c_peer, request->rq_xid, 0,
762                                   LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
763                 if (rc != 0) {
764                         CERROR("LNetMEAttach failed: %d\n", rc);
765                         LASSERT (rc == -ENOMEM);
766                         GOTO(cleanup_bulk, rc = -ENOMEM);
767                 }
768         }
769
770         spin_lock(&request->rq_lock);
771         /* We are responsible for unlinking the reply buffer */
772         request->rq_reply_unlinked = noreply;
773         request->rq_receiving_reply = !noreply;
774         /* Clear any flags that may be present from previous sends. */
775         request->rq_req_unlinked = 0;
776         request->rq_replied = 0;
777         request->rq_err = 0;
778         request->rq_timedout = 0;
779         request->rq_net_err = 0;
780         request->rq_resend = 0;
781         request->rq_restart = 0;
782         request->rq_reply_truncated = 0;
783         spin_unlock(&request->rq_lock);
784
785         if (!noreply) {
786                 reply_md.start     = request->rq_repbuf;
787                 reply_md.length    = request->rq_repbuf_len;
788                 /* Allow multiple early replies */
789                 reply_md.threshold = LNET_MD_THRESH_INF;
790                 /* Manage remote for early replies */
791                 reply_md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
792                         LNET_MD_MANAGE_REMOTE |
793                         LNET_MD_TRUNCATE; /* allow to make EOVERFLOW error */;
794                 reply_md.user_ptr  = &request->rq_reply_cbid;
795                 reply_md.eq_handle = ptlrpc_eq_h;
796
797                 /* We must see the unlink callback to set rq_reply_unlinked,
798                  * so we can't auto-unlink */
799                 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN,
800                                   &request->rq_reply_md_h);
801                 if (rc != 0) {
802                         CERROR("LNetMDAttach failed: %d\n", rc);
803                         LASSERT (rc == -ENOMEM);
804                         spin_lock(&request->rq_lock);
805                         /* ...but the MD attach didn't succeed... */
806                         request->rq_receiving_reply = 0;
807                         spin_unlock(&request->rq_lock);
808                         GOTO(cleanup_me, rc = -ENOMEM);
809                 }
810
811                 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
812                        ", portal %u\n",
813                        request->rq_repbuf_len, request->rq_xid,
814                        request->rq_reply_portal);
815         }
816
817         /* add references on request for request_out_callback */
818         ptlrpc_request_addref(request);
819         if (obd != NULL && obd->obd_svc_stats != NULL)
820                 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
821                         atomic_read(&imp->imp_inflight));
822
823         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
824
825         do_gettimeofday(&request->rq_sent_tv);
826         request->rq_sent = cfs_time_current_sec();
827         /* We give the server rq_timeout secs to process the req, and
828            add the network latency for our local timeout. */
829         request->rq_deadline = request->rq_sent + request->rq_timeout +
830                 ptlrpc_at_get_net_latency(request);
831
832         ptlrpc_pinger_sending_on_import(imp);
833
834         DEBUG_REQ(D_INFO, request, "send flg=%x",
835                   lustre_msg_get_flags(request->rq_reqmsg));
836         rc = ptl_send_buf(&request->rq_req_md_h,
837                           request->rq_reqbuf, request->rq_reqdata_len,
838                           LNET_NOACK_REQ, &request->rq_req_cbid,
839                           connection,
840                           request->rq_request_portal,
841                           request->rq_xid, 0);
842         if (likely(rc == 0))
843                 GOTO(out, rc);
844
845         request->rq_req_unlinked = 1;
846         ptlrpc_req_finished(request);
847         if (noreply)
848                 GOTO(out, rc);
849
850  cleanup_me:
851         /* MEUnlink is safe; the PUT didn't even get off the ground, and
852          * nobody apart from the PUT's target has the right nid+XID to
853          * access the reply buffer. */
854         rc2 = LNetMEUnlink(reply_me_h);
855         LASSERT (rc2 == 0);
856         /* UNLINKED callback called synchronously */
857         LASSERT(!request->rq_receiving_reply);
858
859  cleanup_bulk:
860         /* We do sync unlink here as there was no real transfer here so
861          * the chance to have long unlink to sluggish net is smaller here. */
862         ptlrpc_unregister_bulk(request, 0);
863  out:
864         if (request->rq_memalloc)
865                 cfs_memory_pressure_restore(mpflag);
866         return rc;
867 }
868 EXPORT_SYMBOL(ptl_send_rpc);
869
870 /**
871  * Register request buffer descriptor for request receiving.
872  */
873 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
874 {
875         struct ptlrpc_service     *service = rqbd->rqbd_svcpt->scp_service;
876         static lnet_process_id_t  match_id = {LNET_NID_ANY, LNET_PID_ANY};
877         int                       rc;
878         lnet_md_t                 md;
879         lnet_handle_me_t          me_h;
880
881         CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
882                service->srv_req_portal);
883
884         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
885                 return (-ENOMEM);
886
887         /* NB: CPT affinity service should use new LNet flag LNET_INS_LOCAL,
888          * which means buffer can only be attached on local CPT, and LND
889          * threads can find it by grabbing a local lock */
890         rc = LNetMEAttach(service->srv_req_portal,
891                           match_id, 0, ~0, LNET_UNLINK,
892                           rqbd->rqbd_svcpt->scp_cpt >= 0 ?
893                           LNET_INS_LOCAL : LNET_INS_AFTER, &me_h);
894         if (rc != 0) {
895                 CERROR("LNetMEAttach failed: %d\n", rc);
896                 return (-ENOMEM);
897         }
898
899         LASSERT(rqbd->rqbd_refcount == 0);
900         rqbd->rqbd_refcount = 1;
901
902         md.start     = rqbd->rqbd_buffer;
903         md.length    = service->srv_buf_size;
904         md.max_size  = service->srv_max_req_size;
905         md.threshold = LNET_MD_THRESH_INF;
906         md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
907         md.user_ptr  = &rqbd->rqbd_cbid;
908         md.eq_handle = ptlrpc_eq_h;
909
910         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
911         if (rc == 0)
912                 return (0);
913
914         CERROR("LNetMDAttach failed: %d; \n", rc);
915         LASSERT (rc == -ENOMEM);
916         rc = LNetMEUnlink (me_h);
917         LASSERT (rc == 0);
918         rqbd->rqbd_refcount = 0;
919
920         return (-ENOMEM);
921 }