xref: /linux/fs/xfs/xfs_dquot.h (revision 4eb7ae7a301d3586c3351e81d5c3cfe2304a1a6a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
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 
20 struct xfs_mount;
21 struct xfs_trans;
22 
23 enum {
24 	XFS_QLOWSP_1_PCNT = 0,
25 	XFS_QLOWSP_3_PCNT,
26 	XFS_QLOWSP_5_PCNT,
27 	XFS_QLOWSP_MAX
28 };
29 
30 /*
31  * The incore dquot structure
32  */
33 struct 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;
41 
42 	struct xfs_disk_dquot	q_core;
43 	struct xfs_dq_logitem	q_logitem;
44 	/* 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 };
58 
59 /*
60  * Lock hierarchy for q_qlock:
61  *	XFS_QLOCK_NORMAL is the implicit default,
62  *	XFS_QLOCK_NESTED is the dquot with the higher id in xfs_dqlock2
63  */
64 enum {
65 	XFS_QLOCK_NORMAL = 0,
66 	XFS_QLOCK_NESTED,
67 };
68 
69 /*
70  * Manage the q_flush completion queue embedded in the dquot. This completion
71  * queue synchronizes processes attempting to flush the in-core dquot back to
72  * disk.
73  */
74 static inline void xfs_dqflock(struct xfs_dquot *dqp)
75 {
76 	wait_for_completion(&dqp->q_flush);
77 }
78 
79 static inline bool xfs_dqflock_nowait(struct xfs_dquot *dqp)
80 {
81 	return try_wait_for_completion(&dqp->q_flush);
82 }
83 
84 static inline void xfs_dqfunlock(struct xfs_dquot *dqp)
85 {
86 	complete(&dqp->q_flush);
87 }
88 
89 static inline int xfs_dqlock_nowait(struct xfs_dquot *dqp)
90 {
91 	return mutex_trylock(&dqp->q_qlock);
92 }
93 
94 static inline void xfs_dqlock(struct xfs_dquot *dqp)
95 {
96 	mutex_lock(&dqp->q_qlock);
97 }
98 
99 static inline void xfs_dqunlock(struct xfs_dquot *dqp)
100 {
101 	mutex_unlock(&dqp->q_qlock);
102 }
103 
104 static 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:
110 		return XFS_IS_GQUOTA_ON(mp);
111 	case XFS_DQ_PROJ:
112 		return XFS_IS_PQUOTA_ON(mp);
113 	default:
114 		return 0;
115 	}
116 }
117 
118 static inline struct xfs_dquot *xfs_inode_dquot(struct xfs_inode *ip, int type)
119 {
120 	switch (type & XFS_DQ_ALLTYPES) {
121 	case XFS_DQ_USER:
122 		return ip->i_udquot;
123 	case XFS_DQ_GROUP:
124 		return ip->i_gdquot;
125 	case XFS_DQ_PROJ:
126 		return ip->i_pdquot;
127 	default:
128 		return NULL;
129 	}
130 }
131 
132 /*
133  * Check whether a dquot is under low free space conditions. We assume the quota
134  * is enabled and enforced.
135  */
136 static 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 
147 #define XFS_DQ_IS_LOCKED(dqp)	(mutex_is_locked(&((dqp)->q_qlock)))
148 #define XFS_DQ_IS_DIRTY(dqp)	((dqp)->dq_flags & XFS_DQ_DIRTY)
149 #define XFS_QM_ISUDQ(dqp)	((dqp)->dq_flags & XFS_DQ_USER)
150 #define XFS_QM_ISPDQ(dqp)	((dqp)->dq_flags & XFS_DQ_PROJ)
151 #define XFS_QM_ISGDQ(dqp)	((dqp)->dq_flags & XFS_DQ_GROUP)
152 
153 void		xfs_qm_dqdestroy(struct xfs_dquot *dqp);
154 int		xfs_qm_dqflush(struct xfs_dquot *dqp, struct xfs_buf **bpp);
155 void		xfs_qm_dqunpin_wait(struct xfs_dquot *dqp);
156 void		xfs_qm_adjust_dqtimers(struct xfs_mount *mp,
157 						struct xfs_disk_dquot *d);
158 void		xfs_qm_adjust_dqlimits(struct xfs_mount *mp,
159 						struct xfs_dquot *d);
160 xfs_dqid_t	xfs_qm_id_for_quotatype(struct xfs_inode *ip, uint type);
161 int		xfs_qm_dqget(struct xfs_mount *mp, xfs_dqid_t id,
162 					uint type, bool can_alloc,
163 					struct xfs_dquot **dqpp);
164 int		xfs_qm_dqget_inode(struct xfs_inode *ip, uint type,
165 						bool can_alloc,
166 						struct xfs_dquot **dqpp);
167 int		xfs_qm_dqget_next(struct xfs_mount *mp, xfs_dqid_t id,
168 					uint type, struct xfs_dquot **dqpp);
169 int		xfs_qm_dqget_uncached(struct xfs_mount *mp,
170 						xfs_dqid_t id, uint type,
171 						struct xfs_dquot **dqpp);
172 void		xfs_qm_dqput(struct xfs_dquot *dqp);
173 
174 void		xfs_dqlock2(struct xfs_dquot *, struct xfs_dquot *);
175 
176 void		xfs_dquot_set_prealloc_limits(struct xfs_dquot *);
177 
178 static 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 
186 typedef int (*xfs_qm_dqiterate_fn)(struct xfs_dquot *dq, uint dqtype,
187 		void *priv);
188 int xfs_qm_dqiterate(struct xfs_mount *mp, uint dqtype,
189 		xfs_qm_dqiterate_fn iter_fn, void *priv);
190 
191 #endif /* __XFS_DQUOT_H__ */
192