+/**
+ * Completion AST for DOM discard locks:
+ *
+ * CP AST an DOM discard lock is called always right after enqueue or from
+ * reprocess if lock was blocked, in the latest case l_ast_data is set to
+ * the mdt_object which is kept while there are pending locks on it.
+ */
+int ldlm_dom_discard_cp_ast(struct ldlm_lock *lock, __u64 flags, void *data)
+{
+ struct mdt_object *mo;
+ struct lustre_handle dom_lh;
+ struct lu_env *env;
+
+ ENTRY;
+
+ /* l_ast_data is set when lock was not granted immediately
+ * in mdt_dom_discard_data() below but put into waiting list,
+ * so this CP callback means we are finished and corresponding
+ * MDT object should be released finally as well as lock itself.
+ */
+ lock_res_and_lock(lock);
+ if (!lock->l_ast_data) {
+ unlock_res_and_lock(lock);
+ RETURN(0);
+ }
+
+ mo = lock->l_ast_data;
+ lock->l_ast_data = NULL;
+ unlock_res_and_lock(lock);
+
+ ldlm_lock2handle(lock, &dom_lh);
+ ldlm_lock_decref(&dom_lh, LCK_PW);
+
+ env = lu_env_find();
+ LASSERT(env);
+ mdt_object_put(env, mo);
+
+ RETURN(0);
+}
+
+void mdt_dom_discard_data(struct mdt_thread_info *info,
+ struct mdt_object *mo)
+{
+ struct ptlrpc_request *req = mdt_info_req(info);
+ struct mdt_device *mdt = mdt_dev(mo->mot_obj.lo_dev);
+ union ldlm_policy_data policy;
+ struct ldlm_res_id res_id;
+ struct lustre_handle dom_lh;
+ struct ldlm_lock *lock;
+ __u64 flags = LDLM_FL_AST_DISCARD_DATA;
+ int rc = 0;
+ bool old_client;
+
+ ENTRY;
+
+ if (req && req_is_replay(req))
+ RETURN_EXIT;
+
+ policy.l_inodebits.bits = MDS_INODELOCK_DOM;
+ policy.l_inodebits.try_bits = 0;
+ fid_build_reg_res_name(mdt_object_fid(mo), &res_id);
+
+ /* Keep blocking version of discard for an old client to avoid
+ * crashes on non-patched clients. LU-11359.
+ */
+ old_client = req && !(exp_connect_flags2(req->rq_export) &
+ OBD_CONNECT2_ASYNC_DISCARD);
+
+ /* Tell the clients that the object is gone now and that they should
+ * throw away any cached pages. */
+ rc = ldlm_cli_enqueue_local(info->mti_env, mdt->mdt_namespace, &res_id,
+ LDLM_IBITS, &policy, LCK_PW, &flags,
+ ldlm_blocking_ast, old_client ?
+ ldlm_completion_ast :
+ ldlm_dom_discard_cp_ast,
+ NULL, NULL, 0, LVB_T_NONE, NULL, &dom_lh);
+ if (rc != ELDLM_OK) {
+ CDEBUG(D_DLMTRACE,
+ "Failed to issue discard lock, rc = %d\n", rc);
+ RETURN_EXIT;
+ }
+
+ lock = ldlm_handle2lock(&dom_lh);
+ lock_res_and_lock(lock);
+ /* if lock is not granted then there are BL ASTs in progress and
+ * lock will be granted in result of reprocessing with CP callback
+ * notifying about that. The mdt object has to be kept until that and
+ * it is saved in l_ast_data of the lock. Lock reference is kept too
+ * until that to prevent it from canceling.
+ */
+ if (!is_granted_or_cancelled_nolock(lock)) {
+ mdt_object_get(info->mti_env, mo);
+ lock->l_ast_data = mo;
+ unlock_res_and_lock(lock);
+ } else {
+ unlock_res_and_lock(lock);
+ ldlm_lock_decref_and_cancel(&dom_lh, LCK_PW);
+ }
+ LDLM_LOCK_PUT(lock);
+
+ RETURN_EXIT;
+}