1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#ifndef __XFS_QUOTA_H__
7#define __XFS_QUOTA_H__
8
9#include "xfs_quota_defs.h"
10
11/*
12 * Kernel only quota definitions and functions
13 */
14
15struct xfs_trans;
16struct xfs_buf;
17
18/*
19 * This check is done typically without holding the inode lock;
20 * that may seem racy, but it is harmless in the context that it is used.
21 * The inode cannot go inactive as long a reference is kept, and
22 * therefore if dquot(s) were attached, they'll stay consistent.
23 * If, for example, the ownership of the inode changes while
24 * we didn't have the inode locked, the appropriate dquot(s) will be
25 * attached atomically.
26 */
27#define XFS_NOT_DQATTACHED(mp, ip) \
28	((XFS_IS_UQUOTA_ON(mp) && (ip)->i_udquot == NULL) || \
29	 (XFS_IS_GQUOTA_ON(mp) && (ip)->i_gdquot == NULL) || \
30	 (XFS_IS_PQUOTA_ON(mp) && (ip)->i_pdquot == NULL))
31
32#define XFS_QM_NEED_QUOTACHECK(mp) \
33	((XFS_IS_UQUOTA_ON(mp) && \
34		(mp->m_sb.sb_qflags & XFS_UQUOTA_CHKD) == 0) || \
35	 (XFS_IS_GQUOTA_ON(mp) && \
36		(mp->m_sb.sb_qflags & XFS_GQUOTA_CHKD) == 0) || \
37	 (XFS_IS_PQUOTA_ON(mp) && \
38		(mp->m_sb.sb_qflags & XFS_PQUOTA_CHKD) == 0))
39
40static inline uint
41xfs_quota_chkd_flag(
42	xfs_dqtype_t		type)
43{
44	switch (type) {
45	case XFS_DQTYPE_USER:
46		return XFS_UQUOTA_CHKD;
47	case XFS_DQTYPE_GROUP:
48		return XFS_GQUOTA_CHKD;
49	case XFS_DQTYPE_PROJ:
50		return XFS_PQUOTA_CHKD;
51	default:
52		return 0;
53	}
54}
55
56/*
57 * The structure kept inside the xfs_trans_t keep track of dquot changes
58 * within a transaction and apply them later.
59 */
60struct xfs_dqtrx {
61	struct xfs_dquot *qt_dquot;	  /* the dquot this refers to */
62
63	uint64_t	qt_blk_res;	  /* blks reserved on a dquot */
64	int64_t		qt_bcount_delta;  /* dquot blk count changes */
65	int64_t		qt_delbcnt_delta; /* delayed dquot blk count changes */
66
67	uint64_t	qt_rtblk_res;	  /* # blks reserved on a dquot */
68	uint64_t	qt_rtblk_res_used;/* # blks used from reservation */
69	int64_t		qt_rtbcount_delta;/* dquot realtime blk changes */
70	int64_t		qt_delrtb_delta;  /* delayed RT blk count changes */
71
72	uint64_t	qt_ino_res;	  /* inode reserved on a dquot */
73	uint64_t	qt_ino_res_used;  /* inodes used from the reservation */
74	int64_t		qt_icount_delta;  /* dquot inode count changes */
75};
76
77enum xfs_apply_dqtrx_type {
78	XFS_APPLY_DQTRX_COMMIT = 0,
79	XFS_APPLY_DQTRX_UNRESERVE,
80};
81
82/*
83 * Parameters for applying dqtrx changes to a dquot.  The hook function arg
84 * parameter is enum xfs_apply_dqtrx_type.
85 */
86struct xfs_apply_dqtrx_params {
87	uintptr_t		tx_id;
88	xfs_ino_t		ino;
89	xfs_dqtype_t		q_type;
90	xfs_dqid_t		q_id;
91};
92
93#ifdef CONFIG_XFS_QUOTA
94extern void xfs_trans_dup_dqinfo(struct xfs_trans *, struct xfs_trans *);
95extern void xfs_trans_free_dqinfo(struct xfs_trans *);
96extern void xfs_trans_mod_dquot_byino(struct xfs_trans *, struct xfs_inode *,
97		uint, int64_t);
98extern void xfs_trans_apply_dquot_deltas(struct xfs_trans *);
99extern void xfs_trans_unreserve_and_mod_dquots(struct xfs_trans *);
100int xfs_trans_reserve_quota_nblks(struct xfs_trans *tp, struct xfs_inode *ip,
101		int64_t dblocks, int64_t rblocks, bool force);
102extern int xfs_trans_reserve_quota_bydquots(struct xfs_trans *,
103		struct xfs_mount *, struct xfs_dquot *,
104		struct xfs_dquot *, struct xfs_dquot *, int64_t, long, uint);
105int xfs_trans_reserve_quota_icreate(struct xfs_trans *tp,
106		struct xfs_dquot *udqp, struct xfs_dquot *gdqp,
107		struct xfs_dquot *pdqp, int64_t dblocks);
108
109extern int xfs_qm_vop_dqalloc(struct xfs_inode *, kuid_t, kgid_t,
110		prid_t, uint, struct xfs_dquot **, struct xfs_dquot **,
111		struct xfs_dquot **);
112extern void xfs_qm_vop_create_dqattach(struct xfs_trans *, struct xfs_inode *,
113		struct xfs_dquot *, struct xfs_dquot *, struct xfs_dquot *);
114extern int xfs_qm_vop_rename_dqattach(struct xfs_inode **);
115extern struct xfs_dquot *xfs_qm_vop_chown(struct xfs_trans *,
116		struct xfs_inode *, struct xfs_dquot **, struct xfs_dquot *);
117extern int xfs_qm_dqattach(struct xfs_inode *);
118extern int xfs_qm_dqattach_locked(struct xfs_inode *ip, bool doalloc);
119extern void xfs_qm_dqdetach(struct xfs_inode *);
120extern void xfs_qm_dqrele(struct xfs_dquot *);
121extern void xfs_qm_statvfs(struct xfs_inode *, struct kstatfs *);
122extern int xfs_qm_newmount(struct xfs_mount *, uint *, uint *);
123extern void xfs_qm_mount_quotas(struct xfs_mount *);
124extern void xfs_qm_unmount(struct xfs_mount *);
125extern void xfs_qm_unmount_quotas(struct xfs_mount *);
126bool xfs_inode_near_dquot_enforcement(struct xfs_inode *ip, xfs_dqtype_t type);
127
128# ifdef CONFIG_XFS_LIVE_HOOKS
129void xfs_trans_mod_ino_dquot(struct xfs_trans *tp, struct xfs_inode *ip,
130		struct xfs_dquot *dqp, unsigned int field, int64_t delta);
131
132struct xfs_quotainfo;
133
134struct xfs_dqtrx_hook {
135	struct xfs_hook		mod_hook;
136	struct xfs_hook		apply_hook;
137};
138
139void xfs_dqtrx_hook_disable(void);
140void xfs_dqtrx_hook_enable(void);
141
142int xfs_dqtrx_hook_add(struct xfs_quotainfo *qi, struct xfs_dqtrx_hook *hook);
143void xfs_dqtrx_hook_del(struct xfs_quotainfo *qi, struct xfs_dqtrx_hook *hook);
144void xfs_dqtrx_hook_setup(struct xfs_dqtrx_hook *hook, notifier_fn_t mod_fn,
145		notifier_fn_t apply_fn);
146# else
147#  define xfs_trans_mod_ino_dquot(tp, ip, dqp, field, delta) \
148		xfs_trans_mod_dquot((tp), (dqp), (field), (delta))
149# endif /* CONFIG_XFS_LIVE_HOOKS */
150
151#else
152static inline int
153xfs_qm_vop_dqalloc(struct xfs_inode *ip, kuid_t kuid, kgid_t kgid,
154		prid_t prid, uint flags, struct xfs_dquot **udqp,
155		struct xfs_dquot **gdqp, struct xfs_dquot **pdqp)
156{
157	*udqp = NULL;
158	*gdqp = NULL;
159	*pdqp = NULL;
160	return 0;
161}
162#define xfs_trans_dup_dqinfo(tp, tp2)
163#define xfs_trans_free_dqinfo(tp)
164static inline void xfs_trans_mod_dquot_byino(struct xfs_trans *tp,
165		struct xfs_inode *ip, uint field, int64_t delta)
166{
167}
168#define xfs_trans_apply_dquot_deltas(tp)
169#define xfs_trans_unreserve_and_mod_dquots(tp)
170static inline int xfs_trans_reserve_quota_nblks(struct xfs_trans *tp,
171		struct xfs_inode *ip, int64_t dblocks, int64_t rblocks,
172		bool force)
173{
174	return 0;
175}
176static inline int xfs_trans_reserve_quota_bydquots(struct xfs_trans *tp,
177		struct xfs_mount *mp, struct xfs_dquot *udqp,
178		struct xfs_dquot *gdqp, struct xfs_dquot *pdqp,
179		int64_t nblks, long nions, uint flags)
180{
181	return 0;
182}
183
184static inline int
185xfs_trans_reserve_quota_icreate(struct xfs_trans *tp, struct xfs_dquot *udqp,
186		struct xfs_dquot *gdqp, struct xfs_dquot *pdqp, int64_t dblocks)
187{
188	return 0;
189}
190
191#define xfs_qm_vop_create_dqattach(tp, ip, u, g, p)
192#define xfs_qm_vop_rename_dqattach(it)					(0)
193#define xfs_qm_vop_chown(tp, ip, old, new)				(NULL)
194#define xfs_qm_dqattach(ip)						(0)
195#define xfs_qm_dqattach_locked(ip, fl)					(0)
196#define xfs_qm_dqdetach(ip)
197#define xfs_qm_dqrele(d)			do { (d) = (d); } while(0)
198#define xfs_qm_statvfs(ip, s)			do { } while(0)
199#define xfs_qm_newmount(mp, a, b)					(0)
200#define xfs_qm_mount_quotas(mp)
201#define xfs_qm_unmount(mp)
202#define xfs_qm_unmount_quotas(mp)
203#define xfs_inode_near_dquot_enforcement(ip, type)			(false)
204
205# ifdef CONFIG_XFS_LIVE_HOOKS
206#  define xfs_dqtrx_hook_enable()		((void)0)
207#  define xfs_dqtrx_hook_disable()		((void)0)
208# endif /* CONFIG_XFS_LIVE_HOOKS */
209
210#endif /* CONFIG_XFS_QUOTA */
211
212static inline int
213xfs_quota_reserve_blkres(struct xfs_inode *ip, int64_t blocks)
214{
215	return xfs_trans_reserve_quota_nblks(NULL, ip, blocks, 0, false);
216}
217
218static inline void
219xfs_quota_unreserve_blkres(struct xfs_inode *ip, uint64_t blocks)
220{
221	/* don't return an error as unreserving quotas can't fail */
222	xfs_quota_reserve_blkres(ip, -(int64_t)blocks);
223}
224
225extern int xfs_mount_reset_sbqflags(struct xfs_mount *);
226
227#endif	/* __XFS_QUOTA_H__ */
228