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 <linux/lustre_quota.h>
63 #include <class_hash.h>
64 #include "quota_internal.h"
66 #ifdef HAVE_QUOTA_SUPPORT
69 /* this function is charge of recording lqs_ino_rec and
70 * lqs_blk_rec. when a lquota slave checks a quota
71 * request(check_cur_qunit) and finishes a quota
72 * request(dqacq_completion), it will be called.
73 * is_chk: whether it is checking quota; otherwise, it is finishing
74 * is_acq: whether it is acquiring; otherwise, it is releasing
76 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
77 int is_chk, int is_acq)
81 LASSERT(qdata && lqs);
82 LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
84 rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
86 if (!!is_chk + !!is_acq == 1)
87 *rec -= qdata->qd_count;
89 *rec += qdata->qd_count;
93 static struct lustre_qunit_size *
94 quota_create_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt)
96 struct lustre_qunit_size *lqs = NULL;
101 GOTO(out, rc = -ENOMEM);
103 lqs->lqs_key = lqs_key;
105 spin_lock_init(&lqs->lqs_lock);
106 lqs->lqs_bwrite_pending = 0;
107 lqs->lqs_iwrite_pending = 0;
108 lqs->lqs_ino_rec = 0;
109 lqs->lqs_blk_rec = 0;
110 lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
111 lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
112 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
113 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
114 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
115 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
116 lqs->lqs_ctxt = qctxt;
117 if (qctxt->lqc_handler) {
118 lqs->lqs_last_bshrink = 0;
119 lqs->lqs_last_ishrink = 0;
123 spin_lock(&qctxt->lqc_lock);
124 if (!qctxt->lqc_valid)
127 rc = lustre_hash_add_unique(qctxt->lqc_lqs_hash,
128 &lqs->lqs_key, &lqs->lqs_hash);
129 spin_unlock(&qctxt->lqc_lock);
144 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
145 struct lustre_quota_ctxt *qctxt,
148 struct lustre_qunit_size *lqs;
152 lqs = lustre_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
154 GOTO(out, rc = PTR_ERR(lqs));
156 if (create && lqs == NULL) {
157 /* if quota_create_lqs is successful, it will get a
158 * ref to the lqs. The ref will be released when
159 * qctxt_cleanup() or quota is nullified */
160 lqs = quota_create_lqs(lqs_key, qctxt);
164 GOTO(search_lqs, rc = 0);
165 /* get a reference for the caller when creating lqs
172 LQS_DEBUG(lqs, "%s\n",
173 (create == 1 ? "create lqs" : "search lqs"));
179 CDEBUG(D_ERROR, "get lqs error(rc: %d)\n", rc);
184 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
185 struct lustre_quota_ctxt *qctxt)
187 struct lustre_qunit_size *lqs = NULL;
188 unsigned long *lbunit, *liunit, *lbtune, *litune;
189 signed long b_tmp = 0, i_tmp = 0;
190 cfs_time_t time_limit = 0;
195 lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
197 if (lqs == NULL || IS_ERR(lqs)){
198 CDEBUG(D_ERROR, "fail to find a lqs(%s id: %u)!\n",
199 QAQ_IS_GRP(oqaq) ? "group" : "user", oqaq->qaq_id);
200 RETURN(PTR_ERR(lqs));
203 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
204 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
205 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
206 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
207 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
212 lbunit = &lqs->lqs_bunit_sz;
213 liunit = &lqs->lqs_iunit_sz;
214 lbtune = &lqs->lqs_btune_sz;
215 litune = &lqs->lqs_itune_sz;
217 spin_lock(&lqs->lqs_lock);
218 CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
219 /* adjust the slave's block qunit size */
220 if (QAQ_IS_ADJBLK(oqaq)) {
221 cfs_duration_t sec = cfs_time_seconds(qctxt->lqc_switch_seconds);
223 b_tmp = *lbunit - oqaq->qaq_bunit_sz;
225 if (qctxt->lqc_handler && b_tmp > 0)
226 lqs->lqs_last_bshrink = cfs_time_current();
228 if (qctxt->lqc_handler && b_tmp < 0) {
229 time_limit = cfs_time_add(lqs->lqs_last_bshrink, sec);
230 if (!lqs->lqs_last_bshrink ||
231 cfs_time_after(cfs_time_current(), time_limit)) {
232 *lbunit = oqaq->qaq_bunit_sz;
233 *lbtune = (*lbunit) / 2;
238 *lbunit = oqaq->qaq_bunit_sz;
239 *lbtune = (*lbunit) / 2;
243 /* adjust the slave's file qunit size */
244 if (QAQ_IS_ADJINO(oqaq)) {
245 i_tmp = *liunit - oqaq->qaq_iunit_sz;
247 if (qctxt->lqc_handler && i_tmp > 0)
248 lqs->lqs_last_ishrink = cfs_time_current();
250 if (qctxt->lqc_handler && i_tmp < 0) {
251 time_limit = cfs_time_add(lqs->lqs_last_ishrink,
252 cfs_time_seconds(qctxt->
253 lqc_switch_seconds));
254 if (!lqs->lqs_last_ishrink ||
255 cfs_time_after(cfs_time_current(), time_limit)) {
256 *liunit = oqaq->qaq_iunit_sz;
257 *litune = (*liunit) / 2;
262 *liunit = oqaq->qaq_iunit_sz;
263 *litune = (*liunit) / 2;
266 CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
267 spin_unlock(&lqs->lqs_lock);
271 rc |= LQS_BLK_DECREASE;
273 rc |= LQS_BLK_INCREASE;
276 rc |= LQS_INO_DECREASE;
278 rc |= LQS_INO_INCREASE;
283 int filter_quota_adjust_qunit(struct obd_export *exp,
284 struct quota_adjust_qunit *oqaq,
285 struct lustre_quota_ctxt *qctxt)
287 struct obd_device *obd = exp->exp_obd;
288 unsigned int uid = 0, gid = 0;
293 LASSERT(QAQ_IS_ADJBLK(oqaq));
294 rc = quota_adjust_slave_lqs(oqaq, qctxt);
296 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
299 if (QAQ_IS_GRP(oqaq))
305 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 0, NULL);
306 if (rc == -EDQUOT || rc == -EBUSY ||
307 rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
308 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
312 CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
316 #endif /* __KERNEL__ */
319 int client_quota_adjust_qunit(struct obd_export *exp,
320 struct quota_adjust_qunit *oqaq,
321 struct lustre_quota_ctxt *qctxt)
323 struct ptlrpc_request *req;
324 struct quota_adjust_qunit *oqa;
325 __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
329 /* client don't support this kind of operation, abort it */
330 if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)||
331 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
332 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
333 exp->exp_obd->obd_name);
336 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
339 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
340 OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
342 GOTO(out, rc = -ENOMEM);
344 oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
347 ptlrpc_req_set_repsize(req, 2, size);
349 rc = ptlrpc_queue_wait(req);
351 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
355 ptlrpc_req_finished(req);
360 int lov_quota_adjust_qunit(struct obd_export *exp,
361 struct quota_adjust_qunit *oqaq,
362 struct lustre_quota_ctxt *qctxt)
364 struct obd_device *obd = class_exp2obd(exp);
365 struct lov_obd *lov = &obd->u.lov;
369 if (!QAQ_IS_ADJBLK(oqaq)) {
370 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
374 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
377 if (!lov->lov_tgts[i]->ltd_active) {
378 CDEBUG(D_HA, "ost %d is inactive\n", i);
382 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
385 if (lov->lov_tgts[i]->ltd_active && !rc)