Whamcloud - gitweb
078876dc019c36da68b796c3bab3e1930bd069ea
[fs/lustre-release.git] / lustre / quota / quota_adjust_qunit.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/quota/quota_adjust_qunit.c
5  *
6  *  Copyright (c) 2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   No redistribution or use is permitted outside of Cluster File Systems, Inc.
11  *
12  */
13 #ifndef EXPORT_SYMTAB
14 # define EXPORT_SYMTAB
15 #endif
16 #define DEBUG_SUBSYSTEM S_MDS
17
18 #ifdef __KERNEL__
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>
31 # else
32 #  include <linux/locks.h>
33 # endif
34 #else /* __KERNEL__ */
35 # include <liblustre.h>
36 #endif
37
38 #include <obd_class.h>
39 #include <lustre_mds.h>
40 #include <lustre_dlm.h>
41 #include <lustre_cfg.h>
42 #include <obd_ost.h>
43 #include <lustre_fsfilt.h>
44 #include <linux/lustre_quota.h>
45 #include <class_hash.h>
46 #include "quota_internal.h"
47
48 #ifdef __KERNEL__
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
55  */
56 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
57                       int is_chk, int is_acq)
58 {
59         int is_blk;
60
61         LASSERT(qdata && lqs);
62         LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
63         is_blk = QDATA_IS_BLK(qdata);
64
65         if (is_chk) {
66                 if (is_acq) {
67                         if (is_blk)
68                                 lqs->lqs_blk_rec += qdata->qd_count;
69                         else
70                                 lqs->lqs_ino_rec += qdata->qd_count;
71                 } else {
72                         if (is_blk)
73                                 lqs->lqs_blk_rec -= qdata->qd_count;
74                         else
75                                 lqs->lqs_ino_rec -= qdata->qd_count;
76                 }
77         } else {
78                 if (is_acq) {
79                         if (is_blk)
80                                 lqs->lqs_blk_rec -= qdata->qd_count;
81                         else
82                                 lqs->lqs_ino_rec -= qdata->qd_count;
83                 } else {
84                         if (is_blk)
85                                 lqs->lqs_blk_rec += qdata->qd_count;
86                         else
87                                 lqs->lqs_ino_rec += qdata->qd_count;
88                 }
89         }
90 }
91
92 void qdata_to_oqaq(struct qunit_data *qdata,
93                    struct quota_adjust_qunit *oqaq)
94 {
95         LASSERT(qdata);
96         LASSERT(oqaq);
97
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;
104 }
105
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)
110 {
111         struct quota_adjust_qunit *oqaq_tmp = NULL;
112         ENTRY;
113
114         LASSERT(*lqs_return == NULL);
115         LASSERT(oqaq || qdata);
116
117         if (!oqaq) {
118                 OBD_ALLOC_PTR(oqaq_tmp);
119                 if (!oqaq_tmp)
120                         RETURN(-ENOMEM);
121                 qdata_to_oqaq(qdata, oqaq_tmp);
122         } else {
123                 oqaq_tmp = oqaq;
124         }
125
126         *lqs_return = lustre_hash_get_object_by_key(LQC_HASH_BODY(qctxt),
127                                                     oqaq_tmp);
128         if (*lqs_return)
129                 LQS_DEBUG((*lqs_return), "show lqs\n");
130
131         if (!oqaq)
132                 OBD_FREE_PTR(oqaq_tmp);
133         RETURN(0);
134 }
135
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)
140 {
141         int rc = 0;
142         struct quota_adjust_qunit *oqaq_tmp = NULL;
143         struct lustre_qunit_size *lqs = NULL;
144         ENTRY;
145
146         LASSERT(*lqs_return == NULL);
147         LASSERT(oqaq || qdata);
148
149         if (!oqaq) {
150                 OBD_ALLOC_PTR(oqaq_tmp);
151                 if (!oqaq_tmp)
152                         RETURN(-ENOMEM);
153                 qdata_to_oqaq(qdata, oqaq_tmp);
154         } else {
155                 oqaq_tmp = oqaq;
156         }
157
158         OBD_ALLOC_PTR(lqs);
159         if (!lqs)
160                 GOTO(out, rc = -ENOMEM);
161
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;
176         }
177         lqs_initref(lqs);
178         rc = lustre_hash_additem_unique(LQC_HASH_BODY(qctxt),
179                                         oqaq_tmp, &lqs->lqs_hash);
180         LQS_DEBUG(lqs, "create lqs\n");
181         if (!rc) {
182                 lqs_getref(lqs);
183                 *lqs_return = lqs;
184         }
185  out:
186         if (rc && lqs)
187                 OBD_FREE_PTR(lqs);
188         if (!oqaq)
189                 OBD_FREE_PTR(oqaq_tmp);
190         RETURN(rc);
191 }
192
193 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq, struct
194                           lustre_quota_ctxt *qctxt)
195 {
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;
200         int rc = 0;
201         ENTRY;
202
203         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
204                 RETURN(0);
205
206         LASSERT(qctxt);
207  search_lqs:
208         rc = quota_search_lqs(NULL, oqaq, qctxt, &lqs);
209
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)) {
213                 if (lqs) {
214                         LQS_DEBUG(lqs, "release lqs\n");
215                         /* this is for quota_search_lqs */
216                         lqs_putref(lqs);
217                         /* this is for deleting this lqs */
218                         lqs_putref(lqs);
219                 }
220                 RETURN(rc);
221         }
222
223         if (!lqs) {
224                 rc = quota_create_lqs(NULL, oqaq, qctxt, &lqs);
225                 if (rc == -EALREADY)
226                         goto search_lqs;
227                 if (rc < 0)
228                         RETURN(rc);
229         }
230
231         lbunit = &lqs->lqs_bunit_sz;
232         liunit = &lqs->lqs_iunit_sz;
233         lbtune = &lqs->lqs_btune_sz;
234         litune = &lqs->lqs_itune_sz;
235
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);
241
242                 b_tmp = *lbunit - oqaq->qaq_bunit_sz;
243
244                 if (qctxt->lqc_handler && b_tmp > 0)
245                         lqs->lqs_last_bshrink = cfs_time_current();
246
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;
253                         } else {
254                                 b_tmp = 0;
255                         }
256                 } else {
257                         *lbunit = oqaq->qaq_bunit_sz;
258                         *lbtune = (*lbunit) / 2;
259                 }
260         }
261
262         /* adjust the slave's file qunit size */
263         if (QAQ_IS_ADJINO(oqaq)) {
264                 i_tmp = *liunit - oqaq->qaq_iunit_sz;
265
266                 if (qctxt->lqc_handler && i_tmp > 0)
267                         lqs->lqs_last_ishrink  = cfs_time_current();
268
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;
277                         } else {
278                                 i_tmp = 0;
279                         }
280                 } else {
281                         *liunit = oqaq->qaq_iunit_sz;
282                         *litune = (*liunit) / 2;
283                 }
284         }
285         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
286         spin_unlock(&lqs->lqs_lock);
287
288         lqs_putref(lqs);
289
290         if (b_tmp > 0)
291                 rc |= LQS_BLK_DECREASE;
292         else if (b_tmp < 0)
293                 rc |= LQS_BLK_INCREASE;
294
295         if (i_tmp > 0)
296                 rc |= LQS_INO_DECREASE;
297         else if (i_tmp < 0)
298                 rc |= LQS_INO_INCREASE;
299
300         RETURN(rc);
301 }
302
303 int filter_quota_adjust_qunit(struct obd_export *exp, struct
304                               quota_adjust_qunit *oqaq)
305 {
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;
309         int rc = 0;
310         ENTRY;
311
312         LASSERT(oqaq);
313         LASSERT(QAQ_IS_ADJBLK(oqaq));
314         rc = quota_adjust_slave_lqs(oqaq, qctxt);
315         if (rc < 0) {
316                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
317                 RETURN(rc);
318         }
319         if (QAQ_IS_GRP(oqaq))
320                 gid = oqaq->qaq_id;
321         else
322                 uid = oqaq->qaq_id;
323
324         if (rc > 0) {
325                 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 0);
326                 if (rc)
327                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
328         }
329         RETURN(rc);
330 }
331 #endif /* __KERNEL__ */
332
333 int client_quota_adjust_qunit(struct obd_export *exp, struct
334                               quota_adjust_qunit *oqaq)
335 {
336         struct ptlrpc_request *req;
337         struct quota_adjust_qunit *oqa;
338         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
339         int rc = 0;
340         ENTRY;
341
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);
347                 RETURN(rc);
348         }
349         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
350                 RETURN(-EINVAL);
351
352         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
353                               OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
354         if (!req)
355                 GOTO(out, rc = -ENOMEM);
356
357         oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
358         *oqa = *oqaq;
359
360         ptlrpc_req_set_repsize(req, 2, size);
361
362         rc = ptlrpc_queue_wait(req);
363         if (rc) {
364                 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
365                        __FUNCTION__, rc);
366                 GOTO(out, rc);
367         }
368         ptlrpc_req_finished(req);
369 out:
370         RETURN (rc);
371 }
372
373 int lov_quota_adjust_qunit(struct obd_export *exp, struct
374                            quota_adjust_qunit *oqaq)
375 {
376         struct obd_device *obd = class_exp2obd(exp);
377         struct lov_obd *lov = &obd->u.lov;
378         int i, rc = 0;
379         ENTRY;
380
381         if (!QAQ_IS_ADJBLK(oqaq)) {
382                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
383                 RETURN(-EFAULT);
384         }
385
386         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
387                 int err;
388
389                 if (!lov->lov_tgts[i]->ltd_active) {
390                         CDEBUG(D_HA, "ost %d is inactive\n", i);
391                         continue;
392                 }
393
394                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq);
395                 if (err) {
396                         if (lov->lov_tgts[i]->ltd_active && !rc)
397                                 rc = err;
398                         continue;
399                 }
400         }
401         RETURN(rc);
402 }