1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 # define EXPORT_SYMTAB
39 #define DEBUG_SUBSYSTEM S_LQUOTA
42 # include <linux/version.h>
43 # include <linux/module.h>
44 # include <linux/init.h>
45 # include <linux/fs.h>
46 # include <linux/jbd.h>
47 # include <linux/quota.h>
48 # include <linux/smp_lock.h>
49 # include <linux/buffer_head.h>
50 # include <linux/workqueue.h>
51 # include <linux/mount.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lustre_dlm.h>
59 #include <lustre_cfg.h>
61 #include <lustre_fsfilt.h>
62 #include <lustre_quota.h>
63 #include "quota_internal.h"
65 #ifdef HAVE_QUOTA_SUPPORT
67 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
68 struct obd_quotactl *oqctl)
70 struct obd_device_target *obt = &obd->u.obt;
71 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
72 struct timeval work_start;
73 struct timeval work_end;
78 do_gettimeofday(&work_start);
79 switch (oqctl->qc_cmd) {
81 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
82 rc = mds_quota_on(obd, oqctl);
85 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
86 rc = mds_quota_off(obd, oqctl);
89 rc = mds_set_dqinfo(obd, oqctl);
92 rc = mds_get_dqinfo(obd, oqctl);
95 rc = mds_set_dqblk(obd, oqctl);
98 rc = mds_get_dqblk(obd, oqctl);
102 rc = mds_get_obd_quota(obd, oqctl);
104 case LUSTRE_Q_INVALIDATE:
105 rc = mds_quota_invalidate(obd, oqctl);
107 case LUSTRE_Q_FINVALIDATE:
108 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
109 rc = mds_quota_finvalidate(obd, oqctl);
112 CERROR("%s: unsupported mds_quotactl command: %d\n",
113 obd->obd_name, oqctl->qc_cmd);
118 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
119 "type %d, failed: rc = %d\n",
120 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
121 do_gettimeofday(&work_end);
122 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
123 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
128 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
129 struct obd_quotactl *oqctl)
131 struct obd_device *obd = exp->exp_obd;
132 struct obd_device_target *obt = &obd->u.obt;
133 struct lvfs_run_ctxt saved;
134 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
135 struct timeval work_start;
136 struct timeval work_end;
141 do_gettimeofday(&work_start);
142 switch (oqctl->qc_cmd) {
146 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
147 CDEBUG(D_INFO, "other people are doing quotacheck\n");
148 atomic_inc(&obt->obt_quotachecking);
152 if (oqctl->qc_cmd == Q_FINVALIDATE &&
153 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
157 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
161 /* In recovery scenario, this pending dqacq/dqrel might have
162 * been processed by master successfully before it's dquot
163 * on master enter recovery mode. We must wait for this
164 * dqacq/dqrel done then return the correct limits to master */
165 if (oqctl->qc_stat == QUOTA_RECOVERING)
166 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
167 oqctl->qc_id, oqctl->qc_type,
170 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
171 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
172 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
174 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
175 oqctl->qc_cmd == Q_FINVALIDATE) {
176 if (!rc && oqctl->qc_cmd == Q_QUOTAON)
177 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
178 if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
179 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
180 atomic_inc(&obt->obt_quotachecking);
184 /* currently, it is only used for nullifying the quota */
185 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
186 oqctl->qc_id, oqctl->qc_type, 1);
188 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
189 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
192 oqctl->qc_cmd = Q_SYNC;
193 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
194 oqctl->qc_cmd = Q_SETQUOTA;
196 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
200 unsigned int uid = 0, gid = 0;
202 /* Initialize quota limit to MIN_QLIMIT */
203 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
204 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
206 /* There might be a pending dqacq/dqrel (which is going to
207 * clear stale limits on slave). we should wait for it's
208 * completion then initialize limits */
209 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
210 oqctl->qc_id, oqctl->qc_type, 1);
212 if (!oqctl->qc_dqblk.dqb_bhardlimit)
215 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
216 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
217 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
219 /* Update on-disk quota, in case of lose the changed limits
220 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
222 oqctl->qc_cmd = Q_SYNC;
223 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
224 oqctl->qc_cmd = Q_INITQUOTA;
226 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
231 /* Trigger qunit pre-acquire */
232 if (oqctl->qc_type == USRQUOTA)
237 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
238 uid, gid, 1, 0, NULL);
239 if (rc == -EDQUOT || rc == -EBUSY) {
240 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
247 CERROR("%s: unsupported filter_quotactl command: %d\n",
248 obd->obd_name, oqctl->qc_cmd);
251 do_gettimeofday(&work_end);
252 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
253 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
257 #endif /* __KERNEL__ */
260 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
261 struct obd_quotactl *oqctl)
263 struct ptlrpc_request *req;
264 struct obd_quotactl *oqc;
265 const struct req_format *rf;
269 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
270 rf = &RQF_MDS_QUOTACTL;
271 ver = LUSTRE_MDS_VERSION,
273 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
274 rf = &RQF_OST_QUOTACTL;
275 ver = LUSTRE_OST_VERSION,
281 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
285 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
288 ptlrpc_request_set_replen(req);
290 rc = ptlrpc_queue_wait(req);
292 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
298 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
301 CERROR ("Can't unpack obd_quotactl\n");
302 GOTO(out, rc = -EPROTO);
308 ptlrpc_req_finished(req);
313 * For lmv, only need to send request to master MDT, and the master MDT will
314 * process with other slave MDTs.
316 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
317 struct obd_quotactl *oqctl)
319 struct obd_device *obd = class_exp2obd(exp);
320 struct lmv_obd *lmv = &obd->u.lmv;
321 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
325 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
326 CERROR("master lmv inactive\n");
330 rc = obd_quotactl(tgt->ltd_exp, oqctl);
334 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
335 struct obd_quotactl *oqctl)
337 struct obd_device *obd = class_exp2obd(exp);
338 struct lov_obd *lov = &obd->u.lov;
340 __u64 bhardlimit = 0;
344 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
345 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
346 oqctl->qc_cmd != Q_GETOQUOTA &&
347 oqctl->qc_cmd != Q_INITQUOTA &&
348 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
349 oqctl->qc_cmd != Q_FINVALIDATE) {
350 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
354 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
357 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
358 if (oqctl->qc_cmd == Q_GETOQUOTA) {
359 CERROR("ost %d is inactive\n", i);
362 CDEBUG(D_HA, "ost %d is inactive\n", i);
367 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
369 if (lov->lov_tgts[i]->ltd_active && !rc)
374 if (oqctl->qc_cmd == Q_GETOQUOTA) {
375 curspace += oqctl->qc_dqblk.dqb_curspace;
376 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
380 if (oqctl->qc_cmd == Q_GETOQUOTA) {
381 oqctl->qc_dqblk.dqb_curspace = curspace;
382 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;