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