1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/quota/quota_adjust_qunit.c
6 * Copyright (c) 2005 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * No redistribution or use is permitted outside of Cluster File Systems, Inc.
14 # define EXPORT_SYMTAB
16 #define DEBUG_SUBSYSTEM S_MDS
19 # include <linux/version.h>
20 # include <linux/module.h>
21 # include <linux/init.h>
22 # include <linux/fs.h>
23 # include <linux/jbd.h>
24 # include <linux/ext3_fs.h>
25 # include <linux/quota.h>
26 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
27 # include <linux/smp_lock.h>
28 # include <linux/buffer_head.h>
29 # include <linux/workqueue.h>
30 # include <linux/mount.h>
32 # include <linux/locks.h>
34 #else /* __KERNEL__ */
35 # include <liblustre.h>
38 #include <obd_class.h>
39 #include <lustre_mds.h>
40 #include <lustre_dlm.h>
41 #include <lustre_cfg.h>
43 #include <lustre_fsfilt.h>
44 #include <linux/lustre_quota.h>
45 #include <class_hash.h>
46 #include "quota_internal.h"
49 /* this function is charge of recording lqs_ino_rec and
50 * lqs_blk_rec. when a lquota slave checks a quota
51 * request(check_cur_qunit) and finishes a quota
52 * request(dqacq_completion), it will be called.
53 * is_chk: whether it is checking quota; otherwise, it is finishing
54 * is_acq: whether it is acquiring; otherwise, it is releasing
56 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
57 int is_chk, int is_acq)
61 LASSERT(qdata && lqs);
62 LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
63 is_blk = QDATA_IS_BLK(qdata);
68 lqs->lqs_blk_rec += qdata->qd_count;
70 lqs->lqs_ino_rec += qdata->qd_count;
73 lqs->lqs_blk_rec -= qdata->qd_count;
75 lqs->lqs_ino_rec -= qdata->qd_count;
80 lqs->lqs_blk_rec -= qdata->qd_count;
82 lqs->lqs_ino_rec -= qdata->qd_count;
85 lqs->lqs_blk_rec += qdata->qd_count;
87 lqs->lqs_ino_rec += qdata->qd_count;
92 void qdata_to_oqaq(struct qunit_data *qdata, struct quota_adjust_qunit *oqaq)
97 oqaq->qaq_flags = qdata->qd_flags;
98 oqaq->qaq_id = qdata->qd_id;
99 if (QDATA_IS_ADJBLK(qdata))
100 oqaq->qaq_bunit_sz = qdata->qd_qunit;
101 if (QDATA_IS_ADJINO(qdata))
102 oqaq->qaq_iunit_sz = qdata->qd_qunit;
105 int quota_search_lqs(struct qunit_data *qdata, struct quota_adjust_qunit *oqaq,
106 struct lustre_quota_ctxt *qctxt,
107 struct lustre_qunit_size **lqs_return)
109 struct quota_adjust_qunit *oqaq_tmp = NULL;
112 LASSERT(*lqs_return == NULL);
113 LASSERT(oqaq || qdata);
116 OBD_ALLOC_PTR(oqaq_tmp);
119 qdata_to_oqaq(qdata, oqaq_tmp);
124 *lqs_return = lustre_hash_get_object_by_key(LQC_HASH_BODY(qctxt),
127 LQS_DEBUG((*lqs_return), "show lqs\n");
130 OBD_FREE_PTR(oqaq_tmp);
134 int quota_create_lqs(struct qunit_data *qdata, struct quota_adjust_qunit *oqaq,
135 struct lustre_quota_ctxt *qctxt,
136 struct lustre_qunit_size **lqs_return)
139 struct quota_adjust_qunit *oqaq_tmp = NULL;
140 struct lustre_qunit_size *lqs = NULL;
143 LASSERT(*lqs_return == NULL);
144 LASSERT(oqaq || qdata);
147 OBD_ALLOC_PTR(oqaq_tmp);
150 qdata_to_oqaq(qdata, oqaq_tmp);
157 GOTO(out, rc = -ENOMEM);
159 spin_lock_init(&lqs->lqs_lock);
160 lqs->lqs_bwrite_pending = 0;
161 lqs->lqs_iwrite_pending = 0;
162 lqs->lqs_ino_rec = 0;
163 lqs->lqs_blk_rec = 0;
164 lqs->lqs_id = oqaq_tmp->qaq_id;
165 lqs->lqs_flags = QAQ_IS_GRP(oqaq_tmp);
166 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
167 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
168 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
169 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
170 if (qctxt->lqc_handler) {
171 lqs->lqs_last_bshrink = 0;
172 lqs->lqs_last_ishrink = 0;
175 rc = lustre_hash_additem_unique(LQC_HASH_BODY(qctxt),
176 oqaq_tmp, &lqs->lqs_hash);
177 LQS_DEBUG(lqs, "create lqs\n");
186 OBD_FREE_PTR(oqaq_tmp);
190 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
191 struct lustre_quota_ctxt *qctxt)
193 struct lustre_qunit_size *lqs = NULL;
194 unsigned long *lbunit, *liunit, *lbtune, *litune;
195 signed long b_tmp = 0, i_tmp = 0;
196 cfs_time_t time_limit = 0;
200 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
205 rc = quota_search_lqs(NULL, oqaq, qctxt, &lqs);
207 /* deleting the lqs, because a user sets lfs quota 0 0 0 0 */
208 if (!oqaq->qaq_bunit_sz && !oqaq->qaq_iunit_sz && QAQ_IS_ADJBLK(oqaq) &&
209 QAQ_IS_ADJINO(oqaq)) {
211 LQS_DEBUG(lqs, "release lqs\n");
212 /* this is for quota_search_lqs */
214 /* this is for deleting this lqs */
221 rc = quota_create_lqs(NULL, oqaq, qctxt, &lqs);
228 lbunit = &lqs->lqs_bunit_sz;
229 liunit = &lqs->lqs_iunit_sz;
230 lbtune = &lqs->lqs_btune_sz;
231 litune = &lqs->lqs_itune_sz;
233 spin_lock(&lqs->lqs_lock);
234 CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
235 /* adjust the slave's block qunit size */
236 if (QAQ_IS_ADJBLK(oqaq)) {
237 cfs_duration_t sec = cfs_time_seconds(qctxt->lqc_switch_seconds);
239 b_tmp = *lbunit - oqaq->qaq_bunit_sz;
241 if (qctxt->lqc_handler && b_tmp > 0)
242 lqs->lqs_last_bshrink = cfs_time_current();
244 if (qctxt->lqc_handler && b_tmp < 0) {
245 time_limit = cfs_time_add(lqs->lqs_last_bshrink, sec);
246 if (!lqs->lqs_last_bshrink ||
247 cfs_time_after(cfs_time_current(), time_limit)) {
248 *lbunit = oqaq->qaq_bunit_sz;
249 *lbtune = (*lbunit) / 2;
254 *lbunit = oqaq->qaq_bunit_sz;
255 *lbtune = (*lbunit) / 2;
259 /* adjust the slave's file qunit size */
260 if (QAQ_IS_ADJINO(oqaq)) {
261 i_tmp = *liunit - oqaq->qaq_iunit_sz;
263 if (qctxt->lqc_handler && i_tmp > 0)
264 lqs->lqs_last_ishrink = cfs_time_current();
266 if (qctxt->lqc_handler && i_tmp < 0) {
267 time_limit = cfs_time_add(lqs->lqs_last_ishrink,
268 cfs_time_seconds(qctxt->
269 lqc_switch_seconds));
270 if (!lqs->lqs_last_ishrink ||
271 cfs_time_after(cfs_time_current(), time_limit)) {
272 *liunit = oqaq->qaq_iunit_sz;
273 *litune = (*liunit) / 2;
278 *liunit = oqaq->qaq_iunit_sz;
279 *litune = (*liunit) / 2;
282 CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
283 spin_unlock(&lqs->lqs_lock);
288 rc |= LQS_BLK_DECREASE;
290 rc |= LQS_BLK_INCREASE;
293 rc |= LQS_INO_DECREASE;
295 rc |= LQS_INO_INCREASE;
300 int filter_quota_adjust_qunit(struct obd_export *exp,
301 struct quota_adjust_qunit *oqaq)
303 struct obd_device *obd = exp->exp_obd;
304 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
305 unsigned int uid = 0, gid = 0;
310 LASSERT(QAQ_IS_ADJBLK(oqaq));
311 rc = quota_adjust_slave_lqs(oqaq, qctxt);
313 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
316 if (QAQ_IS_GRP(oqaq))
322 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 0);
323 if (rc == -EDQUOT || rc == -EBUSY) {
324 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
328 CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
332 #endif /* __KERNEL__ */
334 int client_quota_adjust_qunit(struct obd_export *exp,
335 struct quota_adjust_qunit *oqaq)
337 struct ptlrpc_request *req;
338 struct quota_adjust_qunit *oqa;
339 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
343 /* client don't support this kind of operation, abort it */
344 if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)||
345 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
346 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
347 exp->exp_obd->obd_name);
350 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
353 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
354 OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
356 GOTO(out, rc = -ENOMEM);
358 oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
361 ptlrpc_req_set_repsize(req, 2, size);
363 rc = ptlrpc_queue_wait(req);
365 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
369 ptlrpc_req_finished(req);
374 int lov_quota_adjust_qunit(struct obd_export *exp,
375 struct quota_adjust_qunit *oqaq)
377 struct obd_device *obd = class_exp2obd(exp);
378 struct lov_obd *lov = &obd->u.lov;
382 if (!QAQ_IS_ADJBLK(oqaq)) {
383 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
387 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
390 if (!lov->lov_tgts[i]->ltd_active) {
391 CDEBUG(D_HA, "ost %d is inactive\n", i);
395 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq);
397 if (lov->lov_tgts[i]->ltd_active && !rc)