blob: a9e4a7e1b9d9adf48d1430d4441b8af4bbc69a27 [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
Pavel Reichl1cc95e62019-11-12 17:04:27 -080022STATIC int
23xfs_qm_log_quotaoff(
24 struct xfs_mount *mp,
25 struct xfs_qoff_logitem **qoffstartp,
26 uint flags)
27{
28 struct xfs_trans *tp;
29 int error;
30 struct xfs_qoff_logitem *qoffi;
31
Pavel Reichl1cc95e62019-11-12 17:04:27 -080032 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_quotaoff, 0, 0, 0, &tp);
33 if (error)
34 goto out;
35
36 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
37 xfs_trans_log_quotaoff_item(tp, qoffi);
38
39 spin_lock(&mp->m_sb_lock);
40 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
41 spin_unlock(&mp->m_sb_lock);
42
43 xfs_log_sb(tp);
44
45 /*
46 * We have to make sure that the transaction is secure on disk before we
47 * return and actually stop quota accounting. So, make it synchronous.
48 * We don't care about quotoff's performance.
49 */
50 xfs_trans_set_sync(tp);
51 error = xfs_trans_commit(tp);
52 if (error)
53 goto out;
54
55 *qoffstartp = qoffi;
56out:
57 return error;
58}
59
60STATIC int
61xfs_qm_log_quotaoff_end(
62 struct xfs_mount *mp,
Brian Foster8a627142020-03-16 14:26:09 -070063 struct xfs_qoff_logitem **startqoff,
Pavel Reichl1cc95e62019-11-12 17:04:27 -080064 uint flags)
65{
66 struct xfs_trans *tp;
67 int error;
68 struct xfs_qoff_logitem *qoffi;
69
70 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_equotaoff, 0, 0, 0, &tp);
71 if (error)
72 return error;
73
Brian Foster8a627142020-03-16 14:26:09 -070074 qoffi = xfs_trans_get_qoff_item(tp, *startqoff,
Pavel Reichl1cc95e62019-11-12 17:04:27 -080075 flags & XFS_ALL_QUOTA_ACCT);
76 xfs_trans_log_quotaoff_item(tp, qoffi);
Brian Foster8a627142020-03-16 14:26:09 -070077 *startqoff = NULL;
Pavel Reichl1cc95e62019-11-12 17:04:27 -080078
79 /*
80 * We have to make sure that the transaction is secure on disk before we
81 * return and actually stop quota accounting. So, make it synchronous.
82 * We don't care about quotoff's performance.
83 */
84 xfs_trans_set_sync(tp);
85 return xfs_trans_commit(tp);
86}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Turn off quota accounting and/or enforcement for all udquots and/or
90 * gdquots. Called only at unmount time.
91 *
92 * This assumes that there are no dquots of this file system cached
93 * incore, and modifies the ondisk dquot directly. Therefore, for example,
94 * it is an error to call this twice, without purging the cache.
95 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +010096int
Linus Torvalds1da177e2005-04-16 15:20:36 -070097xfs_qm_scall_quotaoff(
98 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010099 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000101 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 uint dqtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int error;
104 uint inactivate_flags;
Brian Foster8a627142020-03-16 14:26:09 -0700105 struct xfs_qoff_logitem *qoffstart = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /*
108 * No file system can have quotas enabled on disk but not in core.
109 * Note that quota utilities (like quotaoff) _expect_
Dave Chinner24513372014-06-25 14:58:08 +1000110 * errno == -EEXIST here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112 if ((mp->m_qflags & flags) == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000113 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 error = 0;
115
116 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
117
118 /*
119 * We don't want to deal with two quotaoffs messing up each other,
120 * so we're going to serialize it. quotaoff isn't exactly a performance
121 * critical thing.
122 * If quotaoff, then we must be dealing with the root filesystem.
123 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000124 ASSERT(q);
125 mutex_lock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /*
128 * If we're just turning off quota enforcement, change mp and go.
129 */
130 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
131 mp->m_qflags &= ~(flags);
132
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000133 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 mp->m_sb.sb_qflags = mp->m_qflags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000135 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000136 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* XXX what to do if error ? Revert back to old vals incore ? */
Dave Chinner61e63ec2015-01-22 09:10:31 +1100139 return xfs_sync_sb(mp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 dqtype = 0;
143 inactivate_flags = 0;
144 /*
145 * If accounting is off, we must turn enforcement off, clear the
146 * quota 'CHKD' certificate to make it known that we have to
147 * do a quotacheck the next time this quota is turned on.
148 */
149 if (flags & XFS_UQUOTA_ACCT) {
150 dqtype |= XFS_QMOPT_UQUOTA;
151 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
152 inactivate_flags |= XFS_UQUOTA_ACTIVE;
153 }
154 if (flags & XFS_GQUOTA_ACCT) {
155 dqtype |= XFS_QMOPT_GQUOTA;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500156 flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500158 }
159 if (flags & XFS_PQUOTA_ACCT) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000160 dqtype |= XFS_QMOPT_PQUOTA;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500161 flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000162 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
165 /*
166 * Nothing to do? Don't complain. This happens when we're just
167 * turning off quota enforcement.
168 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000169 if ((mp->m_qflags & flags) == 0)
170 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /*
173 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
David Chinnercb6edc22008-04-10 12:20:45 +1000174 * and synchronously. If we fail to write, we should abort the
175 * operation as it cannot be recovered safely if we crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
David Chinnercb6edc22008-04-10 12:20:45 +1000177 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
178 if (error)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000179 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
182 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
183 * to take care of the race between dqget and quotaoff. We don't take
184 * any special locks to reset these bits. All processes need to check
185 * these bits *after* taking inode lock(s) to see if the particular
186 * quota type is in the process of being turned off. If *ACTIVE, it is
187 * guaranteed that all dquot structures and all quotainode ptrs will all
188 * stay valid as long as that inode is kept locked.
189 *
190 * There is no turning back after this.
191 */
192 mp->m_qflags &= ~inactivate_flags;
193
194 /*
195 * Give back all the dquot reference(s) held by inodes.
196 * Here we go thru every single incore inode in this file system, and
197 * do a dqrele on the i_udquot/i_gdquot that it may have.
198 * Essentially, as long as somebody has an inode locked, this guarantees
199 * that quotas will not be turned off. This is handy because in a
200 * transaction once we lock the inode(s) and check for quotaon, we can
201 * depend on the quota inodes (and other things) being valid as long as
202 * we keep the lock(s).
203 */
204 xfs_qm_dqrele_all_inodes(mp, flags);
205
206 /*
207 * Next we make the changes in the quota flag in the mount struct.
208 * This isn't protected by a particular lock directly, because we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300209 * don't want to take a mrlock every time we depend on quotas being on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500211 mp->m_qflags &= ~flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /*
214 * Go through all the dquots of this file system and purge them,
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500215 * according to what was turned off.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500217 xfs_qm_dqpurge_all(mp, dqtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /*
220 * Transactions that had started before ACTIVE state bit was cleared
221 * could have logged many dquots, so they'd have higher LSNs than
222 * the first QUOTAOFF log record does. If we happen to crash when
223 * the tail of the log has gone past the QUOTAOFF record, but
224 * before the last dquot modification, those dquots __will__
225 * recover, and that's not good.
226 *
227 * So, we have QUOTAOFF start and end logitems; the start
228 * logitem won't get overwritten until the end logitem appears...
229 */
Brian Foster8a627142020-03-16 14:26:09 -0700230 error = xfs_qm_log_quotaoff_end(mp, &qoffstart, flags);
David Chinnercb6edc22008-04-10 12:20:45 +1000231 if (error) {
232 /* We're screwed now. Shutdown is the only option. */
233 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000234 goto out_unlock;
David Chinnercb6edc22008-04-10 12:20:45 +1000235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /*
Chandra Seetharamanc31ad432013-07-10 18:00:36 -0500238 * If all quotas are completely turned off, close shop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
Chandra Seetharamanc31ad432013-07-10 18:00:36 -0500240 if (mp->m_qflags == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000241 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_qm_destroy_quotainfo(mp);
Eric Sandeend99831f2014-06-22 15:03:54 +1000243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /*
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000247 * Release our quotainode references if we don't need them anymore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000249 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700250 xfs_irele(q->qi_uquotaip);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000251 q->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500253 if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700254 xfs_irele(q->qi_gquotaip);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000255 q->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500257 if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700258 xfs_irele(q->qi_pquotaip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500259 q->qi_pquotaip = NULL;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000262out_unlock:
Brian Foster8a627142020-03-16 14:26:09 -0700263 if (error && qoffstart)
264 xfs_qm_qoff_logitem_relse(qoffstart);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000265 mutex_unlock(&q->qi_quotaofflock);
266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Christoph Hellwig5d188982010-07-20 17:51:31 +1000269STATIC int
270xfs_qm_scall_trunc_qfile(
271 struct xfs_mount *mp,
272 xfs_ino_t ino)
273{
274 struct xfs_inode *ip;
275 struct xfs_trans *tp;
276 int error;
277
278 if (ino == NULLFSINO)
279 return 0;
280
281 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
282 if (error)
283 return error;
284
285 xfs_ilock(ip, XFS_IOLOCK_EXCL);
286
Christoph Hellwig253f4912016-04-06 09:19:55 +1000287 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000288 if (error) {
Christoph Hellwig5d188982010-07-20 17:51:31 +1000289 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
290 goto out_put;
291 }
292
293 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000294 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000295
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000296 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
298
299 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000300 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000301 xfs_trans_cancel(tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000302 goto out_unlock;
303 }
304
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700305 ASSERT(ip->i_df.if_nextents == 0);
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000306
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000307 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Christoph Hellwig70393312015-06-04 13:48:08 +1000308 error = xfs_trans_commit(tp);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000309
310out_unlock:
311 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
312out_put:
Darrick J. Wong44a87362018-07-25 12:52:32 -0700313 xfs_irele(ip);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000314 return error;
315}
316
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100317int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318xfs_qm_scall_trunc_qfiles(
319 xfs_mount_t *mp,
320 uint flags)
321{
Dave Chinner24513372014-06-25 14:58:08 +1000322 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Eric Sandeenf58522c2014-05-05 17:27:06 +1000324 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700325 (flags & ~XFS_QMOPT_QUOTALL)) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500326 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100327 __func__, flags, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700331 if (flags & XFS_QMOPT_UQUOTA) {
Christoph Hellwig5d188982010-07-20 17:51:31 +1000332 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
Jie Liuc61a9e32013-11-22 14:04:00 +0800333 if (error)
334 return error;
335 }
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700336 if (flags & XFS_QMOPT_GQUOTA) {
Jie Liuc61a9e32013-11-22 14:04:00 +0800337 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
338 if (error)
339 return error;
340 }
Darrick J. Wong41ed4a52020-07-14 10:36:09 -0700341 if (flags & XFS_QMOPT_PQUOTA)
Jie Liuc61a9e32013-11-22 14:04:00 +0800342 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jie Liuc61a9e32013-11-22 14:04:00 +0800344 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
348 * Switch on (a given) quota enforcement for a filesystem. This takes
349 * effect immediately.
350 * (Switching on quota accounting must be done at mount time.)
351 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100352int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353xfs_qm_scall_quotaon(
354 xfs_mount_t *mp,
355 uint flags)
356{
357 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 uint qf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
Kaixu Xiacd594552020-04-22 21:54:30 -0700361 * Switching on quota accounting must be done at mount time,
362 * only consider quota enforcement stuff here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Kaixu Xiacd594552020-04-22 21:54:30 -0700364 flags &= XFS_ALL_QUOTA_ENFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (flags == 0) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500367 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100368 __func__, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Can't enforce without accounting. We check the superblock
374 * qflags here instead of m_qflags because rootfs can have
375 * quota acct on ondisk without m_qflags' knowing.
376 */
Jan Karafbf64b32014-10-08 11:52:52 +0200377 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500378 (flags & XFS_UQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200379 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500380 (flags & XFS_GQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200381 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500382 (flags & XFS_PQUOTA_ENFD))) {
Dave Chinner82211122011-03-07 10:07:35 +1100383 xfs_debug(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500384 "%s: Can't enforce without acct, flags=%x sbflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100385 __func__, flags, mp->m_sb.sb_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300389 * If everything's up to-date incore, then don't waste time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391 if ((mp->m_qflags & flags) == flags)
Dave Chinner24513372014-06-25 14:58:08 +1000392 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * Change sb_qflags on disk but not incore mp->qflags
396 * if this is the root filesystem.
397 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000398 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 qf = mp->m_sb.sb_qflags;
400 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000401 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * There's nothing to change if it's the same.
405 */
Dave Chinner4d11a402015-01-22 09:10:26 +1100406 if ((qf & flags) == flags)
Dave Chinner24513372014-06-25 14:58:08 +1000407 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dave Chinner61e63ec2015-01-22 09:10:31 +1100409 error = xfs_sync_sb(mp, false);
410 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * If we aren't trying to switch on quota enforcement, we are done.
414 */
415 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
416 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000417 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
418 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
419 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
Jan Karafbf64b32014-10-08 11:52:52 +0200420 (mp->m_qflags & XFS_GQUOTA_ACCT)))
Eric Sandeend99831f2014-06-22 15:03:54 +1000421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (! XFS_IS_QUOTA_RUNNING(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000424 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /*
427 * Switch on quota enforcement in core.
428 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000429 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000431 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Eric Sandeend99831f2014-06-22 15:03:54 +1000433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Jan Kara14bf61f2014-10-09 16:03:13 +0200436#define XFS_QC_MASK \
437 (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
Christoph Hellwigc472b432010-05-06 17:05:17 -0400438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
440 * Adjust quota limits, and start/stop timers accordingly.
441 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100442int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443xfs_qm_scall_setqlim(
Brian Fosterb1366452013-03-18 10:51:46 -0400444 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 xfs_dqid_t id,
446 uint type,
Jan Kara14bf61f2014-10-09 16:03:13 +0200447 struct qc_dqblk *newlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000449 struct xfs_quotainfo *q = mp->m_quotainfo;
Brian Fosterb1366452013-03-18 10:51:46 -0400450 struct xfs_disk_dquot *ddq;
451 struct xfs_dquot *dqp;
452 struct xfs_trans *tp;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100453 struct xfs_def_quota *defq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int error;
455 xfs_qcnt_t hard, soft;
456
Jan Kara14bf61f2014-10-09 16:03:13 +0200457 if (newlim->d_fieldmask & ~XFS_QC_MASK)
Dave Chinner24513372014-06-25 14:58:08 +1000458 return -EINVAL;
Jan Kara14bf61f2014-10-09 16:03:13 +0200459 if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
Christoph Hellwigc472b432010-05-06 17:05:17 -0400460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dave Chinnerf6481672013-05-21 18:02:00 +1000462 /*
463 * We don't want to race with a quotaoff so take the quotaoff lock.
464 * We don't hold an inode lock, so there's nothing else to stop
465 * a quotaoff from happening.
466 */
467 mutex_lock(&q->qi_quotaofflock);
468
469 /*
470 * Get the dquot (locked) before we start, as we need to do a
471 * transaction to allocate it if it doesn't exist. Once we have the
472 * dquot, unlock it so we can start the next transaction safely. We hold
473 * a reference to the dquot, so it's safe to do this unlock/lock without
474 * it being reclaimed in the mean time.
475 */
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700476 error = xfs_qm_dqget(mp, id, type, true, &dqp);
Dave Chinnerf6481672013-05-21 18:02:00 +1000477 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000478 ASSERT(error != -ENOENT);
Dave Chinnerf6481672013-05-21 18:02:00 +1000479 goto out_unlock;
480 }
Carlos Maiolinobe607942016-02-08 11:27:55 +1100481
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700482 defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
Dave Chinnerf6481672013-05-21 18:02:00 +1000483 xfs_dqunlock(dqp);
484
Christoph Hellwig253f4912016-04-06 09:19:55 +1000485 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
486 if (error)
Dave Chinnerf6481672013-05-21 18:02:00 +1000487 goto out_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dave Chinnerf6481672013-05-21 18:02:00 +1000489 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_trans_dqjoin(tp, dqp);
491 ddq = &dqp->q_core;
492
493 /*
494 * Make sure that hardlimits are >= soft limits before changing.
495 */
Jan Kara14bf61f2014-10-09 16:03:13 +0200496 hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
497 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100498 be64_to_cpu(ddq->d_blk_hardlimit);
Jan Kara14bf61f2014-10-09 16:03:13 +0200499 soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
500 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100501 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100503 ddq->d_blk_hardlimit = cpu_to_be64(hard);
504 ddq->d_blk_softlimit = cpu_to_be64(soft);
Brian Fosterb1366452013-03-18 10:51:46 -0400505 xfs_dquot_set_prealloc_limits(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (id == 0) {
Carlos Maiolinobe607942016-02-08 11:27:55 +1100507 defq->bhardlimit = hard;
508 defq->bsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500511 xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Jan Kara14bf61f2014-10-09 16:03:13 +0200513 hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
514 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100515 be64_to_cpu(ddq->d_rtb_hardlimit);
Jan Kara14bf61f2014-10-09 16:03:13 +0200516 soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
517 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100518 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100520 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
521 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (id == 0) {
Carlos Maiolinobe607942016-02-08 11:27:55 +1100523 defq->rtbhardlimit = hard;
524 defq->rtbsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500527 xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Jan Kara14bf61f2014-10-09 16:03:13 +0200530 hard = (newlim->d_fieldmask & QC_INO_HARD) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100532 be64_to_cpu(ddq->d_ino_hardlimit);
Jan Kara14bf61f2014-10-09 16:03:13 +0200533 soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100535 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100537 ddq->d_ino_hardlimit = cpu_to_be64(hard);
538 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (id == 0) {
Carlos Maiolinobe607942016-02-08 11:27:55 +1100540 defq->ihardlimit = hard;
541 defq->isoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500544 xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Nathan Scott754002b2005-06-21 15:49:06 +1000547 /*
548 * Update warnings counter(s) if requested
549 */
Jan Kara14bf61f2014-10-09 16:03:13 +0200550 if (newlim->d_fieldmask & QC_SPC_WARNS)
551 ddq->d_bwarns = cpu_to_be16(newlim->d_spc_warns);
552 if (newlim->d_fieldmask & QC_INO_WARNS)
553 ddq->d_iwarns = cpu_to_be16(newlim->d_ino_warns);
554 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
555 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rt_spc_warns);
Nathan Scott754002b2005-06-21 15:49:06 +1000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (id == 0) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200558 if (newlim->d_fieldmask & QC_SPC_WARNS)
Eric Sandeene8503012020-05-21 13:07:01 -0700559 defq->bwarnlimit = newlim->d_spc_warns;
Jan Kara14bf61f2014-10-09 16:03:13 +0200560 if (newlim->d_fieldmask & QC_INO_WARNS)
Eric Sandeene8503012020-05-21 13:07:01 -0700561 defq->iwarnlimit = newlim->d_ino_warns;
Jan Kara14bf61f2014-10-09 16:03:13 +0200562 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
Eric Sandeene8503012020-05-21 13:07:01 -0700563 defq->rtbwarnlimit = newlim->d_rt_spc_warns;
Eric Sandeendf42ce642020-05-21 13:07:01 -0700564 }
565
566 /*
567 * Timelimits for the super user set the relative time the other users
568 * can be over quota for this file system. If it is zero a default is
569 * used. Ditto for the default soft and hard limit values (already
570 * done, above), and for warnings.
571 *
572 * For other IDs, userspace can bump out the grace period if over
573 * the soft limit.
574 */
575 if (newlim->d_fieldmask & QC_SPC_TIMER)
576 ddq->d_btimer = cpu_to_be32(newlim->d_spc_timer);
577 if (newlim->d_fieldmask & QC_INO_TIMER)
578 ddq->d_itimer = cpu_to_be32(newlim->d_ino_timer);
579 if (newlim->d_fieldmask & QC_RT_SPC_TIMER)
580 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rt_spc_timer);
581
582 if (id == 0) {
583 if (newlim->d_fieldmask & QC_SPC_TIMER)
584 defq->btimelimit = newlim->d_spc_timer;
585 if (newlim->d_fieldmask & QC_INO_TIMER)
586 defq->itimelimit = newlim->d_ino_timer;
587 if (newlim->d_fieldmask & QC_RT_SPC_TIMER)
588 defq->rtbtimelimit = newlim->d_rt_spc_timer;
589 }
590
591 if (id != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * If the user is now over quota, start the timelimit.
594 * The user will not be 'warned'.
595 * Note that we keep the timers ticking, whether enforcement
596 * is on or off. We don't really want to bother with iterating
597 * over all ondisk dquots and turning the timers on/off.
598 */
Eric Sandeen3dbb9aa2020-05-21 13:07:00 -0700599 xfs_qm_adjust_dqtimers(mp, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700601 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_trans_log_dquot(tp, dqp);
603
Christoph Hellwig70393312015-06-04 13:48:08 +1000604 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dave Chinnerf6481672013-05-21 18:02:00 +1000606out_rele:
607 xfs_qm_dqrele(dqp);
608out_unlock:
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000609 mutex_unlock(&q->qi_quotaofflock);
David Chinnere5720ee2008-04-10 12:21:18 +1000610 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700613/* Fill out the quota context. */
614static void
615xfs_qm_scall_getquota_fill_qc(
Christoph Hellwig18535a72012-02-20 02:28:16 +0000616 struct xfs_mount *mp,
Christoph Hellwig18535a72012-02-20 02:28:16 +0000617 uint type,
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700618 const struct xfs_dquot *dqp,
619 struct qc_dqblk *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 memset(dst, 0, sizeof(*dst));
Jan Kara14bf61f2014-10-09 16:03:13 +0200622 dst->d_spc_hardlimit =
623 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
624 dst->d_spc_softlimit =
625 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
Christoph Hellwig18535a72012-02-20 02:28:16 +0000626 dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
627 dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Jan Kara14bf61f2014-10-09 16:03:13 +0200628 dst->d_space = XFS_FSB_TO_B(mp, dqp->q_res_bcount);
629 dst->d_ino_count = dqp->q_res_icount;
630 dst->d_spc_timer = be32_to_cpu(dqp->q_core.d_btimer);
631 dst->d_ino_timer = be32_to_cpu(dqp->q_core.d_itimer);
632 dst->d_ino_warns = be16_to_cpu(dqp->q_core.d_iwarns);
633 dst->d_spc_warns = be16_to_cpu(dqp->q_core.d_bwarns);
634 dst->d_rt_spc_hardlimit =
635 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
636 dst->d_rt_spc_softlimit =
637 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
638 dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_res_rtbcount);
639 dst->d_rt_spc_timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
640 dst->d_rt_spc_warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /*
643 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000644 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * so return zeroes in that case.
646 */
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500647 if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
648 dqp->q_core.d_flags == XFS_DQ_USER) ||
649 (!XFS_IS_GQUOTA_ENFORCED(mp) &&
650 dqp->q_core.d_flags == XFS_DQ_GROUP) ||
651 (!XFS_IS_PQUOTA_ENFORCED(mp) &&
652 dqp->q_core.d_flags == XFS_DQ_PROJ)) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200653 dst->d_spc_timer = 0;
654 dst->d_ino_timer = 0;
655 dst->d_rt_spc_timer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658#ifdef DEBUG
Jan Kara14bf61f2014-10-09 16:03:13 +0200659 if (((XFS_IS_UQUOTA_ENFORCED(mp) && type == XFS_DQ_USER) ||
660 (XFS_IS_GQUOTA_ENFORCED(mp) && type == XFS_DQ_GROUP) ||
661 (XFS_IS_PQUOTA_ENFORCED(mp) && type == XFS_DQ_PROJ)) &&
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700662 dqp->q_core.d_id != 0) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200663 if ((dst->d_space > dst->d_spc_softlimit) &&
664 (dst->d_spc_softlimit > 0)) {
665 ASSERT(dst->d_spc_timer != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Jan Kara14bf61f2014-10-09 16:03:13 +0200667 if ((dst->d_ino_count > dst->d_ino_softlimit) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 (dst->d_ino_softlimit > 0)) {
Jan Kara14bf61f2014-10-09 16:03:13 +0200669 ASSERT(dst->d_ino_timer != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 }
672#endif
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700673}
674
675/* Return the quota information for the dquot matching id. */
676int
677xfs_qm_scall_getquota(
678 struct xfs_mount *mp,
679 xfs_dqid_t id,
680 uint type,
681 struct qc_dqblk *dst)
682{
683 struct xfs_dquot *dqp;
684 int error;
685
686 /*
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700687 * Try to get the dquot. We don't want it allocated on disk, so don't
688 * set doalloc. If it doesn't exist, we'll get ENOENT back.
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700689 */
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700690 error = xfs_qm_dqget(mp, id, type, false, &dqp);
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700691 if (error)
692 return error;
693
694 /*
695 * If everything's NULL, this dquot doesn't quite exist as far as
696 * our utility programs are concerned.
697 */
698 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
699 error = -ENOENT;
700 goto out_put;
701 }
702
703 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
704
Christoph Hellwig18535a72012-02-20 02:28:16 +0000705out_put:
706 xfs_qm_dqput(dqp);
707 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Darrick J. Wong2e330e72018-05-04 15:30:20 -0700710/*
711 * Return the quota information for the first initialized dquot whose id
712 * is at least as high as id.
713 */
714int
715xfs_qm_scall_getquota_next(
716 struct xfs_mount *mp,
717 xfs_dqid_t *id,
718 uint type,
719 struct qc_dqblk *dst)
720{
721 struct xfs_dquot *dqp;
722 int error;
723
724 error = xfs_qm_dqget_next(mp, *id, type, &dqp);
725 if (error)
726 return error;
727
728 /* Fill in the ID we actually read from disk */
729 *id = be32_to_cpu(dqp->q_core.d_id);
730
731 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
732
733 xfs_qm_dqput(dqp);
734 return error;
735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200737STATIC int
738xfs_dqrele_inode(
739 struct xfs_inode *ip,
Brian Fostera454f742012-11-06 09:50:39 -0500740 void *args)
David Chinner5b4d89a2008-10-30 17:08:03 +1100741{
Darrick J. Wong390600f2020-05-21 13:08:48 -0700742 uint *flags = args;
743
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200744 /* skip quota inodes */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000745 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500746 ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
747 ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200748 ASSERT(ip->i_udquot == NULL);
749 ASSERT(ip->i_gdquot == NULL);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500750 ASSERT(ip->i_pdquot == NULL);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200751 return 0;
752 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100753
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200754 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong390600f2020-05-21 13:08:48 -0700755 if ((*flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200756 xfs_qm_dqrele(ip->i_udquot);
757 ip->i_udquot = NULL;
758 }
Darrick J. Wong390600f2020-05-21 13:08:48 -0700759 if ((*flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200760 xfs_qm_dqrele(ip->i_gdquot);
761 ip->i_gdquot = NULL;
762 }
Darrick J. Wong390600f2020-05-21 13:08:48 -0700763 if ((*flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500764 xfs_qm_dqrele(ip->i_pdquot);
765 ip->i_pdquot = NULL;
766 }
Christoph Hellwigf2d67612010-06-24 11:52:50 +1000767 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200768 return 0;
David Chinner5b4d89a2008-10-30 17:08:03 +1100769}
770
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200771
David Chinner5b4d89a2008-10-30 17:08:03 +1100772/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * Go thru all the inodes in the file system, releasing their dquots.
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * Note that the mount structure gets modified to indicate that quotas are off
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200776 * AFTER this, in the case of quotaoff.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778void
779xfs_qm_dqrele_all_inodes(
Darrick J. Wong390600f2020-05-21 13:08:48 -0700780 struct xfs_mount *mp,
781 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ASSERT(mp->m_quotainfo);
Darrick J. Wong042f65f2020-05-21 13:08:50 -0700784 xfs_inode_walk(mp, XFS_INODE_WALK_INEW_WAIT, xfs_dqrele_inode,
Darrick J. Wong390600f2020-05-21 13:08:48 -0700785 &flags, XFS_ICI_NO_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}