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