162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2000-2006 Silicon Graphics, Inc.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#include "xfs.h"
762306a36Sopenharmony_ci#include "xfs_fs.h"
862306a36Sopenharmony_ci#include "xfs_shared.h"
962306a36Sopenharmony_ci#include "xfs_format.h"
1062306a36Sopenharmony_ci#include "xfs_log_format.h"
1162306a36Sopenharmony_ci#include "xfs_trans_resv.h"
1262306a36Sopenharmony_ci#include "xfs_bit.h"
1362306a36Sopenharmony_ci#include "xfs_mount.h"
1462306a36Sopenharmony_ci#include "xfs_trans.h"
1562306a36Sopenharmony_ci#include "xfs_buf_item.h"
1662306a36Sopenharmony_ci#include "xfs_trans_priv.h"
1762306a36Sopenharmony_ci#include "xfs_trace.h"
1862306a36Sopenharmony_ci#include "xfs_log.h"
1962306a36Sopenharmony_ci#include "xfs_log_priv.h"
2062306a36Sopenharmony_ci#include "xfs_log_recover.h"
2162306a36Sopenharmony_ci#include "xfs_error.h"
2262306a36Sopenharmony_ci#include "xfs_inode.h"
2362306a36Sopenharmony_ci#include "xfs_dir2.h"
2462306a36Sopenharmony_ci#include "xfs_quota.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * This is the number of entries in the l_buf_cancel_table used during
2862306a36Sopenharmony_ci * recovery.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci#define	XLOG_BC_TABLE_SIZE	64
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define XLOG_BUF_CANCEL_BUCKET(log, blkno) \
3362306a36Sopenharmony_ci	((log)->l_buf_cancel_table + ((uint64_t)blkno % XLOG_BC_TABLE_SIZE))
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/*
3662306a36Sopenharmony_ci * This structure is used during recovery to record the buf log items which
3762306a36Sopenharmony_ci * have been canceled and should not be replayed.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_cistruct xfs_buf_cancel {
4062306a36Sopenharmony_ci	xfs_daddr_t		bc_blkno;
4162306a36Sopenharmony_ci	uint			bc_len;
4262306a36Sopenharmony_ci	int			bc_refcount;
4362306a36Sopenharmony_ci	struct list_head	bc_list;
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic struct xfs_buf_cancel *
4762306a36Sopenharmony_cixlog_find_buffer_cancelled(
4862306a36Sopenharmony_ci	struct xlog		*log,
4962306a36Sopenharmony_ci	xfs_daddr_t		blkno,
5062306a36Sopenharmony_ci	uint			len)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	struct list_head	*bucket;
5362306a36Sopenharmony_ci	struct xfs_buf_cancel	*bcp;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	if (!log->l_buf_cancel_table)
5662306a36Sopenharmony_ci		return NULL;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
5962306a36Sopenharmony_ci	list_for_each_entry(bcp, bucket, bc_list) {
6062306a36Sopenharmony_ci		if (bcp->bc_blkno == blkno && bcp->bc_len == len)
6162306a36Sopenharmony_ci			return bcp;
6262306a36Sopenharmony_ci	}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	return NULL;
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic bool
6862306a36Sopenharmony_cixlog_add_buffer_cancelled(
6962306a36Sopenharmony_ci	struct xlog		*log,
7062306a36Sopenharmony_ci	xfs_daddr_t		blkno,
7162306a36Sopenharmony_ci	uint			len)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	struct xfs_buf_cancel	*bcp;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	/*
7662306a36Sopenharmony_ci	 * If we find an existing cancel record, this indicates that the buffer
7762306a36Sopenharmony_ci	 * was cancelled multiple times.  To ensure that during pass 2 we keep
7862306a36Sopenharmony_ci	 * the record in the table until we reach its last occurrence in the
7962306a36Sopenharmony_ci	 * log, a reference count is kept to tell how many times we expect to
8062306a36Sopenharmony_ci	 * see this record during the second pass.
8162306a36Sopenharmony_ci	 */
8262306a36Sopenharmony_ci	bcp = xlog_find_buffer_cancelled(log, blkno, len);
8362306a36Sopenharmony_ci	if (bcp) {
8462306a36Sopenharmony_ci		bcp->bc_refcount++;
8562306a36Sopenharmony_ci		return false;
8662306a36Sopenharmony_ci	}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), 0);
8962306a36Sopenharmony_ci	bcp->bc_blkno = blkno;
9062306a36Sopenharmony_ci	bcp->bc_len = len;
9162306a36Sopenharmony_ci	bcp->bc_refcount = 1;
9262306a36Sopenharmony_ci	list_add_tail(&bcp->bc_list, XLOG_BUF_CANCEL_BUCKET(log, blkno));
9362306a36Sopenharmony_ci	return true;
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/*
9762306a36Sopenharmony_ci * Check if there is and entry for blkno, len in the buffer cancel record table.
9862306a36Sopenharmony_ci */
9962306a36Sopenharmony_cibool
10062306a36Sopenharmony_cixlog_is_buffer_cancelled(
10162306a36Sopenharmony_ci	struct xlog		*log,
10262306a36Sopenharmony_ci	xfs_daddr_t		blkno,
10362306a36Sopenharmony_ci	uint			len)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	return xlog_find_buffer_cancelled(log, blkno, len) != NULL;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/*
10962306a36Sopenharmony_ci * Check if there is and entry for blkno, len in the buffer cancel record table,
11062306a36Sopenharmony_ci * and decremented the reference count on it if there is one.
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci * Remove the cancel record once the refcount hits zero, so that if the same
11362306a36Sopenharmony_ci * buffer is re-used again after its last cancellation we actually replay the
11462306a36Sopenharmony_ci * changes made at that point.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_cistatic bool
11762306a36Sopenharmony_cixlog_put_buffer_cancelled(
11862306a36Sopenharmony_ci	struct xlog		*log,
11962306a36Sopenharmony_ci	xfs_daddr_t		blkno,
12062306a36Sopenharmony_ci	uint			len)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct xfs_buf_cancel	*bcp;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	bcp = xlog_find_buffer_cancelled(log, blkno, len);
12562306a36Sopenharmony_ci	if (!bcp) {
12662306a36Sopenharmony_ci		ASSERT(0);
12762306a36Sopenharmony_ci		return false;
12862306a36Sopenharmony_ci	}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	if (--bcp->bc_refcount == 0) {
13162306a36Sopenharmony_ci		list_del(&bcp->bc_list);
13262306a36Sopenharmony_ci		kmem_free(bcp);
13362306a36Sopenharmony_ci	}
13462306a36Sopenharmony_ci	return true;
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/* log buffer item recovery */
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/*
14062306a36Sopenharmony_ci * Sort buffer items for log recovery.  Most buffer items should end up on the
14162306a36Sopenharmony_ci * buffer list and are recovered first, with the following exceptions:
14262306a36Sopenharmony_ci *
14362306a36Sopenharmony_ci * 1. XFS_BLF_CANCEL buffers must be processed last because some log items
14462306a36Sopenharmony_ci *    might depend on the incor ecancellation record, and replaying a cancelled
14562306a36Sopenharmony_ci *    buffer item can remove the incore record.
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * 2. XFS_BLF_INODE_BUF buffers are handled after most regular items so that
14862306a36Sopenharmony_ci *    we replay di_next_unlinked only after flushing the inode 'free' state
14962306a36Sopenharmony_ci *    to the inode buffer.
15062306a36Sopenharmony_ci *
15162306a36Sopenharmony_ci * See xlog_recover_reorder_trans for more details.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_ciSTATIC enum xlog_recover_reorder
15462306a36Sopenharmony_cixlog_recover_buf_reorder(
15562306a36Sopenharmony_ci	struct xlog_recover_item	*item)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f = item->ri_buf[0].i_addr;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_CANCEL)
16062306a36Sopenharmony_ci		return XLOG_REORDER_CANCEL_LIST;
16162306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
16262306a36Sopenharmony_ci		return XLOG_REORDER_INODE_BUFFER_LIST;
16362306a36Sopenharmony_ci	return XLOG_REORDER_BUFFER_LIST;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciSTATIC void
16762306a36Sopenharmony_cixlog_recover_buf_ra_pass2(
16862306a36Sopenharmony_ci	struct xlog                     *log,
16962306a36Sopenharmony_ci	struct xlog_recover_item        *item)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f = item->ri_buf[0].i_addr;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	xlog_buf_readahead(log, buf_f->blf_blkno, buf_f->blf_len, NULL);
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci/*
17762306a36Sopenharmony_ci * Build up the table of buf cancel records so that we don't replay cancelled
17862306a36Sopenharmony_ci * data in the second pass.
17962306a36Sopenharmony_ci */
18062306a36Sopenharmony_cistatic int
18162306a36Sopenharmony_cixlog_recover_buf_commit_pass1(
18262306a36Sopenharmony_ci	struct xlog			*log,
18362306a36Sopenharmony_ci	struct xlog_recover_item	*item)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	struct xfs_buf_log_format	*bf = item->ri_buf[0].i_addr;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	if (!xfs_buf_log_check_iovec(&item->ri_buf[0])) {
18862306a36Sopenharmony_ci		xfs_err(log->l_mp, "bad buffer log item size (%d)",
18962306a36Sopenharmony_ci				item->ri_buf[0].i_len);
19062306a36Sopenharmony_ci		return -EFSCORRUPTED;
19162306a36Sopenharmony_ci	}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	if (!(bf->blf_flags & XFS_BLF_CANCEL))
19462306a36Sopenharmony_ci		trace_xfs_log_recover_buf_not_cancel(log, bf);
19562306a36Sopenharmony_ci	else if (xlog_add_buffer_cancelled(log, bf->blf_blkno, bf->blf_len))
19662306a36Sopenharmony_ci		trace_xfs_log_recover_buf_cancel_add(log, bf);
19762306a36Sopenharmony_ci	else
19862306a36Sopenharmony_ci		trace_xfs_log_recover_buf_cancel_ref_inc(log, bf);
19962306a36Sopenharmony_ci	return 0;
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/*
20362306a36Sopenharmony_ci * Validate the recovered buffer is of the correct type and attach the
20462306a36Sopenharmony_ci * appropriate buffer operations to them for writeback. Magic numbers are in a
20562306a36Sopenharmony_ci * few places:
20662306a36Sopenharmony_ci *	the first 16 bits of the buffer (inode buffer, dquot buffer),
20762306a36Sopenharmony_ci *	the first 32 bits of the buffer (most blocks),
20862306a36Sopenharmony_ci *	inside a struct xfs_da_blkinfo at the start of the buffer.
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_cistatic void
21162306a36Sopenharmony_cixlog_recover_validate_buf_type(
21262306a36Sopenharmony_ci	struct xfs_mount		*mp,
21362306a36Sopenharmony_ci	struct xfs_buf			*bp,
21462306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f,
21562306a36Sopenharmony_ci	xfs_lsn_t			current_lsn)
21662306a36Sopenharmony_ci{
21762306a36Sopenharmony_ci	struct xfs_da_blkinfo		*info = bp->b_addr;
21862306a36Sopenharmony_ci	uint32_t			magic32;
21962306a36Sopenharmony_ci	uint16_t			magic16;
22062306a36Sopenharmony_ci	uint16_t			magicda;
22162306a36Sopenharmony_ci	char				*warnmsg = NULL;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	/*
22462306a36Sopenharmony_ci	 * We can only do post recovery validation on items on CRC enabled
22562306a36Sopenharmony_ci	 * fielsystems as we need to know when the buffer was written to be able
22662306a36Sopenharmony_ci	 * to determine if we should have replayed the item. If we replay old
22762306a36Sopenharmony_ci	 * metadata over a newer buffer, then it will enter a temporarily
22862306a36Sopenharmony_ci	 * inconsistent state resulting in verification failures. Hence for now
22962306a36Sopenharmony_ci	 * just avoid the verification stage for non-crc filesystems
23062306a36Sopenharmony_ci	 */
23162306a36Sopenharmony_ci	if (!xfs_has_crc(mp))
23262306a36Sopenharmony_ci		return;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
23562306a36Sopenharmony_ci	magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
23662306a36Sopenharmony_ci	magicda = be16_to_cpu(info->magic);
23762306a36Sopenharmony_ci	switch (xfs_blft_from_flags(buf_f)) {
23862306a36Sopenharmony_ci	case XFS_BLFT_BTREE_BUF:
23962306a36Sopenharmony_ci		switch (magic32) {
24062306a36Sopenharmony_ci		case XFS_ABTB_CRC_MAGIC:
24162306a36Sopenharmony_ci		case XFS_ABTB_MAGIC:
24262306a36Sopenharmony_ci			bp->b_ops = &xfs_bnobt_buf_ops;
24362306a36Sopenharmony_ci			break;
24462306a36Sopenharmony_ci		case XFS_ABTC_CRC_MAGIC:
24562306a36Sopenharmony_ci		case XFS_ABTC_MAGIC:
24662306a36Sopenharmony_ci			bp->b_ops = &xfs_cntbt_buf_ops;
24762306a36Sopenharmony_ci			break;
24862306a36Sopenharmony_ci		case XFS_IBT_CRC_MAGIC:
24962306a36Sopenharmony_ci		case XFS_IBT_MAGIC:
25062306a36Sopenharmony_ci			bp->b_ops = &xfs_inobt_buf_ops;
25162306a36Sopenharmony_ci			break;
25262306a36Sopenharmony_ci		case XFS_FIBT_CRC_MAGIC:
25362306a36Sopenharmony_ci		case XFS_FIBT_MAGIC:
25462306a36Sopenharmony_ci			bp->b_ops = &xfs_finobt_buf_ops;
25562306a36Sopenharmony_ci			break;
25662306a36Sopenharmony_ci		case XFS_BMAP_CRC_MAGIC:
25762306a36Sopenharmony_ci		case XFS_BMAP_MAGIC:
25862306a36Sopenharmony_ci			bp->b_ops = &xfs_bmbt_buf_ops;
25962306a36Sopenharmony_ci			break;
26062306a36Sopenharmony_ci		case XFS_RMAP_CRC_MAGIC:
26162306a36Sopenharmony_ci			bp->b_ops = &xfs_rmapbt_buf_ops;
26262306a36Sopenharmony_ci			break;
26362306a36Sopenharmony_ci		case XFS_REFC_CRC_MAGIC:
26462306a36Sopenharmony_ci			bp->b_ops = &xfs_refcountbt_buf_ops;
26562306a36Sopenharmony_ci			break;
26662306a36Sopenharmony_ci		default:
26762306a36Sopenharmony_ci			warnmsg = "Bad btree block magic!";
26862306a36Sopenharmony_ci			break;
26962306a36Sopenharmony_ci		}
27062306a36Sopenharmony_ci		break;
27162306a36Sopenharmony_ci	case XFS_BLFT_AGF_BUF:
27262306a36Sopenharmony_ci		if (magic32 != XFS_AGF_MAGIC) {
27362306a36Sopenharmony_ci			warnmsg = "Bad AGF block magic!";
27462306a36Sopenharmony_ci			break;
27562306a36Sopenharmony_ci		}
27662306a36Sopenharmony_ci		bp->b_ops = &xfs_agf_buf_ops;
27762306a36Sopenharmony_ci		break;
27862306a36Sopenharmony_ci	case XFS_BLFT_AGFL_BUF:
27962306a36Sopenharmony_ci		if (magic32 != XFS_AGFL_MAGIC) {
28062306a36Sopenharmony_ci			warnmsg = "Bad AGFL block magic!";
28162306a36Sopenharmony_ci			break;
28262306a36Sopenharmony_ci		}
28362306a36Sopenharmony_ci		bp->b_ops = &xfs_agfl_buf_ops;
28462306a36Sopenharmony_ci		break;
28562306a36Sopenharmony_ci	case XFS_BLFT_AGI_BUF:
28662306a36Sopenharmony_ci		if (magic32 != XFS_AGI_MAGIC) {
28762306a36Sopenharmony_ci			warnmsg = "Bad AGI block magic!";
28862306a36Sopenharmony_ci			break;
28962306a36Sopenharmony_ci		}
29062306a36Sopenharmony_ci		bp->b_ops = &xfs_agi_buf_ops;
29162306a36Sopenharmony_ci		break;
29262306a36Sopenharmony_ci	case XFS_BLFT_UDQUOT_BUF:
29362306a36Sopenharmony_ci	case XFS_BLFT_PDQUOT_BUF:
29462306a36Sopenharmony_ci	case XFS_BLFT_GDQUOT_BUF:
29562306a36Sopenharmony_ci#ifdef CONFIG_XFS_QUOTA
29662306a36Sopenharmony_ci		if (magic16 != XFS_DQUOT_MAGIC) {
29762306a36Sopenharmony_ci			warnmsg = "Bad DQUOT block magic!";
29862306a36Sopenharmony_ci			break;
29962306a36Sopenharmony_ci		}
30062306a36Sopenharmony_ci		bp->b_ops = &xfs_dquot_buf_ops;
30162306a36Sopenharmony_ci#else
30262306a36Sopenharmony_ci		xfs_alert(mp,
30362306a36Sopenharmony_ci	"Trying to recover dquots without QUOTA support built in!");
30462306a36Sopenharmony_ci		ASSERT(0);
30562306a36Sopenharmony_ci#endif
30662306a36Sopenharmony_ci		break;
30762306a36Sopenharmony_ci	case XFS_BLFT_DINO_BUF:
30862306a36Sopenharmony_ci		if (magic16 != XFS_DINODE_MAGIC) {
30962306a36Sopenharmony_ci			warnmsg = "Bad INODE block magic!";
31062306a36Sopenharmony_ci			break;
31162306a36Sopenharmony_ci		}
31262306a36Sopenharmony_ci		bp->b_ops = &xfs_inode_buf_ops;
31362306a36Sopenharmony_ci		break;
31462306a36Sopenharmony_ci	case XFS_BLFT_SYMLINK_BUF:
31562306a36Sopenharmony_ci		if (magic32 != XFS_SYMLINK_MAGIC) {
31662306a36Sopenharmony_ci			warnmsg = "Bad symlink block magic!";
31762306a36Sopenharmony_ci			break;
31862306a36Sopenharmony_ci		}
31962306a36Sopenharmony_ci		bp->b_ops = &xfs_symlink_buf_ops;
32062306a36Sopenharmony_ci		break;
32162306a36Sopenharmony_ci	case XFS_BLFT_DIR_BLOCK_BUF:
32262306a36Sopenharmony_ci		if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
32362306a36Sopenharmony_ci		    magic32 != XFS_DIR3_BLOCK_MAGIC) {
32462306a36Sopenharmony_ci			warnmsg = "Bad dir block magic!";
32562306a36Sopenharmony_ci			break;
32662306a36Sopenharmony_ci		}
32762306a36Sopenharmony_ci		bp->b_ops = &xfs_dir3_block_buf_ops;
32862306a36Sopenharmony_ci		break;
32962306a36Sopenharmony_ci	case XFS_BLFT_DIR_DATA_BUF:
33062306a36Sopenharmony_ci		if (magic32 != XFS_DIR2_DATA_MAGIC &&
33162306a36Sopenharmony_ci		    magic32 != XFS_DIR3_DATA_MAGIC) {
33262306a36Sopenharmony_ci			warnmsg = "Bad dir data magic!";
33362306a36Sopenharmony_ci			break;
33462306a36Sopenharmony_ci		}
33562306a36Sopenharmony_ci		bp->b_ops = &xfs_dir3_data_buf_ops;
33662306a36Sopenharmony_ci		break;
33762306a36Sopenharmony_ci	case XFS_BLFT_DIR_FREE_BUF:
33862306a36Sopenharmony_ci		if (magic32 != XFS_DIR2_FREE_MAGIC &&
33962306a36Sopenharmony_ci		    magic32 != XFS_DIR3_FREE_MAGIC) {
34062306a36Sopenharmony_ci			warnmsg = "Bad dir3 free magic!";
34162306a36Sopenharmony_ci			break;
34262306a36Sopenharmony_ci		}
34362306a36Sopenharmony_ci		bp->b_ops = &xfs_dir3_free_buf_ops;
34462306a36Sopenharmony_ci		break;
34562306a36Sopenharmony_ci	case XFS_BLFT_DIR_LEAF1_BUF:
34662306a36Sopenharmony_ci		if (magicda != XFS_DIR2_LEAF1_MAGIC &&
34762306a36Sopenharmony_ci		    magicda != XFS_DIR3_LEAF1_MAGIC) {
34862306a36Sopenharmony_ci			warnmsg = "Bad dir leaf1 magic!";
34962306a36Sopenharmony_ci			break;
35062306a36Sopenharmony_ci		}
35162306a36Sopenharmony_ci		bp->b_ops = &xfs_dir3_leaf1_buf_ops;
35262306a36Sopenharmony_ci		break;
35362306a36Sopenharmony_ci	case XFS_BLFT_DIR_LEAFN_BUF:
35462306a36Sopenharmony_ci		if (magicda != XFS_DIR2_LEAFN_MAGIC &&
35562306a36Sopenharmony_ci		    magicda != XFS_DIR3_LEAFN_MAGIC) {
35662306a36Sopenharmony_ci			warnmsg = "Bad dir leafn magic!";
35762306a36Sopenharmony_ci			break;
35862306a36Sopenharmony_ci		}
35962306a36Sopenharmony_ci		bp->b_ops = &xfs_dir3_leafn_buf_ops;
36062306a36Sopenharmony_ci		break;
36162306a36Sopenharmony_ci	case XFS_BLFT_DA_NODE_BUF:
36262306a36Sopenharmony_ci		if (magicda != XFS_DA_NODE_MAGIC &&
36362306a36Sopenharmony_ci		    magicda != XFS_DA3_NODE_MAGIC) {
36462306a36Sopenharmony_ci			warnmsg = "Bad da node magic!";
36562306a36Sopenharmony_ci			break;
36662306a36Sopenharmony_ci		}
36762306a36Sopenharmony_ci		bp->b_ops = &xfs_da3_node_buf_ops;
36862306a36Sopenharmony_ci		break;
36962306a36Sopenharmony_ci	case XFS_BLFT_ATTR_LEAF_BUF:
37062306a36Sopenharmony_ci		if (magicda != XFS_ATTR_LEAF_MAGIC &&
37162306a36Sopenharmony_ci		    magicda != XFS_ATTR3_LEAF_MAGIC) {
37262306a36Sopenharmony_ci			warnmsg = "Bad attr leaf magic!";
37362306a36Sopenharmony_ci			break;
37462306a36Sopenharmony_ci		}
37562306a36Sopenharmony_ci		bp->b_ops = &xfs_attr3_leaf_buf_ops;
37662306a36Sopenharmony_ci		break;
37762306a36Sopenharmony_ci	case XFS_BLFT_ATTR_RMT_BUF:
37862306a36Sopenharmony_ci		if (magic32 != XFS_ATTR3_RMT_MAGIC) {
37962306a36Sopenharmony_ci			warnmsg = "Bad attr remote magic!";
38062306a36Sopenharmony_ci			break;
38162306a36Sopenharmony_ci		}
38262306a36Sopenharmony_ci		bp->b_ops = &xfs_attr3_rmt_buf_ops;
38362306a36Sopenharmony_ci		break;
38462306a36Sopenharmony_ci	case XFS_BLFT_SB_BUF:
38562306a36Sopenharmony_ci		if (magic32 != XFS_SB_MAGIC) {
38662306a36Sopenharmony_ci			warnmsg = "Bad SB block magic!";
38762306a36Sopenharmony_ci			break;
38862306a36Sopenharmony_ci		}
38962306a36Sopenharmony_ci		bp->b_ops = &xfs_sb_buf_ops;
39062306a36Sopenharmony_ci		break;
39162306a36Sopenharmony_ci#ifdef CONFIG_XFS_RT
39262306a36Sopenharmony_ci	case XFS_BLFT_RTBITMAP_BUF:
39362306a36Sopenharmony_ci	case XFS_BLFT_RTSUMMARY_BUF:
39462306a36Sopenharmony_ci		/* no magic numbers for verification of RT buffers */
39562306a36Sopenharmony_ci		bp->b_ops = &xfs_rtbuf_ops;
39662306a36Sopenharmony_ci		break;
39762306a36Sopenharmony_ci#endif /* CONFIG_XFS_RT */
39862306a36Sopenharmony_ci	default:
39962306a36Sopenharmony_ci		xfs_warn(mp, "Unknown buffer type %d!",
40062306a36Sopenharmony_ci			 xfs_blft_from_flags(buf_f));
40162306a36Sopenharmony_ci		break;
40262306a36Sopenharmony_ci	}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	/*
40562306a36Sopenharmony_ci	 * Nothing else to do in the case of a NULL current LSN as this means
40662306a36Sopenharmony_ci	 * the buffer is more recent than the change in the log and will be
40762306a36Sopenharmony_ci	 * skipped.
40862306a36Sopenharmony_ci	 */
40962306a36Sopenharmony_ci	if (current_lsn == NULLCOMMITLSN)
41062306a36Sopenharmony_ci		return;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	if (warnmsg) {
41362306a36Sopenharmony_ci		xfs_warn(mp, warnmsg);
41462306a36Sopenharmony_ci		ASSERT(0);
41562306a36Sopenharmony_ci	}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	/*
41862306a36Sopenharmony_ci	 * We must update the metadata LSN of the buffer as it is written out to
41962306a36Sopenharmony_ci	 * ensure that older transactions never replay over this one and corrupt
42062306a36Sopenharmony_ci	 * the buffer. This can occur if log recovery is interrupted at some
42162306a36Sopenharmony_ci	 * point after the current transaction completes, at which point a
42262306a36Sopenharmony_ci	 * subsequent mount starts recovery from the beginning.
42362306a36Sopenharmony_ci	 *
42462306a36Sopenharmony_ci	 * Write verifiers update the metadata LSN from log items attached to
42562306a36Sopenharmony_ci	 * the buffer. Therefore, initialize a bli purely to carry the LSN to
42662306a36Sopenharmony_ci	 * the verifier.
42762306a36Sopenharmony_ci	 */
42862306a36Sopenharmony_ci	if (bp->b_ops) {
42962306a36Sopenharmony_ci		struct xfs_buf_log_item	*bip;
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci		bp->b_flags |= _XBF_LOGRECOVERY;
43262306a36Sopenharmony_ci		xfs_buf_item_init(bp, mp);
43362306a36Sopenharmony_ci		bip = bp->b_log_item;
43462306a36Sopenharmony_ci		bip->bli_item.li_lsn = current_lsn;
43562306a36Sopenharmony_ci	}
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci/*
43962306a36Sopenharmony_ci * Perform a 'normal' buffer recovery.  Each logged region of the
44062306a36Sopenharmony_ci * buffer should be copied over the corresponding region in the
44162306a36Sopenharmony_ci * given buffer.  The bitmap in the buf log format structure indicates
44262306a36Sopenharmony_ci * where to place the logged data.
44362306a36Sopenharmony_ci */
44462306a36Sopenharmony_ciSTATIC void
44562306a36Sopenharmony_cixlog_recover_do_reg_buffer(
44662306a36Sopenharmony_ci	struct xfs_mount		*mp,
44762306a36Sopenharmony_ci	struct xlog_recover_item	*item,
44862306a36Sopenharmony_ci	struct xfs_buf			*bp,
44962306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f,
45062306a36Sopenharmony_ci	xfs_lsn_t			current_lsn)
45162306a36Sopenharmony_ci{
45262306a36Sopenharmony_ci	int			i;
45362306a36Sopenharmony_ci	int			bit;
45462306a36Sopenharmony_ci	int			nbits;
45562306a36Sopenharmony_ci	xfs_failaddr_t		fa;
45662306a36Sopenharmony_ci	const size_t		size_disk_dquot = sizeof(struct xfs_disk_dquot);
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	bit = 0;
46162306a36Sopenharmony_ci	i = 1;  /* 0 is the buf format structure */
46262306a36Sopenharmony_ci	while (1) {
46362306a36Sopenharmony_ci		bit = xfs_next_bit(buf_f->blf_data_map,
46462306a36Sopenharmony_ci				   buf_f->blf_map_size, bit);
46562306a36Sopenharmony_ci		if (bit == -1)
46662306a36Sopenharmony_ci			break;
46762306a36Sopenharmony_ci		nbits = xfs_contig_bits(buf_f->blf_data_map,
46862306a36Sopenharmony_ci					buf_f->blf_map_size, bit);
46962306a36Sopenharmony_ci		ASSERT(nbits > 0);
47062306a36Sopenharmony_ci		ASSERT(item->ri_buf[i].i_addr != NULL);
47162306a36Sopenharmony_ci		ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
47262306a36Sopenharmony_ci		ASSERT(BBTOB(bp->b_length) >=
47362306a36Sopenharmony_ci		       ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci		/*
47662306a36Sopenharmony_ci		 * The dirty regions logged in the buffer, even though
47762306a36Sopenharmony_ci		 * contiguous, may span multiple chunks. This is because the
47862306a36Sopenharmony_ci		 * dirty region may span a physical page boundary in a buffer
47962306a36Sopenharmony_ci		 * and hence be split into two separate vectors for writing into
48062306a36Sopenharmony_ci		 * the log. Hence we need to trim nbits back to the length of
48162306a36Sopenharmony_ci		 * the current region being copied out of the log.
48262306a36Sopenharmony_ci		 */
48362306a36Sopenharmony_ci		if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
48462306a36Sopenharmony_ci			nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci		/*
48762306a36Sopenharmony_ci		 * Do a sanity check if this is a dquot buffer. Just checking
48862306a36Sopenharmony_ci		 * the first dquot in the buffer should do. XXXThis is
48962306a36Sopenharmony_ci		 * probably a good thing to do for other buf types also.
49062306a36Sopenharmony_ci		 */
49162306a36Sopenharmony_ci		fa = NULL;
49262306a36Sopenharmony_ci		if (buf_f->blf_flags &
49362306a36Sopenharmony_ci		   (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
49462306a36Sopenharmony_ci			if (item->ri_buf[i].i_addr == NULL) {
49562306a36Sopenharmony_ci				xfs_alert(mp,
49662306a36Sopenharmony_ci					"XFS: NULL dquot in %s.", __func__);
49762306a36Sopenharmony_ci				goto next;
49862306a36Sopenharmony_ci			}
49962306a36Sopenharmony_ci			if (item->ri_buf[i].i_len < size_disk_dquot) {
50062306a36Sopenharmony_ci				xfs_alert(mp,
50162306a36Sopenharmony_ci					"XFS: dquot too small (%d) in %s.",
50262306a36Sopenharmony_ci					item->ri_buf[i].i_len, __func__);
50362306a36Sopenharmony_ci				goto next;
50462306a36Sopenharmony_ci			}
50562306a36Sopenharmony_ci			fa = xfs_dquot_verify(mp, item->ri_buf[i].i_addr, -1);
50662306a36Sopenharmony_ci			if (fa) {
50762306a36Sopenharmony_ci				xfs_alert(mp,
50862306a36Sopenharmony_ci	"dquot corrupt at %pS trying to replay into block 0x%llx",
50962306a36Sopenharmony_ci					fa, xfs_buf_daddr(bp));
51062306a36Sopenharmony_ci				goto next;
51162306a36Sopenharmony_ci			}
51262306a36Sopenharmony_ci		}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci		memcpy(xfs_buf_offset(bp,
51562306a36Sopenharmony_ci			(uint)bit << XFS_BLF_SHIFT),	/* dest */
51662306a36Sopenharmony_ci			item->ri_buf[i].i_addr,		/* source */
51762306a36Sopenharmony_ci			nbits<<XFS_BLF_SHIFT);		/* length */
51862306a36Sopenharmony_ci next:
51962306a36Sopenharmony_ci		i++;
52062306a36Sopenharmony_ci		bit += nbits;
52162306a36Sopenharmony_ci	}
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	/* Shouldn't be any more regions */
52462306a36Sopenharmony_ci	ASSERT(i == item->ri_total);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
52762306a36Sopenharmony_ci}
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci/*
53062306a36Sopenharmony_ci * Perform a dquot buffer recovery.
53162306a36Sopenharmony_ci * Simple algorithm: if we have found a QUOTAOFF log item of the same type
53262306a36Sopenharmony_ci * (ie. USR or GRP), then just toss this buffer away; don't recover it.
53362306a36Sopenharmony_ci * Else, treat it as a regular buffer and do recovery.
53462306a36Sopenharmony_ci *
53562306a36Sopenharmony_ci * Return false if the buffer was tossed and true if we recovered the buffer to
53662306a36Sopenharmony_ci * indicate to the caller if the buffer needs writing.
53762306a36Sopenharmony_ci */
53862306a36Sopenharmony_ciSTATIC bool
53962306a36Sopenharmony_cixlog_recover_do_dquot_buffer(
54062306a36Sopenharmony_ci	struct xfs_mount		*mp,
54162306a36Sopenharmony_ci	struct xlog			*log,
54262306a36Sopenharmony_ci	struct xlog_recover_item	*item,
54362306a36Sopenharmony_ci	struct xfs_buf			*bp,
54462306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f)
54562306a36Sopenharmony_ci{
54662306a36Sopenharmony_ci	uint			type;
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	/*
55162306a36Sopenharmony_ci	 * Filesystems are required to send in quota flags at mount time.
55262306a36Sopenharmony_ci	 */
55362306a36Sopenharmony_ci	if (!mp->m_qflags)
55462306a36Sopenharmony_ci		return false;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	type = 0;
55762306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
55862306a36Sopenharmony_ci		type |= XFS_DQTYPE_USER;
55962306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
56062306a36Sopenharmony_ci		type |= XFS_DQTYPE_PROJ;
56162306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
56262306a36Sopenharmony_ci		type |= XFS_DQTYPE_GROUP;
56362306a36Sopenharmony_ci	/*
56462306a36Sopenharmony_ci	 * This type of quotas was turned off, so ignore this buffer
56562306a36Sopenharmony_ci	 */
56662306a36Sopenharmony_ci	if (log->l_quotaoffs_flag & type)
56762306a36Sopenharmony_ci		return false;
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
57062306a36Sopenharmony_ci	return true;
57162306a36Sopenharmony_ci}
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci/*
57462306a36Sopenharmony_ci * Perform recovery for a buffer full of inodes.  In these buffers, the only
57562306a36Sopenharmony_ci * data which should be recovered is that which corresponds to the
57662306a36Sopenharmony_ci * di_next_unlinked pointers in the on disk inode structures.  The rest of the
57762306a36Sopenharmony_ci * data for the inodes is always logged through the inodes themselves rather
57862306a36Sopenharmony_ci * than the inode buffer and is recovered in xlog_recover_inode_pass2().
57962306a36Sopenharmony_ci *
58062306a36Sopenharmony_ci * The only time when buffers full of inodes are fully recovered is when the
58162306a36Sopenharmony_ci * buffer is full of newly allocated inodes.  In this case the buffer will
58262306a36Sopenharmony_ci * not be marked as an inode buffer and so will be sent to
58362306a36Sopenharmony_ci * xlog_recover_do_reg_buffer() below during recovery.
58462306a36Sopenharmony_ci */
58562306a36Sopenharmony_ciSTATIC int
58662306a36Sopenharmony_cixlog_recover_do_inode_buffer(
58762306a36Sopenharmony_ci	struct xfs_mount		*mp,
58862306a36Sopenharmony_ci	struct xlog_recover_item	*item,
58962306a36Sopenharmony_ci	struct xfs_buf			*bp,
59062306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f)
59162306a36Sopenharmony_ci{
59262306a36Sopenharmony_ci	int				i;
59362306a36Sopenharmony_ci	int				item_index = 0;
59462306a36Sopenharmony_ci	int				bit = 0;
59562306a36Sopenharmony_ci	int				nbits = 0;
59662306a36Sopenharmony_ci	int				reg_buf_offset = 0;
59762306a36Sopenharmony_ci	int				reg_buf_bytes = 0;
59862306a36Sopenharmony_ci	int				next_unlinked_offset;
59962306a36Sopenharmony_ci	int				inodes_per_buf;
60062306a36Sopenharmony_ci	xfs_agino_t			*logged_nextp;
60162306a36Sopenharmony_ci	xfs_agino_t			*buffer_nextp;
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	/*
60662306a36Sopenharmony_ci	 * Post recovery validation only works properly on CRC enabled
60762306a36Sopenharmony_ci	 * filesystems.
60862306a36Sopenharmony_ci	 */
60962306a36Sopenharmony_ci	if (xfs_has_crc(mp))
61062306a36Sopenharmony_ci		bp->b_ops = &xfs_inode_buf_ops;
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci	inodes_per_buf = BBTOB(bp->b_length) >> mp->m_sb.sb_inodelog;
61362306a36Sopenharmony_ci	for (i = 0; i < inodes_per_buf; i++) {
61462306a36Sopenharmony_ci		next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
61562306a36Sopenharmony_ci			offsetof(struct xfs_dinode, di_next_unlinked);
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci		while (next_unlinked_offset >=
61862306a36Sopenharmony_ci		       (reg_buf_offset + reg_buf_bytes)) {
61962306a36Sopenharmony_ci			/*
62062306a36Sopenharmony_ci			 * The next di_next_unlinked field is beyond
62162306a36Sopenharmony_ci			 * the current logged region.  Find the next
62262306a36Sopenharmony_ci			 * logged region that contains or is beyond
62362306a36Sopenharmony_ci			 * the current di_next_unlinked field.
62462306a36Sopenharmony_ci			 */
62562306a36Sopenharmony_ci			bit += nbits;
62662306a36Sopenharmony_ci			bit = xfs_next_bit(buf_f->blf_data_map,
62762306a36Sopenharmony_ci					   buf_f->blf_map_size, bit);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci			/*
63062306a36Sopenharmony_ci			 * If there are no more logged regions in the
63162306a36Sopenharmony_ci			 * buffer, then we're done.
63262306a36Sopenharmony_ci			 */
63362306a36Sopenharmony_ci			if (bit == -1)
63462306a36Sopenharmony_ci				return 0;
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci			nbits = xfs_contig_bits(buf_f->blf_data_map,
63762306a36Sopenharmony_ci						buf_f->blf_map_size, bit);
63862306a36Sopenharmony_ci			ASSERT(nbits > 0);
63962306a36Sopenharmony_ci			reg_buf_offset = bit << XFS_BLF_SHIFT;
64062306a36Sopenharmony_ci			reg_buf_bytes = nbits << XFS_BLF_SHIFT;
64162306a36Sopenharmony_ci			item_index++;
64262306a36Sopenharmony_ci		}
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci		/*
64562306a36Sopenharmony_ci		 * If the current logged region starts after the current
64662306a36Sopenharmony_ci		 * di_next_unlinked field, then move on to the next
64762306a36Sopenharmony_ci		 * di_next_unlinked field.
64862306a36Sopenharmony_ci		 */
64962306a36Sopenharmony_ci		if (next_unlinked_offset < reg_buf_offset)
65062306a36Sopenharmony_ci			continue;
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci		ASSERT(item->ri_buf[item_index].i_addr != NULL);
65362306a36Sopenharmony_ci		ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
65462306a36Sopenharmony_ci		ASSERT((reg_buf_offset + reg_buf_bytes) <= BBTOB(bp->b_length));
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci		/*
65762306a36Sopenharmony_ci		 * The current logged region contains a copy of the
65862306a36Sopenharmony_ci		 * current di_next_unlinked field.  Extract its value
65962306a36Sopenharmony_ci		 * and copy it to the buffer copy.
66062306a36Sopenharmony_ci		 */
66162306a36Sopenharmony_ci		logged_nextp = item->ri_buf[item_index].i_addr +
66262306a36Sopenharmony_ci				next_unlinked_offset - reg_buf_offset;
66362306a36Sopenharmony_ci		if (XFS_IS_CORRUPT(mp, *logged_nextp == 0)) {
66462306a36Sopenharmony_ci			xfs_alert(mp,
66562306a36Sopenharmony_ci		"Bad inode buffer log record (ptr = "PTR_FMT", bp = "PTR_FMT"). "
66662306a36Sopenharmony_ci		"Trying to replay bad (0) inode di_next_unlinked field.",
66762306a36Sopenharmony_ci				item, bp);
66862306a36Sopenharmony_ci			return -EFSCORRUPTED;
66962306a36Sopenharmony_ci		}
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci		buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
67262306a36Sopenharmony_ci		*buffer_nextp = *logged_nextp;
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci		/*
67562306a36Sopenharmony_ci		 * If necessary, recalculate the CRC in the on-disk inode. We
67662306a36Sopenharmony_ci		 * have to leave the inode in a consistent state for whoever
67762306a36Sopenharmony_ci		 * reads it next....
67862306a36Sopenharmony_ci		 */
67962306a36Sopenharmony_ci		xfs_dinode_calc_crc(mp,
68062306a36Sopenharmony_ci				xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci	}
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci	return 0;
68562306a36Sopenharmony_ci}
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci/*
68862306a36Sopenharmony_ci * V5 filesystems know the age of the buffer on disk being recovered. We can
68962306a36Sopenharmony_ci * have newer objects on disk than we are replaying, and so for these cases we
69062306a36Sopenharmony_ci * don't want to replay the current change as that will make the buffer contents
69162306a36Sopenharmony_ci * temporarily invalid on disk.
69262306a36Sopenharmony_ci *
69362306a36Sopenharmony_ci * The magic number might not match the buffer type we are going to recover
69462306a36Sopenharmony_ci * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags.  Hence
69562306a36Sopenharmony_ci * extract the LSN of the existing object in the buffer based on it's current
69662306a36Sopenharmony_ci * magic number.  If we don't recognise the magic number in the buffer, then
69762306a36Sopenharmony_ci * return a LSN of -1 so that the caller knows it was an unrecognised block and
69862306a36Sopenharmony_ci * so can recover the buffer.
69962306a36Sopenharmony_ci *
70062306a36Sopenharmony_ci * Note: we cannot rely solely on magic number matches to determine that the
70162306a36Sopenharmony_ci * buffer has a valid LSN - we also need to verify that it belongs to this
70262306a36Sopenharmony_ci * filesystem, so we need to extract the object's LSN and compare it to that
70362306a36Sopenharmony_ci * which we read from the superblock. If the UUIDs don't match, then we've got a
70462306a36Sopenharmony_ci * stale metadata block from an old filesystem instance that we need to recover
70562306a36Sopenharmony_ci * over the top of.
70662306a36Sopenharmony_ci */
70762306a36Sopenharmony_cistatic xfs_lsn_t
70862306a36Sopenharmony_cixlog_recover_get_buf_lsn(
70962306a36Sopenharmony_ci	struct xfs_mount	*mp,
71062306a36Sopenharmony_ci	struct xfs_buf		*bp,
71162306a36Sopenharmony_ci	struct xfs_buf_log_format *buf_f)
71262306a36Sopenharmony_ci{
71362306a36Sopenharmony_ci	uint32_t		magic32;
71462306a36Sopenharmony_ci	uint16_t		magic16;
71562306a36Sopenharmony_ci	uint16_t		magicda;
71662306a36Sopenharmony_ci	void			*blk = bp->b_addr;
71762306a36Sopenharmony_ci	uuid_t			*uuid;
71862306a36Sopenharmony_ci	xfs_lsn_t		lsn = -1;
71962306a36Sopenharmony_ci	uint16_t		blft;
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci	/* v4 filesystems always recover immediately */
72262306a36Sopenharmony_ci	if (!xfs_has_crc(mp))
72362306a36Sopenharmony_ci		goto recover_immediately;
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	/*
72662306a36Sopenharmony_ci	 * realtime bitmap and summary file blocks do not have magic numbers or
72762306a36Sopenharmony_ci	 * UUIDs, so we must recover them immediately.
72862306a36Sopenharmony_ci	 */
72962306a36Sopenharmony_ci	blft = xfs_blft_from_flags(buf_f);
73062306a36Sopenharmony_ci	if (blft == XFS_BLFT_RTBITMAP_BUF || blft == XFS_BLFT_RTSUMMARY_BUF)
73162306a36Sopenharmony_ci		goto recover_immediately;
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	magic32 = be32_to_cpu(*(__be32 *)blk);
73462306a36Sopenharmony_ci	switch (magic32) {
73562306a36Sopenharmony_ci	case XFS_ABTB_CRC_MAGIC:
73662306a36Sopenharmony_ci	case XFS_ABTC_CRC_MAGIC:
73762306a36Sopenharmony_ci	case XFS_ABTB_MAGIC:
73862306a36Sopenharmony_ci	case XFS_ABTC_MAGIC:
73962306a36Sopenharmony_ci	case XFS_RMAP_CRC_MAGIC:
74062306a36Sopenharmony_ci	case XFS_REFC_CRC_MAGIC:
74162306a36Sopenharmony_ci	case XFS_FIBT_CRC_MAGIC:
74262306a36Sopenharmony_ci	case XFS_FIBT_MAGIC:
74362306a36Sopenharmony_ci	case XFS_IBT_CRC_MAGIC:
74462306a36Sopenharmony_ci	case XFS_IBT_MAGIC: {
74562306a36Sopenharmony_ci		struct xfs_btree_block *btb = blk;
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci		lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
74862306a36Sopenharmony_ci		uuid = &btb->bb_u.s.bb_uuid;
74962306a36Sopenharmony_ci		break;
75062306a36Sopenharmony_ci	}
75162306a36Sopenharmony_ci	case XFS_BMAP_CRC_MAGIC:
75262306a36Sopenharmony_ci	case XFS_BMAP_MAGIC: {
75362306a36Sopenharmony_ci		struct xfs_btree_block *btb = blk;
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci		lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
75662306a36Sopenharmony_ci		uuid = &btb->bb_u.l.bb_uuid;
75762306a36Sopenharmony_ci		break;
75862306a36Sopenharmony_ci	}
75962306a36Sopenharmony_ci	case XFS_AGF_MAGIC:
76062306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
76162306a36Sopenharmony_ci		uuid = &((struct xfs_agf *)blk)->agf_uuid;
76262306a36Sopenharmony_ci		break;
76362306a36Sopenharmony_ci	case XFS_AGFL_MAGIC:
76462306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
76562306a36Sopenharmony_ci		uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
76662306a36Sopenharmony_ci		break;
76762306a36Sopenharmony_ci	case XFS_AGI_MAGIC:
76862306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
76962306a36Sopenharmony_ci		uuid = &((struct xfs_agi *)blk)->agi_uuid;
77062306a36Sopenharmony_ci		break;
77162306a36Sopenharmony_ci	case XFS_SYMLINK_MAGIC:
77262306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
77362306a36Sopenharmony_ci		uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
77462306a36Sopenharmony_ci		break;
77562306a36Sopenharmony_ci	case XFS_DIR3_BLOCK_MAGIC:
77662306a36Sopenharmony_ci	case XFS_DIR3_DATA_MAGIC:
77762306a36Sopenharmony_ci	case XFS_DIR3_FREE_MAGIC:
77862306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
77962306a36Sopenharmony_ci		uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
78062306a36Sopenharmony_ci		break;
78162306a36Sopenharmony_ci	case XFS_ATTR3_RMT_MAGIC:
78262306a36Sopenharmony_ci		/*
78362306a36Sopenharmony_ci		 * Remote attr blocks are written synchronously, rather than
78462306a36Sopenharmony_ci		 * being logged. That means they do not contain a valid LSN
78562306a36Sopenharmony_ci		 * (i.e. transactionally ordered) in them, and hence any time we
78662306a36Sopenharmony_ci		 * see a buffer to replay over the top of a remote attribute
78762306a36Sopenharmony_ci		 * block we should simply do so.
78862306a36Sopenharmony_ci		 */
78962306a36Sopenharmony_ci		goto recover_immediately;
79062306a36Sopenharmony_ci	case XFS_SB_MAGIC:
79162306a36Sopenharmony_ci		/*
79262306a36Sopenharmony_ci		 * superblock uuids are magic. We may or may not have a
79362306a36Sopenharmony_ci		 * sb_meta_uuid on disk, but it will be set in the in-core
79462306a36Sopenharmony_ci		 * superblock. We set the uuid pointer for verification
79562306a36Sopenharmony_ci		 * according to the superblock feature mask to ensure we check
79662306a36Sopenharmony_ci		 * the relevant UUID in the superblock.
79762306a36Sopenharmony_ci		 */
79862306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
79962306a36Sopenharmony_ci		if (xfs_has_metauuid(mp))
80062306a36Sopenharmony_ci			uuid = &((struct xfs_dsb *)blk)->sb_meta_uuid;
80162306a36Sopenharmony_ci		else
80262306a36Sopenharmony_ci			uuid = &((struct xfs_dsb *)blk)->sb_uuid;
80362306a36Sopenharmony_ci		break;
80462306a36Sopenharmony_ci	default:
80562306a36Sopenharmony_ci		break;
80662306a36Sopenharmony_ci	}
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_ci	if (lsn != (xfs_lsn_t)-1) {
80962306a36Sopenharmony_ci		if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
81062306a36Sopenharmony_ci			goto recover_immediately;
81162306a36Sopenharmony_ci		return lsn;
81262306a36Sopenharmony_ci	}
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ci	magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
81562306a36Sopenharmony_ci	switch (magicda) {
81662306a36Sopenharmony_ci	case XFS_DIR3_LEAF1_MAGIC:
81762306a36Sopenharmony_ci	case XFS_DIR3_LEAFN_MAGIC:
81862306a36Sopenharmony_ci	case XFS_ATTR3_LEAF_MAGIC:
81962306a36Sopenharmony_ci	case XFS_DA3_NODE_MAGIC:
82062306a36Sopenharmony_ci		lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
82162306a36Sopenharmony_ci		uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
82262306a36Sopenharmony_ci		break;
82362306a36Sopenharmony_ci	default:
82462306a36Sopenharmony_ci		break;
82562306a36Sopenharmony_ci	}
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	if (lsn != (xfs_lsn_t)-1) {
82862306a36Sopenharmony_ci		if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
82962306a36Sopenharmony_ci			goto recover_immediately;
83062306a36Sopenharmony_ci		return lsn;
83162306a36Sopenharmony_ci	}
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci	/*
83462306a36Sopenharmony_ci	 * We do individual object checks on dquot and inode buffers as they
83562306a36Sopenharmony_ci	 * have their own individual LSN records. Also, we could have a stale
83662306a36Sopenharmony_ci	 * buffer here, so we have to at least recognise these buffer types.
83762306a36Sopenharmony_ci	 *
83862306a36Sopenharmony_ci	 * A notd complexity here is inode unlinked list processing - it logs
83962306a36Sopenharmony_ci	 * the inode directly in the buffer, but we don't know which inodes have
84062306a36Sopenharmony_ci	 * been modified, and there is no global buffer LSN. Hence we need to
84162306a36Sopenharmony_ci	 * recover all inode buffer types immediately. This problem will be
84262306a36Sopenharmony_ci	 * fixed by logical logging of the unlinked list modifications.
84362306a36Sopenharmony_ci	 */
84462306a36Sopenharmony_ci	magic16 = be16_to_cpu(*(__be16 *)blk);
84562306a36Sopenharmony_ci	switch (magic16) {
84662306a36Sopenharmony_ci	case XFS_DQUOT_MAGIC:
84762306a36Sopenharmony_ci	case XFS_DINODE_MAGIC:
84862306a36Sopenharmony_ci		goto recover_immediately;
84962306a36Sopenharmony_ci	default:
85062306a36Sopenharmony_ci		break;
85162306a36Sopenharmony_ci	}
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci	/* unknown buffer contents, recover immediately */
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_cirecover_immediately:
85662306a36Sopenharmony_ci	return (xfs_lsn_t)-1;
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci}
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci/*
86162306a36Sopenharmony_ci * This routine replays a modification made to a buffer at runtime.
86262306a36Sopenharmony_ci * There are actually two types of buffer, regular and inode, which
86362306a36Sopenharmony_ci * are handled differently.  Inode buffers are handled differently
86462306a36Sopenharmony_ci * in that we only recover a specific set of data from them, namely
86562306a36Sopenharmony_ci * the inode di_next_unlinked fields.  This is because all other inode
86662306a36Sopenharmony_ci * data is actually logged via inode records and any data we replay
86762306a36Sopenharmony_ci * here which overlaps that may be stale.
86862306a36Sopenharmony_ci *
86962306a36Sopenharmony_ci * When meta-data buffers are freed at run time we log a buffer item
87062306a36Sopenharmony_ci * with the XFS_BLF_CANCEL bit set to indicate that previous copies
87162306a36Sopenharmony_ci * of the buffer in the log should not be replayed at recovery time.
87262306a36Sopenharmony_ci * This is so that if the blocks covered by the buffer are reused for
87362306a36Sopenharmony_ci * file data before we crash we don't end up replaying old, freed
87462306a36Sopenharmony_ci * meta-data into a user's file.
87562306a36Sopenharmony_ci *
87662306a36Sopenharmony_ci * To handle the cancellation of buffer log items, we make two passes
87762306a36Sopenharmony_ci * over the log during recovery.  During the first we build a table of
87862306a36Sopenharmony_ci * those buffers which have been cancelled, and during the second we
87962306a36Sopenharmony_ci * only replay those buffers which do not have corresponding cancel
88062306a36Sopenharmony_ci * records in the table.  See xlog_recover_buf_pass[1,2] above
88162306a36Sopenharmony_ci * for more details on the implementation of the table of cancel records.
88262306a36Sopenharmony_ci */
88362306a36Sopenharmony_ciSTATIC int
88462306a36Sopenharmony_cixlog_recover_buf_commit_pass2(
88562306a36Sopenharmony_ci	struct xlog			*log,
88662306a36Sopenharmony_ci	struct list_head		*buffer_list,
88762306a36Sopenharmony_ci	struct xlog_recover_item	*item,
88862306a36Sopenharmony_ci	xfs_lsn_t			current_lsn)
88962306a36Sopenharmony_ci{
89062306a36Sopenharmony_ci	struct xfs_buf_log_format	*buf_f = item->ri_buf[0].i_addr;
89162306a36Sopenharmony_ci	struct xfs_mount		*mp = log->l_mp;
89262306a36Sopenharmony_ci	struct xfs_buf			*bp;
89362306a36Sopenharmony_ci	int				error;
89462306a36Sopenharmony_ci	uint				buf_flags;
89562306a36Sopenharmony_ci	xfs_lsn_t			lsn;
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_ci	/*
89862306a36Sopenharmony_ci	 * In this pass we only want to recover all the buffers which have
89962306a36Sopenharmony_ci	 * not been cancelled and are not cancellation buffers themselves.
90062306a36Sopenharmony_ci	 */
90162306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_CANCEL) {
90262306a36Sopenharmony_ci		if (xlog_put_buffer_cancelled(log, buf_f->blf_blkno,
90362306a36Sopenharmony_ci				buf_f->blf_len))
90462306a36Sopenharmony_ci			goto cancelled;
90562306a36Sopenharmony_ci	} else {
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci		if (xlog_is_buffer_cancelled(log, buf_f->blf_blkno,
90862306a36Sopenharmony_ci				buf_f->blf_len))
90962306a36Sopenharmony_ci			goto cancelled;
91062306a36Sopenharmony_ci	}
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci	trace_xfs_log_recover_buf_recover(log, buf_f);
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	buf_flags = 0;
91562306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
91662306a36Sopenharmony_ci		buf_flags |= XBF_UNMAPPED;
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_ci	error = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
91962306a36Sopenharmony_ci			  buf_flags, &bp, NULL);
92062306a36Sopenharmony_ci	if (error)
92162306a36Sopenharmony_ci		return error;
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_ci	/*
92462306a36Sopenharmony_ci	 * Recover the buffer only if we get an LSN from it and it's less than
92562306a36Sopenharmony_ci	 * the lsn of the transaction we are replaying.
92662306a36Sopenharmony_ci	 *
92762306a36Sopenharmony_ci	 * Note that we have to be extremely careful of readahead here.
92862306a36Sopenharmony_ci	 * Readahead does not attach verfiers to the buffers so if we don't
92962306a36Sopenharmony_ci	 * actually do any replay after readahead because of the LSN we found
93062306a36Sopenharmony_ci	 * in the buffer if more recent than that current transaction then we
93162306a36Sopenharmony_ci	 * need to attach the verifier directly. Failure to do so can lead to
93262306a36Sopenharmony_ci	 * future recovery actions (e.g. EFI and unlinked list recovery) can
93362306a36Sopenharmony_ci	 * operate on the buffers and they won't get the verifier attached. This
93462306a36Sopenharmony_ci	 * can lead to blocks on disk having the correct content but a stale
93562306a36Sopenharmony_ci	 * CRC.
93662306a36Sopenharmony_ci	 *
93762306a36Sopenharmony_ci	 * It is safe to assume these clean buffers are currently up to date.
93862306a36Sopenharmony_ci	 * If the buffer is dirtied by a later transaction being replayed, then
93962306a36Sopenharmony_ci	 * the verifier will be reset to match whatever recover turns that
94062306a36Sopenharmony_ci	 * buffer into.
94162306a36Sopenharmony_ci	 */
94262306a36Sopenharmony_ci	lsn = xlog_recover_get_buf_lsn(mp, bp, buf_f);
94362306a36Sopenharmony_ci	if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
94462306a36Sopenharmony_ci		trace_xfs_log_recover_buf_skip(log, buf_f);
94562306a36Sopenharmony_ci		xlog_recover_validate_buf_type(mp, bp, buf_f, NULLCOMMITLSN);
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci		/*
94862306a36Sopenharmony_ci		 * We're skipping replay of this buffer log item due to the log
94962306a36Sopenharmony_ci		 * item LSN being behind the ondisk buffer.  Verify the buffer
95062306a36Sopenharmony_ci		 * contents since we aren't going to run the write verifier.
95162306a36Sopenharmony_ci		 */
95262306a36Sopenharmony_ci		if (bp->b_ops) {
95362306a36Sopenharmony_ci			bp->b_ops->verify_read(bp);
95462306a36Sopenharmony_ci			error = bp->b_error;
95562306a36Sopenharmony_ci		}
95662306a36Sopenharmony_ci		goto out_release;
95762306a36Sopenharmony_ci	}
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ci	if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
96062306a36Sopenharmony_ci		error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
96162306a36Sopenharmony_ci		if (error)
96262306a36Sopenharmony_ci			goto out_release;
96362306a36Sopenharmony_ci	} else if (buf_f->blf_flags &
96462306a36Sopenharmony_ci		  (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
96562306a36Sopenharmony_ci		bool	dirty;
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ci		dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
96862306a36Sopenharmony_ci		if (!dirty)
96962306a36Sopenharmony_ci			goto out_release;
97062306a36Sopenharmony_ci	} else {
97162306a36Sopenharmony_ci		xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
97262306a36Sopenharmony_ci	}
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci	/*
97562306a36Sopenharmony_ci	 * Perform delayed write on the buffer.  Asynchronous writes will be
97662306a36Sopenharmony_ci	 * slower when taking into account all the buffers to be flushed.
97762306a36Sopenharmony_ci	 *
97862306a36Sopenharmony_ci	 * Also make sure that only inode buffers with good sizes stay in
97962306a36Sopenharmony_ci	 * the buffer cache.  The kernel moves inodes in buffers of 1 block
98062306a36Sopenharmony_ci	 * or inode_cluster_size bytes, whichever is bigger.  The inode
98162306a36Sopenharmony_ci	 * buffers in the log can be a different size if the log was generated
98262306a36Sopenharmony_ci	 * by an older kernel using unclustered inode buffers or a newer kernel
98362306a36Sopenharmony_ci	 * running with a different inode cluster size.  Regardless, if
98462306a36Sopenharmony_ci	 * the inode buffer size isn't max(blocksize, inode_cluster_size)
98562306a36Sopenharmony_ci	 * for *our* value of inode_cluster_size, then we need to keep
98662306a36Sopenharmony_ci	 * the buffer out of the buffer cache so that the buffer won't
98762306a36Sopenharmony_ci	 * overlap with future reads of those inodes.
98862306a36Sopenharmony_ci	 */
98962306a36Sopenharmony_ci	if (XFS_DINODE_MAGIC ==
99062306a36Sopenharmony_ci	    be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
99162306a36Sopenharmony_ci	    (BBTOB(bp->b_length) != M_IGEO(log->l_mp)->inode_cluster_size)) {
99262306a36Sopenharmony_ci		xfs_buf_stale(bp);
99362306a36Sopenharmony_ci		error = xfs_bwrite(bp);
99462306a36Sopenharmony_ci	} else {
99562306a36Sopenharmony_ci		ASSERT(bp->b_mount == mp);
99662306a36Sopenharmony_ci		bp->b_flags |= _XBF_LOGRECOVERY;
99762306a36Sopenharmony_ci		xfs_buf_delwri_queue(bp, buffer_list);
99862306a36Sopenharmony_ci	}
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ciout_release:
100162306a36Sopenharmony_ci	xfs_buf_relse(bp);
100262306a36Sopenharmony_ci	return error;
100362306a36Sopenharmony_cicancelled:
100462306a36Sopenharmony_ci	trace_xfs_log_recover_buf_cancel(log, buf_f);
100562306a36Sopenharmony_ci	return 0;
100662306a36Sopenharmony_ci}
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_ciconst struct xlog_recover_item_ops xlog_buf_item_ops = {
100962306a36Sopenharmony_ci	.item_type		= XFS_LI_BUF,
101062306a36Sopenharmony_ci	.reorder		= xlog_recover_buf_reorder,
101162306a36Sopenharmony_ci	.ra_pass2		= xlog_recover_buf_ra_pass2,
101262306a36Sopenharmony_ci	.commit_pass1		= xlog_recover_buf_commit_pass1,
101362306a36Sopenharmony_ci	.commit_pass2		= xlog_recover_buf_commit_pass2,
101462306a36Sopenharmony_ci};
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ci#ifdef DEBUG
101762306a36Sopenharmony_civoid
101862306a36Sopenharmony_cixlog_check_buf_cancel_table(
101962306a36Sopenharmony_ci	struct xlog	*log)
102062306a36Sopenharmony_ci{
102162306a36Sopenharmony_ci	int		i;
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
102462306a36Sopenharmony_ci		ASSERT(list_empty(&log->l_buf_cancel_table[i]));
102562306a36Sopenharmony_ci}
102662306a36Sopenharmony_ci#endif
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ciint
102962306a36Sopenharmony_cixlog_alloc_buf_cancel_table(
103062306a36Sopenharmony_ci	struct xlog	*log)
103162306a36Sopenharmony_ci{
103262306a36Sopenharmony_ci	void		*p;
103362306a36Sopenharmony_ci	int		i;
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci	ASSERT(log->l_buf_cancel_table == NULL);
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	p = kmalloc_array(XLOG_BC_TABLE_SIZE, sizeof(struct list_head),
103862306a36Sopenharmony_ci			  GFP_KERNEL);
103962306a36Sopenharmony_ci	if (!p)
104062306a36Sopenharmony_ci		return -ENOMEM;
104162306a36Sopenharmony_ci
104262306a36Sopenharmony_ci	log->l_buf_cancel_table = p;
104362306a36Sopenharmony_ci	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
104462306a36Sopenharmony_ci		INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_ci	return 0;
104762306a36Sopenharmony_ci}
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_civoid
105062306a36Sopenharmony_cixlog_free_buf_cancel_table(
105162306a36Sopenharmony_ci	struct xlog	*log)
105262306a36Sopenharmony_ci{
105362306a36Sopenharmony_ci	int		i;
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci	if (!log->l_buf_cancel_table)
105662306a36Sopenharmony_ci		return;
105762306a36Sopenharmony_ci
105862306a36Sopenharmony_ci	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++) {
105962306a36Sopenharmony_ci		struct xfs_buf_cancel	*bc;
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_ci		while ((bc = list_first_entry_or_null(
106262306a36Sopenharmony_ci				&log->l_buf_cancel_table[i],
106362306a36Sopenharmony_ci				struct xfs_buf_cancel, bc_list))) {
106462306a36Sopenharmony_ci			list_del(&bc->bc_list);
106562306a36Sopenharmony_ci			kmem_free(bc);
106662306a36Sopenharmony_ci		}
106762306a36Sopenharmony_ci	}
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci	kmem_free(log->l_buf_cancel_table);
107062306a36Sopenharmony_ci	log->l_buf_cancel_table = NULL;
107162306a36Sopenharmony_ci}
1072