xref: /linux/fs/xfs/xfs_inode_item.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
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_error.h"
22 #include "xfs_rtbitmap.h"
23 
24 #include <linux/iversion.h>
25 
26 struct kmem_cache	*xfs_ili_cache;		/* inode log item */
27 
28 static 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 
33 static uint64_t
34 xfs_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
41 static void
42 xfs_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  */
100 static int
101 xfs_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  */
230 STATIC void
231 xfs_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 
272 STATIC void
273 xfs_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  */
317 STATIC void
318 xfs_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 
335 STATIC void
336 xfs_inode_item_format_data_fork(
337 	struct xfs_inode_log_item *iip,
338 	struct xfs_inode_log_format *ilf,
339 	struct xlog_format_buf	*lfb)
340 {
341 	struct xfs_inode	*ip = iip->ili_inode;
342 	size_t			data_bytes;
343 
344 	switch (ip->i_df.if_format) {
345 	case XFS_DINODE_FMT_EXTENTS:
346 		iip->ili_fields &=
347 			~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
348 
349 		if ((iip->ili_fields & XFS_ILOG_DEXT) &&
350 		    ip->i_df.if_nextents > 0 &&
351 		    ip->i_df.if_bytes > 0) {
352 			struct xfs_bmbt_rec *p;
353 
354 			ASSERT(xfs_iext_count(&ip->i_df) > 0);
355 
356 			p = xlog_format_start(lfb, XLOG_REG_TYPE_IEXT);
357 			data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
358 			xlog_format_commit(lfb, data_bytes);
359 
360 			ASSERT(data_bytes <= ip->i_df.if_bytes);
361 
362 			ilf->ilf_dsize = data_bytes;
363 			ilf->ilf_size++;
364 		} else {
365 			iip->ili_fields &= ~XFS_ILOG_DEXT;
366 		}
367 		break;
368 	case XFS_DINODE_FMT_BTREE:
369 	case XFS_DINODE_FMT_META_BTREE:
370 		iip->ili_fields &=
371 			~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
372 
373 		if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
374 		    ip->i_df.if_broot_bytes > 0) {
375 			ASSERT(ip->i_df.if_broot != NULL);
376 			xlog_format_copy(lfb, XLOG_REG_TYPE_IBROOT,
377 					ip->i_df.if_broot,
378 					ip->i_df.if_broot_bytes);
379 			ilf->ilf_dsize = ip->i_df.if_broot_bytes;
380 			ilf->ilf_size++;
381 		} else {
382 			ASSERT(!(iip->ili_fields &
383 				 XFS_ILOG_DBROOT));
384 			iip->ili_fields &= ~XFS_ILOG_DBROOT;
385 		}
386 		break;
387 	case XFS_DINODE_FMT_LOCAL:
388 		iip->ili_fields &=
389 			~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
390 		if ((iip->ili_fields & XFS_ILOG_DDATA) &&
391 		    ip->i_df.if_bytes > 0) {
392 			ASSERT(ip->i_df.if_data != NULL);
393 			ASSERT(ip->i_disk_size > 0);
394 			xlog_format_copy(lfb, XLOG_REG_TYPE_ILOCAL,
395 					ip->i_df.if_data,
396 					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 
415 STATIC void
416 xfs_inode_item_format_attr_fork(
417 	struct xfs_inode_log_item *iip,
418 	struct xfs_inode_log_format *ilf,
419 	struct xlog_format_buf	*lfb)
420 {
421 	struct xfs_inode	*ip = iip->ili_inode;
422 	size_t			data_bytes;
423 
424 	switch (ip->i_af.if_format) {
425 	case XFS_DINODE_FMT_EXTENTS:
426 		iip->ili_fields &=
427 			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
428 
429 		if ((iip->ili_fields & XFS_ILOG_AEXT) &&
430 		    ip->i_af.if_nextents > 0 &&
431 		    ip->i_af.if_bytes > 0) {
432 			struct xfs_bmbt_rec *p;
433 
434 			ASSERT(xfs_iext_count(&ip->i_af) ==
435 				ip->i_af.if_nextents);
436 
437 			p = xlog_format_start(lfb, XLOG_REG_TYPE_IATTR_EXT);
438 			data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
439 			xlog_format_commit(lfb, data_bytes);
440 
441 			ilf->ilf_asize = data_bytes;
442 			ilf->ilf_size++;
443 		} else {
444 			iip->ili_fields &= ~XFS_ILOG_AEXT;
445 		}
446 		break;
447 	case XFS_DINODE_FMT_BTREE:
448 		iip->ili_fields &=
449 			~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
450 
451 		if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
452 		    ip->i_af.if_broot_bytes > 0) {
453 			ASSERT(ip->i_af.if_broot != NULL);
454 
455 			xlog_format_copy(lfb, XLOG_REG_TYPE_IATTR_BROOT,
456 					ip->i_af.if_broot,
457 					ip->i_af.if_broot_bytes);
458 			ilf->ilf_asize = ip->i_af.if_broot_bytes;
459 			ilf->ilf_size++;
460 		} else {
461 			iip->ili_fields &= ~XFS_ILOG_ABROOT;
462 		}
463 		break;
464 	case XFS_DINODE_FMT_LOCAL:
465 		iip->ili_fields &=
466 			~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
467 
468 		if ((iip->ili_fields & XFS_ILOG_ADATA) &&
469 		    ip->i_af.if_bytes > 0) {
470 			ASSERT(ip->i_af.if_data != NULL);
471 			xlog_format_copy(lfb, XLOG_REG_TYPE_IATTR_LOCAL,
472 					ip->i_af.if_data,
473 					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  */
490 static inline xfs_log_timestamp_t
491 xfs_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  */
516 static void
517 xfs_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 
535 static inline void
536 xfs_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 
550 static void
551 xfs_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  */
619 static void
620 xfs_inode_item_format_core(
621 	struct xfs_inode	*ip,
622 	struct xlog_format_buf	*lfb)
623 {
624 	struct xfs_log_dinode	*dic;
625 
626 	dic = xlog_format_start(lfb, XLOG_REG_TYPE_ICORE);
627 	xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
628 	xlog_format_commit(lfb, xfs_log_dinode_size(ip->i_mount));
629 }
630 
631 /*
632  * This is called to fill in the vector of log iovecs for the given inode
633  * log item.  It fills the first item with an inode log format structure,
634  * the second with the on-disk inode structure, and a possible third and/or
635  * fourth with the inode data/extents/b-tree root and inode attributes
636  * data/extents/b-tree root.
637  *
638  * Note: Always use the 64 bit inode log format structure so we don't
639  * leave an uninitialised hole in the format item on 64 bit systems. Log
640  * recovery on 32 bit systems handles this just fine, so there's no reason
641  * for not using an initialising the properly padded structure all the time.
642  */
643 STATIC void
644 xfs_inode_item_format(
645 	struct xfs_log_item	*lip,
646 	struct xlog_format_buf	*lfb)
647 {
648 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
649 	struct xfs_inode	*ip = iip->ili_inode;
650 	struct xfs_inode_log_format *ilf;
651 
652 	ilf = xlog_format_start(lfb, XLOG_REG_TYPE_IFORMAT);
653 	ilf->ilf_type = XFS_LI_INODE;
654 	ilf->ilf_ino = ip->i_ino;
655 	ilf->ilf_blkno = ip->i_imap.im_blkno;
656 	ilf->ilf_len = ip->i_imap.im_len;
657 	ilf->ilf_boffset = ip->i_imap.im_boffset;
658 	ilf->ilf_fields = XFS_ILOG_CORE;
659 	ilf->ilf_size = 2; /* format + core */
660 
661 	/*
662 	 * make sure we don't leak uninitialised data into the log in the case
663 	 * when we don't log every field in the inode.
664 	 */
665 	ilf->ilf_dsize = 0;
666 	ilf->ilf_asize = 0;
667 	ilf->ilf_pad = 0;
668 	memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
669 	xlog_format_commit(lfb, sizeof(*ilf));
670 
671 	xfs_inode_item_format_core(ip, lfb);
672 	xfs_inode_item_format_data_fork(iip, ilf, lfb);
673 	if (xfs_inode_has_attr_fork(ip)) {
674 		xfs_inode_item_format_attr_fork(iip, ilf, lfb);
675 	} else {
676 		iip->ili_fields &=
677 			~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
678 	}
679 
680 	/* update the format with the exact fields we actually logged */
681 	ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
682 }
683 
684 /*
685  * This is called to pin the inode associated with the inode log
686  * item in memory so it cannot be written out.
687  */
688 STATIC void
689 xfs_inode_item_pin(
690 	struct xfs_log_item	*lip)
691 {
692 	struct xfs_inode	*ip = INODE_ITEM(lip)->ili_inode;
693 
694 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
695 	ASSERT(lip->li_buf);
696 
697 	trace_xfs_inode_pin(ip, _RET_IP_);
698 	atomic_inc(&ip->i_pincount);
699 }
700 
701 
702 /*
703  * This is called to unpin the inode associated with the inode log
704  * item which was previously pinned with a call to xfs_inode_item_pin().
705  *
706  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
707  *
708  * Note that unpin can race with inode cluster buffer freeing marking the buffer
709  * stale. In that case, flush completions are run from the buffer unpin call,
710  * which may happen before the inode is unpinned. If we lose the race, there
711  * will be no buffer attached to the log item, but the inode will be marked
712  * XFS_ISTALE.
713  */
714 STATIC void
715 xfs_inode_item_unpin(
716 	struct xfs_log_item	*lip,
717 	int			remove)
718 {
719 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
720 	struct xfs_inode	*ip = iip->ili_inode;
721 
722 	trace_xfs_inode_unpin(ip, _RET_IP_);
723 	ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
724 	ASSERT(atomic_read(&ip->i_pincount) > 0);
725 
726 	/*
727 	 * If this is the last unpin, then the inode no longer needs a journal
728 	 * flush to persist it. Hence we can clear the commit sequence numbers
729 	 * as a fsync/fdatasync operation on the inode at this point is a no-op.
730 	 */
731 	if (atomic_dec_and_lock(&ip->i_pincount, &iip->ili_lock)) {
732 		iip->ili_commit_seq = 0;
733 		iip->ili_datasync_seq = 0;
734 		spin_unlock(&iip->ili_lock);
735 		wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
736 	}
737 }
738 
739 STATIC uint
740 xfs_inode_item_push(
741 	struct xfs_log_item	*lip,
742 	struct list_head	*buffer_list)
743 		__releases(&lip->li_ailp->ail_lock)
744 		__acquires(&lip->li_ailp->ail_lock)
745 {
746 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
747 	struct xfs_inode	*ip = iip->ili_inode;
748 	struct xfs_buf		*bp = lip->li_buf;
749 	uint			rval = XFS_ITEM_SUCCESS;
750 	int			error;
751 
752 	if (!bp || (ip->i_flags & XFS_ISTALE)) {
753 		/*
754 		 * Inode item/buffer is being aborted due to cluster
755 		 * buffer deletion. Trigger a log force to have that operation
756 		 * completed and items removed from the AIL before the next push
757 		 * attempt.
758 		 */
759 		trace_xfs_inode_push_stale(ip, _RET_IP_);
760 		return XFS_ITEM_PINNED;
761 	}
762 
763 	if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp)) {
764 		trace_xfs_inode_push_pinned(ip, _RET_IP_);
765 		return XFS_ITEM_PINNED;
766 	}
767 
768 	if (xfs_iflags_test(ip, XFS_IFLUSHING))
769 		return XFS_ITEM_FLUSHING;
770 
771 	if (!xfs_buf_trylock(bp))
772 		return XFS_ITEM_LOCKED;
773 
774 	spin_unlock(&lip->li_ailp->ail_lock);
775 
776 	/*
777 	 * We need to hold a reference for flushing the cluster buffer as it may
778 	 * fail the buffer without IO submission. In which case, we better get a
779 	 * reference for that completion because otherwise we don't get a
780 	 * reference for IO until we queue the buffer for delwri submission.
781 	 */
782 	xfs_buf_hold(bp);
783 	error = xfs_iflush_cluster(bp);
784 	if (!error) {
785 		if (!xfs_buf_delwri_queue(bp, buffer_list))
786 			rval = XFS_ITEM_FLUSHING;
787 		xfs_buf_relse(bp);
788 	} else {
789 		/*
790 		 * Release the buffer if we were unable to flush anything. On
791 		 * any other error, the buffer has already been released.
792 		 */
793 		if (error == -EAGAIN)
794 			xfs_buf_relse(bp);
795 		rval = XFS_ITEM_LOCKED;
796 	}
797 
798 	spin_lock(&lip->li_ailp->ail_lock);
799 	return rval;
800 }
801 
802 /*
803  * Unlock the inode associated with the inode log item.
804  */
805 STATIC void
806 xfs_inode_item_release(
807 	struct xfs_log_item	*lip)
808 {
809 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
810 	struct xfs_inode	*ip = iip->ili_inode;
811 	unsigned short		lock_flags;
812 
813 	ASSERT(ip->i_itemp != NULL);
814 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
815 
816 	lock_flags = iip->ili_lock_flags;
817 	iip->ili_lock_flags = 0;
818 	if (lock_flags)
819 		xfs_iunlock(ip, lock_flags);
820 }
821 
822 /*
823  * This is called to find out where the oldest active copy of the inode log
824  * item in the on disk log resides now that the last log write of it completed
825  * at the given lsn.  Since we always re-log all dirty data in an inode, the
826  * latest copy in the on disk log is the only one that matters.  Therefore,
827  * simply return the given lsn.
828  *
829  * If the inode has been marked stale because the cluster is being freed, we
830  * don't want to (re-)insert this inode into the AIL. There is a race condition
831  * where the cluster buffer may be unpinned before the inode is inserted into
832  * the AIL during transaction committed processing. If the buffer is unpinned
833  * before the inode item has been committed and inserted, then it is possible
834  * for the buffer to be written and IO completes before the inode is inserted
835  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
836  * AIL which will never get removed. It will, however, get reclaimed which
837  * triggers an assert in xfs_inode_free() complaining about freein an inode
838  * still in the AIL.
839  *
840  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
841  * transaction committed code knows that it does not need to do any further
842  * processing on the item.
843  */
844 STATIC xfs_lsn_t
845 xfs_inode_item_committed(
846 	struct xfs_log_item	*lip,
847 	xfs_lsn_t		lsn)
848 {
849 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
850 	struct xfs_inode	*ip = iip->ili_inode;
851 
852 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
853 		xfs_inode_item_unpin(lip, 0);
854 		return -1;
855 	}
856 	return lsn;
857 }
858 
859 /*
860  * The modification is now complete, so before we unlock the inode we need to
861  * update the commit sequence numbers for data integrity journal flushes. We
862  * always record the commit sequence number (ili_commit_seq) so that anything
863  * that needs a full journal sync will capture all of this modification.
864  *
865  * We then
866  * check if the changes will impact a datasync (O_DSYNC) journal flush. If the
867  * changes will require a datasync flush, then we also record the sequence in
868  * ili_datasync_seq.
869  *
870  * These commit sequence numbers will get cleared atomically with the inode being
871  * unpinned (i.e. pin count goes to zero), and so it will only be set when the
872  * inode is dirty in the journal. This removes the need for checking if the
873  * inode is pinned to determine if a journal flush is necessary, and hence
874  * removes the need for holding the ILOCK_SHARED in xfs_file_fsync() to
875  * serialise pin counts against commit sequence number updates.
876  *
877  */
878 STATIC void
879 xfs_inode_item_committing(
880 	struct xfs_log_item	*lip,
881 	xfs_csn_t		seq)
882 {
883 	struct xfs_inode_log_item *iip = INODE_ITEM(lip);
884 
885 	spin_lock(&iip->ili_lock);
886 	iip->ili_commit_seq = seq;
887 	if (iip->ili_dirty_flags & ~(XFS_ILOG_IVERSION | XFS_ILOG_TIMESTAMP))
888 		iip->ili_datasync_seq = seq;
889 	spin_unlock(&iip->ili_lock);
890 
891 	/*
892 	 * Clear the per-transaction dirty flags now that we have finished
893 	 * recording the transaction's inode modifications in the CIL and are
894 	 * about to release and (maybe) unlock the inode.
895 	 */
896 	iip->ili_dirty_flags = 0;
897 
898 	return xfs_inode_item_release(lip);
899 }
900 
901 static const struct xfs_item_ops xfs_inode_item_ops = {
902 	.iop_sort	= xfs_inode_item_sort,
903 	.iop_precommit	= xfs_inode_item_precommit,
904 	.iop_size	= xfs_inode_item_size,
905 	.iop_format	= xfs_inode_item_format,
906 	.iop_pin	= xfs_inode_item_pin,
907 	.iop_unpin	= xfs_inode_item_unpin,
908 	.iop_release	= xfs_inode_item_release,
909 	.iop_committed	= xfs_inode_item_committed,
910 	.iop_push	= xfs_inode_item_push,
911 	.iop_committing	= xfs_inode_item_committing,
912 };
913 
914 
915 /*
916  * Initialize the inode log item for a newly allocated (in-core) inode.
917  */
918 void
919 xfs_inode_item_init(
920 	struct xfs_inode	*ip,
921 	struct xfs_mount	*mp)
922 {
923 	struct xfs_inode_log_item *iip;
924 
925 	ASSERT(ip->i_itemp == NULL);
926 	iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
927 					      GFP_KERNEL | __GFP_NOFAIL);
928 
929 	iip->ili_inode = ip;
930 	spin_lock_init(&iip->ili_lock);
931 	xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
932 						&xfs_inode_item_ops);
933 }
934 
935 /*
936  * Free the inode log item and any memory hanging off of it.
937  */
938 void
939 xfs_inode_item_destroy(
940 	struct xfs_inode	*ip)
941 {
942 	struct xfs_inode_log_item *iip = ip->i_itemp;
943 
944 	ASSERT(iip->ili_item.li_buf == NULL);
945 
946 	ip->i_itemp = NULL;
947 	kvfree(iip->ili_item.li_lv_shadow);
948 	kmem_cache_free(xfs_ili_cache, iip);
949 }
950 
951 
952 /*
953  * We only want to pull the item from the AIL if it is actually there
954  * and its location in the log has not changed since we started the
955  * flush.  Thus, we only bother if the inode's lsn has not changed.
956  */
957 static void
958 xfs_iflush_ail_updates(
959 	struct xfs_ail		*ailp,
960 	struct list_head	*list)
961 {
962 	struct xfs_log_item	*lip;
963 	xfs_lsn_t		tail_lsn = 0;
964 
965 	/* this is an opencoded batch version of xfs_trans_ail_delete */
966 	spin_lock(&ailp->ail_lock);
967 	list_for_each_entry(lip, list, li_bio_list) {
968 		xfs_lsn_t	lsn;
969 
970 		clear_bit(XFS_LI_FAILED, &lip->li_flags);
971 		if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
972 			continue;
973 
974 		/*
975 		 * dgc: Not sure how this happens, but it happens very
976 		 * occassionaly via generic/388.  xfs_iflush_abort() also
977 		 * silently handles this same "under writeback but not in AIL at
978 		 * shutdown" condition via xfs_trans_ail_delete().
979 		 */
980 		if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
981 			ASSERT(xlog_is_shutdown(lip->li_log));
982 			continue;
983 		}
984 
985 		lsn = xfs_ail_delete_one(ailp, lip);
986 		if (!tail_lsn && lsn)
987 			tail_lsn = lsn;
988 	}
989 	xfs_ail_update_finish(ailp, tail_lsn);
990 }
991 
992 /*
993  * Walk the list of inodes that have completed their IOs. If they are clean
994  * remove them from the list and dissociate them from the buffer. Buffers that
995  * are still dirty remain linked to the buffer and on the list. Caller must
996  * handle them appropriately.
997  */
998 static void
999 xfs_iflush_finish(
1000 	struct xfs_buf		*bp,
1001 	struct list_head	*list)
1002 {
1003 	struct xfs_log_item	*lip, *n;
1004 
1005 	list_for_each_entry_safe(lip, n, list, li_bio_list) {
1006 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1007 		bool	drop_buffer = false;
1008 
1009 		spin_lock(&iip->ili_lock);
1010 
1011 		/*
1012 		 * Remove the reference to the cluster buffer if the inode is
1013 		 * clean in memory and drop the buffer reference once we've
1014 		 * dropped the locks we hold.
1015 		 */
1016 		ASSERT(iip->ili_item.li_buf == bp);
1017 		if (!iip->ili_fields) {
1018 			iip->ili_item.li_buf = NULL;
1019 			list_del_init(&lip->li_bio_list);
1020 			drop_buffer = true;
1021 		}
1022 		iip->ili_last_fields = 0;
1023 		iip->ili_flush_lsn = 0;
1024 		clear_bit(XFS_LI_FLUSHING, &lip->li_flags);
1025 		spin_unlock(&iip->ili_lock);
1026 		xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
1027 		if (drop_buffer)
1028 			xfs_buf_rele(bp);
1029 	}
1030 }
1031 
1032 /*
1033  * Inode buffer IO completion routine.  It is responsible for removing inodes
1034  * attached to the buffer from the AIL if they have not been re-logged and
1035  * completing the inode flush.
1036  */
1037 void
1038 xfs_buf_inode_iodone(
1039 	struct xfs_buf		*bp)
1040 {
1041 	struct xfs_log_item	*lip, *n;
1042 	LIST_HEAD(flushed_inodes);
1043 	LIST_HEAD(ail_updates);
1044 
1045 	/*
1046 	 * Pull the attached inodes from the buffer one at a time and take the
1047 	 * appropriate action on them.
1048 	 */
1049 	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
1050 		struct xfs_inode_log_item *iip = INODE_ITEM(lip);
1051 
1052 		if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
1053 			xfs_iflush_abort(iip->ili_inode);
1054 			continue;
1055 		}
1056 		if (!iip->ili_last_fields)
1057 			continue;
1058 
1059 		/* Do an unlocked check for needing the AIL lock. */
1060 		if (iip->ili_flush_lsn == lip->li_lsn ||
1061 		    test_bit(XFS_LI_FAILED, &lip->li_flags))
1062 			list_move_tail(&lip->li_bio_list, &ail_updates);
1063 		else
1064 			list_move_tail(&lip->li_bio_list, &flushed_inodes);
1065 	}
1066 
1067 	if (!list_empty(&ail_updates)) {
1068 		xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
1069 		list_splice_tail(&ail_updates, &flushed_inodes);
1070 	}
1071 
1072 	xfs_iflush_finish(bp, &flushed_inodes);
1073 	if (!list_empty(&flushed_inodes))
1074 		list_splice_tail(&flushed_inodes, &bp->b_li_list);
1075 }
1076 
1077 /*
1078  * Clear the inode logging fields so no more flushes are attempted.  If we are
1079  * on a buffer list, it is now safe to remove it because the buffer is
1080  * guaranteed to be locked. The caller will drop the reference to the buffer
1081  * the log item held.
1082  */
1083 static void
1084 xfs_iflush_abort_clean(
1085 	struct xfs_inode_log_item *iip)
1086 {
1087 	iip->ili_last_fields = 0;
1088 	iip->ili_fields = 0;
1089 	iip->ili_flush_lsn = 0;
1090 	iip->ili_item.li_buf = NULL;
1091 	list_del_init(&iip->ili_item.li_bio_list);
1092 	clear_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags);
1093 }
1094 
1095 /*
1096  * Abort flushing the inode from a context holding the cluster buffer locked.
1097  *
1098  * This is the normal runtime method of aborting writeback of an inode that is
1099  * attached to a cluster buffer. It occurs when the inode and the backing
1100  * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
1101  * flushing or buffer IO completion encounters a log shutdown situation.
1102  *
1103  * If we need to abort inode writeback and we don't already hold the buffer
1104  * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
1105  * necessary in a shutdown situation.
1106  */
1107 void
1108 xfs_iflush_abort(
1109 	struct xfs_inode	*ip)
1110 {
1111 	struct xfs_inode_log_item *iip = ip->i_itemp;
1112 	struct xfs_buf		*bp;
1113 
1114 	if (!iip) {
1115 		/* clean inode, nothing to do */
1116 		xfs_iflags_clear(ip, XFS_IFLUSHING);
1117 		return;
1118 	}
1119 
1120 	/*
1121 	 * Remove the inode item from the AIL before we clear its internal
1122 	 * state. Whilst the inode is in the AIL, it should have a valid buffer
1123 	 * pointer for push operations to access - it is only safe to remove the
1124 	 * inode from the buffer once it has been removed from the AIL.
1125 	 */
1126 	xfs_trans_ail_delete(&iip->ili_item, 0);
1127 
1128 	/*
1129 	 * Grab the inode buffer so can we release the reference the inode log
1130 	 * item holds on it.
1131 	 */
1132 	spin_lock(&iip->ili_lock);
1133 	bp = iip->ili_item.li_buf;
1134 	xfs_iflush_abort_clean(iip);
1135 	spin_unlock(&iip->ili_lock);
1136 
1137 	xfs_iflags_clear(ip, XFS_IFLUSHING);
1138 	if (bp)
1139 		xfs_buf_rele(bp);
1140 }
1141 
1142 /*
1143  * Abort an inode flush in the case of a shutdown filesystem. This can be called
1144  * from anywhere with just an inode reference and does not require holding the
1145  * inode cluster buffer locked. If the inode is attached to a cluster buffer,
1146  * it will grab and lock it safely, then abort the inode flush.
1147  */
1148 void
1149 xfs_iflush_shutdown_abort(
1150 	struct xfs_inode	*ip)
1151 {
1152 	struct xfs_inode_log_item *iip = ip->i_itemp;
1153 	struct xfs_buf		*bp;
1154 
1155 	if (!iip) {
1156 		/* clean inode, nothing to do */
1157 		xfs_iflags_clear(ip, XFS_IFLUSHING);
1158 		return;
1159 	}
1160 
1161 	spin_lock(&iip->ili_lock);
1162 	bp = iip->ili_item.li_buf;
1163 	if (!bp) {
1164 		spin_unlock(&iip->ili_lock);
1165 		xfs_iflush_abort(ip);
1166 		return;
1167 	}
1168 
1169 	/*
1170 	 * We have to take a reference to the buffer so that it doesn't get
1171 	 * freed when we drop the ili_lock and then wait to lock the buffer.
1172 	 * We'll clean up the extra reference after we pick up the ili_lock
1173 	 * again.
1174 	 */
1175 	xfs_buf_hold(bp);
1176 	spin_unlock(&iip->ili_lock);
1177 	xfs_buf_lock(bp);
1178 
1179 	spin_lock(&iip->ili_lock);
1180 	if (!iip->ili_item.li_buf) {
1181 		/*
1182 		 * Raced with another removal, hold the only reference
1183 		 * to bp now. Inode should not be in the AIL now, so just clean
1184 		 * up and return;
1185 		 */
1186 		ASSERT(list_empty(&iip->ili_item.li_bio_list));
1187 		ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
1188 		xfs_iflush_abort_clean(iip);
1189 		spin_unlock(&iip->ili_lock);
1190 		xfs_iflags_clear(ip, XFS_IFLUSHING);
1191 		xfs_buf_relse(bp);
1192 		return;
1193 	}
1194 
1195 	/*
1196 	 * Got two references to bp. The first will get dropped by
1197 	 * xfs_iflush_abort() when the item is removed from the buffer list, but
1198 	 * we can't drop our reference until _abort() returns because we have to
1199 	 * unlock the buffer as well. Hence we abort and then unlock and release
1200 	 * our reference to the buffer.
1201 	 */
1202 	ASSERT(iip->ili_item.li_buf == bp);
1203 	spin_unlock(&iip->ili_lock);
1204 	xfs_iflush_abort(ip);
1205 	xfs_buf_relse(bp);
1206 }
1207 
1208 
1209 /*
1210  * convert an xfs_inode_log_format struct from the old 32 bit version
1211  * (which can have different field alignments) to the native 64 bit version
1212  */
1213 int
1214 xfs_inode_item_format_convert(
1215 	struct kvec			*buf,
1216 	struct xfs_inode_log_format	*in_f)
1217 {
1218 	struct xfs_inode_log_format_32	*in_f32 = buf->iov_base;
1219 
1220 	if (buf->iov_len != sizeof(*in_f32)) {
1221 		XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
1222 		return -EFSCORRUPTED;
1223 	}
1224 
1225 	in_f->ilf_type = in_f32->ilf_type;
1226 	in_f->ilf_size = in_f32->ilf_size;
1227 	in_f->ilf_fields = in_f32->ilf_fields;
1228 	in_f->ilf_asize = in_f32->ilf_asize;
1229 	in_f->ilf_dsize = in_f32->ilf_dsize;
1230 	in_f->ilf_ino = in_f32->ilf_ino;
1231 	memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
1232 	in_f->ilf_blkno = in_f32->ilf_blkno;
1233 	in_f->ilf_len = in_f32->ilf_len;
1234 	in_f->ilf_boffset = in_f32->ilf_boffset;
1235 	return 0;
1236 }
1237