+/**
+ * Calculate the amount of time for lock prolongation.
+ *
+ * This is helper for ofd_prolong_extent_locks() function to get
+ * the timeout extra time.
+ *
+ * \param[in] req current request
+ *
+ * \retval amount of time to extend the timeout with
+ */
+static inline int prolong_timeout(struct ptlrpc_request *req)
+{
+ struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
+ time_t req_timeout;
+
+ if (AT_OFF)
+ return obd_timeout / 2;
+
+ req_timeout = req->rq_deadline - req->rq_arrival_time.tv_sec;
+ return max_t(time_t, at_est2timeout(at_get(&svcpt->scp_at_estimate)),
+ req_timeout);
+}
+
+/**
+ * Prolong lock timeout for the given extent.
+ *
+ * This function finds all locks related with incoming request and
+ * prolongs their timeout.
+ *
+ * If a client is holding a lock for a long time while it sends
+ * read or write RPCs to the OST for the object under this lock,
+ * then we don't want the OST to evict the client. Otherwise,
+ * if the network or disk is very busy then the client may not
+ * be able to make any progress to clear out dirty pages under
+ * the lock and the application will fail.
+ *
+ * Every time a Bulk Read/Write (BRW) request arrives for the object
+ * covered by the lock, extend the timeout on that lock. The RPC should
+ * contain a lock handle for the lock it is using, but this
+ * isn't handled correctly by all client versions, and the
+ * request may cover multiple locks.
+ *
+ * \param[in] tsi target session environment for this request
+ * \param[in] data struct of data to prolong locks
+ *
+ */
+static void ofd_prolong_extent_locks(struct tgt_session_info *tsi,
+ struct ldlm_prolong_args *data)
+{
+ struct obdo *oa = &tsi->tsi_ost_body->oa;
+ struct ldlm_lock *lock;
+
+ ENTRY;
+
+ data->lpa_timeout = prolong_timeout(tgt_ses_req(tsi));
+ data->lpa_export = tsi->tsi_exp;
+ data->lpa_resid = tsi->tsi_resid;
+
+ CDEBUG(D_RPCTRACE, "Prolong locks for req %p with x%llu"
+ " ext(%llu->%llu)\n", tgt_ses_req(tsi),
+ tgt_ses_req(tsi)->rq_xid, data->lpa_extent.start,
+ data->lpa_extent.end);
+
+ if (oa->o_valid & OBD_MD_FLHANDLE) {
+ /* mostly a request should be covered by only one lock, try
+ * fast path. */
+ lock = ldlm_handle2lock(&oa->o_handle);
+ if (lock != NULL) {
+ /* Fast path to check if the lock covers the whole IO
+ * region exclusively. */
+ if (ldlm_extent_contain(&lock->l_policy_data.l_extent,
+ &data->lpa_extent)) {
+ /* bingo */
+ LASSERT(lock->l_export == data->lpa_export);
+ ldlm_lock_prolong_one(lock, data);
+ LDLM_LOCK_PUT(lock);
+ RETURN_EXIT;
+ }
+ lock->l_last_used = cfs_time_current();
+ LDLM_LOCK_PUT(lock);
+ }
+ }
+
+ ldlm_resource_prolong(data);
+ EXIT;
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OFD RW requests.
+ *
+ * Determine if \a lock and the lock from request \a req are equivalent
+ * by comparing their resource names, modes, and extents.
+ *
+ * It is used to give priority to read and write RPCs being done
+ * under this lock so that the client can drop the contended
+ * lock more quickly and let other clients use it. This improves
+ * overall performance in the case where the first client gets a
+ * very large lock extent that prevents other clients from
+ * submitting their writes.
+ *
+ * \param[in] req ptlrpc_request being processed
+ * \param[in] lock contended lock to match
+ *
+ * \retval 1 if lock is matched
+ * \retval 0 otherwise
+ */
+static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
+ struct ldlm_lock *lock)
+{
+ struct niobuf_remote *rnb;
+ struct obd_ioobj *ioo;
+ enum ldlm_mode mode;
+ struct ldlm_extent ext;
+ __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
+
+ ENTRY;
+
+ ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
+ LASSERT(ioo != NULL);
+
+ rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
+ LASSERT(rnb != NULL);
+
+ ext.start = rnb->rnb_offset;
+ rnb += ioo->ioo_bufcnt - 1;
+ ext.end = rnb->rnb_offset + rnb->rnb_len - 1;
+
+ LASSERT(lock->l_resource != NULL);
+ if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
+ RETURN(0);
+
+ /* a bulk write can only hold a reference on a PW extent lock
+ * or GROUP lock.
+ */
+ mode = LCK_PW | LCK_GROUP;
+ if (opc == OST_READ)
+ /* whereas a bulk read can be protected by either a PR or PW
+ * extent lock */
+ mode |= LCK_PR;
+
+ if (!(lock->l_granted_mode & mode))
+ RETURN(0);
+
+ RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OFD RW requests.
+ *
+ * Check for whether the given PTLRPC request (\a req) is blocking
+ * an LDLM lock cancel. Also checks whether the request is covered by an LDLM
+ * lock.
+ *
+ * \param[in] req the incoming request
+ *
+ * \retval 1 if \a req is blocking an LDLM lock cancel
+ * \retval 0 if it is not
+ * \retval -ESTALE if lock is not found
+ */
+static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
+{
+ struct tgt_session_info *tsi;
+ struct obd_ioobj *ioo;
+ struct niobuf_remote *rnb;
+ int opc;
+ struct ldlm_prolong_args pa = { 0 };
+
+ ENTRY;
+
+ /* Don't use tgt_ses_info() to get session info, because lock_match()
+ * can be called while request has no processing thread yet. */
+ tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
+
+ /*
+ * Use LASSERT below because malformed RPCs should have
+ * been filtered out in tgt_hpreq_handler().
+ */
+ opc = lustre_msg_get_opc(req->rq_reqmsg);
+ LASSERT(opc == OST_READ || opc == OST_WRITE);
+
+ ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
+ LASSERT(ioo != NULL);
+
+ rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
+ LASSERT(rnb != NULL);
+ LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
+
+ pa.lpa_mode = LCK_PW | LCK_GROUP;
+ if (opc == OST_READ)
+ pa.lpa_mode |= LCK_PR;
+
+ pa.lpa_extent.start = rnb->rnb_offset;
+ rnb += ioo->ioo_bufcnt - 1;
+ pa.lpa_extent.end = rnb->rnb_offset + rnb->rnb_len - 1;
+
+ DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
+ " (%llu->%llu)\n", tgt_name(tsi->tsi_tgt),
+ current->comm, PFID(&tsi->tsi_fid), pa.lpa_extent.start,
+ pa.lpa_extent.end);
+
+ ofd_prolong_extent_locks(tsi, &pa);
+
+ CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
+ tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
+
+ if (pa.lpa_blocks_cnt > 0)
+ RETURN(1);
+
+ RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OFD RW requests.
+ *
+ * Called after the request has been handled. It refreshes lock timeout again
+ * so that client has more time to send lock cancel RPC.
+ *
+ * \param[in] req request which is being processed.
+ */
+static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
+{
+ ofd_rw_hpreq_check(req);
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OST_PUNCH request.
+ *
+ * This function checks if the given lock is the same by its resname, mode
+ * and extent as one taken from the request.
+ * It is used to give priority to punch/truncate RPCs that might lead to
+ * the fastest release of that lock when a lock is contended.
+ *
+ * \param[in] req ptlrpc_request being processed
+ * \param[in] lock contended lock to match
+ *
+ * \retval 1 if lock is matched
+ * \retval 0 otherwise
+ */
+static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
+ struct ldlm_lock *lock)
+{
+ struct tgt_session_info *tsi;
+ struct obdo *oa;
+ struct ldlm_extent ext;
+
+ ENTRY;
+
+ /* Don't use tgt_ses_info() to get session info, because lock_match()
+ * can be called while request has no processing thread yet. */
+ tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
+
+ /*
+ * Use LASSERT below because malformed RPCs should have
+ * been filtered out in tgt_hpreq_handler().
+ */
+ LASSERT(tsi->tsi_ost_body != NULL);
+ if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
+ tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
+ RETURN(1);
+
+ oa = &tsi->tsi_ost_body->oa;
+ ext.start = oa->o_size;
+ ext.end = oa->o_blocks;
+
+ LASSERT(lock->l_resource != NULL);
+ if (!ostid_res_name_eq(&oa->o_oi, &lock->l_resource->lr_name))
+ RETURN(0);
+
+ if (!(lock->l_granted_mode & (LCK_PW | LCK_GROUP)))
+ RETURN(0);
+
+ RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
+ *
+ * High-priority queue request check for whether the given punch request
+ * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
+ * covered by an LDLM lock.
+ *
+
+ *
+ * \param[in] req the incoming request
+ *
+ * \retval 1 if \a req is blocking an LDLM lock cancel
+ * \retval 0 if it is not
+ * \retval -ESTALE if lock is not found
+ */
+static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
+{
+ struct tgt_session_info *tsi;
+ struct obdo *oa;
+ struct ldlm_prolong_args pa = { 0 };
+
+ ENTRY;
+
+ /* Don't use tgt_ses_info() to get session info, because lock_match()
+ * can be called while request has no processing thread yet. */
+ tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
+ LASSERT(tsi != NULL);
+ oa = &tsi->tsi_ost_body->oa;
+
+ LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
+ oa->o_flags & OBD_FL_SRVLOCK));
+
+ pa.lpa_mode = LCK_PW | LCK_GROUP;
+ pa.lpa_extent.start = oa->o_size;
+ pa.lpa_extent.end = oa->o_blocks;
+
+ CDEBUG(D_DLMTRACE,
+ "%s: refresh locks: %llu/%llu (%llu->%llu)\n",
+ tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
+ tsi->tsi_resid.name[1], pa.lpa_extent.start, pa.lpa_extent.end);
+
+ ofd_prolong_extent_locks(tsi, &pa);
+
+ CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
+ tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
+
+ if (pa.lpa_blocks_cnt > 0)
+ RETURN(1);
+
+ RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
+}
+
+/**
+ * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
+ *
+ * Called after the request has been handled. It refreshes lock timeout again
+ * so that client has more time to send lock cancel RPC.
+ *
+ * \param[in] req request which is being processed.
+ */
+static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
+{
+ ofd_punch_hpreq_check(req);
+}
+
+static struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
+ .hpreq_lock_match = ofd_rw_hpreq_lock_match,
+ .hpreq_check = ofd_rw_hpreq_check,
+ .hpreq_fini = ofd_rw_hpreq_fini
+};
+
+static struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
+ .hpreq_lock_match = ofd_punch_hpreq_lock_match,
+ .hpreq_check = ofd_punch_hpreq_check,
+ .hpreq_fini = ofd_punch_hpreq_fini
+};
+
+/**
+ * Assign high priority operations to an IO request.
+ *
+ * Check if the incoming request is a candidate for
+ * high-priority processing. If it is, assign it a high
+ * priority operations table.
+ *
+ * \param[in] tsi target session environment for this request
+ */
+static void ofd_hp_brw(struct tgt_session_info *tsi)
+{
+ struct niobuf_remote *rnb;
+ struct obd_ioobj *ioo;
+
+ ENTRY;
+
+ ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
+ LASSERT(ioo != NULL); /* must exist after request preprocessing */
+ if (ioo->ioo_bufcnt > 0) {
+ rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
+ LASSERT(rnb != NULL); /* must exist after request preprocessing */
+
+ /* no high priority if server lock is needed */
+ if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
+ (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg)
+ & MSG_REPLAY))
+ return;
+ }
+ tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
+}
+
+/**
+ * Assign high priority operations to an punch request.
+ *
+ * Check if the incoming request is a candidate for
+ * high-priority processing. If it is, assign it a high
+ * priority operations table.
+ *
+ * \param[in] tsi target session environment for this request
+ */
+static void ofd_hp_punch(struct tgt_session_info *tsi)
+{
+ LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
+ /* no high-priority if server lock is needed */
+ if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
+ tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
+ tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
+ lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
+ return;
+ tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
+}
+