168988114SDave Chinner /* 268988114SDave Chinner * Copyright (c) 2000-2006 Silicon Graphics, Inc. 3c24b5dfaSDave Chinner * Copyright (c) 2012 Red Hat, Inc. 468988114SDave Chinner * All Rights Reserved. 568988114SDave Chinner * 668988114SDave Chinner * This program is free software; you can redistribute it and/or 768988114SDave Chinner * modify it under the terms of the GNU General Public License as 868988114SDave Chinner * published by the Free Software Foundation. 968988114SDave Chinner * 1068988114SDave Chinner * This program is distributed in the hope that it would be useful, 1168988114SDave Chinner * but WITHOUT ANY WARRANTY; without even the implied warranty of 1268988114SDave Chinner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1368988114SDave Chinner * GNU General Public License for more details. 1468988114SDave Chinner * 1568988114SDave Chinner * You should have received a copy of the GNU General Public License 1668988114SDave Chinner * along with this program; if not, write the Free Software Foundation, 1768988114SDave Chinner * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 1868988114SDave Chinner */ 1968988114SDave Chinner #include "xfs.h" 2068988114SDave Chinner #include "xfs_fs.h" 2170a9883cSDave Chinner #include "xfs_shared.h" 22239880efSDave Chinner #include "xfs_format.h" 23239880efSDave Chinner #include "xfs_log_format.h" 24239880efSDave Chinner #include "xfs_trans_resv.h" 2568988114SDave Chinner #include "xfs_bit.h" 2668988114SDave Chinner #include "xfs_sb.h" 2768988114SDave Chinner #include "xfs_ag.h" 2868988114SDave Chinner #include "xfs_mount.h" 2957062787SDave Chinner #include "xfs_da_format.h" 3068988114SDave Chinner #include "xfs_inode.h" 3168988114SDave Chinner #include "xfs_btree.h" 32239880efSDave Chinner #include "xfs_trans.h" 3368988114SDave Chinner #include "xfs_extfree_item.h" 3468988114SDave Chinner #include "xfs_alloc.h" 3568988114SDave Chinner #include "xfs_bmap.h" 3668988114SDave Chinner #include "xfs_bmap_util.h" 37a4fbe6abSDave Chinner #include "xfs_bmap_btree.h" 3868988114SDave Chinner #include "xfs_rtalloc.h" 3968988114SDave Chinner #include "xfs_error.h" 4068988114SDave Chinner #include "xfs_quota.h" 4168988114SDave Chinner #include "xfs_trans_space.h" 4268988114SDave Chinner #include "xfs_trace.h" 43c24b5dfaSDave Chinner #include "xfs_icache.h" 44239880efSDave Chinner #include "xfs_log.h" 45a4fbe6abSDave Chinner #include "xfs_dinode.h" 4668988114SDave Chinner 4768988114SDave Chinner /* Kernel only BMAP related definitions and functions */ 4868988114SDave Chinner 4968988114SDave Chinner /* 5068988114SDave Chinner * Convert the given file system block to a disk block. We have to treat it 5168988114SDave Chinner * differently based on whether the file is a real time file or not, because the 5268988114SDave Chinner * bmap code does. 5368988114SDave Chinner */ 5468988114SDave Chinner xfs_daddr_t 5568988114SDave Chinner xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) 5668988114SDave Chinner { 5768988114SDave Chinner return (XFS_IS_REALTIME_INODE(ip) ? \ 5868988114SDave Chinner (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \ 5968988114SDave Chinner XFS_FSB_TO_DADDR((ip)->i_mount, (fsb))); 6068988114SDave Chinner } 6168988114SDave Chinner 6268988114SDave Chinner /* 6368988114SDave Chinner * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi 6468988114SDave Chinner * caller. Frees all the extents that need freeing, which must be done 6568988114SDave Chinner * last due to locking considerations. We never free any extents in 6668988114SDave Chinner * the first transaction. 6768988114SDave Chinner * 6868988114SDave Chinner * Return 1 if the given transaction was committed and a new one 6968988114SDave Chinner * started, and 0 otherwise in the committed parameter. 7068988114SDave Chinner */ 7168988114SDave Chinner int /* error */ 7268988114SDave Chinner xfs_bmap_finish( 7368988114SDave Chinner xfs_trans_t **tp, /* transaction pointer addr */ 7468988114SDave Chinner xfs_bmap_free_t *flist, /* i/o: list extents to free */ 7568988114SDave Chinner int *committed) /* xact committed or not */ 7668988114SDave Chinner { 7768988114SDave Chinner xfs_efd_log_item_t *efd; /* extent free data */ 7868988114SDave Chinner xfs_efi_log_item_t *efi; /* extent free intention */ 7968988114SDave Chinner int error; /* error return value */ 8068988114SDave Chinner xfs_bmap_free_item_t *free; /* free extent item */ 813d3c8b52SJie Liu struct xfs_trans_res tres; /* new log reservation */ 8268988114SDave Chinner xfs_mount_t *mp; /* filesystem mount structure */ 8368988114SDave Chinner xfs_bmap_free_item_t *next; /* next item on free list */ 8468988114SDave Chinner xfs_trans_t *ntp; /* new transaction pointer */ 8568988114SDave Chinner 8668988114SDave Chinner ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); 8768988114SDave Chinner if (flist->xbf_count == 0) { 8868988114SDave Chinner *committed = 0; 8968988114SDave Chinner return 0; 9068988114SDave Chinner } 9168988114SDave Chinner ntp = *tp; 9268988114SDave Chinner efi = xfs_trans_get_efi(ntp, flist->xbf_count); 9368988114SDave Chinner for (free = flist->xbf_first; free; free = free->xbfi_next) 9468988114SDave Chinner xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, 9568988114SDave Chinner free->xbfi_blockcount); 963d3c8b52SJie Liu 973d3c8b52SJie Liu tres.tr_logres = ntp->t_log_res; 983d3c8b52SJie Liu tres.tr_logcount = ntp->t_log_count; 993d3c8b52SJie Liu tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; 10068988114SDave Chinner ntp = xfs_trans_dup(*tp); 10168988114SDave Chinner error = xfs_trans_commit(*tp, 0); 10268988114SDave Chinner *tp = ntp; 10368988114SDave Chinner *committed = 1; 10468988114SDave Chinner /* 10568988114SDave Chinner * We have a new transaction, so we should return committed=1, 10668988114SDave Chinner * even though we're returning an error. 10768988114SDave Chinner */ 10868988114SDave Chinner if (error) 10968988114SDave Chinner return error; 11068988114SDave Chinner 11168988114SDave Chinner /* 11268988114SDave Chinner * transaction commit worked ok so we can drop the extra ticket 11368988114SDave Chinner * reference that we gained in xfs_trans_dup() 11468988114SDave Chinner */ 11568988114SDave Chinner xfs_log_ticket_put(ntp->t_ticket); 11668988114SDave Chinner 1173d3c8b52SJie Liu error = xfs_trans_reserve(ntp, &tres, 0, 0); 1183d3c8b52SJie Liu if (error) 11968988114SDave Chinner return error; 12068988114SDave Chinner efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); 12168988114SDave Chinner for (free = flist->xbf_first; free != NULL; free = next) { 12268988114SDave Chinner next = free->xbfi_next; 12368988114SDave Chinner if ((error = xfs_free_extent(ntp, free->xbfi_startblock, 12468988114SDave Chinner free->xbfi_blockcount))) { 12568988114SDave Chinner /* 12668988114SDave Chinner * The bmap free list will be cleaned up at a 12768988114SDave Chinner * higher level. The EFI will be canceled when 12868988114SDave Chinner * this transaction is aborted. 12968988114SDave Chinner * Need to force shutdown here to make sure it 13068988114SDave Chinner * happens, since this transaction may not be 13168988114SDave Chinner * dirty yet. 13268988114SDave Chinner */ 13368988114SDave Chinner mp = ntp->t_mountp; 13468988114SDave Chinner if (!XFS_FORCED_SHUTDOWN(mp)) 13568988114SDave Chinner xfs_force_shutdown(mp, 1362451337dSDave Chinner (error == -EFSCORRUPTED) ? 13768988114SDave Chinner SHUTDOWN_CORRUPT_INCORE : 13868988114SDave Chinner SHUTDOWN_META_IO_ERROR); 13968988114SDave Chinner return error; 14068988114SDave Chinner } 14168988114SDave Chinner xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, 14268988114SDave Chinner free->xbfi_blockcount); 14368988114SDave Chinner xfs_bmap_del_free(flist, NULL, free); 14468988114SDave Chinner } 14568988114SDave Chinner return 0; 14668988114SDave Chinner } 14768988114SDave Chinner 14868988114SDave Chinner int 14968988114SDave Chinner xfs_bmap_rtalloc( 15068988114SDave Chinner struct xfs_bmalloca *ap) /* bmap alloc argument struct */ 15168988114SDave Chinner { 15268988114SDave Chinner xfs_alloctype_t atype = 0; /* type for allocation routines */ 15368988114SDave Chinner int error; /* error return value */ 15468988114SDave Chinner xfs_mount_t *mp; /* mount point structure */ 15568988114SDave Chinner xfs_extlen_t prod = 0; /* product factor for allocators */ 15668988114SDave Chinner xfs_extlen_t ralen = 0; /* realtime allocation length */ 15768988114SDave Chinner xfs_extlen_t align; /* minimum allocation alignment */ 15868988114SDave Chinner xfs_rtblock_t rtb; 15968988114SDave Chinner 16068988114SDave Chinner mp = ap->ip->i_mount; 16168988114SDave Chinner align = xfs_get_extsz_hint(ap->ip); 16268988114SDave Chinner prod = align / mp->m_sb.sb_rextsize; 16368988114SDave Chinner error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, 16468988114SDave Chinner align, 1, ap->eof, 0, 16568988114SDave Chinner ap->conv, &ap->offset, &ap->length); 16668988114SDave Chinner if (error) 16768988114SDave Chinner return error; 16868988114SDave Chinner ASSERT(ap->length); 16968988114SDave Chinner ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); 17068988114SDave Chinner 17168988114SDave Chinner /* 17268988114SDave Chinner * If the offset & length are not perfectly aligned 17368988114SDave Chinner * then kill prod, it will just get us in trouble. 17468988114SDave Chinner */ 17568988114SDave Chinner if (do_mod(ap->offset, align) || ap->length % align) 17668988114SDave Chinner prod = 1; 17768988114SDave Chinner /* 17868988114SDave Chinner * Set ralen to be the actual requested length in rtextents. 17968988114SDave Chinner */ 18068988114SDave Chinner ralen = ap->length / mp->m_sb.sb_rextsize; 18168988114SDave Chinner /* 18268988114SDave Chinner * If the old value was close enough to MAXEXTLEN that 18368988114SDave Chinner * we rounded up to it, cut it back so it's valid again. 18468988114SDave Chinner * Note that if it's a really large request (bigger than 18568988114SDave Chinner * MAXEXTLEN), we don't hear about that number, and can't 18668988114SDave Chinner * adjust the starting point to match it. 18768988114SDave Chinner */ 18868988114SDave Chinner if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) 18968988114SDave Chinner ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; 19068988114SDave Chinner 19168988114SDave Chinner /* 19268988114SDave Chinner * Lock out other modifications to the RT bitmap inode. 19368988114SDave Chinner */ 19468988114SDave Chinner xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); 19568988114SDave Chinner xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); 19668988114SDave Chinner 19768988114SDave Chinner /* 19868988114SDave Chinner * If it's an allocation to an empty file at offset 0, 19968988114SDave Chinner * pick an extent that will space things out in the rt area. 20068988114SDave Chinner */ 20168988114SDave Chinner if (ap->eof && ap->offset == 0) { 20268988114SDave Chinner xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ 20368988114SDave Chinner 20468988114SDave Chinner error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); 20568988114SDave Chinner if (error) 20668988114SDave Chinner return error; 20768988114SDave Chinner ap->blkno = rtx * mp->m_sb.sb_rextsize; 20868988114SDave Chinner } else { 20968988114SDave Chinner ap->blkno = 0; 21068988114SDave Chinner } 21168988114SDave Chinner 21268988114SDave Chinner xfs_bmap_adjacent(ap); 21368988114SDave Chinner 21468988114SDave Chinner /* 21568988114SDave Chinner * Realtime allocation, done through xfs_rtallocate_extent. 21668988114SDave Chinner */ 21768988114SDave Chinner atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; 21868988114SDave Chinner do_div(ap->blkno, mp->m_sb.sb_rextsize); 21968988114SDave Chinner rtb = ap->blkno; 22068988114SDave Chinner ap->length = ralen; 22168988114SDave Chinner if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, 22268988114SDave Chinner &ralen, atype, ap->wasdel, prod, &rtb))) 22368988114SDave Chinner return error; 22468988114SDave Chinner if (rtb == NULLFSBLOCK && prod > 1 && 22568988114SDave Chinner (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, 22668988114SDave Chinner ap->length, &ralen, atype, 22768988114SDave Chinner ap->wasdel, 1, &rtb))) 22868988114SDave Chinner return error; 22968988114SDave Chinner ap->blkno = rtb; 23068988114SDave Chinner if (ap->blkno != NULLFSBLOCK) { 23168988114SDave Chinner ap->blkno *= mp->m_sb.sb_rextsize; 23268988114SDave Chinner ralen *= mp->m_sb.sb_rextsize; 23368988114SDave Chinner ap->length = ralen; 23468988114SDave Chinner ap->ip->i_d.di_nblocks += ralen; 23568988114SDave Chinner xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); 23668988114SDave Chinner if (ap->wasdel) 23768988114SDave Chinner ap->ip->i_delayed_blks -= ralen; 23868988114SDave Chinner /* 23968988114SDave Chinner * Adjust the disk quota also. This was reserved 24068988114SDave Chinner * earlier. 24168988114SDave Chinner */ 24268988114SDave Chinner xfs_trans_mod_dquot_byino(ap->tp, ap->ip, 24368988114SDave Chinner ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : 24468988114SDave Chinner XFS_TRANS_DQ_RTBCOUNT, (long) ralen); 24568988114SDave Chinner } else { 24668988114SDave Chinner ap->length = 0; 24768988114SDave Chinner } 24868988114SDave Chinner return 0; 24968988114SDave Chinner } 25068988114SDave Chinner 25168988114SDave Chinner /* 25268988114SDave Chinner * Check if the endoff is outside the last extent. If so the caller will grow 25368988114SDave Chinner * the allocation to a stripe unit boundary. All offsets are considered outside 25468988114SDave Chinner * the end of file for an empty fork, so 1 is returned in *eof in that case. 25568988114SDave Chinner */ 25668988114SDave Chinner int 25768988114SDave Chinner xfs_bmap_eof( 25868988114SDave Chinner struct xfs_inode *ip, 25968988114SDave Chinner xfs_fileoff_t endoff, 26068988114SDave Chinner int whichfork, 26168988114SDave Chinner int *eof) 26268988114SDave Chinner { 26368988114SDave Chinner struct xfs_bmbt_irec rec; 26468988114SDave Chinner int error; 26568988114SDave Chinner 26668988114SDave Chinner error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); 26768988114SDave Chinner if (error || *eof) 26868988114SDave Chinner return error; 26968988114SDave Chinner 27068988114SDave Chinner *eof = endoff >= rec.br_startoff + rec.br_blockcount; 27168988114SDave Chinner return 0; 27268988114SDave Chinner } 27368988114SDave Chinner 27468988114SDave Chinner /* 27568988114SDave Chinner * Extent tree block counting routines. 27668988114SDave Chinner */ 27768988114SDave Chinner 27868988114SDave Chinner /* 27968988114SDave Chinner * Count leaf blocks given a range of extent records. 28068988114SDave Chinner */ 28168988114SDave Chinner STATIC void 28268988114SDave Chinner xfs_bmap_count_leaves( 28368988114SDave Chinner xfs_ifork_t *ifp, 28468988114SDave Chinner xfs_extnum_t idx, 28568988114SDave Chinner int numrecs, 28668988114SDave Chinner int *count) 28768988114SDave Chinner { 28868988114SDave Chinner int b; 28968988114SDave Chinner 29068988114SDave Chinner for (b = 0; b < numrecs; b++) { 29168988114SDave Chinner xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); 29268988114SDave Chinner *count += xfs_bmbt_get_blockcount(frp); 29368988114SDave Chinner } 29468988114SDave Chinner } 29568988114SDave Chinner 29668988114SDave Chinner /* 29768988114SDave Chinner * Count leaf blocks given a range of extent records originally 29868988114SDave Chinner * in btree format. 29968988114SDave Chinner */ 30068988114SDave Chinner STATIC void 30168988114SDave Chinner xfs_bmap_disk_count_leaves( 30268988114SDave Chinner struct xfs_mount *mp, 30368988114SDave Chinner struct xfs_btree_block *block, 30468988114SDave Chinner int numrecs, 30568988114SDave Chinner int *count) 30668988114SDave Chinner { 30768988114SDave Chinner int b; 30868988114SDave Chinner xfs_bmbt_rec_t *frp; 30968988114SDave Chinner 31068988114SDave Chinner for (b = 1; b <= numrecs; b++) { 31168988114SDave Chinner frp = XFS_BMBT_REC_ADDR(mp, block, b); 31268988114SDave Chinner *count += xfs_bmbt_disk_get_blockcount(frp); 31368988114SDave Chinner } 31468988114SDave Chinner } 31568988114SDave Chinner 31668988114SDave Chinner /* 31768988114SDave Chinner * Recursively walks each level of a btree 3188be11e92SZhi Yong Wu * to count total fsblocks in use. 31968988114SDave Chinner */ 32068988114SDave Chinner STATIC int /* error */ 32168988114SDave Chinner xfs_bmap_count_tree( 32268988114SDave Chinner xfs_mount_t *mp, /* file system mount point */ 32368988114SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 32468988114SDave Chinner xfs_ifork_t *ifp, /* inode fork pointer */ 32568988114SDave Chinner xfs_fsblock_t blockno, /* file system block number */ 32668988114SDave Chinner int levelin, /* level in btree */ 32768988114SDave Chinner int *count) /* Count of blocks */ 32868988114SDave Chinner { 32968988114SDave Chinner int error; 33068988114SDave Chinner xfs_buf_t *bp, *nbp; 33168988114SDave Chinner int level = levelin; 33268988114SDave Chinner __be64 *pp; 33368988114SDave Chinner xfs_fsblock_t bno = blockno; 33468988114SDave Chinner xfs_fsblock_t nextbno; 33568988114SDave Chinner struct xfs_btree_block *block, *nextblock; 33668988114SDave Chinner int numrecs; 33768988114SDave Chinner 33868988114SDave Chinner error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF, 33968988114SDave Chinner &xfs_bmbt_buf_ops); 34068988114SDave Chinner if (error) 34168988114SDave Chinner return error; 34268988114SDave Chinner *count += 1; 34368988114SDave Chinner block = XFS_BUF_TO_BLOCK(bp); 34468988114SDave Chinner 34568988114SDave Chinner if (--level) { 34668988114SDave Chinner /* Not at node above leaves, count this level of nodes */ 34768988114SDave Chinner nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); 34868988114SDave Chinner while (nextbno != NULLFSBLOCK) { 34968988114SDave Chinner error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp, 35068988114SDave Chinner XFS_BMAP_BTREE_REF, 35168988114SDave Chinner &xfs_bmbt_buf_ops); 35268988114SDave Chinner if (error) 35368988114SDave Chinner return error; 35468988114SDave Chinner *count += 1; 35568988114SDave Chinner nextblock = XFS_BUF_TO_BLOCK(nbp); 35668988114SDave Chinner nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); 35768988114SDave Chinner xfs_trans_brelse(tp, nbp); 35868988114SDave Chinner } 35968988114SDave Chinner 36068988114SDave Chinner /* Dive to the next level */ 36168988114SDave Chinner pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); 36268988114SDave Chinner bno = be64_to_cpu(*pp); 36368988114SDave Chinner if (unlikely((error = 36468988114SDave Chinner xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { 36568988114SDave Chinner xfs_trans_brelse(tp, bp); 36668988114SDave Chinner XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", 36768988114SDave Chinner XFS_ERRLEVEL_LOW, mp); 3682451337dSDave Chinner return -EFSCORRUPTED; 36968988114SDave Chinner } 37068988114SDave Chinner xfs_trans_brelse(tp, bp); 37168988114SDave Chinner } else { 37268988114SDave Chinner /* count all level 1 nodes and their leaves */ 37368988114SDave Chinner for (;;) { 37468988114SDave Chinner nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); 37568988114SDave Chinner numrecs = be16_to_cpu(block->bb_numrecs); 37668988114SDave Chinner xfs_bmap_disk_count_leaves(mp, block, numrecs, count); 37768988114SDave Chinner xfs_trans_brelse(tp, bp); 37868988114SDave Chinner if (nextbno == NULLFSBLOCK) 37968988114SDave Chinner break; 38068988114SDave Chinner bno = nextbno; 38168988114SDave Chinner error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, 38268988114SDave Chinner XFS_BMAP_BTREE_REF, 38368988114SDave Chinner &xfs_bmbt_buf_ops); 38468988114SDave Chinner if (error) 38568988114SDave Chinner return error; 38668988114SDave Chinner *count += 1; 38768988114SDave Chinner block = XFS_BUF_TO_BLOCK(bp); 38868988114SDave Chinner } 38968988114SDave Chinner } 39068988114SDave Chinner return 0; 39168988114SDave Chinner } 39268988114SDave Chinner 39368988114SDave Chinner /* 39468988114SDave Chinner * Count fsblocks of the given fork. 39568988114SDave Chinner */ 39668988114SDave Chinner int /* error */ 39768988114SDave Chinner xfs_bmap_count_blocks( 39868988114SDave Chinner xfs_trans_t *tp, /* transaction pointer */ 39968988114SDave Chinner xfs_inode_t *ip, /* incore inode */ 40068988114SDave Chinner int whichfork, /* data or attr fork */ 40168988114SDave Chinner int *count) /* out: count of blocks */ 40268988114SDave Chinner { 40368988114SDave Chinner struct xfs_btree_block *block; /* current btree block */ 40468988114SDave Chinner xfs_fsblock_t bno; /* block # of "block" */ 40568988114SDave Chinner xfs_ifork_t *ifp; /* fork structure */ 40668988114SDave Chinner int level; /* btree level, for checking */ 40768988114SDave Chinner xfs_mount_t *mp; /* file system mount structure */ 40868988114SDave Chinner __be64 *pp; /* pointer to block address */ 40968988114SDave Chinner 41068988114SDave Chinner bno = NULLFSBLOCK; 41168988114SDave Chinner mp = ip->i_mount; 41268988114SDave Chinner ifp = XFS_IFORK_PTR(ip, whichfork); 41368988114SDave Chinner if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { 41468988114SDave Chinner xfs_bmap_count_leaves(ifp, 0, 41568988114SDave Chinner ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), 41668988114SDave Chinner count); 41768988114SDave Chinner return 0; 41868988114SDave Chinner } 41968988114SDave Chinner 42068988114SDave Chinner /* 42168988114SDave Chinner * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. 42268988114SDave Chinner */ 42368988114SDave Chinner block = ifp->if_broot; 42468988114SDave Chinner level = be16_to_cpu(block->bb_level); 42568988114SDave Chinner ASSERT(level > 0); 42668988114SDave Chinner pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); 42768988114SDave Chinner bno = be64_to_cpu(*pp); 428d5cf09baSChristoph Hellwig ASSERT(bno != NULLFSBLOCK); 42968988114SDave Chinner ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); 43068988114SDave Chinner ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); 43168988114SDave Chinner 43268988114SDave Chinner if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) { 43368988114SDave Chinner XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, 43468988114SDave Chinner mp); 4352451337dSDave Chinner return -EFSCORRUPTED; 43668988114SDave Chinner } 43768988114SDave Chinner 43868988114SDave Chinner return 0; 43968988114SDave Chinner } 44068988114SDave Chinner 44168988114SDave Chinner /* 44268988114SDave Chinner * returns 1 for success, 0 if we failed to map the extent. 44368988114SDave Chinner */ 44468988114SDave Chinner STATIC int 44568988114SDave Chinner xfs_getbmapx_fix_eof_hole( 44668988114SDave Chinner xfs_inode_t *ip, /* xfs incore inode pointer */ 44768988114SDave Chinner struct getbmapx *out, /* output structure */ 44868988114SDave Chinner int prealloced, /* this is a file with 44968988114SDave Chinner * preallocated data space */ 45068988114SDave Chinner __int64_t end, /* last block requested */ 45168988114SDave Chinner xfs_fsblock_t startblock) 45268988114SDave Chinner { 45368988114SDave Chinner __int64_t fixlen; 45468988114SDave Chinner xfs_mount_t *mp; /* file system mount point */ 45568988114SDave Chinner xfs_ifork_t *ifp; /* inode fork pointer */ 45668988114SDave Chinner xfs_extnum_t lastx; /* last extent pointer */ 45768988114SDave Chinner xfs_fileoff_t fileblock; 45868988114SDave Chinner 45968988114SDave Chinner if (startblock == HOLESTARTBLOCK) { 46068988114SDave Chinner mp = ip->i_mount; 46168988114SDave Chinner out->bmv_block = -1; 46268988114SDave Chinner fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); 46368988114SDave Chinner fixlen -= out->bmv_offset; 46468988114SDave Chinner if (prealloced && out->bmv_offset + out->bmv_length == end) { 46568988114SDave Chinner /* Came to hole at EOF. Trim it. */ 46668988114SDave Chinner if (fixlen <= 0) 46768988114SDave Chinner return 0; 46868988114SDave Chinner out->bmv_length = fixlen; 46968988114SDave Chinner } 47068988114SDave Chinner } else { 47168988114SDave Chinner if (startblock == DELAYSTARTBLOCK) 47268988114SDave Chinner out->bmv_block = -2; 47368988114SDave Chinner else 47468988114SDave Chinner out->bmv_block = xfs_fsb_to_db(ip, startblock); 47568988114SDave Chinner fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); 47668988114SDave Chinner ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); 47768988114SDave Chinner if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && 47868988114SDave Chinner (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) 47968988114SDave Chinner out->bmv_oflags |= BMV_OF_LAST; 48068988114SDave Chinner } 48168988114SDave Chinner 48268988114SDave Chinner return 1; 48368988114SDave Chinner } 48468988114SDave Chinner 48568988114SDave Chinner /* 48668988114SDave Chinner * Get inode's extents as described in bmv, and format for output. 48768988114SDave Chinner * Calls formatter to fill the user's buffer until all extents 48868988114SDave Chinner * are mapped, until the passed-in bmv->bmv_count slots have 48968988114SDave Chinner * been filled, or until the formatter short-circuits the loop, 49068988114SDave Chinner * if it is tracking filled-in extents on its own. 49168988114SDave Chinner */ 49268988114SDave Chinner int /* error code */ 49368988114SDave Chinner xfs_getbmap( 49468988114SDave Chinner xfs_inode_t *ip, 49568988114SDave Chinner struct getbmapx *bmv, /* user bmap structure */ 49668988114SDave Chinner xfs_bmap_format_t formatter, /* format to user */ 49768988114SDave Chinner void *arg) /* formatter arg */ 49868988114SDave Chinner { 49968988114SDave Chinner __int64_t bmvend; /* last block requested */ 50068988114SDave Chinner int error = 0; /* return value */ 50168988114SDave Chinner __int64_t fixlen; /* length for -1 case */ 50268988114SDave Chinner int i; /* extent number */ 50368988114SDave Chinner int lock; /* lock state */ 50468988114SDave Chinner xfs_bmbt_irec_t *map; /* buffer for user's data */ 50568988114SDave Chinner xfs_mount_t *mp; /* file system mount point */ 50668988114SDave Chinner int nex; /* # of user extents can do */ 50768988114SDave Chinner int nexleft; /* # of user extents left */ 50868988114SDave Chinner int subnex; /* # of bmapi's can do */ 50968988114SDave Chinner int nmap; /* number of map entries */ 51068988114SDave Chinner struct getbmapx *out; /* output structure */ 51168988114SDave Chinner int whichfork; /* data or attr fork */ 51268988114SDave Chinner int prealloced; /* this is a file with 51368988114SDave Chinner * preallocated data space */ 51468988114SDave Chinner int iflags; /* interface flags */ 51568988114SDave Chinner int bmapi_flags; /* flags for xfs_bmapi */ 51668988114SDave Chinner int cur_ext = 0; 51768988114SDave Chinner 51868988114SDave Chinner mp = ip->i_mount; 51968988114SDave Chinner iflags = bmv->bmv_iflags; 52068988114SDave Chinner whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; 52168988114SDave Chinner 52268988114SDave Chinner if (whichfork == XFS_ATTR_FORK) { 52368988114SDave Chinner if (XFS_IFORK_Q(ip)) { 52468988114SDave Chinner if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && 52568988114SDave Chinner ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && 52668988114SDave Chinner ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) 5272451337dSDave Chinner return -EINVAL; 52868988114SDave Chinner } else if (unlikely( 52968988114SDave Chinner ip->i_d.di_aformat != 0 && 53068988114SDave Chinner ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { 53168988114SDave Chinner XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, 53268988114SDave Chinner ip->i_mount); 5332451337dSDave Chinner return -EFSCORRUPTED; 53468988114SDave Chinner } 53568988114SDave Chinner 53668988114SDave Chinner prealloced = 0; 53768988114SDave Chinner fixlen = 1LL << 32; 53868988114SDave Chinner } else { 53968988114SDave Chinner if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && 54068988114SDave Chinner ip->i_d.di_format != XFS_DINODE_FMT_BTREE && 54168988114SDave Chinner ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) 5422451337dSDave Chinner return -EINVAL; 54368988114SDave Chinner 54468988114SDave Chinner if (xfs_get_extsz_hint(ip) || 54568988114SDave Chinner ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ 54668988114SDave Chinner prealloced = 1; 54768988114SDave Chinner fixlen = mp->m_super->s_maxbytes; 54868988114SDave Chinner } else { 54968988114SDave Chinner prealloced = 0; 55068988114SDave Chinner fixlen = XFS_ISIZE(ip); 55168988114SDave Chinner } 55268988114SDave Chinner } 55368988114SDave Chinner 55468988114SDave Chinner if (bmv->bmv_length == -1) { 55568988114SDave Chinner fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); 55668988114SDave Chinner bmv->bmv_length = 55768988114SDave Chinner max_t(__int64_t, fixlen - bmv->bmv_offset, 0); 55868988114SDave Chinner } else if (bmv->bmv_length == 0) { 55968988114SDave Chinner bmv->bmv_entries = 0; 56068988114SDave Chinner return 0; 56168988114SDave Chinner } else if (bmv->bmv_length < 0) { 5622451337dSDave Chinner return -EINVAL; 56368988114SDave Chinner } 56468988114SDave Chinner 56568988114SDave Chinner nex = bmv->bmv_count - 1; 56668988114SDave Chinner if (nex <= 0) 5672451337dSDave Chinner return -EINVAL; 56868988114SDave Chinner bmvend = bmv->bmv_offset + bmv->bmv_length; 56968988114SDave Chinner 57068988114SDave Chinner 57168988114SDave Chinner if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) 5722451337dSDave Chinner return -ENOMEM; 573fdd3cceeSDave Chinner out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0); 57468988114SDave Chinner if (!out) 5752451337dSDave Chinner return -ENOMEM; 57668988114SDave Chinner 57768988114SDave Chinner xfs_ilock(ip, XFS_IOLOCK_SHARED); 578efa70be1SChristoph Hellwig if (whichfork == XFS_DATA_FORK) { 579efa70be1SChristoph Hellwig if (!(iflags & BMV_IF_DELALLOC) && 580efa70be1SChristoph Hellwig (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) { 5812451337dSDave Chinner error = filemap_write_and_wait(VFS_I(ip)->i_mapping); 58268988114SDave Chinner if (error) 58368988114SDave Chinner goto out_unlock_iolock; 584efa70be1SChristoph Hellwig 58568988114SDave Chinner /* 586efa70be1SChristoph Hellwig * Even after flushing the inode, there can still be 587efa70be1SChristoph Hellwig * delalloc blocks on the inode beyond EOF due to 588efa70be1SChristoph Hellwig * speculative preallocation. These are not removed 589efa70be1SChristoph Hellwig * until the release function is called or the inode 590efa70be1SChristoph Hellwig * is inactivated. Hence we cannot assert here that 591efa70be1SChristoph Hellwig * ip->i_delayed_blks == 0. 59268988114SDave Chinner */ 59368988114SDave Chinner } 59468988114SDave Chinner 595309ecac8SChristoph Hellwig lock = xfs_ilock_data_map_shared(ip); 596efa70be1SChristoph Hellwig } else { 597efa70be1SChristoph Hellwig lock = xfs_ilock_attr_map_shared(ip); 598efa70be1SChristoph Hellwig } 59968988114SDave Chinner 60068988114SDave Chinner /* 60168988114SDave Chinner * Don't let nex be bigger than the number of extents 60268988114SDave Chinner * we can have assuming alternating holes and real extents. 60368988114SDave Chinner */ 60468988114SDave Chinner if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) 60568988114SDave Chinner nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; 60668988114SDave Chinner 60768988114SDave Chinner bmapi_flags = xfs_bmapi_aflag(whichfork); 60868988114SDave Chinner if (!(iflags & BMV_IF_PREALLOC)) 60968988114SDave Chinner bmapi_flags |= XFS_BMAPI_IGSTATE; 61068988114SDave Chinner 61168988114SDave Chinner /* 61268988114SDave Chinner * Allocate enough space to handle "subnex" maps at a time. 61368988114SDave Chinner */ 6142451337dSDave Chinner error = -ENOMEM; 61568988114SDave Chinner subnex = 16; 61668988114SDave Chinner map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); 61768988114SDave Chinner if (!map) 61868988114SDave Chinner goto out_unlock_ilock; 61968988114SDave Chinner 62068988114SDave Chinner bmv->bmv_entries = 0; 62168988114SDave Chinner 62268988114SDave Chinner if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && 62368988114SDave Chinner (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { 62468988114SDave Chinner error = 0; 62568988114SDave Chinner goto out_free_map; 62668988114SDave Chinner } 62768988114SDave Chinner 62868988114SDave Chinner nexleft = nex; 62968988114SDave Chinner 63068988114SDave Chinner do { 63168988114SDave Chinner nmap = (nexleft > subnex) ? subnex : nexleft; 63268988114SDave Chinner error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), 63368988114SDave Chinner XFS_BB_TO_FSB(mp, bmv->bmv_length), 63468988114SDave Chinner map, &nmap, bmapi_flags); 63568988114SDave Chinner if (error) 63668988114SDave Chinner goto out_free_map; 63768988114SDave Chinner ASSERT(nmap <= subnex); 63868988114SDave Chinner 63968988114SDave Chinner for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { 64068988114SDave Chinner out[cur_ext].bmv_oflags = 0; 64168988114SDave Chinner if (map[i].br_state == XFS_EXT_UNWRITTEN) 64268988114SDave Chinner out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; 64368988114SDave Chinner else if (map[i].br_startblock == DELAYSTARTBLOCK) 64468988114SDave Chinner out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; 64568988114SDave Chinner out[cur_ext].bmv_offset = 64668988114SDave Chinner XFS_FSB_TO_BB(mp, map[i].br_startoff); 64768988114SDave Chinner out[cur_ext].bmv_length = 64868988114SDave Chinner XFS_FSB_TO_BB(mp, map[i].br_blockcount); 64968988114SDave Chinner out[cur_ext].bmv_unused1 = 0; 65068988114SDave Chinner out[cur_ext].bmv_unused2 = 0; 65168988114SDave Chinner 65268988114SDave Chinner /* 65368988114SDave Chinner * delayed allocation extents that start beyond EOF can 65468988114SDave Chinner * occur due to speculative EOF allocation when the 65568988114SDave Chinner * delalloc extent is larger than the largest freespace 65668988114SDave Chinner * extent at conversion time. These extents cannot be 65768988114SDave Chinner * converted by data writeback, so can exist here even 65868988114SDave Chinner * if we are not supposed to be finding delalloc 65968988114SDave Chinner * extents. 66068988114SDave Chinner */ 66168988114SDave Chinner if (map[i].br_startblock == DELAYSTARTBLOCK && 66268988114SDave Chinner map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) 66368988114SDave Chinner ASSERT((iflags & BMV_IF_DELALLOC) != 0); 66468988114SDave Chinner 66568988114SDave Chinner if (map[i].br_startblock == HOLESTARTBLOCK && 66668988114SDave Chinner whichfork == XFS_ATTR_FORK) { 66768988114SDave Chinner /* came to the end of attribute fork */ 66868988114SDave Chinner out[cur_ext].bmv_oflags |= BMV_OF_LAST; 66968988114SDave Chinner goto out_free_map; 67068988114SDave Chinner } 67168988114SDave Chinner 67268988114SDave Chinner if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], 67368988114SDave Chinner prealloced, bmvend, 67468988114SDave Chinner map[i].br_startblock)) 67568988114SDave Chinner goto out_free_map; 67668988114SDave Chinner 67768988114SDave Chinner bmv->bmv_offset = 67868988114SDave Chinner out[cur_ext].bmv_offset + 67968988114SDave Chinner out[cur_ext].bmv_length; 68068988114SDave Chinner bmv->bmv_length = 68168988114SDave Chinner max_t(__int64_t, 0, bmvend - bmv->bmv_offset); 68268988114SDave Chinner 68368988114SDave Chinner /* 68468988114SDave Chinner * In case we don't want to return the hole, 68568988114SDave Chinner * don't increase cur_ext so that we can reuse 68668988114SDave Chinner * it in the next loop. 68768988114SDave Chinner */ 68868988114SDave Chinner if ((iflags & BMV_IF_NO_HOLES) && 68968988114SDave Chinner map[i].br_startblock == HOLESTARTBLOCK) { 69068988114SDave Chinner memset(&out[cur_ext], 0, sizeof(out[cur_ext])); 69168988114SDave Chinner continue; 69268988114SDave Chinner } 69368988114SDave Chinner 69468988114SDave Chinner nexleft--; 69568988114SDave Chinner bmv->bmv_entries++; 69668988114SDave Chinner cur_ext++; 69768988114SDave Chinner } 69868988114SDave Chinner } while (nmap && nexleft && bmv->bmv_length); 69968988114SDave Chinner 70068988114SDave Chinner out_free_map: 70168988114SDave Chinner kmem_free(map); 70268988114SDave Chinner out_unlock_ilock: 70301f4f327SChristoph Hellwig xfs_iunlock(ip, lock); 70468988114SDave Chinner out_unlock_iolock: 70568988114SDave Chinner xfs_iunlock(ip, XFS_IOLOCK_SHARED); 70668988114SDave Chinner 70768988114SDave Chinner for (i = 0; i < cur_ext; i++) { 70868988114SDave Chinner int full = 0; /* user array is full */ 70968988114SDave Chinner 71068988114SDave Chinner /* format results & advance arg */ 71168988114SDave Chinner error = formatter(&arg, &out[i], &full); 71268988114SDave Chinner if (error || full) 71368988114SDave Chinner break; 71468988114SDave Chinner } 71568988114SDave Chinner 71668988114SDave Chinner kmem_free(out); 71768988114SDave Chinner return error; 71868988114SDave Chinner } 71968988114SDave Chinner 72068988114SDave Chinner /* 72168988114SDave Chinner * dead simple method of punching delalyed allocation blocks from a range in 72268988114SDave Chinner * the inode. Walks a block at a time so will be slow, but is only executed in 723ad4809bfSZhi Yong Wu * rare error cases so the overhead is not critical. This will always punch out 72468988114SDave Chinner * both the start and end blocks, even if the ranges only partially overlap 72568988114SDave Chinner * them, so it is up to the caller to ensure that partial blocks are not 72668988114SDave Chinner * passed in. 72768988114SDave Chinner */ 72868988114SDave Chinner int 72968988114SDave Chinner xfs_bmap_punch_delalloc_range( 73068988114SDave Chinner struct xfs_inode *ip, 73168988114SDave Chinner xfs_fileoff_t start_fsb, 73268988114SDave Chinner xfs_fileoff_t length) 73368988114SDave Chinner { 73468988114SDave Chinner xfs_fileoff_t remaining = length; 73568988114SDave Chinner int error = 0; 73668988114SDave Chinner 73768988114SDave Chinner ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 73868988114SDave Chinner 73968988114SDave Chinner do { 74068988114SDave Chinner int done; 74168988114SDave Chinner xfs_bmbt_irec_t imap; 74268988114SDave Chinner int nimaps = 1; 74368988114SDave Chinner xfs_fsblock_t firstblock; 74468988114SDave Chinner xfs_bmap_free_t flist; 74568988114SDave Chinner 74668988114SDave Chinner /* 74768988114SDave Chinner * Map the range first and check that it is a delalloc extent 74868988114SDave Chinner * before trying to unmap the range. Otherwise we will be 74968988114SDave Chinner * trying to remove a real extent (which requires a 75068988114SDave Chinner * transaction) or a hole, which is probably a bad idea... 75168988114SDave Chinner */ 75268988114SDave Chinner error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, 75368988114SDave Chinner XFS_BMAPI_ENTIRE); 75468988114SDave Chinner 75568988114SDave Chinner if (error) { 75668988114SDave Chinner /* something screwed, just bail */ 75768988114SDave Chinner if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { 75868988114SDave Chinner xfs_alert(ip->i_mount, 75968988114SDave Chinner "Failed delalloc mapping lookup ino %lld fsb %lld.", 76068988114SDave Chinner ip->i_ino, start_fsb); 76168988114SDave Chinner } 76268988114SDave Chinner break; 76368988114SDave Chinner } 76468988114SDave Chinner if (!nimaps) { 76568988114SDave Chinner /* nothing there */ 76668988114SDave Chinner goto next_block; 76768988114SDave Chinner } 76868988114SDave Chinner if (imap.br_startblock != DELAYSTARTBLOCK) { 76968988114SDave Chinner /* been converted, ignore */ 77068988114SDave Chinner goto next_block; 77168988114SDave Chinner } 77268988114SDave Chinner WARN_ON(imap.br_blockcount == 0); 77368988114SDave Chinner 77468988114SDave Chinner /* 77568988114SDave Chinner * Note: while we initialise the firstblock/flist pair, they 77668988114SDave Chinner * should never be used because blocks should never be 77768988114SDave Chinner * allocated or freed for a delalloc extent and hence we need 77868988114SDave Chinner * don't cancel or finish them after the xfs_bunmapi() call. 77968988114SDave Chinner */ 78068988114SDave Chinner xfs_bmap_init(&flist, &firstblock); 78168988114SDave Chinner error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, 78268988114SDave Chinner &flist, &done); 78368988114SDave Chinner if (error) 78468988114SDave Chinner break; 78568988114SDave Chinner 78668988114SDave Chinner ASSERT(!flist.xbf_count && !flist.xbf_first); 78768988114SDave Chinner next_block: 78868988114SDave Chinner start_fsb++; 78968988114SDave Chinner remaining--; 79068988114SDave Chinner } while(remaining > 0); 79168988114SDave Chinner 79268988114SDave Chinner return error; 79368988114SDave Chinner } 794c24b5dfaSDave Chinner 795c24b5dfaSDave Chinner /* 796c24b5dfaSDave Chinner * Test whether it is appropriate to check an inode for and free post EOF 797c24b5dfaSDave Chinner * blocks. The 'force' parameter determines whether we should also consider 798c24b5dfaSDave Chinner * regular files that are marked preallocated or append-only. 799c24b5dfaSDave Chinner */ 800c24b5dfaSDave Chinner bool 801c24b5dfaSDave Chinner xfs_can_free_eofblocks(struct xfs_inode *ip, bool force) 802c24b5dfaSDave Chinner { 803c24b5dfaSDave Chinner /* prealloc/delalloc exists only on regular files */ 804c24b5dfaSDave Chinner if (!S_ISREG(ip->i_d.di_mode)) 805c24b5dfaSDave Chinner return false; 806c24b5dfaSDave Chinner 807c24b5dfaSDave Chinner /* 808c24b5dfaSDave Chinner * Zero sized files with no cached pages and delalloc blocks will not 809c24b5dfaSDave Chinner * have speculative prealloc/delalloc blocks to remove. 810c24b5dfaSDave Chinner */ 811c24b5dfaSDave Chinner if (VFS_I(ip)->i_size == 0 && 8122667c6f9SDave Chinner VFS_I(ip)->i_mapping->nrpages == 0 && 813c24b5dfaSDave Chinner ip->i_delayed_blks == 0) 814c24b5dfaSDave Chinner return false; 815c24b5dfaSDave Chinner 816c24b5dfaSDave Chinner /* If we haven't read in the extent list, then don't do it now. */ 817c24b5dfaSDave Chinner if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) 818c24b5dfaSDave Chinner return false; 819c24b5dfaSDave Chinner 820c24b5dfaSDave Chinner /* 821c24b5dfaSDave Chinner * Do not free real preallocated or append-only files unless the file 822c24b5dfaSDave Chinner * has delalloc blocks and we are forced to remove them. 823c24b5dfaSDave Chinner */ 824c24b5dfaSDave Chinner if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) 825c24b5dfaSDave Chinner if (!force || ip->i_delayed_blks == 0) 826c24b5dfaSDave Chinner return false; 827c24b5dfaSDave Chinner 828c24b5dfaSDave Chinner return true; 829c24b5dfaSDave Chinner } 830c24b5dfaSDave Chinner 831c24b5dfaSDave Chinner /* 832c24b5dfaSDave Chinner * This is called by xfs_inactive to free any blocks beyond eof 833c24b5dfaSDave Chinner * when the link count isn't zero and by xfs_dm_punch_hole() when 834c24b5dfaSDave Chinner * punching a hole to EOF. 835c24b5dfaSDave Chinner */ 836c24b5dfaSDave Chinner int 837c24b5dfaSDave Chinner xfs_free_eofblocks( 838c24b5dfaSDave Chinner xfs_mount_t *mp, 839c24b5dfaSDave Chinner xfs_inode_t *ip, 840c24b5dfaSDave Chinner bool need_iolock) 841c24b5dfaSDave Chinner { 842c24b5dfaSDave Chinner xfs_trans_t *tp; 843c24b5dfaSDave Chinner int error; 844c24b5dfaSDave Chinner xfs_fileoff_t end_fsb; 845c24b5dfaSDave Chinner xfs_fileoff_t last_fsb; 846c24b5dfaSDave Chinner xfs_filblks_t map_len; 847c24b5dfaSDave Chinner int nimaps; 848c24b5dfaSDave Chinner xfs_bmbt_irec_t imap; 849c24b5dfaSDave Chinner 850c24b5dfaSDave Chinner /* 851c24b5dfaSDave Chinner * Figure out if there are any blocks beyond the end 852c24b5dfaSDave Chinner * of the file. If not, then there is nothing to do. 853c24b5dfaSDave Chinner */ 854c24b5dfaSDave Chinner end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip)); 855c24b5dfaSDave Chinner last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); 856c24b5dfaSDave Chinner if (last_fsb <= end_fsb) 857c24b5dfaSDave Chinner return 0; 858c24b5dfaSDave Chinner map_len = last_fsb - end_fsb; 859c24b5dfaSDave Chinner 860c24b5dfaSDave Chinner nimaps = 1; 861c24b5dfaSDave Chinner xfs_ilock(ip, XFS_ILOCK_SHARED); 862c24b5dfaSDave Chinner error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0); 863c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_ILOCK_SHARED); 864c24b5dfaSDave Chinner 865c24b5dfaSDave Chinner if (!error && (nimaps != 0) && 866c24b5dfaSDave Chinner (imap.br_startblock != HOLESTARTBLOCK || 867c24b5dfaSDave Chinner ip->i_delayed_blks)) { 868c24b5dfaSDave Chinner /* 869c24b5dfaSDave Chinner * Attach the dquots to the inode up front. 870c24b5dfaSDave Chinner */ 871c24b5dfaSDave Chinner error = xfs_qm_dqattach(ip, 0); 872c24b5dfaSDave Chinner if (error) 873c24b5dfaSDave Chinner return error; 874c24b5dfaSDave Chinner 875c24b5dfaSDave Chinner /* 876c24b5dfaSDave Chinner * There are blocks after the end of file. 877c24b5dfaSDave Chinner * Free them up now by truncating the file to 878c24b5dfaSDave Chinner * its current size. 879c24b5dfaSDave Chinner */ 880c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE); 881c24b5dfaSDave Chinner 882c24b5dfaSDave Chinner if (need_iolock) { 883c24b5dfaSDave Chinner if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { 884c24b5dfaSDave Chinner xfs_trans_cancel(tp, 0); 8852451337dSDave Chinner return -EAGAIN; 886c24b5dfaSDave Chinner } 887c24b5dfaSDave Chinner } 888c24b5dfaSDave Chinner 8893d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0); 890c24b5dfaSDave Chinner if (error) { 891c24b5dfaSDave Chinner ASSERT(XFS_FORCED_SHUTDOWN(mp)); 892c24b5dfaSDave Chinner xfs_trans_cancel(tp, 0); 893c24b5dfaSDave Chinner if (need_iolock) 894c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_IOLOCK_EXCL); 895c24b5dfaSDave Chinner return error; 896c24b5dfaSDave Chinner } 897c24b5dfaSDave Chinner 898c24b5dfaSDave Chinner xfs_ilock(ip, XFS_ILOCK_EXCL); 899c24b5dfaSDave Chinner xfs_trans_ijoin(tp, ip, 0); 900c24b5dfaSDave Chinner 901c24b5dfaSDave Chinner /* 902c24b5dfaSDave Chinner * Do not update the on-disk file size. If we update the 903c24b5dfaSDave Chinner * on-disk file size and then the system crashes before the 904c24b5dfaSDave Chinner * contents of the file are flushed to disk then the files 905c24b5dfaSDave Chinner * may be full of holes (ie NULL files bug). 906c24b5dfaSDave Chinner */ 907c24b5dfaSDave Chinner error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 908c24b5dfaSDave Chinner XFS_ISIZE(ip)); 909c24b5dfaSDave Chinner if (error) { 910c24b5dfaSDave Chinner /* 911c24b5dfaSDave Chinner * If we get an error at this point we simply don't 912c24b5dfaSDave Chinner * bother truncating the file. 913c24b5dfaSDave Chinner */ 914c24b5dfaSDave Chinner xfs_trans_cancel(tp, 915c24b5dfaSDave Chinner (XFS_TRANS_RELEASE_LOG_RES | 916c24b5dfaSDave Chinner XFS_TRANS_ABORT)); 917c24b5dfaSDave Chinner } else { 918c24b5dfaSDave Chinner error = xfs_trans_commit(tp, 919c24b5dfaSDave Chinner XFS_TRANS_RELEASE_LOG_RES); 920c24b5dfaSDave Chinner if (!error) 921c24b5dfaSDave Chinner xfs_inode_clear_eofblocks_tag(ip); 922c24b5dfaSDave Chinner } 923c24b5dfaSDave Chinner 924c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL); 925c24b5dfaSDave Chinner if (need_iolock) 926c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_IOLOCK_EXCL); 927c24b5dfaSDave Chinner } 928c24b5dfaSDave Chinner return error; 929c24b5dfaSDave Chinner } 930c24b5dfaSDave Chinner 93183aee9e4SChristoph Hellwig int 932c24b5dfaSDave Chinner xfs_alloc_file_space( 93383aee9e4SChristoph Hellwig struct xfs_inode *ip, 934c24b5dfaSDave Chinner xfs_off_t offset, 935c24b5dfaSDave Chinner xfs_off_t len, 9365f8aca8bSChristoph Hellwig int alloc_type) 937c24b5dfaSDave Chinner { 938c24b5dfaSDave Chinner xfs_mount_t *mp = ip->i_mount; 939c24b5dfaSDave Chinner xfs_off_t count; 940c24b5dfaSDave Chinner xfs_filblks_t allocated_fsb; 941c24b5dfaSDave Chinner xfs_filblks_t allocatesize_fsb; 942c24b5dfaSDave Chinner xfs_extlen_t extsz, temp; 943c24b5dfaSDave Chinner xfs_fileoff_t startoffset_fsb; 944c24b5dfaSDave Chinner xfs_fsblock_t firstfsb; 945c24b5dfaSDave Chinner int nimaps; 946c24b5dfaSDave Chinner int quota_flag; 947c24b5dfaSDave Chinner int rt; 948c24b5dfaSDave Chinner xfs_trans_t *tp; 949c24b5dfaSDave Chinner xfs_bmbt_irec_t imaps[1], *imapp; 950c24b5dfaSDave Chinner xfs_bmap_free_t free_list; 951c24b5dfaSDave Chinner uint qblocks, resblks, resrtextents; 952c24b5dfaSDave Chinner int committed; 953c24b5dfaSDave Chinner int error; 954c24b5dfaSDave Chinner 955c24b5dfaSDave Chinner trace_xfs_alloc_file_space(ip); 956c24b5dfaSDave Chinner 957c24b5dfaSDave Chinner if (XFS_FORCED_SHUTDOWN(mp)) 9582451337dSDave Chinner return -EIO; 959c24b5dfaSDave Chinner 960c24b5dfaSDave Chinner error = xfs_qm_dqattach(ip, 0); 961c24b5dfaSDave Chinner if (error) 962c24b5dfaSDave Chinner return error; 963c24b5dfaSDave Chinner 964c24b5dfaSDave Chinner if (len <= 0) 9652451337dSDave Chinner return -EINVAL; 966c24b5dfaSDave Chinner 967c24b5dfaSDave Chinner rt = XFS_IS_REALTIME_INODE(ip); 968c24b5dfaSDave Chinner extsz = xfs_get_extsz_hint(ip); 969c24b5dfaSDave Chinner 970c24b5dfaSDave Chinner count = len; 971c24b5dfaSDave Chinner imapp = &imaps[0]; 972c24b5dfaSDave Chinner nimaps = 1; 973c24b5dfaSDave Chinner startoffset_fsb = XFS_B_TO_FSBT(mp, offset); 974c24b5dfaSDave Chinner allocatesize_fsb = XFS_B_TO_FSB(mp, count); 975c24b5dfaSDave Chinner 976c24b5dfaSDave Chinner /* 977c24b5dfaSDave Chinner * Allocate file space until done or until there is an error 978c24b5dfaSDave Chinner */ 979c24b5dfaSDave Chinner while (allocatesize_fsb && !error) { 980c24b5dfaSDave Chinner xfs_fileoff_t s, e; 981c24b5dfaSDave Chinner 982c24b5dfaSDave Chinner /* 983c24b5dfaSDave Chinner * Determine space reservations for data/realtime. 984c24b5dfaSDave Chinner */ 985c24b5dfaSDave Chinner if (unlikely(extsz)) { 986c24b5dfaSDave Chinner s = startoffset_fsb; 987c24b5dfaSDave Chinner do_div(s, extsz); 988c24b5dfaSDave Chinner s *= extsz; 989c24b5dfaSDave Chinner e = startoffset_fsb + allocatesize_fsb; 990c24b5dfaSDave Chinner if ((temp = do_mod(startoffset_fsb, extsz))) 991c24b5dfaSDave Chinner e += temp; 992c24b5dfaSDave Chinner if ((temp = do_mod(e, extsz))) 993c24b5dfaSDave Chinner e += extsz - temp; 994c24b5dfaSDave Chinner } else { 995c24b5dfaSDave Chinner s = 0; 996c24b5dfaSDave Chinner e = allocatesize_fsb; 997c24b5dfaSDave Chinner } 998c24b5dfaSDave Chinner 999c24b5dfaSDave Chinner /* 1000c24b5dfaSDave Chinner * The transaction reservation is limited to a 32-bit block 1001c24b5dfaSDave Chinner * count, hence we need to limit the number of blocks we are 1002c24b5dfaSDave Chinner * trying to reserve to avoid an overflow. We can't allocate 1003c24b5dfaSDave Chinner * more than @nimaps extents, and an extent is limited on disk 1004c24b5dfaSDave Chinner * to MAXEXTLEN (21 bits), so use that to enforce the limit. 1005c24b5dfaSDave Chinner */ 1006c24b5dfaSDave Chinner resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps)); 1007c24b5dfaSDave Chinner if (unlikely(rt)) { 1008c24b5dfaSDave Chinner resrtextents = qblocks = resblks; 1009c24b5dfaSDave Chinner resrtextents /= mp->m_sb.sb_rextsize; 1010c24b5dfaSDave Chinner resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 1011c24b5dfaSDave Chinner quota_flag = XFS_QMOPT_RES_RTBLKS; 1012c24b5dfaSDave Chinner } else { 1013c24b5dfaSDave Chinner resrtextents = 0; 1014c24b5dfaSDave Chinner resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks); 1015c24b5dfaSDave Chinner quota_flag = XFS_QMOPT_RES_REGBLKS; 1016c24b5dfaSDave Chinner } 1017c24b5dfaSDave Chinner 1018c24b5dfaSDave Chinner /* 1019c24b5dfaSDave Chinner * Allocate and setup the transaction. 1020c24b5dfaSDave Chinner */ 1021c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); 10223d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, 10233d3c8b52SJie Liu resblks, resrtextents); 1024c24b5dfaSDave Chinner /* 1025c24b5dfaSDave Chinner * Check for running out of space 1026c24b5dfaSDave Chinner */ 1027c24b5dfaSDave Chinner if (error) { 1028c24b5dfaSDave Chinner /* 1029c24b5dfaSDave Chinner * Free the transaction structure. 1030c24b5dfaSDave Chinner */ 10312451337dSDave Chinner ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); 1032c24b5dfaSDave Chinner xfs_trans_cancel(tp, 0); 1033c24b5dfaSDave Chinner break; 1034c24b5dfaSDave Chinner } 1035c24b5dfaSDave Chinner xfs_ilock(ip, XFS_ILOCK_EXCL); 1036c24b5dfaSDave Chinner error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 1037c24b5dfaSDave Chinner 0, quota_flag); 1038c24b5dfaSDave Chinner if (error) 1039c24b5dfaSDave Chinner goto error1; 1040c24b5dfaSDave Chinner 1041c24b5dfaSDave Chinner xfs_trans_ijoin(tp, ip, 0); 1042c24b5dfaSDave Chinner 1043c24b5dfaSDave Chinner xfs_bmap_init(&free_list, &firstfsb); 1044c24b5dfaSDave Chinner error = xfs_bmapi_write(tp, ip, startoffset_fsb, 1045c24b5dfaSDave Chinner allocatesize_fsb, alloc_type, &firstfsb, 1046c24b5dfaSDave Chinner 0, imapp, &nimaps, &free_list); 1047c24b5dfaSDave Chinner if (error) { 1048c24b5dfaSDave Chinner goto error0; 1049c24b5dfaSDave Chinner } 1050c24b5dfaSDave Chinner 1051c24b5dfaSDave Chinner /* 1052c24b5dfaSDave Chinner * Complete the transaction 1053c24b5dfaSDave Chinner */ 1054c24b5dfaSDave Chinner error = xfs_bmap_finish(&tp, &free_list, &committed); 1055c24b5dfaSDave Chinner if (error) { 1056c24b5dfaSDave Chinner goto error0; 1057c24b5dfaSDave Chinner } 1058c24b5dfaSDave Chinner 1059c24b5dfaSDave Chinner error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1060c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL); 1061c24b5dfaSDave Chinner if (error) { 1062c24b5dfaSDave Chinner break; 1063c24b5dfaSDave Chinner } 1064c24b5dfaSDave Chinner 1065c24b5dfaSDave Chinner allocated_fsb = imapp->br_blockcount; 1066c24b5dfaSDave Chinner 1067c24b5dfaSDave Chinner if (nimaps == 0) { 10682451337dSDave Chinner error = -ENOSPC; 1069c24b5dfaSDave Chinner break; 1070c24b5dfaSDave Chinner } 1071c24b5dfaSDave Chinner 1072c24b5dfaSDave Chinner startoffset_fsb += allocated_fsb; 1073c24b5dfaSDave Chinner allocatesize_fsb -= allocated_fsb; 1074c24b5dfaSDave Chinner } 1075c24b5dfaSDave Chinner 1076c24b5dfaSDave Chinner return error; 1077c24b5dfaSDave Chinner 1078c24b5dfaSDave Chinner error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ 1079c24b5dfaSDave Chinner xfs_bmap_cancel(&free_list); 1080c24b5dfaSDave Chinner xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag); 1081c24b5dfaSDave Chinner 1082c24b5dfaSDave Chinner error1: /* Just cancel transaction */ 1083c24b5dfaSDave Chinner xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 1084c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL); 1085c24b5dfaSDave Chinner return error; 1086c24b5dfaSDave Chinner } 1087c24b5dfaSDave Chinner 1088c24b5dfaSDave Chinner /* 1089c24b5dfaSDave Chinner * Zero file bytes between startoff and endoff inclusive. 1090c24b5dfaSDave Chinner * The iolock is held exclusive and no blocks are buffered. 1091c24b5dfaSDave Chinner * 1092c24b5dfaSDave Chinner * This function is used by xfs_free_file_space() to zero 1093c24b5dfaSDave Chinner * partial blocks when the range to free is not block aligned. 1094c24b5dfaSDave Chinner * When unreserving space with boundaries that are not block 1095c24b5dfaSDave Chinner * aligned we round up the start and round down the end 1096c24b5dfaSDave Chinner * boundaries and then use this function to zero the parts of 1097c24b5dfaSDave Chinner * the blocks that got dropped during the rounding. 1098c24b5dfaSDave Chinner */ 1099c24b5dfaSDave Chinner STATIC int 1100c24b5dfaSDave Chinner xfs_zero_remaining_bytes( 1101c24b5dfaSDave Chinner xfs_inode_t *ip, 1102c24b5dfaSDave Chinner xfs_off_t startoff, 1103c24b5dfaSDave Chinner xfs_off_t endoff) 1104c24b5dfaSDave Chinner { 1105c24b5dfaSDave Chinner xfs_bmbt_irec_t imap; 1106c24b5dfaSDave Chinner xfs_fileoff_t offset_fsb; 1107c24b5dfaSDave Chinner xfs_off_t lastoffset; 1108c24b5dfaSDave Chinner xfs_off_t offset; 1109c24b5dfaSDave Chinner xfs_buf_t *bp; 1110c24b5dfaSDave Chinner xfs_mount_t *mp = ip->i_mount; 1111c24b5dfaSDave Chinner int nimap; 1112c24b5dfaSDave Chinner int error = 0; 1113c24b5dfaSDave Chinner 1114c24b5dfaSDave Chinner /* 1115c24b5dfaSDave Chinner * Avoid doing I/O beyond eof - it's not necessary 1116c24b5dfaSDave Chinner * since nothing can read beyond eof. The space will 1117c24b5dfaSDave Chinner * be zeroed when the file is extended anyway. 1118c24b5dfaSDave Chinner */ 1119c24b5dfaSDave Chinner if (startoff >= XFS_ISIZE(ip)) 1120c24b5dfaSDave Chinner return 0; 1121c24b5dfaSDave Chinner 1122c24b5dfaSDave Chinner if (endoff > XFS_ISIZE(ip)) 1123c24b5dfaSDave Chinner endoff = XFS_ISIZE(ip); 1124c24b5dfaSDave Chinner 1125c24b5dfaSDave Chinner bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ? 1126c24b5dfaSDave Chinner mp->m_rtdev_targp : mp->m_ddev_targp, 1127c24b5dfaSDave Chinner BTOBB(mp->m_sb.sb_blocksize), 0); 1128c24b5dfaSDave Chinner if (!bp) 11292451337dSDave Chinner return -ENOMEM; 1130c24b5dfaSDave Chinner 1131c24b5dfaSDave Chinner xfs_buf_unlock(bp); 1132c24b5dfaSDave Chinner 1133c24b5dfaSDave Chinner for (offset = startoff; offset <= endoff; offset = lastoffset + 1) { 11344f317369SChristoph Hellwig uint lock_mode; 11354f317369SChristoph Hellwig 1136c24b5dfaSDave Chinner offset_fsb = XFS_B_TO_FSBT(mp, offset); 1137c24b5dfaSDave Chinner nimap = 1; 11384f317369SChristoph Hellwig 11394f317369SChristoph Hellwig lock_mode = xfs_ilock_data_map_shared(ip); 1140c24b5dfaSDave Chinner error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0); 11414f317369SChristoph Hellwig xfs_iunlock(ip, lock_mode); 11424f317369SChristoph Hellwig 1143c24b5dfaSDave Chinner if (error || nimap < 1) 1144c24b5dfaSDave Chinner break; 1145c24b5dfaSDave Chinner ASSERT(imap.br_blockcount >= 1); 1146c24b5dfaSDave Chinner ASSERT(imap.br_startoff == offset_fsb); 1147c24b5dfaSDave Chinner lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1; 1148c24b5dfaSDave Chinner if (lastoffset > endoff) 1149c24b5dfaSDave Chinner lastoffset = endoff; 1150c24b5dfaSDave Chinner if (imap.br_startblock == HOLESTARTBLOCK) 1151c24b5dfaSDave Chinner continue; 1152c24b5dfaSDave Chinner ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 1153c24b5dfaSDave Chinner if (imap.br_state == XFS_EXT_UNWRITTEN) 1154c24b5dfaSDave Chinner continue; 1155c24b5dfaSDave Chinner XFS_BUF_UNDONE(bp); 1156c24b5dfaSDave Chinner XFS_BUF_UNWRITE(bp); 1157c24b5dfaSDave Chinner XFS_BUF_READ(bp); 1158c24b5dfaSDave Chinner XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock)); 115983a0adc3SChristoph Hellwig 116083a0adc3SChristoph Hellwig if (XFS_FORCED_SHUTDOWN(mp)) { 11612451337dSDave Chinner error = -EIO; 116283a0adc3SChristoph Hellwig break; 116383a0adc3SChristoph Hellwig } 116483a0adc3SChristoph Hellwig xfs_buf_iorequest(bp); 1165c24b5dfaSDave Chinner error = xfs_buf_iowait(bp); 1166c24b5dfaSDave Chinner if (error) { 1167c24b5dfaSDave Chinner xfs_buf_ioerror_alert(bp, 1168c24b5dfaSDave Chinner "xfs_zero_remaining_bytes(read)"); 1169c24b5dfaSDave Chinner break; 1170c24b5dfaSDave Chinner } 1171c24b5dfaSDave Chinner memset(bp->b_addr + 1172c24b5dfaSDave Chinner (offset - XFS_FSB_TO_B(mp, imap.br_startoff)), 1173c24b5dfaSDave Chinner 0, lastoffset - offset + 1); 1174c24b5dfaSDave Chinner XFS_BUF_UNDONE(bp); 1175c24b5dfaSDave Chinner XFS_BUF_UNREAD(bp); 1176c24b5dfaSDave Chinner XFS_BUF_WRITE(bp); 117783a0adc3SChristoph Hellwig 117883a0adc3SChristoph Hellwig if (XFS_FORCED_SHUTDOWN(mp)) { 11792451337dSDave Chinner error = -EIO; 118083a0adc3SChristoph Hellwig break; 118183a0adc3SChristoph Hellwig } 118283a0adc3SChristoph Hellwig xfs_buf_iorequest(bp); 1183c24b5dfaSDave Chinner error = xfs_buf_iowait(bp); 1184c24b5dfaSDave Chinner if (error) { 1185c24b5dfaSDave Chinner xfs_buf_ioerror_alert(bp, 1186c24b5dfaSDave Chinner "xfs_zero_remaining_bytes(write)"); 1187c24b5dfaSDave Chinner break; 1188c24b5dfaSDave Chinner } 1189c24b5dfaSDave Chinner } 1190c24b5dfaSDave Chinner xfs_buf_free(bp); 1191c24b5dfaSDave Chinner return error; 1192c24b5dfaSDave Chinner } 1193c24b5dfaSDave Chinner 119483aee9e4SChristoph Hellwig int 1195c24b5dfaSDave Chinner xfs_free_file_space( 119683aee9e4SChristoph Hellwig struct xfs_inode *ip, 1197c24b5dfaSDave Chinner xfs_off_t offset, 11985f8aca8bSChristoph Hellwig xfs_off_t len) 1199c24b5dfaSDave Chinner { 1200c24b5dfaSDave Chinner int committed; 1201c24b5dfaSDave Chinner int done; 1202c24b5dfaSDave Chinner xfs_fileoff_t endoffset_fsb; 1203c24b5dfaSDave Chinner int error; 1204c24b5dfaSDave Chinner xfs_fsblock_t firstfsb; 1205c24b5dfaSDave Chinner xfs_bmap_free_t free_list; 1206c24b5dfaSDave Chinner xfs_bmbt_irec_t imap; 1207c24b5dfaSDave Chinner xfs_off_t ioffset; 1208c24b5dfaSDave Chinner xfs_extlen_t mod=0; 1209c24b5dfaSDave Chinner xfs_mount_t *mp; 1210c24b5dfaSDave Chinner int nimap; 1211c24b5dfaSDave Chinner uint resblks; 1212c24b5dfaSDave Chinner xfs_off_t rounding; 1213c24b5dfaSDave Chinner int rt; 1214c24b5dfaSDave Chinner xfs_fileoff_t startoffset_fsb; 1215c24b5dfaSDave Chinner xfs_trans_t *tp; 1216c24b5dfaSDave Chinner 1217c24b5dfaSDave Chinner mp = ip->i_mount; 1218c24b5dfaSDave Chinner 1219c24b5dfaSDave Chinner trace_xfs_free_file_space(ip); 1220c24b5dfaSDave Chinner 1221c24b5dfaSDave Chinner error = xfs_qm_dqattach(ip, 0); 1222c24b5dfaSDave Chinner if (error) 1223c24b5dfaSDave Chinner return error; 1224c24b5dfaSDave Chinner 1225c24b5dfaSDave Chinner error = 0; 1226c24b5dfaSDave Chinner if (len <= 0) /* if nothing being freed */ 1227c24b5dfaSDave Chinner return error; 1228c24b5dfaSDave Chinner rt = XFS_IS_REALTIME_INODE(ip); 1229c24b5dfaSDave Chinner startoffset_fsb = XFS_B_TO_FSB(mp, offset); 1230c24b5dfaSDave Chinner endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); 1231c24b5dfaSDave Chinner 1232c24b5dfaSDave Chinner /* wait for the completion of any pending DIOs */ 1233c24b5dfaSDave Chinner inode_dio_wait(VFS_I(ip)); 1234c24b5dfaSDave Chinner 1235c24b5dfaSDave Chinner rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); 1236c24b5dfaSDave Chinner ioffset = offset & ~(rounding - 1); 12372451337dSDave Chinner error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, 1238c24b5dfaSDave Chinner ioffset, -1); 1239c24b5dfaSDave Chinner if (error) 12405f8aca8bSChristoph Hellwig goto out; 1241c24b5dfaSDave Chinner truncate_pagecache_range(VFS_I(ip), ioffset, -1); 1242c24b5dfaSDave Chinner 1243c24b5dfaSDave Chinner /* 1244c24b5dfaSDave Chinner * Need to zero the stuff we're not freeing, on disk. 1245c24b5dfaSDave Chinner * If it's a realtime file & can't use unwritten extents then we 1246c24b5dfaSDave Chinner * actually need to zero the extent edges. Otherwise xfs_bunmapi 1247c24b5dfaSDave Chinner * will take care of it for us. 1248c24b5dfaSDave Chinner */ 1249c24b5dfaSDave Chinner if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) { 1250c24b5dfaSDave Chinner nimap = 1; 1251c24b5dfaSDave Chinner error = xfs_bmapi_read(ip, startoffset_fsb, 1, 1252c24b5dfaSDave Chinner &imap, &nimap, 0); 1253c24b5dfaSDave Chinner if (error) 12545f8aca8bSChristoph Hellwig goto out; 1255c24b5dfaSDave Chinner ASSERT(nimap == 0 || nimap == 1); 1256c24b5dfaSDave Chinner if (nimap && imap.br_startblock != HOLESTARTBLOCK) { 1257c24b5dfaSDave Chinner xfs_daddr_t block; 1258c24b5dfaSDave Chinner 1259c24b5dfaSDave Chinner ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 1260c24b5dfaSDave Chinner block = imap.br_startblock; 1261c24b5dfaSDave Chinner mod = do_div(block, mp->m_sb.sb_rextsize); 1262c24b5dfaSDave Chinner if (mod) 1263c24b5dfaSDave Chinner startoffset_fsb += mp->m_sb.sb_rextsize - mod; 1264c24b5dfaSDave Chinner } 1265c24b5dfaSDave Chinner nimap = 1; 1266c24b5dfaSDave Chinner error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1, 1267c24b5dfaSDave Chinner &imap, &nimap, 0); 1268c24b5dfaSDave Chinner if (error) 12695f8aca8bSChristoph Hellwig goto out; 1270c24b5dfaSDave Chinner ASSERT(nimap == 0 || nimap == 1); 1271c24b5dfaSDave Chinner if (nimap && imap.br_startblock != HOLESTARTBLOCK) { 1272c24b5dfaSDave Chinner ASSERT(imap.br_startblock != DELAYSTARTBLOCK); 1273c24b5dfaSDave Chinner mod++; 1274c24b5dfaSDave Chinner if (mod && (mod != mp->m_sb.sb_rextsize)) 1275c24b5dfaSDave Chinner endoffset_fsb -= mod; 1276c24b5dfaSDave Chinner } 1277c24b5dfaSDave Chinner } 1278c24b5dfaSDave Chinner if ((done = (endoffset_fsb <= startoffset_fsb))) 1279c24b5dfaSDave Chinner /* 1280c24b5dfaSDave Chinner * One contiguous piece to clear 1281c24b5dfaSDave Chinner */ 1282c24b5dfaSDave Chinner error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1); 1283c24b5dfaSDave Chinner else { 1284c24b5dfaSDave Chinner /* 1285c24b5dfaSDave Chinner * Some full blocks, possibly two pieces to clear 1286c24b5dfaSDave Chinner */ 1287c24b5dfaSDave Chinner if (offset < XFS_FSB_TO_B(mp, startoffset_fsb)) 1288c24b5dfaSDave Chinner error = xfs_zero_remaining_bytes(ip, offset, 1289c24b5dfaSDave Chinner XFS_FSB_TO_B(mp, startoffset_fsb) - 1); 1290c24b5dfaSDave Chinner if (!error && 1291c24b5dfaSDave Chinner XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len) 1292c24b5dfaSDave Chinner error = xfs_zero_remaining_bytes(ip, 1293c24b5dfaSDave Chinner XFS_FSB_TO_B(mp, endoffset_fsb), 1294c24b5dfaSDave Chinner offset + len - 1); 1295c24b5dfaSDave Chinner } 1296c24b5dfaSDave Chinner 1297c24b5dfaSDave Chinner /* 1298c24b5dfaSDave Chinner * free file space until done or until there is an error 1299c24b5dfaSDave Chinner */ 1300c24b5dfaSDave Chinner resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); 1301c24b5dfaSDave Chinner while (!error && !done) { 1302c24b5dfaSDave Chinner 1303c24b5dfaSDave Chinner /* 1304c24b5dfaSDave Chinner * allocate and setup the transaction. Allow this 1305c24b5dfaSDave Chinner * transaction to dip into the reserve blocks to ensure 1306c24b5dfaSDave Chinner * the freeing of the space succeeds at ENOSPC. 1307c24b5dfaSDave Chinner */ 1308c24b5dfaSDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); 13093d3c8b52SJie Liu error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, resblks, 0); 1310c24b5dfaSDave Chinner 1311c24b5dfaSDave Chinner /* 1312c24b5dfaSDave Chinner * check for running out of space 1313c24b5dfaSDave Chinner */ 1314c24b5dfaSDave Chinner if (error) { 1315c24b5dfaSDave Chinner /* 1316c24b5dfaSDave Chinner * Free the transaction structure. 1317c24b5dfaSDave Chinner */ 13182451337dSDave Chinner ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); 1319c24b5dfaSDave Chinner xfs_trans_cancel(tp, 0); 1320c24b5dfaSDave Chinner break; 1321c24b5dfaSDave Chinner } 1322c24b5dfaSDave Chinner xfs_ilock(ip, XFS_ILOCK_EXCL); 1323c24b5dfaSDave Chinner error = xfs_trans_reserve_quota(tp, mp, 1324c24b5dfaSDave Chinner ip->i_udquot, ip->i_gdquot, ip->i_pdquot, 1325c24b5dfaSDave Chinner resblks, 0, XFS_QMOPT_RES_REGBLKS); 1326c24b5dfaSDave Chinner if (error) 1327c24b5dfaSDave Chinner goto error1; 1328c24b5dfaSDave Chinner 1329c24b5dfaSDave Chinner xfs_trans_ijoin(tp, ip, 0); 1330c24b5dfaSDave Chinner 1331c24b5dfaSDave Chinner /* 1332c24b5dfaSDave Chinner * issue the bunmapi() call to free the blocks 1333c24b5dfaSDave Chinner */ 1334c24b5dfaSDave Chinner xfs_bmap_init(&free_list, &firstfsb); 1335c24b5dfaSDave Chinner error = xfs_bunmapi(tp, ip, startoffset_fsb, 1336c24b5dfaSDave Chinner endoffset_fsb - startoffset_fsb, 1337c24b5dfaSDave Chinner 0, 2, &firstfsb, &free_list, &done); 1338c24b5dfaSDave Chinner if (error) { 1339c24b5dfaSDave Chinner goto error0; 1340c24b5dfaSDave Chinner } 1341c24b5dfaSDave Chinner 1342c24b5dfaSDave Chinner /* 1343c24b5dfaSDave Chinner * complete the transaction 1344c24b5dfaSDave Chinner */ 1345c24b5dfaSDave Chinner error = xfs_bmap_finish(&tp, &free_list, &committed); 1346c24b5dfaSDave Chinner if (error) { 1347c24b5dfaSDave Chinner goto error0; 1348c24b5dfaSDave Chinner } 1349c24b5dfaSDave Chinner 1350c24b5dfaSDave Chinner error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1351c24b5dfaSDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL); 1352c24b5dfaSDave Chinner } 1353c24b5dfaSDave Chinner 13545f8aca8bSChristoph Hellwig out: 1355c24b5dfaSDave Chinner return error; 1356c24b5dfaSDave Chinner 1357c24b5dfaSDave Chinner error0: 1358c24b5dfaSDave Chinner xfs_bmap_cancel(&free_list); 1359c24b5dfaSDave Chinner error1: 1360c24b5dfaSDave Chinner xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 13615f8aca8bSChristoph Hellwig xfs_iunlock(ip, XFS_ILOCK_EXCL); 13625f8aca8bSChristoph Hellwig goto out; 1363c24b5dfaSDave Chinner } 1364c24b5dfaSDave Chinner 1365c24b5dfaSDave Chinner 1366865e9446SChristoph Hellwig int 1367c24b5dfaSDave Chinner xfs_zero_file_space( 1368c24b5dfaSDave Chinner struct xfs_inode *ip, 1369c24b5dfaSDave Chinner xfs_off_t offset, 13705f8aca8bSChristoph Hellwig xfs_off_t len) 1371c24b5dfaSDave Chinner { 1372c24b5dfaSDave Chinner struct xfs_mount *mp = ip->i_mount; 1373c24b5dfaSDave Chinner uint granularity; 1374c24b5dfaSDave Chinner xfs_off_t start_boundary; 1375c24b5dfaSDave Chinner xfs_off_t end_boundary; 1376c24b5dfaSDave Chinner int error; 1377c24b5dfaSDave Chinner 1378897b73b6SDave Chinner trace_xfs_zero_file_space(ip); 1379897b73b6SDave Chinner 1380c24b5dfaSDave Chinner granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE); 1381c24b5dfaSDave Chinner 1382c24b5dfaSDave Chinner /* 1383c24b5dfaSDave Chinner * Round the range of extents we are going to convert inwards. If the 1384c24b5dfaSDave Chinner * offset is aligned, then it doesn't get changed so we zero from the 1385c24b5dfaSDave Chinner * start of the block offset points to. 1386c24b5dfaSDave Chinner */ 1387c24b5dfaSDave Chinner start_boundary = round_up(offset, granularity); 1388c24b5dfaSDave Chinner end_boundary = round_down(offset + len, granularity); 1389c24b5dfaSDave Chinner 1390c24b5dfaSDave Chinner ASSERT(start_boundary >= offset); 1391c24b5dfaSDave Chinner ASSERT(end_boundary <= offset + len); 1392c24b5dfaSDave Chinner 1393c24b5dfaSDave Chinner if (start_boundary < end_boundary - 1) { 1394897b73b6SDave Chinner /* 1395897b73b6SDave Chinner * punch out delayed allocation blocks and the page cache over 1396897b73b6SDave Chinner * the conversion range 1397897b73b6SDave Chinner */ 1398897b73b6SDave Chinner xfs_ilock(ip, XFS_ILOCK_EXCL); 1399897b73b6SDave Chinner error = xfs_bmap_punch_delalloc_range(ip, 1400897b73b6SDave Chinner XFS_B_TO_FSBT(mp, start_boundary), 1401897b73b6SDave Chinner XFS_B_TO_FSB(mp, end_boundary - start_boundary)); 1402897b73b6SDave Chinner xfs_iunlock(ip, XFS_ILOCK_EXCL); 1403c24b5dfaSDave Chinner truncate_pagecache_range(VFS_I(ip), start_boundary, 1404c24b5dfaSDave Chinner end_boundary - 1); 1405897b73b6SDave Chinner 1406c24b5dfaSDave Chinner /* convert the blocks */ 1407c24b5dfaSDave Chinner error = xfs_alloc_file_space(ip, start_boundary, 1408c24b5dfaSDave Chinner end_boundary - start_boundary - 1, 14095f8aca8bSChristoph Hellwig XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT); 1410c24b5dfaSDave Chinner if (error) 14115f8aca8bSChristoph Hellwig goto out; 1412c24b5dfaSDave Chinner 1413c24b5dfaSDave Chinner /* We've handled the interior of the range, now for the edges */ 14145f8aca8bSChristoph Hellwig if (start_boundary != offset) { 1415c24b5dfaSDave Chinner error = xfs_iozero(ip, offset, start_boundary - offset); 1416c24b5dfaSDave Chinner if (error) 14175f8aca8bSChristoph Hellwig goto out; 14185f8aca8bSChristoph Hellwig } 1419c24b5dfaSDave Chinner 1420c24b5dfaSDave Chinner if (end_boundary != offset + len) 1421c24b5dfaSDave Chinner error = xfs_iozero(ip, end_boundary, 1422c24b5dfaSDave Chinner offset + len - end_boundary); 1423c24b5dfaSDave Chinner 1424c24b5dfaSDave Chinner } else { 1425c24b5dfaSDave Chinner /* 1426c24b5dfaSDave Chinner * It's either a sub-granularity range or the range spanned lies 1427c24b5dfaSDave Chinner * partially across two adjacent blocks. 1428c24b5dfaSDave Chinner */ 1429c24b5dfaSDave Chinner error = xfs_iozero(ip, offset, len); 1430c24b5dfaSDave Chinner } 1431c24b5dfaSDave Chinner 14325f8aca8bSChristoph Hellwig out: 1433c24b5dfaSDave Chinner return error; 1434c24b5dfaSDave Chinner 1435c24b5dfaSDave Chinner } 1436c24b5dfaSDave Chinner 1437c24b5dfaSDave Chinner /* 1438e1d8fb88SNamjae Jeon * xfs_collapse_file_space() 1439e1d8fb88SNamjae Jeon * This routine frees disk space and shift extent for the given file. 1440e1d8fb88SNamjae Jeon * The first thing we do is to free data blocks in the specified range 1441e1d8fb88SNamjae Jeon * by calling xfs_free_file_space(). It would also sync dirty data 1442e1d8fb88SNamjae Jeon * and invalidate page cache over the region on which collapse range 1443e1d8fb88SNamjae Jeon * is working. And Shift extent records to the left to cover a hole. 1444e1d8fb88SNamjae Jeon * RETURNS: 1445e1d8fb88SNamjae Jeon * 0 on success 1446e1d8fb88SNamjae Jeon * errno on error 1447e1d8fb88SNamjae Jeon * 1448e1d8fb88SNamjae Jeon */ 1449e1d8fb88SNamjae Jeon int 1450e1d8fb88SNamjae Jeon xfs_collapse_file_space( 1451e1d8fb88SNamjae Jeon struct xfs_inode *ip, 1452e1d8fb88SNamjae Jeon xfs_off_t offset, 1453e1d8fb88SNamjae Jeon xfs_off_t len) 1454e1d8fb88SNamjae Jeon { 1455e1d8fb88SNamjae Jeon int done = 0; 1456e1d8fb88SNamjae Jeon struct xfs_mount *mp = ip->i_mount; 1457e1d8fb88SNamjae Jeon struct xfs_trans *tp; 1458e1d8fb88SNamjae Jeon int error; 1459e1d8fb88SNamjae Jeon struct xfs_bmap_free free_list; 1460e1d8fb88SNamjae Jeon xfs_fsblock_t first_block; 1461e1d8fb88SNamjae Jeon int committed; 1462e1d8fb88SNamjae Jeon xfs_fileoff_t start_fsb; 1463*2c845f5aSBrian Foster xfs_fileoff_t next_fsb; 1464e1d8fb88SNamjae Jeon xfs_fileoff_t shift_fsb; 1465e1d8fb88SNamjae Jeon 1466e1d8fb88SNamjae Jeon ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 1467e1d8fb88SNamjae Jeon 1468e1d8fb88SNamjae Jeon trace_xfs_collapse_file_space(ip); 1469e1d8fb88SNamjae Jeon 1470*2c845f5aSBrian Foster next_fsb = XFS_B_TO_FSB(mp, offset + len); 1471e1d8fb88SNamjae Jeon shift_fsb = XFS_B_TO_FSB(mp, len); 1472e1d8fb88SNamjae Jeon 14731669a8caSDave Chinner /* 147441b9d726SBrian Foster * Writeback the entire file and force remove any post-eof blocks. The 147541b9d726SBrian Foster * writeback prevents changes to the extent list via concurrent 147641b9d726SBrian Foster * writeback and the eofblocks trim prevents the extent shift algorithm 147741b9d726SBrian Foster * from running into a post-eof delalloc extent. 14781669a8caSDave Chinner * 14791669a8caSDave Chinner * XXX: This is a temporary fix until the extent shift loop below is 14801669a8caSDave Chinner * converted to use offsets and lookups within the ILOCK rather than 14811669a8caSDave Chinner * carrying around the index into the extent list for the next 14821669a8caSDave Chinner * iteration. 14831669a8caSDave Chinner */ 14841669a8caSDave Chinner error = filemap_write_and_wait(VFS_I(ip)->i_mapping); 14851669a8caSDave Chinner if (error) 14861669a8caSDave Chinner return error; 148741b9d726SBrian Foster if (xfs_can_free_eofblocks(ip, true)) { 148841b9d726SBrian Foster error = xfs_free_eofblocks(mp, ip, false); 148941b9d726SBrian Foster if (error) 149041b9d726SBrian Foster return error; 149141b9d726SBrian Foster } 14921669a8caSDave Chinner 1493e1d8fb88SNamjae Jeon error = xfs_free_file_space(ip, offset, len); 1494e1d8fb88SNamjae Jeon if (error) 1495e1d8fb88SNamjae Jeon return error; 1496e1d8fb88SNamjae Jeon 1497e1d8fb88SNamjae Jeon while (!error && !done) { 1498e1d8fb88SNamjae Jeon tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); 1499e1d8fb88SNamjae Jeon /* 1500e1d8fb88SNamjae Jeon * We would need to reserve permanent block for transaction. 1501e1d8fb88SNamjae Jeon * This will come into picture when after shifting extent into 1502e1d8fb88SNamjae Jeon * hole we found that adjacent extents can be merged which 1503e1d8fb88SNamjae Jeon * may lead to freeing of a block during record update. 1504e1d8fb88SNamjae Jeon */ 1505e1d8fb88SNamjae Jeon error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, 1506e1d8fb88SNamjae Jeon XFS_DIOSTRAT_SPACE_RES(mp, 0), 0); 1507e1d8fb88SNamjae Jeon if (error) { 1508e1d8fb88SNamjae Jeon xfs_trans_cancel(tp, 0); 1509e1d8fb88SNamjae Jeon break; 1510e1d8fb88SNamjae Jeon } 1511e1d8fb88SNamjae Jeon 1512e1d8fb88SNamjae Jeon xfs_ilock(ip, XFS_ILOCK_EXCL); 1513e1d8fb88SNamjae Jeon error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, 1514e1d8fb88SNamjae Jeon ip->i_gdquot, ip->i_pdquot, 1515e1d8fb88SNamjae Jeon XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 1516e1d8fb88SNamjae Jeon XFS_QMOPT_RES_REGBLKS); 1517e1d8fb88SNamjae Jeon if (error) 1518e1d8fb88SNamjae Jeon goto out; 1519e1d8fb88SNamjae Jeon 1520e1d8fb88SNamjae Jeon xfs_trans_ijoin(tp, ip, 0); 1521e1d8fb88SNamjae Jeon 1522e1d8fb88SNamjae Jeon xfs_bmap_init(&free_list, &first_block); 1523e1d8fb88SNamjae Jeon 1524e1d8fb88SNamjae Jeon /* 1525e1d8fb88SNamjae Jeon * We are using the write transaction in which max 2 bmbt 1526e1d8fb88SNamjae Jeon * updates are allowed 1527e1d8fb88SNamjae Jeon */ 1528*2c845f5aSBrian Foster start_fsb = next_fsb; 1529*2c845f5aSBrian Foster error = xfs_bmap_shift_extents(tp, ip, start_fsb, shift_fsb, 1530*2c845f5aSBrian Foster &done, &next_fsb, &first_block, &free_list, 1531e1d8fb88SNamjae Jeon XFS_BMAP_MAX_SHIFT_EXTENTS); 1532e1d8fb88SNamjae Jeon if (error) 1533e1d8fb88SNamjae Jeon goto out; 1534e1d8fb88SNamjae Jeon 1535e1d8fb88SNamjae Jeon error = xfs_bmap_finish(&tp, &free_list, &committed); 1536e1d8fb88SNamjae Jeon if (error) 1537e1d8fb88SNamjae Jeon goto out; 1538e1d8fb88SNamjae Jeon 1539e1d8fb88SNamjae Jeon error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 1540e1d8fb88SNamjae Jeon xfs_iunlock(ip, XFS_ILOCK_EXCL); 1541e1d8fb88SNamjae Jeon } 1542e1d8fb88SNamjae Jeon 1543e1d8fb88SNamjae Jeon return error; 1544e1d8fb88SNamjae Jeon 1545e1d8fb88SNamjae Jeon out: 1546e1d8fb88SNamjae Jeon xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); 1547e1d8fb88SNamjae Jeon xfs_iunlock(ip, XFS_ILOCK_EXCL); 1548e1d8fb88SNamjae Jeon return error; 1549e1d8fb88SNamjae Jeon } 1550e1d8fb88SNamjae Jeon 1551e1d8fb88SNamjae Jeon /* 1552a133d952SDave Chinner * We need to check that the format of the data fork in the temporary inode is 1553a133d952SDave Chinner * valid for the target inode before doing the swap. This is not a problem with 1554a133d952SDave Chinner * attr1 because of the fixed fork offset, but attr2 has a dynamically sized 1555a133d952SDave Chinner * data fork depending on the space the attribute fork is taking so we can get 1556a133d952SDave Chinner * invalid formats on the target inode. 1557a133d952SDave Chinner * 1558a133d952SDave Chinner * E.g. target has space for 7 extents in extent format, temp inode only has 1559a133d952SDave Chinner * space for 6. If we defragment down to 7 extents, then the tmp format is a 1560a133d952SDave Chinner * btree, but when swapped it needs to be in extent format. Hence we can't just 1561a133d952SDave Chinner * blindly swap data forks on attr2 filesystems. 1562a133d952SDave Chinner * 1563a133d952SDave Chinner * Note that we check the swap in both directions so that we don't end up with 1564a133d952SDave Chinner * a corrupt temporary inode, either. 1565a133d952SDave Chinner * 1566a133d952SDave Chinner * Note that fixing the way xfs_fsr sets up the attribute fork in the source 1567a133d952SDave Chinner * inode will prevent this situation from occurring, so all we do here is 1568a133d952SDave Chinner * reject and log the attempt. basically we are putting the responsibility on 1569a133d952SDave Chinner * userspace to get this right. 1570a133d952SDave Chinner */ 1571a133d952SDave Chinner static int 1572a133d952SDave Chinner xfs_swap_extents_check_format( 1573a133d952SDave Chinner xfs_inode_t *ip, /* target inode */ 1574a133d952SDave Chinner xfs_inode_t *tip) /* tmp inode */ 1575a133d952SDave Chinner { 1576a133d952SDave Chinner 1577a133d952SDave Chinner /* Should never get a local format */ 1578a133d952SDave Chinner if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL || 1579a133d952SDave Chinner tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) 15802451337dSDave Chinner return -EINVAL; 1581a133d952SDave Chinner 1582a133d952SDave Chinner /* 1583a133d952SDave Chinner * if the target inode has less extents that then temporary inode then 1584a133d952SDave Chinner * why did userspace call us? 1585a133d952SDave Chinner */ 1586a133d952SDave Chinner if (ip->i_d.di_nextents < tip->i_d.di_nextents) 15872451337dSDave Chinner return -EINVAL; 1588a133d952SDave Chinner 1589a133d952SDave Chinner /* 1590a133d952SDave Chinner * if the target inode is in extent form and the temp inode is in btree 1591a133d952SDave Chinner * form then we will end up with the target inode in the wrong format 1592a133d952SDave Chinner * as we already know there are less extents in the temp inode. 1593a133d952SDave Chinner */ 1594a133d952SDave Chinner if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && 1595a133d952SDave Chinner tip->i_d.di_format == XFS_DINODE_FMT_BTREE) 15962451337dSDave Chinner return -EINVAL; 1597a133d952SDave Chinner 1598a133d952SDave Chinner /* Check temp in extent form to max in target */ 1599a133d952SDave Chinner if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && 1600a133d952SDave Chinner XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) > 1601a133d952SDave Chinner XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) 16022451337dSDave Chinner return -EINVAL; 1603a133d952SDave Chinner 1604a133d952SDave Chinner /* Check target in extent form to max in temp */ 1605a133d952SDave Chinner if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && 1606a133d952SDave Chinner XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > 1607a133d952SDave Chinner XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) 16082451337dSDave Chinner return -EINVAL; 1609a133d952SDave Chinner 1610a133d952SDave Chinner /* 1611a133d952SDave Chinner * If we are in a btree format, check that the temp root block will fit 1612a133d952SDave Chinner * in the target and that it has enough extents to be in btree format 1613a133d952SDave Chinner * in the target. 1614a133d952SDave Chinner * 1615a133d952SDave Chinner * Note that we have to be careful to allow btree->extent conversions 1616a133d952SDave Chinner * (a common defrag case) which will occur when the temp inode is in 1617a133d952SDave Chinner * extent format... 1618a133d952SDave Chinner */ 1619a133d952SDave Chinner if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) { 1620a133d952SDave Chinner if (XFS_IFORK_BOFF(ip) && 1621a133d952SDave Chinner XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip)) 16222451337dSDave Chinner return -EINVAL; 1623a133d952SDave Chinner if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <= 1624a133d952SDave Chinner XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) 16252451337dSDave Chinner return -EINVAL; 1626a133d952SDave Chinner } 1627a133d952SDave Chinner 1628a133d952SDave Chinner /* Reciprocal target->temp btree format checks */ 1629a133d952SDave Chinner if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { 1630a133d952SDave Chinner if (XFS_IFORK_BOFF(tip) && 1631a133d952SDave Chinner XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip)) 16322451337dSDave Chinner return -EINVAL; 1633a133d952SDave Chinner if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <= 1634a133d952SDave Chinner XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) 16352451337dSDave Chinner return -EINVAL; 1636a133d952SDave Chinner } 1637a133d952SDave Chinner 1638a133d952SDave Chinner return 0; 1639a133d952SDave Chinner } 1640a133d952SDave Chinner 1641a133d952SDave Chinner int 16424ef897a2SDave Chinner xfs_swap_extent_flush( 16434ef897a2SDave Chinner struct xfs_inode *ip) 16444ef897a2SDave Chinner { 16454ef897a2SDave Chinner int error; 16464ef897a2SDave Chinner 16474ef897a2SDave Chinner error = filemap_write_and_wait(VFS_I(ip)->i_mapping); 16484ef897a2SDave Chinner if (error) 16494ef897a2SDave Chinner return error; 16504ef897a2SDave Chinner truncate_pagecache_range(VFS_I(ip), 0, -1); 16514ef897a2SDave Chinner 16524ef897a2SDave Chinner /* Verify O_DIRECT for ftmp */ 16534ef897a2SDave Chinner if (VFS_I(ip)->i_mapping->nrpages) 16544ef897a2SDave Chinner return -EINVAL; 16554ef897a2SDave Chinner 16564ef897a2SDave Chinner /* 16574ef897a2SDave Chinner * Don't try to swap extents on mmap()d files because we can't lock 16584ef897a2SDave Chinner * out races against page faults safely. 16594ef897a2SDave Chinner */ 16604ef897a2SDave Chinner if (mapping_mapped(VFS_I(ip)->i_mapping)) 16614ef897a2SDave Chinner return -EBUSY; 16624ef897a2SDave Chinner return 0; 16634ef897a2SDave Chinner } 16644ef897a2SDave Chinner 16654ef897a2SDave Chinner int 1666a133d952SDave Chinner xfs_swap_extents( 1667a133d952SDave Chinner xfs_inode_t *ip, /* target inode */ 1668a133d952SDave Chinner xfs_inode_t *tip, /* tmp inode */ 1669a133d952SDave Chinner xfs_swapext_t *sxp) 1670a133d952SDave Chinner { 1671a133d952SDave Chinner xfs_mount_t *mp = ip->i_mount; 1672a133d952SDave Chinner xfs_trans_t *tp; 1673a133d952SDave Chinner xfs_bstat_t *sbp = &sxp->sx_stat; 1674a133d952SDave Chinner xfs_ifork_t *tempifp, *ifp, *tifp; 1675a133d952SDave Chinner int src_log_flags, target_log_flags; 1676a133d952SDave Chinner int error = 0; 1677a133d952SDave Chinner int aforkblks = 0; 1678a133d952SDave Chinner int taforkblks = 0; 1679a133d952SDave Chinner __uint64_t tmp; 168081217683SDave Chinner int lock_flags; 1681a133d952SDave Chinner 1682a133d952SDave Chinner tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL); 1683a133d952SDave Chinner if (!tempifp) { 16842451337dSDave Chinner error = -ENOMEM; 1685a133d952SDave Chinner goto out; 1686a133d952SDave Chinner } 1687a133d952SDave Chinner 1688a133d952SDave Chinner /* 168981217683SDave Chinner * Lock up the inodes against other IO and truncate to begin with. 169081217683SDave Chinner * Then we can ensure the inodes are flushed and have no page cache 169181217683SDave Chinner * safely. Once we have done this we can take the ilocks and do the rest 169281217683SDave Chinner * of the checks. 1693a133d952SDave Chinner */ 169481217683SDave Chinner lock_flags = XFS_IOLOCK_EXCL; 1695a133d952SDave Chinner xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL); 1696a133d952SDave Chinner 1697a133d952SDave Chinner /* Verify that both files have the same format */ 1698a133d952SDave Chinner if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) { 16992451337dSDave Chinner error = -EINVAL; 1700a133d952SDave Chinner goto out_unlock; 1701a133d952SDave Chinner } 1702a133d952SDave Chinner 1703a133d952SDave Chinner /* Verify both files are either real-time or non-realtime */ 1704a133d952SDave Chinner if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) { 17052451337dSDave Chinner error = -EINVAL; 1706a133d952SDave Chinner goto out_unlock; 1707a133d952SDave Chinner } 1708a133d952SDave Chinner 17094ef897a2SDave Chinner error = xfs_swap_extent_flush(ip); 1710a133d952SDave Chinner if (error) 1711a133d952SDave Chinner goto out_unlock; 17124ef897a2SDave Chinner error = xfs_swap_extent_flush(tip); 17134ef897a2SDave Chinner if (error) 17144ef897a2SDave Chinner goto out_unlock; 1715a133d952SDave Chinner 17164ef897a2SDave Chinner tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT); 17174ef897a2SDave Chinner error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0); 17184ef897a2SDave Chinner if (error) { 17194ef897a2SDave Chinner xfs_trans_cancel(tp, 0); 1720a133d952SDave Chinner goto out_unlock; 1721a133d952SDave Chinner } 17224ef897a2SDave Chinner xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL); 17234ef897a2SDave Chinner lock_flags |= XFS_ILOCK_EXCL; 1724a133d952SDave Chinner 1725a133d952SDave Chinner /* Verify all data are being swapped */ 1726a133d952SDave Chinner if (sxp->sx_offset != 0 || 1727a133d952SDave Chinner sxp->sx_length != ip->i_d.di_size || 1728a133d952SDave Chinner sxp->sx_length != tip->i_d.di_size) { 17292451337dSDave Chinner error = -EFAULT; 17304ef897a2SDave Chinner goto out_trans_cancel; 1731a133d952SDave Chinner } 1732a133d952SDave Chinner 1733a133d952SDave Chinner trace_xfs_swap_extent_before(ip, 0); 1734a133d952SDave Chinner trace_xfs_swap_extent_before(tip, 1); 1735a133d952SDave Chinner 1736a133d952SDave Chinner /* check inode formats now that data is flushed */ 1737a133d952SDave Chinner error = xfs_swap_extents_check_format(ip, tip); 1738a133d952SDave Chinner if (error) { 1739a133d952SDave Chinner xfs_notice(mp, 1740a133d952SDave Chinner "%s: inode 0x%llx format is incompatible for exchanging.", 1741a133d952SDave Chinner __func__, ip->i_ino); 17424ef897a2SDave Chinner goto out_trans_cancel; 1743a133d952SDave Chinner } 1744a133d952SDave Chinner 1745a133d952SDave Chinner /* 1746a133d952SDave Chinner * Compare the current change & modify times with that 1747a133d952SDave Chinner * passed in. If they differ, we abort this swap. 1748a133d952SDave Chinner * This is the mechanism used to ensure the calling 1749a133d952SDave Chinner * process that the file was not changed out from 1750a133d952SDave Chinner * under it. 1751a133d952SDave Chinner */ 1752a133d952SDave Chinner if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) || 1753a133d952SDave Chinner (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) || 1754a133d952SDave Chinner (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) || 1755a133d952SDave Chinner (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) { 17562451337dSDave Chinner error = -EBUSY; 175781217683SDave Chinner goto out_trans_cancel; 1758a133d952SDave Chinner } 1759a133d952SDave Chinner /* 1760a133d952SDave Chinner * Count the number of extended attribute blocks 1761a133d952SDave Chinner */ 1762a133d952SDave Chinner if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) && 1763a133d952SDave Chinner (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { 1764a133d952SDave Chinner error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks); 1765a133d952SDave Chinner if (error) 1766a133d952SDave Chinner goto out_trans_cancel; 1767a133d952SDave Chinner } 1768a133d952SDave Chinner if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) && 1769a133d952SDave Chinner (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { 1770a133d952SDave Chinner error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, 1771a133d952SDave Chinner &taforkblks); 1772a133d952SDave Chinner if (error) 1773a133d952SDave Chinner goto out_trans_cancel; 1774a133d952SDave Chinner } 1775a133d952SDave Chinner 177681217683SDave Chinner xfs_trans_ijoin(tp, ip, lock_flags); 177781217683SDave Chinner xfs_trans_ijoin(tp, tip, lock_flags); 177821b5c978SDave Chinner 177921b5c978SDave Chinner /* 178021b5c978SDave Chinner * Before we've swapped the forks, lets set the owners of the forks 178121b5c978SDave Chinner * appropriately. We have to do this as we are demand paging the btree 178221b5c978SDave Chinner * buffers, and so the validation done on read will expect the owner 178321b5c978SDave Chinner * field to be correctly set. Once we change the owners, we can swap the 178421b5c978SDave Chinner * inode forks. 178521b5c978SDave Chinner * 178621b5c978SDave Chinner * Note the trickiness in setting the log flags - we set the owner log 178721b5c978SDave Chinner * flag on the opposite inode (i.e. the inode we are setting the new 178821b5c978SDave Chinner * owner to be) because once we swap the forks and log that, log 178921b5c978SDave Chinner * recovery is going to see the fork as owned by the swapped inode, 179021b5c978SDave Chinner * not the pre-swapped inodes. 179121b5c978SDave Chinner */ 179221b5c978SDave Chinner src_log_flags = XFS_ILOG_CORE; 179321b5c978SDave Chinner target_log_flags = XFS_ILOG_CORE; 179421b5c978SDave Chinner if (ip->i_d.di_version == 3 && 179521b5c978SDave Chinner ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { 1796638f4416SDave Chinner target_log_flags |= XFS_ILOG_DOWNER; 1797638f4416SDave Chinner error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, 1798638f4416SDave Chinner tip->i_ino, NULL); 179921b5c978SDave Chinner if (error) 180021b5c978SDave Chinner goto out_trans_cancel; 180121b5c978SDave Chinner } 180221b5c978SDave Chinner 180321b5c978SDave Chinner if (tip->i_d.di_version == 3 && 180421b5c978SDave Chinner tip->i_d.di_format == XFS_DINODE_FMT_BTREE) { 1805638f4416SDave Chinner src_log_flags |= XFS_ILOG_DOWNER; 1806638f4416SDave Chinner error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK, 1807638f4416SDave Chinner ip->i_ino, NULL); 180821b5c978SDave Chinner if (error) 180921b5c978SDave Chinner goto out_trans_cancel; 181021b5c978SDave Chinner } 181121b5c978SDave Chinner 1812a133d952SDave Chinner /* 1813a133d952SDave Chinner * Swap the data forks of the inodes 1814a133d952SDave Chinner */ 1815a133d952SDave Chinner ifp = &ip->i_df; 1816a133d952SDave Chinner tifp = &tip->i_df; 1817a133d952SDave Chinner *tempifp = *ifp; /* struct copy */ 1818a133d952SDave Chinner *ifp = *tifp; /* struct copy */ 1819a133d952SDave Chinner *tifp = *tempifp; /* struct copy */ 1820a133d952SDave Chinner 1821a133d952SDave Chinner /* 1822a133d952SDave Chinner * Fix the on-disk inode values 1823a133d952SDave Chinner */ 1824a133d952SDave Chinner tmp = (__uint64_t)ip->i_d.di_nblocks; 1825a133d952SDave Chinner ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks; 1826a133d952SDave Chinner tip->i_d.di_nblocks = tmp + taforkblks - aforkblks; 1827a133d952SDave Chinner 1828a133d952SDave Chinner tmp = (__uint64_t) ip->i_d.di_nextents; 1829a133d952SDave Chinner ip->i_d.di_nextents = tip->i_d.di_nextents; 1830a133d952SDave Chinner tip->i_d.di_nextents = tmp; 1831a133d952SDave Chinner 1832a133d952SDave Chinner tmp = (__uint64_t) ip->i_d.di_format; 1833a133d952SDave Chinner ip->i_d.di_format = tip->i_d.di_format; 1834a133d952SDave Chinner tip->i_d.di_format = tmp; 1835a133d952SDave Chinner 1836a133d952SDave Chinner /* 1837a133d952SDave Chinner * The extents in the source inode could still contain speculative 1838a133d952SDave Chinner * preallocation beyond EOF (e.g. the file is open but not modified 1839a133d952SDave Chinner * while defrag is in progress). In that case, we need to copy over the 1840a133d952SDave Chinner * number of delalloc blocks the data fork in the source inode is 1841a133d952SDave Chinner * tracking beyond EOF so that when the fork is truncated away when the 1842a133d952SDave Chinner * temporary inode is unlinked we don't underrun the i_delayed_blks 1843a133d952SDave Chinner * counter on that inode. 1844a133d952SDave Chinner */ 1845a133d952SDave Chinner ASSERT(tip->i_delayed_blks == 0); 1846a133d952SDave Chinner tip->i_delayed_blks = ip->i_delayed_blks; 1847a133d952SDave Chinner ip->i_delayed_blks = 0; 1848a133d952SDave Chinner 1849a133d952SDave Chinner switch (ip->i_d.di_format) { 1850a133d952SDave Chinner case XFS_DINODE_FMT_EXTENTS: 1851a133d952SDave Chinner /* If the extents fit in the inode, fix the 1852a133d952SDave Chinner * pointer. Otherwise it's already NULL or 1853a133d952SDave Chinner * pointing to the extent. 1854a133d952SDave Chinner */ 1855a133d952SDave Chinner if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) { 1856a133d952SDave Chinner ifp->if_u1.if_extents = 1857a133d952SDave Chinner ifp->if_u2.if_inline_ext; 1858a133d952SDave Chinner } 1859a133d952SDave Chinner src_log_flags |= XFS_ILOG_DEXT; 1860a133d952SDave Chinner break; 1861a133d952SDave Chinner case XFS_DINODE_FMT_BTREE: 186221b5c978SDave Chinner ASSERT(ip->i_d.di_version < 3 || 1863638f4416SDave Chinner (src_log_flags & XFS_ILOG_DOWNER)); 1864a133d952SDave Chinner src_log_flags |= XFS_ILOG_DBROOT; 1865a133d952SDave Chinner break; 1866a133d952SDave Chinner } 1867a133d952SDave Chinner 1868a133d952SDave Chinner switch (tip->i_d.di_format) { 1869a133d952SDave Chinner case XFS_DINODE_FMT_EXTENTS: 1870a133d952SDave Chinner /* If the extents fit in the inode, fix the 1871a133d952SDave Chinner * pointer. Otherwise it's already NULL or 1872a133d952SDave Chinner * pointing to the extent. 1873a133d952SDave Chinner */ 1874a133d952SDave Chinner if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) { 1875a133d952SDave Chinner tifp->if_u1.if_extents = 1876a133d952SDave Chinner tifp->if_u2.if_inline_ext; 1877a133d952SDave Chinner } 1878a133d952SDave Chinner target_log_flags |= XFS_ILOG_DEXT; 1879a133d952SDave Chinner break; 1880a133d952SDave Chinner case XFS_DINODE_FMT_BTREE: 1881a133d952SDave Chinner target_log_flags |= XFS_ILOG_DBROOT; 188221b5c978SDave Chinner ASSERT(tip->i_d.di_version < 3 || 1883638f4416SDave Chinner (target_log_flags & XFS_ILOG_DOWNER)); 1884a133d952SDave Chinner break; 1885a133d952SDave Chinner } 1886a133d952SDave Chinner 1887a133d952SDave Chinner xfs_trans_log_inode(tp, ip, src_log_flags); 1888a133d952SDave Chinner xfs_trans_log_inode(tp, tip, target_log_flags); 1889a133d952SDave Chinner 1890a133d952SDave Chinner /* 1891a133d952SDave Chinner * If this is a synchronous mount, make sure that the 1892a133d952SDave Chinner * transaction goes to disk before returning to the user. 1893a133d952SDave Chinner */ 1894a133d952SDave Chinner if (mp->m_flags & XFS_MOUNT_WSYNC) 1895a133d952SDave Chinner xfs_trans_set_sync(tp); 1896a133d952SDave Chinner 1897a133d952SDave Chinner error = xfs_trans_commit(tp, 0); 1898a133d952SDave Chinner 1899a133d952SDave Chinner trace_xfs_swap_extent_after(ip, 0); 1900a133d952SDave Chinner trace_xfs_swap_extent_after(tip, 1); 1901a133d952SDave Chinner out: 1902a133d952SDave Chinner kmem_free(tempifp); 1903a133d952SDave Chinner return error; 1904a133d952SDave Chinner 1905a133d952SDave Chinner out_unlock: 190681217683SDave Chinner xfs_iunlock(ip, lock_flags); 190781217683SDave Chinner xfs_iunlock(tip, lock_flags); 1908a133d952SDave Chinner goto out; 1909a133d952SDave Chinner 1910a133d952SDave Chinner out_trans_cancel: 1911a133d952SDave Chinner xfs_trans_cancel(tp, 0); 1912a133d952SDave Chinner goto out_unlock; 1913a133d952SDave Chinner } 1914