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