blob: 9490b913a4ab4f39ca046a2f4432fcf909a69aa0 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11008#include "xfs_shared.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
Darrick J. Wongebd126a2019-07-02 09:39:39 -070016#include "xfs_iwalk.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_quota.h"
Nathan Scotta844f452005-11-02 14:38:42 +110018#include "xfs_bmap.h"
Christoph Hellwig8bfadd82017-08-29 15:44:14 -070019#include "xfs_bmap_util.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000023#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110024#include "xfs_icache.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070025#include "xfs_error.h"
Dave Chinner9bbafc712021-06-02 10:48:24 +100026#include "xfs_ag.h"
Dave Chinnerb652afd2021-06-02 10:48:24 +100027#include "xfs_ialloc.h"
Dave Chinner01728b42022-03-17 09:09:13 -070028#include "xfs_log_priv.h"
Darrick J. Wong3d8f1422024-02-22 12:30:51 -080029#include "xfs_health.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * The global quota manager. There is only one of these for the entire
33 * system, _not_ one per file system. XQM keeps track of the overall
34 * quota functionality, including maintaining the freelist and hash
35 * tables of dquots.
36 */
Pavel Reichlc072fbe2019-11-12 17:04:26 -080037STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp);
38STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pavel Reichlc072fbe2019-11-12 17:04:26 -080040STATIC void xfs_qm_destroy_quotainos(struct xfs_quotainfo *qi);
Dave Chinnercd56a392013-08-28 10:18:07 +100041STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050043 * We use the batch lookup interface to iterate over the dquots as it
44 * currently is the only interface into the radix tree code that allows
45 * fuzzy lookups instead of exact matches. Holding the lock over multiple
46 * operations is fine as all callers are used either during mount/umount
47 * or quotaoff.
48 */
49#define XFS_DQ_LOOKUP_BATCH 32
50
51STATIC int
52xfs_qm_dquot_walk(
53 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -070054 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +100055 int (*execute)(struct xfs_dquot *dqp, void *data),
56 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050057{
58 struct xfs_quotainfo *qi = mp->m_quotainfo;
Chandra Seetharaman329e0872013-06-27 17:25:05 -050059 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050060 uint32_t next_index;
61 int last_error = 0;
62 int skipped;
63 int nr_found;
64
65restart:
66 skipped = 0;
67 next_index = 0;
68 nr_found = 0;
69
70 while (1) {
71 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
Li zeminge1956052023-01-03 09:54:28 -080072 int error;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050073 int i;
74
75 mutex_lock(&qi->qi_tree_lock);
76 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
77 next_index, XFS_DQ_LOOKUP_BATCH);
78 if (!nr_found) {
79 mutex_unlock(&qi->qi_tree_lock);
80 break;
81 }
82
83 for (i = 0; i < nr_found; i++) {
84 struct xfs_dquot *dqp = batch[i];
85
Darrick J. Wongc51df732020-07-14 10:37:30 -070086 next_index = dqp->q_id + 1;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050087
Christoph Hellwig43ff2122012-04-23 15:58:39 +100088 error = execute(batch[i], data);
Dave Chinner24513372014-06-25 14:58:08 +100089 if (error == -EAGAIN) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050090 skipped++;
91 continue;
92 }
Dave Chinner24513372014-06-25 14:58:08 +100093 if (error && last_error != -EFSCORRUPTED)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050094 last_error = error;
95 }
96
97 mutex_unlock(&qi->qi_tree_lock);
98
99 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000100 if (last_error == -EFSCORRUPTED) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500101 skipped = 0;
102 break;
103 }
Brian Fostercfaf2d02017-07-24 08:33:25 -0700104 /* we're done if id overflows back to zero */
105 if (!next_index)
106 break;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500107 }
108
109 if (skipped) {
110 delay(1);
111 goto restart;
112 }
113
114 return last_error;
115}
116
117
118/*
119 * Purge a dquot from all tracking data structures and free it.
120 */
121STATIC int
122xfs_qm_dqpurge(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000123 struct xfs_dquot *dqp,
124 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500125{
Dave Chinner01728b42022-03-17 09:09:13 -0700126 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700127 int error = -EAGAIN;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500128
129 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700130 if ((dqp->q_flags & XFS_DQFLAG_FREEING) || dqp->q_nrefs != 0)
Brian Foster8d3d7e22020-03-27 08:29:45 -0700131 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500132
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700133 dqp->q_flags |= XFS_DQFLAG_FREEING;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500134
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000135 xfs_dqflock(dqp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500136
137 /*
138 * If we are turning this type of quotas off, we don't care
139 * about the dirty metadata sitting in this dquot. OTOH, if
140 * we're unmounting, we do care, so we flush it and wait.
141 */
142 if (XFS_DQ_IS_DIRTY(dqp)) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000143 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500144
145 /*
146 * We don't care about getting disk errors here. We need
147 * to purge this dquot anyway, so we go ahead regardless.
148 */
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000149 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700150 if (!error) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000151 error = xfs_bwrite(bp);
152 xfs_buf_relse(bp);
Brian Foster8d3d7e22020-03-27 08:29:45 -0700153 } else if (error == -EAGAIN) {
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700154 dqp->q_flags &= ~XFS_DQFLAG_FREEING;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700155 goto out_unlock;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000156 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500157 xfs_dqflock(dqp);
158 }
159
160 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Dave Chinner01728b42022-03-17 09:09:13 -0700161 ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) ||
Dave Chinner22525c12018-05-09 07:47:34 -0700162 !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500163
164 xfs_dqfunlock(dqp);
165 xfs_dqunlock(dqp);
166
Darrick J. Wong51dbb1b2020-07-14 10:37:32 -0700167 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500168 qi->qi_dquots--;
169
170 /*
171 * We move dquots to the freelist as soon as their reference count
172 * hits zero, so it really should be on the freelist here.
173 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500174 ASSERT(!list_empty(&dqp->q_lru));
Nhat Pham0a97c012023-11-30 11:40:18 -0800175 list_lru_del_obj(&qi->qi_lru, &dqp->q_lru);
Dave Chinner01728b42022-03-17 09:09:13 -0700176 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500177
178 xfs_qm_dqdestroy(dqp);
Jie Liudf8052e2013-11-26 21:38:49 +0800179 return 0;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700180
181out_unlock:
182 xfs_dqunlock(dqp);
183 return error;
Jie Liudf8052e2013-11-26 21:38:49 +0800184}
185
186/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500187 * Purge the dquot cache.
188 */
Christoph Hellwig40b52222021-08-06 11:05:36 -0700189static void
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500190xfs_qm_dqpurge_all(
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700191 struct xfs_mount *mp)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500192{
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700193 xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL);
194 xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL);
195 xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500196}
197
198/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * Just destroy the quotainfo structure.
200 */
201void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200202xfs_qm_unmount(
203 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200205 if (mp->m_quotainfo) {
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700206 xfs_qm_dqpurge_all(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Called from the vfsops layer.
213 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100214void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215xfs_qm_unmount_quotas(
216 xfs_mount_t *mp)
217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
219 * Release the dquots that root inode, et al might be holding,
220 * before we flush quotas and blow away the quotainfo structure.
221 */
222 ASSERT(mp->m_rootip);
223 xfs_qm_dqdetach(mp->m_rootip);
224 if (mp->m_rbmip)
225 xfs_qm_dqdetach(mp->m_rbmip);
226 if (mp->m_rsumip)
227 xfs_qm_dqdetach(mp->m_rsumip);
228
229 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100230 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100233 if (mp->m_quotainfo->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700234 xfs_irele(mp->m_quotainfo->qi_uquotaip);
Christoph Hellwige57481d2008-12-03 12:20:36 +0100235 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100237 if (mp->m_quotainfo->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700238 xfs_irele(mp->m_quotainfo->qi_gquotaip);
Christoph Hellwige57481d2008-12-03 12:20:36 +0100239 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500241 if (mp->m_quotainfo->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700242 xfs_irele(mp->m_quotainfo->qi_pquotaip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500243 mp->m_quotainfo->qi_pquotaip = NULL;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248STATIC int
249xfs_qm_dqattach_one(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800250 struct xfs_inode *ip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700251 xfs_dqtype_t type,
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800252 bool doalloc,
253 struct xfs_dquot **IO_idqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800255 struct xfs_dquot *dqp;
256 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +0000258 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000262 * See if we already have it in the inode itself. IO_idqpp is &i_udquot
263 * or &i_gdquot. This made the code look weird, but made the logic a lot
264 * simpler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100266 dqp = *IO_idqpp;
267 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000268 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000273 * Find the dquot from somewhere. This bumps the reference count of
274 * dquot and returns it locked. This can return ENOENT if dquot didn't
275 * exist on disk and we didn't ask it to allocate; ESRCH if quotas got
276 * turned off suddenly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700278 error = xfs_qm_dqget_inode(ip, type, doalloc, &dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100279 if (error)
280 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000282 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * dqget may have dropped and re-acquired the ilock, but it guarantees
286 * that the dquot returned is the one that should go in the inode.
287 */
288 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100289 xfs_dqunlock(dqp);
290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400293static bool
294xfs_qm_need_dqattach(
295 struct xfs_inode *ip)
296{
297 struct xfs_mount *mp = ip->i_mount;
298
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400299 if (!XFS_IS_QUOTA_ON(mp))
300 return false;
301 if (!XFS_NOT_DQATTACHED(mp, ip))
302 return false;
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500303 if (xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400304 return false;
305 return true;
306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000309 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
310 * into account.
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700311 * If @doalloc is true, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Inode may get unlocked and relocked in here, and the caller must deal with
313 * the consequences.
314 */
315int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200316xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfs_inode_t *ip,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700318 bool doalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int error = 0;
322
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400323 if (!xfs_qm_need_dqattach(ip))
Jesper Juhl014c2542006-01-15 02:37:08 +0100324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +0000326 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Dave Chinner3c35337572014-05-05 17:30:15 +1000328 if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700329 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_USER,
330 doalloc, &ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (error)
332 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000333 ASSERT(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000335
Dave Chinner3c35337572014-05-05 17:30:15 +1000336 if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700337 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_GROUP,
338 doalloc, &ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (error)
340 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000341 ASSERT(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
Dave Chinner3c35337572014-05-05 17:30:15 +1000344 if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700345 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_PROJ,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700346 doalloc, &ip->i_pdquot);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500347 if (error)
348 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000349 ASSERT(ip->i_pdquot);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500350 }
351
Dave Chinner3c35337572014-05-05 17:30:15 +1000352done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000354 * Don't worry about the dquots that we may have attached before any
355 * error - they'll get detached later if it has not already been done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +0000357 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200358 return error;
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Christoph Hellwig7d095252009-06-08 15:33:32 +0200361int
362xfs_qm_dqattach(
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700363 struct xfs_inode *ip)
Christoph Hellwig7d095252009-06-08 15:33:32 +0200364{
365 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400367 if (!xfs_qm_need_dqattach(ip))
368 return 0;
369
Christoph Hellwig7d095252009-06-08 15:33:32 +0200370 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700371 error = xfs_qm_dqattach_locked(ip, false);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200372 xfs_iunlock(ip, XFS_ILOCK_EXCL);
373
Jesper Juhl014c2542006-01-15 02:37:08 +0100374 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/*
378 * Release dquots (and their references) if any.
379 * The inode should be locked EXCL except when this's called by
380 * xfs_ireclaim.
381 */
382void
383xfs_qm_dqdetach(
384 xfs_inode_t *ip)
385{
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500386 if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return;
388
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000389 trace_xfs_dquot_dqdetach(ip);
390
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500391 ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, ip->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ip->i_udquot) {
393 xfs_qm_dqrele(ip->i_udquot);
394 ip->i_udquot = NULL;
395 }
396 if (ip->i_gdquot) {
397 xfs_qm_dqrele(ip->i_gdquot);
398 ip->i_gdquot = NULL;
399 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500400 if (ip->i_pdquot) {
401 xfs_qm_dqrele(ip->i_pdquot);
402 ip->i_pdquot = NULL;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Dave Chinnercd56a392013-08-28 10:18:07 +1000406struct xfs_qm_isolate {
407 struct list_head buffers;
408 struct list_head dispose;
409};
410
411static enum lru_status
412xfs_qm_dquot_isolate(
413 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800414 struct list_lru_one *lru,
Dave Chinnercd56a392013-08-28 10:18:07 +1000415 spinlock_t *lru_lock,
416 void *arg)
Dave Chinnerbf1ed382014-09-29 10:43:40 +1000417 __releases(lru_lock) __acquires(lru_lock)
Dave Chinnercd56a392013-08-28 10:18:07 +1000418{
419 struct xfs_dquot *dqp = container_of(item,
420 struct xfs_dquot, q_lru);
421 struct xfs_qm_isolate *isol = arg;
422
423 if (!xfs_dqlock_nowait(dqp))
424 goto out_miss_busy;
425
426 /*
Dave Chinner52f31ed2022-12-08 08:29:22 -0800427 * If something else is freeing this dquot and hasn't yet removed it
428 * from the LRU, leave it for the freeing task to complete the freeing
429 * process rather than risk it being free from under us here.
430 */
431 if (dqp->q_flags & XFS_DQFLAG_FREEING)
432 goto out_miss_unlock;
433
434 /*
Dave Chinnercd56a392013-08-28 10:18:07 +1000435 * This dquot has acquired a reference in the meantime remove it from
436 * the freelist and try again.
437 */
438 if (dqp->q_nrefs) {
439 xfs_dqunlock(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100440 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
Dave Chinnercd56a392013-08-28 10:18:07 +1000441
442 trace_xfs_dqreclaim_want(dqp);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800443 list_lru_isolate(lru, &dqp->q_lru);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100444 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner35163412013-08-28 10:18:08 +1000445 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000446 }
447
448 /*
449 * If the dquot is dirty, flush it. If it's already being flushed, just
450 * skip it so there is time for the IO to complete before we try to
451 * reclaim it again on the next LRU pass.
452 */
Dave Chinner52f31ed2022-12-08 08:29:22 -0800453 if (!xfs_dqflock_nowait(dqp))
454 goto out_miss_unlock;
Dave Chinnercd56a392013-08-28 10:18:07 +1000455
456 if (XFS_DQ_IS_DIRTY(dqp)) {
457 struct xfs_buf *bp = NULL;
458 int error;
459
460 trace_xfs_dqreclaim_dirty(dqp);
461
462 /* we have to drop the LRU lock to flush the dquot */
463 spin_unlock(lru_lock);
464
465 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700466 if (error)
Dave Chinnercd56a392013-08-28 10:18:07 +1000467 goto out_unlock_dirty;
Dave Chinnercd56a392013-08-28 10:18:07 +1000468
469 xfs_buf_delwri_queue(bp, &isol->buffers);
470 xfs_buf_relse(bp);
471 goto out_unlock_dirty;
472 }
473 xfs_dqfunlock(dqp);
474
475 /*
476 * Prevent lookups now that we are past the point of no return.
477 */
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700478 dqp->q_flags |= XFS_DQFLAG_FREEING;
Dave Chinnercd56a392013-08-28 10:18:07 +1000479 xfs_dqunlock(dqp);
480
481 ASSERT(dqp->q_nrefs == 0);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800482 list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100483 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinnercd56a392013-08-28 10:18:07 +1000484 trace_xfs_dqreclaim_done(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100485 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
Dave Chinner35163412013-08-28 10:18:08 +1000486 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000487
Dave Chinner52f31ed2022-12-08 08:29:22 -0800488out_miss_unlock:
489 xfs_dqunlock(dqp);
Dave Chinnercd56a392013-08-28 10:18:07 +1000490out_miss_busy:
491 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100492 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000493 return LRU_SKIP;
Dave Chinnercd56a392013-08-28 10:18:07 +1000494
495out_unlock_dirty:
496 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100497 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000498 xfs_dqunlock(dqp);
499 spin_lock(lru_lock);
500 return LRU_RETRY;
Dave Chinnercd56a392013-08-28 10:18:07 +1000501}
502
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000503static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000504xfs_qm_shrink_scan(
505 struct shrinker *shrink,
506 struct shrink_control *sc)
507{
Qi Zhengfe885272023-09-11 17:44:35 +0800508 struct xfs_quotainfo *qi = shrink->private_data;
Dave Chinnercd56a392013-08-28 10:18:07 +1000509 struct xfs_qm_isolate isol;
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000510 unsigned long freed;
Dave Chinnercd56a392013-08-28 10:18:07 +1000511 int error;
Dave Chinnercd56a392013-08-28 10:18:07 +1000512
Mel Gormand0164ad2015-11-06 16:28:21 -0800513 if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
Dave Chinnercd56a392013-08-28 10:18:07 +1000514 return 0;
515
516 INIT_LIST_HEAD(&isol.buffers);
517 INIT_LIST_HEAD(&isol.dispose);
518
Vladimir Davydov503c3582015-02-12 14:58:47 -0800519 freed = list_lru_shrink_walk(&qi->qi_lru, sc,
520 xfs_qm_dquot_isolate, &isol);
Dave Chinnercd56a392013-08-28 10:18:07 +1000521
522 error = xfs_buf_delwri_submit(&isol.buffers);
523 if (error)
524 xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
525
526 while (!list_empty(&isol.dispose)) {
527 struct xfs_dquot *dqp;
528
529 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
530 list_del_init(&dqp->q_lru);
531 xfs_qm_dqfree_one(dqp);
532 }
533
534 return freed;
535}
536
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000537static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000538xfs_qm_shrink_count(
539 struct shrinker *shrink,
540 struct shrink_control *sc)
541{
Qi Zhengfe885272023-09-11 17:44:35 +0800542 struct xfs_quotainfo *qi = shrink->private_data;
Dave Chinnercd56a392013-08-28 10:18:07 +1000543
Vladimir Davydov503c3582015-02-12 14:58:47 -0800544 return list_lru_shrink_count(&qi->qi_lru, sc);
Dave Chinnercd56a392013-08-28 10:18:07 +1000545}
546
Carlos Maiolinobe607942016-02-08 11:27:55 +1100547STATIC void
548xfs_qm_set_defquota(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800549 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700550 xfs_dqtype_t type,
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800551 struct xfs_quotainfo *qinf)
Carlos Maiolinobe607942016-02-08 11:27:55 +1100552{
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800553 struct xfs_dquot *dqp;
554 struct xfs_def_quota *defq;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100555 int error;
556
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700557 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700558 if (error)
559 return;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100560
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700561 defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
Carlos Maiolinobe607942016-02-08 11:27:55 +1100562
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700563 /*
564 * Timers and warnings have been already set, let's just set the
565 * default limits for this quota type
566 */
Darrick J. Wong438769e2020-07-14 10:37:32 -0700567 defq->blk.hard = dqp->q_blk.hardlimit;
568 defq->blk.soft = dqp->q_blk.softlimit;
569 defq->ino.hard = dqp->q_ino.hardlimit;
570 defq->ino.soft = dqp->q_ino.softlimit;
571 defq->rtb.hard = dqp->q_rtb.hardlimit;
572 defq->rtb.soft = dqp->q_rtb.softlimit;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700573 xfs_qm_dqdestroy(dqp);
574}
575
576/* Initialize quota time limits from the root dquot. */
577static void
578xfs_qm_init_timelimits(
579 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700580 xfs_dqtype_t type)
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700581{
Eric Sandeene8503012020-05-21 13:07:01 -0700582 struct xfs_quotainfo *qinf = mp->m_quotainfo;
583 struct xfs_def_quota *defq;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700584 struct xfs_dquot *dqp;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700585 int error;
586
Eric Sandeene8503012020-05-21 13:07:01 -0700587 defq = xfs_get_defquota(qinf, type);
588
Darrick J. Wong438769e2020-07-14 10:37:32 -0700589 defq->blk.time = XFS_QM_BTIMELIMIT;
590 defq->ino.time = XFS_QM_ITIMELIMIT;
591 defq->rtb.time = XFS_QM_RTBTIMELIMIT;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700592
593 /*
594 * We try to get the limits from the superuser's limits fields.
595 * This is quite hacky, but it is standard quota practice.
596 *
597 * Since we may not have done a quotacheck by this point, just read
598 * the dquot without attaching it to any hashtables or lists.
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700599 */
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700600 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
601 if (error)
602 return;
603
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700604 /*
605 * The warnings and timers set the grace period given to
606 * a user or group before he or she can not perform any
607 * more writing. If it is zero, a default is used.
608 */
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700609 if (dqp->q_blk.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700610 defq->blk.time = dqp->q_blk.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700611 if (dqp->q_ino.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700612 defq->ino.time = dqp->q_ino.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700613 if (dqp->q_rtb.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700614 defq->rtb.time = dqp->q_rtb.timer;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700615
616 xfs_qm_dqdestroy(dqp);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100617}
618
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100619/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * This initializes all the quota information that's kept in the
621 * mount structure
622 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000623STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624xfs_qm_init_quotainfo(
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700625 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700627 struct xfs_quotainfo *qinf;
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700628 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700630 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dave Chinner10634532024-01-16 09:59:39 +1100632 qinf = mp->m_quotainfo = kzalloc(sizeof(struct xfs_quotainfo),
633 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dave Chinner24513372014-06-25 14:58:08 +1000635 error = list_lru_init(&qinf->qi_lru);
Dave Chinneree4eec42014-05-15 09:23:24 +1000636 if (error)
637 goto out_free_qinf;
Glauber Costa5ca302c2013-08-28 10:18:18 +1000638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * See if quotainodes are setup, and if not, allocate them,
641 * and change the superblock accordingly.
642 */
Dave Chinneree4eec42014-05-15 09:23:24 +1000643 error = xfs_qm_init_quotainos(mp);
644 if (error)
645 goto out_free_lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Dave Chinner94a69db2024-01-16 09:59:45 +1100647 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_KERNEL);
648 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_KERNEL);
649 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_KERNEL);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000650 mutex_init(&qinf->qi_tree_lock);
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800653 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Precalc some constants */
656 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000657 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
Dave Chinner38c26bf2021-08-18 18:46:37 -0700658 if (xfs_has_bigtime(mp)) {
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700659 qinf->qi_expiry_min =
660 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
661 qinf->qi_expiry_max =
662 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
663 } else {
664 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
665 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
666 }
Darrick J. Wong06dbf822020-08-24 11:58:01 -0700667 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
668 qinf->qi_expiry_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
671
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700672 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
673 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
674 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700676 if (XFS_IS_UQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700677 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700678 if (XFS_IS_GQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700679 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700680 if (XFS_IS_PQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700681 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100682
Qi Zhengfe885272023-09-11 17:44:35 +0800683 qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s",
684 mp->m_super->s_id);
685 if (!qinf->qi_shrinker) {
686 error = -ENOMEM;
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800687 goto out_free_inos;
Qi Zhengfe885272023-09-11 17:44:35 +0800688 }
689
690 qinf->qi_shrinker->count_objects = xfs_qm_shrink_count;
691 qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan;
692 qinf->qi_shrinker->private_data = qinf;
693
694 shrinker_register(qinf->qi_shrinker);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800695
Darrick J. Wong20049182024-02-22 12:30:55 -0800696 xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks);
697 xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks);
698
Jesper Juhl014c2542006-01-15 02:37:08 +0100699 return 0;
Dave Chinneree4eec42014-05-15 09:23:24 +1000700
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800701out_free_inos:
702 mutex_destroy(&qinf->qi_quotaofflock);
703 mutex_destroy(&qinf->qi_tree_lock);
704 xfs_qm_destroy_quotainos(qinf);
Dave Chinneree4eec42014-05-15 09:23:24 +1000705out_free_lru:
706 list_lru_destroy(&qinf->qi_lru);
707out_free_qinf:
Dave Chinnerd4c75a12024-01-16 09:59:43 +1100708 kfree(qinf);
Dave Chinneree4eec42014-05-15 09:23:24 +1000709 mp->m_quotainfo = NULL;
710 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Gets called when unmounting a filesystem or when all quotas get
715 * turned off.
716 * This purges the quota inodes, destroys locks and frees itself.
717 */
718void
719xfs_qm_destroy_quotainfo(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800720 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800722 struct xfs_quotainfo *qi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 qi = mp->m_quotainfo;
725 ASSERT(qi != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Qi Zhengfe885272023-09-11 17:44:35 +0800727 shrinker_free(qi->qi_shrinker);
Glauber Costaf5e1dd32013-08-28 10:18:18 +1000728 list_lru_destroy(&qi->qi_lru);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800729 xfs_qm_destroy_quotainos(qi);
Aliaksei Karaliou21968812017-12-21 13:18:26 -0800730 mutex_destroy(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 mutex_destroy(&qi->qi_quotaofflock);
Dave Chinnerd4c75a12024-01-16 09:59:43 +1100732 kfree(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 mp->m_quotainfo = NULL;
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * Create an inode and return with a reference already taken, but unlocked
738 * This is how we create quota inodes
739 */
740STATIC int
741xfs_qm_qino_alloc(
Dave Chinner1abcf262020-12-09 10:05:15 -0800742 struct xfs_mount *mp,
743 struct xfs_inode **ipp,
744 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Dave Chinner1abcf262020-12-09 10:05:15 -0800746 struct xfs_trans *tp;
747 int error;
748 bool need_alloc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Chinner1abcf262020-12-09 10:05:15 -0800750 *ipp = NULL;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500751 /*
752 * With superblock that doesn't have separate pquotino, we
753 * share an inode between gquota and pquota. If the on-disk
754 * superblock has GQUOTA and the filesystem is now mounted
755 * with PQUOTA, just use sb_gquotino for sb_pquotino and
756 * vice-versa.
757 */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700758 if (!xfs_has_pquotino(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -0500759 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
760 xfs_ino_t ino = NULLFSINO;
761
762 if ((flags & XFS_QMOPT_PQUOTA) &&
763 (mp->m_sb.sb_gquotino != NULLFSINO)) {
764 ino = mp->m_sb.sb_gquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800765 if (XFS_IS_CORRUPT(mp,
Darrick J. Wong841a5f82024-02-22 12:32:44 -0800766 mp->m_sb.sb_pquotino != NULLFSINO)) {
767 xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700768 return -EFSCORRUPTED;
Darrick J. Wong841a5f82024-02-22 12:32:44 -0800769 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500770 } else if ((flags & XFS_QMOPT_GQUOTA) &&
771 (mp->m_sb.sb_pquotino != NULLFSINO)) {
772 ino = mp->m_sb.sb_pquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800773 if (XFS_IS_CORRUPT(mp,
Darrick J. Wong841a5f82024-02-22 12:32:44 -0800774 mp->m_sb.sb_gquotino != NULLFSINO)) {
775 xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700776 return -EFSCORRUPTED;
Darrick J. Wong841a5f82024-02-22 12:32:44 -0800777 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500778 }
779 if (ino != NULLFSINO) {
Dave Chinner1abcf262020-12-09 10:05:15 -0800780 error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500781 if (error)
782 return error;
783 mp->m_sb.sb_gquotino = NULLFSINO;
784 mp->m_sb.sb_pquotino = NULLFSINO;
Dave Chinner58c90472015-02-23 22:38:08 +1100785 need_alloc = false;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500786 }
787 }
788
Christoph Hellwig253f4912016-04-06 09:19:55 +1000789 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
Kaixu Xiafb353ff2020-04-22 21:54:28 -0700790 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0,
791 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000792 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dave Chinner58c90472015-02-23 22:38:08 +1100795 if (need_alloc) {
Darrick J. Wongba4b39f2024-07-02 11:22:34 -0700796 struct xfs_icreate_args args = {
797 .mode = S_IFREG,
Darrick J. Wongb11b11e2024-07-02 11:22:41 -0700798 .flags = XFS_ICREATE_UNLINKABLE,
Darrick J. Wongba4b39f2024-07-02 11:22:34 -0700799 };
Dave Chinnerb652afd2021-06-02 10:48:24 +1000800 xfs_ino_t ino;
801
802 error = xfs_dialloc(&tp, 0, S_IFREG, &ino);
803 if (!error)
Darrick J. Wongba4b39f2024-07-02 11:22:34 -0700804 error = xfs_icreate(tp, ino, &args, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500805 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000806 xfs_trans_cancel(tp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500807 return error;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * Make the changes in the superblock, and log those too.
813 * sbfields arg may contain fields other than *QUOTINO;
814 * VERSIONNUM for example.
815 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000816 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (flags & XFS_QMOPT_SBVERSION) {
Dave Chinner38c26bf2021-08-18 18:46:37 -0700818 ASSERT(!xfs_has_quota(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dave Chinner38c26bf2021-08-18 18:46:37 -0700820 xfs_add_quota(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 mp->m_sb.sb_uquotino = NULLFSINO;
822 mp->m_sb.sb_gquotino = NULLFSINO;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500823 mp->m_sb.sb_pquotino = NULLFSINO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Chandra Seetharamand892d582013-07-19 17:36:02 -0500825 /* qflags will get updated fully _after_ quotacheck */
826 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 if (flags & XFS_QMOPT_UQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800829 mp->m_sb.sb_uquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500830 else if (flags & XFS_QMOPT_GQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800831 mp->m_sb.sb_gquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500832 else
Dave Chinner1abcf262020-12-09 10:05:15 -0800833 mp->m_sb.sb_pquotino = (*ipp)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000834 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100835 xfs_log_sb(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Christoph Hellwig70393312015-06-04 13:48:08 +1000837 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100838 if (error) {
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700839 ASSERT(xfs_is_shutdown(mp));
Dave Chinner53487782011-03-07 10:05:35 +1100840 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Allison Henderson267979b2024-04-15 14:55:12 -0700842 if (need_alloc) {
843 xfs_iunlock(*ipp, XFS_ILOCK_EXCL);
Dave Chinner1abcf262020-12-09 10:05:15 -0800844 xfs_finish_inode_setup(*ipp);
Allison Henderson267979b2024-04-15 14:55:12 -0700845 }
Dave Chinner58c90472015-02-23 22:38:08 +1100846 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849
David Chinner5b139732008-04-10 12:20:10 +1000850STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851xfs_qm_reset_dqcounts(
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700852 struct xfs_mount *mp,
853 struct xfs_buf *bp,
854 xfs_dqid_t id,
855 xfs_dqtype_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Dave Chinner6fcdc592013-06-03 15:28:46 +1000857 struct xfs_dqblk *dqb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int j;
859
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000860 trace_xfs_reset_dqcounts(bp, _RET_IP_);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * Reset all counters and timers. They'll be
864 * started afresh by xfs_qm_quotacheck.
865 */
866#ifdef DEBUG
Eric Sandeen90115402017-04-19 12:55:57 -0700867 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
Christoph Hellwig11a83f42021-10-11 16:12:37 -0700868 sizeof(struct xfs_dqblk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000869 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#endif
Dave Chinner6fcdc592013-06-03 15:28:46 +1000871 dqb = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000872 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000873 struct xfs_disk_dquot *ddq;
874
875 ddq = (struct xfs_disk_dquot *)&dqb[j];
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * Do a sanity check, and if needed, repair the dqblk. Don't
879 * output any warnings because it's perfectly possible to
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800880 * find uninitialised dquot blks. See comment in
881 * xfs_dquot_verify.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 */
Darrick J. Wongf9751c42020-07-15 17:41:24 -0700883 if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700884 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700885 xfs_dqblk_repair(mp, &dqb[j], id + j, type);
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800886
Jan Karadfcc70a2015-02-23 22:34:17 +1100887 /*
888 * Reset type in case we are reusing group quota file for
889 * project quotas or vice versa
890 */
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700891 ddq->d_type = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100892 ddq->d_bcount = 0;
893 ddq->d_icount = 0;
894 ddq->d_rtbcount = 0;
Darrick J. Wong58855392020-03-24 20:12:53 -0700895
896 /*
897 * dquot id 0 stores the default grace period and the maximum
898 * warning limit that were set by the administrator, so we
899 * should not reset them.
900 */
901 if (ddq->d_id != 0) {
902 ddq->d_btimer = 0;
903 ddq->d_itimer = 0;
904 ddq->d_rtbtimer = 0;
905 ddq->d_bwarns = 0;
906 ddq->d_iwarns = 0;
907 ddq->d_rtbwarns = 0;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700908 if (xfs_has_bigtime(mp))
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700909 ddq->d_type |= XFS_DQTYPE_BIGTIME;
Darrick J. Wong58855392020-03-24 20:12:53 -0700910 }
Dave Chinner6fcdc592013-06-03 15:28:46 +1000911
Dave Chinner38c26bf2021-08-18 18:46:37 -0700912 if (xfs_has_crc(mp)) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000913 xfs_update_cksum((char *)&dqb[j],
914 sizeof(struct xfs_dqblk),
915 XFS_DQUOT_CRC_OFF);
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700921xfs_qm_reset_dqcounts_all(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000922 struct xfs_mount *mp,
923 xfs_dqid_t firstid,
924 xfs_fsblock_t bno,
925 xfs_filblks_t blkcnt,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700926 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000927 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000929 struct xfs_buf *bp;
Darrick J. Wong0dcc0722020-07-14 10:37:12 -0700930 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 ASSERT(blkcnt > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
935 * Blkcnt arg can be a very big number, and might even be
936 * larger than the log itself. So, we have to break it up into
937 * manageable-sized transactions.
938 * Note that we don't start a permanent transaction here; we might
939 * not be able to get a log reservation for the whole thing up front,
940 * and we don't really care to either, because we just discard
941 * everything if we were to crash in the middle of this loop.
942 */
943 while (blkcnt--) {
944 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
945 XFS_FSB_TO_DADDR(mp, bno),
Dave Chinnerc6319192012-11-14 17:50:13 +1100946 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100947 &xfs_dquot_buf_ops);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000948
949 /*
950 * CRC and validation errors will return a EFSCORRUPTED here. If
951 * this occurs, re-read without CRC validation so that we can
952 * repair the damage via xfs_qm_reset_dqcounts(). This process
953 * will leave a trace in the log indicating corruption has
954 * been detected.
955 */
Dave Chinner24513372014-06-25 14:58:08 +1000956 if (error == -EFSCORRUPTED) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000957 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
958 XFS_FSB_TO_DADDR(mp, bno),
959 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
960 NULL);
961 }
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (error)
964 break;
965
Dave Chinner5fd364f2014-08-04 12:43:26 +1000966 /*
967 * A corrupt buffer might not have a verifier attached, so
968 * make sure we have the correct one attached before writeback
969 * occurs.
970 */
971 bp->b_ops = &xfs_dquot_buf_ops;
David Chinner5b139732008-04-10 12:20:10 +1000972 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000973 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwig61551f12011-08-23 08:28:06 +0000974 xfs_buf_relse(bp);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000975
976 /* goto the next block. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000978 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000980
Jesper Juhl014c2542006-01-15 02:37:08 +0100981 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
Darrick J. Wong28b90602018-05-04 15:31:20 -0700985 * Iterate over all allocated dquot blocks in this quota inode, zeroing all
986 * counters for every chunk of dquots that we find.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
988STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700989xfs_qm_reset_dqcounts_buf(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000990 struct xfs_mount *mp,
991 struct xfs_inode *qip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700992 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000993 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000995 struct xfs_bmbt_irec *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 int i, nmaps; /* number of map entries */
997 int error; /* return value */
998 xfs_fileoff_t lblkno;
999 xfs_filblks_t maxlblkcnt;
1000 xfs_dqid_t firstid;
1001 xfs_fsblock_t rablkno;
1002 xfs_filblks_t rablkcnt;
1003
1004 error = 0;
1005 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001006 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 * trans_reserve. But, this gets called during quotacheck, and that
1008 * happens only at mount time which is single threaded.
1009 */
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001010 if (qip->i_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Chinnerf078d4e2024-01-16 09:59:40 +11001013 map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
1014 GFP_KERNEL | __GFP_NOFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 lblkno = 0;
Dave Chinner32972382012-06-08 15:44:54 +10001017 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 do {
Christoph Hellwigda51d322013-12-06 12:30:14 -08001019 uint lock_mode;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 nmaps = XFS_DQITER_MAP_SIZE;
1022 /*
1023 * We aren't changing the inode itself. Just changing
1024 * some of its data. No new blocks are added here, and
1025 * the inode is never added to the transaction.
1026 */
Christoph Hellwigda51d322013-12-06 12:30:14 -08001027 lock_mode = xfs_ilock_data_map_shared(qip);
Dave Chinner5c8ed202011-09-18 20:40:45 +00001028 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1029 map, &nmaps, 0);
Christoph Hellwigda51d322013-12-06 12:30:14 -08001030 xfs_iunlock(qip, lock_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (error)
1032 break;
1033
1034 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1035 for (i = 0; i < nmaps; i++) {
1036 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1037 ASSERT(map[i].br_blockcount);
1038
1039
1040 lblkno += map[i].br_blockcount;
1041
1042 if (map[i].br_startblock == HOLESTARTBLOCK)
1043 continue;
1044
1045 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001046 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * Do a read-ahead on the next extent.
1049 */
1050 if ((i+1 < nmaps) &&
1051 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1052 rablkcnt = map[i+1].br_blockcount;
1053 rablkno = map[i+1].br_startblock;
1054 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001055 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 XFS_FSB_TO_DADDR(mp, rablkno),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001057 mp->m_quotainfo->qi_dqchunklen,
Dave Chinner5fd364f2014-08-04 12:43:26 +10001058 &xfs_dquot_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 rablkno++;
1060 }
1061 }
1062 /*
1063 * Iterate thru all the blks in the extent and
1064 * reset the counters of all the dquots inside them.
1065 */
Darrick J. Wong28b90602018-05-04 15:31:20 -07001066 error = xfs_qm_reset_dqcounts_all(mp, firstid,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001067 map[i].br_startblock,
1068 map[i].br_blockcount,
Darrick J. Wong0dcc0722020-07-14 10:37:12 -07001069 type, buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001070 if (error)
1071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 } while (nmaps > 0);
1074
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001075out:
Dave Chinnerd4c75a12024-01-16 09:59:43 +11001076 kfree(map);
Jesper Juhl014c2542006-01-15 02:37:08 +01001077 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080/*
1081 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001082 *
1083 * Given the inode, and a dquot id this updates both the incore dqout as well
1084 * as the buffer copy. This is so that once the quotacheck is done, we can
1085 * just log all the buffers, as opposed to logging numerous updates to
1086 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001088STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001090 struct xfs_inode *ip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -07001091 xfs_dqtype_t type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xfs_qcnt_t nblks,
1093 xfs_qcnt_t rtblks)
1094{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001095 struct xfs_mount *mp = ip->i_mount;
1096 struct xfs_dquot *dqp;
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001097 xfs_dqid_t id;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001098 int error;
1099
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001100 id = xfs_qm_id_for_quotatype(ip, type);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -07001101 error = xfs_qm_dqget(mp, id, type, true, &dqp);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001102 if (error) {
1103 /*
1104 * Shouldn't be able to turn off quotas here.
1105 */
Dave Chinner24513372014-06-25 14:58:08 +10001106 ASSERT(error != -ESRCH);
1107 ASSERT(error != -ENOENT);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001108 return error;
1109 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001110
1111 trace_xfs_dqadjust(dqp);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * Adjust the inode count and the block count to reflect this inode's
1115 * resource usage.
1116 */
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001117 dqp->q_ino.count++;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001118 dqp->q_ino.reserved++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (nblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001120 dqp->q_blk.count += nblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001121 dqp->q_blk.reserved += nblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 if (rtblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001124 dqp->q_rtb.count += rtblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001125 dqp->q_rtb.reserved += rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 /*
1129 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001130 *
1131 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Darrick J. Wongc51df732020-07-14 10:37:30 -07001133 if (dqp->q_id) {
Darrick J. Wongc8c753e2020-07-14 10:37:33 -07001134 xfs_qm_adjust_dqlimits(dqp);
1135 xfs_qm_adjust_dqtimers(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001138 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001139 xfs_qm_dqput(dqp);
1140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * callback routine supplied to bulkstat(). Given an inumber, find its
1145 * dquots and update them to account for resources taken by that inode.
1146 */
1147/* ARGSUSED */
1148STATIC int
1149xfs_qm_dqusage_adjust(
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001150 struct xfs_mount *mp,
1151 struct xfs_trans *tp,
1152 xfs_ino_t ino,
1153 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001155 struct xfs_inode *ip;
1156 xfs_qcnt_t nblks;
1157 xfs_filblks_t rtblks = 0; /* total rt blks */
1158 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001160 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /*
1163 * rootino must have its resources accounted for, not so with the quota
1164 * inodes.
1165 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001166 if (xfs_is_quota_inode(&mp->m_sb, ino))
1167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 /*
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001170 * We don't _need_ to take the ilock EXCL here because quotacheck runs
1171 * at mount time and therefore nobody will be racing chown/chproj.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001173 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
1174 if (error == -EINVAL || error == -ENOENT)
1175 return 0;
1176 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001177 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Darrick J. Wong537c0132023-09-24 08:35:53 -07001179 /*
1180 * Reload the incore unlinked list to avoid failure in inodegc.
1181 * Use an unlocked check here because unrecovered unlinked inodes
1182 * should be somewhat rare.
1183 */
1184 if (xfs_inode_unlinked_incomplete(ip)) {
1185 error = xfs_inode_reload_unlinked(ip);
1186 if (error) {
1187 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1188 goto error0;
1189 }
1190 }
Darrick J. Wong49813a22023-09-11 08:39:08 -07001191
Christoph Hellwig52fda112010-09-06 01:44:22 +00001192 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Christoph Hellwig52fda112010-09-06 01:44:22 +00001194 if (XFS_IS_REALTIME_INODE(ip)) {
Darrick J. Wong732436e2022-07-09 10:56:05 -07001195 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001196
Christoph Hellwig862a8042021-04-13 11:15:09 -07001197 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1198 if (error)
1199 goto error0;
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001200
1201 xfs_bmap_count_leaves(ifp, &rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001204 nblks = (xfs_qcnt_t)ip->i_nblocks - rtblks;
Darrick J. Wong49813a22023-09-11 08:39:08 -07001205 xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /*
1208 * Add the (disk blocks and inode) resources occupied by this
1209 * inode to its dquots. We do this adjustment in the incore dquot,
1210 * and also copy the changes to its buffer.
1211 * We don't care about putting these changes in a transaction
1212 * envelope because if we crash in the middle of a 'quotacheck'
1213 * we have to start from the beginning anyway.
1214 * Once we're done, we'll log all the dquot bufs.
1215 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001216 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1218 */
1219 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001220 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001221 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001222 if (error)
1223 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Christoph Hellwig52fda112010-09-06 01:44:22 +00001226 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001227 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001228 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001229 if (error)
1230 goto error0;
1231 }
1232
1233 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001234 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001235 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001236 if (error)
1237 goto error0;
1238 }
1239
Christoph Hellwig52fda112010-09-06 01:44:22 +00001240error0:
Darrick J. Wong44a87362018-07-25 12:52:32 -07001241 xfs_irele(ip);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001245STATIC int
1246xfs_qm_flush_one(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001247 struct xfs_dquot *dqp,
1248 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001249{
Brian Foster7912e7f2017-06-14 21:21:45 -07001250 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001251 struct list_head *buffer_list = data;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001252 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001253 int error = 0;
1254
1255 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001256 if (dqp->q_flags & XFS_DQFLAG_FREEING)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001257 goto out_unlock;
1258 if (!XFS_DQ_IS_DIRTY(dqp))
1259 goto out_unlock;
1260
Brian Foster7912e7f2017-06-14 21:21:45 -07001261 /*
1262 * The only way the dquot is already flush locked by the time quotacheck
1263 * gets here is if reclaim flushed it before the dqadjust walk dirtied
1264 * it for the final time. Quotacheck collects all dquot bufs in the
1265 * local delwri queue before dquots are dirtied, so reclaim can't have
1266 * possibly queued it for I/O. The only way out is to push the buffer to
1267 * cycle the flush lock.
1268 */
1269 if (!xfs_dqflock_nowait(dqp)) {
1270 /* buf is pinned in-core by delwri list */
Dave Chinner85c73bf72022-07-07 22:05:18 +10001271 error = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
1272 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1273 if (error)
Brian Foster7912e7f2017-06-14 21:21:45 -07001274 goto out_unlock;
Dave Chinner85c73bf72022-07-07 22:05:18 +10001275
Darrick J. Wongf0c2d7d2022-08-03 17:33:00 -07001276 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1277 error = -EAGAIN;
1278 xfs_buf_relse(bp);
1279 goto out_unlock;
1280 }
Brian Foster7912e7f2017-06-14 21:21:45 -07001281 xfs_buf_unlock(bp);
1282
1283 xfs_buf_delwri_pushbuf(bp, buffer_list);
1284 xfs_buf_rele(bp);
1285
1286 error = -EAGAIN;
1287 goto out_unlock;
1288 }
1289
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001290 error = xfs_qm_dqflush(dqp, &bp);
1291 if (error)
1292 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001293
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001294 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001295 xfs_buf_relse(bp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001296out_unlock:
1297 xfs_dqunlock(dqp);
1298 return error;
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/*
1302 * Walk thru all the filesystem inodes and construct a consistent view
1303 * of the disk quota world. If the quotacheck fails, disable quotas.
1304 */
Jie Liueb866bb2014-07-24 20:49:57 +10001305STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306xfs_qm_quotacheck(
1307 xfs_mount_t *mp)
1308{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001309 int error, error2;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001310 uint flags;
1311 LIST_HEAD (buffer_list);
1312 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
1313 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001314 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 flags = 0;
1317
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001318 ASSERT(uip || gip || pip);
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001319 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Chinner0b932cc2011-03-07 10:08:35 +11001321 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001324 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * their counters to zero. We need a clean slate.
1326 * We don't log our changes till later.
1327 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001328 if (uip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001329 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001330 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001331 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 goto error_return;
1333 flags |= XFS_UQUOTA_CHKD;
1334 }
1335
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001336 if (gip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001337 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001338 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001339 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 goto error_return;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001341 flags |= XFS_GQUOTA_CHKD;
1342 }
1343
1344 if (pip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001345 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001346 &buffer_list);
1347 if (error)
1348 goto error_return;
1349 flags |= XFS_PQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Darrick J. Wong49813a22023-09-11 08:39:08 -07001352 xfs_set_quotacheck_running(mp);
Darrick J. Wong13d59a22019-07-03 20:36:28 -07001353 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
1354 NULL);
Darrick J. Wong49813a22023-09-11 08:39:08 -07001355 xfs_clear_quotacheck_running(mp);
Dave Chinner0c7273e2023-03-05 15:13:22 -08001356
1357 /*
1358 * On error, the inode walk may have partially populated the dquot
1359 * caches. We must purge them before disabling quota and tearing down
1360 * the quotainfo, or else the dquots will leak.
1361 */
1362 if (error)
1363 goto error_purge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001366 * We've made all the changes that we need to make incore. Flush them
1367 * down to disk buffers if everything was updated successfully.
David Chinner4b8879d2008-04-10 12:20:17 +10001368 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001369 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001370 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001371 &buffer_list);
1372 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001373 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001374 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001375 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001376 if (!error)
1377 error = error2;
1378 }
1379 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001380 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001381 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001382 if (!error)
1383 error = error2;
1384 }
David Chinner4b8879d2008-04-10 12:20:17 +10001385
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001386 error2 = xfs_buf_delwri_submit(&buffer_list);
1387 if (!error)
1388 error = error2;
1389
David Chinner4b8879d2008-04-10 12:20:17 +10001390 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * We can get this error if we couldn't do a dquot allocation inside
1392 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1393 * dirty dquots that might be cached, we just want to get rid of them
1394 * and turn quotaoff. The dquots won't be attached to any of the inodes
1395 * at this point (because we intentionally didn't in dqget_noattach).
1396 */
Dave Chinner0c7273e2023-03-05 15:13:22 -08001397 if (error)
1398 goto error_purge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * If one type of quotas is off, then it will lose its
1402 * quotachecked status, since we won't be doing accounting for
1403 * that type anymore.
1404 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001405 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 mp->m_qflags |= flags;
1407
Dave Chinner0c7273e2023-03-05 15:13:22 -08001408error_return:
Brian Foster20e8a062017-04-21 12:40:44 -07001409 xfs_buf_delwri_cancel(&buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001412 xfs_warn(mp,
1413 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1414 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /*
1416 * We must turn off quotas.
1417 */
1418 ASSERT(mp->m_quotainfo != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001420 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001421 xfs_warn(mp,
1422 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001423 }
Darrick J. Wong3d8f1422024-02-22 12:30:51 -08001424 xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
1425 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001426 xfs_notice(mp, "Quotacheck: Done.");
Darrick J. Wong3d8f1422024-02-22 12:30:51 -08001427 xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK);
1428 }
1429
Eric Sandeend99831f2014-06-22 15:03:54 +10001430 return error;
Dave Chinner0c7273e2023-03-05 15:13:22 -08001431
1432error_purge:
1433 /*
1434 * On error, we may have inodes queued for inactivation. This may try
1435 * to attach dquots to the inode before running cleanup operations on
1436 * the inode and this can race with the xfs_qm_destroy_quotainfo() call
1437 * below that frees mp->m_quotainfo. To avoid this race, flush all the
1438 * pending inodegc operations before we purge the dquots from memory,
1439 * ensuring that background inactivation is idle whilst we turn off
1440 * quotas.
1441 */
1442 xfs_inodegc_flush(mp);
1443 xfs_qm_dqpurge_all(mp);
1444 goto error_return;
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448/*
Jie Liueb866bb2014-07-24 20:49:57 +10001449 * This is called from xfs_mountfs to start quotas and initialize all
1450 * necessary data structures like quotainfo. This is also responsible for
1451 * running a quotacheck as necessary. We are guaranteed that the superblock
1452 * is consistently read in at this point.
1453 *
1454 * If we fail here, the mount will continue with quota turned off. We don't
1455 * need to inidicate success or failure at all.
1456 */
1457void
1458xfs_qm_mount_quotas(
1459 struct xfs_mount *mp)
1460{
1461 int error = 0;
1462 uint sbf;
1463
1464 /*
1465 * If quotas on realtime volumes is not supported, we disable
1466 * quotas immediately.
1467 */
1468 if (mp->m_sb.sb_rextents) {
1469 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
1470 mp->m_qflags = 0;
1471 goto write_changes;
1472 }
1473
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001474 ASSERT(XFS_IS_QUOTA_ON(mp));
Jie Liueb866bb2014-07-24 20:49:57 +10001475
1476 /*
1477 * Allocate the quotainfo structure inside the mount struct, and
1478 * create quotainode(s), and change/rev superblock if necessary.
1479 */
1480 error = xfs_qm_init_quotainfo(mp);
1481 if (error) {
1482 /*
1483 * We must turn off quotas.
1484 */
1485 ASSERT(mp->m_quotainfo == NULL);
1486 mp->m_qflags = 0;
1487 goto write_changes;
1488 }
1489 /*
1490 * If any of the quotas are not consistent, do a quotacheck.
1491 */
1492 if (XFS_QM_NEED_QUOTACHECK(mp)) {
1493 error = xfs_qm_quotacheck(mp);
1494 if (error) {
1495 /* Quotacheck failed and disabled quotas. */
1496 return;
1497 }
1498 }
1499 /*
1500 * If one type of quotas is off, then it will lose its
1501 * quotachecked status, since we won't be doing accounting for
1502 * that type anymore.
1503 */
1504 if (!XFS_IS_UQUOTA_ON(mp))
1505 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
1506 if (!XFS_IS_GQUOTA_ON(mp))
1507 mp->m_qflags &= ~XFS_GQUOTA_CHKD;
1508 if (!XFS_IS_PQUOTA_ON(mp))
1509 mp->m_qflags &= ~XFS_PQUOTA_CHKD;
1510
1511 write_changes:
1512 /*
1513 * We actually don't have to acquire the m_sb_lock at all.
1514 * This can only be called from mount, and that's single threaded. XXX
1515 */
1516 spin_lock(&mp->m_sb_lock);
1517 sbf = mp->m_sb.sb_qflags;
1518 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
1519 spin_unlock(&mp->m_sb_lock);
1520
1521 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001522 if (xfs_sync_sb(mp, false)) {
Jie Liueb866bb2014-07-24 20:49:57 +10001523 /*
1524 * We could only have been turning quotas off.
1525 * We aren't in very good shape actually because
1526 * the incore structures are convinced that quotas are
1527 * off, but the on disk superblock doesn't know that !
1528 */
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001529 ASSERT(!(XFS_IS_QUOTA_ON(mp)));
Jie Liueb866bb2014-07-24 20:49:57 +10001530 xfs_alert(mp, "%s: Superblock update failed!",
1531 __func__);
1532 }
1533 }
1534
1535 if (error) {
1536 xfs_warn(mp, "Failed to initialize disk quotas.");
1537 return;
1538 }
1539}
1540
1541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * This is called after the superblock has been read in and we're ready to
1543 * iget the quota inodes.
1544 */
1545STATIC int
1546xfs_qm_init_quotainos(
1547 xfs_mount_t *mp)
1548{
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001549 struct xfs_inode *uip = NULL;
1550 struct xfs_inode *gip = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001551 struct xfs_inode *pip = NULL;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001552 int error;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001553 uint flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 ASSERT(mp->m_quotainfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /*
1558 * Get the uquota and gquota inodes
1559 */
Dave Chinner38c26bf2021-08-18 18:46:37 -07001560 if (xfs_has_quota(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (XFS_IS_UQUOTA_ON(mp) &&
1562 mp->m_sb.sb_uquotino != NULLFSINO) {
1563 ASSERT(mp->m_sb.sb_uquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001564 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1565 0, 0, &uip);
1566 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001567 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001569 if (XFS_IS_GQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 mp->m_sb.sb_gquotino != NULLFSINO) {
1571 ASSERT(mp->m_sb.sb_gquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001572 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1573 0, 0, &gip);
1574 if (error)
1575 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001577 if (XFS_IS_PQUOTA_ON(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -05001578 mp->m_sb.sb_pquotino != NULLFSINO) {
1579 ASSERT(mp->m_sb.sb_pquotino > 0);
1580 error = xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001581 0, 0, &pip);
1582 if (error)
1583 goto error_rele;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 } else {
1586 flags |= XFS_QMOPT_SBVERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588
1589 /*
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001590 * Create the three inodes, if they don't exist already. The changes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * made above will get added to a transaction and logged in one of
1592 * the qino_alloc calls below. If the device is readonly,
1593 * temporarily switch to read-write to do this.
1594 */
1595 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001596 error = xfs_qm_qino_alloc(mp, &uip,
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001597 flags | XFS_QMOPT_UQUOTA);
1598 if (error)
1599 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 flags &= ~XFS_QMOPT_SBVERSION;
1602 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001603 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001604 error = xfs_qm_qino_alloc(mp, &gip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001605 flags | XFS_QMOPT_GQUOTA);
1606 if (error)
1607 goto error_rele;
1608
1609 flags &= ~XFS_QMOPT_SBVERSION;
1610 }
1611 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001612 error = xfs_qm_qino_alloc(mp, &pip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001613 flags | XFS_QMOPT_PQUOTA);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001614 if (error)
1615 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001618 mp->m_quotainfo->qi_uquotaip = uip;
1619 mp->m_quotainfo->qi_gquotaip = gip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001620 mp->m_quotainfo->qi_pquotaip = pip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Jesper Juhl014c2542006-01-15 02:37:08 +01001622 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001623
1624error_rele:
1625 if (uip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001626 xfs_irele(uip);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001627 if (gip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001628 xfs_irele(gip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001629 if (pip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001630 xfs_irele(pip);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001631 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001634STATIC void
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001635xfs_qm_destroy_quotainos(
Pavel Reichlc072fbe2019-11-12 17:04:26 -08001636 struct xfs_quotainfo *qi)
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001637{
1638 if (qi->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001639 xfs_irele(qi->qi_uquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001640 qi->qi_uquotaip = NULL; /* paranoia */
1641 }
1642 if (qi->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001643 xfs_irele(qi->qi_gquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001644 qi->qi_gquotaip = NULL;
1645 }
1646 if (qi->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001647 xfs_irele(qi->qi_pquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001648 qi->qi_pquotaip = NULL;
1649 }
1650}
1651
1652STATIC void
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001653xfs_qm_dqfree_one(
1654 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001656 struct xfs_mount *mp = dqp->q_mount;
1657 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Christoph Hellwig9f920f12012-03-13 08:52:35 +00001659 mutex_lock(&qi->qi_tree_lock);
Darrick J. Wong51dbb1b2020-07-14 10:37:32 -07001660 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001661
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001662 qi->qi_dquots--;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001663 mutex_unlock(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001665 xfs_qm_dqdestroy(dqp);
1666}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/* --------------- utility functions for vnodeops ---------------- */
1669
1670
1671/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001672 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1674 * quotas by creating this file.
1675 * This also attaches dquot(s) to the given inode after locking it,
1676 * and returns the dquots corresponding to the uid and/or gid.
1677 *
1678 * in : inode (unlocked)
1679 * out : udquot, gdquot with references taken and unlocked
1680 */
1681int
1682xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001683 struct xfs_inode *ip,
Christoph Hellwig54295152020-02-21 08:31:27 -08001684 kuid_t uid,
1685 kgid_t gid,
Christoph Hellwig7d095252009-06-08 15:33:32 +02001686 prid_t prid,
1687 uint flags,
1688 struct xfs_dquot **O_udqpp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001689 struct xfs_dquot **O_gdqpp,
1690 struct xfs_dquot **O_pdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001692 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig54295152020-02-21 08:31:27 -08001693 struct inode *inode = VFS_I(ip);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001694 struct user_namespace *user_ns = inode->i_sb->s_user_ns;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001695 struct xfs_dquot *uq = NULL;
1696 struct xfs_dquot *gq = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001697 struct xfs_dquot *pq = NULL;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001698 int error;
1699 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001701 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 0;
1703
1704 lockflags = XFS_ILOCK_EXCL;
1705 xfs_ilock(ip, lockflags);
1706
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001707 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Christoph Hellwig54295152020-02-21 08:31:27 -08001708 gid = inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /*
1711 * Attach the dquot(s) to this inode, doing a dquot allocation
1712 * if necessary. The dquot(s) will not be locked.
1713 */
1714 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wong4882c192018-05-04 15:30:22 -07001715 error = xfs_qm_dqattach_locked(ip, true);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001716 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001718 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 }
1721
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001722 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001723 ASSERT(O_udqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001724 if (!uid_eq(inode->i_uid, uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /*
1726 * What we need is the dquot that has this uid, and
1727 * if we send the inode to dqget, the uid of the inode
1728 * takes priority over what's sent in the uid argument.
1729 * We must unlock inode here before calling dqget if
1730 * we're not sending the inode, because otherwise
1731 * we'll deadlock by doing trans_reserve while
1732 * holding ilock.
1733 */
1734 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001735 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001736 XFS_DQTYPE_USER, true, &uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001737 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001738 ASSERT(error != -ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 /*
1742 * Get the ilock in the right order.
1743 */
1744 xfs_dqunlock(uq);
1745 lockflags = XFS_ILOCK_SHARED;
1746 xfs_ilock(ip, lockflags);
1747 } else {
1748 /*
1749 * Take an extra reference, because we'll return
1750 * this to caller
1751 */
1752 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001753 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001756 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001757 ASSERT(O_gdqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001758 if (!gid_eq(inode->i_gid, gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001760 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001761 XFS_DQTYPE_GROUP, true, &gq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001762 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001763 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001764 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 xfs_dqunlock(gq);
1767 lockflags = XFS_ILOCK_SHARED;
1768 xfs_ilock(ip, lockflags);
1769 } else {
1770 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001771 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001773 }
1774 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001775 ASSERT(O_pdqpp);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001776 if (ip->i_projid != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001777 xfs_iunlock(ip, lockflags);
Kaixu Xia5aff6752020-09-16 14:31:55 -07001778 error = xfs_qm_dqget(mp, prid,
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001779 XFS_DQTYPE_PROJ, true, &pq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001780 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001781 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001782 goto error_rele;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001783 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001784 xfs_dqunlock(pq);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001785 lockflags = XFS_ILOCK_SHARED;
1786 xfs_ilock(ip, lockflags);
1787 } else {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001788 ASSERT(ip->i_pdquot);
1789 pq = xfs_qm_dqhold(ip->i_pdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Kaixu Xiac1407352020-04-22 21:54:27 -07001792 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 xfs_iunlock(ip, lockflags);
1795 if (O_udqpp)
1796 *O_udqpp = uq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001797 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 xfs_qm_dqrele(uq);
1799 if (O_gdqpp)
1800 *O_gdqpp = gq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001801 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 xfs_qm_dqrele(gq);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001803 if (O_pdqpp)
1804 *O_pdqpp = pq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001805 else
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001806 xfs_qm_dqrele(pq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001807 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001808
1809error_rele:
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001810 xfs_qm_dqrele(gq);
1811 xfs_qm_dqrele(uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001812 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816 * Actually transfer ownership, and do dquot modifications.
1817 * These were already reserved.
1818 */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001819struct xfs_dquot *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820xfs_qm_vop_chown(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001821 struct xfs_trans *tp,
1822 struct xfs_inode *ip,
1823 struct xfs_dquot **IO_olddq,
1824 struct xfs_dquot *newdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001826 struct xfs_dquot *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001827 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1828 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1829
Christoph Hellwig7d095252009-06-08 15:33:32 +02001830
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00001831 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001832 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 /* old dquot */
1835 prevdq = *IO_olddq;
1836 ASSERT(prevdq);
1837 ASSERT(prevdq != newdq);
1838
Darrick J. Wong20049182024-02-22 12:30:55 -08001839 xfs_trans_mod_ino_dquot(tp, ip, prevdq, bfield, -(ip->i_nblocks));
1840 xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* the sparkling new dquot */
Darrick J. Wong20049182024-02-22 12:30:55 -08001843 xfs_trans_mod_ino_dquot(tp, ip, newdq, bfield, ip->i_nblocks);
1844 xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 /*
Darrick J. Wong1aecf372021-01-29 19:06:10 -08001847 * Back when we made quota reservations for the chown, we reserved the
1848 * ondisk blocks + delalloc blocks with the new dquot. Now that we've
1849 * switched the dquots, decrease the new dquot's block reservation
1850 * (having already bumped up the real counter) so that we don't have
1851 * any reservation to give back when we commit.
1852 */
1853 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_RES_BLKS,
1854 -ip->i_delayed_blks);
1855
1856 /*
1857 * Give the incore reservation for delalloc blocks back to the old
1858 * dquot. We don't normally handle delalloc quota reservations
1859 * transactionally, so just lock the dquot and subtract from the
1860 * reservation. Dirty the transaction because it's too late to turn
1861 * back now.
1862 */
1863 tp->t_flags |= XFS_TRANS_DIRTY;
1864 xfs_dqlock(prevdq);
1865 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
1866 prevdq->q_blk.reserved -= ip->i_delayed_blks;
1867 xfs_dqunlock(prevdq);
1868
1869 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001870 * Take an extra reference, because the inode is going to keep
1871 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001873 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jesper Juhl014c2542006-01-15 02:37:08 +01001875 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878int
1879xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001880 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001882 struct xfs_mount *mp = i_tab[0]->i_mount;
1883 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001885 if (!XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01001886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Christoph Hellwig7d095252009-06-08 15:33:32 +02001888 for (i = 0; (i < 4 && i_tab[i]); i++) {
1889 struct xfs_inode *ip = i_tab[i];
1890 int error;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * Watch out for duplicate entries in the table.
1894 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001895 if (i == 0 || ip != i_tab[i-1]) {
1896 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001897 error = xfs_qm_dqattach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001899 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 }
1902 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
1906void
Christoph Hellwig7d095252009-06-08 15:33:32 +02001907xfs_qm_vop_create_dqattach(
1908 struct xfs_trans *tp,
1909 struct xfs_inode *ip,
1910 struct xfs_dquot *udqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001911 struct xfs_dquot *gdqp,
1912 struct xfs_dquot *pdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001914 struct xfs_mount *mp = tp->t_mountp;
1915
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001916 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return;
1918
Matthew Wilcox (Oracle)3fed24f2024-02-19 15:41:12 +00001919 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Jie Liu37eb9702013-11-26 21:38:54 +08001921 if (udqp && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ASSERT(ip->i_udquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001923 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001924
1925 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Jie Liu37eb9702013-11-26 21:38:54 +08001927 if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 ASSERT(ip->i_gdquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001929 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
Christoph Hellwig54295152020-02-21 08:31:27 -08001930
Christoph Hellwig78e55892011-12-06 21:58:22 +00001931 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Jie Liu37eb9702013-11-26 21:38:54 +08001933 if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001934 ASSERT(ip->i_pdquot == NULL);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001935 ASSERT(ip->i_projid == pdqp->q_id);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001936
1937 ip->i_pdquot = xfs_qm_dqhold(pdqp);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001938 }
Darrick J. Wong20049182024-02-22 12:30:55 -08001939
1940 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Darrick J. Wong108523b2021-08-06 11:05:40 -07001943/* Decide if this inode's dquot is near an enforcement boundary. */
1944bool
1945xfs_inode_near_dquot_enforcement(
1946 struct xfs_inode *ip,
1947 xfs_dqtype_t type)
1948{
1949 struct xfs_dquot *dqp;
1950 int64_t freesp;
1951
1952 /* We only care for quotas that are enabled and enforced. */
1953 dqp = xfs_inode_dquot(ip, type);
1954 if (!dqp || !xfs_dquot_is_enforced(dqp))
1955 return false;
1956
1957 if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
1958 xfs_dquot_res_over_limits(&dqp->q_rtb))
1959 return true;
1960
1961 /* For space on the data device, check the various thresholds. */
1962 if (!dqp->q_prealloc_hi_wmark)
1963 return false;
1964
1965 if (dqp->q_blk.reserved < dqp->q_prealloc_lo_wmark)
1966 return false;
1967
1968 if (dqp->q_blk.reserved >= dqp->q_prealloc_hi_wmark)
1969 return true;
1970
1971 freesp = dqp->q_prealloc_hi_wmark - dqp->q_blk.reserved;
1972 if (freesp < dqp->q_low_space[XFS_QLOWSP_5_PCNT])
1973 return true;
1974
1975 return false;
1976}