1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #ifndef __XFS_DQUOT_H__
7*4882a593Smuzhiyun #define __XFS_DQUOT_H__
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun * Dquots are structures that hold quota information about a user or a group,
11*4882a593Smuzhiyun * much like inodes are for files. In fact, dquots share many characteristics
12*4882a593Smuzhiyun * with inodes. However, dquots can also be a centralized resource, relative
13*4882a593Smuzhiyun * to a collection of inodes. In this respect, dquots share some characteristics
14*4882a593Smuzhiyun * of the superblock.
15*4882a593Smuzhiyun * XFS dquots exploit both those in its algorithms. They make every attempt
16*4882a593Smuzhiyun * to not be a bottleneck when quotas are on and have minimal impact, if any,
17*4882a593Smuzhiyun * when quotas are off.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct xfs_mount;
21*4882a593Smuzhiyun struct xfs_trans;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun enum {
24*4882a593Smuzhiyun XFS_QLOWSP_1_PCNT = 0,
25*4882a593Smuzhiyun XFS_QLOWSP_3_PCNT,
26*4882a593Smuzhiyun XFS_QLOWSP_5_PCNT,
27*4882a593Smuzhiyun XFS_QLOWSP_MAX
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun struct xfs_dquot_res {
31*4882a593Smuzhiyun /* Total resources allocated and reserved. */
32*4882a593Smuzhiyun xfs_qcnt_t reserved;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* Total resources allocated. */
35*4882a593Smuzhiyun xfs_qcnt_t count;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Absolute and preferred limits. */
38*4882a593Smuzhiyun xfs_qcnt_t hardlimit;
39*4882a593Smuzhiyun xfs_qcnt_t softlimit;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * For root dquots, this is the default grace period, in seconds.
43*4882a593Smuzhiyun * Otherwise, this is when the quota grace period expires,
44*4882a593Smuzhiyun * in seconds since the Unix epoch.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun time64_t timer;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * For root dquots, this is the maximum number of warnings that will
50*4882a593Smuzhiyun * be issued for this quota type. Otherwise, this is the number of
51*4882a593Smuzhiyun * warnings issued against this quota. Note that none of this is
52*4882a593Smuzhiyun * implemented.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun xfs_qwarncnt_t warnings;
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * The incore dquot structure
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun struct xfs_dquot {
61*4882a593Smuzhiyun struct list_head q_lru;
62*4882a593Smuzhiyun struct xfs_mount *q_mount;
63*4882a593Smuzhiyun xfs_dqtype_t q_type;
64*4882a593Smuzhiyun uint16_t q_flags;
65*4882a593Smuzhiyun xfs_dqid_t q_id;
66*4882a593Smuzhiyun uint q_nrefs;
67*4882a593Smuzhiyun int q_bufoffset;
68*4882a593Smuzhiyun xfs_daddr_t q_blkno;
69*4882a593Smuzhiyun xfs_fileoff_t q_fileoffset;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun struct xfs_dquot_res q_blk; /* regular blocks */
72*4882a593Smuzhiyun struct xfs_dquot_res q_ino; /* inodes */
73*4882a593Smuzhiyun struct xfs_dquot_res q_rtb; /* realtime blocks */
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct xfs_dq_logitem q_logitem;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun xfs_qcnt_t q_prealloc_lo_wmark;
78*4882a593Smuzhiyun xfs_qcnt_t q_prealloc_hi_wmark;
79*4882a593Smuzhiyun int64_t q_low_space[XFS_QLOWSP_MAX];
80*4882a593Smuzhiyun struct mutex q_qlock;
81*4882a593Smuzhiyun struct completion q_flush;
82*4882a593Smuzhiyun atomic_t q_pincount;
83*4882a593Smuzhiyun struct wait_queue_head q_pinwait;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * Lock hierarchy for q_qlock:
88*4882a593Smuzhiyun * XFS_QLOCK_NORMAL is the implicit default,
89*4882a593Smuzhiyun * XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2
90*4882a593Smuzhiyun */
91*4882a593Smuzhiyun enum {
92*4882a593Smuzhiyun XFS_QLOCK_NORMAL = 0,
93*4882a593Smuzhiyun XFS_QLOCK_NESTED,
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * Manage the q_flush completion queue embedded in the dquot. This completion
98*4882a593Smuzhiyun * queue synchronizes processes attempting to flush the in-core dquot back to
99*4882a593Smuzhiyun * disk.
100*4882a593Smuzhiyun */
xfs_dqflock(struct xfs_dquot * dqp)101*4882a593Smuzhiyun static inline void xfs_dqflock(struct xfs_dquot *dqp)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun wait_for_completion(&dqp->q_flush);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
xfs_dqflock_nowait(struct xfs_dquot * dqp)106*4882a593Smuzhiyun static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun return try_wait_for_completion(&dqp->q_flush);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
xfs_dqfunlock(struct xfs_dquot * dqp)111*4882a593Smuzhiyun static inline void xfs_dqfunlock(struct xfs_dquot *dqp)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun complete(&dqp->q_flush);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
xfs_dqlock_nowait(struct xfs_dquot * dqp)116*4882a593Smuzhiyun static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun return mutex_trylock(&dqp->q_qlock);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
xfs_dqlock(struct xfs_dquot * dqp)121*4882a593Smuzhiyun static inline void xfs_dqlock(struct xfs_dquot *dqp)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun mutex_lock(&dqp->q_qlock);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
xfs_dqunlock(struct xfs_dquot * dqp)126*4882a593Smuzhiyun static inline void xfs_dqunlock(struct xfs_dquot *dqp)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun mutex_unlock(&dqp->q_qlock);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun static inline int
xfs_dquot_type(const struct xfs_dquot * dqp)132*4882a593Smuzhiyun xfs_dquot_type(const struct xfs_dquot *dqp)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun return dqp->q_type & XFS_DQTYPE_REC_MASK;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
xfs_this_quota_on(struct xfs_mount * mp,xfs_dqtype_t type)137*4882a593Smuzhiyun static inline int xfs_this_quota_on(struct xfs_mount *mp, xfs_dqtype_t type)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun switch (type) {
140*4882a593Smuzhiyun case XFS_DQTYPE_USER:
141*4882a593Smuzhiyun return XFS_IS_UQUOTA_ON(mp);
142*4882a593Smuzhiyun case XFS_DQTYPE_GROUP:
143*4882a593Smuzhiyun return XFS_IS_GQUOTA_ON(mp);
144*4882a593Smuzhiyun case XFS_DQTYPE_PROJ:
145*4882a593Smuzhiyun return XFS_IS_PQUOTA_ON(mp);
146*4882a593Smuzhiyun default:
147*4882a593Smuzhiyun return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
xfs_inode_dquot(struct xfs_inode * ip,xfs_dqtype_t type)151*4882a593Smuzhiyun static inline struct xfs_dquot *xfs_inode_dquot(
152*4882a593Smuzhiyun struct xfs_inode *ip,
153*4882a593Smuzhiyun xfs_dqtype_t type)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun switch (type) {
156*4882a593Smuzhiyun case XFS_DQTYPE_USER:
157*4882a593Smuzhiyun return ip->i_udquot;
158*4882a593Smuzhiyun case XFS_DQTYPE_GROUP:
159*4882a593Smuzhiyun return ip->i_gdquot;
160*4882a593Smuzhiyun case XFS_DQTYPE_PROJ:
161*4882a593Smuzhiyun return ip->i_pdquot;
162*4882a593Smuzhiyun default:
163*4882a593Smuzhiyun return NULL;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* Decide if the dquot's limits are actually being enforced. */
168*4882a593Smuzhiyun static inline bool
xfs_dquot_is_enforced(const struct xfs_dquot * dqp)169*4882a593Smuzhiyun xfs_dquot_is_enforced(
170*4882a593Smuzhiyun const struct xfs_dquot *dqp)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun switch (xfs_dquot_type(dqp)) {
173*4882a593Smuzhiyun case XFS_DQTYPE_USER:
174*4882a593Smuzhiyun return XFS_IS_UQUOTA_ENFORCED(dqp->q_mount);
175*4882a593Smuzhiyun case XFS_DQTYPE_GROUP:
176*4882a593Smuzhiyun return XFS_IS_GQUOTA_ENFORCED(dqp->q_mount);
177*4882a593Smuzhiyun case XFS_DQTYPE_PROJ:
178*4882a593Smuzhiyun return XFS_IS_PQUOTA_ENFORCED(dqp->q_mount);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun ASSERT(0);
181*4882a593Smuzhiyun return false;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * Check whether a dquot is under low free space conditions. We assume the quota
186*4882a593Smuzhiyun * is enabled and enforced.
187*4882a593Smuzhiyun */
xfs_dquot_lowsp(struct xfs_dquot * dqp)188*4882a593Smuzhiyun static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun int64_t freesp;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun freesp = dqp->q_blk.hardlimit - dqp->q_blk.reserved;
193*4882a593Smuzhiyun if (freesp < dqp->q_low_space[XFS_QLOWSP_1_PCNT])
194*4882a593Smuzhiyun return true;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return false;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #define XFS_DQ_IS_LOCKED(dqp) (mutex_is_locked(&((dqp)->q_qlock)))
202*4882a593Smuzhiyun #define XFS_DQ_IS_DIRTY(dqp) ((dqp)->q_flags & XFS_DQFLAG_DIRTY)
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun void xfs_qm_dqdestroy(struct xfs_dquot *dqp);
205*4882a593Smuzhiyun int xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp);
206*4882a593Smuzhiyun void xfs_qm_dqunpin_wait(struct xfs_dquot *dqp);
207*4882a593Smuzhiyun void xfs_qm_adjust_dqtimers(struct xfs_dquot *d);
208*4882a593Smuzhiyun void xfs_qm_adjust_dqlimits(struct xfs_dquot *d);
209*4882a593Smuzhiyun xfs_dqid_t xfs_qm_id_for_quotatype(struct xfs_inode *ip,
210*4882a593Smuzhiyun xfs_dqtype_t type);
211*4882a593Smuzhiyun int xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id,
212*4882a593Smuzhiyun xfs_dqtype_t type, bool can_alloc,
213*4882a593Smuzhiyun struct xfs_dquot **dqpp);
214*4882a593Smuzhiyun int xfs_qm_dqget_inode(struct xfs_inode *ip, xfs_dqtype_t type,
215*4882a593Smuzhiyun bool can_alloc, struct xfs_dquot **dqpp);
216*4882a593Smuzhiyun int xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id,
217*4882a593Smuzhiyun xfs_dqtype_t type, struct xfs_dquot **dqpp);
218*4882a593Smuzhiyun int xfs_qm_dqget_uncached(struct xfs_mount *mp,
219*4882a593Smuzhiyun xfs_dqid_t id, xfs_dqtype_t type,
220*4882a593Smuzhiyun struct xfs_dquot **dqpp);
221*4882a593Smuzhiyun void xfs_qm_dqput(struct xfs_dquot *dqp);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun void xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun void xfs_dquot_set_prealloc_limits(struct xfs_dquot *);
226*4882a593Smuzhiyun
xfs_qm_dqhold(struct xfs_dquot * dqp)227*4882a593Smuzhiyun static inline struct xfs_dquot *xfs_qm_dqhold(struct xfs_dquot *dqp)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun xfs_dqlock(dqp);
230*4882a593Smuzhiyun dqp->q_nrefs++;
231*4882a593Smuzhiyun xfs_dqunlock(dqp);
232*4882a593Smuzhiyun return dqp;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq,
236*4882a593Smuzhiyun xfs_dqtype_t type, void *priv);
237*4882a593Smuzhiyun int xfs_qm_dqiterate(struct xfs_mount *mp, xfs_dqtype_t type,
238*4882a593Smuzhiyun xfs_qm_dqiterate_fn iter_fn, void *priv);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun time64_t xfs_dquot_set_timeout(struct xfs_mount *mp, time64_t timeout);
241*4882a593Smuzhiyun time64_t xfs_dquot_set_grace_period(time64_t grace);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun #endif /* __XFS_DQUOT_H__ */
244