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 # include <linux/smp_lock.h>
26 # include <linux/buffer_head.h>
27 # include <linux/workqueue.h>
28 # include <linux/mount.h>
29 #else /* __KERNEL__ */
30 # include <liblustre.h>
33 #include <obd_class.h>
34 #include <lustre_mds.h>
35 #include <lustre_dlm.h>
36 #include <lustre_cfg.h>
38 #include <lustre_fsfilt.h>
39 #include <lustre_quota.h>
40 #include <lprocfs_status.h>
41 #include "quota_internal.h"
45 /* quota proc file handling functions */
47 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count,
50 struct obd_device *obd = (struct obd_device *)data;
53 return snprintf(page, count, "%lu\n",
54 obd->u.obt.obt_qctxt.lqc_bunit_sz);
56 EXPORT_SYMBOL(lprocfs_rd_bunit);
58 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count,
61 struct obd_device *obd = (struct obd_device *)data;
64 return snprintf(page, count, "%lu\n",
65 obd->u.obt.obt_qctxt.lqc_iunit_sz);
67 EXPORT_SYMBOL(lprocfs_rd_iunit);
69 int lprocfs_wr_bunit(struct file *file, const char *buffer,
70 unsigned long count, void *data)
72 struct obd_device *obd = (struct obd_device *)data;
76 rc = lprocfs_write_helper(buffer, count, &val);
81 if (val % QUOTABLOCK_SIZE ||
82 val <= obd->u.obt.obt_qctxt.lqc_btune_sz)
85 obd->u.obt.obt_qctxt.lqc_bunit_sz = val;
88 EXPORT_SYMBOL(lprocfs_wr_bunit);
90 int lprocfs_wr_iunit(struct file *file, const char *buffer,
91 unsigned long count, void *data)
93 struct obd_device *obd = (struct obd_device *)data;
97 rc = lprocfs_write_helper(buffer, count, &val);
101 if (val <= obd->u.obt.obt_qctxt.lqc_itune_sz)
104 obd->u.obt.obt_qctxt.lqc_iunit_sz = val;
107 EXPORT_SYMBOL(lprocfs_wr_iunit);
109 int lprocfs_rd_btune(char *page, char **start, off_t off, int count,
110 int *eof, void *data)
112 struct obd_device *obd = (struct obd_device *)data;
113 LASSERT(obd != NULL);
115 return snprintf(page, count, "%lu\n",
116 obd->u.obt.obt_qctxt.lqc_btune_sz);
118 EXPORT_SYMBOL(lprocfs_rd_btune);
120 int lprocfs_rd_itune(char *page, char **start, off_t off, int count,
121 int *eof, void *data)
123 struct obd_device *obd = (struct obd_device *)data;
124 LASSERT(obd != NULL);
126 return snprintf(page, count, "%lu\n",
127 obd->u.obt.obt_qctxt.lqc_itune_sz);
129 EXPORT_SYMBOL(lprocfs_rd_itune);
131 int lprocfs_wr_btune(struct file *file, const char *buffer,
132 unsigned long count, void *data)
134 struct obd_device *obd = (struct obd_device *)data;
136 LASSERT(obd != NULL);
138 rc = lprocfs_write_helper(buffer, count, &val);
142 if (val <= QUOTABLOCK_SIZE * MIN_QLIMIT || val % QUOTABLOCK_SIZE ||
143 val >= obd->u.obt.obt_qctxt.lqc_bunit_sz)
146 obd->u.obt.obt_qctxt.lqc_btune_sz = val;
149 EXPORT_SYMBOL(lprocfs_wr_btune);
151 int lprocfs_wr_itune(struct file *file, const char *buffer,
152 unsigned long count, void *data)
154 struct obd_device *obd = (struct obd_device *)data;
156 LASSERT(obd != NULL);
158 rc = lprocfs_write_helper(buffer, count, &val);
162 if (val <= MIN_QLIMIT ||
163 val >= obd->u.obt.obt_qctxt.lqc_iunit_sz)
166 obd->u.obt.obt_qctxt.lqc_itune_sz = val;
169 EXPORT_SYMBOL(lprocfs_wr_itune);
172 #define GROUP_QUOTA 2
174 #define MAX_STYPE_SIZE 4
175 int lprocfs_rd_type(char *page, char **start, off_t off, int count,
176 int *eof, void *data)
178 struct obd_device *obd = (struct obd_device *)data;
179 char stype[MAX_STYPE_SIZE + 1] = "";
180 int type = obd->u.obt.obt_qctxt.lqc_atype;
181 LASSERT(obd != NULL);
184 strcpy(stype, "off");
186 if (type & USER_QUOTA)
188 if (type & GROUP_QUOTA)
192 return snprintf(page, count, "%s\n", stype);
194 EXPORT_SYMBOL(lprocfs_rd_type);
196 static int auto_quota_on(struct obd_device *obd, int type,
197 struct super_block *sb, int is_master)
199 struct obd_quotactl *oqctl;
200 struct lvfs_run_ctxt saved;
204 LASSERT(type == USRQUOTA || type == GRPQUOTA || type == UGQUOTA);
206 /* quota already turned on */
207 if (obd->u.obt.obt_qctxt.lqc_status)
210 OBD_ALLOC_PTR(oqctl);
214 oqctl->qc_type = type;
215 oqctl->qc_cmd = Q_QUOTAON;
216 oqctl->qc_id = QFMT_LDISKFS;
218 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
223 /* turn on cluster wide quota */
224 rc = mds_admin_quota_on(obd, oqctl);
226 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
227 "auto-enable admin quota failed. rc=%d\n", rc);
231 /* turn on local quota */
232 rc = fsfilt_quotactl(obd, sb, oqctl);
234 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
235 "auto-enable local quota failed. rc=%d\n", rc);
237 mds_quota_off(obd, oqctl);
239 obd->u.obt.obt_qctxt.lqc_status = 1;
242 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
249 int lprocfs_wr_type(struct file *file, const char *buffer,
250 unsigned long count, void *data)
252 struct obd_device *obd = (struct obd_device *)data;
253 struct obd_device_target *obt = &obd->u.obt;
255 char stype[MAX_STYPE_SIZE + 1] = "";
256 LASSERT(obd != NULL);
258 if (copy_from_user(stype, buffer, MAX_STYPE_SIZE))
261 if (strchr(stype, 'u'))
263 if (strchr(stype, 'g'))
266 obt->obt_qctxt.lqc_atype = type;
271 if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME))
272 auto_quota_on(obd, type - 1, obt->obt_sb, 1);
273 else if (!strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME))
274 auto_quota_on(obd, type - 1, obt->obt_sb, 0);
280 EXPORT_SYMBOL(lprocfs_wr_type);
283 static int filter_quota_setup(struct obd_device *obd)
286 struct obd_device_target *obt = &obd->u.obt;
289 atomic_set(&obt->obt_quotachecking, 1);
290 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, NULL);
292 CERROR("initialize quota context failed! (rc:%d)\n", rc);
299 static int filter_quota_cleanup(struct obd_device *obd)
301 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
305 static int filter_quota_setinfo(struct obd_export *exp, struct obd_device *obd)
307 struct obd_import *imp;
309 /* setup the quota context import */
310 obd->u.obt.obt_qctxt.lqc_import = exp->exp_imp_reverse;
312 /* make imp's connect flags equal relative exp's connect flags
313 * adding it to avoid the scan export list
315 imp = exp->exp_imp_reverse;
317 imp->imp_connect_data.ocd_connect_flags |=
318 (exp->exp_connect_flags & OBD_CONNECT_QUOTA64);
320 /* start quota slave recovery thread. (release high limits) */
321 qslave_start_recovery(obd, &obd->u.obt.obt_qctxt);
324 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
328 if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
332 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
334 cap_lower(current->cap_effective, CAP_SYS_RESOURCE);
339 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
341 struct obd_device_target *obt = &obd->u.obt;
342 int err, cnt, rc = 0;
343 struct obd_quotactl *oqctl;
346 if (!sb_any_quota_enabled(obt->obt_sb))
349 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
351 OBD_ALLOC_PTR(oqctl);
353 CERROR("Not enough memory!");
357 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
358 memset(oqctl, 0, sizeof(*oqctl));
360 oqctl->qc_cmd = Q_GETQUOTA;
361 oqctl->qc_type = cnt;
362 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
363 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
370 /* set over quota flags for a uid/gid */
371 oa->o_valid |= (cnt == USRQUOTA) ?
372 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA;
373 if (oqctl->qc_dqblk.dqb_bhardlimit &&
374 (toqb(oqctl->qc_dqblk.dqb_curspace) >
375 oqctl->qc_dqblk.dqb_bhardlimit))
376 oa->o_flags |= (cnt == USRQUOTA) ?
377 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
383 static int filter_quota_acquire(struct obd_device *obd, unsigned int uid,
386 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
390 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 1);
391 RETURN(rc == -EAGAIN);
394 static int mds_quota_init(void)
396 return lustre_dquot_init();
399 static int mds_quota_exit(void)
405 /* check whether the left quota of certain uid and uid can satisfy a write rpc
406 * when need to acquire quota, return QUOTA_RET_ACQUOTA */
407 static int filter_quota_check(struct obd_device *obd, unsigned int uid,
408 unsigned int gid, int npage)
410 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
412 __u32 id[MAXQUOTAS] = { uid, gid };
413 struct qunit_data qdata[MAXQUOTAS];
417 CLASSERT(MAXQUOTAS < 4);
418 if (!sb_any_quota_enabled(qctxt->lqc_sb))
421 for (i = 0; i < MAXQUOTAS; i++) {
422 qdata[i].qd_id = id[i];
423 qdata[i].qd_flags = i;
424 qdata[i].qd_flags |= QUOTA_IS_BLOCK;
425 qdata[i].qd_count = 0;
427 qctxt_wait_pending_dqacq(qctxt, id[i], i, 1);
428 rc = compute_remquota(obd, qctxt, &qdata[i]);
429 if (rc == QUOTA_RET_OK &&
430 qdata[i].qd_count < npage * CFS_PAGE_SIZE)
431 RETURN(QUOTA_RET_ACQUOTA);
437 static int mds_quota_setup(struct obd_device *obd)
439 struct obd_device_target *obt = &obd->u.obt;
440 struct mds_obd *mds = &obd->u.mds;
444 atomic_set(&obt->obt_quotachecking, 1);
445 /* initialize quota master and quota context */
446 sema_init(&mds->mds_qonoff_sem, 1);
447 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, dqacq_handler);
449 CERROR("initialize quota context failed! (rc:%d)\n", rc);
456 static int mds_quota_cleanup(struct obd_device *obd)
458 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
462 static int mds_quota_fs_cleanup(struct obd_device *obd)
464 struct mds_obd *mds = &obd->u.mds;
468 /* close admin quota files */
469 down(&mds->mds_qonoff_sem);
470 for (i = 0; i < MAXQUOTAS; i++) {
471 if (mds->mds_quota_info.qi_files[i]) {
472 filp_close(mds->mds_quota_info.qi_files[i], 0);
473 mds->mds_quota_info.qi_files[i] = NULL;
476 up(&mds->mds_qonoff_sem);
479 #endif /* __KERNEL__ */
481 struct osc_quota_info {
482 struct list_head oqi_hash; /* hash list */
483 struct client_obd *oqi_cli; /* osc obd */
484 unsigned int oqi_id; /* uid/gid of a file */
485 short oqi_type; /* quota type */
488 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
490 static struct list_head qinfo_hash[NR_DQHASH];
491 /* SLAB cache for client quota context */
492 cfs_mem_cache_t *qinfo_cachep = NULL;
494 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
495 __attribute__((__const__));
497 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
499 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
500 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
504 /* caller must hold qinfo_list_lock */
505 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
507 struct list_head *head = qinfo_hash +
508 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
510 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
511 list_add(&oqi->oqi_hash, head);
514 /* caller must hold qinfo_list_lock */
515 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
517 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
518 list_del_init(&oqi->oqi_hash);
521 /* caller must hold qinfo_list_lock */
522 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
523 unsigned int id, int type)
525 unsigned int hashent = hashfn(cli, id, type);
526 struct osc_quota_info *oqi;
528 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
529 list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
530 if (oqi->oqi_cli == cli &&
531 oqi->oqi_id == id && oqi->oqi_type == type)
537 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
538 unsigned int id, int type)
540 struct osc_quota_info *oqi;
543 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
547 INIT_LIST_HEAD(&oqi->oqi_hash);
550 oqi->oqi_type = type;
555 static void free_qinfo(struct osc_quota_info *oqi)
557 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
560 int osc_quota_chkdq(struct client_obd *cli,
561 unsigned int uid, unsigned int gid)
564 int cnt, rc = QUOTA_OK;
567 spin_lock(&qinfo_list_lock);
568 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
569 struct osc_quota_info *oqi = NULL;
571 id = (cnt == USRQUOTA) ? uid : gid;
572 oqi = find_qinfo(cli, id, cnt);
578 spin_unlock(&qinfo_list_lock);
583 int osc_quota_setdq(struct client_obd *cli,
584 unsigned int uid, unsigned int gid,
585 obd_flag valid, obd_flag flags)
593 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
594 struct osc_quota_info *oqi, *old;
596 if (!(valid & ((cnt == USRQUOTA) ?
597 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
600 id = (cnt == USRQUOTA) ? uid : gid;
601 noquota = (cnt == USRQUOTA) ?
602 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
604 oqi = alloc_qinfo(cli, id, cnt);
606 spin_lock(&qinfo_list_lock);
608 old = find_qinfo(cli, id, cnt);
610 remove_qinfo_hash(old);
611 else if (!old && noquota)
612 insert_qinfo_hash(oqi);
614 spin_unlock(&qinfo_list_lock);
621 CERROR("not enough mem!\n");
630 int osc_quota_cleanup(struct obd_device *obd)
632 struct client_obd *cli = &obd->u.cli;
633 struct osc_quota_info *oqi, *n;
637 spin_lock(&qinfo_list_lock);
638 for (i = 0; i < NR_DQHASH; i++) {
639 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
640 if (oqi->oqi_cli != cli)
642 remove_qinfo_hash(oqi);
646 spin_unlock(&qinfo_list_lock);
651 int osc_quota_init(void)
656 LASSERT(qinfo_cachep == NULL);
657 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
658 sizeof(struct osc_quota_info),
663 for (i = 0; i < NR_DQHASH; i++)
664 INIT_LIST_HEAD(qinfo_hash + i);
669 int osc_quota_exit(void)
671 struct osc_quota_info *oqi, *n;
675 spin_lock(&qinfo_list_lock);
676 for (i = 0; i < NR_DQHASH; i++) {
677 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
678 remove_qinfo_hash(oqi);
682 spin_unlock(&qinfo_list_lock);
684 rc = cfs_mem_cache_destroy(qinfo_cachep);
685 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
692 quota_interface_t mds_quota_interface = {
693 .quota_init = mds_quota_init,
694 .quota_exit = mds_quota_exit,
695 .quota_setup = mds_quota_setup,
696 .quota_cleanup = mds_quota_cleanup,
697 .quota_check = target_quota_check,
698 .quota_ctl = mds_quota_ctl,
699 .quota_fs_cleanup =mds_quota_fs_cleanup,
700 .quota_recovery = mds_quota_recovery,
701 .quota_adjust = mds_quota_adjust,
704 quota_interface_t filter_quota_interface = {
705 .quota_setup = filter_quota_setup,
706 .quota_cleanup = filter_quota_cleanup,
707 .quota_check = target_quota_check,
708 .quota_ctl = filter_quota_ctl,
709 .quota_setinfo = filter_quota_setinfo,
710 .quota_enforce = filter_quota_enforce,
711 .quota_getflag = filter_quota_getflag,
712 .quota_acquire = filter_quota_acquire,
713 .quota_adjust = filter_quota_adjust,
714 .quota_chkquota = filter_quota_check,
716 #endif /* __KERNEL__ */
718 quota_interface_t mdc_quota_interface = {
719 .quota_ctl = client_quota_ctl,
720 .quota_check = client_quota_check,
721 .quota_poll_check = client_quota_poll_check,
724 quota_interface_t osc_quota_interface = {
725 .quota_ctl = client_quota_ctl,
726 .quota_check = client_quota_check,
727 .quota_poll_check = client_quota_poll_check,
728 .quota_init = osc_quota_init,
729 .quota_exit = osc_quota_exit,
730 .quota_chkdq = osc_quota_chkdq,
731 .quota_setdq = osc_quota_setdq,
732 .quota_cleanup = osc_quota_cleanup,
735 quota_interface_t lov_quota_interface = {
736 .quota_check = lov_quota_check,
737 .quota_ctl = lov_quota_ctl,
741 static int __init init_lustre_quota(void)
743 int rc = qunit_cache_init();
746 PORTAL_SYMBOL_REGISTER(filter_quota_interface);
747 PORTAL_SYMBOL_REGISTER(mds_quota_interface);
748 PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
749 PORTAL_SYMBOL_REGISTER(osc_quota_interface);
750 PORTAL_SYMBOL_REGISTER(lov_quota_interface);
754 static void /*__exit*/ exit_lustre_quota(void)
756 PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
757 PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
758 PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
759 PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
760 PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
762 qunit_cache_cleanup();
765 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
766 MODULE_DESCRIPTION("Lustre Quota");
767 MODULE_LICENSE("GPL");
769 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
771 EXPORT_SYMBOL(mds_quota_interface);
772 EXPORT_SYMBOL(filter_quota_interface);
773 EXPORT_SYMBOL(mdc_quota_interface);
774 EXPORT_SYMBOL(osc_quota_interface);
775 EXPORT_SYMBOL(lov_quota_interface);
776 #endif /* __KERNEL */