Whamcloud - gitweb
5e1c51b7ab609173dab8fd35a0254b4e6dcd58c1
[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 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
49 #  include <linux/smp_lock.h>
50 #  include <linux/buffer_head.h>
51 #  include <linux/workqueue.h>
52 #  include <linux/mount.h>
53 # else
54 #  include <linux/locks.h>
55 # endif
56 #else /* __KERNEL__ */
57 # include <liblustre.h>
58 #endif
59
60 #include <obd_class.h>
61 #include <lustre_mds.h>
62 #include <lustre_dlm.h>
63 #include <lustre_cfg.h>
64 #include <obd_ost.h>
65 #include <lustre_fsfilt.h>
66 #include <lustre_quota.h>
67 #include <lprocfs_status.h>
68 #include "quota_internal.h"
69
70 #ifdef __KERNEL__
71
72 #ifdef HAVE_QUOTA_SUPPORT
73
74 static cfs_time_t last_print = 0;
75 static spinlock_t last_print_lock = SPIN_LOCK_UNLOCKED;
76
77 static int filter_quota_setup(struct obd_device *obd)
78 {
79         int rc = 0;
80         struct obd_device_target *obt = &obd->u.obt;
81         ENTRY;
82
83         init_rwsem(&obt->obt_rwsem);
84         obt->obt_qfmt = LUSTRE_QUOTA_V2;
85         atomic_set(&obt->obt_quotachecking, 1);
86         rc = qctxt_init(obd, NULL);
87         if (rc)
88                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
89
90         RETURN(rc);
91 }
92
93 static int filter_quota_cleanup(struct obd_device *obd)
94 {
95         ENTRY;
96         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
97         RETURN(0);
98 }
99
100 static int filter_quota_setinfo(struct obd_device *obd, void *data)
101 {
102         struct obd_export *exp = data;
103         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
104         struct obd_import *imp;
105         ENTRY;
106
107         /* setup the quota context import */
108         spin_lock(&qctxt->lqc_lock);
109         qctxt->lqc_import = exp->exp_imp_reverse;
110         spin_unlock(&qctxt->lqc_lock);
111         CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated now, \n",
112                obd->obd_name,exp->exp_imp_reverse, obd);
113
114         /* make imp's connect flags equal relative exp's connect flags
115          * adding it to avoid the scan export list
116          */
117         imp = qctxt->lqc_import;
118         if (likely(imp))
119                 imp->imp_connect_data.ocd_connect_flags |=
120                         (exp->exp_connect_flags &
121                          (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
122
123         cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
124         /* start quota slave recovery thread. (release high limits) */
125         qslave_start_recovery(obd, qctxt);
126         RETURN(0);
127 }
128
129 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
130 {
131         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
132         ENTRY;
133
134         /* lquota may be not set up before destroying export, b=14896 */
135         if (!obd->obd_set_up)
136                 RETURN(0);
137
138         /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
139          * should be invalid b=12374 */
140         if (qctxt->lqc_import && qctxt->lqc_import == exp->exp_imp_reverse) {
141                 spin_lock(&qctxt->lqc_lock);
142                 qctxt->lqc_import = NULL;
143                 spin_unlock(&qctxt->lqc_lock);
144                 CDEBUG(D_QUOTA, "%s: lqc_import of obd(%p) is invalid now.\n",
145                        obd->obd_name, obd);
146         }
147         RETURN(0);
148 }
149
150 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
151 {
152         ENTRY;
153
154         if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
155                 RETURN(0);
156
157         if (ignore) {
158                 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
159                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
160         } else {
161                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
162         }
163
164         RETURN(0);
165 }
166
167 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
168 {
169         struct obd_device_target *obt = &obd->u.obt;
170         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
171         int err, cnt, rc = 0;
172         struct obd_quotactl *oqctl;
173         ENTRY;
174
175         if (!sb_any_quota_enabled(obt->obt_sb))
176                 RETURN(0);
177
178         OBD_ALLOC_PTR(oqctl);
179         if (!oqctl) {
180                 CERROR("Not enough memory!");
181                 RETURN(-ENOMEM);
182         }
183
184         /* set over quota flags for a uid/gid */
185         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
186         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
187
188         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
189                 struct quota_adjust_qunit oqaq_tmp;
190                 struct lustre_qunit_size *lqs = NULL;
191
192                 oqaq_tmp.qaq_flags = cnt;
193                 oqaq_tmp.qaq_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
194
195                 quota_search_lqs(NULL, &oqaq_tmp, qctxt, &lqs);
196                 if (lqs) {
197                         spin_lock(&lqs->lqs_lock);
198                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
199                                 oa->o_flags |= (cnt == USRQUOTA) ?
200                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
201                                 spin_unlock(&lqs->lqs_lock);
202                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
203                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
204                                        qctxt->lqc_sync_blk);
205                                 /* this is for quota_search_lqs */
206                                 lqs_putref(lqs);
207                                 continue;
208                         }
209                         spin_unlock(&lqs->lqs_lock);
210                         /* this is for quota_search_lqs */
211                         lqs_putref(lqs);
212                 }
213
214                 memset(oqctl, 0, sizeof(*oqctl));
215
216                 oqctl->qc_cmd = Q_GETQUOTA;
217                 oqctl->qc_type = cnt;
218                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
219                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
220                 if (err) {
221                         if (!rc)
222                                 rc = err;
223                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
224                                                              OBD_MD_FLGRPQUOTA);
225                         continue;
226                 }
227
228                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
229                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
230                     oqctl->qc_dqblk.dqb_bhardlimit))
231                         oa->o_flags |= (cnt == USRQUOTA) ?
232                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
233         }
234         OBD_FREE_PTR(oqctl);
235         RETURN(rc);
236 }
237
238 /**
239  * check whether the left quota of certain uid and gid can satisfy a block_write
240  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
241  */
242 static int quota_check_common(struct obd_device *obd, unsigned int uid,
243                               unsigned int gid, int count, int cycle, int isblk)
244 {
245         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
246         int i;
247         __u32 id[MAXQUOTAS] = { uid, gid };
248         struct qunit_data qdata[MAXQUOTAS];
249         int rc = 0, rc2[2] = { 0, 0 };
250         ENTRY;
251
252         CLASSERT(MAXQUOTAS < 4);
253         if (!sb_any_quota_enabled(qctxt->lqc_sb))
254                 RETURN(rc);
255
256         spin_lock(&qctxt->lqc_lock);
257         if (!qctxt->lqc_valid){
258                 spin_unlock(&qctxt->lqc_lock);
259                 RETURN(rc);
260         }
261         spin_unlock(&qctxt->lqc_lock);
262
263         for (i = 0; i < MAXQUOTAS; i++) {
264                 struct lustre_qunit_size *lqs = NULL;
265
266                 qdata[i].qd_id = id[i];
267                 qdata[i].qd_flags = i;
268                 if (isblk)
269                         QDATA_SET_BLK(&qdata[i]);
270                 qdata[i].qd_count = 0;
271
272                 /* ignore root user */
273                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
274                         continue;
275
276                 quota_search_lqs(&qdata[i], NULL, qctxt, &lqs);
277                 if (!lqs)
278                         continue;
279
280                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
281                 spin_lock(&lqs->lqs_lock);
282                 if (!cycle) {
283                         rc = QUOTA_RET_INC_PENDING;
284                         if (isblk)
285                                 lqs->lqs_bwrite_pending += count;
286                         else
287                                 lqs->lqs_iwrite_pending += count;
288                 }
289                 if (rc2[i] == QUOTA_RET_OK) {
290                         if (isblk && qdata[i].qd_count <
291                             lqs->lqs_bwrite_pending * CFS_PAGE_SIZE)
292                                 rc2[i] = QUOTA_RET_ACQUOTA;
293                         if (!isblk && qdata[i].qd_count <
294                             lqs->lqs_iwrite_pending)
295                                 rc2[i] = QUOTA_RET_ACQUOTA;
296                 }
297                 spin_unlock(&lqs->lqs_lock);
298                 CDEBUG(D_QUOTA, "count: %d, write pending: %lu, qd_count: "LPU64
299                        ".\n", count,
300                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
301                        qdata[i].qd_count);
302
303                 /* When cycle is zero, lqs_*_pending will be changed. We will
304                  * get reference of the lqs here and put reference of lqs in
305                  * quota_pending_commit b=14784 */
306                 if (!cycle)
307                         lqs_getref(lqs);
308
309                 /* this is for quota_search_lqs */
310                 lqs_putref(lqs);
311         }
312
313         if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
314                 RETURN(rc | QUOTA_RET_ACQUOTA);
315         else
316                 RETURN(rc);
317 }
318
319 static int quota_chk_acq_common(struct obd_device *obd, unsigned int uid,
320                                 unsigned int gid, int count, int *pending,
321                                 quota_acquire acquire,
322                                 struct obd_trans_info *oti, int isblk)
323 {
324         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
325         struct timeval work_start;
326         struct timeval work_end;
327         long timediff;
328         struct l_wait_info lwi = { 0 };
329         int rc = 0, cycle = 0, count_err = 1;
330         ENTRY;
331
332         CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
333         *pending = 0;
334         /* Unfortunately, if quota master is too busy to handle the
335          * pre-dqacq in time and quota hash on ost is used up, we
336          * have to wait for the completion of in flight dqacq/dqrel,
337          * in order to get enough quota for write b=12588 */
338         do_gettimeofday(&work_start);
339         while ((rc = quota_check_common(obd, uid, gid, count, cycle, isblk)) &
340                QUOTA_RET_ACQUOTA) {
341
342                 spin_lock(&qctxt->lqc_lock);
343                 if (!qctxt->lqc_import && oti) {
344                         spin_unlock(&qctxt->lqc_lock);
345
346                         LASSERT(oti && oti->oti_thread &&
347                                 oti->oti_thread->t_watchdog);
348
349                         lc_watchdog_disable(oti->oti_thread->t_watchdog);
350                         CDEBUG(D_QUOTA, "sleep for quota master\n");
351                         l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
352                                      &lwi);
353                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
354                         lc_watchdog_touch(oti->oti_thread->t_watchdog);
355                 } else {
356                         spin_unlock(&qctxt->lqc_lock);
357                 }
358
359                 if (rc & QUOTA_RET_INC_PENDING)
360                         *pending = 1;
361
362                 cycle++;
363                 if (isblk)
364                         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
365                 /* after acquire(), we should run quota_check_common again
366                  * so that we confirm there are enough quota to finish write */
367                 rc = acquire(obd, uid, gid, oti, isblk);
368
369                 /* please reference to dqacq_completion for the below */
370                 /* a new request is finished, try again */
371                 if (rc == -EAGAIN) {
372                         CDEBUG(D_QUOTA, "finish a quota req, try again\n");
373                         continue;
374                 }
375
376                 /* it is out of quota already */
377                 if (rc == -EDQUOT) {
378                         CDEBUG(D_QUOTA, "out of quota,  return -EDQUOT\n");
379                         break;
380                 }
381
382                 /* -EBUSY and others, wait a second and try again */
383                 if (rc < 0) {
384                         cfs_waitq_t        waitq;
385                         struct l_wait_info lwi;
386
387                         if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
388                                 lc_watchdog_touch(oti->oti_thread->t_watchdog);
389                         CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
390                                count_err++);
391
392                         init_waitqueue_head(&waitq);
393                         lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
394                                           NULL);
395                         l_wait_event(waitq, 0, &lwi);
396                 }
397
398                 if (rc < 0 || cycle % 10 == 2) {
399                         spin_lock(&last_print_lock);
400                         if (last_print == 0 ||
401                             cfs_time_before((last_print + cfs_time_seconds(30)),
402                                             cfs_time_current())) {
403                                 last_print = cfs_time_current();
404                                 spin_unlock(&last_print_lock);
405                                 CWARN("still haven't managed to acquire quota "
406                                       "space from the quota master after %d "
407                                       "retries (err=%d, rc=%d)\n",
408                                       cycle, count_err - 1, rc);
409                         } else {
410                                 spin_unlock(&last_print_lock);
411                         }
412                 }
413
414                 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
415                        cycle);
416         }
417
418         if (!cycle && rc & QUOTA_RET_INC_PENDING)
419                 *pending = 1;
420
421         do_gettimeofday(&work_end);
422         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
423         lprocfs_counter_add(qctxt->lqc_stats,
424                             isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
425                                     LQUOTA_WAIT_FOR_CHK_INO,
426                             timediff);
427
428         RETURN(rc);
429 }
430
431 /**
432  * when a block_write or inode_create rpc is finished, adjust the record for
433  * pending blocks and inodes
434  */
435 static int quota_pending_commit(struct obd_device *obd, unsigned int uid,
436                                 unsigned int gid, int count, int isblk)
437 {
438         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
439         struct timeval work_start;
440         struct timeval work_end;
441         long timediff;
442         int i;
443         __u32 id[MAXQUOTAS] = { uid, gid };
444         struct qunit_data qdata[MAXQUOTAS];
445         ENTRY;
446
447         CDEBUG(D_QUOTA, "commit pending quota for  %s\n", obd->obd_name);
448         CLASSERT(MAXQUOTAS < 4);
449         if (!sb_any_quota_enabled(qctxt->lqc_sb))
450                 RETURN(0);
451
452         do_gettimeofday(&work_start);
453         for (i = 0; i < MAXQUOTAS; i++) {
454                 struct lustre_qunit_size *lqs = NULL;
455
456                 qdata[i].qd_id = id[i];
457                 qdata[i].qd_flags = i;
458                 if (isblk)
459                         QDATA_SET_BLK(&qdata[i]);
460                 qdata[i].qd_count = 0;
461
462                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
463                         continue;
464
465                 quota_search_lqs(&qdata[i], NULL, qctxt, &lqs);
466                 if (lqs) {
467                         int flag = 0;
468                         CDEBUG(D_QUOTA, "pending: %lu, count: %d.\n",
469                                isblk ? lqs->lqs_bwrite_pending :
470                                lqs->lqs_iwrite_pending, count);
471                         spin_lock(&lqs->lqs_lock);
472                         if (isblk) {
473                                 if (lqs->lqs_bwrite_pending >= count) {
474                                         lqs->lqs_bwrite_pending -= count;
475                                         spin_unlock(&lqs->lqs_lock);
476                                         flag = 1;
477                                 } else {
478                                         spin_unlock(&lqs->lqs_lock);
479                                         CDEBUG(D_ERROR,
480                                                "there are too many blocks!\n");
481                                 }
482                         } else {
483                                 if (lqs->lqs_iwrite_pending >= count) {
484                                         lqs->lqs_iwrite_pending -= count;
485                                         spin_unlock(&lqs->lqs_lock);
486                                         flag = 1;
487                                 } else {
488                                         spin_unlock(&lqs->lqs_lock);
489                                         CDEBUG(D_ERROR,
490                                                "there are too many files!\n");
491                                 }
492                         }
493
494                         lqs_putref(lqs);
495                         /* When lqs_*_pening is changed back, we'll putref lqs
496                          * here b=14784 */
497                         if (flag)
498                                 lqs_putref(lqs);
499                 }
500         }
501         do_gettimeofday(&work_end);
502         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
503         lprocfs_counter_add(qctxt->lqc_stats,
504                             isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
505                                     LQUOTA_WAIT_FOR_COMMIT_INO,
506                             timediff);
507
508         RETURN(0);
509 }
510
511 static int mds_quota_init(void)
512 {
513         return lustre_dquot_init();
514 }
515
516 static int mds_quota_exit(void)
517 {
518         lustre_dquot_exit();
519         return 0;
520 }
521
522 static int mds_quota_setup(struct obd_device *obd)
523 {
524         struct obd_device_target *obt = &obd->u.obt;
525         struct mds_obd *mds = &obd->u.mds;
526         int rc;
527         ENTRY;
528
529         if (unlikely(mds->mds_quota)) {
530                 CWARN("try to reinitialize quota context!\n");
531                 RETURN(0);
532         }
533
534         init_rwsem(&obt->obt_rwsem);
535         obt->obt_qfmt = LUSTRE_QUOTA_V2;
536         mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
537         atomic_set(&obt->obt_quotachecking, 1);
538         /* initialize quota master and quota context */
539         sema_init(&mds->mds_qonoff_sem, 1);
540         rc = qctxt_init(obd, dqacq_handler);
541         if (rc) {
542                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
543                 RETURN(rc);
544         }
545         mds->mds_quota = 1;
546         RETURN(rc);
547 }
548
549 static int mds_quota_cleanup(struct obd_device *obd)
550 {
551         ENTRY;
552         if (unlikely(!obd->u.mds.mds_quota))
553                 RETURN(0);
554
555         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
556         RETURN(0);
557 }
558
559 static int mds_quota_setinfo(struct obd_device *obd, void *data)
560 {
561         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
562         ENTRY;
563
564         if (unlikely(!obd->u.mds.mds_quota))
565                 RETURN(0);
566
567         if (data != NULL)
568                 QUOTA_MASTER_READY(qctxt);
569         else
570                 QUOTA_MASTER_UNREADY(qctxt);
571         RETURN(0);
572 }
573
574 static int mds_quota_fs_cleanup(struct obd_device *obd)
575 {
576         struct mds_obd *mds = &obd->u.mds;
577         struct obd_quotactl oqctl;
578         ENTRY;
579
580         if (unlikely(!mds->mds_quota))
581                 RETURN(0);
582
583         mds->mds_quota = 0;
584         memset(&oqctl, 0, sizeof(oqctl));
585         oqctl.qc_type = UGQUOTA;
586
587         down(&mds->mds_qonoff_sem);
588         mds_admin_quota_off(obd, &oqctl);
589         up(&mds->mds_qonoff_sem);
590         RETURN(0);
591 }
592
593 static int quota_acquire_common(struct obd_device *obd, unsigned int uid,
594                                 unsigned int gid, struct obd_trans_info *oti,
595                                 int isblk)
596 {
597         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
598         int rc;
599         ENTRY;
600
601         rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, isblk, 1, oti);
602         RETURN(rc);
603 }
604
605 #endif /* HAVE_QUOTA_SUPPORT */
606 #endif /* __KERNEL__ */
607
608 struct osc_quota_info {
609         struct list_head        oqi_hash;       /* hash list */
610         struct client_obd      *oqi_cli;        /* osc obd */
611         unsigned int            oqi_id;         /* uid/gid of a file */
612         short                   oqi_type;       /* quota type */
613 };
614
615 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
616
617 static struct list_head qinfo_hash[NR_DQHASH];
618 /* SLAB cache for client quota context */
619 cfs_mem_cache_t *qinfo_cachep = NULL;
620
621 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
622                          __attribute__((__const__));
623
624 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
625 {
626         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
627         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
628         return tmp;
629 }
630
631 /* caller must hold qinfo_list_lock */
632 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
633 {
634         struct list_head *head = qinfo_hash +
635                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
636
637         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
638         list_add(&oqi->oqi_hash, head);
639 }
640
641 /* caller must hold qinfo_list_lock */
642 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
643 {
644         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
645         list_del_init(&oqi->oqi_hash);
646 }
647
648 /* caller must hold qinfo_list_lock */
649 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
650                                                 unsigned int id, int type)
651 {
652         unsigned int hashent = hashfn(cli, id, type);
653         struct osc_quota_info *oqi;
654         ENTRY;
655
656         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
657         list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
658                 if (oqi->oqi_cli == cli &&
659                     oqi->oqi_id == id && oqi->oqi_type == type)
660                         return oqi;
661         }
662         RETURN(NULL);
663 }
664
665 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
666                                           unsigned int id, int type)
667 {
668         struct osc_quota_info *oqi;
669         ENTRY;
670
671         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
672         if(!oqi)
673                 RETURN(NULL);
674
675         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
676         oqi->oqi_cli = cli;
677         oqi->oqi_id = id;
678         oqi->oqi_type = type;
679
680         RETURN(oqi);
681 }
682
683 static void free_qinfo(struct osc_quota_info *oqi)
684 {
685         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
686 }
687
688 int osc_quota_chkdq(struct client_obd *cli, unsigned int uid, unsigned int gid)
689 {
690         unsigned int id;
691         int cnt, rc = QUOTA_OK;
692         ENTRY;
693
694         spin_lock(&qinfo_list_lock);
695         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
696                 struct osc_quota_info *oqi = NULL;
697
698                 id = (cnt == USRQUOTA) ? uid : gid;
699                 oqi = find_qinfo(cli, id, cnt);
700                 if (oqi) {
701                         rc = NO_QUOTA;
702                         break;
703                 }
704         }
705         spin_unlock(&qinfo_list_lock);
706
707         RETURN(rc);
708 }
709
710 int osc_quota_setdq(struct client_obd *cli, unsigned int uid, unsigned int gid,
711                     obd_flag valid, obd_flag flags)
712 {
713         unsigned int id;
714         obd_flag noquota;
715         int cnt, rc = 0;
716         ENTRY;
717
718
719         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
720                 struct osc_quota_info *oqi, *old;
721
722                 if (!(valid & ((cnt == USRQUOTA) ?
723                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
724                         continue;
725
726                 id = (cnt == USRQUOTA) ? uid : gid;
727                 noquota = (cnt == USRQUOTA) ?
728                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
729
730                 oqi = alloc_qinfo(cli, id, cnt);
731                 if (oqi) {
732                         spin_lock(&qinfo_list_lock);
733
734                         old = find_qinfo(cli, id, cnt);
735                         if (old && !noquota)
736                                 remove_qinfo_hash(old);
737                         else if (!old && noquota)
738                                 insert_qinfo_hash(oqi);
739
740                         spin_unlock(&qinfo_list_lock);
741
742                         if (old || !noquota)
743                                 free_qinfo(oqi);
744                         if (old && !noquota)
745                                 free_qinfo(old);
746                 } else {
747                         CERROR("not enough mem!\n");
748                         rc = -ENOMEM;
749                         break;
750                 }
751         }
752
753         RETURN(rc);
754 }
755
756 int osc_quota_cleanup(struct obd_device *obd)
757 {
758         struct client_obd *cli = &obd->u.cli;
759         struct osc_quota_info *oqi, *n;
760         int i;
761         ENTRY;
762
763         spin_lock(&qinfo_list_lock);
764         for (i = 0; i < NR_DQHASH; i++) {
765                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
766                         if (oqi->oqi_cli != cli)
767                                 continue;
768                         remove_qinfo_hash(oqi);
769                         free_qinfo(oqi);
770                 }
771         }
772         spin_unlock(&qinfo_list_lock);
773
774         RETURN(0);
775 }
776
777 int osc_quota_init(void)
778 {
779         int i;
780         ENTRY;
781
782         LASSERT(qinfo_cachep == NULL);
783         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
784                                             sizeof(struct osc_quota_info),
785                                             0, 0);
786         if (!qinfo_cachep)
787                 RETURN(-ENOMEM);
788
789         for (i = 0; i < NR_DQHASH; i++)
790                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
791
792         RETURN(0);
793 }
794
795 int osc_quota_exit(void)
796 {
797         struct osc_quota_info *oqi, *n;
798         int i, rc;
799         ENTRY;
800
801         spin_lock(&qinfo_list_lock);
802         for (i = 0; i < NR_DQHASH; i++) {
803                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
804                         remove_qinfo_hash(oqi);
805                         free_qinfo(oqi);
806                 }
807         }
808         spin_unlock(&qinfo_list_lock);
809
810         rc = cfs_mem_cache_destroy(qinfo_cachep);
811         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
812         qinfo_cachep = NULL;
813
814         RETURN(0);
815 }
816
817 #ifdef __KERNEL__
818 #ifdef HAVE_QUOTA_SUPPORT
819 quota_interface_t mds_quota_interface = {
820         .quota_init     = mds_quota_init,
821         .quota_exit     = mds_quota_exit,
822         .quota_setup    = mds_quota_setup,
823         .quota_cleanup  = mds_quota_cleanup,
824         .quota_check    = target_quota_check,
825         .quota_ctl      = mds_quota_ctl,
826         .quota_setinfo  = mds_quota_setinfo,
827         .quota_fs_cleanup = mds_quota_fs_cleanup,
828         .quota_recovery = mds_quota_recovery,
829         .quota_adjust   = mds_quota_adjust,
830         .quota_chkquota = quota_chk_acq_common,
831         .quota_acquire  = quota_acquire_common,
832         .quota_pending_commit = quota_pending_commit,
833 };
834
835 quota_interface_t filter_quota_interface = {
836         .quota_setup    = filter_quota_setup,
837         .quota_cleanup  = filter_quota_cleanup,
838         .quota_check    = target_quota_check,
839         .quota_ctl      = filter_quota_ctl,
840         .quota_setinfo  = filter_quota_setinfo,
841         .quota_clearinfo = filter_quota_clearinfo,
842         .quota_enforce  = filter_quota_enforce,
843         .quota_getflag  = filter_quota_getflag,
844         .quota_acquire  = quota_acquire_common,
845         .quota_adjust   = filter_quota_adjust,
846         .quota_chkquota = quota_chk_acq_common,
847         .quota_adjust_qunit   = filter_quota_adjust_qunit,
848         .quota_pending_commit = quota_pending_commit,
849 };
850 #endif
851 #endif /* __KERNEL__ */
852
853 quota_interface_t mdc_quota_interface = {
854         .quota_ctl      = client_quota_ctl,
855         .quota_check    = client_quota_check,
856         .quota_poll_check = client_quota_poll_check,
857 };
858
859 quota_interface_t lmv_quota_interface = {
860         .quota_ctl      = lmv_quota_ctl,
861         .quota_check    = lmv_quota_check,
862 };
863
864 quota_interface_t osc_quota_interface = {
865         .quota_ctl      = client_quota_ctl,
866         .quota_check    = client_quota_check,
867         .quota_poll_check = client_quota_poll_check,
868         .quota_init     = osc_quota_init,
869         .quota_exit     = osc_quota_exit,
870         .quota_chkdq    = osc_quota_chkdq,
871         .quota_setdq    = osc_quota_setdq,
872         .quota_cleanup  = osc_quota_cleanup,
873         .quota_adjust_qunit = client_quota_adjust_qunit,
874 };
875
876 quota_interface_t lov_quota_interface = {
877         .quota_ctl      = lov_quota_ctl,
878         .quota_check    = lov_quota_check,
879         .quota_adjust_qunit = lov_quota_adjust_qunit,
880 };
881
882 #ifdef __KERNEL__
883
884 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
885
886 static int __init init_lustre_quota(void)
887 {
888 #ifdef HAVE_QUOTA_SUPPORT
889         int rc = 0;
890
891         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
892                                                 proc_lustre_root,
893                                                 NULL, NULL);
894         if (IS_ERR(lquota_type_proc_dir)) {
895                 CERROR("LProcFS failed in lquota-init\n");
896                 rc = PTR_ERR(lquota_type_proc_dir);
897                 return rc;
898         }
899
900         rc = qunit_cache_init();
901         if (rc)
902                 return rc;
903
904         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
905         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
906 #endif
907         PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
908         PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
909         PORTAL_SYMBOL_REGISTER(osc_quota_interface);
910         PORTAL_SYMBOL_REGISTER(lov_quota_interface);
911         return 0;
912 }
913
914 static void /*__exit*/ exit_lustre_quota(void)
915 {
916         PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
917         PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
918         PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
919         PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
920 #ifdef HAVE_QUOTA_SUPPORT
921         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
922         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
923
924         qunit_cache_cleanup();
925
926         if (lquota_type_proc_dir)
927                 lprocfs_remove(&lquota_type_proc_dir);
928 #endif
929 }
930
931 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
932 MODULE_DESCRIPTION("Lustre Quota");
933 MODULE_LICENSE("GPL");
934
935 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
936
937 #ifdef HAVE_QUOTA_SUPPORT
938 EXPORT_SYMBOL(mds_quota_interface);
939 EXPORT_SYMBOL(filter_quota_interface);
940 #endif
941 EXPORT_SYMBOL(mdc_quota_interface);
942 EXPORT_SYMBOL(lmv_quota_interface);
943 EXPORT_SYMBOL(osc_quota_interface);
944 EXPORT_SYMBOL(lov_quota_interface);
945 #endif /* __KERNEL */