Whamcloud - gitweb
Branch HEAD
[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  2008 Sun Microsystems, Inc. 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 <class_hash.h>
64 #include "quota_internal.h"
65
66 #ifdef HAVE_QUOTA_SUPPORT
67
68 #ifdef __KERNEL__
69 /**
70  * This function is charge of recording lqs_ino_rec and
71  * lqs_blk_rec. when a lquota slave checks a quota
72  * request(check_cur_qunit) and finishes a quota
73  * request(dqacq_completion), it will be called.
74  * is_chk: whether it is checking quota; otherwise, it is finishing
75  * is_acq: whether it is acquiring; otherwise, it is releasing
76  */
77 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
78                        int is_chk, int is_acq)
79 {
80         long long *rec;
81
82         LASSERT(qdata && lqs);
83         LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
84
85         rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
86
87         if (!!is_chk + !!is_acq == 1)
88                 *rec -= qdata->qd_count;
89         else
90                 *rec += qdata->qd_count;
91
92 }
93
94 static struct lustre_qunit_size *
95 quota_create_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt)
96 {
97         struct lustre_qunit_size *lqs = 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         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         spin_lock(&qctxt->lqc_lock);
125         if (!qctxt->lqc_valid)
126                 rc = -EBUSY;
127         else
128                 rc = lustre_hash_add_unique(qctxt->lqc_lqs_hash,
129                                             &lqs->lqs_key, &lqs->lqs_hash);
130         spin_unlock(&qctxt->lqc_lock);
131
132         if (!rc)
133                 lqs_getref(lqs);
134
135  out:
136         if (rc && lqs)
137                 OBD_FREE_PTR(lqs);
138
139         if (rc)
140                 return ERR_PTR(rc);
141         else
142                 return lqs;
143 }
144
145 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
146                                            struct lustre_quota_ctxt *qctxt,
147                                            int create)
148 {
149         struct lustre_qunit_size *lqs;
150         int rc = 0;
151
152  search_lqs:
153         lqs = lustre_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
154         if (IS_ERR(lqs))
155                 GOTO(out, rc = PTR_ERR(lqs));
156
157         if (create && lqs == NULL) {
158                 /* if quota_create_lqs is successful, it will get a
159                  * ref to the lqs. The ref will be released when
160                  * qctxt_cleanup() or quota is nullified */
161                 lqs = quota_create_lqs(lqs_key, qctxt);
162                 if (IS_ERR(lqs))
163                         rc = PTR_ERR(lqs);
164                 if (rc == -EALREADY)
165                         GOTO(search_lqs, rc = 0);
166                 /* get a reference for the caller when creating lqs
167                  * successfully */
168                 if (rc == 0)
169                         lqs_getref(lqs);
170         }
171
172         if (lqs && rc == 0)
173                 LQS_DEBUG(lqs, "%s\n",
174                           (create == 1 ? "create lqs" : "search lqs"));
175
176  out:
177         if (rc == 0) {
178                 return lqs;
179         } else {
180                 CDEBUG(D_ERROR, "get lqs error(rc: %d)\n", rc);
181                 return ERR_PTR(rc);
182         }
183 }
184
185 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
186                            struct lustre_quota_ctxt *qctxt)
187 {
188         struct lustre_qunit_size *lqs = NULL;
189         unsigned long *unit, *tune;
190         signed long tmp = 0;
191         cfs_time_t time_limit = 0, *shrink;
192         int i, rc = 0;
193         ENTRY;
194
195         LASSERT(qctxt);
196         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
197                                qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
198         if (lqs == NULL || IS_ERR(lqs)){
199                 CDEBUG(D_ERROR, "fail to find a lqs(%s id: %u)!\n",
200                        QAQ_IS_GRP(oqaq) ? "group" : "user", oqaq->qaq_id);
201                 RETURN(PTR_ERR(lqs));
202         }
203
204         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
205                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
206         spin_lock(&lqs->lqs_lock);
207         for (i = 0; i < 2; i++) {
208                 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
209                         continue;
210
211                 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
212                         continue;
213
214                 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
215                           (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
216                 shrink = i ? &lqs->lqs_last_ishrink :
217                              &lqs->lqs_last_bshrink;
218                 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
219                                               lqs->lqs_last_bshrink,
220                                    cfs_time_seconds(qctxt->lqc_switch_seconds));
221                 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
222                 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
223
224                 /* quota master shrinks */
225                 if (qctxt->lqc_handler && tmp > 0)
226                         *shrink = cfs_time_current();
227
228                 /* quota master enlarges */
229                 if (qctxt->lqc_handler && tmp < 0) {
230                         /* in case of ping-pong effect, don't enlarge lqs
231                          * in a short time */
232                         if (*shrink &&
233                             cfs_time_before(cfs_time_current(), time_limit))
234                                 tmp = 0;
235                 }
236
237                 /* when setquota, don't enlarge lqs b=18616 */
238                 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
239                         tmp = 0;
240
241                 if (tmp != 0) {
242                         *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
243                         *tune = (*unit) / 2;
244                 }
245
246
247                 if (tmp > 0)
248                         rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
249                 if (tmp < 0)
250                         rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
251         }
252         spin_unlock(&lqs->lqs_lock);
253         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
254                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
255
256         lqs_putref(lqs);
257
258         RETURN(rc);
259 }
260
261 int filter_quota_adjust_qunit(struct obd_export *exp,
262                               struct quota_adjust_qunit *oqaq,
263                               struct lustre_quota_ctxt *qctxt)
264 {
265         struct obd_device *obd = exp->exp_obd;
266         unsigned int id[MAXQUOTAS] = { 0, 0 };
267         int rc = 0;
268         ENTRY;
269
270         LASSERT(oqaq);
271         LASSERT(QAQ_IS_ADJBLK(oqaq));
272         rc = quota_adjust_slave_lqs(oqaq, qctxt);
273         if (rc < 0) {
274                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
275                 RETURN(rc);
276         }
277         if (QAQ_IS_GRP(oqaq))
278                 id[GRPQUOTA] = oqaq->qaq_id;
279         else
280                 id[USRQUOTA] = oqaq->qaq_id;
281
282         if (rc > 0) {
283                 rc = qctxt_adjust_qunit(obd, qctxt, id, 1, 0, NULL);
284                 if (rc == -EDQUOT || rc == -EBUSY ||
285                     rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
286                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
287                         rc = 0;
288                 }
289                 if (rc)
290                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
291         }
292         RETURN(rc);
293 }
294 #endif /* __KERNEL__ */
295 #endif
296
297 int client_quota_adjust_qunit(struct obd_export *exp,
298                               struct quota_adjust_qunit *oqaq,
299                               struct lustre_quota_ctxt *qctxt)
300 {
301         struct ptlrpc_request *req;
302         struct quota_adjust_qunit *oqa;
303         int rc = 0;
304         ENTRY;
305
306         /* client don't support this kind of operation, abort it */
307         if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)) {
308                 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
309                        exp->exp_obd->obd_name);
310                 RETURN(rc);
311         }
312         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
313                 RETURN(-EINVAL);
314
315         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
316                                         &RQF_OST_QUOTA_ADJUST_QUNIT,
317                                         LUSTRE_OST_VERSION,
318                                         OST_QUOTA_ADJUST_QUNIT);
319         if (req == NULL)
320                 RETURN(-ENOMEM);
321
322         oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
323         *oqa = *oqaq;
324
325         ptlrpc_request_set_replen(req);
326
327         rc = ptlrpc_queue_wait(req);
328         if (rc)
329                 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
330                        __FUNCTION__, rc);
331         ptlrpc_req_finished(req);
332         RETURN (rc);
333 }
334
335 int lov_quota_adjust_qunit(struct obd_export *exp,
336                            struct quota_adjust_qunit *oqaq,
337                            struct lustre_quota_ctxt *qctxt)
338 {
339         struct obd_device *obd = class_exp2obd(exp);
340         struct lov_obd *lov = &obd->u.lov;
341         int i, rc = 0;
342         ENTRY;
343
344         if (!QAQ_IS_ADJBLK(oqaq)) {
345                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
346                 RETURN(-EFAULT);
347         }
348
349         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
350                 int err;
351
352                 if (!lov->lov_tgts[i]->ltd_active) {
353                         CDEBUG(D_HA, "ost %d is inactive\n", i);
354                         continue;
355                 }
356
357                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
358                                              NULL);
359                 if (err) {
360                         if (lov->lov_tgts[i]->ltd_active && !rc)
361                                 rc = err;
362                         continue;
363                 }
364         }
365         RETURN(rc);
366 }