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