Merge tag 'for-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/pateldipen19...
[linux-block.git] / fs / xfs / xfs_qm_syscalls.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
4ce3121f
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
16f7e0fe 6
16f7e0fe 7
1da177e4
LT
8#include "xfs.h"
9#include "xfs_fs.h"
70a9883c 10#include "xfs_shared.h"
239880ef
DC
11#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
1da177e4 14#include "xfs_sb.h"
1da177e4 15#include "xfs_mount.h"
1da177e4 16#include "xfs_inode.h"
239880ef 17#include "xfs_trans.h"
a4fbe6ab 18#include "xfs_quota.h"
1da177e4 19#include "xfs_qm.h"
6d8b79cf 20#include "xfs_icache.h"
1da177e4 21
fcafb71b 22int
1da177e4
LT
23xfs_qm_scall_quotaoff(
24 xfs_mount_t *mp,
fcafb71b 25 uint flags)
1da177e4 26{
1da177e4
LT
27 /*
28 * No file system can have quotas enabled on disk but not in core.
29 * Note that quota utilities (like quotaoff) _expect_
2451337d 30 * errno == -EEXIST here.
1da177e4
LT
31 */
32 if ((mp->m_qflags & flags) == 0)
2451337d 33 return -EEXIST;
1da177e4
LT
34
35 /*
40b52225
CH
36 * We do not support actually turning off quota accounting any more.
37 * Just log a warning and ignore the accounting related flags.
1da177e4 38 */
40b52225
CH
39 if (flags & XFS_ALL_QUOTA_ACCT)
40 xfs_info(mp, "disabling of quota accounting not supported.");
1da177e4 41
40b52225
CH
42 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
43 mp->m_qflags &= ~(flags & XFS_ALL_QUOTA_ENFD);
44 spin_lock(&mp->m_sb_lock);
45 mp->m_sb.sb_qflags = mp->m_qflags;
46 spin_unlock(&mp->m_sb_lock);
47 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 48
40b52225
CH
49 /* XXX what to do if error ? Revert back to old vals incore ? */
50 return xfs_sync_sb(mp, false);
1da177e4
LT
51}
52
5d18898b
CH
53STATIC int
54xfs_qm_scall_trunc_qfile(
55 struct xfs_mount *mp,
56 xfs_ino_t ino)
57{
58 struct xfs_inode *ip;
59 struct xfs_trans *tp;
60 int error;
61
62 if (ino == NULLFSINO)
63 return 0;
64
65 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
66 if (error)
67 return error;
68
69 xfs_ilock(ip, XFS_IOLOCK_EXCL);
70
253f4911 71 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
5d18898b 72 if (error) {
5d18898b
CH
73 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
74 goto out_put;
75 }
76
77 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 78 xfs_trans_ijoin(tp, ip, 0);
5d18898b 79
13d2c10b 80 ip->i_disk_size = 0;
673e8e59
CH
81 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
82
83 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
5d18898b 84 if (error) {
4906e215 85 xfs_trans_cancel(tp);
5d18898b
CH
86 goto out_unlock;
87 }
88
daf83964 89 ASSERT(ip->i_df.if_nextents == 0);
673e8e59 90
dcd79a14 91 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
70393313 92 error = xfs_trans_commit(tp);
5d18898b
CH
93
94out_unlock:
95 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
96out_put:
44a8736b 97 xfs_irele(ip);
5d18898b
CH
98 return error;
99}
100
fcafb71b 101int
1da177e4
LT
102xfs_qm_scall_trunc_qfiles(
103 xfs_mount_t *mp,
104 uint flags)
105{
2451337d 106 int error = -EINVAL;
1da177e4 107
38c26bfd 108 if (!xfs_has_quota(mp) || flags == 0 ||
41ed4a5f 109 (flags & ~XFS_QMOPT_QUOTALL)) {
08e96e1a 110 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
8221112b 111 __func__, flags, mp->m_qflags);
2451337d 112 return -EINVAL;
1da177e4
LT
113 }
114
41ed4a5f 115 if (flags & XFS_QMOPT_UQUOTA) {
5d18898b 116 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
c61a9e39
JL
117 if (error)
118 return error;
119 }
41ed4a5f 120 if (flags & XFS_QMOPT_GQUOTA) {
c61a9e39
JL
121 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
122 if (error)
123 return error;
124 }
41ed4a5f 125 if (flags & XFS_QMOPT_PQUOTA)
c61a9e39 126 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
1da177e4 127
c61a9e39 128 return error;
1da177e4
LT
129}
130
1da177e4
LT
131/*
132 * Switch on (a given) quota enforcement for a filesystem. This takes
133 * effect immediately.
134 * (Switching on quota accounting must be done at mount time.)
135 */
fcafb71b 136int
1da177e4
LT
137xfs_qm_scall_quotaon(
138 xfs_mount_t *mp,
139 uint flags)
140{
141 int error;
1da177e4 142 uint qf;
1da177e4 143
1da177e4 144 /*
cd594559
KX
145 * Switching on quota accounting must be done at mount time,
146 * only consider quota enforcement stuff here.
1da177e4 147 */
cd594559 148 flags &= XFS_ALL_QUOTA_ENFD;
1da177e4 149
1da177e4 150 if (flags == 0) {
08e96e1a 151 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
8221112b 152 __func__, mp->m_qflags);
2451337d 153 return -EINVAL;
1da177e4
LT
154 }
155
1da177e4
LT
156 /*
157 * Can't enforce without accounting. We check the superblock
158 * qflags here instead of m_qflags because rootfs can have
159 * quota acct on ondisk without m_qflags' knowing.
160 */
fbf64b3d 161 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
83e782e1 162 (flags & XFS_UQUOTA_ENFD)) ||
fbf64b3d 163 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
83e782e1 164 (flags & XFS_GQUOTA_ENFD)) ||
fbf64b3d 165 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
83e782e1 166 (flags & XFS_PQUOTA_ENFD))) {
8221112b 167 xfs_debug(mp,
08e96e1a 168 "%s: Can't enforce without acct, flags=%x sbflags=%x",
8221112b 169 __func__, flags, mp->m_sb.sb_qflags);
2451337d 170 return -EINVAL;
1da177e4
LT
171 }
172 /*
25985edc 173 * If everything's up to-date incore, then don't waste time.
1da177e4
LT
174 */
175 if ((mp->m_qflags & flags) == flags)
2451337d 176 return -EEXIST;
1da177e4
LT
177
178 /*
179 * Change sb_qflags on disk but not incore mp->qflags
180 * if this is the root filesystem.
181 */
3685c2a1 182 spin_lock(&mp->m_sb_lock);
1da177e4
LT
183 qf = mp->m_sb.sb_qflags;
184 mp->m_sb.sb_qflags = qf | flags;
3685c2a1 185 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
186
187 /*
188 * There's nothing to change if it's the same.
189 */
4d11a402 190 if ((qf & flags) == flags)
2451337d 191 return -EEXIST;
1da177e4 192
61e63ecb
DC
193 error = xfs_sync_sb(mp, false);
194 if (error)
d99831ff 195 return error;
1da177e4
LT
196 /*
197 * If we aren't trying to switch on quota enforcement, we are done.
198 */
199 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
200 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
c8ad20ff
NS
201 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
202 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
203 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
fbf64b3d 204 (mp->m_qflags & XFS_GQUOTA_ACCT)))
d99831ff 205 return 0;
1da177e4 206
149e53af 207 if (!XFS_IS_QUOTA_ON(mp))
2451337d 208 return -ESRCH;
1da177e4
LT
209
210 /*
211 * Switch on quota enforcement in core.
212 */
8a7b8a89 213 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 214 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
8a7b8a89 215 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 216
d99831ff 217 return 0;
1da177e4
LT
218}
219
5349b2af 220#define XFS_QC_MASK (QC_LIMIT_MASK | QC_TIMER_MASK)
c472b432 221
d1520dea
DW
222/*
223 * Adjust limits of this quota, and the defaults if passed in. Returns true
224 * if the new limits made sense and were applied, false otherwise.
225 */
226static inline bool
227xfs_setqlim_limits(
228 struct xfs_mount *mp,
229 struct xfs_dquot_res *res,
230 struct xfs_quota_limits *qlim,
231 xfs_qcnt_t hard,
232 xfs_qcnt_t soft,
233 const char *tag)
234{
235 /* The hard limit can't be less than the soft limit. */
236 if (hard != 0 && hard < soft) {
237 xfs_debug(mp, "%shard %lld < %ssoft %lld", tag, hard, tag,
238 soft);
239 return false;
240 }
241
242 res->hardlimit = hard;
243 res->softlimit = soft;
244 if (qlim) {
245 qlim->hard = hard;
246 qlim->soft = soft;
247 }
248
249 return true;
250}
251
d1520dea
DW
252static inline void
253xfs_setqlim_timer(
11d8a919 254 struct xfs_mount *mp,
d1520dea
DW
255 struct xfs_dquot_res *res,
256 struct xfs_quota_limits *qlim,
257 s64 timer)
258{
11d8a919
DW
259 if (qlim) {
260 /* Set the length of the default grace period. */
ccc8e771
DW
261 res->timer = xfs_dquot_set_grace_period(timer);
262 qlim->time = res->timer;
11d8a919
DW
263 } else {
264 /* Set the grace period expiration on a quota. */
265 res->timer = xfs_dquot_set_timeout(mp, timer);
266 }
d1520dea
DW
267}
268
1da177e4
LT
269/*
270 * Adjust quota limits, and start/stop timers accordingly.
271 */
fcafb71b 272int
1da177e4 273xfs_qm_scall_setqlim(
b1366451 274 struct xfs_mount *mp,
1da177e4 275 xfs_dqid_t id,
1a7ed271 276 xfs_dqtype_t type,
14bf61ff 277 struct qc_dqblk *newlim)
1da177e4 278{
8a7b8a89 279 struct xfs_quotainfo *q = mp->m_quotainfo;
b1366451
BF
280 struct xfs_dquot *dqp;
281 struct xfs_trans *tp;
be607946 282 struct xfs_def_quota *defq;
d1520dea
DW
283 struct xfs_dquot_res *res;
284 struct xfs_quota_limits *qlim;
1da177e4
LT
285 int error;
286 xfs_qcnt_t hard, soft;
287
14bf61ff 288 if (newlim->d_fieldmask & ~XFS_QC_MASK)
2451337d 289 return -EINVAL;
14bf61ff 290 if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
c472b432 291 return 0;
1da177e4 292
1da177e4 293 /*
f648167f
DC
294 * Get the dquot (locked) before we start, as we need to do a
295 * transaction to allocate it if it doesn't exist. Once we have the
296 * dquot, unlock it so we can start the next transaction safely. We hold
297 * a reference to the dquot, so it's safe to do this unlock/lock without
298 * it being reclaimed in the mean time.
1da177e4 299 */
30ab2dcf 300 error = xfs_qm_dqget(mp, id, type, true, &dqp);
f648167f 301 if (error) {
2451337d 302 ASSERT(error != -ENOENT);
59d7fab2 303 return error;
1da177e4 304 }
be607946 305
ce6e7e79 306 defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
f648167f
DC
307 xfs_dqunlock(dqp);
308
253f4911
CH
309 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
310 if (error)
f648167f 311 goto out_rele;
f648167f
DC
312
313 xfs_dqlock(dqp);
1da177e4 314 xfs_trans_dqjoin(tp, dqp);
1da177e4
LT
315
316 /*
d1520dea
DW
317 * Update quota limits, warnings, and timers, and the defaults
318 * if we're touching id == 0.
319 *
1da177e4 320 * Make sure that hardlimits are >= soft limits before changing.
d1520dea
DW
321 *
322 * Update warnings counter(s) if requested.
323 *
324 * Timelimits for the super user set the relative time the other users
325 * can be over quota for this file system. If it is zero a default is
326 * used. Ditto for the default soft and hard limit values (already
327 * done, above), and for warnings.
328 *
329 * For other IDs, userspace can bump out the grace period if over
330 * the soft limit.
1da177e4 331 */
d1520dea
DW
332
333 /* Blocks on the data device. */
14bf61ff
JK
334 hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
335 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
d3537cf9 336 dqp->q_blk.hardlimit;
14bf61ff
JK
337 soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
338 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
d3537cf9 339 dqp->q_blk.softlimit;
d1520dea
DW
340 res = &dqp->q_blk;
341 qlim = id == 0 ? &defq->blk : NULL;
342
343 if (xfs_setqlim_limits(mp, res, qlim, hard, soft, "blk"))
b1366451 344 xfs_dquot_set_prealloc_limits(dqp);
d1520dea 345 if (newlim->d_fieldmask & QC_SPC_TIMER)
11d8a919 346 xfs_setqlim_timer(mp, res, qlim, newlim->d_spc_timer);
d1520dea
DW
347
348 /* Blocks on the realtime device. */
14bf61ff
JK
349 hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
350 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
d3537cf9 351 dqp->q_rtb.hardlimit;
14bf61ff
JK
352 soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
353 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
d3537cf9 354 dqp->q_rtb.softlimit;
d1520dea
DW
355 res = &dqp->q_rtb;
356 qlim = id == 0 ? &defq->rtb : NULL;
357
358 xfs_setqlim_limits(mp, res, qlim, hard, soft, "rtb");
d1520dea 359 if (newlim->d_fieldmask & QC_RT_SPC_TIMER)
11d8a919 360 xfs_setqlim_timer(mp, res, qlim, newlim->d_rt_spc_timer);
1da177e4 361
d1520dea 362 /* Inodes */
14bf61ff 363 hard = (newlim->d_fieldmask & QC_INO_HARD) ?
1da177e4 364 (xfs_qcnt_t) newlim->d_ino_hardlimit :
d3537cf9 365 dqp->q_ino.hardlimit;
14bf61ff 366 soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
1da177e4 367 (xfs_qcnt_t) newlim->d_ino_softlimit :
d3537cf9 368 dqp->q_ino.softlimit;
d1520dea
DW
369 res = &dqp->q_ino;
370 qlim = id == 0 ? &defq->ino : NULL;
1da177e4 371
d1520dea 372 xfs_setqlim_limits(mp, res, qlim, hard, soft, "ino");
df42ce64 373 if (newlim->d_fieldmask & QC_INO_TIMER)
11d8a919 374 xfs_setqlim_timer(mp, res, qlim, newlim->d_ino_timer);
df42ce64
ES
375
376 if (id != 0) {
1da177e4
LT
377 /*
378 * If the user is now over quota, start the timelimit.
379 * The user will not be 'warned'.
380 * Note that we keep the timers ticking, whether enforcement
381 * is on or off. We don't really want to bother with iterating
382 * over all ondisk dquots and turning the timers on/off.
383 */
c8c753e1 384 xfs_qm_adjust_dqtimers(dqp);
1da177e4 385 }
985a78fd 386 dqp->q_flags |= XFS_DQFLAG_DIRTY;
1da177e4
LT
387 xfs_trans_log_dquot(tp, dqp);
388
70393313 389 error = xfs_trans_commit(tp);
1da177e4 390
f648167f
DC
391out_rele:
392 xfs_qm_dqrele(dqp);
e5720eec 393 return error;
1da177e4
LT
394}
395
2e330e76
DW
396/* Fill out the quota context. */
397static void
398xfs_qm_scall_getquota_fill_qc(
18535a7e 399 struct xfs_mount *mp,
1a7ed271 400 xfs_dqtype_t type,
2e330e76
DW
401 const struct xfs_dquot *dqp,
402 struct qc_dqblk *dst)
1da177e4
LT
403{
404 memset(dst, 0, sizeof(*dst));
d3537cf9
DW
405 dst->d_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_blk.hardlimit);
406 dst->d_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_blk.softlimit);
19dce7ea
DW
407 dst->d_ino_hardlimit = dqp->q_ino.hardlimit;
408 dst->d_ino_softlimit = dqp->q_ino.softlimit;
784e80f5
DW
409 dst->d_space = XFS_FSB_TO_B(mp, dqp->q_blk.reserved);
410 dst->d_ino_count = dqp->q_ino.reserved;
19dce7ea
DW
411 dst->d_spc_timer = dqp->q_blk.timer;
412 dst->d_ino_timer = dqp->q_ino.timer;
2e06df55
CH
413 dst->d_ino_warns = 0;
414 dst->d_spc_warns = 0;
d3537cf9
DW
415 dst->d_rt_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.hardlimit);
416 dst->d_rt_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.softlimit);
784e80f5 417 dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_rtb.reserved);
19dce7ea 418 dst->d_rt_spc_timer = dqp->q_rtb.timer;
2e06df55 419 dst->d_rt_spc_warns = 0;
1da177e4
LT
420
421 /*
422 * Internally, we don't reset all the timers when quota enforcement
c41564b5 423 * gets turned off. No need to confuse the user level code,
1da177e4
LT
424 * so return zeroes in that case.
425 */
dbcbc7b9 426 if (!xfs_dquot_is_enforced(dqp)) {
14bf61ff
JK
427 dst->d_spc_timer = 0;
428 dst->d_ino_timer = 0;
429 dst->d_rt_spc_timer = 0;
1da177e4
LT
430 }
431
432#ifdef DEBUG
dbcbc7b9 433 if (xfs_dquot_is_enforced(dqp) && dqp->q_id != 0) {
14bf61ff
JK
434 if ((dst->d_space > dst->d_spc_softlimit) &&
435 (dst->d_spc_softlimit > 0)) {
436 ASSERT(dst->d_spc_timer != 0);
1da177e4 437 }
d3537cf9
DW
438 if ((dst->d_ino_count > dqp->q_ino.softlimit) &&
439 (dqp->q_ino.softlimit > 0)) {
14bf61ff 440 ASSERT(dst->d_ino_timer != 0);
1da177e4
LT
441 }
442 }
443#endif
2e330e76
DW
444}
445
446/* Return the quota information for the dquot matching id. */
447int
448xfs_qm_scall_getquota(
449 struct xfs_mount *mp,
450 xfs_dqid_t id,
1a7ed271 451 xfs_dqtype_t type,
2e330e76
DW
452 struct qc_dqblk *dst)
453{
454 struct xfs_dquot *dqp;
455 int error;
456
5e672cd6
DC
457 /*
458 * Expedite pending inodegc work at the start of a quota reporting
459 * scan but don't block waiting for it to complete.
460 */
01e8f379 461 if (id == 0)
5e672cd6 462 xfs_inodegc_push(mp);
01e8f379 463
2e330e76 464 /*
30ab2dcf
DW
465 * Try to get the dquot. We don't want it allocated on disk, so don't
466 * set doalloc. If it doesn't exist, we'll get ENOENT back.
2e330e76 467 */
30ab2dcf 468 error = xfs_qm_dqget(mp, id, type, false, &dqp);
2e330e76
DW
469 if (error)
470 return error;
471
472 /*
473 * If everything's NULL, this dquot doesn't quite exist as far as
474 * our utility programs are concerned.
475 */
476 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
477 error = -ENOENT;
478 goto out_put;
479 }
480
481 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
482
18535a7e
CH
483out_put:
484 xfs_qm_dqput(dqp);
485 return error;
1da177e4
LT
486}
487
2e330e76
DW
488/*
489 * Return the quota information for the first initialized dquot whose id
490 * is at least as high as id.
491 */
492int
493xfs_qm_scall_getquota_next(
494 struct xfs_mount *mp,
495 xfs_dqid_t *id,
1a7ed271 496 xfs_dqtype_t type,
2e330e76
DW
497 struct qc_dqblk *dst)
498{
499 struct xfs_dquot *dqp;
500 int error;
501
01e8f379
DW
502 /* Flush inodegc work at the start of a quota reporting scan. */
503 if (*id == 0)
5e672cd6 504 xfs_inodegc_push(mp);
01e8f379 505
2e330e76
DW
506 error = xfs_qm_dqget_next(mp, *id, type, &dqp);
507 if (error)
508 return error;
509
510 /* Fill in the ID we actually read from disk */
c51df733 511 *id = dqp->q_id;
2e330e76
DW
512
513 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
514
515 xfs_qm_dqput(dqp);
516 return error;
517}