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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LQUOTA
43 # include <linux/version.h>
44 # include <linux/module.h>
45 # include <linux/init.h>
46 # include <linux/fs.h>
47 # include <linux/jbd.h>
48 # include <linux/smp_lock.h>
49 # include <linux/buffer_head.h>
50 # include <linux/workqueue.h>
51 # include <linux/mount.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lustre_dlm.h>
59 #include <lustre_cfg.h>
61 #include <lustre_fsfilt.h>
62 #include <lustre_quota.h>
63 #include <lprocfs_status.h>
64 #include "quota_internal.h"
68 #ifdef HAVE_QUOTA_SUPPORT
70 static cfs_time_t last_print = 0;
71 static spinlock_t last_print_lock = SPIN_LOCK_UNLOCKED;
73 static int filter_quota_setup(struct obd_device *obd)
76 struct obd_device_target *obt = &obd->u.obt;
79 init_rwsem(&obt->obt_rwsem);
80 obt->obt_qfmt = LUSTRE_QUOTA_V2;
81 sema_init(&obt->obt_quotachecking, 1);
82 rc = qctxt_init(obd, NULL);
84 CERROR("initialize quota context failed! (rc:%d)\n", rc);
89 static int filter_quota_cleanup(struct obd_device *obd)
92 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
96 static int filter_quota_setinfo(struct obd_device *obd, void *data)
98 struct obd_export *exp = data;
99 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
100 struct obd_import *imp = exp->exp_imp_reverse;
103 LASSERT(imp != NULL);
105 /* setup the quota context import */
106 spin_lock(&qctxt->lqc_lock);
107 if (qctxt->lqc_import != NULL) {
108 spin_unlock(&qctxt->lqc_lock);
109 if (qctxt->lqc_import == imp)
110 CDEBUG(D_WARNING, "%s: lqc_import(%p) of obd(%p) was "
111 "activated already.\n", obd->obd_name, imp, obd);
113 CERROR("%s: lqc_import(%p:%p) of obd(%p) was "
114 "activated by others.\n", obd->obd_name,
115 qctxt->lqc_import, imp, obd);
117 qctxt->lqc_import = imp;
118 /* make imp's connect flags equal relative exp's connect flags
119 * adding it to avoid the scan export list */
120 imp->imp_connect_data.ocd_connect_flags |=
121 (exp->exp_connect_flags &
122 (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
123 spin_unlock(&qctxt->lqc_lock);
124 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated "
125 "now.\n", obd->obd_name, imp, obd);
127 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
128 /* start quota slave recovery thread. (release high limits) */
129 qslave_start_recovery(obd, qctxt);
134 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
136 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
137 struct obd_import *imp = exp->exp_imp_reverse;
140 /* lquota may be not set up before destroying export, b=14896 */
141 if (!obd->obd_set_up)
144 if (unlikely(imp == NULL))
147 /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
148 * should be invalid b=12374 */
149 spin_lock(&qctxt->lqc_lock);
150 if (qctxt->lqc_import == imp) {
151 qctxt->lqc_import = NULL;
152 spin_unlock(&qctxt->lqc_lock);
153 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is invalid now.\n",
154 obd->obd_name, imp, obd);
155 ptlrpc_cleanup_imp(imp);
156 dqacq_interrupt(qctxt);
158 spin_unlock(&qctxt->lqc_lock);
163 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
167 if (!ll_sb_any_quota_active(obd->u.obt.obt_sb))
171 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
172 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
174 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
180 #define GET_OA_ID(flag, oa) (flag == USRQUOTA ? oa->o_uid : oa->o_gid)
181 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
183 struct obd_device_target *obt = &obd->u.obt;
184 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
185 int err, cnt, rc = 0;
186 struct obd_quotactl *oqctl;
189 if (!ll_sb_any_quota_active(obt->obt_sb))
192 OBD_ALLOC_PTR(oqctl);
196 /* set over quota flags for a uid/gid */
197 oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
198 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
200 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
201 struct lustre_qunit_size *lqs = NULL;
203 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
205 if (lqs == NULL || IS_ERR(lqs)) {
208 CDEBUG(D_QUOTA, "search lqs for %s %d failed, "
210 cnt == USRQUOTA ? "user" : "group",
211 cnt == USRQUOTA ? oa->o_uid : oa->o_gid,
215 spin_lock(&lqs->lqs_lock);
216 if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
217 oa->o_flags |= (cnt == USRQUOTA) ?
218 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
219 spin_unlock(&lqs->lqs_lock);
220 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
221 "sync_blk(%d)\n", lqs->lqs_bunit_sz,
222 qctxt->lqc_sync_blk);
223 /* this is for quota_search_lqs */
227 spin_unlock(&lqs->lqs_lock);
228 /* this is for quota_search_lqs */
232 memset(oqctl, 0, sizeof(*oqctl));
234 oqctl->qc_cmd = Q_GETQUOTA;
235 oqctl->qc_type = cnt;
236 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
237 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
241 oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
243 CDEBUG(D_QUOTA, "fsfilt getquota for %s %d failed, "
245 cnt == USRQUOTA ? "user" : "group",
246 cnt == USRQUOTA ? oa->o_uid : oa->o_gid, err);
250 if (oqctl->qc_dqblk.dqb_bhardlimit &&
251 (toqb(oqctl->qc_dqblk.dqb_curspace) >=
252 oqctl->qc_dqblk.dqb_bhardlimit)) {
253 oa->o_flags |= (cnt == USRQUOTA) ?
254 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
255 CDEBUG(D_QUOTA, "out of quota for %s %d\n",
256 cnt == USRQUOTA ? "user" : "group",
257 cnt == USRQUOTA ? oa->o_uid : oa->o_gid);
265 * check whether the left quota of certain uid and gid can satisfy a block_write
266 * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
268 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
269 int pending[], int count, int cycle, int isblk,
270 struct inode *inode, int frags)
272 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
274 struct qunit_data qdata[MAXQUOTAS];
276 int rc = 0, rc2[2] = { 0, 0 };
279 spin_lock(&qctxt->lqc_lock);
280 if (!qctxt->lqc_valid){
281 spin_unlock(&qctxt->lqc_lock);
284 spin_unlock(&qctxt->lqc_lock);
286 for (i = 0; i < MAXQUOTAS; i++) {
287 struct lustre_qunit_size *lqs = NULL;
289 qdata[i].qd_id = id[i];
290 qdata[i].qd_flags = i;
292 QDATA_SET_BLK(&qdata[i]);
293 qdata[i].qd_count = 0;
295 /* ignore root user */
296 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
299 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
300 if (lqs == NULL || IS_ERR(lqs))
304 CERROR("can not find lqs for check_common: "
305 "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
306 id[i], i % 2 ? 'g': 'u', isblk, count,
308 RETURN(PTR_ERR(lqs));
311 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
312 spin_lock(&lqs->lqs_lock);
315 pending[i] = count * CFS_PAGE_SIZE;
316 /* in order to complete this write, we need extra
317 * meta blocks. This function can get it through
318 * data needed to be written b=16542 */
321 rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
324 CERROR("%s: can't get extra "
330 lqs->lqs_bwrite_pending += pending[i];
333 lqs->lqs_iwrite_pending += pending[i];
337 /* if xx_rec < 0, that means quota are releasing,
338 * and it may return before we use quota. So if
339 * we find this situation, we assuming it has
340 * returned b=18491 */
341 if (isblk && lqs->lqs_blk_rec < 0) {
342 if (qdata[i].qd_count < -lqs->lqs_blk_rec)
343 qdata[i].qd_count = 0;
345 qdata[i].qd_count += lqs->lqs_blk_rec;
347 if (!isblk && lqs->lqs_ino_rec < 0) {
348 if (qdata[i].qd_count < -lqs->lqs_ino_rec)
349 qdata[i].qd_count = 0;
351 qdata[i].qd_count += lqs->lqs_ino_rec;
354 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
355 " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
356 " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
357 isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
358 qdata[i].qd_count, mb, pending[i]);
359 if (rc2[i] == QUOTA_RET_OK) {
360 if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
361 rc2[i] = QUOTA_RET_ACQUOTA;
362 if (!isblk && qdata[i].qd_count <
363 lqs->lqs_iwrite_pending)
364 rc2[i] = QUOTA_RET_ACQUOTA;
367 spin_unlock(&lqs->lqs_lock);
369 if (lqs->lqs_blk_rec < 0 &&
371 lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
372 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
374 /* When cycle is zero, lqs_*_pending will be changed. We will
375 * get reference of the lqs here and put reference of lqs in
376 * quota_pending_commit b=14784 */
380 /* this is for quota_search_lqs */
384 if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
385 RETURN(QUOTA_RET_ACQUOTA);
390 int quota_is_set(struct obd_device *obd, const unsigned int id[], int flag)
392 struct lustre_qunit_size *lqs;
395 if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
398 for (i = 0; i < MAXQUOTAS; i++) {
399 lqs = quota_search_lqs(LQS_KEY(i, id[i]),
400 &obd->u.obt.obt_qctxt, 0);
401 if (lqs && !IS_ERR(lqs)) {
402 if (lqs->lqs_flags & flag)
411 static int quota_chk_acq_common(struct obd_device *obd, struct obd_export *exp,
412 const unsigned int id[], int pending[],
413 int count, quota_acquire acquire,
414 struct obd_trans_info *oti, int isblk,
415 struct inode *inode, int frags)
417 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
418 struct timeval work_start;
419 struct timeval work_end;
421 struct l_wait_info lwi = { 0 };
422 int rc = 0, cycle = 0, count_err = 1;
425 if (!quota_is_set(obd, id, isblk ? QB_SET : QI_SET))
428 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
429 /* If the client has been evicted or if it
430 * timed out and tried to reconnect already,
431 * abort the request immediately */
434 CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
435 pending[USRQUOTA] = pending[GRPQUOTA] = 0;
436 /* Unfortunately, if quota master is too busy to handle the
437 * pre-dqacq in time and quota hash on ost is used up, we
438 * have to wait for the completion of in flight dqacq/dqrel,
439 * in order to get enough quota for write b=12588 */
440 do_gettimeofday(&work_start);
441 while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
445 spin_lock(&qctxt->lqc_lock);
446 if (!qctxt->lqc_import && oti) {
447 spin_unlock(&qctxt->lqc_lock);
449 LASSERT(oti && oti->oti_thread &&
450 oti->oti_thread->t_watchdog);
452 lc_watchdog_disable(oti->oti_thread->t_watchdog);
453 CDEBUG(D_QUOTA, "sleep for quota master\n");
454 l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
456 CDEBUG(D_QUOTA, "wake up when quota master is back\n");
457 lc_watchdog_touch(oti->oti_thread->t_watchdog,
458 GET_TIMEOUT(oti->oti_thread->t_svc));
460 spin_unlock(&qctxt->lqc_lock);
465 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
466 /* after acquire(), we should run quota_check_common again
467 * so that we confirm there are enough quota to finish write */
468 rc = acquire(obd, id, oti, isblk);
470 /* please reference to dqacq_completion for the below */
471 /* a new request is finished, try again */
472 if (rc == QUOTA_REQ_RETURNED) {
473 CDEBUG(D_QUOTA, "finish a quota req, try again\n");
477 /* it is out of quota already */
479 CDEBUG(D_QUOTA, "out of quota, return -EDQUOT\n");
483 /* Related quota has been disabled by master, but enabled by
484 * slave, do not try again. */
485 if (unlikely(rc == -ESRCH)) {
486 CERROR("mismatched quota configuration, stop try.\n");
490 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
491 /* The client has been evicted or tried to
492 * to reconnect already, abort the request */
495 /* -EBUSY and others, wait a second and try again */
498 struct l_wait_info lwi;
500 if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
501 lc_watchdog_touch(oti->oti_thread->t_watchdog,
502 GET_TIMEOUT(oti->oti_thread->t_svc));
503 CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
506 init_waitqueue_head(&waitq);
507 lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
509 l_wait_event(waitq, 0, &lwi);
512 if (rc < 0 || cycle % 10 == 0) {
513 spin_lock(&last_print_lock);
514 if (last_print == 0 ||
515 cfs_time_before((last_print + cfs_time_seconds(30)),
516 cfs_time_current())) {
517 last_print = cfs_time_current();
518 spin_unlock(&last_print_lock);
519 CWARN("still haven't managed to acquire quota "
520 "space from the quota master after %d "
521 "retries (err=%d, rc=%d)\n",
522 cycle, count_err - 1, rc);
524 spin_unlock(&last_print_lock);
528 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
531 do_gettimeofday(&work_end);
532 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
533 lprocfs_counter_add(qctxt->lqc_stats,
534 isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
535 LQUOTA_WAIT_FOR_CHK_INO,
544 * when a block_write or inode_create rpc is finished, adjust the record for
545 * pending blocks and inodes
547 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
548 int pending[], int isblk)
550 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
551 struct timeval work_start;
552 struct timeval work_end;
555 struct qunit_data qdata[MAXQUOTAS];
558 CDEBUG(D_QUOTA, "commit pending quota for %s\n", obd->obd_name);
559 CLASSERT(MAXQUOTAS < 4);
560 if (!ll_sb_any_quota_active(qctxt->lqc_sb))
563 do_gettimeofday(&work_start);
564 for (i = 0; i < MAXQUOTAS; i++) {
565 struct lustre_qunit_size *lqs = NULL;
567 LASSERT(pending[i] >= 0);
571 qdata[i].qd_id = id[i];
572 qdata[i].qd_flags = i;
574 QDATA_SET_BLK(&qdata[i]);
575 qdata[i].qd_count = 0;
577 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
580 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
581 if (lqs == NULL || IS_ERR(lqs)) {
582 CERROR("can not find lqs for pending_commit: "
583 "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
584 "maybe cause unexpected lqs refcount error!\n",
585 id[i], i ? 'g': 'u', pending[i], isblk,
586 lqs ? PTR_ERR(lqs) : -1);
590 spin_lock(&lqs->lqs_lock);
592 LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
593 "there are too many blocks! [id %u] [%c] "
594 "[bwrite_pending %lu] [pending %u]\n",
595 id[i], i % 2 ? 'g' : 'u',
596 lqs->lqs_bwrite_pending, pending[i]);
598 lqs->lqs_bwrite_pending -= pending[i];
600 LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
601 "there are too many files! [id %u] [%c] "
602 "[iwrite_pending %lu] [pending %u]\n",
603 id[i], i % 2 ? 'g' : 'u',
604 lqs->lqs_iwrite_pending, pending[i]);
606 lqs->lqs_iwrite_pending -= pending[i];
608 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
610 isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
611 i, pending[i], isblk);
612 spin_unlock(&lqs->lqs_lock);
614 /* for quota_search_lqs in pending_commit */
616 /* for quota_search_lqs in quota_check */
619 do_gettimeofday(&work_end);
620 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
621 lprocfs_counter_add(qctxt->lqc_stats,
622 isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
623 LQUOTA_WAIT_FOR_COMMIT_INO,
629 static int mds_quota_init(void)
631 return lustre_dquot_init();
634 static int mds_quota_exit(void)
640 static int mds_quota_setup(struct obd_device *obd)
642 struct obd_device_target *obt = &obd->u.obt;
643 struct mds_obd *mds = &obd->u.mds;
647 if (unlikely(mds->mds_quota)) {
648 CWARN("try to reinitialize quota context!\n");
652 init_rwsem(&obt->obt_rwsem);
653 obt->obt_qfmt = LUSTRE_QUOTA_V2;
654 mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
655 sema_init(&obt->obt_quotachecking, 1);
656 /* initialize quota master and quota context */
657 sema_init(&mds->mds_qonoff_sem, 1);
658 rc = qctxt_init(obd, dqacq_handler);
660 CERROR("%s: initialize quota context failed! (rc:%d)\n",
668 static int mds_quota_cleanup(struct obd_device *obd)
671 if (unlikely(!obd->u.mds.mds_quota))
674 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
678 static int mds_quota_setinfo(struct obd_device *obd, void *data)
680 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
683 if (unlikely(!obd->u.mds.mds_quota))
687 QUOTA_MASTER_READY(qctxt);
689 QUOTA_MASTER_UNREADY(qctxt);
693 static int mds_quota_fs_cleanup(struct obd_device *obd)
695 struct mds_obd *mds = &obd->u.mds;
696 struct obd_quotactl oqctl;
699 if (unlikely(!mds->mds_quota))
703 memset(&oqctl, 0, sizeof(oqctl));
704 oqctl.qc_type = UGQUOTA;
706 down(&mds->mds_qonoff_sem);
707 mds_admin_quota_off(obd, &oqctl);
708 up(&mds->mds_qonoff_sem);
712 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
713 struct obd_trans_info *oti, int isblk)
715 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
719 rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
723 #endif /* HAVE_QUOTA_SUPPORT */
724 #endif /* __KERNEL__ */
726 struct osc_quota_info {
727 struct list_head oqi_hash; /* hash list */
728 struct client_obd *oqi_cli; /* osc obd */
729 unsigned int oqi_id; /* uid/gid of a file */
730 short oqi_type; /* quota type */
733 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
735 static struct list_head qinfo_hash[NR_DQHASH];
736 /* SLAB cache for client quota context */
737 cfs_mem_cache_t *qinfo_cachep = NULL;
739 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
740 __attribute__((__const__));
742 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
744 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
745 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
749 /* caller must hold qinfo_list_lock */
750 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
752 struct list_head *head = qinfo_hash +
753 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
755 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
756 list_add(&oqi->oqi_hash, head);
759 /* caller must hold qinfo_list_lock */
760 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
762 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
763 list_del_init(&oqi->oqi_hash);
766 /* caller must hold qinfo_list_lock */
767 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
768 unsigned int id, int type)
770 unsigned int hashent = hashfn(cli, id, type);
771 struct osc_quota_info *oqi;
774 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
775 list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
776 if (oqi->oqi_cli == cli &&
777 oqi->oqi_id == id && oqi->oqi_type == type)
783 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
784 unsigned int id, int type)
786 struct osc_quota_info *oqi;
789 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
793 CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
796 oqi->oqi_type = type;
801 static void free_qinfo(struct osc_quota_info *oqi)
803 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
806 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
809 int cnt, rc = QUOTA_OK;
812 spin_lock(&qinfo_list_lock);
813 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
814 struct osc_quota_info *oqi = NULL;
816 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
817 oqi = find_qinfo(cli, id, cnt);
823 spin_unlock(&qinfo_list_lock);
826 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
827 cnt == USRQUOTA ? "user" : "group", id);
831 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
832 obd_flag valid, obd_flag flags)
840 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
841 struct osc_quota_info *oqi, *old;
843 if (!(valid & ((cnt == USRQUOTA) ?
844 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
847 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
848 noquota = (cnt == USRQUOTA) ?
849 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
851 oqi = alloc_qinfo(cli, id, cnt);
854 CDEBUG(D_QUOTA, "setdq for %s %d failed, (rc = %d)\n",
855 cnt == USRQUOTA ? "user" : "group", id, rc);
859 spin_lock(&qinfo_list_lock);
860 old = find_qinfo(cli, id, cnt);
862 remove_qinfo_hash(old);
863 else if (!old && noquota)
864 insert_qinfo_hash(oqi);
865 spin_unlock(&qinfo_list_lock);
873 CDEBUG(D_QUOTA, "setdq to remove for %s %d\n",
874 cnt == USRQUOTA ? "user" : "group", id);
875 else if (!old && noquota)
876 CDEBUG(D_QUOTA, "setdq to insert for %s %d\n",
877 cnt == USRQUOTA ? "user" : "group", id);
883 int osc_quota_cleanup(struct obd_device *obd)
885 struct client_obd *cli = &obd->u.cli;
886 struct osc_quota_info *oqi, *n;
890 spin_lock(&qinfo_list_lock);
891 for (i = 0; i < NR_DQHASH; i++) {
892 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
893 if (oqi->oqi_cli != cli)
895 remove_qinfo_hash(oqi);
899 spin_unlock(&qinfo_list_lock);
904 int osc_quota_init(void)
909 LASSERT(qinfo_cachep == NULL);
910 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
911 sizeof(struct osc_quota_info),
916 for (i = 0; i < NR_DQHASH; i++)
917 CFS_INIT_LIST_HEAD(qinfo_hash + i);
922 int osc_quota_exit(void)
924 struct osc_quota_info *oqi, *n;
928 spin_lock(&qinfo_list_lock);
929 for (i = 0; i < NR_DQHASH; i++) {
930 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
931 remove_qinfo_hash(oqi);
935 spin_unlock(&qinfo_list_lock);
937 rc = cfs_mem_cache_destroy(qinfo_cachep);
938 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
945 #ifdef HAVE_QUOTA_SUPPORT
946 quota_interface_t mds_quota_interface = {
947 .quota_init = mds_quota_init,
948 .quota_exit = mds_quota_exit,
949 .quota_setup = mds_quota_setup,
950 .quota_cleanup = mds_quota_cleanup,
951 .quota_check = target_quota_check,
952 .quota_ctl = mds_quota_ctl,
953 .quota_setinfo = mds_quota_setinfo,
954 .quota_fs_cleanup = mds_quota_fs_cleanup,
955 .quota_recovery = mds_quota_recovery,
956 .quota_adjust = mds_quota_adjust,
957 .quota_chkquota = quota_chk_acq_common,
958 .quota_acquire = quota_acquire_common,
959 .quota_pending_commit = quota_pending_commit,
962 quota_interface_t filter_quota_interface = {
963 .quota_setup = filter_quota_setup,
964 .quota_cleanup = filter_quota_cleanup,
965 .quota_check = target_quota_check,
966 .quota_ctl = filter_quota_ctl,
967 .quota_setinfo = filter_quota_setinfo,
968 .quota_clearinfo = filter_quota_clearinfo,
969 .quota_enforce = filter_quota_enforce,
970 .quota_getflag = filter_quota_getflag,
971 .quota_acquire = quota_acquire_common,
972 .quota_adjust = filter_quota_adjust,
973 .quota_chkquota = quota_chk_acq_common,
974 .quota_adjust_qunit = filter_quota_adjust_qunit,
975 .quota_pending_commit = quota_pending_commit,
978 #endif /* __KERNEL__ */
980 quota_interface_t mdc_quota_interface = {
981 .quota_ctl = client_quota_ctl,
982 .quota_check = client_quota_check,
983 .quota_poll_check = client_quota_poll_check,
986 quota_interface_t lmv_quota_interface = {
987 .quota_ctl = lmv_quota_ctl,
988 .quota_check = lmv_quota_check,
991 quota_interface_t osc_quota_interface = {
992 .quota_ctl = client_quota_ctl,
993 .quota_check = client_quota_check,
994 .quota_poll_check = client_quota_poll_check,
995 .quota_init = osc_quota_init,
996 .quota_exit = osc_quota_exit,
997 .quota_chkdq = osc_quota_chkdq,
998 .quota_setdq = osc_quota_setdq,
999 .quota_cleanup = osc_quota_cleanup,
1000 .quota_adjust_qunit = client_quota_adjust_qunit,
1003 quota_interface_t lov_quota_interface = {
1004 .quota_ctl = lov_quota_ctl,
1005 .quota_check = lov_quota_check,
1006 .quota_adjust_qunit = lov_quota_adjust_qunit,
1011 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
1013 static int __init init_lustre_quota(void)
1015 #ifdef HAVE_QUOTA_SUPPORT
1018 lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
1021 if (IS_ERR(lquota_type_proc_dir)) {
1022 CERROR("LProcFS failed in lquota-init\n");
1023 rc = PTR_ERR(lquota_type_proc_dir);
1027 rc = qunit_cache_init();
1031 PORTAL_SYMBOL_REGISTER(filter_quota_interface);
1032 PORTAL_SYMBOL_REGISTER(mds_quota_interface);
1034 PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
1035 PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
1036 PORTAL_SYMBOL_REGISTER(osc_quota_interface);
1037 PORTAL_SYMBOL_REGISTER(lov_quota_interface);
1041 static void /*__exit*/ exit_lustre_quota(void)
1043 PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
1044 PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
1045 PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
1046 PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
1047 #ifdef HAVE_QUOTA_SUPPORT
1048 PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
1049 PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
1051 qunit_cache_cleanup();
1053 if (lquota_type_proc_dir)
1054 lprocfs_remove(&lquota_type_proc_dir);
1058 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1059 MODULE_DESCRIPTION("Lustre Quota");
1060 MODULE_LICENSE("GPL");
1062 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
1064 #ifdef HAVE_QUOTA_SUPPORT
1065 EXPORT_SYMBOL(mds_quota_interface);
1066 EXPORT_SYMBOL(filter_quota_interface);
1068 EXPORT_SYMBOL(mdc_quota_interface);
1069 EXPORT_SYMBOL(lmv_quota_interface);
1070 EXPORT_SYMBOL(osc_quota_interface);
1071 EXPORT_SYMBOL(lov_quota_interface);
1072 #endif /* __KERNEL */