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-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 504 545 92.5 %
Date: 2023-07-31 20:08:07 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    93780056 : xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
      42             : {
      43    93780056 :         if (XFS_IS_REALTIME_INODE(ip))
      44    23490846 :                 return XFS_FSB_TO_BB(ip->i_mount, fsb);
      45    70289210 :         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     8144098 : xfs_bmap_rtalloc(
      75             :         struct xfs_bmalloca     *ap)
      76             : {
      77     8144098 :         struct xfs_mount        *mp = ap->ip->i_mount;
      78     8144098 :         xfs_fileoff_t           orig_offset = ap->offset;
      79     8144098 :         xfs_rtblock_t           rtb;
      80     8144098 :         xfs_extlen_t            prod = 0;  /* product factor for allocators */
      81     8144098 :         xfs_extlen_t            mod = 0;   /* product factor for allocators */
      82     8144098 :         xfs_extlen_t            ralen = 0; /* realtime allocation length */
      83     8144098 :         xfs_extlen_t            align;     /* minimum allocation alignment */
      84     8144098 :         xfs_extlen_t            orig_length = ap->length;
      85     8144098 :         xfs_extlen_t            minlen = mp->m_sb.sb_rextsize;
      86     8144098 :         xfs_extlen_t            raminlen;
      87     8144098 :         bool                    rtlocked = false;
      88     8144098 :         bool                    ignore_locality = false;
      89     8144098 :         int                     error;
      90             : 
      91     8144098 :         align = xfs_get_extsz_hint(ap->ip);
      92             : retry:
      93     8143993 :         prod = align / mp->m_sb.sb_rextsize;
      94     8143993 :         error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
      95     8143993 :                                         align, 1, ap->eof, 0,
      96     8143993 :                                         ap->conv, &ap->offset, &ap->length);
      97     8144058 :         if (error)
      98           0 :                 return error;
      99     8144058 :         ASSERT(ap->length);
     100     8144058 :         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     8144058 :         if (ap->offset != orig_offset)
     109           4 :                 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     8144058 :         div_u64_rem(ap->offset, align, &mod);
     116     8144058 :         if (mod || ap->length % align)
     117          70 :                 prod = 1;
     118             :         /*
     119             :          * Set ralen to be the actual requested length in rtextents.
     120             :          */
     121     8144058 :         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     8144058 :         if (ralen * mp->m_sb.sb_rextsize >= XFS_MAX_BMBT_EXTLEN)
     130           0 :                 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     8144058 :         if (!rtlocked) {
     136     8143896 :                 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
     137     8144290 :                 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
     138     8144290 :                 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
     139     8144290 :                 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
     140     8144290 :                 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     8144452 :         if (ap->eof && ap->offset == 0) {
     148        8581 :                 xfs_rtblock_t rtx; /* realtime extent no */
     149             : 
     150        8581 :                 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
     151        8581 :                 if (error)
     152           0 :                         return error;
     153        8581 :                 ap->blkno = rtx * mp->m_sb.sb_rextsize;
     154             :         } else {
     155     8135871 :                 ap->blkno = 0;
     156             :         }
     157             : 
     158     8144452 :         xfs_bmap_adjacent(ap);
     159             : 
     160             :         /*
     161             :          * Realtime allocation, done through xfs_rtallocate_extent.
     162             :          */
     163     8144290 :         if (ignore_locality)
     164           0 :                 ap->blkno = 0;
     165             :         else
     166     8144290 :                 do_div(ap->blkno, mp->m_sb.sb_rextsize);
     167     8144290 :         rtb = ap->blkno;
     168     8144290 :         ap->length = ralen;
     169     8144290 :         raminlen = max_t(xfs_extlen_t, 1, minlen / mp->m_sb.sb_rextsize);
     170     8144290 :         error = xfs_rtallocate_extent(ap->tp, ap->blkno, raminlen, ap->length,
     171     8144290 :                         &ralen, ap->wasdel, prod, &rtb);
     172     8144290 :         if (error)
     173           0 :                 return error;
     174             : 
     175     8144290 :         if (rtb != NULLRTBLOCK) {
     176     8144290 :                 ap->blkno = rtb * mp->m_sb.sb_rextsize;
     177     8144290 :                 ap->length = ralen * mp->m_sb.sb_rextsize;
     178     8144290 :                 ap->ip->i_nblocks += ap->length;
     179     8144290 :                 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
     180     8144290 :                 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    16288580 :                 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
     187     8144290 :                         ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
     188     8144290 :                                         XFS_TRANS_DQ_RTBCOUNT, ap->length);
     189     8144290 :                 return 0;
     190             :         }
     191             : 
     192           0 :         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           0 :                 ap->offset = orig_offset;
     200           0 :                 ap->length = orig_length;
     201           0 :                 minlen = align = mp->m_sb.sb_rextsize;
     202           0 :                 goto retry;
     203             :         }
     204             : 
     205           0 :         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           0 :                 ignore_locality = true;
     211           0 :                 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    41710644 : xfs_bmap_count_leaves(
     230             :         struct xfs_ifork        *ifp,
     231             :         xfs_filblks_t           *count)
     232             : {
     233    41710644 :         struct xfs_iext_cursor  icur;
     234    41710644 :         struct xfs_bmbt_irec    got;
     235    41710644 :         xfs_extnum_t            numrecs = 0;
     236             : 
     237   258057619 :         for_each_xfs_iext(ifp, &icur, &got) {
     238   216346975 :                 if (!isnullstartblock(got.br_startblock)) {
     239   216344571 :                         *count += got.br_blockcount;
     240   216344571 :                         numrecs++;
     241             :                 }
     242             :         }
     243             : 
     244    41710649 :         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   206529852 : 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   206529852 :         struct xfs_mount        *mp = ip->i_mount;
     260   206529852 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, whichfork);
     261   206531046 :         struct xfs_btree_cur    *cur;
     262   206531046 :         xfs_extlen_t            btblocks = 0;
     263   206531046 :         int                     error;
     264             : 
     265   206531046 :         *nextents = 0;
     266   206531046 :         *count = 0;
     267             : 
     268   206531046 :         if (!ifp)
     269             :                 return 0;
     270             : 
     271   206462503 :         switch (ifp->if_format) {
     272     1550889 :         case XFS_DINODE_FMT_BTREE:
     273     1550889 :                 error = xfs_iread_extents(tp, ip, whichfork);
     274     1550889 :                 if (error)
     275             :                         return error;
     276             : 
     277     1550888 :                 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
     278     1550886 :                 error = xfs_btree_count_blocks(cur, &btblocks);
     279     1550887 :                 xfs_btree_del_cursor(cur, error);
     280     1550890 :                 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     1550890 :                 *count += btblocks - 1;
     289             : 
     290    41710716 :                 fallthrough;
     291    41710716 :         case XFS_DINODE_FMT_EXTENTS:
     292    41710716 :                 *nextents = xfs_bmap_count_leaves(ifp, count);
     293    41710623 :                 break;
     294             :         }
     295             : 
     296             :         return 0;
     297             : }
     298             : 
     299             : static int
     300     4216091 : 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     4216091 :         struct kgetbmap         *p = out + bmv->bmv_entries;
     308     4216091 :         bool                    shared = false;
     309     4216091 :         int                     error;
     310             : 
     311     4216091 :         error = xfs_reflink_trim_around_shared(ip, got, &shared);
     312     4216059 :         if (error)
     313             :                 return error;
     314             : 
     315     4216059 :         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        6464 :                 if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
     324             :                         return 0;
     325             : 
     326        6464 :                 p->bmv_oflags |= BMV_OF_DELALLOC;
     327        6464 :                 p->bmv_block = -2;
     328             :         } else {
     329     4209595 :                 p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
     330             :         }
     331             : 
     332     4216059 :         if (got->br_state == XFS_EXT_UNWRITTEN &&
     333     1281613 :             (bmv->bmv_iflags & BMV_IF_PREALLOC))
     334       77359 :                 p->bmv_oflags |= BMV_OF_PREALLOC;
     335             : 
     336     4216059 :         if (shared)
     337      160348 :                 p->bmv_oflags |= BMV_OF_SHARED;
     338             : 
     339     4216059 :         p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
     340     4216059 :         p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
     341             : 
     342     4216059 :         bmv->bmv_offset = p->bmv_offset + p->bmv_length;
     343     4216059 :         bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
     344     4216059 :         bmv->bmv_entries++;
     345     4216059 :         return 0;
     346             : }
     347             : 
     348             : static void
     349     3621922 : 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     3621922 :         struct kgetbmap         *p = out + bmv->bmv_entries;
     358             : 
     359     3621922 :         if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
     360             :                 return;
     361             : 
     362       17525 :         p->bmv_block = -1;
     363       17525 :         p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
     364       17525 :         p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
     365             : 
     366       17525 :         bmv->bmv_offset = p->bmv_offset + p->bmv_length;
     367       17525 :         bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
     368       17525 :         bmv->bmv_entries++;
     369             : }
     370             : 
     371             : static inline bool
     372             : xfs_getbmap_full(
     373             :         struct getbmapx         *bmv)
     374             : {
     375    11928719 :         return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
     376             : }
     377             : 
     378             : static bool
     379     4013243 : xfs_getbmap_next_rec(
     380             :         struct xfs_bmbt_irec    *rec,
     381             :         xfs_fileoff_t           total_end)
     382             : {
     383     4013243 :         xfs_fileoff_t           end = rec->br_startoff + rec->br_blockcount;
     384             : 
     385     4013243 :         if (end == total_end)
     386             :                 return false;
     387             : 
     388       20773 :         rec->br_startoff += rec->br_blockcount;
     389       20773 :         if (!isnullstartblock(rec->br_startblock) &&
     390             :             rec->br_startblock != DELAYSTARTBLOCK)
     391       20773 :                 rec->br_startblock += rec->br_blockcount;
     392       20773 :         rec->br_blockcount = total_end - end;
     393       20773 :         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     1111868 : xfs_getbmap(
     405             :         struct xfs_inode        *ip,
     406             :         struct getbmapx         *bmv,           /* user bmap structure */
     407             :         struct kgetbmap         *out)
     408             : {
     409     1111868 :         struct xfs_mount        *mp = ip->i_mount;
     410     1111868 :         int                     iflags = bmv->bmv_iflags;
     411     1111868 :         int                     whichfork, lock, error = 0;
     412     1111868 :         int64_t                 bmv_end, max_len;
     413     1111868 :         xfs_fileoff_t           bno, first_bno;
     414     1111868 :         struct xfs_ifork        *ifp;
     415     1111868 :         struct xfs_bmbt_irec    got, rec;
     416     1111868 :         xfs_filblks_t           len;
     417     1111868 :         struct xfs_iext_cursor  icur;
     418             : 
     419     1111868 :         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     1111868 :         if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
     427             :                 return -EINVAL;
     428             : 
     429     1111868 :         if (bmv->bmv_length < -1)
     430             :                 return -EINVAL;
     431     1111868 :         bmv->bmv_entries = 0;
     432     1111868 :         if (bmv->bmv_length == 0)
     433             :                 return 0;
     434             : 
     435      958462 :         if (iflags & BMV_IF_ATTRFORK)
     436             :                 whichfork = XFS_ATTR_FORK;
     437      958457 :         else if (iflags & BMV_IF_COWFORK)
     438             :                 whichfork = XFS_COW_FORK;
     439             :         else
     440      958180 :                 whichfork = XFS_DATA_FORK;
     441             : 
     442      958462 :         xfs_ilock(ip, XFS_IOLOCK_SHARED);
     443      958340 :         switch (whichfork) {
     444           2 :         case XFS_ATTR_FORK:
     445           2 :                 lock = xfs_ilock_attr_map_shared(ip);
     446           2 :                 if (!xfs_inode_has_attr_fork(ip))
     447           0 :                         goto out_unlock_ilock;
     448             : 
     449             :                 max_len = 1LL << 32;
     450             :                 break;
     451          54 :         case XFS_COW_FORK:
     452          54 :                 lock = XFS_ILOCK_SHARED;
     453          54 :                 xfs_ilock(ip, lock);
     454             : 
     455             :                 /* No CoW fork? Just return */
     456          54 :                 if (!xfs_ifork_ptr(ip, whichfork))
     457          14 :                         goto out_unlock_ilock;
     458             : 
     459          40 :                 if (xfs_get_cowextsz_hint(ip))
     460          40 :                         max_len = mp->m_super->s_maxbytes;
     461             :                 else
     462           0 :                         max_len = XFS_ISIZE(ip);
     463             :                 break;
     464      958284 :         case XFS_DATA_FORK:
     465      958284 :                 if (!(iflags & BMV_IF_DELALLOC) &&
     466       88966 :                     (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
     467        4348 :                         error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
     468        4352 :                         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      958288 :                 if (xfs_get_extsz_hint(ip) ||
     482      958125 :                     (ip->i_diflags &
     483             :                      (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
     484      306705 :                         max_len = mp->m_super->s_maxbytes;
     485             :                 else
     486      651468 :                         max_len = XFS_ISIZE(ip);
     487             : 
     488      958173 :                 lock = xfs_ilock_data_map_shared(ip);
     489      958181 :                 break;
     490             :         }
     491             : 
     492      958223 :         ifp = xfs_ifork_ptr(ip, whichfork);
     493             : 
     494      958225 :         switch (ifp->if_format) {
     495             :         case XFS_DINODE_FMT_EXTENTS:
     496             :         case XFS_DINODE_FMT_BTREE:
     497      680080 :                 break;
     498      278145 :         case XFS_DINODE_FMT_LOCAL:
     499             :                 /* Local format inode forks report no extents. */
     500      278145 :                 goto out_unlock_ilock;
     501           0 :         default:
     502           0 :                 error = -EINVAL;
     503           0 :                 goto out_unlock_ilock;
     504             :         }
     505             : 
     506      680080 :         if (bmv->bmv_length == -1) {
     507      675228 :                 max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
     508      675228 :                 bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
     509             :         }
     510             : 
     511      680080 :         bmv_end = bmv->bmv_offset + bmv->bmv_length;
     512             : 
     513      680080 :         first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
     514      680080 :         len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
     515             : 
     516      680080 :         error = xfs_iread_extents(NULL, ip, whichfork);
     517      680076 :         if (error)
     518           0 :                 goto out_unlock_ilock;
     519             : 
     520      680076 :         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      105269 :                 if (iflags & BMV_IF_DELALLOC)
     526      104745 :                         xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
     527      104745 :                                         XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
     528      105269 :                 goto out_unlock_ilock;
     529             :         }
     530             : 
     531     8390925 :         while (!xfs_getbmap_full(bmv)) {
     532     4195447 :                 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     4195602 :                 if (got.br_startoff > bno) {
     539     3323750 :                         xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
     540             :                                         got.br_startoff);
     541     6647500 :                         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     4195569 :                 bno = got.br_startoff + got.br_blockcount;
     551     4195569 :                 rec = got;
     552     4216342 :                 do {
     553     4216342 :                         error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
     554             :                                         &rec);
     555     8432122 :                         if (error || xfs_getbmap_full(bmv))
     556      202816 :                                 goto out_unlock_ilock;
     557     4013242 :                 } while (xfs_getbmap_next_rec(&rec, bno));
     558             : 
     559     3992469 :                 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
     560      371901 :                         xfs_fileoff_t   end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
     561             : 
     562      371901 :                         if (bmv->bmv_entries > 0)
     563      371901 :                                 out[bmv->bmv_entries - 1].bmv_oflags |=
     564             :                                                                 BMV_OF_LAST;
     565             : 
     566      565328 :                         if (whichfork != XFS_ATTR_FORK && bno < end &&
     567             :                             !xfs_getbmap_full(bmv)) {
     568      193427 :                                 xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
     569             :                                                 bno, end);
     570             :                         }
     571             :                         break;
     572             :                 }
     573             : 
     574     3620568 :                 if (bno >= first_bno + len)
     575             :                         break;
     576             :         }
     577             : 
     578          33 : out_unlock_ilock:
     579      958178 :         xfs_iunlock(ip, lock);
     580      958269 : out_unlock_iolock:
     581      958269 :         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
     582      958269 :         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        6464 : xfs_bmap_punch_delalloc_range(
     593             :         struct xfs_inode        *ip,
     594             :         xfs_off_t               start_byte,
     595             :         xfs_off_t               end_byte)
     596             : {
     597        6464 :         struct xfs_mount        *mp = ip->i_mount;
     598        6464 :         struct xfs_ifork        *ifp = &ip->i_df;
     599        6464 :         xfs_fileoff_t           start_fsb = XFS_B_TO_FSBT(mp, start_byte);
     600        6464 :         xfs_fileoff_t           end_fsb = XFS_B_TO_FSB(mp, end_byte);
     601        6464 :         struct xfs_bmbt_irec    got, del;
     602        6464 :         struct xfs_iext_cursor  icur;
     603        6464 :         int                     error = 0;
     604             : 
     605        6464 :         ASSERT(!xfs_need_iread_extents(ifp));
     606             : 
     607        6464 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     608        6464 :         if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
     609           0 :                 goto out_unlock;
     610             : 
     611       12357 :         while (got.br_startoff + got.br_blockcount > start_fsb) {
     612        6806 :                 del = got;
     613        6806 :                 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        6806 :                 if (!del.br_blockcount ||
     621        6519 :                     !isnullstartblock(del.br_startblock)) {
     622        2031 :                         if (!xfs_iext_prev_extent(ifp, &icur, &got))
     623             :                                 break;
     624        1616 :                         continue;
     625             :                 }
     626             : 
     627        4775 :                 error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
     628             :                                                   &got, &del);
     629        4775 :                 if (error || !xfs_iext_get_extent(ifp, &icur, &got))
     630             :                         break;
     631             :         }
     632             : 
     633        6464 : out_unlock:
     634        6464 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     635        6464 :         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   993413047 : xfs_can_free_eofblocks(
     645             :         struct xfs_inode        *ip,
     646             :         bool                    force)
     647             : {
     648   993413047 :         struct xfs_bmbt_irec    imap;
     649   993413047 :         struct xfs_mount        *mp = ip->i_mount;
     650   993413047 :         xfs_fileoff_t           end_fsb;
     651   993413047 :         xfs_fileoff_t           last_fsb;
     652   993413047 :         int                     nimaps = 1;
     653   993413047 :         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   993413047 :         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   993415527 :         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   492221402 :         if (VFS_I(ip)->i_size == 0 &&
     671   124420788 :             VFS_I(ip)->i_mapping->nrpages == 0 &&
     672   124423180 :             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   367797934 :         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   355339865 :         if (xfs_get_extsz_hint(ip) ||
     685   275903432 :             (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) {
     686   209758659 :                 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   145687304 :         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
     696   145687304 :         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   145687304 :         last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
     699   145687304 :         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   145689196 :         xfs_ilock(ip, XFS_ILOCK_SHARED);
     707   145688751 :         error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
     708             :                         0);
     709   145685054 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
     710   145687974 :         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   270517576 :         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     5762581 : xfs_free_eofblocks(
     727             :         struct xfs_inode        *ip)
     728             : {
     729     5762581 :         struct xfs_trans        *tp;
     730     5762581 :         struct xfs_mount        *mp = ip->i_mount;
     731     5762581 :         int                     error;
     732             : 
     733             :         /* Attach the dquots to the inode up front. */
     734     5762581 :         error = xfs_qm_dqattach(ip);
     735     5762640 :         if (error)
     736             :                 return error;
     737             : 
     738             :         /* Wait on dio to ensure i_size has settled. */
     739     5762710 :         inode_dio_wait(VFS_I(ip));
     740             : 
     741     5760211 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
     742     5763630 :         if (error) {
     743           0 :                 ASSERT(xfs_is_shutdown(mp));
     744           0 :                 return error;
     745             :         }
     746             : 
     747     5763630 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     748     5763823 :         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    11526502 :         error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
     757             :                                 XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
     758     5763136 :         if (error)
     759          36 :                 goto err_cancel;
     760             : 
     761     5763100 :         error = xfs_trans_commit(tp);
     762     5762744 :         if (error)
     763           0 :                 goto out_unlock;
     764             : 
     765     5762744 :         xfs_inode_clear_eofblocks_tag(ip);
     766     5763733 :         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          36 :         xfs_trans_cancel(tp);
     774     5763769 : out_unlock:
     775     5763769 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     776     5763769 :         return error;
     777             : }
     778             : 
     779             : int
     780     5046499 : xfs_alloc_file_space(
     781             :         struct xfs_inode        *ip,
     782             :         xfs_off_t               offset,
     783             :         xfs_off_t               len)
     784             : {
     785     5046499 :         xfs_mount_t             *mp = ip->i_mount;
     786     5046499 :         xfs_off_t               count;
     787     5046499 :         xfs_filblks_t           allocated_fsb;
     788     5046499 :         xfs_filblks_t           allocatesize_fsb;
     789     5046499 :         xfs_extlen_t            extsz, temp;
     790     5046499 :         xfs_fileoff_t           startoffset_fsb;
     791     5046499 :         xfs_fileoff_t           endoffset_fsb;
     792     5046499 :         int                     nimaps;
     793     5046499 :         int                     rt;
     794     5046499 :         xfs_trans_t             *tp;
     795     5046499 :         xfs_bmbt_irec_t         imaps[1], *imapp;
     796     5046499 :         int                     error;
     797             : 
     798     5046499 :         trace_xfs_alloc_file_space(ip);
     799             : 
     800    10093004 :         if (xfs_is_shutdown(mp))
     801             :                 return -EIO;
     802             : 
     803     5046499 :         error = xfs_qm_dqattach(ip);
     804     5046490 :         if (error)
     805             :                 return error;
     806             : 
     807     5046490 :         if (len <= 0)
     808             :                 return -EINVAL;
     809             : 
     810     5046490 :         rt = XFS_IS_REALTIME_INODE(ip);
     811     5046490 :         extsz = xfs_get_extsz_hint(ip);
     812             : 
     813     5046488 :         count = len;
     814     5046488 :         imapp = &imaps[0];
     815     5046488 :         nimaps = 1;
     816     5046488 :         startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
     817     5046488 :         endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
     818     5046488 :         allocatesize_fsb = endoffset_fsb - startoffset_fsb;
     819             : 
     820             :         /*
     821             :          * Allocate file space until done or until there is an error
     822             :          */
     823    13265010 :         while (allocatesize_fsb && !error) {
     824     8293300 :                 xfs_fileoff_t   s, e;
     825     8293300 :                 unsigned int    dblocks, rblocks, resblks;
     826             : 
     827             :                 /*
     828             :                  * Determine space reservations for data/realtime.
     829             :                  */
     830     8293300 :                 if (unlikely(extsz)) {
     831     2757547 :                         s = startoffset_fsb;
     832     2757547 :                         do_div(s, extsz);
     833     2757547 :                         s *= extsz;
     834     2757547 :                         e = startoffset_fsb + allocatesize_fsb;
     835     2757547 :                         div_u64_rem(startoffset_fsb, extsz, &temp);
     836     2757547 :                         if (temp)
     837          16 :                                 e += temp;
     838     2757547 :                         div_u64_rem(e, extsz, &temp);
     839     2757547 :                         if (temp)
     840          24 :                                 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     8293300 :                 resblks = min_t(xfs_fileoff_t, (e - s),
     855             :                                 (XFS_MAX_BMBT_EXTLEN * nimaps));
     856     8293300 :                 if (unlikely(rt)) {
     857     2757142 :                         dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
     858     2757142 :                         rblocks = resblks;
     859             :                 } else {
     860     5536158 :                         dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
     861     5536158 :                         rblocks = 0;
     862             :                 }
     863             : 
     864     8293300 :                 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
     865             :                                 dblocks, rblocks, false, &tp);
     866     8293306 :                 if (error)
     867             :                         break;
     868             : 
     869     8218749 :                 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     870             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     871     8218751 :                 if (error == -EFBIG)
     872           4 :                         error = xfs_iext_count_upgrade(tp, ip,
     873             :                                         XFS_IEXT_ADD_NOSPLIT_CNT);
     874     8218751 :                 if (error)
     875           4 :                         goto error;
     876             : 
     877     8218747 :                 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
     878             :                                 allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
     879             :                                 &nimaps);
     880     8218722 :                 if (error)
     881         132 :                         goto error;
     882             : 
     883     8218590 :                 ip->i_diflags |= XFS_DIFLAG_PREALLOC;
     884     8218590 :                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     885             : 
     886     8218592 :                 error = xfs_trans_commit(tp);
     887     8218625 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     888     8218624 :                 if (error)
     889             :                         break;
     890             : 
     891     8218530 :                 allocated_fsb = imapp->br_blockcount;
     892             : 
     893     8218530 :                 if (nimaps == 0) {
     894             :                         error = -ENOSPC;
     895             :                         break;
     896             :                 }
     897             : 
     898     8218522 :                 startoffset_fsb += allocated_fsb;
     899     8218522 :                 allocatesize_fsb -= allocated_fsb;
     900             :         }
     901             : 
     902             :         return error;
     903             : 
     904         136 : error:
     905         136 :         xfs_trans_cancel(tp);
     906         136 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     907         136 :         return error;
     908             : }
     909             : 
     910             : static int
     911    10299132 : 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    10299132 :         struct xfs_mount        *mp = ip->i_mount;
     918    10299132 :         struct xfs_trans        *tp;
     919    10299132 :         uint                    resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
     920    10299132 :         int                     error;
     921             : 
     922    10299132 :         error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
     923             :                         false, &tp);
     924    10299146 :         if (error)
     925             :                 return error;
     926             : 
     927    10284817 :         error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     928             :                         XFS_IEXT_PUNCH_HOLE_CNT);
     929    10284801 :         if (error == -EFBIG)
     930          24 :                 error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
     931    10284801 :         if (error)
     932          24 :                 goto out_trans_cancel;
     933             : 
     934    10284777 :         error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
     935    10284777 :         if (error)
     936           2 :                 goto out_trans_cancel;
     937             : 
     938    10284775 :         error = xfs_trans_commit(tp);
     939    10284814 : out_unlock:
     940    10284814 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     941    10284814 :         return error;
     942             : 
     943          26 : out_trans_cancel:
     944          26 :         xfs_trans_cancel(tp);
     945          26 :         goto out_unlock;
     946             : }
     947             : 
     948             : /* Caller must first wait for the completion of any pending DIOs if required. */
     949             : int
     950   117077659 : xfs_flush_unmap_range(
     951             :         struct xfs_inode        *ip,
     952             :         xfs_off_t               offset,
     953             :         xfs_off_t               len)
     954             : {
     955   117077659 :         struct xfs_mount        *mp = ip->i_mount;
     956   117077659 :         struct inode            *inode = VFS_I(ip);
     957   117077659 :         xfs_off_t               rounding, start, end;
     958   117077659 :         int                     error;
     959             : 
     960   117077659 :         rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
     961   117077659 :         start = round_down(offset, rounding);
     962   117077659 :         end = round_up(offset + len, rounding) - 1;
     963             : 
     964   117077659 :         error = filemap_write_and_wait_range(inode->i_mapping, start, end);
     965   117080148 :         if (error)
     966             :                 return error;
     967   117079387 :         truncate_pagecache_range(inode, start, end);
     968   117079387 :         return 0;
     969             : }
     970             : 
     971             : int
     972    10020182 : xfs_free_file_space(
     973             :         struct xfs_inode        *ip,
     974             :         xfs_off_t               offset,
     975             :         xfs_off_t               len)
     976             : {
     977    10020182 :         struct xfs_mount        *mp = ip->i_mount;
     978    10020182 :         xfs_fileoff_t           startoffset_fsb;
     979    10020182 :         xfs_fileoff_t           endoffset_fsb;
     980    10020182 :         int                     done = 0, error;
     981             : 
     982    10020182 :         trace_xfs_free_file_space(ip);
     983             : 
     984    10020193 :         error = xfs_qm_dqattach(ip);
     985    10020175 :         if (error)
     986             :                 return error;
     987             : 
     988    10020175 :         if (len <= 0)        /* if nothing being freed */
     989             :                 return 0;
     990             : 
     991    10020175 :         startoffset_fsb = XFS_B_TO_FSB(mp, offset);
     992    10020175 :         endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
     993             : 
     994             :         /* We can only free complete realtime extents. */
     995    10020175 :         if (xfs_inode_has_bigrtextents(ip)) {
     996          28 :                 startoffset_fsb = roundup_64(startoffset_fsb,
     997             :                                              mp->m_sb.sb_rextsize);
     998          28 :                 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    10020175 :         if (endoffset_fsb > startoffset_fsb) {
    1006    20147372 :                 while (!done) {
    1007    10299126 :                         error = xfs_unmap_extent(ip, startoffset_fsb,
    1008             :                                         endoffset_fsb - startoffset_fsb, &done);
    1009    10299149 :                         if (error)
    1010       14373 :                                 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    20011650 :         if (offset >= XFS_ISIZE(ip))
    1021             :                 return 0;
    1022     8957248 :         if (offset + len > XFS_ISIZE(ip))
    1023      245267 :                 len = XFS_ISIZE(ip) - offset;
    1024     8957248 :         error = xfs_zero_range(ip, offset, len, NULL);
    1025     8957242 :         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    17914292 :         if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
    1035      325958 :                 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      836686 : xfs_prepare_shift(
    1044             :         struct xfs_inode        *ip,
    1045             :         loff_t                  offset)
    1046             : {
    1047      836686 :         struct xfs_mount        *mp = ip->i_mount;
    1048      836686 :         int                     error;
    1049             : 
    1050             :         /*
    1051             :          * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
    1052             :          * into the accessible region of the file.
    1053             :          */
    1054      836686 :         if (xfs_can_free_eofblocks(ip, true)) {
    1055      120123 :                 error = xfs_free_eofblocks(ip);
    1056      120123 :                 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      836683 :         offset = round_down(offset, mp->m_sb.sb_blocksize);
    1068      836683 :         if (offset)
    1069      768466 :                 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     1673366 :         error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
    1076      836683 :         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     1673206 :         if (xfs_inode_has_cow_data(ip)) {
    1085       38423 :                 error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
    1086             :                                 true);
    1087       38423 :                 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      458553 : xfs_collapse_file_space(
    1108             :         struct xfs_inode        *ip,
    1109             :         xfs_off_t               offset,
    1110             :         xfs_off_t               len)
    1111             : {
    1112      458553 :         struct xfs_mount        *mp = ip->i_mount;
    1113      458553 :         struct xfs_trans        *tp;
    1114      458553 :         int                     error;
    1115      458553 :         xfs_fileoff_t           next_fsb = XFS_B_TO_FSB(mp, offset + len);
    1116      458553 :         xfs_fileoff_t           shift_fsb = XFS_B_TO_FSB(mp, len);
    1117      458553 :         bool                    done = false;
    1118             : 
    1119      458553 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    1120      458553 :         ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
    1121             : 
    1122      458553 :         trace_xfs_collapse_file_space(ip);
    1123             : 
    1124      458553 :         error = xfs_free_file_space(ip, offset, len);
    1125      458552 :         if (error)
    1126             :                 return error;
    1127             : 
    1128      457501 :         error = xfs_prepare_shift(ip, offset);
    1129      457502 :         if (error)
    1130             :                 return error;
    1131             : 
    1132      457450 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
    1133      457450 :         if (error)
    1134             :                 return error;
    1135             : 
    1136      457450 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1137      457450 :         xfs_trans_ijoin(tp, ip, 0);
    1138             : 
    1139    18963645 :         while (!done) {
    1140    18963645 :                 error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
    1141             :                                 &done);
    1142    18963645 :                 if (error)
    1143           2 :                         goto out_trans_cancel;
    1144    18963643 :                 if (done)
    1145             :                         break;
    1146             : 
    1147             :                 /* finish any deferred frees and roll the transaction */
    1148    18506196 :                 error = xfs_defer_finish(&tp);
    1149    18506196 :                 if (error)
    1150           1 :                         goto out_trans_cancel;
    1151             :         }
    1152             : 
    1153      457447 :         error = xfs_trans_commit(tp);
    1154      457447 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1155      457447 :         return error;
    1156             : 
    1157           3 : out_trans_cancel:
    1158           3 :         xfs_trans_cancel(tp);
    1159           3 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1160           3 :         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      379186 : xfs_insert_file_space(
    1177             :         struct xfs_inode        *ip,
    1178             :         loff_t                  offset,
    1179             :         loff_t                  len)
    1180             : {
    1181      379186 :         struct xfs_mount        *mp = ip->i_mount;
    1182      379186 :         struct xfs_trans        *tp;
    1183      379186 :         int                     error;
    1184      379186 :         xfs_fileoff_t           stop_fsb = XFS_B_TO_FSB(mp, offset);
    1185      379186 :         xfs_fileoff_t           next_fsb = NULLFSBLOCK;
    1186      379186 :         xfs_fileoff_t           shift_fsb = XFS_B_TO_FSB(mp, len);
    1187      379186 :         bool                    done = false;
    1188             : 
    1189      379186 :         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
    1190      379186 :         ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
    1191             : 
    1192      379186 :         trace_xfs_insert_file_space(ip);
    1193             : 
    1194      379186 :         error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
    1195      379186 :         if (error)
    1196             :                 return error;
    1197             : 
    1198      379184 :         error = xfs_prepare_shift(ip, offset);
    1199      379184 :         if (error)
    1200             :                 return error;
    1201             : 
    1202      379153 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
    1203      379153 :                         XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
    1204      379153 :         if (error)
    1205             :                 return error;
    1206             : 
    1207      378036 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1208      378036 :         xfs_trans_ijoin(tp, ip, 0);
    1209             : 
    1210      378036 :         error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
    1211             :                         XFS_IEXT_PUNCH_HOLE_CNT);
    1212      378035 :         if (error == -EFBIG)
    1213           2 :                 error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
    1214      378035 :         if (error)
    1215           2 :                 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      378033 :         error = xfs_bmap_split_extent(tp, ip, stop_fsb);
    1223      378033 :         if (error)
    1224           0 :                 goto out_trans_cancel;
    1225             : 
    1226     1568460 :         do {
    1227     1568460 :                 error = xfs_defer_finish(&tp);
    1228     1568460 :                 if (error)
    1229           0 :                         goto out_trans_cancel;
    1230             : 
    1231     1568460 :                 error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
    1232             :                                 &done, stop_fsb);
    1233     1568461 :                 if (error)
    1234           0 :                         goto out_trans_cancel;
    1235     1568461 :         } while (!done);
    1236             : 
    1237      378034 :         error = xfs_trans_commit(tp);
    1238      378034 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1239      378034 :         return error;
    1240             : 
    1241           2 : out_trans_cancel:
    1242           2 :         xfs_trans_cancel(tp);
    1243           2 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1244           2 :         return error;
    1245             : }

Generated by: LCOV version 1.14