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 (c) 2007, 2010, Oracle and/or its affiliates. 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.
37 #ifndef _LUSTRE_QUOTA_H
38 #define _LUSTRE_QUOTA_H
40 /** \defgroup quota quota
45 #if defined(__linux__)
46 #include <linux/lustre_quota.h>
47 #elif defined(__APPLE__)
48 #include <darwin/lustre_quota.h>
49 #elif defined(__WINNT__)
50 #include <winnt/lustre_quota.h>
52 #error Unsupported operating system.
55 #include <lustre_net.h>
56 #include <lustre/lustre_idl.h>
58 #include <obd_support.h>
67 #ifndef QUOTABLOCK_BITS
68 #define QUOTABLOCK_BITS 10
71 #ifndef QUOTABLOCK_SIZE
72 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
76 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
79 #ifdef HAVE_QUOTA_SUPPORT
82 #define MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
86 #define MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
93 LQUOTA_FIRST_STAT = 0,
96 * these four are for measuring quota requests, for both of
97 * quota master and quota slaves
99 LQUOTA_SYNC_ACQ = LQUOTA_FIRST_STAT,
106 * these four measure how much time I/O threads spend on dealing
107 * with quota before and after writing data or creating files,
108 * only for quota slaves(lquota_chkquota and lquota_pending_commit)
110 LQUOTA_WAIT_FOR_CHK_BLK,
111 LQUOTA_WAIT_FOR_CHK_INO,
112 LQUOTA_WAIT_FOR_COMMIT_BLK,
113 LQUOTA_WAIT_FOR_COMMIT_INO,
117 * these two are for measuring time waiting return of quota reqs
118 * (qctxt_wait_pending_dqacq), only for quota salves
120 LQUOTA_WAIT_PENDING_BLK_QUOTA,
121 LQUOTA_WAIT_PENDING_INO_QUOTA,
125 * these two are for those when they are calling
126 * qctxt_wait_pending_dqacq, the quota req has returned already,
127 * only for quota salves
129 LQUOTA_NOWAIT_PENDING_BLK_QUOTA,
130 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
134 * these are for quota ctl
140 * these are for adjust quota qunit, for both of
141 * quota master and quota slaves
149 /* structures to access admin quotafile */
150 struct lustre_mem_dqinfo {
151 unsigned int dqi_bgrace;
152 unsigned int dqi_igrace;
153 unsigned long dqi_flags;
154 unsigned int dqi_blocks;
155 unsigned int dqi_free_blk;
156 unsigned int dqi_free_entry;
159 struct lustre_quota_info {
160 struct file *qi_files[MAXQUOTAS];
161 struct lustre_mem_dqinfo qi_info[MAXQUOTAS];
162 lustre_quota_version_t qi_version;
165 struct lustre_mem_dqblk {
166 __u64 dqb_bhardlimit; /**< absolute limit on disk blks alloc */
167 __u64 dqb_bsoftlimit; /**< preferred limit on disk blks */
168 __u64 dqb_curspace; /**< current used space */
169 __u64 dqb_ihardlimit; /**< absolute limit on allocated inodes */
170 __u64 dqb_isoftlimit; /**< preferred inode limit */
171 __u64 dqb_curinodes; /**< current # allocated inodes */
172 time_t dqb_btime; /**< time limit for excessive disk use */
173 time_t dqb_itime; /**< time limit for excessive inode use */
176 struct lustre_dquot {
177 /** Hash list in memory, protect by dquot_hash_lock */
179 /** Protect the data in lustre_dquot */
180 cfs_semaphore_t dq_sem;
182 cfs_atomic_t dq_refcnt;
183 /** Pointer of quota info it belongs to */
184 struct lustre_quota_info *dq_info;
185 /** Offset of dquot on disk */
187 /** ID this applies to (uid, gid) */
189 /** Type fo quota (USRQUOTA, GRPQUOUTA) */
191 /** See DQ_ in quota.h */
192 unsigned long dq_flags;
193 /** Diskquota usage */
194 struct lustre_mem_dqblk dq_dqb;
202 /* set inode quota limitation on a quota uid/gid */
203 #define QI_SET (1 << 30)
204 /* set block quota limitation on a quota uid/gid */
205 #define QB_SET (1 << 31)
208 #define QFILE_RD_INFO 2
209 #define QFILE_WR_INFO 3
210 #define QFILE_INIT_INFO 4
211 #define QFILE_RD_DQUOT 5
212 #define QFILE_WR_DQUOT 6
213 #define QFILE_CONVERT 7
215 /* admin quotafile operations */
216 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type);
217 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type);
218 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type);
219 int lustre_read_dquot(struct lustre_dquot *dquot);
220 int lustre_commit_dquot(struct lustre_dquot *dquot);
221 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type);
222 int lustre_get_qids(struct file *file, struct inode *inode, int type,
224 int lustre_quota_convert(struct lustre_quota_info *lqi, int type);
226 typedef int (*dqacq_handler_t) (struct obd_device * obd, struct qunit_data * qd,
230 #ifdef HAVE_VFS_DQ_OFF
231 #define LL_DQUOT_OFF(sb, remount) vfs_dq_off(sb, remount)
233 #define LL_DQUOT_OFF(sb, remount) DQUOT_OFF(sb)
237 #define LL_DQUOT_OFF(sb) DQUOT_OFF(sb)
240 /* user quota is turned on on filter */
241 #define LQC_USRQUOTA_FLAG (1 << 0)
242 /* group quota is turned on on filter */
243 #define LQC_GRPQUOTA_FLAG (1 << 1)
245 #define UGQUOTA2LQC(id) ((Q_TYPEMATCH(id, USRQUOTA) ? LQC_USRQUOTA_FLAG : 0) | \
246 (Q_TYPEMATCH(id, GRPQUOTA) ? LQC_GRPQUOTA_FLAG : 0))
248 struct lustre_quota_ctxt {
249 /** superblock this applies to */
250 struct super_block *lqc_sb;
251 /** obd_device_target for obt_rwsem */
252 struct obd_device_target *lqc_obt;
253 /** import used to send dqacq/dqrel RPC */
254 struct obd_import *lqc_import;
255 /** dqacq/dqrel RPC handler, only for quota master */
256 dqacq_handler_t lqc_handler;
258 unsigned long lqc_flags;
260 unsigned long lqc_recovery:1, /** Doing recovery */
262 * the function of change qunit size
265 lqc_valid:1, /** this qctxt is valid or not */
267 * tell whether of not quota_type has
268 * been processed, so that the master
269 * knows when it can start processing
270 * incoming acq/rel quota requests
274 * original unit size of file quota and
275 * upper limitation for adjust file qunit
277 unsigned long lqc_iunit_sz;
279 * Trigger dqacq when available file
280 * quota less than this value, trigger
281 * dqrel when available file quota
282 * more than this value + 1 iunit
284 unsigned long lqc_itune_sz;
286 * original unit size of block quota and
287 * upper limitation for adjust block qunit
289 unsigned long lqc_bunit_sz;
290 /** See comment of lqc_itune_sz */
291 unsigned long lqc_btune_sz;
292 /** all lustre_qunit_size structures */
293 cfs_hash_t *lqc_lqs_hash;
297 * the values below are relative to how master change its qunit sizes
300 * this affects the boundary of
301 * shrinking and enlarging qunit size. default=4
303 unsigned long lqc_cqs_boundary_factor;
304 /** the least value of block qunit */
305 unsigned long lqc_cqs_least_bunit;
306 /** the least value of inode qunit */
307 unsigned long lqc_cqs_least_iunit;
309 * when enlarging, qunit size will
310 * mutilple it; when shrinking,
311 * qunit size will divide it
313 unsigned long lqc_cqs_qs_factor;
315 * avoid ping-pong effect of
316 * adjusting qunit size. How many
317 * seconds must be waited between
318 * enlarging and shinking qunit
321 int lqc_switch_seconds;
323 * when blk qunit reaches this value,
324 * later write reqs from client should be sync b=16642
327 /** guard lqc_imp_valid now */
328 cfs_spinlock_t lqc_lock;
330 * when mds isn't connected, threads
331 * on osts who send the quota reqs
332 * with wait==1 will be put here b=14840
334 cfs_waitq_t lqc_wait_for_qmaster;
335 struct proc_dir_entry *lqc_proc_dir;
336 /** lquota statistics */
337 struct lprocfs_stats *lqc_stats;
338 /** the number of used hashed lqs */
339 cfs_atomic_t lqc_lqs;
340 /** no lqs are in use */
341 cfs_waitq_t lqc_lqs_waitq;
344 #define QUOTA_MASTER_READY(qctxt) (qctxt)->lqc_setup = 1
345 #define QUOTA_MASTER_UNREADY(qctxt) (qctxt)->lqc_setup = 0
347 struct lustre_qunit_size {
348 cfs_hlist_node_t lqs_hash; /** the hash entry */
349 unsigned int lqs_id; /** id of user/group */
350 unsigned long lqs_flags; /** 31st bit is QB_SET, 30th bit is QI_SET
351 * other bits are same as LQUOTA_FLAGS_*
353 unsigned long lqs_iunit_sz; /** Unit size of file quota currently */
355 * Trigger dqacq when available file quota
356 * less than this value, trigger dqrel
357 * when more than this value + 1 iunit
359 unsigned long lqs_itune_sz;
360 unsigned long lqs_bunit_sz; /** Unit size of block quota currently */
361 unsigned long lqs_btune_sz; /** See comment of lqs itune sz */
362 /** the blocks reached ost and don't finish */
363 unsigned long lqs_bwrite_pending;
364 /** the inodes reached mds and don't finish */
365 unsigned long lqs_iwrite_pending;
366 /** when inodes are allocated/released, this value will record it */
367 long long lqs_ino_rec;
368 /** when blocks are allocated/released, this value will record it */
369 long long lqs_blk_rec;
370 cfs_atomic_t lqs_refcount;
371 cfs_time_t lqs_last_bshrink; /** time of last block shrink */
372 cfs_time_t lqs_last_ishrink; /** time of last inode shrink */
373 cfs_spinlock_t lqs_lock;
374 unsigned long long lqs_key; /** hash key */
375 struct lustre_quota_ctxt *lqs_ctxt; /** quota ctxt */
378 #define LQS_IS_GRP(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_GRP)
379 #define LQS_IS_ADJBLK(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_ADJBLK)
380 #define LQS_IS_ADJINO(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_ADJINO)
381 #define LQS_IS_RECOVERY(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_RECOVERY)
382 #define LQS_IS_SETQUOTA(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_SETQUOTA)
384 #define LQS_SET_GRP(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_GRP)
385 #define LQS_SET_ADJBLK(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_ADJBLK)
386 #define LQS_SET_ADJINO(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_ADJINO)
387 #define LQS_SET_RECOVERY(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_RECOVERY)
388 #define LQS_SET_SETQUOTA(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_SETQUOTA)
390 #define LQS_CLEAR_RECOVERY(lqs) ((lqs)->lqs_flags &= ~LQUOTA_FLAGS_RECOVERY)
391 #define LQS_CLEAR_SETQUOTA(lqs) ((lqs)->lqs_flags &= ~LQUOTA_FLAGS_SETQUOTA)
393 /* In the hash for lustre_qunit_size, the key is decided by
394 * grp_or_usr and uid/gid, in here, I combine these two values,
395 * which will make comparing easier and more efficient */
396 #define LQS_KEY(is_grp, id) ((is_grp ? 1ULL << 32: 0) + id)
397 #define LQS_KEY_ID(key) (key & 0xffffffff)
398 #define LQS_KEY_GRP(key) (key >> 32)
400 static inline void lqs_getref(struct lustre_qunit_size *lqs)
402 int count = cfs_atomic_inc_return(&lqs->lqs_refcount);
404 CDEBUG(D_INFO, "lqs=%p refcount %d\n", lqs, count);
407 static inline void lqs_putref(struct lustre_qunit_size *lqs)
409 int count = cfs_atomic_read(&lqs->lqs_refcount);
412 CDEBUG(D_INFO, "lqs=%p refcount %d\n", lqs, count - 1);
414 if (cfs_atomic_dec_and_test(&lqs->lqs_refcount)) {
415 if (cfs_atomic_dec_and_test(&lqs->lqs_ctxt->lqc_lqs))
416 cfs_waitq_signal(&lqs->lqs_ctxt->lqc_lqs_waitq);
423 struct lustre_quota_info {
426 struct lustre_quota_ctxt {
429 #define QUOTA_MASTER_READY(qctxt)
430 #define QUOTA_MASTER_UNREADY(qctxt)
432 #endif /* !__KERNEL__ */
436 #define LL_DQUOT_OFF(sb) do {} while(0)
438 struct lustre_quota_info {
441 struct lustre_quota_ctxt {
444 #endif /* !HAVE_QUOTA_SUPPORT */
446 /* If the (quota limit < qunit * slave count), the slave which can't
447 * acquire qunit should set it's local limit as MIN_QLIMIT */
450 struct quotacheck_thread_args {
451 struct obd_export *qta_exp; /** obd export */
452 struct obd_device *qta_obd; /** obd device */
453 struct obd_quotactl qta_oqctl; /** obd_quotactl args */
454 struct super_block *qta_sb; /** obd super block */
455 cfs_semaphore_t *qta_sem; /** obt_quotachecking */
458 struct obd_trans_info;
459 typedef int (*quota_acquire)(struct obd_device *obd, const unsigned int id[],
460 struct obd_trans_info *oti, int isblk);
463 int (*quota_init) (void);
464 int (*quota_exit) (void);
465 int (*quota_setup) (struct obd_device *);
466 int (*quota_cleanup) (struct obd_device *);
468 * For quota master, close admin quota files
470 int (*quota_fs_cleanup) (struct obd_device *);
471 int (*quota_ctl) (struct obd_device *, struct obd_export *,
472 struct obd_quotactl *);
473 int (*quota_check) (struct obd_device *, struct obd_export *,
474 struct obd_quotactl *);
475 int (*quota_recovery) (struct obd_device *);
478 * For quota master/slave, adjust quota limit after fs operation
480 int (*quota_adjust) (struct obd_device *, const unsigned int[],
481 const unsigned int[], int, int);
484 * For quota slave, set import, trigger quota recovery,
485 * For quota master, set lqc_setup
487 int (*quota_setinfo) (struct obd_device *, void *);
490 * For quota slave, clear import when relative import is invalid
492 int (*quota_clearinfo) (struct obd_export *, struct obd_device *);
495 * For quota slave, set proper thread resoure capability
497 int (*quota_enforce) (struct obd_device *, unsigned int);
500 * For quota slave, check whether specified uid/gid is over quota
502 int (*quota_getflag) (struct obd_device *, struct obdo *);
506 * For quota slave, acquire/release quota from master if needed
508 int (*quota_acquire) (struct obd_device *, const unsigned int [],
509 struct obd_trans_info *, int);
512 * For quota slave, check whether specified uid/gid's remaining quota
513 * can finish a block_write or inode_create rpc. It updates the pending
514 * record of block and inode, acquires quota if necessary
516 int (*quota_chkquota) (struct obd_device *, struct obd_export *,
517 const unsigned int [], int [],
518 int, quota_acquire, struct obd_trans_info *,
519 int, struct inode *, int);
522 * For quota client, the actions after the pending write is committed
524 int (*quota_pending_commit) (struct obd_device *, const unsigned int [],
529 * For quota client, poll if the quota check done
531 int (*quota_poll_check) (struct obd_export *, struct if_quotacheck *);
534 * For quota client, check whether specified uid/gid is over quota
536 int (*quota_chkdq) (struct client_obd *, const unsigned int []);
539 * For quota client, set over quota flag for specifed uid/gid
541 int (*quota_setdq) (struct client_obd *, const unsigned int [],
545 * For adjusting qunit size b=10600
547 int (*quota_adjust_qunit) (struct obd_export *exp,
548 struct quota_adjust_qunit *oqaq,
549 struct lustre_quota_ctxt *qctxt,
550 struct ptlrpc_request_set *rqset);
554 #define Q_COPY(out, in, member) (out)->member = (in)->member
556 #define QUOTA_OP(interface, op) interface->quota_ ## op
558 #define QUOTA_CHECK_OP(interface, op) \
562 if (!QUOTA_OP(interface, op)) { \
563 CERROR("no quota operation: " #op "\n"); \
564 RETURN(-EOPNOTSUPP); \
568 static inline int lquota_init(quota_interface_t *interface)
573 QUOTA_CHECK_OP(interface, init);
574 rc = QUOTA_OP(interface, init)();
578 static inline int lquota_exit(quota_interface_t *interface)
583 QUOTA_CHECK_OP(interface, exit);
584 rc = QUOTA_OP(interface, exit)();
588 static inline int lquota_setup(quota_interface_t *interface,
589 struct obd_device *obd)
594 QUOTA_CHECK_OP(interface, setup);
595 rc = QUOTA_OP(interface, setup)(obd);
599 static inline int lquota_cleanup(quota_interface_t *interface,
600 struct obd_device *obd)
605 QUOTA_CHECK_OP(interface, cleanup);
606 rc = QUOTA_OP(interface, cleanup)(obd);
610 static inline int lquota_fs_cleanup(quota_interface_t *interface,
611 struct obd_device *obd)
616 QUOTA_CHECK_OP(interface, fs_cleanup);
617 rc = QUOTA_OP(interface, fs_cleanup)(obd);
621 static inline int lquota_recovery(quota_interface_t *interface,
622 struct obd_device *obd)
627 QUOTA_CHECK_OP(interface, recovery);
628 rc = QUOTA_OP(interface, recovery)(obd);
632 static inline int lquota_check(quota_interface_t *interface,
633 struct obd_device *obd,
634 struct obd_export *exp,
635 struct obd_quotactl *oqctl)
640 QUOTA_CHECK_OP(interface, check);
641 rc = QUOTA_OP(interface, check)(obd, exp, oqctl);
645 static inline int lquota_ctl(quota_interface_t *interface,
646 struct obd_device *obd,
647 struct obd_quotactl *oqctl)
652 QUOTA_CHECK_OP(interface, ctl);
653 rc = QUOTA_OP(interface, ctl)(obd, NULL, oqctl);
657 static inline int lquota_adjust(quota_interface_t *interface,
658 struct obd_device *obd,
659 const unsigned int qcids[],
660 const unsigned int qpids[],
666 QUOTA_CHECK_OP(interface, adjust);
667 ret = QUOTA_OP(interface, adjust)(obd, qcids, qpids, rc, opc);
671 static inline int lquota_chkdq(quota_interface_t *interface,
672 struct client_obd *cli, const unsigned int qid[])
677 QUOTA_CHECK_OP(interface, chkdq);
678 rc = QUOTA_OP(interface, chkdq)(cli, qid);
682 static inline int lquota_setdq(quota_interface_t *interface,
683 struct client_obd *cli, const unsigned int qid[],
684 obd_flag valid, obd_flag flags)
689 QUOTA_CHECK_OP(interface, setdq);
690 rc = QUOTA_OP(interface, setdq)(cli, qid, valid, flags);
694 static inline int lquota_poll_check(quota_interface_t *interface,
695 struct obd_export *exp,
696 struct if_quotacheck *qchk)
701 QUOTA_CHECK_OP(interface, poll_check);
702 rc = QUOTA_OP(interface, poll_check)(exp, qchk);
706 static inline int lquota_setinfo(quota_interface_t *interface,
707 struct obd_device *obd,
713 QUOTA_CHECK_OP(interface, setinfo);
714 rc = QUOTA_OP(interface, setinfo)(obd, data);
718 static inline int lquota_clearinfo(quota_interface_t *interface,
719 struct obd_export *exp,
720 struct obd_device *obd)
725 QUOTA_CHECK_OP(interface, clearinfo);
726 rc = QUOTA_OP(interface, clearinfo)(exp, obd);
730 static inline int lquota_enforce(quota_interface_t *interface,
731 struct obd_device *obd,
737 QUOTA_CHECK_OP(interface, enforce);
738 rc = QUOTA_OP(interface, enforce)(obd, ignore);
742 static inline int lquota_getflag(quota_interface_t *interface,
743 struct obd_device *obd, struct obdo *oa)
748 QUOTA_CHECK_OP(interface, getflag);
749 rc = QUOTA_OP(interface, getflag)(obd, oa);
754 static inline int lquota_chkquota(quota_interface_t *interface,
755 struct obd_device *obd,
756 struct obd_export *exp,
757 const unsigned int id[], int pending[],
758 int count, struct obd_trans_info *oti,
759 int isblk, void *data, int frags)
764 QUOTA_CHECK_OP(interface, chkquota);
765 QUOTA_CHECK_OP(interface, acquire);
766 rc = QUOTA_OP(interface, chkquota)(obd, exp, id, pending, count,
767 QUOTA_OP(interface, acquire), oti,
768 isblk, (struct inode *)data, frags);
772 static inline int lquota_pending_commit(quota_interface_t *interface,
773 struct obd_device *obd,
774 const unsigned int id[],
775 int pending[], int isblk)
780 QUOTA_CHECK_OP(interface, pending_commit);
781 rc = QUOTA_OP(interface, pending_commit)(obd, id, pending, isblk);
787 extern quota_interface_t osc_quota_interface;
788 extern quota_interface_t lov_quota_interface;
789 extern quota_interface_t mdc_quota_interface;
790 extern quota_interface_t lmv_quota_interface;
806 #define LUSTRE_ADMIN_QUOTAFILES_V2 {\
807 "admin_quotafile_v2.usr", /** user admin quotafile */\
808 "admin_quotafile_v2.grp" /** group admin quotafile */\
813 #endif /* _LUSTRE_QUOTA_H */