1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_MDS
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>
57 #include <obd_class.h>
58 #include <lustre_mds.h>
59 #include <lustre_dlm.h>
60 #include <lustre_cfg.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_quota.h>
64 #include <lprocfs_status.h>
65 #include "quota_internal.h"
69 /* quota proc file handling functions */
71 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count,
74 struct obd_device *obd = (struct obd_device *)data;
77 return snprintf(page, count, "%lu\n",
78 obd->u.obt.obt_qctxt.lqc_bunit_sz);
80 EXPORT_SYMBOL(lprocfs_rd_bunit);
82 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count,
85 struct obd_device *obd = (struct obd_device *)data;
88 return snprintf(page, count, "%lu\n",
89 obd->u.obt.obt_qctxt.lqc_iunit_sz);
91 EXPORT_SYMBOL(lprocfs_rd_iunit);
93 int lprocfs_wr_bunit(struct file *file, const char *buffer,
94 unsigned long count, void *data)
96 struct obd_device *obd = (struct obd_device *)data;
100 rc = lprocfs_write_helper(buffer, count, &val);
105 if (val % QUOTABLOCK_SIZE ||
106 val <= obd->u.obt.obt_qctxt.lqc_btune_sz)
109 obd->u.obt.obt_qctxt.lqc_bunit_sz = val;
112 EXPORT_SYMBOL(lprocfs_wr_bunit);
114 int lprocfs_wr_iunit(struct file *file, const char *buffer,
115 unsigned long count, void *data)
117 struct obd_device *obd = (struct obd_device *)data;
119 LASSERT(obd != NULL);
121 rc = lprocfs_write_helper(buffer, count, &val);
125 if (val <= obd->u.obt.obt_qctxt.lqc_itune_sz)
128 obd->u.obt.obt_qctxt.lqc_iunit_sz = val;
131 EXPORT_SYMBOL(lprocfs_wr_iunit);
133 int lprocfs_rd_btune(char *page, char **start, off_t off, int count,
134 int *eof, void *data)
136 struct obd_device *obd = (struct obd_device *)data;
137 LASSERT(obd != NULL);
139 return snprintf(page, count, "%lu\n",
140 obd->u.obt.obt_qctxt.lqc_btune_sz);
142 EXPORT_SYMBOL(lprocfs_rd_btune);
144 int lprocfs_rd_itune(char *page, char **start, off_t off, int count,
145 int *eof, void *data)
147 struct obd_device *obd = (struct obd_device *)data;
148 LASSERT(obd != NULL);
150 return snprintf(page, count, "%lu\n",
151 obd->u.obt.obt_qctxt.lqc_itune_sz);
153 EXPORT_SYMBOL(lprocfs_rd_itune);
155 int lprocfs_wr_btune(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 <= QUOTABLOCK_SIZE * MIN_QLIMIT || val % QUOTABLOCK_SIZE ||
167 val >= obd->u.obt.obt_qctxt.lqc_bunit_sz)
170 obd->u.obt.obt_qctxt.lqc_btune_sz = val;
173 EXPORT_SYMBOL(lprocfs_wr_btune);
175 int lprocfs_wr_itune(struct file *file, const char *buffer,
176 unsigned long count, void *data)
178 struct obd_device *obd = (struct obd_device *)data;
180 LASSERT(obd != NULL);
182 rc = lprocfs_write_helper(buffer, count, &val);
186 if (val <= MIN_QLIMIT ||
187 val >= obd->u.obt.obt_qctxt.lqc_iunit_sz)
190 obd->u.obt.obt_qctxt.lqc_itune_sz = val;
193 EXPORT_SYMBOL(lprocfs_wr_itune);
196 #define GROUP_QUOTA 2
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)
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);
208 strcpy(stype, "off");
210 if (type & USER_QUOTA)
212 if (type & GROUP_QUOTA)
216 return snprintf(page, count, "%s\n", stype);
218 EXPORT_SYMBOL(lprocfs_rd_type);
220 static int auto_quota_on(struct obd_device *obd, int type,
221 struct super_block *sb, int is_master)
223 struct obd_quotactl *oqctl;
224 struct lvfs_run_ctxt saved;
228 LASSERT(type == USRQUOTA || type == GRPQUOTA || type == UGQUOTA);
230 /* quota already turned on */
231 if (obd->u.obt.obt_qctxt.lqc_status)
234 OBD_ALLOC_PTR(oqctl);
238 oqctl->qc_type = type;
239 oqctl->qc_cmd = Q_QUOTAON;
240 oqctl->qc_id = QFMT_LDISKFS;
242 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
247 /* turn on cluster wide quota */
248 rc = mds_admin_quota_on(obd, oqctl);
250 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
251 "auto-enable admin quota failed. rc=%d\n", rc);
255 /* turn on local quota */
256 rc = fsfilt_quotactl(obd, sb, oqctl);
258 CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
259 "auto-enable local quota failed. rc=%d\n", rc);
261 mds_quota_off(obd, oqctl);
263 obd->u.obt.obt_qctxt.lqc_status = 1;
266 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
273 int lprocfs_wr_type(struct file *file, const char *buffer,
274 unsigned long count, void *data)
276 struct obd_device *obd = (struct obd_device *)data;
277 struct obd_device_target *obt = &obd->u.obt;
279 char stype[MAX_STYPE_SIZE + 1] = "";
280 LASSERT(obd != NULL);
282 if (copy_from_user(stype, buffer, MAX_STYPE_SIZE))
285 if (strchr(stype, 'u'))
287 if (strchr(stype, 'g'))
290 obt->obt_qctxt.lqc_atype = type;
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);
304 EXPORT_SYMBOL(lprocfs_wr_type);
307 static int filter_quota_setup(struct obd_device *obd)
310 struct obd_device_target *obt = &obd->u.obt;
313 atomic_set(&obt->obt_quotachecking, 1);
314 rc = qctxt_init(&obt->obt_qctxt, obt->obt_sb, NULL);
316 CERROR("initialize quota context failed! (rc:%d)\n", rc);
323 static int filter_quota_cleanup(struct obd_device *obd)
325 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
329 static int filter_quota_setinfo(struct obd_export *exp, struct obd_device *obd)
331 struct obd_import *imp;
333 /* setup the quota context import */
334 obd->u.obt.obt_qctxt.lqc_import = exp->exp_imp_reverse;
336 /* make imp's connect flags equal relative exp's connect flags
337 * adding it to avoid the scan export list
339 imp = exp->exp_imp_reverse;
341 imp->imp_connect_data.ocd_connect_flags |=
342 (exp->exp_connect_flags & OBD_CONNECT_QUOTA64);
344 /* start quota slave recovery thread. (release high limits) */
345 qslave_start_recovery(obd, &obd->u.obt.obt_qctxt);
348 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
352 if (!sb_any_quota_enabled(obd->u.obt.obt_sb))
356 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
358 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
363 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
365 struct obd_device_target *obt = &obd->u.obt;
366 int err, cnt, rc = 0;
367 struct obd_quotactl *oqctl;
370 if (!sb_any_quota_enabled(obt->obt_sb))
373 oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
375 OBD_ALLOC_PTR(oqctl);
377 CERROR("Not enough memory!");
381 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
382 memset(oqctl, 0, sizeof(*oqctl));
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);
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;
407 static int filter_quota_acquire(struct obd_device *obd, unsigned int uid,
410 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
414 rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 1, 1);
415 RETURN(rc == -EAGAIN);
418 static int mds_quota_init(void)
420 return lustre_dquot_init();
423 static int mds_quota_exit(void)
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)
434 struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
436 __u32 id[MAXQUOTAS] = { uid, gid };
437 struct qunit_data qdata[MAXQUOTAS];
441 CLASSERT(MAXQUOTAS < 4);
442 if (!sb_any_quota_enabled(qctxt->lqc_sb))
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;
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);
461 static int mds_quota_setup(struct obd_device *obd)
463 struct obd_device_target *obt = &obd->u.obt;
464 struct mds_obd *mds = &obd->u.mds;
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);
473 CERROR("initialize quota context failed! (rc:%d)\n", rc);
480 static int mds_quota_cleanup(struct obd_device *obd)
482 qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
486 static int mds_quota_fs_cleanup(struct obd_device *obd)
488 struct mds_obd *mds = &obd->u.mds;
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;
500 up(&mds->mds_qonoff_sem);
503 #endif /* __KERNEL__ */
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 */
512 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
514 static struct list_head qinfo_hash[NR_DQHASH];
515 /* SLAB cache for client quota context */
516 cfs_mem_cache_t *qinfo_cachep = NULL;
518 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
519 __attribute__((__const__));
521 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
523 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
524 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
528 /* caller must hold qinfo_list_lock */
529 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
531 struct list_head *head = qinfo_hash +
532 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
534 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
535 list_add(&oqi->oqi_hash, head);
538 /* caller must hold qinfo_list_lock */
539 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
541 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
542 list_del_init(&oqi->oqi_hash);
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)
549 unsigned int hashent = hashfn(cli, id, type);
550 struct osc_quota_info *oqi;
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)
561 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
562 unsigned int id, int type)
564 struct osc_quota_info *oqi;
567 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
571 CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
574 oqi->oqi_type = type;
579 static void free_qinfo(struct osc_quota_info *oqi)
581 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
584 int osc_quota_chkdq(struct client_obd *cli,
585 unsigned int uid, unsigned int gid)
588 int cnt, rc = QUOTA_OK;
591 spin_lock(&qinfo_list_lock);
592 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
593 struct osc_quota_info *oqi = NULL;
595 id = (cnt == USRQUOTA) ? uid : gid;
596 oqi = find_qinfo(cli, id, cnt);
602 spin_unlock(&qinfo_list_lock);
607 int osc_quota_setdq(struct client_obd *cli,
608 unsigned int uid, unsigned int gid,
609 obd_flag valid, obd_flag flags)
617 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
618 struct osc_quota_info *oqi, *old;
620 if (!(valid & ((cnt == USRQUOTA) ?
621 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
624 id = (cnt == USRQUOTA) ? uid : gid;
625 noquota = (cnt == USRQUOTA) ?
626 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
628 oqi = alloc_qinfo(cli, id, cnt);
630 spin_lock(&qinfo_list_lock);
632 old = find_qinfo(cli, id, cnt);
634 remove_qinfo_hash(old);
635 else if (!old && noquota)
636 insert_qinfo_hash(oqi);
638 spin_unlock(&qinfo_list_lock);
645 CERROR("not enough mem!\n");
654 int osc_quota_cleanup(struct obd_device *obd)
656 struct client_obd *cli = &obd->u.cli;
657 struct osc_quota_info *oqi, *n;
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)
666 remove_qinfo_hash(oqi);
670 spin_unlock(&qinfo_list_lock);
675 int osc_quota_init(void)
680 LASSERT(qinfo_cachep == NULL);
681 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
682 sizeof(struct osc_quota_info),
687 for (i = 0; i < NR_DQHASH; i++)
688 CFS_INIT_LIST_HEAD(qinfo_hash + i);
693 int osc_quota_exit(void)
695 struct osc_quota_info *oqi, *n;
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);
706 spin_unlock(&qinfo_list_lock);
708 rc = cfs_mem_cache_destroy(qinfo_cachep);
709 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
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,
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,
740 #endif /* __KERNEL__ */
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,
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,
759 quota_interface_t lov_quota_interface = {
760 .quota_check = lov_quota_check,
761 .quota_ctl = lov_quota_ctl,
765 static int __init init_lustre_quota(void)
767 int rc = qunit_cache_init();
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);
778 static void /*__exit*/ exit_lustre_quota(void)
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);
786 qunit_cache_cleanup();
789 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
790 MODULE_DESCRIPTION("Lustre Quota");
791 MODULE_LICENSE("GPL");
793 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
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 */