blob: fe3e46df604b4c2ab92ddacfe093ff3f59a223cf [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott4ce31212005-11-02 14:59:41 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6#ifndef __XFS_DQUOT_H__
7#define __XFS_DQUOT_H__
8
9/*
10 * Dquots are structures that hold quota information about a user or a group,
11 * much like inodes are for files. In fact, dquots share many characteristics
12 * with inodes. However, dquots can also be a centralized resource, relative
13 * to a collection of inodes. In this respect, dquots share some characteristics
14 * of the superblock.
15 * XFS dquots exploit both those in its algorithms. They make every attempt
16 * to not be a bottleneck when quotas are on and have minimal impact, if any,
17 * when quotas are off.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct xfs_mount;
21struct xfs_trans;
22
Brian Fosterb1366452013-03-18 10:51:46 -040023enum {
24 XFS_QLOWSP_1_PCNT = 0,
25 XFS_QLOWSP_3_PCNT,
26 XFS_QLOWSP_5_PCNT,
27 XFS_QLOWSP_MAX
28};
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * The incore dquot structure
32 */
Pavel Reichlaefe69a2019-11-12 17:04:02 -080033struct xfs_dquot {
34 uint dq_flags;
35 struct list_head q_lru;
36 struct xfs_mount *q_mount;
37 uint q_nrefs;
38 xfs_daddr_t q_blkno;
39 int q_bufoffset;
40 xfs_fileoff_t q_fileoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Pavel Reichlaefe69a2019-11-12 17:04:02 -080042 struct xfs_disk_dquot q_core;
Pavel Reichlfd8b81d2019-11-12 17:04:26 -080043 struct xfs_dq_logitem q_logitem;
Pavel Reichlaefe69a2019-11-12 17:04:02 -080044 /* total regular nblks used+reserved */
45 xfs_qcnt_t q_res_bcount;
46 /* total inos allocd+reserved */
47 xfs_qcnt_t q_res_icount;
48 /* total realtime blks used+reserved */
49 xfs_qcnt_t q_res_rtbcount;
50 xfs_qcnt_t q_prealloc_lo_wmark;
51 xfs_qcnt_t q_prealloc_hi_wmark;
52 int64_t q_low_space[XFS_QLOWSP_MAX];
53 struct mutex q_qlock;
54 struct completion q_flush;
55 atomic_t q_pincount;
56 struct wait_queue_head q_pinwait;
57};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Christoph Hellwig5bb87a32009-01-19 02:03:19 +010059/*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020060 * Lock hierarchy for q_qlock:
Christoph Hellwig5bb87a32009-01-19 02:03:19 +010061 * XFS_QLOCK_NORMAL is the implicit default,
Pavel Reichlaefe69a2019-11-12 17:04:02 -080062 * XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2
Christoph Hellwig5bb87a32009-01-19 02:03:19 +010063 */
64enum {
65 XFS_QLOCK_NORMAL = 0,
66 XFS_QLOCK_NESTED,
67};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Pavel Reichlaefe69a2019-11-12 17:04:02 -080070 * Manage the q_flush completion queue embedded in the dquot. This completion
David Chinnere1f49cf2008-08-13 16:41:43 +100071 * queue synchronizes processes attempting to flush the in-core dquot back to
72 * disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Pavel Reichlaefe69a2019-11-12 17:04:02 -080074static inline void xfs_dqflock(struct xfs_dquot *dqp)
David Chinnere1f49cf2008-08-13 16:41:43 +100075{
76 wait_for_completion(&dqp->q_flush);
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Pavel Reichlaefe69a2019-11-12 17:04:02 -080079static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp)
David Chinnere1f49cf2008-08-13 16:41:43 +100080{
81 return try_wait_for_completion(&dqp->q_flush);
82}
83
Pavel Reichlaefe69a2019-11-12 17:04:02 -080084static inline void xfs_dqfunlock(struct xfs_dquot *dqp)
David Chinnere1f49cf2008-08-13 16:41:43 +100085{
86 complete(&dqp->q_flush);
87}
88
Christoph Hellwig800b4842011-12-06 21:58:14 +000089static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp)
90{
91 return mutex_trylock(&dqp->q_qlock);
92}
93
94static inline void xfs_dqlock(struct xfs_dquot *dqp)
95{
96 mutex_lock(&dqp->q_qlock);
97}
98
Christoph Hellwig5b03ff12012-02-20 02:31:22 +000099static inline void xfs_dqunlock(struct xfs_dquot *dqp)
Christoph Hellwig800b4842011-12-06 21:58:14 +0000100{
101 mutex_unlock(&dqp->q_qlock);
102}
103
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000104static inline int xfs_this_quota_on(struct xfs_mount *mp, int type)
105{
106 switch (type & XFS_DQ_ALLTYPES) {
107 case XFS_DQ_USER:
108 return XFS_IS_UQUOTA_ON(mp);
109 case XFS_DQ_GROUP:
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500110 return XFS_IS_GQUOTA_ON(mp);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000111 case XFS_DQ_PROJ:
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500112 return XFS_IS_PQUOTA_ON(mp);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000113 default:
114 return 0;
115 }
116}
117
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800118static inline struct xfs_dquot *xfs_inode_dquot(struct xfs_inode *ip, int type)
Chandra Seetharaman36731412012-01-23 17:31:30 +0000119{
120 switch (type & XFS_DQ_ALLTYPES) {
121 case XFS_DQ_USER:
122 return ip->i_udquot;
123 case XFS_DQ_GROUP:
Chandra Seetharaman36731412012-01-23 17:31:30 +0000124 return ip->i_gdquot;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500125 case XFS_DQ_PROJ:
126 return ip->i_pdquot;
Chandra Seetharaman36731412012-01-23 17:31:30 +0000127 default:
128 return NULL;
129 }
130}
131
Brian Fosterdc06f3982014-07-24 19:49:28 +1000132/*
133 * Check whether a dquot is under low free space conditions. We assume the quota
134 * is enabled and enforced.
135 */
136static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
137{
138 int64_t freesp;
139
140 freesp = be64_to_cpu(dqp->q_core.d_blk_hardlimit) - dqp->q_res_bcount;
141 if (freesp < dqp->q_low_space[XFS_QLOWSP_1_PCNT])
142 return true;
143
144 return false;
145}
146
Christoph Hellwig72018132009-02-09 08:39:24 +0100147#define XFS_DQ_IS_LOCKED(dqp) (mutex_is_locked(&((dqp)->q_qlock)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#define XFS_DQ_IS_DIRTY(dqp) ((dqp)->dq_flags & XFS_DQ_DIRTY)
149#define XFS_QM_ISUDQ(dqp) ((dqp)->dq_flags & XFS_DQ_USER)
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000150#define XFS_QM_ISPDQ(dqp) ((dqp)->dq_flags & XFS_DQ_PROJ)
151#define XFS_QM_ISGDQ(dqp) ((dqp)->dq_flags & XFS_DQ_GROUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800153void xfs_qm_dqdestroy(struct xfs_dquot *dqp);
154int xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp);
155void xfs_qm_dqunpin_wait(struct xfs_dquot *dqp);
156void xfs_qm_adjust_dqtimers(struct xfs_mount *mp,
157 struct xfs_disk_dquot *d);
158void xfs_qm_adjust_dqlimits(struct xfs_mount *mp,
159 struct xfs_dquot *d);
160xfs_dqid_t xfs_qm_id_for_quotatype(struct xfs_inode *ip, uint type);
161int xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id,
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700162 uint type, bool can_alloc,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700163 struct xfs_dquot **dqpp);
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800164int xfs_qm_dqget_inode(struct xfs_inode *ip, uint type,
165 bool can_alloc,
166 struct xfs_dquot **dqpp);
167int xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id,
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700168 uint type, struct xfs_dquot **dqpp);
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800169int xfs_qm_dqget_uncached(struct xfs_mount *mp,
170 xfs_dqid_t id, uint type,
171 struct xfs_dquot **dqpp);
172void xfs_qm_dqput(struct xfs_dquot *dqp);
Christoph Hellwig800b4842011-12-06 21:58:14 +0000173
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800174void xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800176void xfs_dquot_set_prealloc_limits(struct xfs_dquot *);
Brian Fosterb1366452013-03-18 10:51:46 -0400177
Christoph Hellwig78e55892011-12-06 21:58:22 +0000178static inline struct xfs_dquot *xfs_qm_dqhold(struct xfs_dquot *dqp)
179{
180 xfs_dqlock(dqp);
181 dqp->q_nrefs++;
182 xfs_dqunlock(dqp);
183 return dqp;
184}
185
Darrick J. Wong554ba962018-05-04 15:31:21 -0700186typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq, uint dqtype,
187 void *priv);
188int xfs_qm_dqiterate(struct xfs_mount *mp, uint dqtype,
189 xfs_qm_dqiterate_fn iter_fn, void *priv);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif /* __XFS_DQUOT_H__ */