blob: d453c2c32e046f26b35d619067bf77446ca6f257 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigfcafb712009-02-09 08:47:34 +01002/*
3 * Copyright (c) 2008, Christoph Hellwig
4 * All Rights Reserved.
Christoph Hellwigfcafb712009-02-09 08:47:34 +01005 */
6#include "xfs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07007#include "xfs_shared.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100010#include "xfs_trans_resv.h"
Christoph Hellwigfcafb712009-02-09 08:47:34 +010011#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110012#include "xfs_inode.h"
Christoph Hellwigfcafb712009-02-09 08:47:34 +010013#include "xfs_quota.h"
Christoph Hellwigfcafb712009-02-09 08:47:34 +010014#include "xfs_trans.h"
Jan Kara5d3684c2014-11-19 12:03:59 +010015#include "xfs_trace.h"
16#include "xfs_icache.h"
Alex Elder06f8e2d62011-08-12 13:57:55 -050017#include "xfs_qm.h"
Christoph Hellwigfcafb712009-02-09 08:47:34 +010018#include <linux/quota.h>
19
20
Jan Kara5d3684c2014-11-19 12:03:59 +010021static void
22xfs_qm_fill_state(
23 struct qc_type_state *tstate,
24 struct xfs_mount *mp,
25 struct xfs_inode *ip,
26 xfs_ino_t ino)
27{
28 struct xfs_quotainfo *q = mp->m_quotainfo;
29 bool tempqip = false;
30
31 tstate->ino = ino;
32 if (!ip && ino == NULLFSINO)
33 return;
34 if (!ip) {
35 if (xfs_iget(mp, NULL, ino, 0, 0, &ip))
36 return;
37 tempqip = true;
38 }
39 tstate->flags |= QCI_SYSFILE;
40 tstate->blocks = ip->i_d.di_nblocks;
41 tstate->nextents = ip->i_d.di_nextents;
42 tstate->spc_timelimit = q->qi_btimelimit;
43 tstate->ino_timelimit = q->qi_itimelimit;
44 tstate->rt_spc_timelimit = q->qi_rtbtimelimit;
45 tstate->spc_warnlimit = q->qi_bwarnlimit;
46 tstate->ino_warnlimit = q->qi_iwarnlimit;
47 tstate->rt_spc_warnlimit = q->qi_rtbwarnlimit;
48 if (tempqip)
Darrick J. Wong44a87362018-07-25 12:52:32 -070049 xfs_irele(ip);
Jan Kara5d3684c2014-11-19 12:03:59 +010050}
51
52/*
53 * Return quota status information, such as enforcements, quota file inode
54 * numbers etc.
55 */
56static int
57xfs_fs_get_quota_state(
58 struct super_block *sb,
59 struct qc_state *state)
60{
61 struct xfs_mount *mp = XFS_M(sb);
62 struct xfs_quotainfo *q = mp->m_quotainfo;
63
64 memset(state, 0, sizeof(*state));
65 if (!XFS_IS_QUOTA_RUNNING(mp))
66 return 0;
67 state->s_incoredqs = q->qi_dquots;
68 if (XFS_IS_UQUOTA_RUNNING(mp))
69 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
70 if (XFS_IS_UQUOTA_ENFORCED(mp))
71 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
72 if (XFS_IS_GQUOTA_RUNNING(mp))
73 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
74 if (XFS_IS_GQUOTA_ENFORCED(mp))
75 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
76 if (XFS_IS_PQUOTA_RUNNING(mp))
77 state->s_state[PRJQUOTA].flags |= QCI_ACCT_ENABLED;
78 if (XFS_IS_PQUOTA_ENFORCED(mp))
79 state->s_state[PRJQUOTA].flags |= QCI_LIMITS_ENFORCED;
80
81 xfs_qm_fill_state(&state->s_state[USRQUOTA], mp, q->qi_uquotaip,
82 mp->m_sb.sb_uquotino);
83 xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
84 mp->m_sb.sb_gquotino);
85 xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
86 mp->m_sb.sb_pquotino);
87 return 0;
88}
89
Christoph Hellwigfcafb712009-02-09 08:47:34 +010090STATIC int
91xfs_quota_type(int type)
92{
93 switch (type) {
94 case USRQUOTA:
95 return XFS_DQ_USER;
96 case GRPQUOTA:
97 return XFS_DQ_GROUP;
98 default:
99 return XFS_DQ_PROJ;
100 }
101}
102
Jan Karac14cad92014-12-16 13:07:45 +0100103#define XFS_QC_SETINFO_MASK (QC_TIMER_MASK | QC_WARNS_MASK)
104
105/*
106 * Adjust quota timers & warnings
107 */
108static int
109xfs_fs_set_info(
110 struct super_block *sb,
111 int type,
112 struct qc_info *info)
113{
114 struct xfs_mount *mp = XFS_M(sb);
115 struct qc_dqblk newlim;
116
David Howellsbc98a422017-07-17 08:45:34 +0100117 if (sb_rdonly(sb))
Jan Karac14cad92014-12-16 13:07:45 +0100118 return -EROFS;
119 if (!XFS_IS_QUOTA_RUNNING(mp))
120 return -ENOSYS;
121 if (!XFS_IS_QUOTA_ON(mp))
122 return -ESRCH;
123 if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
124 return -EINVAL;
125 if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
126 return 0;
127
128 newlim.d_fieldmask = info->i_fieldmask;
129 newlim.d_spc_timer = info->i_spc_timelimit;
130 newlim.d_ino_timer = info->i_ino_timelimit;
131 newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
132 newlim.d_ino_warns = info->i_ino_warnlimit;
133 newlim.d_spc_warns = info->i_spc_warnlimit;
134 newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
135
136 return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
137}
138
Jan Kara38e478c2014-10-08 15:56:21 +0200139static unsigned int
140xfs_quota_flags(unsigned int uflags)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100141{
Jan Kara38e478c2014-10-08 15:56:21 +0200142 unsigned int flags = 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100143
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200144 if (uflags & FS_QUOTA_UDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100145 flags |= XFS_UQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200146 if (uflags & FS_QUOTA_PDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100147 flags |= XFS_PQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200148 if (uflags & FS_QUOTA_GDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100149 flags |= XFS_GQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200150 if (uflags & FS_QUOTA_UDQ_ENFD)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100151 flags |= XFS_UQUOTA_ENFD;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500152 if (uflags & FS_QUOTA_GDQ_ENFD)
153 flags |= XFS_GQUOTA_ENFD;
154 if (uflags & FS_QUOTA_PDQ_ENFD)
155 flags |= XFS_PQUOTA_ENFD;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100156
Jan Kara38e478c2014-10-08 15:56:21 +0200157 return flags;
158}
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100159
Jan Kara38e478c2014-10-08 15:56:21 +0200160STATIC int
161xfs_quota_enable(
162 struct super_block *sb,
163 unsigned int uflags)
164{
165 struct xfs_mount *mp = XFS_M(sb);
166
David Howellsbc98a422017-07-17 08:45:34 +0100167 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200168 return -EROFS;
169 if (!XFS_IS_QUOTA_RUNNING(mp))
170 return -ENOSYS;
171
172 return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
173}
174
175STATIC int
176xfs_quota_disable(
177 struct super_block *sb,
178 unsigned int uflags)
179{
180 struct xfs_mount *mp = XFS_M(sb);
181
David Howellsbc98a422017-07-17 08:45:34 +0100182 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200183 return -EROFS;
184 if (!XFS_IS_QUOTA_RUNNING(mp))
185 return -ENOSYS;
186 if (!XFS_IS_QUOTA_ON(mp))
187 return -EINVAL;
188
189 return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100190}
191
192STATIC int
Eric Sandeen9da93f92014-05-05 17:25:50 +1000193xfs_fs_rm_xquota(
194 struct super_block *sb,
195 unsigned int uflags)
196{
197 struct xfs_mount *mp = XFS_M(sb);
198 unsigned int flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +1000199
David Howellsbc98a422017-07-17 08:45:34 +0100200 if (sb_rdonly(sb))
Eric Sandeen9da93f92014-05-05 17:25:50 +1000201 return -EROFS;
202
203 if (XFS_IS_QUOTA_ON(mp))
204 return -EINVAL;
205
206 if (uflags & FS_USER_QUOTA)
207 flags |= XFS_DQ_USER;
208 if (uflags & FS_GROUP_QUOTA)
209 flags |= XFS_DQ_GROUP;
Jie Liu74dc93a2014-07-24 21:27:17 +1000210 if (uflags & FS_PROJ_QUOTA)
Eric Sandeen9da93f92014-05-05 17:25:50 +1000211 flags |= XFS_DQ_PROJ;
212
Dave Chinner24513372014-06-25 14:58:08 +1000213 return xfs_qm_scall_trunc_qfiles(mp, flags);
214}
Eric Sandeen9da93f92014-05-05 17:25:50 +1000215
216STATIC int
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400217xfs_fs_get_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100218 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700219 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200220 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100221{
222 struct xfs_mount *mp = XFS_M(sb);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100223 xfs_dqid_t id;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100224
225 if (!XFS_IS_QUOTA_RUNNING(mp))
226 return -ENOSYS;
227 if (!XFS_IS_QUOTA_ON(mp))
228 return -ESRCH;
229
Eric Sandeen296c24e2016-02-08 11:27:38 +1100230 id = from_kqid(&init_user_ns, qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700231 return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100232}
233
234/* Return quota info for active quota >= this qid */
235STATIC int
236xfs_fs_get_nextdqblk(
237 struct super_block *sb,
238 struct kqid *qid,
239 struct qc_dqblk *qdq)
240{
241 int ret;
242 struct xfs_mount *mp = XFS_M(sb);
243 xfs_dqid_t id;
244
245 if (!XFS_IS_QUOTA_RUNNING(mp))
246 return -ENOSYS;
247 if (!XFS_IS_QUOTA_ON(mp))
248 return -ESRCH;
249
250 id = from_kqid(&init_user_ns, *qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700251 ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
252 qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100253 if (ret)
254 return ret;
255
256 /* ID may be different, so convert back what we got */
257 *qid = make_kqid(current_user_ns(), qid->type, id);
258 return 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100259}
260
261STATIC int
Christoph Hellwigc472b432010-05-06 17:05:17 -0400262xfs_fs_set_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100263 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700264 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200265 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100266{
267 struct xfs_mount *mp = XFS_M(sb);
268
David Howellsbc98a422017-07-17 08:45:34 +0100269 if (sb_rdonly(sb))
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100270 return -EROFS;
271 if (!XFS_IS_QUOTA_RUNNING(mp))
272 return -ENOSYS;
273 if (!XFS_IS_QUOTA_ON(mp))
274 return -ESRCH;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100275
Dave Chinner24513372014-06-25 14:58:08 +1000276 return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
Jan Kara14bf61f2014-10-09 16:03:13 +0200277 xfs_quota_type(qid.type), qdq);
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100278}
279
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700280const struct quotactl_ops xfs_quotactl_operations = {
Jan Kara5d3684c2014-11-19 12:03:59 +0100281 .get_state = xfs_fs_get_quota_state,
Jan Karac14cad92014-12-16 13:07:45 +0100282 .set_info = xfs_fs_set_info,
Jan Kara38e478c2014-10-08 15:56:21 +0200283 .quota_enable = xfs_quota_enable,
284 .quota_disable = xfs_quota_disable,
Eric Sandeen9da93f92014-05-05 17:25:50 +1000285 .rm_xquota = xfs_fs_rm_xquota,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400286 .get_dqblk = xfs_fs_get_dqblk,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100287 .get_nextdqblk = xfs_fs_get_nextdqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -0400288 .set_dqblk = xfs_fs_set_dqblk,
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100289};