+
+void qti_lqes_init(const struct lu_env *env)
+{
+ struct qmt_thread_info *qti = qmt_info(env);
+
+ qti->qti_lqes_cnt = 0;
+ qti->qti_glbl_lqe_idx = 0;
+ qti->qti_lqes_num = QMT_MAX_POOL_NUM;
+}
+
+int qti_lqes_add(const struct lu_env *env, struct lquota_entry *lqe)
+{
+ struct qmt_thread_info *qti = qmt_info(env);
+
+ if (qti->qti_lqes_cnt > qti->qti_lqes_num) {
+ struct lquota_entry **lqes;
+ lqes = qti->qti_lqes;
+ OBD_ALLOC(lqes, sizeof(lqe) * qti->qti_lqes_num * 2);
+ if (!lqes)
+ return -ENOMEM;
+ memcpy(lqes, qti_lqes(env), qti->qti_lqes_cnt * sizeof(lqe));
+ /* Don't need to free, if it is the very 1st allocation */
+ if (qti->qti_lqes_num > QMT_MAX_POOL_NUM)
+ OBD_FREE(qti->qti_lqes,
+ qti->qti_lqes_num * sizeof(lqe));
+ qti->qti_lqes = lqes;
+ qti->qti_lqes_num *= 2;
+ }
+
+ if (lqe->lqe_is_global)
+ qti->qti_glbl_lqe_idx = qti->qti_lqes_cnt;
+ qti_lqes(env)[qti->qti_lqes_cnt++] = lqe;
+
+ /* The pool could be accessed directly from lqe, so take
+ * extra reference that is put in qti_lqes_fini */
+ qpi_getref(lqe2qpi(lqe));
+
+ CDEBUG(D_QUOTA, "LQE %p %lu is added, lqe_cnt %d lqes_num %d\n",
+ lqe, (long unsigned)lqe->lqe_id.qid_uid,
+ qti->qti_lqes_cnt, qti->qti_lqes_num);
+ LASSERT(qti->qti_lqes_num != 0);
+
+ return 0;
+}
+
+void qti_lqes_del(const struct lu_env *env, int index)
+{
+ struct lquota_entry **lqes;
+ int lqes_cnt = qti_lqes_cnt(env);
+ int lqep_size = sizeof(struct lquota_entry *);
+
+ if (index == 0) {
+ /* We can't handle non global lqes correctly without
+ * global lqe located at index 0. If we try to do so,
+ * something goes wrong. */
+ LQUOTA_ERROR(qti_lqes_glbl(env),
+ "quota: cannot remove lqe at index 0 as it is global");
+ LASSERT(qti_lqes_glbl(env)->lqe_is_global);
+ return;
+ }
+ lqes = qti_lqes(env);
+ qpi_putref(env, lqe2qpi(lqes[index]));
+ lqe_putref(lqes[index]);
+ memcpy((unsigned char *)lqes + index * lqep_size,
+ (unsigned char *)lqes + (index + 1) * lqep_size,
+ (lqes_cnt - index - 1) * lqep_size);
+ qti_lqes_cnt(env)--;
+}
+
+void qti_lqes_fini(const struct lu_env *env)
+{
+ struct qmt_thread_info *qti = qmt_info(env);
+ struct lquota_entry **lqes = qti->qti_lqes;
+ int i;
+
+ lqes = qti_lqes(env);
+ for (i = 0; i < qti->qti_lqes_cnt; i++) {
+ qpi_putref(env, lqe2qpi(lqes[i]));
+ lqe_putref(lqes[i]);
+ }
+
+ if (qti->qti_lqes_num > QMT_MAX_POOL_NUM)
+ OBD_FREE(qti->qti_lqes,
+ qti->qti_lqes_num * sizeof(struct lquota_entry *));
+}
+
+inline int qti_lqes_min_qunit(const struct lu_env *env)
+{
+ int i, min, qunit;
+
+ for (i = 1, min = qti_lqe_qunit(env, 0); i < qti_lqes_cnt(env); i++) {
+ qunit = qti_lqe_qunit(env, i);
+ if (qunit < min)
+ min = qunit;
+ }
+
+ return min;
+}
+
+inline int qti_lqes_edquot(const struct lu_env *env)
+{
+ int i;
+
+ for (i = 0; i < qti_lqes_cnt(env); i++) {
+ if (qti_lqes(env)[i]->lqe_edquot)
+ return 1;
+ }
+
+ return 0;
+}
+
+inline int qti_lqes_restore_init(const struct lu_env *env)
+{
+ int rc = 0;
+
+ if (qti_lqes_cnt(env) > QMT_MAX_POOL_NUM) {
+ OBD_ALLOC(qmt_info(env)->qti_lqes_rstr,
+ qti_lqes_cnt(env) * sizeof(struct qmt_lqe_restore));
+ if (!qti_lqes_rstr(env))
+ rc = -ENOMEM;
+ }
+
+ return rc;
+}
+
+inline void qti_lqes_restore_fini(const struct lu_env *env)
+{
+ if (qti_lqes_cnt(env) > QMT_MAX_POOL_NUM)
+ OBD_FREE(qmt_info(env)->qti_lqes_rstr,
+ qti_lqes_cnt(env) * sizeof(struct qmt_lqe_restore));
+}
+
+inline void qti_lqes_write_lock(const struct lu_env *env)
+{
+ int i;
+
+ for (i = 0; i < qti_lqes_cnt(env); i++)
+ lqe_write_lock(qti_lqes(env)[i]);
+}
+
+inline void qti_lqes_write_unlock(const struct lu_env *env)
+{
+ int i;
+
+ for (i = 0; i < qti_lqes_cnt(env); i++)
+ lqe_write_unlock(qti_lqes(env)[i]);
+}
+
+#define QMT_INIT_SLV_CNT 64
+struct lqe_glbl_data *qmt_alloc_lqe_gd(struct qmt_pool_info *pool, int qtype)
+{
+ struct lqe_glbl_data *lgd;
+ struct lqe_glbl_entry *lqeg_arr;
+ int slv_cnt, glbe_num;
+
+ OBD_ALLOC(lgd, sizeof(struct lqe_glbl_data));
+ if (!lgd)
+ RETURN(NULL);
+
+ slv_cnt = qpi_slv_nr_by_rtype(pool, qtype);
+
+ glbe_num = slv_cnt < QMT_INIT_SLV_CNT ? QMT_INIT_SLV_CNT : slv_cnt;
+ OBD_ALLOC(lqeg_arr, sizeof(struct lqe_glbl_entry) * glbe_num);
+ if (!lqeg_arr) {
+ OBD_FREE(lgd, sizeof(struct lqe_glbl_data));
+ RETURN(NULL);
+ }
+
+ CDEBUG(D_QUOTA, "slv_cnt %d glbe_num %d\n", slv_cnt, glbe_num);
+
+ lgd->lqeg_num_used = slv_cnt;
+ lgd->lqeg_num_alloc = glbe_num;
+ lgd->lqeg_arr = lqeg_arr;
+
+ RETURN(lgd);
+}
+
+void qmt_free_lqe_gd(struct lqe_glbl_data *lgd)
+{
+ OBD_FREE(lgd->lqeg_arr,
+ sizeof(struct lqe_glbl_entry) * lgd->lqeg_num_alloc);
+ OBD_FREE(lgd, sizeof(struct lqe_glbl_data));
+}
+
+void qmt_seed_glbe_all(const struct lu_env *env, struct lqe_glbl_data *lgd,
+ bool qunit, bool edquot)
+{
+ struct rw_semaphore *sem = NULL;
+ struct qmt_pool_info *qpi;
+ int i, j, idx;
+ ENTRY;
+
+ /* lqes array is sorted by qunit - the first entry has minimum qunit.
+ * Thus start seeding global qunit's array beginning from the 1st lqe
+ * and appropriate pool. If pools overlapped, slaves from this
+ * overlapping get minimum qunit value.
+ * user1: pool1, pool2, pool_glbl;
+ * pool1: OST1; user1_qunit = 10M;
+ * pool2: OST0, OST1, OST2; user1_qunit = 30M;
+ * pool_glbl: OST0, OST1, OST2, OST3; user1_qunit = 160M;
+ * qunit array after seeding should be:
+ * OST0: 30M; OST1: 10M; OST2: 30M; OST3: 160M; */
+
+ /* edquot resetup algorythm works fine
+ * with not sorted lqes */
+ if (qunit)
+ qmt_lqes_sort(env);
+
+ for (i = 0; i < lgd->lqeg_num_used; i++) {
+ lgd->lqeg_arr[i].lge_qunit_set = 0;
+ lgd->lqeg_arr[i].lge_qunit_nu = 0;
+ lgd->lqeg_arr[i].lge_edquot_nu = 0;
+ }
+
+ for (i = 0; i < qti_lqes_cnt(env); i++) {
+ struct lquota_entry *lqe = qti_lqes(env)[i];
+ int slaves_cnt;
+
+ CDEBUG(D_QUOTA, "lqes_cnt %d, i %d\n", qti_lqes_cnt(env), i);
+ qpi = lqe2qpi(lqe);
+ if (qmt_pool_global(qpi)) {
+ slaves_cnt = qpi_slv_nr_by_rtype(lqe2qpi(lqe),
+ lqe_qtype(lqe));
+ } else {
+ sem = qmt_sarr_rwsem(qpi);
+ down_read(sem);
+ slaves_cnt = qmt_sarr_count(qpi);
+ }
+
+ for (j = 0; j < slaves_cnt; j++) {
+ idx = qmt_sarr_get_idx(qpi, j);
+ LASSERT(idx >= 0);
+
+ if (edquot) {
+ int lge_edquot, new_edquot, edquot_nu;
+
+ lge_edquot = lgd->lqeg_arr[idx].lge_edquot;
+ edquot_nu = lgd->lqeg_arr[idx].lge_edquot_nu;
+ new_edquot = lqe->lqe_edquot;
+
+ if (lge_edquot == new_edquot ||
+ (edquot_nu && lge_edquot == 1))
+ goto qunit_lbl;
+ lgd->lqeg_arr[idx].lge_edquot = new_edquot;
+ /* it is needed for the following case:
+ * initial values for idx i -
+ * lqe_edquot = 1, lqe_edquot_nu == 0;
+ * 1: new_edquot == 0 ->
+ * lqe_edquot = 0, lqe_edquot_nu = 1;
+ * 2: new_edquot == 1 ->
+ * lqe_edquot = 1, lqe_edquot_nu = 0;
+ * At the 2nd iteration lge_edquot comes back
+ * to 1, so no changes and we don't need
+ * to notify slave. */
+ lgd->lqeg_arr[idx].lge_edquot_nu = !edquot_nu;
+ }
+qunit_lbl:
+ if (qunit) {
+ __u64 lge_qunit, new_qunit;
+
+ CDEBUG(D_QUOTA,
+ "idx %d lge_qunit_set %d lge_qunit %llu new_qunit %llu\n",
+ idx, lgd->lqeg_arr[idx].lge_qunit_set,
+ lgd->lqeg_arr[idx].lge_qunit,
+ lqe->lqe_qunit);
+ /* lge for this idx is already set
+ * on previous iteration */
+ if (lgd->lqeg_arr[idx].lge_qunit_set)
+ continue;
+ lge_qunit = lgd->lqeg_arr[idx].lge_qunit;
+ new_qunit = lqe->lqe_qunit;
+ /* qunit could be not set,
+ * so use global lqe's qunit */
+ if (!new_qunit)
+ continue;
+
+ if (lge_qunit != new_qunit)
+ lgd->lqeg_arr[idx].lge_qunit =
+ new_qunit;
+
+ /* TODO: initially slaves notification was done
+ * only for qunit shrinking. Should we always
+ * notify slaves with new qunit ? */
+ if (lge_qunit > new_qunit)
+ lgd->lqeg_arr[idx].lge_qunit_nu = 1;
+ lgd->lqeg_arr[idx].lge_qunit_set = 1;
+ }
+ }
+
+ if (!qmt_pool_global(qpi))
+ up_read(sem);
+ }
+ /* TODO: only for debug purposes - remove it later */
+ for (i = 0; i < lgd->lqeg_num_used; i++)
+ CDEBUG(D_QUOTA,
+ "lgd ost %d, qunit %lu nu %d; edquot %d nu %d\n",
+ i, (long unsigned)lgd->lqeg_arr[i].lge_qunit,
+ lgd->lqeg_arr[i].lge_qunit_nu,
+ lgd->lqeg_arr[i].lge_edquot,
+ lgd->lqeg_arr[i].lge_edquot_nu);
+
+ EXIT;
+}
+
+void qmt_setup_lqe_gd(const struct lu_env *env, struct qmt_device *qmt,
+ struct lquota_entry *lqe, struct lqe_glbl_data *lgd,
+ int pool_type)
+{
+ __u64 qunit;
+ bool edquot;
+ int i;
+
+ qunit = lqe->lqe_qunit;
+ edquot = lqe->lqe_edquot;
+
+ /* Firstly set all elements in array with
+ * qunit and edquot of global pool */
+ for (i = 0; i < lgd->lqeg_num_used; i++) {
+ lgd->lqeg_arr[i].lge_qunit = qunit;
+ lgd->lqeg_arr[i].lge_edquot = edquot;
+ /* It is the very first lvb setup - qunit and other flags
+ * will be sent to slaves during qmt_lvbo_fill. */
+ lgd->lqeg_arr[i].lge_qunit_nu = 0;
+ lgd->lqeg_arr[i].lge_edquot_nu = 0;
+ }
+
+ qmt_pool_lqes_lookup_spec(env, qmt, pool_type,
+ lqe_qtype(lqe), &lqe->lqe_id);
+ qmt_seed_glbe(env, lgd);
+
+ lqe->lqe_glbl_data = lgd;
+ qmt_id_lock_notify(qmt, lqe);
+
+ qti_lqes_fini(env);
+}