4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2012, 2017, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann.lombardi@intel.com>
28 * Author: Niu Yawei <yawei.niu@intel.com>
31 #define DEBUG_SUBSYSTEM S_LQUOTA
33 #include <obd_class.h>
34 #include "qmt_internal.h"
37 * Retrieve quota settings for a given identifier.
39 * \param env - is the environment passed by the caller
40 * \param qmt - is the quota master target
41 * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or inode
42 * (i.e. LQUOTA_RES_MD)
43 * \param qtype - is the quota type
44 * \param id - is the quota indentifier for which we want to acces quota
46 * \param hard - is the output variable where to copy the hard limit
47 * \param soft - is the output variable where to copy the soft limit
48 * \param time - is the output variable where to copy the grace time
50 static int qmt_get(const struct lu_env *env, struct qmt_device *qmt,
51 __u8 restype, __u8 qtype, union lquota_id *id,
52 __u64 *hard, __u64 *soft, __u64 *time, bool is_default,
55 struct lquota_entry *lqe;
58 LASSERT(!is_default || id->qid_uid == 0);
59 if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
62 /* look-up lqe structure containing quota settings */
63 lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
67 /* copy quota settings */
69 LQUOTA_DEBUG(lqe, "fetch settings");
71 *hard = lqe->lqe_hardlimit;
73 *soft = lqe->lqe_softlimit;
75 *time = lqe->lqe_gracetime;
76 if (lqe->lqe_is_default)
77 *time |= (__u64)LQUOTA_FLAG_DEFAULT <<
86 struct qmt_entry_iter_data {
87 const struct lu_env *qeid_env;
88 struct qmt_device *qeid_qmt;
91 static int qmt_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
92 struct hlist_node *hnode, void *d)
94 struct qmt_entry_iter_data *iter = (struct qmt_entry_iter_data *)d;
95 struct lquota_entry *lqe;
97 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
98 LASSERT(atomic_read(&lqe->lqe_ref) > 0);
100 if (lqe->lqe_id.qid_uid == 0 || !lqe->lqe_is_default)
103 return qmt_set_with_lqe(iter->qeid_env, iter->qeid_qmt, lqe, 0, 0, 0, 0,
107 static void qmt_set_id_notify(const struct lu_env *env, struct qmt_device *qmt,
108 struct lquota_entry *lqe)
110 struct lquota_entry *lqe_gl;
113 lqe_gl = lqe->lqe_is_global ? lqe : NULL;
114 rc = qmt_pool_lqes_lookup_spec(env, qmt, lqe_rtype(lqe),
115 lqe_qtype(lqe), &lqe->lqe_id);
116 if (!qti_lqes_cnt(env))
119 if (!lqe_gl && qti_lqes_glbl(env)->lqe_is_global)
120 lqe_gl = qti_lqes_glbl(env);
125 mutex_lock(&lqe_gl->lqe_glbl_data_lock);
126 if (lqe_gl->lqe_glbl_data)
127 qmt_seed_glbe(env, lqe_gl->lqe_glbl_data);
128 mutex_unlock(&lqe_gl->lqe_glbl_data_lock);
130 /* Even if slaves haven't enqueued quota lock yet,
131 * it is needed to set lqe_revoke_time in qmt_id_lock_glimpse
132 * in case of reaching qpi_least_qunit */
133 qmt_id_lock_notify(qmt, lqe_gl);
139 * Update quota settings for a given lqe.
141 * \param env - is the environment passed by the caller
142 * \param qmt - is the quota master target
143 * \param lqe - is the lquota_entry for which we want to modify quota
145 * \param hard - is the new hard limit
146 * \param soft - is the new soft limit
147 * \param time - is the new grace time
148 * \param valid - is the list of settings to change
149 * \param is_default - true for default quota setting
150 * \param is_updated - true if the lqe is updated and no need to write back
153 int qmt_set_with_lqe(const struct lu_env *env, struct qmt_device *qmt,
154 struct lquota_entry *lqe, __u64 hard, __u64 soft,
155 __u64 time, __u32 valid, bool is_default, bool is_updated)
157 struct thandle *th = NULL;
160 bool dirtied = false;
162 bool need_id_notify = false;
165 /* need to write back to global quota file? */
167 /* By default we should have here only 1 lqe,
168 * so no allocations should be done. */
169 if (qti_lqes_restore_init(env))
170 GOTO(out_nolock, rc = -ENOMEM);
171 /* allocate & start transaction with enough credits to update
172 * quota settings in the global index file */
173 th = qmt_trans_start(env, lqe);
175 GOTO(out_nolock, rc = PTR_ERR(th));
178 now = ktime_get_real_seconds();
182 "changing quota settings valid:%x hard:%llu soft:%llu time:%llu",
183 valid, hard, soft, time);
185 if (is_default && lqe->lqe_id.qid_uid != 0) {
186 LQUOTA_DEBUG(lqe, "set qid %llu to use default quota setting",
187 lqe->lqe_id.qid_uid);
189 qmt_lqe_set_default(env, lqe->lqe_site->lqs_parent, lqe, false);
193 if ((valid & QIF_TIMES) != 0 && lqe->lqe_gracetime != time) {
194 /* change time settings */
195 lqe->lqe_gracetime = time;
199 if ((valid & QIF_LIMITS) != 0 &&
200 (lqe->lqe_hardlimit != hard || lqe->lqe_softlimit != soft)) {
201 rc = qmt_validate_limits(lqe, hard, soft);
205 /* change quota limits */
206 lqe->lqe_hardlimit = hard;
207 lqe->lqe_softlimit = soft;
210 GOTO(quota_write, 0);
214 /* recompute qunit in case it was never initialized */
215 if (qmt_revalidate(env, lqe))
216 need_id_notify = true;
218 /* clear grace time */
219 if (lqe->lqe_softlimit == 0 ||
220 lqe->lqe_granted <= lqe->lqe_softlimit)
221 /* no soft limit or below soft limit, let's clear grace
223 lqe->lqe_gracetime = 0;
224 else if ((valid & QIF_TIMES) == 0)
225 /* set grace only if user hasn't provided his own */
226 lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
228 /* change enforced status based on new parameters */
229 if (lqe->lqe_id.qid_uid == 0 || (lqe->lqe_hardlimit == 0 &&
230 lqe->lqe_softlimit == 0)) {
231 if (lqe->lqe_enforced) {
232 lqe->lqe_enforced = false;
233 /* Clear qunit and edquot as lqe_adjust_edquot
234 * does not handle not enforced lqes */
237 need_id_notify = true;
240 lqe->lqe_enforced = true;
246 if (!is_default && lqe->lqe_is_default) {
247 LQUOTA_DEBUG(lqe, "the qid %llu has been set quota"
248 " explicitly, clear the default flag",
249 lqe->lqe_id.qid_uid);
251 qmt_lqe_clear_default(lqe);
258 /* write new quota settings to disk */
259 rc = qmt_glb_write(env, th, lqe, LQUOTA_BUMP_VER, &ver);
261 /* restore initial quota settings */
262 qmt_restore(lqe, &qti_lqes_rstr(env)[0]);
266 ver = dt_version_get(env, LQE_GLB_OBJ(lqe));
269 /* compute new qunit value now that we have modified the quota
270 * settings or clear/set edquot flag if needed */
271 need_id_notify |= qmt_adjust_qunit(env, lqe);
272 need_id_notify |= qmt_adjust_edquot(lqe, now);
276 lqe_write_unlock(lqe);
280 if (th != NULL && !IS_ERR(th))
281 dt_trans_stop(env, qmt->qmt_child, th);
282 qti_lqes_restore_fini(env);
285 if (rc == 0 && dirtied) {
286 qmt_glb_lock_notify(env, lqe, ver);
287 if (lqe->lqe_id.qid_uid == 0) {
288 struct qmt_entry_iter_data iter_data;
290 LQUOTA_DEBUG(lqe, "notify all lqe with default quota");
291 iter_data.qeid_env = env;
292 iter_data.qeid_qmt = qmt;
293 cfs_hash_for_each(lqe->lqe_site->lqs_hash,
294 qmt_entry_iter_cb, &iter_data);
295 /* Always notify slaves with default values. Don't
296 * care about overhead as will be sent only not changed
297 * values(see qmt_id_lock_cb for details).*/
298 need_id_notify = true;
300 /* qti_lqes_inited > 0 means we came here from another
301 * qmt_pool_lqes_lookup(qmt_dqacq, intent_policy ...). Thus
302 * we can't init and add new lqes to don't overwrite already
305 if (!qti_lqes_inited(env) && need_id_notify)
306 qmt_set_id_notify(env, qmt, lqe);
313 * Update quota settings for a given identifier.
315 * \param env - is the environment passed by the caller
316 * \param qmt - is the quota master target
317 * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or
318 * inode (i.e. LQUOTA_RES_MD)
319 * \param qtype - is the quota type
320 * \param id - is the quota indentifier for which we want to modify
322 * \param hard - is the new hard limit
323 * \param soft - is the new soft limit
324 * \param time - is the new grace time
325 * \param valid - is the list of settings to change
326 * \param is_default - true for default quota setting
327 * \param is_updated - true if the lqe is updated and no need to write back
329 static int qmt_set(const struct lu_env *env, struct qmt_device *qmt,
330 __u8 restype, __u8 qtype, union lquota_id *id,
331 __u64 hard, __u64 soft, __u64 time, __u32 valid,
332 bool is_default, bool is_updated, char *pool_name)
334 struct lquota_entry *lqe;
338 if (pool_name && !strnlen(pool_name, LOV_MAXPOOLNAME))
341 /* look-up quota entry associated with this ID */
342 lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, id, pool_name);
344 RETURN(PTR_ERR(lqe));
346 lqe->lqe_is_deleted = 0;
347 rc = qmt_set_with_lqe(env, qmt, lqe, hard, soft, time, valid,
348 is_default, is_updated);
350 lqe->lqe_is_deleted = 0;
357 * Delete the quota setting of the specified quota ID
359 * \param env - is the environment passed by the caller
360 * \param qmt - is the quota master target
361 * \param restype - is the pool type, either block (i.e. LQUOTA_RES_DT) or
362 * inode (i.e. LQUOTA_RES_MD)
363 * \param qtype - is the quota type
364 * \param qid - is the quota indentifier for which we want to delete its
367 static int qmt_delete_qid(const struct lu_env *env, struct qmt_device *qmt,
368 __u8 restype, __u8 qtype, __u64 qid)
370 struct qmt_thread_info *qti = qmt_info(env);
371 union lquota_id *quota_id = &qti->qti_id;
372 struct thandle *th = NULL;
373 struct qmt_pool_info *qpi = NULL;
374 struct lquota_entry *lqe = NULL;
380 quota_id->qid_uid = qid;
381 lqe = qmt_pool_lqe_lookup(env, qmt, restype, qtype, quota_id, NULL);
383 RETURN(PTR_ERR(lqe));
387 qpi = qmt_pool_lookup_glb(env, qmt, restype);
389 GOTO(out, rc = -ENOMEM);
391 th = qmt_trans_start(env, lqe);
393 GOTO(out, rc = PTR_ERR(th));
395 rc = lquota_disk_delete(env, th,
396 qpi->qpi_glb_obj[qtype], qid, &ver);
398 dt_trans_stop(env, qmt->qmt_child, th);
401 lqe_set_deleted(lqe);
402 qmt_glb_lock_notify(env, lqe, ver);
403 } else if (rc == -ENOENT) {
408 if (!IS_ERR_OR_NULL(qpi))
409 qpi_putref(env, qpi);
411 lqe_write_unlock(lqe);
418 * Handle quotactl request.
420 * \param env - is the environment passed by the caller
421 * \param ld - is the lu device associated with the qmt
422 * \param oqctl - is the quotactl request
424 static int qmt_quotactl(const struct lu_env *env, struct lu_device *ld,
425 struct obd_quotactl *oqctl)
427 struct qmt_thread_info *qti = qmt_info(env);
428 union lquota_id *id = &qti->qti_id;
429 struct qmt_device *qmt = lu2qmt_dev(ld);
430 struct obd_dqblk *dqb = &oqctl->qc_dqblk;
433 bool is_default = false;
436 LASSERT(qmt != NULL);
438 if (oqctl->qc_type >= LL_MAXQUOTAS)
439 /* invalid quota type */
442 poolname = LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd) ?
443 oqctl->qc_poolname : NULL;
445 switch (oqctl->qc_cmd) {
447 case Q_GETINFO: /* read grace times */
448 case LUSTRE_Q_GETINFOPOOL:
449 /* Global grace time is stored in quota settings of ID 0. */
452 /* read inode grace time */
453 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id, NULL,
454 NULL, &oqctl->qc_dqinfo.dqi_igrace,
456 /* There could be no MD pool, so try to find DT pool */
457 if (rc && rc != -ENOENT)
460 /* read block grace time */
461 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id, NULL,
462 NULL, &oqctl->qc_dqinfo.dqi_bgrace,
466 case Q_SETINFO: /* modify grace times */
467 case LUSTRE_Q_SETINFOPOOL:
468 /* setinfo should be using dqi->dqi_valid, but lfs incorrectly
469 * sets the valid flags in dqb->dqb_valid instead, try to live
472 /* Global grace time is stored in quota settings of ID 0. */
475 if ((dqb->dqb_valid & QIF_ITIME) != 0) {
476 /* set inode grace time */
477 rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
478 id, 0, 0, oqctl->qc_dqinfo.dqi_igrace,
479 QIF_TIMES, false, false,
485 if ((dqb->dqb_valid & QIF_BTIME) != 0)
486 /* set block grace time */
487 rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
488 id, 0, 0, oqctl->qc_dqinfo.dqi_bgrace,
489 QIF_TIMES, false, false,
493 case LUSTRE_Q_GETDEFAULT:
494 case LUSTRE_Q_GETDEFAULT_POOL:
498 case Q_GETQUOTA: /* consult quota limit */
499 case LUSTRE_Q_GETQUOTAPOOL:
500 /* extract quota ID from quotactl request */
501 id->qid_uid = oqctl->qc_id;
503 /* look-up inode quota settings */
504 rc = qmt_get(env, qmt, LQUOTA_RES_MD, oqctl->qc_type, id,
505 &dqb->dqb_ihardlimit, &dqb->dqb_isoftlimit,
506 &dqb->dqb_itime, is_default, poolname);
507 /* There could be no MD pool, so try to find DT pool */
508 if (rc && rc != -ENOENT)
511 dqb->dqb_valid |= QIF_ILIMITS | QIF_ITIME;
513 /* master isn't aware of actual inode usage */
514 dqb->dqb_curinodes = 0;
516 /* look-up block quota settings */
517 rc = qmt_get(env, qmt, LQUOTA_RES_DT, oqctl->qc_type, id,
518 &dqb->dqb_bhardlimit, &dqb->dqb_bsoftlimit,
519 &dqb->dqb_btime, is_default, poolname);
523 dqb->dqb_valid |= QIF_BLIMITS | QIF_BTIME;
524 /* master doesn't know the actual block usage */
525 dqb->dqb_curspace = 0;
528 case LUSTRE_Q_SETDEFAULT:
529 case LUSTRE_Q_SETDEFAULT_POOL:
533 case Q_SETQUOTA: /* change quota limits */
534 case LUSTRE_Q_SETQUOTAPOOL:
535 /* extract quota ID from quotactl request */
536 id->qid_uid = oqctl->qc_id;
538 if ((dqb->dqb_valid & QIF_IFLAGS) != 0) {
539 /* update inode quota settings */
540 rc = qmt_set(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
541 id, dqb->dqb_ihardlimit,
542 dqb->dqb_isoftlimit, dqb->dqb_itime,
543 dqb->dqb_valid & QIF_IFLAGS, is_default,
549 if ((dqb->dqb_valid & QIF_BFLAGS) != 0)
550 /* update block quota settings */
551 rc = qmt_set(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
552 id, dqb->dqb_bhardlimit,
553 dqb->dqb_bsoftlimit, dqb->dqb_btime,
554 dqb->dqb_valid & QIF_BFLAGS, is_default,
558 case LUSTRE_Q_DELETEQID:
559 rc = qmt_delete_qid(env, qmt, LQUOTA_RES_MD, oqctl->qc_type,
564 rc = qmt_delete_qid(env, qmt, LQUOTA_RES_DT, oqctl->qc_type,
569 CERROR("%s: unsupported quotactl command: %d\n",
570 qmt->qmt_svname, oqctl->qc_cmd);
578 void qmt_grant_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
582 for (i = 0; i < qti_lqes_cnt(env); i++)
583 qti_lqe_granted(env, i) += cnt;
588 static inline bool qmt_lqes_can_rel(const struct lu_env *env, __u64 cnt)
590 bool can_release = true;
593 for (i = 0; i < qti_lqes_cnt(env); i++) {
594 if (cnt > qti_lqe_granted(env, i)) {
595 LQUOTA_ERROR(qti_lqes(env)[i],
596 "Can't release %llu that is larger than lqe_granted.\n",
604 static inline void qmt_rel_lqes(const struct lu_env *env, __u64 *slv, __u64 cnt)
608 for (i = 0; i < qti_lqes_cnt(env); i++)
609 qti_lqe_granted(env, i) -= cnt;
614 static inline bool qmt_lqes_cannot_grant(const struct lu_env *env, __u64 cnt)
616 bool cannot_grant = false;
619 for (i = 0; i < qti_lqes_cnt(env); i++) {
620 if (qti_lqe_hard(env, i) != 0 &&
621 qti_lqe_granted(env, i) + cnt > qti_lqe_hard(env, i)) {
629 static inline __u64 qmt_lqes_grant_some_quota(const struct lu_env *env)
631 __u64 min_count, tmp;
635 for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
636 if (!qti_lqes(env)[i]->lqe_enforced &&
637 !qti_lqes(env)[i]->lqe_is_global)
640 tmp = qti_lqe_hard(env, i) - qti_lqe_granted(env, i);
642 min_count = tmp < min_count ? tmp : min_count;
651 static inline __u64 qmt_lqes_alloc_expand(const struct lu_env *env,
652 __u64 slv_granted, __u64 spare)
654 __u64 min_count, tmp;
658 for (i = 0, min_count = 0; i < qti_lqes_cnt(env); i++) {
659 /* Don't take into account not enforced lqes that belong
660 * to non global pool. These lqes present in array to
661 * support actual lqe_granted even for lqes without limits. */
662 if (!qti_lqes(env)[i]->lqe_enforced &&
663 !qti_lqes(env)[i]->lqe_is_global)
666 tmp = qmt_alloc_expand(qti_lqes(env)[i], slv_granted, spare);
668 min_count = tmp < min_count ? tmp : min_count;
677 static inline void qmt_lqes_tune_grace(const struct lu_env *env, __u64 now)
681 for (i = 0; i < qti_lqes_cnt(env); i++) {
682 struct lquota_entry *lqe;
684 lqe = qti_lqes(env)[i];
685 if (lqe->lqe_softlimit != 0) {
686 if (lqe->lqe_granted > lqe->lqe_softlimit &&
687 lqe->lqe_gracetime == 0) {
688 /* First time over soft limit, let's start grace
690 lqe->lqe_gracetime = now + qmt_lqe_grace(lqe);
691 } else if (lqe->lqe_granted <= lqe->lqe_softlimit &&
692 lqe->lqe_gracetime != 0) {
693 /* Clear grace timer */
694 lqe->lqe_gracetime = 0;
701 * Helper function to handle quota request from slave.
703 * \param env - is the environment passed by the caller
704 * \param qmt - is the master device
705 * \param uuid - is the uuid associated with the slave
706 * \param qb_flags - are the quota request flags as packed in the quota_body
707 * \param qb_count - is the amount of quota space the slave wants to
709 * \param qb_usage - is the current space usage on the slave
710 * \param repbody - is the quota_body of reply
712 * \retval 0 : success
713 * \retval -EDQUOT : out of quota
714 * -EINPROGRESS : inform client to retry write/create
715 * -ve : other appropriate errors
717 int qmt_dqacq0(const struct lu_env *env, struct qmt_device *qmt,
718 struct obd_uuid *uuid, __u32 qb_flags, __u64 qb_count,
719 __u64 qb_usage, struct quota_body *repbody)
722 struct dt_object *slv_obj = NULL;
723 __u64 slv_granted, slv_granted_bck;
724 struct thandle *th = NULL;
726 struct lquota_entry *lqe = qti_lqes_glbl(env);
729 LASSERT(uuid != NULL);
731 /* initialize reply */
732 memset(repbody, 0, sizeof(*repbody));
733 memcpy(&repbody->qb_id, &lqe->lqe_id, sizeof(repbody->qb_id));
735 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_RECOVERABLE_ERR))
736 RETURN(-cfs_fail_val);
738 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_PREACQ) &&
739 (req_is_preacq(qb_flags) || req_is_rel(qb_flags)))
742 if (qti_lqes_restore_init(env))
745 /* look-up index file associated with acquiring slave */
746 slv_obj = lquota_disk_slv_find(env, qmt->qmt_child, LQE_ROOT(lqe),
747 lu_object_fid(&LQE_GLB_OBJ(lqe)->do_lu),
750 GOTO(out, rc = PTR_ERR(slv_obj));
752 /* pack slave fid in reply just for sanity check */
753 memcpy(&repbody->qb_slv_fid, lu_object_fid(&slv_obj->do_lu),
754 sizeof(struct lu_fid));
756 /* allocate & start transaction with enough credits to update
757 * global & slave indexes */
758 th = qmt_trans_start_with_slv(env, NULL, slv_obj, false);
760 GOTO(out, rc = PTR_ERR(th));
762 qti_lqes_write_lock(env);
764 LQUOTA_DEBUG_LQES(env, "dqacq starts uuid:%s flags:0x%x wanted:%llu"
765 " usage:%llu", obd_uuid2str(uuid), qb_flags, qb_count,
768 /* Legal race, limits have been removed on master, but slave didn't
769 * receive the change yet. Just return EINPROGRESS until the slave gets
771 if (!lqe->lqe_enforced && !req_is_rel(qb_flags))
772 GOTO(out_locked, rc = -ESRCH);
774 /* recompute qunit in case it was never initialized */
775 qmt_revalidate_lqes(env, qmt, qb_flags);
777 /* slave just wants to acquire per-ID lock */
778 if (req_is_acq(qb_flags) && qb_count == 0)
779 GOTO(out_locked, rc = 0);
781 /* fetch how much quota space is already granted to this slave */
782 rc = qmt_slv_read(env, &lqe->lqe_id, slv_obj, &slv_granted);
784 LQUOTA_ERROR(lqe, "Failed to get granted for slave %s, rc=%d",
785 obd_uuid2str(uuid), rc);
786 GOTO(out_locked, rc);
788 /* recall how much space this slave currently owns in order to restore
789 * it in case of failure */
790 slv_granted_bck = slv_granted;
792 /* record current time for soft limit & grace time management */
793 now = ktime_get_real_seconds();
795 if (req_is_rel(qb_flags)) {
796 /* Slave would like to release quota space */
797 if (slv_granted < qb_count ||
798 !qmt_lqes_can_rel(env, qb_count)) {
799 /* can't release more than granted */
800 LQUOTA_ERROR_LQES(env,
801 "Release too much! uuid:%s release: %llu granted:%llu, total:%llu",
802 obd_uuid2str(uuid), qb_count,
803 slv_granted, lqe->lqe_granted);
804 GOTO(out_locked, rc = -EINVAL);
807 repbody->qb_count = qb_count;
808 /* put released space back to global pool */
809 qmt_rel_lqes(env, &slv_granted, qb_count);
810 GOTO(out_write, rc = 0);
813 if (req_has_rep(qb_flags) && slv_granted < qb_usage) {
814 /* Slave is reporting space usage in quota request and it turns
815 * out to be using more quota space than owned, so we adjust
816 * granted space regardless of the current state of affairs */
817 repbody->qb_count = qb_usage - slv_granted;
818 qmt_grant_lqes(env, &slv_granted, repbody->qb_count);
821 if (!req_is_acq(qb_flags) && !req_is_preacq(qb_flags))
822 GOTO(out_write, rc = 0);
824 qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
825 if (qti_lqes_edquot(env))
826 /* no hope to claim further space back */
827 GOTO(out_write, rc = -EDQUOT);
829 if (qmt_space_exhausted_lqes(env, now)) {
830 /* might have some free space once rebalancing is completed */
831 rc = req_is_acq(qb_flags) ? -EINPROGRESS : -EDQUOT;
835 if (req_is_preacq(qb_flags)) {
836 /* slave would like to pre-acquire quota space. To do so, it
837 * reports in qb_count how much spare quota space it owns and we
838 * can grant back quota space which is consistent with qunit
840 if (qb_count >= qti_lqes_min_qunit(env))
841 /* slave already own the maximum it should */
842 GOTO(out_write, rc = 0);
844 count = qmt_lqes_alloc_expand(env, slv_granted, qb_count);
846 GOTO(out_write, rc = -EDQUOT);
848 repbody->qb_count += count;
849 qmt_grant_lqes(env, &slv_granted, count);
850 GOTO(out_write, rc = 0);
853 /* processing acquire request with clients waiting */
854 if (qmt_lqes_cannot_grant(env, qb_count)) {
855 /* cannot grant as much as asked, but can still afford to grant
856 * some quota space back */
857 count = qmt_lqes_grant_some_quota(env);
858 repbody->qb_count += count;
859 qmt_grant_lqes(env, &slv_granted, count);
860 GOTO(out_write, rc = 0);
863 /* Whouhou! we can satisfy the slave request! */
864 repbody->qb_count += qb_count;
865 qmt_grant_lqes(env, &slv_granted, qb_count);
867 /* Try to expand the acquired count for DQACQ */
868 count = qmt_lqes_alloc_expand(env, slv_granted, 0);
870 /* can even grant more than asked, it is like xmas ... */
871 repbody->qb_count += count;
872 qmt_grant_lqes(env, &slv_granted, count);
873 GOTO(out_write, rc = 0);
876 GOTO(out_write, rc = 0);
878 if (repbody->qb_count == 0)
879 GOTO(out_locked, rc);
881 /* start/stop grace timer if required */
882 qmt_lqes_tune_grace(env, now);
884 /* Update slave index first since it is easier to roll back */
885 ret = qmt_slv_write(env, th, lqe, slv_obj, LQUOTA_BUMP_VER,
886 &repbody->qb_slv_ver, slv_granted);
888 /* restore initial quota settings */
889 qmt_restore_lqes(env);
891 repbody->qb_count = 0;
892 GOTO(out_locked, rc = ret);
895 /* Update global index, no version bump needed */
896 ret = qmt_glb_write_lqes(env, th, 0, NULL);
899 /* restore initial quota settings */
900 qmt_restore_lqes(env);
902 repbody->qb_count = 0;
904 /* restore previous granted value */
905 ret = qmt_slv_write(env, th, lqe, slv_obj, 0, NULL,
908 LQUOTA_ERROR(lqe, "failed to restore initial slave "
909 "value rc:%d ret%d", rc, ret);
912 qmt_adjust_edquot_notify(env, qmt, now, qb_flags);
913 GOTO(out_locked, rc);
916 /* Total granted has been changed, let's try to adjust the qunit
917 * size according to the total granted & limits. */
919 /* clear/set edquot flag and notify slaves via glimpse if needed */
920 qmt_adjust_and_notify(env, qmt, now, qb_flags);
922 LQUOTA_DEBUG_LQES(env, "dqacq ends count:%llu ver:%llu rc:%d",
923 repbody->qb_count, repbody->qb_slv_ver, rc);
924 qti_lqes_write_unlock(env);
926 qti_lqes_restore_fini(env);
928 if (th != NULL && !IS_ERR(th))
929 dt_trans_stop(env, qmt->qmt_child, th);
931 if (slv_obj != NULL && !IS_ERR(slv_obj))
932 dt_object_put(env, slv_obj);
934 if ((req_is_acq(qb_flags) || req_is_preacq(qb_flags)) &&
935 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_EDQUOT)) {
936 /* introduce inconsistency between granted value in slave index
937 * and slave index copy of slave */
938 repbody->qb_count = 0;
946 * Extract index from uuid or quota index file name.
948 * \param[in] uuid uuid or quota index name(0x1020000-OST0001_UUID)
949 * \param[out] idx pointer to save index
951 * \retval slave type(QMT_STYPE_MDT or QMT_STYPE_OST)
952 * \retval -EINVAL wrong uuid
954 int qmt_uuid2idx(struct obd_uuid *uuid, int *idx)
956 char *uuid_str, *name, *dash;
959 uuid_str = (char *)uuid->uuid;
961 if (strnlen(uuid_str, UUID_MAX) >= UUID_MAX) {
962 CERROR("quota: UUID '%.*s' missing trailing NUL: rc = %d\n",
963 UUID_MAX, uuid_str, rc);
967 dash = strrchr(uuid_str, '-');
969 /* Going to get index from MDTXXXX/OSTXXXX. Thus uuid should
970 * have at least 8 bytes after '-': 3 for MDT/OST, 4 for index
971 * and 1 byte for null character. */
972 if (*dash != '-' || ((uuid_str + UUID_MAX - name) < 8)) {
973 CERROR("quota: wrong UUID format '%s': rc = %d\n",
978 rc = target_name2index(name, idx, NULL);
980 case LDD_F_SV_TYPE_MDT:
983 case LDD_F_SV_TYPE_OST:
987 CERROR("quota: wrong UUID type '%s': rc = %d\n", uuid_str, rc);
995 * Handle quota request from slave.
997 * \param env - is the environment passed by the caller
998 * \param ld - is the lu device associated with the qmt
999 * \param req - is the quota acquire request
1001 static int qmt_dqacq(const struct lu_env *env, struct lu_device *ld,
1002 struct ptlrpc_request *req)
1004 struct qmt_device *qmt = lu2qmt_dev(ld);
1005 struct quota_body *qbody, *repbody;
1006 struct obd_uuid *uuid;
1007 struct ldlm_lock *lock;
1012 qbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
1014 RETURN(err_serious(-EPROTO));
1016 repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
1017 if (repbody == NULL)
1018 RETURN(err_serious(-EFAULT));
1020 /* verify if global lock is stale */
1021 if (!lustre_handle_is_used(&qbody->qb_glb_lockh))
1024 lock = ldlm_handle2lock(&qbody->qb_glb_lockh);
1027 LDLM_LOCK_PUT(lock);
1029 uuid = &req->rq_export->exp_client_uuid;
1030 stype = qmt_uuid2idx(uuid, &idx);
1034 if (req_is_rel(qbody->qb_flags) + req_is_acq(qbody->qb_flags) +
1035 req_is_preacq(qbody->qb_flags) > 1) {
1036 CERROR("%s: malformed quota request with conflicting flags set "
1037 "(%x) from slave %s\n", qmt->qmt_svname,
1038 qbody->qb_flags, obd_uuid2str(uuid));
1042 if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
1043 /* acquire and pre-acquire should use a valid ID lock */
1045 if (!lustre_handle_is_used(&qbody->qb_lockh))
1048 lock = ldlm_handle2lock(&qbody->qb_lockh);
1050 /* no lock associated with this handle */
1053 LDLM_DEBUG(lock, "%sacquire request",
1054 req_is_preacq(qbody->qb_flags) ? "pre" : "");
1056 if (!obd_uuid_equals(&lock->l_export->exp_client_uuid, uuid)) {
1057 /* sorry, no way to cheat ... */
1058 LDLM_LOCK_PUT(lock);
1062 if (ldlm_is_ast_sent(lock)) {
1063 struct ptlrpc_service_part *svc;
1066 svc = req->rq_rqbd->rqbd_svcpt;
1067 timeout = at_est2timeout(at_get(&svc->scp_at_estimate));
1068 timeout += (ldlm_bl_timeout(lock) >> 1);
1070 /* lock is being cancelled, prolong timeout */
1071 ldlm_refresh_waiting_lock(lock, timeout);
1073 LDLM_LOCK_PUT(lock);
1076 /* extract quota information from global index FID packed in the
1078 rc = lquota_extract_fid(&qbody->qb_fid, &rtype, &qtype);
1082 /* Find the quota entry associated with the quota id */
1083 rc = qmt_pool_lqes_lookup(env, qmt, rtype, stype, qtype,
1084 &qbody->qb_id, NULL, idx);
1088 rc = qmt_dqacq0(env, qmt, uuid, qbody->qb_flags,
1089 qbody->qb_count, qbody->qb_usage, repbody);
1091 if (lustre_handle_is_used(&qbody->qb_lockh))
1092 /* return current qunit value only to slaves owning an per-ID
1093 * quota lock. For enqueue, the qunit value will be returned in
1095 repbody->qb_qunit = qti_lqes_min_qunit(env);
1096 CDEBUG(D_QUOTA, "qmt_dqacq return qb_qunit %llu qb_count %llu\n",
1097 repbody->qb_qunit, repbody->qb_count);
1102 /* Vector of quota request handlers. This vector is used by the MDT to forward
1103 * requests to the quota master. */
1104 struct qmt_handlers qmt_hdls = {
1105 /* quota request handlers */
1106 .qmth_quotactl = qmt_quotactl,
1107 .qmth_dqacq = qmt_dqacq,
1110 .qmth_intent_policy = qmt_intent_policy,
1111 .qmth_lvbo_init = qmt_lvbo_init,
1112 .qmth_lvbo_update = qmt_lvbo_update,
1113 .qmth_lvbo_size = qmt_lvbo_size,
1114 .qmth_lvbo_fill = qmt_lvbo_fill,
1115 .qmth_lvbo_free = qmt_lvbo_free,
1117 EXPORT_SYMBOL(qmt_hdls);