Whamcloud - gitweb
b=23363 obdfilter-survey: no echo client and ost_name not set
[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 (c) 2008, 2010, Oracle and/or its affiliates. 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/quota.h>
47 #  include <linux/smp_lock.h>
48 #  include <linux/buffer_head.h>
49 #  include <linux/workqueue.h>
50 #  include <linux/mount.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
53 #endif
54
55 #include <obd_class.h>
56 #include <lustre_mds.h>
57 #include <lustre_dlm.h>
58 #include <lustre_cfg.h>
59 #include <obd_ost.h>
60 #include <lustre_fsfilt.h>
61 #include <linux/lustre_quota.h>
62 #include <class_hash.h>
63 #include "quota_internal.h"
64
65 #ifdef HAVE_QUOTA_SUPPORT
66
67 #ifdef __KERNEL__
68 /* this function is charge of recording lqs_ino_rec and
69  * lqs_blk_rec. when a lquota slave checks a quota
70  * request(check_cur_qunit) and finishes a quota
71  * request(dqacq_completion), it will be called.
72  * is_chk: whether it is checking quota; otherwise, it is finishing
73  * is_acq: whether it is acquiring; otherwise, it is releasing
74  */
75 void quota_compute_lqs(struct qunit_data *qdata, struct lustre_qunit_size *lqs,
76                        int is_chk, int is_acq)
77 {
78         long long *rec;
79
80         LASSERT(qdata && lqs);
81         LASSERT_SPIN_LOCKED(&lqs->lqs_lock);
82
83         rec = QDATA_IS_BLK(qdata) ? &lqs->lqs_blk_rec : &lqs->lqs_ino_rec;
84
85         if (!!is_chk + !!is_acq == 1)
86                 *rec -= qdata->qd_count;
87         else
88                 *rec += qdata->qd_count;
89
90 }
91
92 static struct lustre_qunit_size *
93 quota_create_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt)
94 {
95         struct lustre_qunit_size *lqs = NULL;
96         int rc = 0;
97
98         OBD_ALLOC_PTR(lqs);
99         if (!lqs)
100                 GOTO(out, rc = -ENOMEM);
101
102         lqs->lqs_key = lqs_key;
103
104         spin_lock_init(&lqs->lqs_lock);
105         lqs->lqs_bwrite_pending = 0;
106         lqs->lqs_iwrite_pending = 0;
107         lqs->lqs_ino_rec = 0;
108         lqs->lqs_blk_rec = 0;
109         lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key);
110         lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0;
111         lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
112         lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
113         lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
114         lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
115         lqs->lqs_ctxt = qctxt;
116         if (qctxt->lqc_handler) {
117                 lqs->lqs_last_bshrink  = 0;
118                 lqs->lqs_last_ishrink  = 0;
119         }
120         lqs_initref(lqs);
121
122         spin_lock(&qctxt->lqc_lock);
123         if (!qctxt->lqc_valid)
124                 rc = -EBUSY;
125         else
126                 rc = lustre_hash_add_unique(qctxt->lqc_lqs_hash,
127                                     &lqs->lqs_key, &lqs->lqs_hash);
128         spin_unlock(&qctxt->lqc_lock);
129
130         if (!rc)
131                 lqs_getref(lqs);
132
133  out:
134         if (rc && lqs)
135                 OBD_FREE_PTR(lqs);
136
137         if (rc)
138                 return ERR_PTR(rc);
139         else
140                 return lqs;
141 }
142
143 struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key,
144                                            struct lustre_quota_ctxt *qctxt,
145                                            int create)
146 {
147         struct lustre_qunit_size *lqs;
148         int rc = 0;
149
150  search_lqs:
151         lqs = lustre_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key);
152         if (IS_ERR(lqs))
153                 GOTO(out, rc = PTR_ERR(lqs));
154
155         if (create && lqs == NULL) {
156                 /* if quota_create_lqs is successful, it will get a
157                  * ref to the lqs. The ref will be released when
158                  * qctxt_cleanup() or quota is nullified */
159                 lqs = quota_create_lqs(lqs_key, qctxt);
160                 if (IS_ERR(lqs))
161                         rc = PTR_ERR(lqs);
162                 if (rc == -EALREADY)
163                         GOTO(search_lqs, rc = 0);
164                 /* get a reference for the caller when creating lqs
165                  * successfully */
166                 if (rc == 0)
167                         lqs_getref(lqs);
168         }
169
170         if (lqs && rc == 0)
171                 LQS_DEBUG(lqs, "%s\n",
172                           (create == 1 ? "create lqs" : "search lqs"));
173
174  out:
175         if (rc == 0) {
176                 return lqs;
177         } else {
178                 CERROR("get lqs error(rc: %d)\n", rc);
179                 return ERR_PTR(rc);
180         }
181 }
182
183 int quota_adjust_slave_lqs(struct quota_adjust_qunit *oqaq,
184                            struct lustre_quota_ctxt *qctxt)
185 {
186         struct lustre_qunit_size *lqs = NULL;
187         unsigned long *unit, *tune;
188         signed long tmp = 0;
189         cfs_time_t time_limit = 0, *shrink;
190         int i, rc = 0;
191         ENTRY;
192
193         LASSERT(qctxt);
194         lqs = quota_search_lqs(LQS_KEY(QAQ_IS_GRP(oqaq), oqaq->qaq_id),
195                                qctxt, QAQ_IS_CREATE_LQS(oqaq) ? 1 : 0);
196         if (lqs == NULL || IS_ERR(lqs)){
197                 CERROR("fail to find a lqs for %sid %u!\n",
198                        QAQ_IS_GRP(oqaq) ? "g" : "u", oqaq->qaq_id);
199                 RETURN(PTR_ERR(lqs));
200         }
201
202         if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
203                 lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz;
204                 lqs->lqs_btune_sz = qctxt->lqc_btune_sz;
205                 lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz;
206                 lqs->lqs_itune_sz = qctxt->lqc_itune_sz;
207                 lqs_putref(lqs);
208                 RETURN(0);
209         }
210
211         CDEBUG(D_QUOTA, "before: bunit: %lu, iunit: %lu.\n",
212                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
213         spin_lock(&lqs->lqs_lock);
214         for (i = 0; i < 2; i++) {
215                 if (i == 0 && !QAQ_IS_ADJBLK(oqaq))
216                         continue;
217
218                 if (i == 1 && !QAQ_IS_ADJINO(oqaq))
219                         continue;
220
221                 tmp = i ? (lqs->lqs_iunit_sz - oqaq->qaq_iunit_sz) :
222                           (lqs->lqs_bunit_sz - oqaq->qaq_bunit_sz);
223                 shrink = i ? &lqs->lqs_last_ishrink :
224                              &lqs->lqs_last_bshrink;
225                 time_limit = cfs_time_add(i ? lqs->lqs_last_ishrink :
226                                               lqs->lqs_last_bshrink,
227                                    cfs_time_seconds(qctxt->lqc_switch_seconds));
228                 unit = i ? &lqs->lqs_iunit_sz : &lqs->lqs_bunit_sz;
229                 tune = i ? &lqs->lqs_itune_sz : &lqs->lqs_btune_sz;
230
231                 /* quota master shrinks */
232                 if (qctxt->lqc_handler && tmp > 0)
233                         *shrink = cfs_time_current();
234
235                 /* quota master enlarges */
236                 if (qctxt->lqc_handler && tmp < 0) {
237                         /* in case of ping-pong effect, don't enlarge lqs
238                          * in a short time */
239                         if (*shrink &&
240                             cfs_time_before(cfs_time_current(), time_limit))
241                                 tmp = 0;
242                 }
243
244                 /* when setquota, don't enlarge lqs b=18616 */
245                 if (QAQ_IS_CREATE_LQS(oqaq) && tmp < 0)
246                         tmp = 0;
247
248                 if (tmp != 0) {
249                         *unit = i ? oqaq->qaq_iunit_sz : oqaq->qaq_bunit_sz;
250                         *tune = (*unit) / 2;
251                 }
252
253
254                 if (tmp > 0)
255                         rc |= i ? LQS_INO_DECREASE : LQS_BLK_DECREASE;
256                 if (tmp < 0)
257                         rc |= i ? LQS_INO_INCREASE : LQS_BLK_INCREASE;
258         }
259         spin_unlock(&lqs->lqs_lock);
260         CDEBUG(D_QUOTA, "after: bunit: %lu, iunit: %lu.\n",
261                lqs->lqs_bunit_sz, lqs->lqs_iunit_sz);
262
263         lqs_putref(lqs);
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 uid = 0, gid = 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                 gid = oqaq->qaq_id;
286         else
287                 uid = oqaq->qaq_id;
288
289         if (rc > 0) {
290                 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 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         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqaq) };
311         int rc = 0;
312         ENTRY;
313
314         /* client don't support this kind of operation, abort it */
315         if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)||
316             OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS)) {
317                 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
318                        exp->exp_obd->obd_name);
319                 RETURN(rc);
320         }
321         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
322                 RETURN(-EINVAL);
323
324         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OST_VERSION,
325                               OST_QUOTA_ADJUST_QUNIT, 2, size, NULL);
326         if (!req)
327                 GOTO(out, rc = -ENOMEM);
328
329         oqa = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqaq));
330         *oqa = *oqaq;
331
332         ptlrpc_req_set_repsize(req, 2, size);
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                 GOTO(out, rc);
339         }
340         ptlrpc_req_finished(req);
341 out:
342         RETURN (rc);
343 }
344
345 int lov_quota_adjust_qunit(struct obd_export *exp,
346                            struct quota_adjust_qunit *oqaq,
347                            struct lustre_quota_ctxt *qctxt)
348 {
349         struct obd_device *obd = class_exp2obd(exp);
350         struct lov_obd *lov = &obd->u.lov;
351         int i, rc = 0;
352         ENTRY;
353
354         if (!QAQ_IS_ADJBLK(oqaq)) {
355                 CERROR("bad qaq_flags %x for lov obd.\n", oqaq->qaq_flags);
356                 RETURN(-EFAULT);
357         }
358
359         obd_getref(obd);
360         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
361                 int err;
362
363                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
364                         CDEBUG(D_HA, "ost %d is inactive\n", i);
365                         continue;
366                 }
367
368                 err = obd_quota_adjust_qunit(lov->lov_tgts[i]->ltd_exp, oqaq,
369                                              NULL);
370                 if (err) {
371                         if (lov->lov_tgts[i]->ltd_active && !rc)
372                                 rc = err;
373                         continue;
374                 }
375         }
376         obd_putref(obd);
377         RETURN(rc);
378 }