Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct xfs_mount; |
| 21 | struct xfs_trans; |
| 22 | |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 23 | enum { |
| 24 | XFS_QLOWSP_1_PCNT = 0, |
| 25 | XFS_QLOWSP_3_PCNT, |
| 26 | XFS_QLOWSP_5_PCNT, |
| 27 | XFS_QLOWSP_MAX |
| 28 | }; |
| 29 | |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 30 | struct xfs_dquot_res { |
| 31 | /* Total resources allocated and reserved. */ |
| 32 | xfs_qcnt_t reserved; |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 33 | |
Darrick J. Wong | be37d40 | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 34 | /* Total resources allocated. */ |
| 35 | xfs_qcnt_t count; |
| 36 | |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 37 | /* Absolute and preferred limits. */ |
| 38 | xfs_qcnt_t hardlimit; |
| 39 | xfs_qcnt_t softlimit; |
Darrick J. Wong | c8c45fb | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
Darrick J. Wong | 19dce7e | 2020-07-14 10:37:32 -0700 | [diff] [blame] | 42 | * For root dquots, this is the default grace period, in seconds. |
| 43 | * Otherwise, this is when the quota grace period expires, |
| 44 | * in seconds since the Unix epoch. |
| 45 | */ |
| 46 | time64_t timer; |
| 47 | |
| 48 | /* |
Darrick J. Wong | c8c45fb | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 49 | * For root dquots, this is the maximum number of warnings that will |
| 50 | * be issued for this quota type. Otherwise, this is the number of |
| 51 | * warnings issued against this quota. Note that none of this is |
| 52 | * implemented. |
| 53 | */ |
| 54 | xfs_qwarncnt_t warnings; |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * The incore dquot structure |
| 59 | */ |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 60 | struct xfs_dquot { |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 61 | struct list_head q_lru; |
| 62 | struct xfs_mount *q_mount; |
Darrick J. Wong | 985a78f | 2020-07-14 10:37:13 -0700 | [diff] [blame] | 63 | uint8_t dq_flags; |
| 64 | uint16_t q_flags; |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 65 | xfs_dqid_t q_id; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 66 | uint q_nrefs; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 67 | int q_bufoffset; |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 68 | xfs_daddr_t q_blkno; |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 69 | xfs_fileoff_t q_fileoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 71 | struct xfs_dquot_res q_blk; /* regular blocks */ |
| 72 | struct xfs_dquot_res q_ino; /* inodes */ |
| 73 | struct xfs_dquot_res q_rtb; /* realtime blocks */ |
| 74 | |
Pavel Reichl | fd8b81d | 2019-11-12 17:04:26 -0800 | [diff] [blame] | 75 | struct xfs_dq_logitem q_logitem; |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 76 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 77 | xfs_qcnt_t q_prealloc_lo_wmark; |
| 78 | xfs_qcnt_t q_prealloc_hi_wmark; |
| 79 | int64_t q_low_space[XFS_QLOWSP_MAX]; |
| 80 | struct mutex q_qlock; |
| 81 | struct completion q_flush; |
| 82 | atomic_t q_pincount; |
| 83 | struct wait_queue_head q_pinwait; |
| 84 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 86 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 87 | * Lock hierarchy for q_qlock: |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 88 | * XFS_QLOCK_NORMAL is the implicit default, |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 89 | * XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2 |
Christoph Hellwig | 5bb87a3 | 2009-01-19 02:03:19 +0100 | [diff] [blame] | 90 | */ |
| 91 | enum { |
| 92 | XFS_QLOCK_NORMAL = 0, |
| 93 | XFS_QLOCK_NESTED, |
| 94 | }; |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 97 | * Manage the q_flush completion queue embedded in the dquot. This completion |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 98 | * queue synchronizes processes attempting to flush the in-core dquot back to |
| 99 | * disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 101 | static inline void xfs_dqflock(struct xfs_dquot *dqp) |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 102 | { |
| 103 | wait_for_completion(&dqp->q_flush); |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 106 | static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp) |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 107 | { |
| 108 | return try_wait_for_completion(&dqp->q_flush); |
| 109 | } |
| 110 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 111 | static inline void xfs_dqfunlock(struct xfs_dquot *dqp) |
David Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 112 | { |
| 113 | complete(&dqp->q_flush); |
| 114 | } |
| 115 | |
Christoph Hellwig | 800b484 | 2011-12-06 21:58:14 +0000 | [diff] [blame] | 116 | static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp) |
| 117 | { |
| 118 | return mutex_trylock(&dqp->q_qlock); |
| 119 | } |
| 120 | |
| 121 | static inline void xfs_dqlock(struct xfs_dquot *dqp) |
| 122 | { |
| 123 | mutex_lock(&dqp->q_qlock); |
| 124 | } |
| 125 | |
Christoph Hellwig | 5b03ff1 | 2012-02-20 02:31:22 +0000 | [diff] [blame] | 126 | static inline void xfs_dqunlock(struct xfs_dquot *dqp) |
Christoph Hellwig | 800b484 | 2011-12-06 21:58:14 +0000 | [diff] [blame] | 127 | { |
| 128 | mutex_unlock(&dqp->q_qlock); |
| 129 | } |
| 130 | |
Darrick J. Wong | 00a342e | 2020-07-15 17:47:13 -0700 | [diff] [blame] | 131 | static inline int |
| 132 | xfs_dquot_type(const struct xfs_dquot *dqp) |
| 133 | { |
| 134 | return dqp->dq_flags & XFS_DQTYPE_REC_MASK; |
| 135 | } |
| 136 | |
Chandra Seetharaman | 6967b96 | 2012-01-23 17:31:25 +0000 | [diff] [blame] | 137 | static inline int xfs_this_quota_on(struct xfs_mount *mp, int type) |
| 138 | { |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 139 | switch (type & XFS_DQTYPE_REC_MASK) { |
| 140 | case XFS_DQTYPE_USER: |
Chandra Seetharaman | 6967b96 | 2012-01-23 17:31:25 +0000 | [diff] [blame] | 141 | return XFS_IS_UQUOTA_ON(mp); |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 142 | case XFS_DQTYPE_GROUP: |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 143 | return XFS_IS_GQUOTA_ON(mp); |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 144 | case XFS_DQTYPE_PROJ: |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 145 | return XFS_IS_PQUOTA_ON(mp); |
Chandra Seetharaman | 6967b96 | 2012-01-23 17:31:25 +0000 | [diff] [blame] | 146 | default: |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 151 | static inline struct xfs_dquot *xfs_inode_dquot(struct xfs_inode *ip, int type) |
Chandra Seetharaman | 3673141 | 2012-01-23 17:31:30 +0000 | [diff] [blame] | 152 | { |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 153 | switch (type & XFS_DQTYPE_REC_MASK) { |
| 154 | case XFS_DQTYPE_USER: |
Chandra Seetharaman | 3673141 | 2012-01-23 17:31:30 +0000 | [diff] [blame] | 155 | return ip->i_udquot; |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 156 | case XFS_DQTYPE_GROUP: |
Chandra Seetharaman | 3673141 | 2012-01-23 17:31:30 +0000 | [diff] [blame] | 157 | return ip->i_gdquot; |
Darrick J. Wong | 8cd4901 | 2020-07-15 17:42:36 -0700 | [diff] [blame] | 158 | case XFS_DQTYPE_PROJ: |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 159 | return ip->i_pdquot; |
Chandra Seetharaman | 3673141 | 2012-01-23 17:31:30 +0000 | [diff] [blame] | 160 | default: |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
Darrick J. Wong | dbcbc7b | 2020-07-15 17:48:31 -0700 | [diff] [blame] | 165 | /* Decide if the dquot's limits are actually being enforced. */ |
| 166 | static inline bool |
| 167 | xfs_dquot_is_enforced( |
| 168 | const struct xfs_dquot *dqp) |
| 169 | { |
Darrick J. Wong | 0b04dd5 | 2020-07-15 17:51:47 -0700 | [diff] [blame^] | 170 | switch (xfs_dquot_type(dqp)) { |
Darrick J. Wong | dbcbc7b | 2020-07-15 17:48:31 -0700 | [diff] [blame] | 171 | case XFS_DQTYPE_USER: |
| 172 | return XFS_IS_UQUOTA_ENFORCED(dqp->q_mount); |
| 173 | case XFS_DQTYPE_GROUP: |
| 174 | return XFS_IS_GQUOTA_ENFORCED(dqp->q_mount); |
| 175 | case XFS_DQTYPE_PROJ: |
| 176 | return XFS_IS_PQUOTA_ENFORCED(dqp->q_mount); |
| 177 | } |
| 178 | ASSERT(0); |
| 179 | return false; |
| 180 | } |
| 181 | |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 182 | /* |
| 183 | * Check whether a dquot is under low free space conditions. We assume the quota |
| 184 | * is enabled and enforced. |
| 185 | */ |
| 186 | static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp) |
| 187 | { |
| 188 | int64_t freesp; |
| 189 | |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 190 | freesp = dqp->q_blk.hardlimit - dqp->q_blk.reserved; |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 191 | if (freesp < dqp->q_low_space[XFS_QLOWSP_1_PCNT]) |
| 192 | return true; |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
Darrick J. Wong | 0b0fa1d | 2020-07-14 10:37:22 -0700 | [diff] [blame] | 197 | void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp); |
| 198 | |
Christoph Hellwig | 7201813 | 2009-02-09 08:39:24 +0100 | [diff] [blame] | 199 | #define XFS_DQ_IS_LOCKED(dqp) (mutex_is_locked(&((dqp)->q_qlock))) |
Darrick J. Wong | 985a78f | 2020-07-14 10:37:13 -0700 | [diff] [blame] | 200 | #define XFS_DQ_IS_DIRTY(dqp) ((dqp)->q_flags & XFS_DQFLAG_DIRTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 202 | void xfs_qm_dqdestroy(struct xfs_dquot *dqp); |
| 203 | int xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp); |
| 204 | void xfs_qm_dqunpin_wait(struct xfs_dquot *dqp); |
Darrick J. Wong | c8c753e | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 205 | void xfs_qm_adjust_dqtimers(struct xfs_dquot *d); |
| 206 | void xfs_qm_adjust_dqlimits(struct xfs_dquot *d); |
| 207 | xfs_dqid_t xfs_qm_id_for_quotatype(struct xfs_inode *ip, |
| 208 | uint type); |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 209 | int xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id, |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 210 | uint type, bool can_alloc, |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 211 | struct xfs_dquot **dqpp); |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 212 | int xfs_qm_dqget_inode(struct xfs_inode *ip, uint type, |
| 213 | bool can_alloc, |
| 214 | struct xfs_dquot **dqpp); |
| 215 | int xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id, |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 216 | uint type, struct xfs_dquot **dqpp); |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 217 | int xfs_qm_dqget_uncached(struct xfs_mount *mp, |
| 218 | xfs_dqid_t id, uint type, |
| 219 | struct xfs_dquot **dqpp); |
| 220 | void xfs_qm_dqput(struct xfs_dquot *dqp); |
Christoph Hellwig | 800b484 | 2011-12-06 21:58:14 +0000 | [diff] [blame] | 221 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 222 | void xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 224 | void xfs_dquot_set_prealloc_limits(struct xfs_dquot *); |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 225 | |
Christoph Hellwig | 78e5589 | 2011-12-06 21:58:22 +0000 | [diff] [blame] | 226 | static inline struct xfs_dquot *xfs_qm_dqhold(struct xfs_dquot *dqp) |
| 227 | { |
| 228 | xfs_dqlock(dqp); |
| 229 | dqp->q_nrefs++; |
| 230 | xfs_dqunlock(dqp); |
| 231 | return dqp; |
| 232 | } |
| 233 | |
Darrick J. Wong | 554ba96 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 234 | typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq, uint dqtype, |
| 235 | void *priv); |
| 236 | int xfs_qm_dqiterate(struct xfs_mount *mp, uint dqtype, |
| 237 | xfs_qm_dqiterate_fn iter_fn, void *priv); |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | #endif /* __XFS_DQUOT_H__ */ |