X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Fquota%2Fqmt_entry.c;h=92d83b69bf40b986132caf79d8f8ce79d7c49ca4;hp=c6bd58faacb6e5d8b9cb5752c855f26905241024;hb=d4978678b49102226a79a6c8e5d10075d416977d;hpb=530881fe4ee20b56b0426a5c4d3a2b5226768d06 diff --git a/lustre/quota/qmt_entry.c b/lustre/quota/qmt_entry.c index c6bd58f..92d83b6 100644 --- a/lustre/quota/qmt_entry.c +++ b/lustre/quota/qmt_entry.c @@ -44,6 +44,7 @@ static void qmt_lqe_init(struct lquota_entry *lqe, void *arg) lqe->lqe_revoke_time = 0; init_rwsem(&lqe->lqe_sem); + mutex_init(&lqe->lqe_glbl_data_lock); } /* Apply the default quota setting to the specified quota entry @@ -68,7 +69,7 @@ int qmt_lqe_set_default(const struct lu_env *env, struct qmt_pool_info *pool, if (lqe->lqe_id.qid_uid == 0) RETURN(0); - lqe_def = pool->qpi_grace_lqe[lqe->lqe_site->lqs_qtype]; + lqe_def = pool->qpi_grace_lqe[lqe_qtype(lqe)]; LQUOTA_DEBUG(lqe, "inherit default quota"); @@ -102,9 +103,10 @@ int qmt_lqe_set_default(const struct lu_env *env, struct qmt_pool_info *pool, * \param env - the environment passed by the caller * \param lqe - is the quota entry to refresh * \param arg - is the pointer to the qmt_pool_info structure + * \param find - don't create lqe on disk in case of ENOENT if true */ static int qmt_lqe_read(const struct lu_env *env, struct lquota_entry *lqe, - void *arg) + void *arg, bool find) { struct qmt_thread_info *qti = qmt_info(env); struct qmt_pool_info *pool = (struct qmt_pool_info *)arg; @@ -119,6 +121,8 @@ static int qmt_lqe_read(const struct lu_env *env, struct lquota_entry *lqe, switch (rc) { case -ENOENT: + if (find) + RETURN(-ENOENT); qmt_lqe_set_default(env, pool, lqe, true); break; case 0: @@ -146,6 +150,9 @@ static int qmt_lqe_read(const struct lu_env *env, struct lquota_entry *lqe, else lqe->lqe_enforced = true; + if (qmt_pool_global(pool)) + lqe->lqe_is_global = 1; + LQUOTA_DEBUG(lqe, "read"); RETURN(0); } @@ -160,29 +167,27 @@ static int qmt_lqe_read(const struct lu_env *env, struct lquota_entry *lqe, */ static void qmt_lqe_debug(struct lquota_entry *lqe, void *arg, struct libcfs_debug_msg_data *msgdata, - const char *fmt, va_list args) + struct va_format *vaf) { struct qmt_pool_info *pool = (struct qmt_pool_info *)arg; - libcfs_debug_vmsg2(msgdata, fmt, args, - "qmt:%s pool:%d-%s id:%llu enforced:%d hard:%llu" - " soft:%llu granted:%llu time:%llu qunit: %llu" - " edquot:%d may_rel:%llu revoke:%lld default:%s\n", - pool->qpi_qmt->qmt_svname, - pool->qpi_key & 0x0000ffff, - RES_NAME(pool->qpi_key >> 16), - lqe->lqe_id.qid_uid, lqe->lqe_enforced, - lqe->lqe_hardlimit, lqe->lqe_softlimit, - lqe->lqe_granted, lqe->lqe_gracetime, - lqe->lqe_qunit, lqe->lqe_edquot, lqe->lqe_may_rel, - lqe->lqe_revoke_time, - lqe->lqe_is_default ? "yes" : "no"); + libcfs_debug_msg(msgdata, + "%pV qmt:%s pool:%s-%s id:%llu enforced:%d hard:%llu soft:%llu granted:%llu time:%llu qunit: %llu edquot:%d may_rel:%llu revoke:%lld default:%s\n", + vaf, pool->qpi_qmt->qmt_svname, + RES_NAME(pool->qpi_rtype), + pool->qpi_name, + lqe->lqe_id.qid_uid, lqe->lqe_enforced, + lqe->lqe_hardlimit, lqe->lqe_softlimit, + lqe->lqe_granted, lqe->lqe_gracetime, + lqe->lqe_qunit, lqe->lqe_edquot, lqe->lqe_may_rel, + lqe->lqe_revoke_time, + lqe->lqe_is_default ? "yes" : "no"); } /* * Vector of quota entry operations supported on the master */ -struct lquota_entry_operations qmt_lqe_ops = { +const struct lquota_entry_operations qmt_lqe_ops = { .lqe_init = qmt_lqe_init, .lqe_read = qmt_lqe_read, .lqe_debug = qmt_lqe_debug, @@ -194,28 +199,37 @@ struct lquota_entry_operations qmt_lqe_ops = { * * \param env - is the environment passed by the caller * \param lqe - is the quota entry associated with the identifier - * subject to the change + * subject to the change. If it is NULL lqes array is + * taken from env with qti_lqes_env(env). * \param slv_obj - is the dt_object associated with the index file - * \param restore - is a temporary storage for current quota settings which will - * be restored if something goes wrong at index update time. + * \param sync - make transaction sync if true */ struct thandle *qmt_trans_start_with_slv(const struct lu_env *env, struct lquota_entry *lqe, struct dt_object *slv_obj, - struct qmt_lqe_restore *restore) + bool sync) { struct qmt_device *qmt; struct thandle *th; - int rc; + struct lquota_entry **lqes; + struct qmt_lqe_restore *restore; + int rc, i, lqes_cnt; ENTRY; - LASSERT(lqe != NULL); - LASSERT(lqe_is_master(lqe)); + restore = qti_lqes_rstr(env); + if (!lqe) { + lqes_cnt = qti_lqes_cnt(env); + lqes = qti_lqes(env); + } else { + lqes_cnt = 1; + lqes = &lqe; + } - qmt = lqe2qpi(lqe)->qpi_qmt; + /* qmt is the same for all lqes, so take it from the 1st */ + qmt = lqe2qpi(lqes[0])->qpi_qmt; if (slv_obj != NULL) - LQUOTA_DEBUG(lqe, "declare write for slv "DFID, + LQUOTA_DEBUG(lqes[0], "declare write for slv "DFID, PFID(lu_object_fid(&slv_obj->do_lu))); /* start transaction */ @@ -223,19 +237,24 @@ struct thandle *qmt_trans_start_with_slv(const struct lu_env *env, if (IS_ERR(th)) RETURN(th); - if (slv_obj == NULL) + if (sync) /* quota settings on master are updated synchronously for the * time being */ th->th_sync = 1; /* reserve credits for global index update */ - rc = lquota_disk_declare_write(env, th, LQE_GLB_OBJ(lqe), &lqe->lqe_id); - if (rc) - GOTO(out, rc); + for (i = 0; i < lqes_cnt; i++) { + rc = lquota_disk_declare_write(env, th, + LQE_GLB_OBJ(lqes[i]), + &lqes[i]->lqe_id); + if (rc) + GOTO(out, rc); + } if (slv_obj != NULL) { /* reserve credits for slave index update */ - rc = lquota_disk_declare_write(env, th, slv_obj, &lqe->lqe_id); + rc = lquota_disk_declare_write(env, th, slv_obj, + &lqes[0]->lqe_id); if (rc) GOTO(out, rc); } @@ -250,15 +269,17 @@ out: if (rc) { dt_trans_stop(env, qmt->qmt_child, th); th = ERR_PTR(rc); - LQUOTA_ERROR(lqe, "failed to slv declare write for "DFID + LQUOTA_ERROR(lqes[0], "failed to slv declare write for "DFID ", rc:%d", PFID(lu_object_fid(&slv_obj->do_lu)), rc); } else { - restore->qlr_hardlimit = lqe->lqe_hardlimit; - restore->qlr_softlimit = lqe->lqe_softlimit; - restore->qlr_gracetime = lqe->lqe_gracetime; - restore->qlr_granted = lqe->lqe_granted; - restore->qlr_qunit = lqe->lqe_qunit; + for (i = 0; i < lqes_cnt; i++) { + restore[i].qlr_hardlimit = lqes[i]->lqe_hardlimit; + restore[i].qlr_softlimit = lqes[i]->lqe_softlimit; + restore[i].qlr_gracetime = lqes[i]->lqe_gracetime; + restore[i].qlr_granted = lqes[i]->lqe_granted; + restore[i].qlr_qunit = lqes[i]->lqe_qunit; + } } return th; } @@ -272,11 +293,23 @@ out: * be restored if something goes wrong at index update time. */ struct thandle *qmt_trans_start(const struct lu_env *env, - struct lquota_entry *lqe, - struct qmt_lqe_restore *restore) + struct lquota_entry *lqe) { LQUOTA_DEBUG(lqe, "declare write"); - return qmt_trans_start_with_slv(env, lqe, NULL, restore); + return qmt_trans_start_with_slv(env, lqe, NULL, true); +} + +int qmt_glb_write_lqes(const struct lu_env *env, struct thandle *th, + __u32 flags, __u64 *ver) +{ + int i, rc = 0; + + for (i = 0; i < qti_lqes_cnt(env); i++) { + rc = qmt_glb_write(env, th, qti_lqes(env)[i], flags, ver); + if (rc) + break; + } + return rc; } /* @@ -321,7 +354,8 @@ int qmt_glb_write(const struct lu_env *env, struct thandle *th, if (lqe->lqe_is_default) { rec->qbr_hardlimit = 0; rec->qbr_softlimit = 0; - rec->qbr_time = LQUOTA_GRACE_FLAG(0, LQUOTA_FLAG_DEFAULT); + rec->qbr_time = LQUOTA_GRACE_FLAG(lqe->lqe_gracetime, + LQUOTA_FLAG_DEFAULT); } else { rec->qbr_hardlimit = lqe->lqe_hardlimit; rec->qbr_softlimit = lqe->lqe_softlimit; @@ -347,7 +381,7 @@ int qmt_glb_write(const struct lu_env *env, struct thandle *th, * The entry must be at least read locked. * * \param env - the environment passed by the caller - * \param lqe - is the quota entry associated with the identifier to look-up + * \param lqe_id - is the quota id associated with the identifier to look-up * in the slave index * \param slv_obj - is the dt_object associated with the slave index * \param granted - is the output parameter where to return how much space @@ -355,7 +389,7 @@ int qmt_glb_write(const struct lu_env *env, struct thandle *th, * * \retval - 0 on success, appropriate error on failure */ -int qmt_slv_read(const struct lu_env *env, struct lquota_entry *lqe, +int qmt_slv_read(const struct lu_env *env, union lquota_id *qid, struct dt_object *slv_obj, __u64 *granted) { struct qmt_thread_info *qti = qmt_info(env); @@ -363,15 +397,11 @@ int qmt_slv_read(const struct lu_env *env, struct lquota_entry *lqe, int rc; ENTRY; - LASSERT(lqe != NULL); - LASSERT(lqe_is_master(lqe)); - LASSERT(lqe_is_locked(lqe)); - - LQUOTA_DEBUG(lqe, "read slv "DFID, - PFID(lu_object_fid(&slv_obj->do_lu))); + CDEBUG(D_QUOTA, "read id:%llu form slv "DFID"\n", + qid->qid_uid, PFID(lu_object_fid(&slv_obj->do_lu))); /* read slave record from disk */ - rc = lquota_disk_read(env, slv_obj, &lqe->lqe_id, + rc = lquota_disk_read(env, slv_obj, qid, (struct dt_rec *)slv_rec); switch (rc) { case -ENOENT: @@ -382,12 +412,12 @@ int qmt_slv_read(const struct lu_env *env, struct lquota_entry *lqe, *granted = slv_rec->qsr_granted; break; default: - LQUOTA_ERROR(lqe, "failed to read slave record "DFID, - PFID(lu_object_fid(&slv_obj->do_lu))); + CERROR("Failed to read slave record for %llu from "DFID"\n", + qid->qid_uid, PFID(lu_object_fid(&slv_obj->do_lu))); RETURN(rc); } - LQUOTA_DEBUG(lqe, "successful slv read %llu", *granted); + CDEBUG(D_QUOTA, "Successful slv read %llu\n", *granted); RETURN(0); } @@ -434,8 +464,9 @@ int qmt_slv_write(const struct lu_env *env, struct thandle *th, rc = lquota_disk_write(env, th, slv_obj, &lqe->lqe_id, (struct dt_rec *)rec, flags, ver); if (rc) { - LQUOTA_ERROR(lqe, "failed to update slave index "DFID" granted:" - "%llu", PFID(lu_object_fid(&slv_obj->do_lu)), + LQUOTA_ERROR(lqe, + "failed to update slave index "DFID" granted:%llu", + PFID(lu_object_fid(&slv_obj->do_lu)), granted); RETURN(rc); } @@ -467,13 +498,13 @@ int qmt_validate_limits(struct lquota_entry *lqe, __u64 hard, __u64 soft) * \param lqe - is the quota entry to check * \param now - is the current time in second used for grace time managment */ -void qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now) +bool qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now) { struct qmt_pool_info *pool = lqe2qpi(lqe); ENTRY; if (!lqe->lqe_enforced || lqe->lqe_id.qid_uid == 0) - RETURN_EXIT; + RETURN(false); if (!lqe->lqe_edquot) { /* space exhausted flag not set, let's check whether it is time @@ -481,30 +512,30 @@ void qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now) if (!qmt_space_exhausted(lqe, now)) /* the qmt still has available space */ - RETURN_EXIT; + RETURN(false); /* See comment in qmt_adjust_qunit(). LU-4139 */ if (qmt_hard_exhausted(lqe) || - pool->qpi_key >> 16 != LQUOTA_RES_DT) { + pool->qpi_rtype != LQUOTA_RES_DT) { time64_t lapse; /* we haven't reached the minimal qunit yet so there is * still hope that the rebalancing process might free * up some quota space */ if (lqe->lqe_qunit != pool->qpi_least_qunit) - RETURN_EXIT; + RETURN(false); /* least qunit value not sent to all slaves yet */ if (lqe->lqe_revoke_time == 0) - RETURN_EXIT; + RETURN(false); /* Let's give more time to slave to release space */ lapse = ktime_get_seconds() - QMT_REBA_TIMEOUT; if (lqe->lqe_may_rel != 0 && lqe->lqe_revoke_time > lapse) - RETURN_EXIT; + RETURN(false); } else { if (lqe->lqe_qunit > pool->qpi_soft_least_qunit) - RETURN_EXIT; + RETURN(false); } /* set edquot flag */ @@ -515,14 +546,14 @@ void qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now) if (qmt_space_exhausted(lqe, now)) /* the qmt still has not space */ - RETURN_EXIT; + RETURN(false); if (lqe->lqe_hardlimit != 0 && lqe->lqe_granted + pool->qpi_least_qunit > lqe->lqe_hardlimit) /* we clear the flag only once at least one least qunit * is available */ - RETURN_EXIT; + RETURN(false); /* clear edquot flag */ lqe->lqe_edquot = false; @@ -532,8 +563,7 @@ void qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now) /* let's notify slave by issuing glimpse on per-ID lock. * the rebalance thread will take care of this */ - qmt_id_lock_notify(pool->qpi_qmt, lqe); - EXIT; + RETURN(true); } /* Using least_qunit when over block softlimit will seriously impact the @@ -545,7 +575,7 @@ static __u64 qmt_calc_softlimit(struct lquota_entry *lqe, bool *oversoft) LASSERT(lqe->lqe_softlimit != 0); *oversoft = false; /* No need to do special tweaking for inode limit */ - if (pool->qpi_key >> 16 != LQUOTA_RES_DT) + if (pool->qpi_rtype != LQUOTA_RES_DT) return lqe->lqe_softlimit; if (lqe->lqe_granted <= lqe->lqe_softlimit + @@ -579,8 +609,8 @@ __u64 qmt_alloc_expand(struct lquota_entry *lqe, __u64 granted, __u64 spare) LASSERT(lqe->lqe_enforced && lqe->lqe_qunit != 0); - slv_cnt = lqe2qpi(lqe)->qpi_slv_nr[lqe->lqe_site->lqs_qtype]; - qunit = lqe->lqe_qunit; + slv_cnt = qpi_slv_nr(lqe2qpi(lqe), lqe_qtype(lqe)); + qunit = lqe->lqe_qunit; /* See comment in qmt_adjust_qunit(). LU-4139. */ if (lqe->lqe_softlimit != 0) { @@ -623,16 +653,45 @@ __u64 qmt_alloc_expand(struct lquota_entry *lqe, __u64 granted, __u64 spare) RETURN(0); } +static inline void +qmt_adjust_qunit_set_revoke(const struct lu_env *env, struct lquota_entry *lqe, + unsigned long least_qunit) +{ + struct lquota_entry *lqe2; + time64_t min = 0; + int i; + + if (qti_lqes_cnt(env) <= 1) + return; + + for (i = 0; i < qti_lqes_cnt(env); i++) { + lqe2 = qti_lqes(env)[i]; + if ((lqe2->lqe_qunit == least_qunit) && lqe2->lqe_revoke_time) { + if (!min) { + min = lqe2->lqe_revoke_time; + continue; + } + min = lqe2->lqe_revoke_time < min ? + lqe2->lqe_revoke_time : min; + } + } + + lqe->lqe_revoke_time = min; +} + + /* * Adjust qunit size according to quota limits and total granted count. * The caller must have locked the lqe. * * \param env - the environment passed by the caller * \param lqe - is the qid entry to be adjusted + * \retval true - need reseed glbe array */ -void qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe) +bool qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe) { struct qmt_pool_info *pool = lqe2qpi(lqe); + bool need_reseed = false; int slv_cnt; __u64 qunit, limit, qunit2 = 0; ENTRY; @@ -641,13 +700,20 @@ void qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe) if (!lqe->lqe_enforced || lqe->lqe_id.qid_uid == 0) /* no quota limits */ - RETURN_EXIT; + RETURN(need_reseed); /* record how many slaves have already registered */ - slv_cnt = pool->qpi_slv_nr[lqe->lqe_site->lqs_qtype]; - if (slv_cnt == 0) + slv_cnt = qpi_slv_nr(pool, lqe_qtype(lqe)); + if (slv_cnt == 0) { + /* Pool hasn't slaves anymore. Qunit will be adjusted + * again when new slaves would be added. */ + if (lqe->lqe_qunit) { + qunit = 0; + GOTO(done, qunit); + } /* wait for at least one slave to join */ - RETURN_EXIT; + RETURN(need_reseed); + } /* Qunit calculation is based on soft limit, if any, hard limit * otherwise. This means that qunit is shrunk to the minimum when @@ -668,7 +734,7 @@ void qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe) } else { LQUOTA_ERROR(lqe, "enforced bit set, but neither hard nor soft " "limit are set"); - RETURN_EXIT; + RETURN(need_reseed); } qunit = lqe->lqe_qunit == 0 ? pool->qpi_least_qunit : lqe->lqe_qunit; @@ -718,7 +784,7 @@ void qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe) done: if (lqe->lqe_qunit == qunit) /* keep current qunit */ - RETURN_EXIT; + RETURN(need_reseed); LQUOTA_DEBUG(lqe, "%s qunit to %llu", lqe->lqe_qunit < qunit ? "increasing" : "decreasing", @@ -727,29 +793,456 @@ done: /* store new qunit value */ swap(lqe->lqe_qunit, qunit); + /* reseed glbe array and notify + * slave if qunit was shrinked */ + need_reseed = true; /* reset revoke time */ lqe->lqe_revoke_time = 0; - if (lqe->lqe_qunit < qunit) - /* let's notify slave of qunit shrinking */ - qmt_id_lock_notify(pool->qpi_qmt, lqe); - else if (lqe->lqe_qunit == pool->qpi_least_qunit) - /* initial qunit value is the smallest one */ - lqe->lqe_revoke_time = ktime_get_seconds(); - EXIT; + if (lqe->lqe_qunit == pool->qpi_least_qunit) { + if (lqe->lqe_qunit >= qunit) + /* initial qunit value is the smallest one */ + lqe->lqe_revoke_time = ktime_get_seconds(); + /* If there are several lqes and lqe_revoke_time is set for + * some of them, it means appropriate OSTs have been already + * notified with the least qunit and there is no chance to + * free more space. Find an lqe with the minimum(earliest) + * revoke_time and set this time to the current one. + */ + qmt_adjust_qunit_set_revoke(env, lqe, pool->qpi_least_qunit); + } + RETURN(need_reseed); } +bool qmt_adjust_edquot_qunit_notify(const struct lu_env *env, + struct qmt_device *qmt, + __u64 now, bool edquot, + bool qunit, __u32 qb_flags) +{ + struct lquota_entry *lqe_gl, *lqe; + bool need_reseed = false; + int i; + + lqe_gl = qti_lqes_glbl(env); + + for (i = 0; i < qti_lqes_cnt(env); i++) { + lqe = qti_lqes(env)[i]; + if (qunit) + need_reseed |= qmt_adjust_qunit(env, lqe); + if (edquot) + need_reseed |= qmt_adjust_edquot(lqe, now); + } + + LASSERT(lqe_gl); + if (!lqe_gl->lqe_glbl_data && + (req_has_rep(qb_flags) || req_is_rel(qb_flags))) { + if (need_reseed) + CDEBUG(D_QUOTA, + "%s: can not notify - lge_glbl_data is not set\n", + qmt->qmt_svname); + return need_reseed; + } + + if (need_reseed) { + mutex_lock(&lqe_gl->lqe_glbl_data_lock); + if (lqe_gl->lqe_glbl_data) + qmt_seed_glbe_all(env, lqe_gl->lqe_glbl_data, qunit, + edquot); + mutex_unlock(&lqe_gl->lqe_glbl_data_lock); + + qmt_id_lock_notify(qmt, lqe_gl); + } + return need_reseed; +} + + /* * Adjust qunit & edquot flag in case it wasn't initialized already (e.g. * limit set while no slaves were connected yet) */ -void qmt_revalidate(const struct lu_env *env, struct lquota_entry *lqe) +bool qmt_revalidate(const struct lu_env *env, struct lquota_entry *lqe) { + bool need_notify = false; + if (lqe->lqe_qunit == 0) { /* lqe was read from disk, but neither qunit, nor edquot flag * were initialized */ - qmt_adjust_qunit(env, lqe); + need_notify = qmt_adjust_qunit(env, lqe); if (lqe->lqe_qunit != 0) - qmt_adjust_edquot(lqe, ktime_get_real_seconds()); + need_notify |= qmt_adjust_edquot(lqe, + ktime_get_real_seconds()); + } + + return need_notify; +} + +void qmt_revalidate_lqes(const struct lu_env *env, + struct qmt_device *qmt, __u32 qb_flags) +{ + struct lquota_entry *lqe_gl = qti_lqes_glbl(env); + bool need_notify = false; + int i; + + for (i = 0; i < qti_lqes_cnt(env); i++) + need_notify |= qmt_revalidate(env, qti_lqes(env)[i]); + + if (!need_notify) + return; + + /* There could be no ID lock to the moment of reconciliation. + * As a result lqe global data is not initialised yet. It is ok + * for release and report requests. */ + if (!lqe_gl->lqe_glbl_data && + (req_is_rel(qb_flags) || req_has_rep(qb_flags))) { + return; } + + mutex_lock(&lqe_gl->lqe_glbl_data_lock); + if (lqe_gl->lqe_glbl_data) + qmt_seed_glbe(env, lqe_gl->lqe_glbl_data); + mutex_unlock(&lqe_gl->lqe_glbl_data_lock); + + qmt_id_lock_notify(qmt, lqe_gl); +} + +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 *)); + + qti->qti_lqes_num = 0; + qti->qti_lqes_cnt = 0; +} + +__u64 qti_lqes_min_qunit(const struct lu_env *env) +{ + __u64 min, qunit; + int i; + + for (i = 1, min = qti_lqe_qunit(env, 0); i < qti_lqes_cnt(env); i++) { + qunit = qti_lqe_qunit(env, i); + /* if qunit is 0, lqe is not enforced and we can ignore it */ + if (qunit && qunit < min) + min = qunit; + } + + return min; +} + +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; +} + +int qti_lqes_restore_init(const struct lu_env *env) +{ + int rc = 0; + + if (qti_lqes_inited(env) && 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; +} + +void qti_lqes_restore_fini(const struct lu_env *env) +{ + if (qti_lqes_inited(env) && 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)); +} + +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]); +} + +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; + + if (!qti_lqes_cnt(env)) + RETURN_EXIT; + /* 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); }