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