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