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 /* When quotaon, build a lqs for every uid/gid who has been set limitation
69 * for quota. After quota_search_lqs, it will hold one ref for the lqs.
70 * It will be released when qctxt_cleanup() is executed b=18574 */
71 void build_lqs(struct obd_device *obd)
73 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
74 struct list_head id_list;
77 INIT_LIST_HEAD(&id_list);
78 for (i = 0; i < MAXQUOTAS; i++) {
79 struct dquot_id *dqid, *tmp;
81 #ifndef KERNEL_SUPPORTS_QUOTA_READ
82 rc = fsfilt_qids(obd, sb_dqopt(qctxt->lqc_sb)->files[i], NULL,
85 rc = fsfilt_qids(obd, NULL, sb_dqopt(qctxt->lqc_sb)->files[i],
89 CDEBUG(D_ERROR, "fail to get %s qids!\n",
90 i ? "group" : "user");
94 list_for_each_entry_safe(dqid, tmp, &id_list,
96 struct lustre_qunit_size *lqs;
98 list_del_init(&dqid->di_link);
99 lqs = quota_search_lqs(LQS_KEY(i, dqid->di_id),
101 if (lqs && !IS_ERR(lqs)) {
102 lqs->lqs_flags |= dqid->di_flag;
105 CDEBUG(D_ERROR, "fail to create a lqs"
106 "(%s id: %u)!\n", i ? "group" : "user",
115 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
116 struct obd_quotactl *oqctl)
118 struct obd_device_target *obt = &obd->u.obt;
119 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
120 struct timeval work_start;
121 struct timeval work_end;
126 do_gettimeofday(&work_start);
127 switch (oqctl->qc_cmd) {
129 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
130 rc = mds_quota_on(obd, oqctl);
131 /* when quotaon, create lqs for every quota uid/gid b=18574 */
135 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
136 rc = mds_quota_off(obd, oqctl);
139 rc = mds_set_dqinfo(obd, oqctl);
142 rc = mds_get_dqinfo(obd, oqctl);
145 rc = mds_set_dqblk(obd, oqctl);
148 rc = mds_get_dqblk(obd, oqctl);
152 rc = mds_get_obd_quota(obd, oqctl);
154 case LUSTRE_Q_INVALIDATE:
155 rc = mds_quota_invalidate(obd, oqctl);
157 case LUSTRE_Q_FINVALIDATE:
158 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
159 rc = mds_quota_finvalidate(obd, oqctl);
162 CERROR("%s: unsupported mds_quotactl command: %d\n",
163 obd->obd_name, oqctl->qc_cmd);
168 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
169 "type %d, failed: rc = %d\n",
170 oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
171 do_gettimeofday(&work_end);
172 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
173 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
178 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
179 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) {
198 down(&obt->obt_quotachecking);
199 if (oqctl->qc_cmd == Q_FINVALIDATE &&
200 (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
201 CWARN("quota[%u] is on yet\n", oqctl->qc_type);
202 up(&obt->obt_quotachecking);
206 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
210 /* In recovery scenario, this pending dqacq/dqrel might have
211 * been processed by master successfully before it's dquot
212 * on master enter recovery mode. We must wait for this
213 * dqacq/dqrel done then return the correct limits to master */
214 if (oqctl->qc_stat == QUOTA_RECOVERING)
215 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
217 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
218 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
219 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
221 if (oqctl->qc_stat == QUOTA_RECOVERING)
222 quota_unbarrier(handle);
224 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
225 oqctl->qc_cmd == Q_FINVALIDATE) {
226 if (oqctl->qc_cmd == Q_QUOTAON) {
228 obt->obt_qctxt.lqc_flags |=
229 UGQUOTA2LQC(oqctl->qc_type);
230 else if (rc == -EBUSY &&
231 quota_is_on(qctxt, oqctl))
233 } else if (oqctl->qc_cmd == Q_QUOTAOFF) {
235 obt->obt_qctxt.lqc_flags &=
236 ~UGQUOTA2LQC(oqctl->qc_type);
237 else if (quota_is_off(qctxt, oqctl))
240 up(&obt->obt_quotachecking);
243 /* when quotaon, create lqs for every quota uid/gid b=18574 */
244 if (oqctl->qc_cmd == Q_QUOTAON)
248 /* currently, it is only used for nullifying the quota */
249 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
251 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
252 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
255 oqctl->qc_cmd = Q_SYNC;
256 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
257 oqctl->qc_cmd = Q_SETQUOTA;
259 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
260 quota_unbarrier(handle);
262 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
264 if (lqs == NULL || IS_ERR(lqs)){
265 CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
267 lqs->lqs_flags &= ~QB_SET;
274 unsigned int id[MAXQUOTAS] = { 0, 0 };
276 /* Initialize quota limit to MIN_QLIMIT */
277 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
278 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
280 if (!oqctl->qc_dqblk.dqb_bhardlimit)
283 /* There might be a pending dqacq/dqrel (which is going to
284 * clear stale limits on slave). we should wait for it's
285 * completion then initialize limits */
286 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
287 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
288 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
289 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
291 /* Update on-disk quota, in case of lose the changed limits
292 * (MIN_QLIMIT) on crash, which cannot be recovered.*/
294 oqctl->qc_cmd = Q_SYNC;
295 fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
296 oqctl->qc_cmd = Q_INITQUOTA;
298 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
299 quota_unbarrier(handle);
304 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
306 if (lqs == NULL || IS_ERR(lqs)){
307 CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
310 lqs->lqs_flags |= QB_SET;
314 /* Trigger qunit pre-acquire */
315 if (oqctl->qc_type == USRQUOTA)
316 id[USRQUOTA] = oqctl->qc_id;
318 id[GRPQUOTA] = oqctl->qc_id;
320 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
322 if (rc == -EDQUOT || rc == -EBUSY) {
323 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
330 CERROR("%s: unsupported filter_quotactl command: %d\n",
331 obd->obd_name, oqctl->qc_cmd);
334 do_gettimeofday(&work_end);
335 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
336 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
340 #endif /* __KERNEL__ */
343 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
344 struct obd_quotactl *oqctl)
346 struct ptlrpc_request *req;
347 struct obd_quotactl *oqc;
348 const struct req_format *rf;
352 if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
353 rf = &RQF_MDS_QUOTACTL;
354 ver = LUSTRE_MDS_VERSION,
356 } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
357 rf = &RQF_OST_QUOTACTL;
358 ver = LUSTRE_OST_VERSION,
364 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
368 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
371 ptlrpc_request_set_replen(req);
373 rc = ptlrpc_queue_wait(req);
375 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
381 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
384 CERROR ("Can't unpack obd_quotactl\n");
385 GOTO(out, rc = -EPROTO);
391 ptlrpc_req_finished(req);
396 * For lmv, only need to send request to master MDT, and the master MDT will
397 * process with other slave MDTs.
399 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
400 struct obd_quotactl *oqctl)
402 struct obd_device *obd = class_exp2obd(exp);
403 struct lmv_obd *lmv = &obd->u.lmv;
404 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
408 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
409 CERROR("master lmv inactive\n");
413 rc = obd_quotactl(tgt->ltd_exp, oqctl);
417 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
418 struct obd_quotactl *oqctl)
420 struct obd_device *obd = class_exp2obd(exp);
421 struct lov_obd *lov = &obd->u.lov;
422 struct lov_tgt_desc *tgt;
424 __u64 bhardlimit = 0;
428 if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
429 oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
430 oqctl->qc_cmd != Q_GETOQUOTA &&
431 oqctl->qc_cmd != Q_INITQUOTA &&
432 oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
433 oqctl->qc_cmd != Q_FINVALIDATE) {
434 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
440 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
443 tgt = lov->lov_tgts[i];
444 if (!tgt || !tgt->ltd_active || tgt->ltd_reap) {
445 if (oqctl->qc_cmd == Q_GETOQUOTA) {
447 CERROR("ost %d is inactive\n", i);
449 CDEBUG(D_HA, "ost %d is inactive\n", i);
454 err = obd_quotactl(tgt->ltd_exp, oqctl);
456 if (tgt->ltd_active && !rc)
461 if (oqctl->qc_cmd == Q_GETOQUOTA) {
462 curspace += oqctl->qc_dqblk.dqb_curspace;
463 bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
468 if (oqctl->qc_cmd == Q_GETOQUOTA) {
469 oqctl->qc_dqblk.dqb_curspace = curspace;
470 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;