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
68 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
69 struct obd_quotactl *oqctl)
71 struct obd_device_target *obt = &obd->u.obt;
72 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
73 struct timeval work_start;
74 struct timeval work_end;
79 cfs_gettimeofday(&work_start);
80 switch (oqctl->qc_cmd) {
82 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
83 rc = mds_quota_on(obd, oqctl);
86 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
87 rc = mds_quota_off(obd, oqctl);
90 rc = mds_set_dqinfo(obd, oqctl);
93 rc = mds_get_dqinfo(obd, oqctl);
96 rc = mds_set_dqblk(obd, oqctl);
99 rc = mds_get_dqblk(obd, oqctl);
103 rc = mds_get_obd_quota(obd, oqctl);
105 case LUSTRE_Q_INVALIDATE:
106 rc = mds_quota_invalidate(obd, oqctl);
108 case LUSTRE_Q_FINVALIDATE:
109 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
110 rc = mds_quota_finvalidate(obd, oqctl);
113 CERROR("%s: unsupported mds_quotactl command: %d\n",
114 obd->obd_name, oqctl->qc_cmd);
119 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
120 "type %d, failed: rc = %d\n",
121 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
122 cfs_gettimeofday(&work_end);
123 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
124 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
129 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
130 struct obd_quotactl *oqctl)
132 struct obd_device *obd = exp->exp_obd;
133 struct obd_device_target *obt = &obd->u.obt;
134 struct lvfs_run_ctxt saved;
135 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
136 struct lustre_qunit_size *lqs;
138 struct timeval work_start;
139 struct timeval work_end;
144 cfs_gettimeofday(&work_start);
145 switch (oqctl->qc_cmd) {
147 oqctl->qc_id = obt->obt_qfmt;
148 rc = generic_quota_on(obd, oqctl, 0);
152 cfs_down(&obt->obt_quotachecking);
153 if (oqctl->qc_cmd == Q_FINVALIDATE &&
154 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
155 CWARN("quota[%u] is on yet\n", oqctl->qc_type);
156 cfs_up(&obt->obt_quotachecking);
160 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
164 /* In recovery scenario, this pending dqacq/dqrel might have
165 * been processed by master successfully before it's dquot
166 * on master enter recovery mode. We must wait for this
167 * dqacq/dqrel done then return the correct limits to master */
168 if (oqctl->qc_stat == QUOTA_RECOVERING)
169 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
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_stat == QUOTA_RECOVERING)
176 quota_unbarrier(handle);
178 if (oqctl->qc_cmd == Q_QUOTAOFF ||
179 oqctl->qc_cmd == Q_FINVALIDATE) {
180 if (oqctl->qc_cmd == Q_QUOTAOFF) {
182 obt->obt_qctxt.lqc_flags &=
183 ~UGQUOTA2LQC(oqctl->qc_type);
184 else if (quota_is_off(qctxt, oqctl))
187 cfs_up(&obt->obt_quotachecking);
192 /* currently, it is only used for nullifying the quota */
193 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
195 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
196 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
199 oqctl->qc_cmd = Q_SYNC;
200 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
201 oqctl->qc_cmd = Q_SETQUOTA;
203 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
204 quota_unbarrier(handle);
206 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
208 if (lqs == NULL || IS_ERR(lqs)){
209 CERROR("fail to create lqs during setquota operation "
210 "for %sid %u\n", oqctl->qc_type ? "g" : "u",
213 lqs->lqs_flags &= ~QB_SET;
220 unsigned int id[MAXQUOTAS] = { 0, 0 };
222 /* Initialize quota limit to MIN_QLIMIT */
223 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
224 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
226 if (!oqctl->qc_dqblk.dqb_bhardlimit)
229 /* There might be a pending dqacq/dqrel (which is going to
230 * clear stale limits on slave). we should wait for it's
231 * completion then initialize limits */
232 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
233 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
234 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
235 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
237 /* Update on-disk quota, in case of lose the changed limits
238 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
240 oqctl->qc_cmd = Q_SYNC;
241 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
242 oqctl->qc_cmd = Q_INITQUOTA;
244 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
245 quota_unbarrier(handle);
250 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
252 if (lqs == NULL || IS_ERR(lqs)){
253 CERROR("fail to create lqs during setquota operation "
254 "for %sid %u\n", oqctl->qc_type ? "g" : "u",
258 lqs->lqs_flags |= QB_SET;
262 /* Trigger qunit pre-acquire */
263 if (oqctl->qc_type == USRQUOTA)
264 id[USRQUOTA] = oqctl->qc_id;
266 id[GRPQUOTA] = oqctl->qc_id;
268 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
270 if (rc == -EDQUOT || rc == -EBUSY) {
271 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
278 CERROR("%s: unsupported filter_quotactl command: %d\n",
279 obd->obd_name, oqctl->qc_cmd);
282 cfs_gettimeofday(&work_end);
283 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
284 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
288 #endif /* __KERNEL__ */
291 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
292 struct obd_quotactl *oqctl)
294 struct ptlrpc_request *req;
295 struct obd_quotactl *oqc;
296 const struct req_format *rf;
297 int ver, opc, rc, resends = 0;
300 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
301 rf = &RQF_MDS_QUOTACTL;
302 ver = LUSTRE_MDS_VERSION,
304 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
305 rf = &RQF_OST_QUOTACTL;
306 ver = LUSTRE_OST_VERSION,
314 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
318 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
321 ptlrpc_request_set_replen(req);
322 ptlrpc_at_set_req_timeout(req);
323 req->rq_no_resend = 1;
325 rc = ptlrpc_queue_wait(req);
327 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
333 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
336 CERROR ("Can't unpack obd_quotactl\n");
337 GOTO(out, rc = -EPROTO);
343 ptlrpc_req_finished(req);
345 if (client_quota_recoverable_error(rc)) {
347 if (!client_quota_should_resend(resends, &exp->exp_obd->u.cli)) {
348 CERROR("too many resend retries, returning error\n");
352 goto restart_request;
359 * For lmv, only need to send request to master MDT, and the master MDT will
360 * process with other slave MDTs.
362 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
363 struct obd_quotactl *oqctl)
365 struct obd_device *obd = class_exp2obd(exp);
366 struct lmv_obd *lmv = &obd->u.lmv;
367 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
371 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
372 CERROR("master lmv inactive\n");
376 rc = obd_quotactl(tgt->ltd_exp, oqctl);
380 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
381 struct obd_quotactl *oqctl)
383 struct obd_device *obd = class_exp2obd(exp);
384 struct lov_obd *lov = &obd->u.lov;
385 struct lov_tgt_desc *tgt;
387 __u64 bhardlimit = 0;
391 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
392 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
393 oqctl->qc_cmd != Q_GETOQUOTA &&
394 oqctl->qc_cmd != Q_INITQUOTA &&
395 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
396 oqctl->qc_cmd != Q_FINVALIDATE) {
397 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
403 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
406 tgt = lov->lov_tgts[i];
407 if (!tgt || !tgt->ltd_active || tgt->ltd_reap) {
408 if (oqctl->qc_cmd == Q_GETOQUOTA) {
410 CERROR("ost %d is inactive\n", i);
412 CDEBUG(D_HA, "ost %d is inactive\n", i);
417 err = obd_quotactl(tgt->ltd_exp, oqctl);
419 if (tgt->ltd_active && !rc)
424 if (oqctl->qc_cmd == Q_GETOQUOTA) {
425 curspace += oqctl->qc_dqblk.dqb_curspace;
426 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
431 if (oqctl->qc_cmd == Q_GETOQUOTA) {
432 oqctl->qc_dqblk.dqb_curspace = curspace;
433 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;