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 <linux/lustre_quota.h>
62 #include <class_hash.h>
63 #include "quota_internal.h"
65 #ifdef HAVE_QUOTA_SUPPORT
68 /* this function is charge of recording lqs_ino_rec and
69 * lqs_blk_rec. when a lquota slave checks a quota
70 * request(check_cur_qunit) and finishes a quota
71 * request(dqacq_completion), it will be called.
72 * is_chk: whether it is checking quota; otherwise, it is finishing
73 * is_acq: whether it is acquiring; otherwise, it is releasing
75 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
76 int is_chk, int is_acq)
80 LASSERT(qdata && lqs);
81 LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
83 rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
85 if (!!is_chk + !!is_acq == 1)
86 *rec -= qdata->qd_count;
88 *rec += qdata->qd_count;
92 static struct lustre_qunit_size *
93 quota_create_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt)
95 struct lustre_qunit_size *lqs = NULL;
100 GOTO(out, rc = -ENOMEM);
102 lqs->lqs_key = lqs_key;
104 spin_lock_init(&lqs->lqs_lock);
105 lqs->lqs_bwrite_pending = 0;
106 lqs->lqs_iwrite_pending = 0;
107 lqs->lqs_ino_rec = 0;
108 lqs->lqs_blk_rec = 0;
109 lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
110 lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
111 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
112 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
113 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
114 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
115 lqs->lqs_ctxt = qctxt;
116 if (qctxt->lqc_handler) {
117 lqs->lqs_last_bshrink = 0;
118 lqs->lqs_last_ishrink = 0;
122 spin_lock(&qctxt->lqc_lock);
123 if (!qctxt->lqc_valid)
126 rc = lustre_hash_add_unique(qctxt->lqc_lqs_hash,
127 &lqs->lqs_key, &lqs->lqs_hash);
128 spin_unlock(&qctxt->lqc_lock);
143 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
144 struct lustre_quota_ctxt *qctxt,
147 struct lustre_qunit_size *lqs;
151 lqs = lustre_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
153 GOTO(out, rc = PTR_ERR(lqs));
155 if (create && lqs == NULL) {
156 /* if quota_create_lqs is successful, it will get a
157 * ref to the lqs. The ref will be released when
158 * qctxt_cleanup() or quota is nullified */
159 lqs = quota_create_lqs(lqs_key, qctxt);
163 GOTO(search_lqs, rc = 0);
164 /* get a reference for the caller when creating lqs
171 LQS_DEBUG(lqs, "%s\n",
172 (create == 1 ? "create lqs" : "search lqs"));
178 CDEBUG(D_ERROR, "get lqs error(rc: %d)\n", rc);
183 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
184 struct lustre_quota_ctxt *qctxt)
186 struct lustre_qunit_size *lqs = NULL;
187 unsigned long *unit, *tune;
189 cfs_time_t time_limit = 0, *shrink;
194 lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
195 qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
196 if (lqs == NULL || IS_ERR(lqs)){
197 CDEBUG(D_ERROR, "fail to find a lqs(%s id: %u)!\n",
198 QAQ_IS_GRP(oqaq) ? "group" : "user", oqaq->qaq_id);
199 RETURN(PTR_ERR(lqs));
202 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
203 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
204 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
205 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
206 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
211 CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
212 lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
213 spin_lock(&lqs->lqs_lock);
214 for (i = 0; i < 2; i++) {
215 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
218 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
221 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
222 (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
223 shrink = i ? &lqs->lqs_last_ishrink :
224 &lqs->lqs_last_bshrink;
225 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
226 lqs->lqs_last_bshrink,
227 cfs_time_seconds(qctxt->lqc_switch_seconds));
228 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
229 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
231 /* quota master shrinks */
232 if (qctxt->lqc_handler && tmp > 0)
233 *shrink = cfs_time_current();
235 /* quota master enlarges */
236 if (qctxt->lqc_handler && tmp < 0) {
237 /* in case of ping-pong effect, don't enlarge lqs
240 cfs_time_before(cfs_time_current(), time_limit))
244 /* when setquota, don't enlarge lqs b=18616 */
245 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
249 *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
255 rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
257 rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
259 spin_unlock(&lqs->lqs_lock);
260 CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
261 lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
268 int filter_quota_adjust_qunit(struct obd_export *exp,
269 struct quota_adjust_qunit *oqaq,
270 struct lustre_quota_ctxt *qctxt)
272 struct obd_device *obd = exp->exp_obd;
273 unsigned int uid = 0, gid = 0;
278 LASSERT(QAQ_IS_ADJBLK(oqaq));
279 rc = quota_adjust_slave_lqs(oqaq, qctxt);
281 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
284 if (QAQ_IS_GRP(oqaq))
290 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 0, NULL);
291 if (rc == -EDQUOT || rc == -EBUSY ||
292 rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
293 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
297 CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
301 #endif /* __KERNEL__ */
304 int client_quota_adjust_qunit(struct obd_export *exp,
305 struct quota_adjust_qunit *oqaq,
306 struct lustre_quota_ctxt *qctxt)
308 struct ptlrpc_request *req;
309 struct quota_adjust_qunit *oqa;
310 __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
314 /* client don't support this kind of operation, abort it */
315 if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)||
316 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
317 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
318 exp->exp_obd->obd_name);
321 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
324 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
325 OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
327 GOTO(out, rc = -ENOMEM);
329 oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
332 ptlrpc_req_set_repsize(req, 2, size);
334 rc = ptlrpc_queue_wait(req);
336 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
340 ptlrpc_req_finished(req);
345 int lov_quota_adjust_qunit(struct obd_export *exp,
346 struct quota_adjust_qunit *oqaq,
347 struct lustre_quota_ctxt *qctxt)
349 struct obd_device *obd = class_exp2obd(exp);
350 struct lov_obd *lov = &obd->u.lov;
354 if (!QAQ_IS_ADJBLK(oqaq)) {
355 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
359 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
362 if (!lov->lov_tgts[i]->ltd_active) {
363 CDEBUG(D_HA, "ost %d is inactive\n", i);
367 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
370 if (lov->lov_tgts[i]->ltd_active && !rc)