1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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"
20 #include "xfs_log_priv.h"
21 #include "xfs_error.h"
23 #include <linux/iversion.h>
25 struct kmem_cache *xfs_ili_cache; /* inode log item */
27 static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
29 return container_of(lip, struct xfs_inode_log_item, ili_item);
33 * The logged size of an inode fork is always the current size of the inode
34 * fork. This means that when an inode fork is relogged, the size of the logged
35 * region is determined by the current state, not the combination of the
36 * previously logged state + the current state. This is different relogging
37 * behaviour to most other log items which will retain the size of the
38 * previously logged changes when smaller regions are relogged.
40 * Hence operations that remove data from the inode fork (e.g. shortform
41 * dir/attr remove, extent form extent removal, etc), the size of the relogged
42 * inode gets -smaller- rather than stays the same size as the previously logged
43 * size and this can result in the committing transaction reducing the amount of
44 * space being consumed by the CIL.
47 xfs_inode_item_data_fork_size(
48 struct xfs_inode_log_item *iip,
52 struct xfs_inode *ip = iip->ili_inode;
54 switch (ip->i_df.if_format) {
55 case XFS_DINODE_FMT_EXTENTS:
56 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
57 ip->i_df.if_nextents > 0 &&
58 ip->i_df.if_bytes > 0) {
59 /* worst case, doesn't subtract delalloc extents */
60 *nbytes += XFS_IFORK_DSIZE(ip);
64 case XFS_DINODE_FMT_BTREE:
65 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
66 ip->i_df.if_broot_bytes > 0) {
67 *nbytes += ip->i_df.if_broot_bytes;
71 case XFS_DINODE_FMT_LOCAL:
72 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
73 ip->i_df.if_bytes > 0) {
74 *nbytes += roundup(ip->i_df.if_bytes, 4);
79 case XFS_DINODE_FMT_DEV:
88 xfs_inode_item_attr_fork_size(
89 struct xfs_inode_log_item *iip,
93 struct xfs_inode *ip = iip->ili_inode;
95 switch (ip->i_afp->if_format) {
96 case XFS_DINODE_FMT_EXTENTS:
97 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
98 ip->i_afp->if_nextents > 0 &&
99 ip->i_afp->if_bytes > 0) {
100 /* worst case, doesn't subtract unused space */
101 *nbytes += XFS_IFORK_ASIZE(ip);
105 case XFS_DINODE_FMT_BTREE:
106 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
107 ip->i_afp->if_broot_bytes > 0) {
108 *nbytes += ip->i_afp->if_broot_bytes;
112 case XFS_DINODE_FMT_LOCAL:
113 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
114 ip->i_afp->if_bytes > 0) {
115 *nbytes += roundup(ip->i_afp->if_bytes, 4);
126 * This returns the number of iovecs needed to log the given inode item.
128 * We need one iovec for the inode log format structure, one for the
129 * inode core, and possibly one for the inode data/extents/b-tree root
130 * and one for the inode attribute data/extents/b-tree root.
134 struct xfs_log_item *lip,
138 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
139 struct xfs_inode *ip = iip->ili_inode;
142 *nbytes += sizeof(struct xfs_inode_log_format) +
143 xfs_log_dinode_size(ip->i_mount);
145 xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
147 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
151 xfs_inode_item_format_data_fork(
152 struct xfs_inode_log_item *iip,
153 struct xfs_inode_log_format *ilf,
154 struct xfs_log_vec *lv,
155 struct xfs_log_iovec **vecp)
157 struct xfs_inode *ip = iip->ili_inode;
160 switch (ip->i_df.if_format) {
161 case XFS_DINODE_FMT_EXTENTS:
163 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
165 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
166 ip->i_df.if_nextents > 0 &&
167 ip->i_df.if_bytes > 0) {
168 struct xfs_bmbt_rec *p;
170 ASSERT(xfs_iext_count(&ip->i_df) > 0);
172 p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
173 data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
174 xlog_finish_iovec(lv, *vecp, data_bytes);
176 ASSERT(data_bytes <= ip->i_df.if_bytes);
178 ilf->ilf_dsize = data_bytes;
181 iip->ili_fields &= ~XFS_ILOG_DEXT;
184 case XFS_DINODE_FMT_BTREE:
186 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
188 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
189 ip->i_df.if_broot_bytes > 0) {
190 ASSERT(ip->i_df.if_broot != NULL);
191 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
193 ip->i_df.if_broot_bytes);
194 ilf->ilf_dsize = ip->i_df.if_broot_bytes;
197 ASSERT(!(iip->ili_fields &
199 iip->ili_fields &= ~XFS_ILOG_DBROOT;
202 case XFS_DINODE_FMT_LOCAL:
204 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
205 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
206 ip->i_df.if_bytes > 0) {
208 * Round i_bytes up to a word boundary.
209 * The underlying memory is guaranteed
210 * to be there by xfs_idata_realloc().
212 data_bytes = roundup(ip->i_df.if_bytes, 4);
213 ASSERT(ip->i_df.if_u1.if_data != NULL);
214 ASSERT(ip->i_disk_size > 0);
215 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
216 ip->i_df.if_u1.if_data, data_bytes);
217 ilf->ilf_dsize = (unsigned)data_bytes;
220 iip->ili_fields &= ~XFS_ILOG_DDATA;
223 case XFS_DINODE_FMT_DEV:
225 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
226 if (iip->ili_fields & XFS_ILOG_DEV)
227 ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
236 xfs_inode_item_format_attr_fork(
237 struct xfs_inode_log_item *iip,
238 struct xfs_inode_log_format *ilf,
239 struct xfs_log_vec *lv,
240 struct xfs_log_iovec **vecp)
242 struct xfs_inode *ip = iip->ili_inode;
245 switch (ip->i_afp->if_format) {
246 case XFS_DINODE_FMT_EXTENTS:
248 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
250 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
251 ip->i_afp->if_nextents > 0 &&
252 ip->i_afp->if_bytes > 0) {
253 struct xfs_bmbt_rec *p;
255 ASSERT(xfs_iext_count(ip->i_afp) ==
256 ip->i_afp->if_nextents);
258 p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
259 data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
260 xlog_finish_iovec(lv, *vecp, data_bytes);
262 ilf->ilf_asize = data_bytes;
265 iip->ili_fields &= ~XFS_ILOG_AEXT;
268 case XFS_DINODE_FMT_BTREE:
270 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
272 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
273 ip->i_afp->if_broot_bytes > 0) {
274 ASSERT(ip->i_afp->if_broot != NULL);
276 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
278 ip->i_afp->if_broot_bytes);
279 ilf->ilf_asize = ip->i_afp->if_broot_bytes;
282 iip->ili_fields &= ~XFS_ILOG_ABROOT;
285 case XFS_DINODE_FMT_LOCAL:
287 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
289 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
290 ip->i_afp->if_bytes > 0) {
292 * Round i_bytes up to a word boundary.
293 * The underlying memory is guaranteed
294 * to be there by xfs_idata_realloc().
296 data_bytes = roundup(ip->i_afp->if_bytes, 4);
297 ASSERT(ip->i_afp->if_u1.if_data != NULL);
298 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
299 ip->i_afp->if_u1.if_data,
301 ilf->ilf_asize = (unsigned)data_bytes;
304 iip->ili_fields &= ~XFS_ILOG_ADATA;
314 * Convert an incore timestamp to a log timestamp. Note that the log format
315 * specifies host endian format!
317 static inline xfs_log_timestamp_t
318 xfs_inode_to_log_dinode_ts(
319 struct xfs_inode *ip,
320 const struct timespec64 tv)
322 struct xfs_log_legacy_timestamp *lits;
323 xfs_log_timestamp_t its;
325 if (xfs_inode_has_bigtime(ip))
326 return xfs_inode_encode_bigtime(tv);
328 lits = (struct xfs_log_legacy_timestamp *)&its;
329 lits->t_sec = tv.tv_sec;
330 lits->t_nsec = tv.tv_nsec;
336 * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
337 * but not in the in-memory one. But we are guaranteed to have an inode buffer
338 * in memory when logging an inode, so we can just copy it from the on-disk
339 * inode to the in-log inode here so that recovery of file system with these
340 * fields set to non-zero values doesn't lose them. For all other cases we zero
344 xfs_copy_dm_fields_to_log_dinode(
345 struct xfs_inode *ip,
346 struct xfs_log_dinode *to)
348 struct xfs_dinode *dip;
350 dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
351 ip->i_imap.im_boffset);
353 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
354 to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
355 to->di_dmstate = be16_to_cpu(dip->di_dmstate);
363 xfs_inode_to_log_dinode(
364 struct xfs_inode *ip,
365 struct xfs_log_dinode *to,
368 struct inode *inode = VFS_I(ip);
370 to->di_magic = XFS_DINODE_MAGIC;
371 to->di_format = xfs_ifork_format(&ip->i_df);
372 to->di_uid = i_uid_read(inode);
373 to->di_gid = i_gid_read(inode);
374 to->di_projid_lo = ip->i_projid & 0xffff;
375 to->di_projid_hi = ip->i_projid >> 16;
377 memset(to->di_pad, 0, sizeof(to->di_pad));
378 memset(to->di_pad3, 0, sizeof(to->di_pad3));
379 to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode->i_atime);
380 to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode->i_mtime);
381 to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode->i_ctime);
382 to->di_nlink = inode->i_nlink;
383 to->di_gen = inode->i_generation;
384 to->di_mode = inode->i_mode;
386 to->di_size = ip->i_disk_size;
387 to->di_nblocks = ip->i_nblocks;
388 to->di_extsize = ip->i_extsize;
389 to->di_nextents = xfs_ifork_nextents(&ip->i_df);
390 to->di_anextents = xfs_ifork_nextents(ip->i_afp);
391 to->di_forkoff = ip->i_forkoff;
392 to->di_aformat = xfs_ifork_format(ip->i_afp);
393 to->di_flags = ip->i_diflags;
395 xfs_copy_dm_fields_to_log_dinode(ip, to);
397 /* log a dummy value to ensure log structure is fully initialised */
398 to->di_next_unlinked = NULLAGINO;
400 if (xfs_has_v3inodes(ip->i_mount)) {
402 to->di_changecount = inode_peek_iversion(inode);
403 to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
404 to->di_flags2 = ip->i_diflags2;
405 to->di_cowextsize = ip->i_cowextsize;
406 to->di_ino = ip->i_ino;
408 memset(to->di_pad2, 0, sizeof(to->di_pad2));
409 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
410 to->di_flushiter = 0;
413 to->di_flushiter = ip->i_flushiter;
418 * Format the inode core. Current timestamp data is only in the VFS inode
419 * fields, so we need to grab them from there. Hence rather than just copying
420 * the XFS inode core structure, format the fields directly into the iovec.
423 xfs_inode_item_format_core(
424 struct xfs_inode *ip,
425 struct xfs_log_vec *lv,
426 struct xfs_log_iovec **vecp)
428 struct xfs_log_dinode *dic;
430 dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
431 xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
432 xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
436 * This is called to fill in the vector of log iovecs for the given inode
437 * log item. It fills the first item with an inode log format structure,
438 * the second with the on-disk inode structure, and a possible third and/or
439 * fourth with the inode data/extents/b-tree root and inode attributes
440 * data/extents/b-tree root.
442 * Note: Always use the 64 bit inode log format structure so we don't
443 * leave an uninitialised hole in the format item on 64 bit systems. Log
444 * recovery on 32 bit systems handles this just fine, so there's no reason
445 * for not using an initialising the properly padded structure all the time.
448 xfs_inode_item_format(
449 struct xfs_log_item *lip,
450 struct xfs_log_vec *lv)
452 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
453 struct xfs_inode *ip = iip->ili_inode;
454 struct xfs_log_iovec *vecp = NULL;
455 struct xfs_inode_log_format *ilf;
457 ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
458 ilf->ilf_type = XFS_LI_INODE;
459 ilf->ilf_ino = ip->i_ino;
460 ilf->ilf_blkno = ip->i_imap.im_blkno;
461 ilf->ilf_len = ip->i_imap.im_len;
462 ilf->ilf_boffset = ip->i_imap.im_boffset;
463 ilf->ilf_fields = XFS_ILOG_CORE;
464 ilf->ilf_size = 2; /* format + core */
467 * make sure we don't leak uninitialised data into the log in the case
468 * when we don't log every field in the inode.
473 memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
475 xlog_finish_iovec(lv, vecp, sizeof(*ilf));
477 xfs_inode_item_format_core(ip, lv, &vecp);
478 xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
479 if (XFS_IFORK_Q(ip)) {
480 xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
483 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
486 /* update the format with the exact fields we actually logged */
487 ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
491 * This is called to pin the inode associated with the inode log
492 * item in memory so it cannot be written out.
496 struct xfs_log_item *lip)
498 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
500 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
503 trace_xfs_inode_pin(ip, _RET_IP_);
504 atomic_inc(&ip->i_pincount);
509 * This is called to unpin the inode associated with the inode log
510 * item which was previously pinned with a call to xfs_inode_item_pin().
512 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
514 * Note that unpin can race with inode cluster buffer freeing marking the buffer
515 * stale. In that case, flush completions are run from the buffer unpin call,
516 * which may happen before the inode is unpinned. If we lose the race, there
517 * will be no buffer attached to the log item, but the inode will be marked
521 xfs_inode_item_unpin(
522 struct xfs_log_item *lip,
525 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
527 trace_xfs_inode_unpin(ip, _RET_IP_);
528 ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
529 ASSERT(atomic_read(&ip->i_pincount) > 0);
530 if (atomic_dec_and_test(&ip->i_pincount))
531 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
536 struct xfs_log_item *lip,
537 struct list_head *buffer_list)
538 __releases(&lip->li_ailp->ail_lock)
539 __acquires(&lip->li_ailp->ail_lock)
541 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
542 struct xfs_inode *ip = iip->ili_inode;
543 struct xfs_buf *bp = lip->li_buf;
544 uint rval = XFS_ITEM_SUCCESS;
547 if (!bp || (ip->i_flags & XFS_ISTALE)) {
549 * Inode item/buffer is being being aborted due to cluster
550 * buffer deletion. Trigger a log force to have that operation
551 * completed and items removed from the AIL before the next push
554 return XFS_ITEM_PINNED;
557 if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp))
558 return XFS_ITEM_PINNED;
560 if (xfs_iflags_test(ip, XFS_IFLUSHING))
561 return XFS_ITEM_FLUSHING;
563 if (!xfs_buf_trylock(bp))
564 return XFS_ITEM_LOCKED;
566 spin_unlock(&lip->li_ailp->ail_lock);
569 * We need to hold a reference for flushing the cluster buffer as it may
570 * fail the buffer without IO submission. In which case, we better get a
571 * reference for that completion because otherwise we don't get a
572 * reference for IO until we queue the buffer for delwri submission.
575 error = xfs_iflush_cluster(bp);
577 if (!xfs_buf_delwri_queue(bp, buffer_list))
578 rval = XFS_ITEM_FLUSHING;
582 * Release the buffer if we were unable to flush anything. On
583 * any other error, the buffer has already been released.
585 if (error == -EAGAIN)
587 rval = XFS_ITEM_LOCKED;
590 spin_lock(&lip->li_ailp->ail_lock);
595 * Unlock the inode associated with the inode log item.
598 xfs_inode_item_release(
599 struct xfs_log_item *lip)
601 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
602 struct xfs_inode *ip = iip->ili_inode;
603 unsigned short lock_flags;
605 ASSERT(ip->i_itemp != NULL);
606 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
608 lock_flags = iip->ili_lock_flags;
609 iip->ili_lock_flags = 0;
611 xfs_iunlock(ip, lock_flags);
615 * This is called to find out where the oldest active copy of the inode log
616 * item in the on disk log resides now that the last log write of it completed
617 * at the given lsn. Since we always re-log all dirty data in an inode, the
618 * latest copy in the on disk log is the only one that matters. Therefore,
619 * simply return the given lsn.
621 * If the inode has been marked stale because the cluster is being freed, we
622 * don't want to (re-)insert this inode into the AIL. There is a race condition
623 * where the cluster buffer may be unpinned before the inode is inserted into
624 * the AIL during transaction committed processing. If the buffer is unpinned
625 * before the inode item has been committed and inserted, then it is possible
626 * for the buffer to be written and IO completes before the inode is inserted
627 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
628 * AIL which will never get removed. It will, however, get reclaimed which
629 * triggers an assert in xfs_inode_free() complaining about freein an inode
632 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
633 * transaction committed code knows that it does not need to do any further
634 * processing on the item.
637 xfs_inode_item_committed(
638 struct xfs_log_item *lip,
641 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
642 struct xfs_inode *ip = iip->ili_inode;
644 if (xfs_iflags_test(ip, XFS_ISTALE)) {
645 xfs_inode_item_unpin(lip, 0);
652 xfs_inode_item_committing(
653 struct xfs_log_item *lip,
656 INODE_ITEM(lip)->ili_commit_seq = seq;
657 return xfs_inode_item_release(lip);
660 static const struct xfs_item_ops xfs_inode_item_ops = {
661 .iop_size = xfs_inode_item_size,
662 .iop_format = xfs_inode_item_format,
663 .iop_pin = xfs_inode_item_pin,
664 .iop_unpin = xfs_inode_item_unpin,
665 .iop_release = xfs_inode_item_release,
666 .iop_committed = xfs_inode_item_committed,
667 .iop_push = xfs_inode_item_push,
668 .iop_committing = xfs_inode_item_committing,
673 * Initialize the inode log item for a newly allocated (in-core) inode.
677 struct xfs_inode *ip,
678 struct xfs_mount *mp)
680 struct xfs_inode_log_item *iip;
682 ASSERT(ip->i_itemp == NULL);
683 iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
684 GFP_KERNEL | __GFP_NOFAIL);
687 spin_lock_init(&iip->ili_lock);
688 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
689 &xfs_inode_item_ops);
693 * Free the inode log item and any memory hanging off of it.
696 xfs_inode_item_destroy(
697 struct xfs_inode *ip)
699 struct xfs_inode_log_item *iip = ip->i_itemp;
701 ASSERT(iip->ili_item.li_buf == NULL);
704 kmem_free(iip->ili_item.li_lv_shadow);
705 kmem_cache_free(xfs_ili_cache, iip);
710 * We only want to pull the item from the AIL if it is actually there
711 * and its location in the log has not changed since we started the
712 * flush. Thus, we only bother if the inode's lsn has not changed.
715 xfs_iflush_ail_updates(
716 struct xfs_ail *ailp,
717 struct list_head *list)
719 struct xfs_log_item *lip;
720 xfs_lsn_t tail_lsn = 0;
722 /* this is an opencoded batch version of xfs_trans_ail_delete */
723 spin_lock(&ailp->ail_lock);
724 list_for_each_entry(lip, list, li_bio_list) {
727 clear_bit(XFS_LI_FAILED, &lip->li_flags);
728 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
732 * dgc: Not sure how this happens, but it happens very
733 * occassionaly via generic/388. xfs_iflush_abort() also
734 * silently handles this same "under writeback but not in AIL at
735 * shutdown" condition via xfs_trans_ail_delete().
737 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
738 ASSERT(xlog_is_shutdown(lip->li_log));
742 lsn = xfs_ail_delete_one(ailp, lip);
743 if (!tail_lsn && lsn)
746 xfs_ail_update_finish(ailp, tail_lsn);
750 * Walk the list of inodes that have completed their IOs. If they are clean
751 * remove them from the list and dissociate them from the buffer. Buffers that
752 * are still dirty remain linked to the buffer and on the list. Caller must
753 * handle them appropriately.
758 struct list_head *list)
760 struct xfs_log_item *lip, *n;
762 list_for_each_entry_safe(lip, n, list, li_bio_list) {
763 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
764 bool drop_buffer = false;
766 spin_lock(&iip->ili_lock);
769 * Remove the reference to the cluster buffer if the inode is
770 * clean in memory and drop the buffer reference once we've
771 * dropped the locks we hold.
773 ASSERT(iip->ili_item.li_buf == bp);
774 if (!iip->ili_fields) {
775 iip->ili_item.li_buf = NULL;
776 list_del_init(&lip->li_bio_list);
779 iip->ili_last_fields = 0;
780 iip->ili_flush_lsn = 0;
781 spin_unlock(&iip->ili_lock);
782 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
789 * Inode buffer IO completion routine. It is responsible for removing inodes
790 * attached to the buffer from the AIL if they have not been re-logged and
791 * completing the inode flush.
794 xfs_buf_inode_iodone(
797 struct xfs_log_item *lip, *n;
798 LIST_HEAD(flushed_inodes);
799 LIST_HEAD(ail_updates);
802 * Pull the attached inodes from the buffer one at a time and take the
803 * appropriate action on them.
805 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
806 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
808 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
809 xfs_iflush_abort(iip->ili_inode);
812 if (!iip->ili_last_fields)
815 /* Do an unlocked check for needing the AIL lock. */
816 if (iip->ili_flush_lsn == lip->li_lsn ||
817 test_bit(XFS_LI_FAILED, &lip->li_flags))
818 list_move_tail(&lip->li_bio_list, &ail_updates);
820 list_move_tail(&lip->li_bio_list, &flushed_inodes);
823 if (!list_empty(&ail_updates)) {
824 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
825 list_splice_tail(&ail_updates, &flushed_inodes);
828 xfs_iflush_finish(bp, &flushed_inodes);
829 if (!list_empty(&flushed_inodes))
830 list_splice_tail(&flushed_inodes, &bp->b_li_list);
834 xfs_buf_inode_io_fail(
837 struct xfs_log_item *lip;
839 list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
840 set_bit(XFS_LI_FAILED, &lip->li_flags);
844 * Clear the inode logging fields so no more flushes are attempted. If we are
845 * on a buffer list, it is now safe to remove it because the buffer is
846 * guaranteed to be locked. The caller will drop the reference to the buffer
850 xfs_iflush_abort_clean(
851 struct xfs_inode_log_item *iip)
853 iip->ili_last_fields = 0;
855 iip->ili_fsync_fields = 0;
856 iip->ili_flush_lsn = 0;
857 iip->ili_item.li_buf = NULL;
858 list_del_init(&iip->ili_item.li_bio_list);
862 * Abort flushing the inode from a context holding the cluster buffer locked.
864 * This is the normal runtime method of aborting writeback of an inode that is
865 * attached to a cluster buffer. It occurs when the inode and the backing
866 * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
867 * flushing or buffer IO completion encounters a log shutdown situation.
869 * If we need to abort inode writeback and we don't already hold the buffer
870 * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
871 * necessary in a shutdown situation.
875 struct xfs_inode *ip)
877 struct xfs_inode_log_item *iip = ip->i_itemp;
881 /* clean inode, nothing to do */
882 xfs_iflags_clear(ip, XFS_IFLUSHING);
887 * Remove the inode item from the AIL before we clear its internal
888 * state. Whilst the inode is in the AIL, it should have a valid buffer
889 * pointer for push operations to access - it is only safe to remove the
890 * inode from the buffer once it has been removed from the AIL.
892 * We also clear the failed bit before removing the item from the AIL
893 * as xfs_trans_ail_delete()->xfs_clear_li_failed() will release buffer
894 * references the inode item owns and needs to hold until we've fully
895 * aborted the inode log item and detached it from the buffer.
897 clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
898 xfs_trans_ail_delete(&iip->ili_item, 0);
901 * Grab the inode buffer so can we release the reference the inode log
904 spin_lock(&iip->ili_lock);
905 bp = iip->ili_item.li_buf;
906 xfs_iflush_abort_clean(iip);
907 spin_unlock(&iip->ili_lock);
909 xfs_iflags_clear(ip, XFS_IFLUSHING);
915 * Abort an inode flush in the case of a shutdown filesystem. This can be called
916 * from anywhere with just an inode reference and does not require holding the
917 * inode cluster buffer locked. If the inode is attached to a cluster buffer,
918 * it will grab and lock it safely, then abort the inode flush.
921 xfs_iflush_shutdown_abort(
922 struct xfs_inode *ip)
924 struct xfs_inode_log_item *iip = ip->i_itemp;
928 /* clean inode, nothing to do */
929 xfs_iflags_clear(ip, XFS_IFLUSHING);
933 spin_lock(&iip->ili_lock);
934 bp = iip->ili_item.li_buf;
936 spin_unlock(&iip->ili_lock);
937 xfs_iflush_abort(ip);
942 * We have to take a reference to the buffer so that it doesn't get
943 * freed when we drop the ili_lock and then wait to lock the buffer.
944 * We'll clean up the extra reference after we pick up the ili_lock
948 spin_unlock(&iip->ili_lock);
951 spin_lock(&iip->ili_lock);
952 if (!iip->ili_item.li_buf) {
954 * Raced with another removal, hold the only reference
955 * to bp now. Inode should not be in the AIL now, so just clean
958 ASSERT(list_empty(&iip->ili_item.li_bio_list));
959 ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
960 xfs_iflush_abort_clean(iip);
961 spin_unlock(&iip->ili_lock);
962 xfs_iflags_clear(ip, XFS_IFLUSHING);
968 * Got two references to bp. The first will get dropped by
969 * xfs_iflush_abort() when the item is removed from the buffer list, but
970 * we can't drop our reference until _abort() returns because we have to
971 * unlock the buffer as well. Hence we abort and then unlock and release
972 * our reference to the buffer.
974 ASSERT(iip->ili_item.li_buf == bp);
975 spin_unlock(&iip->ili_lock);
976 xfs_iflush_abort(ip);
982 * convert an xfs_inode_log_format struct from the old 32 bit version
983 * (which can have different field alignments) to the native 64 bit version
986 xfs_inode_item_format_convert(
987 struct xfs_log_iovec *buf,
988 struct xfs_inode_log_format *in_f)
990 struct xfs_inode_log_format_32 *in_f32 = buf->i_addr;
992 if (buf->i_len != sizeof(*in_f32)) {
993 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
994 return -EFSCORRUPTED;
997 in_f->ilf_type = in_f32->ilf_type;
998 in_f->ilf_size = in_f32->ilf_size;
999 in_f->ilf_fields = in_f32->ilf_fields;
1000 in_f->ilf_asize = in_f32->ilf_asize;
1001 in_f->ilf_dsize = in_f32->ilf_dsize;
1002 in_f->ilf_ino = in_f32->ilf_ino;
1003 memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
1004 in_f->ilf_blkno = in_f32->ilf_blkno;
1005 in_f->ilf_len = in_f32->ilf_len;
1006 in_f->ilf_boffset = in_f32->ilf_boffset;