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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/quota/quota_master.c
40 * Lustre Quota Master request handler
42 * Author: Niu YaWei <niu@clusterfs.com>
46 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_LQUOTA
51 #include <linux/version.h>
53 #include <asm/unistd.h>
54 #include <linux/slab.h>
55 #include <linux/quotaops.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/quota.h>
60 #include <obd_class.h>
61 #include <lustre_quota.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_mds.h>
65 #include "quota_internal.h"
67 #ifdef HAVE_QUOTA_SUPPORT
69 /* lock ordering: mds->mds_qonoff_sem > dquot->dq_mutex > lqs->lqs_lock */
70 static cfs_list_t lustre_dquot_hash[NR_DQHASH];
71 static cfs_rwlock_t dquot_hash_lock = CFS_RW_LOCK_UNLOCKED;
73 cfs_mem_cache_t *lustre_dquot_cachep;
75 int lustre_dquot_init(void)
80 LASSERT(lustre_dquot_cachep == NULL);
81 lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
82 sizeof(struct lustre_dquot),
84 if (!lustre_dquot_cachep)
87 for (i = 0; i < NR_DQHASH; i++) {
88 CFS_INIT_LIST_HEAD(lustre_dquot_hash + i);
93 void lustre_dquot_exit(void)
97 /* FIXME cleanup work ?? */
99 for (i = 0; i < NR_DQHASH; i++) {
100 LASSERT(cfs_list_empty(lustre_dquot_hash + i));
102 if (lustre_dquot_cachep) {
104 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
105 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
106 lustre_dquot_cachep = NULL;
112 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
113 __attribute__((__const__));
116 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
118 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
119 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
123 /* caller must hold dquot_hash_lock */
124 static struct lustre_dquot *find_dquot(int hashent,
125 struct lustre_quota_info *lqi, qid_t id,
128 struct lustre_dquot *dquot;
131 cfs_list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
132 if (dquot->dq_info == lqi &&
133 dquot->dq_id == id && dquot->dq_type == type)
139 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
142 struct lustre_dquot *dquot = NULL;
145 OBD_SLAB_ALLOC_PTR_GFP(dquot, lustre_dquot_cachep, CFS_ALLOC_IO);
149 CFS_INIT_LIST_HEAD(&dquot->dq_hash);
150 cfs_mutex_init(&dquot->dq_mutex);
151 cfs_mutex_lock(&dquot->dq_mutex);
152 cfs_atomic_set(&dquot->dq_refcnt, 1);
153 dquot->dq_info = lqi;
155 dquot->dq_type = type;
160 static void free_dquot(struct lustre_dquot *dquot)
162 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
165 static void insert_dquot_nolock(struct lustre_dquot *dquot)
167 cfs_list_t *head = lustre_dquot_hash +
168 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
169 LASSERT(cfs_list_empty(&dquot->dq_hash));
170 cfs_list_add(&dquot->dq_hash, head);
173 static void remove_dquot_nolock(struct lustre_dquot *dquot)
175 LASSERT(!cfs_list_empty(&dquot->dq_hash));
176 cfs_list_del_init(&dquot->dq_hash);
179 static void lustre_dqput(struct lustre_dquot *dquot)
182 cfs_write_lock(&dquot_hash_lock);
183 LASSERT(cfs_atomic_read(&dquot->dq_refcnt));
184 cfs_atomic_dec(&dquot->dq_refcnt);
185 if (cfs_atomic_read(&dquot->dq_refcnt) == 0) {
186 remove_dquot_nolock(dquot);
189 cfs_write_unlock(&dquot_hash_lock);
193 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
194 struct lustre_quota_info *lqi,
195 qid_t id, int type, int can_fake)
197 unsigned int hashent = dquot_hashfn(lqi, id, type);
198 struct lustre_dquot *dquot, *empty;
202 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
203 RETURN(ERR_PTR(-ENOMEM));
205 cfs_read_lock(&dquot_hash_lock);
206 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
207 cfs_atomic_inc(&dquot->dq_refcnt);
208 cfs_read_unlock(&dquot_hash_lock);
209 cfs_mutex_unlock(&empty->dq_mutex);
214 cfs_read_unlock(&dquot_hash_lock);
217 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
218 cfs_mutex_unlock(&dquot->dq_mutex);
220 CERROR("can't read dquot from admin quotafile! "
225 cfs_write_lock(&dquot_hash_lock);
226 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
227 cfs_atomic_inc(&dquot->dq_refcnt);
231 insert_dquot_nolock(dquot);
233 cfs_write_unlock(&dquot_hash_lock);
239 if (!can_fake && cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
240 DQUOT_DEBUG(dquot, "It is a fake dquot: unexpected!\n");
242 dquot = ERR_PTR(-ENOENT);
252 static void init_oqaq(struct quota_adjust_qunit *oqaq,
253 struct lustre_quota_ctxt *qctxt,
256 struct lustre_qunit_size *lqs = NULL;
259 oqaq->qaq_flags = type;
260 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 0);
261 if (lqs && !IS_ERR(lqs)) {
262 cfs_spin_lock(&lqs->lqs_lock);
263 oqaq->qaq_bunit_sz = lqs->lqs_bunit_sz;
264 oqaq->qaq_iunit_sz = lqs->lqs_iunit_sz;
265 oqaq->qaq_flags = lqs->lqs_flags;
266 cfs_spin_unlock(&lqs->lqs_lock);
269 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
270 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
271 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
275 int dqacq_adjust_qunit_sz(struct obd_device *obd, qid_t id, int type,
278 struct mds_obd *mds = &obd->u.mds;
279 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
280 __u32 ost_num = mds->mds_lov_objid_count, mdt_num = 1;
281 struct quota_adjust_qunit *oqaq = NULL;
282 unsigned int qid[MAXQUOTAS] = { 0, 0 };
283 struct lustre_quota_info *info = &mds->mds_quota_info;
284 struct lustre_dquot *dquot = NULL;
290 cfs_down_read(&mds->mds_qonoff_sem);
291 dquot = lustre_dqget(obd, info, id, type, 0);
293 RETURN(PTR_ERR(dquot));
295 cfs_up_read(&mds->mds_qonoff_sem);
298 GOTO(out, rc = -ENOMEM);
300 cfs_mutex_lock(&dquot->dq_mutex);
301 init_oqaq(oqaq, qctxt, id, type);
303 rc = dquot_create_oqaq(qctxt, dquot, ost_num, mdt_num,
304 is_blk ? LQUOTA_FLAGS_ADJBLK :
305 LQUOTA_FLAGS_ADJINO, oqaq);
308 CERROR("create oqaq failed! (rc:%d)\n", rc);
311 QAQ_DEBUG(oqaq, "show oqaq.\n")
313 if (!QAQ_IS_ADJBLK(oqaq) && !QAQ_IS_ADJINO(oqaq))
316 /* adjust the mds slave qunit size */
317 adjust_res = quota_adjust_slave_lqs(oqaq, qctxt);
318 if (adjust_res <= 0) {
319 if (adjust_res < 0) {
321 CERROR("adjust mds slave's qunit size failed! "
324 CDEBUG(D_QUOTA, "qunit doesn't need to be adjusted.\n");
330 qid[GRPQUOTA] = dquot->dq_id;
332 qid[USRQUOTA] = dquot->dq_id;
334 cfs_mutex_unlock(&dquot->dq_mutex);
336 rc = qctxt_adjust_qunit(obd, qctxt, qid, is_blk, 0, NULL);
337 if (rc == -EDQUOT || rc == -EBUSY) {
338 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
342 CERROR("%s: mds fail to adjust file quota! (rc:%d)\n",
347 /* only when block qunit is reduced, boardcast to osts */
348 if ((adjust_res & LQS_BLK_DECREASE) && QAQ_IS_ADJBLK(oqaq))
349 rc = obd_quota_adjust_qunit(mds->mds_lov_exp, oqaq, qctxt, NULL);
358 cfs_mutex_unlock(&dquot->dq_mutex);
362 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
364 struct mds_obd *mds = &obd->u.mds;
365 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
366 struct lustre_quota_info *info = &mds->mds_quota_info;
367 struct lustre_dquot *dquot = NULL;
369 __u64 hlimit = 0, slimit = 0;
371 unsigned int grace = 0;
372 struct lustre_qunit_size *lqs = NULL;
376 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_DQACQ))
379 if (!ll_sb_has_quota_active(qctxt->lqc_sb,
380 QDATA_IS_GRP(qdata) ? GRPQUOTA : USRQUOTA))
383 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
392 cfs_spin_lock(&lqs->lqs_lock);
393 if (LQS_IS_RECOVERY(lqs)) {
394 cfs_spin_unlock(&lqs->lqs_lock);
395 LQS_DEBUG(lqs, "this lqs is under recovery\n");
396 GOTO(skip, rc = -EBUSY);
398 cfs_spin_unlock(&lqs->lqs_lock);
400 cfs_down_write(&mds->mds_qonoff_sem);
401 dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata), 0);
403 cfs_up_write(&mds->mds_qonoff_sem);
404 GOTO(skip, rc = PTR_ERR(dquot));
407 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
408 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
410 cfs_mutex_lock(&dquot->dq_mutex);
412 if (QDATA_IS_BLK(qdata)) {
413 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
414 usage = &dquot->dq_dqb.dqb_curspace;
415 hlimit = dquot->dq_dqb.dqb_bhardlimit;
416 slimit = dquot->dq_dqb.dqb_bsoftlimit;
417 time = &dquot->dq_dqb.dqb_btime;
419 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
420 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
421 hlimit = dquot->dq_dqb.dqb_ihardlimit;
422 slimit = dquot->dq_dqb.dqb_isoftlimit;
423 time = &dquot->dq_dqb.dqb_itime;
426 /* if the quota limit in admin quotafile is zero, we just inform
427 * slave to clear quota limit with zero qd_count */
428 if (hlimit == 0 && slimit == 0) {
436 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
438 if (QDATA_IS_CHANGE_QS(qdata) &&
439 QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
440 qdata->qd_count = (hlimit -
441 QUSG(*usage, QDATA_IS_BLK(qdata)))
442 * (QDATA_IS_BLK(qdata) ?
443 QUOTABLOCK_SIZE : 1);
445 GOTO(out, rc = -EDQUOT);
449 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
450 if (*time && cfs_time_current_sec() >= *time)
451 GOTO(out, rc = -EDQUOT);
453 *time = cfs_time_current_sec() + grace;
456 *usage += qdata->qd_count;
459 /* The usage in administrative file might be incorrect before
461 if (*usage < qdata->qd_count)
464 *usage -= qdata->qd_count;
466 /* (usage <= soft limit) but not (usage < soft limit) */
467 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
474 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
477 cfs_mutex_unlock(&dquot->dq_mutex);
478 cfs_up_write(&mds->mds_qonoff_sem);
481 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
482 QDATA_IS_BLK(qdata));
484 cfs_spin_lock(&lqs->lqs_lock);
485 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
487 cfs_spin_unlock(&lqs->lqs_lock);
489 if (QDATA_IS_BLK(qdata))
490 QDATA_SET_ADJBLK(qdata);
492 QDATA_SET_ADJINO(qdata);
494 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
501 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
502 const unsigned int qpids[], int rc, int opc)
504 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
508 if (rc && rc != -EDQUOT && rc != ENOLCK)
512 case FSFILT_OP_SETATTR:
513 /* release file quota on original owner */
514 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
515 /* release block quota on original owner */
516 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
517 /* acquire file quota on current owner */
518 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
519 /* acquire block quota on current owner */
520 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
522 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
523 /* release file quota on child */
524 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
525 /* rlease block quota on child */
526 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
528 case FSFILT_OP_CREATE_PARTIAL_CHILD:
529 /* acquire file quota on child */
530 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
531 /* acquire block quota on child */
532 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
535 /* acquire block quota on parent */
536 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
538 case FSFILT_OP_UNLINK:
539 /* release block quota on parent */
540 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
541 /* release file quota on child */
542 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
543 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
544 /* release block quota on child */
545 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
548 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
549 /* release block quota on parent */
550 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
552 case FSFILT_OP_CREATE:
553 /* acquire block quota on parent */
554 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
555 /* acquire file quota on child */
556 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
557 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
558 /* acquire block quota on child */
559 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
569 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
570 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
574 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
575 const unsigned int qpids[], int rc, int opc)
577 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
581 if (rc && rc != -EDQUOT)
585 case FSFILT_OP_SETATTR:
586 /* acquire/release block quota on original & current owner */
587 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
588 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
590 case FSFILT_OP_UNLINK:
591 /* release block quota on this owner */
592 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
593 /* acquire block quota on this owner */
594 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
605 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
606 rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
612 static const char prefix[] = "OBJECTS/";
614 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
616 struct mds_obd *mds = &obd->u.mds;
617 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
618 struct obd_device_target *obt = &obd->u.obt;
619 int rc = 0, i, rc1 = 0;
620 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
622 struct lvfs_run_ctxt saved;
625 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
627 if (oqctl->qc_type != USRQUOTA &&
628 oqctl->qc_type != GRPQUOTA &&
629 oqctl->qc_type != UGQUOTA)
632 cfs_down(&obt->obt_quotachecking);
633 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
634 cfs_down_write(&mds->mds_qonoff_sem);
636 for (i = 0; i < MAXQUOTAS; i++) {
639 if (!Q_TYPESET(oqctl, i))
642 /* quota file has been opened ? */
643 if (qinfo->qi_files[i]) {
644 CWARN("quota[%d] is on yet\n", i);
649 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
650 sprintf(name, "%s%s", prefix, quotafile[i]);
652 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
655 CERROR("%s: error invalidating admin quotafile %s (rc:%d)\n",
656 obd->obd_name, name, rc);
662 cfs_up_write(&mds->mds_qonoff_sem);
663 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
664 cfs_up(&obt->obt_quotachecking);
668 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
670 struct mds_obd *mds = &obd->u.mds;
671 struct obd_device_target *obt = &obd->u.obt;
673 struct lvfs_run_ctxt saved;
676 if (oqctl->qc_type != USRQUOTA &&
677 oqctl->qc_type != GRPQUOTA &&
678 oqctl->qc_type != UGQUOTA)
681 cfs_down(&obt->obt_quotachecking);
682 if (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
683 GOTO(out, rc = -EBUSY);
684 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
685 cfs_down_write(&mds->mds_qonoff_sem);
687 oqctl->qc_cmd = Q_FINVALIDATE;
688 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
690 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
692 cfs_up_write(&mds->mds_qonoff_sem);
693 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
695 cfs_up(&obt->obt_quotachecking);
699 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
701 struct mds_obd *mds = &obd->u.mds;
702 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
703 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
704 struct lvfs_run_ctxt saved;
709 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
711 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
712 cfs_down_write(&mds->mds_qonoff_sem);
714 for (i = 0; i < MAXQUOTAS && !rc; i++) {
717 if (!Q_TYPESET(oqctl, i))
720 /* quota file has been opened ? */
721 if (qinfo->qi_files[i]) {
722 CWARN("init %s admin quotafile while quota on.\n",
723 i == USRQUOTA ? "user" : "group");
727 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
728 sprintf(name, "%s%s", prefix, quotafile[i]);
730 /* check if quota file exists and is correct */
731 fp = filp_open(name, O_RDONLY, 0);
733 /* irregular file is not the right place for quota */
734 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
735 CERROR("admin quota file %s is not "
741 qinfo->qi_files[i] = fp;
742 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
743 qinfo->qi_files[i] = NULL;
752 /* -EINVAL may be returned by quotainfo for bad quota file */
753 if (rc != -ENOENT && rc != -EINVAL) {
754 CERROR("%s: error opening old quota file %s (%d)\n",
755 obd->obd_name, name, rc);
759 CDEBUG(D_INFO, "%s new quota file %s\n", name,
760 rc == -ENOENT ? "creating" : "overwriting");
762 /* create quota file overwriting old if needed */
763 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
766 CERROR("%s: error creating admin quotafile %s (rc:%d)\n",
767 obd->obd_name, name, rc);
771 qinfo->qi_files[i] = fp;
773 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
775 CERROR("error init %s admin quotafile! (rc:%d)\n",
776 i == USRQUOTA ? "user" : "group", rc);
779 qinfo->qi_files[i] = NULL;
782 cfs_up_write(&mds->mds_qonoff_sem);
783 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
787 static int close_quota_files(struct obd_quotactl *oqctl,
788 struct lustre_quota_info *qinfo)
793 for (i = 0; i < MAXQUOTAS; i++) {
794 if (!Q_TYPESET(oqctl, i))
796 if (qinfo->qi_files[i] == NULL) {
797 CDEBUG(D_QUOTA, "quota[%d] is off already\n", i);
801 filp_close(qinfo->qi_files[i], 0);
802 qinfo->qi_files[i] = NULL;
807 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
809 struct mds_obd *mds = &obd->u.mds;
810 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
811 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
813 int i, rc = 0, rc1 = 0;
816 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
818 /* open admin quota files and read quotafile info */
819 for (i = 0; i < MAXQUOTAS; i++) {
822 if (!Q_TYPESET(oqctl, i) || qinfo->qi_files[i] != NULL)
825 LASSERT(strlen(quotafile[i])
826 + sizeof(prefix) <= sizeof(name));
827 sprintf(name, "%s%s", prefix, quotafile[i]);
828 fp = filp_open(name, O_RDWR, 0);
829 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
830 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
831 CERROR("error open/create %s! (rc:%d)\n", name, rc);
834 qinfo->qi_files[i] = fp;
836 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
838 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
842 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
844 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
850 if (rc && rc1 != -EALREADY)
851 close_quota_files(oqctl, qinfo);
856 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
861 if (oqctl->qc_type != USRQUOTA &&
862 oqctl->qc_type != GRPQUOTA &&
863 oqctl->qc_type != UGQUOTA)
866 rc = generic_quota_on(obd, oqctl, 1);
872 int mds_admin_quota_off(struct obd_device *obd,
873 struct obd_quotactl *oqctl)
875 struct mds_obd *mds = &obd->u.mds;
876 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
880 /* close admin quota files */
881 rc = close_quota_files(oqctl, qinfo);
886 /* with obt->obt_quotachecking held */
887 int do_mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
889 struct mds_obd *mds = &obd->u.mds;
890 struct obd_device_target *obt = &obd->u.obt;
891 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
892 struct lvfs_run_ctxt saved;
893 int rc = 0, rc1 = 0, rc2 = 0;
896 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
898 if (oqctl->qc_type != USRQUOTA &&
899 oqctl->qc_type != GRPQUOTA &&
900 oqctl->qc_type != UGQUOTA)
903 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
904 cfs_down_write(&mds->mds_qonoff_sem);
905 /* close admin quota files */
906 rc2 = mds_admin_quota_off(obd, oqctl);
907 if (rc2 && rc2 != -EALREADY) {
908 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
912 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
914 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
915 } else if (quota_is_off(qctxt, oqctl)) {
916 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
919 if (rc2 != -EALREADY) {
920 CWARN("mds local quota[%d] is failed to be off for %d\n",
921 oqctl->qc_type, rc1);
922 oqctl->qc_cmd = Q_QUOTAON;
923 mds_admin_quota_on(obd, oqctl);
924 oqctl->qc_cmd = Q_QUOTAOFF;
929 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
930 if (rc && rc != -EALREADY) {
931 CWARN("mds remote quota[%d] is failed to be off for %d\n",
933 oqctl->qc_cmd = Q_QUOTAON;
934 if (rc2 != -EALREADY)
935 mds_admin_quota_on(obd, oqctl);
936 if (rc1 != -EALREADY) {
937 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
938 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
940 oqctl->qc_cmd = Q_QUOTAOFF;
945 CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc %u:%lu:%d\n",
946 obd->obd_name, oqctl->qc_type, qctxt->lqc_flags, rc);
947 cfs_up_write(&mds->mds_qonoff_sem);
948 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
949 return rc ? : (rc1 ? : rc2);
952 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
954 struct obd_device_target *obt = &obd->u.obt;
958 cfs_down(&obt->obt_quotachecking);
959 rc = do_mds_quota_off(obd, oqctl);
960 cfs_up(&obt->obt_quotachecking);
964 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
966 struct mds_obd *mds = &obd->u.mds;
967 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
968 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
972 if (oqctl->qc_type != USRQUOTA &&
973 oqctl->qc_type != GRPQUOTA)
976 cfs_down_write(&mds->mds_qonoff_sem);
977 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
978 CWARN("quota[%u] is off\n", oqctl->qc_type);
979 GOTO(out, rc = -ESRCH);
982 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
983 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
984 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
986 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
990 cfs_up_write(&mds->mds_qonoff_sem);
994 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
996 struct mds_obd *mds = &obd->u.mds;
997 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
998 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
1002 if (oqctl->qc_type != USRQUOTA &&
1003 oqctl->qc_type != GRPQUOTA)
1006 cfs_down_read(&mds->mds_qonoff_sem);
1007 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1008 CWARN("quota[%u] is off\n", oqctl->qc_type);
1009 GOTO(out, rc = -ESRCH);
1012 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
1013 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
1014 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
1018 cfs_up_read(&mds->mds_qonoff_sem);
1022 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
1023 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
1024 int type, struct quota_adjust_qunit *oqaq)
1026 __u64 bunit_curr_o, iunit_curr_o;
1027 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
1028 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
1029 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
1030 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
1031 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1032 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1036 if (!dquot || !oqaq)
1038 LASSERT_MUTEX_LOCKED(&dquot->dq_mutex);
1039 LASSERT(oqaq->qaq_iunit_sz);
1040 LASSERT(oqaq->qaq_bunit_sz);
1042 /* don't change qunit size */
1043 if (!qctxt->lqc_switch_qs)
1046 bunit_curr_o = oqaq->qaq_bunit_sz;
1047 iunit_curr_o = oqaq->qaq_iunit_sz;
1049 if (dquot->dq_type == GRPQUOTA)
1052 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1053 __u64 b_limitation =
1054 oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1055 /* enlarge block qunit size */
1057 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1058 oqaq->qaq_bunit_sz =
1059 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1061 if (oqaq->qaq_bunit_sz >= qctxt->lqc_bunit_sz)
1063 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1067 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1068 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1070 /* shrink block qunit size */
1072 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1073 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1074 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1076 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1078 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1082 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1083 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1085 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1086 QAQ_SET_ADJBLK(oqaq);
1090 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1091 __u64 i_limitation =
1092 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1093 /* enlarge file qunit size */
1094 while (ilimit > dquot->dq_dqb.dqb_curinodes
1095 + 2 * i_limitation) {
1096 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1097 if (oqaq->qaq_iunit_sz >= qctxt->lqc_iunit_sz)
1099 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1103 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1104 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1106 /* shrink file qunit size */
1107 while (ilimit < dquot->dq_dqb.dqb_curinodes
1109 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1110 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1112 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1116 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1117 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1119 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1120 QAQ_SET_ADJINO(oqaq);
1124 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1129 static int mds_init_slave_ilimits(struct obd_device *obd,
1130 struct obd_quotactl *oqctl, int set)
1132 /* XXX: for file limits only adjust local now */
1133 struct obd_device_target *obt = &obd->u.obt;
1134 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1135 unsigned int id[MAXQUOTAS] = { 0, 0 };
1136 struct obd_quotactl *ioqc = NULL;
1137 struct lustre_qunit_size *lqs;
1142 /* if we are going to set zero limit, needn't init slaves */
1143 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1147 OBD_ALLOC_PTR(ioqc);
1151 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1152 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1153 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1154 ioqc->qc_id = oqctl->qc_id;
1155 ioqc->qc_type = oqctl->qc_type;
1156 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1157 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1159 /* build lqs for mds */
1160 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1161 qctxt, flag ? 1 : 0);
1162 if (lqs && !IS_ERR(lqs)) {
1164 lqs->lqs_flags |= QI_SET;
1166 lqs->lqs_flags &= ~QI_SET;
1169 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1170 flag ? "create" : "search",
1171 oqctl->qc_type ? "group" : "user",
1173 GOTO(out, rc = PTR_ERR(lqs));
1176 /* set local limit to MIN_QLIMIT */
1177 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1181 /* trigger local qunit pre-acquire */
1182 if (oqctl->qc_type == USRQUOTA)
1183 id[USRQUOTA] = oqctl->qc_id;
1185 id[GRPQUOTA] = oqctl->qc_id;
1187 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1188 if (rc == -EDQUOT || rc == -EBUSY) {
1189 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1193 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1197 /* FIXME initialize all slaves in CMD */
1205 static int mds_init_slave_blimits(struct obd_device *obd,
1206 struct obd_quotactl *oqctl, int set)
1208 struct obd_device_target *obt = &obd->u.obt;
1209 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1210 struct mds_obd *mds = &obd->u.mds;
1211 struct obd_quotactl *ioqc;
1212 struct lustre_qunit_size *lqs;
1213 unsigned int id[MAXQUOTAS] = { 0, 0 };
1218 /* if we are going to set zero limit, needn't init slaves */
1219 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1223 OBD_ALLOC_PTR(ioqc);
1227 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1228 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1229 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1230 ioqc->qc_id = oqctl->qc_id;
1231 ioqc->qc_type = oqctl->qc_type;
1232 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1233 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1235 /* build lqs for mds */
1236 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1237 qctxt, flag ? 1 : 0);
1238 if (lqs && !IS_ERR(lqs)) {
1240 lqs->lqs_flags |= QB_SET;
1242 lqs->lqs_flags &= ~QB_SET;
1245 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1246 flag ? "create" : "search",
1247 oqctl->qc_type ? "group" : "user",
1249 GOTO(out, rc = PTR_ERR(lqs));
1252 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1256 /* trigger local qunit pre-acquire */
1257 if (oqctl->qc_type == USRQUOTA)
1258 id[USRQUOTA] = oqctl->qc_id;
1260 id[GRPQUOTA] = oqctl->qc_id;
1262 /* initialize all slave's limit */
1263 rc = obd_quotactl(mds->mds_lov_exp, ioqc);
1265 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1266 if (rc == -EDQUOT || rc == -EBUSY) {
1267 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1271 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1281 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1283 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1286 QAQ_SET_CREATE_LQS(qaq);
1287 /* adjust local lqs */
1288 rc = quota_adjust_slave_lqs(qaq, qctxt);
1290 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1292 /* adjust remote lqs */
1293 if (QAQ_IS_ADJBLK(qaq)) {
1294 rc = obd_quota_adjust_qunit(obd->u.mds.mds_lov_exp, qaq, qctxt, NULL);
1296 CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1301 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1303 struct mds_obd *mds = &obd->u.mds;
1304 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1305 struct obd_device *lov_obd = class_exp2obd(mds->mds_lov_exp);
1306 struct lov_obd *lov = &lov_obd->u.lov;
1307 struct quota_adjust_qunit *oqaq = NULL;
1308 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1309 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1310 time_t btime, itime;
1311 struct lustre_dquot *dquot;
1312 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1313 /* orig_set means if quota was set before; now_set means we are
1314 * setting/cancelling quota */
1315 int orig_set, now_set;
1316 struct lustre_qunit_size *lqs;
1317 int rc = 0, rc2 = 0, flag = 0;
1320 if (oqctl->qc_type != USRQUOTA &&
1321 oqctl->qc_type != GRPQUOTA)
1324 OBD_ALLOC_PTR(oqaq);
1328 cfs_down_write(&mds->mds_qonoff_sem);
1329 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1331 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1332 CWARN("quota[%u] is off\n", oqctl->qc_type);
1333 GOTO(out_sem, rc = -ESRCH);
1336 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1338 GOTO(out_sem, rc = PTR_ERR(dquot));
1339 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1340 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1342 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id), qctxt, 1);
1350 cfs_mutex_lock(&dquot->dq_mutex);
1351 cfs_spin_lock(&lqs->lqs_lock);
1352 if (LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1353 cfs_spin_unlock(&lqs->lqs_lock);
1354 cfs_mutex_unlock(&dquot->dq_mutex);
1355 GOTO(skip, rc = -EBUSY);
1357 LQS_SET_SETQUOTA(lqs);
1358 cfs_spin_unlock(&lqs->lqs_lock);
1360 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1361 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1362 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1363 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1364 btime = dquot->dq_dqb.dqb_btime;
1365 itime = dquot->dq_dqb.dqb_itime;
1367 if (dqblk->dqb_valid & QIF_BTIME)
1368 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1369 if (dqblk->dqb_valid & QIF_ITIME)
1370 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1372 if (dqblk->dqb_valid & QIF_BLIMITS) {
1373 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1374 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1375 /* clear usage (limit pool) */
1376 if (!dquot->dq_dqb.dqb_bhardlimit &&
1377 !dquot->dq_dqb.dqb_bsoftlimit)
1378 dquot->dq_dqb.dqb_curspace = 0;
1380 /* clear grace time */
1381 if (!dqblk->dqb_bsoftlimit ||
1382 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1383 dquot->dq_dqb.dqb_btime = 0;
1384 /* set grace only if user hasn't provided his own */
1385 else if (!(dqblk->dqb_valid & QIF_BTIME))
1386 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1387 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1389 flag |= LQUOTA_FLAGS_ADJBLK;
1392 if (dqblk->dqb_valid & QIF_ILIMITS) {
1393 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1394 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1395 /* clear usage (limit pool) */
1396 if (!dquot->dq_dqb.dqb_ihardlimit &&
1397 !dquot->dq_dqb.dqb_isoftlimit)
1398 dquot->dq_dqb.dqb_curinodes = 0;
1400 if (!dqblk->dqb_isoftlimit ||
1401 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1402 dquot->dq_dqb.dqb_itime = 0;
1403 else if (!(dqblk->dqb_valid & QIF_ITIME))
1404 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1405 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1407 flag |= LQUOTA_FLAGS_ADJINO;
1409 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1410 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1412 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1414 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1417 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1419 cfs_mutex_unlock(&dquot->dq_mutex);
1422 CERROR("set limit failed! (rc:%d)\n", rc);
1423 GOTO(update_fail, rc);
1426 cfs_up_write(&mds->mds_qonoff_sem);
1427 adjust_lqs(obd, oqaq);
1429 orig_set = ihardlimit || isoftlimit;
1430 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1431 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1432 cfs_mutex_lock(&dquot->dq_mutex);
1433 dquot->dq_dqb.dqb_curinodes = 0;
1434 cfs_mutex_unlock(&dquot->dq_mutex);
1435 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1437 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1442 orig_set = bhardlimit || bsoftlimit;
1443 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1444 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1445 cfs_mutex_lock(&dquot->dq_mutex);
1446 dquot->dq_dqb.dqb_curspace = 0;
1447 cfs_mutex_unlock(&dquot->dq_mutex);
1448 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1450 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1456 cfs_down_write(&mds->mds_qonoff_sem);
1457 cfs_mutex_lock(&dquot->dq_mutex);
1459 /* cancel previous setting */
1460 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1461 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1462 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1463 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1464 dquot->dq_dqb.dqb_btime = btime;
1465 dquot->dq_dqb.dqb_itime = itime;
1467 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1468 cfs_mutex_unlock(&dquot->dq_mutex);
1470 cfs_spin_lock(&lqs->lqs_lock);
1471 LQS_CLEAR_SETQUOTA(lqs);
1472 cfs_spin_unlock(&lqs->lqs_lock);
1476 lustre_dqput(dquot);
1479 cfs_up_write(&mds->mds_qonoff_sem);
1484 return rc ? rc : rc2;
1487 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1489 struct obd_quotactl *soqc;
1490 struct lvfs_run_ctxt saved;
1494 OBD_ALLOC_PTR(soqc);
1498 soqc->qc_cmd = Q_GETOQUOTA;
1499 soqc->qc_id = oqctl->qc_id;
1500 soqc->qc_type = oqctl->qc_type;
1502 /* get block usage from OSS */
1503 soqc->qc_dqblk.dqb_curspace = 0;
1504 rc = obd_quotactl(obd->u.mds.mds_lov_exp, soqc);
1505 if (!rc || rc == -EREMOTEIO) {
1506 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1507 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1510 /* get block/inode usage from MDS */
1511 soqc->qc_dqblk.dqb_curspace = 0;
1512 soqc->qc_dqblk.dqb_curinodes = 0;
1513 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1514 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1515 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1517 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1518 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1519 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1527 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1529 struct mds_obd *mds = &obd->u.mds;
1530 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1531 struct lustre_dquot *dquot;
1532 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1536 if (oqctl->qc_type != USRQUOTA &&
1537 oqctl->qc_type != GRPQUOTA)
1540 cfs_down_read(&mds->mds_qonoff_sem);
1541 dqblk->dqb_valid = 0;
1542 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1543 CWARN("quota[%u] is off\n", oqctl->qc_type);
1544 GOTO(out, rc = -ESRCH);
1547 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1549 GOTO(out, rc = PTR_ERR(dquot));
1551 cfs_mutex_lock(&dquot->dq_mutex);
1552 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1553 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1554 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1555 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1556 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1557 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1558 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1559 cfs_mutex_unlock(&dquot->dq_mutex);
1561 lustre_dqput(dquot);
1562 cfs_up_read(&mds->mds_qonoff_sem);
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);
1572 cfs_up_read(&mds->mds_qonoff_sem);
1576 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1578 struct lvfs_run_ctxt saved;
1582 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1583 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1584 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1590 /* FIXME we only recovery block limit by now, need recovery inode
1591 * limits also after CMD involved in */
1593 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1595 struct mds_obd *mds = &obd->u.mds;
1596 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1597 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1598 struct lustre_qunit_size *lqs;
1599 struct lustre_dquot *dquot;
1600 struct obd_quotactl *qctl;
1601 __u64 total_limits = 0;
1605 OBD_ALLOC_PTR(qctl);
1609 dquot = lustre_dqget(obd, qinfo, id, type, 0);
1610 if (IS_ERR(dquot)) {
1611 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1613 RETURN(PTR_ERR(dquot));
1616 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 1);
1624 cfs_mutex_lock(&dquot->dq_mutex);
1626 /* don't recover the dquot without limits or quota is setting or
1627 * another recovery is already going on */
1628 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1629 LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1630 cfs_mutex_unlock(&dquot->dq_mutex);
1631 GOTO(skip1, rc = 0);
1634 cfs_spin_lock(&lqs->lqs_lock);
1635 LQS_SET_RECOVERY(lqs);
1636 cfs_spin_unlock(&lqs->lqs_lock);
1637 cfs_mutex_unlock(&dquot->dq_mutex);
1639 /* release mds_qonoff_sem during obd_quotactl ops here */
1640 cfs_up_write(&mds->mds_qonoff_sem);
1642 /* get real bhardlimit from all slaves. */
1643 qctl->qc_cmd = Q_GETOQUOTA;
1644 qctl->qc_type = type;
1646 qctl->qc_stat = QUOTA_RECOVERING;
1647 rc = obd_quotactl(mds->mds_lov_exp, qctl);
1648 cfs_down_write(&mds->mds_qonoff_sem);
1651 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1653 /* get real bhardlimit from master */
1654 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1657 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1659 /* amend the usage of the administrative quotafile */
1660 cfs_mutex_lock(&dquot->dq_mutex);
1662 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1664 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1666 CERROR("write dquot failed! (rc:%d)\n", rc);
1668 cfs_mutex_unlock(&dquot->dq_mutex);
1671 cfs_spin_lock(&lqs->lqs_lock);
1672 LQS_CLEAR_RECOVERY(lqs);
1673 cfs_spin_unlock(&lqs->lqs_lock);
1677 lustre_dqput(dquot);
1682 struct qmaster_recov_thread_data {
1683 struct obd_device *obd;
1684 cfs_completion_t comp;
1687 static int qmaster_recovery_main(void *arg)
1689 struct qmaster_recov_thread_data *data = arg;
1690 struct obd_device *obd = data->obd;
1691 struct mds_obd *mds = &obd->u.mds;
1692 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1694 unsigned short type;
1697 cfs_daemonize_ctxt("qmaster_recovd");
1700 class_incref(obd, "qmaster_recovd_mds", obd);
1702 class_incref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1704 cfs_complete(&data->comp);
1706 cfs_down_write(&mds->mds_qonoff_sem);
1707 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1709 struct dquot_id *dqid, *tmp;
1711 if (qinfo->qi_files[type] == NULL)
1714 CFS_INIT_LIST_HEAD(&id_list);
1715 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1718 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1720 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1721 cfs_list_del_init(&dqid->di_link);
1725 rc = dquot_recovery(obd, dqid->di_id, type);
1727 CERROR("%s: qmaster recovery failed for %sid %d"
1728 " rc:%d)\n", obd->obd_name,
1729 type ? "g" : "u", dqid->di_id, rc);
1734 cfs_up_write(&mds->mds_qonoff_sem);
1735 class_decref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1736 class_decref(obd, "qmaster_recovd_mds", obd);
1740 int mds_quota_recovery(struct obd_device *obd)
1742 struct mds_obd *mds = &obd->u.mds;
1743 struct qmaster_recov_thread_data data;
1747 if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
1750 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1753 cfs_mutex_lock(&obd->obd_dev_mutex);
1754 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1755 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1756 mds->mds_lov_desc.ld_active_tgt_count,
1757 mds->mds_lov_objid_count);
1758 cfs_mutex_unlock(&obd->obd_dev_mutex);
1761 cfs_mutex_unlock(&obd->obd_dev_mutex);
1764 cfs_init_completion(&data.comp);
1766 rc = cfs_create_thread(qmaster_recovery_main, &data,
1769 CERROR("%s: cannot start quota recovery thread: rc %d\n",
1772 cfs_wait_for_completion(&data.comp);
1776 #endif /* HAVE_QUOTA_SUPPORT */