Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2003 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 9 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 14 | #include "xfs_quota.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_buf_item.h" |
| 17 | #include "xfs_trans_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs_qm.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 19 | #include "xfs_log.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 21 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* |
| 27 | * returns the number of iovecs needed to log the given dquot item. |
| 28 | */ |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 29 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | xfs_qm_dquot_logitem_size( |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 31 | struct xfs_log_item *lip, |
| 32 | int *nvecs, |
| 33 | int *nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Dave Chinner | 166d136 | 2013-08-12 20:50:04 +1000 | [diff] [blame] | 35 | *nvecs += 2; |
| 36 | *nbytes += sizeof(struct xfs_dq_logformat) + |
| 37 | sizeof(struct xfs_disk_dquot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* |
| 41 | * fills in the vector of log iovecs for the given dquot log item. |
| 42 | */ |
| 43 | STATIC void |
| 44 | xfs_qm_dquot_logitem_format( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 45 | struct xfs_log_item *lip, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 46 | struct xfs_log_vec *lv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Darrick J. Wong | 0b0fa1d | 2020-07-14 10:37:22 -0700 | [diff] [blame] | 48 | struct xfs_disk_dquot ddq; |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 49 | struct xfs_dq_logitem *qlip = DQUOT_ITEM(lip); |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 50 | struct xfs_log_iovec *vecp = NULL; |
Christoph Hellwig | ce8e962 | 2013-12-13 11:34:07 +1100 | [diff] [blame] | 51 | struct xfs_dq_logformat *qlf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Christoph Hellwig | ce8e962 | 2013-12-13 11:34:07 +1100 | [diff] [blame] | 53 | qlf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_QFORMAT); |
| 54 | qlf->qlf_type = XFS_LI_DQUOT; |
| 55 | qlf->qlf_size = 2; |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 56 | qlf->qlf_id = qlip->qli_dquot->q_id; |
Christoph Hellwig | ce8e962 | 2013-12-13 11:34:07 +1100 | [diff] [blame] | 57 | 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 Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 61 | |
Darrick J. Wong | 0b0fa1d | 2020-07-14 10:37:22 -0700 | [diff] [blame] | 62 | xfs_dquot_to_disk(&ddq, qlip->qli_dquot); |
| 63 | |
| 64 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq, |
Christoph Hellwig | bde7cff | 2013-12-13 11:34:02 +1100 | [diff] [blame] | 65 | sizeof(struct xfs_disk_dquot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Increment the pin count of the given dquot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
| 71 | STATIC void |
| 72 | xfs_qm_dquot_logitem_pin( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 73 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 75 | struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
Peter Leckie | d1de802 | 2008-10-30 17:05:18 +1100 | [diff] [blame] | 78 | atomic_inc(&dqp->q_pincount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 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 Leckie | bc3048e | 2008-10-30 17:05:04 +1100 | [diff] [blame] | 84 | * dquot must have been previously pinned with a call to |
| 85 | * xfs_qm_dquot_logitem_pin(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | STATIC void |
| 88 | xfs_qm_dquot_logitem_unpin( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 89 | struct xfs_log_item *lip, |
Christoph Hellwig | 9412e31 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 90 | int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 92 | struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Peter Leckie | bc3048e | 2008-10-30 17:05:04 +1100 | [diff] [blame] | 94 | ASSERT(atomic_read(&dqp->q_pincount) > 0); |
| 95 | if (atomic_dec_and_test(&dqp->q_pincount)) |
| 96 | wake_up(&dqp->q_pinwait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* |
| 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 | */ |
| 103 | void |
| 104 | xfs_qm_dqunpin_wait( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 105 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
Peter Leckie | bc3048e | 2008-10-30 17:05:04 +1100 | [diff] [blame] | 108 | if (atomic_read(&dqp->q_pincount) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Give the log a push so we don't wait here too long. |
| 113 | */ |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 114 | xfs_log_force(dqp->q_mount, 0); |
Peter Leckie | bc3048e | 2008-10-30 17:05:04 +1100 | [diff] [blame] | 115 | wait_event(dqp->q_pinwait, (atomic_read(&dqp->q_pincount) == 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | STATIC uint |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 119 | xfs_qm_dquot_logitem_push( |
| 120 | struct xfs_log_item *lip, |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 121 | struct list_head *buffer_list) |
| 122 | __releases(&lip->li_ailp->ail_lock) |
| 123 | __acquires(&lip->li_ailp->ail_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 125 | struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot; |
Carlos Maiolino | 373b058 | 2017-11-28 08:54:10 -0800 | [diff] [blame] | 126 | struct xfs_buf *bp = lip->li_buf; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 127 | uint rval = XFS_ITEM_SUCCESS; |
| 128 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Peter Leckie | bc3048e | 2008-10-30 17:05:04 +1100 | [diff] [blame] | 130 | if (atomic_read(&dqp->q_pincount) > 0) |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 131 | return XFS_ITEM_PINNED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Christoph Hellwig | 800b484 | 2011-12-06 21:58:14 +0000 | [diff] [blame] | 133 | if (!xfs_dqlock_nowait(dqp)) |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 134 | return XFS_ITEM_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 136 | /* |
| 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 Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 141 | rval = XFS_ITEM_PINNED; |
| 142 | goto out_unlock; |
Christoph Hellwig | fe7257f | 2012-04-23 15:58:37 +1000 | [diff] [blame] | 143 | } |
| 144 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 145 | /* |
| 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 Chinner | e1f49cf | 2008-08-13 16:41:43 +1000 | [diff] [blame] | 150 | if (!xfs_dqflock_nowait(dqp)) { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 151 | rval = XFS_ITEM_FLUSHING; |
| 152 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 155 | spin_unlock(&lip->li_ailp->ail_lock); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 156 | |
| 157 | error = xfs_qm_dqflush(dqp, &bp); |
Darrick J. Wong | 609001b | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 158 | if (!error) { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 159 | if (!xfs_buf_delwri_queue(bp, buffer_list)) |
| 160 | rval = XFS_ITEM_FLUSHING; |
| 161 | xfs_buf_relse(bp); |
Brian Foster | 8d3d7e2 | 2020-03-27 08:29:45 -0700 | [diff] [blame] | 162 | } else if (error == -EAGAIN) |
| 163 | rval = XFS_ITEM_LOCKED; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 164 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 165 | spin_lock(&lip->li_ailp->ail_lock); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 166 | out_unlock: |
| 167 | xfs_dqunlock(dqp); |
| 168 | return rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | STATIC void |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 172 | xfs_qm_dquot_logitem_release( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 173 | struct xfs_log_item *lip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 175 | struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | ASSERT(XFS_DQ_IS_LOCKED(dqp)); |
| 178 | |
| 179 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * 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 Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 188 | STATIC void |
| 189 | xfs_qm_dquot_logitem_committing( |
| 190 | struct xfs_log_item *lip, |
Dave Chinner | 5f9b4b0 | 2021-06-18 08:21:52 -0700 | [diff] [blame] | 191 | xfs_csn_t seq) |
Christoph Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 192 | { |
| 193 | return xfs_qm_dquot_logitem_release(lip); |
| 194 | } |
| 195 | |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 196 | static const struct xfs_item_ops xfs_dquot_item_ops = { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 197 | .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 Hellwig | ddf9205 | 2019-06-28 19:27:32 -0700 | [diff] [blame] | 201 | .iop_release = xfs_qm_dquot_logitem_release, |
| 202 | .iop_committing = xfs_qm_dquot_logitem_committing, |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 203 | .iop_push = xfs_qm_dquot_logitem_push, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }; |
| 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 | */ |
| 211 | void |
| 212 | xfs_qm_dquot_logitem_init( |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 213 | struct xfs_dquot *dqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Christoph Hellwig | 7bfa31d | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 215 | struct xfs_dq_logitem *lp = &dqp->q_logitem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 217 | xfs_log_item_init(dqp->q_mount, &lp->qli_item, XFS_LI_DQUOT, |
| 218 | &xfs_dquot_item_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | lp->qli_dquot = dqp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |