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.
37 #ifndef _LUSTRE_QUOTA_H
38 #define _LUSTRE_QUOTA_H
40 #if defined(__linux__)
41 #include <linux/lustre_quota.h>
42 #elif defined(__APPLE__)
43 #include <darwin/lustre_quota.h>
44 #elif defined(__WINNT__)
45 #include <winnt/lustre_quota.h>
47 #error Unsupported operating system.
50 #include <lustre_net.h>
51 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <class_hash.h>
63 #ifdef HAVE_QUOTA_SUPPORT
69 LQUOTA_FIRST_STAT = 0,
70 /* these four are for measuring quota requests, for both of
71 * quota master and quota slaves */
72 LQUOTA_SYNC_ACQ = LQUOTA_FIRST_STAT,
76 /* these four measure how much time I/O threads spend on dealing
77 * with quota before and after writing data or creating files,
78 * only for quota slaves(lquota_chkquota and lquota_pending_commit) */
79 LQUOTA_WAIT_FOR_CHK_BLK,
80 LQUOTA_WAIT_FOR_CHK_INO,
81 LQUOTA_WAIT_FOR_COMMIT_BLK,
82 LQUOTA_WAIT_FOR_COMMIT_INO,
83 /* these two are for measuring time waiting return of quota reqs
84 * (qctxt_wait_pending_dqacq), only for quota salves */
85 LQUOTA_WAIT_PENDING_BLK_QUOTA,
86 LQUOTA_WAIT_PENDING_INO_QUOTA,
87 /* these two are for those when they are calling
88 * qctxt_wait_pending_dqacq, the quota req has returned already,
89 * only for quota salves */
90 LQUOTA_NOWAIT_PENDING_BLK_QUOTA,
91 LQUOTA_NOWAIT_PENDING_INO_QUOTA,
92 /* these are for quota ctl */
94 /* these are for adjust quota qunit, for both of
95 * quota master and quota slaves */
101 /* structures to access admin quotafile */
102 struct lustre_mem_dqinfo {
103 unsigned int dqi_bgrace;
104 unsigned int dqi_igrace;
105 unsigned long dqi_flags;
106 unsigned int dqi_blocks;
107 unsigned int dqi_free_blk;
108 unsigned int dqi_free_entry;
111 struct lustre_quota_info {
112 struct file *qi_files[MAXQUOTAS];
113 struct lustre_mem_dqinfo qi_info[MAXQUOTAS];
114 lustre_quota_version_t qi_version;
117 #define DQ_STATUS_AVAIL 0x0 /* Available dquot */
118 #define DQ_STATUS_SET 0x01 /* Sombody is setting dquot */
119 #define DQ_STATUS_RECOVERY 0x02 /* dquot is in recovery */
121 struct lustre_mem_dqblk {
122 __u64 dqb_bhardlimit; /* absolute limit on disk blks alloc */
123 __u64 dqb_bsoftlimit; /* preferred limit on disk blks */
124 __u64 dqb_curspace; /* current used space */
125 __u64 dqb_ihardlimit; /* absolute limit on allocated inodes */
126 __u64 dqb_isoftlimit; /* preferred inode limit */
127 __u64 dqb_curinodes; /* current # allocated inodes */
128 time_t dqb_btime; /* time limit for excessive disk use */
129 time_t dqb_itime; /* time limit for excessive inode use */
132 struct lustre_dquot {
133 /* Hash list in memory, protect by dquot_hash_lock */
134 struct list_head dq_hash;
135 /* Protect the data in lustre_dquot */
136 struct semaphore dq_sem;
139 /* Pointer of quota info it belongs to */
140 struct lustre_quota_info *dq_info;
142 loff_t dq_off; /* Offset of dquot on disk */
143 unsigned int dq_id; /* ID this applies to (uid, gid) */
144 int dq_type; /* Type fo quota (USRQUOTA, GRPQUOUTA) */
145 unsigned short dq_status; /* See DQ_STATUS_ */
146 unsigned long dq_flags; /* See DQ_ in quota.h */
147 struct lustre_mem_dqblk dq_dqb; /* Diskquota usage */
151 struct list_head di_link;
155 /* set inode quota limitation on a quota uid/gid */
156 #define QI_SET (1 << 30)
157 /* set block quota limitation on a quota uid/gid */
158 #define QB_SET (1 << 31)
161 #define QFILE_RD_INFO 2
162 #define QFILE_WR_INFO 3
163 #define QFILE_INIT_INFO 4
164 #define QFILE_RD_DQUOT 5
165 #define QFILE_WR_DQUOT 6
166 #define QFILE_CONVERT 7
168 /* admin quotafile operations */
169 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type);
170 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type);
171 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type);
172 int lustre_read_dquot(struct lustre_dquot *dquot);
173 int lustre_commit_dquot(struct lustre_dquot *dquot);
174 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type);
175 int lustre_get_qids(struct file *file, struct inode *inode, int type,
176 struct list_head *list);
177 int lustre_quota_convert(struct lustre_quota_info *lqi, int type);
179 #define LL_DQUOT_OFF(sb) DQUOT_OFF(sb)
181 typedef int (*dqacq_handler_t) (struct obd_device * obd, struct qunit_data * qd,
184 /* user quota is turned on on filter */
185 #define LQC_USRQUOTA_FLAG (1 << 0)
186 /* group quota is turned on on filter */
187 #define LQC_GRPQUOTA_FLAG (1 << 1)
189 #define UGQUOTA2LQC(id) ((Q_TYPEMATCH(id, USRQUOTA) ? LQC_USRQUOTA_FLAG : 0) | \
190 (Q_TYPEMATCH(id, GRPQUOTA) ? LQC_GRPQUOTA_FLAG : 0))
192 struct lustre_quota_ctxt {
193 struct super_block *lqc_sb; /* superblock this applies to */
194 struct obd_import *lqc_import; /* import used to send dqacq/dqrel RPC */
195 dqacq_handler_t lqc_handler; /* dqacq/dqrel RPC handler, only for quota master */
196 unsigned long lqc_flags; /* quota flags */
197 unsigned long lqc_recovery:1, /* Doing recovery */
198 lqc_switch_qs:1, /* the function of change qunit size
200 lqc_valid:1, /* this qctxt is valid or not */
201 lqc_setup:1; /* tell whether of not quota_type has
202 * been processed, so that the master
203 * knows when it can start processing
204 * incoming acq/rel quota requests */
205 unsigned long lqc_iunit_sz; /* original unit size of file quota and
206 * upper limitation for adjust file
208 unsigned long lqc_itune_sz; /* Trigger dqacq when available file
209 * quota less than this value, trigger
210 * dqrel when available file quota
211 * more than this value + 1 iunit */
212 unsigned long lqc_bunit_sz; /* original unit size of block quota and
213 * upper limitation for adjust block
215 unsigned long lqc_btune_sz; /* See comment of lqc_itune_sz */
216 struct lustre_hash *lqc_lqs_hash; /* all lustre_qunit_size structures */
218 /* the values below are relative to how master change its qunit sizes */
219 unsigned long lqc_cqs_boundary_factor; /* this affects the boundary of
220 * shrinking and enlarging qunit
222 unsigned long lqc_cqs_least_bunit; /* the least value of block qunit */
223 unsigned long lqc_cqs_least_iunit; /* the least value of inode qunit */
224 unsigned long lqc_cqs_qs_factor; /* when enlarging, qunit size will
225 * mutilple it; when shrinking,
226 * qunit size will divide it */
227 int lqc_switch_seconds; /* avoid ping-pong effect of
228 * adjusting qunit size. How many
229 * seconds must be waited between
230 * enlarging and shinking qunit */
231 int lqc_sync_blk; /* when blk qunit reaches this value,
232 * later write reqs from client
233 * should be sync b=16642 */
234 spinlock_t lqc_lock; /* guard lqc_imp_valid now */
235 cfs_waitq_t lqc_wait_for_qmaster; /* when mds isn't connected, threads
236 * on osts who send the quota reqs
237 * with wait==1 will be put here
239 struct proc_dir_entry *lqc_proc_dir;
240 struct lprocfs_stats *lqc_stats; /* lquota statistics */
242 atomic_t lqc_lqs; /* the number of used hashed lqs */
243 cfs_waitq_t lqc_lqs_waitq; /* no lqs are in use */
246 #define QUOTA_MASTER_READY(qctxt) (qctxt)->lqc_setup = 1
247 #define QUOTA_MASTER_UNREADY(qctxt) (qctxt)->lqc_setup = 0
249 struct lustre_qunit_size {
250 struct hlist_node lqs_hash; /* the hash entry */
251 unsigned int lqs_id; /* id of user/group */
252 unsigned long lqs_flags; /* 31st bit is QB_SET, 30th bit is QI_SET
253 * other bits are same as LQUOTA_FLAGS_* */
254 unsigned long lqs_iunit_sz; /* Unit size of file quota currently */
255 unsigned long lqs_itune_sz; /* Trigger dqacq when available file quota
256 * less than this value, trigger dqrel
257 * when more than this value + 1 iunit */
258 unsigned long lqs_bunit_sz; /* Unit size of block quota currently */
259 unsigned long lqs_btune_sz; /* See comment of lqs itune sz */
260 unsigned long lqs_bwrite_pending; /* the blocks reached ost and don't
262 unsigned long lqs_iwrite_pending; /* the inodes reached mds and don't
264 long long lqs_ino_rec; /* when inodes are allocated/released,
265 * this value will record it */
266 long long lqs_blk_rec; /* when blocks are allocated/released,
267 * this value will record it */
268 atomic_t lqs_refcount;
269 cfs_time_t lqs_last_bshrink; /* time of last block shrink */
270 cfs_time_t lqs_last_ishrink; /* time of last inode shrink */
272 unsigned long long lqs_key; /* hash key */
273 struct lustre_quota_ctxt *lqs_ctxt; /* quota ctxt */
276 #define LQS_IS_GRP(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_GRP)
277 #define LQS_IS_ADJBLK(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_ADJBLK)
278 #define LQS_IS_ADJINO(lqs) ((lqs)->lqs_flags & LQUOTA_FLAGS_ADJINO)
280 #define LQS_SET_GRP(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_GRP)
281 #define LQS_SET_ADJBLK(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_ADJBLK)
282 #define LQS_SET_ADJINO(lqs) ((lqs)->lqs_flags |= LQUOTA_FLAGS_ADJINO)
284 /* In the hash for lustre_qunit_size, the key is decided by
285 * grp_or_usr and uid/gid, in here, I combine these two values,
286 * which will make comparing easier and more efficient */
287 #define LQS_KEY(is_grp, id) ((is_grp ? 1ULL << 32: 0) + id)
288 #define LQS_KEY_ID(key) (key & 0xffffffff)
289 #define LQS_KEY_GRP(key) (key >> 32)
291 static inline void lqs_getref(struct lustre_qunit_size *lqs)
293 if (atomic_inc_return(&lqs->lqs_refcount) == 2) /* quota_create_lqs */
294 atomic_inc(&lqs->lqs_ctxt->lqc_lqs);
296 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
297 lqs, atomic_read(&lqs->lqs_refcount));
300 static inline void lqs_putref(struct lustre_qunit_size *lqs)
302 LASSERT(atomic_read(&lqs->lqs_refcount) > 0);
304 /* killing last ref, let's let hash table kill it */
305 if (atomic_read(&lqs->lqs_refcount) == 1) {
306 lustre_hash_del(lqs->lqs_ctxt->lqc_lqs_hash,
307 &lqs->lqs_key, &lqs->lqs_hash);
310 if (atomic_dec_return(&lqs->lqs_refcount) == 1)
311 if (atomic_dec_and_test(&lqs->lqs_ctxt->lqc_lqs))
312 cfs_waitq_signal(&lqs->lqs_ctxt->lqc_lqs_waitq);
313 CDEBUG(D_QUOTA, "lqs=%p refcount %d\n",
314 lqs, atomic_read(&lqs->lqs_refcount));
319 static inline void lqs_initref(struct lustre_qunit_size *lqs)
321 atomic_set(&lqs->lqs_refcount, 0);
326 struct lustre_quota_info {
329 struct lustre_quota_ctxt {
332 #endif /* !__KERNEL__ */
336 #define LL_DQUOT_OFF(sb) do {} while(0)
338 struct lustre_quota_info {
341 struct lustre_quota_ctxt {
344 #define QUOTA_MASTER_READY(qctxt)
345 #define QUOTA_MASTER_UNREADY(qctxt)
347 #endif /* !HAVE_QUOTA_SUPPORT */
349 /* If the (quota limit < qunit * slave count), the slave which can't
350 * acquire qunit should set it's local limit as MIN_QLIMIT */
353 struct quotacheck_thread_args {
354 struct obd_export *qta_exp; /* obd export */
355 struct obd_quotactl qta_oqctl; /* obd_quotactl args */
356 struct super_block *qta_sb; /* obd super block */
357 atomic_t *qta_sem; /* obt_quotachecking */
360 struct obd_trans_info;
361 typedef int (*quota_acquire)(struct obd_device *obd, unsigned int uid,
362 unsigned int gid, struct obd_trans_info *oti);
365 int (*quota_init) (void);
366 int (*quota_exit) (void);
367 int (*quota_setup) (struct obd_device *);
368 int (*quota_cleanup) (struct obd_device *);
369 /* For quota master, close admin quota files */
370 int (*quota_fs_cleanup) (struct obd_device *);
371 int (*quota_ctl) (struct obd_export *, struct obd_quotactl *);
372 int (*quota_check) (struct obd_export *, struct obd_quotactl *);
373 int (*quota_recovery) (struct obd_device *);
375 /* For quota master/slave, adjust quota limit after fs operation */
376 int (*quota_adjust) (struct obd_device *, unsigned int[],
377 unsigned int[], int, int);
379 /* For quota slave, set import, trigger quota recovery */
380 int (*quota_setinfo) (struct obd_export *, struct obd_device *);
382 /* For quota slave, clear import when relative import is invalid */
383 int (*quota_clearinfo) (struct obd_export *, struct obd_device *);
385 /* For quota slave, set proper thread resoure capability */
386 int (*quota_enforce) (struct obd_device *, unsigned int);
388 /* For quota slave, check whether specified uid/gid is over quota */
389 int (*quota_getflag) (struct obd_device *, struct obdo *);
391 /* For quota slave, acquire/release quota from master if needed */
392 int (*quota_acquire) (struct obd_device *, unsigned int, unsigned int,
393 struct obd_trans_info *);
395 /* For quota slave, check whether specified uid/gid's remaining quota
396 * can finish a block_write or inode_create rpc. It updates the pending
397 * record of block and inode, acquires quota if necessary */
398 int (*quota_chkquota) (struct obd_device *, unsigned int, unsigned int,
399 int, int *, quota_acquire,
400 struct obd_trans_info *, struct inode *, int);
402 /* For quota client, the actions after the pending write is committed */
403 int (*quota_pending_commit) (struct obd_device *, unsigned int,
406 /* For quota client, poll if the quota check done */
407 int (*quota_poll_check) (struct obd_export *, struct if_quotacheck *);
409 /* For quota client, check whether specified uid/gid is over quota */
410 int (*quota_chkdq) (struct client_obd *, unsigned int, unsigned int);
412 /* For quota client, set over quota flag for specifed uid/gid */
413 int (*quota_setdq) (struct client_obd *, unsigned int, unsigned int,
416 /* For adjusting qunit size b=10600 */
417 int (*quota_adjust_qunit) (struct obd_export *exp,
418 struct quota_adjust_qunit *oqaq,
419 struct lustre_quota_ctxt *qctxt);
423 #define Q_COPY(out, in, member) (out)->member = (in)->member
425 #define QUOTA_OP(interface, op) interface->quota_ ## op
427 #define QUOTA_CHECK_OP(interface, op) \
431 if (!QUOTA_OP(interface, op)) { \
432 CERROR("no quota operation: " #op "\n"); \
433 RETURN(-EOPNOTSUPP); \
437 static inline int lquota_init(quota_interface_t *interface)
442 QUOTA_CHECK_OP(interface, init);
443 rc = QUOTA_OP(interface, init)();
447 static inline int lquota_exit(quota_interface_t *interface)
452 QUOTA_CHECK_OP(interface, exit);
453 rc = QUOTA_OP(interface, exit)();
457 static inline int lquota_setup(quota_interface_t *interface,
458 struct obd_device *obd)
463 QUOTA_CHECK_OP(interface, setup);
464 rc = QUOTA_OP(interface, setup)(obd);
468 static inline int lquota_cleanup(quota_interface_t *interface,
469 struct obd_device *obd)
474 QUOTA_CHECK_OP(interface, cleanup);
475 rc = QUOTA_OP(interface, cleanup)(obd);
479 static inline int lquota_fs_cleanup(quota_interface_t *interface,
480 struct obd_device *obd)
485 QUOTA_CHECK_OP(interface, fs_cleanup);
486 rc = QUOTA_OP(interface, fs_cleanup)(obd);
490 static inline int lquota_recovery(quota_interface_t *interface,
491 struct obd_device *obd)
496 QUOTA_CHECK_OP(interface, recovery);
497 rc = QUOTA_OP(interface, recovery)(obd);
501 static inline int lquota_adjust(quota_interface_t *interface,
502 struct obd_device *obd,
503 unsigned int qcids[],
504 unsigned int qpids[],
510 QUOTA_CHECK_OP(interface, adjust);
511 ret = QUOTA_OP(interface, adjust)(obd, qcids, qpids, rc, opc);
515 static inline int lquota_chkdq(quota_interface_t *interface,
516 struct client_obd *cli,
517 unsigned int uid, unsigned int gid)
522 QUOTA_CHECK_OP(interface, chkdq);
523 rc = QUOTA_OP(interface, chkdq)(cli, uid, gid);
527 static inline int lquota_setdq(quota_interface_t *interface,
528 struct client_obd *cli,
529 unsigned int uid, unsigned int gid,
530 obd_flag valid, obd_flag flags)
535 QUOTA_CHECK_OP(interface, setdq);
536 rc = QUOTA_OP(interface, setdq)(cli, uid, gid, valid, flags);
540 static inline int lquota_poll_check(quota_interface_t *interface,
541 struct obd_export *exp,
542 struct if_quotacheck *qchk)
547 QUOTA_CHECK_OP(interface, poll_check);
548 rc = QUOTA_OP(interface, poll_check)(exp, qchk);
552 static inline int lquota_setinfo(quota_interface_t *interface,
553 struct obd_export *exp,
554 struct obd_device *obd)
559 QUOTA_CHECK_OP(interface, setinfo);
560 rc = QUOTA_OP(interface, setinfo)(exp, obd);
564 static inline int lquota_clearinfo(quota_interface_t *interface,
565 struct obd_export *exp,
566 struct obd_device *obd)
571 QUOTA_CHECK_OP(interface, clearinfo);
572 rc = QUOTA_OP(interface, clearinfo)(exp, obd);
576 static inline int lquota_enforce(quota_interface_t *interface,
577 struct obd_device *obd,
583 QUOTA_CHECK_OP(interface, enforce);
584 rc = QUOTA_OP(interface, enforce)(obd, ignore);
588 static inline int lquota_getflag(quota_interface_t *interface,
589 struct obd_device *obd, struct obdo *oa)
594 QUOTA_CHECK_OP(interface, getflag);
595 rc = QUOTA_OP(interface, getflag)(obd, oa);
600 static inline int lquota_acquire(quota_interface_t *interface,
601 struct obd_device *obd,
602 unsigned int uid, unsigned int gid,
603 struct obd_trans_info *oti)
608 QUOTA_CHECK_OP(interface, acquire);
609 rc = QUOTA_OP(interface, acquire)(obd, uid, gid, oti);
613 static inline int lquota_chkquota(quota_interface_t *interface,
614 struct obd_device *obd,
615 unsigned int uid, unsigned int gid, int count,
616 int *flag, struct obd_trans_info *oti,
617 struct inode *inode, int frags)
622 QUOTA_CHECK_OP(interface, chkquota);
623 QUOTA_CHECK_OP(interface, acquire);
624 rc = QUOTA_OP(interface, chkquota)(obd, uid, gid, count, flag,
625 QUOTA_OP(interface, acquire), oti,
630 static inline int lquota_pending_commit(quota_interface_t *interface,
631 struct obd_device *obd,
632 unsigned int uid, unsigned int gid,
638 QUOTA_CHECK_OP(interface, pending_commit);
639 rc = QUOTA_OP(interface, pending_commit)(obd, uid, gid, pending);
645 extern quota_interface_t osc_quota_interface;
646 extern quota_interface_t mdc_quota_interface;
647 extern quota_interface_t lov_quota_interface;
663 #define LUSTRE_ADMIN_QUOTAFILES_V1 {\
664 "admin_quotafile.usr", /* user admin quotafile */\
665 "admin_quotafile.grp" /* group admin quotafile */\
668 #define LUSTRE_ADMIN_QUOTAFILES_V2 {\
669 "admin_quotafile_v2.usr", /* user admin quotafile */\
670 "admin_quotafile_v2.grp" /* group admin quotafile */\
673 #endif /* _LUSTRE_QUOTA_H */