1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2008, Christoph Hellwig
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_format.h"
8 #include "xfs_log_format.h"
9 #include "xfs_trans_resv.h"
10 #include "xfs_mount.h"
11 #include "xfs_inode.h"
12 #include "xfs_quota.h"
13 #include "xfs_trans.h"
14 #include "xfs_trace.h"
15 #include "xfs_icache.h"
16 #include "xfs_qm.h"
17 #include <linux/quota.h>
18 
19 
20 static void
xfs_qm_fill_state(struct qc_type_state * tstate,struct xfs_mount * mp,struct xfs_inode * ip,xfs_ino_t ino)21 xfs_qm_fill_state(
22 	struct qc_type_state	*tstate,
23 	struct xfs_mount	*mp,
24 	struct xfs_inode	*ip,
25 	xfs_ino_t		ino)
26 {
27 	struct xfs_quotainfo *q = mp->m_quotainfo;
28 	bool tempqip = false;
29 
30 	tstate->ino = ino;
31 	if (!ip && ino == NULLFSINO)
32 		return;
33 	if (!ip) {
34 		if (xfs_iget(mp, NULL, ino, 0, 0, &ip))
35 			return;
36 		tempqip = true;
37 	}
38 	tstate->flags |= QCI_SYSFILE;
39 	tstate->blocks = ip->i_d.di_nblocks;
40 	tstate->nextents = ip->i_d.di_nextents;
41 	tstate->spc_timelimit = q->qi_btimelimit;
42 	tstate->ino_timelimit = q->qi_itimelimit;
43 	tstate->rt_spc_timelimit = q->qi_rtbtimelimit;
44 	tstate->spc_warnlimit = q->qi_bwarnlimit;
45 	tstate->ino_warnlimit = q->qi_iwarnlimit;
46 	tstate->rt_spc_warnlimit = q->qi_rtbwarnlimit;
47 	if (tempqip)
48 		xfs_irele(ip);
49 }
50 
51 /*
52  * Return quota status information, such as enforcements, quota file inode
53  * numbers etc.
54  */
55 static int
xfs_fs_get_quota_state(struct super_block * sb,struct qc_state * state)56 xfs_fs_get_quota_state(
57 	struct super_block	*sb,
58 	struct qc_state		*state)
59 {
60 	struct xfs_mount *mp = XFS_M(sb);
61 	struct xfs_quotainfo *q = mp->m_quotainfo;
62 
63 	memset(state, 0, sizeof(*state));
64 	if (!XFS_IS_QUOTA_RUNNING(mp))
65 		return 0;
66 	state->s_incoredqs = q->qi_dquots;
67 	if (XFS_IS_UQUOTA_RUNNING(mp))
68 		state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
69 	if (XFS_IS_UQUOTA_ENFORCED(mp))
70 		state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
71 	if (XFS_IS_GQUOTA_RUNNING(mp))
72 		state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
73 	if (XFS_IS_GQUOTA_ENFORCED(mp))
74 		state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
75 	if (XFS_IS_PQUOTA_RUNNING(mp))
76 		state->s_state[PRJQUOTA].flags |= QCI_ACCT_ENABLED;
77 	if (XFS_IS_PQUOTA_ENFORCED(mp))
78 		state->s_state[PRJQUOTA].flags |= QCI_LIMITS_ENFORCED;
79 
80 	xfs_qm_fill_state(&state->s_state[USRQUOTA], mp, q->qi_uquotaip,
81 			  mp->m_sb.sb_uquotino);
82 	xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
83 			  mp->m_sb.sb_gquotino);
84 	xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
85 			  mp->m_sb.sb_pquotino);
86 	return 0;
87 }
88 
89 STATIC int
xfs_quota_type(int type)90 xfs_quota_type(int type)
91 {
92 	switch (type) {
93 	case USRQUOTA:
94 		return XFS_DQ_USER;
95 	case GRPQUOTA:
96 		return XFS_DQ_GROUP;
97 	default:
98 		return XFS_DQ_PROJ;
99 	}
100 }
101 
102 #define XFS_QC_SETINFO_MASK (QC_TIMER_MASK | QC_WARNS_MASK)
103 
104 /*
105  * Adjust quota timers & warnings
106  */
107 static int
xfs_fs_set_info(struct super_block * sb,int type,struct qc_info * info)108 xfs_fs_set_info(
109 	struct super_block	*sb,
110 	int			type,
111 	struct qc_info		*info)
112 {
113 	struct xfs_mount *mp = XFS_M(sb);
114 	struct qc_dqblk newlim;
115 
116 	if (sb_rdonly(sb))
117 		return -EROFS;
118 	if (!XFS_IS_QUOTA_RUNNING(mp))
119 		return -ENOSYS;
120 	if (!XFS_IS_QUOTA_ON(mp))
121 		return -ESRCH;
122 	if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
123 		return -EINVAL;
124 	if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
125 		return 0;
126 
127 	newlim.d_fieldmask = info->i_fieldmask;
128 	newlim.d_spc_timer = info->i_spc_timelimit;
129 	newlim.d_ino_timer = info->i_ino_timelimit;
130 	newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
131 	newlim.d_ino_warns = info->i_ino_warnlimit;
132 	newlim.d_spc_warns = info->i_spc_warnlimit;
133 	newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
134 
135 	return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
136 }
137 
138 static unsigned int
xfs_quota_flags(unsigned int uflags)139 xfs_quota_flags(unsigned int uflags)
140 {
141 	unsigned int flags = 0;
142 
143 	if (uflags & FS_QUOTA_UDQ_ACCT)
144 		flags |= XFS_UQUOTA_ACCT;
145 	if (uflags & FS_QUOTA_PDQ_ACCT)
146 		flags |= XFS_PQUOTA_ACCT;
147 	if (uflags & FS_QUOTA_GDQ_ACCT)
148 		flags |= XFS_GQUOTA_ACCT;
149 	if (uflags & FS_QUOTA_UDQ_ENFD)
150 		flags |= XFS_UQUOTA_ENFD;
151 	if (uflags & FS_QUOTA_GDQ_ENFD)
152 		flags |= XFS_GQUOTA_ENFD;
153 	if (uflags & FS_QUOTA_PDQ_ENFD)
154 		flags |= XFS_PQUOTA_ENFD;
155 
156 	return flags;
157 }
158 
159 STATIC int
xfs_quota_enable(struct super_block * sb,unsigned int uflags)160 xfs_quota_enable(
161 	struct super_block	*sb,
162 	unsigned int		uflags)
163 {
164 	struct xfs_mount	*mp = XFS_M(sb);
165 
166 	if (sb_rdonly(sb))
167 		return -EROFS;
168 	if (!XFS_IS_QUOTA_RUNNING(mp))
169 		return -ENOSYS;
170 
171 	return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
172 }
173 
174 STATIC int
xfs_quota_disable(struct super_block * sb,unsigned int uflags)175 xfs_quota_disable(
176 	struct super_block	*sb,
177 	unsigned int		uflags)
178 {
179 	struct xfs_mount	*mp = XFS_M(sb);
180 
181 	if (sb_rdonly(sb))
182 		return -EROFS;
183 	if (!XFS_IS_QUOTA_RUNNING(mp))
184 		return -ENOSYS;
185 	if (!XFS_IS_QUOTA_ON(mp))
186 		return -EINVAL;
187 
188 	return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
189 }
190 
191 STATIC int
xfs_fs_rm_xquota(struct super_block * sb,unsigned int uflags)192 xfs_fs_rm_xquota(
193 	struct super_block	*sb,
194 	unsigned int		uflags)
195 {
196 	struct xfs_mount	*mp = XFS_M(sb);
197 	unsigned int		flags = 0;
198 
199 	if (sb_rdonly(sb))
200 		return -EROFS;
201 
202 	if (XFS_IS_QUOTA_ON(mp))
203 		return -EINVAL;
204 
205 	if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
206 		return -EINVAL;
207 
208 	if (uflags & FS_USER_QUOTA)
209 		flags |= XFS_DQ_USER;
210 	if (uflags & FS_GROUP_QUOTA)
211 		flags |= XFS_DQ_GROUP;
212 	if (uflags & FS_PROJ_QUOTA)
213 		flags |= XFS_DQ_PROJ;
214 
215 	return xfs_qm_scall_trunc_qfiles(mp, flags);
216 }
217 
218 STATIC int
xfs_fs_get_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * qdq)219 xfs_fs_get_dqblk(
220 	struct super_block	*sb,
221 	struct kqid		qid,
222 	struct qc_dqblk		*qdq)
223 {
224 	struct xfs_mount	*mp = XFS_M(sb);
225 	xfs_dqid_t		id;
226 
227 	if (!XFS_IS_QUOTA_RUNNING(mp))
228 		return -ENOSYS;
229 	if (!XFS_IS_QUOTA_ON(mp))
230 		return -ESRCH;
231 
232 	id = from_kqid(&init_user_ns, qid);
233 	return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
234 }
235 
236 /* Return quota info for active quota >= this qid */
237 STATIC int
xfs_fs_get_nextdqblk(struct super_block * sb,struct kqid * qid,struct qc_dqblk * qdq)238 xfs_fs_get_nextdqblk(
239 	struct super_block	*sb,
240 	struct kqid		*qid,
241 	struct qc_dqblk		*qdq)
242 {
243 	int			ret;
244 	struct xfs_mount	*mp = XFS_M(sb);
245 	xfs_dqid_t		id;
246 
247 	if (!XFS_IS_QUOTA_RUNNING(mp))
248 		return -ENOSYS;
249 	if (!XFS_IS_QUOTA_ON(mp))
250 		return -ESRCH;
251 
252 	id = from_kqid(&init_user_ns, *qid);
253 	ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
254 			qdq);
255 	if (ret)
256 		return ret;
257 
258 	/* ID may be different, so convert back what we got */
259 	*qid = make_kqid(current_user_ns(), qid->type, id);
260 	return 0;
261 }
262 
263 STATIC int
xfs_fs_set_dqblk(struct super_block * sb,struct kqid qid,struct qc_dqblk * qdq)264 xfs_fs_set_dqblk(
265 	struct super_block	*sb,
266 	struct kqid		qid,
267 	struct qc_dqblk		*qdq)
268 {
269 	struct xfs_mount	*mp = XFS_M(sb);
270 
271 	if (sb_rdonly(sb))
272 		return -EROFS;
273 	if (!XFS_IS_QUOTA_RUNNING(mp))
274 		return -ENOSYS;
275 	if (!XFS_IS_QUOTA_ON(mp))
276 		return -ESRCH;
277 
278 	return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
279 				     xfs_quota_type(qid.type), qdq);
280 }
281 
282 const struct quotactl_ops xfs_quotactl_operations = {
283 	.get_state		= xfs_fs_get_quota_state,
284 	.set_info		= xfs_fs_set_info,
285 	.quota_enable		= xfs_quota_enable,
286 	.quota_disable		= xfs_quota_disable,
287 	.rm_xquota		= xfs_fs_rm_xquota,
288 	.get_dqblk		= xfs_fs_get_dqblk,
289 	.get_nextdqblk		= xfs_fs_get_nextdqblk,
290 	.set_dqblk		= xfs_fs_set_dqblk,
291 };
292