blob: cd6c7210a37366a9e144f85f5e042cea4aa20968 [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_icache.h"
Alex Elder06f8e2d62011-08-12 13:57:55 -050016#include "xfs_qm.h"
Christoph Hellwigfcafb712009-02-09 08:47:34 +010017
18
Jan Kara5d3684c2014-11-19 12:03:59 +010019static void
20xfs_qm_fill_state(
21 struct qc_type_state *tstate,
22 struct xfs_mount *mp,
23 struct xfs_inode *ip,
24 xfs_ino_t ino)
25{
26 struct xfs_quotainfo *q = mp->m_quotainfo;
27 bool tempqip = false;
28
29 tstate->ino = ino;
30 if (!ip && ino == NULLFSINO)
31 return;
32 if (!ip) {
33 if (xfs_iget(mp, NULL, ino, 0, 0, &ip))
34 return;
35 tempqip = true;
36 }
37 tstate->flags |= QCI_SYSFILE;
38 tstate->blocks = ip->i_d.di_nblocks;
39 tstate->nextents = ip->i_d.di_nextents;
40 tstate->spc_timelimit = q->qi_btimelimit;
41 tstate->ino_timelimit = q->qi_itimelimit;
42 tstate->rt_spc_timelimit = q->qi_rtbtimelimit;
43 tstate->spc_warnlimit = q->qi_bwarnlimit;
44 tstate->ino_warnlimit = q->qi_iwarnlimit;
45 tstate->rt_spc_warnlimit = q->qi_rtbwarnlimit;
46 if (tempqip)
Darrick J. Wong44a87362018-07-25 12:52:32 -070047 xfs_irele(ip);
Jan Kara5d3684c2014-11-19 12:03:59 +010048}
49
50/*
51 * Return quota status information, such as enforcements, quota file inode
52 * numbers etc.
53 */
54static int
55xfs_fs_get_quota_state(
56 struct super_block *sb,
57 struct qc_state *state)
58{
59 struct xfs_mount *mp = XFS_M(sb);
60 struct xfs_quotainfo *q = mp->m_quotainfo;
61
62 memset(state, 0, sizeof(*state));
63 if (!XFS_IS_QUOTA_RUNNING(mp))
64 return 0;
65 state->s_incoredqs = q->qi_dquots;
66 if (XFS_IS_UQUOTA_RUNNING(mp))
67 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
68 if (XFS_IS_UQUOTA_ENFORCED(mp))
69 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
70 if (XFS_IS_GQUOTA_RUNNING(mp))
71 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
72 if (XFS_IS_GQUOTA_ENFORCED(mp))
73 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
74 if (XFS_IS_PQUOTA_RUNNING(mp))
75 state->s_state[PRJQUOTA].flags |= QCI_ACCT_ENABLED;
76 if (XFS_IS_PQUOTA_ENFORCED(mp))
77 state->s_state[PRJQUOTA].flags |= QCI_LIMITS_ENFORCED;
78
79 xfs_qm_fill_state(&state->s_state[USRQUOTA], mp, q->qi_uquotaip,
80 mp->m_sb.sb_uquotino);
81 xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
82 mp->m_sb.sb_gquotino);
83 xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
84 mp->m_sb.sb_pquotino);
85 return 0;
86}
87
Christoph Hellwigfcafb712009-02-09 08:47:34 +010088STATIC int
89xfs_quota_type(int type)
90{
91 switch (type) {
92 case USRQUOTA:
93 return XFS_DQ_USER;
94 case GRPQUOTA:
95 return XFS_DQ_GROUP;
96 default:
97 return XFS_DQ_PROJ;
98 }
99}
100
Jan Karac14cad92014-12-16 13:07:45 +0100101#define XFS_QC_SETINFO_MASK (QC_TIMER_MASK | QC_WARNS_MASK)
102
103/*
104 * Adjust quota timers & warnings
105 */
106static int
107xfs_fs_set_info(
108 struct super_block *sb,
109 int type,
110 struct qc_info *info)
111{
112 struct xfs_mount *mp = XFS_M(sb);
113 struct qc_dqblk newlim;
114
David Howellsbc98a422017-07-17 08:45:34 +0100115 if (sb_rdonly(sb))
Jan Karac14cad92014-12-16 13:07:45 +0100116 return -EROFS;
117 if (!XFS_IS_QUOTA_RUNNING(mp))
118 return -ENOSYS;
119 if (!XFS_IS_QUOTA_ON(mp))
120 return -ESRCH;
121 if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
122 return -EINVAL;
123 if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
124 return 0;
125
126 newlim.d_fieldmask = info->i_fieldmask;
127 newlim.d_spc_timer = info->i_spc_timelimit;
128 newlim.d_ino_timer = info->i_ino_timelimit;
129 newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
130 newlim.d_ino_warns = info->i_ino_warnlimit;
131 newlim.d_spc_warns = info->i_spc_warnlimit;
132 newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
133
134 return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
135}
136
Jan Kara38e478c2014-10-08 15:56:21 +0200137static unsigned int
138xfs_quota_flags(unsigned int uflags)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100139{
Jan Kara38e478c2014-10-08 15:56:21 +0200140 unsigned int flags = 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100141
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200142 if (uflags & FS_QUOTA_UDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100143 flags |= XFS_UQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200144 if (uflags & FS_QUOTA_PDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100145 flags |= XFS_PQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200146 if (uflags & FS_QUOTA_GDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100147 flags |= XFS_GQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200148 if (uflags & FS_QUOTA_UDQ_ENFD)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100149 flags |= XFS_UQUOTA_ENFD;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500150 if (uflags & FS_QUOTA_GDQ_ENFD)
151 flags |= XFS_GQUOTA_ENFD;
152 if (uflags & FS_QUOTA_PDQ_ENFD)
153 flags |= XFS_PQUOTA_ENFD;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100154
Jan Kara38e478c2014-10-08 15:56:21 +0200155 return flags;
156}
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100157
Jan Kara38e478c2014-10-08 15:56:21 +0200158STATIC int
159xfs_quota_enable(
160 struct super_block *sb,
161 unsigned int uflags)
162{
163 struct xfs_mount *mp = XFS_M(sb);
164
David Howellsbc98a422017-07-17 08:45:34 +0100165 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200166 return -EROFS;
167 if (!XFS_IS_QUOTA_RUNNING(mp))
168 return -ENOSYS;
169
170 return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
171}
172
173STATIC int
174xfs_quota_disable(
175 struct super_block *sb,
176 unsigned int uflags)
177{
178 struct xfs_mount *mp = XFS_M(sb);
179
David Howellsbc98a422017-07-17 08:45:34 +0100180 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200181 return -EROFS;
182 if (!XFS_IS_QUOTA_RUNNING(mp))
183 return -ENOSYS;
184 if (!XFS_IS_QUOTA_ON(mp))
185 return -EINVAL;
186
187 return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100188}
189
190STATIC int
Eric Sandeen9da93f92014-05-05 17:25:50 +1000191xfs_fs_rm_xquota(
192 struct super_block *sb,
193 unsigned int uflags)
194{
195 struct xfs_mount *mp = XFS_M(sb);
196 unsigned int flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +1000197
David Howellsbc98a422017-07-17 08:45:34 +0100198 if (sb_rdonly(sb))
Eric Sandeen9da93f92014-05-05 17:25:50 +1000199 return -EROFS;
200
201 if (XFS_IS_QUOTA_ON(mp))
202 return -EINVAL;
203
204 if (uflags & FS_USER_QUOTA)
205 flags |= XFS_DQ_USER;
206 if (uflags & FS_GROUP_QUOTA)
207 flags |= XFS_DQ_GROUP;
Jie Liu74dc93a2014-07-24 21:27:17 +1000208 if (uflags & FS_PROJ_QUOTA)
Eric Sandeen9da93f92014-05-05 17:25:50 +1000209 flags |= XFS_DQ_PROJ;
210
Dave Chinner24513372014-06-25 14:58:08 +1000211 return xfs_qm_scall_trunc_qfiles(mp, flags);
212}
Eric Sandeen9da93f92014-05-05 17:25:50 +1000213
214STATIC int
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400215xfs_fs_get_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100216 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700217 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200218 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100219{
220 struct xfs_mount *mp = XFS_M(sb);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100221 xfs_dqid_t id;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100222
223 if (!XFS_IS_QUOTA_RUNNING(mp))
224 return -ENOSYS;
225 if (!XFS_IS_QUOTA_ON(mp))
226 return -ESRCH;
227
Eric Sandeen296c24e2016-02-08 11:27:38 +1100228 id = from_kqid(&init_user_ns, qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700229 return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100230}
231
232/* Return quota info for active quota >= this qid */
233STATIC int
234xfs_fs_get_nextdqblk(
235 struct super_block *sb,
236 struct kqid *qid,
237 struct qc_dqblk *qdq)
238{
239 int ret;
240 struct xfs_mount *mp = XFS_M(sb);
241 xfs_dqid_t id;
242
243 if (!XFS_IS_QUOTA_RUNNING(mp))
244 return -ENOSYS;
245 if (!XFS_IS_QUOTA_ON(mp))
246 return -ESRCH;
247
248 id = from_kqid(&init_user_ns, *qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700249 ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
250 qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100251 if (ret)
252 return ret;
253
254 /* ID may be different, so convert back what we got */
255 *qid = make_kqid(current_user_ns(), qid->type, id);
256 return 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100257}
258
259STATIC int
Christoph Hellwigc472b432010-05-06 17:05:17 -0400260xfs_fs_set_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100261 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700262 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200263 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100264{
265 struct xfs_mount *mp = XFS_M(sb);
266
David Howellsbc98a422017-07-17 08:45:34 +0100267 if (sb_rdonly(sb))
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100268 return -EROFS;
269 if (!XFS_IS_QUOTA_RUNNING(mp))
270 return -ENOSYS;
271 if (!XFS_IS_QUOTA_ON(mp))
272 return -ESRCH;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100273
Dave Chinner24513372014-06-25 14:58:08 +1000274 return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
Jan Kara14bf61f2014-10-09 16:03:13 +0200275 xfs_quota_type(qid.type), qdq);
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100276}
277
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700278const struct quotactl_ops xfs_quotactl_operations = {
Jan Kara5d3684c2014-11-19 12:03:59 +0100279 .get_state = xfs_fs_get_quota_state,
Jan Karac14cad92014-12-16 13:07:45 +0100280 .set_info = xfs_fs_set_info,
Jan Kara38e478c2014-10-08 15:56:21 +0200281 .quota_enable = xfs_quota_enable,
282 .quota_disable = xfs_quota_disable,
Eric Sandeen9da93f92014-05-05 17:25:50 +1000283 .rm_xquota = xfs_fs_rm_xquota,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400284 .get_dqblk = xfs_fs_get_dqblk,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100285 .get_nextdqblk = xfs_fs_get_nextdqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -0400286 .set_dqblk = xfs_fs_set_dqblk,
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100287};