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, 2013, 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>
32 # define EXPORT_SYMTAB
35 #define DEBUG_SUBSYSTEM S_LQUOTA
37 #include <lustre_dlm.h>
38 #include <obd_class.h>
40 #include "qmt_internal.h"
42 /* intent policy function called from mdt_intent_opc() when the intent is of
44 int qmt_intent_policy(const struct lu_env *env, struct lu_device *ld,
45 struct ptlrpc_request *req, struct ldlm_lock **lockp,
48 struct qmt_device *qmt = lu2qmt_dev(ld);
49 struct ldlm_intent *it;
50 struct quota_body *reqbody;
51 struct quota_body *repbody;
52 struct obd_uuid *uuid;
53 struct lquota_lvb *lvb;
54 struct ldlm_resource *res = (*lockp)->l_resource;
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 uuid = &(*lockp)->l_export->exp_client_uuid;
85 case IT_QUOTA_DQACQ: {
86 struct lquota_entry *lqe;
87 struct ldlm_lock *lock;
89 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
90 /* acquire on global lock? something is wrong ... */
91 GOTO(out, rc = -EPROTO);
93 /* verify global lock isn't stale */
94 if (!lustre_handle_is_used(&reqbody->qb_glb_lockh))
95 GOTO(out, rc = -ENOLCK);
97 lock = ldlm_handle2lock(&reqbody->qb_glb_lockh);
99 GOTO(out, rc = -ENOLCK);
102 lqe = res->lr_lvb_data;
103 LASSERT(lqe != NULL);
106 /* acquire quota space */
107 rc = qmt_dqacq0(env, lqe, qmt, uuid, reqbody->qb_flags,
108 reqbody->qb_count, reqbody->qb_usage,
117 /* new connection from slave */
119 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0)
120 /* connection on per-ID lock? something is wrong ... */
121 GOTO(out, rc = -EPROTO);
123 rc = qmt_pool_new_conn(env, qmt, &reqbody->qb_fid,
124 &repbody->qb_slv_fid,
125 &repbody->qb_slv_ver, uuid);
131 CERROR("%s: invalid intent opcode: "LPU64"\n", qmt->qmt_svname,
133 GOTO(out, rc = err_serious(-EINVAL));
136 /* on success, pack lvb in reply */
137 lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
138 lvb_len = ldlm_lvbo_size(*lockp);
139 lvb_len = ldlm_lvbo_fill(*lockp, lvb, lvb_len);
141 GOTO(out, rc = lvb_len);
143 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB, lvb_len, RCL_SERVER);
150 * Initialize quota LVB associated with quota indexes.
151 * Called with res->lr_lvb_sem held
153 int qmt_lvbo_init(struct lu_device *ld, struct ldlm_resource *res)
156 struct qmt_thread_info *qti;
157 struct qmt_device *qmt = lu2qmt_dev(ld);
158 int pool_id, pool_type, qtype;
162 LASSERT(res != NULL);
164 if (res->lr_type != LDLM_PLAIN)
167 if (res->lr_lvb_data ||
168 res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
175 /* initialize environment */
176 rc = lu_env_init(env, LCT_MD_THREAD);
183 /* extract global index FID and quota identifier */
184 fid_extract_quota_resid(&res->lr_name, &qti->qti_fid, &qti->qti_id);
186 /* sanity check the global index FID */
187 rc = lquota_extract_fid(&qti->qti_fid, &pool_id, &pool_type, &qtype);
189 CERROR("can't extract pool information from FID "DFID"\n",
190 PFID(&qti->qti_fid));
194 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
195 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
196 * we are thus dealing with an ID lock. */
197 struct lquota_entry *lqe;
199 /* Find the quota entry associated with the quota id */
200 lqe = qmt_pool_lqe_lookup(env, qmt, pool_id, pool_type, qtype,
203 GOTO(out, rc = PTR_ERR(lqe));
205 /* store reference to lqe in lr_lvb_data */
206 res->lr_lvb_data = lqe;
207 LQUOTA_DEBUG(lqe, "initialized res lvb");
209 struct dt_object *obj;
211 /* lookup global index */
212 obj = dt_locate(env, qmt->qmt_child, &qti->qti_fid);
214 GOTO(out, rc = PTR_ERR(obj));
215 if (!dt_object_exists(obj)) {
216 lu_object_put(env, &obj->do_lu);
217 GOTO(out, rc = -ENOENT);
220 /* store reference to global index object in lr_lvb_data */
221 res->lr_lvb_data = obj;
222 CDEBUG(D_QUOTA, DFID" initialized lvb\n", PFID(&qti->qti_fid));
225 res->lr_lvb_len = sizeof(struct lquota_lvb);
234 * Update LVB associated with the global quota index.
235 * This function is called from the DLM itself after a glimpse callback, in this
236 * case valid ptlrpc request is passed.
238 int qmt_lvbo_update(struct lu_device *ld, struct ldlm_resource *res,
239 struct ptlrpc_request *req, int increase_only)
242 struct qmt_thread_info *qti;
243 struct qmt_device *qmt = lu2qmt_dev(ld);
244 struct lquota_entry *lqe;
245 struct lquota_lvb *lvb;
246 struct ldlm_lock *lock;
247 struct obd_export *exp;
251 LASSERT(res != NULL);
256 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] == 0)
257 /* no need to update lvb for global quota locks */
260 lvb = req_capsule_server_swab_get(&req->rq_pill, &RMF_DLM_LVB,
261 lustre_swab_lquota_lvb);
263 CERROR("%s: failed to extract lvb from request\n",
268 lqe = res->lr_lvb_data;
269 LASSERT(lqe != NULL);
272 LQUOTA_DEBUG(lqe, "releasing:"LPU64" may release:"LPU64,
273 lvb->lvb_id_rel, lvb->lvb_id_may_rel);
275 if (lvb->lvb_id_rel == 0) {
276 /* nothing to release */
277 if (lvb->lvb_id_may_rel != 0)
278 /* but might still release later ... */
279 lqe->lqe_may_rel += lvb->lvb_id_may_rel;
280 GOTO(out_lqe, rc = 0);
283 /* allocate environement */
286 GOTO(out_lqe, rc = -ENOMEM);
288 /* initialize environment */
289 rc = lu_env_init(env, LCT_MD_THREAD);
294 /* The request is a glimpse callback which was sent via the
295 * reverse import to the slave. What we care about here is the
296 * export associated with the slave and req->rq_export is
297 * definitely not what we are looking for (it is actually set to
299 * Therefore we extract the lock from the request argument
300 * and use lock->l_export. */
301 lock = ldlm_request_lock(req);
303 CERROR("%s: failed to get lock from request!\n",
305 GOTO(out_env_init, rc = PTR_ERR(lock));
308 exp = class_export_get(lock->l_export);
310 CERROR("%s: failed to get export from lock!\n",
312 GOTO(out_env_init, rc = -EFAULT);
315 /* release quota space */
316 rc = qmt_dqacq0(env, lqe, qmt, &exp->exp_client_uuid,
317 QUOTA_DQACQ_FL_REL, lvb->lvb_id_rel, 0, &qti->qti_body);
318 if (rc || qti->qti_body.qb_count != lvb->lvb_id_rel)
319 LQUOTA_ERROR(lqe, "failed to release quota space on glimpse "
320 LPU64"!="LPU64" rc:%d\n", qti->qti_body.qb_count,
321 lvb->lvb_id_rel, rc);
322 class_export_put(exp);
324 GOTO(out_env_init, rc);
336 * Report size of lvb to ldlm layer in order to allocate lvb buffer
337 * As far as quota locks are concerned, the size is static and is the same
338 * for both global and per-ID locks which shares the same lvb format.
340 int qmt_lvbo_size(struct lu_device *ld, struct ldlm_lock *lock)
342 return sizeof(struct lquota_lvb);
346 * Fill request buffer with quota lvb
348 int qmt_lvbo_fill(struct lu_device *ld, struct ldlm_lock *lock, void *lvb,
351 struct ldlm_resource *res = lock->l_resource;
352 struct lquota_lvb *qlvb = lvb;
355 LASSERT(res != NULL);
357 if (res->lr_type != LDLM_PLAIN || res->lr_lvb_data == NULL ||
358 res->lr_name.name[LUSTRE_RES_ID_SEQ_OFF] != FID_SEQ_QUOTA_GLB)
361 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
362 /* no ID quota lock associated with UID/GID 0 or with a seq 0,
363 * we are thus dealing with an ID lock. */
364 struct lquota_entry *lqe = res->lr_lvb_data;
366 /* return current qunit value & edquot flags in lvb */
368 qlvb->lvb_id_qunit = lqe->lqe_qunit;
371 qlvb->lvb_flags = LQUOTA_FL_EDQUOT;
374 /* global quota lock */
377 struct dt_object *obj = res->lr_lvb_data;
383 /* initialize environment */
384 rc = lu_env_init(env, LCT_LOCAL);
390 /* return current version of global index */
391 qlvb->lvb_glb_ver = dt_version_get(env, obj);
397 RETURN(sizeof(struct lquota_lvb));
401 * Free lvb associated with a given ldlm resource
402 * we don't really allocate a lvb, lr_lvb_data just points to
403 * the appropriate backend structures.
405 int qmt_lvbo_free(struct lu_device *ld, struct ldlm_resource *res)
409 if (res->lr_lvb_data == NULL)
412 if (res->lr_name.name[LUSTRE_RES_ID_QUOTA_SEQ_OFF] != 0) {
413 struct lquota_entry *lqe = res->lr_lvb_data;
415 /* release lqe reference */
418 struct dt_object *obj = res->lr_lvb_data;
426 /* initialize environment */
427 rc = lu_env_init(env, LCT_LOCAL);
433 /* release object reference */
434 lu_object_put(env, &obj->do_lu);
439 res->lr_lvb_data = NULL;
445 typedef int (*qmt_glimpse_cb_t)(const struct lu_env *, struct qmt_device *,
446 struct obd_uuid *, union ldlm_gl_desc *,
449 * Send glimpse callback to slaves holding a lock on resource \res.
450 * This is used to notify slaves of new quota settings or to claim quota space
453 * \param env - is the environment passed by the caller
454 * \param qmt - is the quota master target
455 * \param res - is the dlm resource associated with the quota object
456 * \param desc - is the glimpse descriptor to pack in glimpse callback
457 * \param cb - is the callback function called on every lock and determine
458 * whether a glimpse should be issued
459 * \param arg - is an opaq parameter passed to the callback function
461 static int qmt_glimpse_lock(const struct lu_env *env, struct qmt_device *qmt,
462 struct ldlm_resource *res, union ldlm_gl_desc *desc,
463 qmt_glimpse_cb_t cb, void *arg)
465 cfs_list_t *tmp, *pos;
466 CFS_LIST_HEAD(gl_list);
471 /* scan list of granted locks */
472 cfs_list_for_each(pos, &res->lr_granted) {
473 struct ldlm_glimpse_work *work;
474 struct ldlm_lock *lock;
475 struct obd_uuid *uuid;
477 lock = cfs_list_entry(pos, struct ldlm_lock, l_res_link);
478 LASSERT(lock->l_export);
479 uuid = &lock->l_export->exp_client_uuid;
482 rc = cb(env, qmt, uuid, desc, arg);
484 /* slave should not be notified */
487 /* something wrong happened, we still notify */
488 CERROR("%s: callback function failed to "
489 "determine whether slave %s should be "
490 "notified (%d)\n", qmt->qmt_svname,
491 obd_uuid2str(uuid), rc);
496 CERROR("%s: failed to notify %s\n", qmt->qmt_svname,
501 cfs_list_add_tail(&work->gl_list, &gl_list);
502 work->gl_lock = LDLM_LOCK_GET(lock);
504 work->gl_desc = desc;
509 if (cfs_list_empty(&gl_list)) {
510 CDEBUG(D_QUOTA, "%s: nobody to notify\n", qmt->qmt_svname);
514 /* issue glimpse callbacks to all connected slaves */
515 rc = ldlm_glimpse_locks(res, &gl_list);
517 cfs_list_for_each_safe(pos, tmp, &gl_list) {
518 struct ldlm_glimpse_work *work;
520 work = cfs_list_entry(pos, struct ldlm_glimpse_work, gl_list);
522 cfs_list_del(&work->gl_list);
523 CERROR("%s: failed to notify %s of new quota settings\n",
525 obd_uuid2str(&work->gl_lock->l_export->exp_client_uuid));
526 LDLM_LOCK_RELEASE(work->gl_lock);
534 * Send glimpse request to all global quota locks to push new quota setting to
537 * \param env - is the environment passed by the caller
538 * \param lqe - is the lquota entry which has new settings
539 * \param ver - is the version associated with the setting change
541 void qmt_glb_lock_notify(const struct lu_env *env, struct lquota_entry *lqe,
544 struct qmt_thread_info *qti = qmt_info(env);
545 struct qmt_pool_info *pool = lqe2qpi(lqe);
546 struct ldlm_resource *res = NULL;
550 lquota_generate_fid(&qti->qti_fid, pool->qpi_key & 0x0000ffff,
551 pool->qpi_key >> 16, lqe->lqe_site->lqs_qtype);
553 /* send glimpse callback to notify slaves of new quota settings */
554 qti->qti_gl_desc.lquota_desc.gl_id = lqe->lqe_id;
555 qti->qti_gl_desc.lquota_desc.gl_flags = 0;
556 qti->qti_gl_desc.lquota_desc.gl_hardlimit = lqe->lqe_hardlimit;
557 qti->qti_gl_desc.lquota_desc.gl_softlimit = lqe->lqe_softlimit;
558 qti->qti_gl_desc.lquota_desc.gl_time = lqe->lqe_gracetime;
559 qti->qti_gl_desc.lquota_desc.gl_ver = ver;
561 /* look up ldlm resource associated with global index */
562 fid_build_reg_res_name(&qti->qti_fid, &qti->qti_resid);
563 res = ldlm_resource_get(pool->qpi_qmt->qmt_ns, NULL, &qti->qti_resid,
566 /* this might happen if no slaves have enqueued global quota
568 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource associated "
569 "with "DFID, PFID(&qti->qti_fid));
573 rc = qmt_glimpse_lock(env, pool->qpi_qmt, res, &qti->qti_gl_desc,
575 ldlm_resource_putref(res);
579 /* Callback function used to select locks that should be glimpsed when
580 * broadcasting the new qunit value */
581 static int qmt_id_lock_cb(const struct lu_env *env, struct qmt_device *qmt,
582 struct obd_uuid *uuid, union ldlm_gl_desc *desc,
585 struct obd_uuid *slv_uuid = arg;
587 if (slv_uuid != NULL && obd_uuid_equals(uuid, slv_uuid))
593 * Send glimpse request on per-ID lock to push new qunit value to slave.
595 * \param env - is the environment passed by the caller
596 * \param qmt - is the quota master target device
597 * \param lqe - is the lquota entry with the new qunit value
598 * \param uuid - is the uuid of the slave acquiring space, if any
600 static void qmt_id_lock_glimpse(const struct lu_env *env,
601 struct qmt_device *qmt,
602 struct lquota_entry *lqe, struct obd_uuid *uuid)
604 struct qmt_thread_info *qti = qmt_info(env);
605 struct qmt_pool_info *pool = lqe2qpi(lqe);
606 struct ldlm_resource *res = NULL;
610 if (!lqe->lqe_enforced)
613 lquota_generate_fid(&qti->qti_fid, pool->qpi_key & 0x0000ffff,
614 pool->qpi_key >> 16, lqe->lqe_site->lqs_qtype);
615 fid_build_quota_resid(&qti->qti_fid, &lqe->lqe_id, &qti->qti_resid);
616 res = ldlm_resource_get(qmt->qmt_ns, NULL, &qti->qti_resid, LDLM_PLAIN,
619 /* this might legitimately happens if slaves haven't had the
620 * opportunity to enqueue quota lock yet. */
621 LQUOTA_DEBUG(lqe, "failed to lookup ldlm resource for per-ID "
622 "lock "DFID, PFID(&qti->qti_fid));
624 if (lqe->lqe_revoke_time == 0 &&
625 lqe->lqe_qunit == pool->qpi_least_qunit)
626 lqe->lqe_revoke_time = cfs_time_current_64();
627 lqe_write_unlock(lqe);
632 /* The purpose of glimpse callback on per-ID lock is twofold:
633 * - notify slaves of new qunit value and hope they will release some
634 * spare quota space in return
635 * - notify slaves that master ran out of quota space and there is no
636 * need to send acquire request any more until further notice */
638 /* fill glimpse descriptor with lqe settings */
640 qti->qti_gl_desc.lquota_desc.gl_flags = LQUOTA_FL_EDQUOT;
642 qti->qti_gl_desc.lquota_desc.gl_flags = 0;
643 qti->qti_gl_desc.lquota_desc.gl_qunit = lqe->lqe_qunit;
645 if (lqe->lqe_revoke_time == 0 &&
646 qti->qti_gl_desc.lquota_desc.gl_qunit == pool->qpi_least_qunit)
647 /* reset lqe_may_rel, it will be updated on glimpse callback
648 * replies if needed */
649 lqe->lqe_may_rel = 0;
651 /* The rebalance thread is the only thread which can issue glimpses */
652 LASSERT(!lqe->lqe_gl);
654 lqe_write_unlock(lqe);
656 /* issue glimpse callback to slaves */
657 rc = qmt_glimpse_lock(env, qmt, res, &qti->qti_gl_desc,
658 uuid ? qmt_id_lock_cb : NULL, (void *)uuid);
661 if (lqe->lqe_revoke_time == 0 &&
662 qti->qti_gl_desc.lquota_desc.gl_qunit == pool->qpi_least_qunit &&
663 lqe->lqe_qunit == pool->qpi_least_qunit) {
664 lqe->lqe_revoke_time = cfs_time_current_64();
665 qmt_adjust_edquot(lqe, cfs_time_current_sec());
667 LASSERT(lqe->lqe_gl);
669 lqe_write_unlock(lqe);
671 ldlm_resource_putref(res);
676 * Schedule a glimpse request on per-ID locks to push new qunit value or
677 * edquot flag to quota slaves.
679 * \param qmt - is the quota master target device
680 * \param lqe - is the lquota entry with the new qunit value
682 void qmt_id_lock_notify(struct qmt_device *qmt, struct lquota_entry *lqe)
688 spin_lock(&qmt->qmt_reba_lock);
689 if (!qmt->qmt_stopping && cfs_list_empty(&lqe->lqe_link)) {
690 cfs_list_add_tail(&lqe->lqe_link, &qmt->qmt_reba_list);
693 spin_unlock(&qmt->qmt_reba_lock);
696 cfs_waitq_signal(&qmt->qmt_reba_thread.t_ctl_waitq);
703 * The rebalance thread is in charge of sending glimpse callbacks on per-ID
704 * quota locks owned by slaves in order to notify them of:
705 * - a qunit shrink in which case slaves might release quota space back in
707 * - set/clear edquot flag used to cache the "quota exhausted" state of the
708 * master. When the flag is set, slaves know that there is no need to
709 * try to acquire quota from the master since this latter has already
710 * distributed all the space.
712 static int qmt_reba_thread(void *arg)
714 struct qmt_device *qmt = (struct qmt_device *)arg;
715 struct ptlrpc_thread *thread = &qmt->qmt_reba_thread;
716 struct l_wait_info lwi = { 0 };
718 struct lquota_entry *lqe, *tmp;
719 char pname[MTI_NAME_MAXLEN];
727 rc = lu_env_init(env, LCT_MD_THREAD);
729 CERROR("%s: failed to init env.", qmt->qmt_svname);
734 snprintf(pname, MTI_NAME_MAXLEN, "qmt_reba_%s", qmt->qmt_svname);
735 cfs_daemonize(pname);
737 thread_set_flags(thread, SVC_RUNNING);
738 cfs_waitq_signal(&thread->t_ctl_waitq);
741 l_wait_event(thread->t_ctl_waitq,
742 !cfs_list_empty(&qmt->qmt_reba_list) ||
743 !thread_is_running(thread), &lwi);
745 spin_lock(&qmt->qmt_reba_lock);
746 cfs_list_for_each_entry_safe(lqe, tmp, &qmt->qmt_reba_list,
748 cfs_list_del_init(&lqe->lqe_link);
749 spin_unlock(&qmt->qmt_reba_lock);
751 if (thread_is_running(thread))
752 qmt_id_lock_glimpse(env, qmt, lqe, NULL);
755 spin_lock(&qmt->qmt_reba_lock);
757 spin_unlock(&qmt->qmt_reba_lock);
759 if (!thread_is_running(thread))
764 thread_set_flags(thread, SVC_STOPPED);
765 cfs_waitq_signal(&thread->t_ctl_waitq);
770 * Start rebalance thread. Called when the QMT is being setup
772 int qmt_start_reba_thread(struct qmt_device *qmt)
774 struct ptlrpc_thread *thread = &qmt->qmt_reba_thread;
775 struct l_wait_info lwi = { 0 };
779 rc = cfs_create_thread(qmt_reba_thread, (void *)qmt, 0);
781 CERROR("%s: failed to start rebalance thread (%d)\n",
782 qmt->qmt_svname, rc);
783 thread_set_flags(thread, SVC_STOPPED);
787 l_wait_event(thread->t_ctl_waitq,
788 thread_is_running(thread) || thread_is_stopped(thread),
795 * Stop rebalance thread. Called when the QMT is about to shutdown.
797 void qmt_stop_reba_thread(struct qmt_device *qmt)
799 struct ptlrpc_thread *thread = &qmt->qmt_reba_thread;
801 if (!thread_is_stopped(thread)) {
802 struct l_wait_info lwi = { 0 };
804 thread_set_flags(thread, SVC_STOPPING);
805 cfs_waitq_signal(&thread->t_ctl_waitq);
807 l_wait_event(thread->t_ctl_waitq, thread_is_stopped(thread),
810 LASSERT(cfs_list_empty(&qmt->qmt_reba_list));