Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[fs/lustre-release.git] / lustre / ptlrpc / client.c
index 7f279b4..b536445 100644 (file)
@@ -35,6 +35,7 @@
 #include <lustre_lib.h>
 #include <lustre_ha.h>
 #include <lustre_import.h>
+#include <lustre_req_layout.h>
 
 #include "ptlrpc_internal.h"
 
@@ -132,8 +133,8 @@ struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
         return desc;
 }
 
-struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
-                                               int npages, int type, int portal)
+struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
+                                              int npages, int type, int portal)
 {
         struct obd_export *exp = req->rq_export;
         struct ptlrpc_bulk_desc *desc;
@@ -179,6 +180,9 @@ void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
         LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
         LASSERT(!desc->bd_network_rw);         /* network hands off or */
         LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
+
+        sptlrpc_enc_pool_put_pages(desc);
+
         if (desc->bd_export)
                 class_export_put(desc->bd_export);
         else
@@ -200,8 +204,9 @@ void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
         list_for_each_safe(l, tmp, &pool->prp_req_list) {
                 req = list_entry(l, struct ptlrpc_request, rq_list);
                 list_del(&req->rq_list);
-                LASSERT (req->rq_reqmsg);
-                OBD_FREE(req->rq_reqmsg, pool->prp_rq_size);
+                LASSERT(req->rq_reqbuf);
+                LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
+                OBD_FREE(req->rq_reqbuf, pool->prp_rq_size);
                 OBD_FREE(req, sizeof(*req));
         }
         OBD_FREE(pool, sizeof(*pool));
@@ -212,7 +217,7 @@ void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
         int i;
         int size = 1;
 
-        while (size < pool->prp_rq_size)
+        while (size < pool->prp_rq_size + SPTLRPC_MAX_PAYLOAD)
                 size <<= 1;
 
         LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
@@ -234,7 +239,8 @@ void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
                         OBD_FREE(req, sizeof(struct ptlrpc_request));
                         return;
                 }
-                req->rq_reqmsg = msg;
+                req->rq_reqbuf = msg;
+                req->rq_reqbuf_len = size;
                 req->rq_pool = pool;
                 spin_lock(&pool->prp_lock);
                 list_add_tail(&req->rq_list, &pool->prp_req_list);
@@ -273,7 +279,7 @@ struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
 {
         struct ptlrpc_request *request;
-        struct lustre_msg *reqmsg;
+        struct lustre_msg *reqbuf;
 
         if (!pool)
                 return NULL;
@@ -294,51 +300,51 @@ static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_po
         list_del(&request->rq_list);
         spin_unlock(&pool->prp_lock);
 
-        LASSERT(request->rq_reqmsg);
+        LASSERT(request->rq_reqbuf);
         LASSERT(request->rq_pool);
 
-        reqmsg = request->rq_reqmsg;
+        reqbuf = request->rq_reqbuf;
         memset(request, 0, sizeof(*request));
-        request->rq_reqmsg = reqmsg;
+        request->rq_reqbuf = reqbuf;
+        request->rq_reqbuf_len = pool->prp_rq_size;
         request->rq_pool = pool;
-        request->rq_reqlen = pool->prp_rq_size;
         return request;
 }
 
-struct ptlrpc_request *
-ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
-                     int count, int *lengths, char **bufs,
-                     struct ptlrpc_request_pool *pool)
+static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
 {
-        struct ptlrpc_request *request = NULL;
-        int rc;
-        ENTRY;
-
-        /* The obd disconnected */
-        if (imp == NULL)
-                return NULL;
-
-        LASSERT(imp != LP_POISON);
-        LASSERT((unsigned long)imp->imp_client > 0x1000);
-        LASSERT(imp->imp_client != LP_POISON);
+        struct ptlrpc_request_pool *pool = request->rq_pool;
 
-        if (pool)
-                request = ptlrpc_prep_req_from_pool(pool);
+        spin_lock(&pool->prp_lock);
+        LASSERT(list_empty(&request->rq_list));
+        list_add_tail(&request->rq_list, &pool->prp_req_list);
+        spin_unlock(&pool->prp_lock);
+}
 
-        if (!request)
-                OBD_ALLOC(request, sizeof(*request));
+static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
+                                      __u32 version, int opcode,
+                                      int count, int *lengths, char **bufs,
+                                      struct ptlrpc_cli_ctx *ctx)
+{
+        struct obd_import  *imp = request->rq_import;
+        int                 rc;
+        ENTRY;
 
-        if (!request) {
-                CERROR("request allocation out of memory\n");
-                RETURN(NULL);
+        if (unlikely(ctx))
+                request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
+        else {
+                rc = sptlrpc_req_get_ctx(request);
+                if (rc)
+                        GOTO(out_free, rc);
         }
 
-        rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
-                                 bufs);
+        sptlrpc_req_set_flavor(request, opcode);
+
+        rc = lustre_pack_request(request, imp->imp_msg_magic, count,
+                                 lengths, bufs);
         if (rc) {
                 LASSERT(!request->rq_pool);
-                OBD_FREE(request, sizeof(*request));
-                RETURN(NULL);
+                GOTO(out_ctx, rc);
         }
 
         lustre_msg_add_version(request->rq_reqmsg, version);
@@ -349,7 +355,6 @@ ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
                 request->rq_timeout = obd_timeout;
         request->rq_send_state = LUSTRE_IMP_FULL;
         request->rq_type = PTL_RPC_MSG_REQUEST;
-        request->rq_import = class_import_get(imp);
         request->rq_export = NULL;
 
         request->rq_req_cbid.cbid_fn  = request_out_callback;
@@ -367,7 +372,10 @@ ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
         spin_lock_init(&request->rq_lock);
         CFS_INIT_LIST_HEAD(&request->rq_list);
         CFS_INIT_LIST_HEAD(&request->rq_replay_list);
+        CFS_INIT_LIST_HEAD(&request->rq_mod_list);
+        CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
         CFS_INIT_LIST_HEAD(&request->rq_set_chain);
+        CFS_INIT_LIST_HEAD(&request->rq_history_list);
         cfs_waitq_init(&request->rq_reply_waitq);
         request->rq_xid = ptlrpc_next_xid();
         atomic_set(&request->rq_refcount, 1);
@@ -375,7 +383,133 @@ ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
         lustre_msg_set_opc(request->rq_reqmsg, opcode);
         lustre_msg_set_flags(request->rq_reqmsg, 0);
 
-        RETURN(request);
+        RETURN(0);
+out_ctx:
+        sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
+out_free:
+        class_import_put(imp);
+        return rc;
+}
+
+int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
+                             __u32 version, int opcode, char **bufs,
+                             struct ptlrpc_cli_ctx *ctx)
+{
+        int count;
+
+        count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
+        return __ptlrpc_request_bufs_pack(request, version, opcode, count,
+                                          request->rq_pill.rc_area[RCL_CLIENT],
+                                          bufs, ctx);
+}
+EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
+
+int ptlrpc_request_pack(struct ptlrpc_request *request,
+                        __u32 version, int opcode) 
+{
+        return ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
+}
+
+static inline
+struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
+                                              struct ptlrpc_request_pool *pool)
+{
+        struct ptlrpc_request *request = NULL;
+
+        if (pool)
+                request = ptlrpc_prep_req_from_pool(pool);
+
+        if (!request)
+                OBD_ALLOC_PTR(request);
+
+        if (request) {
+                LASSERT((unsigned long)imp > 0x1000);
+                LASSERT(imp != LP_POISON);
+                LASSERT((unsigned long)imp->imp_client > 0x1000);
+                LASSERT(imp->imp_client != LP_POISON);
+
+                request->rq_import = class_import_get(imp);
+        } else {
+                CERROR("request allocation out of memory\n");
+        }
+
+        return request;
+}
+
+static struct ptlrpc_request *
+ptlrpc_request_alloc_internal(struct obd_import *imp,
+                              struct ptlrpc_request_pool * pool,
+                              const struct req_format *format)
+{
+        struct ptlrpc_request *request;
+
+        request = __ptlrpc_request_alloc(imp, pool);
+        if (request == NULL)
+                return NULL;
+
+        req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
+        req_capsule_set(&request->rq_pill, format);
+        return request;
+}
+
+struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
+                                            const struct req_format *format)
+{
+        return ptlrpc_request_alloc_internal(imp, NULL, format);
+}
+
+struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
+                                            struct ptlrpc_request_pool * pool,
+                                            const struct req_format *format)
+{
+        return ptlrpc_request_alloc_internal(imp, pool, format);
+}
+
+void ptlrpc_request_free(struct ptlrpc_request *request)
+{
+        if (request->rq_pool)
+                __ptlrpc_free_req_to_pool(request);
+        else
+                OBD_FREE_PTR(request);
+}
+
+struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
+                                                const struct req_format *format,
+                                                __u32 version, int opcode)
+{
+        struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
+        int                    rc;
+
+        if (req) {
+                rc = ptlrpc_request_pack(req, version, opcode);
+                if (rc) {
+                        ptlrpc_request_free(req);
+                        req = NULL;
+                }
+        }
+        return req;
+}
+
+struct ptlrpc_request *
+ptlrpc_prep_req_pool(struct obd_import *imp,
+                     __u32 version, int opcode,
+                     int count, int *lengths, char **bufs,
+                     struct ptlrpc_request_pool *pool)
+{
+        struct ptlrpc_request *request;
+        int                    rc;
+
+        request = __ptlrpc_request_alloc(imp, pool);
+        if (!request)
+                return NULL;
+
+        rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
+                                        lengths, bufs, NULL);
+        if (rc) {
+                ptlrpc_request_free(request);
+                request = NULL;
+        }
+        return request;
 }
 
 struct ptlrpc_request *
@@ -399,7 +533,8 @@ struct ptlrpc_request_set *ptlrpc_prep_set(void)
         set->set_remaining = 0;
         spin_lock_init(&set->set_new_req_lock);
         CFS_INIT_LIST_HEAD(&set->set_new_requests);
-
+        CFS_INIT_LIST_HEAD(&set->set_cblist);
+        
         RETURN(set);
 }
 
@@ -458,6 +593,22 @@ void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
         EXIT;
 }
 
+int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
+                      set_interpreter_func fn, void *data)
+{
+        struct ptlrpc_set_cbdata *cbdata;
+
+        OBD_ALLOC_PTR(cbdata);
+        if (cbdata == NULL)
+                RETURN(-ENOMEM);
+
+        cbdata->psc_interpret = fn;
+        cbdata->psc_data = data;
+        list_add_tail(&cbdata->psc_item, &set->set_cblist);
+
+        RETURN(0);
+}
+
 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
                         struct ptlrpc_request *req)
 {
@@ -500,7 +651,9 @@ static int ptlrpc_import_delay_req(struct obd_import *imp,
         LASSERT (status != NULL);
         *status = 0;
 
-        if (imp->imp_state == LUSTRE_IMP_NEW) {
+        if (req->rq_ctx_init || req->rq_ctx_fini) {
+                /* always allow ctx init/fini rpc go through */
+        } else if (imp->imp_state == LUSTRE_IMP_NEW) {
                 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
                 *status = -EIO;
                 LBUG();
@@ -508,23 +661,36 @@ static int ptlrpc_import_delay_req(struct obd_import *imp,
                 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
                 *status = -EIO;
         } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
-                 imp->imp_state == LUSTRE_IMP_CONNECTING) {
+                   imp->imp_state == LUSTRE_IMP_CONNECTING) {
                 /* allow CONNECT even if import is invalid */ ;
-        } else if (imp->imp_invalid) {
+                if (atomic_read(&imp->imp_inval_count) != 0) {
+                        DEBUG_REQ(D_ERROR, req, "invalidate in flight");
+                        *status = -EIO;
+                }
+
+        } else if ((imp->imp_invalid && (!imp->imp_recon_bk)) ||
+                                         imp->imp_obd->obd_no_recov) {
                 /* If the import has been invalidated (such as by an OST
-                 * failure), the request must fail with -EIO. */
+                 * failure), and if the import(MGC) tried all of its connection
+                 * list (Bug 13464), the request must fail with -ESHUTDOWN.
+                 * This indicates the requests should be discarded; an -EIO
+                 * may result in a resend of the request. */
                 if (!imp->imp_deactive)
-                        DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
-                *status = -EIO;
+                          DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
+                *status = -ESHUTDOWN; /* bz 12940 */
         } else if (req->rq_import_generation != imp->imp_generation) {
                 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
                 *status = -EIO;
         } else if (req->rq_send_state != imp->imp_state) {
-                if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
-                    req->rq_no_delay)
+                /* invalidate in progress - any requests should be drop */
+                if (atomic_read(&imp->imp_inval_count) != 0) {
+                        DEBUG_REQ(D_ERROR, req, "invalidate in flight");
+                        *status = -EIO;
+                } else if (imp->imp_dlm_fake || req->rq_no_delay) {
                         *status = -EWOULDBLOCK;
-                else
+                } else {
                         delay = 1;
+                }
         }
 
         RETURN(delay);
@@ -570,8 +736,12 @@ static int ptlrpc_check_status(struct ptlrpc_request *req)
 
         err = lustre_msg_get_status(req->rq_repmsg);
         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
-                DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err == %d",
-                          err);
+                struct obd_import *imp = req->rq_import;
+                __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
+                LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
+                                " with %s. The %s operation failed with %d\n",
+                                libcfs_nid2str(imp->imp_connection->c_peer.nid),
+                                ll_opcode2str(opc), err);
                 RETURN(err < 0 ? err : -EINVAL);
         }
 
@@ -588,52 +758,81 @@ static int ptlrpc_check_status(struct ptlrpc_request *req)
 static int after_reply(struct ptlrpc_request *req)
 {
         struct obd_import *imp = req->rq_import;
+        struct obd_device *obd = req->rq_import->imp_obd;
         int rc;
+        struct timeval work_start;
+        long timediff;
         ENTRY;
 
         LASSERT(!req->rq_receiving_reply);
+        LASSERT(obd);
+        LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
 
         /* NB Until this point, the whole of the incoming message,
          * including buflens, status etc is in the sender's byte order. */
 
-#if SWAB_PARANOIA
         /* Clear reply swab mask; this is a new reply in sender's byte order */
         req->rq_rep_swab_mask = 0;
-#endif
-        LASSERT (req->rq_nob_received <= req->rq_replen);
-        rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
+
+        rc = sptlrpc_cli_unwrap_reply(req);
         if (rc) {
-                DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d\n", rc);
+                DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
+                RETURN(rc);
+        }
+
+        /* security layer unwrap might ask resend this request */
+        if (req->rq_resend)
+                RETURN(0);
+
+        rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
+        if (rc) {
+                DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
                 RETURN(-EPROTO);
         }
 
         rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
         if (rc) {
-                DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d\n", rc);
+                DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
                 RETURN(-EPROTO);
         }
 
+        do_gettimeofday(&work_start);
+        timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
+        if (obd->obd_svc_stats != NULL)
+                lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
+                                    timediff);
+
         if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
             lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
-                DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)\n",
+                DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
                           lustre_msg_get_type(req->rq_repmsg));
                 RETURN(-EPROTO);
         }
 
         rc = ptlrpc_check_status(req);
+        imp->imp_connect_error = rc;
 
-        /* Either we've been evicted, or the server has failed for
-         * some reason. Try to reconnect, and if that fails, punt to the
-         * upcall. */
-        if ((rc == -ENOTCONN) || (rc == -ENODEV)) {
-                if (req->rq_send_state != LUSTRE_IMP_FULL ||
-                    imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
-                        RETURN(-ENOTCONN);
+        if (rc) {
+                /* Either we've been evicted, or the server has failed for
+                 * some reason. Try to reconnect, and if that fails, punt to
+                 * the upcall. */
+                if (ll_rpc_recoverable_error(rc)) {
+                        if (req->rq_send_state != LUSTRE_IMP_FULL ||
+                            imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
+                                RETURN(rc);
+                        }
+                        ptlrpc_request_handle_notconn(req);
+                        RETURN(rc);
+                }
+        } else {
+                /* Let's look if server send slv. Do it only for RPC with 
+                 * rc == 0. */
+                if (imp->imp_obd->obd_namespace) {
+                        /* Disconnect rpc is sent when namespace is already 
+                         * destroyed. Let's check this and will not try update
+                         * pool. */
+                        ldlm_cli_update_pool(req);
                 }
-
-                ptlrpc_request_handle_notconn(req);
-
-                RETURN(rc);
         }
 
         /* Store transno in reqmsg for replay. */
@@ -656,9 +855,10 @@ static int after_reply(struct ptlrpc_request *req)
                 }
 
                 /* Replay-enabled imports return commit-status information. */
-                if (lustre_msg_get_last_committed(req->rq_repmsg))
+                if (lustre_msg_get_last_committed(req->rq_repmsg)) {
                         imp->imp_peer_committed_transno =
                                 lustre_msg_get_last_committed(req->rq_repmsg);
+                }
                 ptlrpc_free_committed(imp);
                 spin_unlock(&imp->imp_lock);
         }
@@ -673,6 +873,9 @@ static int ptlrpc_send_new_req(struct ptlrpc_request *req)
         ENTRY;
 
         LASSERT(req->rq_phase == RQ_PHASE_NEW);
+        if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()))
+                RETURN (0);
+        
         req->rq_phase = RQ_PHASE_RPC;
 
         imp = req->rq_import;
@@ -710,6 +913,20 @@ static int ptlrpc_send_new_req(struct ptlrpc_request *req)
         spin_unlock(&imp->imp_lock);
 
         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
+
+        rc = sptlrpc_req_refresh_ctx(req, -1);
+        if (rc) {
+                if (req->rq_err) {
+                        req->rq_status = rc;
+                        RETURN(1);
+                } else {
+                        /* here begins timeout counting */
+                        req->rq_sent = cfs_time_current_sec();
+                        req->rq_wait_ctx = 1;
+                        RETURN(0);
+                }
+        }
+
         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
                " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
                imp->imp_obd->obd_uuid.uuid,
@@ -746,6 +963,9 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                     ptlrpc_send_new_req(req)) {
                         force_timer_recalc = 1;
                 }
+                /* delayed send - skip */
+                if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
+                        continue;
 
                 if (!(req->rq_phase == RQ_PHASE_RPC ||
                       req->rq_phase == RQ_PHASE_BULK ||
@@ -766,6 +986,7 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
 
                 if (req->rq_err) {
                         ptlrpc_unregister_reply(req);
+                        req->rq_replied = 0;
                         if (req->rq_status == 0)
                                 req->rq_status = -EIO;
                         req->rq_phase = RQ_PHASE_INTERPRET;
@@ -782,7 +1003,8 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                  * path sets rq_intr irrespective of whether ptlrpcd has
                  * seen a timeout.  our policy is to only interpret
                  * interrupted rpcs after they have timed out */
-                if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
+                if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
+                                     req->rq_wait_ctx)) {
                         /* NB could be on delayed list */
                         ptlrpc_unregister_reply(req);
                         req->rq_status = -EINTR;
@@ -796,9 +1018,16 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                 }
 
                 if (req->rq_phase == RQ_PHASE_RPC) {
-                        if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
+                        if (req->rq_timedout || req->rq_resend ||
+                            req->rq_waiting || req->rq_wait_ctx) {
                                 int status;
 
+                                /* rq_wait_ctx is only touched in ptlrpcd,
+                                 * no lock needed here.
+                                 */
+                                if (req->rq_wait_ctx)
+                                        goto check_ctx;
+
                                 ptlrpc_unregister_reply(req);
 
                                 spin_lock(&imp->imp_lock);
@@ -815,7 +1044,7 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                                         spin_unlock(&imp->imp_lock);
                                         GOTO(interpret, req->rq_status);
                                 }
-                                if (req->rq_no_resend) {
+                                if (req->rq_no_resend && !req->rq_wait_ctx) {
                                         req->rq_status = -ENOTCONN;
                                         req->rq_phase = RQ_PHASE_INTERPRET;
                                         spin_unlock(&imp->imp_lock);
@@ -843,6 +1072,23 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                                                        old_xid, req->rq_xid);
                                         }
                                 }
+check_ctx:
+                                status = sptlrpc_req_refresh_ctx(req, -1);
+                                if (status) {
+                                        if (req->rq_err) {
+                                                req->rq_status = status;
+                                                force_timer_recalc = 1;
+                                        }
+                                        if (!req->rq_wait_ctx) {
+                                                /* begins timeout counting */
+                                                req->rq_sent = cfs_time_current_sec();
+                                                req->rq_wait_ctx = 1;
+                                        }
+                                        continue;
+                                } else {
+                                        req->rq_sent = 0;
+                                        req->rq_wait_ctx = 0;
+                                }
 
                                 rc = ptl_send_rpc(req, 0);
                                 if (rc) {
@@ -872,10 +1118,10 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                                 /* Add this req to the delayed list so
                                    it can be errored if the import is
                                    evicted after recovery. */
-                                spin_lock(&req->rq_lock);
+                                spin_lock(&imp->imp_lock);
                                 list_add_tail(&req->rq_list,
                                               &imp->imp_delayed_list);
-                                spin_unlock(&req->rq_lock);
+                                spin_unlock(&imp->imp_lock);
                                 continue;
                         }
 
@@ -915,14 +1161,13 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                 if (req->rq_bulk != NULL)
                         ptlrpc_unregister_bulk (req);
 
-                req->rq_phase = RQ_PHASE_COMPLETE;
-
                 if (req->rq_interpret_reply != NULL) {
                         int (*interpreter)(struct ptlrpc_request *,void *,int) =
                                 req->rq_interpret_reply;
                         req->rq_status = interpreter(req, &req->rq_async_args,
                                                      req->rq_status);
                 }
+                req->rq_phase = RQ_PHASE_COMPLETE;
 
                 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
                        "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
@@ -931,9 +1176,8 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
                        lustre_msg_get_opc(req->rq_reqmsg));
 
-                set->set_remaining--;
-
                 atomic_dec(&imp->imp_inflight);
+                set->set_remaining--;
                 cfs_waitq_signal(&imp->imp_recovery_waitq);
         }
 
@@ -944,16 +1188,19 @@ int ptlrpc_check_set(struct ptlrpc_request_set *set)
 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
 {
         struct obd_import *imp = req->rq_import;
+        int rc = 0;
         ENTRY;
 
-        DEBUG_REQ(D_ERROR|D_NETERROR, req, "timeout (sent at %lu, %lus ago)",
-                  (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
+        DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, "CFS_DURATION_T"s ago)",
+                  req->rq_net_err ? "network error" : "timeout",
+                  (long)req->rq_sent, cfs_time_current_sec() - req->rq_sent);
 
         if (imp != NULL && obd_debug_peer_on_timeout)
                 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
 
         spin_lock(&req->rq_lock);
         req->rq_timedout = 1;
+        req->rq_wait_ctx = 0;
         spin_unlock(&req->rq_lock);
 
         ptlrpc_unregister_reply (req);
@@ -975,7 +1222,8 @@ int ptlrpc_expire_one_request(struct ptlrpc_request *req)
 
         /* If this request is for recovery or other primordial tasks,
          * then error it out here. */
-        if (req->rq_send_state != LUSTRE_IMP_FULL ||
+        if (req->rq_ctx_init || req->rq_ctx_fini ||
+            req->rq_send_state != LUSTRE_IMP_FULL ||
             imp->imp_obd->obd_no_recov) {
                 spin_lock(&req->rq_lock);
                 req->rq_status = -ETIMEDOUT;
@@ -983,17 +1231,23 @@ int ptlrpc_expire_one_request(struct ptlrpc_request *req)
                 spin_unlock(&req->rq_lock);
                 RETURN(1);
         }
+        
+        /* if request can't be resend we can't wait answer after timeout */
+        if (req->rq_no_resend) {
+                DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
+                rc = 1;
+        }
 
         ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
 
-        RETURN(0);
+        RETURN(rc);
 }
 
 int ptlrpc_expired_set(void *data)
 {
         struct ptlrpc_request_set *set = data;
         struct list_head          *tmp;
-        time_t                     now = CURRENT_SECONDS;
+        time_t                     now = cfs_time_current_sec();
         ENTRY;
 
         LASSERT(set != NULL);
@@ -1053,7 +1307,7 @@ void ptlrpc_interrupted_set(void *data)
 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
 {
         struct list_head      *tmp;
-        time_t                 now = CURRENT_SECONDS;
+        time_t                 now = cfs_time_current_sec();
         time_t                 deadline;
         int                    timeout = 0;
         struct ptlrpc_request *req;
@@ -1066,13 +1320,18 @@ int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
 
                 /* request in-flight? */
                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
-                      (req->rq_phase == RQ_PHASE_BULK)))
+                      (req->rq_phase == RQ_PHASE_BULK) ||
+                      (req->rq_phase == RQ_PHASE_NEW)))
                         continue;
 
                 if (req->rq_timedout)   /* already timed out */
                         continue;
 
-                deadline = req->rq_sent + req->rq_timeout;
+                if (req->rq_phase == RQ_PHASE_NEW)
+                        deadline = req->rq_sent;
+                else
+                        deadline = req->rq_sent + req->rq_timeout;
+
                 if (deadline <= now)    /* actually expired already */
                         timeout = 1;    /* ASAP */
                 else if (timeout == 0 || timeout > deadline - now)
@@ -1103,7 +1362,7 @@ int ptlrpc_set_wait(struct ptlrpc_request_set *set)
 
                 /* wait until all complete, interrupted, or an in-flight
                  * req times out */
-                CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
+                CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
                        set, timeout);
                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
                                        ptlrpc_expired_set,
@@ -1136,20 +1395,23 @@ int ptlrpc_set_wait(struct ptlrpc_request_set *set)
                 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
                         set->set_interpret;
                 rc = interpreter (set, set->set_arg, rc);
+        } else {
+                struct ptlrpc_set_cbdata *cbdata, *n;
+                int err;
+
+                list_for_each_entry_safe(cbdata, n,
+                                         &set->set_cblist, psc_item) {
+                        list_del_init(&cbdata->psc_item);
+                        err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
+                        if (err && !rc)
+                                rc = err;
+                        OBD_FREE_PTR(cbdata);
+                }
         }
 
         RETURN(rc);
 }
 
-static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
-{
-        struct ptlrpc_request_pool *pool = request->rq_pool;
-
-        spin_lock(&pool->prp_lock);
-        list_add_tail(&request->rq_list, &pool->prp_req_list);
-        spin_unlock(&pool->prp_lock);
-}
-
 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
 {
         ENTRY;
@@ -1162,12 +1424,16 @@ static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
         LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
         LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
         LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
+        LASSERT(request->rq_cli_ctx);
+
+        req_capsule_fini(&request->rq_pill);
 
         /* We must take it off the imp_replay_list first.  Otherwise, we'll set
          * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
         if (request->rq_import != NULL) {
                 if (!locked)
                         spin_lock(&request->rq_import->imp_lock);
+                list_del_init(&request->rq_mod_list);
                 list_del_init(&request->rq_replay_list);
                 if (!locked)
                         spin_unlock(&request->rq_import->imp_lock);
@@ -1180,10 +1446,8 @@ static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
                 LBUG();
         }
 
-        if (request->rq_repmsg != NULL) {
-                OBD_FREE(request->rq_repmsg, request->rq_replen);
-                request->rq_repmsg = NULL;
-        }
+        if (request->rq_repbuf != NULL)
+                sptlrpc_cli_free_repbuf(request);
         if (request->rq_export != NULL) {
                 class_export_put(request->rq_export);
                 request->rq_export = NULL;
@@ -1195,15 +1459,15 @@ static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
         if (request->rq_bulk != NULL)
                 ptlrpc_free_bulk(request->rq_bulk);
 
-        if (request->rq_pool) {
+        if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
+                sptlrpc_cli_free_reqbuf(request);
+
+        sptlrpc_req_put_ctx(request, !locked);
+
+        if (request->rq_pool)
                 __ptlrpc_free_req_to_pool(request);
-        } else {
-                if (request->rq_reqmsg != NULL) {
-                        OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
-                        request->rq_reqmsg = NULL;
-                }
+        else
                 OBD_FREE(request, sizeof(*request));
-        }
         EXIT;
 }
 
@@ -1308,12 +1572,12 @@ void ptlrpc_free_committed(struct obd_import *imp)
 
         if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
             imp->imp_generation == imp->imp_last_generation_checked) {
-                CDEBUG(D_HA, "%s: skip recheck for last_committed "LPU64"\n",
+                CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
                        imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
                 return;
         }
-        
-        CDEBUG(D_HA, "%s: committing for last_committed "LPU64" gen %d\n",
+
+        CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
                imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
                imp->imp_generation);
         imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
@@ -1327,24 +1591,27 @@ void ptlrpc_free_committed(struct obd_import *imp)
                 last_req = req;
 
                 if (req->rq_import_generation < imp->imp_generation) {
-                        DEBUG_REQ(D_HA, req, "freeing request with old gen");
+                        DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
                         GOTO(free_req, 0);
                 }
 
                 if (req->rq_replay) {
-                        DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
+                        DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
                         continue;
                 }
 
                 /* not yet committed */
                 if (req->rq_transno > imp->imp_peer_committed_transno) {
-                        DEBUG_REQ(D_HA, req, "stopping search");
+                        DEBUG_REQ(D_RPCTRACE, req, "stopping search");
                         break;
                 }
 
-                DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
+                DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
                           imp->imp_peer_committed_transno);
 free_req:
+                spin_lock(&req->rq_lock);
+                req->rq_replay = 0;
+                spin_unlock(&req->rq_lock);
                 if (req->rq_commit_cb != NULL)
                         req->rq_commit_cb(req);
                 list_del_init(&req->rq_replay_list);
@@ -1402,6 +1669,10 @@ static int expired_request(void *data)
         struct ptlrpc_request *req = data;
         ENTRY;
 
+        /* some failure can suspend regular timeouts */
+        if (ptlrpc_check_suspend())
+                RETURN(1);
+
         RETURN(ptlrpc_expire_one_request(req));
 }
 
@@ -1566,6 +1837,23 @@ restart:
         list_add_tail(&req->rq_list, &imp->imp_sending_list);
         spin_unlock(&imp->imp_lock);
 
+        rc = sptlrpc_req_refresh_ctx(req, 0);
+        if (rc) {
+                if (req->rq_err) {
+                        /* we got fatal ctx refresh error, directly jump out
+                         * thus we can pass back the actual error code.
+                         */
+                        spin_lock(&imp->imp_lock);
+                        list_del_init(&req->rq_list);
+                        spin_unlock(&imp->imp_lock);
+
+                        CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
+                        GOTO(out, rc);
+                }
+                /* simulating we got error during send rpc */
+                goto after_send;
+        }
+
         rc = ptl_send_rpc(req, 0);
         if (rc) {
                 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
@@ -1575,10 +1863,14 @@ restart:
                 DEBUG_REQ(D_NET, req, 
                           "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
         }
+repeat:
         lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
                                req);
-        l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
+        rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
+        if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
+                goto repeat;
 
+after_send:
         CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
                "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
                imp->imp_obd->obd_uuid.uuid,
@@ -1687,7 +1979,8 @@ static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
         }
 
         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
-            lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN)
+            (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
+             lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
 
         /* The transno had better not change over replay. */
@@ -1710,10 +2003,17 @@ static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
                 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
         }
 
-        spin_lock(&imp->imp_lock);
-        imp->imp_last_replay_transno = req->rq_transno;
-        spin_unlock(&imp->imp_lock);
-
+        /*
+         * Errors while replay can set transno to 0, but
+         * imp_last_replay_transno shouldn't be set to 0 anyway
+         */
+        if (req->rq_transno > 0) {
+                spin_lock(&imp->imp_lock);
+                LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
+                imp->imp_last_replay_transno = req->rq_transno;
+                spin_unlock(&imp->imp_lock);
+        } else
+                CERROR("Transno is 0 during replay!\n");
         /* continue with recovery */
         rc = ptlrpc_import_recovery_state_machine(imp);
  out:
@@ -1726,7 +2026,6 @@ static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
         RETURN(rc);
 }
 
-
 int ptlrpc_replay_req(struct ptlrpc_request *req)
 {
         struct ptlrpc_replay_async_args *aa;
@@ -1777,7 +2076,7 @@ void ptlrpc_abort_inflight(struct obd_import *imp)
                 struct ptlrpc_request *req =
                         list_entry(tmp, struct ptlrpc_request, rq_list);
 
-                DEBUG_REQ(D_HA, req, "inflight");
+                DEBUG_REQ(D_RPCTRACE, req, "inflight");
 
                 spin_lock (&req->rq_lock);
                 if (req->rq_import_generation < imp->imp_generation) {
@@ -1791,7 +2090,7 @@ void ptlrpc_abort_inflight(struct obd_import *imp)
                 struct ptlrpc_request *req =
                         list_entry(tmp, struct ptlrpc_request, rq_list);
 
-                DEBUG_REQ(D_HA, req, "aborting waiting req");
+                DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
 
                 spin_lock (&req->rq_lock);
                 if (req->rq_import_generation < imp->imp_generation) {