1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/quota/quota_master.c
5 * Lustre Quota Master request handler
7 * Copyright (c) 2001-2005 Cluster File Systems, Inc.
8 * Author: Niu YaWei <niu@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * No redistribution or use is permitted outside of Cluster File Systems, Inc.
16 # define EXPORT_SYMTAB
19 #define DEBUG_SUBSYSTEM S_MDS
21 #include <linux/version.h>
23 #include <asm/unistd.h>
24 #include <linux/slab.h>
25 #include <linux/quotaops.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/quota.h>
30 #include <obd_class.h>
31 #include <lustre_quota.h>
32 #include <lustre_fsfilt.h>
33 #include <lustre_mds.h>
35 #include "quota_internal.h"
38 * mds->mds_qonoff_sem > dquot->dq_sem */
39 static struct list_head lustre_dquot_hash[NR_DQHASH];
40 static spinlock_t dquot_hash_lock = SPIN_LOCK_UNLOCKED;
42 kmem_cache_t *lustre_dquot_cachep;
44 int lustre_dquot_init(void)
49 LASSERT(lustre_dquot_cachep == NULL);
50 lustre_dquot_cachep = kmem_cache_create("lustre_dquot_cache",
51 sizeof(struct lustre_dquot),
53 if (!lustre_dquot_cachep)
56 for (i = 0; i < NR_DQHASH; i++) {
57 INIT_LIST_HEAD(lustre_dquot_hash + i);
62 void lustre_dquot_exit(void)
66 /* FIXME cleanup work ?? */
68 for (i = 0; i < NR_DQHASH; i++) {
69 LASSERT(list_empty(lustre_dquot_hash + i));
71 if (lustre_dquot_cachep) {
72 #ifdef HAVE_KMEM_CACHE_DESTROY_INT
74 rc = kmem_cache_destroy(lustre_dquot_cachep);
75 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
77 kmem_cache_destroy(lustre_dquot_cachep);
79 lustre_dquot_cachep = NULL;
85 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
86 __attribute__((__const__));
89 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
91 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
92 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
96 /* caller must hold dquot_hash_lock */
97 static struct lustre_dquot *find_dquot(int hashent,
98 struct lustre_quota_info *lqi, qid_t id,
101 struct lustre_dquot *dquot;
104 LASSERT_SPIN_LOCKED(&dquot_hash_lock);
105 list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
106 if (dquot->dq_info == lqi &&
107 dquot->dq_id == id && dquot->dq_type == type)
113 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
116 struct lustre_dquot *dquot = NULL;
119 OBD_SLAB_ALLOC(dquot, lustre_dquot_cachep, SLAB_NOFS, sizeof(*dquot));
123 INIT_LIST_HEAD(&dquot->dq_hash);
124 init_mutex_locked(&dquot->dq_sem);
125 dquot->dq_refcnt = 1;
126 dquot->dq_info = lqi;
128 dquot->dq_type = type;
129 dquot->dq_status = DQ_STATUS_AVAIL;
134 static void free_dquot(struct lustre_dquot *dquot)
136 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
139 static void insert_dquot_nolock(struct lustre_dquot *dquot)
141 struct list_head *head = lustre_dquot_hash +
142 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
143 LASSERT(list_empty(&dquot->dq_hash));
144 list_add(&dquot->dq_hash, head);
147 static void remove_dquot_nolock(struct lustre_dquot *dquot)
149 LASSERT(!list_empty(&dquot->dq_hash));
150 list_del_init(&dquot->dq_hash);
153 static void lustre_dqput(struct lustre_dquot *dquot)
156 spin_lock(&dquot_hash_lock);
157 LASSERT(dquot->dq_refcnt);
159 if (!dquot->dq_refcnt) {
160 remove_dquot_nolock(dquot);
163 spin_unlock(&dquot_hash_lock);
167 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
168 struct lustre_quota_info *lqi,
171 unsigned int hashent = dquot_hashfn(lqi, id, type);
172 struct lustre_dquot *dquot, *empty;
175 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
176 RETURN(ERR_PTR(-ENOMEM));
178 spin_lock(&dquot_hash_lock);
179 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
181 spin_unlock(&dquot_hash_lock);
187 insert_dquot_nolock(dquot);
188 spin_unlock(&dquot_hash_lock);
190 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
193 CERROR("can't read dquot from admin quotafile! "
205 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
207 struct mds_obd *mds = &obd->u.mds;
208 struct lustre_quota_info *info = &mds->mds_quota_info;
209 struct lustre_dquot *dquot = NULL;
211 __u32 hlimit = 0, slimit = 0;
212 __u32 qdata_type = qdata->qd_flags & QUOTA_IS_GRP;
213 __u32 is_blk = (qdata->qd_flags & QUOTA_IS_BLOCK) >> 1;
215 unsigned int grace = 0;
219 OBD_FAIL_RETURN(OBD_FAIL_OBD_DQACQ, -EIO);
221 /* slaves never acquires qunit for user root */
222 LASSERT(qdata->qd_id || qdata_type);
224 dquot = lustre_dqget(obd, info, qdata->qd_id, qdata_type);
226 RETURN(PTR_ERR(dquot));
228 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
229 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
231 down(&mds->mds_qonoff_sem);
232 down(&dquot->dq_sem);
234 if (dquot->dq_status & DQ_STATUS_RECOVERY) {
235 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
236 GOTO(out, rc = -EBUSY);
240 grace = info->qi_info[qdata_type].dqi_bgrace;
241 usage = &dquot->dq_dqb.dqb_curspace;
242 hlimit = dquot->dq_dqb.dqb_bhardlimit;
243 slimit = dquot->dq_dqb.dqb_bsoftlimit;
244 time = &dquot->dq_dqb.dqb_btime;
246 grace = info->qi_info[qdata_type].dqi_igrace;
247 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
248 hlimit = dquot->dq_dqb.dqb_ihardlimit;
249 slimit = dquot->dq_dqb.dqb_isoftlimit;
250 time = &dquot->dq_dqb.dqb_itime;
253 /* if the quota limit in admin quotafile is zero, we just inform
254 * slave to clear quota limit with zero qd_count */
255 if (hlimit == 0 && slimit == 0) {
263 QUSG(*usage + qdata->qd_count, is_blk) > hlimit)
264 GOTO(out, rc = -EDQUOT);
267 QUSG(*usage + qdata->qd_count, is_blk) > slimit) {
268 if (*time && cfs_time_current_sec() >= *time)
269 GOTO(out, rc = -EDQUOT);
271 *time = cfs_time_current_sec() + grace;
274 *usage += qdata->qd_count;
277 /* The usage in administrative file might be incorrect before
279 if (*usage - qdata->qd_count < 0)
282 *usage -= qdata->qd_count;
284 /* (usage <= soft limit) but not (usage < soft limit) */
285 if (!slimit || QUSG(*usage, is_blk) <= slimit)
292 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
296 up(&mds->mds_qonoff_sem);
301 int mds_quota_adjust(struct obd_device *obd, unsigned int qcids[],
302 unsigned int qpids[], int rc, int opc)
304 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
308 if (rc && rc != -EDQUOT && rc != ENOLCK)
312 case FSFILT_OP_RENAME:
313 /* acquire/release block quota on owner of original parent */
314 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[2], qpids[3], 1, 0);
316 case FSFILT_OP_SETATTR:
317 /* acquire/release file quota on original owner */
318 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 0, 0);
320 case FSFILT_OP_CREATE:
321 case FSFILT_OP_UNLINK:
322 /* acquire/release file/block quota on owner of child (or current owner) */
323 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0);
324 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
325 /* acquire/release block quota on owner of parent (or original owner) */
326 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
334 CERROR("mds adjust qunit failed! (opc:%d rc:%d)\n", opc, rc2);
338 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
339 unsigned int qpids[], int rc, int opc)
341 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
345 if (rc && rc != -EDQUOT)
349 case FSFILT_OP_SETATTR:
350 /* acquire/release block quota on original & current owner */
351 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
352 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
354 case FSFILT_OP_UNLINK:
355 /* release block quota on this owner */
356 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
357 /* acquire block quota on this owner */
358 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
366 CERROR("filter adjust qunit failed! (opc:%d rc%d)\n",
371 #define LUSTRE_ADMIN_QUOTAFILES {\
372 "admin_quotafile.usr", /* user admin quotafile */\
373 "admin_quotafile.grp" /* group admin quotafile */\
375 static const char prefix[] = "OBJECTS/";
377 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
379 struct mds_obd *mds = &obd->u.mds;
380 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
381 const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
382 struct lvfs_run_ctxt saved;
385 struct dentry *dparent = mds->mds_objects_dir;
386 struct inode *iparent = dparent->d_inode;
390 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
392 down(&mds->mds_qonoff_sem);
393 for (i = 0; i < MAXQUOTAS; i++) {
397 if (!Q_TYPESET(oqctl, i))
400 /* quota file has been opened ? */
401 if (qinfo->qi_files[i]) {
402 CWARN("init %s admin quotafile while quota on.\n",
403 i == USRQUOTA ? "user" : "group");
407 /* lookup quota file */
409 LOCK_INODE_MUTEX(iparent);
410 de = lookup_one_len(quotafiles[i], dparent,
411 strlen(quotafiles[i]));
412 UNLOCK_INODE_MUTEX(iparent);
413 if (IS_ERR(de) || de->d_inode == NULL ||
414 !S_ISREG(de->d_inode->i_mode))
415 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
419 if (rc && rc != -ENOENT) {
420 CERROR("error lookup quotafile %s! (rc:%d)\n",
427 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
428 sprintf(name, "%s%s", prefix, quotafiles[i]);
430 LASSERT(rc == -ENOENT);
431 /* create quota file */
432 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
433 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
435 CERROR("error creating admin quotafile %s (rc:%d)\n",
440 qinfo->qi_files[i] = fp;
441 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
443 qinfo->qi_files[i] = NULL;
446 CERROR("error init %s admin quotafile! (rc:%d)\n",
447 i == USRQUOTA ? "user" : "group", rc);
451 up(&mds->mds_qonoff_sem);
453 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
457 static int close_quota_files(struct obd_quotactl *oqctl,
458 struct lustre_quota_info *qinfo)
463 for (i = 0; i < MAXQUOTAS; i++) {
464 if (!Q_TYPESET(oqctl, i))
466 if (qinfo->qi_files[i] == NULL) {
470 filp_close(qinfo->qi_files[i], 0);
471 qinfo->qi_files[i] = NULL;
476 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
478 struct mds_obd *mds = &obd->u.mds;
479 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
480 const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
483 struct inode *iparent = mds->mds_objects_dir->d_inode;
488 /* open admin quota files and read quotafile info */
489 for (i = 0; i < MAXQUOTAS; i++) {
492 if (!Q_TYPESET(oqctl, i))
495 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
496 sprintf(name, "%s%s", prefix, quotafiles[i]);
498 if (qinfo->qi_files[i] != NULL) {
503 fp = filp_open(name, O_RDWR | O_EXCL, 0644);
504 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
506 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
507 "open %s failed! (rc:%d)\n", name, rc);
510 qinfo->qi_files[i] = fp;
512 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
514 CERROR("error read quotainfo of %s! (rc:%d)\n",
520 if (rc && rc != -EBUSY)
521 close_quota_files(oqctl, qinfo);
526 static int mds_admin_quota_off(struct obd_device *obd,
527 struct obd_quotactl *oqctl)
529 struct mds_obd *mds = &obd->u.mds;
530 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
534 /* close admin quota files */
535 rc = close_quota_files(oqctl, qinfo);
539 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
541 struct mds_obd *mds = &obd->u.mds;
542 struct obd_device_target *obt = &obd->u.obt;
543 struct lvfs_run_ctxt saved;
547 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
548 CDEBUG(D_INFO, "other people are doing quotacheck\n");
549 atomic_inc(&obt->obt_quotachecking);
553 down(&mds->mds_qonoff_sem);
554 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
555 rc = mds_admin_quota_on(obd, oqctl);
559 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
563 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
565 obt->obt_qctxt.lqc_status = 1;
567 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
568 up(&mds->mds_qonoff_sem);
569 atomic_inc(&obt->obt_quotachecking);
573 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
575 struct mds_obd *mds = &obd->u.mds;
576 struct obd_device_target *obt = &obd->u.obt;
577 struct lvfs_run_ctxt saved;
581 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
582 CDEBUG(D_INFO, "other people are doing quotacheck\n");
583 atomic_inc(&obt->obt_quotachecking);
587 down(&mds->mds_qonoff_sem);
588 /* close admin quota files */
589 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
590 mds_admin_quota_off(obd, oqctl);
592 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
593 rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
595 obt->obt_qctxt.lqc_status = 0;
597 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
598 up(&mds->mds_qonoff_sem);
599 atomic_inc(&obt->obt_quotachecking);
604 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
606 struct mds_obd *mds = &obd->u.mds;
607 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
608 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
612 down(&mds->mds_qonoff_sem);
613 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
618 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
619 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
620 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
622 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
625 up(&mds->mds_qonoff_sem);
629 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
631 struct mds_obd *mds = &obd->u.mds;
632 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
633 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
637 down(&mds->mds_qonoff_sem);
638 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
643 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
644 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
645 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
648 up(&mds->mds_qonoff_sem);
652 static int mds_init_slave_ilimits(struct obd_device *obd,
653 struct obd_quotactl *oqctl, int set)
655 /* XXX: for file limits only adjust local now */
656 unsigned int uid = 0, gid = 0;
657 struct obd_quotactl *ioqc = NULL;
661 /* if we are going to set zero limit, needn't init slaves */
662 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit)
672 ioqc->qc_cmd = Q_INITQUOTA;
673 ioqc->qc_id = oqctl->qc_id;
674 ioqc->qc_type = oqctl->qc_type;
675 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
676 ioqc->qc_dqblk.dqb_ihardlimit = MIN_QLIMIT;
678 /* set local limit to MIN_QLIMIT */
679 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
683 /* trigger local qunit pre-acquire */
684 if (oqctl->qc_type == USRQUOTA)
689 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0);
691 CERROR("error mds adjust local file quota! (rc:%d)\n", rc);
694 /* FIXME initialize all slaves in CMD */
702 static int mds_init_slave_blimits(struct obd_device *obd,
703 struct obd_quotactl *oqctl, int set)
705 struct mds_obd *mds = &obd->u.mds;
706 struct obd_quotactl *ioqc;
707 unsigned int uid = 0, gid = 0;
711 /* if we are going to set zero limit, needn't init slaves */
712 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit)
719 ioqc->qc_cmd = Q_INITQUOTA;
720 ioqc->qc_id = oqctl->qc_id;
721 ioqc->qc_type = oqctl->qc_type;
722 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
723 ioqc->qc_dqblk.dqb_bhardlimit = set ? MIN_QLIMIT : 0;
725 /* set local limit to MIN_QLIMIT */
727 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
732 /* trigger local qunit pre-acquire */
733 if (oqctl->qc_type == USRQUOTA)
738 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0);
740 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
744 /* initialize all slave's limit */
745 rc = obd_quotactl(mds->mds_osc_exp, ioqc);
752 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
754 struct mds_obd *mds = &obd->u.mds;
755 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
756 __u32 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
758 struct lustre_dquot *dquot;
759 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
763 down(&mds->mds_qonoff_sem);
764 if (qinfo->qi_files[oqctl->qc_type] == NULL)
765 GOTO(out_sem, rc = -ESRCH);
767 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
769 GOTO(out_sem, rc = PTR_ERR(dquot));
770 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
771 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
773 down(&dquot->dq_sem);
775 if (dquot->dq_status) {
778 GOTO(out_sem, rc = -EBUSY);
780 dquot->dq_status |= DQ_STATUS_SET;
782 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
783 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
784 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
785 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
786 btime = dquot->dq_dqb.dqb_btime;
787 itime = dquot->dq_dqb.dqb_itime;
789 if (dqblk->dqb_valid & QIF_BTIME)
790 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
791 if (dqblk->dqb_valid & QIF_ITIME)
792 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
794 if (dqblk->dqb_valid & QIF_BLIMITS) {
795 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
796 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
797 /* clear usage (limit pool) */
798 if (!dquot->dq_dqb.dqb_bhardlimit &&
799 !dquot->dq_dqb.dqb_bsoftlimit)
800 dquot->dq_dqb.dqb_curspace = 0;
802 /* clear grace time */
803 if (!dqblk->dqb_bsoftlimit ||
804 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
805 dquot->dq_dqb.dqb_btime = 0;
806 /* set grace only if user hasn't provided his own */
807 else if (!(dqblk->dqb_valid & QIF_BTIME))
808 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
809 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
812 if (dqblk->dqb_valid & QIF_ILIMITS) {
813 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
814 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
815 /* clear usage (limit pool) */
816 if (!dquot->dq_dqb.dqb_ihardlimit &&
817 !dquot->dq_dqb.dqb_isoftlimit)
818 dquot->dq_dqb.dqb_curinodes = 0;
820 if (!dqblk->dqb_isoftlimit ||
821 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
822 dquot->dq_dqb.dqb_itime = 0;
823 else if (!(dqblk->dqb_valid & QIF_ITIME))
824 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
825 qinfo->qi_info[dquot->dq_type].dqi_igrace;
828 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
833 CERROR("set limit failed! (rc:%d)\n", rc);
837 up(&mds->mds_qonoff_sem);
838 if (dqblk->dqb_valid & QIF_ILIMITS) {
839 set = !(ihardlimit || isoftlimit);
840 rc = mds_init_slave_ilimits(obd, oqctl, set);
842 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
847 if (dqblk->dqb_valid & QIF_BLIMITS) {
848 set = !(bhardlimit || bsoftlimit);
849 rc = mds_init_slave_blimits(obd, oqctl, set);
851 CERROR("init slave blimits failed! (rc:%d)\n", rc);
855 down(&mds->mds_qonoff_sem);
859 /* cancel previous setting */
860 down(&dquot->dq_sem);
861 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
862 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
863 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
864 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
865 dquot->dq_dqb.dqb_btime = btime;
866 dquot->dq_dqb.dqb_itime = itime;
867 fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
871 down(&dquot->dq_sem);
872 dquot->dq_status &= ~DQ_STATUS_SET;
877 up(&mds->mds_qonoff_sem);
881 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
883 struct obd_quotactl *soqc;
884 struct lvfs_run_ctxt saved;
892 soqc->qc_cmd = Q_GETOQUOTA;
893 soqc->qc_id = oqctl->qc_id;
894 soqc->qc_type = oqctl->qc_type;
896 rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
900 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
902 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
903 soqc->qc_dqblk.dqb_curspace = 0;
904 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
905 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
910 oqctl->qc_dqblk.dqb_curinodes += soqc->qc_dqblk.dqb_curinodes;
911 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
918 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
920 struct mds_obd *mds = &obd->u.mds;
921 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
922 struct lustre_dquot *dquot;
923 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
927 down(&mds->mds_qonoff_sem);
928 if (qinfo->qi_files[oqctl->qc_type] == NULL)
929 GOTO(out, rc = -ESRCH);
931 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
933 GOTO(out, rc = PTR_ERR(dquot));
935 down(&dquot->dq_sem);
936 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
937 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
938 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
939 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
940 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
941 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
946 /* the usages in admin quota file is inaccurate */
947 dqblk->dqb_curinodes = 0;
948 dqblk->dqb_curspace = 0;
949 rc = mds_get_space(obd, oqctl);
952 up(&mds->mds_qonoff_sem);
956 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
958 struct lvfs_run_ctxt saved;
962 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
963 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
964 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
970 /* FIXME we only recovery block limit by now, need recovery inode
971 * limits also after CMD involved in */
973 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
975 struct mds_obd *mds = &obd->u.mds;
976 struct lustre_quota_info *qinfo= &obd->u.mds.mds_quota_info;
977 struct lustre_dquot *dquot;
978 struct obd_quotactl *qctl;
979 __u64 total_limits = 0;
987 dquot = lustre_dqget(obd, qinfo, id, type);
989 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
991 RETURN(PTR_ERR(dquot));
994 down(&dquot->dq_sem);
996 /* don't recovery the dquot without limits or under setting */
997 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1000 dquot->dq_status |= DQ_STATUS_RECOVERY;
1004 /* get real bhardlimit from all slaves. */
1005 qctl->qc_cmd = Q_GETOQUOTA;
1006 qctl->qc_type = type;
1008 qctl->qc_stat = QUOTA_RECOVERING;
1009 rc = obd_quotactl(obd->u.mds.mds_osc_exp, qctl);
1012 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1014 /* get real bhardlimit from master */
1015 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1018 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1020 /* amend the usage of the administrative quotafile */
1021 down(&mds->mds_qonoff_sem);
1022 down(&dquot->dq_sem);
1024 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1026 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1028 CERROR("write dquot failed! (rc:%d)\n", rc);
1031 up(&mds->mds_qonoff_sem);
1034 down(&dquot->dq_sem);
1035 dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1039 lustre_dqput(dquot);
1044 struct qmaster_recov_thread_data {
1045 struct obd_device *obd;
1046 struct completion comp;
1049 static int qmaster_recovery_main(void *arg)
1051 struct qmaster_recov_thread_data *data = arg;
1052 struct obd_device *obd = data->obd;
1054 unsigned short type;
1057 ptlrpc_daemonize("qmaster_recovd");
1059 complete(&data->comp);
1061 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1062 struct mds_obd *mds = &obd->u.mds;
1063 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1064 struct list_head id_list;
1065 struct dquot_id *dqid, *tmp;
1067 down(&mds->mds_qonoff_sem);
1068 if (qinfo->qi_files[type] == NULL) {
1069 up(&mds->mds_qonoff_sem);
1072 INIT_LIST_HEAD(&id_list);
1073 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1075 up(&mds->mds_qonoff_sem);
1078 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1080 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1081 list_del_init(&dqid->di_link);
1085 rc = dquot_recovery(obd, dqid->di_id, type);
1087 CERROR("qmaster recovery failed! (id:%d type:%d"
1088 " rc:%d)\n", dqid->di_id, type, rc);
1096 int mds_quota_recovery(struct obd_device *obd)
1098 struct lov_obd *lov = &obd->u.mds.mds_osc_obd->u.lov;
1099 struct qmaster_recov_thread_data data;
1103 mutex_down(&lov->lov_lock);
1104 if (lov->desc.ld_tgt_count != lov->desc.ld_active_tgt_count) {
1105 CWARN("Not all osts are active, abort quota recovery\n");
1106 mutex_up(&lov->lov_lock);
1109 mutex_up(&lov->lov_lock);
1112 init_completion(&data.comp);
1114 rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1116 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1118 wait_for_completion(&data.comp);