xref: /openbmc/linux/fs/xfs/xfs_buf_item.c (revision 55e43d6abd078ed6d219902ce8cb4d68e3c993ba)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
37b718769SNathan Scott  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include "xfs.h"
7a844f451SNathan Scott #include "xfs_fs.h"
85467b34bSDarrick J. Wong #include "xfs_shared.h"
94fb6e8adSChristoph Hellwig #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12a844f451SNathan Scott #include "xfs_bit.h"
131da177e4SLinus Torvalds #include "xfs_mount.h"
14239880efSDave Chinner #include "xfs_trans.h"
153536b61eSDave Chinner #include "xfs_trans_priv.h"
16a844f451SNathan Scott #include "xfs_buf_item.h"
17aac855abSDave Chinner #include "xfs_inode.h"
18aac855abSDave Chinner #include "xfs_inode_item.h"
196f5de180SDave Chinner #include "xfs_quota.h"
206f5de180SDave Chinner #include "xfs_dquot_item.h"
216f5de180SDave Chinner #include "xfs_dquot.h"
220b1b213fSChristoph Hellwig #include "xfs_trace.h"
23239880efSDave Chinner #include "xfs_log.h"
24d86142ddSDave Chinner #include "xfs_log_priv.h"
25*c08d0399SDarrick J. Wong #include "xfs_error.h"
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds 
28182696fbSDarrick J. Wong struct kmem_cache	*xfs_buf_item_cache;
291da177e4SLinus Torvalds 
BUF_ITEM(struct xfs_log_item * lip)307bfa31d8SChristoph Hellwig static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
317bfa31d8SChristoph Hellwig {
327bfa31d8SChristoph Hellwig 	return container_of(lip, struct xfs_buf_log_item, bli_item);
337bfa31d8SChristoph Hellwig }
347bfa31d8SChristoph Hellwig 
358a6453a8SDarrick J. Wong /* Is this log iovec plausibly large enough to contain the buffer log format? */
368a6453a8SDarrick J. Wong bool
xfs_buf_log_check_iovec(struct xfs_log_iovec * iovec)378a6453a8SDarrick J. Wong xfs_buf_log_check_iovec(
388a6453a8SDarrick J. Wong 	struct xfs_log_iovec		*iovec)
398a6453a8SDarrick J. Wong {
408a6453a8SDarrick J. Wong 	struct xfs_buf_log_format	*blfp = iovec->i_addr;
418a6453a8SDarrick J. Wong 	char				*bmp_end;
428a6453a8SDarrick J. Wong 	char				*item_end;
438a6453a8SDarrick J. Wong 
448a6453a8SDarrick J. Wong 	if (offsetof(struct xfs_buf_log_format, blf_data_map) > iovec->i_len)
458a6453a8SDarrick J. Wong 		return false;
468a6453a8SDarrick J. Wong 
478a6453a8SDarrick J. Wong 	item_end = (char *)iovec->i_addr + iovec->i_len;
488a6453a8SDarrick J. Wong 	bmp_end = (char *)&blfp->blf_data_map[blfp->blf_map_size];
498a6453a8SDarrick J. Wong 	return bmp_end <= item_end;
508a6453a8SDarrick J. Wong }
518a6453a8SDarrick J. Wong 
52166d1368SDave Chinner static inline int
xfs_buf_log_format_size(struct xfs_buf_log_format * blfp)53166d1368SDave Chinner xfs_buf_log_format_size(
54166d1368SDave Chinner 	struct xfs_buf_log_format *blfp)
55166d1368SDave Chinner {
56166d1368SDave Chinner 	return offsetof(struct xfs_buf_log_format, blf_data_map) +
57166d1368SDave Chinner 			(blfp->blf_map_size * sizeof(blfp->blf_data_map[0]));
58166d1368SDave Chinner }
59166d1368SDave Chinner 
60c81ea11eSDave Chinner static inline bool
xfs_buf_item_straddle(struct xfs_buf * bp,uint offset,int first_bit,int nbits)61c81ea11eSDave Chinner xfs_buf_item_straddle(
62c81ea11eSDave Chinner 	struct xfs_buf		*bp,
63c81ea11eSDave Chinner 	uint			offset,
64929f8b0dSDave Chinner 	int			first_bit,
65929f8b0dSDave Chinner 	int			nbits)
66c81ea11eSDave Chinner {
67929f8b0dSDave Chinner 	void			*first, *last;
68929f8b0dSDave Chinner 
69929f8b0dSDave Chinner 	first = xfs_buf_offset(bp, offset + (first_bit << XFS_BLF_SHIFT));
70929f8b0dSDave Chinner 	last = xfs_buf_offset(bp,
71929f8b0dSDave Chinner 			offset + ((first_bit + nbits) << XFS_BLF_SHIFT));
72929f8b0dSDave Chinner 
73929f8b0dSDave Chinner 	if (last - first != nbits * XFS_BLF_CHUNK)
74929f8b0dSDave Chinner 		return true;
75929f8b0dSDave Chinner 	return false;
76c81ea11eSDave Chinner }
77c81ea11eSDave Chinner 
781da177e4SLinus Torvalds /*
7919f4e7ccSDave Chinner  * Return the number of log iovecs and space needed to log the given buf log
8019f4e7ccSDave Chinner  * item segment.
811da177e4SLinus Torvalds  *
8219f4e7ccSDave Chinner  * It calculates this as 1 iovec for the buf log format structure and 1 for each
8319f4e7ccSDave Chinner  * stretch of non-contiguous chunks to be logged.  Contiguous chunks are logged
8419f4e7ccSDave Chinner  * in a single iovec.
851da177e4SLinus Torvalds  */
86166d1368SDave Chinner STATIC void
xfs_buf_item_size_segment(struct xfs_buf_log_item * bip,struct xfs_buf_log_format * blfp,uint offset,int * nvecs,int * nbytes)87372cc85eSDave Chinner xfs_buf_item_size_segment(
88372cc85eSDave Chinner 	struct xfs_buf_log_item		*bip,
89166d1368SDave Chinner 	struct xfs_buf_log_format	*blfp,
90c81ea11eSDave Chinner 	uint				offset,
91166d1368SDave Chinner 	int				*nvecs,
92166d1368SDave Chinner 	int				*nbytes)
931da177e4SLinus Torvalds {
947bfa31d8SChristoph Hellwig 	struct xfs_buf			*bp = bip->bli_buf;
95929f8b0dSDave Chinner 	int				first_bit;
96929f8b0dSDave Chinner 	int				nbits;
971da177e4SLinus Torvalds 	int				next_bit;
981da177e4SLinus Torvalds 	int				last_bit;
991da177e4SLinus Torvalds 
100929f8b0dSDave Chinner 	first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
101929f8b0dSDave Chinner 	if (first_bit == -1)
102166d1368SDave Chinner 		return;
1031da177e4SLinus Torvalds 
104929f8b0dSDave Chinner 	(*nvecs)++;
105929f8b0dSDave Chinner 	*nbytes += xfs_buf_log_format_size(blfp);
106372cc85eSDave Chinner 
107929f8b0dSDave Chinner 	do {
108929f8b0dSDave Chinner 		nbits = xfs_contig_bits(blfp->blf_data_map,
109929f8b0dSDave Chinner 					blfp->blf_map_size, first_bit);
110929f8b0dSDave Chinner 		ASSERT(nbits > 0);
111929f8b0dSDave Chinner 
112929f8b0dSDave Chinner 		/*
113929f8b0dSDave Chinner 		 * Straddling a page is rare because we don't log contiguous
114929f8b0dSDave Chinner 		 * chunks of unmapped buffers anywhere.
115929f8b0dSDave Chinner 		 */
116929f8b0dSDave Chinner 		if (nbits > 1 &&
117929f8b0dSDave Chinner 		    xfs_buf_item_straddle(bp, offset, first_bit, nbits))
118929f8b0dSDave Chinner 			goto slow_scan;
119929f8b0dSDave Chinner 
120929f8b0dSDave Chinner 		(*nvecs)++;
121929f8b0dSDave Chinner 		*nbytes += nbits * XFS_BLF_CHUNK;
122929f8b0dSDave Chinner 
123929f8b0dSDave Chinner 		/*
124929f8b0dSDave Chinner 		 * This takes the bit number to start looking from and
125929f8b0dSDave Chinner 		 * returns the next set bit from there.  It returns -1
126929f8b0dSDave Chinner 		 * if there are no more bits set or the start bit is
127929f8b0dSDave Chinner 		 * beyond the end of the bitmap.
128929f8b0dSDave Chinner 		 */
129929f8b0dSDave Chinner 		first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
130929f8b0dSDave Chinner 					(uint)first_bit + nbits + 1);
131929f8b0dSDave Chinner 	} while (first_bit != -1);
132929f8b0dSDave Chinner 
133929f8b0dSDave Chinner 	return;
134929f8b0dSDave Chinner 
135929f8b0dSDave Chinner slow_scan:
136929f8b0dSDave Chinner 	/* Count the first bit we jumped out of the above loop from */
137929f8b0dSDave Chinner 	(*nvecs)++;
138929f8b0dSDave Chinner 	*nbytes += XFS_BLF_CHUNK;
139929f8b0dSDave Chinner 	last_bit = first_bit;
1401da177e4SLinus Torvalds 	while (last_bit != -1) {
1411da177e4SLinus Torvalds 		/*
1421da177e4SLinus Torvalds 		 * This takes the bit number to start looking from and
1431da177e4SLinus Torvalds 		 * returns the next set bit from there.  It returns -1
1441da177e4SLinus Torvalds 		 * if there are no more bits set or the start bit is
1451da177e4SLinus Torvalds 		 * beyond the end of the bitmap.
1461da177e4SLinus Torvalds 		 */
147372cc85eSDave Chinner 		next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
1481da177e4SLinus Torvalds 					last_bit + 1);
1491da177e4SLinus Torvalds 		/*
1501da177e4SLinus Torvalds 		 * If we run out of bits, leave the loop,
1511da177e4SLinus Torvalds 		 * else if we find a new set of bits bump the number of vecs,
1521da177e4SLinus Torvalds 		 * else keep scanning the current set of bits.
1531da177e4SLinus Torvalds 		 */
1541da177e4SLinus Torvalds 		if (next_bit == -1) {
155372cc85eSDave Chinner 			break;
156c81ea11eSDave Chinner 		} else if (next_bit != last_bit + 1 ||
157929f8b0dSDave Chinner 		           xfs_buf_item_straddle(bp, offset, first_bit, nbits)) {
1581da177e4SLinus Torvalds 			last_bit = next_bit;
159929f8b0dSDave Chinner 			first_bit = next_bit;
160166d1368SDave Chinner 			(*nvecs)++;
161929f8b0dSDave Chinner 			nbits = 1;
1621da177e4SLinus Torvalds 		} else {
1631da177e4SLinus Torvalds 			last_bit++;
164929f8b0dSDave Chinner 			nbits++;
1651da177e4SLinus Torvalds 		}
166166d1368SDave Chinner 		*nbytes += XFS_BLF_CHUNK;
1671da177e4SLinus Torvalds 	}
168372cc85eSDave Chinner }
169372cc85eSDave Chinner 
170372cc85eSDave Chinner /*
17119f4e7ccSDave Chinner  * Return the number of log iovecs and space needed to log the given buf log
17219f4e7ccSDave Chinner  * item.
173372cc85eSDave Chinner  *
174b63da6c8SRandy Dunlap  * Discontiguous buffers need a format structure per region that is being
175372cc85eSDave Chinner  * logged. This makes the changes in the buffer appear to log recovery as though
176372cc85eSDave Chinner  * they came from separate buffers, just like would occur if multiple buffers
177372cc85eSDave Chinner  * were used instead of a single discontiguous buffer. This enables
178372cc85eSDave Chinner  * discontiguous buffers to be in-memory constructs, completely transparent to
179372cc85eSDave Chinner  * what ends up on disk.
180372cc85eSDave Chinner  *
181372cc85eSDave Chinner  * If the XFS_BLI_STALE flag has been set, then log nothing but the buf log
18219f4e7ccSDave Chinner  * format structures. If the item has previously been logged and has dirty
18319f4e7ccSDave Chinner  * regions, we do not relog them in stale buffers. This has the effect of
18419f4e7ccSDave Chinner  * reducing the size of the relogged item by the amount of dirty data tracked
18519f4e7ccSDave Chinner  * by the log item. This can result in the committing transaction reducing the
18619f4e7ccSDave Chinner  * amount of space being consumed by the CIL.
187372cc85eSDave Chinner  */
188166d1368SDave Chinner STATIC void
xfs_buf_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)189372cc85eSDave Chinner xfs_buf_item_size(
190166d1368SDave Chinner 	struct xfs_log_item	*lip,
191166d1368SDave Chinner 	int			*nvecs,
192166d1368SDave Chinner 	int			*nbytes)
193372cc85eSDave Chinner {
194372cc85eSDave Chinner 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
195c81ea11eSDave Chinner 	struct xfs_buf		*bp = bip->bli_buf;
196372cc85eSDave Chinner 	int			i;
197accc661bSDave Chinner 	int			bytes;
198c81ea11eSDave Chinner 	uint			offset = 0;
199372cc85eSDave Chinner 
200372cc85eSDave Chinner 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
201372cc85eSDave Chinner 	if (bip->bli_flags & XFS_BLI_STALE) {
202372cc85eSDave Chinner 		/*
20319f4e7ccSDave Chinner 		 * The buffer is stale, so all we need to log is the buf log
20419f4e7ccSDave Chinner 		 * format structure with the cancel flag in it as we are never
20519f4e7ccSDave Chinner 		 * going to replay the changes tracked in the log item.
206372cc85eSDave Chinner 		 */
207372cc85eSDave Chinner 		trace_xfs_buf_item_size_stale(bip);
208b9438173SMark Tinguely 		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
209166d1368SDave Chinner 		*nvecs += bip->bli_format_count;
210166d1368SDave Chinner 		for (i = 0; i < bip->bli_format_count; i++) {
211166d1368SDave Chinner 			*nbytes += xfs_buf_log_format_size(&bip->bli_formats[i]);
212166d1368SDave Chinner 		}
213166d1368SDave Chinner 		return;
214372cc85eSDave Chinner 	}
215372cc85eSDave Chinner 
216372cc85eSDave Chinner 	ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
217372cc85eSDave Chinner 
2185f6bed76SDave Chinner 	if (bip->bli_flags & XFS_BLI_ORDERED) {
2195f6bed76SDave Chinner 		/*
22019f4e7ccSDave Chinner 		 * The buffer has been logged just to order it. It is not being
22119f4e7ccSDave Chinner 		 * included in the transaction commit, so no vectors are used at
22219f4e7ccSDave Chinner 		 * all.
2235f6bed76SDave Chinner 		 */
2245f6bed76SDave Chinner 		trace_xfs_buf_item_size_ordered(bip);
225166d1368SDave Chinner 		*nvecs = XFS_LOG_VEC_ORDERED;
226166d1368SDave Chinner 		return;
2275f6bed76SDave Chinner 	}
2285f6bed76SDave Chinner 
229372cc85eSDave Chinner 	/*
230accc661bSDave Chinner 	 * The vector count is based on the number of buffer vectors we have
231372cc85eSDave Chinner 	 * dirty bits in. This will only be greater than one when we have a
232372cc85eSDave Chinner 	 * compound buffer with more than one segment dirty. Hence for compound
233372cc85eSDave Chinner 	 * buffers we need to track which segment the dirty bits correspond to,
234372cc85eSDave Chinner 	 * and when we move from one segment to the next increment the vector
235372cc85eSDave Chinner 	 * count for the extra buf log format structure that will need to be
236372cc85eSDave Chinner 	 * written.
237372cc85eSDave Chinner 	 */
238accc661bSDave Chinner 	bytes = 0;
239372cc85eSDave Chinner 	for (i = 0; i < bip->bli_format_count; i++) {
240c81ea11eSDave Chinner 		xfs_buf_item_size_segment(bip, &bip->bli_formats[i], offset,
241accc661bSDave Chinner 					  nvecs, &bytes);
242c81ea11eSDave Chinner 		offset += BBTOB(bp->b_maps[i].bm_len);
243372cc85eSDave Chinner 	}
244accc661bSDave Chinner 
245accc661bSDave Chinner 	/*
246accc661bSDave Chinner 	 * Round up the buffer size required to minimise the number of memory
247accc661bSDave Chinner 	 * allocations that need to be done as this item grows when relogged by
248accc661bSDave Chinner 	 * repeated modifications.
249accc661bSDave Chinner 	 */
250accc661bSDave Chinner 	*nbytes = round_up(bytes, 512);
2510b1b213fSChristoph Hellwig 	trace_xfs_buf_item_size(bip);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541234351cSChristoph Hellwig static inline void
xfs_buf_item_copy_iovec(struct xfs_log_vec * lv,struct xfs_log_iovec ** vecp,struct xfs_buf * bp,uint offset,int first_bit,uint nbits)2557aeb7222SChristoph Hellwig xfs_buf_item_copy_iovec(
256bde7cff6SChristoph Hellwig 	struct xfs_log_vec	*lv,
2571234351cSChristoph Hellwig 	struct xfs_log_iovec	**vecp,
2587aeb7222SChristoph Hellwig 	struct xfs_buf		*bp,
2597aeb7222SChristoph Hellwig 	uint			offset,
2607aeb7222SChristoph Hellwig 	int			first_bit,
2617aeb7222SChristoph Hellwig 	uint			nbits)
2627aeb7222SChristoph Hellwig {
2637aeb7222SChristoph Hellwig 	offset += first_bit * XFS_BLF_CHUNK;
264bde7cff6SChristoph Hellwig 	xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BCHUNK,
2651234351cSChristoph Hellwig 			xfs_buf_offset(bp, offset),
2661234351cSChristoph Hellwig 			nbits * XFS_BLF_CHUNK);
2677aeb7222SChristoph Hellwig }
2687aeb7222SChristoph Hellwig 
2691234351cSChristoph Hellwig static void
xfs_buf_item_format_segment(struct xfs_buf_log_item * bip,struct xfs_log_vec * lv,struct xfs_log_iovec ** vecp,uint offset,struct xfs_buf_log_format * blfp)270372cc85eSDave Chinner xfs_buf_item_format_segment(
271372cc85eSDave Chinner 	struct xfs_buf_log_item	*bip,
272bde7cff6SChristoph Hellwig 	struct xfs_log_vec	*lv,
2731234351cSChristoph Hellwig 	struct xfs_log_iovec	**vecp,
274372cc85eSDave Chinner 	uint			offset,
275372cc85eSDave Chinner 	struct xfs_buf_log_format *blfp)
276372cc85eSDave Chinner {
277372cc85eSDave Chinner 	struct xfs_buf		*bp = bip->bli_buf;
278372cc85eSDave Chinner 	uint			base_size;
279372cc85eSDave Chinner 	int			first_bit;
280372cc85eSDave Chinner 	int			last_bit;
281372cc85eSDave Chinner 	int			next_bit;
282372cc85eSDave Chinner 	uint			nbits;
283372cc85eSDave Chinner 
284372cc85eSDave Chinner 	/* copy the flags across from the base format item */
285b9438173SMark Tinguely 	blfp->blf_flags = bip->__bli_format.blf_flags;
286372cc85eSDave Chinner 
287372cc85eSDave Chinner 	/*
288372cc85eSDave Chinner 	 * Base size is the actual size of the ondisk structure - it reflects
289372cc85eSDave Chinner 	 * the actual size of the dirty bitmap rather than the size of the in
290372cc85eSDave Chinner 	 * memory structure.
291372cc85eSDave Chinner 	 */
292166d1368SDave Chinner 	base_size = xfs_buf_log_format_size(blfp);
293820a554fSMark Tinguely 
294820a554fSMark Tinguely 	first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
295820a554fSMark Tinguely 	if (!(bip->bli_flags & XFS_BLI_STALE) && first_bit == -1) {
296820a554fSMark Tinguely 		/*
297820a554fSMark Tinguely 		 * If the map is not be dirty in the transaction, mark
298820a554fSMark Tinguely 		 * the size as zero and do not advance the vector pointer.
299820a554fSMark Tinguely 		 */
300bde7cff6SChristoph Hellwig 		return;
301820a554fSMark Tinguely 	}
302820a554fSMark Tinguely 
303bde7cff6SChristoph Hellwig 	blfp = xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BFORMAT, blfp, base_size);
304bde7cff6SChristoph Hellwig 	blfp->blf_size = 1;
305372cc85eSDave Chinner 
306372cc85eSDave Chinner 	if (bip->bli_flags & XFS_BLI_STALE) {
307372cc85eSDave Chinner 		/*
308372cc85eSDave Chinner 		 * The buffer is stale, so all we need to log
309372cc85eSDave Chinner 		 * is the buf log format structure with the
310372cc85eSDave Chinner 		 * cancel flag in it.
311372cc85eSDave Chinner 		 */
312372cc85eSDave Chinner 		trace_xfs_buf_item_format_stale(bip);
313372cc85eSDave Chinner 		ASSERT(blfp->blf_flags & XFS_BLF_CANCEL);
314bde7cff6SChristoph Hellwig 		return;
315372cc85eSDave Chinner 	}
316372cc85eSDave Chinner 
3175f6bed76SDave Chinner 
318372cc85eSDave Chinner 	/*
319372cc85eSDave Chinner 	 * Fill in an iovec for each set of contiguous chunks.
320372cc85eSDave Chinner 	 */
321929f8b0dSDave Chinner 	do {
322929f8b0dSDave Chinner 		ASSERT(first_bit >= 0);
323929f8b0dSDave Chinner 		nbits = xfs_contig_bits(blfp->blf_data_map,
324929f8b0dSDave Chinner 					blfp->blf_map_size, first_bit);
325929f8b0dSDave Chinner 		ASSERT(nbits > 0);
326929f8b0dSDave Chinner 
327929f8b0dSDave Chinner 		/*
328929f8b0dSDave Chinner 		 * Straddling a page is rare because we don't log contiguous
329929f8b0dSDave Chinner 		 * chunks of unmapped buffers anywhere.
330929f8b0dSDave Chinner 		 */
331929f8b0dSDave Chinner 		if (nbits > 1 &&
332929f8b0dSDave Chinner 		    xfs_buf_item_straddle(bp, offset, first_bit, nbits))
333929f8b0dSDave Chinner 			goto slow_scan;
334929f8b0dSDave Chinner 
335929f8b0dSDave Chinner 		xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
336929f8b0dSDave Chinner 					first_bit, nbits);
337929f8b0dSDave Chinner 		blfp->blf_size++;
338929f8b0dSDave Chinner 
339929f8b0dSDave Chinner 		/*
340929f8b0dSDave Chinner 		 * This takes the bit number to start looking from and
341929f8b0dSDave Chinner 		 * returns the next set bit from there.  It returns -1
342929f8b0dSDave Chinner 		 * if there are no more bits set or the start bit is
343929f8b0dSDave Chinner 		 * beyond the end of the bitmap.
344929f8b0dSDave Chinner 		 */
345929f8b0dSDave Chinner 		first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
346929f8b0dSDave Chinner 					(uint)first_bit + nbits + 1);
347929f8b0dSDave Chinner 	} while (first_bit != -1);
348929f8b0dSDave Chinner 
349929f8b0dSDave Chinner 	return;
350929f8b0dSDave Chinner 
351929f8b0dSDave Chinner slow_scan:
352929f8b0dSDave Chinner 	ASSERT(bp->b_addr == NULL);
353372cc85eSDave Chinner 	last_bit = first_bit;
354372cc85eSDave Chinner 	nbits = 1;
355372cc85eSDave Chinner 	for (;;) {
356372cc85eSDave Chinner 		/*
357372cc85eSDave Chinner 		 * This takes the bit number to start looking from and
358372cc85eSDave Chinner 		 * returns the next set bit from there.  It returns -1
359372cc85eSDave Chinner 		 * if there are no more bits set or the start bit is
360372cc85eSDave Chinner 		 * beyond the end of the bitmap.
361372cc85eSDave Chinner 		 */
362372cc85eSDave Chinner 		next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
363372cc85eSDave Chinner 					(uint)last_bit + 1);
364372cc85eSDave Chinner 		/*
3657aeb7222SChristoph Hellwig 		 * If we run out of bits fill in the last iovec and get out of
3667aeb7222SChristoph Hellwig 		 * the loop.  Else if we start a new set of bits then fill in
3677aeb7222SChristoph Hellwig 		 * the iovec for the series we were looking at and start
3687aeb7222SChristoph Hellwig 		 * counting the bits in the new one.  Else we're still in the
3697aeb7222SChristoph Hellwig 		 * same set of bits so just keep counting and scanning.
370372cc85eSDave Chinner 		 */
371372cc85eSDave Chinner 		if (next_bit == -1) {
372bde7cff6SChristoph Hellwig 			xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
3737aeb7222SChristoph Hellwig 						first_bit, nbits);
374bde7cff6SChristoph Hellwig 			blfp->blf_size++;
375372cc85eSDave Chinner 			break;
3767aeb7222SChristoph Hellwig 		} else if (next_bit != last_bit + 1 ||
377929f8b0dSDave Chinner 		           xfs_buf_item_straddle(bp, offset, first_bit, nbits)) {
378bde7cff6SChristoph Hellwig 			xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
3797aeb7222SChristoph Hellwig 						first_bit, nbits);
380bde7cff6SChristoph Hellwig 			blfp->blf_size++;
381372cc85eSDave Chinner 			first_bit = next_bit;
382372cc85eSDave Chinner 			last_bit = next_bit;
383372cc85eSDave Chinner 			nbits = 1;
384372cc85eSDave Chinner 		} else {
385372cc85eSDave Chinner 			last_bit++;
386372cc85eSDave Chinner 			nbits++;
387372cc85eSDave Chinner 		}
388372cc85eSDave Chinner 	}
389372cc85eSDave Chinner }
390372cc85eSDave Chinner 
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds  * This is called to fill in the vector of log iovecs for the
3931da177e4SLinus Torvalds  * given log buf item.  It fills the first entry with a buf log
3941da177e4SLinus Torvalds  * format structure, and the rest point to contiguous chunks
3951da177e4SLinus Torvalds  * within the buffer.
3961da177e4SLinus Torvalds  */
397ba0f32d4SChristoph Hellwig STATIC void
xfs_buf_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)3981da177e4SLinus Torvalds xfs_buf_item_format(
3997bfa31d8SChristoph Hellwig 	struct xfs_log_item	*lip,
400bde7cff6SChristoph Hellwig 	struct xfs_log_vec	*lv)
4011da177e4SLinus Torvalds {
4027bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
4037bfa31d8SChristoph Hellwig 	struct xfs_buf		*bp = bip->bli_buf;
404bde7cff6SChristoph Hellwig 	struct xfs_log_iovec	*vecp = NULL;
405372cc85eSDave Chinner 	uint			offset = 0;
406372cc85eSDave Chinner 	int			i;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
4091da177e4SLinus Torvalds 	ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
4101da177e4SLinus Torvalds 	       (bip->bli_flags & XFS_BLI_STALE));
4110d612fb5SDave Chinner 	ASSERT((bip->bli_flags & XFS_BLI_STALE) ||
4120d612fb5SDave Chinner 	       (xfs_blft_from_flags(&bip->__bli_format) > XFS_BLFT_UNKNOWN_BUF
4130d612fb5SDave Chinner 	        && xfs_blft_from_flags(&bip->__bli_format) < XFS_BLFT_MAX_BUF));
414e9385cc6SBrian Foster 	ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED) ||
415e9385cc6SBrian Foster 	       (bip->bli_flags & XFS_BLI_STALE));
4160d612fb5SDave Chinner 
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/*
419ccf7c23fSDave Chinner 	 * If it is an inode buffer, transfer the in-memory state to the
420ddf6ad01SDave Chinner 	 * format flags and clear the in-memory state.
421ddf6ad01SDave Chinner 	 *
422ddf6ad01SDave Chinner 	 * For buffer based inode allocation, we do not transfer
423ccf7c23fSDave Chinner 	 * this state if the inode buffer allocation has not yet been committed
424ccf7c23fSDave Chinner 	 * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
425ccf7c23fSDave Chinner 	 * correct replay of the inode allocation.
426ddf6ad01SDave Chinner 	 *
427ddf6ad01SDave Chinner 	 * For icreate item based inode allocation, the buffers aren't written
428ddf6ad01SDave Chinner 	 * to the journal during allocation, and hence we should always tag the
429ddf6ad01SDave Chinner 	 * buffer as an inode buffer so that the correct unlinked list replay
430ddf6ad01SDave Chinner 	 * occurs during recovery.
431ccf7c23fSDave Chinner 	 */
432ccf7c23fSDave Chinner 	if (bip->bli_flags & XFS_BLI_INODE_BUF) {
433d86142ddSDave Chinner 		if (xfs_has_v3inodes(lip->li_log->l_mp) ||
434ddf6ad01SDave Chinner 		    !((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
4357bfa31d8SChristoph Hellwig 		      xfs_log_item_in_current_chkpt(lip)))
436b9438173SMark Tinguely 			bip->__bli_format.blf_flags |= XFS_BLF_INODE_BUF;
437ccf7c23fSDave Chinner 		bip->bli_flags &= ~XFS_BLI_INODE_BUF;
438ccf7c23fSDave Chinner 	}
439ccf7c23fSDave Chinner 
440372cc85eSDave Chinner 	for (i = 0; i < bip->bli_format_count; i++) {
441bde7cff6SChristoph Hellwig 		xfs_buf_item_format_segment(bip, lv, &vecp, offset,
442372cc85eSDave Chinner 					    &bip->bli_formats[i]);
443a3916e52SBrian Foster 		offset += BBTOB(bp->b_maps[i].bm_len);
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/*
4471da177e4SLinus Torvalds 	 * Check to make sure everything is consistent.
4481da177e4SLinus Torvalds 	 */
4490b1b213fSChristoph Hellwig 	trace_xfs_buf_item_format(bip);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
45364fc35deSDave Chinner  * This is called to pin the buffer associated with the buf log item in memory
4544d16e924SChristoph Hellwig  * so it cannot be written out.
45564fc35deSDave Chinner  *
45689a4bf0dSDave Chinner  * We take a reference to the buffer log item here so that the BLI life cycle
45789a4bf0dSDave Chinner  * extends at least until the buffer is unpinned via xfs_buf_item_unpin() and
45889a4bf0dSDave Chinner  * inserted into the AIL.
45989a4bf0dSDave Chinner  *
46089a4bf0dSDave Chinner  * We also need to take a reference to the buffer itself as the BLI unpin
46189a4bf0dSDave Chinner  * processing requires accessing the buffer after the BLI has dropped the final
46289a4bf0dSDave Chinner  * BLI reference. See xfs_buf_item_unpin() for an explanation.
46389a4bf0dSDave Chinner  * If unpins race to drop the final BLI reference and only the
46489a4bf0dSDave Chinner  * BLI owns a reference to the buffer, then the loser of the race can have the
46589a4bf0dSDave Chinner  * buffer fgreed from under it (e.g. on shutdown). Taking a buffer reference per
46689a4bf0dSDave Chinner  * pin count ensures the life cycle of the buffer extends for as
46789a4bf0dSDave Chinner  * long as we hold the buffer pin reference in xfs_buf_item_unpin().
4681da177e4SLinus Torvalds  */
469ba0f32d4SChristoph Hellwig STATIC void
xfs_buf_item_pin(struct xfs_log_item * lip)4701da177e4SLinus Torvalds xfs_buf_item_pin(
4717bfa31d8SChristoph Hellwig 	struct xfs_log_item	*lip)
4721da177e4SLinus Torvalds {
4737bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
4761da177e4SLinus Torvalds 	ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
4775f6bed76SDave Chinner 	       (bip->bli_flags & XFS_BLI_ORDERED) ||
4781da177e4SLinus Torvalds 	       (bip->bli_flags & XFS_BLI_STALE));
4797bfa31d8SChristoph Hellwig 
4800b1b213fSChristoph Hellwig 	trace_xfs_buf_item_pin(bip);
4814d16e924SChristoph Hellwig 
48289a4bf0dSDave Chinner 	xfs_buf_hold(bip->bli_buf);
4834d16e924SChristoph Hellwig 	atomic_inc(&bip->bli_refcount);
4844d16e924SChristoph Hellwig 	atomic_inc(&bip->bli_buf->b_pin_count);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
48889a4bf0dSDave Chinner  * This is called to unpin the buffer associated with the buf log item which was
48989a4bf0dSDave Chinner  * previously pinned with a call to xfs_buf_item_pin().  We enter this function
49089a4bf0dSDave Chinner  * with a buffer pin count, a buffer reference and a BLI reference.
49189a4bf0dSDave Chinner  *
49289a4bf0dSDave Chinner  * We must drop the BLI reference before we unpin the buffer because the AIL
49389a4bf0dSDave Chinner  * doesn't acquire a BLI reference whenever it accesses it. Therefore if the
49489a4bf0dSDave Chinner  * refcount drops to zero, the bli could still be AIL resident and the buffer
49589a4bf0dSDave Chinner  * submitted for I/O at any point before we return. This can result in IO
49689a4bf0dSDave Chinner  * completion freeing the buffer while we are still trying to access it here.
49789a4bf0dSDave Chinner  * This race condition can also occur in shutdown situations where we abort and
49889a4bf0dSDave Chinner  * unpin buffers from contexts other that journal IO completion.
49989a4bf0dSDave Chinner  *
50089a4bf0dSDave Chinner  * Hence we have to hold a buffer reference per pin count to ensure that the
50189a4bf0dSDave Chinner  * buffer cannot be freed until we have finished processing the unpin operation.
50289a4bf0dSDave Chinner  * The reference is taken in xfs_buf_item_pin(), and we must hold it until we
50389a4bf0dSDave Chinner  * are done processing the buffer state. In the case of an abort (remove =
50489a4bf0dSDave Chinner  * true) then we re-use the current pin reference as the IO reference we hand
50589a4bf0dSDave Chinner  * off to IO failure handling.
5061da177e4SLinus Torvalds  */
507ba0f32d4SChristoph Hellwig STATIC void
xfs_buf_item_unpin(struct xfs_log_item * lip,int remove)5081da177e4SLinus Torvalds xfs_buf_item_unpin(
5097bfa31d8SChristoph Hellwig 	struct xfs_log_item	*lip,
5109412e318SChristoph Hellwig 	int			remove)
5111da177e4SLinus Torvalds {
5127bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
513e8222613SDave Chinner 	struct xfs_buf		*bp = bip->bli_buf;
5148e123850SDave Chinner 	int			stale = bip->bli_flags & XFS_BLI_STALE;
5157bfa31d8SChristoph Hellwig 	int			freed;
5161da177e4SLinus Torvalds 
517fb1755a6SCarlos Maiolino 	ASSERT(bp->b_log_item == bip);
5181da177e4SLinus Torvalds 	ASSERT(atomic_read(&bip->bli_refcount) > 0);
5199412e318SChristoph Hellwig 
5200b1b213fSChristoph Hellwig 	trace_xfs_buf_item_unpin(bip);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	freed = atomic_dec_and_test(&bip->bli_refcount);
5234d16e924SChristoph Hellwig 	if (atomic_dec_and_test(&bp->b_pin_count))
5244d16e924SChristoph Hellwig 		wake_up_all(&bp->b_waiters);
5257bfa31d8SChristoph Hellwig 
52689a4bf0dSDave Chinner 	/*
52789a4bf0dSDave Chinner 	 * Nothing to do but drop the buffer pin reference if the BLI is
52889a4bf0dSDave Chinner 	 * still active.
52989a4bf0dSDave Chinner 	 */
53089a4bf0dSDave Chinner 	if (!freed) {
53189a4bf0dSDave Chinner 		xfs_buf_rele(bp);
53284d8949eSBrian Foster 		return;
53389a4bf0dSDave Chinner 	}
53484d8949eSBrian Foster 
53584d8949eSBrian Foster 	if (stale) {
5361da177e4SLinus Torvalds 		ASSERT(bip->bli_flags & XFS_BLI_STALE);
5370c842ad4SChristoph Hellwig 		ASSERT(xfs_buf_islocked(bp));
5385cfd28b6SDave Chinner 		ASSERT(bp->b_flags & XBF_STALE);
539b9438173SMark Tinguely 		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
540e53d3aa0SBrian Foster 		ASSERT(list_empty(&lip->li_trans));
541e53d3aa0SBrian Foster 		ASSERT(!bp->b_transp);
5429412e318SChristoph Hellwig 
5430b1b213fSChristoph Hellwig 		trace_xfs_buf_item_unpin_stale(bip);
5440b1b213fSChristoph Hellwig 
5451da177e4SLinus Torvalds 		/*
54689a4bf0dSDave Chinner 		 * The buffer has been locked and referenced since it was marked
54789a4bf0dSDave Chinner 		 * stale so we own both lock and reference exclusively here. We
54889a4bf0dSDave Chinner 		 * do not need the pin reference any more, so drop it now so
54989a4bf0dSDave Chinner 		 * that we only have one reference to drop once item completion
55089a4bf0dSDave Chinner 		 * processing is complete.
55189a4bf0dSDave Chinner 		 */
55289a4bf0dSDave Chinner 		xfs_buf_rele(bp);
55389a4bf0dSDave Chinner 
55489a4bf0dSDave Chinner 		/*
555849274c1SBrian Foster 		 * If we get called here because of an IO error, we may or may
556849274c1SBrian Foster 		 * not have the item on the AIL. xfs_trans_ail_delete() will
557849274c1SBrian Foster 		 * take care of that situation. xfs_trans_ail_delete() drops
558849274c1SBrian Foster 		 * the AIL lock.
5591da177e4SLinus Torvalds 		 */
5601da177e4SLinus Torvalds 		if (bip->bli_flags & XFS_BLI_STALE_INODE) {
561fec671cdSDave Chinner 			xfs_buf_item_done(bp);
562664ffb8aSChristoph Hellwig 			xfs_buf_inode_iodone(bp);
56348d55e2aSDave Chinner 			ASSERT(list_empty(&bp->b_li_list));
5641da177e4SLinus Torvalds 		} else {
565849274c1SBrian Foster 			xfs_trans_ail_delete(lip, SHUTDOWN_LOG_IO_ERROR);
5661da177e4SLinus Torvalds 			xfs_buf_item_relse(bp);
567fb1755a6SCarlos Maiolino 			ASSERT(bp->b_log_item == NULL);
5681da177e4SLinus Torvalds 		}
5691da177e4SLinus Torvalds 		xfs_buf_relse(bp);
57089a4bf0dSDave Chinner 		return;
57189a4bf0dSDave Chinner 	}
57289a4bf0dSDave Chinner 
57389a4bf0dSDave Chinner 	if (remove) {
574137fff09SDave Chinner 		/*
57589a4bf0dSDave Chinner 		 * We need to simulate an async IO failures here to ensure that
57689a4bf0dSDave Chinner 		 * the correct error completion is run on this buffer. This
57789a4bf0dSDave Chinner 		 * requires a reference to the buffer and for the buffer to be
57889a4bf0dSDave Chinner 		 * locked. We can safely pass ownership of the pin reference to
57989a4bf0dSDave Chinner 		 * the IO to ensure that nothing can free the buffer while we
58089a4bf0dSDave Chinner 		 * wait for the lock and then run the IO failure completion.
581137fff09SDave Chinner 		 */
582960c60afSChristoph Hellwig 		xfs_buf_lock(bp);
583137fff09SDave Chinner 		bp->b_flags |= XBF_ASYNC;
58454b3b1f6SBrian Foster 		xfs_buf_ioend_fail(bp);
58589a4bf0dSDave Chinner 		return;
5861da177e4SLinus Torvalds 	}
58789a4bf0dSDave Chinner 
58889a4bf0dSDave Chinner 	/*
58989a4bf0dSDave Chinner 	 * BLI has no more active references - it will be moved to the AIL to
59089a4bf0dSDave Chinner 	 * manage the remaining BLI/buffer life cycle. There is nothing left for
59189a4bf0dSDave Chinner 	 * us to do here so drop the pin reference to the buffer.
59289a4bf0dSDave Chinner 	 */
59389a4bf0dSDave Chinner 	xfs_buf_rele(bp);
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
596ba0f32d4SChristoph Hellwig STATIC uint
xfs_buf_item_push(struct xfs_log_item * lip,struct list_head * buffer_list)59743ff2122SChristoph Hellwig xfs_buf_item_push(
59843ff2122SChristoph Hellwig 	struct xfs_log_item	*lip,
59943ff2122SChristoph Hellwig 	struct list_head	*buffer_list)
6001da177e4SLinus Torvalds {
6017bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
6027bfa31d8SChristoph Hellwig 	struct xfs_buf		*bp = bip->bli_buf;
60343ff2122SChristoph Hellwig 	uint			rval = XFS_ITEM_SUCCESS;
6041da177e4SLinus Torvalds 
605811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
6061da177e4SLinus Torvalds 		return XFS_ITEM_PINNED;
6075337fe9bSBrian Foster 	if (!xfs_buf_trylock(bp)) {
6085337fe9bSBrian Foster 		/*
6095337fe9bSBrian Foster 		 * If we have just raced with a buffer being pinned and it has
6105337fe9bSBrian Foster 		 * been marked stale, we could end up stalling until someone else
6115337fe9bSBrian Foster 		 * issues a log force to unpin the stale buffer. Check for the
6125337fe9bSBrian Foster 		 * race condition here so xfsaild recognizes the buffer is pinned
6135337fe9bSBrian Foster 		 * and queues a log force to move it along.
6145337fe9bSBrian Foster 		 */
6155337fe9bSBrian Foster 		if (xfs_buf_ispinned(bp))
6165337fe9bSBrian Foster 			return XFS_ITEM_PINNED;
6171da177e4SLinus Torvalds 		return XFS_ITEM_LOCKED;
6185337fe9bSBrian Foster 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
62143ff2122SChristoph Hellwig 
62243ff2122SChristoph Hellwig 	trace_xfs_buf_item_push(bip);
62343ff2122SChristoph Hellwig 
624ac8809f9SDave Chinner 	/* has a previous flush failed due to IO errors? */
625f9bccfccSBrian Foster 	if (bp->b_flags & XBF_WRITE_FAIL) {
626f9bccfccSBrian Foster 		xfs_buf_alert_ratelimited(bp, "XFS: Failing async write",
627fdadf267SEric Sandeen 	    "Failing async write on buffer block 0x%llx. Retrying async write.",
6289343ee76SDave Chinner 					  (long long)xfs_buf_daddr(bp));
629ac8809f9SDave Chinner 	}
630ac8809f9SDave Chinner 
63143ff2122SChristoph Hellwig 	if (!xfs_buf_delwri_queue(bp, buffer_list))
63243ff2122SChristoph Hellwig 		rval = XFS_ITEM_FLUSHING;
63343ff2122SChristoph Hellwig 	xfs_buf_unlock(bp);
63443ff2122SChristoph Hellwig 	return rval;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds /*
63895808459SBrian Foster  * Drop the buffer log item refcount and take appropriate action. This helper
63995808459SBrian Foster  * determines whether the bli must be freed or not, since a decrement to zero
64095808459SBrian Foster  * does not necessarily mean the bli is unused.
64195808459SBrian Foster  *
64295808459SBrian Foster  * Return true if the bli is freed, false otherwise.
64395808459SBrian Foster  */
64495808459SBrian Foster bool
xfs_buf_item_put(struct xfs_buf_log_item * bip)64595808459SBrian Foster xfs_buf_item_put(
64695808459SBrian Foster 	struct xfs_buf_log_item	*bip)
64795808459SBrian Foster {
64895808459SBrian Foster 	struct xfs_log_item	*lip = &bip->bli_item;
64995808459SBrian Foster 	bool			aborted;
65095808459SBrian Foster 	bool			dirty;
65195808459SBrian Foster 
65295808459SBrian Foster 	/* drop the bli ref and return if it wasn't the last one */
65395808459SBrian Foster 	if (!atomic_dec_and_test(&bip->bli_refcount))
65495808459SBrian Foster 		return false;
65595808459SBrian Foster 
65695808459SBrian Foster 	/*
65795808459SBrian Foster 	 * We dropped the last ref and must free the item if clean or aborted.
65895808459SBrian Foster 	 * If the bli is dirty and non-aborted, the buffer was clean in the
65995808459SBrian Foster 	 * transaction but still awaiting writeback from previous changes. In
66095808459SBrian Foster 	 * that case, the bli is freed on buffer writeback completion.
66195808459SBrian Foster 	 */
66295808459SBrian Foster 	aborted = test_bit(XFS_LI_ABORTED, &lip->li_flags) ||
663d86142ddSDave Chinner 			xlog_is_shutdown(lip->li_log);
66495808459SBrian Foster 	dirty = bip->bli_flags & XFS_BLI_DIRTY;
66595808459SBrian Foster 	if (dirty && !aborted)
66695808459SBrian Foster 		return false;
66795808459SBrian Foster 
66895808459SBrian Foster 	/*
66995808459SBrian Foster 	 * The bli is aborted or clean. An aborted item may be in the AIL
67095808459SBrian Foster 	 * regardless of dirty state.  For example, consider an aborted
67195808459SBrian Foster 	 * transaction that invalidated a dirty bli and cleared the dirty
67295808459SBrian Foster 	 * state.
67395808459SBrian Foster 	 */
67495808459SBrian Foster 	if (aborted)
6752b3cf093SBrian Foster 		xfs_trans_ail_delete(lip, 0);
67695808459SBrian Foster 	xfs_buf_item_relse(bip->bli_buf);
67795808459SBrian Foster 	return true;
67895808459SBrian Foster }
67995808459SBrian Foster 
68095808459SBrian Foster /*
68164fc35deSDave Chinner  * Release the buffer associated with the buf log item.  If there is no dirty
68264fc35deSDave Chinner  * logged data associated with the buffer recorded in the buf log item, then
68364fc35deSDave Chinner  * free the buf log item and remove the reference to it in the buffer.
6841da177e4SLinus Torvalds  *
68564fc35deSDave Chinner  * This call ignores the recursion count.  It is only called when the buffer
68664fc35deSDave Chinner  * should REALLY be unlocked, regardless of the recursion count.
6871da177e4SLinus Torvalds  *
68864fc35deSDave Chinner  * We unconditionally drop the transaction's reference to the log item. If the
68964fc35deSDave Chinner  * item was logged, then another reference was taken when it was pinned, so we
69064fc35deSDave Chinner  * can safely drop the transaction reference now.  This also allows us to avoid
69164fc35deSDave Chinner  * potential races with the unpin code freeing the bli by not referencing the
69264fc35deSDave Chinner  * bli after we've dropped the reference count.
69364fc35deSDave Chinner  *
69464fc35deSDave Chinner  * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
69564fc35deSDave Chinner  * if necessary but do not unlock the buffer.  This is for support of
69664fc35deSDave Chinner  * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
69764fc35deSDave Chinner  * free the item.
6981da177e4SLinus Torvalds  */
699ba0f32d4SChristoph Hellwig STATIC void
xfs_buf_item_release(struct xfs_log_item * lip)700ddf92053SChristoph Hellwig xfs_buf_item_release(
7017bfa31d8SChristoph Hellwig 	struct xfs_log_item	*lip)
7021da177e4SLinus Torvalds {
7037bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
7047bfa31d8SChristoph Hellwig 	struct xfs_buf		*bp = bip->bli_buf;
70595808459SBrian Foster 	bool			released;
706d9183105SBrian Foster 	bool			hold = bip->bli_flags & XFS_BLI_HOLD;
707d9183105SBrian Foster 	bool			stale = bip->bli_flags & XFS_BLI_STALE;
7087bf7a193SDarrick J. Wong #if defined(DEBUG) || defined(XFS_WARN)
709d9183105SBrian Foster 	bool			ordered = bip->bli_flags & XFS_BLI_ORDERED;
71095808459SBrian Foster 	bool			dirty = bip->bli_flags & XFS_BLI_DIRTY;
7114d09807fSBrian Foster 	bool			aborted = test_bit(XFS_LI_ABORTED,
7124d09807fSBrian Foster 						   &lip->li_flags);
7137bf7a193SDarrick J. Wong #endif
7141da177e4SLinus Torvalds 
715ddf92053SChristoph Hellwig 	trace_xfs_buf_item_release(bip);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/*
7186453c65dSBrian Foster 	 * The bli dirty state should match whether the blf has logged segments
7196453c65dSBrian Foster 	 * except for ordered buffers, where only the bli should be dirty.
7201da177e4SLinus Torvalds 	 */
7216453c65dSBrian Foster 	ASSERT((!ordered && dirty == xfs_buf_item_dirty_format(bip)) ||
7226453c65dSBrian Foster 	       (ordered && dirty && !xfs_buf_item_dirty_format(bip)));
723d9183105SBrian Foster 	ASSERT(!stale || (bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
724d9183105SBrian Foster 
72546f9d2ebSDave Chinner 	/*
726d9183105SBrian Foster 	 * Clear the buffer's association with this transaction and
727d9183105SBrian Foster 	 * per-transaction state from the bli, which has been copied above.
72846f9d2ebSDave Chinner 	 */
729d9183105SBrian Foster 	bp->b_transp = NULL;
730d9183105SBrian Foster 	bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD | XFS_BLI_ORDERED);
731d9183105SBrian Foster 
732d9183105SBrian Foster 	/*
73395808459SBrian Foster 	 * Unref the item and unlock the buffer unless held or stale. Stale
73495808459SBrian Foster 	 * buffers remain locked until final unpin unless the bli is freed by
73595808459SBrian Foster 	 * the unref call. The latter implies shutdown because buffer
73695808459SBrian Foster 	 * invalidation dirties the bli and transaction.
737d9183105SBrian Foster 	 */
73895808459SBrian Foster 	released = xfs_buf_item_put(bip);
73995808459SBrian Foster 	if (hold || (stale && !released))
740d9183105SBrian Foster 		return;
7414d09807fSBrian Foster 	ASSERT(!stale || aborted);
7421da177e4SLinus Torvalds 	xfs_buf_relse(bp);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
745ddf92053SChristoph Hellwig STATIC void
xfs_buf_item_committing(struct xfs_log_item * lip,xfs_csn_t seq)746ddf92053SChristoph Hellwig xfs_buf_item_committing(
747ddf92053SChristoph Hellwig 	struct xfs_log_item	*lip,
7485f9b4b0dSDave Chinner 	xfs_csn_t		seq)
749ddf92053SChristoph Hellwig {
750ddf92053SChristoph Hellwig 	return xfs_buf_item_release(lip);
751ddf92053SChristoph Hellwig }
752ddf92053SChristoph Hellwig 
7531da177e4SLinus Torvalds /*
7541da177e4SLinus Torvalds  * This is called to find out where the oldest active copy of the
7551da177e4SLinus Torvalds  * buf log item in the on disk log resides now that the last log
7561da177e4SLinus Torvalds  * write of it completed at the given lsn.
7571da177e4SLinus Torvalds  * We always re-log all the dirty data in a buffer, so usually the
7581da177e4SLinus Torvalds  * latest copy in the on disk log is the only one that matters.  For
7591da177e4SLinus Torvalds  * those cases we simply return the given lsn.
7601da177e4SLinus Torvalds  *
7611da177e4SLinus Torvalds  * The one exception to this is for buffers full of newly allocated
7621da177e4SLinus Torvalds  * inodes.  These buffers are only relogged with the XFS_BLI_INODE_BUF
7631da177e4SLinus Torvalds  * flag set, indicating that only the di_next_unlinked fields from the
7641da177e4SLinus Torvalds  * inodes in the buffers will be replayed during recovery.  If the
7651da177e4SLinus Torvalds  * original newly allocated inode images have not yet been flushed
7661da177e4SLinus Torvalds  * when the buffer is so relogged, then we need to make sure that we
7671da177e4SLinus Torvalds  * keep the old images in the 'active' portion of the log.  We do this
7681da177e4SLinus Torvalds  * by returning the original lsn of that transaction here rather than
7691da177e4SLinus Torvalds  * the current one.
7701da177e4SLinus Torvalds  */
771ba0f32d4SChristoph Hellwig STATIC xfs_lsn_t
xfs_buf_item_committed(struct xfs_log_item * lip,xfs_lsn_t lsn)7721da177e4SLinus Torvalds xfs_buf_item_committed(
7737bfa31d8SChristoph Hellwig 	struct xfs_log_item	*lip,
7741da177e4SLinus Torvalds 	xfs_lsn_t		lsn)
7751da177e4SLinus Torvalds {
7767bfa31d8SChristoph Hellwig 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
7777bfa31d8SChristoph Hellwig 
7780b1b213fSChristoph Hellwig 	trace_xfs_buf_item_committed(bip);
7790b1b213fSChristoph Hellwig 
7807bfa31d8SChristoph Hellwig 	if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
7817bfa31d8SChristoph Hellwig 		return lip->li_lsn;
7827bfa31d8SChristoph Hellwig 	return lsn;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
785*c08d0399SDarrick J. Wong #ifdef DEBUG_EXPENSIVE
786*c08d0399SDarrick J. Wong static int
xfs_buf_item_precommit(struct xfs_trans * tp,struct xfs_log_item * lip)787*c08d0399SDarrick J. Wong xfs_buf_item_precommit(
788*c08d0399SDarrick J. Wong 	struct xfs_trans	*tp,
789*c08d0399SDarrick J. Wong 	struct xfs_log_item	*lip)
790*c08d0399SDarrick J. Wong {
791*c08d0399SDarrick J. Wong 	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
792*c08d0399SDarrick J. Wong 	struct xfs_buf		*bp = bip->bli_buf;
793*c08d0399SDarrick J. Wong 	struct xfs_mount	*mp = bp->b_mount;
794*c08d0399SDarrick J. Wong 	xfs_failaddr_t		fa;
795*c08d0399SDarrick J. Wong 
796*c08d0399SDarrick J. Wong 	if (!bp->b_ops || !bp->b_ops->verify_struct)
797*c08d0399SDarrick J. Wong 		return 0;
798*c08d0399SDarrick J. Wong 	if (bip->bli_flags & XFS_BLI_STALE)
799*c08d0399SDarrick J. Wong 		return 0;
800*c08d0399SDarrick J. Wong 
801*c08d0399SDarrick J. Wong 	fa = bp->b_ops->verify_struct(bp);
802*c08d0399SDarrick J. Wong 	if (fa) {
803*c08d0399SDarrick J. Wong 		xfs_buf_verifier_error(bp, -EFSCORRUPTED, bp->b_ops->name,
804*c08d0399SDarrick J. Wong 				bp->b_addr, BBTOB(bp->b_length), fa);
805*c08d0399SDarrick J. Wong 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
806*c08d0399SDarrick J. Wong 		ASSERT(fa == NULL);
807*c08d0399SDarrick J. Wong 	}
808*c08d0399SDarrick J. Wong 
809*c08d0399SDarrick J. Wong 	return 0;
810*c08d0399SDarrick J. Wong }
811*c08d0399SDarrick J. Wong #else
812*c08d0399SDarrick J. Wong # define xfs_buf_item_precommit	NULL
813*c08d0399SDarrick J. Wong #endif
814*c08d0399SDarrick J. Wong 
815272e42b2SChristoph Hellwig static const struct xfs_item_ops xfs_buf_item_ops = {
8167bfa31d8SChristoph Hellwig 	.iop_size	= xfs_buf_item_size,
817*c08d0399SDarrick J. Wong 	.iop_precommit	= xfs_buf_item_precommit,
8187bfa31d8SChristoph Hellwig 	.iop_format	= xfs_buf_item_format,
8197bfa31d8SChristoph Hellwig 	.iop_pin	= xfs_buf_item_pin,
8207bfa31d8SChristoph Hellwig 	.iop_unpin	= xfs_buf_item_unpin,
821ddf92053SChristoph Hellwig 	.iop_release	= xfs_buf_item_release,
822ddf92053SChristoph Hellwig 	.iop_committing	= xfs_buf_item_committing,
8237bfa31d8SChristoph Hellwig 	.iop_committed	= xfs_buf_item_committed,
8247bfa31d8SChristoph Hellwig 	.iop_push	= xfs_buf_item_push,
8251da177e4SLinus Torvalds };
8261da177e4SLinus Torvalds 
827c64dd49bSDarrick J. Wong STATIC void
xfs_buf_item_get_format(struct xfs_buf_log_item * bip,int count)828372cc85eSDave Chinner xfs_buf_item_get_format(
829372cc85eSDave Chinner 	struct xfs_buf_log_item	*bip,
830372cc85eSDave Chinner 	int			count)
831372cc85eSDave Chinner {
832372cc85eSDave Chinner 	ASSERT(bip->bli_formats == NULL);
833372cc85eSDave Chinner 	bip->bli_format_count = count;
834372cc85eSDave Chinner 
835372cc85eSDave Chinner 	if (count == 1) {
836b9438173SMark Tinguely 		bip->bli_formats = &bip->__bli_format;
837c64dd49bSDarrick J. Wong 		return;
838372cc85eSDave Chinner 	}
839372cc85eSDave Chinner 
840372cc85eSDave Chinner 	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
841707e0ddaSTetsuo Handa 				0);
842372cc85eSDave Chinner }
843372cc85eSDave Chinner 
844372cc85eSDave Chinner STATIC void
xfs_buf_item_free_format(struct xfs_buf_log_item * bip)845372cc85eSDave Chinner xfs_buf_item_free_format(
846372cc85eSDave Chinner 	struct xfs_buf_log_item	*bip)
847372cc85eSDave Chinner {
848b9438173SMark Tinguely 	if (bip->bli_formats != &bip->__bli_format) {
849372cc85eSDave Chinner 		kmem_free(bip->bli_formats);
850372cc85eSDave Chinner 		bip->bli_formats = NULL;
851372cc85eSDave Chinner 	}
852372cc85eSDave Chinner }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /*
8551da177e4SLinus Torvalds  * Allocate a new buf log item to go with the given buffer.
856fb1755a6SCarlos Maiolino  * Set the buffer's b_log_item field to point to the new
857fb1755a6SCarlos Maiolino  * buf log item.
8581da177e4SLinus Torvalds  */
859f79af0b9SDave Chinner int
xfs_buf_item_init(struct xfs_buf * bp,struct xfs_mount * mp)8601da177e4SLinus Torvalds xfs_buf_item_init(
861f79af0b9SDave Chinner 	struct xfs_buf	*bp,
862f79af0b9SDave Chinner 	struct xfs_mount *mp)
8631da177e4SLinus Torvalds {
864fb1755a6SCarlos Maiolino 	struct xfs_buf_log_item	*bip = bp->b_log_item;
8651da177e4SLinus Torvalds 	int			chunks;
8661da177e4SLinus Torvalds 	int			map_size;
867372cc85eSDave Chinner 	int			i;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	/*
8701da177e4SLinus Torvalds 	 * Check to see if there is already a buf log item for
871fb1755a6SCarlos Maiolino 	 * this buffer. If we do already have one, there is
8721da177e4SLinus Torvalds 	 * nothing to do here so return.
8731da177e4SLinus Torvalds 	 */
874dbd329f1SChristoph Hellwig 	ASSERT(bp->b_mount == mp);
8751a2ebf83SDave Chinner 	if (bip) {
876fb1755a6SCarlos Maiolino 		ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
8771a2ebf83SDave Chinner 		ASSERT(!bp->b_transp);
8781a2ebf83SDave Chinner 		ASSERT(bip->bli_buf == bp);
879f79af0b9SDave Chinner 		return 0;
880fb1755a6SCarlos Maiolino 	}
8811da177e4SLinus Torvalds 
882182696fbSDarrick J. Wong 	bip = kmem_cache_zalloc(xfs_buf_item_cache, GFP_KERNEL | __GFP_NOFAIL);
88343f5efc5SDave Chinner 	xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
8841da177e4SLinus Torvalds 	bip->bli_buf = bp;
885372cc85eSDave Chinner 
886372cc85eSDave Chinner 	/*
887372cc85eSDave Chinner 	 * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
888372cc85eSDave Chinner 	 * can be divided into. Make sure not to truncate any pieces.
889372cc85eSDave Chinner 	 * map_size is the size of the bitmap needed to describe the
890372cc85eSDave Chinner 	 * chunks of the buffer.
891372cc85eSDave Chinner 	 *
892372cc85eSDave Chinner 	 * Discontiguous buffer support follows the layout of the underlying
893372cc85eSDave Chinner 	 * buffer. This makes the implementation as simple as possible.
894372cc85eSDave Chinner 	 */
895c64dd49bSDarrick J. Wong 	xfs_buf_item_get_format(bip, bp->b_map_count);
896372cc85eSDave Chinner 
897372cc85eSDave Chinner 	for (i = 0; i < bip->bli_format_count; i++) {
898372cc85eSDave Chinner 		chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
899372cc85eSDave Chinner 				      XFS_BLF_CHUNK);
900372cc85eSDave Chinner 		map_size = DIV_ROUND_UP(chunks, NBWORD);
901372cc85eSDave Chinner 
902c3d5f0c2SDarrick J. Wong 		if (map_size > XFS_BLF_DATAMAP_SIZE) {
903182696fbSDarrick J. Wong 			kmem_cache_free(xfs_buf_item_cache, bip);
904c3d5f0c2SDarrick J. Wong 			xfs_err(mp,
905c3d5f0c2SDarrick J. Wong 	"buffer item dirty bitmap (%u uints) too small to reflect %u bytes!",
906c3d5f0c2SDarrick J. Wong 					map_size,
907c3d5f0c2SDarrick J. Wong 					BBTOB(bp->b_maps[i].bm_len));
908c3d5f0c2SDarrick J. Wong 			return -EFSCORRUPTED;
909c3d5f0c2SDarrick J. Wong 		}
910c3d5f0c2SDarrick J. Wong 
911372cc85eSDave Chinner 		bip->bli_formats[i].blf_type = XFS_LI_BUF;
912372cc85eSDave Chinner 		bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
913372cc85eSDave Chinner 		bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
914372cc85eSDave Chinner 		bip->bli_formats[i].blf_map_size = map_size;
915372cc85eSDave Chinner 	}
9161da177e4SLinus Torvalds 
917fb1755a6SCarlos Maiolino 	bp->b_log_item = bip;
918f79af0b9SDave Chinner 	xfs_buf_hold(bp);
919f79af0b9SDave Chinner 	return 0;
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds /*
9241da177e4SLinus Torvalds  * Mark bytes first through last inclusive as dirty in the buf
9251da177e4SLinus Torvalds  * item's bitmap.
9261da177e4SLinus Torvalds  */
927632b89e8SDave Chinner static void
xfs_buf_item_log_segment(uint first,uint last,uint * map)928372cc85eSDave Chinner xfs_buf_item_log_segment(
9291da177e4SLinus Torvalds 	uint			first,
930372cc85eSDave Chinner 	uint			last,
931372cc85eSDave Chinner 	uint			*map)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	uint		first_bit;
9341da177e4SLinus Torvalds 	uint		last_bit;
9351da177e4SLinus Torvalds 	uint		bits_to_set;
9361da177e4SLinus Torvalds 	uint		bits_set;
9371da177e4SLinus Torvalds 	uint		word_num;
9381da177e4SLinus Torvalds 	uint		*wordp;
9391da177e4SLinus Torvalds 	uint		bit;
9401da177e4SLinus Torvalds 	uint		end_bit;
9411da177e4SLinus Torvalds 	uint		mask;
9421da177e4SLinus Torvalds 
943c3d5f0c2SDarrick J. Wong 	ASSERT(first < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
944c3d5f0c2SDarrick J. Wong 	ASSERT(last < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
945c3d5f0c2SDarrick J. Wong 
9461da177e4SLinus Torvalds 	/*
9471da177e4SLinus Torvalds 	 * Convert byte offsets to bit numbers.
9481da177e4SLinus Torvalds 	 */
949c1155410SDave Chinner 	first_bit = first >> XFS_BLF_SHIFT;
950c1155410SDave Chinner 	last_bit = last >> XFS_BLF_SHIFT;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	/*
9531da177e4SLinus Torvalds 	 * Calculate the total number of bits to be set.
9541da177e4SLinus Torvalds 	 */
9551da177e4SLinus Torvalds 	bits_to_set = last_bit - first_bit + 1;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	/*
9581da177e4SLinus Torvalds 	 * Get a pointer to the first word in the bitmap
9591da177e4SLinus Torvalds 	 * to set a bit in.
9601da177e4SLinus Torvalds 	 */
9611da177e4SLinus Torvalds 	word_num = first_bit >> BIT_TO_WORD_SHIFT;
962372cc85eSDave Chinner 	wordp = &map[word_num];
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/*
9651da177e4SLinus Torvalds 	 * Calculate the starting bit in the first word.
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	bit = first_bit & (uint)(NBWORD - 1);
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/*
9701da177e4SLinus Torvalds 	 * First set any bits in the first word of our range.
9711da177e4SLinus Torvalds 	 * If it starts at bit 0 of the word, it will be
9721da177e4SLinus Torvalds 	 * set below rather than here.  That is what the variable
9731da177e4SLinus Torvalds 	 * bit tells us. The variable bits_set tracks the number
9741da177e4SLinus Torvalds 	 * of bits that have been set so far.  End_bit is the number
9751da177e4SLinus Torvalds 	 * of the last bit to be set in this word plus one.
9761da177e4SLinus Torvalds 	 */
9771da177e4SLinus Torvalds 	if (bit) {
9789bb54cb5SDave Chinner 		end_bit = min(bit + bits_to_set, (uint)NBWORD);
97979c350e4SXie XiuQi 		mask = ((1U << (end_bit - bit)) - 1) << bit;
9801da177e4SLinus Torvalds 		*wordp |= mask;
9811da177e4SLinus Torvalds 		wordp++;
9821da177e4SLinus Torvalds 		bits_set = end_bit - bit;
9831da177e4SLinus Torvalds 	} else {
9841da177e4SLinus Torvalds 		bits_set = 0;
9851da177e4SLinus Torvalds 	}
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	/*
9881da177e4SLinus Torvalds 	 * Now set bits a whole word at a time that are between
9891da177e4SLinus Torvalds 	 * first_bit and last_bit.
9901da177e4SLinus Torvalds 	 */
9911da177e4SLinus Torvalds 	while ((bits_to_set - bits_set) >= NBWORD) {
99212025460SDarrick J. Wong 		*wordp = 0xffffffff;
9931da177e4SLinus Torvalds 		bits_set += NBWORD;
9941da177e4SLinus Torvalds 		wordp++;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	/*
9981da177e4SLinus Torvalds 	 * Finally, set any bits left to be set in one last partial word.
9991da177e4SLinus Torvalds 	 */
10001da177e4SLinus Torvalds 	end_bit = bits_to_set - bits_set;
10011da177e4SLinus Torvalds 	if (end_bit) {
100279c350e4SXie XiuQi 		mask = (1U << end_bit) - 1;
10031da177e4SLinus Torvalds 		*wordp |= mask;
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
1007372cc85eSDave Chinner /*
1008372cc85eSDave Chinner  * Mark bytes first through last inclusive as dirty in the buf
1009372cc85eSDave Chinner  * item's bitmap.
1010372cc85eSDave Chinner  */
1011372cc85eSDave Chinner void
xfs_buf_item_log(struct xfs_buf_log_item * bip,uint first,uint last)1012372cc85eSDave Chinner xfs_buf_item_log(
101370a20655SCarlos Maiolino 	struct xfs_buf_log_item	*bip,
1014372cc85eSDave Chinner 	uint			first,
1015372cc85eSDave Chinner 	uint			last)
1016372cc85eSDave Chinner {
1017372cc85eSDave Chinner 	int			i;
1018372cc85eSDave Chinner 	uint			start;
1019372cc85eSDave Chinner 	uint			end;
1020372cc85eSDave Chinner 	struct xfs_buf		*bp = bip->bli_buf;
1021372cc85eSDave Chinner 
1022372cc85eSDave Chinner 	/*
1023372cc85eSDave Chinner 	 * walk each buffer segment and mark them dirty appropriately.
1024372cc85eSDave Chinner 	 */
1025372cc85eSDave Chinner 	start = 0;
1026372cc85eSDave Chinner 	for (i = 0; i < bip->bli_format_count; i++) {
1027372cc85eSDave Chinner 		if (start > last)
1028372cc85eSDave Chinner 			break;
1029a3916e52SBrian Foster 		end = start + BBTOB(bp->b_maps[i].bm_len) - 1;
1030a3916e52SBrian Foster 
1031a3916e52SBrian Foster 		/* skip to the map that includes the first byte to log */
1032372cc85eSDave Chinner 		if (first > end) {
1033372cc85eSDave Chinner 			start += BBTOB(bp->b_maps[i].bm_len);
1034372cc85eSDave Chinner 			continue;
1035372cc85eSDave Chinner 		}
1036a3916e52SBrian Foster 
1037a3916e52SBrian Foster 		/*
1038a3916e52SBrian Foster 		 * Trim the range to this segment and mark it in the bitmap.
1039a3916e52SBrian Foster 		 * Note that we must convert buffer offsets to segment relative
1040a3916e52SBrian Foster 		 * offsets (e.g., the first byte of each segment is byte 0 of
1041a3916e52SBrian Foster 		 * that segment).
1042a3916e52SBrian Foster 		 */
1043372cc85eSDave Chinner 		if (first < start)
1044372cc85eSDave Chinner 			first = start;
1045372cc85eSDave Chinner 		if (end > last)
1046372cc85eSDave Chinner 			end = last;
1047a3916e52SBrian Foster 		xfs_buf_item_log_segment(first - start, end - start,
1048372cc85eSDave Chinner 					 &bip->bli_formats[i].blf_data_map[0]);
1049372cc85eSDave Chinner 
1050a3916e52SBrian Foster 		start += BBTOB(bp->b_maps[i].bm_len);
1051372cc85eSDave Chinner 	}
1052372cc85eSDave Chinner }
1053372cc85eSDave Chinner 
10541da177e4SLinus Torvalds 
10556453c65dSBrian Foster /*
10566453c65dSBrian Foster  * Return true if the buffer has any ranges logged/dirtied by a transaction,
10576453c65dSBrian Foster  * false otherwise.
10586453c65dSBrian Foster  */
10596453c65dSBrian Foster bool
xfs_buf_item_dirty_format(struct xfs_buf_log_item * bip)10606453c65dSBrian Foster xfs_buf_item_dirty_format(
10616453c65dSBrian Foster 	struct xfs_buf_log_item	*bip)
10626453c65dSBrian Foster {
10636453c65dSBrian Foster 	int			i;
10646453c65dSBrian Foster 
10656453c65dSBrian Foster 	for (i = 0; i < bip->bli_format_count; i++) {
10666453c65dSBrian Foster 		if (!xfs_bitmap_empty(bip->bli_formats[i].blf_data_map,
10676453c65dSBrian Foster 			     bip->bli_formats[i].blf_map_size))
10686453c65dSBrian Foster 			return true;
10696453c65dSBrian Foster 	}
10706453c65dSBrian Foster 
10716453c65dSBrian Foster 	return false;
10726453c65dSBrian Foster }
10736453c65dSBrian Foster 
1074e1f5dbd7SLachlan McIlroy STATIC void
xfs_buf_item_free(struct xfs_buf_log_item * bip)1075e1f5dbd7SLachlan McIlroy xfs_buf_item_free(
107670a20655SCarlos Maiolino 	struct xfs_buf_log_item	*bip)
1077e1f5dbd7SLachlan McIlroy {
1078372cc85eSDave Chinner 	xfs_buf_item_free_format(bip);
1079b1c5ebb2SDave Chinner 	kmem_free(bip->bli_item.li_lv_shadow);
1080182696fbSDarrick J. Wong 	kmem_cache_free(xfs_buf_item_cache, bip);
1081e1f5dbd7SLachlan McIlroy }
1082e1f5dbd7SLachlan McIlroy 
10831da177e4SLinus Torvalds /*
1084b01d1461SDave Chinner  * xfs_buf_item_relse() is called when the buf log item is no longer needed.
10851da177e4SLinus Torvalds  */
10861da177e4SLinus Torvalds void
xfs_buf_item_relse(struct xfs_buf * bp)10871da177e4SLinus Torvalds xfs_buf_item_relse(
1088e8222613SDave Chinner 	struct xfs_buf	*bp)
10891da177e4SLinus Torvalds {
1090fb1755a6SCarlos Maiolino 	struct xfs_buf_log_item	*bip = bp->b_log_item;
10911da177e4SLinus Torvalds 
10920b1b213fSChristoph Hellwig 	trace_xfs_buf_item_relse(bp, _RET_IP_);
1093826f7e34SBrian Foster 	ASSERT(!test_bit(XFS_LI_IN_AIL, &bip->bli_item.li_flags));
10940b1b213fSChristoph Hellwig 
1095575689fcSGuo Xuenan 	if (atomic_read(&bip->bli_refcount))
1096575689fcSGuo Xuenan 		return;
1097fb1755a6SCarlos Maiolino 	bp->b_log_item = NULL;
1098e1f5dbd7SLachlan McIlroy 	xfs_buf_rele(bp);
1099e1f5dbd7SLachlan McIlroy 	xfs_buf_item_free(bip);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
1102664ffb8aSChristoph Hellwig void
xfs_buf_item_done(struct xfs_buf * bp)1103fec671cdSDave Chinner xfs_buf_item_done(
1104aac855abSDave Chinner 	struct xfs_buf		*bp)
1105aac855abSDave Chinner {
1106fec671cdSDave Chinner 	/*
1107fec671cdSDave Chinner 	 * If we are forcibly shutting down, this may well be off the AIL
1108fec671cdSDave Chinner 	 * already. That's because we simulate the log-committed callbacks to
1109fec671cdSDave Chinner 	 * unpin these buffers. Or we may never have put this item on AIL
1110fec671cdSDave Chinner 	 * because of the transaction was aborted forcibly.
1111fec671cdSDave Chinner 	 * xfs_trans_ail_delete() takes care of these.
1112fec671cdSDave Chinner 	 *
1113fec671cdSDave Chinner 	 * Either way, AIL is useless if we're forcing a shutdown.
111422c10589SChristoph Hellwig 	 *
111522c10589SChristoph Hellwig 	 * Note that log recovery writes might have buffer items that are not on
111622c10589SChristoph Hellwig 	 * the AIL even when the file system is not shut down.
1117fec671cdSDave Chinner 	 */
1118b840e2adSChristoph Hellwig 	xfs_trans_ail_delete(&bp->b_log_item->bli_item,
111922c10589SChristoph Hellwig 			     (bp->b_flags & _XBF_LOGRECOVERY) ? 0 :
1120b840e2adSChristoph Hellwig 			     SHUTDOWN_CORRUPT_INCORE);
1121b840e2adSChristoph Hellwig 	xfs_buf_item_relse(bp);
11221da177e4SLinus Torvalds }
1123