blob: 94a7932ac570055e910c2a97ddf2e44bba5a9576 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * The global quota manager. There is only one of these for the entire
32 * system, _not_ one per file system. XQM keeps track of the overall
33 * quota functionality, including maintaining the freelist and hash
34 * tables of dquots.
35 */
Pavel Reichlc072fbe2019-11-12 17:04:26 -080036STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp);
37STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Pavel Reichlc072fbe2019-11-12 17:04:26 -080039STATIC void xfs_qm_destroy_quotainos(struct xfs_quotainfo *qi);
Dave Chinnercd56a392013-08-28 10:18:07 +100040STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050042 * We use the batch lookup interface to iterate over the dquots as it
43 * currently is the only interface into the radix tree code that allows
44 * fuzzy lookups instead of exact matches. Holding the lock over multiple
45 * operations is fine as all callers are used either during mount/umount
46 * or quotaoff.
47 */
48#define XFS_DQ_LOOKUP_BATCH 32
49
50STATIC int
51xfs_qm_dquot_walk(
52 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -070053 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +100054 int (*execute)(struct xfs_dquot *dqp, void *data),
55 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050056{
57 struct xfs_quotainfo *qi = mp->m_quotainfo;
Chandra Seetharaman329e0872013-06-27 17:25:05 -050058 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050059 uint32_t next_index;
60 int last_error = 0;
61 int skipped;
62 int nr_found;
63
64restart:
65 skipped = 0;
66 next_index = 0;
67 nr_found = 0;
68
69 while (1) {
70 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
Li zeminge1956052023-01-03 09:54:28 -080071 int error;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050072 int i;
73
74 mutex_lock(&qi->qi_tree_lock);
75 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
76 next_index, XFS_DQ_LOOKUP_BATCH);
77 if (!nr_found) {
78 mutex_unlock(&qi->qi_tree_lock);
79 break;
80 }
81
82 for (i = 0; i < nr_found; i++) {
83 struct xfs_dquot *dqp = batch[i];
84
Darrick J. Wongc51df732020-07-14 10:37:30 -070085 next_index = dqp->q_id + 1;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050086
Christoph Hellwig43ff2122012-04-23 15:58:39 +100087 error = execute(batch[i], data);
Dave Chinner24513372014-06-25 14:58:08 +100088 if (error == -EAGAIN) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050089 skipped++;
90 continue;
91 }
Dave Chinner24513372014-06-25 14:58:08 +100092 if (error && last_error != -EFSCORRUPTED)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050093 last_error = error;
94 }
95
96 mutex_unlock(&qi->qi_tree_lock);
97
98 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +100099 if (last_error == -EFSCORRUPTED) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500100 skipped = 0;
101 break;
102 }
Brian Fostercfaf2d02017-07-24 08:33:25 -0700103 /* we're done if id overflows back to zero */
104 if (!next_index)
105 break;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500106 }
107
108 if (skipped) {
109 delay(1);
110 goto restart;
111 }
112
113 return last_error;
114}
115
116
117/*
118 * Purge a dquot from all tracking data structures and free it.
119 */
120STATIC int
121xfs_qm_dqpurge(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000122 struct xfs_dquot *dqp,
123 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500124{
Dave Chinner01728b42022-03-17 09:09:13 -0700125 struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700126 int error = -EAGAIN;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500127
128 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700129 if ((dqp->q_flags & XFS_DQFLAG_FREEING) || dqp->q_nrefs != 0)
Brian Foster8d3d7e22020-03-27 08:29:45 -0700130 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500131
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700132 dqp->q_flags |= XFS_DQFLAG_FREEING;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500133
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000134 xfs_dqflock(dqp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500135
136 /*
137 * If we are turning this type of quotas off, we don't care
138 * about the dirty metadata sitting in this dquot. OTOH, if
139 * we're unmounting, we do care, so we flush it and wait.
140 */
141 if (XFS_DQ_IS_DIRTY(dqp)) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000142 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500143
144 /*
145 * We don't care about getting disk errors here. We need
146 * to purge this dquot anyway, so we go ahead regardless.
147 */
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000148 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700149 if (!error) {
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000150 error = xfs_bwrite(bp);
151 xfs_buf_relse(bp);
Brian Foster8d3d7e22020-03-27 08:29:45 -0700152 } else if (error == -EAGAIN) {
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700153 dqp->q_flags &= ~XFS_DQFLAG_FREEING;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700154 goto out_unlock;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000155 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500156 xfs_dqflock(dqp);
157 }
158
159 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Dave Chinner01728b42022-03-17 09:09:13 -0700160 ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) ||
Dave Chinner22525c12018-05-09 07:47:34 -0700161 !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500162
163 xfs_dqfunlock(dqp);
164 xfs_dqunlock(dqp);
165
Darrick J. Wong51dbb1b2020-07-14 10:37:32 -0700166 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500167 qi->qi_dquots--;
168
169 /*
170 * We move dquots to the freelist as soon as their reference count
171 * hits zero, so it really should be on the freelist here.
172 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500173 ASSERT(!list_empty(&dqp->q_lru));
Dave Chinnercd56a392013-08-28 10:18:07 +1000174 list_lru_del(&qi->qi_lru, &dqp->q_lru);
Dave Chinner01728b42022-03-17 09:09:13 -0700175 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500176
177 xfs_qm_dqdestroy(dqp);
Jie Liudf8052e2013-11-26 21:38:49 +0800178 return 0;
Brian Foster8d3d7e22020-03-27 08:29:45 -0700179
180out_unlock:
181 xfs_dqunlock(dqp);
182 return error;
Jie Liudf8052e2013-11-26 21:38:49 +0800183}
184
185/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500186 * Purge the dquot cache.
187 */
Christoph Hellwig40b52222021-08-06 11:05:36 -0700188static void
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500189xfs_qm_dqpurge_all(
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700190 struct xfs_mount *mp)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500191{
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700192 xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL);
193 xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL);
194 xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500195}
196
197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Just destroy the quotainfo structure.
199 */
200void
Christoph Hellwig7d095252009-06-08 15:33:32 +0200201xfs_qm_unmount(
202 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200204 if (mp->m_quotainfo) {
Christoph Hellwige497dfba2021-08-06 11:05:36 -0700205 xfs_qm_dqpurge_all(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * Called from the vfsops layer.
212 */
Christoph Hellwige57481d2008-12-03 12:20:36 +0100213void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214xfs_qm_unmount_quotas(
215 xfs_mount_t *mp)
216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Release the dquots that root inode, et al might be holding,
219 * before we flush quotas and blow away the quotainfo structure.
220 */
221 ASSERT(mp->m_rootip);
222 xfs_qm_dqdetach(mp->m_rootip);
223 if (mp->m_rbmip)
224 xfs_qm_dqdetach(mp->m_rbmip);
225 if (mp->m_rsumip)
226 xfs_qm_dqdetach(mp->m_rsumip);
227
228 /*
Christoph Hellwige57481d2008-12-03 12:20:36 +0100229 * Release the quota inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (mp->m_quotainfo) {
Christoph Hellwige57481d2008-12-03 12:20:36 +0100232 if (mp->m_quotainfo->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700233 xfs_irele(mp->m_quotainfo->qi_uquotaip);
Christoph Hellwige57481d2008-12-03 12:20:36 +0100234 mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Christoph Hellwige57481d2008-12-03 12:20:36 +0100236 if (mp->m_quotainfo->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700237 xfs_irele(mp->m_quotainfo->qi_gquotaip);
Christoph Hellwige57481d2008-12-03 12:20:36 +0100238 mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500240 if (mp->m_quotainfo->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700241 xfs_irele(mp->m_quotainfo->qi_pquotaip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500242 mp->m_quotainfo->qi_pquotaip = NULL;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247STATIC int
248xfs_qm_dqattach_one(
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800249 struct xfs_inode *ip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700250 xfs_dqtype_t type,
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800251 bool doalloc,
252 struct xfs_dquot **IO_idqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800254 struct xfs_dquot *dqp;
255 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000257 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 error = 0;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000261 * See if we already have it in the inode itself. IO_idqpp is &i_udquot
262 * or &i_gdquot. This made the code look weird, but made the logic a lot
263 * simpler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100265 dqp = *IO_idqpp;
266 if (dqp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000267 trace_xfs_dqattach_found(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000272 * Find the dquot from somewhere. This bumps the reference count of
273 * dquot and returns it locked. This can return ENOENT if dquot didn't
274 * exist on disk and we didn't ask it to allocate; ESRCH if quotas got
275 * turned off suddenly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700277 error = xfs_qm_dqget_inode(ip, type, doalloc, &dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100278 if (error)
279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000281 trace_xfs_dqattach_get(dqp);
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * dqget may have dropped and re-acquired the ilock, but it guarantees
285 * that the dquot returned is the one that should go in the inode.
286 */
287 *IO_idqpp = dqp;
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100288 xfs_dqunlock(dqp);
289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400292static bool
293xfs_qm_need_dqattach(
294 struct xfs_inode *ip)
295{
296 struct xfs_mount *mp = ip->i_mount;
297
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400298 if (!XFS_IS_QUOTA_ON(mp))
299 return false;
300 if (!XFS_NOT_DQATTACHED(mp, ip))
301 return false;
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500302 if (xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400303 return false;
304 return true;
305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000308 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
309 * into account.
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -0700310 * If @doalloc is true, the dquot(s) will be allocated if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * Inode may get unlocked and relocked in here, and the caller must deal with
312 * the consequences.
313 */
314int
Christoph Hellwig7d095252009-06-08 15:33:32 +0200315xfs_qm_dqattach_locked(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_inode_t *ip,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700317 bool doalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int error = 0;
321
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400322 if (!xfs_qm_need_dqattach(ip))
Jesper Juhl014c2542006-01-15 02:37:08 +0100323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Christoph Hellwig7d095252009-06-08 15:33:32 +0200325 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dave Chinner3c35337572014-05-05 17:30:15 +1000327 if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700328 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_USER,
329 doalloc, &ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (error)
331 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000332 ASSERT(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000334
Dave Chinner3c35337572014-05-05 17:30:15 +1000335 if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700336 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_GROUP,
337 doalloc, &ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (error)
339 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000340 ASSERT(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Dave Chinner3c35337572014-05-05 17:30:15 +1000343 if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
Kaixu Xia74af4c12020-09-22 09:19:18 -0700344 error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_PROJ,
Darrick J. Wong4882c192018-05-04 15:30:22 -0700345 doalloc, &ip->i_pdquot);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500346 if (error)
347 goto done;
Dave Chinner3c35337572014-05-05 17:30:15 +1000348 ASSERT(ip->i_pdquot);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500349 }
350
Dave Chinner3c35337572014-05-05 17:30:15 +1000351done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
Dave Chinner3c35337572014-05-05 17:30:15 +1000353 * Don't worry about the dquots that we may have attached before any
354 * error - they'll get detached later if it has not already been done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200356 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig7d095252009-06-08 15:33:32 +0200357 return error;
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Christoph Hellwig7d095252009-06-08 15:33:32 +0200360int
361xfs_qm_dqattach(
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700362 struct xfs_inode *ip)
Christoph Hellwig7d095252009-06-08 15:33:32 +0200363{
364 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Christoph Hellwigb4d05e32012-03-27 10:34:46 -0400366 if (!xfs_qm_need_dqattach(ip))
367 return 0;
368
Christoph Hellwig7d095252009-06-08 15:33:32 +0200369 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong4882c192018-05-04 15:30:22 -0700370 error = xfs_qm_dqattach_locked(ip, false);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200371 xfs_iunlock(ip, XFS_ILOCK_EXCL);
372
Jesper Juhl014c2542006-01-15 02:37:08 +0100373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
377 * Release dquots (and their references) if any.
378 * The inode should be locked EXCL except when this's called by
379 * xfs_ireclaim.
380 */
381void
382xfs_qm_dqdetach(
383 xfs_inode_t *ip)
384{
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500385 if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return;
387
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000388 trace_xfs_dquot_dqdetach(ip);
389
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500390 ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, ip->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (ip->i_udquot) {
392 xfs_qm_dqrele(ip->i_udquot);
393 ip->i_udquot = NULL;
394 }
395 if (ip->i_gdquot) {
396 xfs_qm_dqrele(ip->i_gdquot);
397 ip->i_gdquot = NULL;
398 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500399 if (ip->i_pdquot) {
400 xfs_qm_dqrele(ip->i_pdquot);
401 ip->i_pdquot = NULL;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Dave Chinnercd56a392013-08-28 10:18:07 +1000405struct xfs_qm_isolate {
406 struct list_head buffers;
407 struct list_head dispose;
408};
409
410static enum lru_status
411xfs_qm_dquot_isolate(
412 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800413 struct list_lru_one *lru,
Dave Chinnercd56a392013-08-28 10:18:07 +1000414 spinlock_t *lru_lock,
415 void *arg)
Dave Chinnerbf1ed382014-09-29 10:43:40 +1000416 __releases(lru_lock) __acquires(lru_lock)
Dave Chinnercd56a392013-08-28 10:18:07 +1000417{
418 struct xfs_dquot *dqp = container_of(item,
419 struct xfs_dquot, q_lru);
420 struct xfs_qm_isolate *isol = arg;
421
422 if (!xfs_dqlock_nowait(dqp))
423 goto out_miss_busy;
424
425 /*
Dave Chinner52f31ed2022-12-08 08:29:22 -0800426 * If something else is freeing this dquot and hasn't yet removed it
427 * from the LRU, leave it for the freeing task to complete the freeing
428 * process rather than risk it being free from under us here.
429 */
430 if (dqp->q_flags & XFS_DQFLAG_FREEING)
431 goto out_miss_unlock;
432
433 /*
Dave Chinnercd56a392013-08-28 10:18:07 +1000434 * This dquot has acquired a reference in the meantime remove it from
435 * the freelist and try again.
436 */
437 if (dqp->q_nrefs) {
438 xfs_dqunlock(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100439 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
Dave Chinnercd56a392013-08-28 10:18:07 +1000440
441 trace_xfs_dqreclaim_want(dqp);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800442 list_lru_isolate(lru, &dqp->q_lru);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100443 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner35163412013-08-28 10:18:08 +1000444 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000445 }
446
447 /*
448 * If the dquot is dirty, flush it. If it's already being flushed, just
449 * skip it so there is time for the IO to complete before we try to
450 * reclaim it again on the next LRU pass.
451 */
Dave Chinner52f31ed2022-12-08 08:29:22 -0800452 if (!xfs_dqflock_nowait(dqp))
453 goto out_miss_unlock;
Dave Chinnercd56a392013-08-28 10:18:07 +1000454
455 if (XFS_DQ_IS_DIRTY(dqp)) {
456 struct xfs_buf *bp = NULL;
457 int error;
458
459 trace_xfs_dqreclaim_dirty(dqp);
460
461 /* we have to drop the LRU lock to flush the dquot */
462 spin_unlock(lru_lock);
463
464 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700465 if (error)
Dave Chinnercd56a392013-08-28 10:18:07 +1000466 goto out_unlock_dirty;
Dave Chinnercd56a392013-08-28 10:18:07 +1000467
468 xfs_buf_delwri_queue(bp, &isol->buffers);
469 xfs_buf_relse(bp);
470 goto out_unlock_dirty;
471 }
472 xfs_dqfunlock(dqp);
473
474 /*
475 * Prevent lookups now that we are past the point of no return.
476 */
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700477 dqp->q_flags |= XFS_DQFLAG_FREEING;
Dave Chinnercd56a392013-08-28 10:18:07 +1000478 xfs_dqunlock(dqp);
479
480 ASSERT(dqp->q_nrefs == 0);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800481 list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100482 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinnercd56a392013-08-28 10:18:07 +1000483 trace_xfs_dqreclaim_done(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100484 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
Dave Chinner35163412013-08-28 10:18:08 +1000485 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000486
Dave Chinner52f31ed2022-12-08 08:29:22 -0800487out_miss_unlock:
488 xfs_dqunlock(dqp);
Dave Chinnercd56a392013-08-28 10:18:07 +1000489out_miss_busy:
490 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100491 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000492 return LRU_SKIP;
Dave Chinnercd56a392013-08-28 10:18:07 +1000493
494out_unlock_dirty:
495 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100496 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000497 xfs_dqunlock(dqp);
498 spin_lock(lru_lock);
499 return LRU_RETRY;
Dave Chinnercd56a392013-08-28 10:18:07 +1000500}
501
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000502static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000503xfs_qm_shrink_scan(
504 struct shrinker *shrink,
505 struct shrink_control *sc)
506{
Qi Zhengfe885272023-09-11 17:44:35 +0800507 struct xfs_quotainfo *qi = shrink->private_data;
Dave Chinnercd56a392013-08-28 10:18:07 +1000508 struct xfs_qm_isolate isol;
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000509 unsigned long freed;
Dave Chinnercd56a392013-08-28 10:18:07 +1000510 int error;
Dave Chinnercd56a392013-08-28 10:18:07 +1000511
Mel Gormand0164ad2015-11-06 16:28:21 -0800512 if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
Dave Chinnercd56a392013-08-28 10:18:07 +1000513 return 0;
514
515 INIT_LIST_HEAD(&isol.buffers);
516 INIT_LIST_HEAD(&isol.dispose);
517
Vladimir Davydov503c3582015-02-12 14:58:47 -0800518 freed = list_lru_shrink_walk(&qi->qi_lru, sc,
519 xfs_qm_dquot_isolate, &isol);
Dave Chinnercd56a392013-08-28 10:18:07 +1000520
521 error = xfs_buf_delwri_submit(&isol.buffers);
522 if (error)
523 xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
524
525 while (!list_empty(&isol.dispose)) {
526 struct xfs_dquot *dqp;
527
528 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
529 list_del_init(&dqp->q_lru);
530 xfs_qm_dqfree_one(dqp);
531 }
532
533 return freed;
534}
535
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000536static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000537xfs_qm_shrink_count(
538 struct shrinker *shrink,
539 struct shrink_control *sc)
540{
Qi Zhengfe885272023-09-11 17:44:35 +0800541 struct xfs_quotainfo *qi = shrink->private_data;
Dave Chinnercd56a392013-08-28 10:18:07 +1000542
Vladimir Davydov503c3582015-02-12 14:58:47 -0800543 return list_lru_shrink_count(&qi->qi_lru, sc);
Dave Chinnercd56a392013-08-28 10:18:07 +1000544}
545
Carlos Maiolinobe607942016-02-08 11:27:55 +1100546STATIC void
547xfs_qm_set_defquota(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800548 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700549 xfs_dqtype_t type,
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800550 struct xfs_quotainfo *qinf)
Carlos Maiolinobe607942016-02-08 11:27:55 +1100551{
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800552 struct xfs_dquot *dqp;
553 struct xfs_def_quota *defq;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100554 int error;
555
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700556 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700557 if (error)
558 return;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100559
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700560 defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
Carlos Maiolinobe607942016-02-08 11:27:55 +1100561
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700562 /*
563 * Timers and warnings have been already set, let's just set the
564 * default limits for this quota type
565 */
Darrick J. Wong438769e2020-07-14 10:37:32 -0700566 defq->blk.hard = dqp->q_blk.hardlimit;
567 defq->blk.soft = dqp->q_blk.softlimit;
568 defq->ino.hard = dqp->q_ino.hardlimit;
569 defq->ino.soft = dqp->q_ino.softlimit;
570 defq->rtb.hard = dqp->q_rtb.hardlimit;
571 defq->rtb.soft = dqp->q_rtb.softlimit;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700572 xfs_qm_dqdestroy(dqp);
573}
574
575/* Initialize quota time limits from the root dquot. */
576static void
577xfs_qm_init_timelimits(
578 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700579 xfs_dqtype_t type)
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700580{
Eric Sandeene8503012020-05-21 13:07:01 -0700581 struct xfs_quotainfo *qinf = mp->m_quotainfo;
582 struct xfs_def_quota *defq;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700583 struct xfs_dquot *dqp;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700584 int error;
585
Eric Sandeene8503012020-05-21 13:07:01 -0700586 defq = xfs_get_defquota(qinf, type);
587
Darrick J. Wong438769e2020-07-14 10:37:32 -0700588 defq->blk.time = XFS_QM_BTIMELIMIT;
589 defq->ino.time = XFS_QM_ITIMELIMIT;
590 defq->rtb.time = XFS_QM_RTBTIMELIMIT;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700591
592 /*
593 * We try to get the limits from the superuser's limits fields.
594 * This is quite hacky, but it is standard quota practice.
595 *
596 * Since we may not have done a quotacheck by this point, just read
597 * the dquot without attaching it to any hashtables or lists.
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700598 */
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700599 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
600 if (error)
601 return;
602
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700603 /*
604 * The warnings and timers set the grace period given to
605 * a user or group before he or she can not perform any
606 * more writing. If it is zero, a default is used.
607 */
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700608 if (dqp->q_blk.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700609 defq->blk.time = dqp->q_blk.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700610 if (dqp->q_ino.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700611 defq->ino.time = dqp->q_ino.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700612 if (dqp->q_rtb.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700613 defq->rtb.time = dqp->q_rtb.timer;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700614
615 xfs_qm_dqdestroy(dqp);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100616}
617
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * This initializes all the quota information that's kept in the
620 * mount structure
621 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000622STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623xfs_qm_init_quotainfo(
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700624 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700626 struct xfs_quotainfo *qinf;
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700627 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700629 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800631 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(struct xfs_quotainfo), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Chinner24513372014-06-25 14:58:08 +1000633 error = list_lru_init(&qinf->qi_lru);
Dave Chinneree4eec42014-05-15 09:23:24 +1000634 if (error)
635 goto out_free_qinf;
Glauber Costa5ca302c2013-08-28 10:18:18 +1000636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * See if quotainodes are setup, and if not, allocate them,
639 * and change the superblock accordingly.
640 */
Dave Chinneree4eec42014-05-15 09:23:24 +1000641 error = xfs_qm_init_quotainos(mp);
642 if (error)
643 goto out_free_lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000645 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
646 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500647 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_NOFS);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000648 mutex_init(&qinf->qi_tree_lock);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800651 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Precalc some constants */
654 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000655 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
Dave Chinner38c26bf2021-08-18 18:46:37 -0700656 if (xfs_has_bigtime(mp)) {
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700657 qinf->qi_expiry_min =
658 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
659 qinf->qi_expiry_max =
660 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
661 } else {
662 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
663 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
664 }
Darrick J. Wong06dbf822020-08-24 11:58:01 -0700665 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
666 qinf->qi_expiry_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
669
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700670 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
671 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
672 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700674 if (XFS_IS_UQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700675 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700676 if (XFS_IS_GQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700677 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700678 if (XFS_IS_PQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700679 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100680
Qi Zhengfe885272023-09-11 17:44:35 +0800681 qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s",
682 mp->m_super->s_id);
683 if (!qinf->qi_shrinker) {
684 error = -ENOMEM;
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800685 goto out_free_inos;
Qi Zhengfe885272023-09-11 17:44:35 +0800686 }
687
688 qinf->qi_shrinker->count_objects = xfs_qm_shrink_count;
689 qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan;
690 qinf->qi_shrinker->private_data = qinf;
691
692 shrinker_register(qinf->qi_shrinker);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800693
Jesper Juhl014c2542006-01-15 02:37:08 +0100694 return 0;
Dave Chinneree4eec42014-05-15 09:23:24 +1000695
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800696out_free_inos:
697 mutex_destroy(&qinf->qi_quotaofflock);
698 mutex_destroy(&qinf->qi_tree_lock);
699 xfs_qm_destroy_quotainos(qinf);
Dave Chinneree4eec42014-05-15 09:23:24 +1000700out_free_lru:
701 list_lru_destroy(&qinf->qi_lru);
702out_free_qinf:
703 kmem_free(qinf);
704 mp->m_quotainfo = NULL;
705 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * Gets called when unmounting a filesystem or when all quotas get
710 * turned off.
711 * This purges the quota inodes, destroys locks and frees itself.
712 */
713void
714xfs_qm_destroy_quotainfo(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800715 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800717 struct xfs_quotainfo *qi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 qi = mp->m_quotainfo;
720 ASSERT(qi != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Qi Zhengfe885272023-09-11 17:44:35 +0800722 shrinker_free(qi->qi_shrinker);
Glauber Costaf5e1dd32013-08-28 10:18:18 +1000723 list_lru_destroy(&qi->qi_lru);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800724 xfs_qm_destroy_quotainos(qi);
Aliaksei Karaliou21968812017-12-21 13:18:26 -0800725 mutex_destroy(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000727 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mp->m_quotainfo = NULL;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Create an inode and return with a reference already taken, but unlocked
733 * This is how we create quota inodes
734 */
735STATIC int
736xfs_qm_qino_alloc(
Dave Chinner1abcf262020-12-09 10:05:15 -0800737 struct xfs_mount *mp,
738 struct xfs_inode **ipp,
739 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Dave Chinner1abcf262020-12-09 10:05:15 -0800741 struct xfs_trans *tp;
742 int error;
743 bool need_alloc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Chinner1abcf262020-12-09 10:05:15 -0800745 *ipp = NULL;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500746 /*
747 * With superblock that doesn't have separate pquotino, we
748 * share an inode between gquota and pquota. If the on-disk
749 * superblock has GQUOTA and the filesystem is now mounted
750 * with PQUOTA, just use sb_gquotino for sb_pquotino and
751 * vice-versa.
752 */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700753 if (!xfs_has_pquotino(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -0500754 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
755 xfs_ino_t ino = NULLFSINO;
756
757 if ((flags & XFS_QMOPT_PQUOTA) &&
758 (mp->m_sb.sb_gquotino != NULLFSINO)) {
759 ino = mp->m_sb.sb_gquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800760 if (XFS_IS_CORRUPT(mp,
761 mp->m_sb.sb_pquotino != NULLFSINO))
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700762 return -EFSCORRUPTED;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500763 } else if ((flags & XFS_QMOPT_GQUOTA) &&
764 (mp->m_sb.sb_pquotino != NULLFSINO)) {
765 ino = mp->m_sb.sb_pquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800766 if (XFS_IS_CORRUPT(mp,
767 mp->m_sb.sb_gquotino != NULLFSINO))
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700768 return -EFSCORRUPTED;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500769 }
770 if (ino != NULLFSINO) {
Dave Chinner1abcf262020-12-09 10:05:15 -0800771 error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500772 if (error)
773 return error;
774 mp->m_sb.sb_gquotino = NULLFSINO;
775 mp->m_sb.sb_pquotino = NULLFSINO;
Dave Chinner58c90472015-02-23 22:38:08 +1100776 need_alloc = false;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500777 }
778 }
779
Christoph Hellwig253f4912016-04-06 09:19:55 +1000780 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
Kaixu Xiafb353ff2020-04-22 21:54:28 -0700781 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0,
782 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000783 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Chinner58c90472015-02-23 22:38:08 +1100786 if (need_alloc) {
Dave Chinnerb652afd2021-06-02 10:48:24 +1000787 xfs_ino_t ino;
788
789 error = xfs_dialloc(&tp, 0, S_IFREG, &ino);
790 if (!error)
Christian Braunerf2d40142023-01-13 12:49:25 +0100791 error = xfs_init_new_inode(&nop_mnt_idmap, tp, NULL, ino,
Dave Chinnerb652afd2021-06-02 10:48:24 +1000792 S_IFREG, 1, 0, 0, false, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500793 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000794 xfs_trans_cancel(tp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500795 return error;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * Make the changes in the superblock, and log those too.
801 * sbfields arg may contain fields other than *QUOTINO;
802 * VERSIONNUM for example.
803 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000804 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (flags & XFS_QMOPT_SBVERSION) {
Dave Chinner38c26bf2021-08-18 18:46:37 -0700806 ASSERT(!xfs_has_quota(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Chinner38c26bf2021-08-18 18:46:37 -0700808 xfs_add_quota(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 mp->m_sb.sb_uquotino = NULLFSINO;
810 mp->m_sb.sb_gquotino = NULLFSINO;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500811 mp->m_sb.sb_pquotino = NULLFSINO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chandra Seetharamand892d582013-07-19 17:36:02 -0500813 /* qflags will get updated fully _after_ quotacheck */
814 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 if (flags & XFS_QMOPT_UQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800817 mp->m_sb.sb_uquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500818 else if (flags & XFS_QMOPT_GQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800819 mp->m_sb.sb_gquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500820 else
Dave Chinner1abcf262020-12-09 10:05:15 -0800821 mp->m_sb.sb_pquotino = (*ipp)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000822 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100823 xfs_log_sb(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Christoph Hellwig70393312015-06-04 13:48:08 +1000825 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100826 if (error) {
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700827 ASSERT(xfs_is_shutdown(mp));
Dave Chinner53487782011-03-07 10:05:35 +1100828 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Dave Chinner58c90472015-02-23 22:38:08 +1100830 if (need_alloc)
Dave Chinner1abcf262020-12-09 10:05:15 -0800831 xfs_finish_inode_setup(*ipp);
Dave Chinner58c90472015-02-23 22:38:08 +1100832 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835
David Chinner5b139732008-04-10 12:20:10 +1000836STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837xfs_qm_reset_dqcounts(
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700838 struct xfs_mount *mp,
839 struct xfs_buf *bp,
840 xfs_dqid_t id,
841 xfs_dqtype_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Dave Chinner6fcdc592013-06-03 15:28:46 +1000843 struct xfs_dqblk *dqb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int j;
845
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000846 trace_xfs_reset_dqcounts(bp, _RET_IP_);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * Reset all counters and timers. They'll be
850 * started afresh by xfs_qm_quotacheck.
851 */
852#ifdef DEBUG
Eric Sandeen90115402017-04-19 12:55:57 -0700853 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
Christoph Hellwig11a83f42021-10-11 16:12:37 -0700854 sizeof(struct xfs_dqblk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000855 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#endif
Dave Chinner6fcdc592013-06-03 15:28:46 +1000857 dqb = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000858 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000859 struct xfs_disk_dquot *ddq;
860
861 ddq = (struct xfs_disk_dquot *)&dqb[j];
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * Do a sanity check, and if needed, repair the dqblk. Don't
865 * output any warnings because it's perfectly possible to
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800866 * find uninitialised dquot blks. See comment in
867 * xfs_dquot_verify.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Darrick J. Wongf9751c42020-07-15 17:41:24 -0700869 if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700870 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700871 xfs_dqblk_repair(mp, &dqb[j], id + j, type);
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800872
Jan Karadfcc70a2015-02-23 22:34:17 +1100873 /*
874 * Reset type in case we are reusing group quota file for
875 * project quotas or vice versa
876 */
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700877 ddq->d_type = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100878 ddq->d_bcount = 0;
879 ddq->d_icount = 0;
880 ddq->d_rtbcount = 0;
Darrick J. Wong58855392020-03-24 20:12:53 -0700881
882 /*
883 * dquot id 0 stores the default grace period and the maximum
884 * warning limit that were set by the administrator, so we
885 * should not reset them.
886 */
887 if (ddq->d_id != 0) {
888 ddq->d_btimer = 0;
889 ddq->d_itimer = 0;
890 ddq->d_rtbtimer = 0;
891 ddq->d_bwarns = 0;
892 ddq->d_iwarns = 0;
893 ddq->d_rtbwarns = 0;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700894 if (xfs_has_bigtime(mp))
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700895 ddq->d_type |= XFS_DQTYPE_BIGTIME;
Darrick J. Wong58855392020-03-24 20:12:53 -0700896 }
Dave Chinner6fcdc592013-06-03 15:28:46 +1000897
Dave Chinner38c26bf2021-08-18 18:46:37 -0700898 if (xfs_has_crc(mp)) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000899 xfs_update_cksum((char *)&dqb[j],
900 sizeof(struct xfs_dqblk),
901 XFS_DQUOT_CRC_OFF);
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700907xfs_qm_reset_dqcounts_all(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000908 struct xfs_mount *mp,
909 xfs_dqid_t firstid,
910 xfs_fsblock_t bno,
911 xfs_filblks_t blkcnt,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700912 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000913 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000915 struct xfs_buf *bp;
Darrick J. Wong0dcc0722020-07-14 10:37:12 -0700916 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 ASSERT(blkcnt > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /*
921 * Blkcnt arg can be a very big number, and might even be
922 * larger than the log itself. So, we have to break it up into
923 * manageable-sized transactions.
924 * Note that we don't start a permanent transaction here; we might
925 * not be able to get a log reservation for the whole thing up front,
926 * and we don't really care to either, because we just discard
927 * everything if we were to crash in the middle of this loop.
928 */
929 while (blkcnt--) {
930 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
931 XFS_FSB_TO_DADDR(mp, bno),
Dave Chinnerc6319192012-11-14 17:50:13 +1100932 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100933 &xfs_dquot_buf_ops);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000934
935 /*
936 * CRC and validation errors will return a EFSCORRUPTED here. If
937 * this occurs, re-read without CRC validation so that we can
938 * repair the damage via xfs_qm_reset_dqcounts(). This process
939 * will leave a trace in the log indicating corruption has
940 * been detected.
941 */
Dave Chinner24513372014-06-25 14:58:08 +1000942 if (error == -EFSCORRUPTED) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000943 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
944 XFS_FSB_TO_DADDR(mp, bno),
945 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
946 NULL);
947 }
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (error)
950 break;
951
Dave Chinner5fd364f2014-08-04 12:43:26 +1000952 /*
953 * A corrupt buffer might not have a verifier attached, so
954 * make sure we have the correct one attached before writeback
955 * occurs.
956 */
957 bp->b_ops = &xfs_dquot_buf_ops;
David Chinner5b139732008-04-10 12:20:10 +1000958 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000959 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwig61551f12011-08-23 08:28:06 +0000960 xfs_buf_relse(bp);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000961
962 /* goto the next block. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000964 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000966
Jesper Juhl014c2542006-01-15 02:37:08 +0100967 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/*
Darrick J. Wong28b90602018-05-04 15:31:20 -0700971 * Iterate over all allocated dquot blocks in this quota inode, zeroing all
972 * counters for every chunk of dquots that we find.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
974STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700975xfs_qm_reset_dqcounts_buf(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000976 struct xfs_mount *mp,
977 struct xfs_inode *qip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700978 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000979 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000981 struct xfs_bmbt_irec *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int i, nmaps; /* number of map entries */
983 int error; /* return value */
984 xfs_fileoff_t lblkno;
985 xfs_filblks_t maxlblkcnt;
986 xfs_dqid_t firstid;
987 xfs_fsblock_t rablkno;
988 xfs_filblks_t rablkcnt;
989
990 error = 0;
991 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000992 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * trans_reserve. But, this gets called during quotacheck, and that
994 * happens only at mount time which is single threaded.
995 */
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700996 if (qip->i_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700999 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 lblkno = 0;
Dave Chinner32972382012-06-08 15:44:54 +10001002 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 do {
Christoph Hellwigda51d322013-12-06 12:30:14 -08001004 uint lock_mode;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 nmaps = XFS_DQITER_MAP_SIZE;
1007 /*
1008 * We aren't changing the inode itself. Just changing
1009 * some of its data. No new blocks are added here, and
1010 * the inode is never added to the transaction.
1011 */
Christoph Hellwigda51d322013-12-06 12:30:14 -08001012 lock_mode = xfs_ilock_data_map_shared(qip);
Dave Chinner5c8ed202011-09-18 20:40:45 +00001013 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1014 map, &nmaps, 0);
Christoph Hellwigda51d322013-12-06 12:30:14 -08001015 xfs_iunlock(qip, lock_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (error)
1017 break;
1018
1019 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1020 for (i = 0; i < nmaps; i++) {
1021 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1022 ASSERT(map[i].br_blockcount);
1023
1024
1025 lblkno += map[i].br_blockcount;
1026
1027 if (map[i].br_startblock == HOLESTARTBLOCK)
1028 continue;
1029
1030 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001031 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * Do a read-ahead on the next extent.
1034 */
1035 if ((i+1 < nmaps) &&
1036 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1037 rablkcnt = map[i+1].br_blockcount;
1038 rablkno = map[i+1].br_startblock;
1039 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001040 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 XFS_FSB_TO_DADDR(mp, rablkno),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001042 mp->m_quotainfo->qi_dqchunklen,
Dave Chinner5fd364f2014-08-04 12:43:26 +10001043 &xfs_dquot_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 rablkno++;
1045 }
1046 }
1047 /*
1048 * Iterate thru all the blks in the extent and
1049 * reset the counters of all the dquots inside them.
1050 */
Darrick J. Wong28b90602018-05-04 15:31:20 -07001051 error = xfs_qm_reset_dqcounts_all(mp, firstid,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001052 map[i].br_startblock,
1053 map[i].br_blockcount,
Darrick J. Wong0dcc0722020-07-14 10:37:12 -07001054 type, buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001055 if (error)
1056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 } while (nmaps > 0);
1059
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001060out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001061 kmem_free(map);
Jesper Juhl014c2542006-01-15 02:37:08 +01001062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
1066 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001067 *
1068 * Given the inode, and a dquot id this updates both the incore dqout as well
1069 * as the buffer copy. This is so that once the quotacheck is done, we can
1070 * just log all the buffers, as opposed to logging numerous updates to
1071 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001073STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001075 struct xfs_inode *ip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -07001076 xfs_dqtype_t type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 xfs_qcnt_t nblks,
1078 xfs_qcnt_t rtblks)
1079{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001080 struct xfs_mount *mp = ip->i_mount;
1081 struct xfs_dquot *dqp;
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001082 xfs_dqid_t id;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001083 int error;
1084
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001085 id = xfs_qm_id_for_quotatype(ip, type);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -07001086 error = xfs_qm_dqget(mp, id, type, true, &dqp);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001087 if (error) {
1088 /*
1089 * Shouldn't be able to turn off quotas here.
1090 */
Dave Chinner24513372014-06-25 14:58:08 +10001091 ASSERT(error != -ESRCH);
1092 ASSERT(error != -ENOENT);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001093 return error;
1094 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001095
1096 trace_xfs_dqadjust(dqp);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
1099 * Adjust the inode count and the block count to reflect this inode's
1100 * resource usage.
1101 */
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001102 dqp->q_ino.count++;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001103 dqp->q_ino.reserved++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (nblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001105 dqp->q_blk.count += nblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001106 dqp->q_blk.reserved += nblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108 if (rtblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001109 dqp->q_rtb.count += rtblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001110 dqp->q_rtb.reserved += rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 /*
1114 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001115 *
1116 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 */
Darrick J. Wongc51df732020-07-14 10:37:30 -07001118 if (dqp->q_id) {
Darrick J. Wongc8c753e2020-07-14 10:37:33 -07001119 xfs_qm_adjust_dqlimits(dqp);
1120 xfs_qm_adjust_dqtimers(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001123 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001124 xfs_qm_dqput(dqp);
1125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * callback routine supplied to bulkstat(). Given an inumber, find its
1130 * dquots and update them to account for resources taken by that inode.
1131 */
1132/* ARGSUSED */
1133STATIC int
1134xfs_qm_dqusage_adjust(
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001135 struct xfs_mount *mp,
1136 struct xfs_trans *tp,
1137 xfs_ino_t ino,
1138 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001140 struct xfs_inode *ip;
1141 xfs_qcnt_t nblks;
1142 xfs_filblks_t rtblks = 0; /* total rt blks */
1143 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001145 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /*
1148 * rootino must have its resources accounted for, not so with the quota
1149 * inodes.
1150 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001151 if (xfs_is_quota_inode(&mp->m_sb, ino))
1152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /*
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001155 * We don't _need_ to take the ilock EXCL here because quotacheck runs
1156 * at mount time and therefore nobody will be racing chown/chproj.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001158 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
1159 if (error == -EINVAL || error == -ENOENT)
1160 return 0;
1161 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001162 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Darrick J. Wong537c0132023-09-24 08:35:53 -07001164 /*
1165 * Reload the incore unlinked list to avoid failure in inodegc.
1166 * Use an unlocked check here because unrecovered unlinked inodes
1167 * should be somewhat rare.
1168 */
1169 if (xfs_inode_unlinked_incomplete(ip)) {
1170 error = xfs_inode_reload_unlinked(ip);
1171 if (error) {
1172 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1173 goto error0;
1174 }
1175 }
Darrick J. Wong49813a22023-09-11 08:39:08 -07001176
Christoph Hellwig52fda112010-09-06 01:44:22 +00001177 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Christoph Hellwig52fda112010-09-06 01:44:22 +00001179 if (XFS_IS_REALTIME_INODE(ip)) {
Darrick J. Wong732436e2022-07-09 10:56:05 -07001180 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001181
Christoph Hellwig862a8042021-04-13 11:15:09 -07001182 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1183 if (error)
1184 goto error0;
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001185
1186 xfs_bmap_count_leaves(ifp, &rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001189 nblks = (xfs_qcnt_t)ip->i_nblocks - rtblks;
Darrick J. Wong49813a22023-09-11 08:39:08 -07001190 xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * Add the (disk blocks and inode) resources occupied by this
1194 * inode to its dquots. We do this adjustment in the incore dquot,
1195 * and also copy the changes to its buffer.
1196 * We don't care about putting these changes in a transaction
1197 * envelope because if we crash in the middle of a 'quotacheck'
1198 * we have to start from the beginning anyway.
1199 * Once we're done, we'll log all the dquot bufs.
1200 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001201 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1203 */
1204 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001205 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001206 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001207 if (error)
1208 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Christoph Hellwig52fda112010-09-06 01:44:22 +00001211 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001212 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001213 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001214 if (error)
1215 goto error0;
1216 }
1217
1218 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001219 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001220 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001221 if (error)
1222 goto error0;
1223 }
1224
Christoph Hellwig52fda112010-09-06 01:44:22 +00001225error0:
Darrick J. Wong44a87362018-07-25 12:52:32 -07001226 xfs_irele(ip);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001227 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001230STATIC int
1231xfs_qm_flush_one(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001232 struct xfs_dquot *dqp,
1233 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001234{
Brian Foster7912e7f2017-06-14 21:21:45 -07001235 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001236 struct list_head *buffer_list = data;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001237 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001238 int error = 0;
1239
1240 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001241 if (dqp->q_flags & XFS_DQFLAG_FREEING)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001242 goto out_unlock;
1243 if (!XFS_DQ_IS_DIRTY(dqp))
1244 goto out_unlock;
1245
Brian Foster7912e7f2017-06-14 21:21:45 -07001246 /*
1247 * The only way the dquot is already flush locked by the time quotacheck
1248 * gets here is if reclaim flushed it before the dqadjust walk dirtied
1249 * it for the final time. Quotacheck collects all dquot bufs in the
1250 * local delwri queue before dquots are dirtied, so reclaim can't have
1251 * possibly queued it for I/O. The only way out is to push the buffer to
1252 * cycle the flush lock.
1253 */
1254 if (!xfs_dqflock_nowait(dqp)) {
1255 /* buf is pinned in-core by delwri list */
Dave Chinner85c73bf72022-07-07 22:05:18 +10001256 error = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
1257 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1258 if (error)
Brian Foster7912e7f2017-06-14 21:21:45 -07001259 goto out_unlock;
Dave Chinner85c73bf72022-07-07 22:05:18 +10001260
Darrick J. Wongf0c2d7d2022-08-03 17:33:00 -07001261 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1262 error = -EAGAIN;
1263 xfs_buf_relse(bp);
1264 goto out_unlock;
1265 }
Brian Foster7912e7f2017-06-14 21:21:45 -07001266 xfs_buf_unlock(bp);
1267
1268 xfs_buf_delwri_pushbuf(bp, buffer_list);
1269 xfs_buf_rele(bp);
1270
1271 error = -EAGAIN;
1272 goto out_unlock;
1273 }
1274
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001275 error = xfs_qm_dqflush(dqp, &bp);
1276 if (error)
1277 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001278
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001279 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001280 xfs_buf_relse(bp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001281out_unlock:
1282 xfs_dqunlock(dqp);
1283 return error;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/*
1287 * Walk thru all the filesystem inodes and construct a consistent view
1288 * of the disk quota world. If the quotacheck fails, disable quotas.
1289 */
Jie Liueb866bb2014-07-24 20:49:57 +10001290STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291xfs_qm_quotacheck(
1292 xfs_mount_t *mp)
1293{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001294 int error, error2;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001295 uint flags;
1296 LIST_HEAD (buffer_list);
1297 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
1298 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001299 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 flags = 0;
1302
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001303 ASSERT(uip || gip || pip);
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001304 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dave Chinner0b932cc2011-03-07 10:08:35 +11001306 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001309 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * their counters to zero. We need a clean slate.
1311 * We don't log our changes till later.
1312 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001313 if (uip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001314 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001315 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001316 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 goto error_return;
1318 flags |= XFS_UQUOTA_CHKD;
1319 }
1320
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001321 if (gip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001322 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001323 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto error_return;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001326 flags |= XFS_GQUOTA_CHKD;
1327 }
1328
1329 if (pip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001330 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001331 &buffer_list);
1332 if (error)
1333 goto error_return;
1334 flags |= XFS_PQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
Darrick J. Wong49813a22023-09-11 08:39:08 -07001337 xfs_set_quotacheck_running(mp);
Darrick J. Wong13d59a22019-07-03 20:36:28 -07001338 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
1339 NULL);
Darrick J. Wong49813a22023-09-11 08:39:08 -07001340 xfs_clear_quotacheck_running(mp);
Dave Chinner0c7273e2023-03-05 15:13:22 -08001341
1342 /*
1343 * On error, the inode walk may have partially populated the dquot
1344 * caches. We must purge them before disabling quota and tearing down
1345 * the quotainfo, or else the dquots will leak.
1346 */
1347 if (error)
1348 goto error_purge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001351 * We've made all the changes that we need to make incore. Flush them
1352 * down to disk buffers if everything was updated successfully.
David Chinner4b8879d2008-04-10 12:20:17 +10001353 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001354 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001355 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001356 &buffer_list);
1357 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001358 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001359 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001360 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001361 if (!error)
1362 error = error2;
1363 }
1364 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001365 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001366 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001367 if (!error)
1368 error = error2;
1369 }
David Chinner4b8879d2008-04-10 12:20:17 +10001370
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001371 error2 = xfs_buf_delwri_submit(&buffer_list);
1372 if (!error)
1373 error = error2;
1374
David Chinner4b8879d2008-04-10 12:20:17 +10001375 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * We can get this error if we couldn't do a dquot allocation inside
1377 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1378 * dirty dquots that might be cached, we just want to get rid of them
1379 * and turn quotaoff. The dquots won't be attached to any of the inodes
1380 * at this point (because we intentionally didn't in dqget_noattach).
1381 */
Dave Chinner0c7273e2023-03-05 15:13:22 -08001382 if (error)
1383 goto error_purge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * If one type of quotas is off, then it will lose its
1387 * quotachecked status, since we won't be doing accounting for
1388 * that type anymore.
1389 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001390 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 mp->m_qflags |= flags;
1392
Dave Chinner0c7273e2023-03-05 15:13:22 -08001393error_return:
Brian Foster20e8a062017-04-21 12:40:44 -07001394 xfs_buf_delwri_cancel(&buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001397 xfs_warn(mp,
1398 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1399 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * We must turn off quotas.
1402 */
1403 ASSERT(mp->m_quotainfo != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001405 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001406 xfs_warn(mp,
1407 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001408 }
Dave Chinner0b932cc2011-03-07 10:08:35 +11001409 } else
1410 xfs_notice(mp, "Quotacheck: Done.");
Eric Sandeend99831f2014-06-22 15:03:54 +10001411 return error;
Dave Chinner0c7273e2023-03-05 15:13:22 -08001412
1413error_purge:
1414 /*
1415 * On error, we may have inodes queued for inactivation. This may try
1416 * to attach dquots to the inode before running cleanup operations on
1417 * the inode and this can race with the xfs_qm_destroy_quotainfo() call
1418 * below that frees mp->m_quotainfo. To avoid this race, flush all the
1419 * pending inodegc operations before we purge the dquots from memory,
1420 * ensuring that background inactivation is idle whilst we turn off
1421 * quotas.
1422 */
1423 xfs_inodegc_flush(mp);
1424 xfs_qm_dqpurge_all(mp);
1425 goto error_return;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429/*
Jie Liueb866bb2014-07-24 20:49:57 +10001430 * This is called from xfs_mountfs to start quotas and initialize all
1431 * necessary data structures like quotainfo. This is also responsible for
1432 * running a quotacheck as necessary. We are guaranteed that the superblock
1433 * is consistently read in at this point.
1434 *
1435 * If we fail here, the mount will continue with quota turned off. We don't
1436 * need to inidicate success or failure at all.
1437 */
1438void
1439xfs_qm_mount_quotas(
1440 struct xfs_mount *mp)
1441{
1442 int error = 0;
1443 uint sbf;
1444
1445 /*
1446 * If quotas on realtime volumes is not supported, we disable
1447 * quotas immediately.
1448 */
1449 if (mp->m_sb.sb_rextents) {
1450 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
1451 mp->m_qflags = 0;
1452 goto write_changes;
1453 }
1454
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001455 ASSERT(XFS_IS_QUOTA_ON(mp));
Jie Liueb866bb2014-07-24 20:49:57 +10001456
1457 /*
1458 * Allocate the quotainfo structure inside the mount struct, and
1459 * create quotainode(s), and change/rev superblock if necessary.
1460 */
1461 error = xfs_qm_init_quotainfo(mp);
1462 if (error) {
1463 /*
1464 * We must turn off quotas.
1465 */
1466 ASSERT(mp->m_quotainfo == NULL);
1467 mp->m_qflags = 0;
1468 goto write_changes;
1469 }
1470 /*
1471 * If any of the quotas are not consistent, do a quotacheck.
1472 */
1473 if (XFS_QM_NEED_QUOTACHECK(mp)) {
1474 error = xfs_qm_quotacheck(mp);
1475 if (error) {
1476 /* Quotacheck failed and disabled quotas. */
1477 return;
1478 }
1479 }
1480 /*
1481 * If one type of quotas is off, then it will lose its
1482 * quotachecked status, since we won't be doing accounting for
1483 * that type anymore.
1484 */
1485 if (!XFS_IS_UQUOTA_ON(mp))
1486 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
1487 if (!XFS_IS_GQUOTA_ON(mp))
1488 mp->m_qflags &= ~XFS_GQUOTA_CHKD;
1489 if (!XFS_IS_PQUOTA_ON(mp))
1490 mp->m_qflags &= ~XFS_PQUOTA_CHKD;
1491
1492 write_changes:
1493 /*
1494 * We actually don't have to acquire the m_sb_lock at all.
1495 * This can only be called from mount, and that's single threaded. XXX
1496 */
1497 spin_lock(&mp->m_sb_lock);
1498 sbf = mp->m_sb.sb_qflags;
1499 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
1500 spin_unlock(&mp->m_sb_lock);
1501
1502 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001503 if (xfs_sync_sb(mp, false)) {
Jie Liueb866bb2014-07-24 20:49:57 +10001504 /*
1505 * We could only have been turning quotas off.
1506 * We aren't in very good shape actually because
1507 * the incore structures are convinced that quotas are
1508 * off, but the on disk superblock doesn't know that !
1509 */
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001510 ASSERT(!(XFS_IS_QUOTA_ON(mp)));
Jie Liueb866bb2014-07-24 20:49:57 +10001511 xfs_alert(mp, "%s: Superblock update failed!",
1512 __func__);
1513 }
1514 }
1515
1516 if (error) {
1517 xfs_warn(mp, "Failed to initialize disk quotas.");
1518 return;
1519 }
1520}
1521
1522/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * This is called after the superblock has been read in and we're ready to
1524 * iget the quota inodes.
1525 */
1526STATIC int
1527xfs_qm_init_quotainos(
1528 xfs_mount_t *mp)
1529{
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001530 struct xfs_inode *uip = NULL;
1531 struct xfs_inode *gip = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001532 struct xfs_inode *pip = NULL;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001533 int error;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001534 uint flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 ASSERT(mp->m_quotainfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /*
1539 * Get the uquota and gquota inodes
1540 */
Dave Chinner38c26bf2021-08-18 18:46:37 -07001541 if (xfs_has_quota(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (XFS_IS_UQUOTA_ON(mp) &&
1543 mp->m_sb.sb_uquotino != NULLFSINO) {
1544 ASSERT(mp->m_sb.sb_uquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001545 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1546 0, 0, &uip);
1547 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001548 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001550 if (XFS_IS_GQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 mp->m_sb.sb_gquotino != NULLFSINO) {
1552 ASSERT(mp->m_sb.sb_gquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001553 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1554 0, 0, &gip);
1555 if (error)
1556 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001558 if (XFS_IS_PQUOTA_ON(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -05001559 mp->m_sb.sb_pquotino != NULLFSINO) {
1560 ASSERT(mp->m_sb.sb_pquotino > 0);
1561 error = xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001562 0, 0, &pip);
1563 if (error)
1564 goto error_rele;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else {
1567 flags |= XFS_QMOPT_SBVERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
1570 /*
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001571 * Create the three inodes, if they don't exist already. The changes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * made above will get added to a transaction and logged in one of
1573 * the qino_alloc calls below. If the device is readonly,
1574 * temporarily switch to read-write to do this.
1575 */
1576 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001577 error = xfs_qm_qino_alloc(mp, &uip,
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001578 flags | XFS_QMOPT_UQUOTA);
1579 if (error)
1580 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 flags &= ~XFS_QMOPT_SBVERSION;
1583 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001584 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001585 error = xfs_qm_qino_alloc(mp, &gip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001586 flags | XFS_QMOPT_GQUOTA);
1587 if (error)
1588 goto error_rele;
1589
1590 flags &= ~XFS_QMOPT_SBVERSION;
1591 }
1592 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001593 error = xfs_qm_qino_alloc(mp, &pip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001594 flags | XFS_QMOPT_PQUOTA);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001595 if (error)
1596 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001599 mp->m_quotainfo->qi_uquotaip = uip;
1600 mp->m_quotainfo->qi_gquotaip = gip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001601 mp->m_quotainfo->qi_pquotaip = pip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Jesper Juhl014c2542006-01-15 02:37:08 +01001603 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001604
1605error_rele:
1606 if (uip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001607 xfs_irele(uip);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001608 if (gip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001609 xfs_irele(gip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001610 if (pip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001611 xfs_irele(pip);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001615STATIC void
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001616xfs_qm_destroy_quotainos(
Pavel Reichlc072fbe2019-11-12 17:04:26 -08001617 struct xfs_quotainfo *qi)
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001618{
1619 if (qi->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001620 xfs_irele(qi->qi_uquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001621 qi->qi_uquotaip = NULL; /* paranoia */
1622 }
1623 if (qi->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001624 xfs_irele(qi->qi_gquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001625 qi->qi_gquotaip = NULL;
1626 }
1627 if (qi->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001628 xfs_irele(qi->qi_pquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001629 qi->qi_pquotaip = NULL;
1630 }
1631}
1632
1633STATIC void
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001634xfs_qm_dqfree_one(
1635 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001637 struct xfs_mount *mp = dqp->q_mount;
1638 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Christoph Hellwig9f920f12012-03-13 08:52:35 +00001640 mutex_lock(&qi->qi_tree_lock);
Darrick J. Wong51dbb1b2020-07-14 10:37:32 -07001641 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001642
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001643 qi->qi_dquots--;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001644 mutex_unlock(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001646 xfs_qm_dqdestroy(dqp);
1647}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/* --------------- utility functions for vnodeops ---------------- */
1650
1651
1652/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001653 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1655 * quotas by creating this file.
1656 * This also attaches dquot(s) to the given inode after locking it,
1657 * and returns the dquots corresponding to the uid and/or gid.
1658 *
1659 * in : inode (unlocked)
1660 * out : udquot, gdquot with references taken and unlocked
1661 */
1662int
1663xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001664 struct xfs_inode *ip,
Christoph Hellwig54295152020-02-21 08:31:27 -08001665 kuid_t uid,
1666 kgid_t gid,
Christoph Hellwig7d095252009-06-08 15:33:32 +02001667 prid_t prid,
1668 uint flags,
1669 struct xfs_dquot **O_udqpp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001670 struct xfs_dquot **O_gdqpp,
1671 struct xfs_dquot **O_pdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001673 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig54295152020-02-21 08:31:27 -08001674 struct inode *inode = VFS_I(ip);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001675 struct user_namespace *user_ns = inode->i_sb->s_user_ns;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001676 struct xfs_dquot *uq = NULL;
1677 struct xfs_dquot *gq = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001678 struct xfs_dquot *pq = NULL;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001679 int error;
1680 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001682 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684
1685 lockflags = XFS_ILOCK_EXCL;
1686 xfs_ilock(ip, lockflags);
1687
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001688 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Christoph Hellwig54295152020-02-21 08:31:27 -08001689 gid = inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /*
1692 * Attach the dquot(s) to this inode, doing a dquot allocation
1693 * if necessary. The dquot(s) will not be locked.
1694 */
1695 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wong4882c192018-05-04 15:30:22 -07001696 error = xfs_qm_dqattach_locked(ip, true);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001697 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001703 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001704 ASSERT(O_udqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001705 if (!uid_eq(inode->i_uid, uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * What we need is the dquot that has this uid, and
1708 * if we send the inode to dqget, the uid of the inode
1709 * takes priority over what's sent in the uid argument.
1710 * We must unlock inode here before calling dqget if
1711 * we're not sending the inode, because otherwise
1712 * we'll deadlock by doing trans_reserve while
1713 * holding ilock.
1714 */
1715 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001716 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001717 XFS_DQTYPE_USER, true, &uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001718 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001719 ASSERT(error != -ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001720 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 /*
1723 * Get the ilock in the right order.
1724 */
1725 xfs_dqunlock(uq);
1726 lockflags = XFS_ILOCK_SHARED;
1727 xfs_ilock(ip, lockflags);
1728 } else {
1729 /*
1730 * Take an extra reference, because we'll return
1731 * this to caller
1732 */
1733 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001734 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001737 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001738 ASSERT(O_gdqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001739 if (!gid_eq(inode->i_gid, gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001741 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001742 XFS_DQTYPE_GROUP, true, &gq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001743 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001744 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001745 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 xfs_dqunlock(gq);
1748 lockflags = XFS_ILOCK_SHARED;
1749 xfs_ilock(ip, lockflags);
1750 } else {
1751 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001752 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001754 }
1755 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001756 ASSERT(O_pdqpp);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001757 if (ip->i_projid != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001758 xfs_iunlock(ip, lockflags);
Kaixu Xia5aff6752020-09-16 14:31:55 -07001759 error = xfs_qm_dqget(mp, prid,
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001760 XFS_DQTYPE_PROJ, true, &pq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001761 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001762 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001763 goto error_rele;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001764 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001765 xfs_dqunlock(pq);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001766 lockflags = XFS_ILOCK_SHARED;
1767 xfs_ilock(ip, lockflags);
1768 } else {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001769 ASSERT(ip->i_pdquot);
1770 pq = xfs_qm_dqhold(ip->i_pdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Kaixu Xiac1407352020-04-22 21:54:27 -07001773 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 xfs_iunlock(ip, lockflags);
1776 if (O_udqpp)
1777 *O_udqpp = uq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001778 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 xfs_qm_dqrele(uq);
1780 if (O_gdqpp)
1781 *O_gdqpp = gq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001782 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 xfs_qm_dqrele(gq);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001784 if (O_pdqpp)
1785 *O_pdqpp = pq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001786 else
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001787 xfs_qm_dqrele(pq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001788 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001789
1790error_rele:
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001791 xfs_qm_dqrele(gq);
1792 xfs_qm_dqrele(uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
1796/*
1797 * Actually transfer ownership, and do dquot modifications.
1798 * These were already reserved.
1799 */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001800struct xfs_dquot *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801xfs_qm_vop_chown(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001802 struct xfs_trans *tp,
1803 struct xfs_inode *ip,
1804 struct xfs_dquot **IO_olddq,
1805 struct xfs_dquot *newdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001807 struct xfs_dquot *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001808 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1809 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1810
Christoph Hellwig7d095252009-06-08 15:33:32 +02001811
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001812 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001813 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 /* old dquot */
1816 prevdq = *IO_olddq;
1817 ASSERT(prevdq);
1818 ASSERT(prevdq != newdq);
1819
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001820 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_nblocks));
Nathan Scott06d10dd2005-06-21 15:48:47 +10001821 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* the sparkling new dquot */
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001824 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_nblocks);
Nathan Scott06d10dd2005-06-21 15:48:47 +10001825 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /*
Darrick J. Wong1aecf372021-01-29 19:06:10 -08001828 * Back when we made quota reservations for the chown, we reserved the
1829 * ondisk blocks + delalloc blocks with the new dquot. Now that we've
1830 * switched the dquots, decrease the new dquot's block reservation
1831 * (having already bumped up the real counter) so that we don't have
1832 * any reservation to give back when we commit.
1833 */
1834 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_RES_BLKS,
1835 -ip->i_delayed_blks);
1836
1837 /*
1838 * Give the incore reservation for delalloc blocks back to the old
1839 * dquot. We don't normally handle delalloc quota reservations
1840 * transactionally, so just lock the dquot and subtract from the
1841 * reservation. Dirty the transaction because it's too late to turn
1842 * back now.
1843 */
1844 tp->t_flags |= XFS_TRANS_DIRTY;
1845 xfs_dqlock(prevdq);
1846 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
1847 prevdq->q_blk.reserved -= ip->i_delayed_blks;
1848 xfs_dqunlock(prevdq);
1849
1850 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001851 * Take an extra reference, because the inode is going to keep
1852 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001854 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jesper Juhl014c2542006-01-15 02:37:08 +01001856 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859int
1860xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001861 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001863 struct xfs_mount *mp = i_tab[0]->i_mount;
1864 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001866 if (!XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Christoph Hellwig7d095252009-06-08 15:33:32 +02001869 for (i = 0; (i < 4 && i_tab[i]); i++) {
1870 struct xfs_inode *ip = i_tab[i];
1871 int error;
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * Watch out for duplicate entries in the table.
1875 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001876 if (i == 0 || ip != i_tab[i-1]) {
1877 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001878 error = xfs_qm_dqattach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001880 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 }
1883 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887void
Christoph Hellwig7d095252009-06-08 15:33:32 +02001888xfs_qm_vop_create_dqattach(
1889 struct xfs_trans *tp,
1890 struct xfs_inode *ip,
1891 struct xfs_dquot *udqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001892 struct xfs_dquot *gdqp,
1893 struct xfs_dquot *pdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001895 struct xfs_mount *mp = tp->t_mountp;
1896
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001897 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return;
1899
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001900 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jie Liu37eb9702013-11-26 21:38:54 +08001902 if (udqp && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 ASSERT(ip->i_udquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001904 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001905
1906 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
1908 }
Jie Liu37eb9702013-11-26 21:38:54 +08001909 if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ASSERT(ip->i_gdquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001911 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
Christoph Hellwig54295152020-02-21 08:31:27 -08001912
Christoph Hellwig78e55892011-12-06 21:58:22 +00001913 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
1915 }
Jie Liu37eb9702013-11-26 21:38:54 +08001916 if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001917 ASSERT(ip->i_pdquot == NULL);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001918 ASSERT(ip->i_projid == pdqp->q_id);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001919
1920 ip->i_pdquot = xfs_qm_dqhold(pdqp);
1921 xfs_trans_mod_dquot(tp, pdqp, XFS_TRANS_DQ_ICOUNT, 1);
1922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Darrick J. Wong108523b2021-08-06 11:05:40 -07001925/* Decide if this inode's dquot is near an enforcement boundary. */
1926bool
1927xfs_inode_near_dquot_enforcement(
1928 struct xfs_inode *ip,
1929 xfs_dqtype_t type)
1930{
1931 struct xfs_dquot *dqp;
1932 int64_t freesp;
1933
1934 /* We only care for quotas that are enabled and enforced. */
1935 dqp = xfs_inode_dquot(ip, type);
1936 if (!dqp || !xfs_dquot_is_enforced(dqp))
1937 return false;
1938
1939 if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
1940 xfs_dquot_res_over_limits(&dqp->q_rtb))
1941 return true;
1942
1943 /* For space on the data device, check the various thresholds. */
1944 if (!dqp->q_prealloc_hi_wmark)
1945 return false;
1946
1947 if (dqp->q_blk.reserved < dqp->q_prealloc_lo_wmark)
1948 return false;
1949
1950 if (dqp->q_blk.reserved >= dqp->q_prealloc_hi_wmark)
1951 return true;
1952
1953 freesp = dqp->q_prealloc_hi_wmark - dqp->q_blk.reserved;
1954 if (freesp < dqp->q_low_space[XFS_QLOWSP_5_PCNT])
1955 return true;
1956
1957 return false;
1958}