+ * Init sub llog context
+ *
+ * Setup update llog ctxt for update recovery threads, then start the
+ * recovery thread (lod_sub_recovery_thread) to read update llog from
+ * the correspondent MDT to do update recovery.
+ *
+ * \param[in] env execution environment
+ * \param[in] lod lod device to do update recovery
+ * \param[in] dt sub dt device for which the recovery thread is
+ *
+ * \retval 0 if initialization succeeds.
+ * \retval negative errno if initialization fails.
+ */
+int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
+ struct dt_device *dt)
+{
+ struct obd_device *obd;
+ struct lod_recovery_data *lrd = NULL;
+ struct ptlrpc_thread *thread;
+ struct task_struct *task;
+ struct l_wait_info lwi = { 0 };
+ struct lod_tgt_desc *sub_ltd = NULL;
+ __u32 index;
+ __u32 master_index;
+ int rc;
+ ENTRY;
+
+ rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
+ if (rc != 0)
+ RETURN(rc);
+
+ OBD_ALLOC_PTR(lrd);
+ if (lrd == NULL)
+ RETURN(-ENOMEM);
+
+ if (lod->lod_child == dt) {
+ thread = &lod->lod_child_recovery_thread;
+ index = master_index;
+ } else {
+ struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
+ struct lod_tgt_desc *tgt = NULL;
+ unsigned int i;
+
+ cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
+ tgt = LTD_TGT(ltd, i);
+ if (tgt->ltd_tgt == dt) {
+ index = tgt->ltd_index;
+ sub_ltd = tgt;
+ break;
+ }
+ }
+ LASSERT(sub_ltd != NULL);
+ OBD_ALLOC_PTR(sub_ltd->ltd_recovery_thread);
+ if (sub_ltd->ltd_recovery_thread == NULL)
+ GOTO(free_lrd, rc = -ENOMEM);
+
+ thread = sub_ltd->ltd_recovery_thread;
+ }
+
+ CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
+ dt->dd_lu_dev.ld_obd->obd_name);
+ lrd->lrd_lod = lod;
+ lrd->lrd_ltd = sub_ltd;
+ lrd->lrd_thread = thread;
+ lrd->lrd_idx = index;
+ init_waitqueue_head(&thread->t_ctl_waitq);
+
+ obd = dt->dd_lu_dev.ld_obd;
+ obd->obd_lvfs_ctxt.dt = dt;
+ rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
+ NULL, &llog_common_cat_ops);
+ if (rc < 0) {
+ CERROR("%s: cannot setup updatelog llog: rc = %d\n",
+ obd->obd_name, rc);
+ GOTO(free_thread, rc);
+ }
+
+ /* Start the recovery thread */
+ task = kthread_run(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
+ master_index, index);
+ if (IS_ERR(task)) {
+ rc = PTR_ERR(task);
+ CERROR("%s: cannot start recovery thread: rc = %d\n",
+ obd->obd_name, rc);
+ GOTO(out_llog, rc);
+ }
+
+ l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
+ thread->t_flags & SVC_STOPPED, &lwi);
+
+ RETURN(0);
+out_llog:
+ lod_sub_fini_llog(env, dt, thread);
+free_thread:
+ if (lod->lod_child != dt) {
+ OBD_FREE_PTR(sub_ltd->ltd_recovery_thread);
+ sub_ltd->ltd_recovery_thread = NULL;
+ }
+free_lrd:
+ OBD_FREE_PTR(lrd);
+ RETURN(rc);
+}
+
+/**
+ * Stop sub recovery thread
+ *
+ * Stop sub recovery thread on all subs.
+ *
+ * \param[in] env execution environment
+ * \param[in] lod lod device to do update recovery
+ */
+static void lod_sub_stop_recovery_threads(const struct lu_env *env,
+ struct lod_device *lod)
+{
+ struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
+ struct ptlrpc_thread *thread;
+ unsigned int i;
+
+ /* Stop the update log commit cancel threads and finish master
+ * llog ctxt */
+ thread = &lod->lod_child_recovery_thread;
+ /* Stop recovery thread first */
+ if (thread != NULL && thread->t_flags & SVC_RUNNING) {
+ thread->t_flags = SVC_STOPPING;
+ wake_up(&thread->t_ctl_waitq);
+ wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
+ }
+
+ lod_getref(ltd);
+ cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
+ struct lod_tgt_desc *tgt;
+
+ tgt = LTD_TGT(ltd, i);
+ thread = tgt->ltd_recovery_thread;
+ if (thread != NULL && thread->t_flags & SVC_RUNNING) {
+ thread->t_flags = SVC_STOPPING;
+ wake_up(&thread->t_ctl_waitq);
+ wait_event(thread->t_ctl_waitq,
+ thread->t_flags & SVC_STOPPED);
+ OBD_FREE_PTR(tgt->ltd_recovery_thread);
+ tgt->ltd_recovery_thread = NULL;
+ }
+ }
+
+ lod_putref(lod, ltd);
+}
+
+/**
+ * finish all sub llog
+ *
+ * cleanup all of sub llog ctxt on the LOD.
+ *
+ * \param[in] env execution environment
+ * \param[in] lod lod device to do update recovery
+ */
+static void lod_sub_fini_all_llogs(const struct lu_env *env,
+ struct lod_device *lod)
+{
+ struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
+ unsigned int i;
+
+ /* Stop the update log commit cancel threads and finish master
+ * llog ctxt */
+ lod_sub_fini_llog(env, lod->lod_child,
+ &lod->lod_child_recovery_thread);
+ lod_getref(ltd);
+ cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
+ struct lod_tgt_desc *tgt;
+
+ tgt = LTD_TGT(ltd, i);
+ lod_sub_fini_llog(env, tgt->ltd_tgt,
+ tgt->ltd_recovery_thread);
+ }
+
+ lod_putref(lod, ltd);
+}
+
+static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
+{
+ struct lod_device *lod = (struct lod_device *)data;
+ struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
+ struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
+ struct lod_tgt_desc *tgt = NULL;
+ char *buf;
+ int len = 0;
+ int rc;
+ int i;
+
+ *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
+ if (*count == 0) {
+ *size = 0;
+ return NULL;
+ }
+
+ *size = 5 * *count + 1;
+ OBD_ALLOC(buf, *size);
+ if (buf == NULL)
+ return NULL;
+
+ *count = 0;
+ memset(buf, 0, *size);
+
+ if (!lod->lod_child_got_update_log) {
+ rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
+ LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
+
+ rc = snprintf(buf + len, *size - len, " %04x", i);
+ LASSERT(rc > 0);
+
+ len += rc;
+ (*count)++;
+ }
+
+ cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
+ tgt = LTD_TGT(ltd, i);
+ if (!tgt->ltd_got_update_log) {
+ rc = snprintf(buf + len, *size - len, " %04x", i);
+ if (unlikely(rc <= 0))
+ break;
+
+ len += rc;
+ (*count)++;
+ }
+ }
+
+ return buf;
+}
+
+/**
+ * Prepare distribute txn
+ *
+ * Prepare distribute txn structure for LOD
+ *
+ * \param[in] env execution environment
+ * \param[in] lod_device LOD device
+ *
+ * \retval 0 if preparation succeeds.
+ * \retval negative errno if preparation fails.
+ */
+static int lod_prepare_distribute_txn(const struct lu_env *env,
+ struct lod_device *lod)
+{
+ struct target_distribute_txn_data *tdtd;
+ struct lu_target *lut;
+ int rc;
+ ENTRY;
+
+ /* Init update recovery data */
+ OBD_ALLOC_PTR(tdtd);
+ if (tdtd == NULL)
+ RETURN(-ENOMEM);
+
+ lut = lod2lu_dev(lod)->ld_site->ls_tgt;
+ tdtd->tdtd_dt = &lod->lod_dt_dev;
+ rc = distribute_txn_init(env, lut, tdtd,
+ lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
+
+ if (rc < 0) {
+ CERROR("%s: cannot init distribute txn: rc = %d\n",
+ lod2obd(lod)->obd_name, rc);
+ OBD_FREE_PTR(tdtd);
+ RETURN(rc);
+ }
+
+ tdtd->tdtd_show_update_logs_retrievers =
+ lod_show_update_logs_retrievers;
+ tdtd->tdtd_show_retrievers_cbdata = lod;
+
+ lut->lut_tdtd = tdtd;
+
+ RETURN(0);
+}
+
+/**
+ * Finish distribute txn
+ *
+ * Release the resource holding by distribute txn, i.e. stop distribute
+ * txn thread.
+ *
+ * \param[in] env execution environment
+ * \param[in] lod lod device
+ */
+static void lod_fini_distribute_txn(const struct lu_env *env,
+ struct lod_device *lod)
+{
+ struct lu_target *lut;
+
+ lut = lod2lu_dev(lod)->ld_site->ls_tgt;
+ target_recovery_fini(lut->lut_obd);
+ if (lut->lut_tdtd == NULL)
+ return;
+
+ distribute_txn_fini(env, lut->lut_tdtd);
+
+ OBD_FREE_PTR(lut->lut_tdtd);
+ lut->lut_tdtd = NULL;
+}
+
+/**