blob: 47fe60e1a887319f2751b3a1a1e46677f2c13bda [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott4ce31212005-11-02 14:59:41 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08006
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "xfs.h"
9#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110010#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_trans.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110018#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_qm.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110020#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Christoph Hellwigfcafb712009-02-09 08:47:34 +010022int
Linus Torvalds1da177e2005-04-16 15:20:36 -070023xfs_qm_scall_quotaoff(
24 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010025 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 /*
28 * No file system can have quotas enabled on disk but not in core.
29 * Note that quota utilities (like quotaoff) _expect_
Dave Chinner24513372014-06-25 14:58:08 +100030 * errno == -EEXIST here.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32 if ((mp->m_qflags & flags) == 0)
Dave Chinner24513372014-06-25 14:58:08 +100033 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 /*
Christoph Hellwig40b52222021-08-06 11:05:36 -070036 * We do not support actually turning off quota accounting any more.
37 * Just log a warning and ignore the accounting related flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Christoph Hellwig40b52222021-08-06 11:05:36 -070039 if (flags & XFS_ALL_QUOTA_ACCT)
40 xfs_info(mp, "disabling of quota accounting not supported.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Christoph Hellwig40b52222021-08-06 11:05:36 -070042 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christoph Hellwig40b52222021-08-06 11:05:36 -070049 /* XXX what to do if error ? Revert back to old vals incore ? */
50 return xfs_sync_sb(mp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Christoph Hellwig5d188982010-07-20 17:51:31 +100053STATIC 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
Christoph Hellwig253f4912016-04-06 09:19:55 +100071 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +100072 if (error) {
Christoph Hellwig5d188982010-07-20 17:51:31 +100073 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
74 goto out_put;
75 }
76
77 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +000078 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +100079
Christoph Hellwig13d2c102021-03-29 11:11:40 -070080 ip->i_disk_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +000081 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
82
83 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +100084 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +100085 xfs_trans_cancel(tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +100086 goto out_unlock;
87 }
88
Christoph Hellwigdaf83962020-05-18 10:27:22 -070089 ASSERT(ip->i_df.if_nextents == 0);
Christoph Hellwig673e8e52011-12-18 20:00:04 +000090
Dave Chinnerdcd79a12010-09-28 12:27:25 +100091 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Christoph Hellwig70393312015-06-04 13:48:08 +100092 error = xfs_trans_commit(tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +100093
94out_unlock:
95 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
96out_put:
Darrick J. Wong44a87362018-07-25 12:52:32 -070097 xfs_irele(ip);
Christoph Hellwig5d188982010-07-20 17:51:31 +100098 return error;
99}
100
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100101int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102xfs_qm_scall_trunc_qfiles(
103 xfs_mount_t *mp,
104 uint flags)
105{
Dave Chinner24513372014-06-25 14:58:08 +1000106 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dave Chinner38c26bf2021-08-18 18:46:37 -0700108 if (!xfs_has_quota(mp) || flags == 0 ||
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700109 (flags & ~XFS_QMOPT_QUOTALL)) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500110 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100111 __func__, flags, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700115 if (flags & XFS_QMOPT_UQUOTA) {
Christoph Hellwig5d188982010-07-20 17:51:31 +1000116 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
Jie Liuc61a9e32013-11-22 14:04:00 +0800117 if (error)
118 return error;
119 }
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700120 if (flags & XFS_QMOPT_GQUOTA) {
Jie Liuc61a9e32013-11-22 14:04:00 +0800121 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
122 if (error)
123 return error;
124 }
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700125 if (flags & XFS_QMOPT_PQUOTA)
Jie Liuc61a9e32013-11-22 14:04:00 +0800126 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jie Liuc61a9e32013-11-22 14:04:00 +0800128 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
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 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100136int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137xfs_qm_scall_quotaon(
138 xfs_mount_t *mp,
139 uint flags)
140{
141 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 uint qf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /*
Kaixu Xiacd594552020-04-22 21:54:30 -0700145 * Switching on quota accounting must be done at mount time,
146 * only consider quota enforcement stuff here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
Kaixu Xiacd594552020-04-22 21:54:30 -0700148 flags &= XFS_ALL_QUOTA_ENFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (flags == 0) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500151 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100152 __func__, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000153 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
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 */
Jan Karafbf64b32014-10-08 11:52:52 +0200161 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500162 (flags & XFS_UQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200163 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500164 (flags & XFS_GQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200165 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500166 (flags & XFS_PQUOTA_ENFD))) {
Dave Chinner82211122011-03-07 10:07:35 +1100167 xfs_debug(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500168 "%s: Can't enforce without acct, flags=%x sbflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100169 __func__, flags, mp->m_sb.sb_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300173 * If everything's up to-date incore, then don't waste time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175 if ((mp->m_qflags & flags) == flags)
Dave Chinner24513372014-06-25 14:58:08 +1000176 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * Change sb_qflags on disk but not incore mp->qflags
180 * if this is the root filesystem.
181 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000182 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 qf = mp->m_sb.sb_qflags;
184 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000185 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /*
188 * There's nothing to change if it's the same.
189 */
Dave Chinner4d11a402015-01-22 09:10:26 +1100190 if ((qf & flags) == flags)
Dave Chinner24513372014-06-25 14:58:08 +1000191 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Dave Chinner61e63ec2015-01-22 09:10:31 +1100193 error = xfs_sync_sb(mp, false);
194 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000195 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
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)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000201 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
202 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
203 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
Jan Karafbf64b32014-10-08 11:52:52 +0200204 (mp->m_qflags & XFS_GQUOTA_ACCT)))
Eric Sandeend99831f2014-06-22 15:03:54 +1000205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700207 if (!XFS_IS_QUOTA_ON(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000208 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /*
211 * Switch on quota enforcement in core.
212 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000213 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000215 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Eric Sandeend99831f2014-06-22 15:03:54 +1000217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Jan Kara14bf61f2014-10-09 16:03:13 +0200220#define XFS_QC_MASK \
221 (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
Christoph Hellwigc472b432010-05-06 17:05:17 -0400222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700224 * Adjust limits of this quota, and the defaults if passed in. Returns true
225 * if the new limits made sense and were applied, false otherwise.
226 */
227static inline bool
228xfs_setqlim_limits(
229 struct xfs_mount *mp,
230 struct xfs_dquot_res *res,
231 struct xfs_quota_limits *qlim,
232 xfs_qcnt_t hard,
233 xfs_qcnt_t soft,
234 const char *tag)
235{
236 /* The hard limit can't be less than the soft limit. */
237 if (hard != 0 && hard < soft) {
238 xfs_debug(mp, "%shard %lld < %ssoft %lld", tag, hard, tag,
239 soft);
240 return false;
241 }
242
243 res->hardlimit = hard;
244 res->softlimit = soft;
245 if (qlim) {
246 qlim->hard = hard;
247 qlim->soft = soft;
248 }
249
250 return true;
251}
252
253static inline void
254xfs_setqlim_warns(
255 struct xfs_dquot_res *res,
256 struct xfs_quota_limits *qlim,
257 int warns)
258{
259 res->warnings = warns;
260 if (qlim)
261 qlim->warn = warns;
262}
263
264static inline void
265xfs_setqlim_timer(
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700266 struct xfs_mount *mp,
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700267 struct xfs_dquot_res *res,
268 struct xfs_quota_limits *qlim,
269 s64 timer)
270{
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700271 if (qlim) {
272 /* Set the length of the default grace period. */
Darrick J. Wongccc8e772020-08-17 09:58:42 -0700273 res->timer = xfs_dquot_set_grace_period(timer);
274 qlim->time = res->timer;
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700275 } else {
276 /* Set the grace period expiration on a quota. */
277 res->timer = xfs_dquot_set_timeout(mp, timer);
278 }
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700279}
280
281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * Adjust quota limits, and start/stop timers accordingly.
283 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100284int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285xfs_qm_scall_setqlim(
Brian Fosterb1366452013-03-18 10:51:46 -0400286 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_dqid_t id,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700288 xfs_dqtype_t type,
Jan Kara14bf61f2014-10-09 16:03:13 +0200289 struct qc_dqblk *newlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000291 struct xfs_quotainfo *q = mp->m_quotainfo;
Brian Fosterb1366452013-03-18 10:51:46 -0400292 struct xfs_dquot *dqp;
293 struct xfs_trans *tp;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100294 struct xfs_def_quota *defq;
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700295 struct xfs_dquot_res *res;
296 struct xfs_quota_limits *qlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int error;
298 xfs_qcnt_t hard, soft;
299
Jan Kara14bf61f2014-10-09 16:03:13 +0200300 if (newlim->d_fieldmask & ~XFS_QC_MASK)
Dave Chinner24513372014-06-25 14:58:08 +1000301 return -EINVAL;
Jan Kara14bf61f2014-10-09 16:03:13 +0200302 if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
Christoph Hellwigc472b432010-05-06 17:05:17 -0400303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dave Chinnerf6481672013-05-21 18:02:00 +1000305 /*
306 * We don't want to race with a quotaoff so take the quotaoff lock.
307 * We don't hold an inode lock, so there's nothing else to stop
308 * a quotaoff from happening.
309 */
310 mutex_lock(&q->qi_quotaofflock);
311
312 /*
313 * Get the dquot (locked) before we start, as we need to do a
314 * transaction to allocate it if it doesn't exist. Once we have the
315 * dquot, unlock it so we can start the next transaction safely. We hold
316 * a reference to the dquot, so it's safe to do this unlock/lock without
317 * it being reclaimed in the mean time.
318 */
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700319 error = xfs_qm_dqget(mp, id, type, true, &dqp);
Dave Chinnerf6481672013-05-21 18:02:00 +1000320 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000321 ASSERT(error != -ENOENT);
Dave Chinnerf6481672013-05-21 18:02:00 +1000322 goto out_unlock;
323 }
Carlos Maiolinobe607942016-02-08 11:27:55 +1100324
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700325 defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
Dave Chinnerf6481672013-05-21 18:02:00 +1000326 xfs_dqunlock(dqp);
327
Christoph Hellwig253f4912016-04-06 09:19:55 +1000328 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
329 if (error)
Dave Chinnerf6481672013-05-21 18:02:00 +1000330 goto out_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Chinnerf6481672013-05-21 18:02:00 +1000332 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_trans_dqjoin(tp, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /*
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700336 * Update quota limits, warnings, and timers, and the defaults
337 * if we're touching id == 0.
338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Make sure that hardlimits are >= soft limits before changing.
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700340 *
341 * Update warnings counter(s) if requested.
342 *
Eric Sandeendf42ce642020-05-21 13:07:01 -0700343 * Timelimits for the super user set the relative time the other users
344 * can be over quota for this file system. If it is zero a default is
345 * used. Ditto for the default soft and hard limit values (already
346 * done, above), and for warnings.
347 *
348 * For other IDs, userspace can bump out the grace period if over
349 * the soft limit.
350 */
Eric Sandeendf42ce642020-05-21 13:07:01 -0700351
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700352 /* Blocks on the data device. */
353 hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
354 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
355 dqp->q_blk.hardlimit;
356 soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
357 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
358 dqp->q_blk.softlimit;
359 res = &dqp->q_blk;
360 qlim = id == 0 ? &defq->blk : NULL;
361
362 if (xfs_setqlim_limits(mp, res, qlim, hard, soft, "blk"))
363 xfs_dquot_set_prealloc_limits(dqp);
364 if (newlim->d_fieldmask & QC_SPC_WARNS)
365 xfs_setqlim_warns(res, qlim, newlim->d_spc_warns);
366 if (newlim->d_fieldmask & QC_SPC_TIMER)
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700367 xfs_setqlim_timer(mp, res, qlim, newlim->d_spc_timer);
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700368
369 /* Blocks on the realtime device. */
370 hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
371 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
372 dqp->q_rtb.hardlimit;
373 soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
374 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
375 dqp->q_rtb.softlimit;
376 res = &dqp->q_rtb;
377 qlim = id == 0 ? &defq->rtb : NULL;
378
379 xfs_setqlim_limits(mp, res, qlim, hard, soft, "rtb");
380 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
381 xfs_setqlim_warns(res, qlim, newlim->d_rt_spc_warns);
382 if (newlim->d_fieldmask & QC_RT_SPC_TIMER)
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700383 xfs_setqlim_timer(mp, res, qlim, newlim->d_rt_spc_timer);
Darrick J. Wongd1520de2020-07-14 10:37:33 -0700384
385 /* Inodes */
386 hard = (newlim->d_fieldmask & QC_INO_HARD) ?
387 (xfs_qcnt_t) newlim->d_ino_hardlimit :
388 dqp->q_ino.hardlimit;
389 soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
390 (xfs_qcnt_t) newlim->d_ino_softlimit :
391 dqp->q_ino.softlimit;
392 res = &dqp->q_ino;
393 qlim = id == 0 ? &defq->ino : NULL;
394
395 xfs_setqlim_limits(mp, res, qlim, hard, soft, "ino");
396 if (newlim->d_fieldmask & QC_INO_WARNS)
397 xfs_setqlim_warns(res, qlim, newlim->d_ino_warns);
398 if (newlim->d_fieldmask & QC_INO_TIMER)
Darrick J. Wong11d8a912020-08-17 09:58:36 -0700399 xfs_setqlim_timer(mp, res, qlim, newlim->d_ino_timer);
Eric Sandeendf42ce642020-05-21 13:07:01 -0700400
401 if (id != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * If the user is now over quota, start the timelimit.
404 * The user will not be 'warned'.
405 * Note that we keep the timers ticking, whether enforcement
406 * is on or off. We don't really want to bother with iterating
407 * over all ondisk dquots and turning the timers on/off.
408 */
Darrick J. Wongc8c753e2020-07-14 10:37:33 -0700409 xfs_qm_adjust_dqtimers(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700411 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 xfs_trans_log_dquot(tp, dqp);
413
Christoph Hellwig70393312015-06-04 13:48:08 +1000414 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dave Chinnerf6481672013-05-21 18:02:00 +1000416out_rele:
417 xfs_qm_dqrele(dqp);
418out_unlock:
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000419 mutex_unlock(&q->qi_quotaofflock);
David Chinnere5720ee2008-04-10 12:21:18 +1000420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700423/* Fill out the quota context. */
424static void
425xfs_qm_scall_getquota_fill_qc(
Christoph Hellwig18535a72012-02-20 02:28:16 +0000426 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700427 xfs_dqtype_t type,
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700428 const struct xfs_dquot *dqp,
429 struct qc_dqblk *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 memset(dst, 0, sizeof(*dst));
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700432 dst->d_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_blk.hardlimit);
433 dst->d_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_blk.softlimit);
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700434 dst->d_ino_hardlimit = dqp->q_ino.hardlimit;
435 dst->d_ino_softlimit = dqp->q_ino.softlimit;
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700436 dst->d_space = XFS_FSB_TO_B(mp, dqp->q_blk.reserved);
437 dst->d_ino_count = dqp->q_ino.reserved;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700438 dst->d_spc_timer = dqp->q_blk.timer;
439 dst->d_ino_timer = dqp->q_ino.timer;
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700440 dst->d_ino_warns = dqp->q_ino.warnings;
441 dst->d_spc_warns = dqp->q_blk.warnings;
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700442 dst->d_rt_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.hardlimit);
443 dst->d_rt_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.softlimit);
Darrick J. Wong784e80f2020-07-14 10:37:30 -0700444 dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_rtb.reserved);
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700445 dst->d_rt_spc_timer = dqp->q_rtb.timer;
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700446 dst->d_rt_spc_warns = dqp->q_rtb.warnings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000450 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * so return zeroes in that case.
452 */
Darrick J. Wongdbcbc7b2020-07-15 17:48:31 -0700453 if (!xfs_dquot_is_enforced(dqp)) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200454 dst->d_spc_timer = 0;
455 dst->d_ino_timer = 0;
456 dst->d_rt_spc_timer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459#ifdef DEBUG
Darrick J. Wongdbcbc7b2020-07-15 17:48:31 -0700460 if (xfs_dquot_is_enforced(dqp) && dqp->q_id != 0) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200461 if ((dst->d_space > dst->d_spc_softlimit) &&
462 (dst->d_spc_softlimit > 0)) {
463 ASSERT(dst->d_spc_timer != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Darrick J. Wongd3537cf2020-07-14 10:37:31 -0700465 if ((dst->d_ino_count > dqp->q_ino.softlimit) &&
466 (dqp->q_ino.softlimit > 0)) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200467 ASSERT(dst->d_ino_timer != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470#endif
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700471}
472
473/* Return the quota information for the dquot matching id. */
474int
475xfs_qm_scall_getquota(
476 struct xfs_mount *mp,
477 xfs_dqid_t id,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700478 xfs_dqtype_t type,
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700479 struct qc_dqblk *dst)
480{
481 struct xfs_dquot *dqp;
482 int error;
483
Darrick J. Wong01e8f372021-08-06 11:05:42 -0700484 /* Flush inodegc work at the start of a quota reporting scan. */
485 if (id == 0)
486 xfs_inodegc_flush(mp);
487
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700488 /*
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700489 * Try to get the dquot. We don't want it allocated on disk, so don't
490 * set doalloc. If it doesn't exist, we'll get ENOENT back.
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700491 */
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700492 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700493 if (error)
494 return error;
495
496 /*
497 * If everything's NULL, this dquot doesn't quite exist as far as
498 * our utility programs are concerned.
499 */
500 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
501 error = -ENOENT;
502 goto out_put;
503 }
504
505 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
506
Christoph Hellwig18535a72012-02-20 02:28:16 +0000507out_put:
508 xfs_qm_dqput(dqp);
509 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700512/*
513 * Return the quota information for the first initialized dquot whose id
514 * is at least as high as id.
515 */
516int
517xfs_qm_scall_getquota_next(
518 struct xfs_mount *mp,
519 xfs_dqid_t *id,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700520 xfs_dqtype_t type,
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700521 struct qc_dqblk *dst)
522{
523 struct xfs_dquot *dqp;
524 int error;
525
Darrick J. Wong01e8f372021-08-06 11:05:42 -0700526 /* Flush inodegc work at the start of a quota reporting scan. */
527 if (*id == 0)
528 xfs_inodegc_flush(mp);
529
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700530 error = xfs_qm_dqget_next(mp, *id, type, &dqp);
531 if (error)
532 return error;
533
534 /* Fill in the ID we actually read from disk */
Darrick J. Wongc51df732020-07-14 10:37:30 -0700535 *id = dqp->q_id;
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700536
537 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
538
539 xfs_qm_dqput(dqp);
540 return error;
541}