+ struct qsd_upd_rec *upd;
+ struct qsd_instance *qsd = qqi->qqi_qsd;
+ __u64 cur_ver;
+ ENTRY;
+
+ CDEBUG(D_QUOTA, "%s: schedule update. global:%s, version:%llu\n",
+ qsd->qsd_svname, global ? "true" : "false", ver);
+
+ upd = qsd_upd_alloc(qqi, lqe, qid, rec, ver, global);
+ if (upd == NULL)
+ RETURN_EXIT;
+
+ /* If we don't want update index version, no need to sort the
+ * records in version order, just schedule the updates instantly. */
+ if (ver == 0) {
+ write_lock(&qsd->qsd_lock);
+ qsd_upd_add(qsd, upd);
+ write_unlock(&qsd->qsd_lock);
+ RETURN_EXIT;
+ }
+
+ write_lock(&qsd->qsd_lock);
+
+ cur_ver = global ? qqi->qqi_glb_ver : qqi->qqi_slv_ver;
+
+ if (ver <= cur_ver) {
+ if (global)
+ /* legitimate race between glimpse AST and
+ * reintegration */
+ CDEBUG(D_QUOTA, "%s: discarding glb update from glimpse"
+ " ver:%llu local ver:%llu\n",
+ qsd->qsd_svname, ver, cur_ver);
+ else
+ CERROR("%s: discard slv update, ver:%llu local ver:"
+ "%llu\n", qsd->qsd_svname, ver, cur_ver);
+ qsd_upd_free(upd);
+ } else if ((ver == cur_ver + 1) && qqi->qqi_glb_uptodate &&
+ qqi->qqi_slv_uptodate) {
+ /* In order update, and reintegration has been done. */
+ qsd_upd_add(qsd, upd);
+ } else {
+ /* Out of order update (the one with smaller version hasn't
+ * reached slave or hasn't been flushed to disk yet), or
+ * the reintegration is in progress. Defer the update. */
+ struct list_head *list = global ? &qqi->qqi_deferred_glb :
+ &qqi->qqi_deferred_slv;
+ qsd_add_deferred(qsd, list, upd);
+ }
+
+ write_unlock(&qsd->qsd_lock);
+
+ EXIT;
+}
+
+static int qsd_process_upd(const struct lu_env *env, struct qsd_upd_rec *upd)
+{
+ struct lquota_entry *lqe = upd->qur_lqe;
+ struct qsd_qtype_info *qqi = upd->qur_qqi;
+ int rc;
+ ENTRY;
+
+ if (lqe == NULL) {
+ lqe = lqe_locate(env, qqi->qqi_site, &upd->qur_qid);
+ if (IS_ERR(lqe))
+ GOTO(out, rc = PTR_ERR(lqe));
+ }
+
+ /* The in-memory lqe update for slave index copy isn't deferred,
+ * we shouldn't touch it here. */
+ if (upd->qur_global) {
+ rc = qsd_update_lqe(env, lqe, upd->qur_global, &upd->qur_rec);
+ if (rc)
+ GOTO(out, rc);
+ /* refresh usage */
+ qsd_refresh_usage(env, lqe);
+
+ spin_lock(&qqi->qqi_qsd->qsd_adjust_lock);
+ lqe->lqe_adjust_time = 0;
+ spin_unlock(&qqi->qqi_qsd->qsd_adjust_lock);
+
+ /* Report usage asynchronously */
+ rc = qsd_adjust(env, lqe);
+ if (rc)
+ LQUOTA_ERROR(lqe, "failed to report usage, rc:%d", rc);
+ }
+
+ rc = qsd_update_index(env, qqi, &upd->qur_qid, upd->qur_global,
+ upd->qur_ver, &upd->qur_rec);
+out:
+ if (upd->qur_global && rc == 0 &&
+ upd->qur_rec.lqr_glb_rec.qbr_softlimit == 0 &&
+ upd->qur_rec.lqr_glb_rec.qbr_hardlimit == 0 &&
+ (LQUOTA_FLAG(upd->qur_rec.lqr_glb_rec.qbr_time) &
+ LQUOTA_FLAG_DEFAULT)) {
+ lqe->lqe_is_default = true;
+ if (qqi->qqi_default_softlimit == 0 &&
+ qqi->qqi_default_hardlimit == 0)
+ lqe->lqe_enforced = false;
+ else
+ lqe->lqe_enforced = true;
+
+ LQUOTA_DEBUG(lqe, "update to use default quota");
+ }
+
+ if (lqe && !IS_ERR(lqe)) {
+ lqe_putref(lqe);
+ upd->qur_lqe = NULL;
+ }
+ RETURN(rc);
+}
+
+void qsd_adjust_schedule(struct lquota_entry *lqe, bool defer, bool cancel)
+{
+ struct qsd_instance *qsd = lqe2qqi(lqe)->qqi_qsd;
+ bool added = false;
+
+ read_lock(&qsd->qsd_lock);
+ if (qsd->qsd_stopping) {
+ read_unlock(&qsd->qsd_lock);
+ return;
+ }
+ read_unlock(&qsd->qsd_lock);
+
+ lqe_getref(lqe);
+ spin_lock(&qsd->qsd_adjust_lock);
+
+ /* the lqe is being queued for the per-ID lock cancel, we should
+ * cancel the lock cancel and re-add it for quota adjust */
+ if (!list_empty(&lqe->lqe_link) &&
+ lqe->lqe_adjust_time == 0) {
+ list_del_init(&lqe->lqe_link);
+ lqe_putref(lqe);
+ }
+
+ if (list_empty(&lqe->lqe_link)) {
+ if (!cancel) {
+ lqe->lqe_adjust_time = ktime_get_seconds();
+ if (defer)
+ lqe->lqe_adjust_time += QSD_WB_INTERVAL;
+ } else {
+ lqe->lqe_adjust_time = 0;
+ }
+
+ /* lqe reference transferred to list */
+ if (defer)
+ list_add_tail(&lqe->lqe_link,
+ &qsd->qsd_adjust_list);
+ else
+ list_add(&lqe->lqe_link, &qsd->qsd_adjust_list);
+ added = true;
+ }
+ spin_unlock(&qsd->qsd_adjust_lock);
+
+ if (added)
+ wake_up(&qsd->qsd_upd_thread.t_ctl_waitq);
+ else
+ lqe_putref(lqe);
+}
+
+/* return true if there is pending writeback records or the pending
+ * adjust requests */
+static bool qsd_job_pending(struct qsd_instance *qsd, struct list_head *upd,
+ bool *uptodate)
+{
+ bool job_pending = false;
+ int qtype;
+
+ LASSERT(list_empty(upd));
+ *uptodate = true;
+
+ spin_lock(&qsd->qsd_adjust_lock);
+ if (!list_empty(&qsd->qsd_adjust_list)) {
+ struct lquota_entry *lqe;
+ lqe = list_entry(qsd->qsd_adjust_list.next,
+ struct lquota_entry, lqe_link);
+ if (ktime_get_seconds() >= lqe->lqe_adjust_time)
+ job_pending = true;
+ }
+ spin_unlock(&qsd->qsd_adjust_lock);
+
+ write_lock(&qsd->qsd_lock);
+ if (!list_empty(&qsd->qsd_upd_list)) {
+ list_splice_init(&qsd->qsd_upd_list, upd);
+ job_pending = true;
+ }
+
+ for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
+ struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
+
+ /* don't bother kicking off reintegration if space accounting
+ * failed to be enabled */
+ if (qqi->qqi_acct_failed)
+ continue;
+
+ if (!qsd_type_enabled(qsd, qtype))
+ continue;
+
+ if ((!qqi->qqi_glb_uptodate || !qqi->qqi_slv_uptodate) &&
+ !qqi->qqi_reint)
+ /* global or slave index not up to date and reint
+ * thread not running */
+ *uptodate = false;
+ }
+
+ write_unlock(&qsd->qsd_lock);
+ return job_pending;
+}
+
+static int qsd_upd_thread(void *arg)
+{
+ struct qsd_instance *qsd = (struct qsd_instance *)arg;
+ struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
+ LIST_HEAD(queue);
+ struct qsd_upd_rec *upd, *n;
+ struct lu_env *env;
+ int qtype, rc = 0;
+ bool uptodate;
+ struct lquota_entry *lqe;
+ time64_t cur_time;
+ ENTRY;
+
+ OBD_ALLOC_PTR(env);
+ if (env == NULL)
+ RETURN(-ENOMEM);
+
+ rc = lu_env_init(env, LCT_DT_THREAD);
+ if (rc) {
+ CERROR("%s: cannot init env: rc = %d\n", qsd->qsd_svname, rc);
+ OBD_FREE_PTR(env);
+ RETURN(rc);
+ }
+
+ thread_set_flags(thread, SVC_RUNNING);
+ wake_up(&thread->t_ctl_waitq);
+
+ while (1) {
+ wait_event_idle_timeout(
+ thread->t_ctl_waitq,
+ qsd_job_pending(qsd, &queue, &uptodate) ||
+ !thread_is_running(thread),
+ cfs_time_seconds(QSD_WB_INTERVAL));
+
+ list_for_each_entry_safe(upd, n, &queue, qur_link) {
+ list_del_init(&upd->qur_link);
+ qsd_process_upd(env, upd);
+ qsd_upd_free(upd);
+ }
+
+ spin_lock(&qsd->qsd_adjust_lock);
+ cur_time = ktime_get_seconds();
+ while (!list_empty(&qsd->qsd_adjust_list)) {
+ lqe = list_entry(qsd->qsd_adjust_list.next,
+ struct lquota_entry, lqe_link);
+ /* deferred items are sorted by time */
+ if (lqe->lqe_adjust_time > cur_time)
+ break;
+
+ list_del_init(&lqe->lqe_link);
+ spin_unlock(&qsd->qsd_adjust_lock);
+
+ if (thread_is_running(thread) && uptodate) {
+ qsd_refresh_usage(env, lqe);
+ if (lqe->lqe_adjust_time == 0)
+ qsd_id_lock_cancel(env, lqe);
+ else
+ qsd_adjust(env, lqe);
+ }
+
+ lqe_putref(lqe);
+ spin_lock(&qsd->qsd_adjust_lock);
+ }
+ spin_unlock(&qsd->qsd_adjust_lock);
+
+ if (!thread_is_running(thread))
+ break;
+
+ if (uptodate)
+ continue;
+
+ for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++)
+ qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
+ }
+ lu_env_fini(env);
+ OBD_FREE_PTR(env);
+ thread_set_flags(thread, SVC_STOPPED);
+ wake_up(&thread->t_ctl_waitq);
+ RETURN(rc);
+}
+
+int qsd_start_upd_thread(struct qsd_instance *qsd)
+{
+ struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
+ struct task_struct *task;
+ ENTRY;
+
+ task = kthread_run(qsd_upd_thread, (void *)qsd,
+ "lquota_wb_%s", qsd->qsd_svname);
+ if (IS_ERR(task)) {
+ CERROR("fail to start quota update thread: rc = %ld\n",
+ PTR_ERR(task));
+ thread_set_flags(thread, SVC_STOPPED);
+ RETURN(PTR_ERR(task));
+ }
+
+ wait_event_idle(thread->t_ctl_waitq,
+ thread_is_running(thread) || thread_is_stopped(thread));
+ RETURN(0);
+}
+
+static void qsd_cleanup_deferred(struct qsd_instance *qsd)
+{
+ int qtype;
+
+ for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
+ struct qsd_upd_rec *upd, *tmp;
+ struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
+
+ if (qqi == NULL)
+ continue;
+
+ write_lock(&qsd->qsd_lock);
+ list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_glb,
+ qur_link) {
+ CWARN("%s: Free global deferred upd: ID:%llu, "
+ "ver:%llu/%llu\n", qsd->qsd_svname,
+ upd->qur_qid.qid_uid, upd->qur_ver,
+ qqi->qqi_glb_ver);
+ list_del_init(&upd->qur_link);
+ qsd_upd_free(upd);
+ }
+ list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_slv,
+ qur_link) {
+ CWARN("%s: Free slave deferred upd: ID:%llu, "
+ "ver:%llu/%llu\n", qsd->qsd_svname,
+ upd->qur_qid.qid_uid, upd->qur_ver,
+ qqi->qqi_slv_ver);
+ list_del_init(&upd->qur_link);
+ qsd_upd_free(upd);
+ }
+ write_unlock(&qsd->qsd_lock);
+ }
+}
+
+static void qsd_cleanup_adjust(struct qsd_instance *qsd)
+{
+ struct lquota_entry *lqe;
+
+ spin_lock(&qsd->qsd_adjust_lock);
+ while (!list_empty(&qsd->qsd_adjust_list)) {
+ lqe = list_entry(qsd->qsd_adjust_list.next,
+ struct lquota_entry, lqe_link);
+ list_del_init(&lqe->lqe_link);
+ lqe_putref(lqe);
+ }
+ spin_unlock(&qsd->qsd_adjust_lock);
+}
+
+void qsd_stop_upd_thread(struct qsd_instance *qsd)
+{
+ struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
+
+ if (!thread_is_stopped(thread)) {
+ thread_set_flags(thread, SVC_STOPPING);
+ wake_up(&thread->t_ctl_waitq);
+
+ wait_event_idle(thread->t_ctl_waitq, thread_is_stopped(thread));
+ }
+ qsd_cleanup_deferred(qsd);
+ qsd_cleanup_adjust(qsd);