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