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