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.
39 # define EXPORT_SYMTAB
41 #define DEBUG_SUBSYSTEM S_LQUOTA
44 # include <linux/version.h>
45 # include <linux/module.h>
46 # include <linux/init.h>
47 # include <linux/fs.h>
48 # include <linux/jbd.h>
49 # include <linux/quota.h>
50 # include <linux/smp_lock.h>
51 # include <linux/buffer_head.h>
52 # include <linux/workqueue.h>
53 # include <linux/mount.h>
54 #else /* __KERNEL__ */
55 # include <liblustre.h>
58 #include <obd_class.h>
59 #include <lustre_mds.h>
60 #include <lustre_dlm.h>
61 #include <lustre_cfg.h>
63 #include <lustre_fsfilt.h>
64 #include <lustre_quota.h>
65 #include "quota_internal.h"
67 #ifdef HAVE_QUOTA_SUPPORT
70 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
71 struct obd_quotactl *oqctl)
73 struct obd_device_target *obt = &obd->u.obt;
74 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
75 struct timeval work_start;
76 struct timeval work_end;
81 cfs_gettimeofday(&work_start);
82 switch (oqctl->qc_cmd) {
84 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
85 rc = mds_quota_on(obd, oqctl);
88 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
89 rc = mds_quota_off(obd, oqctl);
92 rc = mds_set_dqinfo(obd, oqctl);
95 rc = mds_get_dqinfo(obd, oqctl);
98 rc = mds_set_dqblk(obd, oqctl);
101 rc = mds_get_dqblk(obd, oqctl);
105 rc = mds_get_obd_quota(obd, oqctl);
107 case LUSTRE_Q_INVALIDATE:
108 rc = mds_quota_invalidate(obd, oqctl);
110 case LUSTRE_Q_FINVALIDATE:
111 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
112 rc = mds_quota_finvalidate(obd, oqctl);
115 CERROR("%s: unsupported mds_quotactl command: %d\n",
116 obd->obd_name, oqctl->qc_cmd);
121 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
122 "type %d, failed: rc = %d\n",
123 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
124 cfs_gettimeofday(&work_end);
125 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
126 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
131 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
132 struct obd_quotactl *oqctl)
134 struct obd_device *obd = exp->exp_obd;
135 struct obd_device_target *obt = &obd->u.obt;
136 struct lvfs_run_ctxt saved;
137 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
138 struct lustre_qunit_size *lqs;
140 struct timeval work_start;
141 struct timeval work_end;
146 cfs_gettimeofday(&work_start);
147 switch (oqctl->qc_cmd) {
149 oqctl->qc_id = obt->obt_qfmt;
150 rc = generic_quota_on(obd, oqctl, 0);
154 cfs_down(&obt->obt_quotachecking);
155 if (oqctl->qc_cmd == Q_FINVALIDATE &&
156 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
157 CWARN("quota[%u] is on yet\n", oqctl->qc_type);
158 cfs_up(&obt->obt_quotachecking);
162 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
166 /* In recovery scenario, this pending dqacq/dqrel might have
167 * been processed by master successfully before it's dquot
168 * on master enter recovery mode. We must wait for this
169 * dqacq/dqrel done then return the correct limits to master */
170 if (oqctl->qc_stat == QUOTA_RECOVERING)
171 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
173 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
174 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
175 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
177 if (oqctl->qc_stat == QUOTA_RECOVERING)
178 quota_unbarrier(handle);
180 if (oqctl->qc_cmd == Q_QUOTAOFF ||
181 oqctl->qc_cmd == Q_FINVALIDATE) {
182 if (oqctl->qc_cmd == Q_QUOTAOFF) {
184 obt->obt_qctxt.lqc_flags &=
185 ~UGQUOTA2LQC(oqctl->qc_type);
186 else if (quota_is_off(qctxt, oqctl))
188 CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc "
189 "%u:%lu:%d\n", obd->obd_name,
190 oqctl->qc_type, qctxt->lqc_flags, rc);
192 cfs_up(&obt->obt_quotachecking);
197 /* currently, it is only used for nullifying the quota */
198 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
200 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
201 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
204 oqctl->qc_cmd = Q_SYNC;
205 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
206 oqctl->qc_cmd = Q_SETQUOTA;
208 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
209 quota_unbarrier(handle);
211 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
213 if (lqs == NULL || IS_ERR(lqs)){
214 CERROR("fail to create lqs during setquota operation "
215 "for %sid %u\n", oqctl->qc_type ? "g" : "u",
218 lqs->lqs_flags &= ~QB_SET;
225 unsigned int id[MAXQUOTAS] = { 0, 0 };
227 /* Initialize quota limit to MIN_QLIMIT */
228 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
229 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
231 if (!oqctl->qc_dqblk.dqb_bhardlimit)
234 /* There might be a pending dqacq/dqrel (which is going to
235 * clear stale limits on slave). we should wait for it's
236 * completion then initialize limits */
237 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
238 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
239 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
240 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
242 /* Update on-disk quota, in case of lose the changed limits
243 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
245 oqctl->qc_cmd = Q_SYNC;
246 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
247 oqctl->qc_cmd = Q_INITQUOTA;
249 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
250 quota_unbarrier(handle);
255 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
257 if (lqs == NULL || IS_ERR(lqs)){
258 CERROR("fail to create lqs during setquota operation "
259 "for %sid %u\n", oqctl->qc_type ? "g" : "u",
263 lqs->lqs_flags |= QB_SET;
267 /* Trigger qunit pre-acquire */
268 if (oqctl->qc_type == USRQUOTA)
269 id[USRQUOTA] = oqctl->qc_id;
271 id[GRPQUOTA] = oqctl->qc_id;
273 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
275 if (rc == -EDQUOT || rc == -EBUSY) {
276 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
283 CERROR("%s: unsupported filter_quotactl command: %d\n",
284 obd->obd_name, oqctl->qc_cmd);
287 cfs_gettimeofday(&work_end);
288 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
289 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
293 #endif /* __KERNEL__ */