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