162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2000-2006 Silicon Graphics, Inc. 462306a36Sopenharmony_ci * Copyright (c) 2012 Red Hat, Inc. 562306a36Sopenharmony_ci * All Rights Reserved. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci#include "xfs.h" 862306a36Sopenharmony_ci#include "xfs_fs.h" 962306a36Sopenharmony_ci#include "xfs_shared.h" 1062306a36Sopenharmony_ci#include "xfs_format.h" 1162306a36Sopenharmony_ci#include "xfs_log_format.h" 1262306a36Sopenharmony_ci#include "xfs_trans_resv.h" 1362306a36Sopenharmony_ci#include "xfs_bit.h" 1462306a36Sopenharmony_ci#include "xfs_mount.h" 1562306a36Sopenharmony_ci#include "xfs_defer.h" 1662306a36Sopenharmony_ci#include "xfs_inode.h" 1762306a36Sopenharmony_ci#include "xfs_btree.h" 1862306a36Sopenharmony_ci#include "xfs_trans.h" 1962306a36Sopenharmony_ci#include "xfs_alloc.h" 2062306a36Sopenharmony_ci#include "xfs_bmap.h" 2162306a36Sopenharmony_ci#include "xfs_bmap_util.h" 2262306a36Sopenharmony_ci#include "xfs_bmap_btree.h" 2362306a36Sopenharmony_ci#include "xfs_rtalloc.h" 2462306a36Sopenharmony_ci#include "xfs_error.h" 2562306a36Sopenharmony_ci#include "xfs_quota.h" 2662306a36Sopenharmony_ci#include "xfs_trans_space.h" 2762306a36Sopenharmony_ci#include "xfs_trace.h" 2862306a36Sopenharmony_ci#include "xfs_icache.h" 2962306a36Sopenharmony_ci#include "xfs_iomap.h" 3062306a36Sopenharmony_ci#include "xfs_reflink.h" 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci/* Kernel only BMAP related definitions and functions */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/* 3562306a36Sopenharmony_ci * Convert the given file system block to a disk block. We have to treat it 3662306a36Sopenharmony_ci * differently based on whether the file is a real time file or not, because the 3762306a36Sopenharmony_ci * bmap code does. 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cixfs_daddr_t 4062306a36Sopenharmony_cixfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci if (XFS_IS_REALTIME_INODE(ip)) 4362306a36Sopenharmony_ci return XFS_FSB_TO_BB(ip->i_mount, fsb); 4462306a36Sopenharmony_ci return XFS_FSB_TO_DADDR(ip->i_mount, fsb); 4562306a36Sopenharmony_ci} 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* 4862306a36Sopenharmony_ci * Routine to zero an extent on disk allocated to the specific inode. 4962306a36Sopenharmony_ci * 5062306a36Sopenharmony_ci * The VFS functions take a linearised filesystem block offset, so we have to 5162306a36Sopenharmony_ci * convert the sparse xfs fsb to the right format first. 5262306a36Sopenharmony_ci * VFS types are real funky, too. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ciint 5562306a36Sopenharmony_cixfs_zero_extent( 5662306a36Sopenharmony_ci struct xfs_inode *ip, 5762306a36Sopenharmony_ci xfs_fsblock_t start_fsb, 5862306a36Sopenharmony_ci xfs_off_t count_fsb) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 6162306a36Sopenharmony_ci struct xfs_buftarg *target = xfs_inode_buftarg(ip); 6262306a36Sopenharmony_ci xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb); 6362306a36Sopenharmony_ci sector_t block = XFS_BB_TO_FSBT(mp, sector); 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci return blkdev_issue_zeroout(target->bt_bdev, 6662306a36Sopenharmony_ci block << (mp->m_super->s_blocksize_bits - 9), 6762306a36Sopenharmony_ci count_fsb << (mp->m_super->s_blocksize_bits - 9), 6862306a36Sopenharmony_ci GFP_NOFS, 0); 6962306a36Sopenharmony_ci} 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci#ifdef CONFIG_XFS_RT 7262306a36Sopenharmony_ciint 7362306a36Sopenharmony_cixfs_bmap_rtalloc( 7462306a36Sopenharmony_ci struct xfs_bmalloca *ap) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci struct xfs_mount *mp = ap->ip->i_mount; 7762306a36Sopenharmony_ci xfs_fileoff_t orig_offset = ap->offset; 7862306a36Sopenharmony_ci xfs_rtblock_t rtb; 7962306a36Sopenharmony_ci xfs_extlen_t prod = 0; /* product factor for allocators */ 8062306a36Sopenharmony_ci xfs_extlen_t mod = 0; /* product factor for allocators */ 8162306a36Sopenharmony_ci xfs_extlen_t ralen = 0; /* realtime allocation length */ 8262306a36Sopenharmony_ci xfs_extlen_t align; /* minimum allocation alignment */ 8362306a36Sopenharmony_ci xfs_extlen_t orig_length = ap->length; 8462306a36Sopenharmony_ci xfs_extlen_t minlen = mp->m_sb.sb_rextsize; 8562306a36Sopenharmony_ci xfs_extlen_t raminlen; 8662306a36Sopenharmony_ci bool rtlocked = false; 8762306a36Sopenharmony_ci bool ignore_locality = false; 8862306a36Sopenharmony_ci int error; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci align = xfs_get_extsz_hint(ap->ip); 9162306a36Sopenharmony_ciretry: 9262306a36Sopenharmony_ci prod = align / mp->m_sb.sb_rextsize; 9362306a36Sopenharmony_ci error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, 9462306a36Sopenharmony_ci align, 1, ap->eof, 0, 9562306a36Sopenharmony_ci ap->conv, &ap->offset, &ap->length); 9662306a36Sopenharmony_ci if (error) 9762306a36Sopenharmony_ci return error; 9862306a36Sopenharmony_ci ASSERT(ap->length); 9962306a36Sopenharmony_ci ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci /* 10262306a36Sopenharmony_ci * If we shifted the file offset downward to satisfy an extent size 10362306a36Sopenharmony_ci * hint, increase minlen by that amount so that the allocator won't 10462306a36Sopenharmony_ci * give us an allocation that's too short to cover at least one of the 10562306a36Sopenharmony_ci * blocks that the caller asked for. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci if (ap->offset != orig_offset) 10862306a36Sopenharmony_ci minlen += orig_offset - ap->offset; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci /* 11162306a36Sopenharmony_ci * If the offset & length are not perfectly aligned 11262306a36Sopenharmony_ci * then kill prod, it will just get us in trouble. 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_ci div_u64_rem(ap->offset, align, &mod); 11562306a36Sopenharmony_ci if (mod || ap->length % align) 11662306a36Sopenharmony_ci prod = 1; 11762306a36Sopenharmony_ci /* 11862306a36Sopenharmony_ci * Set ralen to be the actual requested length in rtextents. 11962306a36Sopenharmony_ci */ 12062306a36Sopenharmony_ci ralen = ap->length / mp->m_sb.sb_rextsize; 12162306a36Sopenharmony_ci /* 12262306a36Sopenharmony_ci * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that 12362306a36Sopenharmony_ci * we rounded up to it, cut it back so it's valid again. 12462306a36Sopenharmony_ci * Note that if it's a really large request (bigger than 12562306a36Sopenharmony_ci * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't 12662306a36Sopenharmony_ci * adjust the starting point to match it. 12762306a36Sopenharmony_ci */ 12862306a36Sopenharmony_ci if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN) 12962306a36Sopenharmony_ci ralen = XFS_MAX_BMBT_EXTLEN / mp->m_sb.sb_rextsize; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci /* 13262306a36Sopenharmony_ci * Lock out modifications to both the RT bitmap and summary inodes 13362306a36Sopenharmony_ci */ 13462306a36Sopenharmony_ci if (!rtlocked) { 13562306a36Sopenharmony_ci xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); 13662306a36Sopenharmony_ci xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); 13762306a36Sopenharmony_ci xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); 13862306a36Sopenharmony_ci xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL); 13962306a36Sopenharmony_ci rtlocked = true; 14062306a36Sopenharmony_ci } 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci /* 14362306a36Sopenharmony_ci * If it's an allocation to an empty file at offset 0, 14462306a36Sopenharmony_ci * pick an extent that will space things out in the rt area. 14562306a36Sopenharmony_ci */ 14662306a36Sopenharmony_ci if (ap->eof && ap->offset == 0) { 14762306a36Sopenharmony_ci xfs_rtblock_t rtx; /* realtime extent no */ 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); 15062306a36Sopenharmony_ci if (error) 15162306a36Sopenharmony_ci return error; 15262306a36Sopenharmony_ci ap->blkno = rtx * mp->m_sb.sb_rextsize; 15362306a36Sopenharmony_ci } else { 15462306a36Sopenharmony_ci ap->blkno = 0; 15562306a36Sopenharmony_ci } 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci xfs_bmap_adjacent(ap); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci /* 16062306a36Sopenharmony_ci * Realtime allocation, done through xfs_rtallocate_extent. 16162306a36Sopenharmony_ci */ 16262306a36Sopenharmony_ci if (ignore_locality) 16362306a36Sopenharmony_ci ap->blkno = 0; 16462306a36Sopenharmony_ci else 16562306a36Sopenharmony_ci do_div(ap->blkno, mp->m_sb.sb_rextsize); 16662306a36Sopenharmony_ci rtb = ap->blkno; 16762306a36Sopenharmony_ci ap->length = ralen; 16862306a36Sopenharmony_ci raminlen = max_t(xfs_extlen_t, 1, minlen / mp->m_sb.sb_rextsize); 16962306a36Sopenharmony_ci error = xfs_rtallocate_extent(ap->tp, ap->blkno, raminlen, ap->length, 17062306a36Sopenharmony_ci &ralen, ap->wasdel, prod, &rtb); 17162306a36Sopenharmony_ci if (error) 17262306a36Sopenharmony_ci return error; 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci if (rtb != NULLRTBLOCK) { 17562306a36Sopenharmony_ci ap->blkno = rtb * mp->m_sb.sb_rextsize; 17662306a36Sopenharmony_ci ap->length = ralen * mp->m_sb.sb_rextsize; 17762306a36Sopenharmony_ci ap->ip->i_nblocks += ap->length; 17862306a36Sopenharmony_ci xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); 17962306a36Sopenharmony_ci if (ap->wasdel) 18062306a36Sopenharmony_ci ap->ip->i_delayed_blks -= ap->length; 18162306a36Sopenharmony_ci /* 18262306a36Sopenharmony_ci * Adjust the disk quota also. This was reserved 18362306a36Sopenharmony_ci * earlier. 18462306a36Sopenharmony_ci */ 18562306a36Sopenharmony_ci xfs_trans_mod_dquot_byino(ap->tp, ap->ip, 18662306a36Sopenharmony_ci ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : 18762306a36Sopenharmony_ci XFS_TRANS_DQ_RTBCOUNT, ap->length); 18862306a36Sopenharmony_ci return 0; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci if (align > mp->m_sb.sb_rextsize) { 19262306a36Sopenharmony_ci /* 19362306a36Sopenharmony_ci * We previously enlarged the request length to try to satisfy 19462306a36Sopenharmony_ci * an extent size hint. The allocator didn't return anything, 19562306a36Sopenharmony_ci * so reset the parameters to the original values and try again 19662306a36Sopenharmony_ci * without alignment criteria. 19762306a36Sopenharmony_ci */ 19862306a36Sopenharmony_ci ap->offset = orig_offset; 19962306a36Sopenharmony_ci ap->length = orig_length; 20062306a36Sopenharmony_ci minlen = align = mp->m_sb.sb_rextsize; 20162306a36Sopenharmony_ci goto retry; 20262306a36Sopenharmony_ci } 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci if (!ignore_locality && ap->blkno != 0) { 20562306a36Sopenharmony_ci /* 20662306a36Sopenharmony_ci * If we can't allocate near a specific rt extent, try again 20762306a36Sopenharmony_ci * without locality criteria. 20862306a36Sopenharmony_ci */ 20962306a36Sopenharmony_ci ignore_locality = true; 21062306a36Sopenharmony_ci goto retry; 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci ap->blkno = NULLFSBLOCK; 21462306a36Sopenharmony_ci ap->length = 0; 21562306a36Sopenharmony_ci return 0; 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci#endif /* CONFIG_XFS_RT */ 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci/* 22062306a36Sopenharmony_ci * Extent tree block counting routines. 22162306a36Sopenharmony_ci */ 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci/* 22462306a36Sopenharmony_ci * Count leaf blocks given a range of extent records. Delayed allocation 22562306a36Sopenharmony_ci * extents are not counted towards the totals. 22662306a36Sopenharmony_ci */ 22762306a36Sopenharmony_cixfs_extnum_t 22862306a36Sopenharmony_cixfs_bmap_count_leaves( 22962306a36Sopenharmony_ci struct xfs_ifork *ifp, 23062306a36Sopenharmony_ci xfs_filblks_t *count) 23162306a36Sopenharmony_ci{ 23262306a36Sopenharmony_ci struct xfs_iext_cursor icur; 23362306a36Sopenharmony_ci struct xfs_bmbt_irec got; 23462306a36Sopenharmony_ci xfs_extnum_t numrecs = 0; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci for_each_xfs_iext(ifp, &icur, &got) { 23762306a36Sopenharmony_ci if (!isnullstartblock(got.br_startblock)) { 23862306a36Sopenharmony_ci *count += got.br_blockcount; 23962306a36Sopenharmony_ci numrecs++; 24062306a36Sopenharmony_ci } 24162306a36Sopenharmony_ci } 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci return numrecs; 24462306a36Sopenharmony_ci} 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci/* 24762306a36Sopenharmony_ci * Count fsblocks of the given fork. Delayed allocation extents are 24862306a36Sopenharmony_ci * not counted towards the totals. 24962306a36Sopenharmony_ci */ 25062306a36Sopenharmony_ciint 25162306a36Sopenharmony_cixfs_bmap_count_blocks( 25262306a36Sopenharmony_ci struct xfs_trans *tp, 25362306a36Sopenharmony_ci struct xfs_inode *ip, 25462306a36Sopenharmony_ci int whichfork, 25562306a36Sopenharmony_ci xfs_extnum_t *nextents, 25662306a36Sopenharmony_ci xfs_filblks_t *count) 25762306a36Sopenharmony_ci{ 25862306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 25962306a36Sopenharmony_ci struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 26062306a36Sopenharmony_ci struct xfs_btree_cur *cur; 26162306a36Sopenharmony_ci xfs_extlen_t btblocks = 0; 26262306a36Sopenharmony_ci int error; 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci *nextents = 0; 26562306a36Sopenharmony_ci *count = 0; 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci if (!ifp) 26862306a36Sopenharmony_ci return 0; 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci switch (ifp->if_format) { 27162306a36Sopenharmony_ci case XFS_DINODE_FMT_BTREE: 27262306a36Sopenharmony_ci error = xfs_iread_extents(tp, ip, whichfork); 27362306a36Sopenharmony_ci if (error) 27462306a36Sopenharmony_ci return error; 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 27762306a36Sopenharmony_ci error = xfs_btree_count_blocks(cur, &btblocks); 27862306a36Sopenharmony_ci xfs_btree_del_cursor(cur, error); 27962306a36Sopenharmony_ci if (error) 28062306a36Sopenharmony_ci return error; 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci /* 28362306a36Sopenharmony_ci * xfs_btree_count_blocks includes the root block contained in 28462306a36Sopenharmony_ci * the inode fork in @btblocks, so subtract one because we're 28562306a36Sopenharmony_ci * only interested in allocated disk blocks. 28662306a36Sopenharmony_ci */ 28762306a36Sopenharmony_ci *count += btblocks - 1; 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci fallthrough; 29062306a36Sopenharmony_ci case XFS_DINODE_FMT_EXTENTS: 29162306a36Sopenharmony_ci *nextents = xfs_bmap_count_leaves(ifp, count); 29262306a36Sopenharmony_ci break; 29362306a36Sopenharmony_ci } 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci return 0; 29662306a36Sopenharmony_ci} 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_cistatic int 29962306a36Sopenharmony_cixfs_getbmap_report_one( 30062306a36Sopenharmony_ci struct xfs_inode *ip, 30162306a36Sopenharmony_ci struct getbmapx *bmv, 30262306a36Sopenharmony_ci struct kgetbmap *out, 30362306a36Sopenharmony_ci int64_t bmv_end, 30462306a36Sopenharmony_ci struct xfs_bmbt_irec *got) 30562306a36Sopenharmony_ci{ 30662306a36Sopenharmony_ci struct kgetbmap *p = out + bmv->bmv_entries; 30762306a36Sopenharmony_ci bool shared = false; 30862306a36Sopenharmony_ci int error; 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci error = xfs_reflink_trim_around_shared(ip, got, &shared); 31162306a36Sopenharmony_ci if (error) 31262306a36Sopenharmony_ci return error; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci if (isnullstartblock(got->br_startblock) || 31562306a36Sopenharmony_ci got->br_startblock == DELAYSTARTBLOCK) { 31662306a36Sopenharmony_ci /* 31762306a36Sopenharmony_ci * Take the flush completion as being a point-in-time snapshot 31862306a36Sopenharmony_ci * where there are no delalloc extents, and if any new ones 31962306a36Sopenharmony_ci * have been created racily, just skip them as being 'after' 32062306a36Sopenharmony_ci * the flush and so don't get reported. 32162306a36Sopenharmony_ci */ 32262306a36Sopenharmony_ci if (!(bmv->bmv_iflags & BMV_IF_DELALLOC)) 32362306a36Sopenharmony_ci return 0; 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci p->bmv_oflags |= BMV_OF_DELALLOC; 32662306a36Sopenharmony_ci p->bmv_block = -2; 32762306a36Sopenharmony_ci } else { 32862306a36Sopenharmony_ci p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock); 32962306a36Sopenharmony_ci } 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci if (got->br_state == XFS_EXT_UNWRITTEN && 33262306a36Sopenharmony_ci (bmv->bmv_iflags & BMV_IF_PREALLOC)) 33362306a36Sopenharmony_ci p->bmv_oflags |= BMV_OF_PREALLOC; 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci if (shared) 33662306a36Sopenharmony_ci p->bmv_oflags |= BMV_OF_SHARED; 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff); 33962306a36Sopenharmony_ci p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci bmv->bmv_offset = p->bmv_offset + p->bmv_length; 34262306a36Sopenharmony_ci bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); 34362306a36Sopenharmony_ci bmv->bmv_entries++; 34462306a36Sopenharmony_ci return 0; 34562306a36Sopenharmony_ci} 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_cistatic void 34862306a36Sopenharmony_cixfs_getbmap_report_hole( 34962306a36Sopenharmony_ci struct xfs_inode *ip, 35062306a36Sopenharmony_ci struct getbmapx *bmv, 35162306a36Sopenharmony_ci struct kgetbmap *out, 35262306a36Sopenharmony_ci int64_t bmv_end, 35362306a36Sopenharmony_ci xfs_fileoff_t bno, 35462306a36Sopenharmony_ci xfs_fileoff_t end) 35562306a36Sopenharmony_ci{ 35662306a36Sopenharmony_ci struct kgetbmap *p = out + bmv->bmv_entries; 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci if (bmv->bmv_iflags & BMV_IF_NO_HOLES) 35962306a36Sopenharmony_ci return; 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci p->bmv_block = -1; 36262306a36Sopenharmony_ci p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno); 36362306a36Sopenharmony_ci p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno); 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci bmv->bmv_offset = p->bmv_offset + p->bmv_length; 36662306a36Sopenharmony_ci bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); 36762306a36Sopenharmony_ci bmv->bmv_entries++; 36862306a36Sopenharmony_ci} 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_cistatic inline bool 37162306a36Sopenharmony_cixfs_getbmap_full( 37262306a36Sopenharmony_ci struct getbmapx *bmv) 37362306a36Sopenharmony_ci{ 37462306a36Sopenharmony_ci return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1; 37562306a36Sopenharmony_ci} 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_cistatic bool 37862306a36Sopenharmony_cixfs_getbmap_next_rec( 37962306a36Sopenharmony_ci struct xfs_bmbt_irec *rec, 38062306a36Sopenharmony_ci xfs_fileoff_t total_end) 38162306a36Sopenharmony_ci{ 38262306a36Sopenharmony_ci xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount; 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci if (end == total_end) 38562306a36Sopenharmony_ci return false; 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci rec->br_startoff += rec->br_blockcount; 38862306a36Sopenharmony_ci if (!isnullstartblock(rec->br_startblock) && 38962306a36Sopenharmony_ci rec->br_startblock != DELAYSTARTBLOCK) 39062306a36Sopenharmony_ci rec->br_startblock += rec->br_blockcount; 39162306a36Sopenharmony_ci rec->br_blockcount = total_end - end; 39262306a36Sopenharmony_ci return true; 39362306a36Sopenharmony_ci} 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci/* 39662306a36Sopenharmony_ci * Get inode's extents as described in bmv, and format for output. 39762306a36Sopenharmony_ci * Calls formatter to fill the user's buffer until all extents 39862306a36Sopenharmony_ci * are mapped, until the passed-in bmv->bmv_count slots have 39962306a36Sopenharmony_ci * been filled, or until the formatter short-circuits the loop, 40062306a36Sopenharmony_ci * if it is tracking filled-in extents on its own. 40162306a36Sopenharmony_ci */ 40262306a36Sopenharmony_ciint /* error code */ 40362306a36Sopenharmony_cixfs_getbmap( 40462306a36Sopenharmony_ci struct xfs_inode *ip, 40562306a36Sopenharmony_ci struct getbmapx *bmv, /* user bmap structure */ 40662306a36Sopenharmony_ci struct kgetbmap *out) 40762306a36Sopenharmony_ci{ 40862306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 40962306a36Sopenharmony_ci int iflags = bmv->bmv_iflags; 41062306a36Sopenharmony_ci int whichfork, lock, error = 0; 41162306a36Sopenharmony_ci int64_t bmv_end, max_len; 41262306a36Sopenharmony_ci xfs_fileoff_t bno, first_bno; 41362306a36Sopenharmony_ci struct xfs_ifork *ifp; 41462306a36Sopenharmony_ci struct xfs_bmbt_irec got, rec; 41562306a36Sopenharmony_ci xfs_filblks_t len; 41662306a36Sopenharmony_ci struct xfs_iext_cursor icur; 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ci if (bmv->bmv_iflags & ~BMV_IF_VALID) 41962306a36Sopenharmony_ci return -EINVAL; 42062306a36Sopenharmony_ci#ifndef DEBUG 42162306a36Sopenharmony_ci /* Only allow CoW fork queries if we're debugging. */ 42262306a36Sopenharmony_ci if (iflags & BMV_IF_COWFORK) 42362306a36Sopenharmony_ci return -EINVAL; 42462306a36Sopenharmony_ci#endif 42562306a36Sopenharmony_ci if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK)) 42662306a36Sopenharmony_ci return -EINVAL; 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci if (bmv->bmv_length < -1) 42962306a36Sopenharmony_ci return -EINVAL; 43062306a36Sopenharmony_ci bmv->bmv_entries = 0; 43162306a36Sopenharmony_ci if (bmv->bmv_length == 0) 43262306a36Sopenharmony_ci return 0; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci if (iflags & BMV_IF_ATTRFORK) 43562306a36Sopenharmony_ci whichfork = XFS_ATTR_FORK; 43662306a36Sopenharmony_ci else if (iflags & BMV_IF_COWFORK) 43762306a36Sopenharmony_ci whichfork = XFS_COW_FORK; 43862306a36Sopenharmony_ci else 43962306a36Sopenharmony_ci whichfork = XFS_DATA_FORK; 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci xfs_ilock(ip, XFS_IOLOCK_SHARED); 44262306a36Sopenharmony_ci switch (whichfork) { 44362306a36Sopenharmony_ci case XFS_ATTR_FORK: 44462306a36Sopenharmony_ci lock = xfs_ilock_attr_map_shared(ip); 44562306a36Sopenharmony_ci if (!xfs_inode_has_attr_fork(ip)) 44662306a36Sopenharmony_ci goto out_unlock_ilock; 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ci max_len = 1LL << 32; 44962306a36Sopenharmony_ci break; 45062306a36Sopenharmony_ci case XFS_COW_FORK: 45162306a36Sopenharmony_ci lock = XFS_ILOCK_SHARED; 45262306a36Sopenharmony_ci xfs_ilock(ip, lock); 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci /* No CoW fork? Just return */ 45562306a36Sopenharmony_ci if (!xfs_ifork_ptr(ip, whichfork)) 45662306a36Sopenharmony_ci goto out_unlock_ilock; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci if (xfs_get_cowextsz_hint(ip)) 45962306a36Sopenharmony_ci max_len = mp->m_super->s_maxbytes; 46062306a36Sopenharmony_ci else 46162306a36Sopenharmony_ci max_len = XFS_ISIZE(ip); 46262306a36Sopenharmony_ci break; 46362306a36Sopenharmony_ci case XFS_DATA_FORK: 46462306a36Sopenharmony_ci if (!(iflags & BMV_IF_DELALLOC) && 46562306a36Sopenharmony_ci (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) { 46662306a36Sopenharmony_ci error = filemap_write_and_wait(VFS_I(ip)->i_mapping); 46762306a36Sopenharmony_ci if (error) 46862306a36Sopenharmony_ci goto out_unlock_iolock; 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ci /* 47162306a36Sopenharmony_ci * Even after flushing the inode, there can still be 47262306a36Sopenharmony_ci * delalloc blocks on the inode beyond EOF due to 47362306a36Sopenharmony_ci * speculative preallocation. These are not removed 47462306a36Sopenharmony_ci * until the release function is called or the inode 47562306a36Sopenharmony_ci * is inactivated. Hence we cannot assert here that 47662306a36Sopenharmony_ci * ip->i_delayed_blks == 0. 47762306a36Sopenharmony_ci */ 47862306a36Sopenharmony_ci } 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ci if (xfs_get_extsz_hint(ip) || 48162306a36Sopenharmony_ci (ip->i_diflags & 48262306a36Sopenharmony_ci (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) 48362306a36Sopenharmony_ci max_len = mp->m_super->s_maxbytes; 48462306a36Sopenharmony_ci else 48562306a36Sopenharmony_ci max_len = XFS_ISIZE(ip); 48662306a36Sopenharmony_ci 48762306a36Sopenharmony_ci lock = xfs_ilock_data_map_shared(ip); 48862306a36Sopenharmony_ci break; 48962306a36Sopenharmony_ci } 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci ifp = xfs_ifork_ptr(ip, whichfork); 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_ci switch (ifp->if_format) { 49462306a36Sopenharmony_ci case XFS_DINODE_FMT_EXTENTS: 49562306a36Sopenharmony_ci case XFS_DINODE_FMT_BTREE: 49662306a36Sopenharmony_ci break; 49762306a36Sopenharmony_ci case XFS_DINODE_FMT_LOCAL: 49862306a36Sopenharmony_ci /* Local format inode forks report no extents. */ 49962306a36Sopenharmony_ci goto out_unlock_ilock; 50062306a36Sopenharmony_ci default: 50162306a36Sopenharmony_ci error = -EINVAL; 50262306a36Sopenharmony_ci goto out_unlock_ilock; 50362306a36Sopenharmony_ci } 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci if (bmv->bmv_length == -1) { 50662306a36Sopenharmony_ci max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len)); 50762306a36Sopenharmony_ci bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset); 50862306a36Sopenharmony_ci } 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci bmv_end = bmv->bmv_offset + bmv->bmv_length; 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset); 51362306a36Sopenharmony_ci len = XFS_BB_TO_FSB(mp, bmv->bmv_length); 51462306a36Sopenharmony_ci 51562306a36Sopenharmony_ci error = xfs_iread_extents(NULL, ip, whichfork); 51662306a36Sopenharmony_ci if (error) 51762306a36Sopenharmony_ci goto out_unlock_ilock; 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ci if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { 52062306a36Sopenharmony_ci /* 52162306a36Sopenharmony_ci * Report a whole-file hole if the delalloc flag is set to 52262306a36Sopenharmony_ci * stay compatible with the old implementation. 52362306a36Sopenharmony_ci */ 52462306a36Sopenharmony_ci if (iflags & BMV_IF_DELALLOC) 52562306a36Sopenharmony_ci xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, 52662306a36Sopenharmony_ci XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); 52762306a36Sopenharmony_ci goto out_unlock_ilock; 52862306a36Sopenharmony_ci } 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci while (!xfs_getbmap_full(bmv)) { 53162306a36Sopenharmony_ci xfs_trim_extent(&got, first_bno, len); 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci /* 53462306a36Sopenharmony_ci * Report an entry for a hole if this extent doesn't directly 53562306a36Sopenharmony_ci * follow the previous one. 53662306a36Sopenharmony_ci */ 53762306a36Sopenharmony_ci if (got.br_startoff > bno) { 53862306a36Sopenharmony_ci xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, 53962306a36Sopenharmony_ci got.br_startoff); 54062306a36Sopenharmony_ci if (xfs_getbmap_full(bmv)) 54162306a36Sopenharmony_ci break; 54262306a36Sopenharmony_ci } 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci /* 54562306a36Sopenharmony_ci * In order to report shared extents accurately, we report each 54662306a36Sopenharmony_ci * distinct shared / unshared part of a single bmbt record with 54762306a36Sopenharmony_ci * an individual getbmapx record. 54862306a36Sopenharmony_ci */ 54962306a36Sopenharmony_ci bno = got.br_startoff + got.br_blockcount; 55062306a36Sopenharmony_ci rec = got; 55162306a36Sopenharmony_ci do { 55262306a36Sopenharmony_ci error = xfs_getbmap_report_one(ip, bmv, out, bmv_end, 55362306a36Sopenharmony_ci &rec); 55462306a36Sopenharmony_ci if (error || xfs_getbmap_full(bmv)) 55562306a36Sopenharmony_ci goto out_unlock_ilock; 55662306a36Sopenharmony_ci } while (xfs_getbmap_next_rec(&rec, bno)); 55762306a36Sopenharmony_ci 55862306a36Sopenharmony_ci if (!xfs_iext_next_extent(ifp, &icur, &got)) { 55962306a36Sopenharmony_ci xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci if (bmv->bmv_entries > 0) 56262306a36Sopenharmony_ci out[bmv->bmv_entries - 1].bmv_oflags |= 56362306a36Sopenharmony_ci BMV_OF_LAST; 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci if (whichfork != XFS_ATTR_FORK && bno < end && 56662306a36Sopenharmony_ci !xfs_getbmap_full(bmv)) { 56762306a36Sopenharmony_ci xfs_getbmap_report_hole(ip, bmv, out, bmv_end, 56862306a36Sopenharmony_ci bno, end); 56962306a36Sopenharmony_ci } 57062306a36Sopenharmony_ci break; 57162306a36Sopenharmony_ci } 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ci if (bno >= first_bno + len) 57462306a36Sopenharmony_ci break; 57562306a36Sopenharmony_ci } 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ciout_unlock_ilock: 57862306a36Sopenharmony_ci xfs_iunlock(ip, lock); 57962306a36Sopenharmony_ciout_unlock_iolock: 58062306a36Sopenharmony_ci xfs_iunlock(ip, XFS_IOLOCK_SHARED); 58162306a36Sopenharmony_ci return error; 58262306a36Sopenharmony_ci} 58362306a36Sopenharmony_ci 58462306a36Sopenharmony_ci/* 58562306a36Sopenharmony_ci * Dead simple method of punching delalyed allocation blocks from a range in 58662306a36Sopenharmony_ci * the inode. This will always punch out both the start and end blocks, even 58762306a36Sopenharmony_ci * if the ranges only partially overlap them, so it is up to the caller to 58862306a36Sopenharmony_ci * ensure that partial blocks are not passed in. 58962306a36Sopenharmony_ci */ 59062306a36Sopenharmony_ciint 59162306a36Sopenharmony_cixfs_bmap_punch_delalloc_range( 59262306a36Sopenharmony_ci struct xfs_inode *ip, 59362306a36Sopenharmony_ci xfs_off_t start_byte, 59462306a36Sopenharmony_ci xfs_off_t end_byte) 59562306a36Sopenharmony_ci{ 59662306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 59762306a36Sopenharmony_ci struct xfs_ifork *ifp = &ip->i_df; 59862306a36Sopenharmony_ci xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, start_byte); 59962306a36Sopenharmony_ci xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end_byte); 60062306a36Sopenharmony_ci struct xfs_bmbt_irec got, del; 60162306a36Sopenharmony_ci struct xfs_iext_cursor icur; 60262306a36Sopenharmony_ci int error = 0; 60362306a36Sopenharmony_ci 60462306a36Sopenharmony_ci ASSERT(!xfs_need_iread_extents(ifp)); 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci xfs_ilock(ip, XFS_ILOCK_EXCL); 60762306a36Sopenharmony_ci if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) 60862306a36Sopenharmony_ci goto out_unlock; 60962306a36Sopenharmony_ci 61062306a36Sopenharmony_ci while (got.br_startoff + got.br_blockcount > start_fsb) { 61162306a36Sopenharmony_ci del = got; 61262306a36Sopenharmony_ci xfs_trim_extent(&del, start_fsb, end_fsb - start_fsb); 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci /* 61562306a36Sopenharmony_ci * A delete can push the cursor forward. Step back to the 61662306a36Sopenharmony_ci * previous extent on non-delalloc or extents outside the 61762306a36Sopenharmony_ci * target range. 61862306a36Sopenharmony_ci */ 61962306a36Sopenharmony_ci if (!del.br_blockcount || 62062306a36Sopenharmony_ci !isnullstartblock(del.br_startblock)) { 62162306a36Sopenharmony_ci if (!xfs_iext_prev_extent(ifp, &icur, &got)) 62262306a36Sopenharmony_ci break; 62362306a36Sopenharmony_ci continue; 62462306a36Sopenharmony_ci } 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur, 62762306a36Sopenharmony_ci &got, &del); 62862306a36Sopenharmony_ci if (error || !xfs_iext_get_extent(ifp, &icur, &got)) 62962306a36Sopenharmony_ci break; 63062306a36Sopenharmony_ci } 63162306a36Sopenharmony_ci 63262306a36Sopenharmony_ciout_unlock: 63362306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 63462306a36Sopenharmony_ci return error; 63562306a36Sopenharmony_ci} 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci/* 63862306a36Sopenharmony_ci * Test whether it is appropriate to check an inode for and free post EOF 63962306a36Sopenharmony_ci * blocks. The 'force' parameter determines whether we should also consider 64062306a36Sopenharmony_ci * regular files that are marked preallocated or append-only. 64162306a36Sopenharmony_ci */ 64262306a36Sopenharmony_cibool 64362306a36Sopenharmony_cixfs_can_free_eofblocks( 64462306a36Sopenharmony_ci struct xfs_inode *ip, 64562306a36Sopenharmony_ci bool force) 64662306a36Sopenharmony_ci{ 64762306a36Sopenharmony_ci struct xfs_bmbt_irec imap; 64862306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 64962306a36Sopenharmony_ci xfs_fileoff_t end_fsb; 65062306a36Sopenharmony_ci xfs_fileoff_t last_fsb; 65162306a36Sopenharmony_ci int nimaps = 1; 65262306a36Sopenharmony_ci int error; 65362306a36Sopenharmony_ci 65462306a36Sopenharmony_ci /* 65562306a36Sopenharmony_ci * Caller must either hold the exclusive io lock; or be inactivating 65662306a36Sopenharmony_ci * the inode, which guarantees there are no other users of the inode. 65762306a36Sopenharmony_ci */ 65862306a36Sopenharmony_ci ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) || 65962306a36Sopenharmony_ci (VFS_I(ip)->i_state & I_FREEING)); 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci /* prealloc/delalloc exists only on regular files */ 66262306a36Sopenharmony_ci if (!S_ISREG(VFS_I(ip)->i_mode)) 66362306a36Sopenharmony_ci return false; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci /* 66662306a36Sopenharmony_ci * Zero sized files with no cached pages and delalloc blocks will not 66762306a36Sopenharmony_ci * have speculative prealloc/delalloc blocks to remove. 66862306a36Sopenharmony_ci */ 66962306a36Sopenharmony_ci if (VFS_I(ip)->i_size == 0 && 67062306a36Sopenharmony_ci VFS_I(ip)->i_mapping->nrpages == 0 && 67162306a36Sopenharmony_ci ip->i_delayed_blks == 0) 67262306a36Sopenharmony_ci return false; 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_ci /* If we haven't read in the extent list, then don't do it now. */ 67562306a36Sopenharmony_ci if (xfs_need_iread_extents(&ip->i_df)) 67662306a36Sopenharmony_ci return false; 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci /* 67962306a36Sopenharmony_ci * Do not free real preallocated or append-only files unless the file 68062306a36Sopenharmony_ci * has delalloc blocks and we are forced to remove them. 68162306a36Sopenharmony_ci */ 68262306a36Sopenharmony_ci if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) 68362306a36Sopenharmony_ci if (!force || ip->i_delayed_blks == 0) 68462306a36Sopenharmony_ci return false; 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci /* 68762306a36Sopenharmony_ci * Do not try to free post-EOF blocks if EOF is beyond the end of the 68862306a36Sopenharmony_ci * range supported by the page cache, because the truncation will loop 68962306a36Sopenharmony_ci * forever. 69062306a36Sopenharmony_ci */ 69162306a36Sopenharmony_ci end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip)); 69262306a36Sopenharmony_ci if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) 69362306a36Sopenharmony_ci end_fsb = roundup_64(end_fsb, mp->m_sb.sb_rextsize); 69462306a36Sopenharmony_ci last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); 69562306a36Sopenharmony_ci if (last_fsb <= end_fsb) 69662306a36Sopenharmony_ci return false; 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_ci /* 69962306a36Sopenharmony_ci * Look up the mapping for the first block past EOF. If we can't find 70062306a36Sopenharmony_ci * it, there's nothing to free. 70162306a36Sopenharmony_ci */ 70262306a36Sopenharmony_ci xfs_ilock(ip, XFS_ILOCK_SHARED); 70362306a36Sopenharmony_ci error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps, 70462306a36Sopenharmony_ci 0); 70562306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_SHARED); 70662306a36Sopenharmony_ci if (error || nimaps == 0) 70762306a36Sopenharmony_ci return false; 70862306a36Sopenharmony_ci 70962306a36Sopenharmony_ci /* 71062306a36Sopenharmony_ci * If there's a real mapping there or there are delayed allocation 71162306a36Sopenharmony_ci * reservations, then we have post-EOF blocks to try to free. 71262306a36Sopenharmony_ci */ 71362306a36Sopenharmony_ci return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks; 71462306a36Sopenharmony_ci} 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci/* 71762306a36Sopenharmony_ci * This is called to free any blocks beyond eof. The caller must hold 71862306a36Sopenharmony_ci * IOLOCK_EXCL unless we are in the inode reclaim path and have the only 71962306a36Sopenharmony_ci * reference to the inode. 72062306a36Sopenharmony_ci */ 72162306a36Sopenharmony_ciint 72262306a36Sopenharmony_cixfs_free_eofblocks( 72362306a36Sopenharmony_ci struct xfs_inode *ip) 72462306a36Sopenharmony_ci{ 72562306a36Sopenharmony_ci struct xfs_trans *tp; 72662306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 72762306a36Sopenharmony_ci int error; 72862306a36Sopenharmony_ci 72962306a36Sopenharmony_ci /* Attach the dquots to the inode up front. */ 73062306a36Sopenharmony_ci error = xfs_qm_dqattach(ip); 73162306a36Sopenharmony_ci if (error) 73262306a36Sopenharmony_ci return error; 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci /* Wait on dio to ensure i_size has settled. */ 73562306a36Sopenharmony_ci inode_dio_wait(VFS_I(ip)); 73662306a36Sopenharmony_ci 73762306a36Sopenharmony_ci error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); 73862306a36Sopenharmony_ci if (error) { 73962306a36Sopenharmony_ci ASSERT(xfs_is_shutdown(mp)); 74062306a36Sopenharmony_ci return error; 74162306a36Sopenharmony_ci } 74262306a36Sopenharmony_ci 74362306a36Sopenharmony_ci xfs_ilock(ip, XFS_ILOCK_EXCL); 74462306a36Sopenharmony_ci xfs_trans_ijoin(tp, ip, 0); 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci /* 74762306a36Sopenharmony_ci * Do not update the on-disk file size. If we update the on-disk file 74862306a36Sopenharmony_ci * size and then the system crashes before the contents of the file are 74962306a36Sopenharmony_ci * flushed to disk then the files may be full of holes (ie NULL files 75062306a36Sopenharmony_ci * bug). 75162306a36Sopenharmony_ci */ 75262306a36Sopenharmony_ci error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK, 75362306a36Sopenharmony_ci XFS_ISIZE(ip), XFS_BMAPI_NODISCARD); 75462306a36Sopenharmony_ci if (error) 75562306a36Sopenharmony_ci goto err_cancel; 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci error = xfs_trans_commit(tp); 75862306a36Sopenharmony_ci if (error) 75962306a36Sopenharmony_ci goto out_unlock; 76062306a36Sopenharmony_ci 76162306a36Sopenharmony_ci xfs_inode_clear_eofblocks_tag(ip); 76262306a36Sopenharmony_ci goto out_unlock; 76362306a36Sopenharmony_ci 76462306a36Sopenharmony_cierr_cancel: 76562306a36Sopenharmony_ci /* 76662306a36Sopenharmony_ci * If we get an error at this point we simply don't 76762306a36Sopenharmony_ci * bother truncating the file. 76862306a36Sopenharmony_ci */ 76962306a36Sopenharmony_ci xfs_trans_cancel(tp); 77062306a36Sopenharmony_ciout_unlock: 77162306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 77262306a36Sopenharmony_ci return error; 77362306a36Sopenharmony_ci} 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ciint 77662306a36Sopenharmony_cixfs_alloc_file_space( 77762306a36Sopenharmony_ci struct xfs_inode *ip, 77862306a36Sopenharmony_ci xfs_off_t offset, 77962306a36Sopenharmony_ci xfs_off_t len) 78062306a36Sopenharmony_ci{ 78162306a36Sopenharmony_ci xfs_mount_t *mp = ip->i_mount; 78262306a36Sopenharmony_ci xfs_off_t count; 78362306a36Sopenharmony_ci xfs_filblks_t allocatesize_fsb; 78462306a36Sopenharmony_ci xfs_extlen_t extsz, temp; 78562306a36Sopenharmony_ci xfs_fileoff_t startoffset_fsb; 78662306a36Sopenharmony_ci xfs_fileoff_t endoffset_fsb; 78762306a36Sopenharmony_ci int rt; 78862306a36Sopenharmony_ci xfs_trans_t *tp; 78962306a36Sopenharmony_ci xfs_bmbt_irec_t imaps[1], *imapp; 79062306a36Sopenharmony_ci int error; 79162306a36Sopenharmony_ci 79262306a36Sopenharmony_ci trace_xfs_alloc_file_space(ip); 79362306a36Sopenharmony_ci 79462306a36Sopenharmony_ci if (xfs_is_shutdown(mp)) 79562306a36Sopenharmony_ci return -EIO; 79662306a36Sopenharmony_ci 79762306a36Sopenharmony_ci error = xfs_qm_dqattach(ip); 79862306a36Sopenharmony_ci if (error) 79962306a36Sopenharmony_ci return error; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci if (len <= 0) 80262306a36Sopenharmony_ci return -EINVAL; 80362306a36Sopenharmony_ci 80462306a36Sopenharmony_ci rt = XFS_IS_REALTIME_INODE(ip); 80562306a36Sopenharmony_ci extsz = xfs_get_extsz_hint(ip); 80662306a36Sopenharmony_ci 80762306a36Sopenharmony_ci count = len; 80862306a36Sopenharmony_ci imapp = &imaps[0]; 80962306a36Sopenharmony_ci startoffset_fsb = XFS_B_TO_FSBT(mp, offset); 81062306a36Sopenharmony_ci endoffset_fsb = XFS_B_TO_FSB(mp, offset + count); 81162306a36Sopenharmony_ci allocatesize_fsb = endoffset_fsb - startoffset_fsb; 81262306a36Sopenharmony_ci 81362306a36Sopenharmony_ci /* 81462306a36Sopenharmony_ci * Allocate file space until done or until there is an error 81562306a36Sopenharmony_ci */ 81662306a36Sopenharmony_ci while (allocatesize_fsb && !error) { 81762306a36Sopenharmony_ci xfs_fileoff_t s, e; 81862306a36Sopenharmony_ci unsigned int dblocks, rblocks, resblks; 81962306a36Sopenharmony_ci int nimaps = 1; 82062306a36Sopenharmony_ci 82162306a36Sopenharmony_ci /* 82262306a36Sopenharmony_ci * Determine space reservations for data/realtime. 82362306a36Sopenharmony_ci */ 82462306a36Sopenharmony_ci if (unlikely(extsz)) { 82562306a36Sopenharmony_ci s = startoffset_fsb; 82662306a36Sopenharmony_ci do_div(s, extsz); 82762306a36Sopenharmony_ci s *= extsz; 82862306a36Sopenharmony_ci e = startoffset_fsb + allocatesize_fsb; 82962306a36Sopenharmony_ci div_u64_rem(startoffset_fsb, extsz, &temp); 83062306a36Sopenharmony_ci if (temp) 83162306a36Sopenharmony_ci e += temp; 83262306a36Sopenharmony_ci div_u64_rem(e, extsz, &temp); 83362306a36Sopenharmony_ci if (temp) 83462306a36Sopenharmony_ci e += extsz - temp; 83562306a36Sopenharmony_ci } else { 83662306a36Sopenharmony_ci s = 0; 83762306a36Sopenharmony_ci e = allocatesize_fsb; 83862306a36Sopenharmony_ci } 83962306a36Sopenharmony_ci 84062306a36Sopenharmony_ci /* 84162306a36Sopenharmony_ci * The transaction reservation is limited to a 32-bit block 84262306a36Sopenharmony_ci * count, hence we need to limit the number of blocks we are 84362306a36Sopenharmony_ci * trying to reserve to avoid an overflow. We can't allocate 84462306a36Sopenharmony_ci * more than @nimaps extents, and an extent is limited on disk 84562306a36Sopenharmony_ci * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the 84662306a36Sopenharmony_ci * limit. 84762306a36Sopenharmony_ci */ 84862306a36Sopenharmony_ci resblks = min_t(xfs_fileoff_t, (e - s), 84962306a36Sopenharmony_ci (XFS_MAX_BMBT_EXTLEN * nimaps)); 85062306a36Sopenharmony_ci if (unlikely(rt)) { 85162306a36Sopenharmony_ci dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 85262306a36Sopenharmony_ci rblocks = resblks; 85362306a36Sopenharmony_ci } else { 85462306a36Sopenharmony_ci dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks); 85562306a36Sopenharmony_ci rblocks = 0; 85662306a36Sopenharmony_ci } 85762306a36Sopenharmony_ci 85862306a36Sopenharmony_ci error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 85962306a36Sopenharmony_ci dblocks, rblocks, false, &tp); 86062306a36Sopenharmony_ci if (error) 86162306a36Sopenharmony_ci break; 86262306a36Sopenharmony_ci 86362306a36Sopenharmony_ci error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, 86462306a36Sopenharmony_ci XFS_IEXT_ADD_NOSPLIT_CNT); 86562306a36Sopenharmony_ci if (error == -EFBIG) 86662306a36Sopenharmony_ci error = xfs_iext_count_upgrade(tp, ip, 86762306a36Sopenharmony_ci XFS_IEXT_ADD_NOSPLIT_CNT); 86862306a36Sopenharmony_ci if (error) 86962306a36Sopenharmony_ci goto error; 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci error = xfs_bmapi_write(tp, ip, startoffset_fsb, 87262306a36Sopenharmony_ci allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp, 87362306a36Sopenharmony_ci &nimaps); 87462306a36Sopenharmony_ci if (error) 87562306a36Sopenharmony_ci goto error; 87662306a36Sopenharmony_ci 87762306a36Sopenharmony_ci ip->i_diflags |= XFS_DIFLAG_PREALLOC; 87862306a36Sopenharmony_ci xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci error = xfs_trans_commit(tp); 88162306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 88262306a36Sopenharmony_ci if (error) 88362306a36Sopenharmony_ci break; 88462306a36Sopenharmony_ci 88562306a36Sopenharmony_ci /* 88662306a36Sopenharmony_ci * If the allocator cannot find a single free extent large 88762306a36Sopenharmony_ci * enough to cover the start block of the requested range, 88862306a36Sopenharmony_ci * xfs_bmapi_write will return 0 but leave *nimaps set to 0. 88962306a36Sopenharmony_ci * 89062306a36Sopenharmony_ci * In that case we simply need to keep looping with the same 89162306a36Sopenharmony_ci * startoffset_fsb so that one of the following allocations 89262306a36Sopenharmony_ci * will eventually reach the requested range. 89362306a36Sopenharmony_ci */ 89462306a36Sopenharmony_ci if (nimaps) { 89562306a36Sopenharmony_ci startoffset_fsb += imapp->br_blockcount; 89662306a36Sopenharmony_ci allocatesize_fsb -= imapp->br_blockcount; 89762306a36Sopenharmony_ci } 89862306a36Sopenharmony_ci } 89962306a36Sopenharmony_ci 90062306a36Sopenharmony_ci return error; 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_cierror: 90362306a36Sopenharmony_ci xfs_trans_cancel(tp); 90462306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 90562306a36Sopenharmony_ci return error; 90662306a36Sopenharmony_ci} 90762306a36Sopenharmony_ci 90862306a36Sopenharmony_cistatic int 90962306a36Sopenharmony_cixfs_unmap_extent( 91062306a36Sopenharmony_ci struct xfs_inode *ip, 91162306a36Sopenharmony_ci xfs_fileoff_t startoffset_fsb, 91262306a36Sopenharmony_ci xfs_filblks_t len_fsb, 91362306a36Sopenharmony_ci int *done) 91462306a36Sopenharmony_ci{ 91562306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 91662306a36Sopenharmony_ci struct xfs_trans *tp; 91762306a36Sopenharmony_ci uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 91862306a36Sopenharmony_ci int error; 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0, 92162306a36Sopenharmony_ci false, &tp); 92262306a36Sopenharmony_ci if (error) 92362306a36Sopenharmony_ci return error; 92462306a36Sopenharmony_ci 92562306a36Sopenharmony_ci error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, 92662306a36Sopenharmony_ci XFS_IEXT_PUNCH_HOLE_CNT); 92762306a36Sopenharmony_ci if (error == -EFBIG) 92862306a36Sopenharmony_ci error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT); 92962306a36Sopenharmony_ci if (error) 93062306a36Sopenharmony_ci goto out_trans_cancel; 93162306a36Sopenharmony_ci 93262306a36Sopenharmony_ci error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done); 93362306a36Sopenharmony_ci if (error) 93462306a36Sopenharmony_ci goto out_trans_cancel; 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci error = xfs_trans_commit(tp); 93762306a36Sopenharmony_ciout_unlock: 93862306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 93962306a36Sopenharmony_ci return error; 94062306a36Sopenharmony_ci 94162306a36Sopenharmony_ciout_trans_cancel: 94262306a36Sopenharmony_ci xfs_trans_cancel(tp); 94362306a36Sopenharmony_ci goto out_unlock; 94462306a36Sopenharmony_ci} 94562306a36Sopenharmony_ci 94662306a36Sopenharmony_ci/* Caller must first wait for the completion of any pending DIOs if required. */ 94762306a36Sopenharmony_ciint 94862306a36Sopenharmony_cixfs_flush_unmap_range( 94962306a36Sopenharmony_ci struct xfs_inode *ip, 95062306a36Sopenharmony_ci xfs_off_t offset, 95162306a36Sopenharmony_ci xfs_off_t len) 95262306a36Sopenharmony_ci{ 95362306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 95462306a36Sopenharmony_ci struct inode *inode = VFS_I(ip); 95562306a36Sopenharmony_ci xfs_off_t rounding, start, end; 95662306a36Sopenharmony_ci int error; 95762306a36Sopenharmony_ci 95862306a36Sopenharmony_ci rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE); 95962306a36Sopenharmony_ci start = round_down(offset, rounding); 96062306a36Sopenharmony_ci end = round_up(offset + len, rounding) - 1; 96162306a36Sopenharmony_ci 96262306a36Sopenharmony_ci error = filemap_write_and_wait_range(inode->i_mapping, start, end); 96362306a36Sopenharmony_ci if (error) 96462306a36Sopenharmony_ci return error; 96562306a36Sopenharmony_ci truncate_pagecache_range(inode, start, end); 96662306a36Sopenharmony_ci return 0; 96762306a36Sopenharmony_ci} 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ciint 97062306a36Sopenharmony_cixfs_free_file_space( 97162306a36Sopenharmony_ci struct xfs_inode *ip, 97262306a36Sopenharmony_ci xfs_off_t offset, 97362306a36Sopenharmony_ci xfs_off_t len) 97462306a36Sopenharmony_ci{ 97562306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 97662306a36Sopenharmony_ci xfs_fileoff_t startoffset_fsb; 97762306a36Sopenharmony_ci xfs_fileoff_t endoffset_fsb; 97862306a36Sopenharmony_ci int done = 0, error; 97962306a36Sopenharmony_ci 98062306a36Sopenharmony_ci trace_xfs_free_file_space(ip); 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci error = xfs_qm_dqattach(ip); 98362306a36Sopenharmony_ci if (error) 98462306a36Sopenharmony_ci return error; 98562306a36Sopenharmony_ci 98662306a36Sopenharmony_ci if (len <= 0) /* if nothing being freed */ 98762306a36Sopenharmony_ci return 0; 98862306a36Sopenharmony_ci 98962306a36Sopenharmony_ci startoffset_fsb = XFS_B_TO_FSB(mp, offset); 99062306a36Sopenharmony_ci endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); 99162306a36Sopenharmony_ci 99262306a36Sopenharmony_ci /* We can only free complete realtime extents. */ 99362306a36Sopenharmony_ci if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) { 99462306a36Sopenharmony_ci startoffset_fsb = roundup_64(startoffset_fsb, 99562306a36Sopenharmony_ci mp->m_sb.sb_rextsize); 99662306a36Sopenharmony_ci endoffset_fsb = rounddown_64(endoffset_fsb, 99762306a36Sopenharmony_ci mp->m_sb.sb_rextsize); 99862306a36Sopenharmony_ci } 99962306a36Sopenharmony_ci 100062306a36Sopenharmony_ci /* 100162306a36Sopenharmony_ci * Need to zero the stuff we're not freeing, on disk. 100262306a36Sopenharmony_ci */ 100362306a36Sopenharmony_ci if (endoffset_fsb > startoffset_fsb) { 100462306a36Sopenharmony_ci while (!done) { 100562306a36Sopenharmony_ci error = xfs_unmap_extent(ip, startoffset_fsb, 100662306a36Sopenharmony_ci endoffset_fsb - startoffset_fsb, &done); 100762306a36Sopenharmony_ci if (error) 100862306a36Sopenharmony_ci return error; 100962306a36Sopenharmony_ci } 101062306a36Sopenharmony_ci } 101162306a36Sopenharmony_ci 101262306a36Sopenharmony_ci /* 101362306a36Sopenharmony_ci * Now that we've unmap all full blocks we'll have to zero out any 101462306a36Sopenharmony_ci * partial block at the beginning and/or end. xfs_zero_range is smart 101562306a36Sopenharmony_ci * enough to skip any holes, including those we just created, but we 101662306a36Sopenharmony_ci * must take care not to zero beyond EOF and enlarge i_size. 101762306a36Sopenharmony_ci */ 101862306a36Sopenharmony_ci if (offset >= XFS_ISIZE(ip)) 101962306a36Sopenharmony_ci return 0; 102062306a36Sopenharmony_ci if (offset + len > XFS_ISIZE(ip)) 102162306a36Sopenharmony_ci len = XFS_ISIZE(ip) - offset; 102262306a36Sopenharmony_ci error = xfs_zero_range(ip, offset, len, NULL); 102362306a36Sopenharmony_ci if (error) 102462306a36Sopenharmony_ci return error; 102562306a36Sopenharmony_ci 102662306a36Sopenharmony_ci /* 102762306a36Sopenharmony_ci * If we zeroed right up to EOF and EOF straddles a page boundary we 102862306a36Sopenharmony_ci * must make sure that the post-EOF area is also zeroed because the 102962306a36Sopenharmony_ci * page could be mmap'd and xfs_zero_range doesn't do that for us. 103062306a36Sopenharmony_ci * Writeback of the eof page will do this, albeit clumsily. 103162306a36Sopenharmony_ci */ 103262306a36Sopenharmony_ci if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) { 103362306a36Sopenharmony_ci error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, 103462306a36Sopenharmony_ci round_down(offset + len, PAGE_SIZE), LLONG_MAX); 103562306a36Sopenharmony_ci } 103662306a36Sopenharmony_ci 103762306a36Sopenharmony_ci return error; 103862306a36Sopenharmony_ci} 103962306a36Sopenharmony_ci 104062306a36Sopenharmony_cistatic int 104162306a36Sopenharmony_cixfs_prepare_shift( 104262306a36Sopenharmony_ci struct xfs_inode *ip, 104362306a36Sopenharmony_ci loff_t offset) 104462306a36Sopenharmony_ci{ 104562306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 104662306a36Sopenharmony_ci int error; 104762306a36Sopenharmony_ci 104862306a36Sopenharmony_ci /* 104962306a36Sopenharmony_ci * Trim eofblocks to avoid shifting uninitialized post-eof preallocation 105062306a36Sopenharmony_ci * into the accessible region of the file. 105162306a36Sopenharmony_ci */ 105262306a36Sopenharmony_ci if (xfs_can_free_eofblocks(ip, true)) { 105362306a36Sopenharmony_ci error = xfs_free_eofblocks(ip); 105462306a36Sopenharmony_ci if (error) 105562306a36Sopenharmony_ci return error; 105662306a36Sopenharmony_ci } 105762306a36Sopenharmony_ci 105862306a36Sopenharmony_ci /* 105962306a36Sopenharmony_ci * Shift operations must stabilize the start block offset boundary along 106062306a36Sopenharmony_ci * with the full range of the operation. If we don't, a COW writeback 106162306a36Sopenharmony_ci * completion could race with an insert, front merge with the start 106262306a36Sopenharmony_ci * extent (after split) during the shift and corrupt the file. Start 106362306a36Sopenharmony_ci * with the block just prior to the start to stabilize the boundary. 106462306a36Sopenharmony_ci */ 106562306a36Sopenharmony_ci offset = round_down(offset, mp->m_sb.sb_blocksize); 106662306a36Sopenharmony_ci if (offset) 106762306a36Sopenharmony_ci offset -= mp->m_sb.sb_blocksize; 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci /* 107062306a36Sopenharmony_ci * Writeback and invalidate cache for the remainder of the file as we're 107162306a36Sopenharmony_ci * about to shift down every extent from offset to EOF. 107262306a36Sopenharmony_ci */ 107362306a36Sopenharmony_ci error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip)); 107462306a36Sopenharmony_ci if (error) 107562306a36Sopenharmony_ci return error; 107662306a36Sopenharmony_ci 107762306a36Sopenharmony_ci /* 107862306a36Sopenharmony_ci * Clean out anything hanging around in the cow fork now that 107962306a36Sopenharmony_ci * we've flushed all the dirty data out to disk to avoid having 108062306a36Sopenharmony_ci * CoW extents at the wrong offsets. 108162306a36Sopenharmony_ci */ 108262306a36Sopenharmony_ci if (xfs_inode_has_cow_data(ip)) { 108362306a36Sopenharmony_ci error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF, 108462306a36Sopenharmony_ci true); 108562306a36Sopenharmony_ci if (error) 108662306a36Sopenharmony_ci return error; 108762306a36Sopenharmony_ci } 108862306a36Sopenharmony_ci 108962306a36Sopenharmony_ci return 0; 109062306a36Sopenharmony_ci} 109162306a36Sopenharmony_ci 109262306a36Sopenharmony_ci/* 109362306a36Sopenharmony_ci * xfs_collapse_file_space() 109462306a36Sopenharmony_ci * This routine frees disk space and shift extent for the given file. 109562306a36Sopenharmony_ci * The first thing we do is to free data blocks in the specified range 109662306a36Sopenharmony_ci * by calling xfs_free_file_space(). It would also sync dirty data 109762306a36Sopenharmony_ci * and invalidate page cache over the region on which collapse range 109862306a36Sopenharmony_ci * is working. And Shift extent records to the left to cover a hole. 109962306a36Sopenharmony_ci * RETURNS: 110062306a36Sopenharmony_ci * 0 on success 110162306a36Sopenharmony_ci * errno on error 110262306a36Sopenharmony_ci * 110362306a36Sopenharmony_ci */ 110462306a36Sopenharmony_ciint 110562306a36Sopenharmony_cixfs_collapse_file_space( 110662306a36Sopenharmony_ci struct xfs_inode *ip, 110762306a36Sopenharmony_ci xfs_off_t offset, 110862306a36Sopenharmony_ci xfs_off_t len) 110962306a36Sopenharmony_ci{ 111062306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 111162306a36Sopenharmony_ci struct xfs_trans *tp; 111262306a36Sopenharmony_ci int error; 111362306a36Sopenharmony_ci xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len); 111462306a36Sopenharmony_ci xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); 111562306a36Sopenharmony_ci bool done = false; 111662306a36Sopenharmony_ci 111762306a36Sopenharmony_ci ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 111862306a36Sopenharmony_ci ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); 111962306a36Sopenharmony_ci 112062306a36Sopenharmony_ci trace_xfs_collapse_file_space(ip); 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_ci error = xfs_free_file_space(ip, offset, len); 112362306a36Sopenharmony_ci if (error) 112462306a36Sopenharmony_ci return error; 112562306a36Sopenharmony_ci 112662306a36Sopenharmony_ci error = xfs_prepare_shift(ip, offset); 112762306a36Sopenharmony_ci if (error) 112862306a36Sopenharmony_ci return error; 112962306a36Sopenharmony_ci 113062306a36Sopenharmony_ci error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); 113162306a36Sopenharmony_ci if (error) 113262306a36Sopenharmony_ci return error; 113362306a36Sopenharmony_ci 113462306a36Sopenharmony_ci xfs_ilock(ip, XFS_ILOCK_EXCL); 113562306a36Sopenharmony_ci xfs_trans_ijoin(tp, ip, 0); 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci while (!done) { 113862306a36Sopenharmony_ci error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb, 113962306a36Sopenharmony_ci &done); 114062306a36Sopenharmony_ci if (error) 114162306a36Sopenharmony_ci goto out_trans_cancel; 114262306a36Sopenharmony_ci if (done) 114362306a36Sopenharmony_ci break; 114462306a36Sopenharmony_ci 114562306a36Sopenharmony_ci /* finish any deferred frees and roll the transaction */ 114662306a36Sopenharmony_ci error = xfs_defer_finish(&tp); 114762306a36Sopenharmony_ci if (error) 114862306a36Sopenharmony_ci goto out_trans_cancel; 114962306a36Sopenharmony_ci } 115062306a36Sopenharmony_ci 115162306a36Sopenharmony_ci error = xfs_trans_commit(tp); 115262306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 115362306a36Sopenharmony_ci return error; 115462306a36Sopenharmony_ci 115562306a36Sopenharmony_ciout_trans_cancel: 115662306a36Sopenharmony_ci xfs_trans_cancel(tp); 115762306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 115862306a36Sopenharmony_ci return error; 115962306a36Sopenharmony_ci} 116062306a36Sopenharmony_ci 116162306a36Sopenharmony_ci/* 116262306a36Sopenharmony_ci * xfs_insert_file_space() 116362306a36Sopenharmony_ci * This routine create hole space by shifting extents for the given file. 116462306a36Sopenharmony_ci * The first thing we do is to sync dirty data and invalidate page cache 116562306a36Sopenharmony_ci * over the region on which insert range is working. And split an extent 116662306a36Sopenharmony_ci * to two extents at given offset by calling xfs_bmap_split_extent. 116762306a36Sopenharmony_ci * And shift all extent records which are laying between [offset, 116862306a36Sopenharmony_ci * last allocated extent] to the right to reserve hole range. 116962306a36Sopenharmony_ci * RETURNS: 117062306a36Sopenharmony_ci * 0 on success 117162306a36Sopenharmony_ci * errno on error 117262306a36Sopenharmony_ci */ 117362306a36Sopenharmony_ciint 117462306a36Sopenharmony_cixfs_insert_file_space( 117562306a36Sopenharmony_ci struct xfs_inode *ip, 117662306a36Sopenharmony_ci loff_t offset, 117762306a36Sopenharmony_ci loff_t len) 117862306a36Sopenharmony_ci{ 117962306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 118062306a36Sopenharmony_ci struct xfs_trans *tp; 118162306a36Sopenharmony_ci int error; 118262306a36Sopenharmony_ci xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset); 118362306a36Sopenharmony_ci xfs_fileoff_t next_fsb = NULLFSBLOCK; 118462306a36Sopenharmony_ci xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); 118562306a36Sopenharmony_ci bool done = false; 118662306a36Sopenharmony_ci 118762306a36Sopenharmony_ci ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 118862306a36Sopenharmony_ci ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); 118962306a36Sopenharmony_ci 119062306a36Sopenharmony_ci trace_xfs_insert_file_space(ip); 119162306a36Sopenharmony_ci 119262306a36Sopenharmony_ci error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb); 119362306a36Sopenharmony_ci if (error) 119462306a36Sopenharmony_ci return error; 119562306a36Sopenharmony_ci 119662306a36Sopenharmony_ci error = xfs_prepare_shift(ip, offset); 119762306a36Sopenharmony_ci if (error) 119862306a36Sopenharmony_ci return error; 119962306a36Sopenharmony_ci 120062306a36Sopenharmony_ci error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 120162306a36Sopenharmony_ci XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); 120262306a36Sopenharmony_ci if (error) 120362306a36Sopenharmony_ci return error; 120462306a36Sopenharmony_ci 120562306a36Sopenharmony_ci xfs_ilock(ip, XFS_ILOCK_EXCL); 120662306a36Sopenharmony_ci xfs_trans_ijoin(tp, ip, 0); 120762306a36Sopenharmony_ci 120862306a36Sopenharmony_ci error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, 120962306a36Sopenharmony_ci XFS_IEXT_PUNCH_HOLE_CNT); 121062306a36Sopenharmony_ci if (error == -EFBIG) 121162306a36Sopenharmony_ci error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT); 121262306a36Sopenharmony_ci if (error) 121362306a36Sopenharmony_ci goto out_trans_cancel; 121462306a36Sopenharmony_ci 121562306a36Sopenharmony_ci /* 121662306a36Sopenharmony_ci * The extent shifting code works on extent granularity. So, if stop_fsb 121762306a36Sopenharmony_ci * is not the starting block of extent, we need to split the extent at 121862306a36Sopenharmony_ci * stop_fsb. 121962306a36Sopenharmony_ci */ 122062306a36Sopenharmony_ci error = xfs_bmap_split_extent(tp, ip, stop_fsb); 122162306a36Sopenharmony_ci if (error) 122262306a36Sopenharmony_ci goto out_trans_cancel; 122362306a36Sopenharmony_ci 122462306a36Sopenharmony_ci do { 122562306a36Sopenharmony_ci error = xfs_defer_finish(&tp); 122662306a36Sopenharmony_ci if (error) 122762306a36Sopenharmony_ci goto out_trans_cancel; 122862306a36Sopenharmony_ci 122962306a36Sopenharmony_ci error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb, 123062306a36Sopenharmony_ci &done, stop_fsb); 123162306a36Sopenharmony_ci if (error) 123262306a36Sopenharmony_ci goto out_trans_cancel; 123362306a36Sopenharmony_ci } while (!done); 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci error = xfs_trans_commit(tp); 123662306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 123762306a36Sopenharmony_ci return error; 123862306a36Sopenharmony_ci 123962306a36Sopenharmony_ciout_trans_cancel: 124062306a36Sopenharmony_ci xfs_trans_cancel(tp); 124162306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 124262306a36Sopenharmony_ci return error; 124362306a36Sopenharmony_ci} 124462306a36Sopenharmony_ci 124562306a36Sopenharmony_ci/* 124662306a36Sopenharmony_ci * We need to check that the format of the data fork in the temporary inode is 124762306a36Sopenharmony_ci * valid for the target inode before doing the swap. This is not a problem with 124862306a36Sopenharmony_ci * attr1 because of the fixed fork offset, but attr2 has a dynamically sized 124962306a36Sopenharmony_ci * data fork depending on the space the attribute fork is taking so we can get 125062306a36Sopenharmony_ci * invalid formats on the target inode. 125162306a36Sopenharmony_ci * 125262306a36Sopenharmony_ci * E.g. target has space for 7 extents in extent format, temp inode only has 125362306a36Sopenharmony_ci * space for 6. If we defragment down to 7 extents, then the tmp format is a 125462306a36Sopenharmony_ci * btree, but when swapped it needs to be in extent format. Hence we can't just 125562306a36Sopenharmony_ci * blindly swap data forks on attr2 filesystems. 125662306a36Sopenharmony_ci * 125762306a36Sopenharmony_ci * Note that we check the swap in both directions so that we don't end up with 125862306a36Sopenharmony_ci * a corrupt temporary inode, either. 125962306a36Sopenharmony_ci * 126062306a36Sopenharmony_ci * Note that fixing the way xfs_fsr sets up the attribute fork in the source 126162306a36Sopenharmony_ci * inode will prevent this situation from occurring, so all we do here is 126262306a36Sopenharmony_ci * reject and log the attempt. basically we are putting the responsibility on 126362306a36Sopenharmony_ci * userspace to get this right. 126462306a36Sopenharmony_ci */ 126562306a36Sopenharmony_cistatic int 126662306a36Sopenharmony_cixfs_swap_extents_check_format( 126762306a36Sopenharmony_ci struct xfs_inode *ip, /* target inode */ 126862306a36Sopenharmony_ci struct xfs_inode *tip) /* tmp inode */ 126962306a36Sopenharmony_ci{ 127062306a36Sopenharmony_ci struct xfs_ifork *ifp = &ip->i_df; 127162306a36Sopenharmony_ci struct xfs_ifork *tifp = &tip->i_df; 127262306a36Sopenharmony_ci 127362306a36Sopenharmony_ci /* User/group/project quota ids must match if quotas are enforced. */ 127462306a36Sopenharmony_ci if (XFS_IS_QUOTA_ON(ip->i_mount) && 127562306a36Sopenharmony_ci (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) || 127662306a36Sopenharmony_ci !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) || 127762306a36Sopenharmony_ci ip->i_projid != tip->i_projid)) 127862306a36Sopenharmony_ci return -EINVAL; 127962306a36Sopenharmony_ci 128062306a36Sopenharmony_ci /* Should never get a local format */ 128162306a36Sopenharmony_ci if (ifp->if_format == XFS_DINODE_FMT_LOCAL || 128262306a36Sopenharmony_ci tifp->if_format == XFS_DINODE_FMT_LOCAL) 128362306a36Sopenharmony_ci return -EINVAL; 128462306a36Sopenharmony_ci 128562306a36Sopenharmony_ci /* 128662306a36Sopenharmony_ci * if the target inode has less extents that then temporary inode then 128762306a36Sopenharmony_ci * why did userspace call us? 128862306a36Sopenharmony_ci */ 128962306a36Sopenharmony_ci if (ifp->if_nextents < tifp->if_nextents) 129062306a36Sopenharmony_ci return -EINVAL; 129162306a36Sopenharmony_ci 129262306a36Sopenharmony_ci /* 129362306a36Sopenharmony_ci * If we have to use the (expensive) rmap swap method, we can 129462306a36Sopenharmony_ci * handle any number of extents and any format. 129562306a36Sopenharmony_ci */ 129662306a36Sopenharmony_ci if (xfs_has_rmapbt(ip->i_mount)) 129762306a36Sopenharmony_ci return 0; 129862306a36Sopenharmony_ci 129962306a36Sopenharmony_ci /* 130062306a36Sopenharmony_ci * if the target inode is in extent form and the temp inode is in btree 130162306a36Sopenharmony_ci * form then we will end up with the target inode in the wrong format 130262306a36Sopenharmony_ci * as we already know there are less extents in the temp inode. 130362306a36Sopenharmony_ci */ 130462306a36Sopenharmony_ci if (ifp->if_format == XFS_DINODE_FMT_EXTENTS && 130562306a36Sopenharmony_ci tifp->if_format == XFS_DINODE_FMT_BTREE) 130662306a36Sopenharmony_ci return -EINVAL; 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_ci /* Check temp in extent form to max in target */ 130962306a36Sopenharmony_ci if (tifp->if_format == XFS_DINODE_FMT_EXTENTS && 131062306a36Sopenharmony_ci tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) 131162306a36Sopenharmony_ci return -EINVAL; 131262306a36Sopenharmony_ci 131362306a36Sopenharmony_ci /* Check target in extent form to max in temp */ 131462306a36Sopenharmony_ci if (ifp->if_format == XFS_DINODE_FMT_EXTENTS && 131562306a36Sopenharmony_ci ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) 131662306a36Sopenharmony_ci return -EINVAL; 131762306a36Sopenharmony_ci 131862306a36Sopenharmony_ci /* 131962306a36Sopenharmony_ci * If we are in a btree format, check that the temp root block will fit 132062306a36Sopenharmony_ci * in the target and that it has enough extents to be in btree format 132162306a36Sopenharmony_ci * in the target. 132262306a36Sopenharmony_ci * 132362306a36Sopenharmony_ci * Note that we have to be careful to allow btree->extent conversions 132462306a36Sopenharmony_ci * (a common defrag case) which will occur when the temp inode is in 132562306a36Sopenharmony_ci * extent format... 132662306a36Sopenharmony_ci */ 132762306a36Sopenharmony_ci if (tifp->if_format == XFS_DINODE_FMT_BTREE) { 132862306a36Sopenharmony_ci if (xfs_inode_has_attr_fork(ip) && 132962306a36Sopenharmony_ci XFS_BMAP_BMDR_SPACE(tifp->if_broot) > xfs_inode_fork_boff(ip)) 133062306a36Sopenharmony_ci return -EINVAL; 133162306a36Sopenharmony_ci if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) 133262306a36Sopenharmony_ci return -EINVAL; 133362306a36Sopenharmony_ci } 133462306a36Sopenharmony_ci 133562306a36Sopenharmony_ci /* Reciprocal target->temp btree format checks */ 133662306a36Sopenharmony_ci if (ifp->if_format == XFS_DINODE_FMT_BTREE) { 133762306a36Sopenharmony_ci if (xfs_inode_has_attr_fork(tip) && 133862306a36Sopenharmony_ci XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > xfs_inode_fork_boff(tip)) 133962306a36Sopenharmony_ci return -EINVAL; 134062306a36Sopenharmony_ci if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) 134162306a36Sopenharmony_ci return -EINVAL; 134262306a36Sopenharmony_ci } 134362306a36Sopenharmony_ci 134462306a36Sopenharmony_ci return 0; 134562306a36Sopenharmony_ci} 134662306a36Sopenharmony_ci 134762306a36Sopenharmony_cistatic int 134862306a36Sopenharmony_cixfs_swap_extent_flush( 134962306a36Sopenharmony_ci struct xfs_inode *ip) 135062306a36Sopenharmony_ci{ 135162306a36Sopenharmony_ci int error; 135262306a36Sopenharmony_ci 135362306a36Sopenharmony_ci error = filemap_write_and_wait(VFS_I(ip)->i_mapping); 135462306a36Sopenharmony_ci if (error) 135562306a36Sopenharmony_ci return error; 135662306a36Sopenharmony_ci truncate_pagecache_range(VFS_I(ip), 0, -1); 135762306a36Sopenharmony_ci 135862306a36Sopenharmony_ci /* Verify O_DIRECT for ftmp */ 135962306a36Sopenharmony_ci if (VFS_I(ip)->i_mapping->nrpages) 136062306a36Sopenharmony_ci return -EINVAL; 136162306a36Sopenharmony_ci return 0; 136262306a36Sopenharmony_ci} 136362306a36Sopenharmony_ci 136462306a36Sopenharmony_ci/* 136562306a36Sopenharmony_ci * Move extents from one file to another, when rmap is enabled. 136662306a36Sopenharmony_ci */ 136762306a36Sopenharmony_ciSTATIC int 136862306a36Sopenharmony_cixfs_swap_extent_rmap( 136962306a36Sopenharmony_ci struct xfs_trans **tpp, 137062306a36Sopenharmony_ci struct xfs_inode *ip, 137162306a36Sopenharmony_ci struct xfs_inode *tip) 137262306a36Sopenharmony_ci{ 137362306a36Sopenharmony_ci struct xfs_trans *tp = *tpp; 137462306a36Sopenharmony_ci struct xfs_bmbt_irec irec; 137562306a36Sopenharmony_ci struct xfs_bmbt_irec uirec; 137662306a36Sopenharmony_ci struct xfs_bmbt_irec tirec; 137762306a36Sopenharmony_ci xfs_fileoff_t offset_fsb; 137862306a36Sopenharmony_ci xfs_fileoff_t end_fsb; 137962306a36Sopenharmony_ci xfs_filblks_t count_fsb; 138062306a36Sopenharmony_ci int error; 138162306a36Sopenharmony_ci xfs_filblks_t ilen; 138262306a36Sopenharmony_ci xfs_filblks_t rlen; 138362306a36Sopenharmony_ci int nimaps; 138462306a36Sopenharmony_ci uint64_t tip_flags2; 138562306a36Sopenharmony_ci 138662306a36Sopenharmony_ci /* 138762306a36Sopenharmony_ci * If the source file has shared blocks, we must flag the donor 138862306a36Sopenharmony_ci * file as having shared blocks so that we get the shared-block 138962306a36Sopenharmony_ci * rmap functions when we go to fix up the rmaps. The flags 139062306a36Sopenharmony_ci * will be switch for reals later. 139162306a36Sopenharmony_ci */ 139262306a36Sopenharmony_ci tip_flags2 = tip->i_diflags2; 139362306a36Sopenharmony_ci if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK) 139462306a36Sopenharmony_ci tip->i_diflags2 |= XFS_DIFLAG2_REFLINK; 139562306a36Sopenharmony_ci 139662306a36Sopenharmony_ci offset_fsb = 0; 139762306a36Sopenharmony_ci end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip))); 139862306a36Sopenharmony_ci count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); 139962306a36Sopenharmony_ci 140062306a36Sopenharmony_ci while (count_fsb) { 140162306a36Sopenharmony_ci /* Read extent from the donor file */ 140262306a36Sopenharmony_ci nimaps = 1; 140362306a36Sopenharmony_ci error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec, 140462306a36Sopenharmony_ci &nimaps, 0); 140562306a36Sopenharmony_ci if (error) 140662306a36Sopenharmony_ci goto out; 140762306a36Sopenharmony_ci ASSERT(nimaps == 1); 140862306a36Sopenharmony_ci ASSERT(tirec.br_startblock != DELAYSTARTBLOCK); 140962306a36Sopenharmony_ci 141062306a36Sopenharmony_ci trace_xfs_swap_extent_rmap_remap(tip, &tirec); 141162306a36Sopenharmony_ci ilen = tirec.br_blockcount; 141262306a36Sopenharmony_ci 141362306a36Sopenharmony_ci /* Unmap the old blocks in the source file. */ 141462306a36Sopenharmony_ci while (tirec.br_blockcount) { 141562306a36Sopenharmony_ci ASSERT(tp->t_highest_agno == NULLAGNUMBER); 141662306a36Sopenharmony_ci trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec); 141762306a36Sopenharmony_ci 141862306a36Sopenharmony_ci /* Read extent from the source file */ 141962306a36Sopenharmony_ci nimaps = 1; 142062306a36Sopenharmony_ci error = xfs_bmapi_read(ip, tirec.br_startoff, 142162306a36Sopenharmony_ci tirec.br_blockcount, &irec, 142262306a36Sopenharmony_ci &nimaps, 0); 142362306a36Sopenharmony_ci if (error) 142462306a36Sopenharmony_ci goto out; 142562306a36Sopenharmony_ci ASSERT(nimaps == 1); 142662306a36Sopenharmony_ci ASSERT(tirec.br_startoff == irec.br_startoff); 142762306a36Sopenharmony_ci trace_xfs_swap_extent_rmap_remap_piece(ip, &irec); 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_ci /* Trim the extent. */ 143062306a36Sopenharmony_ci uirec = tirec; 143162306a36Sopenharmony_ci uirec.br_blockcount = rlen = min_t(xfs_filblks_t, 143262306a36Sopenharmony_ci tirec.br_blockcount, 143362306a36Sopenharmony_ci irec.br_blockcount); 143462306a36Sopenharmony_ci trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec); 143562306a36Sopenharmony_ci 143662306a36Sopenharmony_ci if (xfs_bmap_is_real_extent(&uirec)) { 143762306a36Sopenharmony_ci error = xfs_iext_count_may_overflow(ip, 143862306a36Sopenharmony_ci XFS_DATA_FORK, 143962306a36Sopenharmony_ci XFS_IEXT_SWAP_RMAP_CNT); 144062306a36Sopenharmony_ci if (error == -EFBIG) 144162306a36Sopenharmony_ci error = xfs_iext_count_upgrade(tp, ip, 144262306a36Sopenharmony_ci XFS_IEXT_SWAP_RMAP_CNT); 144362306a36Sopenharmony_ci if (error) 144462306a36Sopenharmony_ci goto out; 144562306a36Sopenharmony_ci } 144662306a36Sopenharmony_ci 144762306a36Sopenharmony_ci if (xfs_bmap_is_real_extent(&irec)) { 144862306a36Sopenharmony_ci error = xfs_iext_count_may_overflow(tip, 144962306a36Sopenharmony_ci XFS_DATA_FORK, 145062306a36Sopenharmony_ci XFS_IEXT_SWAP_RMAP_CNT); 145162306a36Sopenharmony_ci if (error == -EFBIG) 145262306a36Sopenharmony_ci error = xfs_iext_count_upgrade(tp, ip, 145362306a36Sopenharmony_ci XFS_IEXT_SWAP_RMAP_CNT); 145462306a36Sopenharmony_ci if (error) 145562306a36Sopenharmony_ci goto out; 145662306a36Sopenharmony_ci } 145762306a36Sopenharmony_ci 145862306a36Sopenharmony_ci /* Remove the mapping from the donor file. */ 145962306a36Sopenharmony_ci xfs_bmap_unmap_extent(tp, tip, &uirec); 146062306a36Sopenharmony_ci 146162306a36Sopenharmony_ci /* Remove the mapping from the source file. */ 146262306a36Sopenharmony_ci xfs_bmap_unmap_extent(tp, ip, &irec); 146362306a36Sopenharmony_ci 146462306a36Sopenharmony_ci /* Map the donor file's blocks into the source file. */ 146562306a36Sopenharmony_ci xfs_bmap_map_extent(tp, ip, &uirec); 146662306a36Sopenharmony_ci 146762306a36Sopenharmony_ci /* Map the source file's blocks into the donor file. */ 146862306a36Sopenharmony_ci xfs_bmap_map_extent(tp, tip, &irec); 146962306a36Sopenharmony_ci 147062306a36Sopenharmony_ci error = xfs_defer_finish(tpp); 147162306a36Sopenharmony_ci tp = *tpp; 147262306a36Sopenharmony_ci if (error) 147362306a36Sopenharmony_ci goto out; 147462306a36Sopenharmony_ci 147562306a36Sopenharmony_ci tirec.br_startoff += rlen; 147662306a36Sopenharmony_ci if (tirec.br_startblock != HOLESTARTBLOCK && 147762306a36Sopenharmony_ci tirec.br_startblock != DELAYSTARTBLOCK) 147862306a36Sopenharmony_ci tirec.br_startblock += rlen; 147962306a36Sopenharmony_ci tirec.br_blockcount -= rlen; 148062306a36Sopenharmony_ci } 148162306a36Sopenharmony_ci 148262306a36Sopenharmony_ci /* Roll on... */ 148362306a36Sopenharmony_ci count_fsb -= ilen; 148462306a36Sopenharmony_ci offset_fsb += ilen; 148562306a36Sopenharmony_ci } 148662306a36Sopenharmony_ci 148762306a36Sopenharmony_ci tip->i_diflags2 = tip_flags2; 148862306a36Sopenharmony_ci return 0; 148962306a36Sopenharmony_ci 149062306a36Sopenharmony_ciout: 149162306a36Sopenharmony_ci trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_); 149262306a36Sopenharmony_ci tip->i_diflags2 = tip_flags2; 149362306a36Sopenharmony_ci return error; 149462306a36Sopenharmony_ci} 149562306a36Sopenharmony_ci 149662306a36Sopenharmony_ci/* Swap the extents of two files by swapping data forks. */ 149762306a36Sopenharmony_ciSTATIC int 149862306a36Sopenharmony_cixfs_swap_extent_forks( 149962306a36Sopenharmony_ci struct xfs_trans *tp, 150062306a36Sopenharmony_ci struct xfs_inode *ip, 150162306a36Sopenharmony_ci struct xfs_inode *tip, 150262306a36Sopenharmony_ci int *src_log_flags, 150362306a36Sopenharmony_ci int *target_log_flags) 150462306a36Sopenharmony_ci{ 150562306a36Sopenharmony_ci xfs_filblks_t aforkblks = 0; 150662306a36Sopenharmony_ci xfs_filblks_t taforkblks = 0; 150762306a36Sopenharmony_ci xfs_extnum_t junk; 150862306a36Sopenharmony_ci uint64_t tmp; 150962306a36Sopenharmony_ci int error; 151062306a36Sopenharmony_ci 151162306a36Sopenharmony_ci /* 151262306a36Sopenharmony_ci * Count the number of extended attribute blocks 151362306a36Sopenharmony_ci */ 151462306a36Sopenharmony_ci if (xfs_inode_has_attr_fork(ip) && ip->i_af.if_nextents > 0 && 151562306a36Sopenharmony_ci ip->i_af.if_format != XFS_DINODE_FMT_LOCAL) { 151662306a36Sopenharmony_ci error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk, 151762306a36Sopenharmony_ci &aforkblks); 151862306a36Sopenharmony_ci if (error) 151962306a36Sopenharmony_ci return error; 152062306a36Sopenharmony_ci } 152162306a36Sopenharmony_ci if (xfs_inode_has_attr_fork(tip) && tip->i_af.if_nextents > 0 && 152262306a36Sopenharmony_ci tip->i_af.if_format != XFS_DINODE_FMT_LOCAL) { 152362306a36Sopenharmony_ci error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk, 152462306a36Sopenharmony_ci &taforkblks); 152562306a36Sopenharmony_ci if (error) 152662306a36Sopenharmony_ci return error; 152762306a36Sopenharmony_ci } 152862306a36Sopenharmony_ci 152962306a36Sopenharmony_ci /* 153062306a36Sopenharmony_ci * Btree format (v3) inodes have the inode number stamped in the bmbt 153162306a36Sopenharmony_ci * block headers. We can't start changing the bmbt blocks until the 153262306a36Sopenharmony_ci * inode owner change is logged so recovery does the right thing in the 153362306a36Sopenharmony_ci * event of a crash. Set the owner change log flags now and leave the 153462306a36Sopenharmony_ci * bmbt scan as the last step. 153562306a36Sopenharmony_ci */ 153662306a36Sopenharmony_ci if (xfs_has_v3inodes(ip->i_mount)) { 153762306a36Sopenharmony_ci if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE) 153862306a36Sopenharmony_ci (*target_log_flags) |= XFS_ILOG_DOWNER; 153962306a36Sopenharmony_ci if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE) 154062306a36Sopenharmony_ci (*src_log_flags) |= XFS_ILOG_DOWNER; 154162306a36Sopenharmony_ci } 154262306a36Sopenharmony_ci 154362306a36Sopenharmony_ci /* 154462306a36Sopenharmony_ci * Swap the data forks of the inodes 154562306a36Sopenharmony_ci */ 154662306a36Sopenharmony_ci swap(ip->i_df, tip->i_df); 154762306a36Sopenharmony_ci 154862306a36Sopenharmony_ci /* 154962306a36Sopenharmony_ci * Fix the on-disk inode values 155062306a36Sopenharmony_ci */ 155162306a36Sopenharmony_ci tmp = (uint64_t)ip->i_nblocks; 155262306a36Sopenharmony_ci ip->i_nblocks = tip->i_nblocks - taforkblks + aforkblks; 155362306a36Sopenharmony_ci tip->i_nblocks = tmp + taforkblks - aforkblks; 155462306a36Sopenharmony_ci 155562306a36Sopenharmony_ci /* 155662306a36Sopenharmony_ci * The extents in the source inode could still contain speculative 155762306a36Sopenharmony_ci * preallocation beyond EOF (e.g. the file is open but not modified 155862306a36Sopenharmony_ci * while defrag is in progress). In that case, we need to copy over the 155962306a36Sopenharmony_ci * number of delalloc blocks the data fork in the source inode is 156062306a36Sopenharmony_ci * tracking beyond EOF so that when the fork is truncated away when the 156162306a36Sopenharmony_ci * temporary inode is unlinked we don't underrun the i_delayed_blks 156262306a36Sopenharmony_ci * counter on that inode. 156362306a36Sopenharmony_ci */ 156462306a36Sopenharmony_ci ASSERT(tip->i_delayed_blks == 0); 156562306a36Sopenharmony_ci tip->i_delayed_blks = ip->i_delayed_blks; 156662306a36Sopenharmony_ci ip->i_delayed_blks = 0; 156762306a36Sopenharmony_ci 156862306a36Sopenharmony_ci switch (ip->i_df.if_format) { 156962306a36Sopenharmony_ci case XFS_DINODE_FMT_EXTENTS: 157062306a36Sopenharmony_ci (*src_log_flags) |= XFS_ILOG_DEXT; 157162306a36Sopenharmony_ci break; 157262306a36Sopenharmony_ci case XFS_DINODE_FMT_BTREE: 157362306a36Sopenharmony_ci ASSERT(!xfs_has_v3inodes(ip->i_mount) || 157462306a36Sopenharmony_ci (*src_log_flags & XFS_ILOG_DOWNER)); 157562306a36Sopenharmony_ci (*src_log_flags) |= XFS_ILOG_DBROOT; 157662306a36Sopenharmony_ci break; 157762306a36Sopenharmony_ci } 157862306a36Sopenharmony_ci 157962306a36Sopenharmony_ci switch (tip->i_df.if_format) { 158062306a36Sopenharmony_ci case XFS_DINODE_FMT_EXTENTS: 158162306a36Sopenharmony_ci (*target_log_flags) |= XFS_ILOG_DEXT; 158262306a36Sopenharmony_ci break; 158362306a36Sopenharmony_ci case XFS_DINODE_FMT_BTREE: 158462306a36Sopenharmony_ci (*target_log_flags) |= XFS_ILOG_DBROOT; 158562306a36Sopenharmony_ci ASSERT(!xfs_has_v3inodes(ip->i_mount) || 158662306a36Sopenharmony_ci (*target_log_flags & XFS_ILOG_DOWNER)); 158762306a36Sopenharmony_ci break; 158862306a36Sopenharmony_ci } 158962306a36Sopenharmony_ci 159062306a36Sopenharmony_ci return 0; 159162306a36Sopenharmony_ci} 159262306a36Sopenharmony_ci 159362306a36Sopenharmony_ci/* 159462306a36Sopenharmony_ci * Fix up the owners of the bmbt blocks to refer to the current inode. The 159562306a36Sopenharmony_ci * change owner scan attempts to order all modified buffers in the current 159662306a36Sopenharmony_ci * transaction. In the event of ordered buffer failure, the offending buffer is 159762306a36Sopenharmony_ci * physically logged as a fallback and the scan returns -EAGAIN. We must roll 159862306a36Sopenharmony_ci * the transaction in this case to replenish the fallback log reservation and 159962306a36Sopenharmony_ci * restart the scan. This process repeats until the scan completes. 160062306a36Sopenharmony_ci */ 160162306a36Sopenharmony_cistatic int 160262306a36Sopenharmony_cixfs_swap_change_owner( 160362306a36Sopenharmony_ci struct xfs_trans **tpp, 160462306a36Sopenharmony_ci struct xfs_inode *ip, 160562306a36Sopenharmony_ci struct xfs_inode *tmpip) 160662306a36Sopenharmony_ci{ 160762306a36Sopenharmony_ci int error; 160862306a36Sopenharmony_ci struct xfs_trans *tp = *tpp; 160962306a36Sopenharmony_ci 161062306a36Sopenharmony_ci do { 161162306a36Sopenharmony_ci error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino, 161262306a36Sopenharmony_ci NULL); 161362306a36Sopenharmony_ci /* success or fatal error */ 161462306a36Sopenharmony_ci if (error != -EAGAIN) 161562306a36Sopenharmony_ci break; 161662306a36Sopenharmony_ci 161762306a36Sopenharmony_ci error = xfs_trans_roll(tpp); 161862306a36Sopenharmony_ci if (error) 161962306a36Sopenharmony_ci break; 162062306a36Sopenharmony_ci tp = *tpp; 162162306a36Sopenharmony_ci 162262306a36Sopenharmony_ci /* 162362306a36Sopenharmony_ci * Redirty both inodes so they can relog and keep the log tail 162462306a36Sopenharmony_ci * moving forward. 162562306a36Sopenharmony_ci */ 162662306a36Sopenharmony_ci xfs_trans_ijoin(tp, ip, 0); 162762306a36Sopenharmony_ci xfs_trans_ijoin(tp, tmpip, 0); 162862306a36Sopenharmony_ci xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 162962306a36Sopenharmony_ci xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE); 163062306a36Sopenharmony_ci } while (true); 163162306a36Sopenharmony_ci 163262306a36Sopenharmony_ci return error; 163362306a36Sopenharmony_ci} 163462306a36Sopenharmony_ci 163562306a36Sopenharmony_ciint 163662306a36Sopenharmony_cixfs_swap_extents( 163762306a36Sopenharmony_ci struct xfs_inode *ip, /* target inode */ 163862306a36Sopenharmony_ci struct xfs_inode *tip, /* tmp inode */ 163962306a36Sopenharmony_ci struct xfs_swapext *sxp) 164062306a36Sopenharmony_ci{ 164162306a36Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 164262306a36Sopenharmony_ci struct xfs_trans *tp; 164362306a36Sopenharmony_ci struct xfs_bstat *sbp = &sxp->sx_stat; 164462306a36Sopenharmony_ci int src_log_flags, target_log_flags; 164562306a36Sopenharmony_ci int error = 0; 164662306a36Sopenharmony_ci uint64_t f; 164762306a36Sopenharmony_ci int resblks = 0; 164862306a36Sopenharmony_ci unsigned int flags = 0; 164962306a36Sopenharmony_ci struct timespec64 ctime; 165062306a36Sopenharmony_ci 165162306a36Sopenharmony_ci /* 165262306a36Sopenharmony_ci * Lock the inodes against other IO, page faults and truncate to 165362306a36Sopenharmony_ci * begin with. Then we can ensure the inodes are flushed and have no 165462306a36Sopenharmony_ci * page cache safely. Once we have done this we can take the ilocks and 165562306a36Sopenharmony_ci * do the rest of the checks. 165662306a36Sopenharmony_ci */ 165762306a36Sopenharmony_ci lock_two_nondirectories(VFS_I(ip), VFS_I(tip)); 165862306a36Sopenharmony_ci filemap_invalidate_lock_two(VFS_I(ip)->i_mapping, 165962306a36Sopenharmony_ci VFS_I(tip)->i_mapping); 166062306a36Sopenharmony_ci 166162306a36Sopenharmony_ci /* Verify that both files have the same format */ 166262306a36Sopenharmony_ci if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) { 166362306a36Sopenharmony_ci error = -EINVAL; 166462306a36Sopenharmony_ci goto out_unlock; 166562306a36Sopenharmony_ci } 166662306a36Sopenharmony_ci 166762306a36Sopenharmony_ci /* Verify both files are either real-time or non-realtime */ 166862306a36Sopenharmony_ci if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) { 166962306a36Sopenharmony_ci error = -EINVAL; 167062306a36Sopenharmony_ci goto out_unlock; 167162306a36Sopenharmony_ci } 167262306a36Sopenharmony_ci 167362306a36Sopenharmony_ci error = xfs_qm_dqattach(ip); 167462306a36Sopenharmony_ci if (error) 167562306a36Sopenharmony_ci goto out_unlock; 167662306a36Sopenharmony_ci 167762306a36Sopenharmony_ci error = xfs_qm_dqattach(tip); 167862306a36Sopenharmony_ci if (error) 167962306a36Sopenharmony_ci goto out_unlock; 168062306a36Sopenharmony_ci 168162306a36Sopenharmony_ci error = xfs_swap_extent_flush(ip); 168262306a36Sopenharmony_ci if (error) 168362306a36Sopenharmony_ci goto out_unlock; 168462306a36Sopenharmony_ci error = xfs_swap_extent_flush(tip); 168562306a36Sopenharmony_ci if (error) 168662306a36Sopenharmony_ci goto out_unlock; 168762306a36Sopenharmony_ci 168862306a36Sopenharmony_ci if (xfs_inode_has_cow_data(tip)) { 168962306a36Sopenharmony_ci error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true); 169062306a36Sopenharmony_ci if (error) 169162306a36Sopenharmony_ci goto out_unlock; 169262306a36Sopenharmony_ci } 169362306a36Sopenharmony_ci 169462306a36Sopenharmony_ci /* 169562306a36Sopenharmony_ci * Extent "swapping" with rmap requires a permanent reservation and 169662306a36Sopenharmony_ci * a block reservation because it's really just a remap operation 169762306a36Sopenharmony_ci * performed with log redo items! 169862306a36Sopenharmony_ci */ 169962306a36Sopenharmony_ci if (xfs_has_rmapbt(mp)) { 170062306a36Sopenharmony_ci int w = XFS_DATA_FORK; 170162306a36Sopenharmony_ci uint32_t ipnext = ip->i_df.if_nextents; 170262306a36Sopenharmony_ci uint32_t tipnext = tip->i_df.if_nextents; 170362306a36Sopenharmony_ci 170462306a36Sopenharmony_ci /* 170562306a36Sopenharmony_ci * Conceptually this shouldn't affect the shape of either bmbt, 170662306a36Sopenharmony_ci * but since we atomically move extents one by one, we reserve 170762306a36Sopenharmony_ci * enough space to rebuild both trees. 170862306a36Sopenharmony_ci */ 170962306a36Sopenharmony_ci resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w); 171062306a36Sopenharmony_ci resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w); 171162306a36Sopenharmony_ci 171262306a36Sopenharmony_ci /* 171362306a36Sopenharmony_ci * If either inode straddles a bmapbt block allocation boundary, 171462306a36Sopenharmony_ci * the rmapbt algorithm triggers repeated allocs and frees as 171562306a36Sopenharmony_ci * extents are remapped. This can exhaust the block reservation 171662306a36Sopenharmony_ci * prematurely and cause shutdown. Return freed blocks to the 171762306a36Sopenharmony_ci * transaction reservation to counter this behavior. 171862306a36Sopenharmony_ci */ 171962306a36Sopenharmony_ci flags |= XFS_TRANS_RES_FDBLKS; 172062306a36Sopenharmony_ci } 172162306a36Sopenharmony_ci error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags, 172262306a36Sopenharmony_ci &tp); 172362306a36Sopenharmony_ci if (error) 172462306a36Sopenharmony_ci goto out_unlock; 172562306a36Sopenharmony_ci 172662306a36Sopenharmony_ci /* 172762306a36Sopenharmony_ci * Lock and join the inodes to the tansaction so that transaction commit 172862306a36Sopenharmony_ci * or cancel will unlock the inodes from this point onwards. 172962306a36Sopenharmony_ci */ 173062306a36Sopenharmony_ci xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL); 173162306a36Sopenharmony_ci xfs_trans_ijoin(tp, ip, 0); 173262306a36Sopenharmony_ci xfs_trans_ijoin(tp, tip, 0); 173362306a36Sopenharmony_ci 173462306a36Sopenharmony_ci 173562306a36Sopenharmony_ci /* Verify all data are being swapped */ 173662306a36Sopenharmony_ci if (sxp->sx_offset != 0 || 173762306a36Sopenharmony_ci sxp->sx_length != ip->i_disk_size || 173862306a36Sopenharmony_ci sxp->sx_length != tip->i_disk_size) { 173962306a36Sopenharmony_ci error = -EFAULT; 174062306a36Sopenharmony_ci goto out_trans_cancel; 174162306a36Sopenharmony_ci } 174262306a36Sopenharmony_ci 174362306a36Sopenharmony_ci trace_xfs_swap_extent_before(ip, 0); 174462306a36Sopenharmony_ci trace_xfs_swap_extent_before(tip, 1); 174562306a36Sopenharmony_ci 174662306a36Sopenharmony_ci /* check inode formats now that data is flushed */ 174762306a36Sopenharmony_ci error = xfs_swap_extents_check_format(ip, tip); 174862306a36Sopenharmony_ci if (error) { 174962306a36Sopenharmony_ci xfs_notice(mp, 175062306a36Sopenharmony_ci "%s: inode 0x%llx format is incompatible for exchanging.", 175162306a36Sopenharmony_ci __func__, ip->i_ino); 175262306a36Sopenharmony_ci goto out_trans_cancel; 175362306a36Sopenharmony_ci } 175462306a36Sopenharmony_ci 175562306a36Sopenharmony_ci /* 175662306a36Sopenharmony_ci * Compare the current change & modify times with that 175762306a36Sopenharmony_ci * passed in. If they differ, we abort this swap. 175862306a36Sopenharmony_ci * This is the mechanism used to ensure the calling 175962306a36Sopenharmony_ci * process that the file was not changed out from 176062306a36Sopenharmony_ci * under it. 176162306a36Sopenharmony_ci */ 176262306a36Sopenharmony_ci ctime = inode_get_ctime(VFS_I(ip)); 176362306a36Sopenharmony_ci if ((sbp->bs_ctime.tv_sec != ctime.tv_sec) || 176462306a36Sopenharmony_ci (sbp->bs_ctime.tv_nsec != ctime.tv_nsec) || 176562306a36Sopenharmony_ci (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) || 176662306a36Sopenharmony_ci (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) { 176762306a36Sopenharmony_ci error = -EBUSY; 176862306a36Sopenharmony_ci goto out_trans_cancel; 176962306a36Sopenharmony_ci } 177062306a36Sopenharmony_ci 177162306a36Sopenharmony_ci /* 177262306a36Sopenharmony_ci * Note the trickiness in setting the log flags - we set the owner log 177362306a36Sopenharmony_ci * flag on the opposite inode (i.e. the inode we are setting the new 177462306a36Sopenharmony_ci * owner to be) because once we swap the forks and log that, log 177562306a36Sopenharmony_ci * recovery is going to see the fork as owned by the swapped inode, 177662306a36Sopenharmony_ci * not the pre-swapped inodes. 177762306a36Sopenharmony_ci */ 177862306a36Sopenharmony_ci src_log_flags = XFS_ILOG_CORE; 177962306a36Sopenharmony_ci target_log_flags = XFS_ILOG_CORE; 178062306a36Sopenharmony_ci 178162306a36Sopenharmony_ci if (xfs_has_rmapbt(mp)) 178262306a36Sopenharmony_ci error = xfs_swap_extent_rmap(&tp, ip, tip); 178362306a36Sopenharmony_ci else 178462306a36Sopenharmony_ci error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags, 178562306a36Sopenharmony_ci &target_log_flags); 178662306a36Sopenharmony_ci if (error) 178762306a36Sopenharmony_ci goto out_trans_cancel; 178862306a36Sopenharmony_ci 178962306a36Sopenharmony_ci /* Do we have to swap reflink flags? */ 179062306a36Sopenharmony_ci if ((ip->i_diflags2 & XFS_DIFLAG2_REFLINK) ^ 179162306a36Sopenharmony_ci (tip->i_diflags2 & XFS_DIFLAG2_REFLINK)) { 179262306a36Sopenharmony_ci f = ip->i_diflags2 & XFS_DIFLAG2_REFLINK; 179362306a36Sopenharmony_ci ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK; 179462306a36Sopenharmony_ci ip->i_diflags2 |= tip->i_diflags2 & XFS_DIFLAG2_REFLINK; 179562306a36Sopenharmony_ci tip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK; 179662306a36Sopenharmony_ci tip->i_diflags2 |= f & XFS_DIFLAG2_REFLINK; 179762306a36Sopenharmony_ci } 179862306a36Sopenharmony_ci 179962306a36Sopenharmony_ci /* Swap the cow forks. */ 180062306a36Sopenharmony_ci if (xfs_has_reflink(mp)) { 180162306a36Sopenharmony_ci ASSERT(!ip->i_cowfp || 180262306a36Sopenharmony_ci ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS); 180362306a36Sopenharmony_ci ASSERT(!tip->i_cowfp || 180462306a36Sopenharmony_ci tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS); 180562306a36Sopenharmony_ci 180662306a36Sopenharmony_ci swap(ip->i_cowfp, tip->i_cowfp); 180762306a36Sopenharmony_ci 180862306a36Sopenharmony_ci if (ip->i_cowfp && ip->i_cowfp->if_bytes) 180962306a36Sopenharmony_ci xfs_inode_set_cowblocks_tag(ip); 181062306a36Sopenharmony_ci else 181162306a36Sopenharmony_ci xfs_inode_clear_cowblocks_tag(ip); 181262306a36Sopenharmony_ci if (tip->i_cowfp && tip->i_cowfp->if_bytes) 181362306a36Sopenharmony_ci xfs_inode_set_cowblocks_tag(tip); 181462306a36Sopenharmony_ci else 181562306a36Sopenharmony_ci xfs_inode_clear_cowblocks_tag(tip); 181662306a36Sopenharmony_ci } 181762306a36Sopenharmony_ci 181862306a36Sopenharmony_ci xfs_trans_log_inode(tp, ip, src_log_flags); 181962306a36Sopenharmony_ci xfs_trans_log_inode(tp, tip, target_log_flags); 182062306a36Sopenharmony_ci 182162306a36Sopenharmony_ci /* 182262306a36Sopenharmony_ci * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems 182362306a36Sopenharmony_ci * have inode number owner values in the bmbt blocks that still refer to 182462306a36Sopenharmony_ci * the old inode. Scan each bmbt to fix up the owner values with the 182562306a36Sopenharmony_ci * inode number of the current inode. 182662306a36Sopenharmony_ci */ 182762306a36Sopenharmony_ci if (src_log_flags & XFS_ILOG_DOWNER) { 182862306a36Sopenharmony_ci error = xfs_swap_change_owner(&tp, ip, tip); 182962306a36Sopenharmony_ci if (error) 183062306a36Sopenharmony_ci goto out_trans_cancel; 183162306a36Sopenharmony_ci } 183262306a36Sopenharmony_ci if (target_log_flags & XFS_ILOG_DOWNER) { 183362306a36Sopenharmony_ci error = xfs_swap_change_owner(&tp, tip, ip); 183462306a36Sopenharmony_ci if (error) 183562306a36Sopenharmony_ci goto out_trans_cancel; 183662306a36Sopenharmony_ci } 183762306a36Sopenharmony_ci 183862306a36Sopenharmony_ci /* 183962306a36Sopenharmony_ci * If this is a synchronous mount, make sure that the 184062306a36Sopenharmony_ci * transaction goes to disk before returning to the user. 184162306a36Sopenharmony_ci */ 184262306a36Sopenharmony_ci if (xfs_has_wsync(mp)) 184362306a36Sopenharmony_ci xfs_trans_set_sync(tp); 184462306a36Sopenharmony_ci 184562306a36Sopenharmony_ci error = xfs_trans_commit(tp); 184662306a36Sopenharmony_ci 184762306a36Sopenharmony_ci trace_xfs_swap_extent_after(ip, 0); 184862306a36Sopenharmony_ci trace_xfs_swap_extent_after(tip, 1); 184962306a36Sopenharmony_ci 185062306a36Sopenharmony_ciout_unlock_ilock: 185162306a36Sopenharmony_ci xfs_iunlock(ip, XFS_ILOCK_EXCL); 185262306a36Sopenharmony_ci xfs_iunlock(tip, XFS_ILOCK_EXCL); 185362306a36Sopenharmony_ciout_unlock: 185462306a36Sopenharmony_ci filemap_invalidate_unlock_two(VFS_I(ip)->i_mapping, 185562306a36Sopenharmony_ci VFS_I(tip)->i_mapping); 185662306a36Sopenharmony_ci unlock_two_nondirectories(VFS_I(ip), VFS_I(tip)); 185762306a36Sopenharmony_ci return error; 185862306a36Sopenharmony_ci 185962306a36Sopenharmony_ciout_trans_cancel: 186062306a36Sopenharmony_ci xfs_trans_cancel(tp); 186162306a36Sopenharmony_ci goto out_unlock_ilock; 186262306a36Sopenharmony_ci} 1863