Whamcloud - gitweb
b=16098
[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_MDS
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/ext3_fs.h>
49 # include <linux/smp_lock.h>
50 # include <linux/buffer_head.h>
51 # include <linux/workqueue.h>
52 # include <linux/mount.h>
53 #else /* __KERNEL__ */
54 # include <liblustre.h>
55 #endif
56
57 #include <obd_class.h>
58 #include <lustre_mds.h>
59 #include <lustre_dlm.h>
60 #include <lustre_cfg.h>
61 #include <obd_ost.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_quota.h>
64 #include <lprocfs_status.h>
65 #include "quota_internal.h"
66
67
68 #ifdef __KERNEL__
69 /* quota proc file handling functions */
70 #ifdef LPROCFS
71 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count, 
72                      int *eof, void *data)
73 {
74         struct obd_device *obd = (struct obd_device *)data;
75         LASSERT(obd != NULL);
76
77         return snprintf(page, count, "%lu\n", 
78                         obd->u.obt.obt_qctxt.lqc_bunit_sz);
79 }
80 EXPORT_SYMBOL(lprocfs_rd_bunit);
81
82 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count, 
83                      int *eof, void *data)
84 {
85         struct obd_device *obd = (struct obd_device *)data;
86         LASSERT(obd != NULL);
87
88         return snprintf(page, count, "%lu\n", 
89                         obd->u.obt.obt_qctxt.lqc_iunit_sz);
90 }
91 EXPORT_SYMBOL(lprocfs_rd_iunit);
92
93 int lprocfs_wr_bunit(struct file *file, const char *buffer,
94                      unsigned long count, void *data)
95 {
96         struct obd_device *obd = (struct obd_device *)data;
97         int val, rc;
98         LASSERT(obd != NULL);
99
100         rc = lprocfs_write_helper(buffer, count, &val);
101
102         if (rc)
103                 return rc;
104
105         if (val % QUOTABLOCK_SIZE ||
106             val <= obd->u.obt.obt_qctxt.lqc_btune_sz)
107                 return -EINVAL;
108
109         obd->u.obt.obt_qctxt.lqc_bunit_sz = val;
110         return count;
111 }
112 EXPORT_SYMBOL(lprocfs_wr_bunit);
113
114 int lprocfs_wr_iunit(struct file *file, const char *buffer,
115                      unsigned long count, void *data)
116 {
117         struct obd_device *obd = (struct obd_device *)data;
118         int val, rc;
119         LASSERT(obd != NULL);
120
121         rc = lprocfs_write_helper(buffer, count, &val);
122         if (rc)
123                 return rc;
124
125         if (val <= obd->u.obt.obt_qctxt.lqc_itune_sz)
126                 return -EINVAL;
127
128         obd->u.obt.obt_qctxt.lqc_iunit_sz = val;
129         return count;
130 }
131 EXPORT_SYMBOL(lprocfs_wr_iunit);
132
133 int lprocfs_rd_btune(char *page, char **start, off_t off, int count, 
134                      int *eof, void *data)
135 {
136         struct obd_device *obd = (struct obd_device *)data;
137         LASSERT(obd != NULL);
138
139         return snprintf(page, count, "%lu\n", 
140                         obd->u.obt.obt_qctxt.lqc_btune_sz);
141 }
142 EXPORT_SYMBOL(lprocfs_rd_btune);
143
144 int lprocfs_rd_itune(char *page, char **start, off_t off, int count, 
145                      int *eof, void *data)
146 {
147         struct obd_device *obd = (struct obd_device *)data;
148         LASSERT(obd != NULL);
149
150         return snprintf(page, count, "%lu\n", 
151                         obd->u.obt.obt_qctxt.lqc_itune_sz);
152 }
153 EXPORT_SYMBOL(lprocfs_rd_itune);
154
155 int lprocfs_wr_btune(struct file *file, const char *buffer,
156                      unsigned long count, void *data)
157 {
158         struct obd_device *obd = (struct obd_device *)data;
159         int val, rc;
160         LASSERT(obd != NULL);
161
162         rc = lprocfs_write_helper(buffer, count, &val);
163         if (rc)
164                 return rc;
165         
166         if (val <= QUOTABLOCK_SIZE * MIN_QLIMIT || val % QUOTABLOCK_SIZE || 
167             val >= obd->u.obt.obt_qctxt.lqc_bunit_sz)
168                 return -EINVAL;
169
170         obd->u.obt.obt_qctxt.lqc_btune_sz = val;
171         return count;
172 }
173 EXPORT_SYMBOL(lprocfs_wr_btune);
174
175 int lprocfs_wr_itune(struct file *file, const char *buffer,
176                      unsigned long count, void *data)
177 {
178         struct obd_device *obd = (struct obd_device *)data;
179         int val, rc;
180         LASSERT(obd != NULL);
181  
182         rc = lprocfs_write_helper(buffer, count, &val);
183         if (rc)
184                 return rc;
185
186         if (val <= MIN_QLIMIT || 
187             val >= obd->u.obt.obt_qctxt.lqc_iunit_sz)
188                 return -EINVAL;
189
190         obd->u.obt.obt_qctxt.lqc_itune_sz = val;
191         return count;
192 }
193 EXPORT_SYMBOL(lprocfs_wr_itune);
194
195 #define USER_QUOTA      1
196 #define GROUP_QUOTA     2
197
198 #define MAX_STYPE_SIZE  4
199 int lprocfs_rd_type(char *page, char **start, off_t off, int count, 
200                     int *eof, void *data)
201 {
202         struct obd_device *obd = (struct obd_device *)data;
203         char stype[MAX_STYPE_SIZE + 1] = "";
204         int type = obd->u.obt.obt_qctxt.lqc_atype;
205         LASSERT(obd != NULL);
206
207         if (type == 0) {
208                 strcpy(stype, "off");
209         } else {
210                 if (type & USER_QUOTA)
211                         strcat(stype, "u");
212                 if (type & GROUP_QUOTA)
213                         strcat(stype, "g");
214         }
215         
216         return snprintf(page, count, "%s\n", stype);
217 }
218 EXPORT_SYMBOL(lprocfs_rd_type);
219
220 static int auto_quota_on(struct obd_device *obd, int type,
221                          struct super_block *sb, int is_master)
222 {
223         struct obd_quotactl *oqctl;
224         struct lvfs_run_ctxt saved;
225         int rc;
226         ENTRY;
227
228         LASSERT(type == USRQUOTA || type == GRPQUOTA || type == UGQUOTA);
229
230         /* quota already turned on */
231         if (obd->u.obt.obt_qctxt.lqc_status)
232                 RETURN(0);
233
234         OBD_ALLOC_PTR(oqctl);
235         if (!oqctl)
236                 RETURN(-ENOMEM);
237
238         oqctl->qc_type = type;
239         oqctl->qc_cmd = Q_QUOTAON;
240         oqctl->qc_id = QFMT_LDISKFS;
241
242         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
243
244         if (!is_master)
245                 goto local_quota;
246
247         /* turn on cluster wide quota */
248         rc = mds_admin_quota_on(obd, oqctl);
249         if (rc) {
250                 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR, 
251                        "auto-enable admin quota failed. rc=%d\n", rc);
252                 GOTO(out_pop, rc);
253         }
254 local_quota:
255         /* turn on local quota */
256         rc = fsfilt_quotactl(obd, sb, oqctl);
257         if (rc) {
258                 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR, 
259                        "auto-enable local quota failed. rc=%d\n", rc);
260                 if (is_master)
261                          mds_quota_off(obd, oqctl);
262         } else {
263                 obd->u.obt.obt_qctxt.lqc_status = 1;
264         }
265 out_pop:
266         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
267
268         OBD_FREE_PTR(oqctl);
269         RETURN(rc);
270 }
271
272
273 int lprocfs_wr_type(struct file *file, const char *buffer,
274                     unsigned long count, void *data)
275 {
276         struct obd_device *obd = (struct obd_device *)data;
277         struct obd_device_target *obt = &obd->u.obt;
278         int type = 0;
279         char stype[MAX_STYPE_SIZE + 1] = "";
280         LASSERT(obd != NULL);
281
282         if (copy_from_user(stype, buffer, MAX_STYPE_SIZE))
283                 return -EFAULT;
284
285         if (strchr(stype, 'u'))
286                 type |= USER_QUOTA;
287         if (strchr(stype, 'g'))
288                 type |= GROUP_QUOTA;
289         
290         obt->obt_qctxt.lqc_atype = type;
291
292         if (type == 0)
293                 return count;
294
295         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME))
296                 auto_quota_on(obd, type - 1, obt->obt_sb, 1);
297         else if (!strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME))
298                 auto_quota_on(obd, type - 1, obt->obt_sb, 0);
299         else 
300                 return -EFAULT;
301
302         return count;
303 }
304 EXPORT_SYMBOL(lprocfs_wr_type);
305 #endif /* LPROCFS */
306
307 static int filter_quota_setup(struct obd_device *obd)
308 {
309         int rc = 0;
310         struct obd_device_target *obt = &obd->u.obt;
311         ENTRY;
312
313         atomic_set(&obt->obt_quotachecking, 1);
314         rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, NULL);
315         if (rc) {
316                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
317                 RETURN(rc);
318         }
319
320         RETURN(rc);
321 }
322
323 static int filter_quota_cleanup(struct obd_device *obd)
324 {
325         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
326         return 0;
327 }
328
329 static int filter_quota_setinfo(struct obd_export *exp, struct obd_device *obd)
330 {
331         struct obd_import *imp;
332
333         /* setup the quota context import */
334         obd->u.obt.obt_qctxt.lqc_import = exp->exp_imp_reverse;
335
336         /* make imp's connect flags equal relative exp's connect flags 
337          * adding it to avoid the scan export list
338          */
339         imp = exp->exp_imp_reverse;
340         if (imp)
341                 imp->imp_connect_data.ocd_connect_flags |= 
342                         (exp->exp_connect_flags & OBD_CONNECT_QUOTA64);
343
344         /* start quota slave recovery thread. (release high limits) */
345         qslave_start_recovery(obd, &obd->u.obt.obt_qctxt);
346         return 0;
347 }
348 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
349 {
350         ENTRY;
351
352         if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
353                 RETURN(0);
354
355         if (ignore)
356                 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
357         else
358                 cap_lower(current->cap_effective, CAP_SYS_RESOURCE);
359
360         RETURN(0);
361 }
362
363 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
364 {
365         struct obd_device_target *obt = &obd->u.obt;
366         int err, cnt, rc = 0;
367         struct obd_quotactl *oqctl;
368         ENTRY;
369
370         if (!sb_any_quota_enabled(obt->obt_sb))
371                 RETURN(0);
372
373         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
374
375         OBD_ALLOC_PTR(oqctl);
376         if (!oqctl) {
377                 CERROR("Not enough memory!");
378                 RETURN(-ENOMEM);
379         }
380
381         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
382                 memset(oqctl, 0, sizeof(*oqctl));
383
384                 oqctl->qc_cmd = Q_GETQUOTA;
385                 oqctl->qc_type = cnt;
386                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
387                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
388                 if (err) {
389                         if (!rc)
390                                 rc = err;
391                         continue;
392                 }
393
394                 /* set over quota flags for a uid/gid */
395                 oa->o_valid |= (cnt == USRQUOTA) ?
396                                OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA;
397                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
398                    (toqb(oqctl->qc_dqblk.dqb_curspace) >
399                     oqctl->qc_dqblk.dqb_bhardlimit))
400                         oa->o_flags |= (cnt == USRQUOTA) ?
401                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
402         }
403         OBD_FREE_PTR(oqctl);
404         RETURN(rc);
405 }
406
407 static int filter_quota_acquire(struct obd_device *obd, unsigned int uid,
408                                 unsigned int gid)
409 {
410         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
411         int rc;
412         ENTRY;
413
414         rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 1);
415         RETURN(rc == -EAGAIN);
416 }
417
418 static int mds_quota_init(void)
419 {
420         return lustre_dquot_init();
421 }
422
423 static int mds_quota_exit(void)
424 {
425         lustre_dquot_exit();
426         return 0;
427 }
428
429 /* check whether the left quota of certain uid and uid can satisfy a write rpc
430  * when need to acquire quota, return QUOTA_RET_ACQUOTA */
431 static int filter_quota_check(struct obd_device *obd, unsigned int uid, 
432                               unsigned int gid, int npage)
433 {
434         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
435         int i;
436         __u32 id[MAXQUOTAS] = { uid, gid };
437         struct qunit_data qdata[MAXQUOTAS];
438         int rc;
439         ENTRY;
440
441         CLASSERT(MAXQUOTAS < 4);
442         if (!sb_any_quota_enabled(qctxt->lqc_sb))
443                 RETURN(0);
444
445         for (i = 0; i < MAXQUOTAS; i++) {
446                 qdata[i].qd_id = id[i];
447                 qdata[i].qd_flags = i;
448                 qdata[i].qd_flags |= QUOTA_IS_BLOCK;
449                 qdata[i].qd_count = 0;
450
451                 qctxt_wait_pending_dqacq(qctxt, id[i], i, 1);
452                 rc = compute_remquota(obd, qctxt, &qdata[i]);
453                 if (rc == QUOTA_RET_OK && 
454                     qdata[i].qd_count < npage * CFS_PAGE_SIZE)
455                         RETURN(QUOTA_RET_ACQUOTA);
456         }
457
458         RETURN(rc);
459 }
460
461 static int mds_quota_setup(struct obd_device *obd)
462 {
463         struct obd_device_target *obt = &obd->u.obt;
464         struct mds_obd *mds = &obd->u.mds;
465         int rc;
466         ENTRY;
467
468         atomic_set(&obt->obt_quotachecking, 1);
469         /* initialize quota master and quota context */
470         sema_init(&mds->mds_qonoff_sem, 1);
471         rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, dqacq_handler);
472         if (rc) {
473                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
474                 RETURN(rc);
475         }
476
477         RETURN(rc);
478 }
479
480 static int mds_quota_cleanup(struct obd_device *obd)
481 {
482         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
483         RETURN(0);
484 }
485
486 static int mds_quota_fs_cleanup(struct obd_device *obd)
487 {
488         struct mds_obd *mds = &obd->u.mds;
489         int i;
490         ENTRY;
491
492         /* close admin quota files */
493         down(&mds->mds_qonoff_sem);
494         for (i = 0; i < MAXQUOTAS; i++) {
495                 if (mds->mds_quota_info.qi_files[i]) {
496                         filp_close(mds->mds_quota_info.qi_files[i], 0);
497                         mds->mds_quota_info.qi_files[i] = NULL;
498                 }
499         }
500         up(&mds->mds_qonoff_sem);
501         RETURN(0);
502 }
503 #endif /* __KERNEL__ */
504
505 struct osc_quota_info {
506         struct list_head        oqi_hash;       /* hash list */
507         struct client_obd      *oqi_cli;        /* osc obd */
508         unsigned int            oqi_id;         /* uid/gid of a file */
509         short                   oqi_type;       /* quota type */
510 };
511
512 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
513
514 static struct list_head qinfo_hash[NR_DQHASH];
515 /* SLAB cache for client quota context */
516 cfs_mem_cache_t *qinfo_cachep = NULL;
517
518 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
519                          __attribute__((__const__));
520
521 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
522 {
523         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
524         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
525         return tmp;
526 }
527
528 /* caller must hold qinfo_list_lock */
529 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
530 {
531         struct list_head *head = qinfo_hash +
532                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
533
534         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
535         list_add(&oqi->oqi_hash, head);
536 }
537
538 /* caller must hold qinfo_list_lock */
539 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
540 {
541         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
542         list_del_init(&oqi->oqi_hash);
543 }
544
545 /* caller must hold qinfo_list_lock */
546 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
547                                                 unsigned int id, int type)
548 {
549         unsigned int hashent = hashfn(cli, id, type);
550         struct osc_quota_info *oqi;
551
552         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
553         list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
554                 if (oqi->oqi_cli == cli &&
555                     oqi->oqi_id == id && oqi->oqi_type == type)
556                         return oqi;
557         }
558         return NULL;
559 }
560
561 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
562                                           unsigned int id, int type)
563 {
564         struct osc_quota_info *oqi;
565         ENTRY;
566
567         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
568         if(!oqi)
569                 RETURN(NULL);
570
571         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
572         oqi->oqi_cli = cli;
573         oqi->oqi_id = id;
574         oqi->oqi_type = type;
575
576         RETURN(oqi);
577 }
578
579 static void free_qinfo(struct osc_quota_info *oqi)
580 {
581         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
582 }
583
584 int osc_quota_chkdq(struct client_obd *cli,
585                     unsigned int uid, unsigned int gid)
586 {
587         unsigned int id;
588         int cnt, rc = QUOTA_OK;
589         ENTRY;
590
591         spin_lock(&qinfo_list_lock);
592         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
593                 struct osc_quota_info *oqi = NULL;
594
595                 id = (cnt == USRQUOTA) ? uid : gid;
596                 oqi = find_qinfo(cli, id, cnt);
597                 if (oqi) {
598                         rc = NO_QUOTA;
599                         break;
600                 }
601         }
602         spin_unlock(&qinfo_list_lock);
603
604         RETURN(rc);
605 }
606
607 int osc_quota_setdq(struct client_obd *cli,
608                     unsigned int uid, unsigned int gid,
609                     obd_flag valid, obd_flag flags)
610 {
611         unsigned int id;
612         obd_flag noquota;
613         int cnt, rc = 0;
614         ENTRY;
615
616
617         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
618                 struct osc_quota_info *oqi, *old;
619
620                 if (!(valid & ((cnt == USRQUOTA) ?
621                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
622                         continue;
623
624                 id = (cnt == USRQUOTA) ? uid : gid;
625                 noquota = (cnt == USRQUOTA) ?
626                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
627
628                 oqi = alloc_qinfo(cli, id, cnt);
629                 if (oqi) {
630                         spin_lock(&qinfo_list_lock);
631
632                         old = find_qinfo(cli, id, cnt);
633                         if (old && !noquota)
634                                 remove_qinfo_hash(old);
635                         else if (!old && noquota)
636                                 insert_qinfo_hash(oqi);
637
638                         spin_unlock(&qinfo_list_lock);
639
640                         if (old || !noquota)
641                                 free_qinfo(oqi);
642                         if (old && !noquota)
643                                 free_qinfo(old);
644                 } else {
645                         CERROR("not enough mem!\n");
646                         rc = -ENOMEM;
647                         break;
648                 }
649         }
650
651         RETURN(rc);
652 }
653
654 int osc_quota_cleanup(struct obd_device *obd)
655 {
656         struct client_obd *cli = &obd->u.cli;
657         struct osc_quota_info *oqi, *n;
658         int i;
659         ENTRY;
660
661         spin_lock(&qinfo_list_lock);
662         for (i = 0; i < NR_DQHASH; i++) {
663                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
664                         if (oqi->oqi_cli != cli)
665                                 continue;
666                         remove_qinfo_hash(oqi);
667                         free_qinfo(oqi);
668                 }
669         }
670         spin_unlock(&qinfo_list_lock);
671
672         RETURN(0);
673 }
674
675 int osc_quota_init(void)
676 {
677         int i;
678         ENTRY;
679
680         LASSERT(qinfo_cachep == NULL);
681         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
682                                             sizeof(struct osc_quota_info),
683                                             0, 0);
684         if (!qinfo_cachep)
685                 RETURN(-ENOMEM);
686
687         for (i = 0; i < NR_DQHASH; i++)
688                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
689
690         RETURN(0);
691 }
692
693 int osc_quota_exit(void)
694 {
695         struct osc_quota_info *oqi, *n;
696         int i, rc;
697         ENTRY;
698
699         spin_lock(&qinfo_list_lock);
700         for (i = 0; i < NR_DQHASH; i++) {
701                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
702                         remove_qinfo_hash(oqi);
703                         free_qinfo(oqi);
704                 }
705         }
706         spin_unlock(&qinfo_list_lock);
707
708         rc = cfs_mem_cache_destroy(qinfo_cachep);
709         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
710         qinfo_cachep = NULL;
711
712         RETURN(0);
713 }
714
715 #ifdef __KERNEL__
716 quota_interface_t mds_quota_interface = {
717         .quota_init     = mds_quota_init,
718         .quota_exit     = mds_quota_exit,
719         .quota_setup    = mds_quota_setup,
720         .quota_cleanup  = mds_quota_cleanup,
721         .quota_check    = target_quota_check,
722         .quota_ctl      = mds_quota_ctl,
723         .quota_fs_cleanup       =mds_quota_fs_cleanup,
724         .quota_recovery = mds_quota_recovery,
725         .quota_adjust   = mds_quota_adjust,
726 };
727
728 quota_interface_t filter_quota_interface = {
729         .quota_setup    = filter_quota_setup,
730         .quota_cleanup  = filter_quota_cleanup,
731         .quota_check    = target_quota_check,
732         .quota_ctl      = filter_quota_ctl,
733         .quota_setinfo  = filter_quota_setinfo,
734         .quota_enforce  = filter_quota_enforce,
735         .quota_getflag  = filter_quota_getflag,
736         .quota_acquire  = filter_quota_acquire,
737         .quota_adjust   = filter_quota_adjust,
738         .quota_chkquota = filter_quota_check,
739 };
740 #endif /* __KERNEL__ */
741
742 quota_interface_t mdc_quota_interface = {
743         .quota_ctl      = client_quota_ctl,
744         .quota_check    = client_quota_check,
745         .quota_poll_check = client_quota_poll_check,
746 };
747
748 quota_interface_t osc_quota_interface = {
749         .quota_ctl      = client_quota_ctl,
750         .quota_check    = client_quota_check,
751         .quota_poll_check = client_quota_poll_check,
752         .quota_init     = osc_quota_init,
753         .quota_exit     = osc_quota_exit,
754         .quota_chkdq    = osc_quota_chkdq,
755         .quota_setdq    = osc_quota_setdq,
756         .quota_cleanup  = osc_quota_cleanup,
757 };
758
759 quota_interface_t lov_quota_interface = {
760         .quota_check    = lov_quota_check,
761         .quota_ctl      = lov_quota_ctl,
762 };
763
764 #ifdef __KERNEL__
765 static int __init init_lustre_quota(void)
766 {
767         int rc = qunit_cache_init();
768         if (rc)
769                 return rc;
770         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
771         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
772         PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
773         PORTAL_SYMBOL_REGISTER(osc_quota_interface);
774         PORTAL_SYMBOL_REGISTER(lov_quota_interface);
775         return 0;
776 }
777
778 static void /*__exit*/ exit_lustre_quota(void)
779 {
780         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
781         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
782         PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
783         PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
784         PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
785
786         qunit_cache_cleanup();
787 }
788
789 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
790 MODULE_DESCRIPTION("Lustre Quota");
791 MODULE_LICENSE("GPL");
792
793 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
794
795 EXPORT_SYMBOL(mds_quota_interface);
796 EXPORT_SYMBOL(filter_quota_interface);
797 EXPORT_SYMBOL(mdc_quota_interface);
798 EXPORT_SYMBOL(osc_quota_interface);
799 EXPORT_SYMBOL(lov_quota_interface);
800 #endif /* __KERNEL */