xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_inode_item_recover.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_mount.h"
13*4882a593Smuzhiyun #include "xfs_inode.h"
14*4882a593Smuzhiyun #include "xfs_trans.h"
15*4882a593Smuzhiyun #include "xfs_inode_item.h"
16*4882a593Smuzhiyun #include "xfs_trace.h"
17*4882a593Smuzhiyun #include "xfs_trans_priv.h"
18*4882a593Smuzhiyun #include "xfs_buf_item.h"
19*4882a593Smuzhiyun #include "xfs_log.h"
20*4882a593Smuzhiyun #include "xfs_error.h"
21*4882a593Smuzhiyun #include "xfs_log_priv.h"
22*4882a593Smuzhiyun #include "xfs_log_recover.h"
23*4882a593Smuzhiyun #include "xfs_icache.h"
24*4882a593Smuzhiyun #include "xfs_bmap_btree.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun STATIC void
xlog_recover_inode_ra_pass2(struct xlog * log,struct xlog_recover_item * item)27*4882a593Smuzhiyun xlog_recover_inode_ra_pass2(
28*4882a593Smuzhiyun 	struct xlog                     *log,
29*4882a593Smuzhiyun 	struct xlog_recover_item        *item)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
32*4882a593Smuzhiyun 		struct xfs_inode_log_format	*ilfp = item->ri_buf[0].i_addr;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 		xlog_buf_readahead(log, ilfp->ilf_blkno, ilfp->ilf_len,
35*4882a593Smuzhiyun 				   &xfs_inode_buf_ra_ops);
36*4882a593Smuzhiyun 	} else {
37*4882a593Smuzhiyun 		struct xfs_inode_log_format_32	*ilfp = item->ri_buf[0].i_addr;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 		xlog_buf_readahead(log, ilfp->ilf_blkno, ilfp->ilf_len,
40*4882a593Smuzhiyun 				   &xfs_inode_buf_ra_ops);
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * Inode fork owner changes
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * If we have been told that we have to reparent the inode fork, it's because an
48*4882a593Smuzhiyun  * extent swap operation on a CRC enabled filesystem has been done and we are
49*4882a593Smuzhiyun  * replaying it. We need to walk the BMBT of the appropriate fork and change the
50*4882a593Smuzhiyun  * owners of it.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * The complexity here is that we don't have an inode context to work with, so
53*4882a593Smuzhiyun  * after we've replayed the inode we need to instantiate one.  This is where the
54*4882a593Smuzhiyun  * fun begins.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * We are in the middle of log recovery, so we can't run transactions. That
57*4882a593Smuzhiyun  * means we cannot use cache coherent inode instantiation via xfs_iget(), as
58*4882a593Smuzhiyun  * that will result in the corresponding iput() running the inode through
59*4882a593Smuzhiyun  * xfs_inactive(). If we've just replayed an inode core that changes the link
60*4882a593Smuzhiyun  * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
61*4882a593Smuzhiyun  * transactions (bad!).
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * So, to avoid this, we instantiate an inode directly from the inode core we've
64*4882a593Smuzhiyun  * just recovered. We have the buffer still locked, and all we really need to
65*4882a593Smuzhiyun  * instantiate is the inode core and the forks being modified. We can do this
66*4882a593Smuzhiyun  * manually, then run the inode btree owner change, and then tear down the
67*4882a593Smuzhiyun  * xfs_inode without having to run any transactions at all.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * Also, because we don't have a transaction context available here but need to
70*4882a593Smuzhiyun  * gather all the buffers we modify for writeback so we pass the buffer_list
71*4882a593Smuzhiyun  * instead for the operation to use.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun STATIC int
xfs_recover_inode_owner_change(struct xfs_mount * mp,struct xfs_dinode * dip,struct xfs_inode_log_format * in_f,struct list_head * buffer_list)75*4882a593Smuzhiyun xfs_recover_inode_owner_change(
76*4882a593Smuzhiyun 	struct xfs_mount	*mp,
77*4882a593Smuzhiyun 	struct xfs_dinode	*dip,
78*4882a593Smuzhiyun 	struct xfs_inode_log_format *in_f,
79*4882a593Smuzhiyun 	struct list_head	*buffer_list)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	struct xfs_inode	*ip;
82*4882a593Smuzhiyun 	int			error;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	ip = xfs_inode_alloc(mp, in_f->ilf_ino);
87*4882a593Smuzhiyun 	if (!ip)
88*4882a593Smuzhiyun 		return -ENOMEM;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* instantiate the inode */
91*4882a593Smuzhiyun 	ASSERT(dip->di_version >= 3);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	error = xfs_inode_from_disk(ip, dip);
94*4882a593Smuzhiyun 	if (error)
95*4882a593Smuzhiyun 		goto out_free_ip;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
98*4882a593Smuzhiyun 		ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
99*4882a593Smuzhiyun 		error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
100*4882a593Smuzhiyun 					      ip->i_ino, buffer_list);
101*4882a593Smuzhiyun 		if (error)
102*4882a593Smuzhiyun 			goto out_free_ip;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
106*4882a593Smuzhiyun 		ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
107*4882a593Smuzhiyun 		error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
108*4882a593Smuzhiyun 					      ip->i_ino, buffer_list);
109*4882a593Smuzhiyun 		if (error)
110*4882a593Smuzhiyun 			goto out_free_ip;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun out_free_ip:
114*4882a593Smuzhiyun 	xfs_inode_free(ip);
115*4882a593Smuzhiyun 	return error;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
xfs_log_dinode_has_bigtime(const struct xfs_log_dinode * ld)118*4882a593Smuzhiyun static inline bool xfs_log_dinode_has_bigtime(const struct xfs_log_dinode *ld)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	return ld->di_version >= 3 &&
121*4882a593Smuzhiyun 	       (ld->di_flags2 & XFS_DIFLAG2_BIGTIME);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* Convert a log timestamp to an ondisk timestamp. */
125*4882a593Smuzhiyun static inline xfs_timestamp_t
xfs_log_dinode_to_disk_ts(struct xfs_log_dinode * from,const xfs_ictimestamp_t its)126*4882a593Smuzhiyun xfs_log_dinode_to_disk_ts(
127*4882a593Smuzhiyun 	struct xfs_log_dinode		*from,
128*4882a593Smuzhiyun 	const xfs_ictimestamp_t		its)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct xfs_legacy_timestamp	*lts;
131*4882a593Smuzhiyun 	struct xfs_legacy_ictimestamp	*lits;
132*4882a593Smuzhiyun 	xfs_timestamp_t			ts;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	if (xfs_log_dinode_has_bigtime(from))
135*4882a593Smuzhiyun 		return cpu_to_be64(its);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	lts = (struct xfs_legacy_timestamp *)&ts;
138*4882a593Smuzhiyun 	lits = (struct xfs_legacy_ictimestamp *)&its;
139*4882a593Smuzhiyun 	lts->t_sec = cpu_to_be32(lits->t_sec);
140*4882a593Smuzhiyun 	lts->t_nsec = cpu_to_be32(lits->t_nsec);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return ts;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun STATIC void
xfs_log_dinode_to_disk(struct xfs_log_dinode * from,struct xfs_dinode * to,xfs_lsn_t lsn)146*4882a593Smuzhiyun xfs_log_dinode_to_disk(
147*4882a593Smuzhiyun 	struct xfs_log_dinode	*from,
148*4882a593Smuzhiyun 	struct xfs_dinode	*to,
149*4882a593Smuzhiyun 	xfs_lsn_t		lsn)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	to->di_magic = cpu_to_be16(from->di_magic);
152*4882a593Smuzhiyun 	to->di_mode = cpu_to_be16(from->di_mode);
153*4882a593Smuzhiyun 	to->di_version = from->di_version;
154*4882a593Smuzhiyun 	to->di_format = from->di_format;
155*4882a593Smuzhiyun 	to->di_onlink = 0;
156*4882a593Smuzhiyun 	to->di_uid = cpu_to_be32(from->di_uid);
157*4882a593Smuzhiyun 	to->di_gid = cpu_to_be32(from->di_gid);
158*4882a593Smuzhiyun 	to->di_nlink = cpu_to_be32(from->di_nlink);
159*4882a593Smuzhiyun 	to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
160*4882a593Smuzhiyun 	to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
161*4882a593Smuzhiyun 	memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	to->di_atime = xfs_log_dinode_to_disk_ts(from, from->di_atime);
164*4882a593Smuzhiyun 	to->di_mtime = xfs_log_dinode_to_disk_ts(from, from->di_mtime);
165*4882a593Smuzhiyun 	to->di_ctime = xfs_log_dinode_to_disk_ts(from, from->di_ctime);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	to->di_size = cpu_to_be64(from->di_size);
168*4882a593Smuzhiyun 	to->di_nblocks = cpu_to_be64(from->di_nblocks);
169*4882a593Smuzhiyun 	to->di_extsize = cpu_to_be32(from->di_extsize);
170*4882a593Smuzhiyun 	to->di_nextents = cpu_to_be32(from->di_nextents);
171*4882a593Smuzhiyun 	to->di_anextents = cpu_to_be16(from->di_anextents);
172*4882a593Smuzhiyun 	to->di_forkoff = from->di_forkoff;
173*4882a593Smuzhiyun 	to->di_aformat = from->di_aformat;
174*4882a593Smuzhiyun 	to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
175*4882a593Smuzhiyun 	to->di_dmstate = cpu_to_be16(from->di_dmstate);
176*4882a593Smuzhiyun 	to->di_flags = cpu_to_be16(from->di_flags);
177*4882a593Smuzhiyun 	to->di_gen = cpu_to_be32(from->di_gen);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (from->di_version == 3) {
180*4882a593Smuzhiyun 		to->di_changecount = cpu_to_be64(from->di_changecount);
181*4882a593Smuzhiyun 		to->di_crtime = xfs_log_dinode_to_disk_ts(from,
182*4882a593Smuzhiyun 							  from->di_crtime);
183*4882a593Smuzhiyun 		to->di_flags2 = cpu_to_be64(from->di_flags2);
184*4882a593Smuzhiyun 		to->di_cowextsize = cpu_to_be32(from->di_cowextsize);
185*4882a593Smuzhiyun 		to->di_ino = cpu_to_be64(from->di_ino);
186*4882a593Smuzhiyun 		to->di_lsn = cpu_to_be64(lsn);
187*4882a593Smuzhiyun 		memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
188*4882a593Smuzhiyun 		uuid_copy(&to->di_uuid, &from->di_uuid);
189*4882a593Smuzhiyun 		to->di_flushiter = 0;
190*4882a593Smuzhiyun 	} else {
191*4882a593Smuzhiyun 		to->di_flushiter = cpu_to_be16(from->di_flushiter);
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun STATIC int
xlog_recover_inode_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t current_lsn)196*4882a593Smuzhiyun xlog_recover_inode_commit_pass2(
197*4882a593Smuzhiyun 	struct xlog			*log,
198*4882a593Smuzhiyun 	struct list_head		*buffer_list,
199*4882a593Smuzhiyun 	struct xlog_recover_item	*item,
200*4882a593Smuzhiyun 	xfs_lsn_t			current_lsn)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct xfs_inode_log_format	*in_f;
203*4882a593Smuzhiyun 	struct xfs_mount		*mp = log->l_mp;
204*4882a593Smuzhiyun 	struct xfs_buf			*bp;
205*4882a593Smuzhiyun 	struct xfs_dinode		*dip;
206*4882a593Smuzhiyun 	int				len;
207*4882a593Smuzhiyun 	char				*src;
208*4882a593Smuzhiyun 	char				*dest;
209*4882a593Smuzhiyun 	int				error;
210*4882a593Smuzhiyun 	int				attr_index;
211*4882a593Smuzhiyun 	uint				fields;
212*4882a593Smuzhiyun 	struct xfs_log_dinode		*ldip;
213*4882a593Smuzhiyun 	uint				isize;
214*4882a593Smuzhiyun 	int				need_free = 0;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
217*4882a593Smuzhiyun 		in_f = item->ri_buf[0].i_addr;
218*4882a593Smuzhiyun 	} else {
219*4882a593Smuzhiyun 		in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), 0);
220*4882a593Smuzhiyun 		need_free = 1;
221*4882a593Smuzhiyun 		error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
222*4882a593Smuzhiyun 		if (error)
223*4882a593Smuzhiyun 			goto error;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/*
227*4882a593Smuzhiyun 	 * Inode buffers can be freed, look out for it,
228*4882a593Smuzhiyun 	 * and do not replay the inode.
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	if (xlog_is_buffer_cancelled(log, in_f->ilf_blkno, in_f->ilf_len)) {
231*4882a593Smuzhiyun 		error = 0;
232*4882a593Smuzhiyun 		trace_xfs_log_recover_inode_cancel(log, in_f);
233*4882a593Smuzhiyun 		goto error;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 	trace_xfs_log_recover_inode_recover(log, in_f);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	error = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
238*4882a593Smuzhiyun 			0, &bp, &xfs_inode_buf_ops);
239*4882a593Smuzhiyun 	if (error)
240*4882a593Smuzhiyun 		goto error;
241*4882a593Smuzhiyun 	ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
242*4882a593Smuzhiyun 	dip = xfs_buf_offset(bp, in_f->ilf_boffset);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/*
245*4882a593Smuzhiyun 	 * Make sure the place we're flushing out to really looks
246*4882a593Smuzhiyun 	 * like an inode!
247*4882a593Smuzhiyun 	 */
248*4882a593Smuzhiyun 	if (XFS_IS_CORRUPT(mp, !xfs_verify_magic16(bp, dip->di_magic))) {
249*4882a593Smuzhiyun 		xfs_alert(mp,
250*4882a593Smuzhiyun 	"%s: Bad inode magic number, dip = "PTR_FMT", dino bp = "PTR_FMT", ino = %Ld",
251*4882a593Smuzhiyun 			__func__, dip, bp, in_f->ilf_ino);
252*4882a593Smuzhiyun 		error = -EFSCORRUPTED;
253*4882a593Smuzhiyun 		goto out_release;
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun 	ldip = item->ri_buf[1].i_addr;
256*4882a593Smuzhiyun 	if (XFS_IS_CORRUPT(mp, ldip->di_magic != XFS_DINODE_MAGIC)) {
257*4882a593Smuzhiyun 		xfs_alert(mp,
258*4882a593Smuzhiyun 			"%s: Bad inode log record, rec ptr "PTR_FMT", ino %Ld",
259*4882a593Smuzhiyun 			__func__, item, in_f->ilf_ino);
260*4882a593Smuzhiyun 		error = -EFSCORRUPTED;
261*4882a593Smuzhiyun 		goto out_release;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/*
265*4882a593Smuzhiyun 	 * If the inode has an LSN in it, recover the inode only if the on-disk
266*4882a593Smuzhiyun 	 * inode's LSN is older than the lsn of the transaction we are
267*4882a593Smuzhiyun 	 * replaying. We can have multiple checkpoints with the same start LSN,
268*4882a593Smuzhiyun 	 * so the current LSN being equal to the on-disk LSN doesn't necessarily
269*4882a593Smuzhiyun 	 * mean that the on-disk inode is more recent than the change being
270*4882a593Smuzhiyun 	 * replayed.
271*4882a593Smuzhiyun 	 *
272*4882a593Smuzhiyun 	 * We must check the current_lsn against the on-disk inode
273*4882a593Smuzhiyun 	 * here because the we can't trust the log dinode to contain a valid LSN
274*4882a593Smuzhiyun 	 * (see comment below before replaying the log dinode for details).
275*4882a593Smuzhiyun 	 *
276*4882a593Smuzhiyun 	 * Note: we still need to replay an owner change even though the inode
277*4882a593Smuzhiyun 	 * is more recent than the transaction as there is no guarantee that all
278*4882a593Smuzhiyun 	 * the btree blocks are more recent than this transaction, too.
279*4882a593Smuzhiyun 	 */
280*4882a593Smuzhiyun 	if (dip->di_version >= 3) {
281*4882a593Smuzhiyun 		xfs_lsn_t	lsn = be64_to_cpu(dip->di_lsn);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 		if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) > 0) {
284*4882a593Smuzhiyun 			trace_xfs_log_recover_inode_skip(log, in_f);
285*4882a593Smuzhiyun 			error = 0;
286*4882a593Smuzhiyun 			goto out_owner_change;
287*4882a593Smuzhiyun 		}
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/*
291*4882a593Smuzhiyun 	 * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
292*4882a593Smuzhiyun 	 * are transactional and if ordering is necessary we can determine that
293*4882a593Smuzhiyun 	 * more accurately by the LSN field in the V3 inode core. Don't trust
294*4882a593Smuzhiyun 	 * the inode versions we might be changing them here - use the
295*4882a593Smuzhiyun 	 * superblock flag to determine whether we need to look at di_flushiter
296*4882a593Smuzhiyun 	 * to skip replay when the on disk inode is newer than the log one
297*4882a593Smuzhiyun 	 */
298*4882a593Smuzhiyun 	if (!xfs_sb_version_has_v3inode(&mp->m_sb) &&
299*4882a593Smuzhiyun 	    ldip->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
300*4882a593Smuzhiyun 		/*
301*4882a593Smuzhiyun 		 * Deal with the wrap case, DI_MAX_FLUSH is less
302*4882a593Smuzhiyun 		 * than smaller numbers
303*4882a593Smuzhiyun 		 */
304*4882a593Smuzhiyun 		if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
305*4882a593Smuzhiyun 		    ldip->di_flushiter < (DI_MAX_FLUSH >> 1)) {
306*4882a593Smuzhiyun 			/* do nothing */
307*4882a593Smuzhiyun 		} else {
308*4882a593Smuzhiyun 			trace_xfs_log_recover_inode_skip(log, in_f);
309*4882a593Smuzhiyun 			error = 0;
310*4882a593Smuzhiyun 			goto out_release;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* Take the opportunity to reset the flush iteration count */
315*4882a593Smuzhiyun 	ldip->di_flushiter = 0;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (unlikely(S_ISREG(ldip->di_mode))) {
318*4882a593Smuzhiyun 		if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
319*4882a593Smuzhiyun 		    (ldip->di_format != XFS_DINODE_FMT_BTREE)) {
320*4882a593Smuzhiyun 			XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
321*4882a593Smuzhiyun 					 XFS_ERRLEVEL_LOW, mp, ldip,
322*4882a593Smuzhiyun 					 sizeof(*ldip));
323*4882a593Smuzhiyun 			xfs_alert(mp,
324*4882a593Smuzhiyun 		"%s: Bad regular inode log record, rec ptr "PTR_FMT", "
325*4882a593Smuzhiyun 		"ino ptr = "PTR_FMT", ino bp = "PTR_FMT", ino %Ld",
326*4882a593Smuzhiyun 				__func__, item, dip, bp, in_f->ilf_ino);
327*4882a593Smuzhiyun 			error = -EFSCORRUPTED;
328*4882a593Smuzhiyun 			goto out_release;
329*4882a593Smuzhiyun 		}
330*4882a593Smuzhiyun 	} else if (unlikely(S_ISDIR(ldip->di_mode))) {
331*4882a593Smuzhiyun 		if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
332*4882a593Smuzhiyun 		    (ldip->di_format != XFS_DINODE_FMT_BTREE) &&
333*4882a593Smuzhiyun 		    (ldip->di_format != XFS_DINODE_FMT_LOCAL)) {
334*4882a593Smuzhiyun 			XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
335*4882a593Smuzhiyun 					     XFS_ERRLEVEL_LOW, mp, ldip,
336*4882a593Smuzhiyun 					     sizeof(*ldip));
337*4882a593Smuzhiyun 			xfs_alert(mp,
338*4882a593Smuzhiyun 		"%s: Bad dir inode log record, rec ptr "PTR_FMT", "
339*4882a593Smuzhiyun 		"ino ptr = "PTR_FMT", ino bp = "PTR_FMT", ino %Ld",
340*4882a593Smuzhiyun 				__func__, item, dip, bp, in_f->ilf_ino);
341*4882a593Smuzhiyun 			error = -EFSCORRUPTED;
342*4882a593Smuzhiyun 			goto out_release;
343*4882a593Smuzhiyun 		}
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 	if (unlikely(ldip->di_nextents + ldip->di_anextents > ldip->di_nblocks)){
346*4882a593Smuzhiyun 		XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
347*4882a593Smuzhiyun 				     XFS_ERRLEVEL_LOW, mp, ldip,
348*4882a593Smuzhiyun 				     sizeof(*ldip));
349*4882a593Smuzhiyun 		xfs_alert(mp,
350*4882a593Smuzhiyun 	"%s: Bad inode log record, rec ptr "PTR_FMT", dino ptr "PTR_FMT", "
351*4882a593Smuzhiyun 	"dino bp "PTR_FMT", ino %Ld, total extents = %d, nblocks = %Ld",
352*4882a593Smuzhiyun 			__func__, item, dip, bp, in_f->ilf_ino,
353*4882a593Smuzhiyun 			ldip->di_nextents + ldip->di_anextents,
354*4882a593Smuzhiyun 			ldip->di_nblocks);
355*4882a593Smuzhiyun 		error = -EFSCORRUPTED;
356*4882a593Smuzhiyun 		goto out_release;
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 	if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
359*4882a593Smuzhiyun 		XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
360*4882a593Smuzhiyun 				     XFS_ERRLEVEL_LOW, mp, ldip,
361*4882a593Smuzhiyun 				     sizeof(*ldip));
362*4882a593Smuzhiyun 		xfs_alert(mp,
363*4882a593Smuzhiyun 	"%s: Bad inode log record, rec ptr "PTR_FMT", dino ptr "PTR_FMT", "
364*4882a593Smuzhiyun 	"dino bp "PTR_FMT", ino %Ld, forkoff 0x%x", __func__,
365*4882a593Smuzhiyun 			item, dip, bp, in_f->ilf_ino, ldip->di_forkoff);
366*4882a593Smuzhiyun 		error = -EFSCORRUPTED;
367*4882a593Smuzhiyun 		goto out_release;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 	isize = xfs_log_dinode_size(mp);
370*4882a593Smuzhiyun 	if (unlikely(item->ri_buf[1].i_len > isize)) {
371*4882a593Smuzhiyun 		XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
372*4882a593Smuzhiyun 				     XFS_ERRLEVEL_LOW, mp, ldip,
373*4882a593Smuzhiyun 				     sizeof(*ldip));
374*4882a593Smuzhiyun 		xfs_alert(mp,
375*4882a593Smuzhiyun 			"%s: Bad inode log record length %d, rec ptr "PTR_FMT,
376*4882a593Smuzhiyun 			__func__, item->ri_buf[1].i_len, item);
377*4882a593Smuzhiyun 		error = -EFSCORRUPTED;
378*4882a593Smuzhiyun 		goto out_release;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/*
382*4882a593Smuzhiyun 	 * Recover the log dinode inode into the on disk inode.
383*4882a593Smuzhiyun 	 *
384*4882a593Smuzhiyun 	 * The LSN in the log dinode is garbage - it can be zero or reflect
385*4882a593Smuzhiyun 	 * stale in-memory runtime state that isn't coherent with the changes
386*4882a593Smuzhiyun 	 * logged in this transaction or the changes written to the on-disk
387*4882a593Smuzhiyun 	 * inode.  Hence we write the current lSN into the inode because that
388*4882a593Smuzhiyun 	 * matches what xfs_iflush() would write inode the inode when flushing
389*4882a593Smuzhiyun 	 * the changes in this transaction.
390*4882a593Smuzhiyun 	 */
391*4882a593Smuzhiyun 	xfs_log_dinode_to_disk(ldip, dip, current_lsn);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	fields = in_f->ilf_fields;
394*4882a593Smuzhiyun 	if (fields & XFS_ILOG_DEV)
395*4882a593Smuzhiyun 		xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (in_f->ilf_size == 2)
398*4882a593Smuzhiyun 		goto out_owner_change;
399*4882a593Smuzhiyun 	len = item->ri_buf[2].i_len;
400*4882a593Smuzhiyun 	src = item->ri_buf[2].i_addr;
401*4882a593Smuzhiyun 	ASSERT(in_f->ilf_size <= 4);
402*4882a593Smuzhiyun 	ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
403*4882a593Smuzhiyun 	ASSERT(!(fields & XFS_ILOG_DFORK) ||
404*4882a593Smuzhiyun 	       (len == in_f->ilf_dsize));
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	switch (fields & XFS_ILOG_DFORK) {
407*4882a593Smuzhiyun 	case XFS_ILOG_DDATA:
408*4882a593Smuzhiyun 	case XFS_ILOG_DEXT:
409*4882a593Smuzhiyun 		memcpy(XFS_DFORK_DPTR(dip), src, len);
410*4882a593Smuzhiyun 		break;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	case XFS_ILOG_DBROOT:
413*4882a593Smuzhiyun 		xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
414*4882a593Smuzhiyun 				 (struct xfs_bmdr_block *)XFS_DFORK_DPTR(dip),
415*4882a593Smuzhiyun 				 XFS_DFORK_DSIZE(dip, mp));
416*4882a593Smuzhiyun 		break;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	default:
419*4882a593Smuzhiyun 		/*
420*4882a593Smuzhiyun 		 * There are no data fork flags set.
421*4882a593Smuzhiyun 		 */
422*4882a593Smuzhiyun 		ASSERT((fields & XFS_ILOG_DFORK) == 0);
423*4882a593Smuzhiyun 		break;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	/*
427*4882a593Smuzhiyun 	 * If we logged any attribute data, recover it.  There may or
428*4882a593Smuzhiyun 	 * may not have been any other non-core data logged in this
429*4882a593Smuzhiyun 	 * transaction.
430*4882a593Smuzhiyun 	 */
431*4882a593Smuzhiyun 	if (in_f->ilf_fields & XFS_ILOG_AFORK) {
432*4882a593Smuzhiyun 		if (in_f->ilf_fields & XFS_ILOG_DFORK) {
433*4882a593Smuzhiyun 			attr_index = 3;
434*4882a593Smuzhiyun 		} else {
435*4882a593Smuzhiyun 			attr_index = 2;
436*4882a593Smuzhiyun 		}
437*4882a593Smuzhiyun 		len = item->ri_buf[attr_index].i_len;
438*4882a593Smuzhiyun 		src = item->ri_buf[attr_index].i_addr;
439*4882a593Smuzhiyun 		ASSERT(len == in_f->ilf_asize);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 		switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
442*4882a593Smuzhiyun 		case XFS_ILOG_ADATA:
443*4882a593Smuzhiyun 		case XFS_ILOG_AEXT:
444*4882a593Smuzhiyun 			dest = XFS_DFORK_APTR(dip);
445*4882a593Smuzhiyun 			ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
446*4882a593Smuzhiyun 			memcpy(dest, src, len);
447*4882a593Smuzhiyun 			break;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 		case XFS_ILOG_ABROOT:
450*4882a593Smuzhiyun 			dest = XFS_DFORK_APTR(dip);
451*4882a593Smuzhiyun 			xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
452*4882a593Smuzhiyun 					 len, (struct xfs_bmdr_block *)dest,
453*4882a593Smuzhiyun 					 XFS_DFORK_ASIZE(dip, mp));
454*4882a593Smuzhiyun 			break;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		default:
457*4882a593Smuzhiyun 			xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
458*4882a593Smuzhiyun 			ASSERT(0);
459*4882a593Smuzhiyun 			error = -EFSCORRUPTED;
460*4882a593Smuzhiyun 			goto out_release;
461*4882a593Smuzhiyun 		}
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun out_owner_change:
465*4882a593Smuzhiyun 	/* Recover the swapext owner change unless inode has been deleted */
466*4882a593Smuzhiyun 	if ((in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER)) &&
467*4882a593Smuzhiyun 	    (dip->di_mode != 0))
468*4882a593Smuzhiyun 		error = xfs_recover_inode_owner_change(mp, dip, in_f,
469*4882a593Smuzhiyun 						       buffer_list);
470*4882a593Smuzhiyun 	/* re-generate the checksum. */
471*4882a593Smuzhiyun 	xfs_dinode_calc_crc(log->l_mp, dip);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	ASSERT(bp->b_mount == mp);
474*4882a593Smuzhiyun 	bp->b_flags |= _XBF_LOGRECOVERY;
475*4882a593Smuzhiyun 	xfs_buf_delwri_queue(bp, buffer_list);
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun out_release:
478*4882a593Smuzhiyun 	xfs_buf_relse(bp);
479*4882a593Smuzhiyun error:
480*4882a593Smuzhiyun 	if (need_free)
481*4882a593Smuzhiyun 		kmem_free(in_f);
482*4882a593Smuzhiyun 	return error;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun const struct xlog_recover_item_ops xlog_inode_item_ops = {
486*4882a593Smuzhiyun 	.item_type		= XFS_LI_INODE,
487*4882a593Smuzhiyun 	.ra_pass2		= xlog_recover_inode_ra_pass2,
488*4882a593Smuzhiyun 	.commit_pass2		= xlog_recover_inode_commit_pass2,
489*4882a593Smuzhiyun };
490