blob: 32ac8d9c8940b8bd0227ff5251485ebd46f64cc4 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * The global quota manager. There is only one of these for the entire
31 * system, _not_ one per file system. XQM keeps track of the overall
32 * quota functionality, including maintaining the freelist and hash
33 * tables of dquots.
34 */
Pavel Reichlc072fbe2019-11-12 17:04:26 -080035STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp);
36STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Pavel Reichlc072fbe2019-11-12 17:04:26 -080038STATIC void xfs_qm_destroy_quotainos(struct xfs_quotainfo *qi);
Dave Chinnercd56a392013-08-28 10:18:07 +100039STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050041 * We use the batch lookup interface to iterate over the dquots as it
42 * currently is the only interface into the radix tree code that allows
43 * fuzzy lookups instead of exact matches. Holding the lock over multiple
44 * operations is fine as all callers are used either during mount/umount
45 * or quotaoff.
46 */
47#define XFS_DQ_LOOKUP_BATCH 32
48
49STATIC int
50xfs_qm_dquot_walk(
51 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -070052 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +100053 int (*execute)(struct xfs_dquot *dqp, void *data),
54 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050055{
56 struct xfs_quotainfo *qi = mp->m_quotainfo;
Chandra Seetharaman329e0872013-06-27 17:25:05 -050057 struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050058 uint32_t next_index;
59 int last_error = 0;
60 int skipped;
61 int nr_found;
62
63restart:
64 skipped = 0;
65 next_index = 0;
66 nr_found = 0;
67
68 while (1) {
69 struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
70 int error = 0;
71 int i;
72
73 mutex_lock(&qi->qi_tree_lock);
74 nr_found = radix_tree_gang_lookup(tree, (void **)batch,
75 next_index, XFS_DQ_LOOKUP_BATCH);
76 if (!nr_found) {
77 mutex_unlock(&qi->qi_tree_lock);
78 break;
79 }
80
81 for (i = 0; i < nr_found; i++) {
82 struct xfs_dquot *dqp = batch[i];
83
Darrick J. Wongc51df732020-07-14 10:37:30 -070084 next_index = dqp->q_id + 1;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050085
Christoph Hellwig43ff2122012-04-23 15:58:39 +100086 error = execute(batch[i], data);
Dave Chinner24513372014-06-25 14:58:08 +100087 if (error == -EAGAIN) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050088 skipped++;
89 continue;
90 }
Dave Chinner24513372014-06-25 14:58:08 +100091 if (error && last_error != -EFSCORRUPTED)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050092 last_error = error;
93 }
94
95 mutex_unlock(&qi->qi_tree_lock);
96
97 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +100098 if (last_error == -EFSCORRUPTED) {
Christoph Hellwigb84a3a92012-03-14 11:53:34 -050099 skipped = 0;
100 break;
101 }
Brian Fostercfaf2d02017-07-24 08:33:25 -0700102 /* we're done if id overflows back to zero */
103 if (!next_index)
104 break;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500105 }
106
107 if (skipped) {
108 delay(1);
109 goto restart;
110 }
111
112 return last_error;
113}
114
115
116/*
117 * Purge a dquot from all tracking data structures and free it.
118 */
119STATIC int
120xfs_qm_dqpurge(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000121 struct xfs_dquot *dqp,
122 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500123{
124 struct xfs_mount *mp = dqp->q_mount;
125 struct xfs_quotainfo *qi = mp->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 Chinner75c8c50f2021-08-18 18:46:53 -0700160 ASSERT(xfs_is_shutdown(mp) ||
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);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100175 XFS_STATS_DEC(mp, 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 /*
426 * This dquot has acquired a reference in the meantime remove it from
427 * the freelist and try again.
428 */
429 if (dqp->q_nrefs) {
430 xfs_dqunlock(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100431 XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
Dave Chinnercd56a392013-08-28 10:18:07 +1000432
433 trace_xfs_dqreclaim_want(dqp);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800434 list_lru_isolate(lru, &dqp->q_lru);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100435 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinner35163412013-08-28 10:18:08 +1000436 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000437 }
438
439 /*
440 * If the dquot is dirty, flush it. If it's already being flushed, just
441 * skip it so there is time for the IO to complete before we try to
442 * reclaim it again on the next LRU pass.
443 */
444 if (!xfs_dqflock_nowait(dqp)) {
445 xfs_dqunlock(dqp);
446 goto out_miss_busy;
447 }
448
449 if (XFS_DQ_IS_DIRTY(dqp)) {
450 struct xfs_buf *bp = NULL;
451 int error;
452
453 trace_xfs_dqreclaim_dirty(dqp);
454
455 /* we have to drop the LRU lock to flush the dquot */
456 spin_unlock(lru_lock);
457
458 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700459 if (error)
Dave Chinnercd56a392013-08-28 10:18:07 +1000460 goto out_unlock_dirty;
Dave Chinnercd56a392013-08-28 10:18:07 +1000461
462 xfs_buf_delwri_queue(bp, &isol->buffers);
463 xfs_buf_relse(bp);
464 goto out_unlock_dirty;
465 }
466 xfs_dqfunlock(dqp);
467
468 /*
469 * Prevent lookups now that we are past the point of no return.
470 */
Darrick J. Wong985a78f2020-07-14 10:37:13 -0700471 dqp->q_flags |= XFS_DQFLAG_FREEING;
Dave Chinnercd56a392013-08-28 10:18:07 +1000472 xfs_dqunlock(dqp);
473
474 ASSERT(dqp->q_nrefs == 0);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800475 list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100476 XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
Dave Chinnercd56a392013-08-28 10:18:07 +1000477 trace_xfs_dqreclaim_done(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100478 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
Dave Chinner35163412013-08-28 10:18:08 +1000479 return LRU_REMOVED;
Dave Chinnercd56a392013-08-28 10:18:07 +1000480
481out_miss_busy:
482 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100483 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000484 return LRU_SKIP;
Dave Chinnercd56a392013-08-28 10:18:07 +1000485
486out_unlock_dirty:
487 trace_xfs_dqreclaim_busy(dqp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100488 XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
Dave Chinner35163412013-08-28 10:18:08 +1000489 xfs_dqunlock(dqp);
490 spin_lock(lru_lock);
491 return LRU_RETRY;
Dave Chinnercd56a392013-08-28 10:18:07 +1000492}
493
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000494static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000495xfs_qm_shrink_scan(
496 struct shrinker *shrink,
497 struct shrink_control *sc)
498{
499 struct xfs_quotainfo *qi = container_of(shrink,
500 struct xfs_quotainfo, qi_shrinker);
501 struct xfs_qm_isolate isol;
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000502 unsigned long freed;
Dave Chinnercd56a392013-08-28 10:18:07 +1000503 int error;
Dave Chinnercd56a392013-08-28 10:18:07 +1000504
Mel Gormand0164ad2015-11-06 16:28:21 -0800505 if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
Dave Chinnercd56a392013-08-28 10:18:07 +1000506 return 0;
507
508 INIT_LIST_HEAD(&isol.buffers);
509 INIT_LIST_HEAD(&isol.dispose);
510
Vladimir Davydov503c3582015-02-12 14:58:47 -0800511 freed = list_lru_shrink_walk(&qi->qi_lru, sc,
512 xfs_qm_dquot_isolate, &isol);
Dave Chinnercd56a392013-08-28 10:18:07 +1000513
514 error = xfs_buf_delwri_submit(&isol.buffers);
515 if (error)
516 xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
517
518 while (!list_empty(&isol.dispose)) {
519 struct xfs_dquot *dqp;
520
521 dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
522 list_del_init(&dqp->q_lru);
523 xfs_qm_dqfree_one(dqp);
524 }
525
526 return freed;
527}
528
Andrew Morton2f5b56f2013-08-28 10:18:08 +1000529static unsigned long
Dave Chinnercd56a392013-08-28 10:18:07 +1000530xfs_qm_shrink_count(
531 struct shrinker *shrink,
532 struct shrink_control *sc)
533{
534 struct xfs_quotainfo *qi = container_of(shrink,
535 struct xfs_quotainfo, qi_shrinker);
536
Vladimir Davydov503c3582015-02-12 14:58:47 -0800537 return list_lru_shrink_count(&qi->qi_lru, sc);
Dave Chinnercd56a392013-08-28 10:18:07 +1000538}
539
Carlos Maiolinobe607942016-02-08 11:27:55 +1100540STATIC void
541xfs_qm_set_defquota(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800542 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700543 xfs_dqtype_t type,
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800544 struct xfs_quotainfo *qinf)
Carlos Maiolinobe607942016-02-08 11:27:55 +1100545{
Pavel Reichlaefe69a2019-11-12 17:04:02 -0800546 struct xfs_dquot *dqp;
547 struct xfs_def_quota *defq;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100548 int error;
549
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700550 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700551 if (error)
552 return;
Carlos Maiolinobe607942016-02-08 11:27:55 +1100553
Eric Sandeence6e7e79c2020-05-21 13:07:00 -0700554 defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
Carlos Maiolinobe607942016-02-08 11:27:55 +1100555
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700556 /*
557 * Timers and warnings have been already set, let's just set the
558 * default limits for this quota type
559 */
Darrick J. Wong438769e2020-07-14 10:37:32 -0700560 defq->blk.hard = dqp->q_blk.hardlimit;
561 defq->blk.soft = dqp->q_blk.softlimit;
562 defq->ino.hard = dqp->q_ino.hardlimit;
563 defq->ino.soft = dqp->q_ino.softlimit;
564 defq->rtb.hard = dqp->q_rtb.hardlimit;
565 defq->rtb.soft = dqp->q_rtb.softlimit;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700566 xfs_qm_dqdestroy(dqp);
567}
568
569/* Initialize quota time limits from the root dquot. */
570static void
571xfs_qm_init_timelimits(
572 struct xfs_mount *mp,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700573 xfs_dqtype_t type)
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700574{
Eric Sandeene8503012020-05-21 13:07:01 -0700575 struct xfs_quotainfo *qinf = mp->m_quotainfo;
576 struct xfs_def_quota *defq;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700577 struct xfs_dquot *dqp;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700578 int error;
579
Eric Sandeene8503012020-05-21 13:07:01 -0700580 defq = xfs_get_defquota(qinf, type);
581
Darrick J. Wong438769e2020-07-14 10:37:32 -0700582 defq->blk.time = XFS_QM_BTIMELIMIT;
583 defq->ino.time = XFS_QM_ITIMELIMIT;
584 defq->rtb.time = XFS_QM_RTBTIMELIMIT;
585 defq->blk.warn = XFS_QM_BWARNLIMIT;
586 defq->ino.warn = XFS_QM_IWARNLIMIT;
587 defq->rtb.warn = XFS_QM_RTBWARNLIMIT;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700588
589 /*
590 * We try to get the limits from the superuser's limits fields.
591 * This is quite hacky, but it is standard quota practice.
592 *
593 * Since we may not have done a quotacheck by this point, just read
594 * the dquot without attaching it to any hashtables or lists.
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700595 */
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700596 error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
597 if (error)
598 return;
599
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700600 /*
601 * The warnings and timers set the grace period given to
602 * a user or group before he or she can not perform any
603 * more writing. If it is zero, a default is used.
604 */
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700605 if (dqp->q_blk.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700606 defq->blk.time = dqp->q_blk.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700607 if (dqp->q_ino.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700608 defq->ino.time = dqp->q_ino.timer;
Darrick J. Wong19dce7e2020-07-14 10:37:32 -0700609 if (dqp->q_rtb.timer)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700610 defq->rtb.time = dqp->q_rtb.timer;
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700611 if (dqp->q_blk.warnings)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700612 defq->blk.warn = dqp->q_blk.warnings;
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700613 if (dqp->q_ino.warnings)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700614 defq->ino.warn = dqp->q_ino.warnings;
Darrick J. Wongc8c45fb2020-07-14 10:37:31 -0700615 if (dqp->q_rtb.warnings)
Darrick J. Wong438769e2020-07-14 10:37:32 -0700616 defq->rtb.warn = dqp->q_rtb.warnings;
Darrick J. Wongeac69e12018-05-14 06:34:32 -0700617
618 xfs_qm_dqdestroy(dqp);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100619}
620
Christoph Hellwiga4edd1d2009-01-19 02:03:11 +0100621/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * This initializes all the quota information that's kept in the
623 * mount structure
624 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000625STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626xfs_qm_init_quotainfo(
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700627 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700629 struct xfs_quotainfo *qinf;
Darrick J. Wong114e73c2018-05-04 15:30:23 -0700630 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700632 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800634 qinf = mp->m_quotainfo = kmem_zalloc(sizeof(struct xfs_quotainfo), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dave Chinner24513372014-06-25 14:58:08 +1000636 error = list_lru_init(&qinf->qi_lru);
Dave Chinneree4eec42014-05-15 09:23:24 +1000637 if (error)
638 goto out_free_qinf;
Glauber Costa5ca302c2013-08-28 10:18:18 +1000639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * See if quotainodes are setup, and if not, allocate them,
642 * and change the superblock accordingly.
643 */
Dave Chinneree4eec42014-05-15 09:23:24 +1000644 error = xfs_qm_init_quotainos(mp);
645 if (error)
646 goto out_free_lru;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000648 INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
649 INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500650 INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_NOFS);
Christoph Hellwig9f920f12012-03-13 08:52:35 +0000651 mutex_init(&qinf->qi_tree_lock);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* mutex used to serialize quotaoffs */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800654 mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Precalc some constants */
657 qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000658 qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
Dave Chinner38c26bf2021-08-18 18:46:37 -0700659 if (xfs_has_bigtime(mp)) {
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700660 qinf->qi_expiry_min =
661 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
662 qinf->qi_expiry_max =
663 xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
664 } else {
665 qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
666 qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
667 }
Darrick J. Wong06dbf822020-08-24 11:58:01 -0700668 trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
669 qinf->qi_expiry_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
672
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700673 xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
674 xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
675 xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700677 if (XFS_IS_UQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700678 xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700679 if (XFS_IS_GQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700680 xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700681 if (XFS_IS_PQUOTA_ON(mp))
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700682 xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
Carlos Maiolinobe607942016-02-08 11:27:55 +1100683
Dave Chinnercd56a392013-08-28 10:18:07 +1000684 qinf->qi_shrinker.count_objects = xfs_qm_shrink_count;
685 qinf->qi_shrinker.scan_objects = xfs_qm_shrink_scan;
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000686 qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnercd56a392013-08-28 10:18:07 +1000687 qinf->qi_shrinker.flags = SHRINKER_NUMA_AWARE;
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800688
689 error = register_shrinker(&qinf->qi_shrinker);
690 if (error)
691 goto out_free_inos;
692
Jesper Juhl014c2542006-01-15 02:37:08 +0100693 return 0;
Dave Chinneree4eec42014-05-15 09:23:24 +1000694
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800695out_free_inos:
696 mutex_destroy(&qinf->qi_quotaofflock);
697 mutex_destroy(&qinf->qi_tree_lock);
698 xfs_qm_destroy_quotainos(qinf);
Dave Chinneree4eec42014-05-15 09:23:24 +1000699out_free_lru:
700 list_lru_destroy(&qinf->qi_lru);
701out_free_qinf:
702 kmem_free(qinf);
703 mp->m_quotainfo = NULL;
704 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/*
708 * Gets called when unmounting a filesystem or when all quotas get
709 * turned off.
710 * This purges the quota inodes, destroys locks and frees itself.
711 */
712void
713xfs_qm_destroy_quotainfo(
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800714 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Pavel Reichlc072fbe2019-11-12 17:04:26 -0800716 struct xfs_quotainfo *qi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 qi = mp->m_quotainfo;
719 ASSERT(qi != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Christoph Hellwigf8739c32012-03-13 08:52:34 +0000721 unregister_shrinker(&qi->qi_shrinker);
Glauber Costaf5e1dd32013-08-28 10:18:18 +1000722 list_lru_destroy(&qi->qi_lru);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -0800723 xfs_qm_destroy_quotainos(qi);
Aliaksei Karaliou21968812017-12-21 13:18:26 -0800724 mutex_destroy(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 mutex_destroy(&qi->qi_quotaofflock);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000726 kmem_free(qi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 mp->m_quotainfo = NULL;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
731 * Create an inode and return with a reference already taken, but unlocked
732 * This is how we create quota inodes
733 */
734STATIC int
735xfs_qm_qino_alloc(
Dave Chinner1abcf262020-12-09 10:05:15 -0800736 struct xfs_mount *mp,
737 struct xfs_inode **ipp,
738 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Dave Chinner1abcf262020-12-09 10:05:15 -0800740 struct xfs_trans *tp;
741 int error;
742 bool need_alloc = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dave Chinner1abcf262020-12-09 10:05:15 -0800744 *ipp = NULL;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500745 /*
746 * With superblock that doesn't have separate pquotino, we
747 * share an inode between gquota and pquota. If the on-disk
748 * superblock has GQUOTA and the filesystem is now mounted
749 * with PQUOTA, just use sb_gquotino for sb_pquotino and
750 * vice-versa.
751 */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700752 if (!xfs_has_pquotino(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -0500753 (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
754 xfs_ino_t ino = NULLFSINO;
755
756 if ((flags & XFS_QMOPT_PQUOTA) &&
757 (mp->m_sb.sb_gquotino != NULLFSINO)) {
758 ino = mp->m_sb.sb_gquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800759 if (XFS_IS_CORRUPT(mp,
760 mp->m_sb.sb_pquotino != NULLFSINO))
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700761 return -EFSCORRUPTED;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500762 } else if ((flags & XFS_QMOPT_GQUOTA) &&
763 (mp->m_sb.sb_pquotino != NULLFSINO)) {
764 ino = mp->m_sb.sb_pquotino;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800765 if (XFS_IS_CORRUPT(mp,
766 mp->m_sb.sb_gquotino != NULLFSINO))
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700767 return -EFSCORRUPTED;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500768 }
769 if (ino != NULLFSINO) {
Dave Chinner1abcf262020-12-09 10:05:15 -0800770 error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500771 if (error)
772 return error;
773 mp->m_sb.sb_gquotino = NULLFSINO;
774 mp->m_sb.sb_pquotino = NULLFSINO;
Dave Chinner58c90472015-02-23 22:38:08 +1100775 need_alloc = false;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500776 }
777 }
778
Christoph Hellwig253f4912016-04-06 09:19:55 +1000779 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
Kaixu Xiafb353ff2020-04-22 21:54:28 -0700780 need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0,
781 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000782 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +0100783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Chinner58c90472015-02-23 22:38:08 +1100785 if (need_alloc) {
Dave Chinnerb652afd2021-06-02 10:48:24 +1000786 xfs_ino_t ino;
787
788 error = xfs_dialloc(&tp, 0, S_IFREG, &ino);
789 if (!error)
790 error = xfs_init_new_inode(&init_user_ns, tp, NULL, ino,
791 S_IFREG, 1, 0, 0, false, ipp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500792 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000793 xfs_trans_cancel(tp);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500794 return error;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
798 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * Make the changes in the superblock, and log those too.
800 * sbfields arg may contain fields other than *QUOTINO;
801 * VERSIONNUM for example.
802 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000803 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (flags & XFS_QMOPT_SBVERSION) {
Dave Chinner38c26bf2021-08-18 18:46:37 -0700805 ASSERT(!xfs_has_quota(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dave Chinner38c26bf2021-08-18 18:46:37 -0700807 xfs_add_quota(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mp->m_sb.sb_uquotino = NULLFSINO;
809 mp->m_sb.sb_gquotino = NULLFSINO;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500810 mp->m_sb.sb_pquotino = NULLFSINO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Chandra Seetharamand892d582013-07-19 17:36:02 -0500812 /* qflags will get updated fully _after_ quotacheck */
813 mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 if (flags & XFS_QMOPT_UQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800816 mp->m_sb.sb_uquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500817 else if (flags & XFS_QMOPT_GQUOTA)
Dave Chinner1abcf262020-12-09 10:05:15 -0800818 mp->m_sb.sb_gquotino = (*ipp)->i_ino;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500819 else
Dave Chinner1abcf262020-12-09 10:05:15 -0800820 mp->m_sb.sb_pquotino = (*ipp)->i_ino;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000821 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100822 xfs_log_sb(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Christoph Hellwig70393312015-06-04 13:48:08 +1000824 error = xfs_trans_commit(tp);
Dave Chinner58c90472015-02-23 22:38:08 +1100825 if (error) {
Dave Chinner75c8c50f2021-08-18 18:46:53 -0700826 ASSERT(xfs_is_shutdown(mp));
Dave Chinner53487782011-03-07 10:05:35 +1100827 xfs_alert(mp, "%s failed (error %d)!", __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Dave Chinner58c90472015-02-23 22:38:08 +1100829 if (need_alloc)
Dave Chinner1abcf262020-12-09 10:05:15 -0800830 xfs_finish_inode_setup(*ipp);
Dave Chinner58c90472015-02-23 22:38:08 +1100831 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834
David Chinner5b139732008-04-10 12:20:10 +1000835STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836xfs_qm_reset_dqcounts(
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700837 struct xfs_mount *mp,
838 struct xfs_buf *bp,
839 xfs_dqid_t id,
840 xfs_dqtype_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Dave Chinner6fcdc592013-06-03 15:28:46 +1000842 struct xfs_dqblk *dqb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int j;
844
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000845 trace_xfs_reset_dqcounts(bp, _RET_IP_);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * Reset all counters and timers. They'll be
849 * started afresh by xfs_qm_quotacheck.
850 */
851#ifdef DEBUG
Eric Sandeen90115402017-04-19 12:55:57 -0700852 j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
Christoph Hellwig11a83f42021-10-11 16:12:37 -0700853 sizeof(struct xfs_dqblk);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000854 ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#endif
Dave Chinner6fcdc592013-06-03 15:28:46 +1000856 dqb = bp->b_addr;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000857 for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000858 struct xfs_disk_dquot *ddq;
859
860 ddq = (struct xfs_disk_dquot *)&dqb[j];
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * Do a sanity check, and if needed, repair the dqblk. Don't
864 * output any warnings because it's perfectly possible to
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800865 * find uninitialised dquot blks. See comment in
866 * xfs_dquot_verify.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
Darrick J. Wongf9751c42020-07-15 17:41:24 -0700868 if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700869 (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700870 xfs_dqblk_repair(mp, &dqb[j], id + j, type);
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800871
Jan Karadfcc70a2015-02-23 22:34:17 +1100872 /*
873 * Reset type in case we are reusing group quota file for
874 * project quotas or vice versa
875 */
Darrick J. Wongd8c1af0d2020-07-15 18:05:39 -0700876 ddq->d_type = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100877 ddq->d_bcount = 0;
878 ddq->d_icount = 0;
879 ddq->d_rtbcount = 0;
Darrick J. Wong58855392020-03-24 20:12:53 -0700880
881 /*
882 * dquot id 0 stores the default grace period and the maximum
883 * warning limit that were set by the administrator, so we
884 * should not reset them.
885 */
886 if (ddq->d_id != 0) {
887 ddq->d_btimer = 0;
888 ddq->d_itimer = 0;
889 ddq->d_rtbtimer = 0;
890 ddq->d_bwarns = 0;
891 ddq->d_iwarns = 0;
892 ddq->d_rtbwarns = 0;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700893 if (xfs_has_bigtime(mp))
Darrick J. Wong4ea1ff32020-08-17 09:59:51 -0700894 ddq->d_type |= XFS_DQTYPE_BIGTIME;
Darrick J. Wong58855392020-03-24 20:12:53 -0700895 }
Dave Chinner6fcdc592013-06-03 15:28:46 +1000896
Dave Chinner38c26bf2021-08-18 18:46:37 -0700897 if (xfs_has_crc(mp)) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000898 xfs_update_cksum((char *)&dqb[j],
899 sizeof(struct xfs_dqblk),
900 XFS_DQUOT_CRC_OFF);
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700906xfs_qm_reset_dqcounts_all(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000907 struct xfs_mount *mp,
908 xfs_dqid_t firstid,
909 xfs_fsblock_t bno,
910 xfs_filblks_t blkcnt,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700911 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000912 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000914 struct xfs_buf *bp;
Darrick J. Wong0dcc0722020-07-14 10:37:12 -0700915 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 ASSERT(blkcnt > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /*
920 * Blkcnt arg can be a very big number, and might even be
921 * larger than the log itself. So, we have to break it up into
922 * manageable-sized transactions.
923 * Note that we don't start a permanent transaction here; we might
924 * not be able to get a log reservation for the whole thing up front,
925 * and we don't really care to either, because we just discard
926 * everything if we were to crash in the middle of this loop.
927 */
928 while (blkcnt--) {
929 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
930 XFS_FSB_TO_DADDR(mp, bno),
Dave Chinnerc6319192012-11-14 17:50:13 +1100931 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100932 &xfs_dquot_buf_ops);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000933
934 /*
935 * CRC and validation errors will return a EFSCORRUPTED here. If
936 * this occurs, re-read without CRC validation so that we can
937 * repair the damage via xfs_qm_reset_dqcounts(). This process
938 * will leave a trace in the log indicating corruption has
939 * been detected.
940 */
Dave Chinner24513372014-06-25 14:58:08 +1000941 if (error == -EFSCORRUPTED) {
Dave Chinner6fcdc592013-06-03 15:28:46 +1000942 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
943 XFS_FSB_TO_DADDR(mp, bno),
944 mp->m_quotainfo->qi_dqchunklen, 0, &bp,
945 NULL);
946 }
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (error)
949 break;
950
Dave Chinner5fd364f2014-08-04 12:43:26 +1000951 /*
952 * A corrupt buffer might not have a verifier attached, so
953 * make sure we have the correct one attached before writeback
954 * occurs.
955 */
956 bp->b_ops = &xfs_dquot_buf_ops;
David Chinner5b139732008-04-10 12:20:10 +1000957 xfs_qm_reset_dqcounts(mp, bp, firstid, type);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000958 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwig61551f12011-08-23 08:28:06 +0000959 xfs_buf_relse(bp);
Dave Chinner6fcdc592013-06-03 15:28:46 +1000960
961 /* goto the next block. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 bno++;
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000963 firstid += mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000965
Jesper Juhl014c2542006-01-15 02:37:08 +0100966 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969/*
Darrick J. Wong28b90602018-05-04 15:31:20 -0700970 * Iterate over all allocated dquot blocks in this quota inode, zeroing all
971 * counters for every chunk of dquots that we find.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
973STATIC int
Darrick J. Wong28b90602018-05-04 15:31:20 -0700974xfs_qm_reset_dqcounts_buf(
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000975 struct xfs_mount *mp,
976 struct xfs_inode *qip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700977 xfs_dqtype_t type,
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000978 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000980 struct xfs_bmbt_irec *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int i, nmaps; /* number of map entries */
982 int error; /* return value */
983 xfs_fileoff_t lblkno;
984 xfs_filblks_t maxlblkcnt;
985 xfs_dqid_t firstid;
986 xfs_fsblock_t rablkno;
987 xfs_filblks_t rablkcnt;
988
989 error = 0;
990 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000991 * This looks racy, but we can't keep an inode lock across a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * trans_reserve. But, this gets called during quotacheck, and that
993 * happens only at mount time which is single threaded.
994 */
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700995 if (qip->i_nblocks == 0)
Jesper Juhl014c2542006-01-15 02:37:08 +0100996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700998 map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 lblkno = 0;
Dave Chinner32972382012-06-08 15:44:54 +10001001 maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 do {
Christoph Hellwigda51d322013-12-06 12:30:14 -08001003 uint lock_mode;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 nmaps = XFS_DQITER_MAP_SIZE;
1006 /*
1007 * We aren't changing the inode itself. Just changing
1008 * some of its data. No new blocks are added here, and
1009 * the inode is never added to the transaction.
1010 */
Christoph Hellwigda51d322013-12-06 12:30:14 -08001011 lock_mode = xfs_ilock_data_map_shared(qip);
Dave Chinner5c8ed202011-09-18 20:40:45 +00001012 error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
1013 map, &nmaps, 0);
Christoph Hellwigda51d322013-12-06 12:30:14 -08001014 xfs_iunlock(qip, lock_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (error)
1016 break;
1017
1018 ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
1019 for (i = 0; i < nmaps; i++) {
1020 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
1021 ASSERT(map[i].br_blockcount);
1022
1023
1024 lblkno += map[i].br_blockcount;
1025
1026 if (map[i].br_startblock == HOLESTARTBLOCK)
1027 continue;
1028
1029 firstid = (xfs_dqid_t) map[i].br_startoff *
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001030 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * Do a read-ahead on the next extent.
1033 */
1034 if ((i+1 < nmaps) &&
1035 (map[i+1].br_startblock != HOLESTARTBLOCK)) {
1036 rablkcnt = map[i+1].br_blockcount;
1037 rablkno = map[i+1].br_startblock;
1038 while (rablkcnt--) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001039 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 XFS_FSB_TO_DADDR(mp, rablkno),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001041 mp->m_quotainfo->qi_dqchunklen,
Dave Chinner5fd364f2014-08-04 12:43:26 +10001042 &xfs_dquot_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 rablkno++;
1044 }
1045 }
1046 /*
1047 * Iterate thru all the blks in the extent and
1048 * reset the counters of all the dquots inside them.
1049 */
Darrick J. Wong28b90602018-05-04 15:31:20 -07001050 error = xfs_qm_reset_dqcounts_all(mp, firstid,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001051 map[i].br_startblock,
1052 map[i].br_blockcount,
Darrick J. Wong0dcc0722020-07-14 10:37:12 -07001053 type, buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001054 if (error)
1055 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 } while (nmaps > 0);
1058
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001059out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001060 kmem_free(map);
Jesper Juhl014c2542006-01-15 02:37:08 +01001061 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064/*
1065 * Called by dqusage_adjust in doing a quotacheck.
Christoph Hellwig52fda112010-09-06 01:44:22 +00001066 *
1067 * Given the inode, and a dquot id this updates both the incore dqout as well
1068 * as the buffer copy. This is so that once the quotacheck is done, we can
1069 * just log all the buffers, as opposed to logging numerous updates to
1070 * individual dquots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Christoph Hellwig52fda112010-09-06 01:44:22 +00001072STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073xfs_qm_quotacheck_dqadjust(
Christoph Hellwig52fda112010-09-06 01:44:22 +00001074 struct xfs_inode *ip,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -07001075 xfs_dqtype_t type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 xfs_qcnt_t nblks,
1077 xfs_qcnt_t rtblks)
1078{
Christoph Hellwig52fda112010-09-06 01:44:22 +00001079 struct xfs_mount *mp = ip->i_mount;
1080 struct xfs_dquot *dqp;
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001081 xfs_dqid_t id;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001082 int error;
1083
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001084 id = xfs_qm_id_for_quotatype(ip, type);
Darrick J. Wong30ab2dc2018-05-04 15:30:24 -07001085 error = xfs_qm_dqget(mp, id, type, true, &dqp);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001086 if (error) {
1087 /*
1088 * Shouldn't be able to turn off quotas here.
1089 */
Dave Chinner24513372014-06-25 14:58:08 +10001090 ASSERT(error != -ESRCH);
1091 ASSERT(error != -ENOENT);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001092 return error;
1093 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001094
1095 trace_xfs_dqadjust(dqp);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /*
1098 * Adjust the inode count and the block count to reflect this inode's
1099 * resource usage.
1100 */
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001101 dqp->q_ino.count++;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001102 dqp->q_ino.reserved++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (nblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001104 dqp->q_blk.count += nblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001105 dqp->q_blk.reserved += nblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 if (rtblks) {
Darrick J. Wongbe37d402020-07-14 10:37:31 -07001108 dqp->q_rtb.count += rtblks;
Darrick J. Wong784e80f2020-07-14 10:37:30 -07001109 dqp->q_rtb.reserved += rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
1112 /*
1113 * Set default limits, adjust timers (since we changed usages)
Christoph Hellwig191f8482010-04-20 17:01:53 +10001114 *
1115 * There are no timers for the default values set in the root dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Darrick J. Wongc51df732020-07-14 10:37:30 -07001117 if (dqp->q_id) {
Darrick J. Wongc8c753e2020-07-14 10:37:33 -07001118 xfs_qm_adjust_dqlimits(dqp);
1119 xfs_qm_adjust_dqtimers(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001122 dqp->q_flags |= XFS_DQFLAG_DIRTY;
Christoph Hellwig52fda112010-09-06 01:44:22 +00001123 xfs_qm_dqput(dqp);
1124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * callback routine supplied to bulkstat(). Given an inumber, find its
1129 * dquots and update them to account for resources taken by that inode.
1130 */
1131/* ARGSUSED */
1132STATIC int
1133xfs_qm_dqusage_adjust(
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001134 struct xfs_mount *mp,
1135 struct xfs_trans *tp,
1136 xfs_ino_t ino,
1137 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001139 struct xfs_inode *ip;
1140 xfs_qcnt_t nblks;
1141 xfs_filblks_t rtblks = 0; /* total rt blks */
1142 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001144 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /*
1147 * rootino must have its resources accounted for, not so with the quota
1148 * inodes.
1149 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001150 if (xfs_is_quota_inode(&mp->m_sb, ino))
1151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /*
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001154 * We don't _need_ to take the ilock EXCL here because quotacheck runs
1155 * at mount time and therefore nobody will be racing chown/chproj.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001157 error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
1158 if (error == -EINVAL || error == -ENOENT)
1159 return 0;
1160 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001161 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Christoph Hellwig52fda112010-09-06 01:44:22 +00001163 ASSERT(ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Christoph Hellwig52fda112010-09-06 01:44:22 +00001165 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001166 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1167
Christoph Hellwig862a8042021-04-13 11:15:09 -07001168 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1169 if (error)
1170 goto error0;
Christoph Hellwig8bfadd82017-08-29 15:44:14 -07001171
1172 xfs_bmap_count_leaves(ifp, &rtblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001175 nblks = (xfs_qcnt_t)ip->i_nblocks - rtblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /*
1178 * Add the (disk blocks and inode) resources occupied by this
1179 * inode to its dquots. We do this adjustment in the incore dquot,
1180 * and also copy the changes to its buffer.
1181 * We don't care about putting these changes in a transaction
1182 * envelope because if we crash in the middle of a 'quotacheck'
1183 * we have to start from the beginning anyway.
1184 * Once we're done, we'll log all the dquot bufs.
1185 *
Nathan Scottc41564b2006-03-29 08:55:14 +10001186 * The *QUOTA_ON checks below may look pretty racy, but quotachecks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * and quotaoffs don't race. (Quotachecks happen at mount time only).
1188 */
1189 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001190 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001191 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001192 if (error)
1193 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Christoph Hellwig52fda112010-09-06 01:44:22 +00001196 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001197 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001198 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001199 if (error)
1200 goto error0;
1201 }
1202
1203 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001204 error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
Darrick J. Wong0fcef122018-05-04 15:30:22 -07001205 rtblks);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001206 if (error)
1207 goto error0;
1208 }
1209
Christoph Hellwig52fda112010-09-06 01:44:22 +00001210error0:
Darrick J. Wong44a87362018-07-25 12:52:32 -07001211 xfs_irele(ip);
Christoph Hellwig52fda112010-09-06 01:44:22 +00001212 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001215STATIC int
1216xfs_qm_flush_one(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001217 struct xfs_dquot *dqp,
1218 void *data)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001219{
Brian Foster7912e7f2017-06-14 21:21:45 -07001220 struct xfs_mount *mp = dqp->q_mount;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001221 struct list_head *buffer_list = data;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001222 struct xfs_buf *bp = NULL;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001223 int error = 0;
1224
1225 xfs_dqlock(dqp);
Darrick J. Wong985a78f2020-07-14 10:37:13 -07001226 if (dqp->q_flags & XFS_DQFLAG_FREEING)
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001227 goto out_unlock;
1228 if (!XFS_DQ_IS_DIRTY(dqp))
1229 goto out_unlock;
1230
Brian Foster7912e7f2017-06-14 21:21:45 -07001231 /*
1232 * The only way the dquot is already flush locked by the time quotacheck
1233 * gets here is if reclaim flushed it before the dqadjust walk dirtied
1234 * it for the final time. Quotacheck collects all dquot bufs in the
1235 * local delwri queue before dquots are dirtied, so reclaim can't have
1236 * possibly queued it for I/O. The only way out is to push the buffer to
1237 * cycle the flush lock.
1238 */
1239 if (!xfs_dqflock_nowait(dqp)) {
1240 /* buf is pinned in-core by delwri list */
Dave Chinner8925a3d2018-04-18 08:25:20 -07001241 bp = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
1242 mp->m_quotainfo->qi_dqchunklen, 0);
Brian Foster7912e7f2017-06-14 21:21:45 -07001243 if (!bp) {
1244 error = -EINVAL;
1245 goto out_unlock;
1246 }
1247 xfs_buf_unlock(bp);
1248
1249 xfs_buf_delwri_pushbuf(bp, buffer_list);
1250 xfs_buf_rele(bp);
1251
1252 error = -EAGAIN;
1253 goto out_unlock;
1254 }
1255
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001256 error = xfs_qm_dqflush(dqp, &bp);
1257 if (error)
1258 goto out_unlock;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001259
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001260 xfs_buf_delwri_queue(bp, buffer_list);
Christoph Hellwigfe7257f2012-04-23 15:58:37 +10001261 xfs_buf_relse(bp);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001262out_unlock:
1263 xfs_dqunlock(dqp);
1264 return error;
1265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/*
1268 * Walk thru all the filesystem inodes and construct a consistent view
1269 * of the disk quota world. If the quotacheck fails, disable quotas.
1270 */
Jie Liueb866bb2014-07-24 20:49:57 +10001271STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272xfs_qm_quotacheck(
1273 xfs_mount_t *mp)
1274{
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001275 int error, error2;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001276 uint flags;
1277 LIST_HEAD (buffer_list);
1278 struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
1279 struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001280 struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 flags = 0;
1283
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001284 ASSERT(uip || gip || pip);
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001285 ASSERT(XFS_IS_QUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Dave Chinner0b932cc2011-03-07 10:08:35 +11001287 xfs_notice(mp, "Quotacheck needed: Please wait.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001290 * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * their counters to zero. We need a clean slate.
1292 * We don't log our changes till later.
1293 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001294 if (uip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001295 error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001296 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto error_return;
1299 flags |= XFS_UQUOTA_CHKD;
1300 }
1301
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001302 if (gip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001303 error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001304 &buffer_list);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001305 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto error_return;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001307 flags |= XFS_GQUOTA_CHKD;
1308 }
1309
1310 if (pip) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001311 error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001312 &buffer_list);
1313 if (error)
1314 goto error_return;
1315 flags |= XFS_PQUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Darrick J. Wong13d59a22019-07-03 20:36:28 -07001318 error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
1319 NULL);
Darrick J. Wongebd126a2019-07-02 09:39:39 -07001320 if (error)
1321 goto error_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /*
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001324 * We've made all the changes that we need to make incore. Flush them
1325 * down to disk buffers if everything was updated successfully.
David Chinner4b8879d2008-04-10 12:20:17 +10001326 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001327 if (XFS_IS_UQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001328 error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001329 &buffer_list);
1330 }
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001331 if (XFS_IS_GQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001332 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001333 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001334 if (!error)
1335 error = error2;
1336 }
1337 if (XFS_IS_PQUOTA_ON(mp)) {
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001338 error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001339 &buffer_list);
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001340 if (!error)
1341 error = error2;
1342 }
David Chinner4b8879d2008-04-10 12:20:17 +10001343
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001344 error2 = xfs_buf_delwri_submit(&buffer_list);
1345 if (!error)
1346 error = error2;
1347
David Chinner4b8879d2008-04-10 12:20:17 +10001348 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * We can get this error if we couldn't do a dquot allocation inside
1350 * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
1351 * dirty dquots that might be cached, we just want to get rid of them
1352 * and turn quotaoff. The dquots won't be attached to any of the inodes
1353 * at this point (because we intentionally didn't in dqget_noattach).
1354 */
1355 if (error) {
Christoph Hellwige497dfba2021-08-06 11:05:36 -07001356 xfs_qm_dqpurge_all(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto error_return;
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * If one type of quotas is off, then it will lose its
1362 * quotachecked status, since we won't be doing accounting for
1363 * that type anymore.
1364 */
Chandra Seetharaman4177af32012-01-23 17:31:43 +00001365 mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 mp->m_qflags |= flags;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 error_return:
Brian Foster20e8a062017-04-21 12:40:44 -07001369 xfs_buf_delwri_cancel(&buffer_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001372 xfs_warn(mp,
1373 "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
1374 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
1376 * We must turn off quotas.
1377 */
1378 ASSERT(mp->m_quotainfo != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 xfs_qm_destroy_quotainfo(mp);
David Chinner31d55772008-04-10 12:20:38 +10001380 if (xfs_mount_reset_sbqflags(mp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001381 xfs_warn(mp,
1382 "Quotacheck: Failed to reset quota flags.");
David Chinner31d55772008-04-10 12:20:38 +10001383 }
Dave Chinner0b932cc2011-03-07 10:08:35 +11001384 } else
1385 xfs_notice(mp, "Quotacheck: Done.");
Eric Sandeend99831f2014-06-22 15:03:54 +10001386 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
Jie Liueb866bb2014-07-24 20:49:57 +10001390 * This is called from xfs_mountfs to start quotas and initialize all
1391 * necessary data structures like quotainfo. This is also responsible for
1392 * running a quotacheck as necessary. We are guaranteed that the superblock
1393 * is consistently read in at this point.
1394 *
1395 * If we fail here, the mount will continue with quota turned off. We don't
1396 * need to inidicate success or failure at all.
1397 */
1398void
1399xfs_qm_mount_quotas(
1400 struct xfs_mount *mp)
1401{
1402 int error = 0;
1403 uint sbf;
1404
1405 /*
1406 * If quotas on realtime volumes is not supported, we disable
1407 * quotas immediately.
1408 */
1409 if (mp->m_sb.sb_rextents) {
1410 xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
1411 mp->m_qflags = 0;
1412 goto write_changes;
1413 }
1414
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001415 ASSERT(XFS_IS_QUOTA_ON(mp));
Jie Liueb866bb2014-07-24 20:49:57 +10001416
1417 /*
1418 * Allocate the quotainfo structure inside the mount struct, and
1419 * create quotainode(s), and change/rev superblock if necessary.
1420 */
1421 error = xfs_qm_init_quotainfo(mp);
1422 if (error) {
1423 /*
1424 * We must turn off quotas.
1425 */
1426 ASSERT(mp->m_quotainfo == NULL);
1427 mp->m_qflags = 0;
1428 goto write_changes;
1429 }
1430 /*
1431 * If any of the quotas are not consistent, do a quotacheck.
1432 */
1433 if (XFS_QM_NEED_QUOTACHECK(mp)) {
1434 error = xfs_qm_quotacheck(mp);
1435 if (error) {
1436 /* Quotacheck failed and disabled quotas. */
1437 return;
1438 }
1439 }
1440 /*
1441 * If one type of quotas is off, then it will lose its
1442 * quotachecked status, since we won't be doing accounting for
1443 * that type anymore.
1444 */
1445 if (!XFS_IS_UQUOTA_ON(mp))
1446 mp->m_qflags &= ~XFS_UQUOTA_CHKD;
1447 if (!XFS_IS_GQUOTA_ON(mp))
1448 mp->m_qflags &= ~XFS_GQUOTA_CHKD;
1449 if (!XFS_IS_PQUOTA_ON(mp))
1450 mp->m_qflags &= ~XFS_PQUOTA_CHKD;
1451
1452 write_changes:
1453 /*
1454 * We actually don't have to acquire the m_sb_lock at all.
1455 * This can only be called from mount, and that's single threaded. XXX
1456 */
1457 spin_lock(&mp->m_sb_lock);
1458 sbf = mp->m_sb.sb_qflags;
1459 mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
1460 spin_unlock(&mp->m_sb_lock);
1461
1462 if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001463 if (xfs_sync_sb(mp, false)) {
Jie Liueb866bb2014-07-24 20:49:57 +10001464 /*
1465 * We could only have been turning quotas off.
1466 * We aren't in very good shape actually because
1467 * the incore structures are convinced that quotas are
1468 * off, but the on disk superblock doesn't know that !
1469 */
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001470 ASSERT(!(XFS_IS_QUOTA_ON(mp)));
Jie Liueb866bb2014-07-24 20:49:57 +10001471 xfs_alert(mp, "%s: Superblock update failed!",
1472 __func__);
1473 }
1474 }
1475
1476 if (error) {
1477 xfs_warn(mp, "Failed to initialize disk quotas.");
1478 return;
1479 }
1480}
1481
1482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * This is called after the superblock has been read in and we're ready to
1484 * iget the quota inodes.
1485 */
1486STATIC int
1487xfs_qm_init_quotainos(
1488 xfs_mount_t *mp)
1489{
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001490 struct xfs_inode *uip = NULL;
1491 struct xfs_inode *gip = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001492 struct xfs_inode *pip = NULL;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001493 int error;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001494 uint flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 ASSERT(mp->m_quotainfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /*
1499 * Get the uquota and gquota inodes
1500 */
Dave Chinner38c26bf2021-08-18 18:46:37 -07001501 if (xfs_has_quota(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (XFS_IS_UQUOTA_ON(mp) &&
1503 mp->m_sb.sb_uquotino != NULLFSINO) {
1504 ASSERT(mp->m_sb.sb_uquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001505 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
1506 0, 0, &uip);
1507 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001510 if (XFS_IS_GQUOTA_ON(mp) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 mp->m_sb.sb_gquotino != NULLFSINO) {
1512 ASSERT(mp->m_sb.sb_gquotino > 0);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001513 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
1514 0, 0, &gip);
1515 if (error)
1516 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001518 if (XFS_IS_PQUOTA_ON(mp) &&
Chandra Seetharamand892d582013-07-19 17:36:02 -05001519 mp->m_sb.sb_pquotino != NULLFSINO) {
1520 ASSERT(mp->m_sb.sb_pquotino > 0);
1521 error = xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001522 0, 0, &pip);
1523 if (error)
1524 goto error_rele;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 } else {
1527 flags |= XFS_QMOPT_SBVERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
1530 /*
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001531 * Create the three inodes, if they don't exist already. The changes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * made above will get added to a transaction and logged in one of
1533 * the qino_alloc calls below. If the device is readonly,
1534 * temporarily switch to read-write to do this.
1535 */
1536 if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001537 error = xfs_qm_qino_alloc(mp, &uip,
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001538 flags | XFS_QMOPT_UQUOTA);
1539 if (error)
1540 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 flags &= ~XFS_QMOPT_SBVERSION;
1543 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001544 if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001545 error = xfs_qm_qino_alloc(mp, &gip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001546 flags | XFS_QMOPT_GQUOTA);
1547 if (error)
1548 goto error_rele;
1549
1550 flags &= ~XFS_QMOPT_SBVERSION;
1551 }
1552 if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001553 error = xfs_qm_qino_alloc(mp, &pip,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001554 flags | XFS_QMOPT_PQUOTA);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001555 if (error)
1556 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001559 mp->m_quotainfo->qi_uquotaip = uip;
1560 mp->m_quotainfo->qi_gquotaip = gip;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001561 mp->m_quotainfo->qi_pquotaip = pip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Jesper Juhl014c2542006-01-15 02:37:08 +01001563 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001564
1565error_rele:
1566 if (uip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001567 xfs_irele(uip);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001568 if (gip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001569 xfs_irele(gip);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001570 if (pip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07001571 xfs_irele(pip);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001572 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001575STATIC void
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001576xfs_qm_destroy_quotainos(
Pavel Reichlc072fbe2019-11-12 17:04:26 -08001577 struct xfs_quotainfo *qi)
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001578{
1579 if (qi->qi_uquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001580 xfs_irele(qi->qi_uquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001581 qi->qi_uquotaip = NULL; /* paranoia */
1582 }
1583 if (qi->qi_gquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001584 xfs_irele(qi->qi_gquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001585 qi->qi_gquotaip = NULL;
1586 }
1587 if (qi->qi_pquotaip) {
Darrick J. Wong44a87362018-07-25 12:52:32 -07001588 xfs_irele(qi->qi_pquotaip);
Aliaksei Karaliou3a3882f2017-12-21 13:18:26 -08001589 qi->qi_pquotaip = NULL;
1590 }
1591}
1592
1593STATIC void
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001594xfs_qm_dqfree_one(
1595 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001597 struct xfs_mount *mp = dqp->q_mount;
1598 struct xfs_quotainfo *qi = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Christoph Hellwig9f920f12012-03-13 08:52:35 +00001600 mutex_lock(&qi->qi_tree_lock);
Darrick J. Wong51dbb1b2020-07-14 10:37:32 -07001601 radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001602
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001603 qi->qi_dquots--;
Christoph Hellwigb84a3a92012-03-14 11:53:34 -05001604 mutex_unlock(&qi->qi_tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Christoph Hellwig92b2e5b2012-02-01 13:57:20 +00001606 xfs_qm_dqdestroy(dqp);
1607}
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +00001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609/* --------------- utility functions for vnodeops ---------------- */
1610
1611
1612/*
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +00001613 * Given an inode, a uid, gid and prid make sure that we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * allocated relevant dquot(s) on disk, and that we won't exceed inode
1615 * quotas by creating this file.
1616 * This also attaches dquot(s) to the given inode after locking it,
1617 * and returns the dquots corresponding to the uid and/or gid.
1618 *
1619 * in : inode (unlocked)
1620 * out : udquot, gdquot with references taken and unlocked
1621 */
1622int
1623xfs_qm_vop_dqalloc(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001624 struct xfs_inode *ip,
Christoph Hellwig54295152020-02-21 08:31:27 -08001625 kuid_t uid,
1626 kgid_t gid,
Christoph Hellwig7d095252009-06-08 15:33:32 +02001627 prid_t prid,
1628 uint flags,
1629 struct xfs_dquot **O_udqpp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001630 struct xfs_dquot **O_gdqpp,
1631 struct xfs_dquot **O_pdqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001633 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig54295152020-02-21 08:31:27 -08001634 struct inode *inode = VFS_I(ip);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001635 struct user_namespace *user_ns = inode->i_sb->s_user_ns;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001636 struct xfs_dquot *uq = NULL;
1637 struct xfs_dquot *gq = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001638 struct xfs_dquot *pq = NULL;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001639 int error;
1640 uint lockflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001642 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
1644
1645 lockflags = XFS_ILOCK_EXCL;
1646 xfs_ilock(ip, lockflags);
1647
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001648 if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
Christoph Hellwig54295152020-02-21 08:31:27 -08001649 gid = inode->i_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /*
1652 * Attach the dquot(s) to this inode, doing a dquot allocation
1653 * if necessary. The dquot(s) will not be locked.
1654 */
1655 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wong4882c192018-05-04 15:30:22 -07001656 error = xfs_qm_dqattach_locked(ip, true);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001657 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 xfs_iunlock(ip, lockflags);
Jesper Juhl014c2542006-01-15 02:37:08 +01001659 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 }
1662
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001663 if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001664 ASSERT(O_udqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001665 if (!uid_eq(inode->i_uid, uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /*
1667 * What we need is the dquot that has this uid, and
1668 * if we send the inode to dqget, the uid of the inode
1669 * takes priority over what's sent in the uid argument.
1670 * We must unlock inode here before calling dqget if
1671 * we're not sending the inode, because otherwise
1672 * we'll deadlock by doing trans_reserve while
1673 * holding ilock.
1674 */
1675 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001676 error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001677 XFS_DQTYPE_USER, true, &uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001678 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001679 ASSERT(error != -ENOENT);
Jesper Juhl014c2542006-01-15 02:37:08 +01001680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 /*
1683 * Get the ilock in the right order.
1684 */
1685 xfs_dqunlock(uq);
1686 lockflags = XFS_ILOCK_SHARED;
1687 xfs_ilock(ip, lockflags);
1688 } else {
1689 /*
1690 * Take an extra reference, because we'll return
1691 * this to caller
1692 */
1693 ASSERT(ip->i_udquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001694 uq = xfs_qm_dqhold(ip->i_udquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001697 if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001698 ASSERT(O_gdqpp);
Christoph Hellwig54295152020-02-21 08:31:27 -08001699 if (!gid_eq(inode->i_gid, gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 xfs_iunlock(ip, lockflags);
Christoph Hellwigba8adad2020-02-21 08:31:27 -08001701 error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001702 XFS_DQTYPE_GROUP, true, &gq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001703 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001704 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001705 goto error_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 xfs_dqunlock(gq);
1708 lockflags = XFS_ILOCK_SHARED;
1709 xfs_ilock(ip, lockflags);
1710 } else {
1711 ASSERT(ip->i_gdquot);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001712 gq = xfs_qm_dqhold(ip->i_gdquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001714 }
1715 if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
Kaixu Xia97611f92020-10-06 17:50:14 -07001716 ASSERT(O_pdqpp);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001717 if (ip->i_projid != prid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001718 xfs_iunlock(ip, lockflags);
Kaixu Xia5aff6752020-09-16 14:31:55 -07001719 error = xfs_qm_dqget(mp, prid,
Darrick J. Wong8cd49012020-07-15 17:42:36 -07001720 XFS_DQTYPE_PROJ, true, &pq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001721 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001722 ASSERT(error != -ENOENT);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001723 goto error_rele;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001724 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001725 xfs_dqunlock(pq);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001726 lockflags = XFS_ILOCK_SHARED;
1727 xfs_ilock(ip, lockflags);
1728 } else {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001729 ASSERT(ip->i_pdquot);
1730 pq = xfs_qm_dqhold(ip->i_pdquot);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Kaixu Xiac1407352020-04-22 21:54:27 -07001733 trace_xfs_dquot_dqalloc(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 xfs_iunlock(ip, lockflags);
1736 if (O_udqpp)
1737 *O_udqpp = uq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001738 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 xfs_qm_dqrele(uq);
1740 if (O_gdqpp)
1741 *O_gdqpp = gq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001742 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 xfs_qm_dqrele(gq);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001744 if (O_pdqpp)
1745 *O_pdqpp = pq;
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001746 else
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001747 xfs_qm_dqrele(pq);
Jesper Juhl014c2542006-01-15 02:37:08 +01001748 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001749
1750error_rele:
Markus Elfringd2a5e3c2014-12-01 08:24:20 +11001751 xfs_qm_dqrele(gq);
1752 xfs_qm_dqrele(uq);
Chandra Seetharaman113a5682013-06-27 17:25:07 -05001753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756/*
1757 * Actually transfer ownership, and do dquot modifications.
1758 * These were already reserved.
1759 */
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001760struct xfs_dquot *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761xfs_qm_vop_chown(
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001762 struct xfs_trans *tp,
1763 struct xfs_inode *ip,
1764 struct xfs_dquot **IO_olddq,
1765 struct xfs_dquot *newdq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Pavel Reichlaefe69a2019-11-12 17:04:02 -08001767 struct xfs_dquot *prevdq;
Nathan Scott06d10dd2005-06-21 15:48:47 +10001768 uint bfield = XFS_IS_REALTIME_INODE(ip) ?
1769 XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
1770
Christoph Hellwig7d095252009-06-08 15:33:32 +02001771
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001772 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001773 ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* old dquot */
1776 prevdq = *IO_olddq;
1777 ASSERT(prevdq);
1778 ASSERT(prevdq != newdq);
1779
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001780 xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_nblocks));
Nathan Scott06d10dd2005-06-21 15:48:47 +10001781 xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* the sparkling new dquot */
Christoph Hellwig6e73a542021-03-29 11:11:40 -07001784 xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_nblocks);
Nathan Scott06d10dd2005-06-21 15:48:47 +10001785 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /*
Darrick J. Wong1aecf372021-01-29 19:06:10 -08001788 * Back when we made quota reservations for the chown, we reserved the
1789 * ondisk blocks + delalloc blocks with the new dquot. Now that we've
1790 * switched the dquots, decrease the new dquot's block reservation
1791 * (having already bumped up the real counter) so that we don't have
1792 * any reservation to give back when we commit.
1793 */
1794 xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_RES_BLKS,
1795 -ip->i_delayed_blks);
1796
1797 /*
1798 * Give the incore reservation for delalloc blocks back to the old
1799 * dquot. We don't normally handle delalloc quota reservations
1800 * transactionally, so just lock the dquot and subtract from the
1801 * reservation. Dirty the transaction because it's too late to turn
1802 * back now.
1803 */
1804 tp->t_flags |= XFS_TRANS_DIRTY;
1805 xfs_dqlock(prevdq);
1806 ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
1807 prevdq->q_blk.reserved -= ip->i_delayed_blks;
1808 xfs_dqunlock(prevdq);
1809
1810 /*
Christoph Hellwig78e55892011-12-06 21:58:22 +00001811 * Take an extra reference, because the inode is going to keep
1812 * this dquot pointer even after the trans_commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Christoph Hellwig78e55892011-12-06 21:58:22 +00001814 *IO_olddq = xfs_qm_dqhold(newdq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jesper Juhl014c2542006-01-15 02:37:08 +01001816 return prevdq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819int
1820xfs_qm_vop_rename_dqattach(
Christoph Hellwig7d095252009-06-08 15:33:32 +02001821 struct xfs_inode **i_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001823 struct xfs_mount *mp = i_tab[0]->i_mount;
1824 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001826 if (!XFS_IS_QUOTA_ON(mp))
Jesper Juhl014c2542006-01-15 02:37:08 +01001827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Christoph Hellwig7d095252009-06-08 15:33:32 +02001829 for (i = 0; (i < 4 && i_tab[i]); i++) {
1830 struct xfs_inode *ip = i_tab[i];
1831 int error;
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Watch out for duplicate entries in the table.
1835 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001836 if (i == 0 || ip != i_tab[i-1]) {
1837 if (XFS_NOT_DQATTACHED(mp, ip)) {
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001838 error = xfs_qm_dqattach(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001840 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 }
1843 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847void
Christoph Hellwig7d095252009-06-08 15:33:32 +02001848xfs_qm_vop_create_dqattach(
1849 struct xfs_trans *tp,
1850 struct xfs_inode *ip,
1851 struct xfs_dquot *udqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001852 struct xfs_dquot *gdqp,
1853 struct xfs_dquot *pdqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001855 struct xfs_mount *mp = tp->t_mountp;
1856
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001857 if (!XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return;
1859
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001860 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Jie Liu37eb9702013-11-26 21:38:54 +08001862 if (udqp && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ASSERT(ip->i_udquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001864 ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
Christoph Hellwig78e55892011-12-06 21:58:22 +00001865
1866 ip->i_udquot = xfs_qm_dqhold(udqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
1868 }
Jie Liu37eb9702013-11-26 21:38:54 +08001869 if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 ASSERT(ip->i_gdquot == NULL);
Darrick J. Wongc51df732020-07-14 10:37:30 -07001871 ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
Christoph Hellwig54295152020-02-21 08:31:27 -08001872
Christoph Hellwig78e55892011-12-06 21:58:22 +00001873 ip->i_gdquot = xfs_qm_dqhold(gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
1875 }
Jie Liu37eb9702013-11-26 21:38:54 +08001876 if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001877 ASSERT(ip->i_pdquot == NULL);
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001878 ASSERT(ip->i_projid == pdqp->q_id);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -05001879
1880 ip->i_pdquot = xfs_qm_dqhold(pdqp);
1881 xfs_trans_mod_dquot(tp, pdqp, XFS_TRANS_DQ_ICOUNT, 1);
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Darrick J. Wong108523b2021-08-06 11:05:40 -07001885/* Decide if this inode's dquot is near an enforcement boundary. */
1886bool
1887xfs_inode_near_dquot_enforcement(
1888 struct xfs_inode *ip,
1889 xfs_dqtype_t type)
1890{
1891 struct xfs_dquot *dqp;
1892 int64_t freesp;
1893
1894 /* We only care for quotas that are enabled and enforced. */
1895 dqp = xfs_inode_dquot(ip, type);
1896 if (!dqp || !xfs_dquot_is_enforced(dqp))
1897 return false;
1898
1899 if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
1900 xfs_dquot_res_over_limits(&dqp->q_rtb))
1901 return true;
1902
1903 /* For space on the data device, check the various thresholds. */
1904 if (!dqp->q_prealloc_hi_wmark)
1905 return false;
1906
1907 if (dqp->q_blk.reserved < dqp->q_prealloc_lo_wmark)
1908 return false;
1909
1910 if (dqp->q_blk.reserved >= dqp->q_prealloc_hi_wmark)
1911 return true;
1912
1913 freesp = dqp->q_prealloc_hi_wmark - dqp->q_blk.reserved;
1914 if (freesp < dqp->q_low_space[XFS_QLOWSP_5_PCNT])
1915 return true;
1916
1917 return false;
1918}