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.
36 * lustre/quota/quota_master.c
38 * Lustre Quota Master request handler
40 * Author: Niu YaWei <niu@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_LQUOTA
49 #include <linux/version.h>
51 #include <asm/unistd.h>
52 #include <linux/slab.h>
53 #include <linux/quotaops.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/quota.h>
58 #include <obd_class.h>
59 #include <lustre_quota.h>
60 #include <lustre_fsfilt.h>
61 #include <lustre_mds.h>
63 #include "quota_internal.h"
65 #ifdef HAVE_QUOTA_SUPPORT
67 /* lock ordering: mds->mds_qonoff_sem > dquot->dq_sem */
68 static struct list_head lustre_dquot_hash[NR_DQHASH];
69 static spinlock_t dquot_hash_lock = SPIN_LOCK_UNLOCKED;
71 cfs_mem_cache_t *lustre_dquot_cachep;
73 int lustre_dquot_init(void)
78 LASSERT(lustre_dquot_cachep == NULL);
79 lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
80 sizeof(struct lustre_dquot),
82 if (!lustre_dquot_cachep)
85 for (i = 0; i < NR_DQHASH; i++) {
86 CFS_INIT_LIST_HEAD(lustre_dquot_hash + i);
91 void lustre_dquot_exit(void)
95 /* FIXME cleanup work ?? */
97 for (i = 0; i < NR_DQHASH; i++) {
98 LASSERT(list_empty(lustre_dquot_hash + i));
100 if (lustre_dquot_cachep) {
102 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
103 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
104 lustre_dquot_cachep = NULL;
110 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
111 __attribute__((__const__));
114 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
116 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
117 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
121 /* caller must hold dquot_hash_lock */
122 static struct lustre_dquot *find_dquot(int hashent,
123 struct lustre_quota_info *lqi, qid_t id,
126 struct lustre_dquot *dquot;
129 LASSERT_SPIN_LOCKED(&dquot_hash_lock);
130 list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
131 if (dquot->dq_info == lqi &&
132 dquot->dq_id == id && dquot->dq_type == type)
138 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
141 struct lustre_dquot *dquot = NULL;
144 OBD_SLAB_ALLOC_PTR_GFP(dquot, lustre_dquot_cachep, CFS_ALLOC_IO);
148 CFS_INIT_LIST_HEAD(&dquot->dq_hash);
149 init_mutex_locked(&dquot->dq_sem);
150 dquot->dq_refcnt = 1;
151 dquot->dq_info = lqi;
153 dquot->dq_type = type;
154 dquot->dq_status = DQ_STATUS_AVAIL;
159 static void free_dquot(struct lustre_dquot *dquot)
161 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
164 static void insert_dquot_nolock(struct lustre_dquot *dquot)
166 struct list_head *head = lustre_dquot_hash +
167 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
168 LASSERT(list_empty(&dquot->dq_hash));
169 list_add(&dquot->dq_hash, head);
172 static void remove_dquot_nolock(struct lustre_dquot *dquot)
174 LASSERT(!list_empty(&dquot->dq_hash));
175 list_del_init(&dquot->dq_hash);
178 static void lustre_dqput(struct lustre_dquot *dquot)
181 spin_lock(&dquot_hash_lock);
182 LASSERT(dquot->dq_refcnt);
184 if (!dquot->dq_refcnt) {
185 remove_dquot_nolock(dquot);
188 spin_unlock(&dquot_hash_lock);
192 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
193 struct lustre_quota_info *lqi,
196 unsigned int hashent = dquot_hashfn(lqi, id, type);
197 struct lustre_dquot *dquot, *empty;
200 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
201 RETURN(ERR_PTR(-ENOMEM));
203 spin_lock(&dquot_hash_lock);
204 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
206 spin_unlock(&dquot_hash_lock);
212 insert_dquot_nolock(dquot);
213 spin_unlock(&dquot_hash_lock);
215 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
218 CERROR("can't read dquot from admin quotafile! "
230 static void init_oqaq(struct quota_adjust_qunit *oqaq,
231 struct lustre_quota_ctxt *qctxt,
234 struct lustre_qunit_size *lqs = NULL;
237 oqaq->qaq_flags = type;
238 quota_search_lqs(NULL, oqaq, qctxt, &lqs);
240 spin_lock(&lqs->lqs_lock);
241 oqaq->qaq_bunit_sz = lqs->lqs_bunit_sz;
242 oqaq->qaq_iunit_sz = lqs->lqs_iunit_sz;
243 oqaq->qaq_flags = lqs->lqs_flags;
244 spin_unlock(&lqs->lqs_lock);
247 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
248 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
249 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
253 int dqacq_adjust_qunit_sz(struct obd_device *obd, qid_t id, int type,
256 struct mds_obd *mds = &obd->u.mds;
257 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
258 struct obd_device *lov_mds_obd = class_exp2obd(mds->mds_osc_exp);
259 struct lov_obd *lov = &lov_mds_obd->u.lov;
260 __u32 ost_num = lov->desc.ld_tgt_count, mdt_num = 1;
261 struct quota_adjust_qunit *oqaq = NULL;
262 unsigned int uid = 0, gid = 0;
263 struct lustre_quota_info *info = &mds->mds_quota_info;
264 struct lustre_dquot *dquot = NULL;
270 dquot = lustre_dqget(obd, info, id, type);
272 RETURN(PTR_ERR(dquot));
276 GOTO(out, rc = -ENOMEM);
278 down(&dquot->dq_sem);
279 init_oqaq(oqaq, qctxt, id, type);
281 rc = dquot_create_oqaq(qctxt, dquot, ost_num, mdt_num,
282 is_blk ? LQUOTA_FLAGS_ADJBLK :
283 LQUOTA_FLAGS_ADJINO, oqaq);
286 CDEBUG(D_ERROR, "create oqaq failed! (rc:%d)\n", rc);
289 QAQ_DEBUG(oqaq, "show oqaq.\n")
291 if (!QAQ_IS_ADJBLK(oqaq) && !QAQ_IS_ADJINO(oqaq))
294 /* adjust the mds slave qunit size */
295 adjust_res = quota_adjust_slave_lqs(oqaq, qctxt);
296 if (adjust_res <= 0) {
297 if (adjust_res < 0) {
299 CDEBUG(D_ERROR, "adjust mds slave's qunit size failed! \
302 CDEBUG(D_QUOTA, "qunit doesn't need to be adjusted.\n");
314 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, is_blk, 0, NULL);
315 if (rc == -EDQUOT || rc == -EBUSY) {
316 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
320 CDEBUG(D_ERROR, "mds fail to adjust file quota! \
325 /* only when block qunit is reduced, boardcast to osts */
326 if ((adjust_res & LQS_BLK_DECREASE) && QAQ_IS_ADJBLK(oqaq))
327 rc = obd_quota_adjust_qunit(mds->mds_osc_exp, oqaq, qctxt);
340 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
342 struct mds_obd *mds = &obd->u.mds;
343 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
344 struct lustre_quota_info *info = &mds->mds_quota_info;
345 struct lustre_dquot *dquot = NULL;
347 __u64 hlimit = 0, slimit = 0;
349 unsigned int grace = 0;
350 struct lustre_qunit_size *lqs = NULL;
354 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_DQACQ))
357 dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata));
359 RETURN(PTR_ERR(dquot));
361 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
362 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
364 down(&mds->mds_qonoff_sem);
365 down(&dquot->dq_sem);
367 if (dquot->dq_status & DQ_STATUS_RECOVERY) {
368 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
369 GOTO(out, rc = -EBUSY);
372 if (QDATA_IS_BLK(qdata)) {
373 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
374 usage = &dquot->dq_dqb.dqb_curspace;
375 hlimit = dquot->dq_dqb.dqb_bhardlimit;
376 slimit = dquot->dq_dqb.dqb_bsoftlimit;
377 time = &dquot->dq_dqb.dqb_btime;
379 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
380 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
381 hlimit = dquot->dq_dqb.dqb_ihardlimit;
382 slimit = dquot->dq_dqb.dqb_isoftlimit;
383 time = &dquot->dq_dqb.dqb_itime;
386 /* if the quota limit in admin quotafile is zero, we just inform
387 * slave to clear quota limit with zero qd_count */
388 if (hlimit == 0 && slimit == 0) {
396 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
398 if (QDATA_IS_CHANGE_QS(qdata) &&
399 QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
400 qdata->qd_count = (hlimit -
401 QUSG(*usage, QDATA_IS_BLK(qdata)))
402 * (QDATA_IS_BLK(qdata) ?
403 QUOTABLOCK_SIZE : 1);
405 GOTO(out, rc = -EDQUOT);
409 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
410 if (*time && cfs_time_current_sec() >= *time)
411 GOTO(out, rc = -EDQUOT);
413 *time = cfs_time_current_sec() + grace;
416 *usage += qdata->qd_count;
419 /* The usage in administrative file might be incorrect before
421 if (*usage - qdata->qd_count < 0)
424 *usage -= qdata->qd_count;
426 /* (usage <= soft limit) but not (usage < soft limit) */
427 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
434 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
438 up(&mds->mds_qonoff_sem);
441 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
442 QDATA_IS_BLK(qdata));
444 quota_search_lqs(qdata, NULL, qctxt, &lqs);
445 if (QDATA_IS_BLK(qdata)) {
447 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
448 qdata->qd_qunit = qctxt->lqc_bunit_sz;
450 spin_lock(&lqs->lqs_lock);
451 qdata->qd_qunit = lqs->lqs_bunit_sz;
452 spin_unlock(&lqs->lqs_lock);
454 QDATA_SET_ADJBLK(qdata);
457 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
458 qdata->qd_qunit = qctxt->lqc_iunit_sz;
460 spin_lock(&lqs->lqs_lock);
461 qdata->qd_qunit = lqs->lqs_iunit_sz;
462 spin_unlock(&lqs->lqs_lock);
464 QDATA_SET_ADJINO(qdata);
467 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
474 int mds_quota_adjust(struct obd_device *obd, unsigned int qcids[],
475 unsigned int qpids[], int rc, int opc)
477 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
481 if (rc && rc != -EDQUOT && rc != ENOLCK)
485 case FSFILT_OP_SETATTR:
486 /* release file quota on original owner */
487 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 0, 0,
489 /* release block quota on original owner */
490 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
492 /* acquire file quota on current owner */
493 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
495 /* acquire block quota on current owner */
496 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
499 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
500 /* release file quota on child */
501 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
503 /* rlease block quota on child */
504 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
507 case FSFILT_OP_CREATE_PARTIAL_CHILD:
508 /* acquire file quota on child */
509 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
511 /* acquire block quota on child */
512 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
516 /* acquire block quota on parent */
517 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
520 case FSFILT_OP_UNLINK:
521 /* release block quota on parent */
522 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
524 /* release file quota on child */
525 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
527 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
528 /* release block quota on child */
529 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0],
530 qcids[1], 1, 0, NULL);
532 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
533 /* release block quota on parent */
534 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
537 case FSFILT_OP_CREATE:
538 /* acquire block quota on parent */
539 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
541 /* acquire file quota on child */
542 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
544 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
545 /* acquire block quota on child */
546 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0],
547 qcids[1], 1, 0, NULL);
555 CDEBUG(rc2 == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
556 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
557 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
561 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
562 unsigned int qpids[], int rc, int opc)
564 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
568 if (rc && rc != -EDQUOT)
572 case FSFILT_OP_SETATTR:
573 /* acquire/release block quota on original & current owner */
574 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
576 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
579 case FSFILT_OP_UNLINK:
580 /* release block quota on this owner */
581 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
582 /* acquire block quota on this owner */
583 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
594 CDEBUG(rc == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
595 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
596 QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
602 static const char prefix[] = "OBJECTS/";
604 int mds_quota_invalidate(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;
609 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
611 struct lvfs_run_ctxt saved;
613 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
615 if (oqctl->qc_type != USRQUOTA &&
616 oqctl->qc_type != GRPQUOTA &&
617 oqctl->qc_type != UGQUOTA)
620 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
622 down(&mds->mds_qonoff_sem);
624 for (i = 0; i < MAXQUOTAS; i++) {
627 if (!Q_TYPESET(oqctl, i))
630 /* quota file has been opened ? */
631 if (qinfo->qi_files[i]) {
636 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
637 sprintf(name, "%s%s", prefix, quotafile[i]);
639 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
642 CERROR("error invalidating admin quotafile %s (rc:%d)\n",
650 up(&mds->mds_qonoff_sem);
652 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
657 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
659 struct mds_obd *mds = &obd->u.mds;
661 struct lvfs_run_ctxt saved;
663 if (oqctl->qc_type != USRQUOTA &&
664 oqctl->qc_type != GRPQUOTA &&
665 oqctl->qc_type != UGQUOTA)
668 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
669 down(&mds->mds_qonoff_sem);
671 oqctl->qc_cmd = Q_FINVALIDATE;
672 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
674 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
676 up(&mds->mds_qonoff_sem);
677 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
682 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
684 struct mds_obd *mds = &obd->u.mds;
685 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
686 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
687 struct lvfs_run_ctxt saved;
692 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
694 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
696 down(&mds->mds_qonoff_sem);
698 for (i = 0; i < MAXQUOTAS && !rc; i++) {
701 if (!Q_TYPESET(oqctl, i))
704 /* quota file has been opened ? */
705 if (qinfo->qi_files[i]) {
706 CWARN("init %s admin quotafile while quota on.\n",
707 i == USRQUOTA ? "user" : "group");
711 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
712 sprintf(name, "%s%s", prefix, quotafile[i]);
714 /* check if quota file exists and is correct */
715 fp = filp_open(name, O_RDONLY, 0);
717 /* irregular file is not the right place for quota */
718 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
719 CERROR("admin quota file %s is not "
725 qinfo->qi_files[i] = fp;
726 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
727 qinfo->qi_files[i] = 0;
736 /* -EINVAL may be returned by quotainfo for bad quota file */
737 if (rc != -ENOENT && rc != -EINVAL) {
738 CERROR("error opening old quota file %s (%d)\n",
743 CDEBUG(D_INFO, "%s new quota file %s\n", name,
744 rc == -ENOENT ? "creating" : "overwriting");
746 /* create quota file overwriting old if needed */
747 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
750 CERROR("error creating admin quotafile %s (rc:%d)\n",
755 qinfo->qi_files[i] = fp;
757 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
759 CERROR("error init %s admin quotafile! (rc:%d)\n",
760 i == USRQUOTA ? "user" : "group", rc);
763 qinfo->qi_files[i] = NULL;
765 up(&mds->mds_qonoff_sem);
767 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
771 static int close_quota_files(struct obd_quotactl *oqctl,
772 struct lustre_quota_info *qinfo)
777 for (i = 0; i < MAXQUOTAS; i++) {
778 if (!Q_TYPESET(oqctl, i))
780 if (qinfo->qi_files[i] == NULL) {
784 filp_close(qinfo->qi_files[i], 0);
785 qinfo->qi_files[i] = NULL;
790 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
792 struct mds_obd *mds = &obd->u.mds;
793 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
794 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
799 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
801 /* open admin quota files and read quotafile info */
802 for (i = 0; i < MAXQUOTAS; i++) {
805 if (!Q_TYPESET(oqctl, i))
808 LASSERT(strlen(quotafile[i])
809 + sizeof(prefix) <= sizeof(name));
810 sprintf(name, "%s%s", prefix, quotafile[i]);
812 if (qinfo->qi_files[i] != NULL) {
817 fp = filp_open(name, O_RDWR, 0);
818 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
819 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
820 CERROR("error open/create %s! (rc:%d)\n", name, rc);
823 qinfo->qi_files[i] = fp;
825 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
827 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
831 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
833 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
839 if (rc && rc != -EBUSY)
840 close_quota_files(oqctl, qinfo);
845 int mds_admin_quota_off(struct obd_device *obd,
846 struct obd_quotactl *oqctl)
848 struct mds_obd *mds = &obd->u.mds;
849 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
853 /* close admin quota files */
854 rc = close_quota_files(oqctl, qinfo);
858 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
860 struct mds_obd *mds = &obd->u.mds;
861 struct obd_device_target *obt = &obd->u.obt;
862 struct lvfs_run_ctxt saved;
866 if (oqctl->qc_type != USRQUOTA &&
867 oqctl->qc_type != GRPQUOTA &&
868 oqctl->qc_type != UGQUOTA)
871 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
872 CDEBUG(D_INFO, "other people are doing quotacheck\n");
873 atomic_inc(&obt->obt_quotachecking);
877 down(&mds->mds_qonoff_sem);
878 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
879 rc = mds_admin_quota_on(obd, oqctl);
883 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
885 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
889 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
892 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
893 up(&mds->mds_qonoff_sem);
894 atomic_inc(&obt->obt_quotachecking);
898 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
900 struct mds_obd *mds = &obd->u.mds;
901 struct obd_device_target *obt = &obd->u.obt;
902 struct lvfs_run_ctxt saved;
906 if (oqctl->qc_type != USRQUOTA &&
907 oqctl->qc_type != GRPQUOTA &&
908 oqctl->qc_type != UGQUOTA)
911 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
912 CDEBUG(D_INFO, "other people are doing quotacheck\n");
913 atomic_inc(&obt->obt_quotachecking);
917 down(&mds->mds_qonoff_sem);
918 /* close admin quota files */
919 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
920 mds_admin_quota_off(obd, oqctl);
922 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
923 rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
925 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
927 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
928 up(&mds->mds_qonoff_sem);
929 atomic_inc(&obt->obt_quotachecking);
934 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
936 struct mds_obd *mds = &obd->u.mds;
937 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
938 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
942 if (oqctl->qc_type != USRQUOTA &&
943 oqctl->qc_type != GRPQUOTA)
946 down(&mds->mds_qonoff_sem);
947 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
952 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
953 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
954 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
956 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
959 up(&mds->mds_qonoff_sem);
963 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
965 struct mds_obd *mds = &obd->u.mds;
966 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
967 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
971 if (oqctl->qc_type != USRQUOTA &&
972 oqctl->qc_type != GRPQUOTA)
975 down(&mds->mds_qonoff_sem);
976 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
981 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
982 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
983 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
986 up(&mds->mds_qonoff_sem);
990 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
991 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
992 int type, struct quota_adjust_qunit *oqaq)
994 __u64 bunit_curr_o, iunit_curr_o;
995 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
996 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
997 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
998 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
999 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1000 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1004 if (!dquot || !oqaq)
1006 LASSERT_SEM_LOCKED(&dquot->dq_sem);
1007 LASSERT(oqaq->qaq_iunit_sz);
1008 LASSERT(oqaq->qaq_bunit_sz);
1010 /* don't change qunit size */
1011 if (!qctxt->lqc_switch_qs)
1014 bunit_curr_o = oqaq->qaq_bunit_sz;
1015 iunit_curr_o = oqaq->qaq_iunit_sz;
1017 if (dquot->dq_type == GRPQUOTA)
1020 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1021 __u64 b_limitation =
1022 oqaq->qaq_bunit_sz * ost_num * shrink_qunit_limit;
1023 /* enlarge block qunit size */
1025 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1026 oqaq->qaq_bunit_sz =
1027 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1029 b_limitation = oqaq->qaq_bunit_sz * ost_num *
1033 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1034 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1036 /* shrink block qunit size */
1038 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1039 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1040 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1042 b_limitation = oqaq->qaq_bunit_sz * ost_num *
1044 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1048 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1049 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1051 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1052 QAQ_SET_ADJBLK(oqaq);
1056 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1057 __u64 i_limitation =
1058 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1059 /* enlarge file qunit size */
1060 while (ilimit > dquot->dq_dqb.dqb_curinodes
1061 + 2 * i_limitation) {
1062 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1063 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1067 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1068 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1070 /* shrink file qunit size */
1071 while (ilimit < dquot->dq_dqb.dqb_curinodes
1073 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1074 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1076 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1080 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1081 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1083 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1084 QAQ_SET_ADJINO(oqaq);
1088 if (!dquot->dq_dqb.dqb_bhardlimit && !dquot->dq_dqb.dqb_bsoftlimit &&
1089 !dquot->dq_dqb.dqb_ihardlimit && !dquot->dq_dqb.dqb_isoftlimit) {
1090 oqaq->qaq_bunit_sz = 0;
1091 oqaq->qaq_iunit_sz = 0;
1092 QAQ_SET_ADJBLK(oqaq);
1093 QAQ_SET_ADJINO(oqaq);
1096 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1101 static int mds_init_slave_ilimits(struct obd_device *obd,
1102 struct obd_quotactl *oqctl, int set,
1103 struct quota_adjust_qunit *oqaq)
1105 /* XXX: for file limits only adjust local now */
1106 struct obd_device_target *obt = &obd->u.obt;
1107 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1108 unsigned int uid = 0, gid = 0;
1109 struct obd_quotactl *ioqc = NULL;
1114 /* if we are going to set zero limit, needn't init slaves */
1115 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1119 OBD_ALLOC_PTR(ioqc);
1123 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1124 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1125 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1126 ioqc->qc_id = oqctl->qc_id;
1127 ioqc->qc_type = oqctl->qc_type;
1128 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1129 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1131 if (QAQ_IS_ADJINO(oqaq)) {
1132 /* adjust the mds slave's inode qunit size */
1133 rc = quota_adjust_slave_lqs(oqaq, qctxt);
1135 CDEBUG(D_ERROR, "adjust mds slave's inode qunit size \
1136 failed! (rc:%d)\n", rc);
1139 /* set local limit to MIN_QLIMIT */
1140 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1144 /* trigger local qunit pre-acquire */
1145 if (oqctl->qc_type == USRQUOTA)
1150 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0,
1152 if (rc == -EDQUOT || rc == -EBUSY) {
1153 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1157 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1161 /* FIXME initialize all slaves in CMD */
1169 static int mds_init_slave_blimits(struct obd_device *obd,
1170 struct obd_quotactl *oqctl, int set,
1171 struct quota_adjust_qunit *oqaq)
1173 struct obd_device_target *obt = &obd->u.obt;
1174 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1175 struct mds_obd *mds = &obd->u.mds;
1176 struct obd_quotactl *ioqc;
1177 unsigned int uid = 0, gid = 0;
1182 /* if we are going to set zero limit, needn't init slaves */
1183 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1187 OBD_ALLOC_PTR(ioqc);
1191 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1192 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1193 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1194 ioqc->qc_id = oqctl->qc_id;
1195 ioqc->qc_type = oqctl->qc_type;
1196 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1197 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1198 if (QAQ_IS_ADJBLK(oqaq)) {
1199 /* adjust the mds slave's block qunit size */
1200 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
1202 CERROR("adjust mds slave's block qunit size failed!"
1206 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1210 /* trigger local qunit pre-acquire */
1211 if (oqctl->qc_type == USRQUOTA)
1216 /* initialize all slave's limit */
1217 rc = obd_quotactl(mds->mds_osc_exp, ioqc);
1219 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0,
1221 if (rc == -EDQUOT || rc == -EBUSY) {
1222 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1226 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1230 /* adjust all slave's qunit size when setting quota
1231 * this is will create a lqs for every ost, which will present
1232 * certain uid/gid is set quota or not */
1233 QAQ_SET_ADJBLK(oqaq);
1234 rc = obd_quota_adjust_qunit(mds->mds_osc_exp, oqaq, qctxt);
1242 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1244 struct mds_obd *mds = &obd->u.mds;
1245 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1246 struct obd_device *lov_obd = class_exp2obd(mds->mds_osc_exp);
1247 struct lov_obd *lov = &lov_obd->u.lov;
1248 struct quota_adjust_qunit *oqaq = NULL;
1249 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1250 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1251 time_t btime, itime;
1252 struct lustre_dquot *dquot;
1253 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1254 /* orig_set means if quota was set before; now_set means we are
1255 * setting/cancelling quota */
1256 int orig_set, now_set;
1257 int rc, rc2 = 0, flag = 0;
1260 if (oqctl->qc_type != USRQUOTA &&
1261 oqctl->qc_type != GRPQUOTA)
1264 OBD_ALLOC_PTR(oqaq);
1267 down(&mds->mds_qonoff_sem);
1268 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1270 if (qinfo->qi_files[oqctl->qc_type] == NULL)
1271 GOTO(out_sem, rc = -ESRCH);
1273 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1275 GOTO(out_sem, rc = PTR_ERR(dquot));
1276 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1277 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1279 down(&dquot->dq_sem);
1281 if (dquot->dq_status) {
1283 lustre_dqput(dquot);
1284 GOTO(out_sem, rc = -EBUSY);
1286 dquot->dq_status |= DQ_STATUS_SET;
1288 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1289 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1290 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1291 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1292 btime = dquot->dq_dqb.dqb_btime;
1293 itime = dquot->dq_dqb.dqb_itime;
1295 if (dqblk->dqb_valid & QIF_BTIME)
1296 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1297 if (dqblk->dqb_valid & QIF_ITIME)
1298 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1300 if (dqblk->dqb_valid & QIF_BLIMITS) {
1301 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1302 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1303 /* clear usage (limit pool) */
1304 if (!dquot->dq_dqb.dqb_bhardlimit &&
1305 !dquot->dq_dqb.dqb_bsoftlimit)
1306 dquot->dq_dqb.dqb_curspace = 0;
1308 /* clear grace time */
1309 if (!dqblk->dqb_bsoftlimit ||
1310 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1311 dquot->dq_dqb.dqb_btime = 0;
1312 /* set grace only if user hasn't provided his own */
1313 else if (!(dqblk->dqb_valid & QIF_BTIME))
1314 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1315 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1317 flag |= LQUOTA_FLAGS_ADJBLK;
1320 if (dqblk->dqb_valid & QIF_ILIMITS) {
1321 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1322 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1323 /* clear usage (limit pool) */
1324 if (!dquot->dq_dqb.dqb_ihardlimit &&
1325 !dquot->dq_dqb.dqb_isoftlimit)
1326 dquot->dq_dqb.dqb_curinodes = 0;
1328 if (!dqblk->dqb_isoftlimit ||
1329 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1330 dquot->dq_dqb.dqb_itime = 0;
1331 else if (!(dqblk->dqb_valid & QIF_ITIME))
1332 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1333 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1335 flag |= LQUOTA_FLAGS_ADJINO;
1337 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1338 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1340 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1342 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1345 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1350 CERROR("set limit failed! (rc:%d)\n", rc);
1354 up(&mds->mds_qonoff_sem);
1355 orig_set = ihardlimit || isoftlimit;
1356 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1357 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1358 down(&dquot->dq_sem);
1359 dquot->dq_dqb.dqb_curinodes = 0;
1361 rc = mds_init_slave_ilimits(obd, oqctl, orig_set, oqaq);
1363 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1368 orig_set = bhardlimit || bsoftlimit;
1369 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1370 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1371 down(&dquot->dq_sem);
1372 dquot->dq_dqb.dqb_curspace = 0;
1374 rc = mds_init_slave_blimits(obd, oqctl, orig_set, oqaq);
1376 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1382 down(&mds->mds_qonoff_sem);
1383 down(&dquot->dq_sem);
1385 /* cancel previous setting */
1386 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1387 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1388 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1389 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1390 dquot->dq_dqb.dqb_btime = btime;
1391 dquot->dq_dqb.dqb_itime = itime;
1393 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1397 down(&dquot->dq_sem);
1398 dquot->dq_status &= ~DQ_STATUS_SET;
1400 lustre_dqput(dquot);
1403 up(&mds->mds_qonoff_sem);
1408 return rc ? rc : rc2;
1411 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1413 struct obd_quotactl *soqc;
1414 struct lvfs_run_ctxt saved;
1418 OBD_ALLOC_PTR(soqc);
1422 soqc->qc_cmd = Q_GETOQUOTA;
1423 soqc->qc_id = oqctl->qc_id;
1424 soqc->qc_type = oqctl->qc_type;
1426 /* get block usage from OSS */
1427 soqc->qc_dqblk.dqb_curspace = 0;
1428 rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
1430 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1431 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1434 /* get block/inode usage from MDS */
1435 soqc->qc_dqblk.dqb_curspace = 0;
1436 soqc->qc_dqblk.dqb_curinodes = 0;
1437 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1438 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1439 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1441 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1442 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1443 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1451 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1453 struct mds_obd *mds = &obd->u.mds;
1454 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1455 struct lustre_dquot *dquot;
1456 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1460 if (oqctl->qc_type != USRQUOTA &&
1461 oqctl->qc_type != GRPQUOTA)
1464 down(&mds->mds_qonoff_sem);
1465 dqblk->dqb_valid = 0;
1466 if (qinfo->qi_files[oqctl->qc_type] == NULL)
1467 GOTO(out, rc = -ESRCH);
1469 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1471 GOTO(out, rc = PTR_ERR(dquot));
1473 down(&dquot->dq_sem);
1474 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1475 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1476 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1477 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1478 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1479 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1480 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1483 lustre_dqput(dquot);
1485 /* the usages in admin quota file is inaccurate */
1486 dqblk->dqb_curinodes = 0;
1487 dqblk->dqb_curspace = 0;
1488 rc = mds_get_space(obd, oqctl);
1491 up(&mds->mds_qonoff_sem);
1495 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1497 struct lvfs_run_ctxt saved;
1501 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1502 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1503 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1509 /* FIXME we only recovery block limit by now, need recovery inode
1510 * limits also after CMD involved in */
1512 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1514 struct mds_obd *mds = &obd->u.mds;
1515 struct lustre_quota_info *qinfo= &mds->mds_quota_info;
1516 struct lustre_dquot *dquot;
1517 struct obd_quotactl *qctl;
1518 __u64 total_limits = 0;
1522 OBD_ALLOC_PTR(qctl);
1526 dquot = lustre_dqget(obd, qinfo, id, type);
1527 if (IS_ERR(dquot)) {
1528 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1530 RETURN(PTR_ERR(dquot));
1533 down(&dquot->dq_sem);
1535 /* don't recovery the dquot without limits or under setting */
1536 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1539 dquot->dq_status |= DQ_STATUS_RECOVERY;
1543 /* get real bhardlimit from all slaves. */
1544 qctl->qc_cmd = Q_GETOQUOTA;
1545 qctl->qc_type = type;
1547 qctl->qc_stat = QUOTA_RECOVERING;
1548 rc = obd_quotactl(mds->mds_osc_exp, qctl);
1551 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1553 /* get real bhardlimit from master */
1554 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1557 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1559 /* amend the usage of the administrative quotafile */
1560 down(&mds->mds_qonoff_sem);
1561 down(&dquot->dq_sem);
1563 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1565 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1567 CERROR("write dquot failed! (rc:%d)\n", rc);
1570 up(&mds->mds_qonoff_sem);
1573 down(&dquot->dq_sem);
1574 dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1578 lustre_dqput(dquot);
1583 struct qmaster_recov_thread_data {
1584 struct obd_device *obd;
1585 struct completion comp;
1588 static int qmaster_recovery_main(void *arg)
1590 struct qmaster_recov_thread_data *data = arg;
1591 struct obd_device *obd = data->obd;
1592 struct mds_obd *mds = &obd->u.mds;
1593 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1595 unsigned short type;
1598 ptlrpc_daemonize("qmaster_recovd");
1601 class_incref(obd, "qmaster_recovd_mds", obd);
1603 class_incref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1605 complete(&data->comp);
1607 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1608 struct list_head id_list;
1609 struct dquot_id *dqid, *tmp;
1611 down(&mds->mds_qonoff_sem);
1612 if (qinfo->qi_files[type] == NULL) {
1613 up(&mds->mds_qonoff_sem);
1616 CFS_INIT_LIST_HEAD(&id_list);
1617 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1619 up(&mds->mds_qonoff_sem);
1622 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1624 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1625 list_del_init(&dqid->di_link);
1629 rc = dquot_recovery(obd, dqid->di_id, type);
1631 CERROR("qmaster recovery failed! (id:%d type:%d"
1632 " rc:%d)\n", dqid->di_id, type, rc);
1637 class_decref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1638 class_decref(obd, "qmaster_recovd_mds", obd);
1642 int mds_quota_recovery(struct obd_device *obd)
1644 struct mds_obd *mds = &obd->u.mds;
1645 struct qmaster_recov_thread_data data;
1649 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1652 mutex_down(&obd->obd_dev_sem);
1653 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1654 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1655 mds->mds_lov_desc.ld_active_tgt_count,
1656 mds->mds_lov_objid_count);
1657 mutex_up(&obd->obd_dev_sem);
1660 mutex_up(&obd->obd_dev_sem);
1663 init_completion(&data.comp);
1665 rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1667 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1669 wait_for_completion(&data.comp);
1673 #endif /* HAVE_QUOTA_SUPPORT */