Whamcloud - gitweb
45f8c388a65a4ef493eac0e9b694dc0cb8d9f1c6
[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 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_LQUOTA
40
41 #ifdef __KERNEL__
42 # include <linux/version.h>
43 # include <linux/module.h>
44 # include <linux/init.h>
45 # include <linux/fs.h>
46 # include <linux/jbd.h>
47 # include <linux/quota.h>
48 # include <linux/smp_lock.h>
49 # include <linux/buffer_head.h>
50 # include <linux/workqueue.h>
51 # include <linux/mount.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 #endif
55
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lustre_dlm.h>
59 #include <lustre_cfg.h>
60 #include <obd_ost.h>
61 #include <lustre_fsfilt.h>
62 #include <linux/lustre_quota.h>
63 #include "quota_internal.h"
64
65 #ifdef HAVE_QUOTA_SUPPORT
66
67 #ifdef __KERNEL__
68 /**
69  * This function is charge of recording lqs_ino_rec and
70  * lqs_blk_rec. when a lquota slave checks a quota
71  * request(check_cur_qunit) and finishes a quota
72  * request(dqacq_completion), it will be called.
73  * is_chk: whether it is checking quota; otherwise, it is finishing
74  * is_acq: whether it is acquiring; otherwise, it is releasing
75  */
76 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
77                        int is_chk, int is_acq)
78 {
79         long long *rec;
80
81         LASSERT(qdata && lqs);
82         LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
83
84         rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
85
86         if (!!is_chk + !!is_acq == 1)
87                 *rec -= qdata->qd_count;
88         else
89                 *rec += qdata->qd_count;
90
91 }
92
93 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
94                                            struct lustre_quota_ctxt *qctxt,
95                                            int create)
96 {
97         struct lustre_qunit_size *lqs;
98         struct lustre_qunit_size *lqs2;
99         cfs_hash_t *hs = NULL;
100         int rc = 0;
101
102         cfs_spin_lock(&qctxt->lqc_lock);
103         if (qctxt->lqc_valid) {
104                 LASSERT(qctxt->lqc_lqs_hash != NULL);
105                 hs = cfs_hash_getref(qctxt->lqc_lqs_hash);
106         }
107         cfs_spin_unlock(&qctxt->lqc_lock);
108
109         if (hs == NULL) {
110                 rc = -EBUSY;
111                 goto out;
112         }
113
114         /* cfs_hash_lookup will +1 refcount for caller */
115         lqs = cfs_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
116         if (lqs != NULL) /* found */
117                 goto out_put;
118
119         if (!create)
120                 goto out_put;
121
122         OBD_ALLOC_PTR(lqs);
123         if (!lqs) {
124                 rc = -ENOMEM;
125                 goto out_put;
126         }
127
128         lqs->lqs_key = lqs_key;
129
130         cfs_spin_lock_init(&lqs->lqs_lock);
131
132         lqs->lqs_bwrite_pending = 0;
133         lqs->lqs_iwrite_pending = 0;
134         lqs->lqs_ino_rec = 0;
135         lqs->lqs_blk_rec = 0;
136         lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
137         lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
138         lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
139         lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
140         lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
141         lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
142         if (qctxt->lqc_handler) {
143                 lqs->lqs_last_bshrink  = 0;
144                 lqs->lqs_last_ishrink  = 0;
145         }
146
147         lqs->lqs_ctxt = qctxt; /* must be called before lqs_initref */
148         cfs_atomic_set(&lqs->lqs_refcount, 1); /* 1 for caller */
149         cfs_atomic_inc(&lqs->lqs_ctxt->lqc_lqs);
150
151         /* lqc_lqs_hash will take +1 refcount on lqs on adding */
152         lqs2 = cfs_hash_findadd_unique(qctxt->lqc_lqs_hash,
153                                        &lqs->lqs_key, &lqs->lqs_hash);
154         if (lqs2 == lqs) /* added to hash */
155                 goto out_put;
156
157         create = 0;
158         lqs_putref(lqs);
159         lqs = lqs2;
160
161  out_put:
162         cfs_hash_putref(hs);
163  out:
164         if (rc != 0) { /* error */
165                 CERROR("get lqs error(rc: %d)\n", rc);
166                 return ERR_PTR(rc);
167         }
168
169         if (lqs != NULL) {
170                 LQS_DEBUG(lqs, "%s\n",
171                           (create == 1 ? "create lqs" : "search lqs"));
172         }
173         return lqs;
174 }
175
176 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
177                            struct lustre_quota_ctxt *qctxt)
178 {
179         struct lustre_qunit_size *lqs = NULL;
180         unsigned long *unit, *tune;
181         signed long tmp = 0;
182         cfs_time_t time_limit = 0, *shrink;
183         int i, rc = 0;
184         ENTRY;
185
186         LASSERT(qctxt);
187         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
188                                qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
189         if (lqs == NULL || IS_ERR(lqs)){
190                 CERROR("fail to find a lqs for %sid %u!\n",
191                        QAQ_IS_GRP(oqaq) ? "g" : "u", oqaq->qaq_id);
192                 RETURN(PTR_ERR(lqs));
193         }
194
195         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
196                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
197         cfs_spin_lock(&lqs->lqs_lock);
198         for (i = 0; i < 2; i++) {
199                 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
200                         continue;
201
202                 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
203                         continue;
204
205                 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
206                           (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
207                 shrink = i ? &lqs->lqs_last_ishrink :
208                              &lqs->lqs_last_bshrink;
209                 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
210                                               lqs->lqs_last_bshrink,
211                                    cfs_time_seconds(qctxt->lqc_switch_seconds));
212                 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
213                 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
214
215                 /* quota master shrinks */
216                 if (qctxt->lqc_handler && tmp > 0)
217                         *shrink = cfs_time_current();
218
219                 /* quota master enlarges */
220                 if (qctxt->lqc_handler && tmp < 0) {
221                         /* in case of ping-pong effect, don't enlarge lqs
222                          * in a short time */
223                         if (*shrink &&
224                             cfs_time_before(cfs_time_current(), time_limit))
225                                 tmp = 0;
226                 }
227
228                 /* when setquota, don't enlarge lqs b=18616 */
229                 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
230                         tmp = 0;
231
232                 if (tmp != 0) {
233                         *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
234                         *tune = (*unit) / 2;
235                 }
236
237
238                 if (tmp > 0)
239                         rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
240                 if (tmp < 0)
241                         rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
242         }
243         cfs_spin_unlock(&lqs->lqs_lock);
244         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
245                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
246
247         lqs_putref(lqs);
248
249         RETURN(rc);
250 }
251
252 int filter_quota_adjust_qunit(struct obd_export *exp,
253                               struct quota_adjust_qunit *oqaq,
254                               struct lustre_quota_ctxt *qctxt,
255                               struct ptlrpc_request_set *rqset)
256 {
257         struct obd_device *obd = exp->exp_obd;
258         unsigned int id[MAXQUOTAS] = { 0, 0 };
259         int rc = 0;
260         ENTRY;
261
262         LASSERT(oqaq);
263         LASSERT(QAQ_IS_ADJBLK(oqaq));
264         rc = quota_adjust_slave_lqs(oqaq, qctxt);
265         if (rc < 0) {
266                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
267                 RETURN(rc);
268         }
269         if (QAQ_IS_GRP(oqaq))
270                 id[GRPQUOTA] = oqaq->qaq_id;
271         else
272                 id[USRQUOTA] = oqaq->qaq_id;
273
274         if (rc > 0) {
275                 rc = qctxt_adjust_qunit(obd, qctxt, id, 1, 0, NULL);
276                 if (rc == -EDQUOT || rc == -EBUSY ||
277                     rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
278                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
279                         rc = 0;
280                 }
281                 if (rc)
282                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
283         }
284         RETURN(rc);
285 }
286 #endif /* __KERNEL__ */
287 #endif
288
289 int client_quota_adjust_qunit(struct obd_export *exp,
290                               struct quota_adjust_qunit *oqaq,
291                               struct lustre_quota_ctxt *qctxt,
292                               struct ptlrpc_request_set *rqset)
293 {
294         struct ptlrpc_request *req;
295         struct quota_adjust_qunit *oqa;
296         int rc = 0;
297         ENTRY;
298
299         /* client don't support this kind of operation, abort it */
300         if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)) {
301                 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
302                        exp->exp_obd->obd_name);
303                 RETURN(rc);
304         }
305         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
306                 RETURN(-EINVAL);
307
308         LASSERT(rqset);
309
310         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
311                                         &RQF_OST_QUOTA_ADJUST_QUNIT,
312                                         LUSTRE_OST_VERSION,
313                                         OST_QUOTA_ADJUST_QUNIT);
314         if (req == NULL)
315                 RETURN(-ENOMEM);
316
317         oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
318         *oqa = *oqaq;
319
320         ptlrpc_request_set_replen(req);
321
322         ptlrpc_set_add_req(rqset, req);
323         RETURN (rc);
324 }
325
326 int lov_quota_adjust_qunit(struct obd_export *exp,
327                            struct quota_adjust_qunit *oqaq,
328                            struct lustre_quota_ctxt *qctxt,
329                            struct ptlrpc_request_set *rqset)
330 {
331         struct obd_device *obd = class_exp2obd(exp);
332         struct lov_obd *lov = &obd->u.lov;
333         int i, err, rc = 0;
334         unsigned no_set = 0;
335         ENTRY;
336
337         if (!QAQ_IS_ADJBLK(oqaq)) {
338                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
339                 RETURN(-EFAULT);
340         }
341
342
343         if (rqset == NULL) {
344                 rqset = ptlrpc_prep_set();
345                 if (!rqset)
346                         RETURN(-ENOMEM);
347                 no_set = 1;
348         }
349
350         obd_getref(obd);
351         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
352
353                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
354                         CDEBUG(D_HA, "ost %d is inactive\n", i);
355                         continue;
356                 }
357
358                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
359                                              NULL, rqset);
360                 if (err) {
361                         if (lov->lov_tgts[i]->ltd_active && !rc)
362                                 rc = err;
363                         continue;
364                 }
365         }
366
367         err = ptlrpc_set_wait(rqset);
368         if (!rc)
369                 rc = err;
370
371         /* Destroy the set if none was provided by the caller */
372         if (no_set)
373                 ptlrpc_set_destroy(rqset);
374
375         obd_putref(obd);
376         RETURN(rc);
377 }