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