blob: 6a1aae799cf16df4e28eb86b0bee173a32267d6a [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-2003 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"
Darrick J. Wong5467b342019-06-28 19:25:35 -07008#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"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110014#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_buf_item.h"
17#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_qm.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100021static inline struct xfs_dq_logitem *DQUOT_ITEM(struct xfs_log_item *lip)
22{
23 return container_of(lip, struct xfs_dq_logitem, qli_item);
24}
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
27 * returns the number of iovecs needed to log the given dquot item.
28 */
Dave Chinner166d1362013-08-12 20:50:04 +100029STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070030xfs_qm_dquot_logitem_size(
Dave Chinner166d1362013-08-12 20:50:04 +100031 struct xfs_log_item *lip,
32 int *nvecs,
33 int *nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Dave Chinner166d1362013-08-12 20:50:04 +100035 *nvecs += 2;
36 *nbytes += sizeof(struct xfs_dq_logformat) +
37 sizeof(struct xfs_disk_dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40/*
41 * fills in the vector of log iovecs for the given dquot log item.
42 */
43STATIC void
44xfs_qm_dquot_logitem_format(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100045 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110046 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -070048 struct xfs_disk_dquot ddq;
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100049 struct xfs_dq_logitem *qlip = DQUOT_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110050 struct xfs_log_iovec *vecp = NULL;
Christoph Hellwigce8e9622013-12-13 11:34:07 +110051 struct xfs_dq_logformat *qlf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Hellwigce8e9622013-12-13 11:34:07 +110053 qlf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_QFORMAT);
54 qlf->qlf_type = XFS_LI_DQUOT;
55 qlf->qlf_size = 2;
Darrick J. Wongc51df732020-07-14 10:37:30 -070056 qlf->qlf_id = qlip->qli_dquot->q_id;
Christoph Hellwigce8e9622013-12-13 11:34:07 +110057 qlf->qlf_blkno = qlip->qli_dquot->q_blkno;
58 qlf->qlf_len = 1;
59 qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
60 xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110061
Darrick J. Wong0b0fa1d2020-07-14 10:37:22 -070062 xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
63
64 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110065 sizeof(struct xfs_disk_dquot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/*
69 * Increment the pin count of the given dquot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71STATIC void
72xfs_qm_dquot_logitem_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100073 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100075 struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Peter Leckied1de8022008-10-30 17:05:18 +110078 atomic_inc(&dqp->q_pincount);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/*
82 * Decrement the pin count of the given dquot, and wake up
83 * anyone in xfs_dqwait_unpin() if the count goes to 0. The
Peter Leckiebc3048e2008-10-30 17:05:04 +110084 * dquot must have been previously pinned with a call to
85 * xfs_qm_dquot_logitem_pin().
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087STATIC void
88xfs_qm_dquot_logitem_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100089 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +100090 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100092 struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Peter Leckiebc3048e2008-10-30 17:05:04 +110094 ASSERT(atomic_read(&dqp->q_pincount) > 0);
95 if (atomic_dec_and_test(&dqp->q_pincount))
96 wake_up(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * This is called to wait for the given dquot to be unpinned.
101 * Most of these pin/unpin routines are plagiarized from inode code.
102 */
103void
104xfs_qm_dqunpin_wait(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000105 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Peter Leckiebc3048e2008-10-30 17:05:04 +1100108 if (atomic_read(&dqp->q_pincount) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /*
112 * Give the log a push so we don't wait here too long.
113 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000114 xfs_log_force(dqp->q_mount, 0);
Peter Leckiebc3048e2008-10-30 17:05:04 +1100115 wait_event(dqp->q_pinwait, (atomic_read(&dqp->q_pincount) == 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000119xfs_qm_dquot_logitem_push(
120 struct xfs_log_item *lip,
Matthew Wilcox57e80952018-03-07 14:59:39 -0800121 struct list_head *buffer_list)
122 __releases(&lip->li_ailp->ail_lock)
123 __acquires(&lip->li_ailp->ail_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000125 struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
Carlos Maiolino373b0582017-11-28 08:54:10 -0800126 struct xfs_buf *bp = lip->li_buf;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000127 uint rval = XFS_ITEM_SUCCESS;
128 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Peter Leckiebc3048e2008-10-30 17:05:04 +1100130 if (atomic_read(&dqp->q_pincount) > 0)
Dave Chinnerd808f612010-02-02 10:13:42 +1100131 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Christoph Hellwig800b4842011-12-06 21:58:14 +0000133 if (!xfs_dqlock_nowait(dqp))
Dave Chinnerd808f612010-02-02 10:13:42 +1100134 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000136 /*
137 * Re-check the pincount now that we stabilized the value by
138 * taking the quota lock.
139 */
140 if (atomic_read(&dqp->q_pincount) > 0) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000141 rval = XFS_ITEM_PINNED;
142 goto out_unlock;
Christoph Hellwigfe7257f2012-04-23 15:58:37 +1000143 }
144
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000145 /*
146 * Someone else is already flushing the dquot. Nothing we can do
147 * here but wait for the flush to finish and remove the item from
148 * the AIL.
149 */
David Chinnere1f49cf2008-08-13 16:41:43 +1000150 if (!xfs_dqflock_nowait(dqp)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000151 rval = XFS_ITEM_FLUSHING;
152 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Matthew Wilcox57e80952018-03-07 14:59:39 -0800155 spin_unlock(&lip->li_ailp->ail_lock);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000156
157 error = xfs_qm_dqflush(dqp, &bp);
Darrick J. Wong609001b2018-05-04 15:30:20 -0700158 if (!error) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000159 if (!xfs_buf_delwri_queue(bp, buffer_list))
160 rval = XFS_ITEM_FLUSHING;
161 xfs_buf_relse(bp);
Brian Foster8d3d7e22020-03-27 08:29:45 -0700162 } else if (error == -EAGAIN)
163 rval = XFS_ITEM_LOCKED;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000164
Matthew Wilcox57e80952018-03-07 14:59:39 -0800165 spin_lock(&lip->li_ailp->ail_lock);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000166out_unlock:
167 xfs_dqunlock(dqp);
168 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700172xfs_qm_dquot_logitem_release(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000173 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000175 struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ASSERT(XFS_DQ_IS_LOCKED(dqp));
178
179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * dquots are never 'held' from getting unlocked at the end of
181 * a transaction. Their locking and unlocking is hidden inside the
182 * transaction layer, within trans_commit. Hence, no LI_HOLD flag
183 * for the logitem.
184 */
185 xfs_dqunlock(dqp);
186}
187
Christoph Hellwigddf92052019-06-28 19:27:32 -0700188STATIC void
189xfs_qm_dquot_logitem_committing(
190 struct xfs_log_item *lip,
Dave Chinner5f9b4b02021-06-18 08:21:52 -0700191 xfs_csn_t seq)
Christoph Hellwigddf92052019-06-28 19:27:32 -0700192{
193 return xfs_qm_dquot_logitem_release(lip);
194}
195
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000196static const struct xfs_item_ops xfs_dquot_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000197 .iop_size = xfs_qm_dquot_logitem_size,
198 .iop_format = xfs_qm_dquot_logitem_format,
199 .iop_pin = xfs_qm_dquot_logitem_pin,
200 .iop_unpin = xfs_qm_dquot_logitem_unpin,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700201 .iop_release = xfs_qm_dquot_logitem_release,
202 .iop_committing = xfs_qm_dquot_logitem_committing,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000203 .iop_push = xfs_qm_dquot_logitem_push,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206/*
207 * Initialize the dquot log item for a newly allocated dquot.
208 * The dquot isn't locked at this point, but it isn't on any of the lists
209 * either, so we don't care.
210 */
211void
212xfs_qm_dquot_logitem_init(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000213 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000215 struct xfs_dq_logitem *lp = &dqp->q_logitem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dave Chinner43f5efc2010-03-23 10:10:00 +1100217 xfs_log_item_init(dqp->q_mount, &lp->qli_item, XFS_LI_DQUOT,
218 &xfs_dquot_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 lp->qli_dquot = dqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}