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 <linux/kthread.h>
35 #include <lustre_dlm.h>
36 #include <lustre_swab.h>
37 #include <obd_class.h>
39 #include "qmt_internal.h"
41 /* intent policy function called from mdt_intent_opc() when the intent is of
43 int qmt_intent_policy(const struct lu_env *env, struct lu_device *ld,
44 struct ptlrpc_request *req, struct ldlm_lock **lockp,
47 struct qmt_device *qmt = lu2qmt_dev(ld);
48 struct ldlm_intent *it;
49 struct quota_body *reqbody;
50 struct quota_body *repbody;
51 struct obd_uuid *uuid;
52 struct lquota_lvb *lvb;
53 struct ldlm_resource *res = (*lockp)->l_resource;
54 struct ldlm_reply *ldlm_rep;
58 req_capsule_extend(&req->rq_pill, &RQF_LDLM_INTENT_QUOTA);
59 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
60 ldlm_lvbo_size(*lockp));
62 /* extract quota body and intent opc */
63 it = req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
65 RETURN(err_serious(-EFAULT));
67 reqbody = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_BODY);
69 RETURN(err_serious(-EFAULT));
72 rc = req_capsule_server_pack(&req->rq_pill);
74 CERROR("Can't pack response, rc %d\n", rc);
75 RETURN(err_serious(rc));
78 repbody = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_BODY);
80 RETURN(err_serious(-EFAULT));
82 ldlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
84 RETURN(err_serious(-EFAULT));
86 uuid = &(*lockp)->l_export->exp_client_uuid;
89 case IT_QUOTA_DQACQ: {
90 struct lquota_entry *lqe;
91 struct ldlm_lock *lock;
94 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
95 /* acquire on global lock? something is wrong ... */
96 GOTO(out, rc = -EPROTO);
98 /* verify global lock isn't stale */
99 if (!lustre_handle_is_used(&reqbody->qb_glb_lockh))
100 GOTO(out, rc = -ENOLCK);
102 lock = ldlm_handle2lock(&reqbody->qb_glb_lockh);
104 GOTO(out, rc = -ENOLCK);
107 stype = qmt_uuid2idx(uuid, &idx);
109 GOTO(out, rc = -EINVAL);
111 /* TODO: it seems we don't need to get lqe from
112 * lq_lvb_data anymore ... And do extra get
114 lqe = res->lr_lvb_data;
115 LASSERT(lqe != NULL);
118 rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), stype,
119 lqe_qtype(lqe), &reqbody->qb_id,
126 /* acquire quota space */
127 rc = qmt_dqacq0(env, qmt, uuid,
128 reqbody->qb_flags, reqbody->qb_count,
129 reqbody->qb_usage, repbody,
130 qmt_dom(lqe_rtype(lqe), stype) ? -1 : idx);
139 /* new connection from slave */
141 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0)
142 /* connection on per-ID lock? something is wrong ... */
143 GOTO(out, rc = -EPROTO);
145 rc = qmt_pool_new_conn(env, qmt, &reqbody->qb_fid,
146 &repbody->qb_slv_fid,
147 &repbody->qb_slv_ver, uuid);
153 CERROR("%s: invalid intent opcode: %llu\n", qmt->qmt_svname,
155 GOTO(out, rc = -EINVAL);
158 /* on success, pack lvb in reply */
159 lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
160 lvb_len = ldlm_lvbo_size(*lockp);
161 lvb_len = ldlm_lvbo_fill(*lockp, lvb, &lvb_len);
163 GOTO(out, rc = lvb_len);
165 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len, RCL_SERVER);
167 ldlm_rep->lock_policy_res2 = clear_serious(rc);
173 * Initialize quota LVB associated with quota indexes.
174 * Called with res->lr_lvb_sem held
176 int qmt_lvbo_init(struct lu_device *ld, struct ldlm_resource *res)
179 struct qmt_thread_info *qti;
180 struct qmt_device *qmt = lu2qmt_dev(ld);
181 int pool_type, qtype;
185 LASSERT(res != NULL);
187 if (res->lr_type != LDLM_PLAIN)
190 if (res->lr_lvb_data ||
191 res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
198 /* extract global index FID and quota identifier */
199 fid_extract_from_quota_res(&qti->qti_fid, &qti->qti_id, &res->lr_name);
201 /* sanity check the global index FID */
202 rc = lquota_extract_fid(&qti->qti_fid, &pool_type, &qtype);
204 CERROR("can't extract glb index information from FID "DFID"\n",
205 PFID(&qti->qti_fid));
209 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
210 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
211 * we are thus dealing with an ID lock. */
212 struct qmt_pool_info *pool;
213 struct lquota_entry *lqe;
214 struct lqe_glbl_data *lgd;
216 pool = qmt_pool_lookup_glb(env, qmt, pool_type);
218 GOTO(out, rc = -ENOMEM);
220 /* Find the quota entry associated with the quota id */
221 lqe = qmt_pool_lqe_lookup(env, qmt, pool_type, qtype,
224 qpi_putref(env, pool);
225 GOTO(out, rc = PTR_ERR(lqe));
228 /* TODO: need something like qmt_extend_lqe_gd that has
229 * to be calledeach time when qpi_slv_nr is incremented */
230 lgd = qmt_alloc_lqe_gd(pool, qtype);
233 qpi_putref(env, pool);
234 GOTO(out, rc = -ENOMEM);
237 qmt_setup_lqe_gd(env, qmt, lqe, lgd, pool_type);
239 /* store reference to lqe in lr_lvb_data */
240 res->lr_lvb_data = lqe;
241 qpi_putref(env, pool);
242 LQUOTA_DEBUG(lqe, "initialized res lvb");
244 struct dt_object *obj;
246 /* lookup global index */
247 obj = dt_locate(env, qmt->qmt_child, &qti->qti_fid);
249 GOTO(out, rc = PTR_ERR(obj));
250 if (!dt_object_exists(obj)) {
251 dt_object_put(env, obj);
252 GOTO(out, rc = -ENOENT);
255 /* store reference to global index object in lr_lvb_data */
256 res->lr_lvb_data = obj;
257 CDEBUG(D_QUOTA, DFID" initialized lvb\n", PFID(&qti->qti_fid));
260 res->lr_lvb_len = sizeof(struct lquota_lvb);
266 /* clear lge_qunit/edquot_nu flags -
267 * slave recieved new qunit and edquot.
269 * \retval true if revoke is needed - qunit
270 * for this slave reaches least_qunit
272 static bool qmt_clear_lgeg_arr_nu(struct lquota_entry *lqe, int stype, int idx)
274 unsigned long least_qunit = lqe2qpi(lqe)->qpi_least_qunit;
275 struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
277 /* There is no array to store lge for the case of DOM.
278 * Ignore it until MDT pools will be ready. */
279 if (!qmt_dom(lqe_rtype(lqe), stype)) {
280 lqe->lqe_glbl_data->lqeg_arr[idx].lge_qunit_nu = 0;
281 lqe->lqe_glbl_data->lqeg_arr[idx].lge_edquot_nu = 0;
283 /* We shouldn't call revoke for DOM case, it will be updated
284 * at qmt_id_lock_glimpse. */
285 return (lgd->lqeg_arr[idx].lge_qunit == least_qunit);
291 static bool qmt_set_revoke(struct lu_env *env, struct lquota_entry *lqe,
294 unsigned long least_qunit = lqe2qpi(lqe)->qpi_least_qunit;
295 struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
298 if (lgd->lqeg_arr[idx].lge_qunit == least_qunit) {
301 qti_lqes_write_lock(env);
302 for (i = 0; i < qti_lqes_cnt(env); i++) {
303 LQUOTA_DEBUG(qti_lqes(env)[i],
304 "idx %d lge_qunit %llu least_qunit %lu\n",
305 idx, lgd->lqeg_arr[idx].lge_qunit,
307 if (qti_lqes(env)[i]->lqe_qunit == least_qunit) {
308 qti_lqes(env)[i]->lqe_revoke_time =
310 notify |= qmt_adjust_edquot(qti_lqes(env)[i],
311 ktime_get_real_seconds());
314 qti_lqes_write_unlock(env);
320 * Update LVB associated with the global quota index.
321 * This function is called from the DLM itself after a glimpse callback, in this
322 * case valid ptlrpc request is passed.
324 int qmt_lvbo_update(struct lu_device *ld, struct ldlm_resource *res,
325 struct ptlrpc_request *req, int increase_only)
328 struct qmt_thread_info *qti;
329 struct qmt_device *qmt = lu2qmt_dev(ld);
330 struct lquota_entry *lqe;
331 struct lquota_lvb *lvb;
332 struct ldlm_lock *lock;
333 struct obd_export *exp;
335 int rc = 0, idx, stype;
338 LASSERT(res != NULL);
343 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
344 /* no need to update lvb for global quota locks */
347 lvb = req_capsule_server_swab_get(&req->rq_pill, &RMF_DLM_LVB,
348 lustre_swab_lquota_lvb);
350 CERROR("%s: failed to extract lvb from request\n",
355 lqe = res->lr_lvb_data;
356 LASSERT(lqe != NULL);
359 /* allocate environement */
364 /* The request is a glimpse callback which was sent via the
365 * reverse import to the slave. What we care about here is the
366 * export associated with the slave and req->rq_export is
367 * definitely not what we are looking for (it is actually set to
369 * Therefore we extract the lock from the request argument
370 * and use lock->l_export. */
371 lock = ldlm_request_lock(req);
373 CERROR("%s: failed to get lock from request!\n",
375 GOTO(out, rc = PTR_ERR(lock));
378 exp = class_export_get(lock->l_export);
380 CERROR("%s: failed to get export from lock!\n",
382 GOTO(out, rc = -EFAULT);
385 stype = qmt_uuid2idx(&exp->exp_client_uuid, &idx);
387 GOTO(out_exp, rc = stype);
389 need_revoke = qmt_clear_lgeg_arr_nu(lqe, stype, idx);
390 if (lvb->lvb_id_rel == 0) {
391 /* nothing to release */
392 if (lvb->lvb_id_may_rel != 0)
393 /* but might still release later ... */
394 lqe->lqe_may_rel += lvb->lvb_id_may_rel;
397 if (!need_revoke && lvb->lvb_id_rel == 0)
398 GOTO(out_exp, rc = 0);
400 rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), stype,
401 lqe_qtype(lqe), &lqe->lqe_id, NULL, idx);
405 if (need_revoke && qmt_set_revoke(env, lqe, stype, idx)) {
406 mutex_lock(&lqe->lqe_glbl_data_lock);
407 if (lqe->lqe_glbl_data) {
408 qmt_seed_glbe_edquot(env, lqe->lqe_glbl_data);
409 qmt_id_lock_notify(qmt, lqe);
411 mutex_unlock(&lqe->lqe_glbl_data_lock);
414 if (lvb->lvb_id_rel) {
415 LQUOTA_DEBUG(lqe, "releasing:%llu may release:%llu",
416 lvb->lvb_id_rel, lvb->lvb_id_may_rel);
418 /* release quota space */
419 rc = qmt_dqacq0(env, qmt, &exp->exp_client_uuid,
420 QUOTA_DQACQ_FL_REL, lvb->lvb_id_rel,
422 qmt_dom(lqe_rtype(lqe), stype) ? -1 : idx);
423 if (rc || qti->qti_body.qb_count != lvb->lvb_id_rel)
425 "failed to release quota space on glimpse %llu!=%llu : rc = %d\n",
426 qti->qti_body.qb_count,
427 lvb->lvb_id_rel, rc);
434 class_export_put(exp);
441 * Report size of lvb to ldlm layer in order to allocate lvb buffer
442 * As far as quota locks are concerned, the size is static and is the same
443 * for both global and per-ID locks which shares the same lvb format.
445 int qmt_lvbo_size(struct lu_device *ld, struct ldlm_lock *lock)
447 return sizeof(struct lquota_lvb);
451 * Fill request buffer with quota lvb
453 int qmt_lvbo_fill(struct lu_device *ld, struct ldlm_lock *lock, void *lvb,
456 struct ldlm_resource *res = lock->l_resource;
457 struct lquota_lvb *qlvb = lvb;
462 LASSERT(res != NULL);
465 if (res->lr_type != LDLM_PLAIN || res->lr_lvb_data == NULL ||
466 res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
472 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
473 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
474 * we are thus dealing with an ID lock. */
475 struct lquota_entry *lqe = res->lr_lvb_data;
476 struct qmt_device *qmt;
477 struct obd_uuid *uuid;
480 uuid = &(lock)->l_export->exp_client_uuid;
481 rc = qmt_uuid2idx(uuid, &idx);
484 qmt = lu2qmt_dev(ld);
485 /* return current qunit value & edquot flags in lvb */
487 rc = qmt_pool_lqes_lookup(env, qmt, lqe_rtype(lqe), rc,
488 lqe_qtype(lqe), &lqe->lqe_id,
491 qlvb->lvb_id_qunit = qti_lqes_min_qunit(env);
493 if (qti_lqes_edquot(env))
494 qlvb->lvb_flags = LQUOTA_FL_EDQUOT;
497 CDEBUG(D_QUOTA, "uuid %s lqe_id %lu, edquot %llu qunit %llu\n",
498 (char *)uuid, (unsigned long)lqe->lqe_id.qid_uid,
499 qlvb->lvb_flags, qlvb->lvb_id_qunit);
502 /* global quota lock */
503 struct dt_object *obj = res->lr_lvb_data;
505 /* return current version of global index */
506 qlvb->lvb_glb_ver = dt_version_get(env, obj);
509 RETURN(rc = rc ?: sizeof(struct lquota_lvb));
513 * Free lvb associated with a given ldlm resource
514 * we don't really allocate a lvb, lr_lvb_data just points to
515 * the appropriate backend structures.
517 int qmt_lvbo_free(struct lu_device *ld, struct ldlm_resource *res)
521 if (res->lr_lvb_data == NULL)
524 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
525 struct lquota_entry *lqe = res->lr_lvb_data;
526 struct lqe_glbl_data *lgd;
528 mutex_lock(&lqe->lqe_glbl_data_lock);
529 lgd = lqe->lqe_glbl_data;
530 lqe->lqe_glbl_data = NULL;
531 mutex_unlock(&lqe->lqe_glbl_data_lock);
532 qmt_free_lqe_gd(lgd);
534 /* release lqe reference */
537 struct dt_object *obj = res->lr_lvb_data;
538 /* release object reference */
539 dt_object_put(lu_env_find(), obj);
542 res->lr_lvb_data = NULL;
548 typedef int (*qmt_glimpse_cb_t)(struct ldlm_lock *, struct lquota_entry *);
550 struct qmt_gl_lock_array {
553 struct ldlm_lock **q_locks;
556 static void qmt_free_lock_array(struct qmt_gl_lock_array *array)
560 if (array->q_max == 0) {
561 LASSERT(array->q_locks == NULL);
565 for (i = 0; i < array->q_cnt; i++) {
566 LASSERT(array->q_locks[i]);
567 LDLM_LOCK_RELEASE(array->q_locks[i]);
568 array->q_locks[i] = NULL;
571 OBD_FREE_PTR_ARRAY(array->q_locks, array->q_max);
572 array->q_locks = NULL;
576 static int qmt_alloc_lock_array(struct ldlm_resource *res,
577 struct qmt_gl_lock_array *array,
578 qmt_glimpse_cb_t cb, void *arg)
580 struct lquota_entry *lqe = arg;
581 struct list_head *pos;
582 unsigned long count = 0;
586 LASSERT(!array->q_max && !array->q_cnt && !array->q_locks);
589 mutex_lock(&lqe->lqe_glbl_data_lock);
591 /* scan list of granted locks */
592 list_for_each(pos, &res->lr_granted) {
593 struct ldlm_lock *lock;
596 lock = list_entry(pos, struct ldlm_lock, l_res_link);
597 LASSERT(lock->l_export);
601 /* slave should not be notified */
607 if (array->q_max != 0 && array->q_cnt < array->q_max) {
608 array->q_locks[array->q_cnt] = LDLM_LOCK_GET(lock);
614 mutex_unlock(&lqe->lqe_glbl_data_lock);
616 if (count > array->q_max) {
617 qmt_free_lock_array(array);
621 * allocate more slots in case of more qualified locks are
622 * found during next loop
624 array->q_max = count + count / 2 + 10;
626 LASSERT(array->q_locks == NULL && array->q_cnt == 0);
627 OBD_ALLOC_PTR_ARRAY(array->q_locks, array->q_max);
628 if (array->q_locks == NULL) {
638 void qmt_setup_id_desc(struct ldlm_lock *lock, union ldlm_gl_desc *desc,
639 struct lquota_entry *lqe)
641 struct obd_uuid *uuid = &(lock)->l_export->exp_client_uuid;
646 stype = qmt_uuid2idx(uuid, &idx);
649 /* DOM case - set global lqe settings */
650 if (qmt_dom(lqe_rtype(lqe), stype)) {
651 edquot = lqe->lqe_edquot;
652 qunit = lqe->lqe_qunit;
654 struct lqe_glbl_data *lgd;
656 mutex_lock(&lqe->lqe_glbl_data_lock);
657 lgd = lqe->lqe_glbl_data;
659 edquot = lgd->lqeg_arr[idx].lge_edquot;
660 qunit = lgd->lqeg_arr[idx].lge_qunit;
662 edquot = lqe->lqe_edquot;
663 qunit = lqe->lqe_qunit;
665 mutex_unlock(&lqe->lqe_glbl_data_lock);
668 /* fill glimpse descriptor with lqe settings */
669 desc->lquota_desc.gl_flags = edquot ? LQUOTA_FL_EDQUOT : 0;
670 desc->lquota_desc.gl_qunit = qunit;
671 CDEBUG(D_QUOTA, "setup desc: stype %d idx %d, edquot %llu qunit %llu\n",
672 stype, idx, desc->lquota_desc.gl_flags,
673 desc->lquota_desc.gl_qunit);
677 * Send glimpse callback to slaves holding a lock on resource \res.
678 * This is used to notify slaves of new quota settings or to claim quota space
681 * \param env - is the environment passed by the caller
682 * \param qmt - is the quota master target
683 * \param res - is the dlm resource associated with the quota object
684 * \param desc - is the glimpse descriptor to pack in glimpse callback
685 * \param cb - is the callback function called on every lock and determine
686 * whether a glimpse should be issued
687 * \param arg - is an opaq parameter passed to the callback function
689 static int qmt_glimpse_lock(const struct lu_env *env, struct qmt_device *qmt,
690 struct ldlm_resource *res, union ldlm_gl_desc *desc,
691 qmt_glimpse_cb_t cb, struct lquota_entry *lqe)
693 union ldlm_gl_desc *descs = NULL;
694 struct list_head *tmp, *pos;
696 struct qmt_gl_lock_array locks;
697 unsigned long i, locks_count;
701 memset(&locks, 0, sizeof(locks));
702 rc = qmt_alloc_lock_array(res, &locks, cb, lqe);
704 CERROR("%s: failed to allocate glimpse lock array (%d)\n",
705 qmt->qmt_svname, rc);
709 CDEBUG(D_QUOTA, "%s: no granted locks to send glimpse\n",
713 CDEBUG(D_QUOTA, "found granted locks %lu\n", locks.q_cnt);
714 locks_count = locks.q_cnt;
716 /* Use one desc for all works, when called from qmt_glb_lock_notify */
717 if (cb && locks.q_cnt > 1) {
718 /* TODO: think about to store this preallocated descs
719 * in lqe_global in lqeg_arr as a part of lqe_glbl_entry.
720 * The benefit is that we don't need to allocate/free
721 * and setup this descs each time. But the drawback is
722 * memory use (sizeof ldlm_gl_desc * OST_COUNT * user_number).
723 * for examfple it could be 88 * 256 * 10 000 about 225 MB. */
725 sizeof(struct ldlm_gl_lquota_desc) * locks.q_cnt);
727 CERROR("%s: alloc glimpse lock array failed: rc = %d\n",
728 qmt->qmt_svname, rc);
729 qmt_free_lock_array(&locks);
734 for (i = locks.q_cnt; i > 0; i--) {
735 struct ldlm_glimpse_work *work;
739 CERROR("%s: failed to notify a lock.\n",
746 desc = &descs[i - 1];
747 qmt_setup_id_desc(locks.q_locks[i - 1], desc, lqe);
748 work->gl_interpret_data = lqe;
751 list_add_tail(&work->gl_list, &gl_list);
752 work->gl_lock = locks.q_locks[i - 1];
754 work->gl_desc = desc;
756 locks.q_locks[i - 1] = NULL;
760 qmt_free_lock_array(&locks);
762 if (list_empty(&gl_list)) {
763 CDEBUG(D_QUOTA, "%s: nobody to notify\n", qmt->qmt_svname);
767 /* issue glimpse callbacks to all connected slaves */
768 rc = ldlm_glimpse_locks(res, &gl_list);
770 list_for_each_safe(pos, tmp, &gl_list) {
771 struct ldlm_glimpse_work *work;
773 work = list_entry(pos, struct ldlm_glimpse_work, gl_list);
775 list_del(&work->gl_list);
776 CERROR("%s: failed to notify %s of new quota settings\n",
778 obd_uuid2str(&work->gl_lock->l_export->exp_client_uuid));
779 LDLM_LOCK_RELEASE(work->gl_lock);
785 sizeof(struct ldlm_gl_lquota_desc) * locks_count);
791 * Send glimpse request to all global quota locks to push new quota setting to
794 * \param env - is the environment passed by the caller
795 * \param lqe - is the lquota entry which has new settings
796 * \param ver - is the version associated with the setting change
798 void qmt_glb_lock_notify(const struct lu_env *env, struct lquota_entry *lqe,
801 struct qmt_thread_info *qti = qmt_info(env);
802 struct qmt_pool_info *pool = lqe2qpi(lqe);
803 struct ldlm_resource *res = NULL;
806 lquota_generate_fid(&qti->qti_fid, pool->qpi_rtype, lqe_qtype(lqe));
808 /* send glimpse callback to notify slaves of new quota settings */
809 qti->qti_gl_desc.lquota_desc.gl_id = lqe->lqe_id;
810 qti->qti_gl_desc.lquota_desc.gl_flags = 0;
811 if (lqe->lqe_is_default) {
812 qti->qti_gl_desc.lquota_desc.gl_hardlimit = 0;
813 qti->qti_gl_desc.lquota_desc.gl_softlimit = 0;
814 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
815 LQUOTA_FLAG_DEFAULT);
817 } else if (lqe->lqe_is_deleted) {
818 qti->qti_gl_desc.lquota_desc.gl_hardlimit = 0;
819 qti->qti_gl_desc.lquota_desc.gl_softlimit = 0;
820 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
821 LQUOTA_FLAG_DELETED);
822 } else if (lqe->lqe_is_reset) {
823 qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
824 qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
825 qti->qti_gl_desc.lquota_desc.gl_time = LQUOTA_GRACE_FLAG(0,
828 qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
829 qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
830 qti->qti_gl_desc.lquota_desc.gl_time = lqe->lqe_gracetime;
832 qti->qti_gl_desc.lquota_desc.gl_ver = ver;
834 /* look up ldlm resource associated with global index */
835 fid_build_reg_res_name(&qti->qti_fid, &qti->qti_resid);
836 res = ldlm_resource_get(pool->qpi_qmt->qmt_ns, &qti->qti_resid,
839 /* this might happen if no slaves have enqueued global quota
841 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource associated "
842 "with "DFID, PFID(&qti->qti_fid));
846 qmt_glimpse_lock(env, pool->qpi_qmt, res, &qti->qti_gl_desc,
848 ldlm_resource_putref(res);
852 /* Callback function used to select locks that should be glimpsed when
853 * broadcasting the new qunit value */
854 static int qmt_id_lock_cb(struct ldlm_lock *lock, struct lquota_entry *lqe)
856 struct obd_uuid *uuid = &(lock)->l_export->exp_client_uuid;
857 struct lqe_glbl_data *lgd = lqe->lqe_glbl_data;
859 int stype = qmt_uuid2idx(uuid, &idx);
861 LASSERT(stype == QMT_STYPE_OST || stype == QMT_STYPE_MDT);
863 /* Quota pools support only OSTs, despite MDTs also could be registered
864 * as LQUOTA_RES_DT devices(DOM). */
865 if (qmt_dom(lqe_rtype(lqe), stype))
868 return lgd ? lgd->lqeg_arr[idx].lge_edquot_nu ||
869 lgd->lqeg_arr[idx].lge_qunit_nu : 0;
874 * Send glimpse request on per-ID lock to push new qunit value to slave.
876 * \param env - is the environment passed by the caller
877 * \param qmt - is the quota master target device
878 * \param lqe - is the lquota entry with the new qunit value
879 * \param uuid - is the uuid of the slave acquiring space, if any
881 static void qmt_id_lock_glimpse(const struct lu_env *env,
882 struct qmt_device *qmt,
883 struct lquota_entry *lqe, struct obd_uuid *uuid)
885 struct qmt_thread_info *qti = qmt_info(env);
886 struct qmt_pool_info *pool = lqe2qpi(lqe);
887 struct ldlm_resource *res = NULL;
890 if (!lqe->lqe_enforced)
893 lquota_generate_fid(&qti->qti_fid, pool->qpi_rtype, lqe_qtype(lqe));
894 fid_build_quota_res_name(&qti->qti_fid, &lqe->lqe_id, &qti->qti_resid);
895 res = ldlm_resource_get(qmt->qmt_ns, &qti->qti_resid, LDLM_PLAIN, 0);
897 /* this might legitimately happens if slaves haven't had the
898 * opportunity to enqueue quota lock yet. */
899 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource for per-ID "
900 "lock "DFID, PFID(&qti->qti_fid));
902 if (lqe->lqe_revoke_time == 0 &&
903 lqe->lqe_qunit == pool->qpi_least_qunit)
904 lqe->lqe_revoke_time = ktime_get_seconds();
905 lqe_write_unlock(lqe);
910 /* The purpose of glimpse callback on per-ID lock is twofold:
911 * - notify slaves of new qunit value and hope they will release some
912 * spare quota space in return
913 * - notify slaves that master ran out of quota space and there is no
914 * need to send acquire request any more until further notice */
916 /* TODO: it is not clear how to implement below case for all lqes
917 * from where slaves will be notified in qmt_glimpse_lock. Because
918 * here we have just global lqe with an array of OSTs that should
919 * be notified. Theoretically we can find all lqes that includes
920 * these OSTs, but it is not trivial. So I would propose to move
921 * this case to another place ... */
922 if (lqe->lqe_revoke_time == 0 &&
923 lqe->lqe_qunit == pool->qpi_least_qunit)
924 /* reset lqe_may_rel, it will be updated on glimpse callback
925 * replies if needed */
926 lqe->lqe_may_rel = 0;
928 /* The rebalance thread is the only thread which can issue glimpses */
929 LASSERT(!lqe->lqe_gl);
931 lqe_write_unlock(lqe);
933 /* issue glimpse callback to slaves */
934 if (lqe->lqe_glbl_data)
935 qmt_glimpse_lock(env, qmt, res, &qti->qti_gl_desc,
936 qmt_id_lock_cb, lqe);
939 if (lqe->lqe_revoke_time == 0 &&
940 lqe->lqe_qunit == pool->qpi_least_qunit) {
941 lqe->lqe_revoke_time = ktime_get_seconds();
942 qmt_adjust_edquot(lqe, ktime_get_real_seconds());
944 LASSERT(lqe->lqe_gl);
946 lqe_write_unlock(lqe);
948 ldlm_resource_putref(res);
953 * Schedule a glimpse request on per-ID locks to push new qunit value or
954 * edquot flag to quota slaves.
956 * \param qmt - is the quota master target device
957 * \param lqe - is the lquota entry with the new qunit value
959 void qmt_id_lock_notify(struct qmt_device *qmt, struct lquota_entry *lqe)
964 LASSERT(lqe->lqe_is_global);
966 spin_lock(&qmt->qmt_reba_lock);
967 if (!qmt->qmt_stopping && list_empty(&lqe->lqe_link)) {
968 list_add_tail(&lqe->lqe_link, &qmt->qmt_reba_list);
970 if (qmt->qmt_reba_task)
971 wake_up_process(qmt->qmt_reba_task);
973 spin_unlock(&qmt->qmt_reba_lock);
980 struct qmt_reba_args {
981 struct qmt_device *qra_dev;
982 struct lu_env qra_env;
983 struct completion *qra_started;
987 #define TASK_IDLE TASK_INTERRUPTIBLE
991 * The rebalance thread is in charge of sending glimpse callbacks on per-ID
992 * quota locks owned by slaves in order to notify them of:
993 * - a qunit shrink in which case slaves might release quota space back in
995 * - set/clear edquot flag used to cache the "quota exhausted" state of the
996 * master. When the flag is set, slaves know that there is no need to
997 * try to acquire quota from the master since this latter has already
998 * distributed all the space.
1000 static int qmt_reba_thread(void *_args)
1002 struct qmt_reba_args *args = _args;
1003 struct qmt_device *qmt = args->qra_dev;
1004 struct lu_env *env = &args->qra_env;
1005 struct lquota_entry *lqe, *tmp;
1008 complete(args->qra_started);
1009 while (({set_current_state(TASK_IDLE);
1010 !kthread_should_stop(); })) {
1012 spin_lock(&qmt->qmt_reba_lock);
1013 list_for_each_entry_safe(lqe, tmp, &qmt->qmt_reba_list,
1015 __set_current_state(TASK_RUNNING);
1016 list_del_init(&lqe->lqe_link);
1017 spin_unlock(&qmt->qmt_reba_lock);
1019 /* lqe_ref == 1 means we hold the last ref,
1020 * so no need to send glimpse callbacks.
1022 if (!kthread_should_stop() &&
1023 atomic_read(&lqe->lqe_ref) > 1)
1024 qmt_id_lock_glimpse(env, qmt, lqe, NULL);
1027 spin_lock(&qmt->qmt_reba_lock);
1029 spin_unlock(&qmt->qmt_reba_lock);
1032 __set_current_state(TASK_RUNNING);
1041 * Start rebalance thread. Called when the QMT is being setup
1043 int qmt_start_reba_thread(struct qmt_device *qmt)
1045 struct task_struct *task;
1046 struct qmt_reba_args *args;
1047 DECLARE_COMPLETION_ONSTACK(started);
1051 OBD_ALLOC_PTR(args);
1054 args->qra_dev = qmt;
1055 args->qra_started = &started;
1057 rc = lu_env_init(&args->qra_env, LCT_MD_THREAD);
1059 CERROR("%s: failed to init env.\n", qmt->qmt_svname);
1063 task = kthread_create(qmt_reba_thread, args,
1064 "qmt_reba_%s", qmt->qmt_svname);
1066 CERROR("%s: failed to start rebalance thread (%ld)\n",
1067 qmt->qmt_svname, PTR_ERR(task));
1068 GOTO(out_env_fini, rc = PTR_ERR(task));
1071 rc = lu_env_add_task(&args->qra_env, task);
1074 GOTO(out_env_fini, rc);
1076 qmt->qmt_reba_task = task;
1077 wake_up_process(task);
1078 wait_for_completion(&started);
1082 lu_env_fini(&args->qra_env);
1089 * Stop rebalance thread. Called when the QMT is about to shutdown.
1091 void qmt_stop_reba_thread(struct qmt_device *qmt)
1093 struct task_struct *task;
1095 spin_lock(&qmt->qmt_reba_lock);
1096 task = qmt->qmt_reba_task;
1097 qmt->qmt_reba_task = NULL;
1098 spin_unlock(&qmt->qmt_reba_lock);
1103 LASSERT(list_empty(&qmt->qmt_reba_list));