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.
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 > lqs->lqs_lock */
68 static cfs_list_t lustre_dquot_hash[NR_DQHASH];
69 static cfs_rwlock_t dquot_hash_lock = CFS_RW_LOCK_UNLOCKED;
71 cfs_mem_cache_t *lustre_dquot_cachep;
73 int lustre_dquot_init(void)
78 LASSERT(lustre_dquot_cachep == NULL);
79 lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
80 sizeof(struct lustre_dquot),
82 if (!lustre_dquot_cachep)
85 for (i = 0; i < NR_DQHASH; i++) {
86 CFS_INIT_LIST_HEAD(lustre_dquot_hash + i);
91 void lustre_dquot_exit(void)
95 /* FIXME cleanup work ?? */
97 for (i = 0; i < NR_DQHASH; i++) {
98 LASSERT(cfs_list_empty(lustre_dquot_hash + i));
100 if (lustre_dquot_cachep) {
102 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
103 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
104 lustre_dquot_cachep = NULL;
110 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
111 __attribute__((__const__));
114 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
116 unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
117 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
121 /* caller must hold dquot_hash_lock */
122 static struct lustre_dquot *find_dquot(int hashent,
123 struct lustre_quota_info *lqi, qid_t id,
126 struct lustre_dquot *dquot;
129 cfs_list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
130 if (dquot->dq_info == lqi &&
131 dquot->dq_id == id && dquot->dq_type == type)
137 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
140 struct lustre_dquot *dquot = NULL;
143 OBD_SLAB_ALLOC_PTR_GFP(dquot, lustre_dquot_cachep, CFS_ALLOC_IO);
147 CFS_INIT_LIST_HEAD(&dquot->dq_hash);
148 cfs_init_mutex_locked(&dquot->dq_sem);
149 cfs_atomic_set(&dquot->dq_refcnt, 1);
150 dquot->dq_info = lqi;
152 dquot->dq_type = type;
157 static void free_dquot(struct lustre_dquot *dquot)
159 OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
162 static void insert_dquot_nolock(struct lustre_dquot *dquot)
164 cfs_list_t *head = lustre_dquot_hash +
165 dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
166 LASSERT(cfs_list_empty(&dquot->dq_hash));
167 cfs_list_add(&dquot->dq_hash, head);
170 static void remove_dquot_nolock(struct lustre_dquot *dquot)
172 LASSERT(!cfs_list_empty(&dquot->dq_hash));
173 cfs_list_del_init(&dquot->dq_hash);
176 static void lustre_dqput(struct lustre_dquot *dquot)
179 cfs_write_lock(&dquot_hash_lock);
180 LASSERT(cfs_atomic_read(&dquot->dq_refcnt));
181 cfs_atomic_dec(&dquot->dq_refcnt);
182 if (cfs_atomic_read(&dquot->dq_refcnt) == 0) {
183 remove_dquot_nolock(dquot);
186 cfs_write_unlock(&dquot_hash_lock);
190 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
191 struct lustre_quota_info *lqi,
192 qid_t id, int type, int can_fake)
194 unsigned int hashent = dquot_hashfn(lqi, id, type);
195 struct lustre_dquot *dquot, *empty;
199 if ((empty = alloc_dquot(lqi, id, type)) == NULL)
200 RETURN(ERR_PTR(-ENOMEM));
202 cfs_read_lock(&dquot_hash_lock);
203 if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
204 cfs_atomic_inc(&dquot->dq_refcnt);
205 cfs_read_unlock(&dquot_hash_lock);
210 cfs_read_unlock(&dquot_hash_lock);
213 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
214 cfs_up(&dquot->dq_sem);
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_down(&dquot->dq_sem);
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_up(&dquot->dq_sem);
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_up(&dquot->dq_sem);
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 lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
384 cfs_spin_lock(&lqs->lqs_lock);
385 if (LQS_IS_RECOVERY(lqs)) {
386 cfs_spin_unlock(&lqs->lqs_lock);
387 LQS_DEBUG(lqs, "this lqs is under recovery\n");
388 GOTO(skip, rc = -EBUSY);
390 cfs_spin_unlock(&lqs->lqs_lock);
392 cfs_down_write(&mds->mds_qonoff_sem);
393 dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata), 0);
395 cfs_up_write(&mds->mds_qonoff_sem);
396 GOTO(skip, rc = PTR_ERR(dquot));
399 DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
400 QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
402 cfs_down(&dquot->dq_sem);
404 if (QDATA_IS_BLK(qdata)) {
405 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
406 usage = &dquot->dq_dqb.dqb_curspace;
407 hlimit = dquot->dq_dqb.dqb_bhardlimit;
408 slimit = dquot->dq_dqb.dqb_bsoftlimit;
409 time = &dquot->dq_dqb.dqb_btime;
411 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
412 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
413 hlimit = dquot->dq_dqb.dqb_ihardlimit;
414 slimit = dquot->dq_dqb.dqb_isoftlimit;
415 time = &dquot->dq_dqb.dqb_itime;
418 /* if the quota limit in admin quotafile is zero, we just inform
419 * slave to clear quota limit with zero qd_count */
420 if (hlimit == 0 && slimit == 0) {
428 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
430 if (QDATA_IS_CHANGE_QS(qdata) &&
431 QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
432 qdata->qd_count = (hlimit -
433 QUSG(*usage, QDATA_IS_BLK(qdata)))
434 * (QDATA_IS_BLK(qdata) ?
435 QUOTABLOCK_SIZE : 1);
437 GOTO(out, rc = -EDQUOT);
441 QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
442 if (*time && cfs_time_current_sec() >= *time)
443 GOTO(out, rc = -EDQUOT);
445 *time = cfs_time_current_sec() + grace;
448 *usage += qdata->qd_count;
451 /* The usage in administrative file might be incorrect before
453 if (*usage - qdata->qd_count < 0)
456 *usage -= qdata->qd_count;
458 /* (usage <= soft limit) but not (usage < soft limit) */
459 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
466 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
469 cfs_up(&dquot->dq_sem);
470 cfs_up_write(&mds->mds_qonoff_sem);
473 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
474 QDATA_IS_BLK(qdata));
476 cfs_spin_lock(&lqs->lqs_lock);
477 qdata->qd_qunit = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
479 cfs_spin_unlock(&lqs->lqs_lock);
481 if (QDATA_IS_BLK(qdata))
482 QDATA_SET_ADJBLK(qdata);
484 QDATA_SET_ADJINO(qdata);
486 QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
493 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
494 const unsigned int qpids[], int rc, int opc)
496 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
500 if (rc && rc != -EDQUOT && rc != ENOLCK)
504 case FSFILT_OP_SETATTR:
505 /* release file quota on original owner */
506 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
507 /* release block quota on original owner */
508 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
509 /* acquire file quota on current owner */
510 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
511 /* acquire block quota on current owner */
512 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
514 case FSFILT_OP_UNLINK_PARTIAL_CHILD:
515 /* release file quota on child */
516 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
517 /* rlease block quota on child */
518 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
520 case FSFILT_OP_CREATE_PARTIAL_CHILD:
521 /* acquire file quota on child */
522 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
523 /* acquire block quota on child */
524 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
527 /* acquire block quota on parent */
528 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
530 case FSFILT_OP_UNLINK:
531 /* release block quota on parent */
532 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
533 /* release file quota on child */
534 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
535 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
536 /* release block quota on child */
537 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
540 case FSFILT_OP_UNLINK_PARTIAL_PARENT:
541 /* release block quota on parent */
542 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
544 case FSFILT_OP_CREATE:
545 /* acquire block quota on parent */
546 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
547 /* acquire file quota on child */
548 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
549 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
550 /* acquire block quota on child */
551 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
561 "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
562 rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
566 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
567 const unsigned int qpids[], int rc, int opc)
569 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
573 if (rc && rc != -EDQUOT)
577 case FSFILT_OP_SETATTR:
578 /* acquire/release block quota on original & current owner */
579 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
580 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
582 case FSFILT_OP_UNLINK:
583 /* release block quota on this owner */
584 case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
585 /* acquire block quota on this owner */
586 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
597 "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
598 rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
604 static const char prefix[] = "OBJECTS/";
606 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
608 struct mds_obd *mds = &obd->u.mds;
609 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
610 struct obd_device_target *obt = &obd->u.obt;
611 int rc = 0, i, rc1 = 0;
612 char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
614 struct lvfs_run_ctxt saved;
617 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
619 if (oqctl->qc_type != USRQUOTA &&
620 oqctl->qc_type != GRPQUOTA &&
621 oqctl->qc_type != UGQUOTA)
624 cfs_down(&obt->obt_quotachecking);
625 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
626 cfs_down_write(&mds->mds_qonoff_sem);
628 for (i = 0; i < MAXQUOTAS; i++) {
631 if (!Q_TYPESET(oqctl, i))
634 /* quota file has been opened ? */
635 if (qinfo->qi_files[i]) {
636 CWARN("quota[%d] is on yet\n", i);
641 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
642 sprintf(name, "%s%s", prefix, quotafile[i]);
644 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
647 CERROR("%s: error invalidating admin quotafile %s (rc:%d)\n",
648 obd->obd_name, name, rc);
654 cfs_up_write(&mds->mds_qonoff_sem);
655 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
656 cfs_up(&obt->obt_quotachecking);
660 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
662 struct mds_obd *mds = &obd->u.mds;
663 struct obd_device_target *obt = &obd->u.obt;
665 struct lvfs_run_ctxt saved;
668 if (oqctl->qc_type != USRQUOTA &&
669 oqctl->qc_type != GRPQUOTA &&
670 oqctl->qc_type != UGQUOTA)
673 cfs_down(&obt->obt_quotachecking);
674 if (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
675 GOTO(out, rc = -EBUSY);
676 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
677 cfs_down_write(&mds->mds_qonoff_sem);
679 oqctl->qc_cmd = Q_FINVALIDATE;
680 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
682 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
684 cfs_up_write(&mds->mds_qonoff_sem);
685 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
687 cfs_up(&obt->obt_quotachecking);
691 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
693 struct mds_obd *mds = &obd->u.mds;
694 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
695 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
696 struct lvfs_run_ctxt saved;
701 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
703 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
704 cfs_down_write(&mds->mds_qonoff_sem);
706 for (i = 0; i < MAXQUOTAS && !rc; i++) {
709 if (!Q_TYPESET(oqctl, i))
712 /* quota file has been opened ? */
713 if (qinfo->qi_files[i]) {
714 CWARN("init %s admin quotafile while quota on.\n",
715 i == USRQUOTA ? "user" : "group");
719 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
720 sprintf(name, "%s%s", prefix, quotafile[i]);
722 /* check if quota file exists and is correct */
723 fp = filp_open(name, O_RDONLY, 0);
725 /* irregular file is not the right place for quota */
726 if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
727 CERROR("admin quota file %s is not "
733 qinfo->qi_files[i] = fp;
734 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
735 qinfo->qi_files[i] = NULL;
744 /* -EINVAL may be returned by quotainfo for bad quota file */
745 if (rc != -ENOENT && rc != -EINVAL) {
746 CERROR("%s: error opening old quota file %s (%d)\n",
747 obd->obd_name, name, rc);
751 CDEBUG(D_INFO, "%s new quota file %s\n", name,
752 rc == -ENOENT ? "creating" : "overwriting");
754 /* create quota file overwriting old if needed */
755 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
758 CERROR("%s: error creating admin quotafile %s (rc:%d)\n",
759 obd->obd_name, name, rc);
763 qinfo->qi_files[i] = fp;
765 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
767 CERROR("error init %s admin quotafile! (rc:%d)\n",
768 i == USRQUOTA ? "user" : "group", rc);
771 qinfo->qi_files[i] = NULL;
774 cfs_up_write(&mds->mds_qonoff_sem);
775 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
779 static int close_quota_files(struct obd_quotactl *oqctl,
780 struct lustre_quota_info *qinfo)
785 for (i = 0; i < MAXQUOTAS; i++) {
786 if (!Q_TYPESET(oqctl, i))
788 if (qinfo->qi_files[i] == NULL) {
789 CWARN("quota[%d] is off already\n", i);
793 filp_close(qinfo->qi_files[i], 0);
794 qinfo->qi_files[i] = NULL;
799 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
801 struct mds_obd *mds = &obd->u.mds;
802 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
803 const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
805 int i, rc = 0, rc1 = 0;
808 LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
810 /* open admin quota files and read quotafile info */
811 for (i = 0; i < MAXQUOTAS; i++) {
814 if (!Q_TYPESET(oqctl, i) || qinfo->qi_files[i] != NULL)
817 LASSERT(strlen(quotafile[i])
818 + sizeof(prefix) <= sizeof(name));
819 sprintf(name, "%s%s", prefix, quotafile[i]);
820 fp = filp_open(name, O_RDWR, 0);
821 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
822 rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
823 CERROR("error open/create %s! (rc:%d)\n", name, rc);
826 qinfo->qi_files[i] = fp;
828 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
830 CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
834 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
836 CERROR("error read quotainfo of %s! (rc:%d)\n", name,
842 if (rc && rc1 != -EALREADY)
843 close_quota_files(oqctl, qinfo);
848 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
853 if (oqctl->qc_type != USRQUOTA &&
854 oqctl->qc_type != GRPQUOTA &&
855 oqctl->qc_type != UGQUOTA)
858 rc = generic_quota_on(obd, oqctl, 1);
864 int mds_admin_quota_off(struct obd_device *obd,
865 struct obd_quotactl *oqctl)
867 struct mds_obd *mds = &obd->u.mds;
868 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
872 /* close admin quota files */
873 rc = close_quota_files(oqctl, qinfo);
878 /* with obt->obt_quotachecking held */
879 int do_mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
881 struct mds_obd *mds = &obd->u.mds;
882 struct obd_device_target *obt = &obd->u.obt;
883 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
884 struct lvfs_run_ctxt saved;
885 int rc = 0, rc1 = 0, rc2 = 0;
888 LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
890 if (oqctl->qc_type != USRQUOTA &&
891 oqctl->qc_type != GRPQUOTA &&
892 oqctl->qc_type != UGQUOTA)
895 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
896 cfs_down_write(&mds->mds_qonoff_sem);
897 /* close admin quota files */
898 rc2 = mds_admin_quota_off(obd, oqctl);
899 if (rc2 && rc2 != -EALREADY) {
900 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
904 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
906 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
907 } else if (quota_is_off(qctxt, oqctl)) {
908 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
911 if (rc2 != -EALREADY) {
912 CWARN("mds local quota[%d] is failed to be off for %d\n",
913 oqctl->qc_type, rc1);
914 oqctl->qc_cmd = Q_QUOTAON;
915 mds_admin_quota_on(obd, oqctl);
916 oqctl->qc_cmd = Q_QUOTAOFF;
921 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
922 if (rc && rc != -EALREADY) {
923 CWARN("mds remote quota[%d] is failed to be off for %d\n",
925 oqctl->qc_cmd = Q_QUOTAON;
926 if (rc2 != -EALREADY)
927 mds_admin_quota_on(obd, oqctl);
928 if (rc1 != -EALREADY) {
929 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
930 qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
932 oqctl->qc_cmd = Q_QUOTAOFF;
937 cfs_up_write(&mds->mds_qonoff_sem);
938 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
939 return rc ? : (rc1 ? : rc2);
942 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
944 struct obd_device_target *obt = &obd->u.obt;
948 cfs_down(&obt->obt_quotachecking);
949 rc = do_mds_quota_off(obd, oqctl);
950 cfs_up(&obt->obt_quotachecking);
954 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
956 struct mds_obd *mds = &obd->u.mds;
957 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
958 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
962 if (oqctl->qc_type != USRQUOTA &&
963 oqctl->qc_type != GRPQUOTA)
966 cfs_down_write(&mds->mds_qonoff_sem);
967 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
968 CWARN("quota[%u] is off\n", oqctl->qc_type);
969 GOTO(out, rc = -ESRCH);
972 qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
973 qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
974 qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
976 rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
980 cfs_up_write(&mds->mds_qonoff_sem);
984 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
986 struct mds_obd *mds = &obd->u.mds;
987 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
988 struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
992 if (oqctl->qc_type != USRQUOTA &&
993 oqctl->qc_type != GRPQUOTA)
996 cfs_down_read(&mds->mds_qonoff_sem);
997 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
998 CWARN("quota[%u] is off\n", oqctl->qc_type);
999 GOTO(out, rc = -ESRCH);
1002 dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
1003 dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
1004 dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
1008 cfs_up_read(&mds->mds_qonoff_sem);
1012 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
1013 struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
1014 int type, struct quota_adjust_qunit *oqaq)
1016 __u64 bunit_curr_o, iunit_curr_o;
1017 unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
1018 unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
1019 __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
1020 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
1021 __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1022 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1026 if (!dquot || !oqaq)
1028 LASSERT_SEM_LOCKED(&dquot->dq_sem);
1029 LASSERT(oqaq->qaq_iunit_sz);
1030 LASSERT(oqaq->qaq_bunit_sz);
1032 /* don't change qunit size */
1033 if (!qctxt->lqc_switch_qs)
1036 bunit_curr_o = oqaq->qaq_bunit_sz;
1037 iunit_curr_o = oqaq->qaq_iunit_sz;
1039 if (dquot->dq_type == GRPQUOTA)
1042 if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1043 __u64 b_limitation =
1044 oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1045 /* enlarge block qunit size */
1047 QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1048 oqaq->qaq_bunit_sz =
1049 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1051 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1055 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1056 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1058 /* shrink block qunit size */
1060 QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1061 do_div(oqaq->qaq_bunit_sz , cqs_factor);
1062 oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1064 b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1066 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1070 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1071 oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1073 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1074 QAQ_SET_ADJBLK(oqaq);
1078 if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1079 __u64 i_limitation =
1080 oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1081 /* enlarge file qunit size */
1082 while (ilimit > dquot->dq_dqb.dqb_curinodes
1083 + 2 * i_limitation) {
1084 oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1085 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1089 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1090 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1092 /* shrink file qunit size */
1093 while (ilimit < dquot->dq_dqb.dqb_curinodes
1095 do_div(oqaq->qaq_iunit_sz, cqs_factor);
1096 i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1098 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1102 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1103 oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1105 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1106 QAQ_SET_ADJINO(oqaq);
1110 QAQ_DEBUG(oqaq, "the oqaq computed\n");
1115 static int mds_init_slave_ilimits(struct obd_device *obd,
1116 struct obd_quotactl *oqctl, int set)
1118 /* XXX: for file limits only adjust local now */
1119 struct obd_device_target *obt = &obd->u.obt;
1120 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1121 unsigned int id[MAXQUOTAS] = { 0, 0 };
1122 struct obd_quotactl *ioqc = NULL;
1123 struct lustre_qunit_size *lqs;
1128 /* if we are going to set zero limit, needn't init slaves */
1129 if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1133 OBD_ALLOC_PTR(ioqc);
1137 flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1138 oqctl->qc_dqblk.dqb_isoftlimit || !set;
1139 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1140 ioqc->qc_id = oqctl->qc_id;
1141 ioqc->qc_type = oqctl->qc_type;
1142 ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1143 ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1145 /* build lqs for mds */
1146 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1147 qctxt, flag ? 1 : 0);
1148 if (lqs && !IS_ERR(lqs)) {
1150 lqs->lqs_flags |= QI_SET;
1152 lqs->lqs_flags &= ~QI_SET;
1155 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1156 flag ? "create" : "search",
1157 oqctl->qc_type ? "group" : "user",
1159 GOTO(out, rc = PTR_ERR(lqs));
1162 /* set local limit to MIN_QLIMIT */
1163 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1167 /* trigger local qunit pre-acquire */
1168 if (oqctl->qc_type == USRQUOTA)
1169 id[USRQUOTA] = oqctl->qc_id;
1171 id[GRPQUOTA] = oqctl->qc_id;
1173 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1174 if (rc == -EDQUOT || rc == -EBUSY) {
1175 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1179 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1183 /* FIXME initialize all slaves in CMD */
1191 static int mds_init_slave_blimits(struct obd_device *obd,
1192 struct obd_quotactl *oqctl, int set)
1194 struct obd_device_target *obt = &obd->u.obt;
1195 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1196 struct mds_obd *mds = &obd->u.mds;
1197 struct obd_quotactl *ioqc;
1198 struct lustre_qunit_size *lqs;
1199 unsigned int id[MAXQUOTAS] = { 0, 0 };
1204 /* if we are going to set zero limit, needn't init slaves */
1205 if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1209 OBD_ALLOC_PTR(ioqc);
1213 flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1214 oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1215 ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1216 ioqc->qc_id = oqctl->qc_id;
1217 ioqc->qc_type = oqctl->qc_type;
1218 ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1219 ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1221 /* build lqs for mds */
1222 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1223 qctxt, flag ? 1 : 0);
1224 if (lqs && !IS_ERR(lqs)) {
1226 lqs->lqs_flags |= QB_SET;
1228 lqs->lqs_flags &= ~QB_SET;
1231 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1232 flag ? "create" : "search",
1233 oqctl->qc_type ? "group" : "user",
1235 GOTO(out, rc = PTR_ERR(lqs));
1238 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1242 /* trigger local qunit pre-acquire */
1243 if (oqctl->qc_type == USRQUOTA)
1244 id[USRQUOTA] = oqctl->qc_id;
1246 id[GRPQUOTA] = oqctl->qc_id;
1248 /* initialize all slave's limit */
1249 rc = obd_quotactl(mds->mds_lov_exp, ioqc);
1251 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1252 if (rc == -EDQUOT || rc == -EBUSY) {
1253 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1257 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1267 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1269 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1272 QAQ_SET_CREATE_LQS(qaq);
1273 /* adjust local lqs */
1274 rc = quota_adjust_slave_lqs(qaq, qctxt);
1276 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1278 /* adjust remote lqs */
1279 if (QAQ_IS_ADJBLK(qaq)) {
1280 rc = obd_quota_adjust_qunit(obd->u.mds.mds_lov_exp, qaq, qctxt, NULL);
1282 CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1287 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1289 struct mds_obd *mds = &obd->u.mds;
1290 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1291 struct obd_device *lov_obd = class_exp2obd(mds->mds_lov_exp);
1292 struct lov_obd *lov = &lov_obd->u.lov;
1293 struct quota_adjust_qunit *oqaq = NULL;
1294 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1295 __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1296 time_t btime, itime;
1297 struct lustre_dquot *dquot;
1298 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1299 /* orig_set means if quota was set before; now_set means we are
1300 * setting/cancelling quota */
1301 int orig_set, now_set;
1302 struct lustre_qunit_size *lqs;
1303 int rc = 0, rc2 = 0, flag = 0;
1306 if (oqctl->qc_type != USRQUOTA &&
1307 oqctl->qc_type != GRPQUOTA)
1310 OBD_ALLOC_PTR(oqaq);
1314 cfs_down_write(&mds->mds_qonoff_sem);
1315 init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1317 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1318 CWARN("quota[%u] is off\n", oqctl->qc_type);
1319 GOTO(out_sem, rc = -ESRCH);
1322 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1324 GOTO(out_sem, rc = PTR_ERR(dquot));
1325 DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1326 QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1328 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id), qctxt, 1);
1336 cfs_down(&dquot->dq_sem);
1337 cfs_spin_lock(&lqs->lqs_lock);
1338 if (LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1339 cfs_spin_unlock(&lqs->lqs_lock);
1340 cfs_up(&dquot->dq_sem);
1341 GOTO(skip, rc = -EBUSY);
1343 LQS_SET_SETQUOTA(lqs);
1344 cfs_spin_unlock(&lqs->lqs_lock);
1346 ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1347 isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1348 bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1349 bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1350 btime = dquot->dq_dqb.dqb_btime;
1351 itime = dquot->dq_dqb.dqb_itime;
1353 if (dqblk->dqb_valid & QIF_BTIME)
1354 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1355 if (dqblk->dqb_valid & QIF_ITIME)
1356 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1358 if (dqblk->dqb_valid & QIF_BLIMITS) {
1359 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1360 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1361 /* clear usage (limit pool) */
1362 if (!dquot->dq_dqb.dqb_bhardlimit &&
1363 !dquot->dq_dqb.dqb_bsoftlimit)
1364 dquot->dq_dqb.dqb_curspace = 0;
1366 /* clear grace time */
1367 if (!dqblk->dqb_bsoftlimit ||
1368 toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1369 dquot->dq_dqb.dqb_btime = 0;
1370 /* set grace only if user hasn't provided his own */
1371 else if (!(dqblk->dqb_valid & QIF_BTIME))
1372 dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1373 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1375 flag |= LQUOTA_FLAGS_ADJBLK;
1378 if (dqblk->dqb_valid & QIF_ILIMITS) {
1379 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1380 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1381 /* clear usage (limit pool) */
1382 if (!dquot->dq_dqb.dqb_ihardlimit &&
1383 !dquot->dq_dqb.dqb_isoftlimit)
1384 dquot->dq_dqb.dqb_curinodes = 0;
1386 if (!dqblk->dqb_isoftlimit ||
1387 dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1388 dquot->dq_dqb.dqb_itime = 0;
1389 else if (!(dqblk->dqb_valid & QIF_ITIME))
1390 dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1391 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1393 flag |= LQUOTA_FLAGS_ADJINO;
1395 QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1396 rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1398 QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1400 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1403 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1405 cfs_up(&dquot->dq_sem);
1408 CERROR("set limit failed! (rc:%d)\n", rc);
1409 GOTO(update_fail, rc);
1412 cfs_up_write(&mds->mds_qonoff_sem);
1413 adjust_lqs(obd, oqaq);
1415 orig_set = ihardlimit || isoftlimit;
1416 now_set = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1417 if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1418 cfs_down(&dquot->dq_sem);
1419 dquot->dq_dqb.dqb_curinodes = 0;
1420 cfs_up(&dquot->dq_sem);
1421 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1423 CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1428 orig_set = bhardlimit || bsoftlimit;
1429 now_set = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1430 if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1431 cfs_down(&dquot->dq_sem);
1432 dquot->dq_dqb.dqb_curspace = 0;
1433 cfs_up(&dquot->dq_sem);
1434 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1436 CERROR("init slave blimits failed! (rc:%d)\n", rc);
1442 cfs_down_write(&mds->mds_qonoff_sem);
1443 cfs_down(&dquot->dq_sem);
1445 /* cancel previous setting */
1446 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1447 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1448 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1449 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1450 dquot->dq_dqb.dqb_btime = btime;
1451 dquot->dq_dqb.dqb_itime = itime;
1453 rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1454 cfs_up(&dquot->dq_sem);
1456 cfs_spin_lock(&lqs->lqs_lock);
1457 LQS_CLEAR_SETQUOTA(lqs);
1458 cfs_spin_unlock(&lqs->lqs_lock);
1462 lustre_dqput(dquot);
1465 cfs_up_write(&mds->mds_qonoff_sem);
1470 return rc ? rc : rc2;
1473 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1475 struct obd_quotactl *soqc;
1476 struct lvfs_run_ctxt saved;
1480 OBD_ALLOC_PTR(soqc);
1484 soqc->qc_cmd = Q_GETOQUOTA;
1485 soqc->qc_id = oqctl->qc_id;
1486 soqc->qc_type = oqctl->qc_type;
1488 /* get block usage from OSS */
1489 soqc->qc_dqblk.dqb_curspace = 0;
1490 rc = obd_quotactl(obd->u.mds.mds_lov_exp, soqc);
1491 if (!rc || rc == -EREMOTEIO) {
1492 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1493 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1496 /* get block/inode usage from MDS */
1497 soqc->qc_dqblk.dqb_curspace = 0;
1498 soqc->qc_dqblk.dqb_curinodes = 0;
1499 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1500 rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1501 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1503 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1504 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1505 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1513 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1515 struct mds_obd *mds = &obd->u.mds;
1516 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1517 struct lustre_dquot *dquot;
1518 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1522 if (oqctl->qc_type != USRQUOTA &&
1523 oqctl->qc_type != GRPQUOTA)
1526 cfs_down_read(&mds->mds_qonoff_sem);
1527 dqblk->dqb_valid = 0;
1528 if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1529 CWARN("quota[%u] is off\n", oqctl->qc_type);
1530 GOTO(out, rc = -ESRCH);
1533 dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type, 1);
1535 GOTO(out, rc = PTR_ERR(dquot));
1537 cfs_down(&dquot->dq_sem);
1538 dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1539 dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1540 dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1541 dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1542 dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1543 dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1544 dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1545 cfs_up(&dquot->dq_sem);
1547 lustre_dqput(dquot);
1548 cfs_up_read(&mds->mds_qonoff_sem);
1550 /* the usages in admin quota file is inaccurate */
1551 dqblk->dqb_curinodes = 0;
1552 dqblk->dqb_curspace = 0;
1553 rc = mds_get_space(obd, oqctl);
1556 * Querying of curinodes and/or curspace may have failed, administrative
1557 * quota data are likely to be better approximation to the real usage in
1560 if (!(dqblk->dqb_valid & QIF_INODES) && dquot->dq_dqb.dqb_curinodes > 0)
1561 dqblk->dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
1563 if (!(dqblk->dqb_valid & QIF_SPACE) && dquot->dq_dqb.dqb_curspace > 0)
1564 dqblk->dqb_curspace = dquot->dq_dqb.dqb_curspace;
1569 cfs_up_read(&mds->mds_qonoff_sem);
1573 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1575 struct lvfs_run_ctxt saved;
1579 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1580 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1581 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1587 /* FIXME we only recovery block limit by now, need recovery inode
1588 * limits also after CMD involved in */
1590 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1592 struct mds_obd *mds = &obd->u.mds;
1593 struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1594 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1595 struct lustre_qunit_size *lqs;
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, 0);
1607 if (IS_ERR(dquot)) {
1608 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1610 RETURN(PTR_ERR(dquot));
1613 lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 1);
1621 cfs_down(&dquot->dq_sem);
1623 /* don't recover the dquot without limits or quota is setting or
1624 * another recovery is already going on */
1625 if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1626 LQS_IS_SETQUOTA(lqs) || LQS_IS_RECOVERY(lqs)) {
1627 cfs_up(&dquot->dq_sem);
1628 GOTO(skip1, rc = 0);
1631 cfs_spin_lock(&lqs->lqs_lock);
1632 LQS_SET_RECOVERY(lqs);
1633 cfs_spin_unlock(&lqs->lqs_lock);
1634 cfs_up(&dquot->dq_sem);
1636 /* release mds_qonoff_sem during obd_quotactl ops here */
1637 cfs_up_write(&mds->mds_qonoff_sem);
1639 /* get real bhardlimit from all slaves. */
1640 qctl->qc_cmd = Q_GETOQUOTA;
1641 qctl->qc_type = type;
1643 qctl->qc_stat = QUOTA_RECOVERING;
1644 rc = obd_quotactl(mds->mds_lov_exp, qctl);
1645 cfs_down_write(&mds->mds_qonoff_sem);
1648 total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1650 /* get real bhardlimit from master */
1651 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1654 total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1656 /* amend the usage of the administrative quotafile */
1657 cfs_down(&dquot->dq_sem);
1659 dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1661 rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1663 CERROR("write dquot failed! (rc:%d)\n", rc);
1665 cfs_up(&dquot->dq_sem);
1668 cfs_spin_lock(&lqs->lqs_lock);
1669 LQS_CLEAR_RECOVERY(lqs);
1670 cfs_spin_unlock(&lqs->lqs_lock);
1674 lustre_dqput(dquot);
1679 struct qmaster_recov_thread_data {
1680 struct obd_device *obd;
1681 cfs_completion_t comp;
1684 static int qmaster_recovery_main(void *arg)
1686 struct qmaster_recov_thread_data *data = arg;
1687 struct obd_device *obd = data->obd;
1688 struct mds_obd *mds = &obd->u.mds;
1689 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1691 unsigned short type;
1694 cfs_daemonize_ctxt("qmaster_recovd");
1697 class_incref(obd, "qmaster_recovd_mds", obd);
1699 class_incref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1701 cfs_complete(&data->comp);
1703 cfs_down_write(&mds->mds_qonoff_sem);
1704 for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1706 struct dquot_id *dqid, *tmp;
1708 if (qinfo->qi_files[type] == NULL)
1711 CFS_INIT_LIST_HEAD(&id_list);
1712 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1715 CERROR("error get ids from admin quotafile.(%d)\n", rc);
1717 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1718 cfs_list_del_init(&dqid->di_link);
1722 rc = dquot_recovery(obd, dqid->di_id, type);
1724 CERROR("%s: qmaster recovery failed for %sid %d"
1725 " rc:%d)\n", obd->obd_name,
1726 type ? "g" : "u", dqid->di_id, rc);
1731 cfs_up_write(&mds->mds_qonoff_sem);
1732 class_decref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1733 class_decref(obd, "qmaster_recovd_mds", obd);
1737 int mds_quota_recovery(struct obd_device *obd)
1739 struct mds_obd *mds = &obd->u.mds;
1740 struct qmaster_recov_thread_data data;
1744 if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
1747 if (unlikely(!mds->mds_quota || obd->obd_stopping))
1750 cfs_mutex_down(&obd->obd_dev_sem);
1751 if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1752 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1753 mds->mds_lov_desc.ld_active_tgt_count,
1754 mds->mds_lov_objid_count);
1755 cfs_mutex_up(&obd->obd_dev_sem);
1758 cfs_mutex_up(&obd->obd_dev_sem);
1761 cfs_init_completion(&data.comp);
1763 rc = cfs_kernel_thread(qmaster_recovery_main, &data,
1764 CLONE_VM|CLONE_FILES);
1766 CERROR("%s: cannot start quota recovery thread: rc %d\n",
1769 cfs_wait_for_completion(&data.comp);
1773 #endif /* HAVE_QUOTA_SUPPORT */