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"
49 /* quota proc file handling functions */
51 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count,
54 struct obd_device *obd = (struct obd_device *)data;
57 return snprintf(page, count, "%lu\n",
58 obd->u.obt.obt_qctxt.lqc_bunit_sz);
60 EXPORT_SYMBOL(lprocfs_rd_bunit);
62 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count,
65 struct obd_device *obd = (struct obd_device *)data;
68 return snprintf(page, count, "%lu\n",
69 obd->u.obt.obt_qctxt.lqc_iunit_sz);
71 EXPORT_SYMBOL(lprocfs_rd_iunit);
73 int lprocfs_wr_bunit(struct file *file, const char *buffer,
74 unsigned long count, void *data)
76 struct obd_device *obd = (struct obd_device *)data;
80 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);
253 int lprocfs_wr_type(struct file *file, const char *buffer,
254 unsigned long count, void *data)
256 struct obd_device *obd = (struct obd_device *)data;
257 struct obd_device_target *obt = &obd->u.obt;
259 char stype[MAX_STYPE_SIZE + 1] = "";
260 LASSERT(obd != NULL);
262 if (copy_from_user(stype, buffer, MAX_STYPE_SIZE))
265 if (strchr(stype, 'u'))
267 if (strchr(stype, 'g'))
270 obt->obt_qctxt.lqc_atype = type;
275 if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME))
276 auto_quota_on(obd, type - 1, obt->obt_sb, 1);
277 else if (!strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME))
278 auto_quota_on(obd, type - 1, obt->obt_sb, 0);
284 EXPORT_SYMBOL(lprocfs_wr_type);
287 static int filter_quota_setup(struct obd_device *obd)
290 struct obd_device_target *obt = &obd->u.obt;
293 atomic_set(&obt->obt_quotachecking, 1);
294 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, NULL);
296 CERROR("initialize quota context failed! (rc:%d)\n", rc);
303 static int filter_quota_cleanup(struct obd_device *obd)
305 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
309 static int filter_quota_setinfo(struct obd_export *exp, struct obd_device *obd)
311 struct obd_import *imp;
313 /* setup the quota context import */
314 obd->u.obt.obt_qctxt.lqc_import = exp->exp_imp_reverse;
316 /* make imp's connect flags equal relative exp's connect flags
317 * adding it to avoid the scan export list
319 imp = exp->exp_imp_reverse;
321 imp->imp_connect_data.ocd_connect_flags |=
322 (exp->exp_connect_flags & OBD_CONNECT_QUOTA64);
324 /* start quota slave recovery thread. (release high limits) */
325 qslave_start_recovery(obd, &obd->u.obt.obt_qctxt);
328 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
332 if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
336 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
338 cap_lower(current->cap_effective, CAP_SYS_RESOURCE);
343 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
345 struct obd_device_target *obt = &obd->u.obt;
346 int err, cnt, rc = 0;
347 struct obd_quotactl *oqctl;
350 if (!sb_any_quota_enabled(obt->obt_sb))
353 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
355 OBD_ALLOC_PTR(oqctl);
357 CERROR("Not enough memory!");
361 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
362 memset(oqctl, 0, sizeof(*oqctl));
364 oqctl->qc_cmd = Q_GETQUOTA;
365 oqctl->qc_type = cnt;
366 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
367 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
374 /* set over quota flags for a uid/gid */
375 oa->o_valid |= (cnt == USRQUOTA) ?
376 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA;
377 if (oqctl->qc_dqblk.dqb_bhardlimit &&
378 (toqb(oqctl->qc_dqblk.dqb_curspace) >
379 oqctl->qc_dqblk.dqb_bhardlimit))
380 oa->o_flags |= (cnt == USRQUOTA) ?
381 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
387 static int filter_quota_acquire(struct obd_device *obd, unsigned int uid,
390 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
394 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 1);
395 RETURN(rc == -EAGAIN);
398 static int mds_quota_init(void)
400 return lustre_dquot_init();
403 static int mds_quota_exit(void)
409 /* check whether the left quota of certain uid and uid can satisfy a write rpc
410 * when need to acquire quota, return QUOTA_RET_ACQUOTA */
411 static int filter_quota_check(struct obd_device *obd, unsigned int uid,
412 unsigned int gid, int npage)
414 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
416 __u32 id[MAXQUOTAS] = { uid, gid };
417 struct qunit_data qdata[MAXQUOTAS];
421 CLASSERT(MAXQUOTAS < 4);
422 if (!sb_any_quota_enabled(qctxt->lqc_sb))
425 for (i = 0; i < MAXQUOTAS; i++) {
426 qdata[i].qd_id = id[i];
427 qdata[i].qd_flags = i;
428 qdata[i].qd_flags |= QUOTA_IS_BLOCK;
429 qdata[i].qd_count = 0;
431 qctxt_wait_pending_dqacq(qctxt, id[i], i, 1);
432 rc = compute_remquota(obd, qctxt, &qdata[i]);
433 if (rc == QUOTA_RET_OK &&
434 qdata[i].qd_count < npage * CFS_PAGE_SIZE)
435 RETURN(QUOTA_RET_ACQUOTA);
441 static int mds_quota_setup(struct obd_device *obd)
443 struct obd_device_target *obt = &obd->u.obt;
444 struct mds_obd *mds = &obd->u.mds;
448 atomic_set(&obt->obt_quotachecking, 1);
449 /* initialize quota master and quota context */
450 sema_init(&mds->mds_qonoff_sem, 1);
451 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, dqacq_handler);
453 CERROR("initialize quota context failed! (rc:%d)\n", rc);
460 static int mds_quota_cleanup(struct obd_device *obd)
462 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
466 static int mds_quota_fs_cleanup(struct obd_device *obd)
468 struct mds_obd *mds = &obd->u.mds;
472 /* close admin quota files */
473 down(&mds->mds_qonoff_sem);
474 for (i = 0; i < MAXQUOTAS; i++) {
475 if (mds->mds_quota_info.qi_files[i]) {
476 filp_close(mds->mds_quota_info.qi_files[i], 0);
477 mds->mds_quota_info.qi_files[i] = NULL;
480 up(&mds->mds_qonoff_sem);
483 #endif /* __KERNEL__ */
485 struct osc_quota_info {
486 struct list_head oqi_hash; /* hash list */
487 struct client_obd *oqi_cli; /* osc obd */
488 unsigned int oqi_id; /* uid/gid of a file */
489 short oqi_type; /* quota type */
492 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
494 static struct list_head qinfo_hash[NR_DQHASH];
495 /* SLAB cache for client quota context */
496 cfs_mem_cache_t *qinfo_cachep = NULL;
498 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
499 __attribute__((__const__));
501 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
503 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
504 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
508 /* caller must hold qinfo_list_lock */
509 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
511 struct list_head *head = qinfo_hash +
512 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
514 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
515 list_add(&oqi->oqi_hash, head);
518 /* caller must hold qinfo_list_lock */
519 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
521 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
522 list_del_init(&oqi->oqi_hash);
525 /* caller must hold qinfo_list_lock */
526 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
527 unsigned int id, int type)
529 unsigned int hashent = hashfn(cli, id, type);
530 struct osc_quota_info *oqi;
532 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
533 list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
534 if (oqi->oqi_cli == cli &&
535 oqi->oqi_id == id && oqi->oqi_type == type)
541 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
542 unsigned int id, int type)
544 struct osc_quota_info *oqi;
547 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
551 INIT_LIST_HEAD(&oqi->oqi_hash);
554 oqi->oqi_type = type;
559 static void free_qinfo(struct osc_quota_info *oqi)
561 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
564 int osc_quota_chkdq(struct client_obd *cli,
565 unsigned int uid, unsigned int gid)
568 int cnt, rc = QUOTA_OK;
571 spin_lock(&qinfo_list_lock);
572 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
573 struct osc_quota_info *oqi = NULL;
575 id = (cnt == USRQUOTA) ? uid : gid;
576 oqi = find_qinfo(cli, id, cnt);
582 spin_unlock(&qinfo_list_lock);
587 int osc_quota_setdq(struct client_obd *cli,
588 unsigned int uid, unsigned int gid,
589 obd_flag valid, obd_flag flags)
597 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
598 struct osc_quota_info *oqi, *old;
600 if (!(valid & ((cnt == USRQUOTA) ?
601 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
604 id = (cnt == USRQUOTA) ? uid : gid;
605 noquota = (cnt == USRQUOTA) ?
606 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
608 oqi = alloc_qinfo(cli, id, cnt);
610 spin_lock(&qinfo_list_lock);
612 old = find_qinfo(cli, id, cnt);
614 remove_qinfo_hash(old);
615 else if (!old && noquota)
616 insert_qinfo_hash(oqi);
618 spin_unlock(&qinfo_list_lock);
625 CERROR("not enough mem!\n");
634 int osc_quota_cleanup(struct obd_device *obd)
636 struct client_obd *cli = &obd->u.cli;
637 struct osc_quota_info *oqi, *n;
641 spin_lock(&qinfo_list_lock);
642 for (i = 0; i < NR_DQHASH; i++) {
643 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
644 if (oqi->oqi_cli != cli)
646 remove_qinfo_hash(oqi);
650 spin_unlock(&qinfo_list_lock);
655 int osc_quota_init(void)
660 LASSERT(qinfo_cachep == NULL);
661 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
662 sizeof(struct osc_quota_info),
667 for (i = 0; i < NR_DQHASH; i++)
668 INIT_LIST_HEAD(qinfo_hash + i);
673 int osc_quota_exit(void)
675 struct osc_quota_info *oqi, *n;
679 spin_lock(&qinfo_list_lock);
680 for (i = 0; i < NR_DQHASH; i++) {
681 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
682 remove_qinfo_hash(oqi);
686 spin_unlock(&qinfo_list_lock);
688 rc = cfs_mem_cache_destroy(qinfo_cachep);
689 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
696 quota_interface_t mds_quota_interface = {
697 .quota_init = mds_quota_init,
698 .quota_exit = mds_quota_exit,
699 .quota_setup = mds_quota_setup,
700 .quota_cleanup = mds_quota_cleanup,
701 .quota_check = target_quota_check,
702 .quota_ctl = mds_quota_ctl,
703 .quota_fs_cleanup =mds_quota_fs_cleanup,
704 .quota_recovery = mds_quota_recovery,
705 .quota_adjust = mds_quota_adjust,
708 quota_interface_t filter_quota_interface = {
709 .quota_setup = filter_quota_setup,
710 .quota_cleanup = filter_quota_cleanup,
711 .quota_check = target_quota_check,
712 .quota_ctl = filter_quota_ctl,
713 .quota_setinfo = filter_quota_setinfo,
714 .quota_enforce = filter_quota_enforce,
715 .quota_getflag = filter_quota_getflag,
716 .quota_acquire = filter_quota_acquire,
717 .quota_adjust = filter_quota_adjust,
718 .quota_chkquota = filter_quota_check,
720 #endif /* __KERNEL__ */
722 quota_interface_t mdc_quota_interface = {
723 .quota_ctl = client_quota_ctl,
724 .quota_check = client_quota_check,
725 .quota_poll_check = client_quota_poll_check,
728 quota_interface_t osc_quota_interface = {
729 .quota_ctl = client_quota_ctl,
730 .quota_check = client_quota_check,
731 .quota_poll_check = client_quota_poll_check,
732 .quota_init = osc_quota_init,
733 .quota_exit = osc_quota_exit,
734 .quota_chkdq = osc_quota_chkdq,
735 .quota_setdq = osc_quota_setdq,
736 .quota_cleanup = osc_quota_cleanup,
739 quota_interface_t lov_quota_interface = {
740 .quota_check = lov_quota_check,
741 .quota_ctl = lov_quota_ctl,
745 static int __init init_lustre_quota(void)
747 int rc = qunit_cache_init();
750 PORTAL_SYMBOL_REGISTER(filter_quota_interface);
751 PORTAL_SYMBOL_REGISTER(mds_quota_interface);
752 PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
753 PORTAL_SYMBOL_REGISTER(osc_quota_interface);
754 PORTAL_SYMBOL_REGISTER(lov_quota_interface);
758 static void /*__exit*/ exit_lustre_quota(void)
760 PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
761 PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
762 PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
763 PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
764 PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
766 qunit_cache_cleanup();
769 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
770 MODULE_DESCRIPTION("Lustre Quota");
771 MODULE_LICENSE("GPL");
773 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
775 EXPORT_SYMBOL(mds_quota_interface);
776 EXPORT_SYMBOL(filter_quota_interface);
777 EXPORT_SYMBOL(mdc_quota_interface);
778 EXPORT_SYMBOL(osc_quota_interface);
779 EXPORT_SYMBOL(lov_quota_interface);
780 #endif /* __KERNEL */