LCOV - code coverage report
Current view: top level - fs/xfs - xfs_bmap_util.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 517 544 95.0 %
Date: 2023-07-31 20:08:12 Functions: 18 19 94.7 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
       4             :  * Copyright (c) 2012 Red Hat, Inc.
       5             :  * All Rights Reserved.
       6             :  */
       7             : #include "xfs.h"
       8             : #include "xfs_fs.h"
       9             : #include "xfs_shared.h"
      10             : #include "xfs_format.h"
      11             : #include "xfs_log_format.h"
      12             : #include "xfs_trans_resv.h"
      13             : #include "xfs_bit.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_defer.h"
      16             : #include "xfs_inode.h"
      17             : #include "xfs_btree.h"
      18             : #include "xfs_trans.h"
      19             : #include "xfs_alloc.h"
      20             : #include "xfs_bmap.h"
      21             : #include "xfs_bmap_util.h"
      22             : #include "xfs_bmap_btree.h"
      23             : #include "xfs_rtalloc.h"
      24             : #include "xfs_error.h"
      25             : #include "xfs_quota.h"
      26             : #include "xfs_trans_space.h"
      27             : #include "xfs_trace.h"
      28             : #include "xfs_icache.h"
      29             : #include "xfs_iomap.h"
      30             : #include "xfs_reflink.h"
      31             : #include "xfs_swapext.h"
      32             : 
      33             : /* Kernel only BMAP related definitions and functions */
      34             : 
      35             : /*
      36             :  * Convert the given file system block to a disk block.  We have to treat it
      37             :  * differently based on whether the file is a real time file or not, because the
      38             :  * bmap code does.
      39             :  */
      40             : xfs_daddr_t
      41   489553339 : xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
      42             : {
      43   489553339 :         if (XFS_IS_REALTIME_INODE(ip))
      44   214221151 :                 return XFS_FSB_TO_BB(ip->i_mount, fsb);
      45   275332188 :         return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
      46             : }
      47             : 
      48             : /*
      49             :  * Routine to zero an extent on disk allocated to the specific inode.
      50             :  *
      51             :  * The VFS functions take a linearised filesystem block offset, so we have to
      52             :  * convert the sparse xfs fsb to the right format first.
      53             :  * VFS types are real funky, too.
      54             :  */
      55             : int
      56           0 : xfs_zero_extent(
      57             :         struct xfs_inode        *ip,
      58             :         xfs_fsblock_t           start_fsb,
      59             :         xfs_off_t               count_fsb)
      60             : {
      61           0 :         struct xfs_mount        *mp = ip->i_mount;
      62           0 :         struct xfs_buftarg      *target = xfs_inode_buftarg(ip);
      63           0 :         xfs_daddr_t             sector = xfs_fsb_to_db(ip, start_fsb);
      64           0 :         sector_t                block = XFS_BB_TO_FSBT(mp, sector);
      65             : 
      66           0 :         return xfs_buftarg_zeroout(target,
      67           0 :                         block << (mp->m_super->s_blocksize_bits - 9),
      68           0 :                         count_fsb << (mp->m_super->s_blocksize_bits - 9),
      69             :                         GFP_NOFS, 0);
      70             : }
      71             : 
      72             : #ifdef CONFIG_XFS_RT
      73             : int
      74    57287185 : xfs_bmap_rtalloc(
      75             :         struct xfs_bmalloca     *ap)
      76             : {
      77    57287185 :         struct xfs_mount        *mp = ap->ip->i_mount;
      78    57287185 :         xfs_fileoff_t           orig_offset = ap->offset;
      79    57287185 :         xfs_rtblock_t           rtb;
      80    57287185 :         xfs_extlen_t            prod = 0;  /* product factor for allocators */
      81    57287185 :         xfs_extlen_t            mod = 0;   /* product factor for allocators */
      82    57287185 :         xfs_extlen_t            ralen = 0; /* realtime allocation length */
      83    57287185 :         xfs_extlen_t            align;     /* minimum allocation alignment */
      84    57287185 :         xfs_extlen_t            orig_length = ap->length;
      85    57287185 :         xfs_extlen_t            minlen = mp->m_sb.sb_rextsize;
      86    57287185 :         xfs_extlen_t            raminlen;
      87    57287185 :         bool                    rtlocked = false;
      88    57287185 :         bool                    ignore_locality = false;
      89    57287185 :         int                     error;
      90             : 
      91    57287185 :         align = xfs_get_extsz_hint(ap->ip);
      92             : retry:
      93    57268175 :         prod = align / mp->m_sb.sb_rextsize;
      94   171804525 :         error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
      95    57268175 :                                         align, 1, ap->eof, 0,
      96    57268175 :                                         ap->conv, &ap->offset, &ap->length);
      97    57254612 :         if (error)
      98           0 :                 return error;
      99    57254612 :         ASSERT(ap->length);
     100    57254612 :         ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
     101             : 
     102             :         /*
     103             :          * If we shifted the file offset downward to satisfy an extent size
     104             :          * hint, increase minlen by that amount so that the allocator won't
     105             :          * give us an allocation that's too short to cover at least one of the
     106             :          * blocks that the caller asked for.
     107             :          */
     108    57254612 :         if (ap->offset != orig_offset)
     109     2453749 :                 minlen += orig_offset - ap->offset;
     110             : 
     111             :         /*
     112             :          * If the offset & length are not perfectly aligned
     113             :          * then kill prod, it will just get us in trouble.
     114             :          */
     115    57254612 :         div_u64_rem(ap->offset, align, &mod);
     116    57307979 :         if (mod || ap->length % align)
     117             :                 prod = 1;
     118             :         /*
     119             :          * Set ralen to be the actual requested length in rtextents.
     120             :          */
     121    57307979 :         ralen = ap->length / mp->m_sb.sb_rextsize;
     122             :         /*
     123             :          * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
     124             :          * we rounded up to it, cut it back so it's valid again.
     125             :          * Note that if it's a really large request (bigger than
     126             :          * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
     127             :          * adjust the starting point to match it.
     128             :          */
     129    57307979 :         if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN)
     130       15748 :                 ralen = XFS_MAX_BMBT_EXTLEN / mp->m_sb.sb_rextsize;
     131             : 
     132             :         /*
     133             :          * Lock out modifications to both the RT bitmap and summary inodes
     134             :          */
     135    57307979 :         if (!rtlocked) {
     136    57307959 :                 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
     137    57355582 :                 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
     138    57355582 :                 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
     139    57355582 :                 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
     140    57355582 :                 rtlocked = true;
     141             :         }
     142             : 
     143             :         /*
     144             :          * If it's an allocation to an empty file at offset 0,
     145             :          * pick an extent that will space things out in the rt area.
     146             :          */
     147    57355602 :         if (ap->eof && ap->offset == 0) {
     148     6259690 :                 xfs_rtblock_t rtx; /* realtime extent no */
     149             : 
     150     6259690 :                 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
     151     6259690 :                 if (error)
     152           0 :                         return error;
     153     6259690 :                 ap->blkno = rtx * mp->m_sb.sb_rextsize;
     154             :         } else {
     155    51095912 :                 ap->blkno = 0;
     156             :         }
     157             : 
     158    57355602 :         xfs_bmap_adjacent(ap);
     159             : 
     160             :         /*
     161             :          * Realtime allocation, done through xfs_rtallocate_extent.
     162             :          */
     163    57355602 :         if (ignore_locality)
     164          10 :                 ap->blkno = 0;
     165             :         else
     166    57355592 :                 do_div(ap->blkno, mp->m_sb.sb_rextsize);
     167    57355602 :         rtb = ap->blkno;
     168    57355602 :         ap->length = ralen;
     169    57355602 :         raminlen = max_t(xfs_extlen_t, 1, minlen / mp->m_sb.sb_rextsize);
     170   114711204 :         error = xfs_rtallocate_extent(ap->tp, ap->blkno, raminlen, ap->length,
     171    57355602 :                         &ralen, ap->wasdel, prod, &rtb);
     172    57355602 :         if (error)
     173          12 :                 return error;
     174             : 
     175    57355590 :         if (rtb != NULLRTBLOCK) {
     176    57355570 :                 ap->blkno = rtb * mp->m_sb.sb_rextsize;
     177    57355570 :                 ap->length = ralen * mp->m_sb.sb_rextsize;
     178    57355570 :                 ap->ip->i_nblocks += ap->length;
     179    57355570 :                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
     180    57355570 :                 if (ap->wasdel)
     181           0 :                         ap->ip->i_delayed_blks -= ap->length;
     182             :                 /*
     183             :                  * Adjust the disk quota also. This was reserved
     184             :                  * earlier.
     185             :                  */
     186   172066710 :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
     187    57355570 :                         ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
     188    57355570 :                                         XFS_TRANS_DQ_RTBCOUNT, ap->length);
     189    57355570 :                 return 0;
     190             :         }
     191             : 
     192          20 :         if (align > mp->m_sb.sb_rextsize) {
     193             :                 /*
     194             :                  * We previously enlarged the request length to try to satisfy
     195             :                  * an extent size hint.  The allocator didn't return anything,
     196             :                  * so reset the parameters to the original values and try again
     197             :                  * without alignment criteria.
     198             :                  */
     199          10 :                 ap->offset = orig_offset;
     200          10 :                 ap->length = orig_length;
     201          10 :                 minlen = align = mp->m_sb.sb_rextsize;
     202          10 :                 goto retry;
     203             :         }
     204             : 
     205          10 :         if (!ignore_locality && ap->blkno != 0) {
     206             :                 /*
     207             :                  * If we can't allocate near a specific rt extent, try again
     208             :                  * without locality criteria.
     209             :                  */
     210          10 :                 ignore_locality = true;
     211          10 :                 goto retry;
     212             :         }
     213             : 
     214           0 :         ap->blkno = NULLFSBLOCK;
     215           0 :         ap->length = 0;
     216           0 :         return 0;
     217             : }
     218             : #endif /* CONFIG_XFS_RT */
     219             : 
     220             : /*
     221             :  * Extent tree block counting routines.
     222             :  */
     223             : 
     224             : /*
     225             :  * Count leaf blocks given a range of extent records.  Delayed allocation
     226             :  * extents are not counted towards the totals.
     227             :  */
     228             : xfs_extnum_t
     229   100212759 : xfs_bmap_count_leaves(
     230             :         struct xfs_ifork        *ifp,
     231             :         xfs_filblks_t           *count)
     232             : {
     233   100212759 :         struct xfs_iext_cursor  icur;
     234   100212759 :         struct xfs_bmbt_irec    got;
     235   100212759 :         xfs_extnum_t            numrecs = 0;
     236             : 
     237   435001442 :         for_each_xfs_iext(ifp, &icur, &got) {
     238   334788683 :                 if (!isnullstartblock(got.br_startblock)) {
     239   334783845 :                         *count += got.br_blockcount;
     240   334783845 :                         numrecs++;
     241             :                 }
     242             :         }
     243             : 
     244   100250567 :         return numrecs;
     245             : }
     246             : 
     247             : /*
     248             :  * Count fsblocks of the given fork.  Delayed allocation extents are
     249             :  * not counted towards the totals.
     250             :  */
     251             : int
     252   359554317 : xfs_bmap_count_blocks(
     253             :         struct xfs_trans        *tp,
     254             :         struct xfs_inode        *ip,
     255             :         int                     whichfork,
     256             :         xfs_extnum_t            *nextents,
     257             :         xfs_filblks_t           *count)
     258             : {
     259   359554317 :         struct xfs_mount        *mp = ip->i_mount;
     260   359554317 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     261   359492077 :         struct xfs_btree_cur    *cur;
     262   359492077 :         xfs_extlen_t            btblocks = 0;
     263   359492077 :         int                     error;
     264             : 
     265   359492077 :         *nextents = 0;
     266   359492077 :         *count = 0;
     267             : 
     268   359492077 :         if (!ifp)
     269             :                 return 0;
     270             : 
     271   354156651 :         switch (ifp->if_format) {
     272     2375688 :         case XFS_DINODE_FMT_BTREE:
     273     2375688 :                 error = xfs_iread_extents(tp, ip, whichfork);
     274     2375766 :                 if (error)
     275             :                         return error;
     276             : 
     277     2375774 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     278     2376399 :                 error = xfs_btree_count_blocks(cur, &btblocks);
     279     2376148 :                 xfs_btree_del_cursor(cur, error);
     280     2376125 :                 if (error)
     281             :                         return error;
     282             : 
     283             :                 /*
     284             :                  * xfs_btree_count_blocks includes the root block contained in
     285             :                  * the inode fork in @btblocks, so subtract one because we're
     286             :                  * only interested in allocated disk blocks.
     287             :                  */
     288     2376125 :                 *count += btblocks - 1;
     289             : 
     290   100100689 :                 fallthrough;
     291   100100689 :         case XFS_DINODE_FMT_EXTENTS:
     292   100100689 :                 *nextents = xfs_bmap_count_leaves(ifp, count);
     293   100273053 :                 break;
     294             :         }
     295             : 
     296             :         return 0;
     297             : }
     298             : 
     299             : static int
     300     3618326 : xfs_getbmap_report_one(
     301             :         struct xfs_inode        *ip,
     302             :         struct getbmapx         *bmv,
     303             :         struct kgetbmap         *out,
     304             :         int64_t                 bmv_end,
     305             :         struct xfs_bmbt_irec    *got)
     306             : {
     307     3618326 :         struct kgetbmap         *p = out + bmv->bmv_entries;
     308     3618326 :         bool                    shared = false;
     309     3618326 :         int                     error;
     310             : 
     311     3618326 :         error = xfs_reflink_trim_around_shared(ip, got, &shared);
     312     3618331 :         if (error)
     313             :                 return error;
     314             : 
     315     3618331 :         if (isnullstartblock(got->br_startblock) ||
     316             :             got->br_startblock == DELAYSTARTBLOCK) {
     317             :                 /*
     318             :                  * Take the flush completion as being a point-in-time snapshot
     319             :                  * where there are no delalloc extents, and if any new ones
     320             :                  * have been created racily, just skip them as being 'after'
     321             :                  * the flush and so don't get reported.
     322             :                  */
     323       15125 :                 if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
     324             :                         return 0;
     325             : 
     326       15125 :                 p->bmv_oflags |= BMV_OF_DELALLOC;
     327       15125 :                 p->bmv_block = -2;
     328             :         } else {
     329     3603206 :                 p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
     330             :         }
     331             : 
     332     3618305 :         if (got->br_state == XFS_EXT_UNWRITTEN &&
     333     1612925 :             (bmv->bmv_iflags & BMV_IF_PREALLOC))
     334      433971 :                 p->bmv_oflags |= BMV_OF_PREALLOC;
     335             : 
     336     3618305 :         if (shared)
     337      391061 :                 p->bmv_oflags |= BMV_OF_SHARED;
     338             : 
     339     3618305 :         p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
     340     3618305 :         p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
     341             : 
     342     3618305 :         bmv->bmv_offset = p->bmv_offset + p->bmv_length;
     343     3618305 :         bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
     344     3618305 :         bmv->bmv_entries++;
     345     3618305 :         return 0;
     346             : }
     347             : 
     348             : static void
     349     1895739 : xfs_getbmap_report_hole(
     350             :         struct xfs_inode        *ip,
     351             :         struct getbmapx         *bmv,
     352             :         struct kgetbmap         *out,
     353             :         int64_t                 bmv_end,
     354             :         xfs_fileoff_t           bno,
     355             :         xfs_fileoff_t           end)
     356             : {
     357     1895739 :         struct kgetbmap         *p = out + bmv->bmv_entries;
     358             : 
     359     1895739 :         if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
     360             :                 return;
     361             : 
     362      106876 :         p->bmv_block = -1;
     363      106876 :         p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
     364      106876 :         p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
     365             : 
     366      106876 :         bmv->bmv_offset = p->bmv_offset + p->bmv_length;
     367      106876 :         bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
     368      106876 :         bmv->bmv_entries++;
     369             : }
     370             : 
     371             : static inline bool
     372             : xfs_getbmap_full(
     373             :         struct getbmapx         *bmv)
     374             : {
     375     9049417 :         return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
     376             : }
     377             : 
     378             : static bool
     379     3397702 : xfs_getbmap_next_rec(
     380             :         struct xfs_bmbt_irec    *rec,
     381             :         xfs_fileoff_t           total_end)
     382             : {
     383     3397702 :         xfs_fileoff_t           end = rec->br_startoff + rec->br_blockcount;
     384             : 
     385     3397702 :         if (end == total_end)
     386             :                 return false;
     387             : 
     388       32447 :         rec->br_startoff += rec->br_blockcount;
     389       32447 :         if (!isnullstartblock(rec->br_startblock) &&
     390             :             rec->br_startblock != DELAYSTARTBLOCK)
     391       32447 :                 rec->br_startblock += rec->br_blockcount;
     392       32447 :         rec->br_blockcount = total_end - end;
     393       32447 :         return true;
     394             : }
     395             : 
     396             : /*
     397             :  * Get inode's extents as described in bmv, and format for output.
     398             :  * Calls formatter to fill the user's buffer until all extents
     399             :  * are mapped, until the passed-in bmv->bmv_count slots have
     400             :  * been filled, or until the formatter short-circuits the loop,
     401             :  * if it is tracking filled-in extents on its own.
     402             :  */
     403             : int                                             /* error code */
     404      779996 : xfs_getbmap(
     405             :         struct xfs_inode        *ip,
     406             :         struct getbmapx         *bmv,           /* user bmap structure */
     407             :         struct kgetbmap         *out)
     408             : {
     409      779996 :         struct xfs_mount        *mp = ip->i_mount;
     410      779996 :         int                     iflags = bmv->bmv_iflags;
     411      779996 :         int                     whichfork, lock, error = 0;
     412      779996 :         int64_t                 bmv_end, max_len;
     413      779996 :         xfs_fileoff_t           bno, first_bno;
     414      779996 :         struct xfs_ifork        *ifp;
     415      779996 :         struct xfs_bmbt_irec    got, rec;
     416      779996 :         xfs_filblks_t           len;
     417      779996 :         struct xfs_iext_cursor  icur;
     418             : 
     419      779996 :         if (bmv->bmv_iflags & ~BMV_IF_VALID)
     420             :                 return -EINVAL;
     421             : #ifndef DEBUG
     422             :         /* Only allow CoW fork queries if we're debugging. */
     423             :         if (iflags & BMV_IF_COWFORK)
     424             :                 return -EINVAL;
     425             : #endif
     426      779996 :         if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
     427             :                 return -EINVAL;
     428             : 
     429      779996 :         if (bmv->bmv_length < -1)
     430             :                 return -EINVAL;
     431      779996 :         bmv->bmv_entries = 0;
     432      779996 :         if (bmv->bmv_length == 0)
     433             :                 return 0;
     434             : 
     435      716849 :         if (iflags & BMV_IF_ATTRFORK)
     436             :                 whichfork = XFS_ATTR_FORK;
     437      716821 :         else if (iflags & BMV_IF_COWFORK)
     438             :                 whichfork = XFS_COW_FORK;
     439             :         else
     440      716626 :                 whichfork = XFS_DATA_FORK;
     441             : 
     442      716849 :         xfs_ilock(ip, XFS_IOLOCK_SHARED);
     443      716798 :         switch (whichfork) {
     444          18 :         case XFS_ATTR_FORK:
     445          18 :                 lock = xfs_ilock_attr_map_shared(ip);
     446          18 :                 if (!xfs_inode_has_attr_fork(ip))
     447           0 :                         goto out_unlock_ilock;
     448             : 
     449             :                 max_len = 1LL << 32;
     450             :                 break;
     451         166 :         case XFS_COW_FORK:
     452         166 :                 lock = XFS_ILOCK_SHARED;
     453         166 :                 xfs_ilock(ip, lock);
     454             : 
     455             :                 /* No CoW fork? Just return */
     456         166 :                 if (!xfs_ifork_ptr(ip, whichfork))
     457          48 :                         goto out_unlock_ilock;
     458             : 
     459         118 :                 if (xfs_get_cowextsz_hint(ip))
     460         118 :                         max_len = mp->m_super->s_maxbytes;
     461             :                 else
     462           0 :                         max_len = XFS_ISIZE(ip);
     463             :                 break;
     464      716614 :         case XFS_DATA_FORK:
     465      716614 :                 if (!(iflags & BMV_IF_DELALLOC) &&
     466      490927 :                     (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
     467       23722 :                         error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
     468       23736 :                         if (error)
     469           0 :                                 goto out_unlock_iolock;
     470             : 
     471             :                         /*
     472             :                          * Even after flushing the inode, there can still be
     473             :                          * delalloc blocks on the inode beyond EOF due to
     474             :                          * speculative preallocation.  These are not removed
     475             :                          * until the release function is called or the inode
     476             :                          * is inactivated.  Hence we cannot assert here that
     477             :                          * ip->i_delayed_blks == 0.
     478             :                          */
     479             :                 }
     480             : 
     481      716628 :                 if (xfs_get_extsz_hint(ip) ||
     482      644252 :                     (ip->i_diflags &
     483             :                      (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
     484      270021 :                         max_len = mp->m_super->s_maxbytes;
     485             :                 else
     486      446545 :                         max_len = XFS_ISIZE(ip);
     487             : 
     488      716566 :                 lock = xfs_ilock_data_map_shared(ip);
     489      716636 :                 break;
     490             :         }
     491             : 
     492      716772 :         ifp = xfs_ifork_ptr(ip, whichfork);
     493             : 
     494      716782 :         switch (ifp->if_format) {
     495             :         case XFS_DINODE_FMT_EXTENTS:
     496             :         case XFS_DINODE_FMT_BTREE:
     497      576269 :                 break;
     498      140513 :         case XFS_DINODE_FMT_LOCAL:
     499             :                 /* Local format inode forks report no extents. */
     500      140513 :                 goto out_unlock_ilock;
     501           0 :         default:
     502           0 :                 error = -EINVAL;
     503           0 :                 goto out_unlock_ilock;
     504             :         }
     505             : 
     506      576269 :         if (bmv->bmv_length == -1) {
     507      574297 :                 max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
     508      574297 :                 bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
     509             :         }
     510             : 
     511      576269 :         bmv_end = bmv->bmv_offset + bmv->bmv_length;
     512             : 
     513      576269 :         first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
     514      576269 :         len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
     515             : 
     516      576269 :         error = xfs_iread_extents(NULL, ip, whichfork);
     517      576239 :         if (error)
     518           0 :                 goto out_unlock_ilock;
     519             : 
     520      576239 :         if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
     521             :                 /*
     522             :                  * Report a whole-file hole if the delalloc flag is set to
     523             :                  * stay compatible with the old implementation.
     524             :                  */
     525       72341 :                 if (iflags & BMV_IF_DELALLOC)
     526      152247 :                         xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
     527      101498 :                                         XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
     528       72341 :                 goto out_unlock_ilock;
     529             :         }
     530             : 
     531     7172242 :         while (!xfs_getbmap_full(bmv)) {
     532     3586117 :                 xfs_trim_extent(&got, first_bno, len);
     533             : 
     534             :                 /*
     535             :                  * Report an entry for a hole if this extent doesn't directly
     536             :                  * follow the previous one.
     537             :                  */
     538     3586055 :                 if (got.br_startoff > bno) {
     539     1777332 :                         xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
     540             :                                         got.br_startoff);
     541     3554664 :                         if (xfs_getbmap_full(bmv))
     542             :                                 break;
     543             :                 }
     544             : 
     545             :                 /*
     546             :                  * In order to report shared extents accurately, we report each
     547             :                  * distinct shared / unshared part of a single bmbt record with
     548             :                  * an individual getbmapx record.
     549             :                  */
     550     3585872 :                 bno = got.br_startoff + got.br_blockcount;
     551     3585872 :                 rec = got;
     552     3618319 :                 do {
     553     3618319 :                         error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
     554             :                                         &rec);
     555     7236605 :                         if (error || xfs_getbmap_full(bmv))
     556      220600 :                                 goto out_unlock_ilock;
     557     3397702 :                 } while (xfs_getbmap_next_rec(&rec, bno));
     558             : 
     559     3365255 :                 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
     560      566164 :                         xfs_fileoff_t   end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
     561             : 
     562      283082 :                         if (bmv->bmv_entries > 0)
     563      283081 :                                 out[bmv->bmv_entries - 1].bmv_oflags |=
     564             :                                                                 BMV_OF_LAST;
     565             : 
     566      350740 :                         if (whichfork != XFS_ATTR_FORK && bno < end &&
     567             :                             !xfs_getbmap_full(bmv)) {
     568       67658 :                                 xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
     569             :                                                 bno, end);
     570             :                         }
     571             :                         break;
     572             :                 }
     573             : 
     574     3082174 :                 if (bno >= first_bno + len)
     575             :                         break;
     576             :         }
     577             : 
     578         184 : out_unlock_ilock:
     579      716768 :         xfs_iunlock(ip, lock);
     580      716798 : out_unlock_iolock:
     581      716798 :         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
     582      716798 :         return error;
     583             : }
     584             : 
     585             : /*
     586             :  * Dead simple method of punching delalyed allocation blocks from a range in
     587             :  * the inode.  This will always punch out both the start and end blocks, even
     588             :  * if the ranges only partially overlap them, so it is up to the caller to
     589             :  * ensure that partial blocks are not passed in.
     590             :  */
     591             : int
     592       42565 : xfs_bmap_punch_delalloc_range(
     593             :         struct xfs_inode        *ip,
     594             :         xfs_off_t               start_byte,
     595             :         xfs_off_t               end_byte)
     596             : {
     597       42565 :         struct xfs_mount        *mp = ip->i_mount;
     598       42565 :         struct xfs_ifork        *ifp = &ip->i_df;
     599       42565 :         xfs_fileoff_t           start_fsb = XFS_B_TO_FSBT(mp, start_byte);
     600       42565 :         xfs_fileoff_t           end_fsb = XFS_B_TO_FSB(mp, end_byte);
     601       42565 :         struct xfs_bmbt_irec    got, del;
     602       42565 :         struct xfs_iext_cursor  icur;
     603       42565 :         int                     error = 0;
     604             : 
     605       42565 :         ASSERT(!xfs_need_iread_extents(ifp));
     606             : 
     607       42565 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     608       42565 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
     609         351 :                 goto out_unlock;
     610             : 
     611       81399 :         while (got.br_startoff + got.br_blockcount > start_fsb) {
     612       46889 :                 del = got;
     613       46889 :                 xfs_trim_extent(&del, start_fsb, end_fsb - start_fsb);
     614             : 
     615             :                 /*
     616             :                  * A delete can push the cursor forward. Step back to the
     617             :                  * previous extent on non-delalloc or extents outside the
     618             :                  * target range.
     619             :                  */
     620       46890 :                 if (!del.br_blockcount ||
     621       42738 :                     !isnullstartblock(del.br_startblock)) {
     622       20163 :                         if (!xfs_iext_prev_extent(ifp, &icur, &got))
     623             :                                 break;
     624       14584 :                         continue;
     625             :                 }
     626             : 
     627       26727 :                 error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
     628             :                                                   &got, &del);
     629       26726 :                 if (error || !xfs_iext_get_extent(ifp, &icur, &got))
     630             :                         break;
     631             :         }
     632             : 
     633       42215 : out_unlock:
     634       42566 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     635       42565 :         return error;
     636             : }
     637             : 
     638             : /*
     639             :  * Test whether it is appropriate to check an inode for and free post EOF
     640             :  * blocks. The 'force' parameter determines whether we should also consider
     641             :  * regular files that are marked preallocated or append-only.
     642             :  */
     643             : bool
     644  1098993662 : xfs_can_free_eofblocks(
     645             :         struct xfs_inode        *ip,
     646             :         bool                    force)
     647             : {
     648  1098993662 :         struct xfs_bmbt_irec    imap;
     649  1098993662 :         struct xfs_mount        *mp = ip->i_mount;
     650  1098993662 :         xfs_fileoff_t           end_fsb;
     651  1098993662 :         xfs_fileoff_t           last_fsb;
     652  1098993662 :         int                     nimaps = 1;
     653  1098993662 :         int                     error;
     654             : 
     655             :         /*
     656             :          * Caller must either hold the exclusive io lock; or be inactivating
     657             :          * the inode, which guarantees there are no other users of the inode.
     658             :          */
     659  1098993662 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) ||
     660             :                (VFS_I(ip)->i_state & I_FREEING));
     661             : 
     662             :         /* prealloc/delalloc exists only on regular files */
     663  1098640585 :         if (!S_ISREG(VFS_I(ip)->i_mode))
     664             :                 return false;
     665             : 
     666             :         /*
     667             :          * Zero sized files with no cached pages and delalloc blocks will not
     668             :          * have speculative prealloc/delalloc blocks to remove.
     669             :          */
     670   733290904 :         if (VFS_I(ip)->i_size == 0 &&
     671   215757304 :             VFS_I(ip)->i_mapping->nrpages == 0 &&
     672   215831324 :             ip->i_delayed_blks == 0)
     673             :                 return false;
     674             : 
     675             :         /* If we haven't read in the extent list, then don't do it now. */
     676   517471659 :         if (xfs_need_iread_extents(&ip->i_df))
     677             :                 return false;
     678             : 
     679             :         /*
     680             :          * Do not free extent size hints, real preallocated or append-only files
     681             :          * unless the file has delalloc blocks and we are forced to remove
     682             :          * them.
     683             :          */
     684   503612570 :         if (xfs_get_extsz_hint(ip) ||
     685   357599717 :             (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) {
     686   295289467 :                 if (!force || ip->i_delayed_blks == 0)
     687             :                         return false;
     688             :         }
     689             : 
     690             :         /*
     691             :          * Do not try to free post-EOF blocks if EOF is beyond the end of the
     692             :          * range supported by the page cache, because the truncation will loop
     693             :          * forever.
     694             :          */
     695   419141848 :         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
     696   209570924 :         if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1)
     697           0 :                 end_fsb = roundup_64(end_fsb, mp->m_sb.sb_rextsize);
     698   209570924 :         last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
     699   209570924 :         if (last_fsb <= end_fsb)
     700             :                 return false;
     701             : 
     702             :         /*
     703             :          * Look up the mapping for the first block past EOF.  If we can't find
     704             :          * it, there's nothing to free.
     705             :          */
     706   209532768 :         xfs_ilock(ip, XFS_ILOCK_SHARED);
     707   209577342 :         error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
     708             :                         0);
     709   209583982 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
     710   209588972 :         if (error || nimaps == 0)
     711             :                 return false;
     712             : 
     713             :         /*
     714             :          * If there's a real mapping there or there are delayed allocation
     715             :          * reservations, then we have post-EOF blocks to try to free.
     716             :          */
     717   366059137 :         return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks;
     718             : }
     719             : 
     720             : /*
     721             :  * This is called to free any blocks beyond eof. The caller must hold
     722             :  * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
     723             :  * reference to the inode.
     724             :  */
     725             : int
     726    13123350 : xfs_free_eofblocks(
     727             :         struct xfs_inode        *ip)
     728             : {
     729    13123350 :         struct xfs_trans        *tp;
     730    13123350 :         struct xfs_mount        *mp = ip->i_mount;
     731    13123350 :         int                     error;
     732             : 
     733             :         /* Attach the dquots to the inode up front. */
     734    13123350 :         error = xfs_qm_dqattach(ip);
     735    13073771 :         if (error)
     736             :                 return error;
     737             : 
     738             :         /* Wait on dio to ensure i_size has settled. */
     739    13083510 :         inode_dio_wait(VFS_I(ip));
     740             : 
     741    13065112 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
     742    13248280 :         if (error) {
     743           0 :                 ASSERT(xfs_is_shutdown(mp));
     744           0 :                 return error;
     745             :         }
     746             : 
     747    13248280 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     748    13239538 :         xfs_trans_ijoin(tp, ip, 0);
     749             : 
     750             :         /*
     751             :          * Do not update the on-disk file size.  If we update the on-disk file
     752             :          * size and then the system crashes before the contents of the file are
     753             :          * flushed to disk then the files may be full of holes (ie NULL files
     754             :          * bug).
     755             :          */
     756    26434226 :         error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
     757             :                                 XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
     758    13249159 :         if (error)
     759          32 :                 goto err_cancel;
     760             : 
     761    13249127 :         error = xfs_trans_commit(tp);
     762    13238499 :         if (error)
     763           0 :                 goto out_unlock;
     764             : 
     765    13238499 :         xfs_inode_clear_eofblocks_tag(ip);
     766    13245270 :         goto out_unlock;
     767             : 
     768             : err_cancel:
     769             :         /*
     770             :          * If we get an error at this point we simply don't
     771             :          * bother truncating the file.
     772             :          */
     773          32 :         xfs_trans_cancel(tp);
     774    13245302 : out_unlock:
     775    13245302 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     776    13245302 :         return error;
     777             : }
     778             : 
     779             : int
     780    12340243 : xfs_alloc_file_space(
     781             :         struct xfs_inode        *ip,
     782             :         xfs_off_t               offset,
     783             :         xfs_off_t               len)
     784             : {
     785    12340243 :         xfs_mount_t             *mp = ip->i_mount;
     786    12340243 :         xfs_off_t               count;
     787    12340243 :         xfs_filblks_t           allocated_fsb;
     788    12340243 :         xfs_filblks_t           allocatesize_fsb;
     789    12340243 :         xfs_extlen_t            extsz, temp;
     790    12340243 :         xfs_fileoff_t           startoffset_fsb;
     791    12340243 :         xfs_fileoff_t           endoffset_fsb;
     792    12340243 :         int                     nimaps;
     793    12340243 :         int                     rt;
     794    12340243 :         xfs_trans_t             *tp;
     795    12340243 :         xfs_bmbt_irec_t         imaps[1], *imapp;
     796    12340243 :         int                     error;
     797             : 
     798    12340243 :         trace_xfs_alloc_file_space(ip);
     799             : 
     800    24680374 :         if (xfs_is_shutdown(mp))
     801             :                 return -EIO;
     802             : 
     803    12340182 :         error = xfs_qm_dqattach(ip);
     804    12340204 :         if (error)
     805             :                 return error;
     806             : 
     807    12340204 :         if (len <= 0)
     808             :                 return -EINVAL;
     809             : 
     810    12340204 :         rt = XFS_IS_REALTIME_INODE(ip);
     811    12340204 :         extsz = xfs_get_extsz_hint(ip);
     812             : 
     813    12340149 :         count = len;
     814    12340149 :         imapp = &imaps[0];
     815    12340149 :         nimaps = 1;
     816    12340149 :         startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
     817    12340149 :         endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
     818    12340149 :         allocatesize_fsb = endoffset_fsb - startoffset_fsb;
     819             : 
     820             :         /*
     821             :          * Allocate file space until done or until there is an error
     822             :          */
     823    34461400 :         while (allocatesize_fsb && !error) {
     824    22399841 :                 xfs_fileoff_t   s, e;
     825    22399841 :                 unsigned int    dblocks, rblocks, resblks;
     826             : 
     827             :                 /*
     828             :                  * Determine space reservations for data/realtime.
     829             :                  */
     830    22399841 :                 if (unlikely(extsz)) {
     831     9690915 :                         s = startoffset_fsb;
     832     9690915 :                         do_div(s, extsz);
     833     9690915 :                         s *= extsz;
     834     9690915 :                         e = startoffset_fsb + allocatesize_fsb;
     835     9690915 :                         div_u64_rem(startoffset_fsb, extsz, &temp);
     836     9690918 :                         if (temp)
     837     1735704 :                                 e += temp;
     838     9690918 :                         div_u64_rem(e, extsz, &temp);
     839     9690938 :                         if (temp)
     840     2221051 :                                 e += extsz - temp;
     841             :                 } else {
     842             :                         s = 0;
     843             :                         e = allocatesize_fsb;
     844             :                 }
     845             : 
     846             :                 /*
     847             :                  * The transaction reservation is limited to a 32-bit block
     848             :                  * count, hence we need to limit the number of blocks we are
     849             :                  * trying to reserve to avoid an overflow. We can't allocate
     850             :                  * more than @nimaps extents, and an extent is limited on disk
     851             :                  * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
     852             :                  * limit.
     853             :                  */
     854    22399864 :                 resblks = min_t(xfs_fileoff_t, (e - s),
     855             :                                 (XFS_MAX_BMBT_EXTLEN * nimaps));
     856    22399864 :                 if (unlikely(rt)) {
     857     9689767 :                         dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
     858     9689767 :                         rblocks = resblks;
     859             :                 } else {
     860    12710097 :                         dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
     861    12710097 :                         rblocks = 0;
     862             :                 }
     863             : 
     864    22399864 :                 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
     865             :                                 dblocks, rblocks, false, &tp);
     866    22399469 :                 if (error)
     867             :                         break;
     868             : 
     869    22121214 :                 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     870             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     871    22120482 :                 if (error == -EFBIG)
     872          15 :                         error = xfs_iext_count_upgrade(tp, ip,
     873             :                                         XFS_IEXT_ADD_NOSPLIT_CNT);
     874    22120482 :                 if (error)
     875          15 :                         goto error;
     876             : 
     877    22120467 :                 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
     878             :                                 allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
     879             :                                 &nimaps);
     880    22121991 :                 if (error)
     881         161 :                         goto error;
     882             : 
     883    22121830 :                 ip->i_diflags |= XFS_DIFLAG_PREALLOC;
     884    22121830 :                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     885             : 
     886    22122681 :                 error = xfs_trans_commit(tp);
     887    22122234 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     888    22121597 :                 if (error)
     889             :                         break;
     890             : 
     891    22121331 :                 allocated_fsb = imapp->br_blockcount;
     892             : 
     893    22121331 :                 if (nimaps == 0) {
     894             :                         error = -ENOSPC;
     895             :                         break;
     896             :                 }
     897             : 
     898    22121251 :                 startoffset_fsb += allocated_fsb;
     899    22121251 :                 allocatesize_fsb -= allocated_fsb;
     900             :         }
     901             : 
     902             :         return error;
     903             : 
     904         176 : error:
     905         176 :         xfs_trans_cancel(tp);
     906         176 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     907         176 :         return error;
     908             : }
     909             : 
     910             : static int
     911    47002320 : xfs_unmap_extent(
     912             :         struct xfs_inode        *ip,
     913             :         xfs_fileoff_t           startoffset_fsb,
     914             :         xfs_filblks_t           len_fsb,
     915             :         int                     *done)
     916             : {
     917    47002320 :         struct xfs_mount        *mp = ip->i_mount;
     918    47002320 :         struct xfs_trans        *tp;
     919    47002320 :         uint                    resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
     920    47002320 :         int                     error;
     921             : 
     922    47002320 :         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
     923             :                         false, &tp);
     924    47002679 :         if (error)
     925             :                 return error;
     926             : 
     927    46953258 :         error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     928             :                         XFS_IEXT_PUNCH_HOLE_CNT);
     929    46952747 :         if (error == -EFBIG)
     930         128 :                 error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
     931    46952747 :         if (error)
     932         128 :                 goto out_trans_cancel;
     933             : 
     934    46952619 :         error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
     935    46953121 :         if (error)
     936          12 :                 goto out_trans_cancel;
     937             : 
     938    46953109 :         error = xfs_trans_commit(tp);
     939    46953505 : out_unlock:
     940    46953505 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     941    46953505 :         return error;
     942             : 
     943         140 : out_trans_cancel:
     944         140 :         xfs_trans_cancel(tp);
     945         140 :         goto out_unlock;
     946             : }
     947             : 
     948             : /* Caller must first wait for the completion of any pending DIOs if required. */
     949             : int
     950   169188089 : xfs_flush_unmap_range(
     951             :         struct xfs_inode        *ip,
     952             :         xfs_off_t               offset,
     953             :         xfs_off_t               len)
     954             : {
     955   169188089 :         struct xfs_mount        *mp = ip->i_mount;
     956   169188089 :         struct inode            *inode = VFS_I(ip);
     957   169188089 :         xfs_off_t               rounding, start, end;
     958   169188089 :         int                     error;
     959             : 
     960   169188089 :         rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
     961   169188089 :         start = round_down(offset, rounding);
     962   169188089 :         end = round_up(offset + len, rounding) - 1;
     963             : 
     964   169188089 :         error = filemap_write_and_wait_range(inode->i_mapping, start, end);
     965   169184649 :         if (error)
     966             :                 return error;
     967   169184502 :         truncate_pagecache_range(inode, start, end);
     968   169184502 :         return 0;
     969             : }
     970             : 
     971             : int
     972    45180224 : xfs_free_file_space(
     973             :         struct xfs_inode        *ip,
     974             :         xfs_off_t               offset,
     975             :         xfs_off_t               len)
     976             : {
     977    45180224 :         struct xfs_mount        *mp = ip->i_mount;
     978    45180224 :         xfs_fileoff_t           startoffset_fsb;
     979    45180224 :         xfs_fileoff_t           endoffset_fsb;
     980    45180224 :         int                     done = 0, error;
     981             : 
     982    45180224 :         trace_xfs_free_file_space(ip);
     983             : 
     984    45179948 :         error = xfs_qm_dqattach(ip);
     985    45179736 :         if (error)
     986             :                 return error;
     987             : 
     988    45179736 :         if (len <= 0)        /* if nothing being freed */
     989             :                 return 0;
     990             : 
     991    45179736 :         startoffset_fsb = XFS_B_TO_FSB(mp, offset);
     992    45179736 :         endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
     993             : 
     994             :         /* We can only free complete realtime extents. */
     995    45179736 :         if (xfs_inode_has_bigrtextents(ip)) {
     996     1636381 :                 startoffset_fsb = roundup_64(startoffset_fsb,
     997             :                                              mp->m_sb.sb_rextsize);
     998     1636380 :                 endoffset_fsb = rounddown_64(endoffset_fsb,
     999             :                                              mp->m_sb.sb_rextsize);
    1000             :         }
    1001             : 
    1002             :         /*
    1003             :          * Need to zero the stuff we're not freeing, on disk.
    1004             :          */
    1005    45179735 :         if (endoffset_fsb > startoffset_fsb) {
    1006    91229517 :                 while (!done) {
    1007    47002109 :                         error = xfs_unmap_extent(ip, startoffset_fsb,
    1008             :                                         endoffset_fsb - startoffset_fsb, &done);
    1009    47002853 :                         if (error)
    1010       49572 :                                 return error;
    1011             :                 }
    1012             :         }
    1013             : 
    1014             :         /*
    1015             :          * Now that we've unmap all full blocks we'll have to zero out any
    1016             :          * partial block at the beginning and/or end.  xfs_zero_range is smart
    1017             :          * enough to skip any holes, including those we just created, but we
    1018             :          * must take care not to zero beyond EOF and enlarge i_size.
    1019             :          */
    1020    90261814 :         if (offset >= XFS_ISIZE(ip))
    1021             :                 return 0;
    1022    42806109 :         if (offset + len > XFS_ISIZE(ip))
    1023      663174 :                 len = XFS_ISIZE(ip) - offset;
    1024    42806109 :         error = xfs_zero_range(ip, offset, len, NULL);
    1025    42806011 :         if (error)
    1026             :                 return error;
    1027             : 
    1028             :         /*
    1029             :          * If we zeroed right up to EOF and EOF straddles a page boundary we
    1030             :          * must make sure that the post-EOF area is also zeroed because the
    1031             :          * page could be mmap'd and xfs_zero_range doesn't do that for us.
    1032             :          * Writeback of the eof page will do this, albeit clumsily.
    1033             :          */
    1034    85609706 :         if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
    1035      826988 :                 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
    1036             :                                 round_down(offset + len, PAGE_SIZE), LLONG_MAX);
    1037             :         }
    1038             : 
    1039             :         return error;
    1040             : }
    1041             : 
    1042             : static int
    1043     4199750 : xfs_prepare_shift(
    1044             :         struct xfs_inode        *ip,
    1045             :         loff_t                  offset)
    1046             : {
    1047     4199750 :         struct xfs_mount        *mp = ip->i_mount;
    1048     4199750 :         int                     error;
    1049             : 
    1050             :         /*
    1051             :          * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
    1052             :          * into the accessible region of the file.
    1053             :          */
    1054     4199750 :         if (xfs_can_free_eofblocks(ip, true)) {
    1055     1581819 :                 error = xfs_free_eofblocks(ip);
    1056     1581825 :                 if (error)
    1057             :                         return error;
    1058             :         }
    1059             : 
    1060             :         /*
    1061             :          * Shift operations must stabilize the start block offset boundary along
    1062             :          * with the full range of the operation. If we don't, a COW writeback
    1063             :          * completion could race with an insert, front merge with the start
    1064             :          * extent (after split) during the shift and corrupt the file. Start
    1065             :          * with the block just prior to the start to stabilize the boundary.
    1066             :          */
    1067     4199755 :         offset = round_down(offset, mp->m_sb.sb_blocksize);
    1068     4199755 :         if (offset)
    1069     4026882 :                 offset -= mp->m_sb.sb_blocksize;
    1070             : 
    1071             :         /*
    1072             :          * Writeback and invalidate cache for the remainder of the file as we're
    1073             :          * about to shift down every extent from offset to EOF.
    1074             :          */
    1075     8399510 :         error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
    1076     4199760 :         if (error)
    1077             :                 return error;
    1078             : 
    1079             :         /*
    1080             :          * Clean out anything hanging around in the cow fork now that
    1081             :          * we've flushed all the dirty data out to disk to avoid having
    1082             :          * CoW extents at the wrong offsets.
    1083             :          */
    1084     8399374 :         if (xfs_inode_has_cow_data(ip)) {
    1085     1303189 :                 error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
    1086             :                                 true);
    1087     1303189 :                 if (error)
    1088           0 :                         return error;
    1089             :         }
    1090             : 
    1091             :         return 0;
    1092             : }
    1093             : 
    1094             : /*
    1095             :  * xfs_collapse_file_space()
    1096             :  *      This routine frees disk space and shift extent for the given file.
    1097             :  *      The first thing we do is to free data blocks in the specified range
    1098             :  *      by calling xfs_free_file_space(). It would also sync dirty data
    1099             :  *      and invalidate page cache over the region on which collapse range
    1100             :  *      is working. And Shift extent records to the left to cover a hole.
    1101             :  * RETURNS:
    1102             :  *      0 on success
    1103             :  *      errno on error
    1104             :  *
    1105             :  */
    1106             : int
    1107     2406026 : xfs_collapse_file_space(
    1108             :         struct xfs_inode        *ip,
    1109             :         xfs_off_t               offset,
    1110             :         xfs_off_t               len)
    1111             : {
    1112     2406026 :         struct xfs_mount        *mp = ip->i_mount;
    1113     2406026 :         struct xfs_trans        *tp;
    1114     2406026 :         int                     error;
    1115     2406026 :         xfs_fileoff_t           next_fsb = XFS_B_TO_FSB(mp, offset + len);
    1116     2406026 :         xfs_fileoff_t           shift_fsb = XFS_B_TO_FSB(mp, len);
    1117     2406026 :         bool                    done = false;
    1118             : 
    1119     2406026 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    1120     2406024 :         ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
    1121             : 
    1122     2406023 :         trace_xfs_collapse_file_space(ip);
    1123             : 
    1124     2406025 :         error = xfs_free_file_space(ip, offset, len);
    1125     2406029 :         if (error)
    1126             :                 return error;
    1127             : 
    1128     2404236 :         error = xfs_prepare_shift(ip, offset);
    1129     2404242 :         if (error)
    1130             :                 return error;
    1131             : 
    1132     2404186 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
    1133     2404187 :         if (error)
    1134             :                 return error;
    1135             : 
    1136     2404187 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1137     2404187 :         xfs_trans_ijoin(tp, ip, 0);
    1138             : 
    1139   105502793 :         while (!done) {
    1140   105502793 :                 error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
    1141             :                                 &done);
    1142   105502792 :                 if (error)
    1143           3 :                         goto out_trans_cancel;
    1144   105502789 :                 if (done)
    1145             :                         break;
    1146             : 
    1147             :                 /* finish any deferred frees and roll the transaction */
    1148   103098607 :                 error = xfs_defer_finish(&tp);
    1149   103098608 :                 if (error)
    1150           2 :                         goto out_trans_cancel;
    1151             :         }
    1152             : 
    1153     2404182 :         error = xfs_trans_commit(tp);
    1154     2404182 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1155     2404182 :         return error;
    1156             : 
    1157           5 : out_trans_cancel:
    1158           5 :         xfs_trans_cancel(tp);
    1159           5 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1160           5 :         return error;
    1161             : }
    1162             : 
    1163             : /*
    1164             :  * xfs_insert_file_space()
    1165             :  *      This routine create hole space by shifting extents for the given file.
    1166             :  *      The first thing we do is to sync dirty data and invalidate page cache
    1167             :  *      over the region on which insert range is working. And split an extent
    1168             :  *      to two extents at given offset by calling xfs_bmap_split_extent.
    1169             :  *      And shift all extent records which are laying between [offset,
    1170             :  *      last allocated extent] to the right to reserve hole range.
    1171             :  * RETURNS:
    1172             :  *      0 on success
    1173             :  *      errno on error
    1174             :  */
    1175             : int
    1176     1795515 : xfs_insert_file_space(
    1177             :         struct xfs_inode        *ip,
    1178             :         loff_t                  offset,
    1179             :         loff_t                  len)
    1180             : {
    1181     1795515 :         struct xfs_mount        *mp = ip->i_mount;
    1182     1795515 :         struct xfs_trans        *tp;
    1183     1795515 :         int                     error;
    1184     1795515 :         xfs_fileoff_t           stop_fsb = XFS_B_TO_FSB(mp, offset);
    1185     1795515 :         xfs_fileoff_t           next_fsb = NULLFSBLOCK;
    1186     1795515 :         xfs_fileoff_t           shift_fsb = XFS_B_TO_FSB(mp, len);
    1187     1795515 :         bool                    done = false;
    1188             : 
    1189     1795515 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    1190     1795510 :         ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
    1191             : 
    1192     1795510 :         trace_xfs_insert_file_space(ip);
    1193             : 
    1194     1795510 :         error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
    1195     1795515 :         if (error)
    1196             :                 return error;
    1197             : 
    1198     1795513 :         error = xfs_prepare_shift(ip, offset);
    1199     1795520 :         if (error)
    1200             :                 return error;
    1201             : 
    1202     1795503 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
    1203     1795503 :                         XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
    1204     1795503 :         if (error)
    1205             :                 return error;
    1206             : 
    1207     1793527 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1208     1793527 :         xfs_trans_ijoin(tp, ip, 0);
    1209             : 
    1210     1793527 :         error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
    1211             :                         XFS_IEXT_PUNCH_HOLE_CNT);
    1212     1793526 :         if (error == -EFBIG)
    1213          11 :                 error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
    1214     1793526 :         if (error)
    1215          11 :                 goto out_trans_cancel;
    1216             : 
    1217             :         /*
    1218             :          * The extent shifting code works on extent granularity. So, if stop_fsb
    1219             :          * is not the starting block of extent, we need to split the extent at
    1220             :          * stop_fsb.
    1221             :          */
    1222     1793515 :         error = xfs_bmap_split_extent(tp, ip, stop_fsb);
    1223     1793515 :         if (error)
    1224           1 :                 goto out_trans_cancel;
    1225             : 
    1226     9738233 :         do {
    1227     9738233 :                 error = xfs_defer_finish(&tp);
    1228     9738225 :                 if (error)
    1229           1 :                         goto out_trans_cancel;
    1230             : 
    1231     9738224 :                 error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
    1232             :                                 &done, stop_fsb);
    1233     9738232 :                 if (error)
    1234           0 :                         goto out_trans_cancel;
    1235     9738232 :         } while (!done);
    1236             : 
    1237     1793513 :         error = xfs_trans_commit(tp);
    1238     1793514 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1239     1793514 :         return error;
    1240             : 
    1241          13 : out_trans_cancel:
    1242          13 :         xfs_trans_cancel(tp);
    1243          13 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1244          13 :         return error;
    1245             : }

Generated by: LCOV version 1.14