1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/quota/quota_interface.c
6 * Copyright (c) 2001-2005 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * No redistribution or use is permitted outside of Cluster File Systems, Inc.
14 # define EXPORT_SYMTAB
16 #define DEBUG_SUBSYSTEM S_MDS
19 # include <linux/version.h>
20 # include <linux/module.h>
21 # include <linux/init.h>
22 # include <linux/fs.h>
23 # include <linux/jbd.h>
24 # include <linux/ext3_fs.h>
25 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
26 # include <linux/smp_lock.h>
27 # include <linux/buffer_head.h>
28 # include <linux/workqueue.h>
29 # include <linux/mount.h>
31 # include <linux/locks.h>
33 #else /* __KERNEL__ */
34 # include <liblustre.h>
37 #include <obd_class.h>
38 #include <lustre_mds.h>
39 #include <lustre_dlm.h>
40 #include <lustre_cfg.h>
42 #include <lustre_fsfilt.h>
43 #include <lustre_quota.h>
44 #include <lprocfs_status.h>
45 #include "quota_internal.h"
50 /* quota proc file handling functions */
52 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count,
55 struct obd_device *obd = (struct obd_device *)data;
58 return snprintf(page, count, "%lu\n",
59 obd->u.obt.obt_qctxt.lqc_bunit_sz);
61 EXPORT_SYMBOL(lprocfs_rd_bunit);
63 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count,
66 struct obd_device *obd = (struct obd_device *)data;
69 return snprintf(page, count, "%lu\n",
70 obd->u.obt.obt_qctxt.lqc_iunit_sz);
72 EXPORT_SYMBOL(lprocfs_rd_iunit);
74 int lprocfs_wr_bunit(struct file *file, const char *buffer,
75 unsigned long count, void *data)
77 struct obd_device *obd = (struct obd_device *)data;
81 rc = lprocfs_write_helper(buffer, count, &val);
85 if (val % QUOTABLOCK_SIZE ||
86 val <= obd->u.obt.obt_qctxt.lqc_btune_sz)
89 obd->u.obt.obt_qctxt.lqc_bunit_sz = val;
92 EXPORT_SYMBOL(lprocfs_wr_bunit);
94 int lprocfs_wr_iunit(struct file *file, const char *buffer,
95 unsigned long count, void *data)
97 struct obd_device *obd = (struct obd_device *)data;
101 rc = lprocfs_write_helper(buffer, count, &val);
105 if (val <= obd->u.obt.obt_qctxt.lqc_itune_sz)
108 obd->u.obt.obt_qctxt.lqc_iunit_sz = val;
111 EXPORT_SYMBOL(lprocfs_wr_iunit);
113 int lprocfs_rd_btune(char *page, char **start, off_t off, int count,
114 int *eof, void *data)
116 struct obd_device *obd = (struct obd_device *)data;
117 LASSERT(obd != NULL);
119 return snprintf(page, count, "%lu\n",
120 obd->u.obt.obt_qctxt.lqc_btune_sz);
122 EXPORT_SYMBOL(lprocfs_rd_btune);
124 int lprocfs_rd_itune(char *page, char **start, off_t off, int count,
125 int *eof, void *data)
127 struct obd_device *obd = (struct obd_device *)data;
128 LASSERT(obd != NULL);
130 return snprintf(page, count, "%lu\n",
131 obd->u.obt.obt_qctxt.lqc_itune_sz);
133 EXPORT_SYMBOL(lprocfs_rd_itune);
135 int lprocfs_wr_btune(struct file *file, const char *buffer,
136 unsigned long count, void *data)
138 struct obd_device *obd = (struct obd_device *)data;
140 LASSERT(obd != NULL);
142 rc = lprocfs_write_helper(buffer, count, &val);
146 if (val <= QUOTABLOCK_SIZE * MIN_QLIMIT || val % QUOTABLOCK_SIZE ||
147 val >= obd->u.obt.obt_qctxt.lqc_bunit_sz)
150 obd->u.obt.obt_qctxt.lqc_btune_sz = val;
153 EXPORT_SYMBOL(lprocfs_wr_btune);
155 int lprocfs_wr_itune(struct file *file, const char *buffer,
156 unsigned long count, void *data)
158 struct obd_device *obd = (struct obd_device *)data;
160 LASSERT(obd != NULL);
162 rc = lprocfs_write_helper(buffer, count, &val);
166 if (val <= MIN_QLIMIT ||
167 val >= obd->u.obt.obt_qctxt.lqc_iunit_sz)
170 obd->u.obt.obt_qctxt.lqc_itune_sz = val;
173 EXPORT_SYMBOL(lprocfs_wr_itune);
176 #define GROUP_QUOTA 2
178 #define MAX_STYPE_SIZE 4
179 int lprocfs_rd_type(char *page, char **start, off_t off, int count,
180 int *eof, void *data)
182 struct obd_device *obd = (struct obd_device *)data;
183 char stype[MAX_STYPE_SIZE + 1] = "";
184 int type = obd->u.obt.obt_qctxt.lqc_atype;
185 LASSERT(obd != NULL);
188 strcpy(stype, "off");
190 if (type & USER_QUOTA)
192 if (type & GROUP_QUOTA)
196 return snprintf(page, count, "%s\n", stype);
198 EXPORT_SYMBOL(lprocfs_rd_type);
200 static int auto_quota_on(struct obd_device *obd, int type,
201 struct super_block *sb, int is_master)
203 struct obd_quotactl *oqctl;
204 struct lvfs_run_ctxt saved;
208 LASSERT(type == USRQUOTA || type == GRPQUOTA || type == UGQUOTA);
210 /* quota already turned on */
211 if (obd->u.obt.obt_qctxt.lqc_status)
214 OBD_ALLOC_PTR(oqctl);
218 oqctl->qc_type = type;
219 oqctl->qc_cmd = Q_QUOTAON;
220 oqctl->qc_id = QFMT_LDISKFS;
222 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
227 /* turn on cluster wide quota */
228 rc = mds_admin_quota_on(obd, oqctl);
230 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
231 "auto-enable admin quota failed. rc=%d\n", rc);
235 /* turn on local quota */
236 rc = fsfilt_quotactl(obd, sb, oqctl);
238 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
239 "auto-enable local quota failed. rc=%d\n", rc);
241 mds_quota_off(obd, oqctl);
243 obd->u.obt.obt_qctxt.lqc_status = 1;
246 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
252 int lprocfs_wr_type(struct file *file, const char *buffer,
253 unsigned long count, void *data)
255 struct obd_device *obd = (struct obd_device *)data;
256 struct obd_device_target *obt = &obd->u.obt;
258 char stype[MAX_STYPE_SIZE + 1] = "";
259 LASSERT(obd != NULL);
261 if (copy_from_user(stype, buffer, MAX_STYPE_SIZE))
264 if (strchr(stype, 'u'))
266 if (strchr(stype, 'g'))
269 obt->obt_qctxt.lqc_atype = type;
274 if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME))
275 auto_quota_on(obd, type - 1, obt->obt_sb, 1);
276 else if (!strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME))
277 auto_quota_on(obd, type - 1, obt->obt_sb, 0);
283 EXPORT_SYMBOL(lprocfs_wr_type);
286 static int filter_quota_setup(struct obd_device *obd)
289 struct obd_device_target *obt = &obd->u.obt;
292 atomic_set(&obt->obt_quotachecking, 1);
293 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, NULL);
295 CERROR("initialize quota context failed! (rc:%d)\n", rc);
301 static int filter_quota_cleanup(struct obd_device *obd)
303 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
307 static int filter_quota_setinfo(struct obd_export *exp, struct obd_device *obd)
309 struct obd_import *imp;
311 /* setup the quota context import */
312 obd->u.obt.obt_qctxt.lqc_import = exp->exp_imp_reverse;
314 /* make imp's connect flags equal relative exp's connect flags
315 * adding it to avoid the scan export list
317 imp = exp->exp_imp_reverse;
319 imp->imp_connect_data.ocd_connect_flags |=
320 (exp->exp_connect_flags & OBD_CONNECT_QUOTA64);
322 /* start quota slave recovery thread. (release high limits) */
323 qslave_start_recovery(obd, &obd->u.obt.obt_qctxt);
326 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
330 if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
334 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
336 cap_lower(current->cap_effective, CAP_SYS_RESOURCE);
341 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
343 struct obd_device_target *obt = &obd->u.obt;
344 int err, cnt, rc = 0;
345 struct obd_quotactl *oqctl;
348 if (!sb_any_quota_enabled(obt->obt_sb))
351 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
353 OBD_ALLOC_PTR(oqctl);
355 CERROR("Not enough memory!");
359 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
360 memset(oqctl, 0, sizeof(*oqctl));
362 oqctl->qc_cmd = Q_GETQUOTA;
363 oqctl->qc_type = cnt;
364 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
365 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
372 /* set over quota flags for a uid/gid */
373 oa->o_valid |= (cnt == USRQUOTA) ?
374 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA;
375 if (oqctl->qc_dqblk.dqb_bhardlimit &&
376 (toqb(oqctl->qc_dqblk.dqb_curspace) >
377 oqctl->qc_dqblk.dqb_bhardlimit))
378 oa->o_flags |= (cnt == USRQUOTA) ?
379 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
385 static int filter_quota_acquire(struct obd_device *obd, unsigned int uid,
388 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
392 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 1);
393 RETURN(rc == -EAGAIN);
396 static int mds_quota_init(void)
398 return lustre_dquot_init();
401 static int mds_quota_exit(void)
407 static int mds_quota_setup(struct obd_device *obd)
409 struct obd_device_target *obt = &obd->u.obt;
410 struct mds_obd *mds = &obd->u.mds;
414 atomic_set(&obt->obt_quotachecking, 1);
415 /* initialize quota master and quota context */
416 sema_init(&mds->mds_qonoff_sem, 1);
417 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, dqacq_handler);
419 CERROR("initialize quota context failed! (rc:%d)\n", rc);
425 static int mds_quota_cleanup(struct obd_device *obd)
427 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
431 static int mds_quota_fs_cleanup(struct obd_device *obd)
433 struct mds_obd *mds = &obd->u.mds;
437 /* close admin quota files */
438 down(&mds->mds_qonoff_sem);
439 for (i = 0; i < MAXQUOTAS; i++) {
440 if (mds->mds_quota_info.qi_files[i]) {
441 filp_close(mds->mds_quota_info.qi_files[i], 0);
442 mds->mds_quota_info.qi_files[i] = NULL;
445 up(&mds->mds_qonoff_sem);
448 #endif /* __KERNEL__ */
450 struct osc_quota_info {
451 struct list_head oqi_hash; /* hash list */
452 struct client_obd *oqi_cli; /* osc obd */
453 unsigned int oqi_id; /* uid/gid of a file */
454 short oqi_type; /* quota type */
457 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
459 static struct list_head qinfo_hash[NR_DQHASH];
460 /* SLAB cache for client quota context */
461 cfs_mem_cache_t *qinfo_cachep = NULL;
463 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
464 __attribute__((__const__));
466 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
468 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
469 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
473 /* caller must hold qinfo_list_lock */
474 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
476 struct list_head *head = qinfo_hash +
477 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
479 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
480 list_add(&oqi->oqi_hash, head);
483 /* caller must hold qinfo_list_lock */
484 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
486 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
487 list_del_init(&oqi->oqi_hash);
490 /* caller must hold qinfo_list_lock */
491 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
492 unsigned int id, int type)
494 unsigned int hashent = hashfn(cli, id, type);
495 struct osc_quota_info *oqi;
497 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
498 list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
499 if (oqi->oqi_cli == cli &&
500 oqi->oqi_id == id && oqi->oqi_type == type)
506 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
507 unsigned int id, int type)
509 struct osc_quota_info *oqi;
512 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
516 INIT_LIST_HEAD(&oqi->oqi_hash);
519 oqi->oqi_type = type;
524 static void free_qinfo(struct osc_quota_info *oqi)
526 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
529 int osc_quota_chkdq(struct client_obd *cli,
530 unsigned int uid, unsigned int gid)
533 int cnt, rc = QUOTA_OK;
536 spin_lock(&qinfo_list_lock);
537 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
538 struct osc_quota_info *oqi = NULL;
540 id = (cnt == USRQUOTA) ? uid : gid;
541 oqi = find_qinfo(cli, id, cnt);
547 spin_unlock(&qinfo_list_lock);
552 int osc_quota_setdq(struct client_obd *cli,
553 unsigned int uid, unsigned int gid,
554 obd_flag valid, obd_flag flags)
562 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
563 struct osc_quota_info *oqi, *old;
565 if (!(valid & ((cnt == USRQUOTA) ?
566 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
569 id = (cnt == USRQUOTA) ? uid : gid;
570 noquota = (cnt == USRQUOTA) ?
571 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
573 oqi = alloc_qinfo(cli, id, cnt);
575 spin_lock(&qinfo_list_lock);
577 old = find_qinfo(cli, id, cnt);
579 remove_qinfo_hash(old);
580 else if (!old && noquota)
581 insert_qinfo_hash(oqi);
583 spin_unlock(&qinfo_list_lock);
590 CERROR("not enough mem!\n");
599 int osc_quota_cleanup(struct obd_device *obd)
601 struct client_obd *cli = &obd->u.cli;
602 struct osc_quota_info *oqi, *n;
606 spin_lock(&qinfo_list_lock);
607 for (i = 0; i < NR_DQHASH; i++) {
608 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
609 if (oqi->oqi_cli != cli)
611 remove_qinfo_hash(oqi);
615 spin_unlock(&qinfo_list_lock);
620 int osc_quota_init(void)
625 LASSERT(qinfo_cachep == NULL);
626 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
627 sizeof(struct osc_quota_info),
632 for (i = 0; i < NR_DQHASH; i++)
633 INIT_LIST_HEAD(qinfo_hash + i);
638 int osc_quota_exit(void)
640 struct osc_quota_info *oqi, *n;
644 spin_lock(&qinfo_list_lock);
645 for (i = 0; i < NR_DQHASH; i++) {
646 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
647 remove_qinfo_hash(oqi);
651 spin_unlock(&qinfo_list_lock);
653 rc = cfs_mem_cache_destroy(qinfo_cachep);
654 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
661 quota_interface_t mds_quota_interface = {
662 .quota_init = mds_quota_init,
663 .quota_exit = mds_quota_exit,
664 .quota_setup = mds_quota_setup,
665 .quota_cleanup = mds_quota_cleanup,
666 .quota_check = target_quota_check,
667 .quota_ctl = mds_quota_ctl,
668 .quota_fs_cleanup =mds_quota_fs_cleanup,
669 .quota_recovery = mds_quota_recovery,
670 .quota_adjust = mds_quota_adjust,
673 quota_interface_t filter_quota_interface = {
674 .quota_setup = filter_quota_setup,
675 .quota_cleanup = filter_quota_cleanup,
676 .quota_check = target_quota_check,
677 .quota_ctl = filter_quota_ctl,
678 .quota_setinfo = filter_quota_setinfo,
679 .quota_enforce = filter_quota_enforce,
680 .quota_getflag = filter_quota_getflag,
681 .quota_acquire = filter_quota_acquire,
682 .quota_adjust = filter_quota_adjust,
684 #endif /* __KERNEL__ */
686 quota_interface_t mdc_quota_interface = {
687 .quota_ctl = client_quota_ctl,
688 .quota_check = client_quota_check,
689 .quota_poll_check = client_quota_poll_check,
692 quota_interface_t osc_quota_interface = {
693 .quota_ctl = client_quota_ctl,
694 .quota_check = client_quota_check,
695 .quota_poll_check = client_quota_poll_check,
696 .quota_init = osc_quota_init,
697 .quota_exit = osc_quota_exit,
698 .quota_chkdq = osc_quota_chkdq,
699 .quota_setdq = osc_quota_setdq,
700 .quota_cleanup = osc_quota_cleanup,
703 quota_interface_t lov_quota_interface = {
704 .quota_check = lov_quota_check,
705 .quota_ctl = lov_quota_ctl,
709 static int __init init_lustre_quota(void)
711 int rc = qunit_cache_init();
714 PORTAL_SYMBOL_REGISTER(filter_quota_interface);
715 PORTAL_SYMBOL_REGISTER(mds_quota_interface);
716 PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
717 PORTAL_SYMBOL_REGISTER(osc_quota_interface);
718 PORTAL_SYMBOL_REGISTER(lov_quota_interface);
722 static void /*__exit*/ exit_lustre_quota(void)
724 PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
725 PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
726 PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
727 PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
728 PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
730 qunit_cache_cleanup();
733 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
734 MODULE_DESCRIPTION("Lustre Quota");
735 MODULE_LICENSE("GPL");
737 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
739 EXPORT_SYMBOL(mds_quota_interface);
740 EXPORT_SYMBOL(filter_quota_interface);
741 EXPORT_SYMBOL(mdc_quota_interface);
742 EXPORT_SYMBOL(osc_quota_interface);
743 EXPORT_SYMBOL(lov_quota_interface);
744 #endif /* __KERNEL */