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