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         rc = lustre_hash_add_unique(qctxt->lqc_lqs_hash, &lqs->lqs_key,
124                                     &lqs->lqs_hash);
125         if (!rc)
126                 lqs_getref(lqs);
127
128  out:
129         if (rc && lqs)
130                 OBD_FREE_PTR(lqs);
131
132         if (rc)
133                 return ERR_PTR(rc);
134         else
135                 return lqs;
136 }
137
138 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
139                                            struct lustre_quota_ctxt *qctxt,
140                                            int create)
141 {
142         int rc = 0;
143         struct lustre_qunit_size *lqs;
144
145  search_lqs:
146         lqs = lustre_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
147         if (lqs == NULL && create) {
148                 lqs = quota_create_lqs(lqs_key, qctxt);
149                 if (IS_ERR(lqs))
150                         rc = PTR_ERR(lqs);
151                 if (rc == -EALREADY) {
152                         rc = 0;
153                         goto search_lqs;
154                 }
155         }
156
157         if (lqs)
158                 LQS_DEBUG(lqs, "%s\n",
159                           (create == 1 ? "create lqs" : "search lqs"));
160
161         if (rc == 0) {
162                 return lqs;
163         } else {
164                 CDEBUG(D_ERROR, "get lqs error(rc: %d)\n", rc);
165                 return ERR_PTR(rc);
166         }
167 }
168
169 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
170                            struct lustre_quota_ctxt *qctxt)
171 {
172         struct lustre_qunit_size *lqs = NULL;
173         unsigned long *lbunit, *liunit, *lbtune, *litune;
174         signed long b_tmp = 0, i_tmp = 0;
175         cfs_time_t time_limit = 0;
176         int rc = 0;
177         ENTRY;
178
179         LASSERT(qctxt);
180         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
181                                qctxt, 1);
182         if (lqs == NULL || IS_ERR(lqs))
183                 RETURN(PTR_ERR(lqs));
184
185         /* deleting the lqs, because a user sets lfs quota 0 0 0 0  */
186         if (!oqaq->qaq_bunit_sz && !oqaq->qaq_iunit_sz && QAQ_IS_ADJBLK(oqaq) &&
187             QAQ_IS_ADJINO(oqaq)) {
188                 LQS_DEBUG(lqs, "release lqs\n");
189                 /* this is for quota_search_lqs */
190                 lqs_putref(lqs);
191                 /* kill lqs */
192                 lqs_putref(lqs);
193                 RETURN(rc);
194         }
195
196         lbunit = &lqs->lqs_bunit_sz;
197         liunit = &lqs->lqs_iunit_sz;
198         lbtune = &lqs->lqs_btune_sz;
199         litune = &lqs->lqs_itune_sz;
200
201         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
202         spin_lock(&lqs->lqs_lock);
203         /* adjust the slave's block qunit size */
204         if (QAQ_IS_ADJBLK(oqaq)) {
205                 cfs_duration_t sec = cfs_time_seconds(qctxt->lqc_switch_seconds);
206
207                 b_tmp = *lbunit - oqaq->qaq_bunit_sz;
208
209                 if (qctxt->lqc_handler && b_tmp > 0)
210                         lqs->lqs_last_bshrink = cfs_time_current();
211
212                 if (qctxt->lqc_handler && b_tmp < 0) {
213                         time_limit = cfs_time_add(lqs->lqs_last_bshrink, sec);
214                         if (!lqs->lqs_last_bshrink ||
215                             cfs_time_after(cfs_time_current(), time_limit)) {
216                                 *lbunit = oqaq->qaq_bunit_sz;
217                                 *lbtune = (*lbunit) / 2;
218                         } else {
219                                 b_tmp = 0;
220                         }
221                 } else {
222                         *lbunit = oqaq->qaq_bunit_sz;
223                         *lbtune = (*lbunit) / 2;
224                 }
225         }
226
227         /* adjust the slave's file qunit size */
228         if (QAQ_IS_ADJINO(oqaq)) {
229                 i_tmp = *liunit - oqaq->qaq_iunit_sz;
230
231                 if (qctxt->lqc_handler && i_tmp > 0)
232                         lqs->lqs_last_ishrink  = cfs_time_current();
233
234                 if (qctxt->lqc_handler && i_tmp < 0) {
235                         time_limit = cfs_time_add(lqs->lqs_last_ishrink,
236                                                   cfs_time_seconds(qctxt->
237                                                   lqc_switch_seconds));
238                         if (!lqs->lqs_last_ishrink ||
239                             cfs_time_after(cfs_time_current(), time_limit)) {
240                                 *liunit = oqaq->qaq_iunit_sz;
241                                 *litune = (*liunit) / 2;
242                         } else {
243                                 i_tmp = 0;
244                         }
245                 } else {
246                         *liunit = oqaq->qaq_iunit_sz;
247                         *litune = (*liunit) / 2;
248                 }
249         }
250         spin_unlock(&lqs->lqs_lock);
251         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n", *lbunit, *liunit);
252
253         lqs_putref(lqs);
254
255         if (b_tmp > 0)
256                 rc |= LQS_BLK_DECREASE;
257         else if (b_tmp < 0)
258                 rc |= LQS_BLK_INCREASE;
259
260         if (i_tmp > 0)
261                 rc |= LQS_INO_DECREASE;
262         else if (i_tmp < 0)
263                 rc |= LQS_INO_INCREASE;
264
265         RETURN(rc);
266 }
267
268 int filter_quota_adjust_qunit(struct obd_export *exp,
269                               struct quota_adjust_qunit *oqaq,
270                               struct lustre_quota_ctxt *qctxt)
271 {
272         struct obd_device *obd = exp->exp_obd;
273         unsigned int id[MAXQUOTAS] = { 0, 0 };
274         int rc = 0;
275         ENTRY;
276
277         LASSERT(oqaq);
278         LASSERT(QAQ_IS_ADJBLK(oqaq));
279         rc = quota_adjust_slave_lqs(oqaq, qctxt);
280         if (rc < 0) {
281                 CERROR("adjust mds slave's qunit size failed!(rc:%d)\n", rc);
282                 RETURN(rc);
283         }
284         if (QAQ_IS_GRP(oqaq))
285                 id[GRPQUOTA] = oqaq->qaq_id;
286         else
287                 id[USRQUOTA] = oqaq->qaq_id;
288
289         if (rc > 0) {
290                 rc = qctxt_adjust_qunit(obd, qctxt, id, 1, 0, NULL);
291                 if (rc == -EDQUOT || rc == -EBUSY ||
292                     rc == QUOTA_REQ_RETURNED || rc == -EAGAIN) {
293                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
294                         rc = 0;
295                 }
296                 if (rc)
297                         CERROR("slave adjust block quota failed!(rc:%d)\n", rc);
298         }
299         RETURN(rc);
300 }
301 #endif /* __KERNEL__ */
302 #endif
303
304 int client_quota_adjust_qunit(struct obd_export *exp,
305                               struct quota_adjust_qunit *oqaq,
306                               struct lustre_quota_ctxt *qctxt)
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         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
323                                         &RQF_OST_QUOTA_ADJUST_QUNIT,
324                                         LUSTRE_OST_VERSION,
325                                         OST_QUOTA_ADJUST_QUNIT);
326         if (req == NULL)
327                 RETURN(-ENOMEM);
328
329         oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
330         *oqa = *oqaq;
331
332         ptlrpc_request_set_replen(req);
333
334         rc = ptlrpc_queue_wait(req);
335         if (rc)
336                 CERROR("%s: %s failed: rc = %d\n", exp->exp_obd->obd_name,
337                        __FUNCTION__, rc);
338         ptlrpc_req_finished(req);
339         RETURN (rc);
340 }
341
342 int lov_quota_adjust_qunit(struct obd_export *exp,
343                            struct quota_adjust_qunit *oqaq,
344                            struct lustre_quota_ctxt *qctxt)
345 {
346         struct obd_device *obd = class_exp2obd(exp);
347         struct lov_obd *lov = &obd->u.lov;
348         int i, rc = 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         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
357                 int err;
358
359                 if (!lov->lov_tgts[i]->ltd_active) {
360                         CDEBUG(D_HA, "ost %d is inactive\n", i);
361                         continue;
362                 }
363
364                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
365                                              NULL);
366                 if (err) {
367                         if (lov->lov_tgts[i]->ltd_active && !rc)
368                                 rc = err;
369                         continue;
370                 }
371         }
372         RETURN(rc);
373 }