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,
93 struct quota_adjust_qunit *oqaq)
98 oqaq->qaq_flags = qdata->qd_flags;
99 oqaq->qaq_id = qdata->qd_id;
100 if (QDATA_IS_ADJBLK(qdata))
101 oqaq->qaq_bunit_sz = qdata->qd_qunit;
102 if (QDATA_IS_ADJINO(qdata))
103 oqaq->qaq_iunit_sz = qdata->qd_qunit;
106 int quota_search_lqs(struct qunit_data *qdata,
107 struct quota_adjust_qunit *oqaq,
108 struct lustre_quota_ctxt *qctxt,
109 struct lustre_qunit_size **lqs_return)
111 struct quota_adjust_qunit *oqaq_tmp = NULL;
114 LASSERT(*lqs_return == NULL);
115 LASSERT(oqaq || qdata);
118 OBD_ALLOC_PTR(oqaq_tmp);
121 qdata_to_oqaq(qdata, oqaq_tmp);
126 *lqs_return = lustre_hash_get_object_by_key(LQC_HASH_BODY(qctxt),
129 LQS_DEBUG((*lqs_return), "show lqs\n");
132 OBD_FREE_PTR(oqaq_tmp);
136 int quota_create_lqs(struct qunit_data *qdata,
137 struct quota_adjust_qunit *oqaq,
138 struct lustre_quota_ctxt *qctxt,
139 struct lustre_qunit_size **lqs_return)
142 struct quota_adjust_qunit *oqaq_tmp = NULL;
143 struct lustre_qunit_size *lqs = NULL;
146 LASSERT(*lqs_return == NULL);
147 LASSERT(oqaq || qdata);
150 OBD_ALLOC_PTR(oqaq_tmp);
153 qdata_to_oqaq(qdata, oqaq_tmp);
160 GOTO(out, rc = -ENOMEM);
162 spin_lock_init(&lqs->lqs_lock);
163 lqs->lqs_bwrite_pending = 0;
164 lqs->lqs_iwrite_pending = 0;
165 lqs->lqs_ino_rec = 0;
166 lqs->lqs_blk_rec = 0;
167 lqs->lqs_id = oqaq_tmp->qaq_id;
168 lqs->lqs_flags = QAQ_IS_GRP(oqaq_tmp);
169 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
170 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
171 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
172 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
173 if (qctxt->lqc_handler) {
174 lqs->lqs_last_bshrink = 0;
175 lqs->lqs_last_ishrink = 0;
178 rc = lustre_hash_additem_unique(LQC_HASH_BODY(qctxt),
179 oqaq_tmp, &lqs->lqs_hash);
180 LQS_DEBUG(lqs, "create lqs\n");
189 OBD_FREE_PTR(oqaq_tmp);
193 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq, struct
194 lustre_quota_ctxt *qctxt)
196 struct lustre_qunit_size *lqs = NULL;
197 unsigned long *lbunit, *liunit, *lbtune, *litune;
198 signed long b_tmp = 0, i_tmp = 0;
199 static cfs_time_t time_limit = 0;
203 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
208 rc = quota_search_lqs(NULL, oqaq, qctxt, &lqs);
210 /* deleting the lqs, because a user sets lfs quota 0 0 0 0 */
211 if (!oqaq->qaq_bunit_sz && !oqaq->qaq_iunit_sz && QAQ_IS_ADJBLK(oqaq) &&
212 QAQ_IS_ADJINO(oqaq)) {
214 LQS_DEBUG(lqs, "release lqs\n");
215 /* this is for quota_search_lqs */
217 /* this is for deleting this lqs */
224 rc = quota_create_lqs(NULL, oqaq, qctxt, &lqs);
231 lbunit = &lqs->lqs_bunit_sz;
232 liunit = &lqs->lqs_iunit_sz;
233 lbtune = &lqs->lqs_btune_sz;
234 litune = &lqs->lqs_itune_sz;
236 spin_lock(&lqs->lqs_lock);
237 CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
238 /* adjust the slave's block qunit size */
239 if (QAQ_IS_ADJBLK(oqaq)) {
240 cfs_duration_t sec = cfs_time_seconds(qctxt->lqc_switch_seconds);
242 b_tmp = *lbunit - oqaq->qaq_bunit_sz;
244 if (qctxt->lqc_handler && b_tmp > 0)
245 lqs->lqs_last_bshrink = cfs_time_current();
247 if (qctxt->lqc_handler && b_tmp < 0) {
248 time_limit = cfs_time_add(lqs->lqs_last_bshrink, sec);
249 if (!lqs->lqs_last_bshrink ||
250 cfs_time_after(cfs_time_current(), time_limit)) {
251 *lbunit = oqaq->qaq_bunit_sz;
252 *lbtune = (*lbunit) / 2;
257 *lbunit = oqaq->qaq_bunit_sz;
258 *lbtune = (*lbunit) / 2;
262 /* adjust the slave's file qunit size */
263 if (QAQ_IS_ADJINO(oqaq)) {
264 i_tmp = *liunit - oqaq->qaq_iunit_sz;
266 if (qctxt->lqc_handler && i_tmp > 0)
267 lqs->lqs_last_ishrink = cfs_time_current();
269 if (qctxt->lqc_handler && i_tmp < 0) {
270 time_limit = cfs_time_add(lqs->lqs_last_ishrink,
271 cfs_time_seconds(qctxt->
272 lqc_switch_seconds));
273 if (!lqs->lqs_last_ishrink ||
274 cfs_time_after(cfs_time_current(), time_limit)) {
275 *liunit = oqaq->qaq_iunit_sz;
276 *litune = (*liunit) / 2;
281 *liunit = oqaq->qaq_iunit_sz;
282 *litune = (*liunit) / 2;
285 CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
286 spin_unlock(&lqs->lqs_lock);
291 rc |= LQS_BLK_DECREASE;
293 rc |= LQS_BLK_INCREASE;
296 rc |= LQS_INO_DECREASE;
298 rc |= LQS_INO_INCREASE;
303 int filter_quota_adjust_qunit(struct obd_export *exp, struct
304 quota_adjust_qunit *oqaq)
306 struct obd_device *obd = exp->exp_obd;
307 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
308 unsigned int uid = 0, gid = 0;
313 LASSERT(QAQ_IS_ADJBLK(oqaq));
314 rc = quota_adjust_slave_lqs(oqaq, qctxt);
316 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
319 if (QAQ_IS_GRP(oqaq))
325 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 0);
327 CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
331 #endif /* __KERNEL__ */
333 int client_quota_adjust_qunit(struct obd_export *exp, struct
334 quota_adjust_qunit *oqaq)
336 struct ptlrpc_request *req;
337 struct quota_adjust_qunit *oqa;
338 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
342 /* client don't support this kind of operation, abort it */
343 if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)||
344 OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
345 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
346 exp->exp_obd->obd_name);
349 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
352 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
353 OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
355 GOTO(out, rc = -ENOMEM);
357 oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
360 ptlrpc_req_set_repsize(req, 2, size);
362 rc = ptlrpc_queue_wait(req);
364 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
368 ptlrpc_req_finished(req);
373 int lov_quota_adjust_qunit(struct obd_export *exp, struct
374 quota_adjust_qunit *oqaq)
376 struct obd_device *obd = class_exp2obd(exp);
377 struct lov_obd *lov = &obd->u.lov;
381 if (!QAQ_IS_ADJBLK(oqaq)) {
382 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
386 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
389 if (!lov->lov_tgts[i]->ltd_active) {
390 CDEBUG(D_HA, "ost %d is inactive\n", i);
394 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq);
396 if (lov->lov_tgts[i]->ltd_active && !rc)