File Index
Files and directories
Sign in
FILE

xfs_inode_item.c

File preview with syntax highlighting.

linux-6.19.14/fs/xfs/xfs_inode_item.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
   4 * All Rights Reserved.
   5 */
   6#include "xfs.h"
   7#include "xfs_fs.h"
   8#include "xfs_shared.h"
   9#include "xfs_format.h"
  10#include "xfs_log_format.h"
  11#include "xfs_trans_resv.h"
  12#include "xfs_mount.h"
  13#include "xfs_inode.h"
  14#include "xfs_trans.h"
  15#include "xfs_inode_item.h"
  16#include "xfs_trace.h"
  17#include "xfs_trans_priv.h"
  18#include "xfs_buf_item.h"
  19#include "xfs_log.h"
  20#include "xfs_log_priv.h"
  21#include "xfs_error.h"
  22#include "xfs_rtbitmap.h"
  23
  24#include <linux/iversion.h>
  25
  26struct kmem_cache	*xfs_ili_cache;		/* inode log item */
  27
  28static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  29{
  30	return container_of(lip, struct xfs_inode_log_item, ili_item);
  31}
  32
  33static uint64_t
  34xfs_inode_item_sort(
  35	struct xfs_log_item	*lip)
  36{
  37	return INODE_ITEM(lip)->ili_inode->i_ino;
  38}
  39
  40#ifdef DEBUG_EXPENSIVE
  41static void
  42xfs_inode_item_precommit_check(
  43	struct xfs_inode	*ip)
  44{
  45	struct xfs_mount	*mp = ip->i_mount;
  46	struct xfs_dinode	*dip;
  47	xfs_failaddr_t		fa;
  48
  49	dip = kzalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | GFP_NOFS);
  50	if (!dip) {
  51		ASSERT(dip != NULL);
  52		return;
  53	}
  54
  55	xfs_inode_to_disk(ip, dip, 0);
  56	xfs_dinode_calc_crc(mp, dip);
  57	fa = xfs_dinode_verify(mp, ip->i_ino, dip);
  58	if (fa) {
  59		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
  60				sizeof(*dip), fa);
  61		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  62		ASSERT(fa == NULL);
  63	}
  64	kfree(dip);
  65}
  66#else
  67# define xfs_inode_item_precommit_check(ip)	((void)0)
  68#endif
  69
  70/*
  71 * Prior to finally logging the inode, we have to ensure that all the
  72 * per-modification inode state changes are applied. This includes VFS inode
  73 * state updates, format conversions, verifier state synchronisation and
  74 * ensuring the inode buffer remains in memory whilst the inode is dirty.
  75 *
  76 * We have to be careful when we grab the inode cluster buffer due to lock
  77 * ordering constraints. The unlinked inode modifications (xfs_iunlink_item)
  78 * require AGI -> inode cluster buffer lock order. The inode cluster buffer is
  79 * not locked until ->precommit, so it happens after everything else has been
  80 * modified.
  81 *
  82 * Further, we have AGI -> AGF lock ordering, and with O_TMPFILE handling we
  83 * have AGI -> AGF -> iunlink item -> inode cluster buffer lock order. Hence we
  84 * cannot safely lock the inode cluster buffer in xfs_trans_log_inode() because
  85 * it can be called on a inode (e.g. via bumplink/droplink) before we take the
  86 * AGF lock modifying directory blocks.
  87 *
  88 * Rather than force a complete rework of all the transactions to call
  89 * xfs_trans_log_inode() once and once only at the end of every transaction, we
  90 * move the pinning of the inode cluster buffer to a ->precommit operation. This
  91 * matches how the xfs_iunlink_item locks the inode cluster buffer, and it
  92 * ensures that the inode cluster buffer locking is always done last in a
  93 * transaction. i.e. we ensure the lock order is always AGI -> AGF -> inode
  94 * cluster buffer.
  95 *
  96 * If we return the inode number as the precommit sort key then we'll also
  97 * guarantee that the order all inode cluster buffer locking is the same all the
  98 * inodes and unlink items in the transaction.
  99 */
 100static int
 101xfs_inode_item_precommit(
 102	struct xfs_trans	*tp,
 103	struct xfs_log_item	*lip)
 104{
 105	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 106	struct xfs_inode	*ip = iip->ili_inode;
 107	struct inode		*inode = VFS_I(ip);
 108	unsigned int		flags = iip->ili_dirty_flags;
 109
 110	/*
 111	 * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
 112	 * don't matter - we either will need an extra transaction in 24 hours
 113	 * to log the timestamps, or will clear already cleared fields in the
 114	 * worst case.
 115	 */
 116	if (inode_state_read_once(inode) & I_DIRTY_TIME) {
 117		spin_lock(&inode->i_lock);
 118		inode_state_clear(inode, I_DIRTY_TIME);
 119		spin_unlock(&inode->i_lock);
 120	}
 121
 122	/*
 123	 * If we're updating the inode core or the timestamps and it's possible
 124	 * to upgrade this inode to bigtime format, do so now.
 125	 */
 126	if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
 127	    xfs_has_bigtime(ip->i_mount) &&
 128	    !xfs_inode_has_bigtime(ip)) {
 129		ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
 130		flags |= XFS_ILOG_CORE;
 131	}
 132
 133	/*
 134	 * Inode verifiers do not check that the extent size hints are an
 135	 * integer multiple of the rt extent size on a directory with
 136	 * rtinherit flags set.  If we're logging a directory that is
 137	 * misconfigured in this way, clear the bad hints.
 138	 */
 139	if (ip->i_diflags & XFS_DIFLAG_RTINHERIT) {
 140		if ((ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
 141		    xfs_extlen_to_rtxmod(ip->i_mount, ip->i_extsize) > 0) {
 142			ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
 143					   XFS_DIFLAG_EXTSZINHERIT);
 144			ip->i_extsize = 0;
 145			flags |= XFS_ILOG_CORE;
 146		}
 147		if ((ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
 148		    xfs_extlen_to_rtxmod(ip->i_mount, ip->i_cowextsize) > 0) {
 149			ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
 150			ip->i_cowextsize = 0;
 151			flags |= XFS_ILOG_CORE;
 152		}
 153	}
 154
 155	spin_lock(&iip->ili_lock);
 156	if (!iip->ili_item.li_buf) {
 157		struct xfs_buf	*bp;
 158		int		error;
 159
 160		/*
 161		 * We hold the ILOCK here, so this inode is not going to be
 162		 * flushed while we are here. Further, because there is no
 163		 * buffer attached to the item, we know that there is no IO in
 164		 * progress, so nothing will clear the ili_fields while we read
 165		 * in the buffer. Hence we can safely drop the spin lock and
 166		 * read the buffer knowing that the state will not change from
 167		 * here.
 168		 */
 169		spin_unlock(&iip->ili_lock);
 170		error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
 171		if (error)
 172			return error;
 173
 174		/*
 175		 * We need an explicit buffer reference for the log item but
 176		 * don't want the buffer to remain attached to the transaction.
 177		 * Hold the buffer but release the transaction reference once
 178		 * we've attached the inode log item to the buffer log item
 179		 * list.
 180		 */
 181		xfs_buf_hold(bp);
 182		spin_lock(&iip->ili_lock);
 183		iip->ili_item.li_buf = bp;
 184		bp->b_iodone = xfs_buf_inode_iodone;
 185		list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
 186		xfs_trans_brelse(tp, bp);
 187	}
 188
 189	/*
 190	 * Store the dirty flags back into the inode item as this state is used
 191	 * later on in xfs_inode_item_committing() to determine whether the
 192	 * transaction is relevant to fsync state or not.
 193	 */
 194	iip->ili_dirty_flags = flags;
 195
 196	/*
 197	 * Convert the flags on-disk fields that have been modified in the
 198	 * transaction so that ili_fields tracks the changes correctly.
 199	 */
 200	if (flags & XFS_ILOG_IVERSION)
 201		flags = ((flags & ~XFS_ILOG_IVERSION) | XFS_ILOG_CORE);
 202
 203	/*
 204	 * Always OR in the bits from the ili_last_fields field.  This is to
 205	 * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines
 206	 * in the eventual clearing of the ili_fields bits.  See the big comment
 207	 * in xfs_iflush() for an explanation of this coordination mechanism.
 208	 */
 209	iip->ili_fields |= (flags | iip->ili_last_fields);
 210	spin_unlock(&iip->ili_lock);
 211
 212	xfs_inode_item_precommit_check(ip);
 213	return 0;
 214}
 215
 216/*
 217 * The logged size of an inode fork is always the current size of the inode
 218 * fork. This means that when an inode fork is relogged, the size of the logged
 219 * region is determined by the current state, not the combination of the
 220 * previously logged state + the current state. This is different relogging
 221 * behaviour to most other log items which will retain the size of the
 222 * previously logged changes when smaller regions are relogged.
 223 *
 224 * Hence operations that remove data from the inode fork (e.g. shortform
 225 * dir/attr remove, extent form extent removal, etc), the size of the relogged
 226 * inode gets -smaller- rather than stays the same size as the previously logged
 227 * size and this can result in the committing transaction reducing the amount of
 228 * space being consumed by the CIL.
 229 */
 230STATIC void
 231xfs_inode_item_data_fork_size(
 232	struct xfs_inode_log_item *iip,
 233	int			*nvecs,
 234	int			*nbytes)
 235{
 236	struct xfs_inode	*ip = iip->ili_inode;
 237
 238	switch (ip->i_df.if_format) {
 239	case XFS_DINODE_FMT_EXTENTS:
 240		if ((iip->ili_fields & XFS_ILOG_DEXT) &&
 241		    ip->i_df.if_nextents > 0 &&
 242		    ip->i_df.if_bytes > 0) {
 243			/* worst case, doesn't subtract delalloc extents */
 244			*nbytes += xfs_inode_data_fork_size(ip);
 245			*nvecs += 1;
 246		}
 247		break;
 248	case XFS_DINODE_FMT_BTREE:
 249	case XFS_DINODE_FMT_META_BTREE:
 250		if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
 251		    ip->i_df.if_broot_bytes > 0) {
 252			*nbytes += ip->i_df.if_broot_bytes;
 253			*nvecs += 1;
 254		}
 255		break;
 256	case XFS_DINODE_FMT_LOCAL:
 257		if ((iip->ili_fields & XFS_ILOG_DDATA) &&
 258		    ip->i_df.if_bytes > 0) {
 259			*nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
 260			*nvecs += 1;
 261		}
 262		break;
 263
 264	case XFS_DINODE_FMT_DEV:
 265		break;
 266	default:
 267		ASSERT(0);
 268		break;
 269	}
 270}
 271
 272STATIC void
 273xfs_inode_item_attr_fork_size(
 274	struct xfs_inode_log_item *iip,
 275	int			*nvecs,
 276	int			*nbytes)
 277{
 278	struct xfs_inode	*ip = iip->ili_inode;
 279
 280	switch (ip->i_af.if_format) {
 281	case XFS_DINODE_FMT_EXTENTS:
 282		if ((iip->ili_fields & XFS_ILOG_AEXT) &&
 283		    ip->i_af.if_nextents > 0 &&
 284		    ip->i_af.if_bytes > 0) {
 285			/* worst case, doesn't subtract unused space */
 286			*nbytes += xfs_inode_attr_fork_size(ip);
 287			*nvecs += 1;
 288		}
 289		break;
 290	case XFS_DINODE_FMT_BTREE:
 291		if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
 292		    ip->i_af.if_broot_bytes > 0) {
 293			*nbytes += ip->i_af.if_broot_bytes;
 294			*nvecs += 1;
 295		}
 296		break;
 297	case XFS_DINODE_FMT_LOCAL:
 298		if ((iip->ili_fields & XFS_ILOG_ADATA) &&
 299		    ip->i_af.if_bytes > 0) {
 300			*nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes);
 301			*nvecs += 1;
 302		}
 303		break;
 304	default:
 305		ASSERT(0);
 306		break;
 307	}
 308}
 309
 310/*
 311 * This returns the number of iovecs needed to log the given inode item.
 312 *
 313 * We need one iovec for the inode log format structure, one for the
 314 * inode core, and possibly one for the inode data/extents/b-tree root
 315 * and one for the inode attribute data/extents/b-tree root.
 316 */
 317STATIC void
 318xfs_inode_item_size(
 319	struct xfs_log_item	*lip,
 320	int			*nvecs,
 321	int			*nbytes)
 322{
 323	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 324	struct xfs_inode	*ip = iip->ili_inode;
 325
 326	*nvecs += 2;
 327	*nbytes += sizeof(struct xfs_inode_log_format) +
 328		   xfs_log_dinode_size(ip->i_mount);
 329
 330	xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
 331	if (xfs_inode_has_attr_fork(ip))
 332		xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
 333}
 334
 335STATIC void
 336xfs_inode_item_format_data_fork(
 337	struct xfs_inode_log_item *iip,
 338	struct xfs_inode_log_format *ilf,
 339	struct xfs_log_vec	*lv,
 340	struct xfs_log_iovec	**vecp)
 341{
 342	struct xfs_inode	*ip = iip->ili_inode;
 343	size_t			data_bytes;
 344
 345	switch (ip->i_df.if_format) {
 346	case XFS_DINODE_FMT_EXTENTS:
 347		iip->ili_fields &=
 348			~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
 349
 350		if ((iip->ili_fields & XFS_ILOG_DEXT) &&
 351		    ip->i_df.if_nextents > 0 &&
 352		    ip->i_df.if_bytes > 0) {
 353			struct xfs_bmbt_rec *p;
 354
 355			ASSERT(xfs_iext_count(&ip->i_df) > 0);
 356
 357			p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
 358			data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
 359			xlog_finish_iovec(lv, *vecp, data_bytes);
 360
 361			ASSERT(data_bytes <= ip->i_df.if_bytes);
 362
 363			ilf->ilf_dsize = data_bytes;
 364			ilf->ilf_size++;
 365		} else {
 366			iip->ili_fields &= ~XFS_ILOG_DEXT;
 367		}
 368		break;
 369	case XFS_DINODE_FMT_BTREE:
 370	case XFS_DINODE_FMT_META_BTREE:
 371		iip->ili_fields &=
 372			~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
 373
 374		if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
 375		    ip->i_df.if_broot_bytes > 0) {
 376			ASSERT(ip->i_df.if_broot != NULL);
 377			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
 378					ip->i_df.if_broot,
 379					ip->i_df.if_broot_bytes);
 380			ilf->ilf_dsize = ip->i_df.if_broot_bytes;
 381			ilf->ilf_size++;
 382		} else {
 383			ASSERT(!(iip->ili_fields &
 384				 XFS_ILOG_DBROOT));
 385			iip->ili_fields &= ~XFS_ILOG_DBROOT;
 386		}
 387		break;
 388	case XFS_DINODE_FMT_LOCAL:
 389		iip->ili_fields &=
 390			~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
 391		if ((iip->ili_fields & XFS_ILOG_DDATA) &&
 392		    ip->i_df.if_bytes > 0) {
 393			ASSERT(ip->i_df.if_data != NULL);
 394			ASSERT(ip->i_disk_size > 0);
 395			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
 396					ip->i_df.if_data, ip->i_df.if_bytes);
 397			ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
 398			ilf->ilf_size++;
 399		} else {
 400			iip->ili_fields &= ~XFS_ILOG_DDATA;
 401		}
 402		break;
 403	case XFS_DINODE_FMT_DEV:
 404		iip->ili_fields &=
 405			~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
 406		if (iip->ili_fields & XFS_ILOG_DEV)
 407			ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
 408		break;
 409	default:
 410		ASSERT(0);
 411		break;
 412	}
 413}
 414
 415STATIC void
 416xfs_inode_item_format_attr_fork(
 417	struct xfs_inode_log_item *iip,
 418	struct xfs_inode_log_format *ilf,
 419	struct xfs_log_vec	*lv,
 420	struct xfs_log_iovec	**vecp)
 421{
 422	struct xfs_inode	*ip = iip->ili_inode;
 423	size_t			data_bytes;
 424
 425	switch (ip->i_af.if_format) {
 426	case XFS_DINODE_FMT_EXTENTS:
 427		iip->ili_fields &=
 428			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
 429
 430		if ((iip->ili_fields & XFS_ILOG_AEXT) &&
 431		    ip->i_af.if_nextents > 0 &&
 432		    ip->i_af.if_bytes > 0) {
 433			struct xfs_bmbt_rec *p;
 434
 435			ASSERT(xfs_iext_count(&ip->i_af) ==
 436				ip->i_af.if_nextents);
 437
 438			p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
 439			data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
 440			xlog_finish_iovec(lv, *vecp, data_bytes);
 441
 442			ilf->ilf_asize = data_bytes;
 443			ilf->ilf_size++;
 444		} else {
 445			iip->ili_fields &= ~XFS_ILOG_AEXT;
 446		}
 447		break;
 448	case XFS_DINODE_FMT_BTREE:
 449		iip->ili_fields &=
 450			~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
 451
 452		if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
 453		    ip->i_af.if_broot_bytes > 0) {
 454			ASSERT(ip->i_af.if_broot != NULL);
 455
 456			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
 457					ip->i_af.if_broot,
 458					ip->i_af.if_broot_bytes);
 459			ilf->ilf_asize = ip->i_af.if_broot_bytes;
 460			ilf->ilf_size++;
 461		} else {
 462			iip->ili_fields &= ~XFS_ILOG_ABROOT;
 463		}
 464		break;
 465	case XFS_DINODE_FMT_LOCAL:
 466		iip->ili_fields &=
 467			~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
 468
 469		if ((iip->ili_fields & XFS_ILOG_ADATA) &&
 470		    ip->i_af.if_bytes > 0) {
 471			ASSERT(ip->i_af.if_data != NULL);
 472			xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
 473					ip->i_af.if_data, ip->i_af.if_bytes);
 474			ilf->ilf_asize = (unsigned)ip->i_af.if_bytes;
 475			ilf->ilf_size++;
 476		} else {
 477			iip->ili_fields &= ~XFS_ILOG_ADATA;
 478		}
 479		break;
 480	default:
 481		ASSERT(0);
 482		break;
 483	}
 484}
 485
 486/*
 487 * Convert an incore timestamp to a log timestamp.  Note that the log format
 488 * specifies host endian format!
 489 */
 490static inline xfs_log_timestamp_t
 491xfs_inode_to_log_dinode_ts(
 492	struct xfs_inode		*ip,
 493	const struct timespec64		tv)
 494{
 495	struct xfs_log_legacy_timestamp	*lits;
 496	xfs_log_timestamp_t		its;
 497
 498	if (xfs_inode_has_bigtime(ip))
 499		return xfs_inode_encode_bigtime(tv);
 500
 501	lits = (struct xfs_log_legacy_timestamp *)&its;
 502	lits->t_sec = tv.tv_sec;
 503	lits->t_nsec = tv.tv_nsec;
 504
 505	return its;
 506}
 507
 508/*
 509 * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
 510 * but not in the in-memory one.  But we are guaranteed to have an inode buffer
 511 * in memory when logging an inode, so we can just copy it from the on-disk
 512 * inode to the in-log inode here so that recovery of file system with these
 513 * fields set to non-zero values doesn't lose them.  For all other cases we zero
 514 * the fields.
 515 */
 516static void
 517xfs_copy_dm_fields_to_log_dinode(
 518	struct xfs_inode	*ip,
 519	struct xfs_log_dinode	*to)
 520{
 521	struct xfs_dinode	*dip;
 522
 523	dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
 524			     ip->i_imap.im_boffset);
 525
 526	if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
 527		to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
 528		to->di_dmstate = be16_to_cpu(dip->di_dmstate);
 529	} else {
 530		to->di_dmevmask = 0;
 531		to->di_dmstate = 0;
 532	}
 533}
 534
 535static inline void
 536xfs_inode_to_log_dinode_iext_counters(
 537	struct xfs_inode	*ip,
 538	struct xfs_log_dinode	*to)
 539{
 540	if (xfs_inode_has_large_extent_counts(ip)) {
 541		to->di_big_nextents = xfs_ifork_nextents(&ip->i_df);
 542		to->di_big_anextents = xfs_ifork_nextents(&ip->i_af);
 543		to->di_nrext64_pad = 0;
 544	} else {
 545		to->di_nextents = xfs_ifork_nextents(&ip->i_df);
 546		to->di_anextents = xfs_ifork_nextents(&ip->i_af);
 547	}
 548}
 549
 550static void
 551xfs_inode_to_log_dinode(
 552	struct xfs_inode	*ip,
 553	struct xfs_log_dinode	*to,
 554	xfs_lsn_t		lsn)
 555{
 556	struct inode		*inode = VFS_I(ip);
 557
 558	to->di_magic = XFS_DINODE_MAGIC;
 559	to->di_format = xfs_ifork_format(&ip->i_df);
 560	to->di_uid = i_uid_read(inode);
 561	to->di_gid = i_gid_read(inode);
 562	to->di_projid_lo = ip->i_projid & 0xffff;
 563	to->di_projid_hi = ip->i_projid >> 16;
 564
 565	to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode_get_atime(inode));
 566	to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode_get_mtime(inode));
 567	to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode_get_ctime(inode));
 568	to->di_nlink = inode->i_nlink;
 569	to->di_gen = inode->i_generation;
 570	to->di_mode = inode->i_mode;
 571
 572	to->di_size = ip->i_disk_size;
 573	to->di_nblocks = ip->i_nblocks;
 574	to->di_extsize = ip->i_extsize;
 575	to->di_forkoff = ip->i_forkoff;
 576	to->di_aformat = xfs_ifork_format(&ip->i_af);
 577	to->di_flags = ip->i_diflags;
 578
 579	xfs_copy_dm_fields_to_log_dinode(ip, to);
 580
 581	/* log a dummy value to ensure log structure is fully initialised */
 582	to->di_next_unlinked = NULLAGINO;
 583
 584	if (xfs_has_v3inodes(ip->i_mount)) {
 585		to->di_version = 3;
 586		to->di_changecount = inode_peek_iversion(inode);
 587		to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
 588		to->di_flags2 = ip->i_diflags2;
 589		/* also covers the di_used_blocks union arm: */
 590		to->di_cowextsize = ip->i_cowextsize;
 591		to->di_ino = ip->i_ino;
 592		to->di_lsn = lsn;
 593		memset(to->di_pad2, 0, sizeof(to->di_pad2));
 594		uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
 595		to->di_v3_pad = 0;
 596
 597		/* dummy value for initialisation */
 598		to->di_crc = 0;
 599
 600		if (xfs_is_metadir_inode(ip))
 601			to->di_metatype = ip->i_metatype;
 602		else
 603			to->di_metatype = 0;
 604	} else {
 605		to->di_version = 2;
 606		to->di_flushiter = ip->i_flushiter;
 607		memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
 608		to->di_metatype = 0;
 609	}
 610
 611	xfs_inode_to_log_dinode_iext_counters(ip, to);
 612}
 613
 614/*
 615 * Format the inode core. Current timestamp data is only in the VFS inode
 616 * fields, so we need to grab them from there. Hence rather than just copying
 617 * the XFS inode core structure, format the fields directly into the iovec.
 618 */
 619static void
 620xfs_inode_item_format_core(
 621	struct xfs_inode	*ip,
 622	struct xfs_log_vec	*lv,
 623	struct xfs_log_iovec	**vecp)
 624{
 625	struct xfs_log_dinode	*dic;
 626
 627	dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
 628	xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
 629	xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
 630}
 631
 632/*
 633 * This is called to fill in the vector of log iovecs for the given inode
 634 * log item.  It fills the first item with an inode log format structure,
 635 * the second with the on-disk inode structure, and a possible third and/or
 636 * fourth with the inode data/extents/b-tree root and inode attributes
 637 * data/extents/b-tree root.
 638 *
 639 * Note: Always use the 64 bit inode log format structure so we don't
 640 * leave an uninitialised hole in the format item on 64 bit systems. Log
 641 * recovery on 32 bit systems handles this just fine, so there's no reason
 642 * for not using an initialising the properly padded structure all the time.
 643 */
 644STATIC void
 645xfs_inode_item_format(
 646	struct xfs_log_item	*lip,
 647	struct xfs_log_vec	*lv)
 648{
 649	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 650	struct xfs_inode	*ip = iip->ili_inode;
 651	struct xfs_log_iovec	*vecp = NULL;
 652	struct xfs_inode_log_format *ilf;
 653
 654	ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
 655	ilf->ilf_type = XFS_LI_INODE;
 656	ilf->ilf_ino = ip->i_ino;
 657	ilf->ilf_blkno = ip->i_imap.im_blkno;
 658	ilf->ilf_len = ip->i_imap.im_len;
 659	ilf->ilf_boffset = ip->i_imap.im_boffset;
 660	ilf->ilf_fields = XFS_ILOG_CORE;
 661	ilf->ilf_size = 2; /* format + core */
 662
 663	/*
 664	 * make sure we don't leak uninitialised data into the log in the case
 665	 * when we don't log every field in the inode.
 666	 */
 667	ilf->ilf_dsize = 0;
 668	ilf->ilf_asize = 0;
 669	ilf->ilf_pad = 0;
 670	memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
 671
 672	xlog_finish_iovec(lv, vecp, sizeof(*ilf));
 673
 674	xfs_inode_item_format_core(ip, lv, &vecp);
 675	xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
 676	if (xfs_inode_has_attr_fork(ip)) {
 677		xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
 678	} else {
 679		iip->ili_fields &=
 680			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
 681	}
 682
 683	/* update the format with the exact fields we actually logged */
 684	ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
 685}
 686
 687/*
 688 * This is called to pin the inode associated with the inode log
 689 * item in memory so it cannot be written out.
 690 */
 691STATIC void
 692xfs_inode_item_pin(
 693	struct xfs_log_item	*lip)
 694{
 695	struct xfs_inode	*ip = INODE_ITEM(lip)->ili_inode;
 696
 697	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
 698	ASSERT(lip->li_buf);
 699
 700	trace_xfs_inode_pin(ip, _RET_IP_);
 701	atomic_inc(&ip->i_pincount);
 702}
 703
 704
 705/*
 706 * This is called to unpin the inode associated with the inode log
 707 * item which was previously pinned with a call to xfs_inode_item_pin().
 708 *
 709 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
 710 *
 711 * Note that unpin can race with inode cluster buffer freeing marking the buffer
 712 * stale. In that case, flush completions are run from the buffer unpin call,
 713 * which may happen before the inode is unpinned. If we lose the race, there
 714 * will be no buffer attached to the log item, but the inode will be marked
 715 * XFS_ISTALE.
 716 */
 717STATIC void
 718xfs_inode_item_unpin(
 719	struct xfs_log_item	*lip,
 720	int			remove)
 721{
 722	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 723	struct xfs_inode	*ip = iip->ili_inode;
 724
 725	trace_xfs_inode_unpin(ip, _RET_IP_);
 726	ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
 727	ASSERT(atomic_read(&ip->i_pincount) > 0);
 728
 729	/*
 730	 * If this is the last unpin, then the inode no longer needs a journal
 731	 * flush to persist it. Hence we can clear the commit sequence numbers
 732	 * as a fsync/fdatasync operation on the inode at this point is a no-op.
 733	 */
 734	if (atomic_dec_and_lock(&ip->i_pincount, &iip->ili_lock)) {
 735		iip->ili_commit_seq = 0;
 736		iip->ili_datasync_seq = 0;
 737		spin_unlock(&iip->ili_lock);
 738		wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
 739	}
 740}
 741
 742STATIC uint
 743xfs_inode_item_push(
 744	struct xfs_log_item	*lip,
 745	struct list_head	*buffer_list)
 746		__releases(&lip->li_ailp->ail_lock)
 747		__acquires(&lip->li_ailp->ail_lock)
 748{
 749	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 750	struct xfs_inode	*ip = iip->ili_inode;
 751	struct xfs_buf		*bp = lip->li_buf;
 752	struct xfs_ail		*ailp = lip->li_ailp;
 753	uint			rval = XFS_ITEM_SUCCESS;
 754	int			error;
 755
 756	if (!bp || (ip->i_flags & XFS_ISTALE)) {
 757		/*
 758		 * Inode item/buffer is being aborted due to cluster
 759		 * buffer deletion. Trigger a log force to have that operation
 760		 * completed and items removed from the AIL before the next push
 761		 * attempt.
 762		 */
 763		trace_xfs_inode_push_stale(ip, _RET_IP_);
 764		return XFS_ITEM_PINNED;
 765	}
 766
 767	if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp)) {
 768		trace_xfs_inode_push_pinned(ip, _RET_IP_);
 769		return XFS_ITEM_PINNED;
 770	}
 771
 772	if (xfs_iflags_test(ip, XFS_IFLUSHING))
 773		return XFS_ITEM_FLUSHING;
 774
 775	if (!xfs_buf_trylock(bp))
 776		return XFS_ITEM_LOCKED;
 777
 778	spin_unlock(&ailp->ail_lock);
 779
 780	/*
 781	 * We need to hold a reference for flushing the cluster buffer as it may
 782	 * fail the buffer without IO submission. In which case, we better get a
 783	 * reference for that completion because otherwise we don't get a
 784	 * reference for IO until we queue the buffer for delwri submission.
 785	 */
 786	xfs_buf_hold(bp);
 787	error = xfs_iflush_cluster(bp);
 788	if (!error) {
 789		if (!xfs_buf_delwri_queue(bp, buffer_list))
 790			rval = XFS_ITEM_FLUSHING;
 791		xfs_buf_relse(bp);
 792	} else {
 793		/*
 794		 * Release the buffer if we were unable to flush anything. On
 795		 * any other error, the buffer has already been released.
 796		 */
 797		if (error == -EAGAIN)
 798			xfs_buf_relse(bp);
 799		rval = XFS_ITEM_LOCKED;
 800	}
 801
 802	/*
 803	 * The buffer no longer protects the log item from reclaim, so
 804	 * do not reference lip after this point.
 805	 */
 806	spin_lock(&ailp->ail_lock);
 807	return rval;
 808}
 809
 810/*
 811 * Unlock the inode associated with the inode log item.
 812 */
 813STATIC void
 814xfs_inode_item_release(
 815	struct xfs_log_item	*lip)
 816{
 817	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 818	struct xfs_inode	*ip = iip->ili_inode;
 819	unsigned short		lock_flags;
 820
 821	ASSERT(ip->i_itemp != NULL);
 822	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
 823
 824	lock_flags = iip->ili_lock_flags;
 825	iip->ili_lock_flags = 0;
 826	if (lock_flags)
 827		xfs_iunlock(ip, lock_flags);
 828}
 829
 830/*
 831 * This is called to find out where the oldest active copy of the inode log
 832 * item in the on disk log resides now that the last log write of it completed
 833 * at the given lsn.  Since we always re-log all dirty data in an inode, the
 834 * latest copy in the on disk log is the only one that matters.  Therefore,
 835 * simply return the given lsn.
 836 *
 837 * If the inode has been marked stale because the cluster is being freed, we
 838 * don't want to (re-)insert this inode into the AIL. There is a race condition
 839 * where the cluster buffer may be unpinned before the inode is inserted into
 840 * the AIL during transaction committed processing. If the buffer is unpinned
 841 * before the inode item has been committed and inserted, then it is possible
 842 * for the buffer to be written and IO completes before the inode is inserted
 843 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
 844 * AIL which will never get removed. It will, however, get reclaimed which
 845 * triggers an assert in xfs_inode_free() complaining about freein an inode
 846 * still in the AIL.
 847 *
 848 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
 849 * transaction committed code knows that it does not need to do any further
 850 * processing on the item.
 851 */
 852STATIC xfs_lsn_t
 853xfs_inode_item_committed(
 854	struct xfs_log_item	*lip,
 855	xfs_lsn_t		lsn)
 856{
 857	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 858	struct xfs_inode	*ip = iip->ili_inode;
 859
 860	if (xfs_iflags_test(ip, XFS_ISTALE)) {
 861		xfs_inode_item_unpin(lip, 0);
 862		return -1;
 863	}
 864	return lsn;
 865}
 866
 867/*
 868 * The modification is now complete, so before we unlock the inode we need to
 869 * update the commit sequence numbers for data integrity journal flushes. We
 870 * always record the commit sequence number (ili_commit_seq) so that anything
 871 * that needs a full journal sync will capture all of this modification.
 872 *
 873 * We then
 874 * check if the changes will impact a datasync (O_DSYNC) journal flush. If the
 875 * changes will require a datasync flush, then we also record the sequence in
 876 * ili_datasync_seq.
 877 *
 878 * These commit sequence numbers will get cleared atomically with the inode being
 879 * unpinned (i.e. pin count goes to zero), and so it will only be set when the
 880 * inode is dirty in the journal. This removes the need for checking if the
 881 * inode is pinned to determine if a journal flush is necessary, and hence
 882 * removes the need for holding the ILOCK_SHARED in xfs_file_fsync() to
 883 * serialise pin counts against commit sequence number updates.
 884 *
 885 */
 886STATIC void
 887xfs_inode_item_committing(
 888	struct xfs_log_item	*lip,
 889	xfs_csn_t		seq)
 890{
 891	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 892
 893	spin_lock(&iip->ili_lock);
 894	iip->ili_commit_seq = seq;
 895	if (iip->ili_dirty_flags & ~(XFS_ILOG_IVERSION | XFS_ILOG_TIMESTAMP))
 896		iip->ili_datasync_seq = seq;
 897	spin_unlock(&iip->ili_lock);
 898
 899	/*
 900	 * Clear the per-transaction dirty flags now that we have finished
 901	 * recording the transaction's inode modifications in the CIL and are
 902	 * about to release and (maybe) unlock the inode.
 903	 */
 904	iip->ili_dirty_flags = 0;
 905
 906	return xfs_inode_item_release(lip);
 907}
 908
 909static const struct xfs_item_ops xfs_inode_item_ops = {
 910	.iop_sort	= xfs_inode_item_sort,
 911	.iop_precommit	= xfs_inode_item_precommit,
 912	.iop_size	= xfs_inode_item_size,
 913	.iop_format	= xfs_inode_item_format,
 914	.iop_pin	= xfs_inode_item_pin,
 915	.iop_unpin	= xfs_inode_item_unpin,
 916	.iop_release	= xfs_inode_item_release,
 917	.iop_committed	= xfs_inode_item_committed,
 918	.iop_push	= xfs_inode_item_push,
 919	.iop_committing	= xfs_inode_item_committing,
 920};
 921
 922
 923/*
 924 * Initialize the inode log item for a newly allocated (in-core) inode.
 925 */
 926void
 927xfs_inode_item_init(
 928	struct xfs_inode	*ip,
 929	struct xfs_mount	*mp)
 930{
 931	struct xfs_inode_log_item *iip;
 932
 933	ASSERT(ip->i_itemp == NULL);
 934	iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
 935					      GFP_KERNEL | __GFP_NOFAIL);
 936
 937	iip->ili_inode = ip;
 938	spin_lock_init(&iip->ili_lock);
 939	xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
 940						&xfs_inode_item_ops);
 941}
 942
 943/*
 944 * Free the inode log item and any memory hanging off of it.
 945 */
 946void
 947xfs_inode_item_destroy(
 948	struct xfs_inode	*ip)
 949{
 950	struct xfs_inode_log_item *iip = ip->i_itemp;
 951
 952	ASSERT(iip->ili_item.li_buf == NULL);
 953
 954	ip->i_itemp = NULL;
 955	kvfree(iip->ili_item.li_lv_shadow);
 956	kmem_cache_free(xfs_ili_cache, iip);
 957}
 958
 959
 960/*
 961 * We only want to pull the item from the AIL if it is actually there
 962 * and its location in the log has not changed since we started the
 963 * flush.  Thus, we only bother if the inode's lsn has not changed.
 964 */
 965static void
 966xfs_iflush_ail_updates(
 967	struct xfs_ail		*ailp,
 968	struct list_head	*list)
 969{
 970	struct xfs_log_item	*lip;
 971	xfs_lsn_t		tail_lsn = 0;
 972
 973	/* this is an opencoded batch version of xfs_trans_ail_delete */
 974	spin_lock(&ailp->ail_lock);
 975	list_for_each_entry(lip, list, li_bio_list) {
 976		xfs_lsn_t	lsn;
 977
 978		clear_bit(XFS_LI_FAILED, &lip->li_flags);
 979		if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
 980			continue;
 981
 982		/*
 983		 * dgc: Not sure how this happens, but it happens very
 984		 * occassionaly via generic/388.  xfs_iflush_abort() also
 985		 * silently handles this same "under writeback but not in AIL at
 986		 * shutdown" condition via xfs_trans_ail_delete().
 987		 */
 988		if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
 989			ASSERT(xlog_is_shutdown(lip->li_log));
 990			continue;
 991		}
 992
 993		lsn = xfs_ail_delete_one(ailp, lip);
 994		if (!tail_lsn && lsn)
 995			tail_lsn = lsn;
 996	}
 997	xfs_ail_update_finish(ailp, tail_lsn);
 998}
 999
1000/*
1001 * Walk the list of inodes that have completed their IOs. If they are clean
1002 * remove them from the list and dissociate them from the buffer. Buffers that
1003 * are still dirty remain linked to the buffer and on the list. Caller must
1004 * handle them appropriately.
1005 */
1006static void
1007xfs_iflush_finish(
1008	struct xfs_buf		*bp,
1009	struct list_head	*list)
1010{
1011	struct xfs_log_item	*lip, *n;
1012
1013	list_for_each_entry_safe(lip, n, list, li_bio_list) {
1014		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1015		bool	drop_buffer = false;
1016
1017		spin_lock(&iip->ili_lock);
1018
1019		/*
1020		 * Remove the reference to the cluster buffer if the inode is
1021		 * clean in memory and drop the buffer reference once we've
1022		 * dropped the locks we hold.
1023		 */
1024		ASSERT(iip->ili_item.li_buf == bp);
1025		if (!iip->ili_fields) {
1026			iip->ili_item.li_buf = NULL;
1027			list_del_init(&lip->li_bio_list);
1028			drop_buffer = true;
1029		}
1030		iip->ili_last_fields = 0;
1031		iip->ili_flush_lsn = 0;
1032		clear_bit(XFS_LI_FLUSHING, &lip->li_flags);
1033		spin_unlock(&iip->ili_lock);
1034		xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
1035		if (drop_buffer)
1036			xfs_buf_rele(bp);
1037	}
1038}
1039
1040/*
1041 * Inode buffer IO completion routine.  It is responsible for removing inodes
1042 * attached to the buffer from the AIL if they have not been re-logged and
1043 * completing the inode flush.
1044 */
1045void
1046xfs_buf_inode_iodone(
1047	struct xfs_buf		*bp)
1048{
1049	struct xfs_log_item	*lip, *n;
1050	LIST_HEAD(flushed_inodes);
1051	LIST_HEAD(ail_updates);
1052
1053	/*
1054	 * Pull the attached inodes from the buffer one at a time and take the
1055	 * appropriate action on them.
1056	 */
1057	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1058		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1059
1060		if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
1061			xfs_iflush_abort(iip->ili_inode);
1062			continue;
1063		}
1064		if (!iip->ili_last_fields)
1065			continue;
1066
1067		/* Do an unlocked check for needing the AIL lock. */
1068		if (iip->ili_flush_lsn == lip->li_lsn ||
1069		    test_bit(XFS_LI_FAILED, &lip->li_flags))
1070			list_move_tail(&lip->li_bio_list, &ail_updates);
1071		else
1072			list_move_tail(&lip->li_bio_list, &flushed_inodes);
1073	}
1074
1075	if (!list_empty(&ail_updates)) {
1076		xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
1077		list_splice_tail(&ail_updates, &flushed_inodes);
1078	}
1079
1080	xfs_iflush_finish(bp, &flushed_inodes);
1081	if (!list_empty(&flushed_inodes))
1082		list_splice_tail(&flushed_inodes, &bp->b_li_list);
1083}
1084
1085/*
1086 * Clear the inode logging fields so no more flushes are attempted.  If we are
1087 * on a buffer list, it is now safe to remove it because the buffer is
1088 * guaranteed to be locked. The caller will drop the reference to the buffer
1089 * the log item held.
1090 */
1091static void
1092xfs_iflush_abort_clean(
1093	struct xfs_inode_log_item *iip)
1094{
1095	iip->ili_last_fields = 0;
1096	iip->ili_fields = 0;
1097	iip->ili_flush_lsn = 0;
1098	iip->ili_item.li_buf = NULL;
1099	list_del_init(&iip->ili_item.li_bio_list);
1100	clear_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags);
1101}
1102
1103/*
1104 * Abort flushing the inode from a context holding the cluster buffer locked.
1105 *
1106 * This is the normal runtime method of aborting writeback of an inode that is
1107 * attached to a cluster buffer. It occurs when the inode and the backing
1108 * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
1109 * flushing or buffer IO completion encounters a log shutdown situation.
1110 *
1111 * If we need to abort inode writeback and we don't already hold the buffer
1112 * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
1113 * necessary in a shutdown situation.
1114 */
1115void
1116xfs_iflush_abort(
1117	struct xfs_inode	*ip)
1118{
1119	struct xfs_inode_log_item *iip = ip->i_itemp;
1120	struct xfs_buf		*bp;
1121
1122	if (!iip) {
1123		/* clean inode, nothing to do */
1124		xfs_iflags_clear(ip, XFS_IFLUSHING);
1125		return;
1126	}
1127
1128	/*
1129	 * Remove the inode item from the AIL before we clear its internal
1130	 * state. Whilst the inode is in the AIL, it should have a valid buffer
1131	 * pointer for push operations to access - it is only safe to remove the
1132	 * inode from the buffer once it has been removed from the AIL.
1133	 */
1134	xfs_trans_ail_delete(&iip->ili_item, 0);
1135
1136	/*
1137	 * Grab the inode buffer so can we release the reference the inode log
1138	 * item holds on it.
1139	 */
1140	spin_lock(&iip->ili_lock);
1141	bp = iip->ili_item.li_buf;
1142	xfs_iflush_abort_clean(iip);
1143	spin_unlock(&iip->ili_lock);
1144
1145	xfs_iflags_clear(ip, XFS_IFLUSHING);
1146	if (bp)
1147		xfs_buf_rele(bp);
1148}
1149
1150/*
1151 * Abort an inode flush in the case of a shutdown filesystem. This can be called
1152 * from anywhere with just an inode reference and does not require holding the
1153 * inode cluster buffer locked. If the inode is attached to a cluster buffer,
1154 * it will grab and lock it safely, then abort the inode flush.
1155 */
1156void
1157xfs_iflush_shutdown_abort(
1158	struct xfs_inode	*ip)
1159{
1160	struct xfs_inode_log_item *iip = ip->i_itemp;
1161	struct xfs_buf		*bp;
1162
1163	if (!iip) {
1164		/* clean inode, nothing to do */
1165		xfs_iflags_clear(ip, XFS_IFLUSHING);
1166		return;
1167	}
1168
1169	spin_lock(&iip->ili_lock);
1170	bp = iip->ili_item.li_buf;
1171	if (!bp) {
1172		spin_unlock(&iip->ili_lock);
1173		xfs_iflush_abort(ip);
1174		return;
1175	}
1176
1177	/*
1178	 * We have to take a reference to the buffer so that it doesn't get
1179	 * freed when we drop the ili_lock and then wait to lock the buffer.
1180	 * We'll clean up the extra reference after we pick up the ili_lock
1181	 * again.
1182	 */
1183	xfs_buf_hold(bp);
1184	spin_unlock(&iip->ili_lock);
1185	xfs_buf_lock(bp);
1186
1187	spin_lock(&iip->ili_lock);
1188	if (!iip->ili_item.li_buf) {
1189		/*
1190		 * Raced with another removal, hold the only reference
1191		 * to bp now. Inode should not be in the AIL now, so just clean
1192		 * up and return;
1193		 */
1194		ASSERT(list_empty(&iip->ili_item.li_bio_list));
1195		ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
1196		xfs_iflush_abort_clean(iip);
1197		spin_unlock(&iip->ili_lock);
1198		xfs_iflags_clear(ip, XFS_IFLUSHING);
1199		xfs_buf_relse(bp);
1200		return;
1201	}
1202
1203	/*
1204	 * Got two references to bp. The first will get dropped by
1205	 * xfs_iflush_abort() when the item is removed from the buffer list, but
1206	 * we can't drop our reference until _abort() returns because we have to
1207	 * unlock the buffer as well. Hence we abort and then unlock and release
1208	 * our reference to the buffer.
1209	 */
1210	ASSERT(iip->ili_item.li_buf == bp);
1211	spin_unlock(&iip->ili_lock);
1212	xfs_iflush_abort(ip);
1213	xfs_buf_relse(bp);
1214}
1215
1216
1217/*
1218 * convert an xfs_inode_log_format struct from the old 32 bit version
1219 * (which can have different field alignments) to the native 64 bit version
1220 */
1221int
1222xfs_inode_item_format_convert(
1223	struct kvec			*buf,
1224	struct xfs_inode_log_format	*in_f)
1225{
1226	struct xfs_inode_log_format_32	*in_f32 = buf->iov_base;
1227
1228	if (buf->iov_len != sizeof(*in_f32)) {
1229		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
1230		return -EFSCORRUPTED;
1231	}
1232
1233	in_f->ilf_type = in_f32->ilf_type;
1234	in_f->ilf_size = in_f32->ilf_size;
1235	in_f->ilf_fields = in_f32->ilf_fields;
1236	in_f->ilf_asize = in_f32->ilf_asize;
1237	in_f->ilf_dsize = in_f32->ilf_dsize;
1238	in_f->ilf_ino = in_f32->ilf_ino;
1239	memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
1240	in_f->ilf_blkno = in_f32->ilf_blkno;
1241	in_f->ilf_len = in_f32->ilf_len;
1242	in_f->ilf_boffset = in_f32->ilf_boffset;
1243	return 0;
1244}
~/ Read-only file preview.