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))) {
154 atomic_inc(&obt->obt_quotachecking);
158 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
162 /* In recovery scenario, this pending dqacq/dqrel might have
163 * been processed by master successfully before it's dquot
164 * on master enter recovery mode. We must wait for this
165 * dqacq/dqrel done then return the correct limits to master */
166 if (oqctl->qc_stat == QUOTA_RECOVERING)
167 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
168 oqctl->qc_id, oqctl->qc_type,
171 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
172 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
173 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
175 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
176 oqctl->qc_cmd == Q_FINVALIDATE) {
177 if (!rc && oqctl->qc_cmd == Q_QUOTAON)
178 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
179 if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
180 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
181 atomic_inc(&obt->obt_quotachecking);
185 /* currently, it is only used for nullifying the quota */
186 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
187 oqctl->qc_id, oqctl->qc_type, 1);
189 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
190 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
193 oqctl->qc_cmd = Q_SYNC;
194 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
195 oqctl->qc_cmd = Q_SETQUOTA;
197 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
201 unsigned int uid = 0, gid = 0;
203 /* Initialize quota limit to MIN_QLIMIT */
204 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
205 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
207 /* There might be a pending dqacq/dqrel (which is going to
208 * clear stale limits on slave). we should wait for it's
209 * completion then initialize limits */
210 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
211 oqctl->qc_id, oqctl->qc_type, 1);
213 if (!oqctl->qc_dqblk.dqb_bhardlimit)
216 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
217 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
218 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
220 /* Update on-disk quota, in case of lose the changed limits
221 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
223 oqctl->qc_cmd = Q_SYNC;
224 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
225 oqctl->qc_cmd = Q_INITQUOTA;
227 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
232 /* Trigger qunit pre-acquire */
233 if (oqctl->qc_type == USRQUOTA)
238 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
239 uid, gid, 1, 0, NULL);
240 if (rc == -EDQUOT || rc == -EBUSY) {
241 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
248 CERROR("%s: unsupported filter_quotactl command: %d\n",
249 obd->obd_name, oqctl->qc_cmd);
252 do_gettimeofday(&work_end);
253 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
254 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
258 #endif /* __KERNEL__ */
261 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
262 struct obd_quotactl *oqctl)
264 struct ptlrpc_request *req;
265 struct obd_quotactl *oqc;
266 const struct req_format *rf;
270 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
271 rf = &RQF_MDS_QUOTACTL;
272 ver = LUSTRE_MDS_VERSION,
274 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
275 rf = &RQF_OST_QUOTACTL;
276 ver = LUSTRE_OST_VERSION,
282 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
286 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
289 ptlrpc_request_set_replen(req);
291 rc = ptlrpc_queue_wait(req);
293 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
299 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
302 CERROR ("Can't unpack obd_quotactl\n");
303 GOTO(out, rc = -EPROTO);
309 ptlrpc_req_finished(req);
314 * For lmv, only need to send request to master MDT, and the master MDT will
315 * process with other slave MDTs.
317 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
318 struct obd_quotactl *oqctl)
320 struct obd_device *obd = class_exp2obd(exp);
321 struct lmv_obd *lmv = &obd->u.lmv;
322 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
326 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
327 CERROR("master lmv inactive\n");
331 rc = obd_quotactl(tgt->ltd_exp, oqctl);
335 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
336 struct obd_quotactl *oqctl)
338 struct obd_device *obd = class_exp2obd(exp);
339 struct lov_obd *lov = &obd->u.lov;
341 __u64 bhardlimit = 0;
345 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
346 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
347 oqctl->qc_cmd != Q_GETOQUOTA &&
348 oqctl->qc_cmd != Q_INITQUOTA &&
349 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
350 oqctl->qc_cmd != Q_FINVALIDATE) {
351 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
355 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
358 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
359 if (oqctl->qc_cmd == Q_GETOQUOTA) {
360 CERROR("ost %d is inactive\n", i);
363 CDEBUG(D_HA, "ost %d is inactive\n", i);
368 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
370 if (lov->lov_tgts[i]->ltd_active && !rc)
375 if (oqctl->qc_cmd == Q_GETOQUOTA) {
376 curspace += oqctl->qc_dqblk.dqb_curspace;
377 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
381 if (oqctl->qc_cmd == Q_GETOQUOTA) {
382 oqctl->qc_dqblk.dqb_curspace = curspace;
383 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;