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 cfs_list_t lustre_dquot_hash[NR_DQHASH];
69 static cfs_spinlock_t dquot_hash_lock = CFS_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(cfs_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 cfs_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 cfs_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 cfs_list_t *head = lustre_dquot_hash +
167 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
168 LASSERT(cfs_list_empty(&dquot->dq_hash));
169 cfs_list_add(&dquot->dq_hash, head);
172 static void remove_dquot_nolock(struct lustre_dquot *dquot)
174 LASSERT(!cfs_list_empty(&dquot->dq_hash));
175 cfs_list_del_init(&dquot->dq_hash);
178 static void lustre_dqput(struct lustre_dquot *dquot)
181 cfs_spin_lock(&dquot_hash_lock);
182 LASSERT(dquot->dq_refcnt);
184 if (!dquot->dq_refcnt) {
185 remove_dquot_nolock(dquot);
188 cfs_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 cfs_spin_lock(&dquot_hash_lock);
204 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
206 cfs_spin_unlock(&dquot_hash_lock);
212 insert_dquot_nolock(dquot);
213 cfs_spin_unlock(&dquot_hash_lock);
215 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
216 cfs_up(&dquot->dq_sem);
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 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 0);
239 if (lqs && !IS_ERR(lqs)) {
240 cfs_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 cfs_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 __u32 ost_num = mds->mds_lov_objid_count, mdt_num = 1;
259 struct quota_adjust_qunit *oqaq = NULL;
260 unsigned int qid[MAXQUOTAS] = { 0, 0 };
261 struct lustre_quota_info *info = &mds->mds_quota_info;
262 struct lustre_dquot *dquot = NULL;
268 dquot = lustre_dqget(obd, info, id, type);
270 RETURN(PTR_ERR(dquot));
274 GOTO(out, rc = -ENOMEM);
276 cfs_down(&dquot->dq_sem);
277 init_oqaq(oqaq, qctxt, id, type);
279 rc = dquot_create_oqaq(qctxt, dquot, ost_num, mdt_num,
280 is_blk ? LQUOTA_FLAGS_ADJBLK :
281 LQUOTA_FLAGS_ADJINO, oqaq);
284 CERROR("create oqaq failed! (rc:%d)\n", rc);
287 QAQ_DEBUG(oqaq, "show oqaq.\n")
289 if (!QAQ_IS_ADJBLK(oqaq) && !QAQ_IS_ADJINO(oqaq))
292 /* adjust the mds slave qunit size */
293 adjust_res = quota_adjust_slave_lqs(oqaq, qctxt);
294 if (adjust_res <= 0) {
295 if (adjust_res < 0) {
297 CERROR("adjust mds slave's qunit size failed! "
300 CDEBUG(D_QUOTA, "qunit doesn't need to be adjusted.\n");
306 qid[GRPQUOTA] = dquot->dq_id;
308 qid[USRQUOTA] = dquot->dq_id;
310 cfs_up(&dquot->dq_sem);
312 rc = qctxt_adjust_qunit(obd, qctxt, qid, is_blk, 0, NULL);
313 if (rc == -EDQUOT || rc == -EBUSY) {
314 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
318 CERROR("%s: mds fail to adjust file quota! (rc:%d)\n",
323 /* only when block qunit is reduced, boardcast to osts */
324 if ((adjust_res & LQS_BLK_DECREASE) && QAQ_IS_ADJBLK(oqaq))
325 rc = obd_quota_adjust_qunit(mds->mds_osc_exp, oqaq, qctxt);
334 cfs_up(&dquot->dq_sem);
338 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
340 struct mds_obd *mds = &obd->u.mds;
341 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
342 struct lustre_quota_info *info = &mds->mds_quota_info;
343 struct lustre_dquot *dquot = NULL;
345 __u64 hlimit = 0, slimit = 0;
347 unsigned int grace = 0;
348 struct lustre_qunit_size *lqs = NULL;
352 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_DQACQ))
355 dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata));
357 RETURN(PTR_ERR(dquot));
359 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
360 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
362 cfs_down(&mds->mds_qonoff_sem);
363 cfs_down(&dquot->dq_sem);
365 if (dquot->dq_status & DQ_STATUS_RECOVERY) {
366 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
367 GOTO(out, rc = -EBUSY);
370 if (QDATA_IS_BLK(qdata)) {
371 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
372 usage = &dquot->dq_dqb.dqb_curspace;
373 hlimit = dquot->dq_dqb.dqb_bhardlimit;
374 slimit = dquot->dq_dqb.dqb_bsoftlimit;
375 time = &dquot->dq_dqb.dqb_btime;
377 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
378 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
379 hlimit = dquot->dq_dqb.dqb_ihardlimit;
380 slimit = dquot->dq_dqb.dqb_isoftlimit;
381 time = &dquot->dq_dqb.dqb_itime;
384 /* if the quota limit in admin quotafile is zero, we just inform
385 * slave to clear quota limit with zero qd_count */
386 if (hlimit == 0 && slimit == 0) {
394 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
396 if (QDATA_IS_CHANGE_QS(qdata) &&
397 QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
398 qdata->qd_count = (hlimit -
399 QUSG(*usage, QDATA_IS_BLK(qdata)))
400 * (QDATA_IS_BLK(qdata) ?
401 QUOTABLOCK_SIZE : 1);
403 GOTO(out, rc = -EDQUOT);
407 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
408 if (*time && cfs_time_current_sec() >= *time)
409 GOTO(out, rc = -EDQUOT);
411 *time = cfs_time_current_sec() + grace;
414 *usage += qdata->qd_count;
417 /* The usage in administrative file might be incorrect before
419 if (*usage - qdata->qd_count < 0)
422 *usage -= qdata->qd_count;
424 /* (usage <= soft limit) but not (usage < soft limit) */
425 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
432 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
435 cfs_up(&dquot->dq_sem);
436 cfs_up(&mds->mds_qonoff_sem);
439 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
440 QDATA_IS_BLK(qdata));
442 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
444 if (lqs == NULL || IS_ERR(lqs)) {
445 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
446 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? qctxt->lqc_bunit_sz :
449 cfs_spin_lock(&lqs->lqs_lock);
450 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
452 cfs_spin_unlock(&lqs->lqs_lock);
455 if (QDATA_IS_BLK(qdata))
456 QDATA_SET_ADJBLK(qdata);
458 QDATA_SET_ADJINO(qdata);
460 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
467 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
468 const unsigned int qpids[], int rc, int opc)
470 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
474 if (rc && rc != -EDQUOT && rc != ENOLCK)
478 case FSFILT_OP_SETATTR:
479 /* release file quota on original owner */
480 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
481 /* release block quota on original owner */
482 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
483 /* acquire file quota on current owner */
484 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
485 /* acquire block quota on current owner */
486 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
488 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
489 /* release file quota on child */
490 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
491 /* rlease block quota on child */
492 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
494 case FSFILT_OP_CREATE_PARTIAL_CHILD:
495 /* acquire file quota on child */
496 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
497 /* acquire block quota on child */
498 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
501 /* acquire block quota on parent */
502 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
504 case FSFILT_OP_UNLINK:
505 /* release block quota on parent */
506 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
507 /* release file quota on child */
508 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
509 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
510 /* release block quota on child */
511 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
514 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
515 /* release block quota on parent */
516 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
518 case FSFILT_OP_CREATE:
519 /* acquire block quota on parent */
520 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
521 /* acquire file quota on child */
522 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
523 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
524 /* acquire block quota on child */
525 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
535 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
536 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
540 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
541 const unsigned int qpids[], int rc, int opc)
543 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
547 if (rc && rc != -EDQUOT)
551 case FSFILT_OP_SETATTR:
552 /* acquire/release block quota on original & current owner */
553 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
554 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
556 case FSFILT_OP_UNLINK:
557 /* release block quota on this owner */
558 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
559 /* acquire block quota on this owner */
560 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
571 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
572 rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
578 static const char prefix[] = "OBJECTS/";
580 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
582 struct mds_obd *mds = &obd->u.mds;
583 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
584 struct obd_device_target *obt = &obd->u.obt;
585 int rc = 0, i, rc1 = 0;
586 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
588 struct lvfs_run_ctxt saved;
591 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
593 if (oqctl->qc_type != USRQUOTA &&
594 oqctl->qc_type != GRPQUOTA &&
595 oqctl->qc_type != UGQUOTA)
598 cfs_down(&obt->obt_quotachecking);
599 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
600 cfs_down(&mds->mds_qonoff_sem);
602 for (i = 0; i < MAXQUOTAS; i++) {
605 if (!Q_TYPESET(oqctl, i))
608 /* quota file has been opened ? */
609 if (qinfo->qi_files[i]) {
610 CWARN("quota[%d] is on yet\n", i);
615 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
616 sprintf(name, "%s%s", prefix, quotafile[i]);
618 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
621 CERROR("%s: error invalidating admin quotafile %s (rc:%d)\n",
622 obd->obd_name, name, rc);
628 cfs_up(&mds->mds_qonoff_sem);
629 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
630 cfs_up(&obt->obt_quotachecking);
634 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
636 struct mds_obd *mds = &obd->u.mds;
637 struct obd_device_target *obt = &obd->u.obt;
639 struct lvfs_run_ctxt saved;
642 if (oqctl->qc_type != USRQUOTA &&
643 oqctl->qc_type != GRPQUOTA &&
644 oqctl->qc_type != UGQUOTA)
647 cfs_down(&obt->obt_quotachecking);
648 if (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
649 GOTO(out, rc = -EBUSY);
650 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
651 cfs_down(&mds->mds_qonoff_sem);
653 oqctl->qc_cmd = Q_FINVALIDATE;
654 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
656 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
658 cfs_up(&mds->mds_qonoff_sem);
659 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
661 cfs_up(&obt->obt_quotachecking);
665 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
667 struct mds_obd *mds = &obd->u.mds;
668 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
669 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
670 struct lvfs_run_ctxt saved;
675 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
677 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
679 cfs_down(&mds->mds_qonoff_sem);
681 for (i = 0; i < MAXQUOTAS && !rc; i++) {
684 if (!Q_TYPESET(oqctl, i))
687 /* quota file has been opened ? */
688 if (qinfo->qi_files[i]) {
689 CWARN("init %s admin quotafile while quota on.\n",
690 i == USRQUOTA ? "user" : "group");
694 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
695 sprintf(name, "%s%s", prefix, quotafile[i]);
697 /* check if quota file exists and is correct */
698 fp = filp_open(name, O_RDONLY, 0);
700 /* irregular file is not the right place for quota */
701 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
702 CERROR("admin quota file %s is not "
708 qinfo->qi_files[i] = fp;
709 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
710 qinfo->qi_files[i] = 0;
719 /* -EINVAL may be returned by quotainfo for bad quota file */
720 if (rc != -ENOENT && rc != -EINVAL) {
721 CERROR("%s: error opening old quota file %s (%d)\n",
722 obd->obd_name, name, rc);
726 CDEBUG(D_INFO, "%s new quota file %s\n", name,
727 rc == -ENOENT ? "creating" : "overwriting");
729 /* create quota file overwriting old if needed */
730 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
733 CERROR("%s: error creating admin quotafile %s (rc:%d)\n",
734 obd->obd_name, name, rc);
738 qinfo->qi_files[i] = fp;
740 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
742 CERROR("error init %s admin quotafile! (rc:%d)\n",
743 i == USRQUOTA ? "user" : "group", rc);
746 qinfo->qi_files[i] = NULL;
748 cfs_up(&mds->mds_qonoff_sem);
750 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
754 static int close_quota_files(struct obd_quotactl *oqctl,
755 struct lustre_quota_info *qinfo)
760 for (i = 0; i < MAXQUOTAS; i++) {
761 if (!Q_TYPESET(oqctl, i))
763 if (qinfo->qi_files[i] == NULL) {
764 CWARN("quota[%d] is off already\n", i);
768 filp_close(qinfo->qi_files[i], 0);
769 qinfo->qi_files[i] = NULL;
774 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
776 struct mds_obd *mds = &obd->u.mds;
777 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
778 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
780 int i, rc = 0, rc1 = 0;
783 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
785 /* open admin quota files and read quotafile info */
786 for (i = 0; i < MAXQUOTAS; i++) {
789 if (!Q_TYPESET(oqctl, i) || qinfo->qi_files[i] != NULL)
792 LASSERT(strlen(quotafile[i])
793 + sizeof(prefix) <= sizeof(name));
794 sprintf(name, "%s%s", prefix, quotafile[i]);
795 fp = filp_open(name, O_RDWR, 0);
796 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
797 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
798 CERROR("error open/create %s! (rc:%d)\n", name, rc);
801 qinfo->qi_files[i] = fp;
803 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
805 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
809 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
811 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
817 if (rc && rc1 != -EALREADY)
818 close_quota_files(oqctl, qinfo);
823 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
828 if (oqctl->qc_type != USRQUOTA &&
829 oqctl->qc_type != GRPQUOTA &&
830 oqctl->qc_type != UGQUOTA)
833 rc = generic_quota_on(obd, oqctl, 1);
839 int mds_admin_quota_off(struct obd_device *obd,
840 struct obd_quotactl *oqctl)
842 struct mds_obd *mds = &obd->u.mds;
843 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
847 /* close admin quota files */
848 rc = close_quota_files(oqctl, qinfo);
853 /* with obt->obt_quotachecking held */
854 int do_mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
856 struct mds_obd *mds = &obd->u.mds;
857 struct obd_device_target *obt = &obd->u.obt;
858 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
859 struct lvfs_run_ctxt saved;
860 int rc = 0, rc1 = 0, rc2 = 0;
863 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
865 if (oqctl->qc_type != USRQUOTA &&
866 oqctl->qc_type != GRPQUOTA &&
867 oqctl->qc_type != UGQUOTA)
870 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
871 cfs_down(&mds->mds_qonoff_sem);
872 /* close admin quota files */
873 rc2 = mds_admin_quota_off(obd, oqctl);
874 if (rc2 && rc2 != -EALREADY) {
875 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
879 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
881 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
882 } else if (quota_is_off(qctxt, oqctl)) {
883 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
886 if (rc2 != -EALREADY) {
887 CWARN("mds local quota[%d] is failed to be off for %d\n",
888 oqctl->qc_type, rc1);
889 oqctl->qc_cmd = Q_QUOTAON;
890 mds_admin_quota_on(obd, oqctl);
891 oqctl->qc_cmd = Q_QUOTAOFF;
896 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
897 if (rc && rc != -EALREADY) {
898 CWARN("mds remote quota[%d] is failed to be off for %d\n",
900 oqctl->qc_cmd = Q_QUOTAON;
901 if (rc2 != -EALREADY)
902 mds_admin_quota_on(obd, oqctl);
903 if (rc1 != -EALREADY) {
904 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
905 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
907 oqctl->qc_cmd = Q_QUOTAOFF;
912 cfs_up(&mds->mds_qonoff_sem);
913 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
914 return rc ? : (rc1 ? : rc2);
917 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
919 struct obd_device_target *obt = &obd->u.obt;
923 cfs_down(&obt->obt_quotachecking);
924 rc = do_mds_quota_off(obd, oqctl);
925 cfs_up(&obt->obt_quotachecking);
929 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
931 struct mds_obd *mds = &obd->u.mds;
932 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
933 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
937 if (oqctl->qc_type != USRQUOTA &&
938 oqctl->qc_type != GRPQUOTA)
941 cfs_down(&mds->mds_qonoff_sem);
942 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
943 CWARN("quota[%u] is off\n", oqctl->qc_type);
944 GOTO(out, rc = -ESRCH);
947 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
948 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
949 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
951 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
955 cfs_up(&mds->mds_qonoff_sem);
959 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
961 struct mds_obd *mds = &obd->u.mds;
962 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
963 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
967 if (oqctl->qc_type != USRQUOTA &&
968 oqctl->qc_type != GRPQUOTA)
971 cfs_down(&mds->mds_qonoff_sem);
972 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
973 CWARN("quota[%u] is off\n", oqctl->qc_type);
974 GOTO(out, rc = -ESRCH);
977 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
978 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
979 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
983 cfs_up(&mds->mds_qonoff_sem);
987 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
988 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
989 int type, struct quota_adjust_qunit *oqaq)
991 __u64 bunit_curr_o, iunit_curr_o;
992 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
993 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
994 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
995 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
996 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
997 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1001 if (!dquot || !oqaq)
1003 LASSERT_SEM_LOCKED(&dquot->dq_sem);
1004 LASSERT(oqaq->qaq_iunit_sz);
1005 LASSERT(oqaq->qaq_bunit_sz);
1007 /* don't change qunit size */
1008 if (!qctxt->lqc_switch_qs)
1011 bunit_curr_o = oqaq->qaq_bunit_sz;
1012 iunit_curr_o = oqaq->qaq_iunit_sz;
1014 if (dquot->dq_type == GRPQUOTA)
1017 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1018 __u64 b_limitation =
1019 oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1020 /* enlarge block qunit size */
1022 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1023 oqaq->qaq_bunit_sz =
1024 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1026 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1030 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1031 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1033 /* shrink block qunit size */
1035 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1036 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1037 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1039 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1041 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1045 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1046 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1048 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1049 QAQ_SET_ADJBLK(oqaq);
1053 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1054 __u64 i_limitation =
1055 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1056 /* enlarge file qunit size */
1057 while (ilimit > dquot->dq_dqb.dqb_curinodes
1058 + 2 * i_limitation) {
1059 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1060 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1064 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1065 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1067 /* shrink file qunit size */
1068 while (ilimit < dquot->dq_dqb.dqb_curinodes
1070 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1071 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1073 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1077 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1078 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1080 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1081 QAQ_SET_ADJINO(oqaq);
1085 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1090 static int mds_init_slave_ilimits(struct obd_device *obd,
1091 struct obd_quotactl *oqctl, int set)
1093 /* XXX: for file limits only adjust local now */
1094 struct obd_device_target *obt = &obd->u.obt;
1095 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1096 unsigned int id[MAXQUOTAS] = { 0, 0 };
1097 struct obd_quotactl *ioqc = NULL;
1098 struct lustre_qunit_size *lqs;
1103 /* if we are going to set zero limit, needn't init slaves */
1104 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1108 OBD_ALLOC_PTR(ioqc);
1112 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1113 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1114 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1115 ioqc->qc_id = oqctl->qc_id;
1116 ioqc->qc_type = oqctl->qc_type;
1117 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1118 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1120 /* build lqs for mds */
1121 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1122 qctxt, flag ? 1 : 0);
1123 if (lqs && !IS_ERR(lqs)) {
1125 lqs->lqs_flags |= QI_SET;
1127 lqs->lqs_flags &= ~QI_SET;
1130 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1131 flag ? "create" : "search",
1132 oqctl->qc_type ? "group" : "user",
1134 GOTO(out, rc = PTR_ERR(lqs));
1137 /* set local limit to MIN_QLIMIT */
1138 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1142 /* trigger local qunit pre-acquire */
1143 if (oqctl->qc_type == USRQUOTA)
1144 id[USRQUOTA] = oqctl->qc_id;
1146 id[GRPQUOTA] = oqctl->qc_id;
1148 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1149 if (rc == -EDQUOT || rc == -EBUSY) {
1150 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1154 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1158 /* FIXME initialize all slaves in CMD */
1166 static int mds_init_slave_blimits(struct obd_device *obd,
1167 struct obd_quotactl *oqctl, int set)
1169 struct obd_device_target *obt = &obd->u.obt;
1170 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1171 struct mds_obd *mds = &obd->u.mds;
1172 struct obd_quotactl *ioqc;
1173 struct lustre_qunit_size *lqs;
1174 unsigned int id[MAXQUOTAS] = { 0, 0 };
1179 /* if we are going to set zero limit, needn't init slaves */
1180 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1184 OBD_ALLOC_PTR(ioqc);
1188 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1189 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1190 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1191 ioqc->qc_id = oqctl->qc_id;
1192 ioqc->qc_type = oqctl->qc_type;
1193 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1194 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1196 /* build lqs for mds */
1197 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1198 qctxt, flag ? 1 : 0);
1199 if (lqs && !IS_ERR(lqs)) {
1201 lqs->lqs_flags |= QB_SET;
1203 lqs->lqs_flags &= ~QB_SET;
1206 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1207 flag ? "create" : "search",
1208 oqctl->qc_type ? "group" : "user",
1210 GOTO(out, rc = PTR_ERR(lqs));
1213 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1217 /* trigger local qunit pre-acquire */
1218 if (oqctl->qc_type == USRQUOTA)
1219 id[USRQUOTA] = oqctl->qc_id;
1221 id[GRPQUOTA] = oqctl->qc_id;
1223 /* initialize all slave's limit */
1224 rc = obd_quotactl(mds->mds_osc_exp, ioqc);
1226 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1227 if (rc == -EDQUOT || rc == -EBUSY) {
1228 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1232 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1242 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1244 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1247 QAQ_SET_CREATE_LQS(qaq);
1248 /* adjust local lqs */
1249 rc = quota_adjust_slave_lqs(qaq, qctxt);
1251 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1253 /* adjust remote lqs */
1254 if (QAQ_IS_ADJBLK(qaq)) {
1255 rc = obd_quota_adjust_qunit(obd->u.mds.mds_osc_exp, qaq, qctxt);
1257 CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1262 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1264 struct mds_obd *mds = &obd->u.mds;
1265 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1266 struct obd_device *lov_obd = class_exp2obd(mds->mds_osc_exp);
1267 struct lov_obd *lov = &lov_obd->u.lov;
1268 struct quota_adjust_qunit *oqaq = NULL;
1269 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1270 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1271 time_t btime, itime;
1272 struct lustre_dquot *dquot;
1273 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1274 /* orig_set means if quota was set before; now_set means we are
1275 * setting/cancelling quota */
1276 int orig_set, now_set;
1277 int rc, rc2 = 0, flag = 0;
1280 if (oqctl->qc_type != USRQUOTA &&
1281 oqctl->qc_type != GRPQUOTA)
1284 OBD_ALLOC_PTR(oqaq);
1287 cfs_down(&mds->mds_qonoff_sem);
1288 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1290 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1291 CWARN("quota[%u] is off\n", oqctl->qc_type);
1292 GOTO(out_sem, rc = -ESRCH);
1295 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1297 GOTO(out_sem, rc = PTR_ERR(dquot));
1298 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1299 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1301 cfs_down(&dquot->dq_sem);
1303 if (dquot->dq_status) {
1304 cfs_up(&dquot->dq_sem);
1305 lustre_dqput(dquot);
1306 GOTO(out_sem, rc = -EBUSY);
1308 dquot->dq_status |= DQ_STATUS_SET;
1310 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1311 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1312 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1313 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1314 btime = dquot->dq_dqb.dqb_btime;
1315 itime = dquot->dq_dqb.dqb_itime;
1317 if (dqblk->dqb_valid & QIF_BTIME)
1318 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1319 if (dqblk->dqb_valid & QIF_ITIME)
1320 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1322 if (dqblk->dqb_valid & QIF_BLIMITS) {
1323 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1324 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1325 /* clear usage (limit pool) */
1326 if (!dquot->dq_dqb.dqb_bhardlimit &&
1327 !dquot->dq_dqb.dqb_bsoftlimit)
1328 dquot->dq_dqb.dqb_curspace = 0;
1330 /* clear grace time */
1331 if (!dqblk->dqb_bsoftlimit ||
1332 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1333 dquot->dq_dqb.dqb_btime = 0;
1334 /* set grace only if user hasn't provided his own */
1335 else if (!(dqblk->dqb_valid & QIF_BTIME))
1336 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1337 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1339 flag |= LQUOTA_FLAGS_ADJBLK;
1342 if (dqblk->dqb_valid & QIF_ILIMITS) {
1343 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1344 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1345 /* clear usage (limit pool) */
1346 if (!dquot->dq_dqb.dqb_ihardlimit &&
1347 !dquot->dq_dqb.dqb_isoftlimit)
1348 dquot->dq_dqb.dqb_curinodes = 0;
1350 if (!dqblk->dqb_isoftlimit ||
1351 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1352 dquot->dq_dqb.dqb_itime = 0;
1353 else if (!(dqblk->dqb_valid & QIF_ITIME))
1354 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1355 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1357 flag |= LQUOTA_FLAGS_ADJINO;
1359 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1360 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1362 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1364 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1367 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1369 cfs_up(&dquot->dq_sem);
1372 CERROR("set limit failed! (rc:%d)\n", rc);
1376 cfs_up(&mds->mds_qonoff_sem);
1378 adjust_lqs(obd, oqaq);
1380 orig_set = ihardlimit || isoftlimit;
1381 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1382 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1383 cfs_down(&dquot->dq_sem);
1384 dquot->dq_dqb.dqb_curinodes = 0;
1385 cfs_up(&dquot->dq_sem);
1386 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1388 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1393 orig_set = bhardlimit || bsoftlimit;
1394 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1395 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1396 cfs_down(&dquot->dq_sem);
1397 dquot->dq_dqb.dqb_curspace = 0;
1398 cfs_up(&dquot->dq_sem);
1399 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1401 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1407 cfs_down(&mds->mds_qonoff_sem);
1408 cfs_down(&dquot->dq_sem);
1410 /* cancel previous setting */
1411 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1412 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1413 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1414 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1415 dquot->dq_dqb.dqb_btime = btime;
1416 dquot->dq_dqb.dqb_itime = itime;
1418 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1419 cfs_up(&dquot->dq_sem);
1422 cfs_down(&dquot->dq_sem);
1423 dquot->dq_status &= ~DQ_STATUS_SET;
1424 cfs_up(&dquot->dq_sem);
1425 lustre_dqput(dquot);
1428 cfs_up(&mds->mds_qonoff_sem);
1433 return rc ? rc : rc2;
1436 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1438 struct obd_quotactl *soqc;
1439 struct lvfs_run_ctxt saved;
1443 OBD_ALLOC_PTR(soqc);
1447 soqc->qc_cmd = Q_GETOQUOTA;
1448 soqc->qc_id = oqctl->qc_id;
1449 soqc->qc_type = oqctl->qc_type;
1451 /* get block usage from OSS */
1452 soqc->qc_dqblk.dqb_curspace = 0;
1453 rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
1454 if (!rc || rc == -EREMOTEIO) {
1455 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1456 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1459 /* get block/inode usage from MDS */
1460 soqc->qc_dqblk.dqb_curspace = 0;
1461 soqc->qc_dqblk.dqb_curinodes = 0;
1462 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1463 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1464 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1466 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1467 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1468 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1476 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1478 struct mds_obd *mds = &obd->u.mds;
1479 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1480 struct lustre_dquot *dquot;
1481 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1485 if (oqctl->qc_type != USRQUOTA &&
1486 oqctl->qc_type != GRPQUOTA)
1489 cfs_down(&mds->mds_qonoff_sem);
1490 dqblk->dqb_valid = 0;
1491 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1492 CWARN("quota[%u] is off\n", oqctl->qc_type);
1493 GOTO(out, rc = -ESRCH);
1496 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1498 GOTO(out, rc = PTR_ERR(dquot));
1500 cfs_down(&dquot->dq_sem);
1501 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1502 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1503 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1504 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1505 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1506 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1507 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1508 cfs_up(&dquot->dq_sem);
1510 lustre_dqput(dquot);
1511 cfs_up(&mds->mds_qonoff_sem);
1513 /* the usages in admin quota file is inaccurate */
1514 dqblk->dqb_curinodes = 0;
1515 dqblk->dqb_curspace = 0;
1516 rc = mds_get_space(obd, oqctl);
1520 cfs_up(&mds->mds_qonoff_sem);
1524 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1526 struct lvfs_run_ctxt saved;
1530 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1531 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1532 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1538 /* FIXME we only recovery block limit by now, need recovery inode
1539 * limits also after CMD involved in */
1541 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1543 struct mds_obd *mds = &obd->u.mds;
1544 struct lustre_quota_info *qinfo= &mds->mds_quota_info;
1545 struct lustre_dquot *dquot;
1546 struct obd_quotactl *qctl;
1547 __u64 total_limits = 0;
1551 OBD_ALLOC_PTR(qctl);
1555 dquot = lustre_dqget(obd, qinfo, id, type);
1556 if (IS_ERR(dquot)) {
1557 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1559 RETURN(PTR_ERR(dquot));
1562 cfs_down(&dquot->dq_sem);
1564 /* don't recovery the dquot without limits or under setting */
1565 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1568 dquot->dq_status |= DQ_STATUS_RECOVERY;
1570 cfs_up(&dquot->dq_sem);
1572 /* get real bhardlimit from all slaves. */
1573 qctl->qc_cmd = Q_GETOQUOTA;
1574 qctl->qc_type = type;
1576 qctl->qc_stat = QUOTA_RECOVERING;
1577 rc = obd_quotactl(mds->mds_osc_exp, qctl);
1580 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1582 /* get real bhardlimit from master */
1583 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1586 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1588 /* amend the usage of the administrative quotafile */
1589 cfs_down(&mds->mds_qonoff_sem);
1590 cfs_down(&dquot->dq_sem);
1592 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1594 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1596 CERROR("write dquot failed! (rc:%d)\n", rc);
1598 cfs_up(&dquot->dq_sem);
1599 cfs_up(&mds->mds_qonoff_sem);
1602 cfs_down(&dquot->dq_sem);
1603 dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1605 cfs_up(&dquot->dq_sem);
1607 lustre_dqput(dquot);
1612 struct qmaster_recov_thread_data {
1613 struct obd_device *obd;
1614 cfs_completion_t comp;
1617 static int qmaster_recovery_main(void *arg)
1619 struct qmaster_recov_thread_data *data = arg;
1620 struct obd_device *obd = data->obd;
1621 struct mds_obd *mds = &obd->u.mds;
1622 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1624 unsigned short type;
1627 cfs_daemonize_ctxt("qmaster_recovd");
1630 class_incref(obd, "qmaster_recovd_mds", obd);
1632 class_incref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1634 cfs_complete(&data->comp);
1636 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1638 struct dquot_id *dqid, *tmp;
1640 cfs_down(&mds->mds_qonoff_sem);
1641 if (qinfo->qi_files[type] == NULL) {
1642 cfs_up(&mds->mds_qonoff_sem);
1645 CFS_INIT_LIST_HEAD(&id_list);
1646 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1648 cfs_up(&mds->mds_qonoff_sem);
1651 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1653 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1654 cfs_list_del_init(&dqid->di_link);
1658 rc = dquot_recovery(obd, dqid->di_id, type);
1660 CERROR("%s: qmaster recovery failed for %sid %d"
1661 " rc:%d)\n", obd->obd_name,
1662 type ? "g" : "u", dqid->di_id, rc);
1667 class_decref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1668 class_decref(obd, "qmaster_recovd_mds", obd);
1672 int mds_quota_recovery(struct obd_device *obd)
1674 struct mds_obd *mds = &obd->u.mds;
1675 struct qmaster_recov_thread_data data;
1679 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1682 cfs_mutex_down(&obd->obd_dev_sem);
1683 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1684 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1685 mds->mds_lov_desc.ld_active_tgt_count,
1686 mds->mds_lov_objid_count);
1687 cfs_mutex_up(&obd->obd_dev_sem);
1690 cfs_mutex_up(&obd->obd_dev_sem);
1693 cfs_init_completion(&data.comp);
1695 rc = cfs_kernel_thread(qmaster_recovery_main, &data,
1696 CLONE_VM|CLONE_FILES);
1698 CERROR("%s: cannot start quota recovery thread: rc %d\n",
1701 cfs_wait_for_completion(&data.comp);
1705 #endif /* HAVE_QUOTA_SUPPORT */