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 <linux/obd_class.h>
31 #include <linux/lustre_quota.h>
32 #include <linux/lustre_fsfilt.h>
33 #include <linux/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) {
73 rc = kmem_cache_destroy(lustre_dquot_cachep);
75 lustre_dquot_cachep = NULL;
80 static inline int const dquot_hashfn(struct lustre_quota_info *info,
81 unsigned int id, int type)
83 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
84 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
88 /* caller must hold dquot_hash_lock */
89 static struct lustre_dquot *find_dquot(int hashent,
90 struct lustre_quota_info *lqi, qid_t id,
93 struct lustre_dquot *dquot;
96 LASSERT_SPIN_LOCKED(&dquot_hash_lock);
97 list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
98 if (dquot->dq_info == lqi &&
99 dquot->dq_id == id && dquot->dq_type == type)
105 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
108 struct lustre_dquot *dquot = NULL;
111 OBD_SLAB_ALLOC(dquot, lustre_dquot_cachep, SLAB_NOFS, sizeof(*dquot));
115 INIT_LIST_HEAD(&dquot->dq_hash);
116 init_mutex_locked(&dquot->dq_sem);
117 dquot->dq_refcnt = 1;
118 dquot->dq_info = lqi;
120 dquot->dq_type = type;
121 dquot->dq_status = DQ_STATUS_AVAIL;
126 static void free_dquot(struct lustre_dquot *dquot)
128 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
131 static void insert_dquot_nolock(struct lustre_dquot *dquot)
133 struct list_head *head = lustre_dquot_hash +
134 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
135 LASSERT(list_empty(&dquot->dq_hash));
136 list_add(&dquot->dq_hash, head);
139 static void remove_dquot_nolock(struct lustre_dquot *dquot)
141 LASSERT(!list_empty(&dquot->dq_hash));
142 list_del_init(&dquot->dq_hash);
145 static void lustre_dqput(struct lustre_dquot *dquot)
148 spin_lock(&dquot_hash_lock);
149 LASSERT(dquot->dq_refcnt);
151 if (!dquot->dq_refcnt) {
152 remove_dquot_nolock(dquot);
155 spin_unlock(&dquot_hash_lock);
159 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
160 struct lustre_quota_info *lqi,
163 unsigned int hashent = dquot_hashfn(lqi, id, type);
164 struct lustre_dquot *dquot, *empty;
167 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
168 RETURN(ERR_PTR(-ENOMEM));
170 spin_lock(&dquot_hash_lock);
171 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
173 spin_unlock(&dquot_hash_lock);
179 insert_dquot_nolock(dquot);
180 spin_unlock(&dquot_hash_lock);
182 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
185 CERROR("can't read dquot from admin quotafile! "
197 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
199 struct mds_obd *mds = &obd->u.mds;
200 struct lustre_quota_info *info = &mds->mds_quota_info;
201 struct lustre_dquot *dquot = NULL;
203 __u32 hlimit = 0, slimit = 0;
205 unsigned int grace = 0;
209 OBD_FAIL_RETURN(OBD_FAIL_OBD_DQACQ, -EIO);
211 /* slaves never acquires qunit for user root */
212 LASSERT(qdata->qd_id || qdata->qd_type == GRPQUOTA);
214 dquot = lustre_dqget(obd, info, qdata->qd_id, qdata->qd_type);
216 RETURN(PTR_ERR(dquot));
218 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
219 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
221 down(&mds->mds_qonoff_sem);
222 down(&dquot->dq_sem);
224 if (dquot->dq_status & DQ_STATUS_RECOVERY) {
225 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
226 GOTO(out, rc = -EBUSY);
229 if (qdata->qd_isblk) {
230 grace = info->qi_info[qdata->qd_type].dqi_bgrace;
231 usage = &dquot->dq_dqb.dqb_curspace;
232 hlimit = dquot->dq_dqb.dqb_bhardlimit;
233 slimit = dquot->dq_dqb.dqb_bsoftlimit;
234 time = &dquot->dq_dqb.dqb_btime;
236 grace = info->qi_info[qdata->qd_type].dqi_igrace;
237 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
238 hlimit = dquot->dq_dqb.dqb_ihardlimit;
239 slimit = dquot->dq_dqb.dqb_isoftlimit;
240 time = &dquot->dq_dqb.dqb_itime;
243 /* if the quota limit in admin quotafile is zero, we just inform
244 * slave to clear quota limit with zero qd_count */
245 if (hlimit == 0 && slimit == 0) {
253 QUSG(*usage + qdata->qd_count, qdata->qd_isblk) > hlimit)
254 GOTO(out, rc = -EDQUOT);
257 QUSG(*usage + qdata->qd_count, qdata->qd_isblk) > slimit) {
258 if (*time && cfs_time_current_sec() >= *time)
259 GOTO(out, rc = -EDQUOT);
261 *time = cfs_time_current_sec() + grace;
264 *usage += qdata->qd_count;
267 /* The usage in administrative file might be incorrect before
269 if (*usage - qdata->qd_count < 0)
272 *usage -= qdata->qd_count;
274 /* (usage <= soft limit) but not (usage < soft limit) */
275 if (!slimit || QUSG(*usage, qdata->qd_isblk) <= slimit)
282 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
286 up(&mds->mds_qonoff_sem);
291 int mds_quota_adjust(struct obd_device *obd, unsigned int qcids[],
292 unsigned int qpids[], int rc, int opc)
294 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
298 if (rc && rc != -EDQUOT)
302 case FSFILT_OP_RENAME:
303 /* acquire/release block quota on owner of original parent */
304 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[2], qpids[3], 1, 0);
306 case FSFILT_OP_SETATTR:
307 /* acquire/release file quota on original owner */
308 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 0, 0);
310 case FSFILT_OP_CREATE:
311 case FSFILT_OP_UNLINK:
312 /* acquire/release file/block quota on owner of child (or current owner) */
313 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0);
314 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
315 /* acquire/release block quota on owner of parent (or original owner) */
316 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
324 CERROR("mds adjust qunit failed! (opc:%d rc:%d)\n", opc, rc2);
328 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
329 unsigned int qpids[], int rc, int opc)
331 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
335 if (rc && rc != -EDQUOT)
339 case FSFILT_OP_SETATTR:
340 /* acquire/release block quota on original & current owner */
341 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
342 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
344 case FSFILT_OP_UNLINK:
345 /* release block quota on this owner */
346 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
347 /* acquire block quota on this owner */
348 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
356 CERROR("filter adjust qunit failed! (opc:%d rc%d)\n",
361 #define LUSTRE_ADMIN_QUOTAFILES {\
362 "admin_quotafile.usr", /* user admin quotafile */\
363 "admin_quotafile.grp" /* group admin quotafile */\
365 static const char prefix[] = "OBJECTS/";
367 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
369 struct mds_obd *mds = &obd->u.mds;
370 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
371 const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
372 struct lvfs_run_ctxt saved;
375 struct dentry *dparent = mds->mds_objects_dir;
376 struct inode *iparent = dparent->d_inode;
380 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
382 down(&mds->mds_qonoff_sem);
383 for (i = 0; i < MAXQUOTAS; i++) {
387 if (!Q_TYPESET(oqctl, i))
390 /* quota file has been opened ? */
391 if (qinfo->qi_files[i]) {
392 CWARN("init %s admin quotafile while quota on.\n",
393 i == USRQUOTA ? "user" : "group");
397 /* lookup quota file */
399 LOCK_INODE_MUTEX(iparent);
400 de = lookup_one_len(quotafiles[i], dparent,
401 strlen(quotafiles[i]));
402 UNLOCK_INODE_MUTEX(iparent);
403 if (IS_ERR(de) || de->d_inode == NULL ||
404 !S_ISREG(de->d_inode->i_mode))
405 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
409 if (rc && rc != -ENOENT) {
410 CERROR("error lookup quotafile %s! (rc:%d)\n",
417 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
418 sprintf(name, "%s%s", prefix, quotafiles[i]);
420 LASSERT(rc == -ENOENT);
421 /* create quota file */
422 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
423 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
425 CERROR("error creating admin quotafile %s (rc:%d)\n",
430 qinfo->qi_files[i] = fp;
431 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
433 qinfo->qi_files[i] = NULL;
436 CERROR("error init %s admin quotafile! (rc:%d)\n",
437 i == USRQUOTA ? "user" : "group", rc);
441 up(&mds->mds_qonoff_sem);
443 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
447 static int close_quota_files(struct obd_quotactl *oqctl,
448 struct lustre_quota_info *qinfo)
453 for (i = 0; i < MAXQUOTAS; i++) {
454 if (!Q_TYPESET(oqctl, i))
456 if (qinfo->qi_files[i] == NULL) {
460 filp_close(qinfo->qi_files[i], 0);
461 qinfo->qi_files[i] = NULL;
466 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
468 struct mds_obd *mds = &obd->u.mds;
469 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
470 const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
473 struct inode *iparent = mds->mds_objects_dir->d_inode;
478 /* open admin quota files and read quotafile info */
479 for (i = 0; i < MAXQUOTAS; i++) {
482 if (!Q_TYPESET(oqctl, i))
485 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
486 sprintf(name, "%s%s", prefix, quotafiles[i]);
488 if (qinfo->qi_files[i] != NULL) {
493 fp = filp_open(name, O_RDWR | O_EXCL, 0644);
494 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
496 CERROR("error open %s! (rc:%d)\n", name, rc);
499 qinfo->qi_files[i] = fp;
501 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
503 CERROR("error read quotainfo of %s! (rc:%d)\n",
509 if (rc && rc != -EBUSY)
510 close_quota_files(oqctl, qinfo);
515 static int mds_admin_quota_off(struct obd_device *obd,
516 struct obd_quotactl *oqctl)
518 struct mds_obd *mds = &obd->u.mds;
519 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
523 /* close admin quota files */
524 rc = close_quota_files(oqctl, qinfo);
528 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
530 struct mds_obd *mds = &obd->u.mds;
531 struct lvfs_run_ctxt saved;
535 down(&mds->mds_qonoff_sem);
536 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
537 rc = mds_admin_quota_on(obd, oqctl);
541 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
545 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
547 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
548 up(&mds->mds_qonoff_sem);
552 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
554 struct mds_obd *mds = &obd->u.mds;
555 struct lvfs_run_ctxt saved;
559 down(&mds->mds_qonoff_sem);
560 /* close admin quota files */
561 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
562 mds_admin_quota_off(obd, oqctl);
564 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
565 rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
567 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
568 up(&mds->mds_qonoff_sem);
572 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
574 struct mds_obd *mds = &obd->u.mds;
575 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
576 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
580 down(&mds->mds_qonoff_sem);
581 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
586 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
587 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
588 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
590 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
593 up(&mds->mds_qonoff_sem);
597 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
599 struct mds_obd *mds = &obd->u.mds;
600 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
601 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
605 down(&mds->mds_qonoff_sem);
606 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
611 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
612 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
613 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
616 up(&mds->mds_qonoff_sem);
620 static int mds_init_slave_ilimits(struct obd_device *obd,
621 struct obd_quotactl *oqctl, int set)
623 /* XXX: for file limits only adjust local now */
624 unsigned int uid = 0, gid = 0;
625 struct obd_quotactl *ioqc = NULL;
629 /* if we are going to set zero limit, needn't init slaves */
630 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit)
640 ioqc->qc_cmd = Q_INITQUOTA;
641 ioqc->qc_id = oqctl->qc_id;
642 ioqc->qc_type = oqctl->qc_type;
643 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
644 ioqc->qc_dqblk.dqb_ihardlimit = MIN_QLIMIT;
646 /* set local limit to MIN_QLIMIT */
647 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
651 /* trigger local qunit pre-acquire */
652 if (oqctl->qc_type == USRQUOTA)
657 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0);
659 CERROR("error mds adjust local file quota! (rc:%d)\n", rc);
662 /* FIXME initialize all slaves in CMD */
670 static int mds_init_slave_blimits(struct obd_device *obd,
671 struct obd_quotactl *oqctl, int set)
673 struct mds_obd *mds = &obd->u.mds;
674 struct obd_quotactl *ioqc;
675 unsigned int uid = 0, gid = 0;
679 /* if we are going to set zero limit, needn't init slaves */
680 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit)
687 ioqc->qc_cmd = Q_INITQUOTA;
688 ioqc->qc_id = oqctl->qc_id;
689 ioqc->qc_type = oqctl->qc_type;
690 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
691 ioqc->qc_dqblk.dqb_bhardlimit = set ? MIN_QLIMIT : 0;
693 /* set local limit to MIN_QLIMIT */
695 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
700 /* trigger local qunit pre-acquire */
701 if (oqctl->qc_type == USRQUOTA)
706 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0);
708 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
712 /* initialize all slave's limit */
713 rc = obd_quotactl(mds->mds_osc_exp, ioqc);
720 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
722 struct mds_obd *mds = &obd->u.mds;
723 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
724 __u32 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
726 struct lustre_dquot *dquot;
727 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
731 down(&mds->mds_qonoff_sem);
732 if (qinfo->qi_files[oqctl->qc_type] == NULL)
733 GOTO(out_sem, rc = -ESRCH);
735 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
737 GOTO(out_sem, rc = PTR_ERR(dquot));
738 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
739 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
741 down(&dquot->dq_sem);
743 if (dquot->dq_status) {
746 GOTO(out_sem, rc = -EBUSY);
748 dquot->dq_status |= DQ_STATUS_SET;
750 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
751 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
752 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
753 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
754 btime = dquot->dq_dqb.dqb_btime;
755 itime = dquot->dq_dqb.dqb_itime;
757 if (dqblk->dqb_valid & QIF_BTIME)
758 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
759 if (dqblk->dqb_valid & QIF_ITIME)
760 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
762 if (dqblk->dqb_valid & QIF_BLIMITS) {
763 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
764 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
765 /* clear usage (limit pool) */
766 if (!dquot->dq_dqb.dqb_bhardlimit &&
767 !dquot->dq_dqb.dqb_bsoftlimit)
768 dquot->dq_dqb.dqb_curspace = 0;
770 /* clear grace time */
771 if (!dqblk->dqb_bsoftlimit ||
772 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
773 dquot->dq_dqb.dqb_btime = 0;
774 /* set grace only if user hasn't provided his own */
775 else if (!(dqblk->dqb_valid & QIF_BTIME))
776 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
777 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
780 if (dqblk->dqb_valid & QIF_ILIMITS) {
781 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
782 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
783 /* clear usage (limit pool) */
784 if (!dquot->dq_dqb.dqb_ihardlimit &&
785 !dquot->dq_dqb.dqb_isoftlimit)
786 dquot->dq_dqb.dqb_curinodes = 0;
788 if (!dqblk->dqb_isoftlimit ||
789 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
790 dquot->dq_dqb.dqb_itime = 0;
791 else if (!(dqblk->dqb_valid & QIF_ITIME))
792 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
793 qinfo->qi_info[dquot->dq_type].dqi_igrace;
796 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
801 CERROR("set limit failed! (rc:%d)\n", rc);
805 up(&mds->mds_qonoff_sem);
806 if (dqblk->dqb_valid & QIF_ILIMITS) {
807 set = !(ihardlimit || isoftlimit);
808 rc = mds_init_slave_ilimits(obd, oqctl, set);
810 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
815 if (dqblk->dqb_valid & QIF_BLIMITS) {
816 set = !(bhardlimit || bsoftlimit);
817 rc = mds_init_slave_blimits(obd, oqctl, set);
819 CERROR("init slave blimits failed! (rc:%d)\n", rc);
823 down(&mds->mds_qonoff_sem);
827 /* cancel previous setting */
828 down(&dquot->dq_sem);
829 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
830 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
831 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
832 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
833 dquot->dq_dqb.dqb_btime = btime;
834 dquot->dq_dqb.dqb_itime = itime;
835 fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
839 down(&dquot->dq_sem);
840 dquot->dq_status &= ~DQ_STATUS_SET;
845 up(&mds->mds_qonoff_sem);
849 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
851 struct obd_quotactl *soqc;
852 struct lvfs_run_ctxt saved;
860 soqc->qc_cmd = Q_GETOQUOTA;
861 soqc->qc_id = oqctl->qc_id;
862 soqc->qc_type = oqctl->qc_type;
864 rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
868 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
870 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
871 soqc->qc_dqblk.dqb_curspace = 0;
872 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
873 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
878 oqctl->qc_dqblk.dqb_curinodes += soqc->qc_dqblk.dqb_curinodes;
879 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
886 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
888 struct mds_obd *mds = &obd->u.mds;
889 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
890 struct lustre_dquot *dquot;
891 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
895 down(&mds->mds_qonoff_sem);
896 if (qinfo->qi_files[oqctl->qc_type] == NULL)
897 GOTO(out, rc = -ESRCH);
899 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
901 GOTO(out, rc = PTR_ERR(dquot));
903 down(&dquot->dq_sem);
904 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
905 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
906 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
907 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
908 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
909 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
914 /* the usages in admin quota file is inaccurate */
915 dqblk->dqb_curinodes = 0;
916 dqblk->dqb_curspace = 0;
917 rc = mds_get_space(obd, oqctl);
920 up(&mds->mds_qonoff_sem);
924 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
926 struct lvfs_run_ctxt saved;
930 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
931 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
932 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
938 /* FIXME we only recovery block limit by now, need recovery inode
939 * limits also after CMD involved in */
941 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
943 struct mds_obd *mds = &obd->u.mds;
944 struct lustre_quota_info *qinfo= &obd->u.mds.mds_quota_info;
945 struct lustre_dquot *dquot;
946 struct obd_quotactl *qctl;
947 __u64 total_limits = 0;
955 dquot = lustre_dqget(obd, qinfo, id, type);
957 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
959 RETURN(PTR_ERR(dquot));
962 down(&dquot->dq_sem);
964 /* don't recovery the dquot without limits or under setting */
965 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
968 dquot->dq_status |= DQ_STATUS_RECOVERY;
972 /* get real bhardlimit from all slaves. */
973 qctl->qc_cmd = Q_GETOQUOTA;
974 qctl->qc_type = type;
976 qctl->qc_stat = QUOTA_RECOVERING;
977 rc = obd_quotactl(obd->u.mds.mds_osc_exp, qctl);
980 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
982 /* get real bhardlimit from master */
983 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
986 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
988 /* amend the usage of the administrative quotafile */
989 down(&mds->mds_qonoff_sem);
990 down(&dquot->dq_sem);
992 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
994 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
996 CERROR("write dquot failed! (rc:%d)\n", rc);
999 up(&mds->mds_qonoff_sem);
1002 down(&dquot->dq_sem);
1003 dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1007 lustre_dqput(dquot);
1012 struct qmaster_recov_thread_data {
1013 struct obd_device *obd;
1014 struct completion comp;
1017 static int qmaster_recovery_main(void *arg)
1019 struct qmaster_recov_thread_data *data = arg;
1020 struct obd_device *obd = data->obd;
1022 unsigned short type;
1025 ptlrpc_daemonize("qmaster_recovd");
1027 complete(&data->comp);
1029 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1030 struct mds_obd *mds = &obd->u.mds;
1031 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1032 struct list_head id_list;
1033 struct dquot_id *dqid, *tmp;
1035 down(&mds->mds_qonoff_sem);
1036 if (qinfo->qi_files[type] == NULL) {
1037 up(&mds->mds_qonoff_sem);
1040 INIT_LIST_HEAD(&id_list);
1041 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1043 up(&mds->mds_qonoff_sem);
1046 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1048 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1049 list_del_init(&dqid->di_link);
1053 rc = dquot_recovery(obd, dqid->di_id, type);
1055 CERROR("qmaster recovery failed! (id:%d type:%d"
1056 " rc:%d)\n", dqid->di_id, type, rc);
1064 int mds_quota_recovery(struct obd_device *obd)
1066 struct lov_obd *lov = &obd->u.mds.mds_osc_obd->u.lov;
1067 struct qmaster_recov_thread_data data;
1071 down(&lov->lov_lock);
1072 if (lov->desc.ld_tgt_count != lov->desc.ld_active_tgt_count) {
1073 CWARN("Not all osts are active, abort quota recovery\n");
1080 init_completion(&data.comp);
1082 rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1084 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1086 wait_for_completion(&data.comp);