+ struct mdd_device *mdd = (struct mdd_device *)data;
+ struct lu_env *env = NULL;
+ int rc;
+ struct llog_ctxt *ctxt;
+ struct mdd_changelog_gc mcgc = {
+ .mcgc_mdd = mdd,
+ .mcgc_found = false,
+ .mcgc_maxtime = 0,
+ .mcgc_maxindexes = 0,
+ };
+ ENTRY;
+
+ mdd->mdd_cl.mc_gc_task = current;
+
+ CDEBUG(D_HA, "%s: ChangeLog garbage collect thread start with PID %d\n",
+ mdd2obd_dev(mdd)->obd_name, current->pid);
+
+ OBD_ALLOC_PTR(env);
+ if (env == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ rc = lu_env_init(env, LCT_MD_THREAD);
+ if (rc)
+ GOTO(out, rc);
+
+ for (;;) {
+ ctxt = llog_get_context(mdd2obd_dev(mdd),
+ LLOG_CHANGELOG_USER_ORIG_CTXT);
+ if (ctxt == NULL ||
+ (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
+ GOTO(out_ctxt, rc = -ENXIO);
+
+ rc = llog_cat_process(env, ctxt->loc_handle,
+ mdd_changelog_gc_cb, &mcgc, 0, 0);
+ if (rc != 0 || mcgc.mcgc_found == false)
+ break;
+ llog_ctxt_put(ctxt);
+
+ if (mcgc.mcgc_maxindexes != 0)
+ CWARN("%s: Force deregister of ChangeLog user cl%d "
+ "idle with more than %llu unprocessed records\n",
+ mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
+ mcgc.mcgc_maxindexes);
+ else
+ CWARN("%s: Force deregister of ChangeLog user cl%d "
+ "idle since more than %us\n",
+ mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
+ mcgc.mcgc_maxtime);
+
+ mdd_changelog_user_purge(env, mdd, mcgc.mcgc_id);
+
+ if (kthread_should_stop())
+ GOTO(out_env, rc = 0);
+
+ /* try again to search for another candidate */
+ mcgc.mcgc_found = false;
+ mcgc.mcgc_maxtime = 0;
+ mcgc.mcgc_maxindexes = 0;
+ }
+
+out_ctxt:
+ if (ctxt != NULL)
+ llog_ctxt_put(ctxt);
+
+out_env:
+ lu_env_fini(env);
+ GOTO(out, rc);
+out:
+ if (env)
+ OBD_FREE_PTR(env);
+
+ spin_lock(&mdd->mdd_cl.mc_lock);
+ mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
+ spin_unlock(&mdd->mdd_cl.mc_lock);
+
+ return rc;
+}
+
+int mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
+ int result, struct thandle *handle)
+{
+ int rc;
+
+ handle->th_result = result;
+ rc = mdd_child_ops(mdd)->dt_trans_stop(env, mdd->mdd_child, handle);
+ barrier_exit(mdd->mdd_bottom);
+
+ /* bottom half of changelog garbage-collection mechanism, started
+ * from mdd_changelog_store(). This is required, as running a
+ * kthead can't occur during a journal transaction is being filled
+ * because otherwise a deadlock can happen if memory reclaim is
+ * triggered by kthreadd when forking the new thread, and thus
+ * I/Os could be attempted to the same device from shrinkers
+ * requiring a new journal transaction to be started when current
+ * could never complete (LU-10680).
+ */
+ if (unlikely(mdd->mdd_cl.mc_flags & CLM_ON &&
+ cmpxchg(&mdd->mdd_cl.mc_gc_task, MDD_CHLG_GC_NEED,
+ MDD_CHLG_GC_START) == MDD_CHLG_GC_NEED)) {
+ /* XXX we may want to cmpxchg() only if MDD_CHLG_GC_NEED
+ * to save its cost in the frequent case and have an extra
+ * if/test cost in the rare case where we need to spawn?
+ */
+ struct task_struct *gc_task;
+ struct obd_device *obd = mdd2obd_dev(mdd);
+
+ gc_task = kthread_run(mdd_chlg_garbage_collect, mdd,
+ "chlg_gc_thread");
+ if (IS_ERR(gc_task)) {
+ CERROR("%s: cannot start ChangeLog garbage collection "
+ "thread: rc = %ld\n", obd->obd_name,
+ PTR_ERR(gc_task));
+ mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
+ } else {
+ CDEBUG(D_HA, "%s: a ChangeLog garbage collection "
+ "thread has been started\n", obd->obd_name);
+ }
+ }
+
+ /* if operation failed, return \a result, otherwise return status of
+ * dt_trans_stop */
+ return result ?: rc;