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_export *exp, struct obd_quotactl *oqctl)
69 struct obd_device *obd = exp->exp_obd;
70 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
71 struct timeval work_start;
72 struct timeval work_end;
77 do_gettimeofday(&work_start);
78 switch (oqctl->qc_cmd) {
80 rc = mds_quota_on(obd, oqctl);
83 mds_quota_off(obd, oqctl);
86 rc = mds_set_dqinfo(obd, oqctl);
89 rc = mds_get_dqinfo(obd, oqctl);
92 rc = mds_set_dqblk(obd, oqctl);
95 rc = mds_get_dqblk(obd, oqctl);
99 rc = mds_get_obd_quota(obd, oqctl);
101 case LUSTRE_Q_INVALIDATE:
102 rc = mds_quota_invalidate(obd, oqctl);
104 case LUSTRE_Q_FINVALIDATE:
105 rc = mds_quota_finvalidate(obd, oqctl);
108 CERROR("%s: unsupported mds_quotactl command: %d\n",
109 obd->obd_name, oqctl->qc_cmd);
114 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
115 "type %d, failed: rc = %d\n",
116 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
117 do_gettimeofday(&work_end);
118 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
119 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
124 int filter_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
126 struct obd_device *obd = exp->exp_obd;
127 struct obd_device_target *obt = &obd->u.obt;
128 struct lvfs_run_ctxt saved;
129 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
130 struct timeval work_start;
131 struct timeval work_end;
136 do_gettimeofday(&work_start);
137 switch (oqctl->qc_cmd) {
141 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
142 CDEBUG(D_INFO, "other people are doing quotacheck\n");
143 atomic_inc(&obt->obt_quotachecking);
147 if (oqctl->qc_cmd == Q_FINVALIDATE &&
148 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
152 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
156 /* In recovery scenario, this pending dqacq/dqrel might have
157 * been processed by master successfully before it's dquot
158 * on master enter recovery mode. We must wait for this
159 * dqacq/dqrel done then return the correct limits to master */
160 if (oqctl->qc_stat == QUOTA_RECOVERING)
161 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
162 oqctl->qc_id, oqctl->qc_type,
165 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
166 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
167 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
169 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
170 oqctl->qc_cmd == Q_FINVALIDATE) {
171 if (!rc && oqctl->qc_cmd == Q_QUOTAON)
172 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
173 if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
174 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
175 atomic_inc(&obt->obt_quotachecking);
179 /* currently, it is only used for nullifying the quota */
180 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
181 oqctl->qc_id, oqctl->qc_type, 1);
183 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
184 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
187 oqctl->qc_cmd = Q_SYNC;
188 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
189 oqctl->qc_cmd = Q_SETQUOTA;
191 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
195 unsigned int uid = 0, gid = 0;
197 /* Initialize quota limit to MIN_QLIMIT */
198 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
199 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
201 /* There might be a pending dqacq/dqrel (which is going to
202 * clear stale limits on slave). we should wait for it's
203 * completion then initialize limits */
204 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
205 oqctl->qc_id, oqctl->qc_type, 1);
207 if (!oqctl->qc_dqblk.dqb_bhardlimit)
210 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
211 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
212 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
214 /* Update on-disk quota, in case of lose the changed limits
215 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
217 oqctl->qc_cmd = Q_SYNC;
218 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
219 oqctl->qc_cmd = Q_INITQUOTA;
221 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
226 /* Trigger qunit pre-acquire */
227 if (oqctl->qc_type == USRQUOTA)
232 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
233 uid, gid, 1, 0, NULL);
234 if (rc == -EDQUOT || rc == -EBUSY) {
235 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
242 CERROR("%s: unsupported filter_quotactl command: %d\n",
243 obd->obd_name, oqctl->qc_cmd);
246 do_gettimeofday(&work_end);
247 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
248 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
252 #endif /* __KERNEL__ */
255 int client_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
257 struct ptlrpc_request *req;
258 struct obd_quotactl *oqc;
259 __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqctl) };
263 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
264 ver = LUSTRE_MDS_VERSION,
266 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
267 ver = LUSTRE_OST_VERSION,
273 req = ptlrpc_prep_req(class_exp2cliimp(exp), ver, opc, 2, size, NULL);
275 GOTO(out, rc = -ENOMEM);
277 oqc = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqctl));
280 ptlrpc_req_set_repsize(req, 2, size);
282 rc = ptlrpc_queue_wait(req);
284 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
290 oqc = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*oqc),
291 lustre_swab_obd_quotactl);
293 CERROR ("Can't unpack obd_quotactl\n");
294 GOTO(out, rc = -EPROTO);
300 ptlrpc_req_finished(req);
304 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
306 struct obd_device *obd = class_exp2obd(exp);
307 struct lov_obd *lov = &obd->u.lov;
309 __u64 bhardlimit = 0;
313 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
314 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
315 oqctl->qc_cmd != Q_GETOQUOTA &&
316 oqctl->qc_cmd != Q_INITQUOTA &&
317 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
318 oqctl->qc_cmd != Q_FINVALIDATE) {
319 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
323 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
326 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
327 if (oqctl->qc_cmd == Q_GETOQUOTA) {
328 CERROR("ost %d is inactive\n", i);
331 CDEBUG(D_HA, "ost %d is inactive\n", i);
336 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
338 if (lov->lov_tgts[i]->ltd_active && !rc)
343 if (oqctl->qc_cmd == Q_GETOQUOTA) {
344 curspace += oqctl->qc_dqblk.dqb_curspace;
345 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
349 if (oqctl->qc_cmd == Q_GETOQUOTA) {
350 oqctl->qc_dqblk.dqb_curspace = curspace;
351 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;