LCOV - code coverage report
Current view: top level - fs/xfs - xfs_rtalloc.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 919 1035 88.8 %
Date: 2023-07-31 20:08:34 Functions: 35 35 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_mount.h"
      14             : #include "xfs_inode.h"
      15             : #include "xfs_bmap.h"
      16             : #include "xfs_bmap_btree.h"
      17             : #include "xfs_trans.h"
      18             : #include "xfs_trans_space.h"
      19             : #include "xfs_icache.h"
      20             : #include "xfs_rtalloc.h"
      21             : #include "xfs_sb.h"
      22             : #include "xfs_log_priv.h"
      23             : #include "xfs_health.h"
      24             : #include "xfs_trace.h"
      25             : #include "xfs_da_format.h"
      26             : #include "xfs_imeta.h"
      27             : #include "xfs_imeta_utils.h"
      28             : #include "xfs_rtbitmap.h"
      29             : #include "xfs_rtgroup.h"
      30             : #include "xfs_error.h"
      31             : #include "xfs_btree.h"
      32             : #include "xfs_rmap.h"
      33             : #include "xfs_rtrmap_btree.h"
      34             : #include "xfs_rtrefcount_btree.h"
      35             : #include "xfs_quota.h"
      36             : 
      37             : /*
      38             :  * Realtime metadata files are not quite regular files because userspace can't
      39             :  * access the realtime bitmap directly, and because we take the ILOCK of the rt
      40             :  * bitmap file while holding the ILOCK of a regular realtime file.  This double
      41             :  * locking confuses lockdep, so create different lockdep classes here to help
      42             :  * it keep things straight.
      43             :  */
      44             : static struct lock_class_key xfs_rbmip_key;
      45             : static struct lock_class_key xfs_rsumip_key;
      46             : static struct lock_class_key xfs_rrmapip_key;
      47             : static struct lock_class_key xfs_rrefcountip_key;
      48             : 
      49             : /*
      50             :  * Read and return the summary information for a given extent size,
      51             :  * bitmap block combination.
      52             :  * Keeps track of a current summary block, so we don't keep reading
      53             :  * it from the buffer cache.
      54             :  */
      55             : static int
      56             : xfs_rtget_summary(
      57             :         xfs_mount_t     *mp,            /* file system mount structure */
      58             :         xfs_trans_t     *tp,            /* transaction pointer */
      59             :         int             log,            /* log2 of extent size */
      60             :         xfs_fileoff_t   bbno,           /* bitmap block number */
      61             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
      62             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
      63             :         xfs_suminfo_t   *sum)           /* out: summary info for this block */
      64             : {
      65  2835430226 :         return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
      66             : }
      67             : 
      68             : /*
      69             :  * Return whether there are any free extents in the size range given
      70             :  * by low and high, for the bitmap block bbno.
      71             :  */
      72             : STATIC int                              /* error */
      73   398747091 : xfs_rtany_summary(
      74             :         xfs_mount_t     *mp,            /* file system mount structure */
      75             :         xfs_trans_t     *tp,            /* transaction pointer */
      76             :         int             low,            /* low log2 extent size */
      77             :         int             high,           /* high log2 extent size */
      78             :         xfs_fileoff_t   bbno,           /* bitmap block number */
      79             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
      80             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
      81             :         int             *stat)          /* out: any good extents here? */
      82             : {
      83   398747091 :         int             error;          /* error value */
      84   398747091 :         int             log;            /* loop counter, log2 of ext. size */
      85   398747091 :         xfs_suminfo_t   sum;            /* summary data */
      86             : 
      87             :         /* There are no extents at levels < m_rsum_cache[bbno]. */
      88   398747091 :         if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
      89             :                 low = mp->m_rsum_cache[bbno];
      90             : 
      91             :         /*
      92             :          * Loop over logs of extent sizes.
      93             :          */
      94   596000113 :         for (log = low; log <= high; log++) {
      95             :                 /*
      96             :                  * Get one summary datum.
      97             :                  */
      98   243088215 :                 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
      99   243088215 :                 if (error) {
     100           1 :                         return error;
     101             :                 }
     102             :                 /*
     103             :                  * If there are any, return success.
     104             :                  */
     105   243088214 :                 if (sum) {
     106    45835192 :                         *stat = 1;
     107    45835192 :                         goto out;
     108             :                 }
     109             :         }
     110             :         /*
     111             :          * Found nothing, return failure.
     112             :          */
     113   352911898 :         *stat = 0;
     114   398747090 : out:
     115             :         /* There were no extents at levels < log. */
     116   398747090 :         if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
     117     9821887 :                 mp->m_rsum_cache[bbno] = log;
     118             :         return 0;
     119             : }
     120             : 
     121             : 
     122             : /*
     123             :  * Copy and transform the summary file, given the old and new
     124             :  * parameters in the mount structures.
     125             :  */
     126             : STATIC int                              /* error */
     127        1492 : xfs_rtcopy_summary(
     128             :         xfs_mount_t     *omp,           /* old file system mount point */
     129             :         xfs_mount_t     *nmp,           /* new file system mount point */
     130             :         xfs_trans_t     *tp)            /* transaction pointer */
     131             : {
     132        1492 :         xfs_fileoff_t   bbno;           /* bitmap block number */
     133        1492 :         struct xfs_buf  *bp;            /* summary buffer */
     134        1492 :         int             error;          /* error return value */
     135        1492 :         int             log;            /* summary level number (log length) */
     136        1492 :         xfs_suminfo_t   sum;            /* summary data */
     137        1492 :         xfs_fileoff_t   sumbno;         /* summary block number */
     138             : 
     139        1492 :         bp = NULL;
     140       33566 :         for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
     141       32074 :                 for (bbno = omp->m_sb.sb_rbmblocks - 1;
     142     3800239 :                      (xfs_srtblock_t)bbno >= 0;
     143     3768165 :                      bbno--) {
     144     3768165 :                         error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
     145             :                                 &sumbno, &sum);
     146     3768165 :                         if (error)
     147           0 :                                 return error;
     148     3768165 :                         if (sum == 0)
     149     3682642 :                                 continue;
     150       85523 :                         error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
     151             :                                 &bp, &sumbno);
     152       85523 :                         if (error)
     153           0 :                                 return error;
     154       85523 :                         error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
     155             :                                 &bp, &sumbno);
     156       85523 :                         if (error)
     157           0 :                                 return error;
     158       85523 :                         ASSERT(sum > 0);
     159             :                 }
     160             :         }
     161             :         return 0;
     162             : }
     163             : /*
     164             :  * Mark an extent specified by start and len allocated.
     165             :  * Updates all the summary information as well as the bitmap.
     166             :  */
     167             : STATIC int                              /* error */
     168    86773138 : xfs_rtallocate_range(
     169             :         xfs_mount_t     *mp,            /* file system mount point */
     170             :         xfs_trans_t     *tp,            /* transaction pointer */
     171             :         xfs_rtxnum_t    start,          /* start rtext to allocate */
     172             :         xfs_rtxlen_t    len,            /* length to allocate */
     173             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     174             :         xfs_fileoff_t   *rsb)           /* in/out: summary block number */
     175             : {
     176    86773138 :         xfs_rtxnum_t    end;            /* end of the allocated rtext */
     177    86773138 :         int             error;          /* error value */
     178    86773138 :         xfs_rtxnum_t    postblock = 0;  /* first rtext allocated > end */
     179    86773138 :         xfs_rtxnum_t    preblock = 0;   /* first rtext allocated < start */
     180             : 
     181    86773138 :         end = start + len - 1;
     182             :         /*
     183             :          * Assume we're allocating out of the middle of a free extent.
     184             :          * We need to find the beginning and end of the extent so we can
     185             :          * properly update the summary.
     186             :          */
     187    86773138 :         error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
     188    86773138 :         if (error) {
     189             :                 return error;
     190             :         }
     191             :         /*
     192             :          * Find the next allocated block (end of free extent).
     193             :          */
     194    86773138 :         error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
     195             :                 &postblock);
     196    86773138 :         if (error) {
     197             :                 return error;
     198             :         }
     199             :         /*
     200             :          * Decrement the summary information corresponding to the entire
     201             :          * (old) free extent.
     202             :          */
     203    86773136 :         error = xfs_rtmodify_summary(mp, tp,
     204    86773136 :                 XFS_RTBLOCKLOG(postblock + 1 - preblock),
     205             :                 xfs_rtx_to_rbmblock(mp, preblock), -1, rbpp, rsb);
     206    86773136 :         if (error) {
     207             :                 return error;
     208             :         }
     209             :         /*
     210             :          * If there are blocks not being allocated at the front of the
     211             :          * old extent, add summary data for them to be free.
     212             :          */
     213    86773135 :         if (preblock < start) {
     214     6677631 :                 error = xfs_rtmodify_summary(mp, tp,
     215             :                         XFS_RTBLOCKLOG(start - preblock),
     216             :                         xfs_rtx_to_rbmblock(mp, preblock), 1, rbpp, rsb);
     217     6677631 :                 if (error) {
     218             :                         return error;
     219             :                 }
     220             :         }
     221             :         /*
     222             :          * If there are blocks not being allocated at the end of the
     223             :          * old extent, add summary data for them to be free.
     224             :          */
     225    86773135 :         if (postblock > end) {
     226    59138513 :                 error = xfs_rtmodify_summary(mp, tp,
     227             :                         XFS_RTBLOCKLOG(postblock - end),
     228             :                         xfs_rtx_to_rbmblock(mp, end + 1), 1, rbpp, rsb);
     229    59138513 :                 if (error) {
     230             :                         return error;
     231             :                 }
     232             :         }
     233             :         /*
     234             :          * Modify the bitmap to mark this extent allocated.
     235             :          */
     236    86773135 :         error = xfs_rtmodify_range(mp, tp, start, len, 0);
     237    86773135 :         return error;
     238             : }
     239             : 
     240             : /*
     241             :  * Attempt to allocate an extent minlen<=len<=maxlen starting from
     242             :  * bitmap block bbno.  If we don't get maxlen then use prod to trim
     243             :  * the length, if given.  Returns error; returns starting block in *rtx.
     244             :  * The lengths are all in rtextents.
     245             :  */
     246             : STATIC int                              /* error */
     247   140449902 : xfs_rtallocate_extent_block(
     248             :         xfs_mount_t     *mp,            /* file system mount point */
     249             :         xfs_trans_t     *tp,            /* transaction pointer */
     250             :         xfs_fileoff_t   bbno,           /* bitmap block number */
     251             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     252             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     253             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     254             :         xfs_rtxnum_t    *nextp,         /* out: next rtext to try */
     255             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     256             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     257             :         xfs_rtxlen_t    prod,           /* extent product factor */
     258             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     259             : {
     260   140449902 :         xfs_rtxnum_t    besti;          /* best rtext found so far */
     261   140449902 :         xfs_rtxnum_t    bestlen;        /* best length found so far */
     262   140449902 :         xfs_rtxnum_t    end;            /* last rtext in chunk */
     263   140449902 :         int             error;          /* error value */
     264   140449902 :         xfs_rtxnum_t    i;              /* current rtext trying */
     265   140449902 :         xfs_rtxnum_t    next;           /* next rtext to try */
     266   140449902 :         int             stat;           /* status from internal calls */
     267             : 
     268             :         /*
     269             :          * Loop over all the extents starting in this bitmap block,
     270             :          * looking for one that's long enough.
     271             :          */
     272   140449902 :         for (i = xfs_rbmblock_to_rtx(mp, bbno), besti = -1, bestlen = 0,
     273   140449902 :                 end = xfs_rbmblock_to_rtx(mp, bbno + 1) - 1;
     274   547698058 :              i <= end;
     275   407248156 :              i++) {
     276             :                 /*
     277             :                  * Make sure we don't run off the end of the rt volume.  Be
     278             :                  * careful that adjusting maxlen downwards doesn't cause us to
     279             :                  * fail the alignment checks.
     280             :                  */
     281   441120926 :                 maxlen = min(mp->m_sb.sb_rextents, i + maxlen) - i;
     282   441120926 :                 maxlen -= maxlen % prod;
     283             : 
     284             :                 /*
     285             :                  * See if there's a free extent of maxlen starting at i.
     286             :                  * If it's not so then next will contain the first non-free.
     287             :                  */
     288   441120926 :                 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
     289   441120926 :                 if (error) {
     290           0 :                         return error;
     291             :                 }
     292   441120926 :                 if (stat) {
     293             :                         /*
     294             :                          * i for maxlen is all free, allocate and return that.
     295             :                          */
     296    33813109 :                         error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
     297             :                                 rsb);
     298    33813109 :                         if (error) {
     299             :                                 return error;
     300             :                         }
     301    33813108 :                         *len = maxlen;
     302    33813108 :                         *rtx = i;
     303    33813108 :                         return 0;
     304             :                 }
     305             :                 /*
     306             :                  * In the case where we have a variable-sized allocation
     307             :                  * request, figure out how big this free piece is,
     308             :                  * and if it's big enough for the minimum, and the best
     309             :                  * so far, remember it.
     310             :                  */
     311   407307817 :                 if (minlen < maxlen) {
     312   351901229 :                         xfs_rtxnum_t    thislen;        /* this extent size */
     313             : 
     314   351901229 :                         thislen = next - i;
     315   351901229 :                         if (thislen >= minlen && thislen > bestlen) {
     316    38495682 :                                 besti = i;
     317    38495682 :                                 bestlen = thislen;
     318             :                         }
     319             :                 }
     320             :                 /*
     321             :                  * If not done yet, find the start of the next free space.
     322             :                  */
     323   407307817 :                 if (next < end) {
     324   407248156 :                         error = xfs_rtfind_forw(mp, tp, next, end, &i);
     325   407248156 :                         if (error) {
     326           0 :                                 return error;
     327             :                         }
     328             :                 } else
     329             :                         break;
     330             :         }
     331             :         /*
     332             :          * Searched the whole thing & didn't find a maxlen free extent.
     333             :          */
     334   106636793 :         if (minlen < maxlen && besti != -1) {
     335    15404388 :                 xfs_rtxlen_t    p;      /* amount to trim length by */
     336             : 
     337             :                 /*
     338             :                  * If size should be a multiple of prod, make that so.
     339             :                  */
     340    15404388 :                 if (prod > 1) {
     341         230 :                         div_u64_rem(bestlen, prod, &p);
     342         230 :                         if (p)
     343         187 :                                 bestlen -= p;
     344             :                 }
     345             : 
     346             :                 /*
     347             :                  * Allocate besti for bestlen & return that.
     348             :                  */
     349    15404388 :                 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
     350    15404388 :                 if (error) {
     351             :                         return error;
     352             :                 }
     353    15404388 :                 *len = bestlen;
     354    15404388 :                 *rtx = besti;
     355    15404388 :                 return 0;
     356             :         }
     357             :         /*
     358             :          * Allocation failed.  Set *nextp to the next block to try.
     359             :          */
     360    91232405 :         *nextp = next;
     361    91232405 :         *rtx = NULLRTEXTNO;
     362    91232405 :         return 0;
     363             : }
     364             : 
     365             : /*
     366             :  * Allocate an extent of length minlen<=len<=maxlen, starting at block
     367             :  * bno.  If we don't get maxlen then use prod to trim the length, if given.
     368             :  * Returns error; returns starting block in *rtx.
     369             :  * The lengths are all in rtextents.
     370             :  */
     371             : STATIC int                              /* error */
     372    83390777 : xfs_rtallocate_extent_exact(
     373             :         xfs_mount_t     *mp,            /* file system mount point */
     374             :         xfs_trans_t     *tp,            /* transaction pointer */
     375             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
     376             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     377             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     378             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     379             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     380             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     381             :         xfs_rtxlen_t    prod,           /* extent product factor */
     382             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     383             : {
     384    83390777 :         int             error;          /* error value */
     385    83390777 :         xfs_rtxlen_t    i;              /* extent length trimmed due to prod */
     386    83390777 :         int             isfree;         /* extent is free */
     387    83390777 :         xfs_rtxnum_t    next;           /* next rtext to try (dummy) */
     388             : 
     389    83390777 :         ASSERT(minlen % prod == 0);
     390    83390777 :         ASSERT(maxlen % prod == 0);
     391             :         /*
     392             :          * Check if the range in question (for maxlen) is free.
     393             :          */
     394    83390777 :         error = xfs_rtcheck_range(mp, tp, start, maxlen, 1, &next, &isfree);
     395    83390777 :         if (error) {
     396             :                 return error;
     397             :         }
     398    83390772 :         if (isfree) {
     399             :                 /*
     400             :                  * If it is, allocate it and return success.
     401             :                  */
     402    36495663 :                 error = xfs_rtallocate_range(mp, tp, start, maxlen, rbpp, rsb);
     403    36495663 :                 if (error) {
     404             :                         return error;
     405             :                 }
     406    36495661 :                 *len = maxlen;
     407    36495661 :                 *rtx = start;
     408    36495661 :                 return 0;
     409             :         }
     410             :         /*
     411             :          * If not, allocate what there is, if it's at least minlen.
     412             :          */
     413    46895109 :         maxlen = next - start;
     414    46895109 :         if (maxlen < minlen) {
     415             :                 /*
     416             :                  * Failed, return failure status.
     417             :                  */
     418    45835131 :                 *rtx = NULLRTEXTNO;
     419    45835131 :                 return 0;
     420             :         }
     421             :         /*
     422             :          * Trim off tail of extent, if prod is specified.
     423             :          */
     424     1059978 :         if (prod > 1 && (i = maxlen % prod)) {
     425         166 :                 maxlen -= i;
     426         166 :                 if (maxlen < minlen) {
     427             :                         /*
     428             :                          * Now we can't do it, return failure status.
     429             :                          */
     430           0 :                         *rtx = NULLRTEXTNO;
     431           0 :                         return 0;
     432             :                 }
     433             :         }
     434             :         /*
     435             :          * Allocate what we can and return it.
     436             :          */
     437     1059978 :         error = xfs_rtallocate_range(mp, tp, start, maxlen, rbpp, rsb);
     438     1059978 :         if (error) {
     439             :                 return error;
     440             :         }
     441     1059978 :         *len = maxlen;
     442     1059978 :         *rtx = start;
     443     1059978 :         return 0;
     444             : }
     445             : 
     446             : /*
     447             :  * Allocate an extent of length minlen<=len<=maxlen, starting as near
     448             :  * to start as possible.  If we don't get maxlen then use prod to trim
     449             :  * the length, if given.  The lengths are all in rtextents.
     450             :  */
     451             : STATIC int                              /* error */
     452    83390777 : xfs_rtallocate_extent_near(
     453             :         xfs_mount_t     *mp,            /* file system mount point */
     454             :         xfs_trans_t     *tp,            /* transaction pointer */
     455             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
     456             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     457             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     458             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     459             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     460             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     461             :         xfs_rtxlen_t    prod,           /* extent product factor */
     462             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     463             : {
     464    83390777 :         int             any;            /* any useful extents from summary */
     465    83390777 :         xfs_fileoff_t   bbno;           /* bitmap block number */
     466    83390777 :         int             error;          /* error value */
     467    83390777 :         int             i;              /* bitmap block offset (loop control) */
     468    83390777 :         int             j;              /* secondary loop control */
     469    83390777 :         int             log2len;        /* log2 of minlen */
     470    83390777 :         xfs_rtxnum_t    n;              /* next rtext to try */
     471    83390777 :         xfs_rtxnum_t    r;              /* result rtext */
     472             : 
     473    83390777 :         ASSERT(minlen % prod == 0);
     474    83390777 :         ASSERT(maxlen % prod == 0);
     475             : 
     476             :         /*
     477             :          * If the block number given is off the end, silently set it to
     478             :          * the last block.
     479             :          */
     480    83390777 :         if (start >= mp->m_sb.sb_rextents)
     481          76 :                 start = mp->m_sb.sb_rextents - 1;
     482             : 
     483             :         /*
     484             :          * Make sure we don't run off the end of the rt volume.  Be careful
     485             :          * that adjusting maxlen downwards doesn't cause us to fail the
     486             :          * alignment checks.
     487             :          */
     488    83390777 :         maxlen = min(mp->m_sb.sb_rextents, start + maxlen) - start;
     489    83390777 :         maxlen -= maxlen % prod;
     490    83390777 :         if (maxlen < minlen) {
     491           0 :                 *rtx = NULLRTEXTNO;
     492           0 :                 return 0;
     493             :         }
     494             : 
     495             :         /*
     496             :          * Try the exact allocation first.
     497             :          */
     498    83390777 :         error = xfs_rtallocate_extent_exact(mp, tp, start, minlen, maxlen, len,
     499             :                 rbpp, rsb, prod, &r);
     500    83390777 :         if (error) {
     501             :                 return error;
     502             :         }
     503             :         /*
     504             :          * If the exact allocation worked, return that.
     505             :          */
     506    83390770 :         if (r != NULLRTEXTNO) {
     507    37555639 :                 *rtx = r;
     508    37555639 :                 return 0;
     509             :         }
     510    45835131 :         bbno = xfs_rtx_to_rbmblock(mp, start);
     511    45835131 :         i = 0;
     512    45835131 :         ASSERT(minlen != 0);
     513    45835131 :         log2len = xfs_highbit32(minlen);
     514             :         /*
     515             :          * Loop over all bitmap blocks (bbno + i is current block).
     516             :          */
     517   308091564 :         for (;;) {
     518             :                 /*
     519             :                  * Get summary information of extents of all useful levels
     520             :                  * starting in this bitmap block.
     521             :                  */
     522   308091564 :                 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
     523             :                         bbno + i, rbpp, rsb, &any);
     524   308091564 :                 if (error) {
     525           1 :                         return error;
     526             :                 }
     527             :                 /*
     528             :                  * If there are any useful extents starting here, try
     529             :                  * allocating one.
     530             :                  */
     531   308091563 :                 if (any) {
     532             :                         /*
     533             :                          * On the positive side of the starting location.
     534             :                          */
     535    45835133 :                         if (i >= 0) {
     536             :                                 /*
     537             :                                  * Try to allocate an extent starting in
     538             :                                  * this block.
     539             :                                  */
     540    36817415 :                                 error = xfs_rtallocate_extent_block(mp, tp,
     541             :                                         bbno + i, minlen, maxlen, len, &n, rbpp,
     542             :                                         rsb, prod, &r);
     543    36817415 :                                 if (error) {
     544           0 :                                         return error;
     545             :                                 }
     546             :                                 /*
     547             :                                  * If it worked, return it.
     548             :                                  */
     549    36817415 :                                 if (r != NULLRTEXTNO) {
     550    36817372 :                                         *rtx = r;
     551    36817372 :                                         return 0;
     552             :                                 }
     553             :                         }
     554             :                         /*
     555             :                          * On the negative side of the starting location.
     556             :                          */
     557             :                         else {          /* i < 0 */
     558             :                                 /*
     559             :                                  * Loop backwards through the bitmap blocks from
     560             :                                  * the starting point-1 up to where we are now.
     561             :                                  * There should be an extent which ends in this
     562             :                                  * bitmap block and is long enough.
     563             :                                  */
     564    99575454 :                                 for (j = -1; j > i; j--) {
     565             :                                         /*
     566             :                                          * Grab the summary information for
     567             :                                          * this bitmap block.
     568             :                                          */
     569    90655527 :                                         error = xfs_rtany_summary(mp, tp,
     570    90655527 :                                                 log2len, mp->m_rsumlevels - 1,
     571             :                                                 bbno + j, rbpp, rsb, &any);
     572    90655527 :                                         if (error) {
     573           0 :                                                 return error;
     574             :                                         }
     575             :                                         /*
     576             :                                          * If there's no extent given in the
     577             :                                          * summary that means the extent we
     578             :                                          * found must carry over from an
     579             :                                          * earlier block.  If there is an
     580             :                                          * extent given, we've already tried
     581             :                                          * that allocation, don't do it again.
     582             :                                          */
     583    90655527 :                                         if (any)
     584          59 :                                                 continue;
     585    90655468 :                                         error = xfs_rtallocate_extent_block(mp,
     586             :                                                 tp, bbno + j, minlen, maxlen,
     587             :                                                 len, &n, rbpp, rsb, prod, &r);
     588    90655468 :                                         if (error) {
     589           0 :                                                 return error;
     590             :                                         }
     591             :                                         /*
     592             :                                          * If it works, return the extent.
     593             :                                          */
     594    90655468 :                                         if (r != NULLRTEXTNO) {
     595       97791 :                                                 *rtx = r;
     596       97791 :                                                 return 0;
     597             :                                         }
     598             :                                 }
     599             :                                 /*
     600             :                                  * There weren't intervening bitmap blocks
     601             :                                  * with a long enough extent, or the
     602             :                                  * allocation didn't work for some reason
     603             :                                  * (i.e. it's a little * too short).
     604             :                                  * Try to allocate from the summary block
     605             :                                  * that we found.
     606             :                                  */
     607     8919927 :                                 error = xfs_rtallocate_extent_block(mp, tp,
     608             :                                         bbno + i, minlen, maxlen, len, &n, rbpp,
     609             :                                         rsb, prod, &r);
     610     8919927 :                                 if (error) {
     611           0 :                                         return error;
     612             :                                 }
     613             :                                 /*
     614             :                                  * If it works, return the extent.
     615             :                                  */
     616     8919927 :                                 if (r != NULLRTEXTNO) {
     617     8919881 :                                         *rtx = r;
     618     8919881 :                                         return 0;
     619             :                                 }
     620             :                         }
     621             :                 }
     622             :                 /*
     623             :                  * Loop control.  If we were on the positive side, and there's
     624             :                  * still more blocks on the negative side, go there.
     625             :                  */
     626   262256519 :                 if (i > 0 && (int)bbno - i >= 0)
     627   103044973 :                         i = -i;
     628             :                 /*
     629             :                  * If positive, and no more negative, but there are more
     630             :                  * positive, go there.
     631             :                  */
     632   159211546 :                 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
     633     6640339 :                         i++;
     634             :                 /*
     635             :                  * If negative or 0 (just started), and there are positive
     636             :                  * blocks to go, go there.  The 0 case moves to block 1.
     637             :                  */
     638   152571207 :                 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
     639   113302741 :                         i = 1 - i;
     640             :                 /*
     641             :                  * If negative or 0 and there are more negative blocks,
     642             :                  * go there.
     643             :                  */
     644    39268466 :                 else if (i <= 0 && (int)bbno + i > 0)
     645    39268380 :                         i--;
     646             :                 /*
     647             :                  * Must be done.  Return failure.
     648             :                  */
     649             :                 else
     650             :                         break;
     651             :         }
     652          86 :         *rtx = NULLRTEXTNO;
     653          86 :         return 0;
     654             : }
     655             : 
     656             : /*
     657             :  * Allocate an extent of length minlen<=len<=maxlen, with no position
     658             :  * specified.  If we don't get maxlen then use prod to trim
     659             :  * the length, if given.  The lengths are all in rtextents.
     660             :  */
     661             : STATIC int                              /* error */
     662     3383152 : xfs_rtallocate_extent_size(
     663             :         xfs_mount_t     *mp,            /* file system mount point */
     664             :         xfs_trans_t     *tp,            /* transaction pointer */
     665             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
     666             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
     667             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
     668             :         struct xfs_buf  **rbpp,         /* in/out: summary block buffer */
     669             :         xfs_fileoff_t   *rsb,           /* in/out: summary block number */
     670             :         xfs_rtxlen_t    prod,           /* extent product factor */
     671             :         xfs_rtxnum_t    *rtx)           /* out: start rtext allocated */
     672             : {
     673     3383152 :         int             error;          /* error value */
     674     3383152 :         xfs_fileoff_t   i;              /* bitmap block number */
     675     3383152 :         int             l;              /* level number (loop control) */
     676     3383152 :         xfs_rtxnum_t    n;              /* next rtext to be tried */
     677     3383152 :         xfs_rtxnum_t    r;              /* result rtext number */
     678     3383152 :         xfs_suminfo_t   sum;            /* summary information for extents */
     679             : 
     680     3383152 :         ASSERT(minlen % prod == 0);
     681     3383152 :         ASSERT(maxlen % prod == 0);
     682     3383152 :         ASSERT(maxlen != 0);
     683             : 
     684             :         /*
     685             :          * Loop over all the levels starting with maxlen.
     686             :          * At each level, look at all the bitmap blocks, to see if there
     687             :          * are extents starting there that are long enough (>= maxlen).
     688             :          * Note, only on the initial level can the allocation fail if
     689             :          * the summary says there's an extent.
     690             :          */
     691    12663208 :         for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
     692             :                 /*
     693             :                  * Loop over all the bitmap blocks.
     694             :                  */
     695  2597820587 :                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
     696             :                         /*
     697             :                          * Get the summary for this level/block.
     698             :                          */
     699  2588540531 :                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
     700             :                                 &sum);
     701  2588540531 :                         if (error) {
     702           1 :                                 return error;
     703             :                         }
     704             :                         /*
     705             :                          * Nothing there, on to the next block.
     706             :                          */
     707  2588540530 :                         if (!sum)
     708  2584490104 :                                 continue;
     709             :                         /*
     710             :                          * Try allocating the extent.
     711             :                          */
     712     4050426 :                         error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
     713             :                                 maxlen, len, &n, rbpp, rsb, prod, &r);
     714     4050426 :                         if (error) {
     715           1 :                                 return error;
     716             :                         }
     717             :                         /*
     718             :                          * If it worked, return that.
     719             :                          */
     720     4050425 :                         if (r != NULLRTEXTNO) {
     721     3375787 :                                 *rtx = r;
     722     3375787 :                                 return 0;
     723             :                         }
     724             :                         /*
     725             :                          * If the "next block to try" returned from the
     726             :                          * allocator is beyond the next bitmap block,
     727             :                          * skip to that bitmap block.
     728             :                          */
     729      674638 :                         if (xfs_rtx_to_rbmblock(mp, n) > i + 1)
     730           1 :                                 i = xfs_rtx_to_rbmblock(mp, n) - 1;
     731             :                 }
     732             :         }
     733             :         /*
     734             :          * Didn't find any maxlen blocks.  Try smaller ones, unless
     735             :          * we're asking for a fixed size extent.
     736             :          */
     737        7363 :         if (minlen > --maxlen) {
     738         698 :                 *rtx = NULLRTEXTNO;
     739         698 :                 return 0;
     740             :         }
     741        6665 :         ASSERT(minlen != 0);
     742        6665 :         ASSERT(maxlen != 0);
     743             : 
     744             :         /*
     745             :          * Loop over sizes, from maxlen down to minlen.
     746             :          * This time, when we do the allocations, allow smaller ones
     747             :          * to succeed.
     748             :          */
     749        9422 :         for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
     750             :                 /*
     751             :                  * Loop over all the bitmap blocks, try an allocation
     752             :                  * starting in that block.
     753             :                  */
     754       36072 :                 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
     755             :                         /*
     756             :                          * Get the summary information for this level/block.
     757             :                          */
     758       33315 :                         error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
     759             :                                                   &sum);
     760       33315 :                         if (error) {
     761           0 :                                 return error;
     762             :                         }
     763             :                         /*
     764             :                          * If nothing there, go on to next.
     765             :                          */
     766       33315 :                         if (!sum)
     767       26649 :                                 continue;
     768             :                         /*
     769             :                          * Try the allocation.  Make sure the specified
     770             :                          * minlen/maxlen are in the possible range for
     771             :                          * this summary level.
     772             :                          */
     773       10725 :                         error = xfs_rtallocate_extent_block(mp, tp, i,
     774        6666 :                                         XFS_RTMAX(minlen, 1 << l),
     775        6666 :                                         XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
     776             :                                         len, &n, rbpp, rsb, prod, &r);
     777        6666 :                         if (error) {
     778           0 :                                 return error;
     779             :                         }
     780             :                         /*
     781             :                          * If it worked, return that extent.
     782             :                          */
     783        6666 :                         if (r != NULLRTEXTNO) {
     784        6665 :                                 *rtx = r;
     785        6665 :                                 return 0;
     786             :                         }
     787             :                         /*
     788             :                          * If the "next block to try" returned from the
     789             :                          * allocator is beyond the next bitmap block,
     790             :                          * skip to that bitmap block.
     791             :                          */
     792           1 :                         if (xfs_rtx_to_rbmblock(mp, n) > i + 1)
     793           0 :                                 i = xfs_rtx_to_rbmblock(mp, n) - 1;
     794             :                 }
     795             :         }
     796             :         /*
     797             :          * Got nothing, return failure.
     798             :          */
     799           0 :         *rtx = NULLRTEXTNO;
     800           0 :         return 0;
     801             : }
     802             : 
     803             : /* Get a buffer for the block. */
     804             : static int
     805        1601 : xfs_growfs_init_rtbuf(
     806             :         struct xfs_trans        *tp,
     807             :         struct xfs_inode        *ip,
     808             :         xfs_fsblock_t           fsbno,
     809             :         enum xfs_blft           buf_type)
     810             : {
     811        1601 :         struct xfs_mount        *mp = tp->t_mountp;
     812        1601 :         struct xfs_buf          *bp;
     813        1601 :         xfs_daddr_t             d;
     814        1601 :         int                     error;
     815             : 
     816        1601 :         d = XFS_FSB_TO_DADDR(mp, fsbno);
     817        1601 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, 0,
     818             :                         &bp);
     819        1601 :         if (error)
     820             :                 return error;
     821             : 
     822        1601 :         xfs_trans_buf_set_type(tp, bp, buf_type);
     823        1601 :         bp->b_ops = xfs_rtblock_ops(mp, buf_type == XFS_BLFT_RTSUMMARY_BUF);
     824        1601 :         memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
     825             : 
     826        1601 :         if (xfs_has_rtgroups(mp)) {
     827        1508 :                 struct xfs_rtbuf_blkinfo        *hdr = bp->b_addr;
     828             : 
     829        1508 :                 if (buf_type == XFS_BLFT_RTBITMAP_BUF)
     830        1476 :                         hdr->rt_magic = cpu_to_be32(XFS_RTBITMAP_MAGIC);
     831             :                 else
     832          32 :                         hdr->rt_magic = cpu_to_be32(XFS_RTSUMMARY_MAGIC);
     833        1508 :                 hdr->rt_owner = cpu_to_be64(ip->i_ino);
     834        1508 :                 hdr->rt_blkno = cpu_to_be64(d);
     835        1508 :                 uuid_copy(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid);
     836             :         }
     837             : 
     838        1601 :         xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
     839        1601 :         return 0;
     840             : }
     841             : 
     842             : /*
     843             :  * Allocate space to the bitmap or summary file, and zero it, for growfs.
     844             :  */
     845             : STATIC int
     846          93 : xfs_growfs_rt_alloc(
     847             :         struct xfs_mount        *mp,            /* file system mount point */
     848             :         xfs_extlen_t            oblocks,        /* old count of blocks */
     849             :         xfs_extlen_t            nblocks,        /* new count of blocks */
     850             :         struct xfs_inode        *ip)            /* inode (bitmap/summary) */
     851             : {
     852          93 :         xfs_fileoff_t           bno;            /* block number in file */
     853          93 :         int                     error;          /* error return value */
     854          93 :         xfs_fsblock_t           fsbno;          /* filesystem block for bno */
     855          93 :         struct xfs_bmbt_irec    map;            /* block map output */
     856          93 :         int                     nmap;           /* number of block maps */
     857          93 :         int                     resblks;        /* space reservation */
     858          93 :         enum xfs_blft           buf_type;
     859          93 :         struct xfs_trans        *tp;
     860             : 
     861          93 :         if (ip == mp->m_rsumip)
     862             :                 buf_type = XFS_BLFT_RTSUMMARY_BUF;
     863             :         else
     864          52 :                 buf_type = XFS_BLFT_RTBITMAP_BUF;
     865             : 
     866             :         /*
     867             :          * Allocate space to the file, as necessary.
     868             :          */
     869         208 :         while (oblocks < nblocks) {
     870         126 :                 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
     871             :                 /*
     872             :                  * Reserve space & log for one extent added to the file.
     873             :                  */
     874         126 :                 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_growrtalloc,
     875             :                                 resblks, 0, false, &tp);
     876         126 :                 if (error)
     877           0 :                         return error;
     878             : 
     879         126 :                 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
     880             :                                 XFS_IEXT_ADD_NOSPLIT_CNT);
     881         126 :                 if (error == -EFBIG)
     882          10 :                         error = xfs_iext_count_upgrade(tp, ip,
     883             :                                         XFS_IEXT_ADD_NOSPLIT_CNT);
     884         126 :                 if (error)
     885          10 :                         goto out_trans_cancel;
     886             : 
     887             :                 /*
     888             :                  * Allocate blocks to the bitmap file.
     889             :                  */
     890         116 :                 nmap = 1;
     891         116 :                 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
     892             :                                         XFS_BMAPI_METADATA, 0, &map, &nmap);
     893         116 :                 if (!error && nmap < 1)
     894             :                         error = -ENOSPC;
     895         115 :                 if (error)
     896           1 :                         goto out_trans_cancel;
     897             :                 /*
     898             :                  * Free any blocks freed up in the transaction, then commit.
     899             :                  */
     900         115 :                 error = xfs_trans_commit(tp);
     901         115 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     902         115 :                 if (error)
     903           0 :                         return error;
     904             :                 /*
     905             :                  * Now we need to clear the allocated blocks.
     906             :                  * Do this one block per transaction, to keep it simple.
     907             :                  */
     908         115 :                 for (bno = map.br_startoff, fsbno = map.br_startblock;
     909        1716 :                      bno < map.br_startoff + map.br_blockcount;
     910        1601 :                      bno++, fsbno++) {
     911             :                         /*
     912             :                          * Reserve log for one block zeroing.
     913             :                          */
     914        1601 :                         error = xfs_trans_alloc_inode(ip,
     915             :                                         &M_RES(mp)->tr_growrtzero, 0, 0, false,
     916             :                                         &tp);
     917        1601 :                         if (error)
     918           0 :                                 return error;
     919             : 
     920        1601 :                         error = xfs_growfs_init_rtbuf(tp, ip, fsbno, buf_type);
     921        1601 :                         if (error)
     922           0 :                                 goto out_trans_cancel;
     923             : 
     924             :                         /*
     925             :                          * Commit the transaction.
     926             :                          */
     927        1601 :                         error = xfs_trans_commit(tp);
     928        1601 :                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     929        1601 :                         if (error)
     930           0 :                                 return error;
     931             :                 }
     932             :                 /*
     933             :                  * Go on to the next extent, if any.
     934             :                  */
     935         115 :                 oblocks = map.br_startoff + map.br_blockcount;
     936             :         }
     937             : 
     938             :         return 0;
     939             : 
     940          11 : out_trans_cancel:
     941          11 :         xfs_trans_cancel(tp);
     942          11 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     943          11 :         return error;
     944             : }
     945             : 
     946             : static void
     947       66802 : xfs_alloc_rsum_cache(
     948             :         xfs_mount_t     *mp,            /* file system mount structure */
     949             :         xfs_extlen_t    rbmblocks)      /* number of rt bitmap blocks */
     950             : {
     951             :         /*
     952             :          * The rsum cache is initialized to all zeroes, which is trivially a
     953             :          * lower bound on the minimum level with any free extents. We can
     954             :          * continue without the cache if it couldn't be allocated.
     955             :          */
     956       66802 :         mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
     957       66802 :         if (!mp->m_rsum_cache)
     958           0 :                 xfs_warn(mp, "could not allocate realtime summary cache");
     959       66802 : }
     960             : 
     961             : /*
     962             :  * Visible (exported) functions.
     963             :  */
     964             : 
     965             : static int
     966        1518 : xfs_growfs_rt_free_new(
     967             :         struct xfs_trans        *tp,
     968             :         struct xfs_mount        *nmp,
     969             :         xfs_rtbxlen_t           *freed_rtx)
     970             : {
     971        1518 :         struct xfs_mount        *mp = tp->t_mountp;
     972        1518 :         struct xfs_sb           *sbp = &mp->m_sb;
     973        1518 :         struct xfs_sb           *nsbp = &nmp->m_sb;
     974        1518 :         struct xfs_buf          *bp = NULL;
     975        1518 :         xfs_fileoff_t           sumbno;
     976        1518 :         xfs_rtblock_t           rtbno, next_rtbno;
     977        1518 :         int                     error = 0;
     978             : 
     979        1518 :         if (!xfs_has_rtgroups(mp)) {
     980           8 :                 *freed_rtx = nsbp->sb_rextents - sbp->sb_rextents;
     981           8 :                 return xfs_rtfree_range(nmp, tp, sbp->sb_rextents, *freed_rtx,
     982             :                                 &bp, &sumbno);
     983             :         }
     984             : 
     985        1510 :         *freed_rtx = 0;
     986             : 
     987        1510 :         rtbno = xfs_rtx_to_rtb(nmp, sbp->sb_rextents);
     988        1510 :         next_rtbno = xfs_rtx_to_rtb(nmp, nsbp->sb_rextents);
     989        6095 :         while (rtbno < next_rtbno) {
     990        4585 :                 xfs_rtxnum_t    start_rtx, next_rtx;
     991        4585 :                 xfs_rtblock_t   next_free_rtbno;
     992        4585 :                 xfs_rgnumber_t  rgno;
     993        4585 :                 xfs_rgblock_t   rgbno;
     994             : 
     995             :                 /*
     996             :                  * Compute the first new extent that we want to free, being
     997             :                  * careful to skip past a realtime superblock at the start of
     998             :                  * the new region.
     999             :                  */
    1000        4585 :                 rgbno = xfs_rtb_to_rgbno(nmp, rtbno, &rgno);
    1001        4585 :                 if (rgbno == 0) {
    1002        3083 :                         rtbno += nsbp->sb_rextsize;
    1003        3083 :                         if (rtbno >= next_rtbno)
    1004             :                                 break;
    1005             :                 }
    1006             : 
    1007        4585 :                 start_rtx = xfs_rtb_to_rtxt(nmp, rtbno);
    1008             : 
    1009             :                 /*
    1010             :                  * Stop freeing either at the end of the new rt section or at
    1011             :                  * the start of the next realtime group.
    1012             :                  */
    1013        4585 :                 next_free_rtbno = xfs_rgbno_to_rtb(nmp, rgno + 1, 0);
    1014        4585 :                 next_rtx = xfs_rtb_to_rtxt(nmp, next_free_rtbno);
    1015        4585 :                 next_rtx = min(next_rtx, nsbp->sb_rextents);
    1016             : 
    1017        4585 :                 bp = NULL;
    1018        4585 :                 *freed_rtx += next_rtx - start_rtx;
    1019        4585 :                 error = xfs_rtfree_range(nmp, tp, start_rtx,
    1020             :                                 next_rtx - start_rtx, &bp, &sumbno);
    1021        4585 :                 if (error)
    1022             :                         break;
    1023             : 
    1024        4585 :                 rtbno = next_free_rtbno;
    1025             :         }
    1026             : 
    1027             :         return error;
    1028             : }
    1029             : 
    1030             : static int
    1031           2 : xfs_growfs_rt_init_primary(
    1032             :         struct xfs_mount        *mp)
    1033             : {
    1034           2 :         struct xfs_buf          *rtsb_bp;
    1035           2 :         int                     error;
    1036             : 
    1037           2 :         error = xfs_buf_get_uncached(mp->m_rtdev_targp, XFS_FSB_TO_BB(mp, 1),
    1038             :                         0, &rtsb_bp);
    1039           2 :         if (error)
    1040             :                 return error;
    1041             : 
    1042           2 :         rtsb_bp->b_maps[0].bm_bn = XFS_RTSB_DADDR;
    1043           2 :         rtsb_bp->b_ops = &xfs_rtsb_buf_ops;
    1044             : 
    1045           2 :         xfs_rtgroup_update_super(rtsb_bp, mp->m_sb_bp);
    1046           2 :         mp->m_rtsb_bp = rtsb_bp;
    1047           2 :         xfs_buf_unlock(rtsb_bp);
    1048           2 :         return 0;
    1049             : }
    1050             : 
    1051             : /* Add a metadata inode for a realtime rmap btree. */
    1052             : static int
    1053        5785 : xfs_growfsrt_create_rtrmap(
    1054             :         struct xfs_rtgroup      *rtg)
    1055             : {
    1056        5785 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1057        5785 :         struct xfs_imeta_update upd;
    1058        5785 :         struct xfs_rmap_irec    rmap = {
    1059             :                 .rm_startblock  = 0,
    1060        5785 :                 .rm_blockcount  = mp->m_sb.sb_rextsize,
    1061             :                 .rm_owner       = XFS_RMAP_OWN_FS,
    1062             :                 .rm_offset      = 0,
    1063             :                 .rm_flags       = 0,
    1064             :         };
    1065        5785 :         struct xfs_btree_cur    *cur;
    1066        5785 :         struct xfs_imeta_path   *path;
    1067        5785 :         struct xfs_inode        *ip = NULL;
    1068        5785 :         int                     error;
    1069             : 
    1070        5785 :         if (!xfs_has_rtrmapbt(mp) || rtg->rtg_rmapip)
    1071             :                 return 0;
    1072             : 
    1073        3083 :         error = xfs_rtrmapbt_create_path(mp, rtg->rtg_rgno, &path);
    1074        3083 :         if (error)
    1075             :                 return error;
    1076             : 
    1077        3083 :         error = xfs_imeta_ensure_dirpath(mp, path);
    1078        3083 :         if (error)
    1079           0 :                 goto out_path;
    1080             : 
    1081        3083 :         error = xfs_imeta_start_create(mp, path, &upd);
    1082        3083 :         if (error)
    1083           0 :                 goto out_path;
    1084             : 
    1085        3083 :         error = xfs_rtrmapbt_create(&upd, &ip);
    1086        3083 :         if (error)
    1087           0 :                 goto out_cancel;
    1088             : 
    1089        3083 :         lockdep_set_class(&ip->i_lock.mr_lock, &xfs_rrmapip_key);
    1090             : 
    1091             :         /* Rmap the rtgroup superblock; this had better fit in the data fork. */
    1092        3083 :         cur = xfs_rtrmapbt_init_cursor(mp, upd.tp, rtg, ip);
    1093        3083 :         error = xfs_rmap_map_raw(cur, &rmap);
    1094        3083 :         xfs_btree_del_cursor(cur, error);
    1095        3083 :         if (error)
    1096           0 :                 goto out_cancel;
    1097             : 
    1098        3083 :         error = xfs_imeta_commit_update(&upd);
    1099        3083 :         if (error)
    1100           0 :                 goto out_path;
    1101             : 
    1102        3083 :         xfs_imeta_free_path(path);
    1103        3083 :         xfs_finish_inode_setup(ip);
    1104        3083 :         rtg->rtg_rmapip = ip;
    1105        3083 :         return 0;
    1106             : 
    1107           0 : out_cancel:
    1108           0 :         xfs_imeta_cancel_update(&upd, error);
    1109             :         /* Have to finish setting up the inode to ensure it's deleted. */
    1110           0 :         if (ip) {
    1111           0 :                 xfs_finish_inode_setup(ip);
    1112           0 :                 xfs_irele(ip);
    1113             :         }
    1114           0 : out_path:
    1115           0 :         xfs_imeta_free_path(path);
    1116           0 :         return error;
    1117             : }
    1118             : 
    1119             : /* Add a metadata inode for a realtime refcount btree. */
    1120             : static int
    1121        5785 : xfs_growfsrt_create_rtrefcount(
    1122             :         struct xfs_rtgroup      *rtg)
    1123             : {
    1124        5785 :         struct xfs_imeta_update upd;
    1125        5785 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1126        5785 :         struct xfs_imeta_path   *path;
    1127        5785 :         struct xfs_inode        *ip = NULL;
    1128        5785 :         int                     error;
    1129             : 
    1130        5785 :         if (!xfs_has_rtreflink(mp) || rtg->rtg_refcountip)
    1131             :                 return 0;
    1132             : 
    1133        3083 :         error = xfs_rtrefcountbt_create_path(mp, rtg->rtg_rgno, &path);
    1134        3083 :         if (error)
    1135             :                 return error;
    1136             : 
    1137        3083 :         error = xfs_imeta_ensure_dirpath(mp, path);
    1138        3083 :         if (error)
    1139           0 :                 goto out_path;
    1140             : 
    1141        3083 :         error = xfs_imeta_start_create(mp, path, &upd);
    1142        3083 :         if (error)
    1143           0 :                 goto out_path;
    1144             : 
    1145        3083 :         error = xfs_rtrefcountbt_create(&upd, &ip);
    1146        3083 :         if (error)
    1147           0 :                 goto out_cancel;
    1148             : 
    1149        3083 :         lockdep_set_class(&ip->i_lock.mr_lock, &xfs_rrefcountip_key);
    1150             : 
    1151        3083 :         error = xfs_imeta_commit_update(&upd);
    1152        3083 :         if (error)
    1153           0 :                 goto out_path;
    1154             : 
    1155        3083 :         xfs_imeta_free_path(path);
    1156        3083 :         xfs_finish_inode_setup(ip);
    1157        3083 :         rtg->rtg_refcountip = ip;
    1158        3083 :         return 0;
    1159             : 
    1160             : out_cancel:
    1161           0 :         xfs_imeta_cancel_update(&upd, error);
    1162             :         /* Have to finish setting up the inode to ensure it's deleted. */
    1163           0 :         if (ip) {
    1164           0 :                 xfs_finish_inode_setup(ip);
    1165           0 :                 xfs_irele(ip);
    1166             :         }
    1167           0 : out_path:
    1168           0 :         xfs_imeta_free_path(path);
    1169           0 :         return error;
    1170             : }
    1171             : 
    1172             : /*
    1173             :  * Check that changes to the realtime geometry won't affect the minimum
    1174             :  * log size, which would cause the fs to become unusable.
    1175             :  */
    1176             : int
    1177        1870 : xfs_growfs_check_rtgeom(
    1178             :         const struct xfs_mount  *mp,
    1179             :         xfs_rfsblock_t          dblocks,
    1180             :         xfs_rfsblock_t          rblocks,
    1181             :         xfs_agblock_t           rextsize,
    1182             :         xfs_rtblock_t           rextents,
    1183             :         xfs_extlen_t            rbmblocks,
    1184             :         uint8_t                 rextslog)
    1185             : {
    1186        1870 :         struct xfs_mount        *fake_mp;
    1187        1870 :         int                     min_logfsbs;
    1188             : 
    1189        1870 :         fake_mp = kmem_alloc(sizeof(struct xfs_mount), KM_MAYFAIL);
    1190        1870 :         if (!fake_mp)
    1191             :                 return -ENOMEM;
    1192             : 
    1193             :         /*
    1194             :          * Create a dummy xfs_mount with the new rt geometry, and compute the
    1195             :          * new minimum log size.  This ensures that the log is big enough to
    1196             :          * handle the larger transactions that we could start sending.
    1197             :          */
    1198        3740 :         memcpy(fake_mp, mp, sizeof(struct xfs_mount));
    1199             : 
    1200        1870 :         fake_mp->m_sb.sb_dblocks = dblocks;
    1201        1870 :         fake_mp->m_sb.sb_rblocks = rblocks;
    1202        1870 :         fake_mp->m_sb.sb_rextents = rextents;
    1203        1870 :         fake_mp->m_sb.sb_rextsize = rextsize;
    1204        1870 :         fake_mp->m_sb.sb_rbmblocks = rbmblocks;
    1205        1870 :         fake_mp->m_sb.sb_rextslog = rextslog;
    1206        1870 :         if (rblocks > 0)
    1207         906 :                 fake_mp->m_features |= XFS_FEAT_REALTIME;
    1208             : 
    1209        1870 :         xfs_rtrmapbt_compute_maxlevels(fake_mp);
    1210        1870 :         xfs_rtrefcountbt_compute_maxlevels(fake_mp);
    1211             : 
    1212        1870 :         xfs_trans_resv_calc(fake_mp, M_RES(fake_mp));
    1213        1870 :         min_logfsbs = xfs_log_calc_minimum_size(fake_mp);
    1214        1870 :         trace_xfs_growfs_check_rtgeom(mp, min_logfsbs);
    1215             : 
    1216        1870 :         kmem_free(fake_mp);
    1217             : 
    1218        1870 :         if (mp->m_sb.sb_logblocks < min_logfsbs)
    1219           0 :                 return -ENOSPC;
    1220             : 
    1221             :         return 0;
    1222             : }
    1223             : 
    1224             : /*
    1225             :  * Grow the realtime area of the filesystem.
    1226             :  */
    1227             : int
    1228          55 : xfs_growfs_rt(
    1229             :         xfs_mount_t     *mp,            /* mount point for filesystem */
    1230             :         xfs_growfs_rt_t *in)            /* growfs rt input struct */
    1231             : {
    1232          55 :         xfs_fileoff_t   bmbno;          /* bitmap block number */
    1233          55 :         struct xfs_buf  *bp;            /* temporary buffer */
    1234          55 :         int             error;          /* error return value */
    1235          55 :         xfs_mount_t     *nmp;           /* new (fake) mount structure */
    1236          55 :         xfs_rfsblock_t  nrblocks;       /* new number of realtime blocks */
    1237          55 :         xfs_extlen_t    nrbmblocks;     /* new number of rt bitmap blocks */
    1238          55 :         xfs_rtxnum_t    nrextents;      /* new number of realtime extents */
    1239          55 :         uint8_t         nrextslog;      /* new log2 of sb_rextents */
    1240          55 :         xfs_extlen_t    nrsumblocks;    /* new number of summary blocks */
    1241          55 :         uint            nrsumlevels;    /* new rt summary levels */
    1242          55 :         uint            nrsumsize;      /* new size of rt summary, bytes */
    1243          55 :         xfs_sb_t        *nsbp;          /* new superblock */
    1244          55 :         xfs_extlen_t    rbmblocks;      /* current number of rt bitmap blocks */
    1245          55 :         xfs_extlen_t    rsumblocks;     /* current number of rt summary blks */
    1246          55 :         xfs_sb_t        *sbp;           /* old superblock */
    1247          55 :         uint8_t         *rsum_cache;    /* old summary cache */
    1248             : 
    1249          55 :         sbp = &mp->m_sb;
    1250             : 
    1251          55 :         if (!capable(CAP_SYS_ADMIN))
    1252             :                 return -EPERM;
    1253             : 
    1254             :         /* Needs to have been mounted with an rt device. */
    1255          55 :         if (!XFS_IS_REALTIME_MOUNT(mp))
    1256             :                 return -EINVAL;
    1257             :         /*
    1258             :          * Mount should fail if the rt bitmap/summary files don't load, but
    1259             :          * we'll check anyway.
    1260             :          */
    1261          55 :         if (!mp->m_rbmip || !mp->m_rsumip)
    1262             :                 return -EINVAL;
    1263             : 
    1264             :         /* Shrink not supported. */
    1265          55 :         if (in->newblocks <= sbp->sb_rblocks)
    1266             :                 return -EINVAL;
    1267             : 
    1268             :         /* Can only change rt extent size when adding rt volume. */
    1269          55 :         if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
    1270             :                 return -EINVAL;
    1271             : 
    1272             :         /* Range check the extent size. */
    1273          55 :         if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
    1274          55 :             XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
    1275           0 :                 return -EINVAL;
    1276             : 
    1277             :         /* Unsupported realtime features. */
    1278          55 :         if (!xfs_has_rtgroups(mp) && (xfs_has_rmapbt(mp) || xfs_has_reflink(mp)))
    1279             :                 return -EOPNOTSUPP;
    1280          92 :         if (xfs_has_reflink(mp) && !is_power_of_2(mp->m_sb.sb_rextsize) &&
    1281           8 :             (XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) & ~PAGE_MASK))
    1282             :                 return -EOPNOTSUPP;
    1283             : 
    1284          54 :         nrblocks = in->newblocks;
    1285          54 :         error = xfs_sb_validate_fsb_count(sbp, nrblocks);
    1286          54 :         if (error)
    1287             :                 return error;
    1288             :         /*
    1289             :          * Read in the last block of the device, make sure it exists.
    1290             :          */
    1291         162 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp,
    1292          54 :                                 XFS_FSB_TO_BB(mp, nrblocks - 1),
    1293          54 :                                 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
    1294          54 :         if (error)
    1295             :                 return error;
    1296          54 :         xfs_buf_relse(bp);
    1297             : 
    1298             :         /*
    1299             :          * Calculate new parameters.  These are the final values to be reached.
    1300             :          */
    1301          54 :         nrextents = nrblocks;
    1302          54 :         do_div(nrextents, in->extsize);
    1303          54 :         nrbmblocks = xfs_rtbitmap_blockcount(mp, nrextents);
    1304          54 :         nrextslog = xfs_highbit32(nrextents);
    1305          54 :         nrsumlevels = nrextslog + 1;
    1306          54 :         nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels, nrbmblocks);
    1307          54 :         nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
    1308             :         /*
    1309             :          * New summary size can't be more than half the size of
    1310             :          * the log.  This prevents us from getting a log overflow,
    1311             :          * since we'll log basically the whole summary file at once.
    1312             :          */
    1313          54 :         if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
    1314             :                 return -EINVAL;
    1315             : 
    1316             :         /* Make sure the new fs size won't cause problems with the log. */
    1317          54 :         error = xfs_growfs_check_rtgeom(mp, mp->m_sb.sb_dblocks, nrblocks,
    1318             :                         in->extsize, nrextents, nrbmblocks, nrextslog);
    1319          54 :         if (error)
    1320             :                 return error;
    1321             : 
    1322             :         /* Allocate the new rt group structures */
    1323          54 :         if (xfs_has_rtgroups(mp)) {
    1324          38 :                 uint64_t        new_rgcount;
    1325             : 
    1326          38 :                 new_rgcount = howmany_64(nrblocks, mp->m_sb.sb_rgblocks);
    1327          38 :                 if (new_rgcount > XFS_MAX_RGNUMBER)
    1328             :                         return -EINVAL;
    1329             : 
    1330             :                 /*
    1331             :                  * We don't support changing the group size to match the extent
    1332             :                  * size, even if the size of the rt section is currently zero.
    1333             :                  */
    1334          38 :                 if (mp->m_sb.sb_rgblocks % in->extsize != 0)
    1335             :                         return -EOPNOTSUPP;
    1336             : 
    1337          36 :                 if (mp->m_sb.sb_rblocks == 0) {
    1338           2 :                         error = xfs_growfs_rt_init_primary(mp);
    1339           2 :                         if (error)
    1340             :                                 return error;
    1341             :                 }
    1342             : 
    1343          36 :                 if (new_rgcount > mp->m_sb.sb_rgcount) {
    1344          25 :                         error = xfs_initialize_rtgroups(mp, new_rgcount);
    1345          25 :                         if (error)
    1346             :                                 return error;
    1347             :                 }
    1348             :         }
    1349             : 
    1350             :         /*
    1351             :          * Get the old block counts for bitmap and summary inodes.
    1352             :          * These can't change since other growfs callers are locked out.
    1353             :          */
    1354          52 :         rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
    1355          52 :         rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
    1356             :         /*
    1357             :          * Allocate space to the bitmap and summary files, as necessary.
    1358             :          */
    1359          52 :         error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
    1360          52 :         if (error)
    1361             :                 return error;
    1362          41 :         error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
    1363          41 :         if (error)
    1364             :                 return error;
    1365             : 
    1366          41 :         rsum_cache = mp->m_rsum_cache;
    1367          41 :         if (nrbmblocks != sbp->sb_rbmblocks)
    1368          19 :                 xfs_alloc_rsum_cache(mp, nrbmblocks);
    1369             : 
    1370             :         /*
    1371             :          * Allocate a new (fake) mount/sb.
    1372             :          */
    1373          41 :         nmp = kmem_alloc(sizeof(*nmp), 0);
    1374             :         /*
    1375             :          * Loop over the bitmap blocks.
    1376             :          * We will do everything one bitmap block at a time.
    1377             :          * Skip the current block if it is exactly full.
    1378             :          * This also deals with the case where there were no rtextents before.
    1379             :          */
    1380          41 :         bmbno = sbp->sb_rbmblocks;
    1381          41 :         if (xfs_rtx_to_rbmword(mp, sbp->sb_rextents) != 0)
    1382          39 :                 bmbno--;
    1383        1559 :         for (; bmbno < nrbmblocks; bmbno++) {
    1384        1518 :                 struct xfs_trans        *tp;
    1385        1518 :                 struct xfs_rtgroup      *rtg;
    1386        1518 :                 xfs_rfsblock_t          nrblocks_step;
    1387        1518 :                 xfs_rtbxlen_t           freed_rtx = 0;
    1388        1518 :                 xfs_rgnumber_t          last_rgno = mp->m_sb.sb_rgcount - 1;
    1389             : 
    1390        1518 :                 *nmp = *mp;
    1391        1518 :                 nsbp = &nmp->m_sb;
    1392             :                 /*
    1393             :                  * Calculate new sb and mount fields for this round.
    1394             :                  */
    1395        1518 :                 nsbp->sb_rextsize = in->extsize;
    1396        1518 :                 nmp->m_rtxblklog = -1; /* don't use shift or masking */
    1397        1518 :                 nsbp->sb_rbmblocks = bmbno + 1;
    1398        1518 :                 nrblocks_step = (bmbno + 1) * mp->m_rtx_per_rbmblock *
    1399        1518 :                                 nsbp->sb_rextsize;
    1400        1518 :                 nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
    1401        1518 :                 nsbp->sb_rextents = xfs_rtb_to_rtxt(nmp, nsbp->sb_rblocks);
    1402        1518 :                 ASSERT(nsbp->sb_rextents != 0);
    1403        1518 :                 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
    1404        1518 :                 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
    1405        1518 :                 nrsumblocks = xfs_rtsummary_blockcount(mp, nrsumlevels,
    1406             :                                 nsbp->sb_rbmblocks);
    1407        1518 :                 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
    1408             : 
    1409        1518 :                 if (xfs_has_rtgroups(mp)) {
    1410        1510 :                         xfs_rgnumber_t  rgno = last_rgno;
    1411             : 
    1412        1510 :                         nsbp->sb_rgcount = howmany_64(nsbp->sb_rblocks,
    1413             :                                                       nsbp->sb_rgblocks);
    1414             : 
    1415        7295 :                         for_each_rtgroup_range(mp, rgno, nsbp->sb_rgcount, rtg) {
    1416        5785 :                                 error = xfs_growfsrt_create_rtrmap(rtg);
    1417        5785 :                                 if (error) {
    1418           0 :                                         xfs_rtgroup_rele(rtg);
    1419           0 :                                         break;
    1420             :                                 }
    1421             : 
    1422        5785 :                                 error = xfs_growfsrt_create_rtrefcount(rtg);
    1423        5785 :                                 if (error) {
    1424           0 :                                         xfs_rtgroup_rele(rtg);
    1425           0 :                                         break;
    1426             :                                 }
    1427             :                         }
    1428             :                 }
    1429             : 
    1430             :                 /*
    1431             :                  * Start a transaction, get the log reservation.
    1432             :                  */
    1433        1518 :                 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
    1434             :                                 &tp);
    1435        1518 :                 if (error)
    1436             :                         break;
    1437             :                 /*
    1438             :                  * Lock out other callers by grabbing the bitmap and summary
    1439             :                  * inode locks and joining them to the transaction.
    1440             :                  */
    1441        1518 :                 xfs_rtbitmap_lock(tp, mp);
    1442             :                 /*
    1443             :                  * Update the bitmap inode's size ondisk and incore.  We need
    1444             :                  * to update the incore size so that inode inactivation won't
    1445             :                  * punch what it thinks are "posteof" blocks.
    1446             :                  */
    1447        1518 :                 mp->m_rbmip->i_disk_size =
    1448        1518 :                         nsbp->sb_rbmblocks * nsbp->sb_blocksize;
    1449        1518 :                 i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
    1450        1518 :                 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
    1451             :                 /*
    1452             :                  * Update the summary inode's size.  We need to update the
    1453             :                  * incore size so that inode inactivation won't punch what it
    1454             :                  * thinks are "posteof" blocks.
    1455             :                  */
    1456        1518 :                 mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
    1457        1518 :                 i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
    1458        1518 :                 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
    1459             :                 /*
    1460             :                  * Copy summary data from old to new sizes.
    1461             :                  * Do this when the real size (not block-aligned) changes.
    1462             :                  */
    1463        1518 :                 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
    1464          39 :                     mp->m_rsumlevels != nmp->m_rsumlevels) {
    1465        1492 :                         error = xfs_rtcopy_summary(mp, nmp, tp);
    1466        1492 :                         if (error)
    1467           0 :                                 goto error_cancel;
    1468             :                 }
    1469             : 
    1470             :                 /*
    1471             :                  * Update superblock fields.
    1472             :                  */
    1473        1518 :                 if (nsbp->sb_rextsize != sbp->sb_rextsize)
    1474           2 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
    1475           2 :                                 nsbp->sb_rextsize - sbp->sb_rextsize);
    1476        1518 :                 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
    1477        1479 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
    1478        1479 :                                 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
    1479        1518 :                 if (nsbp->sb_rblocks != sbp->sb_rblocks)
    1480        1518 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
    1481        1518 :                                 nsbp->sb_rblocks - sbp->sb_rblocks);
    1482        1518 :                 if (nsbp->sb_rextents != sbp->sb_rextents)
    1483        1518 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
    1484        1518 :                                 nsbp->sb_rextents - sbp->sb_rextents);
    1485        1518 :                 if (nsbp->sb_rextslog != sbp->sb_rextslog)
    1486         100 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
    1487         100 :                                 nsbp->sb_rextslog - sbp->sb_rextslog);
    1488        1518 :                 if (nsbp->sb_rgcount != sbp->sb_rgcount)
    1489         751 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RGCOUNT,
    1490         751 :                                 nsbp->sb_rgcount - sbp->sb_rgcount);
    1491             :                 /*
    1492             :                  * Free new extent.
    1493             :                  */
    1494        1518 :                 error = xfs_growfs_rt_free_new(tp, nmp, &freed_rtx);
    1495        1518 :                 if (error) {
    1496           0 : error_cancel:
    1497           0 :                         xfs_trans_cancel(tp);
    1498           0 :                         break;
    1499             :                 }
    1500             :                 /*
    1501             :                  * Mark more blocks free in the superblock.
    1502             :                  */
    1503        1518 :                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, freed_rtx);
    1504             :                 /*
    1505             :                  * Update mp values into the real mp structure.
    1506             :                  */
    1507        1518 :                 mp->m_rsumlevels = nrsumlevels;
    1508        1518 :                 mp->m_rsumsize = nrsumsize;
    1509             : 
    1510        1518 :                 error = xfs_trans_commit(tp);
    1511        1518 :                 if (error)
    1512             :                         break;
    1513             : 
    1514        6107 :                 for_each_rtgroup_from(mp, last_rgno, rtg)
    1515        4589 :                         rtg->rtg_blockcount = xfs_rtgroup_block_count(mp,
    1516             :                                                                 rtg->rtg_rgno);
    1517             : 
    1518             :                 /*
    1519             :                  * Ensure the mount RT feature flag is now set, and compute new
    1520             :                  * maxlevels for rt btrees.
    1521             :                  */
    1522        1518 :                 mp->m_features |= XFS_FEAT_REALTIME;
    1523        1518 :                 xfs_rtrmapbt_compute_maxlevels(mp);
    1524        1518 :                 xfs_rtrefcountbt_compute_maxlevels(mp);
    1525             :         }
    1526          41 :         if (error)
    1527           0 :                 goto out_free;
    1528             : 
    1529             :         /* Update secondary superblocks now the physical grow has completed */
    1530          41 :         error = xfs_update_secondary_sbs(mp);
    1531          41 :         if (error)
    1532           0 :                 goto out_free;
    1533             : 
    1534          41 :         error = xfs_rtgroup_update_secondary_sbs(mp);
    1535          41 :         if (error)
    1536           0 :                 goto out_free;
    1537             : 
    1538             :         /* Reset the rt metadata btree space reservations. */
    1539          41 :         xfs_rt_resv_free(mp);
    1540          41 :         error = xfs_rt_resv_init(mp);
    1541          41 :         if (error == -ENOSPC)
    1542           0 :                 error = 0;
    1543             : 
    1544          41 : out_free:
    1545             :         /*
    1546             :          * Free the fake mp structure.
    1547             :          */
    1548          41 :         kmem_free(nmp);
    1549             : 
    1550             :         /*
    1551             :          * If we had to allocate a new rsum_cache, we either need to free the
    1552             :          * old one (if we succeeded) or free the new one and restore the old one
    1553             :          * (if there was an error).
    1554             :          */
    1555          41 :         if (rsum_cache != mp->m_rsum_cache) {
    1556          19 :                 if (error) {
    1557           0 :                         kmem_free(mp->m_rsum_cache);
    1558           0 :                         mp->m_rsum_cache = rsum_cache;
    1559             :                 } else {
    1560          19 :                         kmem_free(rsum_cache);
    1561             :                 }
    1562             :         }
    1563             : 
    1564             :         return error;
    1565             : }
    1566             : 
    1567             : /*
    1568             :  * Allocate an extent in the realtime subvolume, with the usual allocation
    1569             :  * parameters.  The length units are all in realtime extents, as is the
    1570             :  * result block number.
    1571             :  */
    1572             : int                                     /* error */
    1573    86773557 : xfs_rtallocate_extent(
    1574             :         xfs_trans_t     *tp,            /* transaction pointer */
    1575             :         xfs_rtxnum_t    start,          /* starting rtext number to allocate */
    1576             :         xfs_rtxlen_t    minlen,         /* minimum length to allocate */
    1577             :         xfs_rtxlen_t    maxlen,         /* maximum length to allocate */
    1578             :         xfs_rtxlen_t    *len,           /* out: actual length allocated */
    1579             :         int             wasdel,         /* was a delayed allocation extent */
    1580             :         xfs_rtxlen_t    prod,           /* extent product factor */
    1581             :         xfs_rtxnum_t    *rtblock)       /* out: start rtext allocated */
    1582             : {
    1583    86773557 :         xfs_mount_t     *mp = tp->t_mountp;
    1584    86773557 :         int             error;          /* error value */
    1585    86773557 :         xfs_rtxnum_t    r;              /* result allocated rtext */
    1586    86773557 :         xfs_fileoff_t   sb;             /* summary file block number */
    1587    86773557 :         struct xfs_buf  *sumbp;         /* summary file block buffer */
    1588             : 
    1589    86773557 :         ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
    1590    86773557 :         ASSERT(minlen > 0 && minlen <= maxlen);
    1591             : 
    1592             :         /*
    1593             :          * If prod is set then figure out what to do to minlen and maxlen.
    1594             :          */
    1595    86773557 :         if (prod > 1) {
    1596      533547 :                 xfs_rtxlen_t    i;
    1597             : 
    1598      533547 :                 if ((i = maxlen % prod))
    1599           0 :                         maxlen -= i;
    1600      533547 :                 if ((i = minlen % prod))
    1601      518493 :                         minlen += prod - i;
    1602      533547 :                 if (maxlen < minlen) {
    1603           0 :                         *rtblock = NULLRTEXTNO;
    1604           0 :                         return 0;
    1605             :                 }
    1606             :         }
    1607             : 
    1608    86773557 : retry:
    1609    86773929 :         sumbp = NULL;
    1610    86773929 :         if (start == 0) {
    1611     3383152 :                 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
    1612             :                                 &sumbp,     &sb, prod, &r);
    1613             :         } else {
    1614    83390777 :                 error = xfs_rtallocate_extent_near(mp, tp, start, minlen, maxlen,
    1615             :                                 len, &sumbp, &sb, prod, &r);
    1616             :         }
    1617             : 
    1618    86773929 :         if (error)
    1619          10 :                 return error;
    1620             : 
    1621             :         /*
    1622             :          * If it worked, update the superblock.
    1623             :          */
    1624    86773919 :         if (r != NULLRTEXTNO) {
    1625    86773135 :                 long    slen = (long)*len;
    1626             : 
    1627    86773135 :                 ASSERT(*len >= minlen && *len <= maxlen);
    1628    86773135 :                 if (wasdel)
    1629           0 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
    1630             :                 else
    1631    86773135 :                         xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
    1632         784 :         } else if (prod > 1) {
    1633         372 :                 prod = 1;
    1634         372 :                 goto retry;
    1635             :         }
    1636             : 
    1637    86773547 :         *rtblock = r;
    1638    86773547 :         return 0;
    1639             : }
    1640             : 
    1641             : /* Read the primary realtime group's superblock and attach it to the mount. */
    1642             : int
    1643       66982 : xfs_rtmount_readsb(
    1644             :         struct xfs_mount        *mp)
    1645             : {
    1646       66982 :         struct xfs_buf          *bp;
    1647       66982 :         int                     error;
    1648             : 
    1649       66982 :         if (!xfs_has_rtgroups(mp))
    1650             :                 return 0;
    1651       60441 :         if (mp->m_sb.sb_rblocks == 0)
    1652             :                 return 0;
    1653       18702 :         if (mp->m_rtdev_targp == NULL) {
    1654           0 :                 xfs_warn(mp,
    1655             :         "Filesystem has a realtime volume, use rtdev=device option");
    1656           0 :                 return -ENODEV;
    1657             :         }
    1658             : 
    1659             :         /* m_blkbb_log is not set up yet */
    1660       18702 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp, XFS_RTSB_DADDR,
    1661       18702 :                         mp->m_sb.sb_blocksize >> BBSHIFT, XBF_NO_IOACCT, &bp,
    1662             :                         &xfs_rtsb_buf_ops);
    1663       18702 :         if (error) {
    1664           0 :                 xfs_warn(mp, "rt sb validate failed with error %d.", error);
    1665             :                 /* bad CRC means corrupted metadata */
    1666           0 :                 if (error == -EFSBADCRC)
    1667           0 :                         error = -EFSCORRUPTED;
    1668           0 :                 return error;
    1669             :         }
    1670             : 
    1671       18702 :         mp->m_rtsb_bp = bp;
    1672       18702 :         xfs_buf_unlock(bp);
    1673       18702 :         return 0;
    1674             : }
    1675             : 
    1676             : /* Detach the realtime superblock from the mount and free it. */
    1677             : void
    1678       66993 : xfs_rtmount_freesb(
    1679             :         struct xfs_mount        *mp)
    1680             : {
    1681       66993 :         struct xfs_buf          *bp = mp->m_rtsb_bp;
    1682             : 
    1683       66993 :         if (!bp)
    1684             :                 return;
    1685             : 
    1686       18708 :         xfs_buf_lock(bp);
    1687       18708 :         mp->m_rtsb_bp = NULL;
    1688       18708 :         xfs_buf_relse(bp);
    1689             : }
    1690             : 
    1691             : /*
    1692             :  * Initialize realtime fields in the mount structure.
    1693             :  */
    1694             : int                             /* error */
    1695       66956 : xfs_rtmount_init(
    1696             :         struct xfs_mount        *mp)    /* file system mount structure */
    1697             : {
    1698       66956 :         struct xfs_buf          *bp;    /* buffer for last block of subvolume */
    1699       66956 :         struct xfs_sb           *sbp;   /* filesystem superblock copy in mount */
    1700       66956 :         xfs_daddr_t             d;      /* address of last block of subvolume */
    1701       66956 :         unsigned int            rsumblocks;
    1702       66956 :         int                     error;
    1703             : 
    1704       66956 :         sbp = &mp->m_sb;
    1705       66956 :         if (sbp->sb_rblocks == 0)
    1706             :                 return 0;
    1707       23007 :         if (mp->m_rtdev_targp == NULL) {
    1708         100 :                 xfs_warn(mp,
    1709             :         "Filesystem has a realtime volume, use rtdev=device option");
    1710         100 :                 return -ENODEV;
    1711             :         }
    1712       22907 :         mp->m_rsumlevels = sbp->sb_rextslog + 1;
    1713       22907 :         rsumblocks = xfs_rtsummary_blockcount(mp, mp->m_rsumlevels,
    1714             :                         mp->m_sb.sb_rbmblocks);
    1715       22907 :         mp->m_rsumsize = XFS_FSB_TO_B(mp, rsumblocks);
    1716       22907 :         mp->m_rbmip = mp->m_rsumip = NULL;
    1717             :         /*
    1718             :          * Check that the realtime section is an ok size.
    1719             :          */
    1720       22907 :         d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
    1721       22907 :         if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
    1722           0 :                 xfs_warn(mp, "realtime mount -- %llu != %llu",
    1723             :                         (unsigned long long) XFS_BB_TO_FSB(mp, d),
    1724             :                         (unsigned long long) mp->m_sb.sb_rblocks);
    1725           0 :                 return -EFBIG;
    1726             :         }
    1727       68721 :         error = xfs_buf_read_uncached(mp->m_rtdev_targp,
    1728       22907 :                                         d - XFS_FSB_TO_BB(mp, 1),
    1729       22907 :                                         XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
    1730       22907 :         if (error) {
    1731           0 :                 xfs_warn(mp, "realtime device size check failed");
    1732           0 :                 return error;
    1733             :         }
    1734       22907 :         xfs_buf_relse(bp);
    1735       22907 :         return 0;
    1736             : }
    1737             : 
    1738             : static int
    1739       80095 : xfs_rtalloc_count_frextent(
    1740             :         struct xfs_mount                *mp,
    1741             :         struct xfs_trans                *tp,
    1742             :         const struct xfs_rtalloc_rec    *rec,
    1743             :         void                            *priv)
    1744             : {
    1745       80095 :         uint64_t                        *valp = priv;
    1746             : 
    1747       80095 :         *valp += rec->ar_extcount;
    1748       80095 :         return 0;
    1749             : }
    1750             : 
    1751             : /*
    1752             :  * Reinitialize the number of free realtime extents from the realtime bitmap.
    1753             :  * Callers must ensure that there is no other activity in the filesystem.
    1754             :  */
    1755             : int
    1756        1854 : xfs_rtalloc_reinit_frextents(
    1757             :         struct xfs_mount        *mp)
    1758             : {
    1759        1854 :         uint64_t                val = 0;
    1760        1854 :         int                     error;
    1761             : 
    1762        1854 :         xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
    1763        1854 :         error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
    1764             :                         &val);
    1765        1854 :         xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
    1766        1854 :         if (error)
    1767             :                 return error;
    1768             : 
    1769        1854 :         spin_lock(&mp->m_sb_lock);
    1770        1854 :         mp->m_sb.sb_frextents = val;
    1771        1854 :         spin_unlock(&mp->m_sb_lock);
    1772        1854 :         percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
    1773        1854 :         return 0;
    1774             : }
    1775             : 
    1776             : /* Free space reservations for rt metadata inodes. */
    1777             : void
    1778       45917 : xfs_rt_resv_free(
    1779             :         struct xfs_mount        *mp)
    1780             : {
    1781       45917 :         struct xfs_rtgroup      *rtg;
    1782       45917 :         xfs_rgnumber_t          rgno;
    1783             : 
    1784      247672 :         for_each_rtgroup(mp, rgno, rtg) {
    1785      201755 :                 xfs_imeta_resv_free_inode(rtg->rtg_refcountip);
    1786      201755 :                 xfs_imeta_resv_free_inode(rtg->rtg_rmapip);
    1787             :         }
    1788       45917 : }
    1789             : 
    1790             : /* Reserve space for rt metadata inodes' space expansion. */
    1791             : int
    1792       45873 : xfs_rt_resv_init(
    1793             :         struct xfs_mount        *mp)
    1794             : {
    1795       45873 :         struct xfs_rtgroup      *rtg;
    1796       45873 :         xfs_filblks_t           ask;
    1797       45873 :         xfs_rgnumber_t          rgno;
    1798       45873 :         int                     error = 0;
    1799             : 
    1800      244198 :         for_each_rtgroup(mp, rgno, rtg) {
    1801      198325 :                 int             err2;
    1802             : 
    1803      198325 :                 ask = xfs_rtrmapbt_calc_reserves(mp);
    1804      198325 :                 err2 = xfs_imeta_resv_init_inode(rtg->rtg_rmapip, ask);
    1805      198325 :                 if (err2 && !error)
    1806           0 :                         error = err2;
    1807             : 
    1808      198325 :                 ask = xfs_rtrefcountbt_calc_reserves(mp);
    1809      198325 :                 err2 = xfs_imeta_resv_init_inode(rtg->rtg_refcountip, ask);
    1810      198325 :                 if (err2 && !error)
    1811           3 :                         error = err2;
    1812             :         }
    1813             : 
    1814       45873 :         return error;
    1815             : }
    1816             : 
    1817             : static inline int
    1818             : __xfs_rt_iget(
    1819             :         struct xfs_mount        *mp,
    1820             :         xfs_ino_t               ino,
    1821             :         struct lock_class_key   *lockdep_key,
    1822             :         const char              *lockdep_key_name,
    1823             :         struct xfs_inode        **ipp)
    1824             : {
    1825      328296 :         int                     error;
    1826             : 
    1827      328296 :         error = xfs_imeta_iget(mp, ino, XFS_DIR3_FT_REG_FILE, ipp);
    1828      328296 :         if (error)
    1829           0 :                 return error;
    1830             : 
    1831             :         lockdep_set_class_and_name(&(*ipp)->i_lock.mr_lock, lockdep_key,
    1832             :                         lockdep_key_name);
    1833             :         return 0;
    1834             : }
    1835             : 
    1836             : #define xfs_rt_iget(mp, ino, lockdep_key, ipp) \
    1837             :         __xfs_rt_iget((mp), (ino), (lockdep_key), #lockdep_key, (ipp))
    1838             : 
    1839             : /* Load realtime rmap btree inode. */
    1840             : STATIC int
    1841       97442 : xfs_rtmount_rmapbt(
    1842             :         struct xfs_rtgroup      *rtg)
    1843             : {
    1844       97442 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1845       97442 :         struct xfs_imeta_path   *path;
    1846       97442 :         struct xfs_inode        *ip;
    1847       97442 :         xfs_ino_t               ino;
    1848       97442 :         int                     error;
    1849             : 
    1850       97442 :         if (!xfs_has_rtrmapbt(mp))
    1851             :                 return 0;
    1852             : 
    1853       97417 :         error = xfs_rtrmapbt_create_path(mp, rtg->rtg_rgno, &path);
    1854       97417 :         if (error)
    1855             :                 return error;
    1856             : 
    1857       97417 :         error = xfs_imeta_lookup(mp, path, &ino);
    1858       97417 :         if (error)
    1859           0 :                 goto out_path;
    1860             : 
    1861       97417 :         if (ino == NULLFSINO) {
    1862           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_RMAPBT);
    1863           0 :                 error = -EFSCORRUPTED;
    1864           0 :                 goto out_path;
    1865             :         }
    1866             : 
    1867       97417 :         error = xfs_rt_iget(mp, ino, &xfs_rrmapip_key, &ip);
    1868       97417 :         if (error)
    1869           6 :                 goto out_path;
    1870             : 
    1871       97411 :         if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_RMAP)) {
    1872           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_RMAPBT);
    1873           0 :                 error = -EFSCORRUPTED;
    1874           0 :                 goto out_rele;
    1875             :         }
    1876             : 
    1877       97411 :         rtg->rtg_rmapip = ip;
    1878       97411 :         ip = NULL;
    1879       97411 : out_rele:
    1880       97411 :         if (ip)
    1881           0 :                 xfs_imeta_irele(ip);
    1882       97411 : out_path:
    1883       97417 :         xfs_imeta_free_path(path);
    1884       97417 :         return error;
    1885             : }
    1886             : 
    1887             : /*
    1888             :  * Read in the bmbt of an rt metadata inode so that we never have to load them
    1889             :  * at runtime.  This enables the use of shared ILOCKs for rtbitmap scans.  Use
    1890             :  * an empty transaction to avoid deadlocking on loops in the bmbt.
    1891             :  */
    1892             : static inline int
    1893      133578 : xfs_rtmount_iread_extents(
    1894             :         struct xfs_inode        *ip)
    1895             : {
    1896      133578 :         struct xfs_trans        *tp;
    1897      133578 :         int                     error;
    1898             : 
    1899      133578 :         error = xfs_trans_alloc_empty(ip->i_mount, &tp);
    1900      133578 :         if (error)
    1901             :                 return error;
    1902             : 
    1903      133578 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    1904             : 
    1905      133578 :         error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
    1906      133578 :         if (error)
    1907           0 :                 goto out_unlock;
    1908             : 
    1909      133578 :         if (xfs_inode_has_attr_fork(ip)) {
    1910      120058 :                 error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
    1911      120058 :                 if (error)
    1912           0 :                         goto out_unlock;
    1913             :         }
    1914             : 
    1915      133578 : out_unlock:
    1916      133578 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    1917      133578 :         xfs_trans_cancel(tp);
    1918      133578 :         return error;
    1919             : }
    1920             : 
    1921             : /* Load realtime refcount btree inode. */
    1922             : STATIC int
    1923       97436 : xfs_rtmount_refcountbt(
    1924             :         struct xfs_rtgroup      *rtg)
    1925             : {
    1926       97436 :         struct xfs_mount        *mp = rtg->rtg_mount;
    1927       97436 :         struct xfs_imeta_path   *path;
    1928       97436 :         struct xfs_inode        *ip;
    1929       97436 :         xfs_ino_t               ino;
    1930       97436 :         int                     error;
    1931             : 
    1932       97436 :         if (!xfs_has_rtreflink(mp))
    1933             :                 return 0;
    1934             : 
    1935       97301 :         error = xfs_rtrefcountbt_create_path(mp, rtg->rtg_rgno, &path);
    1936       97301 :         if (error)
    1937             :                 return error;
    1938             : 
    1939       97301 :         error = xfs_imeta_lookup(mp, path, &ino);
    1940       97301 :         if (error)
    1941           0 :                 goto out_path;
    1942             : 
    1943       97301 :         if (ino == NULLFSINO) {
    1944           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_REFCNTBT);
    1945           0 :                 error = -EFSCORRUPTED;
    1946           0 :                 goto out_path;
    1947             :         }
    1948             : 
    1949       97301 :         error = xfs_rt_iget(mp, ino, &xfs_rrefcountip_key, &ip);
    1950       97301 :         if (error)
    1951           0 :                 goto out_path;
    1952             : 
    1953       97301 :         if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_REFCOUNT)) {
    1954           0 :                 xfs_rtgroup_mark_sick(rtg, XFS_SICK_RT_REFCNTBT);
    1955           0 :                 error = -EFSCORRUPTED;
    1956           0 :                 goto out_rele;
    1957             :         }
    1958             : 
    1959       97301 :         rtg->rtg_refcountip = ip;
    1960       97301 :         ip = NULL;
    1961       97301 : out_rele:
    1962       97301 :         if (ip)
    1963           0 :                 xfs_imeta_irele(ip);
    1964       97301 : out_path:
    1965       97301 :         xfs_imeta_free_path(path);
    1966       97301 :         return error;
    1967             : }
    1968             : 
    1969             : /*
    1970             :  * Get the bitmap and summary inodes and the summary cache into the mount
    1971             :  * structure at mount time.
    1972             :  */
    1973             : int
    1974       66789 : xfs_rtmount_inodes(
    1975             :         struct xfs_mount        *mp)            /* file system mount structure */
    1976             : {
    1977       66789 :         struct xfs_sb           *sbp;
    1978       66789 :         struct xfs_rtgroup      *rtg;
    1979       66789 :         xfs_rgnumber_t          rgno;
    1980       66789 :         int                     error;          /* error return value */
    1981             : 
    1982       66789 :         sbp = &mp->m_sb;
    1983       66789 :         error = xfs_rt_iget(mp, mp->m_sb.sb_rbmino, &xfs_rbmip_key,
    1984             :                         &mp->m_rbmip);
    1985       66789 :         if (xfs_metadata_is_sick(error))
    1986           0 :                 xfs_rt_mark_sick(mp, XFS_SICK_RT_BITMAP);
    1987       66789 :         if (error)
    1988             :                 return error;
    1989       66789 :         ASSERT(mp->m_rbmip != NULL);
    1990             : 
    1991       66789 :         error = xfs_rtmount_iread_extents(mp->m_rbmip);
    1992       66789 :         if (error)
    1993           0 :                 goto out_rele_bitmap;
    1994             : 
    1995       66789 :         error = xfs_rt_iget(mp, mp->m_sb.sb_rsumino, &xfs_rsumip_key,
    1996             :                         &mp->m_rsumip);
    1997       66789 :         if (xfs_metadata_is_sick(error))
    1998           0 :                 xfs_rt_mark_sick(mp, XFS_SICK_RT_SUMMARY);
    1999       66789 :         if (error)
    2000           0 :                 goto out_rele_bitmap;
    2001       66789 :         ASSERT(mp->m_rsumip != NULL);
    2002             : 
    2003       66789 :         error = xfs_rtmount_iread_extents(mp->m_rsumip);
    2004       66789 :         if (error)
    2005           0 :                 goto out_rele_summary;
    2006             : 
    2007      164225 :         for_each_rtgroup(mp, rgno, rtg) {
    2008       97442 :                 rtg->rtg_blockcount = xfs_rtgroup_block_count(mp,
    2009             :                                                               rtg->rtg_rgno);
    2010             : 
    2011       97442 :                 error = xfs_rtmount_rmapbt(rtg);
    2012       97442 :                 if (error) {
    2013           6 :                         xfs_rtgroup_rele(rtg);
    2014           6 :                         goto out_rele_rtgroup;
    2015             :                 }
    2016             : 
    2017       97436 :                 error = xfs_rtmount_refcountbt(rtg);
    2018       97436 :                 if (error) {
    2019           0 :                         xfs_rtgroup_rele(rtg);
    2020           0 :                         goto out_rele_rtgroup;
    2021             :                 }
    2022             :         }
    2023             : 
    2024       66783 :         xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
    2025       66783 :         return 0;
    2026             : 
    2027           6 : out_rele_rtgroup:
    2028          36 :         for_each_rtgroup(mp, rgno, rtg) {
    2029          30 :                 if (rtg->rtg_refcountip)
    2030           0 :                         xfs_imeta_irele(rtg->rtg_refcountip);
    2031          30 :                 rtg->rtg_refcountip = NULL;
    2032             : 
    2033          30 :                 if (rtg->rtg_rmapip)
    2034           0 :                         xfs_imeta_irele(rtg->rtg_rmapip);
    2035          30 :                 rtg->rtg_rmapip = NULL;
    2036             :         }
    2037           6 : out_rele_summary:
    2038           6 :         xfs_imeta_irele(mp->m_rsumip);
    2039           6 : out_rele_bitmap:
    2040           6 :         xfs_imeta_irele(mp->m_rbmip);
    2041           6 :         return error;
    2042             : }
    2043             : 
    2044             : /*
    2045             :  * Attach dquots for realtime metadata files.  Prior to the introduction of the
    2046             :  * metadata directory tree, the rtbitmap and rtsummary inodes were counted in
    2047             :  * the root dquot icount, so we must dqattach them to maintain correct counts.
    2048             :  */
    2049             : int
    2050       57552 : xfs_rtmount_dqattach(
    2051             :         struct xfs_mount        *mp)
    2052             : {
    2053       57552 :         int                     error;
    2054             : 
    2055       57552 :         if (xfs_has_metadir(mp))
    2056             :                 return 0;
    2057             : 
    2058        4345 :         error = xfs_qm_dqattach(mp->m_rbmip);
    2059        4345 :         if (error)
    2060             :                 return error;
    2061             : 
    2062        4345 :         return xfs_qm_dqattach(mp->m_rsumip);
    2063             : }
    2064             : 
    2065             : void
    2066       66794 : xfs_rtunmount_inodes(
    2067             :         struct xfs_mount        *mp)
    2068             : {
    2069       66794 :         struct xfs_rtgroup      *rtg;
    2070       66794 :         xfs_rgnumber_t          rgno;
    2071             : 
    2072       66794 :         kmem_free(mp->m_rsum_cache);
    2073             : 
    2074      167333 :         for_each_rtgroup(mp, rgno, rtg) {
    2075      100539 :                 if (rtg->rtg_refcountip)
    2076      100404 :                         xfs_imeta_irele(rtg->rtg_refcountip);
    2077      100539 :                 rtg->rtg_refcountip = NULL;
    2078             : 
    2079      100539 :                 if (rtg->rtg_rmapip)
    2080      100514 :                         xfs_imeta_irele(rtg->rtg_rmapip);
    2081      100539 :                 rtg->rtg_rmapip = NULL;
    2082             :         }
    2083       66794 :         if (mp->m_rbmip)
    2084       66794 :                 xfs_imeta_irele(mp->m_rbmip);
    2085       66794 :         if (mp->m_rsumip)
    2086       66794 :                 xfs_imeta_irele(mp->m_rsumip);
    2087       66794 : }
    2088             : 
    2089             : /*
    2090             :  * Pick an extent for allocation at the start of a new realtime file.
    2091             :  * Use the sequence number stored in the atime field of the bitmap inode.
    2092             :  * Translate this to a fraction of the rtextents, and return the product
    2093             :  * of rtextents and the fraction.
    2094             :  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
    2095             :  */
    2096             : int                                     /* error */
    2097     6310155 : xfs_rtpick_extent(
    2098             :         xfs_mount_t     *mp,            /* file system mount point */
    2099             :         xfs_trans_t     *tp,            /* transaction pointer */
    2100             :         xfs_rtxlen_t    len,            /* allocation length (rtextents) */
    2101             :         xfs_rtxnum_t    *pick)          /* result rt extent */
    2102             : {
    2103     6310155 :         xfs_rtxnum_t    b;              /* result rtext */
    2104     6310155 :         int             log2;           /* log of sequence number */
    2105     6310155 :         uint64_t        resid;          /* residual after log removed */
    2106     6310155 :         uint64_t        seq;            /* sequence number of file creation */
    2107     6310155 :         uint64_t        *seqp;          /* pointer to seqno in inode */
    2108             : 
    2109     6310155 :         ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
    2110             : 
    2111     6310155 :         seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
    2112     6310155 :         if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
    2113           5 :                 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
    2114           5 :                 *seqp = 0;
    2115             :         }
    2116     6310155 :         seq = *seqp;
    2117     6310155 :         if ((log2 = xfs_highbit64(seq)) == -1)
    2118       50076 :                 b = 0;
    2119             :         else {
    2120     6260079 :                 resid = seq - (1ULL << log2);
    2121           0 :                 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
    2122     6260079 :                     (log2 + 1);
    2123     6260079 :                 if (b >= mp->m_sb.sb_rextents)
    2124           0 :                         div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
    2125     6260079 :                 if (b + len > mp->m_sb.sb_rextents)
    2126         107 :                         b = mp->m_sb.sb_rextents - len;
    2127             :         }
    2128     6310155 :         *seqp = seq + 1;
    2129     6310155 :         xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
    2130     6310155 :         *pick = b;
    2131     6310155 :         return 0;
    2132             : }
    2133             : 
    2134             : /*
    2135             :  * Decide if this is an unwritten extent that isn't aligned to a rt extent
    2136             :  * boundary.  If it is, shorten the mapping so that we're ready to convert
    2137             :  * everything up to the next rt extent to a zeroed written extent.  If not,
    2138             :  * return false.
    2139             :  */
    2140             : static inline bool
    2141    58753247 : xfs_rtfile_want_conversion(
    2142             :         struct xfs_mount        *mp,
    2143             :         struct xfs_bmbt_irec    *irec)
    2144             : {
    2145    58753247 :         xfs_fileoff_t           rext_next;
    2146    58753247 :         xfs_extlen_t            modoff, modcnt;
    2147             : 
    2148    58753247 :         if (irec->br_state != XFS_EXT_UNWRITTEN)
    2149             :                 return false;
    2150             : 
    2151      982399 :         xfs_rtb_to_rtx(mp, irec->br_startoff, &modoff);
    2152      982396 :         if (modoff == 0) {
    2153      845881 :                 xfs_rtbxlen_t   rexts;
    2154             : 
    2155      845881 :                 rexts = xfs_rtb_to_rtx(mp, irec->br_blockcount, &modcnt);
    2156      845879 :                 if (rexts > 0) {
    2157             :                         /*
    2158             :                          * Unwritten mapping starts at an rt extent boundary
    2159             :                          * and is longer than one rt extent.  Round the length
    2160             :                          * down to the nearest extent but don't select it for
    2161             :                          * conversion.
    2162             :                          */
    2163      702838 :                         irec->br_blockcount -= modcnt;
    2164      702838 :                         modcnt = 0;
    2165             :                 }
    2166             : 
    2167             :                 /* Unwritten mapping is perfectly aligned, do not convert. */
    2168      845879 :                 if (modcnt == 0)
    2169             :                         return false;
    2170             :         }
    2171             : 
    2172             :         /*
    2173             :          * Unaligned and unwritten; trim to the current rt extent and select it
    2174             :          * for conversion.
    2175             :          */
    2176      279556 :         rext_next = (irec->br_startoff - modoff) + mp->m_sb.sb_rextsize;
    2177      279556 :         xfs_trim_extent(irec, irec->br_startoff, rext_next - irec->br_startoff);
    2178      279556 :         return true;
    2179             : }
    2180             : 
    2181             : /*
    2182             :  * Find an unwritten extent in the given file range, zero it, and convert the
    2183             :  * mapping to written.  Adjust the scan cursor on the way out.
    2184             :  */
    2185             : STATIC int
    2186    28867970 : xfs_rtfile_convert_one(
    2187             :         struct xfs_inode        *ip,
    2188             :         xfs_fileoff_t           *offp,
    2189             :         xfs_fileoff_t           endoff)
    2190             : {
    2191    28867970 :         struct xfs_bmbt_irec    irec;
    2192    28867970 :         struct xfs_mount        *mp = ip->i_mount;
    2193    28867970 :         struct xfs_trans        *tp;
    2194    28867970 :         unsigned int            resblks;
    2195    28867970 :         int                     nmap;
    2196    28867970 :         int                     error;
    2197             : 
    2198    28867970 :         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 1);
    2199    28867970 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
    2200    28869534 :         if (error)
    2201             :                 return error;
    2202             : 
    2203    28869711 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
    2204    28868016 :         xfs_trans_ijoin(tp, ip, 0);
    2205             : 
    2206             :         /*
    2207             :          * Read the mapping.  If we find an unwritten extent that isn't aligned
    2208             :          * to an rt extent boundary...
    2209             :          */
    2210    58750410 : retry:
    2211    58750410 :         nmap = 1;
    2212    58750410 :         error = xfs_bmapi_read(ip, *offp, endoff - *offp, &irec, &nmap, 0);
    2213    58753179 :         if (error)
    2214           1 :                 goto out_cancel;
    2215    58753178 :         ASSERT(nmap == 1);
    2216    58753178 :         ASSERT(irec.br_startoff == *offp);
    2217    58753178 :         if (!xfs_rtfile_want_conversion(mp, &irec)) {
    2218    58473638 :                 *offp = irec.br_startoff + irec.br_blockcount;
    2219    58473638 :                 if (*offp >= endoff)
    2220    28590482 :                         goto out_cancel;
    2221    29883156 :                 goto retry;
    2222             :         }
    2223             : 
    2224             :         /*
    2225             :          * ...make sure this partially unwritten rt extent gets converted to a
    2226             :          * zeroed written extent that we can remap.
    2227             :          */
    2228      279557 :         nmap = 1;
    2229      279557 :         error = xfs_bmapi_write(tp, ip, irec.br_startoff, irec.br_blockcount,
    2230             :                         XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO, 0, &irec, &nmap);
    2231      279555 :         if (error)
    2232           0 :                 goto out_cancel;
    2233      279555 :         ASSERT(nmap == 1);
    2234      279555 :         if (irec.br_state != XFS_EXT_NORM) {
    2235           0 :                 ASSERT(0);
    2236           0 :                 error = -EIO;
    2237           0 :                 goto out_cancel;
    2238             :         }
    2239      279555 :         error = xfs_trans_commit(tp);
    2240      279557 :         if (error)
    2241           0 :                 goto out_unlock;
    2242             : 
    2243      279557 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    2244      279557 :         *offp = irec.br_startoff + irec.br_blockcount;
    2245      279557 :         return 0;
    2246             : 
    2247    28590483 : out_cancel:
    2248    28590483 :         xfs_trans_cancel(tp);
    2249    28589883 : out_unlock:
    2250    28589883 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
    2251    28589883 :         return error;
    2252             : }
    2253             : 
    2254             : /*
    2255             :  * For all realtime extents backing the given range of a file, search for
    2256             :  * unwritten mappings that do not cover a full rt extent and convert them
    2257             :  * to zeroed written mappings.  The goal is to end up with one mapping per rt
    2258             :  * extent so that we can perform a remapping operation.  Callers must ensure
    2259             :  * that there are no dirty pages in the given range.
    2260             :  */
    2261             : int
    2262    28674724 : xfs_rtfile_convert_unwritten(
    2263             :         struct xfs_inode        *ip,
    2264             :         loff_t                  pos,
    2265             :         uint64_t                len)
    2266             : {
    2267    28674724 :         struct xfs_mount        *mp = ip->i_mount;
    2268    28674724 :         xfs_fileoff_t           off;
    2269    28674724 :         xfs_fileoff_t           endoff;
    2270    28674724 :         int                     error;
    2271             : 
    2272    28674724 :         if (mp->m_sb.sb_rextsize == 1)
    2273             :                 return 0;
    2274             : 
    2275    28674724 :         off = xfs_rtb_rounddown_rtx(mp, XFS_B_TO_FSBT(mp, pos));
    2276    28673810 :         endoff = xfs_rtb_roundup_rtx(mp, XFS_B_TO_FSB(mp, pos + len));
    2277             : 
    2278    28673500 :         trace_xfs_rtfile_convert_unwritten(ip, pos, len);
    2279             : 
    2280    57542028 :         while (off < endoff) {
    2281    28866208 :                 if (fatal_signal_pending(current))
    2282             :                         return -EINTR;
    2283             : 
    2284    28867788 :                 error = xfs_rtfile_convert_one(ip, &off, endoff);
    2285    28868532 :                 if (error)
    2286           4 :                         return error;
    2287             :         }
    2288             : 
    2289             :         return 0;
    2290             : }
    2291             : 
    2292             : /*
    2293             :  * Find the next free realtime extent starting at @rtx and going no higher than
    2294             :  * @end_rtx.  Set @rtx and @len_rtx to whatever free extents we find, or to
    2295             :  * @end_rtx if we find no space.
    2296             :  */
    2297             : int
    2298         520 : xfs_rtalloc_find_freesp(
    2299             :         struct xfs_trans        *tp,
    2300             :         xfs_rtxnum_t            *rtx,
    2301             :         xfs_rtxnum_t            end_rtx,
    2302             :         xfs_rtxlen_t            *len_rtx)
    2303             : {
    2304         520 :         struct xfs_mount        *mp = tp->t_mountp;
    2305         520 :         unsigned int            max_rt_extlen;
    2306         520 :         int                     error;
    2307             : 
    2308         520 :         trace_xfs_rtalloc_find_freesp(mp, *rtx, end_rtx - *rtx);
    2309             : 
    2310         520 :         max_rt_extlen = xfs_rtb_to_rtxt(mp, XFS_MAX_BMBT_EXTLEN);
    2311             : 
    2312         540 :         while (*rtx < end_rtx) {
    2313         537 :                 xfs_rtblock_t   range_end_rtx;
    2314         537 :                 int             is_free = 0;
    2315             : 
    2316             :                 /* Is the first block in the range free? */
    2317         537 :                 error = xfs_rtcheck_range(mp, tp, *rtx, 1, 1, &range_end_rtx,
    2318             :                                 &is_free);
    2319         537 :                 if (error)
    2320         517 :                         return error;
    2321             : 
    2322             :                 /* Free or not, how many more rtx have the same status? */
    2323         537 :                 error = xfs_rtfind_forw(mp, tp, *rtx, end_rtx,
    2324             :                                 &range_end_rtx);
    2325         537 :                 if (error)
    2326           0 :                         return error;
    2327             : 
    2328         537 :                 if (is_free) {
    2329         517 :                         trace_xfs_rtalloc_find_freesp_done(mp, *rtx, *len_rtx);
    2330         517 :                         *len_rtx = min_t(xfs_rtblock_t, max_rt_extlen,
    2331             :                                          range_end_rtx - *rtx + 1);
    2332         517 :                         return 0;
    2333             :                 }
    2334             : 
    2335          20 :                 *rtx = range_end_rtx + 1;
    2336             :         }
    2337             : 
    2338             :         return 0;
    2339             : }

Generated by: LCOV version 1.14