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