1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 # define EXPORT_SYMTAB
43 #define DEBUG_SUBSYSTEM S_LQUOTA
46 # include <linux/version.h>
47 # include <linux/module.h>
48 # include <linux/init.h>
49 # include <linux/fs.h>
50 # include <linux/jbd.h>
51 # include <linux/smp_lock.h>
52 # include <linux/buffer_head.h>
53 # include <linux/workqueue.h>
54 # include <linux/mount.h>
55 #else /* __KERNEL__ */
56 # include <liblustre.h>
59 #include <obd_class.h>
60 #include <lustre_mds.h>
61 #include <lustre_dlm.h>
62 #include <lustre_cfg.h>
64 #include <lustre_fsfilt.h>
65 #include <lustre_quota.h>
66 #include <lprocfs_status.h>
67 #include "quota_internal.h"
71 #ifdef HAVE_QUOTA_SUPPORT
73 static cfs_time_t last_print = 0;
74 static cfs_spinlock_t last_print_lock = CFS_SPIN_LOCK_UNLOCKED;
76 static int filter_quota_setup(struct obd_device *obd)
79 struct obd_device_target *obt = &obd->u.obt;
82 cfs_init_rwsem(&obt->obt_rwsem);
83 obt->obt_qfmt = LUSTRE_QUOTA_V2;
84 cfs_sema_init(&obt->obt_quotachecking, 1);
85 rc = qctxt_init(obd, NULL);
87 CERROR("initialize quota context failed! (rc:%d)\n", rc);
92 static int filter_quota_cleanup(struct obd_device *obd)
95 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
99 static int filter_quota_setinfo(struct obd_device *obd, void *data)
101 struct obd_export *exp = data;
102 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
103 struct obd_import *imp = exp->exp_imp_reverse;
106 LASSERT(imp != NULL);
108 /* setup the quota context import */
109 cfs_spin_lock(&qctxt->lqc_lock);
110 if (qctxt->lqc_import != NULL) {
111 cfs_spin_unlock(&qctxt->lqc_lock);
112 if (qctxt->lqc_import == imp)
113 CDEBUG(D_WARNING, "%s: lqc_import(%p) of obd(%p) was "
114 "activated already.\n", obd->obd_name, imp, obd);
116 CERROR("%s: lqc_import(%p:%p) of obd(%p) was "
117 "activated by others.\n", obd->obd_name,
118 qctxt->lqc_import, imp, obd);
120 qctxt->lqc_import = imp;
121 /* make imp's connect flags equal relative exp's connect flags
122 * adding it to avoid the scan export list */
123 imp->imp_connect_data.ocd_connect_flags |=
124 (exp->exp_connect_flags &
125 (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
126 cfs_spin_unlock(&qctxt->lqc_lock);
127 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated "
128 "now.\n", obd->obd_name, imp, obd);
130 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
131 /* start quota slave recovery thread. (release high limits) */
132 qslave_start_recovery(obd, qctxt);
137 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
139 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
140 struct obd_import *imp = exp->exp_imp_reverse;
143 /* lquota may be not set up before destroying export, b=14896 */
144 if (!obd->obd_set_up)
147 if (unlikely(imp == NULL))
150 /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
151 * should be invalid b=12374 */
152 cfs_spin_lock(&qctxt->lqc_lock);
153 if (qctxt->lqc_import == imp) {
154 qctxt->lqc_import = NULL;
155 cfs_spin_unlock(&qctxt->lqc_lock);
156 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is invalid now.\n",
157 obd->obd_name, imp, obd);
158 ptlrpc_cleanup_imp(imp);
159 dqacq_interrupt(qctxt);
161 cfs_spin_unlock(&qctxt->lqc_lock);
166 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
170 if (!ll_sb_any_quota_active(obd->u.obt.obt_sb))
174 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
175 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
177 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
183 #define GET_OA_ID(flag, oa) (flag == USRQUOTA ? oa->o_uid : oa->o_gid)
184 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
186 struct obd_device_target *obt = &obd->u.obt;
187 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
188 int err, cnt, rc = 0;
189 struct obd_quotactl *oqctl;
192 if (!ll_sb_any_quota_active(obt->obt_sb))
195 OBD_ALLOC_PTR(oqctl);
199 /* set over quota flags for a uid/gid */
200 oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
201 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
203 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
204 struct lustre_qunit_size *lqs = NULL;
206 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
210 CDEBUG(D_QUOTA, "search lqs for %s %d failed, "
212 cnt == USRQUOTA ? "user" : "group",
213 GET_OA_ID(cnt, oa), rc);
215 } else if (lqs == NULL) {
216 /* continue to check group quota if the file's owner
217 * doesn't have quota limit. LU-530 */
220 cfs_spin_lock(&lqs->lqs_lock);
221 if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
222 oa->o_flags |= (cnt == USRQUOTA) ?
223 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
224 cfs_spin_unlock(&lqs->lqs_lock);
225 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
226 "sync_blk(%d)\n", lqs->lqs_bunit_sz,
227 qctxt->lqc_sync_blk);
228 /* this is for quota_search_lqs */
232 cfs_spin_unlock(&lqs->lqs_lock);
233 /* this is for quota_search_lqs */
237 memset(oqctl, 0, sizeof(*oqctl));
239 oqctl->qc_cmd = Q_GETQUOTA;
240 oqctl->qc_type = cnt;
241 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
242 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
246 oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
248 CDEBUG(D_QUOTA, "fsfilt getquota for %s %d failed, "
250 cnt == USRQUOTA ? "user" : "group",
251 cnt == USRQUOTA ? oa->o_uid : oa->o_gid, err);
255 if (oqctl->qc_dqblk.dqb_bhardlimit &&
256 (toqb(oqctl->qc_dqblk.dqb_curspace) >=
257 oqctl->qc_dqblk.dqb_bhardlimit)) {
258 oa->o_flags |= (cnt == USRQUOTA) ?
259 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
260 CDEBUG(D_QUOTA, "out of quota for %s %d\n",
261 cnt == USRQUOTA ? "user" : "group",
262 cnt == USRQUOTA ? oa->o_uid : oa->o_gid);
270 * check whether the left quota of certain uid and gid can satisfy a block_write
271 * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
273 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
274 int pending[], int count, int cycle, int isblk,
275 struct inode *inode, int frags)
277 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
279 struct qunit_data qdata[MAXQUOTAS];
281 int rc = 0, rc2[2] = { 0, 0 };
284 cfs_spin_lock(&qctxt->lqc_lock);
285 if (!qctxt->lqc_valid){
286 cfs_spin_unlock(&qctxt->lqc_lock);
289 cfs_spin_unlock(&qctxt->lqc_lock);
291 for (i = 0; i < MAXQUOTAS; i++) {
292 struct lustre_qunit_size *lqs = NULL;
294 qdata[i].qd_id = id[i];
295 qdata[i].qd_flags = i;
297 QDATA_SET_BLK(&qdata[i]);
298 qdata[i].qd_count = 0;
300 /* ignore root user */
301 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
304 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
305 if (lqs == NULL || IS_ERR(lqs))
309 CERROR("can not find lqs for check_common: "
310 "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
311 id[i], i % 2 ? 'g': 'u', isblk, count,
313 RETURN(PTR_ERR(lqs));
316 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
317 cfs_spin_lock(&lqs->lqs_lock);
320 pending[i] = count * CFS_PAGE_SIZE;
321 /* in order to complete this write, we need extra
322 * meta blocks. This function can get it through
323 * data needed to be written b=16542 */
326 rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
330 CERROR("%s: can't get extra "
336 LASSERTF(pending[i] >= 0, "pending is not valid"
337 ", count=%d, mb=%d\n", count, mb);
338 lqs->lqs_bwrite_pending += pending[i];
341 lqs->lqs_iwrite_pending += pending[i];
345 /* if xx_rec < 0, that means quota are releasing,
346 * and it may return before we use quota. So if
347 * we find this situation, we assuming it has
348 * returned b=18491 */
349 if (isblk && lqs->lqs_blk_rec < 0) {
350 if (qdata[i].qd_count < -lqs->lqs_blk_rec)
351 qdata[i].qd_count = 0;
353 qdata[i].qd_count += lqs->lqs_blk_rec;
355 if (!isblk && lqs->lqs_ino_rec < 0) {
356 if (qdata[i].qd_count < -lqs->lqs_ino_rec)
357 qdata[i].qd_count = 0;
359 qdata[i].qd_count += lqs->lqs_ino_rec;
362 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
363 " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
364 " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
365 isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
366 qdata[i].qd_count, mb, pending[i]);
367 if (rc2[i] == QUOTA_RET_OK) {
368 if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
369 rc2[i] = QUOTA_RET_ACQUOTA;
370 if (!isblk && qdata[i].qd_count <
371 lqs->lqs_iwrite_pending)
372 rc2[i] = QUOTA_RET_ACQUOTA;
375 cfs_spin_unlock(&lqs->lqs_lock);
377 if (lqs->lqs_blk_rec < 0 &&
379 lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
380 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
382 /* When cycle is zero, lqs_*_pending will be changed. We will
383 * get reference of the lqs here and put reference of lqs in
384 * quota_pending_commit b=14784 */
388 /* this is for quota_search_lqs */
392 if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
393 RETURN(QUOTA_RET_ACQUOTA);
398 int quota_is_set(struct obd_device *obd, const unsigned int id[], int flag)
400 struct lustre_qunit_size *lqs;
403 if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
406 for (i = 0; i < MAXQUOTAS; i++) {
407 lqs = quota_search_lqs(LQS_KEY(i, id[i]),
408 &obd->u.obt.obt_qctxt, 0);
409 if (lqs && !IS_ERR(lqs)) {
410 if (lqs->lqs_flags & flag)
419 static int quota_chk_acq_common(struct obd_device *obd, struct obd_export *exp,
420 const unsigned int id[], int pending[],
421 int count, quota_acquire acquire,
422 struct obd_trans_info *oti, int isblk,
423 struct inode *inode, int frags)
425 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
426 struct timeval work_start;
427 struct timeval work_end;
429 struct l_wait_info lwi = { 0 };
430 int rc = 0, cycle = 0, count_err = 1;
433 if (!quota_is_set(obd, id, isblk ? QB_SET : QI_SET))
436 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
437 /* If the client has been evicted or if it
438 * timed out and tried to reconnect already,
439 * abort the request immediately */
442 CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
443 pending[USRQUOTA] = pending[GRPQUOTA] = 0;
444 /* Unfortunately, if quota master is too busy to handle the
445 * pre-dqacq in time and quota hash on ost is used up, we
446 * have to wait for the completion of in flight dqacq/dqrel,
447 * in order to get enough quota for write b=12588 */
448 cfs_gettimeofday(&work_start);
449 while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
453 cfs_spin_lock(&qctxt->lqc_lock);
454 if (!qctxt->lqc_import && oti) {
455 cfs_spin_unlock(&qctxt->lqc_lock);
456 LASSERT(oti->oti_thread);
457 /* The recovery thread doesn't have watchdog
458 * attached. LU-369 */
459 if (oti->oti_thread->t_watchdog)
460 lc_watchdog_disable(oti->oti_thread->\
462 CDEBUG(D_QUOTA, "sleep for quota master\n");
463 l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
465 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
466 if (oti->oti_thread->t_watchdog)
467 lc_watchdog_touch(oti->oti_thread->t_watchdog,
468 CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
470 cfs_spin_unlock(&qctxt->lqc_lock);
475 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
476 /* after acquire(), we should run quota_check_common again
477 * so that we confirm there are enough quota to finish write */
478 rc = acquire(obd, id, oti, isblk);
480 /* please reference to dqacq_completion for the below */
481 /* a new request is finished, try again */
482 if (rc == QUOTA_REQ_RETURNED) {
483 CDEBUG(D_QUOTA, "finish a quota req, try again\n");
487 /* it is out of quota already */
489 CDEBUG(D_QUOTA, "out of quota, return -EDQUOT\n");
493 /* Related quota has been disabled by master, but enabled by
494 * slave, do not try again. */
495 if (unlikely(rc == -ESRCH)) {
496 CERROR("mismatched quota configuration, stop try.\n");
500 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
501 /* The client has been evicted or tried to
502 * to reconnect already, abort the request */
505 /* -EBUSY and others, wait a second and try again */
508 struct l_wait_info lwi;
510 if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
511 lc_watchdog_touch(oti->oti_thread->t_watchdog,
512 CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
513 CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
516 cfs_waitq_init(&waitq);
517 lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
519 l_wait_event(waitq, 0, &lwi);
522 if (rc < 0 || cycle % 10 == 0) {
523 cfs_spin_lock(&last_print_lock);
524 if (last_print == 0 ||
525 cfs_time_before((last_print + cfs_time_seconds(30)),
526 cfs_time_current())) {
527 last_print = cfs_time_current();
528 cfs_spin_unlock(&last_print_lock);
529 CWARN("still haven't managed to acquire quota "
530 "space from the quota master after %d "
531 "retries (err=%d, rc=%d)\n",
532 cycle, count_err - 1, rc);
534 cfs_spin_unlock(&last_print_lock);
538 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
541 cfs_gettimeofday(&work_end);
542 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
543 lprocfs_counter_add(qctxt->lqc_stats,
544 isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
545 LQUOTA_WAIT_FOR_CHK_INO,
554 * when a block_write or inode_create rpc is finished, adjust the record for
555 * pending blocks and inodes
557 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
558 int pending[], int isblk)
560 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
561 struct timeval work_start;
562 struct timeval work_end;
565 struct qunit_data qdata[MAXQUOTAS];
568 CDEBUG(D_QUOTA, "commit pending quota for %s\n", obd->obd_name);
569 CLASSERT(MAXQUOTAS < 4);
570 if (!ll_sb_any_quota_active(qctxt->lqc_sb))
573 cfs_gettimeofday(&work_start);
574 for (i = 0; i < MAXQUOTAS; i++) {
575 struct lustre_qunit_size *lqs = NULL;
577 LASSERT(pending[i] >= 0);
581 qdata[i].qd_id = id[i];
582 qdata[i].qd_flags = i;
584 QDATA_SET_BLK(&qdata[i]);
585 qdata[i].qd_count = 0;
587 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
590 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
591 if (lqs == NULL || IS_ERR(lqs)) {
592 CERROR("can not find lqs for pending_commit: "
593 "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
594 "maybe cause unexpected lqs refcount error!\n",
595 id[i], i ? 'g': 'u', pending[i], isblk,
596 lqs ? PTR_ERR(lqs) : -1);
600 cfs_spin_lock(&lqs->lqs_lock);
602 LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
603 "there are too many blocks! [id %u] [%c] "
604 "[bwrite_pending %lu] [pending %u]\n",
605 id[i], i % 2 ? 'g' : 'u',
606 lqs->lqs_bwrite_pending, pending[i]);
608 lqs->lqs_bwrite_pending -= pending[i];
610 LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
611 "there are too many files! [id %u] [%c] "
612 "[iwrite_pending %lu] [pending %u]\n",
613 id[i], i % 2 ? 'g' : 'u',
614 lqs->lqs_iwrite_pending, pending[i]);
616 lqs->lqs_iwrite_pending -= pending[i];
618 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
620 isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
621 i, pending[i], isblk);
622 cfs_spin_unlock(&lqs->lqs_lock);
624 /* for quota_search_lqs in pending_commit */
626 /* for quota_search_lqs in quota_check */
629 cfs_gettimeofday(&work_end);
630 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
631 lprocfs_counter_add(qctxt->lqc_stats,
632 isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
633 LQUOTA_WAIT_FOR_COMMIT_INO,
639 static int mds_quota_init(void)
641 return lustre_dquot_init();
644 static int mds_quota_exit(void)
650 static int mds_quota_setup(struct obd_device *obd)
652 struct obd_device_target *obt = &obd->u.obt;
653 struct mds_obd *mds = &obd->u.mds;
657 if (unlikely(mds->mds_quota)) {
658 CWARN("try to reinitialize quota context!\n");
662 cfs_init_rwsem(&obt->obt_rwsem);
663 obt->obt_qfmt = LUSTRE_QUOTA_V2;
664 mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
665 cfs_sema_init(&obt->obt_quotachecking, 1);
666 /* initialize quota master and quota context */
667 cfs_init_rwsem(&mds->mds_qonoff_sem);
668 rc = qctxt_init(obd, dqacq_handler);
670 CERROR("%s: initialize quota context failed! (rc:%d)\n",
678 static int mds_quota_cleanup(struct obd_device *obd)
681 if (unlikely(!obd->u.mds.mds_quota))
684 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
688 static int mds_quota_setinfo(struct obd_device *obd, void *data)
690 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
693 if (unlikely(!obd->u.mds.mds_quota))
697 QUOTA_MASTER_READY(qctxt);
699 QUOTA_MASTER_UNREADY(qctxt);
703 static int mds_quota_fs_cleanup(struct obd_device *obd)
705 struct mds_obd *mds = &obd->u.mds;
706 struct obd_quotactl oqctl;
709 if (unlikely(!mds->mds_quota))
713 memset(&oqctl, 0, sizeof(oqctl));
714 oqctl.qc_type = UGQUOTA;
716 cfs_down_write(&mds->mds_qonoff_sem);
717 mds_admin_quota_off(obd, &oqctl);
718 cfs_up_write(&mds->mds_qonoff_sem);
722 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
723 struct obd_trans_info *oti, int isblk)
725 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
729 rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
733 #endif /* HAVE_QUOTA_SUPPORT */
734 #endif /* __KERNEL__ */
736 struct osc_quota_info {
737 /** linkage for quota hash table */
738 cfs_hlist_node_t oqi_hash;
742 /* SLAB cache for client quota context */
743 cfs_mem_cache_t *qinfo_cachep = NULL;
745 static inline struct osc_quota_info *osc_oqi_alloc(obd_uid id)
747 struct osc_quota_info *oqi;
749 OBD_SLAB_ALLOC_PTR(oqi, qinfo_cachep);
756 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
761 for (type = 0; type < MAXQUOTAS; type++) {
762 struct osc_quota_info *oqi;
764 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
766 obd_uid id = oqi->oqi_id;
768 LASSERTF(id == qid[type],
769 "The ids don't match %u != %u\n",
772 /* the slot is busy, the user is about to run out of
773 * quota space on this OST */
774 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
775 type == USRQUOTA ? "user" : "grout", qid[type]);
783 #define MD_QUOTA_FLAG(type) ((type == USRQUOTA) ? OBD_MD_FLUSRQUOTA \
785 #define FL_QUOTA_FLAG(type) ((type == USRQUOTA) ? OBD_FL_NO_USRQUOTA \
786 : OBD_FL_NO_GRPQUOTA)
788 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
789 obd_flag valid, obd_flag flags)
795 if ((valid & (OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA)) == 0)
798 for (type = 0; type < MAXQUOTAS; type++) {
799 struct osc_quota_info *oqi;
801 if ((valid & MD_QUOTA_FLAG(type)) == 0)
804 /* lookup the ID in the per-type hash table */
805 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
806 if ((flags & FL_QUOTA_FLAG(type)) != 0) {
807 /* This ID is getting close to its quota limit, let's
808 * switch to sync I/O */
812 oqi = osc_oqi_alloc(qid[type]);
818 rc = cfs_hash_add_unique(cli->cl_quota_hash[type],
819 &qid[type], &oqi->oqi_hash);
820 /* race with others? */
821 if (rc == -EALREADY) {
823 OBD_SLAB_FREE_PTR(oqi, qinfo_cachep);
826 CDEBUG(D_QUOTA, "%s: setdq to insert for %s %d (%d)\n",
827 cli->cl_import->imp_obd->obd_name,
828 type == USRQUOTA ? "user" : "group",
831 /* This ID is now off the hook, let's remove it from
836 oqi = cfs_hash_del_key(cli->cl_quota_hash[type],
839 OBD_SLAB_FREE_PTR(oqi, qinfo_cachep);
841 CDEBUG(D_QUOTA, "%s: setdq to remove for %s %d (%p)\n",
842 cli->cl_import->imp_obd->obd_name,
843 type == USRQUOTA ? "user" : "group",
852 * Hash operations for uid/gid <-> osc_quota_info
855 oqi_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
857 return cfs_hash_u32_hash(*((__u32*)key), mask);
861 oqi_keycmp(const void *key, cfs_hlist_node_t *hnode)
863 struct osc_quota_info *oqi;
866 LASSERT(key != NULL);
867 uid = *((obd_uid*)key);
868 oqi = cfs_hlist_entry(hnode, struct osc_quota_info, oqi_hash);
870 return uid == oqi->oqi_id;
874 oqi_key(cfs_hlist_node_t *hnode)
876 struct osc_quota_info *oqi;
877 oqi = cfs_hlist_entry(hnode, struct osc_quota_info, oqi_hash);
882 oqi_object(cfs_hlist_node_t *hnode)
884 return cfs_hlist_entry(hnode, struct osc_quota_info, oqi_hash);
888 oqi_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
893 oqi_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
898 oqi_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
900 struct osc_quota_info *oqi;
902 oqi = cfs_hlist_entry(hnode, struct osc_quota_info, oqi_hash);
904 OBD_SLAB_FREE_PTR(oqi, qinfo_cachep);
907 #define HASH_QUOTA_BKT_BITS 5
908 #define HASH_QUOTA_CUR_BITS 5
909 #define HASH_QUOTA_MAX_BITS 15
911 static cfs_hash_ops_t quota_hash_ops = {
912 .hs_hash = oqi_hashfn,
913 .hs_keycmp = oqi_keycmp,
915 .hs_object = oqi_object,
917 .hs_put_locked = oqi_put_locked,
921 int osc_quota_setup(struct obd_device *obd)
923 struct client_obd *cli = &obd->u.cli;
927 for (type = 0; type < MAXQUOTAS; type++) {
928 cli->cl_quota_hash[type] = cfs_hash_create("QUOTA_HASH",
937 if (cli->cl_quota_hash[type] == NULL)
941 if (type == MAXQUOTAS)
944 for (i = 0; i < type; i++)
945 cfs_hash_putref(cli->cl_quota_hash[i]);
950 int osc_quota_cleanup(struct obd_device *obd)
952 struct client_obd *cli = &obd->u.cli;
956 for (type = 0; type < MAXQUOTAS; type++)
957 cfs_hash_putref(cli->cl_quota_hash[type]);
962 int osc_quota_init(void)
966 LASSERT(qinfo_cachep == NULL);
967 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
968 sizeof(struct osc_quota_info),
976 int osc_quota_exit(void)
981 rc = cfs_mem_cache_destroy(qinfo_cachep);
982 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
990 #ifdef HAVE_QUOTA_SUPPORT
991 quota_interface_t mds_quota_interface = {
992 .quota_init = mds_quota_init,
993 .quota_exit = mds_quota_exit,
994 .quota_setup = mds_quota_setup,
995 .quota_cleanup = mds_quota_cleanup,
996 .quota_check = target_quota_check,
997 .quota_ctl = mds_quota_ctl,
998 .quota_setinfo = mds_quota_setinfo,
999 .quota_fs_cleanup = mds_quota_fs_cleanup,
1000 .quota_recovery = mds_quota_recovery,
1001 .quota_adjust = mds_quota_adjust,
1002 .quota_chkquota = quota_chk_acq_common,
1003 .quota_acquire = quota_acquire_common,
1004 .quota_pending_commit = quota_pending_commit,
1007 quota_interface_t filter_quota_interface = {
1008 .quota_setup = filter_quota_setup,
1009 .quota_cleanup = filter_quota_cleanup,
1010 .quota_check = target_quota_check,
1011 .quota_ctl = filter_quota_ctl,
1012 .quota_setinfo = filter_quota_setinfo,
1013 .quota_clearinfo = filter_quota_clearinfo,
1014 .quota_enforce = filter_quota_enforce,
1015 .quota_getflag = filter_quota_getflag,
1016 .quota_acquire = quota_acquire_common,
1017 .quota_adjust = filter_quota_adjust,
1018 .quota_chkquota = quota_chk_acq_common,
1019 .quota_adjust_qunit = filter_quota_adjust_qunit,
1020 .quota_pending_commit = quota_pending_commit,
1023 #endif /* __KERNEL__ */
1025 quota_interface_t mdc_quota_interface = {
1026 .quota_ctl = client_quota_ctl,
1027 .quota_check = client_quota_check,
1028 .quota_poll_check = client_quota_poll_check,
1031 quota_interface_t lmv_quota_interface = {
1032 .quota_ctl = lmv_quota_ctl,
1033 .quota_check = lmv_quota_check,
1036 quota_interface_t osc_quota_interface = {
1037 .quota_ctl = client_quota_ctl,
1038 .quota_check = client_quota_check,
1039 .quota_poll_check = client_quota_poll_check,
1040 .quota_init = osc_quota_init,
1041 .quota_exit = osc_quota_exit,
1042 .quota_setup = osc_quota_setup,
1043 .quota_chkdq = osc_quota_chkdq,
1044 .quota_setdq = osc_quota_setdq,
1045 .quota_cleanup = osc_quota_cleanup,
1046 .quota_adjust_qunit = client_quota_adjust_qunit,
1049 quota_interface_t lov_quota_interface = {
1050 .quota_ctl = lov_quota_ctl,
1051 .quota_check = lov_quota_check,
1052 .quota_adjust_qunit = lov_quota_adjust_qunit,
1057 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
1059 static int __init init_lustre_quota(void)
1061 #ifdef HAVE_QUOTA_SUPPORT
1064 lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
1067 if (IS_ERR(lquota_type_proc_dir)) {
1068 CERROR("LProcFS failed in lquota-init\n");
1069 rc = PTR_ERR(lquota_type_proc_dir);
1073 rc = qunit_cache_init();
1077 PORTAL_SYMBOL_REGISTER(filter_quota_interface);
1078 PORTAL_SYMBOL_REGISTER(mds_quota_interface);
1080 PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
1081 PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
1082 PORTAL_SYMBOL_REGISTER(osc_quota_interface);
1083 PORTAL_SYMBOL_REGISTER(lov_quota_interface);
1087 static void /*__exit*/ exit_lustre_quota(void)
1089 PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
1090 PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
1091 PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
1092 PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
1093 #ifdef HAVE_QUOTA_SUPPORT
1094 PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
1095 PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
1097 qunit_cache_cleanup();
1099 if (lquota_type_proc_dir)
1100 lprocfs_remove(&lquota_type_proc_dir);
1104 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1105 MODULE_DESCRIPTION("Lustre Quota");
1106 MODULE_LICENSE("GPL");
1108 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
1110 #ifdef HAVE_QUOTA_SUPPORT
1111 EXPORT_SYMBOL(mds_quota_interface);
1112 EXPORT_SYMBOL(filter_quota_interface);
1114 EXPORT_SYMBOL(mdc_quota_interface);
1115 EXPORT_SYMBOL(lmv_quota_interface);
1116 EXPORT_SYMBOL(osc_quota_interface);
1117 EXPORT_SYMBOL(lov_quota_interface);
1118 #endif /* __KERNEL */