Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / quota / quota_interface.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LQUOTA
41
42 #ifdef __KERNEL__
43 # include <linux/version.h>
44 # include <linux/module.h>
45 # include <linux/init.h>
46 # include <linux/fs.h>
47 # include <linux/jbd.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 <lustre_quota.h>
63 #include <lprocfs_status.h>
64 #include "quota_internal.h"
65
66 #ifdef __KERNEL__
67
68 #ifdef HAVE_QUOTA_SUPPORT
69
70 static cfs_time_t last_print = 0;
71 static cfs_spinlock_t last_print_lock = CFS_SPIN_LOCK_UNLOCKED;
72
73 static int filter_quota_setup(struct obd_device *obd)
74 {
75         int rc = 0;
76         struct obd_device_target *obt = &obd->u.obt;
77         ENTRY;
78
79         cfs_init_rwsem(&obt->obt_rwsem);
80         obt->obt_qfmt = LUSTRE_QUOTA_V2;
81         cfs_sema_init(&obt->obt_quotachecking, 1);
82         rc = qctxt_init(obd, NULL);
83         if (rc)
84                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
85
86         RETURN(rc);
87 }
88
89 static int filter_quota_cleanup(struct obd_device *obd)
90 {
91         ENTRY;
92         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
93         RETURN(0);
94 }
95
96 static int filter_quota_setinfo(struct obd_device *obd, void *data)
97 {
98         struct obd_export *exp = data;
99         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
100         struct obd_import *imp = exp->exp_imp_reverse;
101         ENTRY;
102
103         LASSERT(imp != NULL);
104
105         /* setup the quota context import */
106         cfs_spin_lock(&qctxt->lqc_lock);
107         if (qctxt->lqc_import != NULL) {
108                 cfs_spin_unlock(&qctxt->lqc_lock);
109                 if (qctxt->lqc_import == imp)
110                         CDEBUG(D_WARNING, "%s: lqc_import(%p) of obd(%p) was "
111                                "activated already.\n", obd->obd_name, imp, obd);
112                 else
113                         CERROR("%s: lqc_import(%p:%p) of obd(%p) was "
114                                "activated by others.\n", obd->obd_name,
115                                qctxt->lqc_import, imp, obd);
116         } else {
117                 qctxt->lqc_import = imp;
118                 /* make imp's connect flags equal relative exp's connect flags
119                  * adding it to avoid the scan export list */
120                 imp->imp_connect_data.ocd_connect_flags |=
121                                 (exp->exp_connect_flags &
122                                  (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
123                 cfs_spin_unlock(&qctxt->lqc_lock);
124                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated "
125                        "now.\n", obd->obd_name, imp, obd);
126
127                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
128                 /* start quota slave recovery thread. (release high limits) */
129                 qslave_start_recovery(obd, qctxt);
130         }
131         RETURN(0);
132 }
133
134 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
135 {
136         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
137         struct obd_import *imp = exp->exp_imp_reverse;
138         ENTRY;
139
140         /* lquota may be not set up before destroying export, b=14896 */
141         if (!obd->obd_set_up)
142                 RETURN(0);
143
144         if (unlikely(imp == NULL))
145                 RETURN(0);
146
147         /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
148          * should be invalid b=12374 */
149         cfs_spin_lock(&qctxt->lqc_lock);
150         if (qctxt->lqc_import == imp) {
151                 qctxt->lqc_import = NULL;
152                 cfs_spin_unlock(&qctxt->lqc_lock);
153                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is invalid now.\n",
154                        obd->obd_name, imp, obd);
155                 ptlrpc_cleanup_imp(imp);
156                 dqacq_interrupt(qctxt);
157         } else {
158                 cfs_spin_unlock(&qctxt->lqc_lock);
159         }
160         RETURN(0);
161 }
162
163 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
164 {
165         ENTRY;
166
167         if (!ll_sb_any_quota_active(obd->u.obt.obt_sb))
168                 RETURN(0);
169
170         if (ignore) {
171                 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
172                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
173         } else {
174                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
175         }
176
177         RETURN(0);
178 }
179
180 #define GET_OA_ID(flag, oa) (flag == USRQUOTA ? oa->o_uid : oa->o_gid)
181 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
182 {
183         struct obd_device_target *obt = &obd->u.obt;
184         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
185         int err, cnt, rc = 0;
186         struct obd_quotactl *oqctl;
187         ENTRY;
188
189         if (!ll_sb_any_quota_active(obt->obt_sb))
190                 RETURN(0);
191
192         OBD_ALLOC_PTR(oqctl);
193         if (!oqctl)
194                 RETURN(-ENOMEM);
195
196         /* set over quota flags for a uid/gid */
197         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
198         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
199
200         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
201                 struct lustre_qunit_size *lqs = NULL;
202
203                 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
204                                        qctxt, 0);
205                 if (IS_ERR(lqs)) {
206                         rc = PTR_ERR(lqs);
207                         CDEBUG(D_QUOTA, "search lqs for %s %d failed, "
208                                "(rc = %d)\n",
209                                cnt == USRQUOTA ? "user" : "group",
210                                GET_OA_ID(cnt, oa), rc);
211                         break;
212                 } else if (lqs == NULL) {
213                         /* continue to check group quota if the file's owner
214                          * doesn't have quota limit. LU-530 */
215                         continue;
216                 } else {
217                         cfs_spin_lock(&lqs->lqs_lock);
218                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
219                                 oa->o_flags |= (cnt == USRQUOTA) ?
220                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
221                                 cfs_spin_unlock(&lqs->lqs_lock);
222                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
223                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
224                                        qctxt->lqc_sync_blk);
225                                 /* this is for quota_search_lqs */
226                                 lqs_putref(lqs);
227                                 continue;
228                         }
229                         cfs_spin_unlock(&lqs->lqs_lock);
230                         /* this is for quota_search_lqs */
231                         lqs_putref(lqs);
232                 }
233
234                 memset(oqctl, 0, sizeof(*oqctl));
235
236                 oqctl->qc_cmd = Q_GETQUOTA;
237                 oqctl->qc_type = cnt;
238                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
239                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
240                 if (err) {
241                         if (!rc)
242                                 rc = err;
243                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
244                                                              OBD_MD_FLGRPQUOTA);
245                         CDEBUG(D_QUOTA, "fsfilt getquota for %s %d failed, "
246                                "(rc = %d)\n",
247                                cnt == USRQUOTA ? "user" : "group",
248                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid, err);
249                         continue;
250                 }
251
252                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
253                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
254                     oqctl->qc_dqblk.dqb_bhardlimit)) {
255                         oa->o_flags |= (cnt == USRQUOTA) ?
256                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
257                         CDEBUG(D_QUOTA, "out of quota for %s %d\n",
258                                cnt == USRQUOTA ? "user" : "group",
259                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid);
260                 }
261         }
262         OBD_FREE_PTR(oqctl);
263         RETURN(rc);
264 }
265
266 /**
267  * check whether the left quota of certain uid and gid can satisfy a block_write
268  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
269  */
270 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
271                               int pending[], int count, int cycle, int isblk,
272                               struct inode *inode, int frags)
273 {
274         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
275         int i;
276         struct qunit_data qdata[MAXQUOTAS];
277         int mb = 0;
278         int rc = 0, rc2[2] = { 0, 0 };
279         ENTRY;
280
281         cfs_spin_lock(&qctxt->lqc_lock);
282         if (!qctxt->lqc_valid){
283                 cfs_spin_unlock(&qctxt->lqc_lock);
284                 RETURN(rc);
285         }
286         cfs_spin_unlock(&qctxt->lqc_lock);
287
288         for (i = 0; i < MAXQUOTAS; i++) {
289                 struct lustre_qunit_size *lqs = NULL;
290
291                 qdata[i].qd_id = id[i];
292                 qdata[i].qd_flags = i;
293                 if (isblk)
294                         QDATA_SET_BLK(&qdata[i]);
295                 qdata[i].qd_count = 0;
296
297                 /* ignore root user */
298                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
299                         continue;
300
301                 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
302                 if (lqs == NULL || IS_ERR(lqs))
303                         continue;
304
305                 if (IS_ERR(lqs)) {
306                         CERROR("can not find lqs for check_common: "
307                                "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
308                                id[i], i % 2 ? 'g': 'u', isblk, count,
309                                PTR_ERR(lqs));
310                         RETURN(PTR_ERR(lqs));
311                 }
312
313                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
314                 cfs_spin_lock(&lqs->lqs_lock);
315                 if (!cycle) {
316                         if (isblk) {
317                                 pending[i] = count * CFS_PAGE_SIZE;
318                                 /* in order to complete this write, we need extra
319                                  * meta blocks. This function can get it through
320                                  * data needed to be written b=16542 */
321                                 if (inode) {
322                                         mb = pending[i];
323                                         rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
324                                                              &mb, inode,
325                                                              frags);
326                                         if (rc)
327                                                 CERROR("%s: can't get extra "
328                                                        "meta blocks\n",
329                                                        obd->obd_name);
330                                         else
331                                                 pending[i] += mb;
332                                 }
333                                 LASSERTF(pending[i] >= 0, "pending is not valid"
334                                          ", count=%d, mb=%d\n", count, mb);
335                                 lqs->lqs_bwrite_pending += pending[i];
336                         } else {
337                                 pending[i] = count;
338                                 lqs->lqs_iwrite_pending += pending[i];
339                         }
340                 }
341
342                 /* if xx_rec < 0, that means quota are releasing,
343                  * and it may return before we use quota. So if
344                  * we find this situation, we assuming it has
345                  * returned b=18491 */
346                 if (isblk && lqs->lqs_blk_rec < 0) {
347                         if (qdata[i].qd_count < -lqs->lqs_blk_rec)
348                                 qdata[i].qd_count = 0;
349                         else
350                                 qdata[i].qd_count += lqs->lqs_blk_rec;
351                 }
352                 if (!isblk && lqs->lqs_ino_rec < 0) {
353                         if (qdata[i].qd_count < -lqs->lqs_ino_rec)
354                                 qdata[i].qd_count = 0;
355                         else
356                                 qdata[i].qd_count += lqs->lqs_ino_rec;
357                 }
358
359                 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
360                        " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
361                        " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
362                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
363                        qdata[i].qd_count, mb, pending[i]);
364                 if (rc2[i] == QUOTA_RET_OK) {
365                         if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
366                                 rc2[i] = QUOTA_RET_ACQUOTA;
367                         if (!isblk && qdata[i].qd_count <
368                             lqs->lqs_iwrite_pending)
369                                 rc2[i] = QUOTA_RET_ACQUOTA;
370                 }
371
372                 cfs_spin_unlock(&lqs->lqs_lock);
373
374                 if (lqs->lqs_blk_rec  < 0 &&
375                     qdata[i].qd_count <
376                     lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
377                         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
378
379                 /* When cycle is zero, lqs_*_pending will be changed. We will
380                  * get reference of the lqs here and put reference of lqs in
381                  * quota_pending_commit b=14784 */
382                 if (!cycle)
383                         lqs_getref(lqs);
384
385                 /* this is for quota_search_lqs */
386                 lqs_putref(lqs);
387         }
388
389         if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
390                 RETURN(QUOTA_RET_ACQUOTA);
391         else
392                 RETURN(rc);
393 }
394
395 int quota_is_set(struct obd_device *obd, const unsigned int id[], int flag)
396 {
397         struct lustre_qunit_size *lqs;
398         int i, q_set = 0;
399
400         if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
401                 RETURN(0);
402
403         for (i = 0; i < MAXQUOTAS; i++) {
404                 lqs = quota_search_lqs(LQS_KEY(i, id[i]),
405                                        &obd->u.obt.obt_qctxt, 0);
406                 if (lqs && !IS_ERR(lqs)) {
407                         if (lqs->lqs_flags & flag)
408                                 q_set = 1;
409                         lqs_putref(lqs);
410                 }
411         }
412
413         return q_set;
414 }
415
416 static int quota_chk_acq_common(struct obd_device *obd, struct obd_export *exp,
417                                 const unsigned int id[], int pending[],
418                                 int count, quota_acquire acquire,
419                                 struct obd_trans_info *oti, int isblk,
420                                 struct inode *inode, int frags)
421 {
422         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
423         struct timeval work_start;
424         struct timeval work_end;
425         long timediff;
426         struct l_wait_info lwi = { 0 };
427         int rc = 0, cycle = 0, count_err = 1;
428         ENTRY;
429
430         if (!quota_is_set(obd, id, isblk ? QB_SET : QI_SET))
431                 RETURN(0);
432
433         if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
434                 /* If the client has been evicted or if it
435                  * timed out and tried to reconnect already,
436                  * abort the request immediately */
437                 RETURN(-ENOTCONN);
438
439         CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
440         pending[USRQUOTA] = pending[GRPQUOTA] = 0;
441         /* Unfortunately, if quota master is too busy to handle the
442          * pre-dqacq in time and quota hash on ost is used up, we
443          * have to wait for the completion of in flight dqacq/dqrel,
444          * in order to get enough quota for write b=12588 */
445         cfs_gettimeofday(&work_start);
446         while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
447                                         inode, frags)) &
448                QUOTA_RET_ACQUOTA) {
449
450                 cfs_spin_lock(&qctxt->lqc_lock);
451                 if (!qctxt->lqc_import && oti) {
452                         cfs_spin_unlock(&qctxt->lqc_lock);
453                         LASSERT(oti->oti_thread);
454                         /* The recovery thread doesn't have watchdog
455                          * attached. LU-369 */
456                         if (oti->oti_thread->t_watchdog)
457                                 lc_watchdog_disable(oti->oti_thread->\
458                                                 t_watchdog);
459                         CDEBUG(D_QUOTA, "sleep for quota master\n");
460                         l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
461                                      &lwi);
462                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
463                         if (oti->oti_thread->t_watchdog)
464                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
465                                        CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
466                 } else {
467                         cfs_spin_unlock(&qctxt->lqc_lock);
468                 }
469
470                 cycle++;
471                 if (isblk)
472                         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
473                 /* after acquire(), we should run quota_check_common again
474                  * so that we confirm there are enough quota to finish write */
475                 rc = acquire(obd, id, oti, isblk);
476
477                 /* please reference to dqacq_completion for the below */
478                 /* a new request is finished, try again */
479                 if (rc == QUOTA_REQ_RETURNED) {
480                         CDEBUG(D_QUOTA, "finish a quota req, try again\n");
481                         continue;
482                 }
483
484                 /* it is out of quota already */
485                 if (rc == -EDQUOT) {
486                         CDEBUG(D_QUOTA, "out of quota,  return -EDQUOT\n");
487                         break;
488                 }
489
490                 /* Related quota has been disabled by master, but enabled by
491                  * slave, do not try again. */
492                 if (unlikely(rc == -ESRCH)) {
493                         CERROR("mismatched quota configuration, stop try.\n");
494                         break;
495                 }
496
497                 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
498                         /* The client has been evicted or tried to
499                          * to reconnect already, abort the request */
500                         RETURN(-ENOTCONN);
501
502                 /* -EBUSY and others, wait a second and try again */
503                 if (rc < 0) {
504                         cfs_waitq_t        waitq;
505                         struct l_wait_info lwi;
506
507                         if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
508                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
509                                        CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
510                         CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
511                                count_err++);
512
513                         cfs_waitq_init(&waitq);
514                         lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
515                                           NULL);
516                         l_wait_event(waitq, 0, &lwi);
517                 }
518
519                 if (rc < 0 || cycle % 10 == 0) {
520                         cfs_spin_lock(&last_print_lock);
521                         if (last_print == 0 ||
522                             cfs_time_before((last_print + cfs_time_seconds(30)),
523                                             cfs_time_current())) {
524                                 last_print = cfs_time_current();
525                                 cfs_spin_unlock(&last_print_lock);
526                                 CWARN("still haven't managed to acquire quota "
527                                       "space from the quota master after %d "
528                                       "retries (err=%d, rc=%d)\n",
529                                       cycle, count_err - 1, rc);
530                         } else {
531                                 cfs_spin_unlock(&last_print_lock);
532                         }
533                 }
534
535                 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
536                        cycle);
537         }
538         cfs_gettimeofday(&work_end);
539         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
540         lprocfs_counter_add(qctxt->lqc_stats,
541                             isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
542                                     LQUOTA_WAIT_FOR_CHK_INO,
543                             timediff);
544
545         if (rc > 0)
546                 rc = 0;
547         RETURN(rc);
548 }
549
550 /**
551  * when a block_write or inode_create rpc is finished, adjust the record for
552  * pending blocks and inodes
553  */
554 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
555                                 int pending[], int isblk)
556 {
557         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
558         struct timeval work_start;
559         struct timeval work_end;
560         long timediff;
561         int i;
562         struct qunit_data qdata[MAXQUOTAS];
563         ENTRY;
564
565         CDEBUG(D_QUOTA, "commit pending quota for  %s\n", obd->obd_name);
566         CLASSERT(MAXQUOTAS < 4);
567         if (!ll_sb_any_quota_active(qctxt->lqc_sb))
568                 RETURN(0);
569
570         cfs_gettimeofday(&work_start);
571         for (i = 0; i < MAXQUOTAS; i++) {
572                 struct lustre_qunit_size *lqs = NULL;
573
574                 LASSERT(pending[i] >= 0);
575                 if (pending[i] == 0)
576                         continue;
577
578                 qdata[i].qd_id = id[i];
579                 qdata[i].qd_flags = i;
580                 if (isblk)
581                         QDATA_SET_BLK(&qdata[i]);
582                 qdata[i].qd_count = 0;
583
584                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
585                         continue;
586
587                 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
588                 if (lqs == NULL || IS_ERR(lqs)) {
589                         CERROR("can not find lqs for pending_commit: "
590                                "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
591                                "maybe cause unexpected lqs refcount error!\n",
592                                id[i], i ? 'g': 'u', pending[i], isblk,
593                                lqs ? PTR_ERR(lqs) : -1);
594                         continue;
595                 }
596
597                 cfs_spin_lock(&lqs->lqs_lock);
598                 if (isblk) {
599                         LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
600                                  "there are too many blocks! [id %u] [%c] "
601                                  "[bwrite_pending %lu] [pending %u]\n",
602                                  id[i], i % 2 ? 'g' : 'u',
603                                  lqs->lqs_bwrite_pending, pending[i]);
604
605                         lqs->lqs_bwrite_pending -= pending[i];
606                 } else {
607                         LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
608                                 "there are too many files! [id %u] [%c] "
609                                 "[iwrite_pending %lu] [pending %u]\n",
610                                 id[i], i % 2 ? 'g' : 'u',
611                                 lqs->lqs_iwrite_pending, pending[i]);
612
613                         lqs->lqs_iwrite_pending -= pending[i];
614                 }
615                 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
616                        obd->obd_name,
617                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
618                        i, pending[i], isblk);
619                 cfs_spin_unlock(&lqs->lqs_lock);
620
621                 /* for quota_search_lqs in pending_commit */
622                 lqs_putref(lqs);
623                 /* for quota_search_lqs in quota_check */
624                 lqs_putref(lqs);
625         }
626         cfs_gettimeofday(&work_end);
627         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
628         lprocfs_counter_add(qctxt->lqc_stats,
629                             isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
630                                     LQUOTA_WAIT_FOR_COMMIT_INO,
631                             timediff);
632
633         RETURN(0);
634 }
635
636 static int mds_quota_init(void)
637 {
638         return lustre_dquot_init();
639 }
640
641 static int mds_quota_exit(void)
642 {
643         lustre_dquot_exit();
644         return 0;
645 }
646
647 static int mds_quota_setup(struct obd_device *obd)
648 {
649         struct obd_device_target *obt = &obd->u.obt;
650         struct mds_obd *mds = &obd->u.mds;
651         int rc;
652         ENTRY;
653
654         if (unlikely(mds->mds_quota)) {
655                 CWARN("try to reinitialize quota context!\n");
656                 RETURN(0);
657         }
658
659         cfs_init_rwsem(&obt->obt_rwsem);
660         obt->obt_qfmt = LUSTRE_QUOTA_V2;
661         mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
662         cfs_sema_init(&obt->obt_quotachecking, 1);
663         /* initialize quota master and quota context */
664         cfs_init_rwsem(&mds->mds_qonoff_sem);
665         rc = qctxt_init(obd, dqacq_handler);
666         if (rc) {
667                 CERROR("%s: initialize quota context failed! (rc:%d)\n",
668                        obd->obd_name, rc);
669                 RETURN(rc);
670         }
671         mds->mds_quota = 1;
672         RETURN(rc);
673 }
674
675 static int mds_quota_cleanup(struct obd_device *obd)
676 {
677         ENTRY;
678         if (unlikely(!obd->u.mds.mds_quota))
679                 RETURN(0);
680
681         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
682         RETURN(0);
683 }
684
685 static int mds_quota_setinfo(struct obd_device *obd, void *data)
686 {
687         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
688         ENTRY;
689
690         if (unlikely(!obd->u.mds.mds_quota))
691                 RETURN(0);
692
693         if (data != NULL)
694                 QUOTA_MASTER_READY(qctxt);
695         else
696                 QUOTA_MASTER_UNREADY(qctxt);
697         RETURN(0);
698 }
699
700 static int mds_quota_fs_cleanup(struct obd_device *obd)
701 {
702         struct mds_obd *mds = &obd->u.mds;
703         struct obd_quotactl oqctl;
704         ENTRY;
705
706         if (unlikely(!mds->mds_quota))
707                 RETURN(0);
708
709         mds->mds_quota = 0;
710         memset(&oqctl, 0, sizeof(oqctl));
711         oqctl.qc_type = UGQUOTA;
712
713         cfs_down_write(&mds->mds_qonoff_sem);
714         mds_admin_quota_off(obd, &oqctl);
715         cfs_up_write(&mds->mds_qonoff_sem);
716         RETURN(0);
717 }
718
719 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
720                                 struct obd_trans_info *oti, int isblk)
721 {
722         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
723         int rc;
724         ENTRY;
725
726         rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
727         RETURN(rc);
728 }
729
730 #endif /* HAVE_QUOTA_SUPPORT */
731 #endif /* __KERNEL__ */
732
733 #ifdef __KERNEL__
734 #ifdef HAVE_QUOTA_SUPPORT
735 quota_interface_t mds_quota_interface = {
736         .quota_init     = mds_quota_init,
737         .quota_exit     = mds_quota_exit,
738         .quota_setup    = mds_quota_setup,
739         .quota_cleanup  = mds_quota_cleanup,
740         .quota_check    = target_quota_check,
741         .quota_ctl      = mds_quota_ctl,
742         .quota_setinfo  = mds_quota_setinfo,
743         .quota_fs_cleanup = mds_quota_fs_cleanup,
744         .quota_recovery = mds_quota_recovery,
745         .quota_adjust   = mds_quota_adjust,
746         .quota_chkquota = quota_chk_acq_common,
747         .quota_acquire  = quota_acquire_common,
748         .quota_pending_commit = quota_pending_commit,
749 };
750
751 quota_interface_t filter_quota_interface = {
752         .quota_setup    = filter_quota_setup,
753         .quota_cleanup  = filter_quota_cleanup,
754         .quota_check    = target_quota_check,
755         .quota_ctl      = filter_quota_ctl,
756         .quota_setinfo  = filter_quota_setinfo,
757         .quota_clearinfo = filter_quota_clearinfo,
758         .quota_enforce  = filter_quota_enforce,
759         .quota_getflag  = filter_quota_getflag,
760         .quota_acquire  = quota_acquire_common,
761         .quota_adjust   = filter_quota_adjust,
762         .quota_chkquota = quota_chk_acq_common,
763         .quota_adjust_qunit   = filter_quota_adjust_qunit,
764         .quota_pending_commit = quota_pending_commit,
765 };
766 #endif
767 #endif /* __KERNEL__ */
768
769 #ifdef __KERNEL__
770
771 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
772
773 static int __init init_lustre_quota(void)
774 {
775 #ifdef HAVE_QUOTA_SUPPORT
776         int rc = 0;
777
778         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
779                                                 proc_lustre_root,
780                                                 NULL, NULL);
781         if (IS_ERR(lquota_type_proc_dir)) {
782                 CERROR("LProcFS failed in lquota-init\n");
783                 rc = PTR_ERR(lquota_type_proc_dir);
784                 return rc;
785         }
786
787         rc = qunit_cache_init();
788         if (rc)
789                 return rc;
790
791         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
792         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
793 #endif
794         return 0;
795 }
796
797 static void /*__exit*/ exit_lustre_quota(void)
798 {
799 #ifdef HAVE_QUOTA_SUPPORT
800         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
801         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
802
803         qunit_cache_cleanup();
804
805         if (lquota_type_proc_dir)
806                 lprocfs_remove(&lquota_type_proc_dir);
807 #endif
808 }
809
810 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
811 MODULE_DESCRIPTION("Lustre Quota");
812 MODULE_LICENSE("GPL");
813
814 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
815
816 #ifdef HAVE_QUOTA_SUPPORT
817 EXPORT_SYMBOL(mds_quota_interface);
818 EXPORT_SYMBOL(filter_quota_interface);
819 #endif
820 #endif /* __KERNEL */