Whamcloud - gitweb
LU-1415 tests: Handle OFD procfs changes
[fs/lustre-release.git] / lustre / quota / quota_adjust_qunit.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LQUOTA
38
39 #ifdef __KERNEL__
40 # include <linux/version.h>
41 # include <linux/module.h>
42 # include <linux/init.h>
43 # include <linux/fs.h>
44 # include <linux/jbd.h>
45 # include <linux/quota.h>
46 # include <linux/smp_lock.h>
47 # include <linux/buffer_head.h>
48 # include <linux/workqueue.h>
49 # include <linux/mount.h>
50 #else /* __KERNEL__ */
51 # include <liblustre.h>
52 #endif
53
54 #include <obd_class.h>
55 #include <lustre_mds.h>
56 #include <lustre_dlm.h>
57 #include <lustre_cfg.h>
58 #include <obd_ost.h>
59 #include <lustre_fsfilt.h>
60 #include <linux/lustre_quota.h>
61 #include "quota_internal.h"
62
63 #ifdef __KERNEL__
64 /**
65  * This function is charge of recording lqs_ino_rec and
66  * lqs_blk_rec. when a lquota slave checks a quota
67  * request(check_cur_qunit) and finishes a quota
68  * request(dqacq_completion), it will be called.
69  * is_chk: whether it is checking quota; otherwise, it is finishing
70  * is_acq: whether it is acquiring; otherwise, it is releasing
71  */
72 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
73                        int is_chk, int is_acq)
74 {
75         long long *rec;
76
77         LASSERT(qdata && lqs);
78         LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
79
80         rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
81
82         if (!!is_chk + !!is_acq == 1)
83                 *rec -= qdata->qd_count;
84         else
85                 *rec += qdata->qd_count;
86
87 }
88
89 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
90                                            struct lustre_quota_ctxt *qctxt,
91                                            int create)
92 {
93         struct lustre_qunit_size *lqs;
94         struct lustre_qunit_size *lqs2;
95         cfs_hash_t *hs = NULL;
96         int rc = 0;
97
98         cfs_spin_lock(&qctxt->lqc_lock);
99         if (qctxt->lqc_valid) {
100                 LASSERT(qctxt->lqc_lqs_hash != NULL);
101                 hs = cfs_hash_getref(qctxt->lqc_lqs_hash);
102         }
103         cfs_spin_unlock(&qctxt->lqc_lock);
104
105         if (hs == NULL) {
106                 rc = -EBUSY;
107                 goto out;
108         }
109
110         /* cfs_hash_lookup will +1 refcount for caller */
111         lqs = cfs_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
112         if (lqs != NULL) /* found */
113                 goto out_put;
114
115         if (!create)
116                 goto out_put;
117
118         OBD_ALLOC_PTR(lqs);
119         if (!lqs) {
120                 rc = -ENOMEM;
121                 goto out_put;
122         }
123
124         lqs->lqs_key = lqs_key;
125
126         cfs_spin_lock_init(&lqs->lqs_lock);
127
128         lqs->lqs_bwrite_pending = 0;
129         lqs->lqs_iwrite_pending = 0;
130         lqs->lqs_ino_rec = 0;
131         lqs->lqs_blk_rec = 0;
132         lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
133         lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
134         lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
135         lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
136         lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
137         lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
138         if (qctxt->lqc_handler) {
139                 lqs->lqs_last_bshrink  = 0;
140                 lqs->lqs_last_ishrink  = 0;
141         }
142
143         lqs->lqs_ctxt = qctxt; /* must be called before lqs_initref */
144         cfs_atomic_set(&lqs->lqs_refcount, 1); /* 1 for caller */
145         cfs_atomic_inc(&lqs->lqs_ctxt->lqc_lqs);
146
147         /* lqc_lqs_hash will take +1 refcount on lqs on adding */
148         lqs2 = cfs_hash_findadd_unique(qctxt->lqc_lqs_hash,
149                                        &lqs->lqs_key, &lqs->lqs_hash);
150         if (lqs2 == lqs) /* added to hash */
151                 goto out_put;
152
153         create = 0;
154         lqs_putref(lqs);
155         lqs = lqs2;
156
157  out_put:
158         cfs_hash_putref(hs);
159  out:
160         if (rc != 0) { /* error */
161                 CERROR("get lqs error(rc: %d)\n", rc);
162                 return ERR_PTR(rc);
163         }
164
165         if (lqs != NULL) {
166                 LQS_DEBUG(lqs, "%s\n",
167                           (create == 1 ? "create lqs" : "search lqs"));
168         }
169         return lqs;
170 }
171
172 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
173                            struct lustre_quota_ctxt *qctxt)
174 {
175         struct lustre_qunit_size *lqs = NULL;
176         unsigned long *unit, *tune;
177         signed long tmp = 0;
178         cfs_time_t time_limit = 0, *shrink;
179         int i, rc = 0;
180         ENTRY;
181
182         LASSERT(qctxt);
183         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
184                                qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
185         if (lqs == NULL || IS_ERR(lqs)){
186                 CERROR("fail to find a lqs for %sid %u!\n",
187                        QAQ_IS_GRP(oqaq) ? "g" : "u", oqaq->qaq_id);
188                 RETURN(PTR_ERR(lqs));
189         }
190
191         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
192                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
193         cfs_spin_lock(&lqs->lqs_lock);
194         for (i = 0; i < 2; i++) {
195                 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
196                         continue;
197
198                 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
199                         continue;
200
201                 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
202                           (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
203                 shrink = i ? &lqs->lqs_last_ishrink :
204                              &lqs->lqs_last_bshrink;
205                 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
206                                               lqs->lqs_last_bshrink,
207                                    cfs_time_seconds(qctxt->lqc_switch_seconds));
208                 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
209                 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
210
211                 /* quota master shrinks */
212                 if (qctxt->lqc_handler && tmp > 0)
213                         *shrink = cfs_time_current();
214
215                 /* quota master enlarges */
216                 if (qctxt->lqc_handler && tmp < 0) {
217                         /* in case of ping-pong effect, don't enlarge lqs
218                          * in a short time */
219                         if (*shrink &&
220                             cfs_time_before(cfs_time_current(), time_limit))
221                                 tmp = 0;
222                 }
223
224                 /* when setquota, don't enlarge lqs b=18616 */
225                 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
226                         tmp = 0;
227
228                 if (tmp != 0) {
229                         *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
230                         *tune = (*unit) / 2;
231                 }
232
233
234                 if (tmp > 0)
235                         rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
236                 if (tmp < 0)
237                         rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
238         }
239         cfs_spin_unlock(&lqs->lqs_lock);
240         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
241                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
242
243         lqs_putref(lqs);
244
245         RETURN(rc);
246 }
247
248 int filter_quota_adjust_qunit(struct obd_export *exp,
249                               struct quota_adjust_qunit *oqaq,
250                               struct lustre_quota_ctxt *qctxt,
251                               struct ptlrpc_request_set *rqset)
252 {
253         struct obd_device *obd = exp->exp_obd;
254         unsigned int id[MAXQUOTAS] = { 0, 0 };
255         int rc = 0;
256         ENTRY;
257
258         LASSERT(oqaq);
259         LASSERT(QAQ_IS_ADJBLK(oqaq));
260         rc = quota_adjust_slave_lqs(oqaq, qctxt);
261         if (rc < 0) {
262                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
263                 RETURN(rc);
264         }
265         if (QAQ_IS_GRP(oqaq))
266                 id[GRPQUOTA] = oqaq->qaq_id;
267         else
268                 id[USRQUOTA] = oqaq->qaq_id;
269
270         if (rc > 0) {
271                 rc = qctxt_adjust_qunit(obd, qctxt, id, 1, 0, NULL);
272                 if (rc == -EDQUOT || rc == -EBUSY ||
273                     rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
274                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
275                         rc = 0;
276                 }
277                 if (rc)
278                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
279         }
280         RETURN(rc);
281 }
282 #endif /* __KERNEL__ */