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