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