blob: 07989bd677289abab2514174508831bcb54e9de1 [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,
Eric Sandeene8503012020-05-21 13:07:01 -070024 xfs_ino_t ino,
25 struct xfs_def_quota *defq)
Jan Kara5d3684c2014-11-19 12:03:59 +010026{
Eric Sandeen8d077f52020-05-21 13:07:00 -070027 bool tempqip = false;
Jan Kara5d3684c2014-11-19 12:03:59 +010028
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;
Christoph Hellwig6e73a542021-03-29 11:11:40 -070038 tstate->blocks = ip->i_nblocks;
Christoph Hellwigdaf83962020-05-18 10:27:22 -070039 tstate->nextents = ip->i_df.if_nextents;
Darrick J. Wong438769e2020-07-14 10:37:32 -070040 tstate->spc_timelimit = (u32)defq->blk.time;
41 tstate->ino_timelimit = (u32)defq->ino.time;
42 tstate->rt_spc_timelimit = (u32)defq->rtb.time;
43 tstate->spc_warnlimit = defq->blk.warn;
44 tstate->ino_warnlimit = defq->ino.warn;
45 tstate->rt_spc_warnlimit = defq->rtb.warn;
Jan Kara5d3684c2014-11-19 12:03:59 +010046 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));
Christoph Hellwig149e53a2021-08-06 11:05:37 -070063 if (!XFS_IS_QUOTA_ON(mp))
Jan Kara5d3684c2014-11-19 12:03:59 +010064 return 0;
65 state->s_incoredqs = q->qi_dquots;
Christoph Hellwig149e53a2021-08-06 11:05:37 -070066 if (XFS_IS_UQUOTA_ON(mp))
Jan Kara5d3684c2014-11-19 12:03:59 +010067 state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED;
68 if (XFS_IS_UQUOTA_ENFORCED(mp))
69 state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
Christoph Hellwig149e53a2021-08-06 11:05:37 -070070 if (XFS_IS_GQUOTA_ON(mp))
Jan Kara5d3684c2014-11-19 12:03:59 +010071 state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED;
72 if (XFS_IS_GQUOTA_ENFORCED(mp))
73 state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
Christoph Hellwig149e53a2021-08-06 11:05:37 -070074 if (XFS_IS_PQUOTA_ON(mp))
Jan Kara5d3684c2014-11-19 12:03:59 +010075 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,
Eric Sandeene8503012020-05-21 13:07:01 -070080 mp->m_sb.sb_uquotino, &q->qi_usr_default);
Jan Kara5d3684c2014-11-19 12:03:59 +010081 xfs_qm_fill_state(&state->s_state[GRPQUOTA], mp, q->qi_gquotaip,
Eric Sandeene8503012020-05-21 13:07:01 -070082 mp->m_sb.sb_gquotino, &q->qi_grp_default);
Jan Kara5d3684c2014-11-19 12:03:59 +010083 xfs_qm_fill_state(&state->s_state[PRJQUOTA], mp, q->qi_pquotaip,
Eric Sandeene8503012020-05-21 13:07:01 -070084 mp->m_sb.sb_pquotino, &q->qi_prj_default);
Jan Kara5d3684c2014-11-19 12:03:59 +010085 return 0;
86}
87
Darrick J. Wong1a7ed272020-07-15 17:53:43 -070088STATIC xfs_dqtype_t
Christoph Hellwigfcafb712009-02-09 08:47:34 +010089xfs_quota_type(int type)
90{
91 switch (type) {
92 case USRQUOTA:
Darrick J. Wong8cd49012020-07-15 17:42:36 -070093 return XFS_DQTYPE_USER;
Christoph Hellwigfcafb712009-02-09 08:47:34 +010094 case GRPQUOTA:
Darrick J. Wong8cd49012020-07-15 17:42:36 -070095 return XFS_DQTYPE_GROUP;
Christoph Hellwigfcafb712009-02-09 08:47:34 +010096 default:
Darrick J. Wong8cd49012020-07-15 17:42:36 -070097 return XFS_DQTYPE_PROJ;
Christoph Hellwigfcafb712009-02-09 08:47:34 +010098 }
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{
Eric Sandeen8d077f52020-05-21 13:07:00 -0700112 struct xfs_mount *mp = XFS_M(sb);
113 struct qc_dqblk newlim;
Jan Karac14cad92014-12-16 13:07:45 +0100114
David Howellsbc98a422017-07-17 08:45:34 +0100115 if (sb_rdonly(sb))
Jan Karac14cad92014-12-16 13:07:45 +0100116 return -EROFS;
Jan Karac14cad92014-12-16 13:07:45 +0100117 if (!XFS_IS_QUOTA_ON(mp))
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700118 return -ENOSYS;
Jan Karac14cad92014-12-16 13:07:45 +0100119 if (info->i_fieldmask & ~XFS_QC_SETINFO_MASK)
120 return -EINVAL;
121 if ((info->i_fieldmask & XFS_QC_SETINFO_MASK) == 0)
122 return 0;
123
124 newlim.d_fieldmask = info->i_fieldmask;
125 newlim.d_spc_timer = info->i_spc_timelimit;
126 newlim.d_ino_timer = info->i_ino_timelimit;
127 newlim.d_rt_spc_timer = info->i_rt_spc_timelimit;
128 newlim.d_ino_warns = info->i_ino_warnlimit;
129 newlim.d_spc_warns = info->i_spc_warnlimit;
130 newlim.d_rt_spc_warns = info->i_rt_spc_warnlimit;
131
132 return xfs_qm_scall_setqlim(mp, 0, xfs_quota_type(type), &newlim);
133}
134
Jan Kara38e478c2014-10-08 15:56:21 +0200135static unsigned int
136xfs_quota_flags(unsigned int uflags)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100137{
Jan Kara38e478c2014-10-08 15:56:21 +0200138 unsigned int flags = 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100139
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200140 if (uflags & FS_QUOTA_UDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100141 flags |= XFS_UQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200142 if (uflags & FS_QUOTA_PDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100143 flags |= XFS_PQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200144 if (uflags & FS_QUOTA_GDQ_ACCT)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100145 flags |= XFS_GQUOTA_ACCT;
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200146 if (uflags & FS_QUOTA_UDQ_ENFD)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100147 flags |= XFS_UQUOTA_ENFD;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500148 if (uflags & FS_QUOTA_GDQ_ENFD)
149 flags |= XFS_GQUOTA_ENFD;
150 if (uflags & FS_QUOTA_PDQ_ENFD)
151 flags |= XFS_PQUOTA_ENFD;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100152
Jan Kara38e478c2014-10-08 15:56:21 +0200153 return flags;
154}
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100155
Jan Kara38e478c2014-10-08 15:56:21 +0200156STATIC int
157xfs_quota_enable(
158 struct super_block *sb,
159 unsigned int uflags)
160{
161 struct xfs_mount *mp = XFS_M(sb);
162
David Howellsbc98a422017-07-17 08:45:34 +0100163 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200164 return -EROFS;
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700165 if (!XFS_IS_QUOTA_ON(mp))
Jan Kara38e478c2014-10-08 15:56:21 +0200166 return -ENOSYS;
167
168 return xfs_qm_scall_quotaon(mp, xfs_quota_flags(uflags));
169}
170
171STATIC int
172xfs_quota_disable(
173 struct super_block *sb,
174 unsigned int uflags)
175{
176 struct xfs_mount *mp = XFS_M(sb);
177
David Howellsbc98a422017-07-17 08:45:34 +0100178 if (sb_rdonly(sb))
Jan Kara38e478c2014-10-08 15:56:21 +0200179 return -EROFS;
Jan Kara38e478c2014-10-08 15:56:21 +0200180 if (!XFS_IS_QUOTA_ON(mp))
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700181 return -ENOSYS;
Jan Kara38e478c2014-10-08 15:56:21 +0200182
183 return xfs_qm_scall_quotaoff(mp, xfs_quota_flags(uflags));
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100184}
185
186STATIC int
Eric Sandeen9da93f92014-05-05 17:25:50 +1000187xfs_fs_rm_xquota(
188 struct super_block *sb,
189 unsigned int uflags)
190{
191 struct xfs_mount *mp = XFS_M(sb);
192 unsigned int flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +1000193
David Howellsbc98a422017-07-17 08:45:34 +0100194 if (sb_rdonly(sb))
Eric Sandeen9da93f92014-05-05 17:25:50 +1000195 return -EROFS;
196
197 if (XFS_IS_QUOTA_ON(mp))
198 return -EINVAL;
199
Jan Kara3dd4d402019-10-23 17:00:45 -0700200 if (uflags & ~(FS_USER_QUOTA | FS_GROUP_QUOTA | FS_PROJ_QUOTA))
201 return -EINVAL;
202
Eric Sandeen9da93f92014-05-05 17:25:50 +1000203 if (uflags & FS_USER_QUOTA)
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700204 flags |= XFS_QMOPT_UQUOTA;
Eric Sandeen9da93f92014-05-05 17:25:50 +1000205 if (uflags & FS_GROUP_QUOTA)
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700206 flags |= XFS_QMOPT_GQUOTA;
Jie Liu74dc93a2014-07-24 21:27:17 +1000207 if (uflags & FS_PROJ_QUOTA)
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700208 flags |= XFS_QMOPT_PQUOTA;
Eric Sandeen9da93f92014-05-05 17:25:50 +1000209
Dave Chinner24513372014-06-25 14:58:08 +1000210 return xfs_qm_scall_trunc_qfiles(mp, flags);
211}
Eric Sandeen9da93f92014-05-05 17:25:50 +1000212
213STATIC int
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400214xfs_fs_get_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100215 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700216 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200217 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100218{
219 struct xfs_mount *mp = XFS_M(sb);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100220 xfs_dqid_t id;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100221
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100222 if (!XFS_IS_QUOTA_ON(mp))
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700223 return -ENOSYS;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100224
Eric Sandeen296c24e2016-02-08 11:27:38 +1100225 id = from_kqid(&init_user_ns, qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700226 return xfs_qm_scall_getquota(mp, id, xfs_quota_type(qid.type), qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100227}
228
229/* Return quota info for active quota >= this qid */
230STATIC int
231xfs_fs_get_nextdqblk(
232 struct super_block *sb,
233 struct kqid *qid,
234 struct qc_dqblk *qdq)
235{
236 int ret;
237 struct xfs_mount *mp = XFS_M(sb);
238 xfs_dqid_t id;
239
Eric Sandeen296c24e2016-02-08 11:27:38 +1100240 if (!XFS_IS_QUOTA_ON(mp))
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700241 return -ENOSYS;
Eric Sandeen296c24e2016-02-08 11:27:38 +1100242
243 id = from_kqid(&init_user_ns, *qid);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700244 ret = xfs_qm_scall_getquota_next(mp, &id, xfs_quota_type(qid->type),
245 qdq);
Eric Sandeen296c24e2016-02-08 11:27:38 +1100246 if (ret)
247 return ret;
248
249 /* ID may be different, so convert back what we got */
250 *qid = make_kqid(current_user_ns(), qid->type, id);
251 return 0;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100252}
253
254STATIC int
Christoph Hellwigc472b432010-05-06 17:05:17 -0400255xfs_fs_set_dqblk(
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100256 struct super_block *sb,
Eric W. Biederman74a8a102012-09-16 02:07:49 -0700257 struct kqid qid,
Jan Kara14bf61f2014-10-09 16:03:13 +0200258 struct qc_dqblk *qdq)
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100259{
260 struct xfs_mount *mp = XFS_M(sb);
261
David Howellsbc98a422017-07-17 08:45:34 +0100262 if (sb_rdonly(sb))
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100263 return -EROFS;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100264 if (!XFS_IS_QUOTA_ON(mp))
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700265 return -ENOSYS;
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100266
Dave Chinner24513372014-06-25 14:58:08 +1000267 return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
Jan Kara14bf61f2014-10-09 16:03:13 +0200268 xfs_quota_type(qid.type), qdq);
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100269}
270
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700271const struct quotactl_ops xfs_quotactl_operations = {
Jan Kara5d3684c2014-11-19 12:03:59 +0100272 .get_state = xfs_fs_get_quota_state,
Jan Karac14cad92014-12-16 13:07:45 +0100273 .set_info = xfs_fs_set_info,
Jan Kara38e478c2014-10-08 15:56:21 +0200274 .quota_enable = xfs_quota_enable,
275 .quota_disable = xfs_quota_disable,
Eric Sandeen9da93f92014-05-05 17:25:50 +1000276 .rm_xquota = xfs_fs_rm_xquota,
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400277 .get_dqblk = xfs_fs_get_dqblk,
Eric Sandeen296c24e2016-02-08 11:27:38 +1100278 .get_nextdqblk = xfs_fs_get_nextdqblk,
Christoph Hellwigc472b432010-05-06 17:05:17 -0400279 .set_dqblk = xfs_fs_set_dqblk,
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100280};