xref: /linux/fs/xfs/xfs_dquot_item.c (revision 7a08cb9b4bb92fb86f5fe8a3aa0ac08a9b3d783b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_quota.h"
15 #include "xfs_trans.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_qm.h"
19 #include "xfs_log.h"
20 #include "xfs_error.h"
21 
22 static inline struct xfs_dq_logitem *DQUOT_ITEM(struct xfs_log_item *lip)
23 {
24 	return container_of(lip, struct xfs_dq_logitem, qli_item);
25 }
26 
27 /*
28  * returns the number of iovecs needed to log the given dquot item.
29  */
30 STATIC void
31 xfs_qm_dquot_logitem_size(
32 	struct xfs_log_item	*lip,
33 	int			*nvecs,
34 	int			*nbytes)
35 {
36 	*nvecs += 2;
37 	*nbytes += sizeof(struct xfs_dq_logformat) +
38 		   sizeof(struct xfs_disk_dquot);
39 }
40 
41 /*
42  * fills in the vector of log iovecs for the given dquot log item.
43  */
44 STATIC void
45 xfs_qm_dquot_logitem_format(
46 	struct xfs_log_item	*lip,
47 	struct xfs_log_vec	*lv)
48 {
49 	struct xfs_disk_dquot	ddq;
50 	struct xfs_dq_logitem	*qlip = DQUOT_ITEM(lip);
51 	struct xfs_log_iovec	*vecp = NULL;
52 	struct xfs_dq_logformat	*qlf;
53 
54 	qlf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_QFORMAT);
55 	qlf->qlf_type = XFS_LI_DQUOT;
56 	qlf->qlf_size = 2;
57 	qlf->qlf_id = qlip->qli_dquot->q_id;
58 	qlf->qlf_blkno = qlip->qli_dquot->q_blkno;
59 	qlf->qlf_len = 1;
60 	qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
61 	xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
62 
63 	xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
64 
65 	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
66 			sizeof(struct xfs_disk_dquot));
67 }
68 
69 /*
70  * Increment the pin count of the given dquot.
71  */
72 STATIC void
73 xfs_qm_dquot_logitem_pin(
74 	struct xfs_log_item	*lip)
75 {
76 	struct xfs_dquot	*dqp = DQUOT_ITEM(lip)->qli_dquot;
77 
78 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
79 	atomic_inc(&dqp->q_pincount);
80 }
81 
82 /*
83  * Decrement the pin count of the given dquot, and wake up
84  * anyone in xfs_dqwait_unpin() if the count goes to 0.	 The
85  * dquot must have been previously pinned with a call to
86  * xfs_qm_dquot_logitem_pin().
87  */
88 STATIC void
89 xfs_qm_dquot_logitem_unpin(
90 	struct xfs_log_item	*lip,
91 	int			remove)
92 {
93 	struct xfs_dquot	*dqp = DQUOT_ITEM(lip)->qli_dquot;
94 
95 	ASSERT(atomic_read(&dqp->q_pincount) > 0);
96 	if (atomic_dec_and_test(&dqp->q_pincount))
97 		wake_up(&dqp->q_pinwait);
98 }
99 
100 /*
101  * This is called to wait for the given dquot to be unpinned.
102  * Most of these pin/unpin routines are plagiarized from inode code.
103  */
104 void
105 xfs_qm_dqunpin_wait(
106 	struct xfs_dquot	*dqp)
107 {
108 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
109 	if (atomic_read(&dqp->q_pincount) == 0)
110 		return;
111 
112 	/*
113 	 * Give the log a push so we don't wait here too long.
114 	 */
115 	xfs_log_force(dqp->q_mount, 0);
116 	wait_event(dqp->q_pinwait, (atomic_read(&dqp->q_pincount) == 0));
117 }
118 
119 STATIC uint
120 xfs_qm_dquot_logitem_push(
121 	struct xfs_log_item	*lip,
122 	struct list_head	*buffer_list)
123 		__releases(&lip->li_ailp->ail_lock)
124 		__acquires(&lip->li_ailp->ail_lock)
125 {
126 	struct xfs_dquot	*dqp = DQUOT_ITEM(lip)->qli_dquot;
127 	struct xfs_buf		*bp = lip->li_buf;
128 	uint			rval = XFS_ITEM_SUCCESS;
129 	int			error;
130 
131 	if (atomic_read(&dqp->q_pincount) > 0)
132 		return XFS_ITEM_PINNED;
133 
134 	if (!xfs_dqlock_nowait(dqp))
135 		return XFS_ITEM_LOCKED;
136 
137 	/*
138 	 * Re-check the pincount now that we stabilized the value by
139 	 * taking the quota lock.
140 	 */
141 	if (atomic_read(&dqp->q_pincount) > 0) {
142 		rval = XFS_ITEM_PINNED;
143 		goto out_unlock;
144 	}
145 
146 	/*
147 	 * Someone else is already flushing the dquot.  Nothing we can do
148 	 * here but wait for the flush to finish and remove the item from
149 	 * the AIL.
150 	 */
151 	if (!xfs_dqflock_nowait(dqp)) {
152 		rval = XFS_ITEM_FLUSHING;
153 		goto out_unlock;
154 	}
155 
156 	spin_unlock(&lip->li_ailp->ail_lock);
157 
158 	error = xfs_qm_dqflush(dqp, &bp);
159 	if (!error) {
160 		if (!xfs_buf_delwri_queue(bp, buffer_list))
161 			rval = XFS_ITEM_FLUSHING;
162 		xfs_buf_relse(bp);
163 	} else if (error == -EAGAIN)
164 		rval = XFS_ITEM_LOCKED;
165 
166 	spin_lock(&lip->li_ailp->ail_lock);
167 out_unlock:
168 	xfs_dqunlock(dqp);
169 	return rval;
170 }
171 
172 STATIC void
173 xfs_qm_dquot_logitem_release(
174 	struct xfs_log_item	*lip)
175 {
176 	struct xfs_dquot	*dqp = DQUOT_ITEM(lip)->qli_dquot;
177 
178 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
179 
180 	/*
181 	 * dquots are never 'held' from getting unlocked at the end of
182 	 * a transaction.  Their locking and unlocking is hidden inside the
183 	 * transaction layer, within trans_commit. Hence, no LI_HOLD flag
184 	 * for the logitem.
185 	 */
186 	xfs_dqunlock(dqp);
187 }
188 
189 STATIC void
190 xfs_qm_dquot_logitem_committing(
191 	struct xfs_log_item	*lip,
192 	xfs_csn_t		seq)
193 {
194 	return xfs_qm_dquot_logitem_release(lip);
195 }
196 
197 #ifdef DEBUG_EXPENSIVE
198 static int
199 xfs_qm_dquot_logitem_precommit(
200 	struct xfs_trans	*tp,
201 	struct xfs_log_item	*lip)
202 {
203 	struct xfs_dquot	*dqp = DQUOT_ITEM(lip)->qli_dquot;
204 	struct xfs_mount	*mp = dqp->q_mount;
205 	struct xfs_disk_dquot	ddq = { };
206 	xfs_failaddr_t		fa;
207 
208 	xfs_dquot_to_disk(&ddq, dqp);
209 	fa = xfs_dquot_verify(mp, &ddq, dqp->q_id);
210 	if (fa) {
211 		XFS_CORRUPTION_ERROR("Bad dquot during logging",
212 				XFS_ERRLEVEL_LOW, mp, &ddq, sizeof(ddq));
213 		xfs_alert(mp,
214  "Metadata corruption detected at %pS, dquot 0x%x",
215 				fa, dqp->q_id);
216 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
217 		ASSERT(fa == NULL);
218 	}
219 
220 	return 0;
221 }
222 #else
223 # define xfs_qm_dquot_logitem_precommit	NULL
224 #endif
225 
226 static const struct xfs_item_ops xfs_dquot_item_ops = {
227 	.iop_size	= xfs_qm_dquot_logitem_size,
228 	.iop_precommit	= xfs_qm_dquot_logitem_precommit,
229 	.iop_format	= xfs_qm_dquot_logitem_format,
230 	.iop_pin	= xfs_qm_dquot_logitem_pin,
231 	.iop_unpin	= xfs_qm_dquot_logitem_unpin,
232 	.iop_release	= xfs_qm_dquot_logitem_release,
233 	.iop_committing	= xfs_qm_dquot_logitem_committing,
234 	.iop_push	= xfs_qm_dquot_logitem_push,
235 };
236 
237 /*
238  * Initialize the dquot log item for a newly allocated dquot.
239  * The dquot isn't locked at this point, but it isn't on any of the lists
240  * either, so we don't care.
241  */
242 void
243 xfs_qm_dquot_logitem_init(
244 	struct xfs_dquot	*dqp)
245 {
246 	struct xfs_dq_logitem	*lp = &dqp->q_logitem;
247 
248 	xfs_log_item_init(dqp->q_mount, &lp->qli_item, XFS_LI_DQUOT,
249 					&xfs_dquot_item_ops);
250 	lp->qli_dquot = dqp;
251 }
252