Whamcloud - gitweb
8f01e0dfb3716e76d6fb6afcf37b23e8feb24897
[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 static struct lustre_qunit_size *
94 quota_create_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt)
95 {
96         struct lustre_qunit_size *lqs = NULL;
97         cfs_hash_t *hs = NULL;
98         int rc = 0;
99
100         OBD_ALLOC_PTR(lqs);
101         if (!lqs)
102                 GOTO(out, rc = -ENOMEM);
103
104         lqs->lqs_key = lqs_key;
105
106         cfs_spin_lock_init(&lqs->lqs_lock);
107         lqs->lqs_bwrite_pending = 0;
108         lqs->lqs_iwrite_pending = 0;
109         lqs->lqs_ino_rec = 0;
110         lqs->lqs_blk_rec = 0;
111         lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
112         lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
113         lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
114         lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
115         lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
116         lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
117         lqs->lqs_ctxt = qctxt;
118         if (qctxt->lqc_handler) {
119                 lqs->lqs_last_bshrink  = 0;
120                 lqs->lqs_last_ishrink  = 0;
121         }
122         lqs_initref(lqs);
123
124         cfs_spin_lock(&qctxt->lqc_lock);
125         if (qctxt->lqc_valid)
126                 hs = cfs_hash_getref(qctxt->lqc_lqs_hash);
127         cfs_spin_unlock(&qctxt->lqc_lock);
128
129         if (hs) {
130                 lqs_getref(lqs);
131                 rc = cfs_hash_add_unique(qctxt->lqc_lqs_hash,
132                                          &lqs->lqs_key, &lqs->lqs_hash);
133                 if (rc)
134                         lqs_putref(lqs);
135                 cfs_hash_putref(hs);
136         } else {
137                 rc = -EBUSY;
138         }
139
140  out:
141         if (rc && lqs)
142                 OBD_FREE_PTR(lqs);
143
144         if (rc)
145                 return ERR_PTR(rc);
146         else
147                 return lqs;
148 }
149
150 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
151                                            struct lustre_quota_ctxt *qctxt,
152                                            int create)
153 {
154         struct lustre_qunit_size *lqs;
155         int rc = 0;
156
157  search_lqs:
158         lqs = cfs_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
159         if (IS_ERR(lqs))
160                 GOTO(out, rc = PTR_ERR(lqs));
161
162         if (create && lqs == NULL) {
163                 /* if quota_create_lqs is successful, it will get a
164                  * ref to the lqs. The ref will be released when
165                  * qctxt_cleanup() or quota is nullified */
166                 lqs = quota_create_lqs(lqs_key, qctxt);
167                 if (IS_ERR(lqs))
168                         rc = PTR_ERR(lqs);
169                 if (rc == -EALREADY)
170                         GOTO(search_lqs, rc = 0);
171                 /* get a reference for the caller when creating lqs
172                  * successfully */
173                 if (rc == 0)
174                         lqs_getref(lqs);
175         }
176
177         if (lqs && rc == 0)
178                 LQS_DEBUG(lqs, "%s\n",
179                           (create == 1 ? "create lqs" : "search lqs"));
180
181  out:
182         if (rc == 0) {
183                 return lqs;
184         } else {
185                 CERROR("get lqs error(rc: %d)\n", rc);
186                 return ERR_PTR(rc);
187         }
188 }
189
190 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
191                            struct lustre_quota_ctxt *qctxt)
192 {
193         struct lustre_qunit_size *lqs = NULL;
194         unsigned long *unit, *tune;
195         signed long tmp = 0;
196         cfs_time_t time_limit = 0, *shrink;
197         int i, rc = 0;
198         ENTRY;
199
200         LASSERT(qctxt);
201         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
202                                qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
203         if (lqs == NULL || IS_ERR(lqs)){
204                 CERROR("fail to find a lqs for %sid %u!\n",
205                        QAQ_IS_GRP(oqaq) ? "g" : "u", oqaq->qaq_id);
206                 RETURN(PTR_ERR(lqs));
207         }
208
209         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
210                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
211         cfs_spin_lock(&lqs->lqs_lock);
212         for (i = 0; i < 2; i++) {
213                 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
214                         continue;
215
216                 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
217                         continue;
218
219                 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
220                           (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
221                 shrink = i ? &lqs->lqs_last_ishrink :
222                              &lqs->lqs_last_bshrink;
223                 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
224                                               lqs->lqs_last_bshrink,
225                                    cfs_time_seconds(qctxt->lqc_switch_seconds));
226                 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
227                 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
228
229                 /* quota master shrinks */
230                 if (qctxt->lqc_handler && tmp > 0)
231                         *shrink = cfs_time_current();
232
233                 /* quota master enlarges */
234                 if (qctxt->lqc_handler && tmp < 0) {
235                         /* in case of ping-pong effect, don't enlarge lqs
236                          * in a short time */
237                         if (*shrink &&
238                             cfs_time_before(cfs_time_current(), time_limit))
239                                 tmp = 0;
240                 }
241
242                 /* when setquota, don't enlarge lqs b=18616 */
243                 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
244                         tmp = 0;
245
246                 if (tmp != 0) {
247                         *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
248                         *tune = (*unit) / 2;
249                 }
250
251
252                 if (tmp > 0)
253                         rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
254                 if (tmp < 0)
255                         rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
256         }
257         cfs_spin_unlock(&lqs->lqs_lock);
258         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
259                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
260
261         lqs_putref(lqs);
262
263         RETURN(rc);
264 }
265
266 int filter_quota_adjust_qunit(struct obd_export *exp,
267                               struct quota_adjust_qunit *oqaq,
268                               struct lustre_quota_ctxt *qctxt,
269                               struct ptlrpc_request_set *rqset)
270 {
271         struct obd_device *obd = exp->exp_obd;
272         unsigned int id[MAXQUOTAS] = { 0, 0 };
273         int rc = 0;
274         ENTRY;
275
276         LASSERT(oqaq);
277         LASSERT(QAQ_IS_ADJBLK(oqaq));
278         rc = quota_adjust_slave_lqs(oqaq, qctxt);
279         if (rc < 0) {
280                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
281                 RETURN(rc);
282         }
283         if (QAQ_IS_GRP(oqaq))
284                 id[GRPQUOTA] = oqaq->qaq_id;
285         else
286                 id[USRQUOTA] = oqaq->qaq_id;
287
288         if (rc > 0) {
289                 rc = qctxt_adjust_qunit(obd, qctxt, id, 1, 0, NULL);
290                 if (rc == -EDQUOT || rc == -EBUSY ||
291                     rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
292                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
293                         rc = 0;
294                 }
295                 if (rc)
296                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
297         }
298         RETURN(rc);
299 }
300 #endif /* __KERNEL__ */
301 #endif
302
303 int client_quota_adjust_qunit(struct obd_export *exp,
304                               struct quota_adjust_qunit *oqaq,
305                               struct lustre_quota_ctxt *qctxt,
306                               struct ptlrpc_request_set *rqset)
307 {
308         struct ptlrpc_request *req;
309         struct quota_adjust_qunit *oqa;
310         int rc = 0;
311         ENTRY;
312
313         /* client don't support this kind of operation, abort it */
314         if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)) {
315                 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
316                        exp->exp_obd->obd_name);
317                 RETURN(rc);
318         }
319         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
320                 RETURN(-EINVAL);
321
322         LASSERT(rqset);
323
324         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
325                                         &RQF_OST_QUOTA_ADJUST_QUNIT,
326                                         LUSTRE_OST_VERSION,
327                                         OST_QUOTA_ADJUST_QUNIT);
328         if (req == NULL)
329                 RETURN(-ENOMEM);
330
331         oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
332         *oqa = *oqaq;
333
334         ptlrpc_request_set_replen(req);
335
336         ptlrpc_set_add_req(rqset, req);
337         RETURN (rc);
338 }
339
340 int lov_quota_adjust_qunit(struct obd_export *exp,
341                            struct quota_adjust_qunit *oqaq,
342                            struct lustre_quota_ctxt *qctxt,
343                            struct ptlrpc_request_set *rqset)
344 {
345         struct obd_device *obd = class_exp2obd(exp);
346         struct lov_obd *lov = &obd->u.lov;
347         int i, err, rc = 0;
348         unsigned no_set = 0;
349         ENTRY;
350
351         if (!QAQ_IS_ADJBLK(oqaq)) {
352                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
353                 RETURN(-EFAULT);
354         }
355
356
357         if (rqset == NULL) {
358                 rqset = ptlrpc_prep_set();
359                 if (!rqset)
360                         RETURN(-ENOMEM);
361                 no_set = 1;
362         }
363
364         obd_getref(obd);
365         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
366
367                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
368                         CDEBUG(D_HA, "ost %d is inactive\n", i);
369                         continue;
370                 }
371
372                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
373                                              NULL, rqset);
374                 if (err) {
375                         if (lov->lov_tgts[i]->ltd_active && !rc)
376                                 rc = err;
377                         continue;
378                 }
379         }
380
381         err = ptlrpc_set_wait(rqset);
382         if (!rc)
383                 rc = err;
384
385         /* Destroy the set if none was provided by the caller */
386         if (no_set)
387                 ptlrpc_set_destroy(rqset);
388
389         obd_putref(obd);
390         RETURN(rc);
391 }