4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 /* lock ordering: mds->mds_qonoff_sem > dquot->dq_mutex > lqs->lqs_lock */
66 static cfs_list_t lustre_dquot_hash[NR_DQHASH];
67 static cfs_rwlock_t dquot_hash_lock = CFS_RW_LOCK_UNLOCKED;
69 cfs_mem_cache_t *lustre_dquot_cachep;
71 int lustre_dquot_init(void)
76 LASSERT(lustre_dquot_cachep == NULL);
77 lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
78 sizeof(struct lustre_dquot),
80 if (!lustre_dquot_cachep)
83 for (i = 0; i < NR_DQHASH; i++) {
84 CFS_INIT_LIST_HEAD(lustre_dquot_hash + i);
89 void lustre_dquot_exit(void)
93 /* FIXME cleanup work ?? */
95 for (i = 0; i < NR_DQHASH; i++) {
96 LASSERT(cfs_list_empty(lustre_dquot_hash + i));
98 if (lustre_dquot_cachep) {
100 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
101 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
102 lustre_dquot_cachep = NULL;
108 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
109 __attribute__((__const__));
112 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
114 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
115 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
119 /* caller must hold dquot_hash_lock */
120 static struct lustre_dquot *find_dquot(int hashent,
121 struct lustre_quota_info *lqi, qid_t id,
124 struct lustre_dquot *dquot;
127 cfs_list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
128 if (dquot->dq_info == lqi &&
129 dquot->dq_id == id && dquot->dq_type == type)
135 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
138 struct lustre_dquot *dquot = NULL;
141 OBD_SLAB_ALLOC_PTR_GFP(dquot, lustre_dquot_cachep, CFS_ALLOC_IO);
145 CFS_INIT_LIST_HEAD(&dquot->dq_hash);
146 cfs_mutex_init(&dquot->dq_mutex);
147 cfs_mutex_lock(&dquot->dq_mutex);
148 cfs_atomic_set(&dquot->dq_refcnt, 1);
149 dquot->dq_info = lqi;
151 dquot->dq_type = type;
156 static void free_dquot(struct lustre_dquot *dquot)
158 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
161 static void insert_dquot_nolock(struct lustre_dquot *dquot)
163 cfs_list_t *head = lustre_dquot_hash +
164 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
165 LASSERT(cfs_list_empty(&dquot->dq_hash));
166 cfs_list_add(&dquot->dq_hash, head);
169 static void remove_dquot_nolock(struct lustre_dquot *dquot)
171 LASSERT(!cfs_list_empty(&dquot->dq_hash));
172 cfs_list_del_init(&dquot->dq_hash);
175 static void lustre_dqput(struct lustre_dquot *dquot)
178 cfs_write_lock(&dquot_hash_lock);
179 LASSERT(cfs_atomic_read(&dquot->dq_refcnt));
180 cfs_atomic_dec(&dquot->dq_refcnt);
181 if (cfs_atomic_read(&dquot->dq_refcnt) == 0) {
182 remove_dquot_nolock(dquot);
185 cfs_write_unlock(&dquot_hash_lock);
189 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
190 struct lustre_quota_info *lqi,
191 qid_t id, int type, int can_fake)
193 unsigned int hashent = dquot_hashfn(lqi, id, type);
194 struct lustre_dquot *dquot, *empty;
198 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
199 RETURN(ERR_PTR(-ENOMEM));
201 cfs_read_lock(&dquot_hash_lock);
202 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
203 cfs_atomic_inc(&dquot->dq_refcnt);
204 cfs_read_unlock(&dquot_hash_lock);
205 cfs_mutex_unlock(&empty->dq_mutex);
210 cfs_read_unlock(&dquot_hash_lock);
213 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
214 cfs_mutex_unlock(&dquot->dq_mutex);
216 CERROR("can't read dquot from admin quotafile! "
221 cfs_write_lock(&dquot_hash_lock);
222 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
223 cfs_atomic_inc(&dquot->dq_refcnt);
227 insert_dquot_nolock(dquot);
229 cfs_write_unlock(&dquot_hash_lock);
235 if (!can_fake && cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
236 DQUOT_DEBUG(dquot, "It is a fake dquot: unexpected!\n");
238 dquot = ERR_PTR(-ENOENT);
248 static void init_oqaq(struct quota_adjust_qunit *oqaq,
249 struct lustre_quota_ctxt *qctxt,
252 struct lustre_qunit_size *lqs = NULL;
255 oqaq->qaq_flags = type;
256 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 0);
257 if (lqs && !IS_ERR(lqs)) {
258 cfs_spin_lock(&lqs->lqs_lock);
259 oqaq->qaq_bunit_sz = lqs->lqs_bunit_sz;
260 oqaq->qaq_iunit_sz = lqs->lqs_iunit_sz;
261 oqaq->qaq_flags = lqs->lqs_flags;
262 cfs_spin_unlock(&lqs->lqs_lock);
265 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
266 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
267 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
271 int dqacq_adjust_qunit_sz(struct obd_device *obd, qid_t id, int type,
274 struct mds_obd *mds = &obd->u.mds;
275 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
276 __u32 ost_num = mds->mds_lov_objid_count, mdt_num = 1;
277 struct quota_adjust_qunit *oqaq = NULL;
278 unsigned int qid[MAXQUOTAS] = { 0, 0 };
279 struct lustre_quota_info *info = &mds->mds_quota_info;
280 struct lustre_dquot *dquot = NULL;
286 cfs_down_read(&mds->mds_qonoff_sem);
287 dquot = lustre_dqget(obd, info, id, type, 0);
289 RETURN(PTR_ERR(dquot));
291 cfs_up_read(&mds->mds_qonoff_sem);
294 GOTO(out, rc = -ENOMEM);
296 cfs_mutex_lock(&dquot->dq_mutex);
297 init_oqaq(oqaq, qctxt, id, type);
299 rc = dquot_create_oqaq(qctxt, dquot, ost_num, mdt_num,
300 is_blk ? LQUOTA_FLAGS_ADJBLK :
301 LQUOTA_FLAGS_ADJINO, oqaq);
304 CERROR("create oqaq failed! (rc:%d)\n", rc);
307 QAQ_DEBUG(oqaq, "show oqaq.\n")
309 if (!QAQ_IS_ADJBLK(oqaq) && !QAQ_IS_ADJINO(oqaq))
312 /* adjust the mds slave qunit size */
313 adjust_res = quota_adjust_slave_lqs(oqaq, qctxt);
314 if (adjust_res <= 0) {
315 if (adjust_res < 0) {
317 CERROR("adjust mds slave's qunit size failed! "
320 CDEBUG(D_QUOTA, "qunit doesn't need to be adjusted.\n");
326 qid[GRPQUOTA] = dquot->dq_id;
328 qid[USRQUOTA] = dquot->dq_id;
330 cfs_mutex_unlock(&dquot->dq_mutex);
332 rc = qctxt_adjust_qunit(obd, qctxt, qid, is_blk, 0, NULL);
333 if (rc == -EDQUOT || rc == -EBUSY) {
334 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
338 CERROR("%s: mds fail to adjust file quota! (rc:%d)\n",
343 /* only when block qunit is reduced, boardcast to osts */
344 if ((adjust_res & LQS_BLK_DECREASE) && QAQ_IS_ADJBLK(oqaq))
345 rc = obd_quota_adjust_qunit(mds->mds_lov_exp, oqaq, qctxt, NULL);
354 cfs_mutex_unlock(&dquot->dq_mutex);
358 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
360 struct mds_obd *mds = &obd->u.mds;
361 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
362 struct lustre_quota_info *info = &mds->mds_quota_info;
363 struct lustre_dquot *dquot = NULL;
365 __u64 hlimit = 0, slimit = 0;
367 unsigned int grace = 0;
368 struct lustre_qunit_size *lqs = NULL;
372 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_DQACQ))
375 if (!ll_sb_has_quota_active(qctxt->lqc_sb,
376 QDATA_IS_GRP(qdata) ? GRPQUOTA : USRQUOTA))
379 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
388 cfs_spin_lock(&lqs->lqs_lock);
389 if (LQS_IS_RECOVERY(lqs)) {
390 cfs_spin_unlock(&lqs->lqs_lock);
391 LQS_DEBUG(lqs, "this lqs is under recovery\n");
392 GOTO(skip, rc = -EBUSY);
394 cfs_spin_unlock(&lqs->lqs_lock);
396 cfs_down_write(&mds->mds_qonoff_sem);
397 dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata), 0);
399 cfs_up_write(&mds->mds_qonoff_sem);
400 GOTO(skip, rc = PTR_ERR(dquot));
403 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
404 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
406 cfs_mutex_lock(&dquot->dq_mutex);
408 if (QDATA_IS_BLK(qdata)) {
409 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
410 usage = &dquot->dq_dqb.dqb_curspace;
411 hlimit = dquot->dq_dqb.dqb_bhardlimit;
412 slimit = dquot->dq_dqb.dqb_bsoftlimit;
413 time = &dquot->dq_dqb.dqb_btime;
415 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
416 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
417 hlimit = dquot->dq_dqb.dqb_ihardlimit;
418 slimit = dquot->dq_dqb.dqb_isoftlimit;
419 time = &dquot->dq_dqb.dqb_itime;
422 /* if the quota limit in admin quotafile is zero, we just inform
423 * slave to clear quota limit with zero qd_count */
424 if (hlimit == 0 && slimit == 0) {
432 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
434 if (QDATA_IS_CHANGE_QS(qdata) &&
435 QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
436 qdata->qd_count = (hlimit -
437 QUSG(*usage, QDATA_IS_BLK(qdata)))
438 * (QDATA_IS_BLK(qdata) ?
439 QUOTABLOCK_SIZE : 1);
441 GOTO(out, rc = -EDQUOT);
445 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
446 if (*time && cfs_time_current_sec() >= *time)
447 GOTO(out, rc = -EDQUOT);
449 *time = cfs_time_current_sec() + grace;
452 *usage += qdata->qd_count;
455 /* The usage in administrative file might be incorrect before
457 if (*usage < qdata->qd_count)
460 *usage -= qdata->qd_count;
462 /* (usage <= soft limit) but not (usage < soft limit) */
463 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
470 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
473 cfs_mutex_unlock(&dquot->dq_mutex);
474 cfs_up_write(&mds->mds_qonoff_sem);
477 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
478 QDATA_IS_BLK(qdata));
480 cfs_spin_lock(&lqs->lqs_lock);
481 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
483 cfs_spin_unlock(&lqs->lqs_lock);
485 if (QDATA_IS_BLK(qdata))
486 QDATA_SET_ADJBLK(qdata);
488 QDATA_SET_ADJINO(qdata);
490 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
497 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
498 const unsigned int qpids[], int rc, int opc)
500 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
504 if (rc && rc != -EDQUOT && rc != ENOLCK)
508 case FSFILT_OP_SETATTR:
509 /* release file quota on original owner */
510 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
511 /* release block quota on original owner */
512 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
513 /* acquire file quota on current owner */
514 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
515 /* acquire block quota on current owner */
516 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
518 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
519 /* release file quota on child */
520 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
521 /* rlease block quota on child */
522 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
524 case FSFILT_OP_CREATE_PARTIAL_CHILD:
525 /* acquire file quota on child */
526 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
527 /* acquire block quota on child */
528 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
531 /* acquire block quota on parent */
532 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
534 case FSFILT_OP_UNLINK:
535 /* release block quota on parent */
536 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
537 /* release file quota on child */
538 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
539 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
540 /* release block quota on child */
541 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
544 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
545 /* release block quota on parent */
546 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
548 case FSFILT_OP_CREATE:
549 /* acquire block quota on parent */
550 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
551 /* acquire file quota on child */
552 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
553 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
554 /* acquire block quota on child */
555 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
565 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
566 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
570 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
571 const unsigned int qpids[], int rc, int opc)
573 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
577 if (rc && rc != -EDQUOT)
581 case FSFILT_OP_SETATTR:
582 /* acquire/release block quota on original & current owner */
583 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
584 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
586 case FSFILT_OP_UNLINK:
587 /* release block quota on this owner */
588 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
589 /* acquire block quota on this owner */
590 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
601 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
602 rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
608 static const char prefix[] = "OBJECTS/";
610 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
612 struct mds_obd *mds = &obd->u.mds;
613 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
614 struct obd_device_target *obt = &obd->u.obt;
615 int rc = 0, i, rc1 = 0;
616 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
618 struct lvfs_run_ctxt saved;
621 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
623 if (oqctl->qc_type != USRQUOTA &&
624 oqctl->qc_type != GRPQUOTA &&
625 oqctl->qc_type != UGQUOTA)
628 cfs_down(&obt->obt_quotachecking);
629 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
630 cfs_down_write(&mds->mds_qonoff_sem);
632 for (i = 0; i < MAXQUOTAS; i++) {
635 if (!Q_TYPESET(oqctl, i))
638 /* quota file has been opened ? */
639 if (qinfo->qi_files[i]) {
640 CWARN("quota[%d] is on yet\n", i);
645 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
646 sprintf(name, "%s%s", prefix, quotafile[i]);
648 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
651 CERROR("%s: error invalidating admin quotafile %s (rc:%d)\n",
652 obd->obd_name, name, rc);
658 cfs_up_write(&mds->mds_qonoff_sem);
659 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
660 cfs_up(&obt->obt_quotachecking);
664 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
666 struct mds_obd *mds = &obd->u.mds;
667 struct obd_device_target *obt = &obd->u.obt;
669 struct lvfs_run_ctxt saved;
672 if (oqctl->qc_type != USRQUOTA &&
673 oqctl->qc_type != GRPQUOTA &&
674 oqctl->qc_type != UGQUOTA)
677 cfs_down(&obt->obt_quotachecking);
678 if (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
679 GOTO(out, rc = -EBUSY);
680 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
681 cfs_down_write(&mds->mds_qonoff_sem);
683 oqctl->qc_cmd = Q_FINVALIDATE;
684 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
686 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
688 cfs_up_write(&mds->mds_qonoff_sem);
689 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
691 cfs_up(&obt->obt_quotachecking);
695 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
697 struct mds_obd *mds = &obd->u.mds;
698 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
699 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
700 struct lvfs_run_ctxt saved;
705 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
707 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
708 cfs_down_write(&mds->mds_qonoff_sem);
710 for (i = 0; i < MAXQUOTAS && !rc; i++) {
713 if (!Q_TYPESET(oqctl, i))
716 /* quota file has been opened ? */
717 if (qinfo->qi_files[i]) {
718 CWARN("init %s admin quotafile while quota on.\n",
719 i == USRQUOTA ? "user" : "group");
723 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
724 sprintf(name, "%s%s", prefix, quotafile[i]);
726 /* check if quota file exists and is correct */
727 fp = filp_open(name, O_RDONLY, 0);
729 /* irregular file is not the right place for quota */
730 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
731 CERROR("admin quota file %s is not "
737 qinfo->qi_files[i] = fp;
738 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
739 qinfo->qi_files[i] = NULL;
748 /* -EINVAL may be returned by quotainfo for bad quota file */
749 if (rc != -ENOENT && rc != -EINVAL) {
750 CERROR("%s: error opening old quota file %s (%d)\n",
751 obd->obd_name, name, rc);
755 CDEBUG(D_INFO, "%s new quota file %s\n", name,
756 rc == -ENOENT ? "creating" : "overwriting");
758 /* create quota file overwriting old if needed */
759 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
762 CERROR("%s: error creating admin quotafile %s (rc:%d)\n",
763 obd->obd_name, name, rc);
767 qinfo->qi_files[i] = fp;
769 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
771 CERROR("error init %s admin quotafile! (rc:%d)\n",
772 i == USRQUOTA ? "user" : "group", rc);
775 qinfo->qi_files[i] = NULL;
778 cfs_up_write(&mds->mds_qonoff_sem);
779 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
783 static int close_quota_files(struct obd_quotactl *oqctl,
784 struct lustre_quota_info *qinfo)
789 for (i = 0; i < MAXQUOTAS; i++) {
790 if (!Q_TYPESET(oqctl, i))
792 if (qinfo->qi_files[i] == NULL) {
793 CDEBUG(D_QUOTA, "quota[%d] is off already\n", i);
797 filp_close(qinfo->qi_files[i], 0);
798 qinfo->qi_files[i] = NULL;
803 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
805 struct mds_obd *mds = &obd->u.mds;
806 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
807 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
809 int i, rc = 0, rc1 = 0;
812 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
814 /* open admin quota files and read quotafile info */
815 for (i = 0; i < MAXQUOTAS; i++) {
818 if (!Q_TYPESET(oqctl, i) || qinfo->qi_files[i] != NULL)
821 LASSERT(strlen(quotafile[i])
822 + sizeof(prefix) <= sizeof(name));
823 sprintf(name, "%s%s", prefix, quotafile[i]);
824 fp = filp_open(name, O_RDWR, 0);
825 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
826 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
827 CERROR("error open/create %s! (rc:%d)\n", name, rc);
830 qinfo->qi_files[i] = fp;
832 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
834 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
838 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
840 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
846 if (rc && rc1 != -EALREADY)
847 close_quota_files(oqctl, qinfo);
852 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
857 if (oqctl->qc_type != USRQUOTA &&
858 oqctl->qc_type != GRPQUOTA &&
859 oqctl->qc_type != UGQUOTA)
862 rc = generic_quota_on(obd, oqctl, 1);
868 int mds_admin_quota_off(struct obd_device *obd,
869 struct obd_quotactl *oqctl)
871 struct mds_obd *mds = &obd->u.mds;
872 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
876 /* close admin quota files */
877 rc = close_quota_files(oqctl, qinfo);
882 /* with obt->obt_quotachecking held */
883 int do_mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
885 struct mds_obd *mds = &obd->u.mds;
886 struct obd_device_target *obt = &obd->u.obt;
887 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
888 struct lvfs_run_ctxt saved;
889 int rc = 0, rc1 = 0, rc2 = 0;
892 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
894 if (oqctl->qc_type != USRQUOTA &&
895 oqctl->qc_type != GRPQUOTA &&
896 oqctl->qc_type != UGQUOTA)
899 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
900 cfs_down_write(&mds->mds_qonoff_sem);
901 /* close admin quota files */
902 rc2 = mds_admin_quota_off(obd, oqctl);
903 if (rc2 && rc2 != -EALREADY) {
904 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
908 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
910 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
911 } else if (quota_is_off(qctxt, oqctl)) {
912 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
915 if (rc2 != -EALREADY) {
916 CWARN("mds local quota[%d] is failed to be off for %d\n",
917 oqctl->qc_type, rc1);
918 oqctl->qc_cmd = Q_QUOTAON;
919 mds_admin_quota_on(obd, oqctl);
920 oqctl->qc_cmd = Q_QUOTAOFF;
925 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
926 if (rc && rc != -EALREADY) {
927 CWARN("mds remote quota[%d] is failed to be off for %d\n",
929 oqctl->qc_cmd = Q_QUOTAON;
930 if (rc2 != -EALREADY)
931 mds_admin_quota_on(obd, oqctl);
932 if (rc1 != -EALREADY) {
933 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
934 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
936 oqctl->qc_cmd = Q_QUOTAOFF;
941 CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc %u:%lu:%d\n",
942 obd->obd_name, oqctl->qc_type, qctxt->lqc_flags, rc);
943 cfs_up_write(&mds->mds_qonoff_sem);
944 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
945 return rc ? : (rc1 ? : rc2);
948 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
950 struct obd_device_target *obt = &obd->u.obt;
954 cfs_down(&obt->obt_quotachecking);
955 rc = do_mds_quota_off(obd, oqctl);
956 cfs_up(&obt->obt_quotachecking);
960 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
962 struct mds_obd *mds = &obd->u.mds;
963 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
964 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
968 if (oqctl->qc_type != USRQUOTA &&
969 oqctl->qc_type != GRPQUOTA)
972 cfs_down_write(&mds->mds_qonoff_sem);
973 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
974 CWARN("quota[%u] is off\n", oqctl->qc_type);
975 GOTO(out, rc = -ESRCH);
978 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
979 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
980 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
982 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
986 cfs_up_write(&mds->mds_qonoff_sem);
990 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
992 struct mds_obd *mds = &obd->u.mds;
993 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
994 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
998 if (oqctl->qc_type != USRQUOTA &&
999 oqctl->qc_type != GRPQUOTA)
1002 cfs_down_read(&mds->mds_qonoff_sem);
1003 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1004 CWARN("quota[%u] is off\n", oqctl->qc_type);
1005 GOTO(out, rc = -ESRCH);
1008 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
1009 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
1010 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
1014 cfs_up_read(&mds->mds_qonoff_sem);
1018 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
1019 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
1020 int type, struct quota_adjust_qunit *oqaq)
1022 __u64 bunit_curr_o, iunit_curr_o;
1023 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
1024 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
1025 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
1026 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
1027 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1028 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1032 if (!dquot || !oqaq)
1034 LASSERT_MUTEX_LOCKED(&dquot->dq_mutex);
1035 LASSERT(oqaq->qaq_iunit_sz);
1036 LASSERT(oqaq->qaq_bunit_sz);
1038 /* don't change qunit size */
1039 if (!qctxt->lqc_switch_qs)
1042 bunit_curr_o = oqaq->qaq_bunit_sz;
1043 iunit_curr_o = oqaq->qaq_iunit_sz;
1045 if (dquot->dq_type == GRPQUOTA)
1048 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1049 __u64 b_limitation =
1050 oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1051 /* enlarge block qunit size */
1053 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1054 oqaq->qaq_bunit_sz =
1055 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1057 if (oqaq->qaq_bunit_sz >= qctxt->lqc_bunit_sz)
1059 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1063 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1064 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1066 /* shrink block qunit size */
1068 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1069 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1070 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1072 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1074 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1078 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1079 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1081 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1082 QAQ_SET_ADJBLK(oqaq);
1086 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1087 __u64 i_limitation =
1088 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1089 /* enlarge file qunit size */
1090 while (ilimit > dquot->dq_dqb.dqb_curinodes
1091 + 2 * i_limitation) {
1092 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1093 if (oqaq->qaq_iunit_sz >= qctxt->lqc_iunit_sz)
1095 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1099 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1100 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1102 /* shrink file qunit size */
1103 while (ilimit < dquot->dq_dqb.dqb_curinodes
1105 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1106 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1108 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1112 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1113 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1115 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1116 QAQ_SET_ADJINO(oqaq);
1120 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1125 static int mds_init_slave_ilimits(struct obd_device *obd,
1126 struct obd_quotactl *oqctl, int set)
1128 /* XXX: for file limits only adjust local now */
1129 struct obd_device_target *obt = &obd->u.obt;
1130 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1131 unsigned int id[MAXQUOTAS] = { 0, 0 };
1132 struct obd_quotactl *ioqc = NULL;
1133 struct lustre_qunit_size *lqs;
1138 /* if we are going to set zero limit, needn't init slaves */
1139 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1143 OBD_ALLOC_PTR(ioqc);
1147 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1148 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1149 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1150 ioqc->qc_id = oqctl->qc_id;
1151 ioqc->qc_type = oqctl->qc_type;
1152 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1153 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1155 /* build lqs for mds */
1156 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1157 qctxt, flag ? 1 : 0);
1158 if (lqs && !IS_ERR(lqs)) {
1160 lqs->lqs_flags |= QI_SET;
1162 lqs->lqs_flags &= ~QI_SET;
1165 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1166 flag ? "create" : "search",
1167 oqctl->qc_type ? "group" : "user",
1169 GOTO(out, rc = PTR_ERR(lqs));
1172 /* set local limit to MIN_QLIMIT */
1173 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1177 /* trigger local qunit pre-acquire */
1178 if (oqctl->qc_type == USRQUOTA)
1179 id[USRQUOTA] = oqctl->qc_id;
1181 id[GRPQUOTA] = oqctl->qc_id;
1183 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1184 if (rc == -EDQUOT || rc == -EBUSY) {
1185 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1189 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1193 /* FIXME initialize all slaves in CMD */
1201 static int mds_init_slave_blimits(struct obd_device *obd,
1202 struct obd_quotactl *oqctl, int set)
1204 struct obd_device_target *obt = &obd->u.obt;
1205 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1206 struct mds_obd *mds = &obd->u.mds;
1207 struct obd_quotactl *ioqc;
1208 struct lustre_qunit_size *lqs;
1209 unsigned int id[MAXQUOTAS] = { 0, 0 };
1214 /* if we are going to set zero limit, needn't init slaves */
1215 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1219 OBD_ALLOC_PTR(ioqc);
1223 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1224 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1225 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1226 ioqc->qc_id = oqctl->qc_id;
1227 ioqc->qc_type = oqctl->qc_type;
1228 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1229 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1231 /* build lqs for mds */
1232 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1233 qctxt, flag ? 1 : 0);
1234 if (lqs && !IS_ERR(lqs)) {
1236 lqs->lqs_flags |= QB_SET;
1238 lqs->lqs_flags &= ~QB_SET;
1241 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1242 flag ? "create" : "search",
1243 oqctl->qc_type ? "group" : "user",
1245 GOTO(out, rc = PTR_ERR(lqs));
1248 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1252 /* trigger local qunit pre-acquire */
1253 if (oqctl->qc_type == USRQUOTA)
1254 id[USRQUOTA] = oqctl->qc_id;
1256 id[GRPQUOTA] = oqctl->qc_id;
1258 /* initialize all slave's limit */
1259 rc = obd_quotactl(mds->mds_lov_exp, ioqc);
1261 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1262 if (rc == -EDQUOT || rc == -EBUSY) {
1263 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1267 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1277 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1279 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1282 QAQ_SET_CREATE_LQS(qaq);
1283 /* adjust local lqs */
1284 rc = quota_adjust_slave_lqs(qaq, qctxt);
1286 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1288 /* adjust remote lqs */
1289 if (QAQ_IS_ADJBLK(qaq)) {
1290 rc = obd_quota_adjust_qunit(obd->u.mds.mds_lov_exp, qaq, qctxt, NULL);
1292 CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1297 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1299 struct mds_obd *mds = &obd->u.mds;
1300 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1301 struct obd_device *lov_obd = class_exp2obd(mds->mds_lov_exp);
1302 struct lov_obd *lov = &lov_obd->u.lov;
1303 struct quota_adjust_qunit *oqaq = NULL;
1304 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1305 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1306 time_t btime, itime;
1307 struct lustre_dquot *dquot;
1308 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1309 /* orig_set means if quota was set before; now_set means we are
1310 * setting/cancelling quota */
1311 int orig_set, now_set;
1312 struct lustre_qunit_size *lqs;
1313 int rc = 0, rc2 = 0, flag = 0;
1316 if (oqctl->qc_type != USRQUOTA &&
1317 oqctl->qc_type != GRPQUOTA)
1320 OBD_ALLOC_PTR(oqaq);
1324 cfs_down_write(&mds->mds_qonoff_sem);
1325 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1327 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1328 CWARN("quota[%u] is off\n", oqctl->qc_type);
1329 GOTO(out_sem, rc = -ESRCH);
1332 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1334 GOTO(out_sem, rc = PTR_ERR(dquot));
1335 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1336 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1338 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id), qctxt, 1);
1346 cfs_mutex_lock(&dquot->dq_mutex);
1347 cfs_spin_lock(&lqs->lqs_lock);
1348 if (LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1349 cfs_spin_unlock(&lqs->lqs_lock);
1350 cfs_mutex_unlock(&dquot->dq_mutex);
1351 GOTO(skip, rc = -EBUSY);
1353 LQS_SET_SETQUOTA(lqs);
1354 cfs_spin_unlock(&lqs->lqs_lock);
1356 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1357 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1358 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1359 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1360 btime = dquot->dq_dqb.dqb_btime;
1361 itime = dquot->dq_dqb.dqb_itime;
1363 if (dqblk->dqb_valid & QIF_BTIME)
1364 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1365 if (dqblk->dqb_valid & QIF_ITIME)
1366 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1368 if (dqblk->dqb_valid & QIF_BLIMITS) {
1369 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1370 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1371 /* clear usage (limit pool) */
1372 if (!dquot->dq_dqb.dqb_bhardlimit &&
1373 !dquot->dq_dqb.dqb_bsoftlimit)
1374 dquot->dq_dqb.dqb_curspace = 0;
1376 /* clear grace time */
1377 if (!dqblk->dqb_bsoftlimit ||
1378 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1379 dquot->dq_dqb.dqb_btime = 0;
1380 /* set grace only if user hasn't provided his own */
1381 else if (!(dqblk->dqb_valid & QIF_BTIME))
1382 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1383 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1385 flag |= LQUOTA_FLAGS_ADJBLK;
1388 if (dqblk->dqb_valid & QIF_ILIMITS) {
1389 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1390 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1391 /* clear usage (limit pool) */
1392 if (!dquot->dq_dqb.dqb_ihardlimit &&
1393 !dquot->dq_dqb.dqb_isoftlimit)
1394 dquot->dq_dqb.dqb_curinodes = 0;
1396 if (!dqblk->dqb_isoftlimit ||
1397 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1398 dquot->dq_dqb.dqb_itime = 0;
1399 else if (!(dqblk->dqb_valid & QIF_ITIME))
1400 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1401 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1403 flag |= LQUOTA_FLAGS_ADJINO;
1405 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1406 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1408 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1410 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1413 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1415 cfs_mutex_unlock(&dquot->dq_mutex);
1418 CERROR("set limit failed! (rc:%d)\n", rc);
1419 GOTO(update_fail, rc);
1422 cfs_up_write(&mds->mds_qonoff_sem);
1423 adjust_lqs(obd, oqaq);
1425 orig_set = ihardlimit || isoftlimit;
1426 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1427 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1428 cfs_mutex_lock(&dquot->dq_mutex);
1429 dquot->dq_dqb.dqb_curinodes = 0;
1430 cfs_mutex_unlock(&dquot->dq_mutex);
1431 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1433 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1438 orig_set = bhardlimit || bsoftlimit;
1439 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1440 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1441 cfs_mutex_lock(&dquot->dq_mutex);
1442 dquot->dq_dqb.dqb_curspace = 0;
1443 cfs_mutex_unlock(&dquot->dq_mutex);
1444 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1446 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1452 cfs_down_write(&mds->mds_qonoff_sem);
1453 cfs_mutex_lock(&dquot->dq_mutex);
1455 /* cancel previous setting */
1456 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1457 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1458 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1459 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1460 dquot->dq_dqb.dqb_btime = btime;
1461 dquot->dq_dqb.dqb_itime = itime;
1463 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1464 cfs_mutex_unlock(&dquot->dq_mutex);
1466 cfs_spin_lock(&lqs->lqs_lock);
1467 LQS_CLEAR_SETQUOTA(lqs);
1468 cfs_spin_unlock(&lqs->lqs_lock);
1472 lustre_dqput(dquot);
1475 cfs_up_write(&mds->mds_qonoff_sem);
1480 return rc ? rc : rc2;
1483 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1485 struct obd_quotactl *soqc;
1486 struct lvfs_run_ctxt saved;
1490 OBD_ALLOC_PTR(soqc);
1494 soqc->qc_cmd = Q_GETOQUOTA;
1495 soqc->qc_id = oqctl->qc_id;
1496 soqc->qc_type = oqctl->qc_type;
1498 /* get block usage from OSS */
1499 soqc->qc_dqblk.dqb_curspace = 0;
1500 rc = obd_quotactl(obd->u.mds.mds_lov_exp, soqc);
1501 if (!rc || rc == -EREMOTEIO) {
1502 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1503 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1506 /* get block/inode usage from MDS */
1507 soqc->qc_dqblk.dqb_curspace = 0;
1508 soqc->qc_dqblk.dqb_curinodes = 0;
1509 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1510 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1511 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1513 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1514 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1515 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1523 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1525 struct mds_obd *mds = &obd->u.mds;
1526 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1527 struct lustre_dquot *dquot;
1528 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1532 if (oqctl->qc_type != USRQUOTA &&
1533 oqctl->qc_type != GRPQUOTA)
1536 cfs_down_read(&mds->mds_qonoff_sem);
1537 dqblk->dqb_valid = 0;
1538 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1539 CWARN("quota[%u] is off\n", oqctl->qc_type);
1540 GOTO(out, rc = -ESRCH);
1543 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1545 GOTO(out, rc = PTR_ERR(dquot));
1547 cfs_mutex_lock(&dquot->dq_mutex);
1548 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1549 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1550 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1551 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1552 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1553 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1554 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1555 cfs_mutex_unlock(&dquot->dq_mutex);
1557 lustre_dqput(dquot);
1558 cfs_up_read(&mds->mds_qonoff_sem);
1560 /* the usages in admin quota file is inaccurate */
1561 dqblk->dqb_curinodes = 0;
1562 dqblk->dqb_curspace = 0;
1563 rc = mds_get_space(obd, oqctl);
1568 cfs_up_read(&mds->mds_qonoff_sem);
1572 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1574 struct lvfs_run_ctxt saved;
1578 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1579 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1580 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1586 /* FIXME we only recovery block limit by now, need recovery inode
1587 * limits also after CMD involved in */
1589 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1591 struct mds_obd *mds = &obd->u.mds;
1592 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1593 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1594 struct lustre_qunit_size *lqs;
1595 struct lustre_dquot *dquot;
1596 struct obd_quotactl *qctl;
1597 __u64 total_limits = 0;
1601 OBD_ALLOC_PTR(qctl);
1605 dquot = lustre_dqget(obd, qinfo, id, type, 0);
1606 if (IS_ERR(dquot)) {
1607 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1609 RETURN(PTR_ERR(dquot));
1612 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 1);
1620 cfs_mutex_lock(&dquot->dq_mutex);
1622 /* don't recover the dquot without limits or quota is setting or
1623 * another recovery is already going on */
1624 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1625 LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1626 cfs_mutex_unlock(&dquot->dq_mutex);
1627 GOTO(skip1, rc = 0);
1630 cfs_spin_lock(&lqs->lqs_lock);
1631 LQS_SET_RECOVERY(lqs);
1632 cfs_spin_unlock(&lqs->lqs_lock);
1633 cfs_mutex_unlock(&dquot->dq_mutex);
1635 /* release mds_qonoff_sem during obd_quotactl ops here */
1636 cfs_up_write(&mds->mds_qonoff_sem);
1638 /* get real bhardlimit from all slaves. */
1639 qctl->qc_cmd = Q_GETOQUOTA;
1640 qctl->qc_type = type;
1642 qctl->qc_stat = QUOTA_RECOVERING;
1643 rc = obd_quotactl(mds->mds_lov_exp, qctl);
1644 cfs_down_write(&mds->mds_qonoff_sem);
1647 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1649 /* get real bhardlimit from master */
1650 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1653 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1655 /* amend the usage of the administrative quotafile */
1656 cfs_mutex_lock(&dquot->dq_mutex);
1658 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1660 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1662 CERROR("write dquot failed! (rc:%d)\n", rc);
1664 cfs_mutex_unlock(&dquot->dq_mutex);
1667 cfs_spin_lock(&lqs->lqs_lock);
1668 LQS_CLEAR_RECOVERY(lqs);
1669 cfs_spin_unlock(&lqs->lqs_lock);
1673 lustre_dqput(dquot);
1678 struct qmaster_recov_thread_data {
1679 struct obd_device *obd;
1680 cfs_completion_t comp;
1683 static int qmaster_recovery_main(void *arg)
1685 struct qmaster_recov_thread_data *data = arg;
1686 struct obd_device *obd = data->obd;
1687 struct mds_obd *mds = &obd->u.mds;
1688 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1690 unsigned short type;
1693 cfs_daemonize_ctxt("qmaster_recovd");
1696 class_incref(obd, "qmaster_recovd_mds", obd);
1698 class_incref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1700 cfs_complete(&data->comp);
1702 cfs_down_write(&mds->mds_qonoff_sem);
1703 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1705 struct dquot_id *dqid, *tmp;
1707 if (qinfo->qi_files[type] == NULL)
1710 CFS_INIT_LIST_HEAD(&id_list);
1711 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1714 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1716 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1717 cfs_list_del_init(&dqid->di_link);
1721 rc = dquot_recovery(obd, dqid->di_id, type);
1723 CERROR("%s: qmaster recovery failed for %sid %d"
1724 " rc:%d)\n", obd->obd_name,
1725 type ? "g" : "u", dqid->di_id, rc);
1730 cfs_up_write(&mds->mds_qonoff_sem);
1731 class_decref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1732 class_decref(obd, "qmaster_recovd_mds", obd);
1736 int mds_quota_recovery(struct obd_device *obd)
1738 struct mds_obd *mds = &obd->u.mds;
1739 struct qmaster_recov_thread_data data;
1743 if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
1746 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1749 cfs_mutex_lock(&obd->obd_dev_mutex);
1750 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1751 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1752 mds->mds_lov_desc.ld_active_tgt_count,
1753 mds->mds_lov_objid_count);
1754 cfs_mutex_unlock(&obd->obd_dev_mutex);
1757 cfs_mutex_unlock(&obd->obd_dev_mutex);
1760 cfs_init_completion(&data.comp);
1762 rc = cfs_create_thread(qmaster_recovery_main, &data,
1765 CERROR("%s: cannot start quota recovery thread: rc %d\n",
1768 cfs_wait_for_completion(&data.comp);