Whamcloud - gitweb
b=16890 CERROR cleanups
[fs/lustre-release.git] / lustre / quota / quota_interface.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
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 spinlock_t last_print_lock = 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         init_rwsem(&obt->obt_rwsem);
80         obt->obt_qfmt = LUSTRE_QUOTA_V2;
81         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         spin_lock(&qctxt->lqc_lock);
107         if (qctxt->lqc_import != NULL) {
108                 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                 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         spin_lock(&qctxt->lqc_lock);
150         if (qctxt->lqc_import == imp) {
151                 qctxt->lqc_import = NULL;
152                 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                 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 (lqs == NULL || IS_ERR(lqs)) {
206                         rc = PTR_ERR(lqs);
207                         break;
208                 } else {
209                         spin_lock(&lqs->lqs_lock);
210                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
211                                 oa->o_flags |= (cnt == USRQUOTA) ?
212                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
213                                 spin_unlock(&lqs->lqs_lock);
214                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
215                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
216                                        qctxt->lqc_sync_blk);
217                                 /* this is for quota_search_lqs */
218                                 lqs_putref(lqs);
219                                 continue;
220                         }
221                         spin_unlock(&lqs->lqs_lock);
222                         /* this is for quota_search_lqs */
223                         lqs_putref(lqs);
224                 }
225
226                 memset(oqctl, 0, sizeof(*oqctl));
227
228                 oqctl->qc_cmd = Q_GETQUOTA;
229                 oqctl->qc_type = cnt;
230                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
231                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
232                 if (err) {
233                         if (!rc)
234                                 rc = err;
235                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
236                                                              OBD_MD_FLGRPQUOTA);
237                         continue;
238                 }
239
240                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
241                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
242                     oqctl->qc_dqblk.dqb_bhardlimit))
243                         oa->o_flags |= (cnt == USRQUOTA) ?
244                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
245         }
246         OBD_FREE_PTR(oqctl);
247         RETURN(rc);
248 }
249
250 /**
251  * check whether the left quota of certain uid and gid can satisfy a block_write
252  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
253  */
254 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
255                               int pending[], int count, int cycle, int isblk,
256                               struct inode *inode, int frags)
257 {
258         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
259         int i;
260         struct qunit_data qdata[MAXQUOTAS];
261         int mb = 0;
262         int rc = 0, rc2[2] = { 0, 0 };
263         ENTRY;
264
265         spin_lock(&qctxt->lqc_lock);
266         if (!qctxt->lqc_valid){
267                 spin_unlock(&qctxt->lqc_lock);
268                 RETURN(rc);
269         }
270         spin_unlock(&qctxt->lqc_lock);
271
272         for (i = 0; i < MAXQUOTAS; i++) {
273                 struct lustre_qunit_size *lqs = NULL;
274
275                 qdata[i].qd_id = id[i];
276                 qdata[i].qd_flags = i;
277                 if (isblk)
278                         QDATA_SET_BLK(&qdata[i]);
279                 qdata[i].qd_count = 0;
280
281                 /* ignore root user */
282                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
283                         continue;
284
285                 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
286                 if (lqs == NULL || IS_ERR(lqs))
287                         continue;
288
289                 if (IS_ERR(lqs)) {
290                         CERROR("can not find lqs for check_common: "
291                                "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
292                                id[i], i % 2 ? 'g': 'u', isblk, count,
293                                PTR_ERR(lqs));
294                         RETURN(PTR_ERR(lqs));
295                 }
296
297                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
298                 spin_lock(&lqs->lqs_lock);
299                 if (!cycle) {
300                         if (isblk) {
301                                 pending[i] = count * CFS_PAGE_SIZE;
302                                 /* in order to complete this write, we need extra
303                                  * meta blocks. This function can get it through
304                                  * data needed to be written b=16542 */
305                                 if (inode) {
306                                         mb = pending[i];
307                                         rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
308                                                              &mb, inode,frags);
309                                         if (rc)
310                                                 CERROR("%s: can't get extra "
311                                                        "meta blocks\n",
312                                                        obd->obd_name);
313                                         else
314                                                 pending[i] += mb;
315                                 }
316                                 lqs->lqs_bwrite_pending += pending[i];
317                         } else {
318                                 pending[i] = count;
319                                 lqs->lqs_iwrite_pending += pending[i];
320                         }
321                 }
322
323                 /* if xx_rec < 0, that means quota are releasing,
324                  * and it may return before we use quota. So if
325                  * we find this situation, we assuming it has
326                  * returned b=18491 */
327                 if (isblk && lqs->lqs_blk_rec < 0) {
328                         if (qdata[i].qd_count < -lqs->lqs_blk_rec)
329                                 qdata[i].qd_count = 0;
330                         else
331                                 qdata[i].qd_count += lqs->lqs_blk_rec;
332                 }
333                 if (!isblk && lqs->lqs_ino_rec < 0) {
334                         if (qdata[i].qd_count < -lqs->lqs_ino_rec)
335                                 qdata[i].qd_count = 0;
336                         else
337                                 qdata[i].qd_count += lqs->lqs_ino_rec;
338                 }
339
340                 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
341                        " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
342                        " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
343                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
344                        qdata[i].qd_count, mb, pending[i]);
345                 if (rc2[i] == QUOTA_RET_OK) {
346                         if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
347                                 rc2[i] = QUOTA_RET_ACQUOTA;
348                         if (!isblk && qdata[i].qd_count <
349                             lqs->lqs_iwrite_pending)
350                                 rc2[i] = QUOTA_RET_ACQUOTA;
351                 }
352
353                 spin_unlock(&lqs->lqs_lock);
354
355                 if (lqs->lqs_blk_rec  < 0 &&
356                     qdata[i].qd_count <
357                     lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
358                         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
359
360                 /* When cycle is zero, lqs_*_pending will be changed. We will
361                  * get reference of the lqs here and put reference of lqs in
362                  * quota_pending_commit b=14784 */
363                 if (!cycle)
364                         lqs_getref(lqs);
365
366                 /* this is for quota_search_lqs */
367                 lqs_putref(lqs);
368         }
369
370         if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
371                 RETURN(QUOTA_RET_ACQUOTA);
372         else
373                 RETURN(rc);
374 }
375
376 int quota_is_set(struct obd_device *obd, const unsigned int id[], int flag)
377 {
378         struct lustre_qunit_size *lqs;
379         int i, q_set = 0;
380
381         if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
382                 RETURN(0);
383
384         for (i = 0; i < MAXQUOTAS; i++) {
385                 lqs = quota_search_lqs(LQS_KEY(i, id[i]),
386                                        &obd->u.obt.obt_qctxt, 0);
387                 if (lqs && !IS_ERR(lqs)) {
388                         if (lqs->lqs_flags & flag)
389                                 q_set = 1;
390                         lqs_putref(lqs);
391                 }
392         }
393
394         return q_set;
395 }
396
397 static int quota_chk_acq_common(struct obd_device *obd, struct obd_export *exp,
398                                 const unsigned int id[], int pending[],
399                                 int count, quota_acquire acquire,
400                                 struct obd_trans_info *oti, int isblk,
401                                 struct inode *inode, int frags)
402 {
403         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
404         struct timeval work_start;
405         struct timeval work_end;
406         long timediff;
407         struct l_wait_info lwi = { 0 };
408         int rc = 0, cycle = 0, count_err = 1;
409         ENTRY;
410
411         if (!quota_is_set(obd, id, isblk ? QB_SET : QI_SET))
412                 RETURN(0);
413
414         if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
415                 /* If the client has been evicted or if it
416                  * timed out and tried to reconnect already,
417                  * abort the request immediately */
418                 RETURN(-ENOTCONN);
419
420         CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
421         pending[USRQUOTA] = pending[GRPQUOTA] = 0;
422         /* Unfortunately, if quota master is too busy to handle the
423          * pre-dqacq in time and quota hash on ost is used up, we
424          * have to wait for the completion of in flight dqacq/dqrel,
425          * in order to get enough quota for write b=12588 */
426         do_gettimeofday(&work_start);
427         while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
428                                         inode, frags)) &
429                QUOTA_RET_ACQUOTA) {
430
431                 spin_lock(&qctxt->lqc_lock);
432                 if (!qctxt->lqc_import && oti) {
433                         spin_unlock(&qctxt->lqc_lock);
434
435                         LASSERT(oti && oti->oti_thread &&
436                                 oti->oti_thread->t_watchdog);
437
438                         lc_watchdog_disable(oti->oti_thread->t_watchdog);
439                         CDEBUG(D_QUOTA, "sleep for quota master\n");
440                         l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
441                                      &lwi);
442                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
443                         lc_watchdog_touch(oti->oti_thread->t_watchdog,
444                                  GET_TIMEOUT(oti->oti_thread->t_svc));
445                 } else {
446                         spin_unlock(&qctxt->lqc_lock);
447                 }
448
449                 cycle++;
450                 if (isblk)
451                         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
452                 /* after acquire(), we should run quota_check_common again
453                  * so that we confirm there are enough quota to finish write */
454                 rc = acquire(obd, id, oti, isblk);
455
456                 /* please reference to dqacq_completion for the below */
457                 /* a new request is finished, try again */
458                 if (rc == QUOTA_REQ_RETURNED) {
459                         CDEBUG(D_QUOTA, "finish a quota req, try again\n");
460                         continue;
461                 }
462
463                 /* it is out of quota already */
464                 if (rc == -EDQUOT) {
465                         CDEBUG(D_QUOTA, "out of quota,  return -EDQUOT\n");
466                         break;
467                 }
468
469                 /* Related quota has been disabled by master, but enabled by
470                  * slave, do not try again. */
471                 if (unlikely(rc == -ESRCH)) {
472                         CERROR("mismatched quota configuration, stop try.\n");
473                         break;
474                 }
475
476                 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
477                         /* The client has been evicted or tried to
478                          * to reconnect already, abort the request */
479                         RETURN(-ENOTCONN);
480
481                 /* -EBUSY and others, wait a second and try again */
482                 if (rc < 0) {
483                         cfs_waitq_t        waitq;
484                         struct l_wait_info lwi;
485
486                         if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
487                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
488                                          GET_TIMEOUT(oti->oti_thread->t_svc));
489                         CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
490                                count_err++);
491
492                         init_waitqueue_head(&waitq);
493                         lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
494                                           NULL);
495                         l_wait_event(waitq, 0, &lwi);
496                 }
497
498                 if (rc < 0 || cycle % 10 == 0) {
499                         spin_lock(&last_print_lock);
500                         if (last_print == 0 ||
501                             cfs_time_before((last_print + cfs_time_seconds(30)),
502                                             cfs_time_current())) {
503                                 last_print = cfs_time_current();
504                                 spin_unlock(&last_print_lock);
505                                 CWARN("still haven't managed to acquire quota "
506                                       "space from the quota master after %d "
507                                       "retries (err=%d, rc=%d)\n",
508                                       cycle, count_err - 1, rc);
509                         } else {
510                                 spin_unlock(&last_print_lock);
511                         }
512                 }
513
514                 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
515                        cycle);
516         }
517         do_gettimeofday(&work_end);
518         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
519         lprocfs_counter_add(qctxt->lqc_stats,
520                             isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
521                                     LQUOTA_WAIT_FOR_CHK_INO,
522                             timediff);
523
524         if (rc > 0)
525                 rc = 0;
526         RETURN(rc);
527 }
528
529 /**
530  * when a block_write or inode_create rpc is finished, adjust the record for
531  * pending blocks and inodes
532  */
533 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
534                                 int pending[], int isblk)
535 {
536         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
537         struct timeval work_start;
538         struct timeval work_end;
539         long timediff;
540         int i;
541         struct qunit_data qdata[MAXQUOTAS];
542         ENTRY;
543
544         CDEBUG(D_QUOTA, "commit pending quota for  %s\n", obd->obd_name);
545         CLASSERT(MAXQUOTAS < 4);
546         if (!ll_sb_any_quota_active(qctxt->lqc_sb))
547                 RETURN(0);
548
549         do_gettimeofday(&work_start);
550         for (i = 0; i < MAXQUOTAS; i++) {
551                 struct lustre_qunit_size *lqs = NULL;
552
553                 LASSERT(pending[i] >= 0);
554                 if (pending[i] == 0)
555                         continue;
556
557                 qdata[i].qd_id = id[i];
558                 qdata[i].qd_flags = i;
559                 if (isblk)
560                         QDATA_SET_BLK(&qdata[i]);
561                 qdata[i].qd_count = 0;
562
563                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
564                         continue;
565
566                 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
567                 if (lqs == NULL || IS_ERR(lqs)) {
568                         CERROR("can not find lqs for pending_commit: "
569                                "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
570                                "maybe cause unexpected lqs refcount error!\n",
571                                id[i], i ? 'g': 'u', pending[i], isblk,
572                                lqs ? PTR_ERR(lqs) : -1);
573                         continue;
574                 }
575
576                 spin_lock(&lqs->lqs_lock);
577                 if (isblk) {
578                         LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
579                                  "there are too many blocks! [id %u] [%c] "
580                                  "[bwrite_pending %lu] [pending %u]\n",
581                                  id[i], i % 2 ? 'g' : 'u',
582                                  lqs->lqs_bwrite_pending, pending[i]);
583
584                         lqs->lqs_bwrite_pending -= pending[i];
585                 } else {
586                         LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
587                                 "there are too many files! [id %u] [%c] "
588                                 "[iwrite_pending %lu] [pending %u]\n",
589                                 id[i], i % 2 ? 'g' : 'u',
590                                 lqs->lqs_iwrite_pending, pending[i]);
591
592                         lqs->lqs_iwrite_pending -= pending[i];
593                 }
594                 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
595                        obd->obd_name,
596                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
597                        i, pending[i], isblk);
598                 spin_unlock(&lqs->lqs_lock);
599
600                 /* for quota_search_lqs in pending_commit */
601                 lqs_putref(lqs);
602                 /* for quota_search_lqs in quota_check */
603                 lqs_putref(lqs);
604         }
605         do_gettimeofday(&work_end);
606         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
607         lprocfs_counter_add(qctxt->lqc_stats,
608                             isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
609                                     LQUOTA_WAIT_FOR_COMMIT_INO,
610                             timediff);
611
612         RETURN(0);
613 }
614
615 static int mds_quota_init(void)
616 {
617         return lustre_dquot_init();
618 }
619
620 static int mds_quota_exit(void)
621 {
622         lustre_dquot_exit();
623         return 0;
624 }
625
626 static int mds_quota_setup(struct obd_device *obd)
627 {
628         struct obd_device_target *obt = &obd->u.obt;
629         struct mds_obd *mds = &obd->u.mds;
630         int rc;
631         ENTRY;
632
633         if (unlikely(mds->mds_quota)) {
634                 CWARN("try to reinitialize quota context!\n");
635                 RETURN(0);
636         }
637
638         init_rwsem(&obt->obt_rwsem);
639         obt->obt_qfmt = LUSTRE_QUOTA_V2;
640         mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
641         sema_init(&obt->obt_quotachecking, 1);
642         /* initialize quota master and quota context */
643         sema_init(&mds->mds_qonoff_sem, 1);
644         rc = qctxt_init(obd, dqacq_handler);
645         if (rc) {
646                 CERROR("%s: initialize quota context failed! (rc:%d)\n",
647                        obd->obd_name, rc);
648                 RETURN(rc);
649         }
650         mds->mds_quota = 1;
651         RETURN(rc);
652 }
653
654 static int mds_quota_cleanup(struct obd_device *obd)
655 {
656         ENTRY;
657         if (unlikely(!obd->u.mds.mds_quota))
658                 RETURN(0);
659
660         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
661         RETURN(0);
662 }
663
664 static int mds_quota_setinfo(struct obd_device *obd, void *data)
665 {
666         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
667         ENTRY;
668
669         if (unlikely(!obd->u.mds.mds_quota))
670                 RETURN(0);
671
672         if (data != NULL)
673                 QUOTA_MASTER_READY(qctxt);
674         else
675                 QUOTA_MASTER_UNREADY(qctxt);
676         RETURN(0);
677 }
678
679 static int mds_quota_fs_cleanup(struct obd_device *obd)
680 {
681         struct mds_obd *mds = &obd->u.mds;
682         struct obd_quotactl oqctl;
683         ENTRY;
684
685         if (unlikely(!mds->mds_quota))
686                 RETURN(0);
687
688         mds->mds_quota = 0;
689         memset(&oqctl, 0, sizeof(oqctl));
690         oqctl.qc_type = UGQUOTA;
691
692         down(&mds->mds_qonoff_sem);
693         mds_admin_quota_off(obd, &oqctl);
694         up(&mds->mds_qonoff_sem);
695         RETURN(0);
696 }
697
698 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
699                                 struct obd_trans_info *oti, int isblk)
700 {
701         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
702         int rc;
703         ENTRY;
704
705         rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
706         RETURN(rc);
707 }
708
709 #endif /* HAVE_QUOTA_SUPPORT */
710 #endif /* __KERNEL__ */
711
712 struct osc_quota_info {
713         struct list_head        oqi_hash;       /* hash list */
714         struct client_obd      *oqi_cli;        /* osc obd */
715         unsigned int            oqi_id;         /* uid/gid of a file */
716         short                   oqi_type;       /* quota type */
717 };
718
719 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
720
721 static struct list_head qinfo_hash[NR_DQHASH];
722 /* SLAB cache for client quota context */
723 cfs_mem_cache_t *qinfo_cachep = NULL;
724
725 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
726                          __attribute__((__const__));
727
728 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
729 {
730         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
731         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
732         return tmp;
733 }
734
735 /* caller must hold qinfo_list_lock */
736 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
737 {
738         struct list_head *head = qinfo_hash +
739                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
740
741         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
742         list_add(&oqi->oqi_hash, head);
743 }
744
745 /* caller must hold qinfo_list_lock */
746 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
747 {
748         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
749         list_del_init(&oqi->oqi_hash);
750 }
751
752 /* caller must hold qinfo_list_lock */
753 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
754                                                 unsigned int id, int type)
755 {
756         unsigned int hashent = hashfn(cli, id, type);
757         struct osc_quota_info *oqi;
758         ENTRY;
759
760         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
761         list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
762                 if (oqi->oqi_cli == cli &&
763                     oqi->oqi_id == id && oqi->oqi_type == type)
764                         return oqi;
765         }
766         RETURN(NULL);
767 }
768
769 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
770                                           unsigned int id, int type)
771 {
772         struct osc_quota_info *oqi;
773         ENTRY;
774
775         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
776         if(!oqi)
777                 RETURN(NULL);
778
779         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
780         oqi->oqi_cli = cli;
781         oqi->oqi_id = id;
782         oqi->oqi_type = type;
783
784         RETURN(oqi);
785 }
786
787 static void free_qinfo(struct osc_quota_info *oqi)
788 {
789         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
790 }
791
792 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
793 {
794         unsigned int id;
795         int cnt, rc = QUOTA_OK;
796         ENTRY;
797
798         spin_lock(&qinfo_list_lock);
799         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
800                 struct osc_quota_info *oqi = NULL;
801
802                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
803                 oqi = find_qinfo(cli, id, cnt);
804                 if (oqi) {
805                         rc = NO_QUOTA;
806                         break;
807                 }
808         }
809         spin_unlock(&qinfo_list_lock);
810
811         RETURN(rc);
812 }
813
814 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
815                     obd_flag valid, obd_flag flags)
816 {
817         unsigned int id;
818         obd_flag noquota;
819         int cnt, rc = 0;
820         ENTRY;
821
822
823         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
824                 struct osc_quota_info *oqi, *old;
825
826                 if (!(valid & ((cnt == USRQUOTA) ?
827                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
828                         continue;
829
830                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
831                 noquota = (cnt == USRQUOTA) ?
832                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
833
834                 oqi = alloc_qinfo(cli, id, cnt);
835                 if (!oqi) {
836                         rc = -ENOMEM;
837                         break;
838                 }
839
840                 spin_lock(&qinfo_list_lock);
841                 old = find_qinfo(cli, id, cnt);
842                 if (old && !noquota)
843                         remove_qinfo_hash(old);
844                 else if (!old && noquota)
845                         insert_qinfo_hash(oqi);
846                 spin_unlock(&qinfo_list_lock);
847
848                 if (old || !noquota)
849                         free_qinfo(oqi);
850                 if (old && !noquota)
851                         free_qinfo(old);
852         }
853
854         RETURN(rc);
855 }
856
857 int osc_quota_cleanup(struct obd_device *obd)
858 {
859         struct client_obd *cli = &obd->u.cli;
860         struct osc_quota_info *oqi, *n;
861         int i;
862         ENTRY;
863
864         spin_lock(&qinfo_list_lock);
865         for (i = 0; i < NR_DQHASH; i++) {
866                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
867                         if (oqi->oqi_cli != cli)
868                                 continue;
869                         remove_qinfo_hash(oqi);
870                         free_qinfo(oqi);
871                 }
872         }
873         spin_unlock(&qinfo_list_lock);
874
875         RETURN(0);
876 }
877
878 int osc_quota_init(void)
879 {
880         int i;
881         ENTRY;
882
883         LASSERT(qinfo_cachep == NULL);
884         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
885                                             sizeof(struct osc_quota_info),
886                                             0, 0);
887         if (!qinfo_cachep)
888                 RETURN(-ENOMEM);
889
890         for (i = 0; i < NR_DQHASH; i++)
891                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
892
893         RETURN(0);
894 }
895
896 int osc_quota_exit(void)
897 {
898         struct osc_quota_info *oqi, *n;
899         int i, rc;
900         ENTRY;
901
902         spin_lock(&qinfo_list_lock);
903         for (i = 0; i < NR_DQHASH; i++) {
904                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
905                         remove_qinfo_hash(oqi);
906                         free_qinfo(oqi);
907                 }
908         }
909         spin_unlock(&qinfo_list_lock);
910
911         rc = cfs_mem_cache_destroy(qinfo_cachep);
912         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
913         qinfo_cachep = NULL;
914
915         RETURN(0);
916 }
917
918 #ifdef __KERNEL__
919 #ifdef HAVE_QUOTA_SUPPORT
920 quota_interface_t mds_quota_interface = {
921         .quota_init     = mds_quota_init,
922         .quota_exit     = mds_quota_exit,
923         .quota_setup    = mds_quota_setup,
924         .quota_cleanup  = mds_quota_cleanup,
925         .quota_check    = target_quota_check,
926         .quota_ctl      = mds_quota_ctl,
927         .quota_setinfo  = mds_quota_setinfo,
928         .quota_fs_cleanup = mds_quota_fs_cleanup,
929         .quota_recovery = mds_quota_recovery,
930         .quota_adjust   = mds_quota_adjust,
931         .quota_chkquota = quota_chk_acq_common,
932         .quota_acquire  = quota_acquire_common,
933         .quota_pending_commit = quota_pending_commit,
934 };
935
936 quota_interface_t filter_quota_interface = {
937         .quota_setup    = filter_quota_setup,
938         .quota_cleanup  = filter_quota_cleanup,
939         .quota_check    = target_quota_check,
940         .quota_ctl      = filter_quota_ctl,
941         .quota_setinfo  = filter_quota_setinfo,
942         .quota_clearinfo = filter_quota_clearinfo,
943         .quota_enforce  = filter_quota_enforce,
944         .quota_getflag  = filter_quota_getflag,
945         .quota_acquire  = quota_acquire_common,
946         .quota_adjust   = filter_quota_adjust,
947         .quota_chkquota = quota_chk_acq_common,
948         .quota_adjust_qunit   = filter_quota_adjust_qunit,
949         .quota_pending_commit = quota_pending_commit,
950 };
951 #endif
952 #endif /* __KERNEL__ */
953
954 quota_interface_t mdc_quota_interface = {
955         .quota_ctl      = client_quota_ctl,
956         .quota_check    = client_quota_check,
957         .quota_poll_check = client_quota_poll_check,
958 };
959
960 quota_interface_t lmv_quota_interface = {
961         .quota_ctl      = lmv_quota_ctl,
962         .quota_check    = lmv_quota_check,
963 };
964
965 quota_interface_t osc_quota_interface = {
966         .quota_ctl      = client_quota_ctl,
967         .quota_check    = client_quota_check,
968         .quota_poll_check = client_quota_poll_check,
969         .quota_init     = osc_quota_init,
970         .quota_exit     = osc_quota_exit,
971         .quota_chkdq    = osc_quota_chkdq,
972         .quota_setdq    = osc_quota_setdq,
973         .quota_cleanup  = osc_quota_cleanup,
974         .quota_adjust_qunit = client_quota_adjust_qunit,
975 };
976
977 quota_interface_t lov_quota_interface = {
978         .quota_ctl      = lov_quota_ctl,
979         .quota_check    = lov_quota_check,
980         .quota_adjust_qunit = lov_quota_adjust_qunit,
981 };
982
983 #ifdef __KERNEL__
984
985 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
986
987 static int __init init_lustre_quota(void)
988 {
989 #ifdef HAVE_QUOTA_SUPPORT
990         int rc = 0;
991
992         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
993                                                 proc_lustre_root,
994                                                 NULL, NULL);
995         if (IS_ERR(lquota_type_proc_dir)) {
996                 CERROR("LProcFS failed in lquota-init\n");
997                 rc = PTR_ERR(lquota_type_proc_dir);
998                 return rc;
999         }
1000
1001         rc = qunit_cache_init();
1002         if (rc)
1003                 return rc;
1004
1005         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
1006         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
1007 #endif
1008         PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
1009         PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
1010         PORTAL_SYMBOL_REGISTER(osc_quota_interface);
1011         PORTAL_SYMBOL_REGISTER(lov_quota_interface);
1012         return 0;
1013 }
1014
1015 static void /*__exit*/ exit_lustre_quota(void)
1016 {
1017         PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
1018         PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
1019         PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
1020         PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
1021 #ifdef HAVE_QUOTA_SUPPORT
1022         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
1023         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
1024
1025         qunit_cache_cleanup();
1026
1027         if (lquota_type_proc_dir)
1028                 lprocfs_remove(&lquota_type_proc_dir);
1029 #endif
1030 }
1031
1032 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1033 MODULE_DESCRIPTION("Lustre Quota");
1034 MODULE_LICENSE("GPL");
1035
1036 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
1037
1038 #ifdef HAVE_QUOTA_SUPPORT
1039 EXPORT_SYMBOL(mds_quota_interface);
1040 EXPORT_SYMBOL(filter_quota_interface);
1041 #endif
1042 EXPORT_SYMBOL(mdc_quota_interface);
1043 EXPORT_SYMBOL(lmv_quota_interface);
1044 EXPORT_SYMBOL(osc_quota_interface);
1045 EXPORT_SYMBOL(lov_quota_interface);
1046 #endif /* __KERNEL */