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/quota.h>
47 # include <linux/smp_lock.h>
48 # include <linux/buffer_head.h>
49 # include <linux/workqueue.h>
50 # include <linux/mount.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
55 #include <obd_class.h>
56 #include <lustre_mds.h>
57 #include <lustre_dlm.h>
58 #include <lustre_cfg.h>
60 #include <lustre_fsfilt.h>
61 #include <lustre_quota.h>
62 #include "quota_internal.h"
64 #ifdef HAVE_QUOTA_SUPPORT
67 /* When quotaon, build a lqs for every uid/gid who has been set limitation
68 * for quota. After quota_search_lqs, it will hold one ref for the lqs.
69 * It will be released when qctxt_cleanup() is executed b=18574 */
70 void build_lqs(struct obd_device *obd)
72 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
73 struct list_head id_list;
76 INIT_LIST_HEAD(&id_list);
77 for (i = 0; i < MAXQUOTAS; i++) {
78 struct dquot_id *dqid, *tmp;
80 if (sb_dqopt(qctxt->lqc_sb)->files[i] == NULL)
83 #ifndef KERNEL_SUPPORTS_QUOTA_READ
84 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
87 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
91 CERROR("%s: failed to get %s qids\n", obd->obd_name,
92 i ? "group" : "user");
96 list_for_each_entry_safe(dqid, tmp, &id_list,
98 struct lustre_qunit_size *lqs;
100 list_del_init(&dqid->di_link);
101 lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
103 if (lqs && !IS_ERR(lqs)) {
104 lqs->lqs_flags |= dqid->di_flag;
107 CERROR("%s: failed to create a lqs for %sid %u"
108 "\n", obd->obd_name, i ? "g" : "u",
117 int mds_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
119 struct obd_device *obd = exp->exp_obd;
120 struct obd_device_target *obt = &obd->u.obt;
121 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
122 struct timeval work_start;
123 struct timeval work_end;
128 do_gettimeofday(&work_start);
129 switch (oqctl->qc_cmd) {
131 rc = mds_quota_on(obd, oqctl);
132 /* when quotaon, create lqs for every quota uid/gid b=18574 */
136 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
137 mds_quota_off(obd, oqctl);
140 rc = mds_set_dqinfo(obd, oqctl);
143 rc = mds_get_dqinfo(obd, oqctl);
146 rc = mds_set_dqblk(obd, oqctl);
149 rc = mds_get_dqblk(obd, oqctl);
153 rc = mds_get_obd_quota(obd, oqctl);
155 case LUSTRE_Q_INVALIDATE:
156 rc = mds_quota_invalidate(obd, oqctl);
158 case LUSTRE_Q_FINVALIDATE:
159 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
160 rc = mds_quota_finvalidate(obd, oqctl);
163 CERROR("%s: unsupported mds_quotactl command: %d\n",
164 obd->obd_name, oqctl->qc_cmd);
169 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
170 "type %d, failed: rc = %d\n",
171 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
172 do_gettimeofday(&work_end);
173 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
174 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
179 int filter_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
181 struct obd_device *obd = exp->exp_obd;
182 struct obd_device_target *obt = &obd->u.obt;
183 struct lvfs_run_ctxt saved;
184 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
185 struct lustre_qunit_size *lqs;
187 struct timeval work_start;
188 struct timeval work_end;
193 do_gettimeofday(&work_start);
194 switch (oqctl->qc_cmd) {
196 oqctl->qc_id = obt->obt_qfmt;
197 rc = generic_quota_on(obd, oqctl, 0);
198 /* when quotaon, create lqs for every quota uid/gid b=18574 */
203 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
204 CDEBUG(D_INFO, "other people are doing quotacheck\n");
205 atomic_inc(&obt->obt_quotachecking);
209 if (oqctl->qc_cmd == Q_FINVALIDATE &&
210 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
211 atomic_inc(&obt->obt_quotachecking);
215 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
219 /* In recovery scenario, this pending dqacq/dqrel might have
220 * been processed by master successfully before it's dquot
221 * on master enter recovery mode. We must wait for this
222 * dqacq/dqrel done then return the correct limits to master */
223 if (oqctl->qc_stat == QUOTA_RECOVERING)
224 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
226 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
227 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
228 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
230 if (oqctl->qc_stat == QUOTA_RECOVERING)
231 quota_unbarrier(handle);
233 if (oqctl->qc_cmd == Q_QUOTAOFF ||
234 oqctl->qc_cmd == Q_FINVALIDATE) {
235 if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
236 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
237 atomic_inc(&obt->obt_quotachecking);
241 /* currently, it is only used for nullifying the quota */
242 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
244 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
245 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
248 oqctl->qc_cmd = Q_SYNC;
249 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
250 oqctl->qc_cmd = Q_SETQUOTA;
252 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
253 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",
262 lqs->lqs_flags &= ~QB_SET;
269 unsigned int uid = 0, gid = 0;
271 /* Initialize quota limit to MIN_QLIMIT */
272 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
273 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
275 if (!oqctl->qc_dqblk.dqb_bhardlimit)
278 /* There might be a pending dqacq/dqrel (which is going to
279 * clear stale limits on slave). we should wait for it's
280 * completion then initialize limits */
281 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
282 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
283 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
284 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
286 /* Update on-disk quota, in case of lose the changed limits
287 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
289 oqctl->qc_cmd = Q_SYNC;
290 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
291 oqctl->qc_cmd = Q_INITQUOTA;
293 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
294 quota_unbarrier(handle);
299 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
301 if (lqs == NULL || IS_ERR(lqs)){
302 CERROR("fail to create lqs during setquota operation "
303 "for %sid %u\n", oqctl->qc_type ? "g" : "u",
307 lqs->lqs_flags |= QB_SET;
308 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
309 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
310 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
311 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
312 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
317 /* Trigger qunit pre-acquire */
318 if (oqctl->qc_type == USRQUOTA)
323 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
324 uid, gid, 1, 0, NULL);
325 if (rc == -EDQUOT || rc == -EBUSY) {
326 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
333 CERROR("%s: unsupported filter_quotactl command: %d\n",
334 obd->obd_name, oqctl->qc_cmd);
337 do_gettimeofday(&work_end);
338 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
339 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
343 #endif /* __KERNEL__ */
346 int client_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
348 struct ptlrpc_request *req;
349 struct obd_quotactl *oqc;
350 __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqctl) };
351 int ver, opc, rc, resends = 0;
354 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
355 ver = LUSTRE_MDS_VERSION,
357 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
358 ver = LUSTRE_OST_VERSION,
366 req = ptlrpc_prep_req(class_exp2cliimp(exp), ver, opc, 2, size, NULL);
368 GOTO(out, rc = -ENOMEM);
370 oqc = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqctl));
373 ptlrpc_req_set_repsize(req, 2, size);
374 ptlrpc_at_set_req_timeout(req);
375 req->rq_no_resend = 1;
377 rc = ptlrpc_queue_wait(req);
379 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
385 oqc = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*oqc),
386 lustre_swab_obd_quotactl);
388 CERROR ("Can't unpack obd_quotactl\n");
389 GOTO(out, rc = -EPROTO);
395 ptlrpc_req_finished(req);
397 if (client_quota_recoverable_error(rc)) {
399 if (!client_quota_should_resend(resends, &exp->exp_obd->u.cli)) {
400 CERROR("too many resend retries, returning error "
401 "(cmd = %d, id = %u, type = %d)\n",
402 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type);
406 goto restart_request;
412 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
414 struct obd_device *obd = class_exp2obd(exp);
415 struct lov_obd *lov = &obd->u.lov;
417 __u64 bhardlimit = 0;
421 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
422 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
423 oqctl->qc_cmd != Q_GETOQUOTA &&
424 oqctl->qc_cmd != Q_INITQUOTA &&
425 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
426 oqctl->qc_cmd != Q_FINVALIDATE) {
427 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
432 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
435 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
436 if (oqctl->qc_cmd == Q_GETOQUOTA) {
437 CERROR("ost %d is inactive\n", i);
440 CDEBUG(D_HA, "ost %d is inactive\n", i);
445 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
447 if (lov->lov_tgts[i]->ltd_active && !rc)
452 if (oqctl->qc_cmd == Q_GETOQUOTA) {
453 curspace += oqctl->qc_dqblk.dqb_curspace;
454 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
459 if (oqctl->qc_cmd == Q_GETOQUOTA) {
460 oqctl->qc_dqblk.dqb_curspace = curspace;
461 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;