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 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 0);
239 if (lqs && !IS_ERR(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 qid[MAXQUOTAS] = { 0, 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");
308 qid[GRPQUOTA] = dquot->dq_id;
310 qid[USRQUOTA] = dquot->dq_id;
314 rc = qctxt_adjust_qunit(obd, qctxt, qid, 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 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
446 if (lqs == NULL || IS_ERR(lqs)) {
447 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
448 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? qctxt->lqc_bunit_sz :
451 spin_lock(&lqs->lqs_lock);
452 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
454 spin_unlock(&lqs->lqs_lock);
457 if (QDATA_IS_BLK(qdata))
458 QDATA_SET_ADJBLK(qdata);
460 QDATA_SET_ADJINO(qdata);
462 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
469 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
470 const unsigned int qpids[], int rc, int opc)
472 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
476 if (rc && rc != -EDQUOT && rc != ENOLCK)
480 case FSFILT_OP_SETATTR:
481 /* release file quota on original owner */
482 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
483 /* release block quota on original owner */
484 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
485 /* acquire file quota on current owner */
486 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
487 /* acquire block quota on current owner */
488 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
490 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
491 /* release file quota on child */
492 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
493 /* rlease block quota on child */
494 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
496 case FSFILT_OP_CREATE_PARTIAL_CHILD:
497 /* acquire file quota on child */
498 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
499 /* acquire block quota on child */
500 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
503 /* acquire block quota on parent */
504 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
506 case FSFILT_OP_UNLINK:
507 /* release block quota on parent */
508 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
509 /* release file quota on child */
510 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
511 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
512 /* release block quota on child */
513 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
516 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
517 /* release block quota on parent */
518 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
520 case FSFILT_OP_CREATE:
521 /* acquire block quota on parent */
522 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
523 /* acquire file quota on child */
524 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
525 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
526 /* acquire block quota on child */
527 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
536 CDEBUG(rc2 == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
537 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
538 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
542 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
543 const unsigned int qpids[], int rc, int opc)
545 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
549 if (rc && rc != -EDQUOT)
553 case FSFILT_OP_SETATTR:
554 /* acquire/release block quota on original & current owner */
555 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
556 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
558 case FSFILT_OP_UNLINK:
559 /* release block quota on this owner */
560 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
561 /* acquire block quota on this owner */
562 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
572 CDEBUG(rc == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
573 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
574 QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
580 static const char prefix[] = "OBJECTS/";
582 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
584 struct mds_obd *mds = &obd->u.mds;
585 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
586 struct obd_device_target *obt = &obd->u.obt;
587 int rc = 0, i, rc1 = 0;
588 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
590 struct lvfs_run_ctxt saved;
593 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
595 if (oqctl->qc_type != USRQUOTA &&
596 oqctl->qc_type != GRPQUOTA &&
597 oqctl->qc_type != UGQUOTA)
600 down(&obt->obt_quotachecking);
601 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
602 down(&mds->mds_qonoff_sem);
604 for (i = 0; i < MAXQUOTAS; i++) {
607 if (!Q_TYPESET(oqctl, i))
610 /* quota file has been opened ? */
611 if (qinfo->qi_files[i]) {
612 CWARN("quota[%d] is on yet\n", i);
617 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
618 sprintf(name, "%s%s", prefix, quotafile[i]);
620 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
623 CERROR("error invalidating admin quotafile %s (rc:%d)\n",
630 up(&mds->mds_qonoff_sem);
631 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
632 up(&obt->obt_quotachecking);
636 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
638 struct mds_obd *mds = &obd->u.mds;
639 struct obd_device_target *obt = &obd->u.obt;
641 struct lvfs_run_ctxt saved;
644 if (oqctl->qc_type != USRQUOTA &&
645 oqctl->qc_type != GRPQUOTA &&
646 oqctl->qc_type != UGQUOTA)
649 down(&obt->obt_quotachecking);
650 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
651 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 up(&mds->mds_qonoff_sem);
659 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
660 up(&obt->obt_quotachecking);
664 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
666 struct mds_obd *mds = &obd->u.mds;
667 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
668 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
669 struct lvfs_run_ctxt saved;
674 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
676 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
678 down(&mds->mds_qonoff_sem);
680 for (i = 0; i < MAXQUOTAS && !rc; i++) {
683 if (!Q_TYPESET(oqctl, i))
686 /* quota file has been opened ? */
687 if (qinfo->qi_files[i]) {
688 CWARN("init %s admin quotafile while quota on.\n",
689 i == USRQUOTA ? "user" : "group");
693 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
694 sprintf(name, "%s%s", prefix, quotafile[i]);
696 /* check if quota file exists and is correct */
697 fp = filp_open(name, O_RDONLY, 0);
699 /* irregular file is not the right place for quota */
700 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
701 CERROR("admin quota file %s is not "
707 qinfo->qi_files[i] = fp;
708 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
709 qinfo->qi_files[i] = 0;
718 /* -EINVAL may be returned by quotainfo for bad quota file */
719 if (rc != -ENOENT && rc != -EINVAL) {
720 CERROR("error opening old quota file %s (%d)\n",
725 CDEBUG(D_INFO, "%s new quota file %s\n", name,
726 rc == -ENOENT ? "creating" : "overwriting");
728 /* create quota file overwriting old if needed */
729 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
732 CERROR("error creating admin quotafile %s (rc:%d)\n",
737 qinfo->qi_files[i] = fp;
739 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
741 CERROR("error init %s admin quotafile! (rc:%d)\n",
742 i == USRQUOTA ? "user" : "group", rc);
745 qinfo->qi_files[i] = NULL;
747 up(&mds->mds_qonoff_sem);
749 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
753 static int close_quota_files(struct obd_quotactl *oqctl,
754 struct lustre_quota_info *qinfo)
759 for (i = 0; i < MAXQUOTAS; i++) {
760 if (!Q_TYPESET(oqctl, i))
762 if (qinfo->qi_files[i] == NULL) {
763 CWARN("quota[%d] is off already\n", i);
767 filp_close(qinfo->qi_files[i], 0);
768 qinfo->qi_files[i] = NULL;
773 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
775 struct mds_obd *mds = &obd->u.mds;
776 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
777 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
779 int i, rc = 0, rc1 = 0;
782 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
784 /* open admin quota files and read quotafile info */
785 for (i = 0; i < MAXQUOTAS; i++) {
788 if (!Q_TYPESET(oqctl, i))
791 LASSERT(strlen(quotafile[i])
792 + sizeof(prefix) <= sizeof(name));
793 sprintf(name, "%s%s", prefix, quotafile[i]);
795 if (qinfo->qi_files[i] != NULL) {
796 CWARN("quota[%d] is on already\n", i);
801 fp = filp_open(name, O_RDWR, 0);
802 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
803 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
804 CERROR("error open/create %s! (rc:%d)\n", name, rc);
807 qinfo->qi_files[i] = fp;
809 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
811 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
815 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
817 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
823 if (rc && rc1 != -EALREADY)
824 close_quota_files(oqctl, qinfo);
829 int mds_admin_quota_off(struct obd_device *obd,
830 struct obd_quotactl *oqctl)
832 struct mds_obd *mds = &obd->u.mds;
833 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
837 /* close admin quota files */
838 rc = close_quota_files(oqctl, qinfo);
842 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
844 struct mds_obd *mds = &obd->u.mds;
845 struct obd_device_target *obt = &obd->u.obt;
846 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
847 struct lvfs_run_ctxt saved;
848 int rc = 0, rc1 = 0, rc2 = 0;
851 if (oqctl->qc_type != USRQUOTA &&
852 oqctl->qc_type != GRPQUOTA &&
853 oqctl->qc_type != UGQUOTA)
856 down(&obt->obt_quotachecking);
857 if (obt->obt_qctxt.lqc_immutable) {
858 LCONSOLE_ERROR("Failed to turn Quota on, immutable mode "
859 "(is SOM enabled?)\n");
860 up(&obt->obt_quotachecking);
864 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
865 down(&mds->mds_qonoff_sem);
866 rc2 = mds_admin_quota_on(obd, oqctl);
867 if (rc2 && rc2 != -EALREADY) {
868 CWARN("mds quota[%d] is failed to be on for %d\n", oqctl->qc_type, rc2);
872 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
874 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
875 } else if (rc1 == -EBUSY && quota_is_on(qctxt, oqctl)) {
876 CWARN("mds local quota[%d] is on already\n", oqctl->qc_type);
879 if (rc2 != -EALREADY) {
880 CWARN("mds local quota[%d] is failed to be on for %d\n",
881 oqctl->qc_type, rc1);
882 oqctl->qc_cmd = Q_QUOTAOFF;
883 mds_admin_quota_off(obd, oqctl);
884 oqctl->qc_cmd = Q_QUOTAON;
889 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
890 if (rc && rc != -EALREADY) {
891 CWARN("mds remote quota[%d] is failed to be on for %d\n",
893 oqctl->qc_cmd = Q_QUOTAOFF;
894 if (rc2 != -EALREADY)
895 mds_admin_quota_off(obd, oqctl);
896 if (rc1 != -EALREADY) {
897 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
898 qctxt->lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
900 oqctl->qc_cmd = Q_QUOTAON;
905 up(&mds->mds_qonoff_sem);
906 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
907 up(&obt->obt_quotachecking);
908 return rc ? : (rc1 ? : rc2);
911 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
913 struct mds_obd *mds = &obd->u.mds;
914 struct obd_device_target *obt = &obd->u.obt;
915 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
916 struct lvfs_run_ctxt saved;
917 int rc = 0, rc1 = 0, rc2 = 0, imm;
920 imm = oqctl->qc_type & IMMQUOTA;
921 oqctl->qc_type &= ~IMMQUOTA;
923 if (oqctl->qc_type != USRQUOTA &&
924 oqctl->qc_type != GRPQUOTA &&
925 oqctl->qc_type != UGQUOTA)
928 down(&obt->obt_quotachecking);
929 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
930 down(&mds->mds_qonoff_sem);
931 /* close admin quota files */
932 rc2 = mds_admin_quota_off(obd, oqctl);
933 if (rc2 && rc2 != -EALREADY) {
934 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
938 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
941 obt->obt_qctxt.lqc_immutable = 1;
942 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
943 } else if (quota_is_off(qctxt, oqctl)) {
944 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
947 if (rc2 != -EALREADY) {
948 CWARN("mds local quota[%d] is failed to be off for %d\n",
949 oqctl->qc_type, rc1);
950 oqctl->qc_cmd = Q_QUOTAON;
951 mds_admin_quota_on(obd, oqctl);
952 oqctl->qc_cmd = Q_QUOTAOFF;
957 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
958 if (rc && rc != -EALREADY) {
959 CWARN("mds remote quota[%d] is failed to be off for %d\n",
961 oqctl->qc_cmd = Q_QUOTAON;
962 if (rc2 != -EALREADY)
963 mds_admin_quota_on(obd, oqctl);
964 if (rc1 != -EALREADY) {
965 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
967 obt->obt_qctxt.lqc_immutable = 0;
968 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
970 oqctl->qc_cmd = Q_QUOTAOFF;
975 up(&mds->mds_qonoff_sem);
976 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
977 up(&obt->obt_quotachecking);
978 return rc ? : (rc1 ? : rc2);
981 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
983 struct mds_obd *mds = &obd->u.mds;
984 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
985 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
989 if (oqctl->qc_type != USRQUOTA &&
990 oqctl->qc_type != GRPQUOTA)
993 down(&mds->mds_qonoff_sem);
994 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
995 CWARN("quota[%u] is off\n", oqctl->qc_type);
996 GOTO(out, rc = -ESRCH);
999 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
1000 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
1001 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
1003 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
1007 up(&mds->mds_qonoff_sem);
1011 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
1013 struct mds_obd *mds = &obd->u.mds;
1014 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1015 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
1019 if (oqctl->qc_type != USRQUOTA &&
1020 oqctl->qc_type != GRPQUOTA)
1023 down(&mds->mds_qonoff_sem);
1024 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1025 CWARN("quota[%u] is off\n", oqctl->qc_type);
1026 GOTO(out, rc = -ESRCH);
1029 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
1030 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
1031 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
1035 up(&mds->mds_qonoff_sem);
1039 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
1040 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
1041 int type, struct quota_adjust_qunit *oqaq)
1043 __u64 bunit_curr_o, iunit_curr_o;
1044 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
1045 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
1046 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
1047 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
1048 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1049 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1053 if (!dquot || !oqaq)
1055 LASSERT_SEM_LOCKED(&dquot->dq_sem);
1056 LASSERT(oqaq->qaq_iunit_sz);
1057 LASSERT(oqaq->qaq_bunit_sz);
1059 /* don't change qunit size */
1060 if (!qctxt->lqc_switch_qs)
1063 bunit_curr_o = oqaq->qaq_bunit_sz;
1064 iunit_curr_o = oqaq->qaq_iunit_sz;
1066 if (dquot->dq_type == GRPQUOTA)
1069 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1070 __u64 b_limitation =
1071 oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1072 /* enlarge block qunit size */
1074 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1075 oqaq->qaq_bunit_sz =
1076 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1078 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1082 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1083 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1085 /* shrink block qunit size */
1087 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1088 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1089 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1091 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1093 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1097 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1098 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1100 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1101 QAQ_SET_ADJBLK(oqaq);
1105 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1106 __u64 i_limitation =
1107 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1108 /* enlarge file qunit size */
1109 while (ilimit > dquot->dq_dqb.dqb_curinodes
1110 + 2 * i_limitation) {
1111 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1112 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1116 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1117 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1119 /* shrink file qunit size */
1120 while (ilimit < dquot->dq_dqb.dqb_curinodes
1122 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1123 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1125 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1129 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1130 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1132 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1133 QAQ_SET_ADJINO(oqaq);
1137 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1142 static int mds_init_slave_ilimits(struct obd_device *obd,
1143 struct obd_quotactl *oqctl, int set)
1145 /* XXX: for file limits only adjust local now */
1146 struct obd_device_target *obt = &obd->u.obt;
1147 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1148 unsigned int id[MAXQUOTAS] = { 0, 0 };
1149 struct obd_quotactl *ioqc = NULL;
1150 struct lustre_qunit_size *lqs;
1155 /* if we are going to set zero limit, needn't init slaves */
1156 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1160 OBD_ALLOC_PTR(ioqc);
1164 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1165 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1166 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1167 ioqc->qc_id = oqctl->qc_id;
1168 ioqc->qc_type = oqctl->qc_type;
1169 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1170 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1172 /* build lqs for mds */
1173 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1174 qctxt, flag ? 1 : 0);
1175 if (lqs && !IS_ERR(lqs)) {
1177 lqs->lqs_flags |= QI_SET;
1179 lqs->lqs_flags &= ~QI_SET;
1182 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1183 flag ? "create" : "search",
1184 oqctl->qc_type ? "group" : "user",
1186 GOTO(out, rc = PTR_ERR(lqs));
1189 /* set local limit to MIN_QLIMIT */
1190 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1194 /* trigger local qunit pre-acquire */
1195 if (oqctl->qc_type == USRQUOTA)
1196 id[USRQUOTA] = oqctl->qc_id;
1198 id[GRPQUOTA] = oqctl->qc_id;
1200 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1201 if (rc == -EDQUOT || rc == -EBUSY) {
1202 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1206 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1210 /* FIXME initialize all slaves in CMD */
1218 static int mds_init_slave_blimits(struct obd_device *obd,
1219 struct obd_quotactl *oqctl, int set)
1221 struct obd_device_target *obt = &obd->u.obt;
1222 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1223 struct mds_obd *mds = &obd->u.mds;
1224 struct obd_quotactl *ioqc;
1225 struct lustre_qunit_size *lqs;
1226 unsigned int id[MAXQUOTAS] = { 0, 0 };
1231 /* if we are going to set zero limit, needn't init slaves */
1232 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1236 OBD_ALLOC_PTR(ioqc);
1240 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1241 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1242 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1243 ioqc->qc_id = oqctl->qc_id;
1244 ioqc->qc_type = oqctl->qc_type;
1245 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1246 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1248 /* build lqs for mds */
1249 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1250 qctxt, flag ? 1 : 0);
1251 if (lqs && !IS_ERR(lqs)) {
1253 lqs->lqs_flags |= QB_SET;
1255 lqs->lqs_flags &= ~QB_SET;
1258 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1259 flag ? "create" : "search",
1260 oqctl->qc_type ? "group" : "user",
1262 GOTO(out, rc = PTR_ERR(lqs));
1265 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1269 /* trigger local qunit pre-acquire */
1270 if (oqctl->qc_type == USRQUOTA)
1271 id[USRQUOTA] = oqctl->qc_id;
1273 id[GRPQUOTA] = oqctl->qc_id;
1275 /* initialize all slave's limit */
1276 rc = obd_quotactl(mds->mds_osc_exp, ioqc);
1278 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1279 if (rc == -EDQUOT || rc == -EBUSY) {
1280 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1284 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1294 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1296 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1299 QAQ_SET_CREATE_LQS(qaq);
1300 /* adjust local lqs */
1301 rc = quota_adjust_slave_lqs(qaq, qctxt);
1303 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1305 /* adjust remote lqs */
1306 if (QAQ_IS_ADJBLK(qaq)) {
1307 rc = obd_quota_adjust_qunit(obd->u.mds.mds_osc_exp, qaq, qctxt);
1309 CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1314 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1316 struct mds_obd *mds = &obd->u.mds;
1317 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1318 struct obd_device *lov_obd = class_exp2obd(mds->mds_osc_exp);
1319 struct lov_obd *lov = &lov_obd->u.lov;
1320 struct quota_adjust_qunit *oqaq = NULL;
1321 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1322 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1323 time_t btime, itime;
1324 struct lustre_dquot *dquot;
1325 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1326 /* orig_set means if quota was set before; now_set means we are
1327 * setting/cancelling quota */
1328 int orig_set, now_set;
1329 int rc, rc2 = 0, flag = 0;
1332 if (oqctl->qc_type != USRQUOTA &&
1333 oqctl->qc_type != GRPQUOTA)
1336 OBD_ALLOC_PTR(oqaq);
1339 down(&mds->mds_qonoff_sem);
1340 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1342 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1343 CWARN("quota[%u] is off\n", oqctl->qc_type);
1344 GOTO(out_sem, rc = -ESRCH);
1347 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1349 GOTO(out_sem, rc = PTR_ERR(dquot));
1350 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1351 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1353 down(&dquot->dq_sem);
1355 if (dquot->dq_status) {
1357 lustre_dqput(dquot);
1358 GOTO(out_sem, rc = -EBUSY);
1360 dquot->dq_status |= DQ_STATUS_SET;
1362 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1363 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1364 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1365 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1366 btime = dquot->dq_dqb.dqb_btime;
1367 itime = dquot->dq_dqb.dqb_itime;
1369 if (dqblk->dqb_valid & QIF_BTIME)
1370 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1371 if (dqblk->dqb_valid & QIF_ITIME)
1372 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1374 if (dqblk->dqb_valid & QIF_BLIMITS) {
1375 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1376 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1377 /* clear usage (limit pool) */
1378 if (!dquot->dq_dqb.dqb_bhardlimit &&
1379 !dquot->dq_dqb.dqb_bsoftlimit)
1380 dquot->dq_dqb.dqb_curspace = 0;
1382 /* clear grace time */
1383 if (!dqblk->dqb_bsoftlimit ||
1384 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1385 dquot->dq_dqb.dqb_btime = 0;
1386 /* set grace only if user hasn't provided his own */
1387 else if (!(dqblk->dqb_valid & QIF_BTIME))
1388 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1389 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1391 flag |= LQUOTA_FLAGS_ADJBLK;
1394 if (dqblk->dqb_valid & QIF_ILIMITS) {
1395 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1396 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1397 /* clear usage (limit pool) */
1398 if (!dquot->dq_dqb.dqb_ihardlimit &&
1399 !dquot->dq_dqb.dqb_isoftlimit)
1400 dquot->dq_dqb.dqb_curinodes = 0;
1402 if (!dqblk->dqb_isoftlimit ||
1403 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1404 dquot->dq_dqb.dqb_itime = 0;
1405 else if (!(dqblk->dqb_valid & QIF_ITIME))
1406 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1407 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1409 flag |= LQUOTA_FLAGS_ADJINO;
1411 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1412 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1414 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1416 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1419 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1424 CERROR("set limit failed! (rc:%d)\n", rc);
1428 up(&mds->mds_qonoff_sem);
1430 adjust_lqs(obd, oqaq);
1432 orig_set = ihardlimit || isoftlimit;
1433 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1434 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1435 down(&dquot->dq_sem);
1436 dquot->dq_dqb.dqb_curinodes = 0;
1438 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1440 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1445 orig_set = bhardlimit || bsoftlimit;
1446 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1447 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1448 down(&dquot->dq_sem);
1449 dquot->dq_dqb.dqb_curspace = 0;
1451 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1453 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1459 down(&mds->mds_qonoff_sem);
1460 down(&dquot->dq_sem);
1462 /* cancel previous setting */
1463 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1464 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1465 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1466 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1467 dquot->dq_dqb.dqb_btime = btime;
1468 dquot->dq_dqb.dqb_itime = itime;
1470 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1474 down(&dquot->dq_sem);
1475 dquot->dq_status &= ~DQ_STATUS_SET;
1477 lustre_dqput(dquot);
1480 up(&mds->mds_qonoff_sem);
1485 return rc ? rc : rc2;
1488 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1490 struct obd_quotactl *soqc;
1491 struct lvfs_run_ctxt saved;
1495 OBD_ALLOC_PTR(soqc);
1499 soqc->qc_cmd = Q_GETOQUOTA;
1500 soqc->qc_id = oqctl->qc_id;
1501 soqc->qc_type = oqctl->qc_type;
1503 /* get block usage from OSS */
1504 soqc->qc_dqblk.dqb_curspace = 0;
1505 rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
1506 if (!rc || rc == -EREMOTEIO) {
1507 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1508 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1511 /* get block/inode usage from MDS */
1512 soqc->qc_dqblk.dqb_curspace = 0;
1513 soqc->qc_dqblk.dqb_curinodes = 0;
1514 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1515 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1516 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1518 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1519 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1520 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1528 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1530 struct mds_obd *mds = &obd->u.mds;
1531 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1532 struct lustre_dquot *dquot;
1533 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1537 if (oqctl->qc_type != USRQUOTA &&
1538 oqctl->qc_type != GRPQUOTA)
1541 down(&mds->mds_qonoff_sem);
1542 dqblk->dqb_valid = 0;
1543 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1544 CWARN("quota[%u] is off\n", oqctl->qc_type);
1545 GOTO(out, rc = -ESRCH);
1548 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1550 GOTO(out, rc = PTR_ERR(dquot));
1552 down(&dquot->dq_sem);
1553 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1554 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1555 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1556 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1557 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1558 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1559 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1562 lustre_dqput(dquot);
1564 /* the usages in admin quota file is inaccurate */
1565 dqblk->dqb_curinodes = 0;
1566 dqblk->dqb_curspace = 0;
1567 rc = mds_get_space(obd, oqctl);
1571 up(&mds->mds_qonoff_sem);
1575 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1577 struct lvfs_run_ctxt saved;
1581 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1582 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1583 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1589 /* FIXME we only recovery block limit by now, need recovery inode
1590 * limits also after CMD involved in */
1592 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1594 struct mds_obd *mds = &obd->u.mds;
1595 struct lustre_quota_info *qinfo= &mds->mds_quota_info;
1596 struct lustre_dquot *dquot;
1597 struct obd_quotactl *qctl;
1598 __u64 total_limits = 0;
1602 OBD_ALLOC_PTR(qctl);
1606 dquot = lustre_dqget(obd, qinfo, id, type);
1607 if (IS_ERR(dquot)) {
1608 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1610 RETURN(PTR_ERR(dquot));
1613 down(&dquot->dq_sem);
1615 /* don't recovery the dquot without limits or under setting */
1616 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1619 dquot->dq_status |= DQ_STATUS_RECOVERY;
1623 /* get real bhardlimit from all slaves. */
1624 qctl->qc_cmd = Q_GETOQUOTA;
1625 qctl->qc_type = type;
1627 qctl->qc_stat = QUOTA_RECOVERING;
1628 rc = obd_quotactl(mds->mds_osc_exp, qctl);
1631 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1633 /* get real bhardlimit from master */
1634 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1637 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1639 /* amend the usage of the administrative quotafile */
1640 down(&mds->mds_qonoff_sem);
1641 down(&dquot->dq_sem);
1643 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1645 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1647 CERROR("write dquot failed! (rc:%d)\n", rc);
1650 up(&mds->mds_qonoff_sem);
1653 down(&dquot->dq_sem);
1654 dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1658 lustre_dqput(dquot);
1663 struct qmaster_recov_thread_data {
1664 struct obd_device *obd;
1665 struct completion comp;
1668 static int qmaster_recovery_main(void *arg)
1670 struct qmaster_recov_thread_data *data = arg;
1671 struct obd_device *obd = data->obd;
1672 struct mds_obd *mds = &obd->u.mds;
1673 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1675 unsigned short type;
1678 ptlrpc_daemonize("qmaster_recovd");
1681 class_incref(obd, "qmaster_recovd_mds", obd);
1683 class_incref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1685 complete(&data->comp);
1687 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1688 struct list_head id_list;
1689 struct dquot_id *dqid, *tmp;
1691 down(&mds->mds_qonoff_sem);
1692 if (qinfo->qi_files[type] == NULL) {
1693 up(&mds->mds_qonoff_sem);
1696 CFS_INIT_LIST_HEAD(&id_list);
1697 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1699 up(&mds->mds_qonoff_sem);
1702 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1704 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1705 list_del_init(&dqid->di_link);
1709 rc = dquot_recovery(obd, dqid->di_id, type);
1711 CERROR("qmaster recovery failed! (id:%d type:%d"
1712 " rc:%d)\n", dqid->di_id, type, rc);
1717 class_decref(mds->mds_osc_obd, "qmaster_recovd_lov", mds->mds_osc_obd);
1718 class_decref(obd, "qmaster_recovd_mds", obd);
1722 int mds_quota_recovery(struct obd_device *obd)
1724 struct mds_obd *mds = &obd->u.mds;
1725 struct qmaster_recov_thread_data data;
1729 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1732 mutex_down(&obd->obd_dev_sem);
1733 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1734 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1735 mds->mds_lov_desc.ld_active_tgt_count,
1736 mds->mds_lov_objid_count);
1737 mutex_up(&obd->obd_dev_sem);
1740 mutex_up(&obd->obd_dev_sem);
1743 init_completion(&data.comp);
1745 rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1747 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1749 wait_for_completion(&data.comp);
1753 #endif /* HAVE_QUOTA_SUPPORT */